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
);
181 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
182 stats
.bounces
[i
] += pcs
->bounces
[i
];
188 void clear_lock_stats(struct lock_class
*class)
192 for_each_possible_cpu(cpu
) {
193 struct lock_class_stats
*cpu_stats
=
194 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
196 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
198 memset(class->contention_point
, 0, sizeof(class->contention_point
));
201 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
203 return &get_cpu_var(lock_stats
)[class - lock_classes
];
206 static void put_lock_stats(struct lock_class_stats
*stats
)
208 put_cpu_var(lock_stats
);
211 static void lock_release_holdtime(struct held_lock
*hlock
)
213 struct lock_class_stats
*stats
;
219 holdtime
= sched_clock() - hlock
->holdtime_stamp
;
221 stats
= get_lock_stats(hlock
->class);
223 lock_time_inc(&stats
->read_holdtime
, holdtime
);
225 lock_time_inc(&stats
->write_holdtime
, holdtime
);
226 put_lock_stats(stats
);
229 static inline void lock_release_holdtime(struct held_lock
*hlock
)
235 * We keep a global list of all lock classes. The list only grows,
236 * never shrinks. The list is only accessed with the lockdep
237 * spinlock lock held.
239 LIST_HEAD(all_lock_classes
);
242 * The lockdep classes are in a hash-table as well, for fast lookup:
244 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
245 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
246 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
247 #define classhashentry(key) (classhash_table + __classhashfn((key)))
249 static struct list_head classhash_table
[CLASSHASH_SIZE
];
252 * We put the lock dependency chains into a hash-table as well, to cache
255 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
256 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
257 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
258 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
260 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
263 * The hash key of the lock dependency chains is a hash itself too:
264 * it's a hash of all locks taken up to that lock, including that lock.
265 * It's a 64-bit hash, because it's important for the keys to be
268 #define iterate_chain_key(key1, key2) \
269 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
270 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
273 void lockdep_off(void)
275 current
->lockdep_recursion
++;
278 EXPORT_SYMBOL(lockdep_off
);
280 void lockdep_on(void)
282 current
->lockdep_recursion
--;
285 EXPORT_SYMBOL(lockdep_on
);
288 * Debugging switches:
292 #define VERY_VERBOSE 0
295 # define HARDIRQ_VERBOSE 1
296 # define SOFTIRQ_VERBOSE 1
298 # define HARDIRQ_VERBOSE 0
299 # define SOFTIRQ_VERBOSE 0
302 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
304 * Quick filtering for interesting events:
306 static int class_filter(struct lock_class
*class)
310 if (class->name_version
== 1 &&
311 !strcmp(class->name
, "lockname"))
313 if (class->name_version
== 1 &&
314 !strcmp(class->name
, "&struct->lockfield"))
317 /* Filter everything else. 1 would be to allow everything else */
322 static int verbose(struct lock_class
*class)
325 return class_filter(class);
331 * Stack-trace: tightly packed array of stack backtrace
332 * addresses. Protected by the graph_lock.
334 unsigned long nr_stack_trace_entries
;
335 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
337 static int save_trace(struct stack_trace
*trace
)
339 trace
->nr_entries
= 0;
340 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
341 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
345 save_stack_trace(trace
);
347 trace
->max_entries
= trace
->nr_entries
;
349 nr_stack_trace_entries
+= trace
->nr_entries
;
351 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
352 if (!debug_locks_off_graph_unlock())
355 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
356 printk("turning off the locking correctness validator.\n");
365 unsigned int nr_hardirq_chains
;
366 unsigned int nr_softirq_chains
;
367 unsigned int nr_process_chains
;
368 unsigned int max_lockdep_depth
;
369 unsigned int max_recursion_depth
;
371 #ifdef CONFIG_DEBUG_LOCKDEP
373 * We cannot printk in early bootup code. Not even early_printk()
374 * might work. So we mark any initialization errors and printk
375 * about it later on, in lockdep_info().
377 static int lockdep_init_error
;
378 static unsigned long lockdep_init_trace_data
[20];
379 static struct stack_trace lockdep_init_trace
= {
380 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
381 .entries
= lockdep_init_trace_data
,
385 * Various lockdep statistics:
387 atomic_t chain_lookup_hits
;
388 atomic_t chain_lookup_misses
;
389 atomic_t hardirqs_on_events
;
390 atomic_t hardirqs_off_events
;
391 atomic_t redundant_hardirqs_on
;
392 atomic_t redundant_hardirqs_off
;
393 atomic_t softirqs_on_events
;
394 atomic_t softirqs_off_events
;
395 atomic_t redundant_softirqs_on
;
396 atomic_t redundant_softirqs_off
;
397 atomic_t nr_unused_locks
;
398 atomic_t nr_cyclic_checks
;
399 atomic_t nr_cyclic_check_recursions
;
400 atomic_t nr_find_usage_forwards_checks
;
401 atomic_t nr_find_usage_forwards_recursions
;
402 atomic_t nr_find_usage_backwards_checks
;
403 atomic_t nr_find_usage_backwards_recursions
;
404 # define debug_atomic_inc(ptr) atomic_inc(ptr)
405 # define debug_atomic_dec(ptr) atomic_dec(ptr)
406 # define debug_atomic_read(ptr) atomic_read(ptr)
408 # define debug_atomic_inc(ptr) do { } while (0)
409 # define debug_atomic_dec(ptr) do { } while (0)
410 # define debug_atomic_read(ptr) 0
417 static const char *usage_str
[] =
419 [LOCK_USED
] = "initial-use ",
420 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
421 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
422 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
423 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
424 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
425 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
426 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
427 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
430 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
432 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
436 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
438 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
440 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
443 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
446 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
449 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
452 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
454 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
456 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
460 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
462 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
464 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
469 static void print_lock_name(struct lock_class
*class)
471 char str
[KSYM_NAME_LEN
], c1
, c2
, c3
, c4
;
474 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
478 name
= __get_key_name(class->key
, str
);
479 printk(" (%s", name
);
481 printk(" (%s", name
);
482 if (class->name_version
> 1)
483 printk("#%d", class->name_version
);
485 printk("/%d", class->subclass
);
487 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
490 static void print_lockdep_cache(struct lockdep_map
*lock
)
493 char str
[KSYM_NAME_LEN
];
497 name
= __get_key_name(lock
->key
->subkeys
, str
);
502 static void print_lock(struct held_lock
*hlock
)
504 print_lock_name(hlock
->class);
506 print_ip_sym(hlock
->acquire_ip
);
509 static void lockdep_print_held_locks(struct task_struct
*curr
)
511 int i
, depth
= curr
->lockdep_depth
;
514 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
517 printk("%d lock%s held by %s/%d:\n",
518 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
520 for (i
= 0; i
< depth
; i
++) {
522 print_lock(curr
->held_locks
+ i
);
526 static void print_lock_class_header(struct lock_class
*class, int depth
)
530 printk("%*s->", depth
, "");
531 print_lock_name(class);
532 printk(" ops: %lu", class->ops
);
535 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
536 if (class->usage_mask
& (1 << bit
)) {
539 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
540 len
+= printk(" at:\n");
541 print_stack_trace(class->usage_traces
+ bit
, len
);
544 printk("%*s }\n", depth
, "");
546 printk("%*s ... key at: ",depth
,"");
547 print_ip_sym((unsigned long)class->key
);
551 * printk all lock dependencies starting at <entry>:
553 static void print_lock_dependencies(struct lock_class
*class, int depth
)
555 struct lock_list
*entry
;
557 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
560 print_lock_class_header(class, depth
);
562 list_for_each_entry(entry
, &class->locks_after
, entry
) {
563 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
566 print_lock_dependencies(entry
->class, depth
+ 1);
568 printk("%*s ... acquired at:\n",depth
,"");
569 print_stack_trace(&entry
->trace
, 2);
574 static void print_kernel_version(void)
576 printk("%s %.*s\n", init_utsname()->release
,
577 (int)strcspn(init_utsname()->version
, " "),
578 init_utsname()->version
);
581 static int very_verbose(struct lock_class
*class)
584 return class_filter(class);
590 * Is this the address of a static object:
592 static int static_obj(void *obj
)
594 unsigned long start
= (unsigned long) &_stext
,
595 end
= (unsigned long) &_end
,
596 addr
= (unsigned long) obj
;
604 if ((addr
>= start
) && (addr
< end
))
611 for_each_possible_cpu(i
) {
612 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
613 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
616 if ((addr
>= start
) && (addr
< end
))
624 return is_module_address(addr
);
628 * To make lock name printouts unique, we calculate a unique
629 * class->name_version generation counter:
631 static int count_matching_names(struct lock_class
*new_class
)
633 struct lock_class
*class;
636 if (!new_class
->name
)
639 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
640 if (new_class
->key
- new_class
->subclass
== class->key
)
641 return class->name_version
;
642 if (class->name
&& !strcmp(class->name
, new_class
->name
))
643 count
= max(count
, class->name_version
);
650 * Register a lock's class in the hash-table, if the class is not present
651 * yet. Otherwise we look it up. We cache the result in the lock object
652 * itself, so actual lookup of the hash should be once per lock object.
654 static inline struct lock_class
*
655 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
657 struct lockdep_subclass_key
*key
;
658 struct list_head
*hash_head
;
659 struct lock_class
*class;
661 #ifdef CONFIG_DEBUG_LOCKDEP
663 * If the architecture calls into lockdep before initializing
664 * the hashes then we'll warn about it later. (we cannot printk
667 if (unlikely(!lockdep_initialized
)) {
669 lockdep_init_error
= 1;
670 save_stack_trace(&lockdep_init_trace
);
675 * Static locks do not have their class-keys yet - for them the key
676 * is the lock object itself:
678 if (unlikely(!lock
->key
))
679 lock
->key
= (void *)lock
;
682 * NOTE: the class-key must be unique. For dynamic locks, a static
683 * lock_class_key variable is passed in through the mutex_init()
684 * (or spin_lock_init()) call - which acts as the key. For static
685 * locks we use the lock object itself as the key.
687 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
688 sizeof(struct lockdep_map
));
690 key
= lock
->key
->subkeys
+ subclass
;
692 hash_head
= classhashentry(key
);
695 * We can walk the hash lockfree, because the hash only
696 * grows, and we are careful when adding entries to the end:
698 list_for_each_entry(class, hash_head
, hash_entry
) {
699 if (class->key
== key
) {
700 WARN_ON_ONCE(class->name
!= lock
->name
);
709 * Register a lock's class in the hash-table, if the class is not present
710 * yet. Otherwise we look it up. We cache the result in the lock object
711 * itself, so actual lookup of the hash should be once per lock object.
713 static inline struct lock_class
*
714 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
716 struct lockdep_subclass_key
*key
;
717 struct list_head
*hash_head
;
718 struct lock_class
*class;
721 class = look_up_lock_class(lock
, subclass
);
726 * Debug-check: all keys must be persistent!
728 if (!static_obj(lock
->key
)) {
730 printk("INFO: trying to register non-static key.\n");
731 printk("the code is fine but needs lockdep annotation.\n");
732 printk("turning off the locking correctness validator.\n");
738 key
= lock
->key
->subkeys
+ subclass
;
739 hash_head
= classhashentry(key
);
741 raw_local_irq_save(flags
);
743 raw_local_irq_restore(flags
);
747 * We have to do the hash-walk again, to avoid races
750 list_for_each_entry(class, hash_head
, hash_entry
)
751 if (class->key
== key
)
754 * Allocate a new key from the static array, and add it to
757 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
758 if (!debug_locks_off_graph_unlock()) {
759 raw_local_irq_restore(flags
);
762 raw_local_irq_restore(flags
);
764 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
765 printk("turning off the locking correctness validator.\n");
768 class = lock_classes
+ nr_lock_classes
++;
769 debug_atomic_inc(&nr_unused_locks
);
771 class->name
= lock
->name
;
772 class->subclass
= subclass
;
773 INIT_LIST_HEAD(&class->lock_entry
);
774 INIT_LIST_HEAD(&class->locks_before
);
775 INIT_LIST_HEAD(&class->locks_after
);
776 class->name_version
= count_matching_names(class);
778 * We use RCU's safe list-add method to make
779 * parallel walking of the hash-list safe:
781 list_add_tail_rcu(&class->hash_entry
, hash_head
);
783 * Add it to the global list of classes:
785 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
787 if (verbose(class)) {
789 raw_local_irq_restore(flags
);
791 printk("\nnew class %p: %s", class->key
, class->name
);
792 if (class->name_version
> 1)
793 printk("#%d", class->name_version
);
797 raw_local_irq_save(flags
);
799 raw_local_irq_restore(flags
);
805 raw_local_irq_restore(flags
);
807 if (!subclass
|| force
)
808 lock
->class_cache
= class;
810 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
816 #ifdef CONFIG_PROVE_LOCKING
818 * Allocate a lockdep entry. (assumes the graph_lock held, returns
819 * with NULL on failure)
821 static struct lock_list
*alloc_list_entry(void)
823 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
824 if (!debug_locks_off_graph_unlock())
827 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
828 printk("turning off the locking correctness validator.\n");
831 return list_entries
+ nr_list_entries
++;
835 * Add a new dependency to the head of the list:
837 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
838 struct list_head
*head
, unsigned long ip
, int distance
)
840 struct lock_list
*entry
;
842 * Lock not present yet - get a new dependency struct and
843 * add it to the list:
845 entry
= alloc_list_entry();
850 entry
->distance
= distance
;
851 if (!save_trace(&entry
->trace
))
855 * Since we never remove from the dependency list, the list can
856 * be walked lockless by other CPUs, it's only allocation
857 * that must be protected by the spinlock. But this also means
858 * we must make new entries visible only once writes to the
859 * entry become visible - hence the RCU op:
861 list_add_tail_rcu(&entry
->entry
, head
);
867 * Recursive, forwards-direction lock-dependency checking, used for
868 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
871 * (to keep the stackframe of the recursive functions small we
872 * use these global variables, and we also mark various helper
873 * functions as noinline.)
875 static struct held_lock
*check_source
, *check_target
;
878 * Print a dependency chain entry (this is only done when a deadlock
879 * has been detected):
882 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
884 if (debug_locks_silent
)
886 printk("\n-> #%u", depth
);
887 print_lock_name(target
->class);
889 print_stack_trace(&target
->trace
, 6);
895 * When a circular dependency is detected, print the
899 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
901 struct task_struct
*curr
= current
;
903 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
906 printk("\n=======================================================\n");
907 printk( "[ INFO: possible circular locking dependency detected ]\n");
908 print_kernel_version();
909 printk( "-------------------------------------------------------\n");
910 printk("%s/%d is trying to acquire lock:\n",
911 curr
->comm
, task_pid_nr(curr
));
912 print_lock(check_source
);
913 printk("\nbut task is already holding lock:\n");
914 print_lock(check_target
);
915 printk("\nwhich lock already depends on the new lock.\n\n");
916 printk("\nthe existing dependency chain (in reverse order) is:\n");
918 print_circular_bug_entry(entry
, depth
);
923 static noinline
int print_circular_bug_tail(void)
925 struct task_struct
*curr
= current
;
926 struct lock_list
this;
928 if (debug_locks_silent
)
931 this.class = check_source
->class;
932 if (!save_trace(&this.trace
))
935 print_circular_bug_entry(&this, 0);
937 printk("\nother info that might help us debug this:\n\n");
938 lockdep_print_held_locks(curr
);
940 printk("\nstack backtrace:\n");
946 #define RECURSION_LIMIT 40
948 static int noinline
print_infinite_recursion_bug(void)
950 if (!debug_locks_off_graph_unlock())
959 * Prove that the dependency graph starting at <entry> can not
960 * lead to <target>. Print an error and return 0 if it does.
963 check_noncircular(struct lock_class
*source
, unsigned int depth
)
965 struct lock_list
*entry
;
967 debug_atomic_inc(&nr_cyclic_check_recursions
);
968 if (depth
> max_recursion_depth
)
969 max_recursion_depth
= depth
;
970 if (depth
>= RECURSION_LIMIT
)
971 return print_infinite_recursion_bug();
973 * Check this lock's dependency list:
975 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
976 if (entry
->class == check_target
->class)
977 return print_circular_bug_header(entry
, depth
+1);
978 debug_atomic_inc(&nr_cyclic_checks
);
979 if (!check_noncircular(entry
->class, depth
+1))
980 return print_circular_bug_entry(entry
, depth
+1);
985 #ifdef CONFIG_TRACE_IRQFLAGS
987 * Forwards and backwards subgraph searching, for the purposes of
988 * proving that two subgraphs can be connected by a new dependency
989 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
991 static enum lock_usage_bit find_usage_bit
;
992 static struct lock_class
*forwards_match
, *backwards_match
;
995 * Find a node in the forwards-direction dependency sub-graph starting
996 * at <source> that matches <find_usage_bit>.
998 * Return 2 if such a node exists in the subgraph, and put that node
999 * into <forwards_match>.
1001 * Return 1 otherwise and keep <forwards_match> unchanged.
1002 * Return 0 on error.
1005 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
1007 struct lock_list
*entry
;
1010 if (depth
> max_recursion_depth
)
1011 max_recursion_depth
= depth
;
1012 if (depth
>= RECURSION_LIMIT
)
1013 return print_infinite_recursion_bug();
1015 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1016 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1017 forwards_match
= source
;
1022 * Check this lock's dependency list:
1024 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
1025 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
1026 ret
= find_usage_forwards(entry
->class, depth
+1);
1027 if (ret
== 2 || ret
== 0)
1034 * Find a node in the backwards-direction dependency sub-graph starting
1035 * at <source> that matches <find_usage_bit>.
1037 * Return 2 if such a node exists in the subgraph, and put that node
1038 * into <backwards_match>.
1040 * Return 1 otherwise and keep <backwards_match> unchanged.
1041 * Return 0 on error.
1044 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
1046 struct lock_list
*entry
;
1049 if (!__raw_spin_is_locked(&lockdep_lock
))
1050 return DEBUG_LOCKS_WARN_ON(1);
1052 if (depth
> max_recursion_depth
)
1053 max_recursion_depth
= depth
;
1054 if (depth
>= RECURSION_LIMIT
)
1055 return print_infinite_recursion_bug();
1057 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1058 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1059 backwards_match
= source
;
1064 * Check this lock's dependency list:
1066 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
1067 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
1068 ret
= find_usage_backwards(entry
->class, depth
+1);
1069 if (ret
== 2 || ret
== 0)
1076 print_bad_irq_dependency(struct task_struct
*curr
,
1077 struct held_lock
*prev
,
1078 struct held_lock
*next
,
1079 enum lock_usage_bit bit1
,
1080 enum lock_usage_bit bit2
,
1081 const char *irqclass
)
1083 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1086 printk("\n======================================================\n");
1087 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1088 irqclass
, irqclass
);
1089 print_kernel_version();
1090 printk( "------------------------------------------------------\n");
1091 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1092 curr
->comm
, task_pid_nr(curr
),
1093 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1094 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1095 curr
->hardirqs_enabled
,
1096 curr
->softirqs_enabled
);
1099 printk("\nand this task is already holding:\n");
1101 printk("which would create a new lock dependency:\n");
1102 print_lock_name(prev
->class);
1104 print_lock_name(next
->class);
1107 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1109 print_lock_name(backwards_match
);
1110 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1112 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
1114 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1115 print_lock_name(forwards_match
);
1116 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1119 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
1121 printk("\nother info that might help us debug this:\n\n");
1122 lockdep_print_held_locks(curr
);
1124 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
1125 print_lock_dependencies(backwards_match
, 0);
1127 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
1128 print_lock_dependencies(forwards_match
, 0);
1130 printk("\nstack backtrace:\n");
1137 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1138 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1139 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1143 find_usage_bit
= bit_backwards
;
1144 /* fills in <backwards_match> */
1145 ret
= find_usage_backwards(prev
->class, 0);
1146 if (!ret
|| ret
== 1)
1149 find_usage_bit
= bit_forwards
;
1150 ret
= find_usage_forwards(next
->class, 0);
1151 if (!ret
|| ret
== 1)
1154 return print_bad_irq_dependency(curr
, prev
, next
,
1155 bit_backwards
, bit_forwards
, irqclass
);
1159 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1160 struct held_lock
*next
)
1163 * Prove that the new dependency does not connect a hardirq-safe
1164 * lock with a hardirq-unsafe lock - to achieve this we search
1165 * the backwards-subgraph starting at <prev>, and the
1166 * forwards-subgraph starting at <next>:
1168 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
1169 LOCK_ENABLED_HARDIRQS
, "hard"))
1173 * Prove that the new dependency does not connect a hardirq-safe-read
1174 * lock with a hardirq-unsafe lock - to achieve this we search
1175 * the backwards-subgraph starting at <prev>, and the
1176 * forwards-subgraph starting at <next>:
1178 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
1179 LOCK_ENABLED_HARDIRQS
, "hard-read"))
1183 * Prove that the new dependency does not connect a softirq-safe
1184 * lock with a softirq-unsafe lock - to achieve this we search
1185 * the backwards-subgraph starting at <prev>, and the
1186 * forwards-subgraph starting at <next>:
1188 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
1189 LOCK_ENABLED_SOFTIRQS
, "soft"))
1192 * Prove that the new dependency does not connect a softirq-safe-read
1193 * lock with a softirq-unsafe lock - to achieve this we search
1194 * the backwards-subgraph starting at <prev>, and the
1195 * forwards-subgraph starting at <next>:
1197 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
1198 LOCK_ENABLED_SOFTIRQS
, "soft"))
1204 static void inc_chains(void)
1206 if (current
->hardirq_context
)
1207 nr_hardirq_chains
++;
1209 if (current
->softirq_context
)
1210 nr_softirq_chains
++;
1212 nr_process_chains
++;
1219 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1220 struct held_lock
*next
)
1225 static inline void inc_chains(void)
1227 nr_process_chains
++;
1233 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1234 struct held_lock
*next
)
1236 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1239 printk("\n=============================================\n");
1240 printk( "[ INFO: possible recursive locking detected ]\n");
1241 print_kernel_version();
1242 printk( "---------------------------------------------\n");
1243 printk("%s/%d is trying to acquire lock:\n",
1244 curr
->comm
, task_pid_nr(curr
));
1246 printk("\nbut task is already holding lock:\n");
1249 printk("\nother info that might help us debug this:\n");
1250 lockdep_print_held_locks(curr
);
1252 printk("\nstack backtrace:\n");
1259 * Check whether we are holding such a class already.
1261 * (Note that this has to be done separately, because the graph cannot
1262 * detect such classes of deadlocks.)
1264 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1267 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1268 struct lockdep_map
*next_instance
, int read
)
1270 struct held_lock
*prev
;
1273 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1274 prev
= curr
->held_locks
+ i
;
1275 if (prev
->class != next
->class)
1278 * Allow read-after-read recursion of the same
1279 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1281 if ((read
== 2) && prev
->read
)
1283 return print_deadlock_bug(curr
, prev
, next
);
1289 * There was a chain-cache miss, and we are about to add a new dependency
1290 * to a previous lock. We recursively validate the following rules:
1292 * - would the adding of the <prev> -> <next> dependency create a
1293 * circular dependency in the graph? [== circular deadlock]
1295 * - does the new prev->next dependency connect any hardirq-safe lock
1296 * (in the full backwards-subgraph starting at <prev>) with any
1297 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1298 * <next>)? [== illegal lock inversion with hardirq contexts]
1300 * - does the new prev->next dependency connect any softirq-safe lock
1301 * (in the full backwards-subgraph starting at <prev>) with any
1302 * softirq-unsafe lock (in the full forwards-subgraph starting at
1303 * <next>)? [== illegal lock inversion with softirq contexts]
1305 * any of these scenarios could lead to a deadlock.
1307 * Then if all the validations pass, we add the forwards and backwards
1311 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1312 struct held_lock
*next
, int distance
)
1314 struct lock_list
*entry
;
1318 * Prove that the new <prev> -> <next> dependency would not
1319 * create a circular dependency in the graph. (We do this by
1320 * forward-recursing into the graph starting at <next>, and
1321 * checking whether we can reach <prev>.)
1323 * We are using global variables to control the recursion, to
1324 * keep the stackframe size of the recursive functions low:
1326 check_source
= next
;
1327 check_target
= prev
;
1328 if (!(check_noncircular(next
->class, 0)))
1329 return print_circular_bug_tail();
1331 if (!check_prev_add_irq(curr
, prev
, next
))
1335 * For recursive read-locks we do all the dependency checks,
1336 * but we dont store read-triggered dependencies (only
1337 * write-triggered dependencies). This ensures that only the
1338 * write-side dependencies matter, and that if for example a
1339 * write-lock never takes any other locks, then the reads are
1340 * equivalent to a NOP.
1342 if (next
->read
== 2 || prev
->read
== 2)
1345 * Is the <prev> -> <next> dependency already present?
1347 * (this may occur even though this is a new chain: consider
1348 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1349 * chains - the second one will be new, but L1 already has
1350 * L2 added to its dependency list, due to the first chain.)
1352 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
1353 if (entry
->class == next
->class) {
1355 entry
->distance
= 1;
1361 * Ok, all validations passed, add the new lock
1362 * to the previous lock's dependency list:
1364 ret
= add_lock_to_list(prev
->class, next
->class,
1365 &prev
->class->locks_after
, next
->acquire_ip
, distance
);
1370 ret
= add_lock_to_list(next
->class, prev
->class,
1371 &next
->class->locks_before
, next
->acquire_ip
, distance
);
1376 * Debugging printouts:
1378 if (verbose(prev
->class) || verbose(next
->class)) {
1380 printk("\n new dependency: ");
1381 print_lock_name(prev
->class);
1383 print_lock_name(next
->class);
1386 return graph_lock();
1392 * Add the dependency to all directly-previous locks that are 'relevant'.
1393 * The ones that are relevant are (in increasing distance from curr):
1394 * all consecutive trylock entries and the final non-trylock entry - or
1395 * the end of this context's lock-chain - whichever comes first.
1398 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1400 int depth
= curr
->lockdep_depth
;
1401 struct held_lock
*hlock
;
1406 * Depth must not be zero for a non-head lock:
1411 * At least two relevant locks must exist for this
1414 if (curr
->held_locks
[depth
].irq_context
!=
1415 curr
->held_locks
[depth
-1].irq_context
)
1419 int distance
= curr
->lockdep_depth
- depth
+ 1;
1420 hlock
= curr
->held_locks
+ depth
-1;
1422 * Only non-recursive-read entries get new dependencies
1425 if (hlock
->read
!= 2) {
1426 if (!check_prev_add(curr
, hlock
, next
, distance
))
1429 * Stop after the first non-trylock entry,
1430 * as non-trylock entries have added their
1431 * own direct dependencies already, so this
1432 * lock is connected to them indirectly:
1434 if (!hlock
->trylock
)
1439 * End of lock-stack?
1444 * Stop the search if we cross into another context:
1446 if (curr
->held_locks
[depth
].irq_context
!=
1447 curr
->held_locks
[depth
-1].irq_context
)
1452 if (!debug_locks_off_graph_unlock())
1460 unsigned long nr_lock_chains
;
1461 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1462 int nr_chain_hlocks
;
1463 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1465 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1467 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1471 * Look up a dependency chain. If the key is not present yet then
1472 * add it and return 1 - in this case the new dependency chain is
1473 * validated. If the key is already hashed, return 0.
1474 * (On return with 1 graph_lock is held.)
1476 static inline int lookup_chain_cache(struct task_struct
*curr
,
1477 struct held_lock
*hlock
,
1480 struct lock_class
*class = hlock
->class;
1481 struct list_head
*hash_head
= chainhashentry(chain_key
);
1482 struct lock_chain
*chain
;
1483 struct held_lock
*hlock_curr
, *hlock_next
;
1486 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1489 * We can walk it lock-free, because entries only get added
1492 list_for_each_entry(chain
, hash_head
, entry
) {
1493 if (chain
->chain_key
== chain_key
) {
1495 debug_atomic_inc(&chain_lookup_hits
);
1496 if (very_verbose(class))
1497 printk("\nhash chain already cached, key: "
1498 "%016Lx tail class: [%p] %s\n",
1499 (unsigned long long)chain_key
,
1500 class->key
, class->name
);
1504 if (very_verbose(class))
1505 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1506 (unsigned long long)chain_key
, class->key
, class->name
);
1508 * Allocate a new chain entry from the static array, and add
1514 * We have to walk the chain again locked - to avoid duplicates:
1516 list_for_each_entry(chain
, hash_head
, entry
) {
1517 if (chain
->chain_key
== chain_key
) {
1522 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1523 if (!debug_locks_off_graph_unlock())
1526 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1527 printk("turning off the locking correctness validator.\n");
1530 chain
= lock_chains
+ nr_lock_chains
++;
1531 chain
->chain_key
= chain_key
;
1532 chain
->irq_context
= hlock
->irq_context
;
1533 /* Find the first held_lock of current chain */
1535 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
1536 hlock_curr
= curr
->held_locks
+ i
;
1537 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
1542 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
1543 cn
= nr_chain_hlocks
;
1544 while (cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
) {
1545 n
= cmpxchg(&nr_chain_hlocks
, cn
, cn
+ chain
->depth
);
1550 if (likely(cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
1552 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
1553 int lock_id
= curr
->held_locks
[i
].class - lock_classes
;
1554 chain_hlocks
[chain
->base
+ j
] = lock_id
;
1556 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
1558 list_add_tail_rcu(&chain
->entry
, hash_head
);
1559 debug_atomic_inc(&chain_lookup_misses
);
1565 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1566 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
1569 * Trylock needs to maintain the stack of held locks, but it
1570 * does not add new dependencies, because trylock can be done
1573 * We look up the chain_key and do the O(N^2) check and update of
1574 * the dependencies only if this is a new dependency chain.
1575 * (If lookup_chain_cache() returns with 1 it acquires
1576 * graph_lock for us)
1578 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1579 lookup_chain_cache(curr
, hlock
, chain_key
)) {
1581 * Check whether last held lock:
1583 * - is irq-safe, if this lock is irq-unsafe
1584 * - is softirq-safe, if this lock is hardirq-unsafe
1586 * And check whether the new lock's dependency graph
1587 * could lead back to the previous lock.
1589 * any of these scenarios could lead to a deadlock. If
1592 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1597 * Mark recursive read, as we jump over it when
1598 * building dependencies (just like we jump over
1604 * Add dependency only if this lock is not the head
1605 * of the chain, and if it's not a secondary read-lock:
1607 if (!chain_head
&& ret
!= 2)
1608 if (!check_prevs_add(curr
, hlock
))
1612 /* after lookup_chain_cache(): */
1613 if (unlikely(!debug_locks
))
1619 static inline int validate_chain(struct task_struct
*curr
,
1620 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1621 int chain_head
, u64 chain_key
)
1628 * We are building curr_chain_key incrementally, so double-check
1629 * it from scratch, to make sure that it's done correctly:
1631 static void check_chain_key(struct task_struct
*curr
)
1633 #ifdef CONFIG_DEBUG_LOCKDEP
1634 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1638 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1639 hlock
= curr
->held_locks
+ i
;
1640 if (chain_key
!= hlock
->prev_chain_key
) {
1642 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1643 curr
->lockdep_depth
, i
,
1644 (unsigned long long)chain_key
,
1645 (unsigned long long)hlock
->prev_chain_key
);
1649 id
= hlock
->class - lock_classes
;
1650 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1653 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1654 hlock
->irq_context
))
1656 chain_key
= iterate_chain_key(chain_key
, id
);
1659 if (chain_key
!= curr
->curr_chain_key
) {
1661 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1662 curr
->lockdep_depth
, i
,
1663 (unsigned long long)chain_key
,
1664 (unsigned long long)curr
->curr_chain_key
);
1671 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1672 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1674 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1677 printk("\n=================================\n");
1678 printk( "[ INFO: inconsistent lock state ]\n");
1679 print_kernel_version();
1680 printk( "---------------------------------\n");
1682 printk("inconsistent {%s} -> {%s} usage.\n",
1683 usage_str
[prev_bit
], usage_str
[new_bit
]);
1685 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1686 curr
->comm
, task_pid_nr(curr
),
1687 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1688 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1689 trace_hardirqs_enabled(curr
),
1690 trace_softirqs_enabled(curr
));
1693 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1694 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1696 print_irqtrace_events(curr
);
1697 printk("\nother info that might help us debug this:\n");
1698 lockdep_print_held_locks(curr
);
1700 printk("\nstack backtrace:\n");
1707 * Print out an error if an invalid bit is set:
1710 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1711 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1713 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1714 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1718 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1719 enum lock_usage_bit new_bit
);
1721 #ifdef CONFIG_TRACE_IRQFLAGS
1724 * print irq inversion bug:
1727 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1728 struct held_lock
*this, int forwards
,
1729 const char *irqclass
)
1731 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1734 printk("\n=========================================================\n");
1735 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1736 print_kernel_version();
1737 printk( "---------------------------------------------------------\n");
1738 printk("%s/%d just changed the state of lock:\n",
1739 curr
->comm
, task_pid_nr(curr
));
1742 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1744 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1745 print_lock_name(other
);
1746 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1748 printk("\nother info that might help us debug this:\n");
1749 lockdep_print_held_locks(curr
);
1751 printk("\nthe first lock's dependencies:\n");
1752 print_lock_dependencies(this->class, 0);
1754 printk("\nthe second lock's dependencies:\n");
1755 print_lock_dependencies(other
, 0);
1757 printk("\nstack backtrace:\n");
1764 * Prove that in the forwards-direction subgraph starting at <this>
1765 * there is no lock matching <mask>:
1768 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1769 enum lock_usage_bit bit
, const char *irqclass
)
1773 find_usage_bit
= bit
;
1774 /* fills in <forwards_match> */
1775 ret
= find_usage_forwards(this->class, 0);
1776 if (!ret
|| ret
== 1)
1779 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1783 * Prove that in the backwards-direction subgraph starting at <this>
1784 * there is no lock matching <mask>:
1787 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1788 enum lock_usage_bit bit
, const char *irqclass
)
1792 find_usage_bit
= bit
;
1793 /* fills in <backwards_match> */
1794 ret
= find_usage_backwards(this->class, 0);
1795 if (!ret
|| ret
== 1)
1798 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1801 void print_irqtrace_events(struct task_struct
*curr
)
1803 printk("irq event stamp: %u\n", curr
->irq_events
);
1804 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1805 print_ip_sym(curr
->hardirq_enable_ip
);
1806 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1807 print_ip_sym(curr
->hardirq_disable_ip
);
1808 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1809 print_ip_sym(curr
->softirq_enable_ip
);
1810 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1811 print_ip_sym(curr
->softirq_disable_ip
);
1814 static int hardirq_verbose(struct lock_class
*class)
1817 return class_filter(class);
1822 static int softirq_verbose(struct lock_class
*class)
1825 return class_filter(class);
1830 #define STRICT_READ_CHECKS 1
1832 static int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
1833 enum lock_usage_bit new_bit
)
1838 case LOCK_USED_IN_HARDIRQ
:
1839 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1841 if (!valid_state(curr
, this, new_bit
,
1842 LOCK_ENABLED_HARDIRQS_READ
))
1845 * just marked it hardirq-safe, check that this lock
1846 * took no hardirq-unsafe lock in the past:
1848 if (!check_usage_forwards(curr
, this,
1849 LOCK_ENABLED_HARDIRQS
, "hard"))
1851 #if STRICT_READ_CHECKS
1853 * just marked it hardirq-safe, check that this lock
1854 * took no hardirq-unsafe-read lock in the past:
1856 if (!check_usage_forwards(curr
, this,
1857 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1860 if (hardirq_verbose(this->class))
1863 case LOCK_USED_IN_SOFTIRQ
:
1864 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1866 if (!valid_state(curr
, this, new_bit
,
1867 LOCK_ENABLED_SOFTIRQS_READ
))
1870 * just marked it softirq-safe, check that this lock
1871 * took no softirq-unsafe lock in the past:
1873 if (!check_usage_forwards(curr
, this,
1874 LOCK_ENABLED_SOFTIRQS
, "soft"))
1876 #if STRICT_READ_CHECKS
1878 * just marked it softirq-safe, check that this lock
1879 * took no softirq-unsafe-read lock in the past:
1881 if (!check_usage_forwards(curr
, this,
1882 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1885 if (softirq_verbose(this->class))
1888 case LOCK_USED_IN_HARDIRQ_READ
:
1889 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1892 * just marked it hardirq-read-safe, check that this lock
1893 * took no hardirq-unsafe lock in the past:
1895 if (!check_usage_forwards(curr
, this,
1896 LOCK_ENABLED_HARDIRQS
, "hard"))
1898 if (hardirq_verbose(this->class))
1901 case LOCK_USED_IN_SOFTIRQ_READ
:
1902 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1905 * just marked it softirq-read-safe, check that this lock
1906 * took no softirq-unsafe lock in the past:
1908 if (!check_usage_forwards(curr
, this,
1909 LOCK_ENABLED_SOFTIRQS
, "soft"))
1911 if (softirq_verbose(this->class))
1914 case LOCK_ENABLED_HARDIRQS
:
1915 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1917 if (!valid_state(curr
, this, new_bit
,
1918 LOCK_USED_IN_HARDIRQ_READ
))
1921 * just marked it hardirq-unsafe, check that no hardirq-safe
1922 * lock in the system ever took it in the past:
1924 if (!check_usage_backwards(curr
, this,
1925 LOCK_USED_IN_HARDIRQ
, "hard"))
1927 #if STRICT_READ_CHECKS
1929 * just marked it hardirq-unsafe, check that no
1930 * hardirq-safe-read lock in the system ever took
1933 if (!check_usage_backwards(curr
, this,
1934 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1937 if (hardirq_verbose(this->class))
1940 case LOCK_ENABLED_SOFTIRQS
:
1941 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1943 if (!valid_state(curr
, this, new_bit
,
1944 LOCK_USED_IN_SOFTIRQ_READ
))
1947 * just marked it softirq-unsafe, check that no softirq-safe
1948 * lock in the system ever took it in the past:
1950 if (!check_usage_backwards(curr
, this,
1951 LOCK_USED_IN_SOFTIRQ
, "soft"))
1953 #if STRICT_READ_CHECKS
1955 * just marked it softirq-unsafe, check that no
1956 * softirq-safe-read lock in the system ever took
1959 if (!check_usage_backwards(curr
, this,
1960 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1963 if (softirq_verbose(this->class))
1966 case LOCK_ENABLED_HARDIRQS_READ
:
1967 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1969 #if STRICT_READ_CHECKS
1971 * just marked it hardirq-read-unsafe, check that no
1972 * hardirq-safe lock in the system ever took it in the past:
1974 if (!check_usage_backwards(curr
, this,
1975 LOCK_USED_IN_HARDIRQ
, "hard"))
1978 if (hardirq_verbose(this->class))
1981 case LOCK_ENABLED_SOFTIRQS_READ
:
1982 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1984 #if STRICT_READ_CHECKS
1986 * just marked it softirq-read-unsafe, check that no
1987 * softirq-safe lock in the system ever took it in the past:
1989 if (!check_usage_backwards(curr
, this,
1990 LOCK_USED_IN_SOFTIRQ
, "soft"))
1993 if (softirq_verbose(this->class))
2005 * Mark all held locks with a usage bit:
2008 mark_held_locks(struct task_struct
*curr
, int hardirq
)
2010 enum lock_usage_bit usage_bit
;
2011 struct held_lock
*hlock
;
2014 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2015 hlock
= curr
->held_locks
+ i
;
2019 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
2021 usage_bit
= LOCK_ENABLED_HARDIRQS
;
2024 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
2026 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
2028 if (!mark_lock(curr
, hlock
, usage_bit
))
2036 * Debugging helper: via this flag we know that we are in
2037 * 'early bootup code', and will warn about any invalid irqs-on event:
2039 static int early_boot_irqs_enabled
;
2041 void early_boot_irqs_off(void)
2043 early_boot_irqs_enabled
= 0;
2046 void early_boot_irqs_on(void)
2048 early_boot_irqs_enabled
= 1;
2052 * Hardirqs will be enabled:
2054 void trace_hardirqs_on(void)
2056 struct task_struct
*curr
= current
;
2059 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2062 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2065 if (unlikely(curr
->hardirqs_enabled
)) {
2066 debug_atomic_inc(&redundant_hardirqs_on
);
2069 /* we'll do an OFF -> ON transition: */
2070 curr
->hardirqs_enabled
= 1;
2071 ip
= (unsigned long) __builtin_return_address(0);
2073 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2075 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2078 * We are going to turn hardirqs on, so set the
2079 * usage bit for all held locks:
2081 if (!mark_held_locks(curr
, 1))
2084 * If we have softirqs enabled, then set the usage
2085 * bit for all held locks. (disabled hardirqs prevented
2086 * this bit from being set before)
2088 if (curr
->softirqs_enabled
)
2089 if (!mark_held_locks(curr
, 0))
2092 curr
->hardirq_enable_ip
= ip
;
2093 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2094 debug_atomic_inc(&hardirqs_on_events
);
2097 EXPORT_SYMBOL(trace_hardirqs_on
);
2100 * Hardirqs were disabled:
2102 void trace_hardirqs_off(void)
2104 struct task_struct
*curr
= current
;
2106 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2109 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2112 if (curr
->hardirqs_enabled
) {
2114 * We have done an ON -> OFF transition:
2116 curr
->hardirqs_enabled
= 0;
2117 curr
->hardirq_disable_ip
= _RET_IP_
;
2118 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2119 debug_atomic_inc(&hardirqs_off_events
);
2121 debug_atomic_inc(&redundant_hardirqs_off
);
2124 EXPORT_SYMBOL(trace_hardirqs_off
);
2127 * Softirqs will be enabled:
2129 void trace_softirqs_on(unsigned long ip
)
2131 struct task_struct
*curr
= current
;
2133 if (unlikely(!debug_locks
))
2136 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2139 if (curr
->softirqs_enabled
) {
2140 debug_atomic_inc(&redundant_softirqs_on
);
2145 * We'll do an OFF -> ON transition:
2147 curr
->softirqs_enabled
= 1;
2148 curr
->softirq_enable_ip
= ip
;
2149 curr
->softirq_enable_event
= ++curr
->irq_events
;
2150 debug_atomic_inc(&softirqs_on_events
);
2152 * We are going to turn softirqs on, so set the
2153 * usage bit for all held locks, if hardirqs are
2156 if (curr
->hardirqs_enabled
)
2157 mark_held_locks(curr
, 0);
2161 * Softirqs were disabled:
2163 void trace_softirqs_off(unsigned long ip
)
2165 struct task_struct
*curr
= current
;
2167 if (unlikely(!debug_locks
))
2170 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2173 if (curr
->softirqs_enabled
) {
2175 * We have done an ON -> OFF transition:
2177 curr
->softirqs_enabled
= 0;
2178 curr
->softirq_disable_ip
= ip
;
2179 curr
->softirq_disable_event
= ++curr
->irq_events
;
2180 debug_atomic_inc(&softirqs_off_events
);
2181 DEBUG_LOCKS_WARN_ON(!softirq_count());
2183 debug_atomic_inc(&redundant_softirqs_off
);
2186 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2189 * If non-trylock use in a hardirq or softirq context, then
2190 * mark the lock as used in these contexts:
2192 if (!hlock
->trylock
) {
2194 if (curr
->hardirq_context
)
2195 if (!mark_lock(curr
, hlock
,
2196 LOCK_USED_IN_HARDIRQ_READ
))
2198 if (curr
->softirq_context
)
2199 if (!mark_lock(curr
, hlock
,
2200 LOCK_USED_IN_SOFTIRQ_READ
))
2203 if (curr
->hardirq_context
)
2204 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2206 if (curr
->softirq_context
)
2207 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2211 if (!hlock
->hardirqs_off
) {
2213 if (!mark_lock(curr
, hlock
,
2214 LOCK_ENABLED_HARDIRQS_READ
))
2216 if (curr
->softirqs_enabled
)
2217 if (!mark_lock(curr
, hlock
,
2218 LOCK_ENABLED_SOFTIRQS_READ
))
2221 if (!mark_lock(curr
, hlock
,
2222 LOCK_ENABLED_HARDIRQS
))
2224 if (curr
->softirqs_enabled
)
2225 if (!mark_lock(curr
, hlock
,
2226 LOCK_ENABLED_SOFTIRQS
))
2234 static int separate_irq_context(struct task_struct
*curr
,
2235 struct held_lock
*hlock
)
2237 unsigned int depth
= curr
->lockdep_depth
;
2240 * Keep track of points where we cross into an interrupt context:
2242 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2243 curr
->softirq_context
;
2245 struct held_lock
*prev_hlock
;
2247 prev_hlock
= curr
->held_locks
+ depth
-1;
2249 * If we cross into another context, reset the
2250 * hash key (this also prevents the checking and the
2251 * adding of the dependency to 'prev'):
2253 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2262 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2263 enum lock_usage_bit new_bit
)
2269 static inline int mark_irqflags(struct task_struct
*curr
,
2270 struct held_lock
*hlock
)
2275 static inline int separate_irq_context(struct task_struct
*curr
,
2276 struct held_lock
*hlock
)
2284 * Mark a lock with a usage bit, and validate the state transition:
2286 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2287 enum lock_usage_bit new_bit
)
2289 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2292 * If already set then do not dirty the cacheline,
2293 * nor do any checks:
2295 if (likely(this->class->usage_mask
& new_mask
))
2301 * Make sure we didnt race:
2303 if (unlikely(this->class->usage_mask
& new_mask
)) {
2308 this->class->usage_mask
|= new_mask
;
2310 if (!save_trace(this->class->usage_traces
+ new_bit
))
2314 case LOCK_USED_IN_HARDIRQ
:
2315 case LOCK_USED_IN_SOFTIRQ
:
2316 case LOCK_USED_IN_HARDIRQ_READ
:
2317 case LOCK_USED_IN_SOFTIRQ_READ
:
2318 case LOCK_ENABLED_HARDIRQS
:
2319 case LOCK_ENABLED_SOFTIRQS
:
2320 case LOCK_ENABLED_HARDIRQS_READ
:
2321 case LOCK_ENABLED_SOFTIRQS_READ
:
2322 ret
= mark_lock_irq(curr
, this, new_bit
);
2327 debug_atomic_dec(&nr_unused_locks
);
2330 if (!debug_locks_off_graph_unlock())
2339 * We must printk outside of the graph_lock:
2342 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2344 print_irqtrace_events(curr
);
2352 * Initialize a lock instance's lock-class mapping info:
2354 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2355 struct lock_class_key
*key
, int subclass
)
2357 if (unlikely(!debug_locks
))
2360 if (DEBUG_LOCKS_WARN_ON(!key
))
2362 if (DEBUG_LOCKS_WARN_ON(!name
))
2365 * Sanity check, the lock-class key must be persistent:
2367 if (!static_obj(key
)) {
2368 printk("BUG: key %p not in .data!\n", key
);
2369 DEBUG_LOCKS_WARN_ON(1);
2374 lock
->class_cache
= NULL
;
2375 #ifdef CONFIG_LOCK_STAT
2376 lock
->cpu
= raw_smp_processor_id();
2379 register_lock_class(lock
, subclass
, 1);
2382 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2385 * This gets called for every mutex_lock*()/spin_lock*() operation.
2386 * We maintain the dependency maps and validate the locking attempt:
2388 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2389 int trylock
, int read
, int check
, int hardirqs_off
,
2392 struct task_struct
*curr
= current
;
2393 struct lock_class
*class = NULL
;
2394 struct held_lock
*hlock
;
2395 unsigned int depth
, id
;
2402 if (unlikely(!debug_locks
))
2405 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2408 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2410 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2411 printk("turning off the locking correctness validator.\n");
2416 class = lock
->class_cache
;
2418 * Not cached yet or subclass?
2420 if (unlikely(!class)) {
2421 class = register_lock_class(lock
, subclass
, 0);
2425 debug_atomic_inc((atomic_t
*)&class->ops
);
2426 if (very_verbose(class)) {
2427 printk("\nacquire class [%p] %s", class->key
, class->name
);
2428 if (class->name_version
> 1)
2429 printk("#%d", class->name_version
);
2435 * Add the lock to the list of currently held locks.
2436 * (we dont increase the depth just yet, up until the
2437 * dependency checks are done)
2439 depth
= curr
->lockdep_depth
;
2440 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2443 hlock
= curr
->held_locks
+ depth
;
2445 hlock
->class = class;
2446 hlock
->acquire_ip
= ip
;
2447 hlock
->instance
= lock
;
2448 hlock
->trylock
= trylock
;
2450 hlock
->check
= check
;
2451 hlock
->hardirqs_off
= hardirqs_off
;
2452 #ifdef CONFIG_LOCK_STAT
2453 hlock
->waittime_stamp
= 0;
2454 hlock
->holdtime_stamp
= sched_clock();
2457 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2460 /* mark it as used: */
2461 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2465 * Calculate the chain hash: it's the combined hash of all the
2466 * lock keys along the dependency chain. We save the hash value
2467 * at every step so that we can get the current hash easily
2468 * after unlock. The chain hash is then used to cache dependency
2471 * The 'key ID' is what is the most compact key value to drive
2472 * the hash, not class->key.
2474 id
= class - lock_classes
;
2475 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2478 chain_key
= curr
->curr_chain_key
;
2480 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2485 hlock
->prev_chain_key
= chain_key
;
2486 if (separate_irq_context(curr
, hlock
)) {
2490 chain_key
= iterate_chain_key(chain_key
, id
);
2492 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
2495 curr
->curr_chain_key
= chain_key
;
2496 curr
->lockdep_depth
++;
2497 check_chain_key(curr
);
2498 #ifdef CONFIG_DEBUG_LOCKDEP
2499 if (unlikely(!debug_locks
))
2502 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2504 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2505 printk("turning off the locking correctness validator.\n");
2509 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2510 max_lockdep_depth
= curr
->lockdep_depth
;
2516 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2519 if (!debug_locks_off())
2521 if (debug_locks_silent
)
2524 printk("\n=====================================\n");
2525 printk( "[ BUG: bad unlock balance detected! ]\n");
2526 printk( "-------------------------------------\n");
2527 printk("%s/%d is trying to release lock (",
2528 curr
->comm
, task_pid_nr(curr
));
2529 print_lockdep_cache(lock
);
2532 printk("but there are no more locks to release!\n");
2533 printk("\nother info that might help us debug this:\n");
2534 lockdep_print_held_locks(curr
);
2536 printk("\nstack backtrace:\n");
2543 * Common debugging checks for both nested and non-nested unlock:
2545 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2548 if (unlikely(!debug_locks
))
2550 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2553 if (curr
->lockdep_depth
<= 0)
2554 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2560 * Remove the lock to the list of currently held locks in a
2561 * potentially non-nested (out of order) manner. This is a
2562 * relatively rare operation, as all the unlock APIs default
2563 * to nested mode (which uses lock_release()):
2566 lock_release_non_nested(struct task_struct
*curr
,
2567 struct lockdep_map
*lock
, unsigned long ip
)
2569 struct held_lock
*hlock
, *prev_hlock
;
2574 * Check whether the lock exists in the current stack
2577 depth
= curr
->lockdep_depth
;
2578 if (DEBUG_LOCKS_WARN_ON(!depth
))
2582 for (i
= depth
-1; i
>= 0; i
--) {
2583 hlock
= curr
->held_locks
+ i
;
2585 * We must not cross into another context:
2587 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2589 if (hlock
->instance
== lock
)
2593 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2596 lock_release_holdtime(hlock
);
2599 * We have the right lock to unlock, 'hlock' points to it.
2600 * Now we remove it from the stack, and add back the other
2601 * entries (if any), recalculating the hash along the way:
2603 curr
->lockdep_depth
= i
;
2604 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2606 for (i
++; i
< depth
; i
++) {
2607 hlock
= curr
->held_locks
+ i
;
2608 if (!__lock_acquire(hlock
->instance
,
2609 hlock
->class->subclass
, hlock
->trylock
,
2610 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2615 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2621 * Remove the lock to the list of currently held locks - this gets
2622 * called on mutex_unlock()/spin_unlock*() (or on a failed
2623 * mutex_lock_interruptible()). This is done for unlocks that nest
2624 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2626 static int lock_release_nested(struct task_struct
*curr
,
2627 struct lockdep_map
*lock
, unsigned long ip
)
2629 struct held_lock
*hlock
;
2633 * Pop off the top of the lock stack:
2635 depth
= curr
->lockdep_depth
- 1;
2636 hlock
= curr
->held_locks
+ depth
;
2639 * Is the unlock non-nested:
2641 if (hlock
->instance
!= lock
)
2642 return lock_release_non_nested(curr
, lock
, ip
);
2643 curr
->lockdep_depth
--;
2645 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2648 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2650 lock_release_holdtime(hlock
);
2652 #ifdef CONFIG_DEBUG_LOCKDEP
2653 hlock
->prev_chain_key
= 0;
2654 hlock
->class = NULL
;
2655 hlock
->acquire_ip
= 0;
2656 hlock
->irq_context
= 0;
2662 * Remove the lock to the list of currently held locks - this gets
2663 * called on mutex_unlock()/spin_unlock*() (or on a failed
2664 * mutex_lock_interruptible()). This is done for unlocks that nest
2665 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2668 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2670 struct task_struct
*curr
= current
;
2672 if (!check_unlock(curr
, lock
, ip
))
2676 if (!lock_release_nested(curr
, lock
, ip
))
2679 if (!lock_release_non_nested(curr
, lock
, ip
))
2683 check_chain_key(curr
);
2687 * Check whether we follow the irq-flags state precisely:
2689 static void check_flags(unsigned long flags
)
2691 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2695 if (irqs_disabled_flags(flags
)) {
2696 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
2697 printk("possible reason: unannotated irqs-off.\n");
2700 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
2701 printk("possible reason: unannotated irqs-on.\n");
2706 * We dont accurately track softirq state in e.g.
2707 * hardirq contexts (such as on 4KSTACKS), so only
2708 * check if not in hardirq contexts:
2710 if (!hardirq_count()) {
2711 if (softirq_count())
2712 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2714 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2718 print_irqtrace_events(current
);
2723 * We are not always called with irqs disabled - do that here,
2724 * and also avoid lockdep recursion:
2726 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2727 int trylock
, int read
, int check
, unsigned long ip
)
2729 unsigned long flags
;
2731 if (unlikely(!lock_stat
&& !prove_locking
))
2734 if (unlikely(current
->lockdep_recursion
))
2737 raw_local_irq_save(flags
);
2740 current
->lockdep_recursion
= 1;
2741 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2742 irqs_disabled_flags(flags
), ip
);
2743 current
->lockdep_recursion
= 0;
2744 raw_local_irq_restore(flags
);
2747 EXPORT_SYMBOL_GPL(lock_acquire
);
2749 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2751 unsigned long flags
;
2753 if (unlikely(!lock_stat
&& !prove_locking
))
2756 if (unlikely(current
->lockdep_recursion
))
2759 raw_local_irq_save(flags
);
2761 current
->lockdep_recursion
= 1;
2762 __lock_release(lock
, nested
, ip
);
2763 current
->lockdep_recursion
= 0;
2764 raw_local_irq_restore(flags
);
2767 EXPORT_SYMBOL_GPL(lock_release
);
2769 #ifdef CONFIG_LOCK_STAT
2771 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2774 if (!debug_locks_off())
2776 if (debug_locks_silent
)
2779 printk("\n=================================\n");
2780 printk( "[ BUG: bad contention detected! ]\n");
2781 printk( "---------------------------------\n");
2782 printk("%s/%d is trying to contend lock (",
2783 curr
->comm
, task_pid_nr(curr
));
2784 print_lockdep_cache(lock
);
2787 printk("but there are no locks held!\n");
2788 printk("\nother info that might help us debug this:\n");
2789 lockdep_print_held_locks(curr
);
2791 printk("\nstack backtrace:\n");
2798 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2800 struct task_struct
*curr
= current
;
2801 struct held_lock
*hlock
, *prev_hlock
;
2802 struct lock_class_stats
*stats
;
2806 depth
= curr
->lockdep_depth
;
2807 if (DEBUG_LOCKS_WARN_ON(!depth
))
2811 for (i
= depth
-1; i
>= 0; i
--) {
2812 hlock
= curr
->held_locks
+ i
;
2814 * We must not cross into another context:
2816 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2818 if (hlock
->instance
== lock
)
2822 print_lock_contention_bug(curr
, lock
, ip
);
2826 hlock
->waittime_stamp
= sched_clock();
2828 point
= lock_contention_point(hlock
->class, ip
);
2830 stats
= get_lock_stats(hlock
->class);
2831 if (point
< ARRAY_SIZE(stats
->contention_point
))
2832 stats
->contention_point
[i
]++;
2833 if (lock
->cpu
!= smp_processor_id())
2834 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
2835 put_lock_stats(stats
);
2839 __lock_acquired(struct lockdep_map
*lock
)
2841 struct task_struct
*curr
= current
;
2842 struct held_lock
*hlock
, *prev_hlock
;
2843 struct lock_class_stats
*stats
;
2849 depth
= curr
->lockdep_depth
;
2850 if (DEBUG_LOCKS_WARN_ON(!depth
))
2854 for (i
= depth
-1; i
>= 0; i
--) {
2855 hlock
= curr
->held_locks
+ i
;
2857 * We must not cross into another context:
2859 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2861 if (hlock
->instance
== lock
)
2865 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
2869 cpu
= smp_processor_id();
2870 if (hlock
->waittime_stamp
) {
2871 now
= sched_clock();
2872 waittime
= now
- hlock
->waittime_stamp
;
2873 hlock
->holdtime_stamp
= now
;
2876 stats
= get_lock_stats(hlock
->class);
2879 lock_time_inc(&stats
->read_waittime
, waittime
);
2881 lock_time_inc(&stats
->write_waittime
, waittime
);
2883 if (lock
->cpu
!= cpu
)
2884 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
2885 put_lock_stats(stats
);
2890 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2892 unsigned long flags
;
2894 if (unlikely(!lock_stat
))
2897 if (unlikely(current
->lockdep_recursion
))
2900 raw_local_irq_save(flags
);
2902 current
->lockdep_recursion
= 1;
2903 __lock_contended(lock
, ip
);
2904 current
->lockdep_recursion
= 0;
2905 raw_local_irq_restore(flags
);
2907 EXPORT_SYMBOL_GPL(lock_contended
);
2909 void lock_acquired(struct lockdep_map
*lock
)
2911 unsigned long flags
;
2913 if (unlikely(!lock_stat
))
2916 if (unlikely(current
->lockdep_recursion
))
2919 raw_local_irq_save(flags
);
2921 current
->lockdep_recursion
= 1;
2922 __lock_acquired(lock
);
2923 current
->lockdep_recursion
= 0;
2924 raw_local_irq_restore(flags
);
2926 EXPORT_SYMBOL_GPL(lock_acquired
);
2930 * Used by the testsuite, sanitize the validator state
2931 * after a simulated failure:
2934 void lockdep_reset(void)
2936 unsigned long flags
;
2939 raw_local_irq_save(flags
);
2940 current
->curr_chain_key
= 0;
2941 current
->lockdep_depth
= 0;
2942 current
->lockdep_recursion
= 0;
2943 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2944 nr_hardirq_chains
= 0;
2945 nr_softirq_chains
= 0;
2946 nr_process_chains
= 0;
2948 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2949 INIT_LIST_HEAD(chainhash_table
+ i
);
2950 raw_local_irq_restore(flags
);
2953 static void zap_class(struct lock_class
*class)
2958 * Remove all dependencies this lock is
2961 for (i
= 0; i
< nr_list_entries
; i
++) {
2962 if (list_entries
[i
].class == class)
2963 list_del_rcu(&list_entries
[i
].entry
);
2966 * Unhash the class and remove it from the all_lock_classes list:
2968 list_del_rcu(&class->hash_entry
);
2969 list_del_rcu(&class->lock_entry
);
2973 static inline int within(const void *addr
, void *start
, unsigned long size
)
2975 return addr
>= start
&& addr
< start
+ size
;
2978 void lockdep_free_key_range(void *start
, unsigned long size
)
2980 struct lock_class
*class, *next
;
2981 struct list_head
*head
;
2982 unsigned long flags
;
2986 raw_local_irq_save(flags
);
2987 locked
= graph_lock();
2990 * Unhash all classes that were created by this module:
2992 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2993 head
= classhash_table
+ i
;
2994 if (list_empty(head
))
2996 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2997 if (within(class->key
, start
, size
))
2999 else if (within(class->name
, start
, size
))
3006 raw_local_irq_restore(flags
);
3009 void lockdep_reset_lock(struct lockdep_map
*lock
)
3011 struct lock_class
*class, *next
;
3012 struct list_head
*head
;
3013 unsigned long flags
;
3017 raw_local_irq_save(flags
);
3020 * Remove all classes this lock might have:
3022 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3024 * If the class exists we look it up and zap it:
3026 class = look_up_lock_class(lock
, j
);
3031 * Debug check: in the end all mapped classes should
3034 locked
= graph_lock();
3035 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3036 head
= classhash_table
+ i
;
3037 if (list_empty(head
))
3039 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3040 if (unlikely(class == lock
->class_cache
)) {
3041 if (debug_locks_off_graph_unlock())
3051 raw_local_irq_restore(flags
);
3054 void lockdep_init(void)
3059 * Some architectures have their own start_kernel()
3060 * code which calls lockdep_init(), while we also
3061 * call lockdep_init() from the start_kernel() itself,
3062 * and we want to initialize the hashes only once:
3064 if (lockdep_initialized
)
3067 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3068 INIT_LIST_HEAD(classhash_table
+ i
);
3070 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3071 INIT_LIST_HEAD(chainhash_table
+ i
);
3073 lockdep_initialized
= 1;
3076 void __init
lockdep_info(void)
3078 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3080 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3081 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3082 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3083 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3084 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3085 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3086 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3088 printk(" memory used by lock dependency info: %lu kB\n",
3089 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3090 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3091 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3092 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3093 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
3095 printk(" per task-struct memory footprint: %lu bytes\n",
3096 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3098 #ifdef CONFIG_DEBUG_LOCKDEP
3099 if (lockdep_init_error
) {
3100 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3101 printk("Call stack leading to lockdep invocation was:\n");
3102 print_stack_trace(&lockdep_init_trace
, 0);
3108 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3109 const void *mem_to
, struct held_lock
*hlock
)
3111 if (!debug_locks_off())
3113 if (debug_locks_silent
)
3116 printk("\n=========================\n");
3117 printk( "[ BUG: held lock freed! ]\n");
3118 printk( "-------------------------\n");
3119 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3120 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3122 lockdep_print_held_locks(curr
);
3124 printk("\nstack backtrace:\n");
3128 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3129 const void* lock_from
, unsigned long lock_len
)
3131 return lock_from
+ lock_len
<= mem_from
||
3132 mem_from
+ mem_len
<= lock_from
;
3136 * Called when kernel memory is freed (or unmapped), or if a lock
3137 * is destroyed or reinitialized - this code checks whether there is
3138 * any held lock in the memory range of <from> to <to>:
3140 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3142 struct task_struct
*curr
= current
;
3143 struct held_lock
*hlock
;
3144 unsigned long flags
;
3147 if (unlikely(!debug_locks
))
3150 local_irq_save(flags
);
3151 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3152 hlock
= curr
->held_locks
+ i
;
3154 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3155 sizeof(*hlock
->instance
)))
3158 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3161 local_irq_restore(flags
);
3163 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3165 static void print_held_locks_bug(struct task_struct
*curr
)
3167 if (!debug_locks_off())
3169 if (debug_locks_silent
)
3172 printk("\n=====================================\n");
3173 printk( "[ BUG: lock held at task exit time! ]\n");
3174 printk( "-------------------------------------\n");
3175 printk("%s/%d is exiting with locks still held!\n",
3176 curr
->comm
, task_pid_nr(curr
));
3177 lockdep_print_held_locks(curr
);
3179 printk("\nstack backtrace:\n");
3183 void debug_check_no_locks_held(struct task_struct
*task
)
3185 if (unlikely(task
->lockdep_depth
> 0))
3186 print_held_locks_bug(task
);
3189 void debug_show_all_locks(void)
3191 struct task_struct
*g
, *p
;
3195 if (unlikely(!debug_locks
)) {
3196 printk("INFO: lockdep is turned off.\n");
3199 printk("\nShowing all locks held in the system:\n");
3202 * Here we try to get the tasklist_lock as hard as possible,
3203 * if not successful after 2 seconds we ignore it (but keep
3204 * trying). This is to enable a debug printout even if a
3205 * tasklist_lock-holding task deadlocks or crashes.
3208 if (!read_trylock(&tasklist_lock
)) {
3210 printk("hm, tasklist_lock locked, retrying... ");
3213 printk(" #%d", 10-count
);
3217 printk(" ignoring it.\n");
3221 printk(" locked it.\n");
3223 do_each_thread(g
, p
) {
3225 * It's not reliable to print a task's held locks
3226 * if it's not sleeping (or if it's not the current
3229 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3231 if (p
->lockdep_depth
)
3232 lockdep_print_held_locks(p
);
3234 if (read_trylock(&tasklist_lock
))
3236 } while_each_thread(g
, p
);
3239 printk("=============================================\n\n");
3242 read_unlock(&tasklist_lock
);
3245 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3248 * Careful: only use this function if you are sure that
3249 * the task cannot run in parallel!
3251 void __debug_show_held_locks(struct task_struct
*task
)
3253 if (unlikely(!debug_locks
)) {
3254 printk("INFO: lockdep is turned off.\n");
3257 lockdep_print_held_locks(task
);
3259 EXPORT_SYMBOL_GPL(__debug_show_held_locks
);
3261 void debug_show_held_locks(struct task_struct
*task
)
3263 __debug_show_held_locks(task
);
3266 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3268 void lockdep_sys_exit(void)
3270 struct task_struct
*curr
= current
;
3272 if (unlikely(curr
->lockdep_depth
)) {
3273 if (!debug_locks_off())
3275 printk("\n================================================\n");
3276 printk( "[ BUG: lock held when returning to user space! ]\n");
3277 printk( "------------------------------------------------\n");
3278 printk("%s/%d is leaving the kernel with locks still held!\n",
3279 curr
->comm
, curr
->pid
);
3280 lockdep_print_held_locks(curr
);