4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10 * this code maps all the lock dependencies as they occur in a live kernel
11 * and will warn about the following classes of locking bugs:
13 * - lock inversion scenarios
14 * - circular lock dependencies
15 * - hardirq/softirq safe/unsafe locking bugs
17 * Bugs are reported even if the current locking scenario does not cause
18 * any deadlock at this point.
20 * I.e. if anytime in the past two locks were taken in a different order,
21 * even if it happened for another task, even if those were different
22 * locks (but of the same class as this lock), this code will detect it.
24 * Thanks to Arjan van de Ven for coming up with the initial idea of
25 * mapping lock dependencies runtime.
27 #include <linux/mutex.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/spinlock.h>
34 #include <linux/kallsyms.h>
35 #include <linux/interrupt.h>
36 #include <linux/stacktrace.h>
37 #include <linux/debug_locks.h>
38 #include <linux/irqflags.h>
39 #include <linux/utsname.h>
41 #include <asm/sections.h>
43 #include "lockdep_internals.h"
45 #ifdef CONFIG_PROVE_LOCKING
46 int prove_locking
= 1;
47 module_param(prove_locking
, int, 0644);
49 #define prove_locking 0
52 #ifdef CONFIG_LOCK_STAT
54 module_param(lock_stat
, int, 0644);
60 * lockdep_lock: protects the lockdep graph, the hashes and the
61 * class/list/hash allocators.
63 * This is one of the rare exceptions where it's justified
64 * to use a raw spinlock - we really dont want the spinlock
65 * code to recurse back into the lockdep code...
67 static raw_spinlock_t lockdep_lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
69 static int graph_lock(void)
71 __raw_spin_lock(&lockdep_lock
);
73 * Make sure that if another CPU detected a bug while
74 * walking the graph we dont change it (while the other
75 * CPU is busy printing out stuff with the graph lock
79 __raw_spin_unlock(&lockdep_lock
);
85 static inline int graph_unlock(void)
87 if (debug_locks
&& !__raw_spin_is_locked(&lockdep_lock
))
88 return DEBUG_LOCKS_WARN_ON(1);
90 __raw_spin_unlock(&lockdep_lock
);
95 * Turn lock debugging off and return with 0 if it was off already,
96 * and also release the graph lock:
98 static inline int debug_locks_off_graph_unlock(void)
100 int ret
= debug_locks_off();
102 __raw_spin_unlock(&lockdep_lock
);
107 static int lockdep_initialized
;
109 unsigned long nr_list_entries
;
110 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
113 * All data structures here are protected by the global debug_lock.
115 * Mutex key structs only get allocated, once during bootup, and never
116 * get freed - this significantly simplifies the debugging code.
118 unsigned long nr_lock_classes
;
119 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
121 #ifdef CONFIG_LOCK_STAT
122 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], lock_stats
);
124 static int lock_contention_point(struct lock_class
*class, unsigned long ip
)
128 for (i
= 0; i
< ARRAY_SIZE(class->contention_point
); i
++) {
129 if (class->contention_point
[i
] == 0) {
130 class->contention_point
[i
] = ip
;
133 if (class->contention_point
[i
] == ip
)
140 static void lock_time_inc(struct lock_time
*lt
, s64 time
)
145 if (time
< lt
->min
|| !lt
->min
)
152 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
154 return &get_cpu_var(lock_stats
)[class - lock_classes
];
157 static void put_lock_stats(struct lock_class_stats
*stats
)
159 put_cpu_var(lock_stats
);
162 static void lock_release_holdtime(struct held_lock
*hlock
)
164 struct lock_class_stats
*stats
;
170 holdtime
= sched_clock() - hlock
->holdtime_stamp
;
172 stats
= get_lock_stats(hlock
->class);
174 lock_time_inc(&stats
->read_holdtime
, holdtime
);
176 lock_time_inc(&stats
->write_holdtime
, holdtime
);
177 put_lock_stats(stats
);
180 static inline void lock_release_holdtime(struct held_lock
*hlock
)
186 * We keep a global list of all lock classes. The list only grows,
187 * never shrinks. The list is only accessed with the lockdep
188 * spinlock lock held.
190 LIST_HEAD(all_lock_classes
);
193 * The lockdep classes are in a hash-table as well, for fast lookup:
195 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
196 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
197 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
198 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
199 #define classhashentry(key) (classhash_table + __classhashfn((key)))
201 static struct list_head classhash_table
[CLASSHASH_SIZE
];
204 * We put the lock dependency chains into a hash-table as well, to cache
207 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
208 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
209 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
210 #define __chainhashfn(chain) \
211 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
212 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
214 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
217 * The hash key of the lock dependency chains is a hash itself too:
218 * it's a hash of all locks taken up to that lock, including that lock.
219 * It's a 64-bit hash, because it's important for the keys to be
222 #define iterate_chain_key(key1, key2) \
223 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
224 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
227 void lockdep_off(void)
229 current
->lockdep_recursion
++;
232 EXPORT_SYMBOL(lockdep_off
);
234 void lockdep_on(void)
236 current
->lockdep_recursion
--;
239 EXPORT_SYMBOL(lockdep_on
);
242 * Debugging switches:
246 #define VERY_VERBOSE 0
249 # define HARDIRQ_VERBOSE 1
250 # define SOFTIRQ_VERBOSE 1
252 # define HARDIRQ_VERBOSE 0
253 # define SOFTIRQ_VERBOSE 0
256 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
258 * Quick filtering for interesting events:
260 static int class_filter(struct lock_class
*class)
264 if (class->name_version
== 1 &&
265 !strcmp(class->name
, "lockname"))
267 if (class->name_version
== 1 &&
268 !strcmp(class->name
, "&struct->lockfield"))
271 /* Filter everything else. 1 would be to allow everything else */
276 static int verbose(struct lock_class
*class)
279 return class_filter(class);
285 * Stack-trace: tightly packed array of stack backtrace
286 * addresses. Protected by the graph_lock.
288 unsigned long nr_stack_trace_entries
;
289 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
291 static int save_trace(struct stack_trace
*trace
)
293 trace
->nr_entries
= 0;
294 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
295 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
299 save_stack_trace(trace
);
301 trace
->max_entries
= trace
->nr_entries
;
303 nr_stack_trace_entries
+= trace
->nr_entries
;
305 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
306 if (!debug_locks_off_graph_unlock())
309 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
310 printk("turning off the locking correctness validator.\n");
319 unsigned int nr_hardirq_chains
;
320 unsigned int nr_softirq_chains
;
321 unsigned int nr_process_chains
;
322 unsigned int max_lockdep_depth
;
323 unsigned int max_recursion_depth
;
325 #ifdef CONFIG_DEBUG_LOCKDEP
327 * We cannot printk in early bootup code. Not even early_printk()
328 * might work. So we mark any initialization errors and printk
329 * about it later on, in lockdep_info().
331 static int lockdep_init_error
;
334 * Various lockdep statistics:
336 atomic_t chain_lookup_hits
;
337 atomic_t chain_lookup_misses
;
338 atomic_t hardirqs_on_events
;
339 atomic_t hardirqs_off_events
;
340 atomic_t redundant_hardirqs_on
;
341 atomic_t redundant_hardirqs_off
;
342 atomic_t softirqs_on_events
;
343 atomic_t softirqs_off_events
;
344 atomic_t redundant_softirqs_on
;
345 atomic_t redundant_softirqs_off
;
346 atomic_t nr_unused_locks
;
347 atomic_t nr_cyclic_checks
;
348 atomic_t nr_cyclic_check_recursions
;
349 atomic_t nr_find_usage_forwards_checks
;
350 atomic_t nr_find_usage_forwards_recursions
;
351 atomic_t nr_find_usage_backwards_checks
;
352 atomic_t nr_find_usage_backwards_recursions
;
353 # define debug_atomic_inc(ptr) atomic_inc(ptr)
354 # define debug_atomic_dec(ptr) atomic_dec(ptr)
355 # define debug_atomic_read(ptr) atomic_read(ptr)
357 # define debug_atomic_inc(ptr) do { } while (0)
358 # define debug_atomic_dec(ptr) do { } while (0)
359 # define debug_atomic_read(ptr) 0
366 static const char *usage_str
[] =
368 [LOCK_USED
] = "initial-use ",
369 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
370 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
371 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
372 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
373 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
374 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
375 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
376 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
379 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
381 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
385 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
387 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
389 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
392 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
395 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
398 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
401 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
403 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
405 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
409 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
411 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
413 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
418 static void print_lock_name(struct lock_class
*class)
420 char str
[KSYM_NAME_LEN
], c1
, c2
, c3
, c4
;
423 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
427 name
= __get_key_name(class->key
, str
);
428 printk(" (%s", name
);
430 printk(" (%s", name
);
431 if (class->name_version
> 1)
432 printk("#%d", class->name_version
);
434 printk("/%d", class->subclass
);
436 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
439 static void print_lockdep_cache(struct lockdep_map
*lock
)
442 char str
[KSYM_NAME_LEN
];
446 name
= __get_key_name(lock
->key
->subkeys
, str
);
451 static void print_lock(struct held_lock
*hlock
)
453 print_lock_name(hlock
->class);
455 print_ip_sym(hlock
->acquire_ip
);
458 static void lockdep_print_held_locks(struct task_struct
*curr
)
460 int i
, depth
= curr
->lockdep_depth
;
463 printk("no locks held by %s/%d.\n", curr
->comm
, curr
->pid
);
466 printk("%d lock%s held by %s/%d:\n",
467 depth
, depth
> 1 ? "s" : "", curr
->comm
, curr
->pid
);
469 for (i
= 0; i
< depth
; i
++) {
471 print_lock(curr
->held_locks
+ i
);
475 static void print_lock_class_header(struct lock_class
*class, int depth
)
479 printk("%*s->", depth
, "");
480 print_lock_name(class);
481 printk(" ops: %lu", class->ops
);
484 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
485 if (class->usage_mask
& (1 << bit
)) {
488 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
489 len
+= printk(" at:\n");
490 print_stack_trace(class->usage_traces
+ bit
, len
);
493 printk("%*s }\n", depth
, "");
495 printk("%*s ... key at: ",depth
,"");
496 print_ip_sym((unsigned long)class->key
);
500 * printk all lock dependencies starting at <entry>:
502 static void print_lock_dependencies(struct lock_class
*class, int depth
)
504 struct lock_list
*entry
;
506 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
509 print_lock_class_header(class, depth
);
511 list_for_each_entry(entry
, &class->locks_after
, entry
) {
512 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
515 print_lock_dependencies(entry
->class, depth
+ 1);
517 printk("%*s ... acquired at:\n",depth
,"");
518 print_stack_trace(&entry
->trace
, 2);
523 static void print_kernel_version(void)
525 printk("%s %.*s\n", init_utsname()->release
,
526 (int)strcspn(init_utsname()->version
, " "),
527 init_utsname()->version
);
530 static int very_verbose(struct lock_class
*class)
533 return class_filter(class);
539 * Is this the address of a static object:
541 static int static_obj(void *obj
)
543 unsigned long start
= (unsigned long) &_stext
,
544 end
= (unsigned long) &_end
,
545 addr
= (unsigned long) obj
;
553 if ((addr
>= start
) && (addr
< end
))
560 for_each_possible_cpu(i
) {
561 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
562 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
565 if ((addr
>= start
) && (addr
< end
))
573 return is_module_address(addr
);
577 * To make lock name printouts unique, we calculate a unique
578 * class->name_version generation counter:
580 static int count_matching_names(struct lock_class
*new_class
)
582 struct lock_class
*class;
585 if (!new_class
->name
)
588 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
589 if (new_class
->key
- new_class
->subclass
== class->key
)
590 return class->name_version
;
591 if (class->name
&& !strcmp(class->name
, new_class
->name
))
592 count
= max(count
, class->name_version
);
599 * Register a lock's class in the hash-table, if the class is not present
600 * yet. Otherwise we look it up. We cache the result in the lock object
601 * itself, so actual lookup of the hash should be once per lock object.
603 static inline struct lock_class
*
604 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
606 struct lockdep_subclass_key
*key
;
607 struct list_head
*hash_head
;
608 struct lock_class
*class;
610 #ifdef CONFIG_DEBUG_LOCKDEP
612 * If the architecture calls into lockdep before initializing
613 * the hashes then we'll warn about it later. (we cannot printk
616 if (unlikely(!lockdep_initialized
)) {
618 lockdep_init_error
= 1;
623 * Static locks do not have their class-keys yet - for them the key
624 * is the lock object itself:
626 if (unlikely(!lock
->key
))
627 lock
->key
= (void *)lock
;
630 * NOTE: the class-key must be unique. For dynamic locks, a static
631 * lock_class_key variable is passed in through the mutex_init()
632 * (or spin_lock_init()) call - which acts as the key. For static
633 * locks we use the lock object itself as the key.
635 BUILD_BUG_ON(sizeof(struct lock_class_key
) > sizeof(struct lock_class
));
637 key
= lock
->key
->subkeys
+ subclass
;
639 hash_head
= classhashentry(key
);
642 * We can walk the hash lockfree, because the hash only
643 * grows, and we are careful when adding entries to the end:
645 list_for_each_entry(class, hash_head
, hash_entry
)
646 if (class->key
== key
)
653 * Register a lock's class in the hash-table, if the class is not present
654 * yet. Otherwise we look it up. We cache the result in the lock object
655 * itself, so actual lookup of the hash should be once per lock object.
657 static inline struct lock_class
*
658 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
660 struct lockdep_subclass_key
*key
;
661 struct list_head
*hash_head
;
662 struct lock_class
*class;
665 class = look_up_lock_class(lock
, subclass
);
670 * Debug-check: all keys must be persistent!
672 if (!static_obj(lock
->key
)) {
674 printk("INFO: trying to register non-static key.\n");
675 printk("the code is fine but needs lockdep annotation.\n");
676 printk("turning off the locking correctness validator.\n");
682 key
= lock
->key
->subkeys
+ subclass
;
683 hash_head
= classhashentry(key
);
685 raw_local_irq_save(flags
);
687 raw_local_irq_restore(flags
);
691 * We have to do the hash-walk again, to avoid races
694 list_for_each_entry(class, hash_head
, hash_entry
)
695 if (class->key
== key
)
698 * Allocate a new key from the static array, and add it to
701 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
702 if (!debug_locks_off_graph_unlock()) {
703 raw_local_irq_restore(flags
);
706 raw_local_irq_restore(flags
);
708 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
709 printk("turning off the locking correctness validator.\n");
712 class = lock_classes
+ nr_lock_classes
++;
713 debug_atomic_inc(&nr_unused_locks
);
715 class->name
= lock
->name
;
716 class->subclass
= subclass
;
717 INIT_LIST_HEAD(&class->lock_entry
);
718 INIT_LIST_HEAD(&class->locks_before
);
719 INIT_LIST_HEAD(&class->locks_after
);
720 class->name_version
= count_matching_names(class);
722 * We use RCU's safe list-add method to make
723 * parallel walking of the hash-list safe:
725 list_add_tail_rcu(&class->hash_entry
, hash_head
);
727 if (verbose(class)) {
729 raw_local_irq_restore(flags
);
731 printk("\nnew class %p: %s", class->key
, class->name
);
732 if (class->name_version
> 1)
733 printk("#%d", class->name_version
);
737 raw_local_irq_save(flags
);
739 raw_local_irq_restore(flags
);
745 raw_local_irq_restore(flags
);
747 if (!subclass
|| force
)
748 lock
->class_cache
= class;
750 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
756 #ifdef CONFIG_PROVE_LOCKING
758 * Allocate a lockdep entry. (assumes the graph_lock held, returns
759 * with NULL on failure)
761 static struct lock_list
*alloc_list_entry(void)
763 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
764 if (!debug_locks_off_graph_unlock())
767 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
768 printk("turning off the locking correctness validator.\n");
771 return list_entries
+ nr_list_entries
++;
775 * Add a new dependency to the head of the list:
777 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
778 struct list_head
*head
, unsigned long ip
, int distance
)
780 struct lock_list
*entry
;
782 * Lock not present yet - get a new dependency struct and
783 * add it to the list:
785 entry
= alloc_list_entry();
790 entry
->distance
= distance
;
791 if (!save_trace(&entry
->trace
))
795 * Since we never remove from the dependency list, the list can
796 * be walked lockless by other CPUs, it's only allocation
797 * that must be protected by the spinlock. But this also means
798 * we must make new entries visible only once writes to the
799 * entry become visible - hence the RCU op:
801 list_add_tail_rcu(&entry
->entry
, head
);
807 * Recursive, forwards-direction lock-dependency checking, used for
808 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
811 * (to keep the stackframe of the recursive functions small we
812 * use these global variables, and we also mark various helper
813 * functions as noinline.)
815 static struct held_lock
*check_source
, *check_target
;
818 * Print a dependency chain entry (this is only done when a deadlock
819 * has been detected):
822 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
824 if (debug_locks_silent
)
826 printk("\n-> #%u", depth
);
827 print_lock_name(target
->class);
829 print_stack_trace(&target
->trace
, 6);
835 * When a circular dependency is detected, print the
839 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
841 struct task_struct
*curr
= current
;
843 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
846 printk("\n=======================================================\n");
847 printk( "[ INFO: possible circular locking dependency detected ]\n");
848 print_kernel_version();
849 printk( "-------------------------------------------------------\n");
850 printk("%s/%d is trying to acquire lock:\n",
851 curr
->comm
, curr
->pid
);
852 print_lock(check_source
);
853 printk("\nbut task is already holding lock:\n");
854 print_lock(check_target
);
855 printk("\nwhich lock already depends on the new lock.\n\n");
856 printk("\nthe existing dependency chain (in reverse order) is:\n");
858 print_circular_bug_entry(entry
, depth
);
863 static noinline
int print_circular_bug_tail(void)
865 struct task_struct
*curr
= current
;
866 struct lock_list
this;
868 if (debug_locks_silent
)
871 this.class = check_source
->class;
872 if (!save_trace(&this.trace
))
875 print_circular_bug_entry(&this, 0);
877 printk("\nother info that might help us debug this:\n\n");
878 lockdep_print_held_locks(curr
);
880 printk("\nstack backtrace:\n");
886 #define RECURSION_LIMIT 40
888 static int noinline
print_infinite_recursion_bug(void)
890 if (!debug_locks_off_graph_unlock())
899 * Prove that the dependency graph starting at <entry> can not
900 * lead to <target>. Print an error and return 0 if it does.
903 check_noncircular(struct lock_class
*source
, unsigned int depth
)
905 struct lock_list
*entry
;
907 debug_atomic_inc(&nr_cyclic_check_recursions
);
908 if (depth
> max_recursion_depth
)
909 max_recursion_depth
= depth
;
910 if (depth
>= RECURSION_LIMIT
)
911 return print_infinite_recursion_bug();
913 * Check this lock's dependency list:
915 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
916 if (entry
->class == check_target
->class)
917 return print_circular_bug_header(entry
, depth
+1);
918 debug_atomic_inc(&nr_cyclic_checks
);
919 if (!check_noncircular(entry
->class, depth
+1))
920 return print_circular_bug_entry(entry
, depth
+1);
925 #ifdef CONFIG_TRACE_IRQFLAGS
927 * Forwards and backwards subgraph searching, for the purposes of
928 * proving that two subgraphs can be connected by a new dependency
929 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
931 static enum lock_usage_bit find_usage_bit
;
932 static struct lock_class
*forwards_match
, *backwards_match
;
935 * Find a node in the forwards-direction dependency sub-graph starting
936 * at <source> that matches <find_usage_bit>.
938 * Return 2 if such a node exists in the subgraph, and put that node
939 * into <forwards_match>.
941 * Return 1 otherwise and keep <forwards_match> unchanged.
945 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
947 struct lock_list
*entry
;
950 if (depth
> max_recursion_depth
)
951 max_recursion_depth
= depth
;
952 if (depth
>= RECURSION_LIMIT
)
953 return print_infinite_recursion_bug();
955 debug_atomic_inc(&nr_find_usage_forwards_checks
);
956 if (source
->usage_mask
& (1 << find_usage_bit
)) {
957 forwards_match
= source
;
962 * Check this lock's dependency list:
964 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
965 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
966 ret
= find_usage_forwards(entry
->class, depth
+1);
967 if (ret
== 2 || ret
== 0)
974 * Find a node in the backwards-direction dependency sub-graph starting
975 * at <source> that matches <find_usage_bit>.
977 * Return 2 if such a node exists in the subgraph, and put that node
978 * into <backwards_match>.
980 * Return 1 otherwise and keep <backwards_match> unchanged.
984 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
986 struct lock_list
*entry
;
989 if (!__raw_spin_is_locked(&lockdep_lock
))
990 return DEBUG_LOCKS_WARN_ON(1);
992 if (depth
> max_recursion_depth
)
993 max_recursion_depth
= depth
;
994 if (depth
>= RECURSION_LIMIT
)
995 return print_infinite_recursion_bug();
997 debug_atomic_inc(&nr_find_usage_backwards_checks
);
998 if (source
->usage_mask
& (1 << find_usage_bit
)) {
999 backwards_match
= source
;
1004 * Check this lock's dependency list:
1006 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
1007 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
1008 ret
= find_usage_backwards(entry
->class, depth
+1);
1009 if (ret
== 2 || ret
== 0)
1016 print_bad_irq_dependency(struct task_struct
*curr
,
1017 struct held_lock
*prev
,
1018 struct held_lock
*next
,
1019 enum lock_usage_bit bit1
,
1020 enum lock_usage_bit bit2
,
1021 const char *irqclass
)
1023 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1026 printk("\n======================================================\n");
1027 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1028 irqclass
, irqclass
);
1029 print_kernel_version();
1030 printk( "------------------------------------------------------\n");
1031 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1032 curr
->comm
, curr
->pid
,
1033 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1034 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1035 curr
->hardirqs_enabled
,
1036 curr
->softirqs_enabled
);
1039 printk("\nand this task is already holding:\n");
1041 printk("which would create a new lock dependency:\n");
1042 print_lock_name(prev
->class);
1044 print_lock_name(next
->class);
1047 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1049 print_lock_name(backwards_match
);
1050 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1052 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
1054 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1055 print_lock_name(forwards_match
);
1056 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1059 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
1061 printk("\nother info that might help us debug this:\n\n");
1062 lockdep_print_held_locks(curr
);
1064 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
1065 print_lock_dependencies(backwards_match
, 0);
1067 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
1068 print_lock_dependencies(forwards_match
, 0);
1070 printk("\nstack backtrace:\n");
1077 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1078 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1079 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1083 find_usage_bit
= bit_backwards
;
1084 /* fills in <backwards_match> */
1085 ret
= find_usage_backwards(prev
->class, 0);
1086 if (!ret
|| ret
== 1)
1089 find_usage_bit
= bit_forwards
;
1090 ret
= find_usage_forwards(next
->class, 0);
1091 if (!ret
|| ret
== 1)
1094 return print_bad_irq_dependency(curr
, prev
, next
,
1095 bit_backwards
, bit_forwards
, irqclass
);
1099 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1100 struct held_lock
*next
)
1103 * Prove that the new dependency does not connect a hardirq-safe
1104 * lock with a hardirq-unsafe lock - to achieve this we search
1105 * the backwards-subgraph starting at <prev>, and the
1106 * forwards-subgraph starting at <next>:
1108 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
1109 LOCK_ENABLED_HARDIRQS
, "hard"))
1113 * Prove that the new dependency does not connect a hardirq-safe-read
1114 * lock with a hardirq-unsafe lock - to achieve this we search
1115 * the backwards-subgraph starting at <prev>, and the
1116 * forwards-subgraph starting at <next>:
1118 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
1119 LOCK_ENABLED_HARDIRQS
, "hard-read"))
1123 * Prove that the new dependency does not connect a softirq-safe
1124 * lock with a softirq-unsafe lock - to achieve this we search
1125 * the backwards-subgraph starting at <prev>, and the
1126 * forwards-subgraph starting at <next>:
1128 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
1129 LOCK_ENABLED_SOFTIRQS
, "soft"))
1132 * Prove that the new dependency does not connect a softirq-safe-read
1133 * lock with a softirq-unsafe lock - to achieve this we search
1134 * the backwards-subgraph starting at <prev>, and the
1135 * forwards-subgraph starting at <next>:
1137 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
1138 LOCK_ENABLED_SOFTIRQS
, "soft"))
1144 static void inc_chains(void)
1146 if (current
->hardirq_context
)
1147 nr_hardirq_chains
++;
1149 if (current
->softirq_context
)
1150 nr_softirq_chains
++;
1152 nr_process_chains
++;
1159 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1160 struct held_lock
*next
)
1165 static inline void inc_chains(void)
1167 nr_process_chains
++;
1173 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1174 struct held_lock
*next
)
1176 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1179 printk("\n=============================================\n");
1180 printk( "[ INFO: possible recursive locking detected ]\n");
1181 print_kernel_version();
1182 printk( "---------------------------------------------\n");
1183 printk("%s/%d is trying to acquire lock:\n",
1184 curr
->comm
, curr
->pid
);
1186 printk("\nbut task is already holding lock:\n");
1189 printk("\nother info that might help us debug this:\n");
1190 lockdep_print_held_locks(curr
);
1192 printk("\nstack backtrace:\n");
1199 * Check whether we are holding such a class already.
1201 * (Note that this has to be done separately, because the graph cannot
1202 * detect such classes of deadlocks.)
1204 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1207 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1208 struct lockdep_map
*next_instance
, int read
)
1210 struct held_lock
*prev
;
1213 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1214 prev
= curr
->held_locks
+ i
;
1215 if (prev
->class != next
->class)
1218 * Allow read-after-read recursion of the same
1219 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1221 if ((read
== 2) && prev
->read
)
1223 return print_deadlock_bug(curr
, prev
, next
);
1229 * There was a chain-cache miss, and we are about to add a new dependency
1230 * to a previous lock. We recursively validate the following rules:
1232 * - would the adding of the <prev> -> <next> dependency create a
1233 * circular dependency in the graph? [== circular deadlock]
1235 * - does the new prev->next dependency connect any hardirq-safe lock
1236 * (in the full backwards-subgraph starting at <prev>) with any
1237 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1238 * <next>)? [== illegal lock inversion with hardirq contexts]
1240 * - does the new prev->next dependency connect any softirq-safe lock
1241 * (in the full backwards-subgraph starting at <prev>) with any
1242 * softirq-unsafe lock (in the full forwards-subgraph starting at
1243 * <next>)? [== illegal lock inversion with softirq contexts]
1245 * any of these scenarios could lead to a deadlock.
1247 * Then if all the validations pass, we add the forwards and backwards
1251 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1252 struct held_lock
*next
, int distance
)
1254 struct lock_list
*entry
;
1258 * Prove that the new <prev> -> <next> dependency would not
1259 * create a circular dependency in the graph. (We do this by
1260 * forward-recursing into the graph starting at <next>, and
1261 * checking whether we can reach <prev>.)
1263 * We are using global variables to control the recursion, to
1264 * keep the stackframe size of the recursive functions low:
1266 check_source
= next
;
1267 check_target
= prev
;
1268 if (!(check_noncircular(next
->class, 0)))
1269 return print_circular_bug_tail();
1271 if (!check_prev_add_irq(curr
, prev
, next
))
1275 * For recursive read-locks we do all the dependency checks,
1276 * but we dont store read-triggered dependencies (only
1277 * write-triggered dependencies). This ensures that only the
1278 * write-side dependencies matter, and that if for example a
1279 * write-lock never takes any other locks, then the reads are
1280 * equivalent to a NOP.
1282 if (next
->read
== 2 || prev
->read
== 2)
1285 * Is the <prev> -> <next> dependency already present?
1287 * (this may occur even though this is a new chain: consider
1288 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1289 * chains - the second one will be new, but L1 already has
1290 * L2 added to its dependency list, due to the first chain.)
1292 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
1293 if (entry
->class == next
->class) {
1295 entry
->distance
= 1;
1301 * Ok, all validations passed, add the new lock
1302 * to the previous lock's dependency list:
1304 ret
= add_lock_to_list(prev
->class, next
->class,
1305 &prev
->class->locks_after
, next
->acquire_ip
, distance
);
1310 ret
= add_lock_to_list(next
->class, prev
->class,
1311 &next
->class->locks_before
, next
->acquire_ip
, distance
);
1316 * Debugging printouts:
1318 if (verbose(prev
->class) || verbose(next
->class)) {
1320 printk("\n new dependency: ");
1321 print_lock_name(prev
->class);
1323 print_lock_name(next
->class);
1326 return graph_lock();
1332 * Add the dependency to all directly-previous locks that are 'relevant'.
1333 * The ones that are relevant are (in increasing distance from curr):
1334 * all consecutive trylock entries and the final non-trylock entry - or
1335 * the end of this context's lock-chain - whichever comes first.
1338 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1340 int depth
= curr
->lockdep_depth
;
1341 struct held_lock
*hlock
;
1346 * Depth must not be zero for a non-head lock:
1351 * At least two relevant locks must exist for this
1354 if (curr
->held_locks
[depth
].irq_context
!=
1355 curr
->held_locks
[depth
-1].irq_context
)
1359 int distance
= curr
->lockdep_depth
- depth
+ 1;
1360 hlock
= curr
->held_locks
+ depth
-1;
1362 * Only non-recursive-read entries get new dependencies
1365 if (hlock
->read
!= 2) {
1366 if (!check_prev_add(curr
, hlock
, next
, distance
))
1369 * Stop after the first non-trylock entry,
1370 * as non-trylock entries have added their
1371 * own direct dependencies already, so this
1372 * lock is connected to them indirectly:
1374 if (!hlock
->trylock
)
1379 * End of lock-stack?
1384 * Stop the search if we cross into another context:
1386 if (curr
->held_locks
[depth
].irq_context
!=
1387 curr
->held_locks
[depth
-1].irq_context
)
1392 if (!debug_locks_off_graph_unlock())
1400 unsigned long nr_lock_chains
;
1401 static struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1404 * Look up a dependency chain. If the key is not present yet then
1405 * add it and return 1 - in this case the new dependency chain is
1406 * validated. If the key is already hashed, return 0.
1407 * (On return with 1 graph_lock is held.)
1409 static inline int lookup_chain_cache(u64 chain_key
, struct lock_class
*class)
1411 struct list_head
*hash_head
= chainhashentry(chain_key
);
1412 struct lock_chain
*chain
;
1414 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1417 * We can walk it lock-free, because entries only get added
1420 list_for_each_entry(chain
, hash_head
, entry
) {
1421 if (chain
->chain_key
== chain_key
) {
1423 debug_atomic_inc(&chain_lookup_hits
);
1424 if (very_verbose(class))
1425 printk("\nhash chain already cached, key: "
1426 "%016Lx tail class: [%p] %s\n",
1427 (unsigned long long)chain_key
,
1428 class->key
, class->name
);
1432 if (very_verbose(class))
1433 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1434 (unsigned long long)chain_key
, class->key
, class->name
);
1436 * Allocate a new chain entry from the static array, and add
1442 * We have to walk the chain again locked - to avoid duplicates:
1444 list_for_each_entry(chain
, hash_head
, entry
) {
1445 if (chain
->chain_key
== chain_key
) {
1450 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1451 if (!debug_locks_off_graph_unlock())
1454 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1455 printk("turning off the locking correctness validator.\n");
1458 chain
= lock_chains
+ nr_lock_chains
++;
1459 chain
->chain_key
= chain_key
;
1460 list_add_tail_rcu(&chain
->entry
, hash_head
);
1461 debug_atomic_inc(&chain_lookup_misses
);
1467 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1468 struct held_lock
*hlock
, int chain_head
)
1471 * Trylock needs to maintain the stack of held locks, but it
1472 * does not add new dependencies, because trylock can be done
1475 * We look up the chain_key and do the O(N^2) check and update of
1476 * the dependencies only if this is a new dependency chain.
1477 * (If lookup_chain_cache() returns with 1 it acquires
1478 * graph_lock for us)
1480 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1481 lookup_chain_cache(curr
->curr_chain_key
, hlock
->class)) {
1483 * Check whether last held lock:
1485 * - is irq-safe, if this lock is irq-unsafe
1486 * - is softirq-safe, if this lock is hardirq-unsafe
1488 * And check whether the new lock's dependency graph
1489 * could lead back to the previous lock.
1491 * any of these scenarios could lead to a deadlock. If
1494 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1499 * Mark recursive read, as we jump over it when
1500 * building dependencies (just like we jump over
1506 * Add dependency only if this lock is not the head
1507 * of the chain, and if it's not a secondary read-lock:
1509 if (!chain_head
&& ret
!= 2)
1510 if (!check_prevs_add(curr
, hlock
))
1514 /* after lookup_chain_cache(): */
1515 if (unlikely(!debug_locks
))
1521 static inline int validate_chain(struct task_struct
*curr
,
1522 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1530 * We are building curr_chain_key incrementally, so double-check
1531 * it from scratch, to make sure that it's done correctly:
1533 static void check_chain_key(struct task_struct
*curr
)
1535 #ifdef CONFIG_DEBUG_LOCKDEP
1536 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1540 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1541 hlock
= curr
->held_locks
+ i
;
1542 if (chain_key
!= hlock
->prev_chain_key
) {
1544 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1545 curr
->lockdep_depth
, i
,
1546 (unsigned long long)chain_key
,
1547 (unsigned long long)hlock
->prev_chain_key
);
1551 id
= hlock
->class - lock_classes
;
1552 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1555 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1556 hlock
->irq_context
))
1558 chain_key
= iterate_chain_key(chain_key
, id
);
1561 if (chain_key
!= curr
->curr_chain_key
) {
1563 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1564 curr
->lockdep_depth
, i
,
1565 (unsigned long long)chain_key
,
1566 (unsigned long long)curr
->curr_chain_key
);
1573 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1574 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1576 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1579 printk("\n=================================\n");
1580 printk( "[ INFO: inconsistent lock state ]\n");
1581 print_kernel_version();
1582 printk( "---------------------------------\n");
1584 printk("inconsistent {%s} -> {%s} usage.\n",
1585 usage_str
[prev_bit
], usage_str
[new_bit
]);
1587 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1588 curr
->comm
, curr
->pid
,
1589 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1590 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1591 trace_hardirqs_enabled(curr
),
1592 trace_softirqs_enabled(curr
));
1595 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1596 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1598 print_irqtrace_events(curr
);
1599 printk("\nother info that might help us debug this:\n");
1600 lockdep_print_held_locks(curr
);
1602 printk("\nstack backtrace:\n");
1609 * Print out an error if an invalid bit is set:
1612 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1613 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1615 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1616 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1620 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1621 enum lock_usage_bit new_bit
);
1623 #ifdef CONFIG_TRACE_IRQFLAGS
1626 * print irq inversion bug:
1629 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1630 struct held_lock
*this, int forwards
,
1631 const char *irqclass
)
1633 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1636 printk("\n=========================================================\n");
1637 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1638 print_kernel_version();
1639 printk( "---------------------------------------------------------\n");
1640 printk("%s/%d just changed the state of lock:\n",
1641 curr
->comm
, curr
->pid
);
1644 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1646 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1647 print_lock_name(other
);
1648 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1650 printk("\nother info that might help us debug this:\n");
1651 lockdep_print_held_locks(curr
);
1653 printk("\nthe first lock's dependencies:\n");
1654 print_lock_dependencies(this->class, 0);
1656 printk("\nthe second lock's dependencies:\n");
1657 print_lock_dependencies(other
, 0);
1659 printk("\nstack backtrace:\n");
1666 * Prove that in the forwards-direction subgraph starting at <this>
1667 * there is no lock matching <mask>:
1670 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1671 enum lock_usage_bit bit
, const char *irqclass
)
1675 find_usage_bit
= bit
;
1676 /* fills in <forwards_match> */
1677 ret
= find_usage_forwards(this->class, 0);
1678 if (!ret
|| ret
== 1)
1681 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1685 * Prove that in the backwards-direction subgraph starting at <this>
1686 * there is no lock matching <mask>:
1689 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1690 enum lock_usage_bit bit
, const char *irqclass
)
1694 find_usage_bit
= bit
;
1695 /* fills in <backwards_match> */
1696 ret
= find_usage_backwards(this->class, 0);
1697 if (!ret
|| ret
== 1)
1700 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1703 void print_irqtrace_events(struct task_struct
*curr
)
1705 printk("irq event stamp: %u\n", curr
->irq_events
);
1706 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1707 print_ip_sym(curr
->hardirq_enable_ip
);
1708 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1709 print_ip_sym(curr
->hardirq_disable_ip
);
1710 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1711 print_ip_sym(curr
->softirq_enable_ip
);
1712 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1713 print_ip_sym(curr
->softirq_disable_ip
);
1716 static int hardirq_verbose(struct lock_class
*class)
1719 return class_filter(class);
1724 static int softirq_verbose(struct lock_class
*class)
1727 return class_filter(class);
1732 #define STRICT_READ_CHECKS 1
1734 static int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
1735 enum lock_usage_bit new_bit
)
1740 case LOCK_USED_IN_HARDIRQ
:
1741 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1743 if (!valid_state(curr
, this, new_bit
,
1744 LOCK_ENABLED_HARDIRQS_READ
))
1747 * just marked it hardirq-safe, check that this lock
1748 * took no hardirq-unsafe lock in the past:
1750 if (!check_usage_forwards(curr
, this,
1751 LOCK_ENABLED_HARDIRQS
, "hard"))
1753 #if STRICT_READ_CHECKS
1755 * just marked it hardirq-safe, check that this lock
1756 * took no hardirq-unsafe-read lock in the past:
1758 if (!check_usage_forwards(curr
, this,
1759 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1762 if (hardirq_verbose(this->class))
1765 case LOCK_USED_IN_SOFTIRQ
:
1766 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1768 if (!valid_state(curr
, this, new_bit
,
1769 LOCK_ENABLED_SOFTIRQS_READ
))
1772 * just marked it softirq-safe, check that this lock
1773 * took no softirq-unsafe lock in the past:
1775 if (!check_usage_forwards(curr
, this,
1776 LOCK_ENABLED_SOFTIRQS
, "soft"))
1778 #if STRICT_READ_CHECKS
1780 * just marked it softirq-safe, check that this lock
1781 * took no softirq-unsafe-read lock in the past:
1783 if (!check_usage_forwards(curr
, this,
1784 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1787 if (softirq_verbose(this->class))
1790 case LOCK_USED_IN_HARDIRQ_READ
:
1791 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1794 * just marked it hardirq-read-safe, check that this lock
1795 * took no hardirq-unsafe lock in the past:
1797 if (!check_usage_forwards(curr
, this,
1798 LOCK_ENABLED_HARDIRQS
, "hard"))
1800 if (hardirq_verbose(this->class))
1803 case LOCK_USED_IN_SOFTIRQ_READ
:
1804 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1807 * just marked it softirq-read-safe, check that this lock
1808 * took no softirq-unsafe lock in the past:
1810 if (!check_usage_forwards(curr
, this,
1811 LOCK_ENABLED_SOFTIRQS
, "soft"))
1813 if (softirq_verbose(this->class))
1816 case LOCK_ENABLED_HARDIRQS
:
1817 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1819 if (!valid_state(curr
, this, new_bit
,
1820 LOCK_USED_IN_HARDIRQ_READ
))
1823 * just marked it hardirq-unsafe, check that no hardirq-safe
1824 * lock in the system ever took it in the past:
1826 if (!check_usage_backwards(curr
, this,
1827 LOCK_USED_IN_HARDIRQ
, "hard"))
1829 #if STRICT_READ_CHECKS
1831 * just marked it hardirq-unsafe, check that no
1832 * hardirq-safe-read lock in the system ever took
1835 if (!check_usage_backwards(curr
, this,
1836 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1839 if (hardirq_verbose(this->class))
1842 case LOCK_ENABLED_SOFTIRQS
:
1843 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1845 if (!valid_state(curr
, this, new_bit
,
1846 LOCK_USED_IN_SOFTIRQ_READ
))
1849 * just marked it softirq-unsafe, check that no softirq-safe
1850 * lock in the system ever took it in the past:
1852 if (!check_usage_backwards(curr
, this,
1853 LOCK_USED_IN_SOFTIRQ
, "soft"))
1855 #if STRICT_READ_CHECKS
1857 * just marked it softirq-unsafe, check that no
1858 * softirq-safe-read lock in the system ever took
1861 if (!check_usage_backwards(curr
, this,
1862 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1865 if (softirq_verbose(this->class))
1868 case LOCK_ENABLED_HARDIRQS_READ
:
1869 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1871 #if STRICT_READ_CHECKS
1873 * just marked it hardirq-read-unsafe, check that no
1874 * hardirq-safe lock in the system ever took it in the past:
1876 if (!check_usage_backwards(curr
, this,
1877 LOCK_USED_IN_HARDIRQ
, "hard"))
1880 if (hardirq_verbose(this->class))
1883 case LOCK_ENABLED_SOFTIRQS_READ
:
1884 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1886 #if STRICT_READ_CHECKS
1888 * just marked it softirq-read-unsafe, check that no
1889 * softirq-safe lock in the system ever took it in the past:
1891 if (!check_usage_backwards(curr
, this,
1892 LOCK_USED_IN_SOFTIRQ
, "soft"))
1895 if (softirq_verbose(this->class))
1907 * Mark all held locks with a usage bit:
1910 mark_held_locks(struct task_struct
*curr
, int hardirq
)
1912 enum lock_usage_bit usage_bit
;
1913 struct held_lock
*hlock
;
1916 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1917 hlock
= curr
->held_locks
+ i
;
1921 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1923 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1926 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1928 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1930 if (!mark_lock(curr
, hlock
, usage_bit
))
1938 * Debugging helper: via this flag we know that we are in
1939 * 'early bootup code', and will warn about any invalid irqs-on event:
1941 static int early_boot_irqs_enabled
;
1943 void early_boot_irqs_off(void)
1945 early_boot_irqs_enabled
= 0;
1948 void early_boot_irqs_on(void)
1950 early_boot_irqs_enabled
= 1;
1954 * Hardirqs will be enabled:
1956 void trace_hardirqs_on(void)
1958 struct task_struct
*curr
= current
;
1961 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1964 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
1967 if (unlikely(curr
->hardirqs_enabled
)) {
1968 debug_atomic_inc(&redundant_hardirqs_on
);
1971 /* we'll do an OFF -> ON transition: */
1972 curr
->hardirqs_enabled
= 1;
1973 ip
= (unsigned long) __builtin_return_address(0);
1975 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1977 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
1980 * We are going to turn hardirqs on, so set the
1981 * usage bit for all held locks:
1983 if (!mark_held_locks(curr
, 1))
1986 * If we have softirqs enabled, then set the usage
1987 * bit for all held locks. (disabled hardirqs prevented
1988 * this bit from being set before)
1990 if (curr
->softirqs_enabled
)
1991 if (!mark_held_locks(curr
, 0))
1994 curr
->hardirq_enable_ip
= ip
;
1995 curr
->hardirq_enable_event
= ++curr
->irq_events
;
1996 debug_atomic_inc(&hardirqs_on_events
);
1999 EXPORT_SYMBOL(trace_hardirqs_on
);
2002 * Hardirqs were disabled:
2004 void trace_hardirqs_off(void)
2006 struct task_struct
*curr
= current
;
2008 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2011 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2014 if (curr
->hardirqs_enabled
) {
2016 * We have done an ON -> OFF transition:
2018 curr
->hardirqs_enabled
= 0;
2019 curr
->hardirq_disable_ip
= _RET_IP_
;
2020 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2021 debug_atomic_inc(&hardirqs_off_events
);
2023 debug_atomic_inc(&redundant_hardirqs_off
);
2026 EXPORT_SYMBOL(trace_hardirqs_off
);
2029 * Softirqs will be enabled:
2031 void trace_softirqs_on(unsigned long ip
)
2033 struct task_struct
*curr
= current
;
2035 if (unlikely(!debug_locks
))
2038 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2041 if (curr
->softirqs_enabled
) {
2042 debug_atomic_inc(&redundant_softirqs_on
);
2047 * We'll do an OFF -> ON transition:
2049 curr
->softirqs_enabled
= 1;
2050 curr
->softirq_enable_ip
= ip
;
2051 curr
->softirq_enable_event
= ++curr
->irq_events
;
2052 debug_atomic_inc(&softirqs_on_events
);
2054 * We are going to turn softirqs on, so set the
2055 * usage bit for all held locks, if hardirqs are
2058 if (curr
->hardirqs_enabled
)
2059 mark_held_locks(curr
, 0);
2063 * Softirqs were disabled:
2065 void trace_softirqs_off(unsigned long ip
)
2067 struct task_struct
*curr
= current
;
2069 if (unlikely(!debug_locks
))
2072 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2075 if (curr
->softirqs_enabled
) {
2077 * We have done an ON -> OFF transition:
2079 curr
->softirqs_enabled
= 0;
2080 curr
->softirq_disable_ip
= ip
;
2081 curr
->softirq_disable_event
= ++curr
->irq_events
;
2082 debug_atomic_inc(&softirqs_off_events
);
2083 DEBUG_LOCKS_WARN_ON(!softirq_count());
2085 debug_atomic_inc(&redundant_softirqs_off
);
2088 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2091 * If non-trylock use in a hardirq or softirq context, then
2092 * mark the lock as used in these contexts:
2094 if (!hlock
->trylock
) {
2096 if (curr
->hardirq_context
)
2097 if (!mark_lock(curr
, hlock
,
2098 LOCK_USED_IN_HARDIRQ_READ
))
2100 if (curr
->softirq_context
)
2101 if (!mark_lock(curr
, hlock
,
2102 LOCK_USED_IN_SOFTIRQ_READ
))
2105 if (curr
->hardirq_context
)
2106 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2108 if (curr
->softirq_context
)
2109 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2113 if (!hlock
->hardirqs_off
) {
2115 if (!mark_lock(curr
, hlock
,
2116 LOCK_ENABLED_HARDIRQS_READ
))
2118 if (curr
->softirqs_enabled
)
2119 if (!mark_lock(curr
, hlock
,
2120 LOCK_ENABLED_SOFTIRQS_READ
))
2123 if (!mark_lock(curr
, hlock
,
2124 LOCK_ENABLED_HARDIRQS
))
2126 if (curr
->softirqs_enabled
)
2127 if (!mark_lock(curr
, hlock
,
2128 LOCK_ENABLED_SOFTIRQS
))
2136 static int separate_irq_context(struct task_struct
*curr
,
2137 struct held_lock
*hlock
)
2139 unsigned int depth
= curr
->lockdep_depth
;
2142 * Keep track of points where we cross into an interrupt context:
2144 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2145 curr
->softirq_context
;
2147 struct held_lock
*prev_hlock
;
2149 prev_hlock
= curr
->held_locks
+ depth
-1;
2151 * If we cross into another context, reset the
2152 * hash key (this also prevents the checking and the
2153 * adding of the dependency to 'prev'):
2155 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2164 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2165 enum lock_usage_bit new_bit
)
2171 static inline int mark_irqflags(struct task_struct
*curr
,
2172 struct held_lock
*hlock
)
2177 static inline int separate_irq_context(struct task_struct
*curr
,
2178 struct held_lock
*hlock
)
2186 * Mark a lock with a usage bit, and validate the state transition:
2188 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2189 enum lock_usage_bit new_bit
)
2191 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2194 * If already set then do not dirty the cacheline,
2195 * nor do any checks:
2197 if (likely(this->class->usage_mask
& new_mask
))
2203 * Make sure we didnt race:
2205 if (unlikely(this->class->usage_mask
& new_mask
)) {
2210 this->class->usage_mask
|= new_mask
;
2212 if (!save_trace(this->class->usage_traces
+ new_bit
))
2216 case LOCK_USED_IN_HARDIRQ
:
2217 case LOCK_USED_IN_SOFTIRQ
:
2218 case LOCK_USED_IN_HARDIRQ_READ
:
2219 case LOCK_USED_IN_SOFTIRQ_READ
:
2220 case LOCK_ENABLED_HARDIRQS
:
2221 case LOCK_ENABLED_SOFTIRQS
:
2222 case LOCK_ENABLED_HARDIRQS_READ
:
2223 case LOCK_ENABLED_SOFTIRQS_READ
:
2224 ret
= mark_lock_irq(curr
, this, new_bit
);
2230 * Add it to the global list of classes:
2232 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
2233 debug_atomic_dec(&nr_unused_locks
);
2236 if (!debug_locks_off_graph_unlock())
2245 * We must printk outside of the graph_lock:
2248 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2250 print_irqtrace_events(curr
);
2258 * Initialize a lock instance's lock-class mapping info:
2260 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2261 struct lock_class_key
*key
, int subclass
)
2263 if (unlikely(!debug_locks
))
2266 if (DEBUG_LOCKS_WARN_ON(!key
))
2268 if (DEBUG_LOCKS_WARN_ON(!name
))
2271 * Sanity check, the lock-class key must be persistent:
2273 if (!static_obj(key
)) {
2274 printk("BUG: key %p not in .data!\n", key
);
2275 DEBUG_LOCKS_WARN_ON(1);
2280 lock
->class_cache
= NULL
;
2282 register_lock_class(lock
, subclass
, 1);
2285 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2288 * This gets called for every mutex_lock*()/spin_lock*() operation.
2289 * We maintain the dependency maps and validate the locking attempt:
2291 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2292 int trylock
, int read
, int check
, int hardirqs_off
,
2295 struct task_struct
*curr
= current
;
2296 struct lock_class
*class = NULL
;
2297 struct held_lock
*hlock
;
2298 unsigned int depth
, id
;
2305 if (unlikely(!debug_locks
))
2308 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2311 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2313 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2314 printk("turning off the locking correctness validator.\n");
2319 class = lock
->class_cache
;
2321 * Not cached yet or subclass?
2323 if (unlikely(!class)) {
2324 class = register_lock_class(lock
, subclass
, 0);
2328 debug_atomic_inc((atomic_t
*)&class->ops
);
2329 if (very_verbose(class)) {
2330 printk("\nacquire class [%p] %s", class->key
, class->name
);
2331 if (class->name_version
> 1)
2332 printk("#%d", class->name_version
);
2338 * Add the lock to the list of currently held locks.
2339 * (we dont increase the depth just yet, up until the
2340 * dependency checks are done)
2342 depth
= curr
->lockdep_depth
;
2343 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2346 hlock
= curr
->held_locks
+ depth
;
2348 hlock
->class = class;
2349 hlock
->acquire_ip
= ip
;
2350 hlock
->instance
= lock
;
2351 hlock
->trylock
= trylock
;
2353 hlock
->check
= check
;
2354 hlock
->hardirqs_off
= hardirqs_off
;
2355 #ifdef CONFIG_LOCK_STAT
2356 hlock
->waittime_stamp
= 0;
2357 hlock
->holdtime_stamp
= sched_clock();
2360 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2363 /* mark it as used: */
2364 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2368 * Calculate the chain hash: it's the combined has of all the
2369 * lock keys along the dependency chain. We save the hash value
2370 * at every step so that we can get the current hash easily
2371 * after unlock. The chain hash is then used to cache dependency
2374 * The 'key ID' is what is the most compact key value to drive
2375 * the hash, not class->key.
2377 id
= class - lock_classes
;
2378 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2381 chain_key
= curr
->curr_chain_key
;
2383 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2388 hlock
->prev_chain_key
= chain_key
;
2389 if (separate_irq_context(curr
, hlock
)) {
2393 chain_key
= iterate_chain_key(chain_key
, id
);
2394 curr
->curr_chain_key
= chain_key
;
2396 if (!validate_chain(curr
, lock
, hlock
, chain_head
))
2399 curr
->lockdep_depth
++;
2400 check_chain_key(curr
);
2401 #ifdef CONFIG_DEBUG_LOCKDEP
2402 if (unlikely(!debug_locks
))
2405 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2407 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2408 printk("turning off the locking correctness validator.\n");
2412 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2413 max_lockdep_depth
= curr
->lockdep_depth
;
2419 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2422 if (!debug_locks_off())
2424 if (debug_locks_silent
)
2427 printk("\n=====================================\n");
2428 printk( "[ BUG: bad unlock balance detected! ]\n");
2429 printk( "-------------------------------------\n");
2430 printk("%s/%d is trying to release lock (",
2431 curr
->comm
, curr
->pid
);
2432 print_lockdep_cache(lock
);
2435 printk("but there are no more locks to release!\n");
2436 printk("\nother info that might help us debug this:\n");
2437 lockdep_print_held_locks(curr
);
2439 printk("\nstack backtrace:\n");
2446 * Common debugging checks for both nested and non-nested unlock:
2448 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2451 if (unlikely(!debug_locks
))
2453 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2456 if (curr
->lockdep_depth
<= 0)
2457 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2463 * Remove the lock to the list of currently held locks in a
2464 * potentially non-nested (out of order) manner. This is a
2465 * relatively rare operation, as all the unlock APIs default
2466 * to nested mode (which uses lock_release()):
2469 lock_release_non_nested(struct task_struct
*curr
,
2470 struct lockdep_map
*lock
, unsigned long ip
)
2472 struct held_lock
*hlock
, *prev_hlock
;
2477 * Check whether the lock exists in the current stack
2480 depth
= curr
->lockdep_depth
;
2481 if (DEBUG_LOCKS_WARN_ON(!depth
))
2485 for (i
= depth
-1; i
>= 0; i
--) {
2486 hlock
= curr
->held_locks
+ i
;
2488 * We must not cross into another context:
2490 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2492 if (hlock
->instance
== lock
)
2496 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2499 lock_release_holdtime(hlock
);
2502 * We have the right lock to unlock, 'hlock' points to it.
2503 * Now we remove it from the stack, and add back the other
2504 * entries (if any), recalculating the hash along the way:
2506 curr
->lockdep_depth
= i
;
2507 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2509 for (i
++; i
< depth
; i
++) {
2510 hlock
= curr
->held_locks
+ i
;
2511 if (!__lock_acquire(hlock
->instance
,
2512 hlock
->class->subclass
, hlock
->trylock
,
2513 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2518 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2524 * Remove the lock to the list of currently held locks - this gets
2525 * called on mutex_unlock()/spin_unlock*() (or on a failed
2526 * mutex_lock_interruptible()). This is done for unlocks that nest
2527 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2529 static int lock_release_nested(struct task_struct
*curr
,
2530 struct lockdep_map
*lock
, unsigned long ip
)
2532 struct held_lock
*hlock
;
2536 * Pop off the top of the lock stack:
2538 depth
= curr
->lockdep_depth
- 1;
2539 hlock
= curr
->held_locks
+ depth
;
2542 * Is the unlock non-nested:
2544 if (hlock
->instance
!= lock
)
2545 return lock_release_non_nested(curr
, lock
, ip
);
2546 curr
->lockdep_depth
--;
2548 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2551 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2553 lock_release_holdtime(hlock
);
2555 #ifdef CONFIG_DEBUG_LOCKDEP
2556 hlock
->prev_chain_key
= 0;
2557 hlock
->class = NULL
;
2558 hlock
->acquire_ip
= 0;
2559 hlock
->irq_context
= 0;
2565 * Remove the lock to the list of currently held locks - this gets
2566 * called on mutex_unlock()/spin_unlock*() (or on a failed
2567 * mutex_lock_interruptible()). This is done for unlocks that nest
2568 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2571 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2573 struct task_struct
*curr
= current
;
2575 if (!check_unlock(curr
, lock
, ip
))
2579 if (!lock_release_nested(curr
, lock
, ip
))
2582 if (!lock_release_non_nested(curr
, lock
, ip
))
2586 check_chain_key(curr
);
2590 * Check whether we follow the irq-flags state precisely:
2592 static void check_flags(unsigned long flags
)
2594 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2598 if (irqs_disabled_flags(flags
))
2599 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2601 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2604 * We dont accurately track softirq state in e.g.
2605 * hardirq contexts (such as on 4KSTACKS), so only
2606 * check if not in hardirq contexts:
2608 if (!hardirq_count()) {
2609 if (softirq_count())
2610 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2612 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2616 print_irqtrace_events(current
);
2621 * We are not always called with irqs disabled - do that here,
2622 * and also avoid lockdep recursion:
2624 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2625 int trylock
, int read
, int check
, unsigned long ip
)
2627 unsigned long flags
;
2629 if (unlikely(!lock_stat
&& !prove_locking
))
2632 if (unlikely(current
->lockdep_recursion
))
2635 raw_local_irq_save(flags
);
2638 current
->lockdep_recursion
= 1;
2639 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2640 irqs_disabled_flags(flags
), ip
);
2641 current
->lockdep_recursion
= 0;
2642 raw_local_irq_restore(flags
);
2645 EXPORT_SYMBOL_GPL(lock_acquire
);
2647 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2649 unsigned long flags
;
2651 if (unlikely(!lock_stat
&& !prove_locking
))
2654 if (unlikely(current
->lockdep_recursion
))
2657 raw_local_irq_save(flags
);
2659 current
->lockdep_recursion
= 1;
2660 __lock_release(lock
, nested
, ip
);
2661 current
->lockdep_recursion
= 0;
2662 raw_local_irq_restore(flags
);
2665 EXPORT_SYMBOL_GPL(lock_release
);
2667 #ifdef CONFIG_LOCK_STAT
2669 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2672 if (!debug_locks_off())
2674 if (debug_locks_silent
)
2677 printk("\n=================================\n");
2678 printk( "[ BUG: bad contention detected! ]\n");
2679 printk( "---------------------------------\n");
2680 printk("%s/%d is trying to contend lock (",
2681 curr
->comm
, curr
->pid
);
2682 print_lockdep_cache(lock
);
2685 printk("but there are no locks held!\n");
2686 printk("\nother info that might help us debug this:\n");
2687 lockdep_print_held_locks(curr
);
2689 printk("\nstack backtrace:\n");
2696 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2698 struct task_struct
*curr
= current
;
2699 struct held_lock
*hlock
, *prev_hlock
;
2700 struct lock_class_stats
*stats
;
2704 depth
= curr
->lockdep_depth
;
2705 if (DEBUG_LOCKS_WARN_ON(!depth
))
2709 for (i
= depth
-1; i
>= 0; i
--) {
2710 hlock
= curr
->held_locks
+ i
;
2712 * We must not cross into another context:
2714 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2716 if (hlock
->instance
== lock
)
2720 print_lock_contention_bug(curr
, lock
, ip
);
2724 hlock
->waittime_stamp
= sched_clock();
2726 point
= lock_contention_point(hlock
->class, ip
);
2728 stats
= get_lock_stats(hlock
->class);
2729 if (point
< ARRAY_SIZE(stats
->contention_point
))
2730 stats
->contention_point
[i
]++;
2731 put_lock_stats(stats
);
2735 __lock_acquired(struct lockdep_map
*lock
)
2737 struct task_struct
*curr
= current
;
2738 struct held_lock
*hlock
, *prev_hlock
;
2739 struct lock_class_stats
*stats
;
2745 depth
= curr
->lockdep_depth
;
2746 if (DEBUG_LOCKS_WARN_ON(!depth
))
2750 for (i
= depth
-1; i
>= 0; i
--) {
2751 hlock
= curr
->held_locks
+ i
;
2753 * We must not cross into another context:
2755 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2757 if (hlock
->instance
== lock
)
2761 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
2765 if (!hlock
->waittime_stamp
)
2768 now
= sched_clock();
2769 waittime
= now
- hlock
->waittime_stamp
;
2770 hlock
->holdtime_stamp
= now
;
2772 stats
= get_lock_stats(hlock
->class);
2774 lock_time_inc(&stats
->read_waittime
, waittime
);
2776 lock_time_inc(&stats
->write_waittime
, waittime
);
2777 put_lock_stats(stats
);
2780 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2782 unsigned long flags
;
2784 if (unlikely(!lock_stat
))
2787 if (unlikely(current
->lockdep_recursion
))
2790 raw_local_irq_save(flags
);
2792 current
->lockdep_recursion
= 1;
2793 __lock_contended(lock
, ip
);
2794 current
->lockdep_recursion
= 0;
2795 raw_local_irq_restore(flags
);
2797 EXPORT_SYMBOL_GPL(lock_contended
);
2799 void lock_acquired(struct lockdep_map
*lock
)
2801 unsigned long flags
;
2803 if (unlikely(!lock_stat
))
2806 if (unlikely(current
->lockdep_recursion
))
2809 raw_local_irq_save(flags
);
2811 current
->lockdep_recursion
= 1;
2812 __lock_acquired(lock
);
2813 current
->lockdep_recursion
= 0;
2814 raw_local_irq_restore(flags
);
2816 EXPORT_SYMBOL_GPL(lock_acquired
);
2820 * Used by the testsuite, sanitize the validator state
2821 * after a simulated failure:
2824 void lockdep_reset(void)
2826 unsigned long flags
;
2829 raw_local_irq_save(flags
);
2830 current
->curr_chain_key
= 0;
2831 current
->lockdep_depth
= 0;
2832 current
->lockdep_recursion
= 0;
2833 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2834 nr_hardirq_chains
= 0;
2835 nr_softirq_chains
= 0;
2836 nr_process_chains
= 0;
2838 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2839 INIT_LIST_HEAD(chainhash_table
+ i
);
2840 raw_local_irq_restore(flags
);
2843 static void zap_class(struct lock_class
*class)
2848 * Remove all dependencies this lock is
2851 for (i
= 0; i
< nr_list_entries
; i
++) {
2852 if (list_entries
[i
].class == class)
2853 list_del_rcu(&list_entries
[i
].entry
);
2856 * Unhash the class and remove it from the all_lock_classes list:
2858 list_del_rcu(&class->hash_entry
);
2859 list_del_rcu(&class->lock_entry
);
2863 static inline int within(void *addr
, void *start
, unsigned long size
)
2865 return addr
>= start
&& addr
< start
+ size
;
2868 void lockdep_free_key_range(void *start
, unsigned long size
)
2870 struct lock_class
*class, *next
;
2871 struct list_head
*head
;
2872 unsigned long flags
;
2875 raw_local_irq_save(flags
);
2879 * Unhash all classes that were created by this module:
2881 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2882 head
= classhash_table
+ i
;
2883 if (list_empty(head
))
2885 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2886 if (within(class->key
, start
, size
))
2891 raw_local_irq_restore(flags
);
2894 void lockdep_reset_lock(struct lockdep_map
*lock
)
2896 struct lock_class
*class, *next
;
2897 struct list_head
*head
;
2898 unsigned long flags
;
2901 raw_local_irq_save(flags
);
2904 * Remove all classes this lock might have:
2906 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2908 * If the class exists we look it up and zap it:
2910 class = look_up_lock_class(lock
, j
);
2915 * Debug check: in the end all mapped classes should
2919 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2920 head
= classhash_table
+ i
;
2921 if (list_empty(head
))
2923 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2924 if (unlikely(class == lock
->class_cache
)) {
2925 if (debug_locks_off_graph_unlock())
2934 raw_local_irq_restore(flags
);
2937 void lockdep_init(void)
2942 * Some architectures have their own start_kernel()
2943 * code which calls lockdep_init(), while we also
2944 * call lockdep_init() from the start_kernel() itself,
2945 * and we want to initialize the hashes only once:
2947 if (lockdep_initialized
)
2950 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2951 INIT_LIST_HEAD(classhash_table
+ i
);
2953 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2954 INIT_LIST_HEAD(chainhash_table
+ i
);
2956 lockdep_initialized
= 1;
2959 void __init
lockdep_info(void)
2961 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2963 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
2964 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
2965 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
2966 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
2967 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
2968 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
2969 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
2971 printk(" memory used by lock dependency info: %lu kB\n",
2972 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
2973 sizeof(struct list_head
) * CLASSHASH_SIZE
+
2974 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
2975 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
2976 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
2978 printk(" per task-struct memory footprint: %lu bytes\n",
2979 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
2981 #ifdef CONFIG_DEBUG_LOCKDEP
2982 if (lockdep_init_error
)
2983 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2987 static inline int in_range(const void *start
, const void *addr
, const void *end
)
2989 return addr
>= start
&& addr
<= end
;
2993 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
2994 const void *mem_to
, struct held_lock
*hlock
)
2996 if (!debug_locks_off())
2998 if (debug_locks_silent
)
3001 printk("\n=========================\n");
3002 printk( "[ BUG: held lock freed! ]\n");
3003 printk( "-------------------------\n");
3004 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3005 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
3007 lockdep_print_held_locks(curr
);
3009 printk("\nstack backtrace:\n");
3014 * Called when kernel memory is freed (or unmapped), or if a lock
3015 * is destroyed or reinitialized - this code checks whether there is
3016 * any held lock in the memory range of <from> to <to>:
3018 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3020 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
3021 struct task_struct
*curr
= current
;
3022 struct held_lock
*hlock
;
3023 unsigned long flags
;
3026 if (unlikely(!debug_locks
))
3029 local_irq_save(flags
);
3030 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3031 hlock
= curr
->held_locks
+ i
;
3033 lock_from
= (void *)hlock
->instance
;
3034 lock_to
= (void *)(hlock
->instance
+ 1);
3036 if (!in_range(mem_from
, lock_from
, mem_to
) &&
3037 !in_range(mem_from
, lock_to
, mem_to
))
3040 print_freed_lock_bug(curr
, mem_from
, mem_to
, hlock
);
3043 local_irq_restore(flags
);
3045 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3047 static void print_held_locks_bug(struct task_struct
*curr
)
3049 if (!debug_locks_off())
3051 if (debug_locks_silent
)
3054 printk("\n=====================================\n");
3055 printk( "[ BUG: lock held at task exit time! ]\n");
3056 printk( "-------------------------------------\n");
3057 printk("%s/%d is exiting with locks still held!\n",
3058 curr
->comm
, curr
->pid
);
3059 lockdep_print_held_locks(curr
);
3061 printk("\nstack backtrace:\n");
3065 void debug_check_no_locks_held(struct task_struct
*task
)
3067 if (unlikely(task
->lockdep_depth
> 0))
3068 print_held_locks_bug(task
);
3071 void debug_show_all_locks(void)
3073 struct task_struct
*g
, *p
;
3077 if (unlikely(!debug_locks
)) {
3078 printk("INFO: lockdep is turned off.\n");
3081 printk("\nShowing all locks held in the system:\n");
3084 * Here we try to get the tasklist_lock as hard as possible,
3085 * if not successful after 2 seconds we ignore it (but keep
3086 * trying). This is to enable a debug printout even if a
3087 * tasklist_lock-holding task deadlocks or crashes.
3090 if (!read_trylock(&tasklist_lock
)) {
3092 printk("hm, tasklist_lock locked, retrying... ");
3095 printk(" #%d", 10-count
);
3099 printk(" ignoring it.\n");
3103 printk(" locked it.\n");
3105 do_each_thread(g
, p
) {
3106 if (p
->lockdep_depth
)
3107 lockdep_print_held_locks(p
);
3109 if (read_trylock(&tasklist_lock
))
3111 } while_each_thread(g
, p
);
3114 printk("=============================================\n\n");
3117 read_unlock(&tasklist_lock
);
3120 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3122 void debug_show_held_locks(struct task_struct
*task
)
3124 if (unlikely(!debug_locks
)) {
3125 printk("INFO: lockdep is turned off.\n");
3128 lockdep_print_held_locks(task
);
3131 EXPORT_SYMBOL_GPL(debug_show_held_locks
);