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 #ifdef CONFIG_LOCK_STAT
128 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], lock_stats
);
130 static int lock_contention_point(struct lock_class
*class, unsigned long ip
)
134 for (i
= 0; i
< ARRAY_SIZE(class->contention_point
); i
++) {
135 if (class->contention_point
[i
] == 0) {
136 class->contention_point
[i
] = ip
;
139 if (class->contention_point
[i
] == ip
)
146 static void lock_time_inc(struct lock_time
*lt
, s64 time
)
151 if (time
< lt
->min
|| !lt
->min
)
158 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
160 dst
->min
+= src
->min
;
161 dst
->max
+= src
->max
;
162 dst
->total
+= src
->total
;
166 struct lock_class_stats
lock_stats(struct lock_class
*class)
168 struct lock_class_stats stats
;
171 memset(&stats
, 0, sizeof(struct lock_class_stats
));
172 for_each_possible_cpu(cpu
) {
173 struct lock_class_stats
*pcs
=
174 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
176 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
177 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
179 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
180 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
182 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
183 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
185 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
186 stats
.bounces
[i
] += pcs
->bounces
[i
];
192 void clear_lock_stats(struct lock_class
*class)
196 for_each_possible_cpu(cpu
) {
197 struct lock_class_stats
*cpu_stats
=
198 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
200 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
202 memset(class->contention_point
, 0, sizeof(class->contention_point
));
205 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
207 return &get_cpu_var(lock_stats
)[class - lock_classes
];
210 static void put_lock_stats(struct lock_class_stats
*stats
)
212 put_cpu_var(lock_stats
);
215 static void lock_release_holdtime(struct held_lock
*hlock
)
217 struct lock_class_stats
*stats
;
223 holdtime
= sched_clock() - hlock
->holdtime_stamp
;
225 stats
= get_lock_stats(hlock
->class);
227 lock_time_inc(&stats
->read_holdtime
, holdtime
);
229 lock_time_inc(&stats
->write_holdtime
, holdtime
);
230 put_lock_stats(stats
);
233 static inline void lock_release_holdtime(struct held_lock
*hlock
)
239 * We keep a global list of all lock classes. The list only grows,
240 * never shrinks. The list is only accessed with the lockdep
241 * spinlock lock held.
243 LIST_HEAD(all_lock_classes
);
246 * The lockdep classes are in a hash-table as well, for fast lookup:
248 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
249 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
250 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
251 #define classhashentry(key) (classhash_table + __classhashfn((key)))
253 static struct list_head classhash_table
[CLASSHASH_SIZE
];
256 * We put the lock dependency chains into a hash-table as well, to cache
259 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
260 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
261 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
262 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
264 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
267 * The hash key of the lock dependency chains is a hash itself too:
268 * it's a hash of all locks taken up to that lock, including that lock.
269 * It's a 64-bit hash, because it's important for the keys to be
272 #define iterate_chain_key(key1, key2) \
273 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
274 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
277 void lockdep_off(void)
279 current
->lockdep_recursion
++;
282 EXPORT_SYMBOL(lockdep_off
);
284 void lockdep_on(void)
286 current
->lockdep_recursion
--;
289 EXPORT_SYMBOL(lockdep_on
);
292 * Debugging switches:
296 #define VERY_VERBOSE 0
299 # define HARDIRQ_VERBOSE 1
300 # define SOFTIRQ_VERBOSE 1
302 # define HARDIRQ_VERBOSE 0
303 # define SOFTIRQ_VERBOSE 0
306 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
308 * Quick filtering for interesting events:
310 static int class_filter(struct lock_class
*class)
314 if (class->name_version
== 1 &&
315 !strcmp(class->name
, "lockname"))
317 if (class->name_version
== 1 &&
318 !strcmp(class->name
, "&struct->lockfield"))
321 /* Filter everything else. 1 would be to allow everything else */
326 static int verbose(struct lock_class
*class)
329 return class_filter(class);
335 * Stack-trace: tightly packed array of stack backtrace
336 * addresses. Protected by the graph_lock.
338 unsigned long nr_stack_trace_entries
;
339 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
341 static int save_trace(struct stack_trace
*trace
)
343 trace
->nr_entries
= 0;
344 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
345 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
349 save_stack_trace(trace
);
351 trace
->max_entries
= trace
->nr_entries
;
353 nr_stack_trace_entries
+= trace
->nr_entries
;
355 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
356 if (!debug_locks_off_graph_unlock())
359 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
360 printk("turning off the locking correctness validator.\n");
369 unsigned int nr_hardirq_chains
;
370 unsigned int nr_softirq_chains
;
371 unsigned int nr_process_chains
;
372 unsigned int max_lockdep_depth
;
373 unsigned int max_recursion_depth
;
375 static unsigned int lockdep_dependency_gen_id
;
377 static bool lockdep_dependency_visit(struct lock_class
*source
,
381 lockdep_dependency_gen_id
++;
382 if (source
->dep_gen_id
== lockdep_dependency_gen_id
)
384 source
->dep_gen_id
= lockdep_dependency_gen_id
;
388 #ifdef CONFIG_DEBUG_LOCKDEP
390 * We cannot printk in early bootup code. Not even early_printk()
391 * might work. So we mark any initialization errors and printk
392 * about it later on, in lockdep_info().
394 static int lockdep_init_error
;
395 static unsigned long lockdep_init_trace_data
[20];
396 static struct stack_trace lockdep_init_trace
= {
397 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
398 .entries
= lockdep_init_trace_data
,
402 * Various lockdep statistics:
404 atomic_t chain_lookup_hits
;
405 atomic_t chain_lookup_misses
;
406 atomic_t hardirqs_on_events
;
407 atomic_t hardirqs_off_events
;
408 atomic_t redundant_hardirqs_on
;
409 atomic_t redundant_hardirqs_off
;
410 atomic_t softirqs_on_events
;
411 atomic_t softirqs_off_events
;
412 atomic_t redundant_softirqs_on
;
413 atomic_t redundant_softirqs_off
;
414 atomic_t nr_unused_locks
;
415 atomic_t nr_cyclic_checks
;
416 atomic_t nr_cyclic_check_recursions
;
417 atomic_t nr_find_usage_forwards_checks
;
418 atomic_t nr_find_usage_forwards_recursions
;
419 atomic_t nr_find_usage_backwards_checks
;
420 atomic_t nr_find_usage_backwards_recursions
;
421 # define debug_atomic_inc(ptr) atomic_inc(ptr)
422 # define debug_atomic_dec(ptr) atomic_dec(ptr)
423 # define debug_atomic_read(ptr) atomic_read(ptr)
425 # define debug_atomic_inc(ptr) do { } while (0)
426 # define debug_atomic_dec(ptr) do { } while (0)
427 # define debug_atomic_read(ptr) 0
434 static const char *usage_str
[] =
436 [LOCK_USED
] = "initial-use ",
437 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
438 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
439 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
440 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
441 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
442 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
443 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
444 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
447 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
449 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
453 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
455 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
457 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
460 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
463 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
466 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
469 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
471 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
473 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
477 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
479 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
481 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
486 static void print_lock_name(struct lock_class
*class)
488 char str
[KSYM_NAME_LEN
], c1
, c2
, c3
, c4
;
491 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
495 name
= __get_key_name(class->key
, str
);
496 printk(" (%s", name
);
498 printk(" (%s", name
);
499 if (class->name_version
> 1)
500 printk("#%d", class->name_version
);
502 printk("/%d", class->subclass
);
504 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
507 static void print_lockdep_cache(struct lockdep_map
*lock
)
510 char str
[KSYM_NAME_LEN
];
514 name
= __get_key_name(lock
->key
->subkeys
, str
);
519 static void print_lock(struct held_lock
*hlock
)
521 print_lock_name(hlock
->class);
523 print_ip_sym(hlock
->acquire_ip
);
526 static void lockdep_print_held_locks(struct task_struct
*curr
)
528 int i
, depth
= curr
->lockdep_depth
;
531 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
534 printk("%d lock%s held by %s/%d:\n",
535 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
537 for (i
= 0; i
< depth
; i
++) {
539 print_lock(curr
->held_locks
+ i
);
543 static void print_lock_class_header(struct lock_class
*class, int depth
)
547 printk("%*s->", depth
, "");
548 print_lock_name(class);
549 printk(" ops: %lu", class->ops
);
552 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
553 if (class->usage_mask
& (1 << bit
)) {
556 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
557 len
+= printk(" at:\n");
558 print_stack_trace(class->usage_traces
+ bit
, len
);
561 printk("%*s }\n", depth
, "");
563 printk("%*s ... key at: ",depth
,"");
564 print_ip_sym((unsigned long)class->key
);
568 * printk all lock dependencies starting at <entry>:
570 static void print_lock_dependencies(struct lock_class
*class, int depth
)
572 struct lock_list
*entry
;
574 if (lockdep_dependency_visit(class, depth
))
577 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
580 print_lock_class_header(class, depth
);
582 list_for_each_entry(entry
, &class->locks_after
, entry
) {
583 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
586 print_lock_dependencies(entry
->class, depth
+ 1);
588 printk("%*s ... acquired at:\n",depth
,"");
589 print_stack_trace(&entry
->trace
, 2);
594 static void print_kernel_version(void)
596 printk("%s %.*s\n", init_utsname()->release
,
597 (int)strcspn(init_utsname()->version
, " "),
598 init_utsname()->version
);
601 static int very_verbose(struct lock_class
*class)
604 return class_filter(class);
610 * Is this the address of a static object:
612 static int static_obj(void *obj
)
614 unsigned long start
= (unsigned long) &_stext
,
615 end
= (unsigned long) &_end
,
616 addr
= (unsigned long) obj
;
624 if ((addr
>= start
) && (addr
< end
))
631 for_each_possible_cpu(i
) {
632 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
633 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
636 if ((addr
>= start
) && (addr
< end
))
644 return is_module_address(addr
);
648 * To make lock name printouts unique, we calculate a unique
649 * class->name_version generation counter:
651 static int count_matching_names(struct lock_class
*new_class
)
653 struct lock_class
*class;
656 if (!new_class
->name
)
659 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
660 if (new_class
->key
- new_class
->subclass
== class->key
)
661 return class->name_version
;
662 if (class->name
&& !strcmp(class->name
, new_class
->name
))
663 count
= max(count
, class->name_version
);
670 * Register a lock's class in the hash-table, if the class is not present
671 * yet. Otherwise we look it up. We cache the result in the lock object
672 * itself, so actual lookup of the hash should be once per lock object.
674 static inline struct lock_class
*
675 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
677 struct lockdep_subclass_key
*key
;
678 struct list_head
*hash_head
;
679 struct lock_class
*class;
681 #ifdef CONFIG_DEBUG_LOCKDEP
683 * If the architecture calls into lockdep before initializing
684 * the hashes then we'll warn about it later. (we cannot printk
687 if (unlikely(!lockdep_initialized
)) {
689 lockdep_init_error
= 1;
690 save_stack_trace(&lockdep_init_trace
);
695 * Static locks do not have their class-keys yet - for them the key
696 * is the lock object itself:
698 if (unlikely(!lock
->key
))
699 lock
->key
= (void *)lock
;
702 * NOTE: the class-key must be unique. For dynamic locks, a static
703 * lock_class_key variable is passed in through the mutex_init()
704 * (or spin_lock_init()) call - which acts as the key. For static
705 * locks we use the lock object itself as the key.
707 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
708 sizeof(struct lockdep_map
));
710 key
= lock
->key
->subkeys
+ subclass
;
712 hash_head
= classhashentry(key
);
715 * We can walk the hash lockfree, because the hash only
716 * grows, and we are careful when adding entries to the end:
718 list_for_each_entry(class, hash_head
, hash_entry
) {
719 if (class->key
== key
) {
720 WARN_ON_ONCE(class->name
!= lock
->name
);
729 * Register a lock's class in the hash-table, if the class is not present
730 * yet. Otherwise we look it up. We cache the result in the lock object
731 * itself, so actual lookup of the hash should be once per lock object.
733 static inline struct lock_class
*
734 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
736 struct lockdep_subclass_key
*key
;
737 struct list_head
*hash_head
;
738 struct lock_class
*class;
741 class = look_up_lock_class(lock
, subclass
);
746 * Debug-check: all keys must be persistent!
748 if (!static_obj(lock
->key
)) {
750 printk("INFO: trying to register non-static key.\n");
751 printk("the code is fine but needs lockdep annotation.\n");
752 printk("turning off the locking correctness validator.\n");
758 key
= lock
->key
->subkeys
+ subclass
;
759 hash_head
= classhashentry(key
);
761 raw_local_irq_save(flags
);
763 raw_local_irq_restore(flags
);
767 * We have to do the hash-walk again, to avoid races
770 list_for_each_entry(class, hash_head
, hash_entry
)
771 if (class->key
== key
)
774 * Allocate a new key from the static array, and add it to
777 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
778 if (!debug_locks_off_graph_unlock()) {
779 raw_local_irq_restore(flags
);
782 raw_local_irq_restore(flags
);
784 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
785 printk("turning off the locking correctness validator.\n");
788 class = lock_classes
+ nr_lock_classes
++;
789 debug_atomic_inc(&nr_unused_locks
);
791 class->name
= lock
->name
;
792 class->subclass
= subclass
;
793 INIT_LIST_HEAD(&class->lock_entry
);
794 INIT_LIST_HEAD(&class->locks_before
);
795 INIT_LIST_HEAD(&class->locks_after
);
796 class->name_version
= count_matching_names(class);
798 * We use RCU's safe list-add method to make
799 * parallel walking of the hash-list safe:
801 list_add_tail_rcu(&class->hash_entry
, hash_head
);
803 * Add it to the global list of classes:
805 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
807 if (verbose(class)) {
809 raw_local_irq_restore(flags
);
811 printk("\nnew class %p: %s", class->key
, class->name
);
812 if (class->name_version
> 1)
813 printk("#%d", class->name_version
);
817 raw_local_irq_save(flags
);
819 raw_local_irq_restore(flags
);
825 raw_local_irq_restore(flags
);
827 if (!subclass
|| force
)
828 lock
->class_cache
= class;
830 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
836 #ifdef CONFIG_PROVE_LOCKING
838 * Allocate a lockdep entry. (assumes the graph_lock held, returns
839 * with NULL on failure)
841 static struct lock_list
*alloc_list_entry(void)
843 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
844 if (!debug_locks_off_graph_unlock())
847 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
848 printk("turning off the locking correctness validator.\n");
851 return list_entries
+ nr_list_entries
++;
855 * Add a new dependency to the head of the list:
857 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
858 struct list_head
*head
, unsigned long ip
, int distance
)
860 struct lock_list
*entry
;
862 * Lock not present yet - get a new dependency struct and
863 * add it to the list:
865 entry
= alloc_list_entry();
870 entry
->distance
= distance
;
871 if (!save_trace(&entry
->trace
))
875 * Since we never remove from the dependency list, the list can
876 * be walked lockless by other CPUs, it's only allocation
877 * that must be protected by the spinlock. But this also means
878 * we must make new entries visible only once writes to the
879 * entry become visible - hence the RCU op:
881 list_add_tail_rcu(&entry
->entry
, head
);
887 * Recursive, forwards-direction lock-dependency checking, used for
888 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
891 * (to keep the stackframe of the recursive functions small we
892 * use these global variables, and we also mark various helper
893 * functions as noinline.)
895 static struct held_lock
*check_source
, *check_target
;
898 * Print a dependency chain entry (this is only done when a deadlock
899 * has been detected):
902 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
904 if (debug_locks_silent
)
906 printk("\n-> #%u", depth
);
907 print_lock_name(target
->class);
909 print_stack_trace(&target
->trace
, 6);
915 * When a circular dependency is detected, print the
919 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
921 struct task_struct
*curr
= current
;
923 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
926 printk("\n=======================================================\n");
927 printk( "[ INFO: possible circular locking dependency detected ]\n");
928 print_kernel_version();
929 printk( "-------------------------------------------------------\n");
930 printk("%s/%d is trying to acquire lock:\n",
931 curr
->comm
, task_pid_nr(curr
));
932 print_lock(check_source
);
933 printk("\nbut task is already holding lock:\n");
934 print_lock(check_target
);
935 printk("\nwhich lock already depends on the new lock.\n\n");
936 printk("\nthe existing dependency chain (in reverse order) is:\n");
938 print_circular_bug_entry(entry
, depth
);
943 static noinline
int print_circular_bug_tail(void)
945 struct task_struct
*curr
= current
;
946 struct lock_list
this;
948 if (debug_locks_silent
)
951 this.class = check_source
->class;
952 if (!save_trace(&this.trace
))
955 print_circular_bug_entry(&this, 0);
957 printk("\nother info that might help us debug this:\n\n");
958 lockdep_print_held_locks(curr
);
960 printk("\nstack backtrace:\n");
966 #define RECURSION_LIMIT 40
968 static int noinline
print_infinite_recursion_bug(void)
970 if (!debug_locks_off_graph_unlock())
978 unsigned long __lockdep_count_forward_deps(struct lock_class
*class,
981 struct lock_list
*entry
;
982 unsigned long ret
= 1;
984 if (lockdep_dependency_visit(class, depth
))
988 * Recurse this class's dependency list:
990 list_for_each_entry(entry
, &class->locks_after
, entry
)
991 ret
+= __lockdep_count_forward_deps(entry
->class, depth
+ 1);
996 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
998 unsigned long ret
, flags
;
1000 local_irq_save(flags
);
1001 __raw_spin_lock(&lockdep_lock
);
1002 ret
= __lockdep_count_forward_deps(class, 0);
1003 __raw_spin_unlock(&lockdep_lock
);
1004 local_irq_restore(flags
);
1009 unsigned long __lockdep_count_backward_deps(struct lock_class
*class,
1012 struct lock_list
*entry
;
1013 unsigned long ret
= 1;
1015 if (lockdep_dependency_visit(class, depth
))
1018 * Recurse this class's dependency list:
1020 list_for_each_entry(entry
, &class->locks_before
, entry
)
1021 ret
+= __lockdep_count_backward_deps(entry
->class, depth
+ 1);
1026 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1028 unsigned long ret
, flags
;
1030 local_irq_save(flags
);
1031 __raw_spin_lock(&lockdep_lock
);
1032 ret
= __lockdep_count_backward_deps(class, 0);
1033 __raw_spin_unlock(&lockdep_lock
);
1034 local_irq_restore(flags
);
1040 * Prove that the dependency graph starting at <entry> can not
1041 * lead to <target>. Print an error and return 0 if it does.
1044 check_noncircular(struct lock_class
*source
, unsigned int depth
)
1046 struct lock_list
*entry
;
1048 if (lockdep_dependency_visit(source
, depth
))
1051 debug_atomic_inc(&nr_cyclic_check_recursions
);
1052 if (depth
> max_recursion_depth
)
1053 max_recursion_depth
= depth
;
1054 if (depth
>= RECURSION_LIMIT
)
1055 return print_infinite_recursion_bug();
1057 * Check this lock's dependency list:
1059 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
1060 if (entry
->class == check_target
->class)
1061 return print_circular_bug_header(entry
, depth
+1);
1062 debug_atomic_inc(&nr_cyclic_checks
);
1063 if (!check_noncircular(entry
->class, depth
+1))
1064 return print_circular_bug_entry(entry
, depth
+1);
1069 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1071 * Forwards and backwards subgraph searching, for the purposes of
1072 * proving that two subgraphs can be connected by a new dependency
1073 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1075 static enum lock_usage_bit find_usage_bit
;
1076 static struct lock_class
*forwards_match
, *backwards_match
;
1079 * Find a node in the forwards-direction dependency sub-graph starting
1080 * at <source> that matches <find_usage_bit>.
1082 * Return 2 if such a node exists in the subgraph, and put that node
1083 * into <forwards_match>.
1085 * Return 1 otherwise and keep <forwards_match> unchanged.
1086 * Return 0 on error.
1089 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
1091 struct lock_list
*entry
;
1094 if (lockdep_dependency_visit(source
, depth
))
1097 if (depth
> max_recursion_depth
)
1098 max_recursion_depth
= depth
;
1099 if (depth
>= RECURSION_LIMIT
)
1100 return print_infinite_recursion_bug();
1102 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1103 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1104 forwards_match
= source
;
1109 * Check this lock's dependency list:
1111 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
1112 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
1113 ret
= find_usage_forwards(entry
->class, depth
+1);
1114 if (ret
== 2 || ret
== 0)
1121 * Find a node in the backwards-direction dependency sub-graph starting
1122 * at <source> that matches <find_usage_bit>.
1124 * Return 2 if such a node exists in the subgraph, and put that node
1125 * into <backwards_match>.
1127 * Return 1 otherwise and keep <backwards_match> unchanged.
1128 * Return 0 on error.
1131 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
1133 struct lock_list
*entry
;
1136 if (lockdep_dependency_visit(source
, depth
))
1139 if (!__raw_spin_is_locked(&lockdep_lock
))
1140 return DEBUG_LOCKS_WARN_ON(1);
1142 if (depth
> max_recursion_depth
)
1143 max_recursion_depth
= depth
;
1144 if (depth
>= RECURSION_LIMIT
)
1145 return print_infinite_recursion_bug();
1147 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1148 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1149 backwards_match
= source
;
1154 * Check this lock's dependency list:
1156 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
1157 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
1158 ret
= find_usage_backwards(entry
->class, depth
+1);
1159 if (ret
== 2 || ret
== 0)
1166 print_bad_irq_dependency(struct task_struct
*curr
,
1167 struct held_lock
*prev
,
1168 struct held_lock
*next
,
1169 enum lock_usage_bit bit1
,
1170 enum lock_usage_bit bit2
,
1171 const char *irqclass
)
1173 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1176 printk("\n======================================================\n");
1177 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1178 irqclass
, irqclass
);
1179 print_kernel_version();
1180 printk( "------------------------------------------------------\n");
1181 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1182 curr
->comm
, task_pid_nr(curr
),
1183 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1184 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1185 curr
->hardirqs_enabled
,
1186 curr
->softirqs_enabled
);
1189 printk("\nand this task is already holding:\n");
1191 printk("which would create a new lock dependency:\n");
1192 print_lock_name(prev
->class);
1194 print_lock_name(next
->class);
1197 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1199 print_lock_name(backwards_match
);
1200 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1202 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
1204 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1205 print_lock_name(forwards_match
);
1206 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1209 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
1211 printk("\nother info that might help us debug this:\n\n");
1212 lockdep_print_held_locks(curr
);
1214 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
1215 print_lock_dependencies(backwards_match
, 0);
1217 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
1218 print_lock_dependencies(forwards_match
, 0);
1220 printk("\nstack backtrace:\n");
1227 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1228 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1229 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1233 find_usage_bit
= bit_backwards
;
1234 /* fills in <backwards_match> */
1235 ret
= find_usage_backwards(prev
->class, 0);
1236 if (!ret
|| ret
== 1)
1239 find_usage_bit
= bit_forwards
;
1240 ret
= find_usage_forwards(next
->class, 0);
1241 if (!ret
|| ret
== 1)
1244 return print_bad_irq_dependency(curr
, prev
, next
,
1245 bit_backwards
, bit_forwards
, irqclass
);
1249 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1250 struct held_lock
*next
)
1253 * Prove that the new dependency does not connect a hardirq-safe
1254 * lock with a hardirq-unsafe lock - to achieve this we search
1255 * the backwards-subgraph starting at <prev>, and the
1256 * forwards-subgraph starting at <next>:
1258 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
1259 LOCK_ENABLED_HARDIRQS
, "hard"))
1263 * Prove that the new dependency does not connect a hardirq-safe-read
1264 * lock with a hardirq-unsafe lock - to achieve this we search
1265 * the backwards-subgraph starting at <prev>, and the
1266 * forwards-subgraph starting at <next>:
1268 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
1269 LOCK_ENABLED_HARDIRQS
, "hard-read"))
1273 * Prove that the new dependency does not connect a softirq-safe
1274 * lock with a softirq-unsafe lock - to achieve this we search
1275 * the backwards-subgraph starting at <prev>, and the
1276 * forwards-subgraph starting at <next>:
1278 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
1279 LOCK_ENABLED_SOFTIRQS
, "soft"))
1282 * Prove that the new dependency does not connect a softirq-safe-read
1283 * lock with a softirq-unsafe lock - to achieve this we search
1284 * the backwards-subgraph starting at <prev>, and the
1285 * forwards-subgraph starting at <next>:
1287 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
1288 LOCK_ENABLED_SOFTIRQS
, "soft"))
1294 static void inc_chains(void)
1296 if (current
->hardirq_context
)
1297 nr_hardirq_chains
++;
1299 if (current
->softirq_context
)
1300 nr_softirq_chains
++;
1302 nr_process_chains
++;
1309 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1310 struct held_lock
*next
)
1315 static inline void inc_chains(void)
1317 nr_process_chains
++;
1323 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1324 struct held_lock
*next
)
1326 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1329 printk("\n=============================================\n");
1330 printk( "[ INFO: possible recursive locking detected ]\n");
1331 print_kernel_version();
1332 printk( "---------------------------------------------\n");
1333 printk("%s/%d is trying to acquire lock:\n",
1334 curr
->comm
, task_pid_nr(curr
));
1336 printk("\nbut task is already holding lock:\n");
1339 printk("\nother info that might help us debug this:\n");
1340 lockdep_print_held_locks(curr
);
1342 printk("\nstack backtrace:\n");
1349 * Check whether we are holding such a class already.
1351 * (Note that this has to be done separately, because the graph cannot
1352 * detect such classes of deadlocks.)
1354 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1357 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1358 struct lockdep_map
*next_instance
, int read
)
1360 struct held_lock
*prev
;
1363 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1364 prev
= curr
->held_locks
+ i
;
1365 if (prev
->class != next
->class)
1368 * Allow read-after-read recursion of the same
1369 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1371 if ((read
== 2) && prev
->read
)
1373 return print_deadlock_bug(curr
, prev
, next
);
1379 * There was a chain-cache miss, and we are about to add a new dependency
1380 * to a previous lock. We recursively validate the following rules:
1382 * - would the adding of the <prev> -> <next> dependency create a
1383 * circular dependency in the graph? [== circular deadlock]
1385 * - does the new prev->next dependency connect any hardirq-safe lock
1386 * (in the full backwards-subgraph starting at <prev>) with any
1387 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1388 * <next>)? [== illegal lock inversion with hardirq contexts]
1390 * - does the new prev->next dependency connect any softirq-safe lock
1391 * (in the full backwards-subgraph starting at <prev>) with any
1392 * softirq-unsafe lock (in the full forwards-subgraph starting at
1393 * <next>)? [== illegal lock inversion with softirq contexts]
1395 * any of these scenarios could lead to a deadlock.
1397 * Then if all the validations pass, we add the forwards and backwards
1401 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1402 struct held_lock
*next
, int distance
)
1404 struct lock_list
*entry
;
1408 * Prove that the new <prev> -> <next> dependency would not
1409 * create a circular dependency in the graph. (We do this by
1410 * forward-recursing into the graph starting at <next>, and
1411 * checking whether we can reach <prev>.)
1413 * We are using global variables to control the recursion, to
1414 * keep the stackframe size of the recursive functions low:
1416 check_source
= next
;
1417 check_target
= prev
;
1418 if (!(check_noncircular(next
->class, 0)))
1419 return print_circular_bug_tail();
1421 if (!check_prev_add_irq(curr
, prev
, next
))
1425 * For recursive read-locks we do all the dependency checks,
1426 * but we dont store read-triggered dependencies (only
1427 * write-triggered dependencies). This ensures that only the
1428 * write-side dependencies matter, and that if for example a
1429 * write-lock never takes any other locks, then the reads are
1430 * equivalent to a NOP.
1432 if (next
->read
== 2 || prev
->read
== 2)
1435 * Is the <prev> -> <next> dependency already present?
1437 * (this may occur even though this is a new chain: consider
1438 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1439 * chains - the second one will be new, but L1 already has
1440 * L2 added to its dependency list, due to the first chain.)
1442 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
1443 if (entry
->class == next
->class) {
1445 entry
->distance
= 1;
1451 * Ok, all validations passed, add the new lock
1452 * to the previous lock's dependency list:
1454 ret
= add_lock_to_list(prev
->class, next
->class,
1455 &prev
->class->locks_after
, next
->acquire_ip
, distance
);
1460 ret
= add_lock_to_list(next
->class, prev
->class,
1461 &next
->class->locks_before
, next
->acquire_ip
, distance
);
1466 * Debugging printouts:
1468 if (verbose(prev
->class) || verbose(next
->class)) {
1470 printk("\n new dependency: ");
1471 print_lock_name(prev
->class);
1473 print_lock_name(next
->class);
1476 return graph_lock();
1482 * Add the dependency to all directly-previous locks that are 'relevant'.
1483 * The ones that are relevant are (in increasing distance from curr):
1484 * all consecutive trylock entries and the final non-trylock entry - or
1485 * the end of this context's lock-chain - whichever comes first.
1488 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1490 int depth
= curr
->lockdep_depth
;
1491 struct held_lock
*hlock
;
1496 * Depth must not be zero for a non-head lock:
1501 * At least two relevant locks must exist for this
1504 if (curr
->held_locks
[depth
].irq_context
!=
1505 curr
->held_locks
[depth
-1].irq_context
)
1509 int distance
= curr
->lockdep_depth
- depth
+ 1;
1510 hlock
= curr
->held_locks
+ depth
-1;
1512 * Only non-recursive-read entries get new dependencies
1515 if (hlock
->read
!= 2) {
1516 if (!check_prev_add(curr
, hlock
, next
, distance
))
1519 * Stop after the first non-trylock entry,
1520 * as non-trylock entries have added their
1521 * own direct dependencies already, so this
1522 * lock is connected to them indirectly:
1524 if (!hlock
->trylock
)
1529 * End of lock-stack?
1534 * Stop the search if we cross into another context:
1536 if (curr
->held_locks
[depth
].irq_context
!=
1537 curr
->held_locks
[depth
-1].irq_context
)
1542 if (!debug_locks_off_graph_unlock())
1550 unsigned long nr_lock_chains
;
1551 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1552 int nr_chain_hlocks
;
1553 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1555 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1557 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1561 * Look up a dependency chain. If the key is not present yet then
1562 * add it and return 1 - in this case the new dependency chain is
1563 * validated. If the key is already hashed, return 0.
1564 * (On return with 1 graph_lock is held.)
1566 static inline int lookup_chain_cache(struct task_struct
*curr
,
1567 struct held_lock
*hlock
,
1570 struct lock_class
*class = hlock
->class;
1571 struct list_head
*hash_head
= chainhashentry(chain_key
);
1572 struct lock_chain
*chain
;
1573 struct held_lock
*hlock_curr
, *hlock_next
;
1576 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1579 * We can walk it lock-free, because entries only get added
1582 list_for_each_entry(chain
, hash_head
, entry
) {
1583 if (chain
->chain_key
== chain_key
) {
1585 debug_atomic_inc(&chain_lookup_hits
);
1586 if (very_verbose(class))
1587 printk("\nhash chain already cached, key: "
1588 "%016Lx tail class: [%p] %s\n",
1589 (unsigned long long)chain_key
,
1590 class->key
, class->name
);
1594 if (very_verbose(class))
1595 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1596 (unsigned long long)chain_key
, class->key
, class->name
);
1598 * Allocate a new chain entry from the static array, and add
1604 * We have to walk the chain again locked - to avoid duplicates:
1606 list_for_each_entry(chain
, hash_head
, entry
) {
1607 if (chain
->chain_key
== chain_key
) {
1612 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1613 if (!debug_locks_off_graph_unlock())
1616 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1617 printk("turning off the locking correctness validator.\n");
1620 chain
= lock_chains
+ nr_lock_chains
++;
1621 chain
->chain_key
= chain_key
;
1622 chain
->irq_context
= hlock
->irq_context
;
1623 /* Find the first held_lock of current chain */
1625 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
1626 hlock_curr
= curr
->held_locks
+ i
;
1627 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
1632 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
1633 cn
= nr_chain_hlocks
;
1634 while (cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
) {
1635 n
= cmpxchg(&nr_chain_hlocks
, cn
, cn
+ chain
->depth
);
1640 if (likely(cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
1642 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
1643 int lock_id
= curr
->held_locks
[i
].class - lock_classes
;
1644 chain_hlocks
[chain
->base
+ j
] = lock_id
;
1646 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
1648 list_add_tail_rcu(&chain
->entry
, hash_head
);
1649 debug_atomic_inc(&chain_lookup_misses
);
1655 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1656 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
1659 * Trylock needs to maintain the stack of held locks, but it
1660 * does not add new dependencies, because trylock can be done
1663 * We look up the chain_key and do the O(N^2) check and update of
1664 * the dependencies only if this is a new dependency chain.
1665 * (If lookup_chain_cache() returns with 1 it acquires
1666 * graph_lock for us)
1668 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1669 lookup_chain_cache(curr
, hlock
, chain_key
)) {
1671 * Check whether last held lock:
1673 * - is irq-safe, if this lock is irq-unsafe
1674 * - is softirq-safe, if this lock is hardirq-unsafe
1676 * And check whether the new lock's dependency graph
1677 * could lead back to the previous lock.
1679 * any of these scenarios could lead to a deadlock. If
1682 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1687 * Mark recursive read, as we jump over it when
1688 * building dependencies (just like we jump over
1694 * Add dependency only if this lock is not the head
1695 * of the chain, and if it's not a secondary read-lock:
1697 if (!chain_head
&& ret
!= 2)
1698 if (!check_prevs_add(curr
, hlock
))
1702 /* after lookup_chain_cache(): */
1703 if (unlikely(!debug_locks
))
1709 static inline int validate_chain(struct task_struct
*curr
,
1710 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1711 int chain_head
, u64 chain_key
)
1718 * We are building curr_chain_key incrementally, so double-check
1719 * it from scratch, to make sure that it's done correctly:
1721 static void check_chain_key(struct task_struct
*curr
)
1723 #ifdef CONFIG_DEBUG_LOCKDEP
1724 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1728 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1729 hlock
= curr
->held_locks
+ i
;
1730 if (chain_key
!= hlock
->prev_chain_key
) {
1732 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1733 curr
->lockdep_depth
, i
,
1734 (unsigned long long)chain_key
,
1735 (unsigned long long)hlock
->prev_chain_key
);
1739 id
= hlock
->class - lock_classes
;
1740 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1743 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1744 hlock
->irq_context
))
1746 chain_key
= iterate_chain_key(chain_key
, id
);
1749 if (chain_key
!= curr
->curr_chain_key
) {
1751 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1752 curr
->lockdep_depth
, i
,
1753 (unsigned long long)chain_key
,
1754 (unsigned long long)curr
->curr_chain_key
);
1761 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1762 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1764 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1767 printk("\n=================================\n");
1768 printk( "[ INFO: inconsistent lock state ]\n");
1769 print_kernel_version();
1770 printk( "---------------------------------\n");
1772 printk("inconsistent {%s} -> {%s} usage.\n",
1773 usage_str
[prev_bit
], usage_str
[new_bit
]);
1775 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1776 curr
->comm
, task_pid_nr(curr
),
1777 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1778 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1779 trace_hardirqs_enabled(curr
),
1780 trace_softirqs_enabled(curr
));
1783 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1784 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1786 print_irqtrace_events(curr
);
1787 printk("\nother info that might help us debug this:\n");
1788 lockdep_print_held_locks(curr
);
1790 printk("\nstack backtrace:\n");
1797 * Print out an error if an invalid bit is set:
1800 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1801 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1803 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1804 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1808 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1809 enum lock_usage_bit new_bit
);
1811 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1814 * print irq inversion bug:
1817 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1818 struct held_lock
*this, int forwards
,
1819 const char *irqclass
)
1821 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1824 printk("\n=========================================================\n");
1825 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1826 print_kernel_version();
1827 printk( "---------------------------------------------------------\n");
1828 printk("%s/%d just changed the state of lock:\n",
1829 curr
->comm
, task_pid_nr(curr
));
1832 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1834 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1835 print_lock_name(other
);
1836 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1838 printk("\nother info that might help us debug this:\n");
1839 lockdep_print_held_locks(curr
);
1841 printk("\nthe first lock's dependencies:\n");
1842 print_lock_dependencies(this->class, 0);
1844 printk("\nthe second lock's dependencies:\n");
1845 print_lock_dependencies(other
, 0);
1847 printk("\nstack backtrace:\n");
1854 * Prove that in the forwards-direction subgraph starting at <this>
1855 * there is no lock matching <mask>:
1858 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1859 enum lock_usage_bit bit
, const char *irqclass
)
1863 find_usage_bit
= bit
;
1864 /* fills in <forwards_match> */
1865 ret
= find_usage_forwards(this->class, 0);
1866 if (!ret
|| ret
== 1)
1869 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1873 * Prove that in the backwards-direction subgraph starting at <this>
1874 * there is no lock matching <mask>:
1877 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1878 enum lock_usage_bit bit
, const char *irqclass
)
1882 find_usage_bit
= bit
;
1883 /* fills in <backwards_match> */
1884 ret
= find_usage_backwards(this->class, 0);
1885 if (!ret
|| ret
== 1)
1888 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1891 void print_irqtrace_events(struct task_struct
*curr
)
1893 printk("irq event stamp: %u\n", curr
->irq_events
);
1894 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1895 print_ip_sym(curr
->hardirq_enable_ip
);
1896 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1897 print_ip_sym(curr
->hardirq_disable_ip
);
1898 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1899 print_ip_sym(curr
->softirq_enable_ip
);
1900 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1901 print_ip_sym(curr
->softirq_disable_ip
);
1904 static int hardirq_verbose(struct lock_class
*class)
1907 return class_filter(class);
1912 static int softirq_verbose(struct lock_class
*class)
1915 return class_filter(class);
1920 #define STRICT_READ_CHECKS 1
1922 static int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
1923 enum lock_usage_bit new_bit
)
1928 case LOCK_USED_IN_HARDIRQ
:
1929 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1931 if (!valid_state(curr
, this, new_bit
,
1932 LOCK_ENABLED_HARDIRQS_READ
))
1935 * just marked it hardirq-safe, check that this lock
1936 * took no hardirq-unsafe lock in the past:
1938 if (!check_usage_forwards(curr
, this,
1939 LOCK_ENABLED_HARDIRQS
, "hard"))
1941 #if STRICT_READ_CHECKS
1943 * just marked it hardirq-safe, check that this lock
1944 * took no hardirq-unsafe-read lock in the past:
1946 if (!check_usage_forwards(curr
, this,
1947 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1950 if (hardirq_verbose(this->class))
1953 case LOCK_USED_IN_SOFTIRQ
:
1954 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1956 if (!valid_state(curr
, this, new_bit
,
1957 LOCK_ENABLED_SOFTIRQS_READ
))
1960 * just marked it softirq-safe, check that this lock
1961 * took no softirq-unsafe lock in the past:
1963 if (!check_usage_forwards(curr
, this,
1964 LOCK_ENABLED_SOFTIRQS
, "soft"))
1966 #if STRICT_READ_CHECKS
1968 * just marked it softirq-safe, check that this lock
1969 * took no softirq-unsafe-read lock in the past:
1971 if (!check_usage_forwards(curr
, this,
1972 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1975 if (softirq_verbose(this->class))
1978 case LOCK_USED_IN_HARDIRQ_READ
:
1979 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1982 * just marked it hardirq-read-safe, check that this lock
1983 * took no hardirq-unsafe lock in the past:
1985 if (!check_usage_forwards(curr
, this,
1986 LOCK_ENABLED_HARDIRQS
, "hard"))
1988 if (hardirq_verbose(this->class))
1991 case LOCK_USED_IN_SOFTIRQ_READ
:
1992 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1995 * just marked it softirq-read-safe, check that this lock
1996 * took no softirq-unsafe lock in the past:
1998 if (!check_usage_forwards(curr
, this,
1999 LOCK_ENABLED_SOFTIRQS
, "soft"))
2001 if (softirq_verbose(this->class))
2004 case LOCK_ENABLED_HARDIRQS
:
2005 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
2007 if (!valid_state(curr
, this, new_bit
,
2008 LOCK_USED_IN_HARDIRQ_READ
))
2011 * just marked it hardirq-unsafe, check that no hardirq-safe
2012 * lock in the system ever took it in the past:
2014 if (!check_usage_backwards(curr
, this,
2015 LOCK_USED_IN_HARDIRQ
, "hard"))
2017 #if STRICT_READ_CHECKS
2019 * just marked it hardirq-unsafe, check that no
2020 * hardirq-safe-read lock in the system ever took
2023 if (!check_usage_backwards(curr
, this,
2024 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
2027 if (hardirq_verbose(this->class))
2030 case LOCK_ENABLED_SOFTIRQS
:
2031 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
2033 if (!valid_state(curr
, this, new_bit
,
2034 LOCK_USED_IN_SOFTIRQ_READ
))
2037 * just marked it softirq-unsafe, check that no softirq-safe
2038 * lock in the system ever took it in the past:
2040 if (!check_usage_backwards(curr
, this,
2041 LOCK_USED_IN_SOFTIRQ
, "soft"))
2043 #if STRICT_READ_CHECKS
2045 * just marked it softirq-unsafe, check that no
2046 * softirq-safe-read lock in the system ever took
2049 if (!check_usage_backwards(curr
, this,
2050 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
2053 if (softirq_verbose(this->class))
2056 case LOCK_ENABLED_HARDIRQS_READ
:
2057 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
2059 #if STRICT_READ_CHECKS
2061 * just marked it hardirq-read-unsafe, check that no
2062 * hardirq-safe lock in the system ever took it in the past:
2064 if (!check_usage_backwards(curr
, this,
2065 LOCK_USED_IN_HARDIRQ
, "hard"))
2068 if (hardirq_verbose(this->class))
2071 case LOCK_ENABLED_SOFTIRQS_READ
:
2072 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
2074 #if STRICT_READ_CHECKS
2076 * just marked it softirq-read-unsafe, check that no
2077 * softirq-safe lock in the system ever took it in the past:
2079 if (!check_usage_backwards(curr
, this,
2080 LOCK_USED_IN_SOFTIRQ
, "soft"))
2083 if (softirq_verbose(this->class))
2095 * Mark all held locks with a usage bit:
2098 mark_held_locks(struct task_struct
*curr
, int hardirq
)
2100 enum lock_usage_bit usage_bit
;
2101 struct held_lock
*hlock
;
2104 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2105 hlock
= curr
->held_locks
+ i
;
2109 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
2111 usage_bit
= LOCK_ENABLED_HARDIRQS
;
2114 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
2116 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
2118 if (!mark_lock(curr
, hlock
, usage_bit
))
2126 * Debugging helper: via this flag we know that we are in
2127 * 'early bootup code', and will warn about any invalid irqs-on event:
2129 static int early_boot_irqs_enabled
;
2131 void early_boot_irqs_off(void)
2133 early_boot_irqs_enabled
= 0;
2136 void early_boot_irqs_on(void)
2138 early_boot_irqs_enabled
= 1;
2142 * Hardirqs will be enabled:
2144 void trace_hardirqs_on_caller(unsigned long a0
)
2146 struct task_struct
*curr
= current
;
2149 time_hardirqs_on(CALLER_ADDR0
, a0
);
2151 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2154 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2157 if (unlikely(curr
->hardirqs_enabled
)) {
2158 debug_atomic_inc(&redundant_hardirqs_on
);
2161 /* we'll do an OFF -> ON transition: */
2162 curr
->hardirqs_enabled
= 1;
2163 ip
= (unsigned long) __builtin_return_address(0);
2165 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2167 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2170 * We are going to turn hardirqs on, so set the
2171 * usage bit for all held locks:
2173 if (!mark_held_locks(curr
, 1))
2176 * If we have softirqs enabled, then set the usage
2177 * bit for all held locks. (disabled hardirqs prevented
2178 * this bit from being set before)
2180 if (curr
->softirqs_enabled
)
2181 if (!mark_held_locks(curr
, 0))
2184 curr
->hardirq_enable_ip
= ip
;
2185 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2186 debug_atomic_inc(&hardirqs_on_events
);
2188 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2190 void trace_hardirqs_on(void)
2192 trace_hardirqs_on_caller(CALLER_ADDR0
);
2194 EXPORT_SYMBOL(trace_hardirqs_on
);
2197 * Hardirqs were disabled:
2199 void trace_hardirqs_off_caller(unsigned long a0
)
2201 struct task_struct
*curr
= current
;
2203 time_hardirqs_off(CALLER_ADDR0
, a0
);
2205 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2208 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2211 if (curr
->hardirqs_enabled
) {
2213 * We have done an ON -> OFF transition:
2215 curr
->hardirqs_enabled
= 0;
2216 curr
->hardirq_disable_ip
= _RET_IP_
;
2217 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2218 debug_atomic_inc(&hardirqs_off_events
);
2220 debug_atomic_inc(&redundant_hardirqs_off
);
2222 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2224 void trace_hardirqs_off(void)
2226 trace_hardirqs_off_caller(CALLER_ADDR0
);
2228 EXPORT_SYMBOL(trace_hardirqs_off
);
2231 * Softirqs will be enabled:
2233 void trace_softirqs_on(unsigned long ip
)
2235 struct task_struct
*curr
= current
;
2237 if (unlikely(!debug_locks
))
2240 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2243 if (curr
->softirqs_enabled
) {
2244 debug_atomic_inc(&redundant_softirqs_on
);
2249 * We'll do an OFF -> ON transition:
2251 curr
->softirqs_enabled
= 1;
2252 curr
->softirq_enable_ip
= ip
;
2253 curr
->softirq_enable_event
= ++curr
->irq_events
;
2254 debug_atomic_inc(&softirqs_on_events
);
2256 * We are going to turn softirqs on, so set the
2257 * usage bit for all held locks, if hardirqs are
2260 if (curr
->hardirqs_enabled
)
2261 mark_held_locks(curr
, 0);
2265 * Softirqs were disabled:
2267 void trace_softirqs_off(unsigned long ip
)
2269 struct task_struct
*curr
= current
;
2271 if (unlikely(!debug_locks
))
2274 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2277 if (curr
->softirqs_enabled
) {
2279 * We have done an ON -> OFF transition:
2281 curr
->softirqs_enabled
= 0;
2282 curr
->softirq_disable_ip
= ip
;
2283 curr
->softirq_disable_event
= ++curr
->irq_events
;
2284 debug_atomic_inc(&softirqs_off_events
);
2285 DEBUG_LOCKS_WARN_ON(!softirq_count());
2287 debug_atomic_inc(&redundant_softirqs_off
);
2290 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2293 * If non-trylock use in a hardirq or softirq context, then
2294 * mark the lock as used in these contexts:
2296 if (!hlock
->trylock
) {
2298 if (curr
->hardirq_context
)
2299 if (!mark_lock(curr
, hlock
,
2300 LOCK_USED_IN_HARDIRQ_READ
))
2302 if (curr
->softirq_context
)
2303 if (!mark_lock(curr
, hlock
,
2304 LOCK_USED_IN_SOFTIRQ_READ
))
2307 if (curr
->hardirq_context
)
2308 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2310 if (curr
->softirq_context
)
2311 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2315 if (!hlock
->hardirqs_off
) {
2317 if (!mark_lock(curr
, hlock
,
2318 LOCK_ENABLED_HARDIRQS_READ
))
2320 if (curr
->softirqs_enabled
)
2321 if (!mark_lock(curr
, hlock
,
2322 LOCK_ENABLED_SOFTIRQS_READ
))
2325 if (!mark_lock(curr
, hlock
,
2326 LOCK_ENABLED_HARDIRQS
))
2328 if (curr
->softirqs_enabled
)
2329 if (!mark_lock(curr
, hlock
,
2330 LOCK_ENABLED_SOFTIRQS
))
2338 static int separate_irq_context(struct task_struct
*curr
,
2339 struct held_lock
*hlock
)
2341 unsigned int depth
= curr
->lockdep_depth
;
2344 * Keep track of points where we cross into an interrupt context:
2346 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2347 curr
->softirq_context
;
2349 struct held_lock
*prev_hlock
;
2351 prev_hlock
= curr
->held_locks
+ depth
-1;
2353 * If we cross into another context, reset the
2354 * hash key (this also prevents the checking and the
2355 * adding of the dependency to 'prev'):
2357 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2366 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2367 enum lock_usage_bit new_bit
)
2373 static inline int mark_irqflags(struct task_struct
*curr
,
2374 struct held_lock
*hlock
)
2379 static inline int separate_irq_context(struct task_struct
*curr
,
2380 struct held_lock
*hlock
)
2388 * Mark a lock with a usage bit, and validate the state transition:
2390 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2391 enum lock_usage_bit new_bit
)
2393 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2396 * If already set then do not dirty the cacheline,
2397 * nor do any checks:
2399 if (likely(this->class->usage_mask
& new_mask
))
2405 * Make sure we didnt race:
2407 if (unlikely(this->class->usage_mask
& new_mask
)) {
2412 this->class->usage_mask
|= new_mask
;
2414 if (!save_trace(this->class->usage_traces
+ new_bit
))
2418 case LOCK_USED_IN_HARDIRQ
:
2419 case LOCK_USED_IN_SOFTIRQ
:
2420 case LOCK_USED_IN_HARDIRQ_READ
:
2421 case LOCK_USED_IN_SOFTIRQ_READ
:
2422 case LOCK_ENABLED_HARDIRQS
:
2423 case LOCK_ENABLED_SOFTIRQS
:
2424 case LOCK_ENABLED_HARDIRQS_READ
:
2425 case LOCK_ENABLED_SOFTIRQS_READ
:
2426 ret
= mark_lock_irq(curr
, this, new_bit
);
2431 debug_atomic_dec(&nr_unused_locks
);
2434 if (!debug_locks_off_graph_unlock())
2443 * We must printk outside of the graph_lock:
2446 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2448 print_irqtrace_events(curr
);
2456 * Initialize a lock instance's lock-class mapping info:
2458 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2459 struct lock_class_key
*key
, int subclass
)
2461 if (unlikely(!debug_locks
))
2464 if (DEBUG_LOCKS_WARN_ON(!key
))
2466 if (DEBUG_LOCKS_WARN_ON(!name
))
2469 * Sanity check, the lock-class key must be persistent:
2471 if (!static_obj(key
)) {
2472 printk("BUG: key %p not in .data!\n", key
);
2473 DEBUG_LOCKS_WARN_ON(1);
2478 lock
->class_cache
= NULL
;
2479 #ifdef CONFIG_LOCK_STAT
2480 lock
->cpu
= raw_smp_processor_id();
2483 register_lock_class(lock
, subclass
, 1);
2486 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2489 * This gets called for every mutex_lock*()/spin_lock*() operation.
2490 * We maintain the dependency maps and validate the locking attempt:
2492 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2493 int trylock
, int read
, int check
, int hardirqs_off
,
2496 struct task_struct
*curr
= current
;
2497 struct lock_class
*class = NULL
;
2498 struct held_lock
*hlock
;
2499 unsigned int depth
, id
;
2506 if (unlikely(!debug_locks
))
2509 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2512 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2514 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2515 printk("turning off the locking correctness validator.\n");
2520 class = lock
->class_cache
;
2522 * Not cached yet or subclass?
2524 if (unlikely(!class)) {
2525 class = register_lock_class(lock
, subclass
, 0);
2529 debug_atomic_inc((atomic_t
*)&class->ops
);
2530 if (very_verbose(class)) {
2531 printk("\nacquire class [%p] %s", class->key
, class->name
);
2532 if (class->name_version
> 1)
2533 printk("#%d", class->name_version
);
2539 * Add the lock to the list of currently held locks.
2540 * (we dont increase the depth just yet, up until the
2541 * dependency checks are done)
2543 depth
= curr
->lockdep_depth
;
2544 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2547 hlock
= curr
->held_locks
+ depth
;
2549 hlock
->class = class;
2550 hlock
->acquire_ip
= ip
;
2551 hlock
->instance
= lock
;
2552 hlock
->trylock
= trylock
;
2554 hlock
->check
= check
;
2555 hlock
->hardirqs_off
= hardirqs_off
;
2556 #ifdef CONFIG_LOCK_STAT
2557 hlock
->waittime_stamp
= 0;
2558 hlock
->holdtime_stamp
= sched_clock();
2561 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2564 /* mark it as used: */
2565 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2569 * Calculate the chain hash: it's the combined hash of all the
2570 * lock keys along the dependency chain. We save the hash value
2571 * at every step so that we can get the current hash easily
2572 * after unlock. The chain hash is then used to cache dependency
2575 * The 'key ID' is what is the most compact key value to drive
2576 * the hash, not class->key.
2578 id
= class - lock_classes
;
2579 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2582 chain_key
= curr
->curr_chain_key
;
2584 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2589 hlock
->prev_chain_key
= chain_key
;
2590 if (separate_irq_context(curr
, hlock
)) {
2594 chain_key
= iterate_chain_key(chain_key
, id
);
2596 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
2599 curr
->curr_chain_key
= chain_key
;
2600 curr
->lockdep_depth
++;
2601 check_chain_key(curr
);
2602 #ifdef CONFIG_DEBUG_LOCKDEP
2603 if (unlikely(!debug_locks
))
2606 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2608 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2609 printk("turning off the locking correctness validator.\n");
2613 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2614 max_lockdep_depth
= curr
->lockdep_depth
;
2620 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2623 if (!debug_locks_off())
2625 if (debug_locks_silent
)
2628 printk("\n=====================================\n");
2629 printk( "[ BUG: bad unlock balance detected! ]\n");
2630 printk( "-------------------------------------\n");
2631 printk("%s/%d is trying to release lock (",
2632 curr
->comm
, task_pid_nr(curr
));
2633 print_lockdep_cache(lock
);
2636 printk("but there are no more locks to release!\n");
2637 printk("\nother info that might help us debug this:\n");
2638 lockdep_print_held_locks(curr
);
2640 printk("\nstack backtrace:\n");
2647 * Common debugging checks for both nested and non-nested unlock:
2649 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2652 if (unlikely(!debug_locks
))
2654 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2657 if (curr
->lockdep_depth
<= 0)
2658 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2664 __lock_set_subclass(struct lockdep_map
*lock
,
2665 unsigned int subclass
, unsigned long ip
)
2667 struct task_struct
*curr
= current
;
2668 struct held_lock
*hlock
, *prev_hlock
;
2669 struct lock_class
*class;
2673 depth
= curr
->lockdep_depth
;
2674 if (DEBUG_LOCKS_WARN_ON(!depth
))
2678 for (i
= depth
-1; i
>= 0; i
--) {
2679 hlock
= curr
->held_locks
+ i
;
2681 * We must not cross into another context:
2683 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2685 if (hlock
->instance
== lock
)
2689 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2692 class = register_lock_class(lock
, subclass
, 0);
2693 hlock
->class = class;
2695 curr
->lockdep_depth
= i
;
2696 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2698 for (; i
< depth
; i
++) {
2699 hlock
= curr
->held_locks
+ i
;
2700 if (!__lock_acquire(hlock
->instance
,
2701 hlock
->class->subclass
, hlock
->trylock
,
2702 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2707 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
2713 * Remove the lock to the list of currently held locks in a
2714 * potentially non-nested (out of order) manner. This is a
2715 * relatively rare operation, as all the unlock APIs default
2716 * to nested mode (which uses lock_release()):
2719 lock_release_non_nested(struct task_struct
*curr
,
2720 struct lockdep_map
*lock
, unsigned long ip
)
2722 struct held_lock
*hlock
, *prev_hlock
;
2727 * Check whether the lock exists in the current stack
2730 depth
= curr
->lockdep_depth
;
2731 if (DEBUG_LOCKS_WARN_ON(!depth
))
2735 for (i
= depth
-1; i
>= 0; i
--) {
2736 hlock
= curr
->held_locks
+ i
;
2738 * We must not cross into another context:
2740 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2742 if (hlock
->instance
== lock
)
2746 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2749 lock_release_holdtime(hlock
);
2752 * We have the right lock to unlock, 'hlock' points to it.
2753 * Now we remove it from the stack, and add back the other
2754 * entries (if any), recalculating the hash along the way:
2756 curr
->lockdep_depth
= i
;
2757 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2759 for (i
++; i
< depth
; i
++) {
2760 hlock
= curr
->held_locks
+ i
;
2761 if (!__lock_acquire(hlock
->instance
,
2762 hlock
->class->subclass
, hlock
->trylock
,
2763 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2768 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2774 * Remove the lock to the list of currently held locks - this gets
2775 * called on mutex_unlock()/spin_unlock*() (or on a failed
2776 * mutex_lock_interruptible()). This is done for unlocks that nest
2777 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2779 static int lock_release_nested(struct task_struct
*curr
,
2780 struct lockdep_map
*lock
, unsigned long ip
)
2782 struct held_lock
*hlock
;
2786 * Pop off the top of the lock stack:
2788 depth
= curr
->lockdep_depth
- 1;
2789 hlock
= curr
->held_locks
+ depth
;
2792 * Is the unlock non-nested:
2794 if (hlock
->instance
!= lock
)
2795 return lock_release_non_nested(curr
, lock
, ip
);
2796 curr
->lockdep_depth
--;
2798 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2801 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2803 lock_release_holdtime(hlock
);
2805 #ifdef CONFIG_DEBUG_LOCKDEP
2806 hlock
->prev_chain_key
= 0;
2807 hlock
->class = NULL
;
2808 hlock
->acquire_ip
= 0;
2809 hlock
->irq_context
= 0;
2815 * Remove the lock to the list of currently held locks - this gets
2816 * called on mutex_unlock()/spin_unlock*() (or on a failed
2817 * mutex_lock_interruptible()). This is done for unlocks that nest
2818 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2821 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2823 struct task_struct
*curr
= current
;
2825 if (!check_unlock(curr
, lock
, ip
))
2829 if (!lock_release_nested(curr
, lock
, ip
))
2832 if (!lock_release_non_nested(curr
, lock
, ip
))
2836 check_chain_key(curr
);
2840 * Check whether we follow the irq-flags state precisely:
2842 static void check_flags(unsigned long flags
)
2844 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2845 defined(CONFIG_TRACE_IRQFLAGS)
2849 if (irqs_disabled_flags(flags
)) {
2850 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
2851 printk("possible reason: unannotated irqs-off.\n");
2854 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
2855 printk("possible reason: unannotated irqs-on.\n");
2860 * We dont accurately track softirq state in e.g.
2861 * hardirq contexts (such as on 4KSTACKS), so only
2862 * check if not in hardirq contexts:
2864 if (!hardirq_count()) {
2865 if (softirq_count())
2866 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2868 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2872 print_irqtrace_events(current
);
2877 lock_set_subclass(struct lockdep_map
*lock
,
2878 unsigned int subclass
, unsigned long ip
)
2880 unsigned long flags
;
2882 if (unlikely(current
->lockdep_recursion
))
2885 raw_local_irq_save(flags
);
2886 current
->lockdep_recursion
= 1;
2888 if (__lock_set_subclass(lock
, subclass
, ip
))
2889 check_chain_key(current
);
2890 current
->lockdep_recursion
= 0;
2891 raw_local_irq_restore(flags
);
2894 EXPORT_SYMBOL_GPL(lock_set_subclass
);
2897 * We are not always called with irqs disabled - do that here,
2898 * and also avoid lockdep recursion:
2900 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2901 int trylock
, int read
, int check
, unsigned long ip
)
2903 unsigned long flags
;
2905 if (unlikely(!lock_stat
&& !prove_locking
))
2908 if (unlikely(current
->lockdep_recursion
))
2911 raw_local_irq_save(flags
);
2914 current
->lockdep_recursion
= 1;
2915 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2916 irqs_disabled_flags(flags
), ip
);
2917 current
->lockdep_recursion
= 0;
2918 raw_local_irq_restore(flags
);
2921 EXPORT_SYMBOL_GPL(lock_acquire
);
2923 void lock_release(struct lockdep_map
*lock
, int nested
,
2926 unsigned long flags
;
2928 if (unlikely(!lock_stat
&& !prove_locking
))
2931 if (unlikely(current
->lockdep_recursion
))
2934 raw_local_irq_save(flags
);
2936 current
->lockdep_recursion
= 1;
2937 __lock_release(lock
, nested
, ip
);
2938 current
->lockdep_recursion
= 0;
2939 raw_local_irq_restore(flags
);
2942 EXPORT_SYMBOL_GPL(lock_release
);
2944 #ifdef CONFIG_LOCK_STAT
2946 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2949 if (!debug_locks_off())
2951 if (debug_locks_silent
)
2954 printk("\n=================================\n");
2955 printk( "[ BUG: bad contention detected! ]\n");
2956 printk( "---------------------------------\n");
2957 printk("%s/%d is trying to contend lock (",
2958 curr
->comm
, task_pid_nr(curr
));
2959 print_lockdep_cache(lock
);
2962 printk("but there are no locks held!\n");
2963 printk("\nother info that might help us debug this:\n");
2964 lockdep_print_held_locks(curr
);
2966 printk("\nstack backtrace:\n");
2973 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2975 struct task_struct
*curr
= current
;
2976 struct held_lock
*hlock
, *prev_hlock
;
2977 struct lock_class_stats
*stats
;
2981 depth
= curr
->lockdep_depth
;
2982 if (DEBUG_LOCKS_WARN_ON(!depth
))
2986 for (i
= depth
-1; i
>= 0; i
--) {
2987 hlock
= curr
->held_locks
+ i
;
2989 * We must not cross into another context:
2991 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2993 if (hlock
->instance
== lock
)
2997 print_lock_contention_bug(curr
, lock
, ip
);
3001 hlock
->waittime_stamp
= sched_clock();
3003 point
= lock_contention_point(hlock
->class, ip
);
3005 stats
= get_lock_stats(hlock
->class);
3006 if (point
< ARRAY_SIZE(stats
->contention_point
))
3007 stats
->contention_point
[i
]++;
3008 if (lock
->cpu
!= smp_processor_id())
3009 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3010 put_lock_stats(stats
);
3014 __lock_acquired(struct lockdep_map
*lock
)
3016 struct task_struct
*curr
= current
;
3017 struct held_lock
*hlock
, *prev_hlock
;
3018 struct lock_class_stats
*stats
;
3024 depth
= curr
->lockdep_depth
;
3025 if (DEBUG_LOCKS_WARN_ON(!depth
))
3029 for (i
= depth
-1; i
>= 0; i
--) {
3030 hlock
= curr
->held_locks
+ i
;
3032 * We must not cross into another context:
3034 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3036 if (hlock
->instance
== lock
)
3040 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3044 cpu
= smp_processor_id();
3045 if (hlock
->waittime_stamp
) {
3046 now
= sched_clock();
3047 waittime
= now
- hlock
->waittime_stamp
;
3048 hlock
->holdtime_stamp
= now
;
3051 stats
= get_lock_stats(hlock
->class);
3054 lock_time_inc(&stats
->read_waittime
, waittime
);
3056 lock_time_inc(&stats
->write_waittime
, waittime
);
3058 if (lock
->cpu
!= cpu
)
3059 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3060 put_lock_stats(stats
);
3065 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3067 unsigned long flags
;
3069 if (unlikely(!lock_stat
))
3072 if (unlikely(current
->lockdep_recursion
))
3075 raw_local_irq_save(flags
);
3077 current
->lockdep_recursion
= 1;
3078 __lock_contended(lock
, ip
);
3079 current
->lockdep_recursion
= 0;
3080 raw_local_irq_restore(flags
);
3082 EXPORT_SYMBOL_GPL(lock_contended
);
3084 void lock_acquired(struct lockdep_map
*lock
)
3086 unsigned long flags
;
3088 if (unlikely(!lock_stat
))
3091 if (unlikely(current
->lockdep_recursion
))
3094 raw_local_irq_save(flags
);
3096 current
->lockdep_recursion
= 1;
3097 __lock_acquired(lock
);
3098 current
->lockdep_recursion
= 0;
3099 raw_local_irq_restore(flags
);
3101 EXPORT_SYMBOL_GPL(lock_acquired
);
3105 * Used by the testsuite, sanitize the validator state
3106 * after a simulated failure:
3109 void lockdep_reset(void)
3111 unsigned long flags
;
3114 raw_local_irq_save(flags
);
3115 current
->curr_chain_key
= 0;
3116 current
->lockdep_depth
= 0;
3117 current
->lockdep_recursion
= 0;
3118 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3119 nr_hardirq_chains
= 0;
3120 nr_softirq_chains
= 0;
3121 nr_process_chains
= 0;
3123 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3124 INIT_LIST_HEAD(chainhash_table
+ i
);
3125 raw_local_irq_restore(flags
);
3128 static void zap_class(struct lock_class
*class)
3133 * Remove all dependencies this lock is
3136 for (i
= 0; i
< nr_list_entries
; i
++) {
3137 if (list_entries
[i
].class == class)
3138 list_del_rcu(&list_entries
[i
].entry
);
3141 * Unhash the class and remove it from the all_lock_classes list:
3143 list_del_rcu(&class->hash_entry
);
3144 list_del_rcu(&class->lock_entry
);
3148 static inline int within(const void *addr
, void *start
, unsigned long size
)
3150 return addr
>= start
&& addr
< start
+ size
;
3153 void lockdep_free_key_range(void *start
, unsigned long size
)
3155 struct lock_class
*class, *next
;
3156 struct list_head
*head
;
3157 unsigned long flags
;
3161 raw_local_irq_save(flags
);
3162 locked
= graph_lock();
3165 * Unhash all classes that were created by this module:
3167 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3168 head
= classhash_table
+ i
;
3169 if (list_empty(head
))
3171 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3172 if (within(class->key
, start
, size
))
3174 else if (within(class->name
, start
, size
))
3181 raw_local_irq_restore(flags
);
3184 void lockdep_reset_lock(struct lockdep_map
*lock
)
3186 struct lock_class
*class, *next
;
3187 struct list_head
*head
;
3188 unsigned long flags
;
3192 raw_local_irq_save(flags
);
3195 * Remove all classes this lock might have:
3197 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3199 * If the class exists we look it up and zap it:
3201 class = look_up_lock_class(lock
, j
);
3206 * Debug check: in the end all mapped classes should
3209 locked
= graph_lock();
3210 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3211 head
= classhash_table
+ i
;
3212 if (list_empty(head
))
3214 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3215 if (unlikely(class == lock
->class_cache
)) {
3216 if (debug_locks_off_graph_unlock())
3226 raw_local_irq_restore(flags
);
3229 void lockdep_init(void)
3234 * Some architectures have their own start_kernel()
3235 * code which calls lockdep_init(), while we also
3236 * call lockdep_init() from the start_kernel() itself,
3237 * and we want to initialize the hashes only once:
3239 if (lockdep_initialized
)
3242 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3243 INIT_LIST_HEAD(classhash_table
+ i
);
3245 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3246 INIT_LIST_HEAD(chainhash_table
+ i
);
3248 lockdep_initialized
= 1;
3251 void __init
lockdep_info(void)
3253 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3255 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3256 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3257 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3258 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3259 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3260 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3261 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3263 printk(" memory used by lock dependency info: %lu kB\n",
3264 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3265 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3266 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3267 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3268 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
3270 printk(" per task-struct memory footprint: %lu bytes\n",
3271 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3273 #ifdef CONFIG_DEBUG_LOCKDEP
3274 if (lockdep_init_error
) {
3275 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3276 printk("Call stack leading to lockdep invocation was:\n");
3277 print_stack_trace(&lockdep_init_trace
, 0);
3283 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3284 const void *mem_to
, struct held_lock
*hlock
)
3286 if (!debug_locks_off())
3288 if (debug_locks_silent
)
3291 printk("\n=========================\n");
3292 printk( "[ BUG: held lock freed! ]\n");
3293 printk( "-------------------------\n");
3294 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3295 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3297 lockdep_print_held_locks(curr
);
3299 printk("\nstack backtrace:\n");
3303 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3304 const void* lock_from
, unsigned long lock_len
)
3306 return lock_from
+ lock_len
<= mem_from
||
3307 mem_from
+ mem_len
<= lock_from
;
3311 * Called when kernel memory is freed (or unmapped), or if a lock
3312 * is destroyed or reinitialized - this code checks whether there is
3313 * any held lock in the memory range of <from> to <to>:
3315 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3317 struct task_struct
*curr
= current
;
3318 struct held_lock
*hlock
;
3319 unsigned long flags
;
3322 if (unlikely(!debug_locks
))
3325 local_irq_save(flags
);
3326 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3327 hlock
= curr
->held_locks
+ i
;
3329 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3330 sizeof(*hlock
->instance
)))
3333 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3336 local_irq_restore(flags
);
3338 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3340 static void print_held_locks_bug(struct task_struct
*curr
)
3342 if (!debug_locks_off())
3344 if (debug_locks_silent
)
3347 printk("\n=====================================\n");
3348 printk( "[ BUG: lock held at task exit time! ]\n");
3349 printk( "-------------------------------------\n");
3350 printk("%s/%d is exiting with locks still held!\n",
3351 curr
->comm
, task_pid_nr(curr
));
3352 lockdep_print_held_locks(curr
);
3354 printk("\nstack backtrace:\n");
3358 void debug_check_no_locks_held(struct task_struct
*task
)
3360 if (unlikely(task
->lockdep_depth
> 0))
3361 print_held_locks_bug(task
);
3364 void debug_show_all_locks(void)
3366 struct task_struct
*g
, *p
;
3370 if (unlikely(!debug_locks
)) {
3371 printk("INFO: lockdep is turned off.\n");
3374 printk("\nShowing all locks held in the system:\n");
3377 * Here we try to get the tasklist_lock as hard as possible,
3378 * if not successful after 2 seconds we ignore it (but keep
3379 * trying). This is to enable a debug printout even if a
3380 * tasklist_lock-holding task deadlocks or crashes.
3383 if (!read_trylock(&tasklist_lock
)) {
3385 printk("hm, tasklist_lock locked, retrying... ");
3388 printk(" #%d", 10-count
);
3392 printk(" ignoring it.\n");
3396 printk(" locked it.\n");
3398 do_each_thread(g
, p
) {
3400 * It's not reliable to print a task's held locks
3401 * if it's not sleeping (or if it's not the current
3404 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3406 if (p
->lockdep_depth
)
3407 lockdep_print_held_locks(p
);
3409 if (read_trylock(&tasklist_lock
))
3411 } while_each_thread(g
, p
);
3414 printk("=============================================\n\n");
3417 read_unlock(&tasklist_lock
);
3420 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3423 * Careful: only use this function if you are sure that
3424 * the task cannot run in parallel!
3426 void __debug_show_held_locks(struct task_struct
*task
)
3428 if (unlikely(!debug_locks
)) {
3429 printk("INFO: lockdep is turned off.\n");
3432 lockdep_print_held_locks(task
);
3434 EXPORT_SYMBOL_GPL(__debug_show_held_locks
);
3436 void debug_show_held_locks(struct task_struct
*task
)
3438 __debug_show_held_locks(task
);
3441 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3443 void lockdep_sys_exit(void)
3445 struct task_struct
*curr
= current
;
3447 if (unlikely(curr
->lockdep_depth
)) {
3448 if (!debug_locks_off())
3450 printk("\n================================================\n");
3451 printk( "[ BUG: lock held when returning to user space! ]\n");
3452 printk( "------------------------------------------------\n");
3453 printk("%s/%d is leaving the kernel with locks still held!\n",
3454 curr
->comm
, curr
->pid
);
3455 lockdep_print_held_locks(curr
);