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 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/spinlock.h>
36 #include <linux/kallsyms.h>
37 #include <linux/interrupt.h>
38 #include <linux/stacktrace.h>
39 #include <linux/debug_locks.h>
40 #include <linux/irqflags.h>
41 #include <linux/utsname.h>
42 #include <linux/hash.h>
43 #include <linux/ftrace.h>
44 #include <linux/stringify.h>
45 #include <trace/lockdep.h>
47 #include <asm/sections.h>
49 #include "lockdep_internals.h"
51 #ifdef CONFIG_PROVE_LOCKING
52 int prove_locking
= 1;
53 module_param(prove_locking
, int, 0644);
55 #define prove_locking 0
58 #ifdef CONFIG_LOCK_STAT
60 module_param(lock_stat
, int, 0644);
66 * lockdep_lock: protects the lockdep graph, the hashes and the
67 * class/list/hash allocators.
69 * This is one of the rare exceptions where it's justified
70 * to use a raw spinlock - we really dont want the spinlock
71 * code to recurse back into the lockdep code...
73 static raw_spinlock_t lockdep_lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
75 static int graph_lock(void)
77 __raw_spin_lock(&lockdep_lock
);
79 * Make sure that if another CPU detected a bug while
80 * walking the graph we dont change it (while the other
81 * CPU is busy printing out stuff with the graph lock
85 __raw_spin_unlock(&lockdep_lock
);
88 /* prevent any recursions within lockdep from causing deadlocks */
89 current
->lockdep_recursion
++;
93 static inline int graph_unlock(void)
95 if (debug_locks
&& !__raw_spin_is_locked(&lockdep_lock
))
96 return DEBUG_LOCKS_WARN_ON(1);
98 current
->lockdep_recursion
--;
99 __raw_spin_unlock(&lockdep_lock
);
104 * Turn lock debugging off and return with 0 if it was off already,
105 * and also release the graph lock:
107 static inline int debug_locks_off_graph_unlock(void)
109 int ret
= debug_locks_off();
111 __raw_spin_unlock(&lockdep_lock
);
116 static int lockdep_initialized
;
118 unsigned long nr_list_entries
;
119 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
122 * All data structures here are protected by the global debug_lock.
124 * Mutex key structs only get allocated, once during bootup, and never
125 * get freed - this significantly simplifies the debugging code.
127 unsigned long nr_lock_classes
;
128 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
130 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
132 if (!hlock
->class_idx
) {
133 DEBUG_LOCKS_WARN_ON(1);
136 return lock_classes
+ hlock
->class_idx
- 1;
139 #ifdef CONFIG_LOCK_STAT
140 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], lock_stats
);
142 static int lock_point(unsigned long points
[], unsigned long ip
)
146 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
147 if (points
[i
] == 0) {
158 static void lock_time_inc(struct lock_time
*lt
, s64 time
)
163 if (time
< lt
->min
|| !lt
->min
)
170 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
172 dst
->min
+= src
->min
;
173 dst
->max
+= src
->max
;
174 dst
->total
+= src
->total
;
178 struct lock_class_stats
lock_stats(struct lock_class
*class)
180 struct lock_class_stats stats
;
183 memset(&stats
, 0, sizeof(struct lock_class_stats
));
184 for_each_possible_cpu(cpu
) {
185 struct lock_class_stats
*pcs
=
186 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
188 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
189 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
191 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
192 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
194 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
195 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
197 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
198 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
200 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
201 stats
.bounces
[i
] += pcs
->bounces
[i
];
207 void clear_lock_stats(struct lock_class
*class)
211 for_each_possible_cpu(cpu
) {
212 struct lock_class_stats
*cpu_stats
=
213 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
215 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
217 memset(class->contention_point
, 0, sizeof(class->contention_point
));
218 memset(class->contending_point
, 0, sizeof(class->contending_point
));
221 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
223 return &get_cpu_var(lock_stats
)[class - lock_classes
];
226 static void put_lock_stats(struct lock_class_stats
*stats
)
228 put_cpu_var(lock_stats
);
231 static void lock_release_holdtime(struct held_lock
*hlock
)
233 struct lock_class_stats
*stats
;
239 holdtime
= sched_clock() - hlock
->holdtime_stamp
;
241 stats
= get_lock_stats(hlock_class(hlock
));
243 lock_time_inc(&stats
->read_holdtime
, holdtime
);
245 lock_time_inc(&stats
->write_holdtime
, holdtime
);
246 put_lock_stats(stats
);
249 static inline void lock_release_holdtime(struct held_lock
*hlock
)
255 * We keep a global list of all lock classes. The list only grows,
256 * never shrinks. The list is only accessed with the lockdep
257 * spinlock lock held.
259 LIST_HEAD(all_lock_classes
);
262 * The lockdep classes are in a hash-table as well, for fast lookup:
264 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
265 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
266 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
267 #define classhashentry(key) (classhash_table + __classhashfn((key)))
269 static struct list_head classhash_table
[CLASSHASH_SIZE
];
272 * We put the lock dependency chains into a hash-table as well, to cache
275 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
276 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
277 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
278 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
280 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
283 * The hash key of the lock dependency chains is a hash itself too:
284 * it's a hash of all locks taken up to that lock, including that lock.
285 * It's a 64-bit hash, because it's important for the keys to be
288 #define iterate_chain_key(key1, key2) \
289 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
290 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
293 void lockdep_off(void)
295 current
->lockdep_recursion
++;
297 EXPORT_SYMBOL(lockdep_off
);
299 void lockdep_on(void)
301 current
->lockdep_recursion
--;
303 EXPORT_SYMBOL(lockdep_on
);
306 * Debugging switches:
310 #define VERY_VERBOSE 0
313 # define HARDIRQ_VERBOSE 1
314 # define SOFTIRQ_VERBOSE 1
315 # define RECLAIM_VERBOSE 1
317 # define HARDIRQ_VERBOSE 0
318 # define SOFTIRQ_VERBOSE 0
319 # define RECLAIM_VERBOSE 0
322 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
324 * Quick filtering for interesting events:
326 static int class_filter(struct lock_class
*class)
330 if (class->name_version
== 1 &&
331 !strcmp(class->name
, "lockname"))
333 if (class->name_version
== 1 &&
334 !strcmp(class->name
, "&struct->lockfield"))
337 /* Filter everything else. 1 would be to allow everything else */
342 static int verbose(struct lock_class
*class)
345 return class_filter(class);
351 * Stack-trace: tightly packed array of stack backtrace
352 * addresses. Protected by the graph_lock.
354 unsigned long nr_stack_trace_entries
;
355 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
357 static int save_trace(struct stack_trace
*trace
)
359 trace
->nr_entries
= 0;
360 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
361 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
365 save_stack_trace(trace
);
367 trace
->max_entries
= trace
->nr_entries
;
369 nr_stack_trace_entries
+= trace
->nr_entries
;
371 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
372 if (!debug_locks_off_graph_unlock())
375 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
376 printk("turning off the locking correctness validator.\n");
385 unsigned int nr_hardirq_chains
;
386 unsigned int nr_softirq_chains
;
387 unsigned int nr_process_chains
;
388 unsigned int max_lockdep_depth
;
389 unsigned int max_recursion_depth
;
391 static unsigned int lockdep_dependency_gen_id
;
393 static bool lockdep_dependency_visit(struct lock_class
*source
,
397 lockdep_dependency_gen_id
++;
398 if (source
->dep_gen_id
== lockdep_dependency_gen_id
)
400 source
->dep_gen_id
= lockdep_dependency_gen_id
;
404 #ifdef CONFIG_DEBUG_LOCKDEP
406 * We cannot printk in early bootup code. Not even early_printk()
407 * might work. So we mark any initialization errors and printk
408 * about it later on, in lockdep_info().
410 static int lockdep_init_error
;
411 static unsigned long lockdep_init_trace_data
[20];
412 static struct stack_trace lockdep_init_trace
= {
413 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
414 .entries
= lockdep_init_trace_data
,
418 * Various lockdep statistics:
420 atomic_t chain_lookup_hits
;
421 atomic_t chain_lookup_misses
;
422 atomic_t hardirqs_on_events
;
423 atomic_t hardirqs_off_events
;
424 atomic_t redundant_hardirqs_on
;
425 atomic_t redundant_hardirqs_off
;
426 atomic_t softirqs_on_events
;
427 atomic_t softirqs_off_events
;
428 atomic_t redundant_softirqs_on
;
429 atomic_t redundant_softirqs_off
;
430 atomic_t nr_unused_locks
;
431 atomic_t nr_cyclic_checks
;
432 atomic_t nr_cyclic_check_recursions
;
433 atomic_t nr_find_usage_forwards_checks
;
434 atomic_t nr_find_usage_forwards_recursions
;
435 atomic_t nr_find_usage_backwards_checks
;
436 atomic_t nr_find_usage_backwards_recursions
;
443 #define __USAGE(__STATE) \
444 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
445 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
446 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
447 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
449 static const char *usage_str
[] =
451 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
452 #include "lockdep_states.h"
454 [LOCK_USED
] = "INITIAL USE",
457 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
459 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
462 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
467 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
471 if (class->usage_mask
& lock_flag(bit
+ 2))
473 if (class->usage_mask
& lock_flag(bit
)) {
475 if (class->usage_mask
& lock_flag(bit
+ 2))
482 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
486 #define LOCKDEP_STATE(__STATE) \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
488 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
489 #include "lockdep_states.h"
495 static void print_lock_name(struct lock_class
*class)
497 char str
[KSYM_NAME_LEN
], usage
[LOCK_USAGE_CHARS
];
500 get_usage_chars(class, usage
);
504 name
= __get_key_name(class->key
, str
);
505 printk(" (%s", name
);
507 printk(" (%s", name
);
508 if (class->name_version
> 1)
509 printk("#%d", class->name_version
);
511 printk("/%d", class->subclass
);
513 printk("){%s}", usage
);
516 static void print_lockdep_cache(struct lockdep_map
*lock
)
519 char str
[KSYM_NAME_LEN
];
523 name
= __get_key_name(lock
->key
->subkeys
, str
);
528 static void print_lock(struct held_lock
*hlock
)
530 print_lock_name(hlock_class(hlock
));
532 print_ip_sym(hlock
->acquire_ip
);
535 static void lockdep_print_held_locks(struct task_struct
*curr
)
537 int i
, depth
= curr
->lockdep_depth
;
540 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
543 printk("%d lock%s held by %s/%d:\n",
544 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
546 for (i
= 0; i
< depth
; i
++) {
548 print_lock(curr
->held_locks
+ i
);
552 static void print_lock_class_header(struct lock_class
*class, int depth
)
556 printk("%*s->", depth
, "");
557 print_lock_name(class);
558 printk(" ops: %lu", class->ops
);
561 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
562 if (class->usage_mask
& (1 << bit
)) {
565 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
566 len
+= printk(" at:\n");
567 print_stack_trace(class->usage_traces
+ bit
, len
);
570 printk("%*s }\n", depth
, "");
572 printk("%*s ... key at: ",depth
,"");
573 print_ip_sym((unsigned long)class->key
);
577 * printk all lock dependencies starting at <entry>:
580 print_lock_dependencies(struct lock_class
*class, int depth
)
582 struct lock_list
*entry
;
584 if (lockdep_dependency_visit(class, depth
))
587 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
590 print_lock_class_header(class, depth
);
592 list_for_each_entry(entry
, &class->locks_after
, entry
) {
593 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
596 print_lock_dependencies(entry
->class, depth
+ 1);
598 printk("%*s ... acquired at:\n",depth
,"");
599 print_stack_trace(&entry
->trace
, 2);
604 static void print_kernel_version(void)
606 printk("%s %.*s\n", init_utsname()->release
,
607 (int)strcspn(init_utsname()->version
, " "),
608 init_utsname()->version
);
611 static int very_verbose(struct lock_class
*class)
614 return class_filter(class);
620 * Is this the address of a static object:
622 static int static_obj(void *obj
)
624 unsigned long start
= (unsigned long) &_stext
,
625 end
= (unsigned long) &_end
,
626 addr
= (unsigned long) obj
;
634 if ((addr
>= start
) && (addr
< end
))
641 for_each_possible_cpu(i
) {
642 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
643 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
646 if ((addr
>= start
) && (addr
< end
))
654 return is_module_address(addr
);
658 * To make lock name printouts unique, we calculate a unique
659 * class->name_version generation counter:
661 static int count_matching_names(struct lock_class
*new_class
)
663 struct lock_class
*class;
666 if (!new_class
->name
)
669 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
670 if (new_class
->key
- new_class
->subclass
== class->key
)
671 return class->name_version
;
672 if (class->name
&& !strcmp(class->name
, new_class
->name
))
673 count
= max(count
, class->name_version
);
680 * Register a lock's class in the hash-table, if the class is not present
681 * yet. Otherwise we look it up. We cache the result in the lock object
682 * itself, so actual lookup of the hash should be once per lock object.
684 static inline struct lock_class
*
685 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
687 struct lockdep_subclass_key
*key
;
688 struct list_head
*hash_head
;
689 struct lock_class
*class;
691 #ifdef CONFIG_DEBUG_LOCKDEP
693 * If the architecture calls into lockdep before initializing
694 * the hashes then we'll warn about it later. (we cannot printk
697 if (unlikely(!lockdep_initialized
)) {
699 lockdep_init_error
= 1;
700 save_stack_trace(&lockdep_init_trace
);
705 * Static locks do not have their class-keys yet - for them the key
706 * is the lock object itself:
708 if (unlikely(!lock
->key
))
709 lock
->key
= (void *)lock
;
712 * NOTE: the class-key must be unique. For dynamic locks, a static
713 * lock_class_key variable is passed in through the mutex_init()
714 * (or spin_lock_init()) call - which acts as the key. For static
715 * locks we use the lock object itself as the key.
717 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
718 sizeof(struct lockdep_map
));
720 key
= lock
->key
->subkeys
+ subclass
;
722 hash_head
= classhashentry(key
);
725 * We can walk the hash lockfree, because the hash only
726 * grows, and we are careful when adding entries to the end:
728 list_for_each_entry(class, hash_head
, hash_entry
) {
729 if (class->key
== key
) {
730 WARN_ON_ONCE(class->name
!= lock
->name
);
739 * Register a lock's class in the hash-table, if the class is not present
740 * yet. Otherwise we look it up. We cache the result in the lock object
741 * itself, so actual lookup of the hash should be once per lock object.
743 static inline struct lock_class
*
744 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
746 struct lockdep_subclass_key
*key
;
747 struct list_head
*hash_head
;
748 struct lock_class
*class;
751 class = look_up_lock_class(lock
, subclass
);
756 * Debug-check: all keys must be persistent!
758 if (!static_obj(lock
->key
)) {
760 printk("INFO: trying to register non-static key.\n");
761 printk("the code is fine but needs lockdep annotation.\n");
762 printk("turning off the locking correctness validator.\n");
768 key
= lock
->key
->subkeys
+ subclass
;
769 hash_head
= classhashentry(key
);
771 raw_local_irq_save(flags
);
773 raw_local_irq_restore(flags
);
777 * We have to do the hash-walk again, to avoid races
780 list_for_each_entry(class, hash_head
, hash_entry
)
781 if (class->key
== key
)
784 * Allocate a new key from the static array, and add it to
787 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
788 if (!debug_locks_off_graph_unlock()) {
789 raw_local_irq_restore(flags
);
792 raw_local_irq_restore(flags
);
794 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
795 printk("turning off the locking correctness validator.\n");
798 class = lock_classes
+ nr_lock_classes
++;
799 debug_atomic_inc(&nr_unused_locks
);
801 class->name
= lock
->name
;
802 class->subclass
= subclass
;
803 INIT_LIST_HEAD(&class->lock_entry
);
804 INIT_LIST_HEAD(&class->locks_before
);
805 INIT_LIST_HEAD(&class->locks_after
);
806 class->name_version
= count_matching_names(class);
808 * We use RCU's safe list-add method to make
809 * parallel walking of the hash-list safe:
811 list_add_tail_rcu(&class->hash_entry
, hash_head
);
813 * Add it to the global list of classes:
815 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
817 if (verbose(class)) {
819 raw_local_irq_restore(flags
);
821 printk("\nnew class %p: %s", class->key
, class->name
);
822 if (class->name_version
> 1)
823 printk("#%d", class->name_version
);
827 raw_local_irq_save(flags
);
829 raw_local_irq_restore(flags
);
835 raw_local_irq_restore(flags
);
837 if (!subclass
|| force
)
838 lock
->class_cache
= class;
840 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
846 #ifdef CONFIG_PROVE_LOCKING
848 * Allocate a lockdep entry. (assumes the graph_lock held, returns
849 * with NULL on failure)
851 static struct lock_list
*alloc_list_entry(void)
853 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
854 if (!debug_locks_off_graph_unlock())
857 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
858 printk("turning off the locking correctness validator.\n");
861 return list_entries
+ nr_list_entries
++;
865 * Add a new dependency to the head of the list:
867 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
868 struct list_head
*head
, unsigned long ip
, int distance
)
870 struct lock_list
*entry
;
872 * Lock not present yet - get a new dependency struct and
873 * add it to the list:
875 entry
= alloc_list_entry();
879 if (!save_trace(&entry
->trace
))
883 entry
->distance
= distance
;
885 * Since we never remove from the dependency list, the list can
886 * be walked lockless by other CPUs, it's only allocation
887 * that must be protected by the spinlock. But this also means
888 * we must make new entries visible only once writes to the
889 * entry become visible - hence the RCU op:
891 list_add_tail_rcu(&entry
->entry
, head
);
897 * Recursive, forwards-direction lock-dependency checking, used for
898 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
901 * (to keep the stackframe of the recursive functions small we
902 * use these global variables, and we also mark various helper
903 * functions as noinline.)
905 static struct held_lock
*check_source
, *check_target
;
908 * Print a dependency chain entry (this is only done when a deadlock
909 * has been detected):
912 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
914 if (debug_locks_silent
)
916 printk("\n-> #%u", depth
);
917 print_lock_name(target
->class);
919 print_stack_trace(&target
->trace
, 6);
925 * When a circular dependency is detected, print the
929 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
931 struct task_struct
*curr
= current
;
933 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
936 printk("\n=======================================================\n");
937 printk( "[ INFO: possible circular locking dependency detected ]\n");
938 print_kernel_version();
939 printk( "-------------------------------------------------------\n");
940 printk("%s/%d is trying to acquire lock:\n",
941 curr
->comm
, task_pid_nr(curr
));
942 print_lock(check_source
);
943 printk("\nbut task is already holding lock:\n");
944 print_lock(check_target
);
945 printk("\nwhich lock already depends on the new lock.\n\n");
946 printk("\nthe existing dependency chain (in reverse order) is:\n");
948 print_circular_bug_entry(entry
, depth
);
953 static noinline
int print_circular_bug_tail(void)
955 struct task_struct
*curr
= current
;
956 struct lock_list
this;
958 if (debug_locks_silent
)
961 this.class = hlock_class(check_source
);
962 if (!save_trace(&this.trace
))
965 print_circular_bug_entry(&this, 0);
967 printk("\nother info that might help us debug this:\n\n");
968 lockdep_print_held_locks(curr
);
970 printk("\nstack backtrace:\n");
976 #define RECURSION_LIMIT 40
978 static int noinline
print_infinite_recursion_bug(void)
980 if (!debug_locks_off_graph_unlock())
988 unsigned long __lockdep_count_forward_deps(struct lock_class
*class,
991 struct lock_list
*entry
;
992 unsigned long ret
= 1;
994 if (lockdep_dependency_visit(class, depth
))
998 * Recurse this class's dependency list:
1000 list_for_each_entry(entry
, &class->locks_after
, entry
)
1001 ret
+= __lockdep_count_forward_deps(entry
->class, depth
+ 1);
1006 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1008 unsigned long ret
, flags
;
1010 local_irq_save(flags
);
1011 __raw_spin_lock(&lockdep_lock
);
1012 ret
= __lockdep_count_forward_deps(class, 0);
1013 __raw_spin_unlock(&lockdep_lock
);
1014 local_irq_restore(flags
);
1019 unsigned long __lockdep_count_backward_deps(struct lock_class
*class,
1022 struct lock_list
*entry
;
1023 unsigned long ret
= 1;
1025 if (lockdep_dependency_visit(class, depth
))
1028 * Recurse this class's dependency list:
1030 list_for_each_entry(entry
, &class->locks_before
, entry
)
1031 ret
+= __lockdep_count_backward_deps(entry
->class, depth
+ 1);
1036 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1038 unsigned long ret
, flags
;
1040 local_irq_save(flags
);
1041 __raw_spin_lock(&lockdep_lock
);
1042 ret
= __lockdep_count_backward_deps(class, 0);
1043 __raw_spin_unlock(&lockdep_lock
);
1044 local_irq_restore(flags
);
1050 * Prove that the dependency graph starting at <entry> can not
1051 * lead to <target>. Print an error and return 0 if it does.
1054 check_noncircular(struct lock_class
*source
, unsigned int depth
)
1056 struct lock_list
*entry
;
1058 if (lockdep_dependency_visit(source
, depth
))
1061 debug_atomic_inc(&nr_cyclic_check_recursions
);
1062 if (depth
> max_recursion_depth
)
1063 max_recursion_depth
= depth
;
1064 if (depth
>= RECURSION_LIMIT
)
1065 return print_infinite_recursion_bug();
1067 * Check this lock's dependency list:
1069 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
1070 if (entry
->class == hlock_class(check_target
))
1071 return print_circular_bug_header(entry
, depth
+1);
1072 debug_atomic_inc(&nr_cyclic_checks
);
1073 if (!check_noncircular(entry
->class, depth
+1))
1074 return print_circular_bug_entry(entry
, depth
+1);
1079 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1081 * Forwards and backwards subgraph searching, for the purposes of
1082 * proving that two subgraphs can be connected by a new dependency
1083 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1085 static enum lock_usage_bit find_usage_bit
;
1086 static struct lock_class
*forwards_match
, *backwards_match
;
1089 * Find a node in the forwards-direction dependency sub-graph starting
1090 * at <source> that matches <find_usage_bit>.
1092 * Return 2 if such a node exists in the subgraph, and put that node
1093 * into <forwards_match>.
1095 * Return 1 otherwise and keep <forwards_match> unchanged.
1096 * Return 0 on error.
1099 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
1101 struct lock_list
*entry
;
1104 if (lockdep_dependency_visit(source
, depth
))
1107 if (depth
> max_recursion_depth
)
1108 max_recursion_depth
= depth
;
1109 if (depth
>= RECURSION_LIMIT
)
1110 return print_infinite_recursion_bug();
1112 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1113 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1114 forwards_match
= source
;
1119 * Check this lock's dependency list:
1121 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
1122 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
1123 ret
= find_usage_forwards(entry
->class, depth
+1);
1124 if (ret
== 2 || ret
== 0)
1131 * Find a node in the backwards-direction dependency sub-graph starting
1132 * at <source> that matches <find_usage_bit>.
1134 * Return 2 if such a node exists in the subgraph, and put that node
1135 * into <backwards_match>.
1137 * Return 1 otherwise and keep <backwards_match> unchanged.
1138 * Return 0 on error.
1141 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
1143 struct lock_list
*entry
;
1146 if (lockdep_dependency_visit(source
, depth
))
1149 if (!__raw_spin_is_locked(&lockdep_lock
))
1150 return DEBUG_LOCKS_WARN_ON(1);
1152 if (depth
> max_recursion_depth
)
1153 max_recursion_depth
= depth
;
1154 if (depth
>= RECURSION_LIMIT
)
1155 return print_infinite_recursion_bug();
1157 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1158 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1159 backwards_match
= source
;
1163 if (!source
&& debug_locks_off_graph_unlock()) {
1169 * Check this lock's dependency list:
1171 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
1172 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
1173 ret
= find_usage_backwards(entry
->class, depth
+1);
1174 if (ret
== 2 || ret
== 0)
1181 print_bad_irq_dependency(struct task_struct
*curr
,
1182 struct held_lock
*prev
,
1183 struct held_lock
*next
,
1184 enum lock_usage_bit bit1
,
1185 enum lock_usage_bit bit2
,
1186 const char *irqclass
)
1188 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1191 printk("\n======================================================\n");
1192 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1193 irqclass
, irqclass
);
1194 print_kernel_version();
1195 printk( "------------------------------------------------------\n");
1196 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1197 curr
->comm
, task_pid_nr(curr
),
1198 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1199 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1200 curr
->hardirqs_enabled
,
1201 curr
->softirqs_enabled
);
1204 printk("\nand this task is already holding:\n");
1206 printk("which would create a new lock dependency:\n");
1207 print_lock_name(hlock_class(prev
));
1209 print_lock_name(hlock_class(next
));
1212 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1214 print_lock_name(backwards_match
);
1215 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1217 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
1219 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1220 print_lock_name(forwards_match
);
1221 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1224 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
1226 printk("\nother info that might help us debug this:\n\n");
1227 lockdep_print_held_locks(curr
);
1229 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
1230 print_lock_dependencies(backwards_match
, 0);
1232 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
1233 print_lock_dependencies(forwards_match
, 0);
1235 printk("\nstack backtrace:\n");
1242 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1243 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1244 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1248 find_usage_bit
= bit_backwards
;
1249 /* fills in <backwards_match> */
1250 ret
= find_usage_backwards(hlock_class(prev
), 0);
1251 if (!ret
|| ret
== 1)
1254 find_usage_bit
= bit_forwards
;
1255 ret
= find_usage_forwards(hlock_class(next
), 0);
1256 if (!ret
|| ret
== 1)
1259 return print_bad_irq_dependency(curr
, prev
, next
,
1260 bit_backwards
, bit_forwards
, irqclass
);
1263 static const char *state_names
[] = {
1264 #define LOCKDEP_STATE(__STATE) \
1265 __stringify(__STATE),
1266 #include "lockdep_states.h"
1267 #undef LOCKDEP_STATE
1270 static const char *state_rnames
[] = {
1271 #define LOCKDEP_STATE(__STATE) \
1272 __stringify(__STATE)"-READ",
1273 #include "lockdep_states.h"
1274 #undef LOCKDEP_STATE
1277 static inline const char *state_name(enum lock_usage_bit bit
)
1279 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1282 static int exclusive_bit(int new_bit
)
1290 * bit 0 - write/read
1291 * bit 1 - used_in/enabled
1295 int state
= new_bit
& ~3;
1296 int dir
= new_bit
& 2;
1299 * keep state, bit flip the direction and strip read.
1301 return state
| (dir
^ 2);
1304 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1305 struct held_lock
*next
, enum lock_usage_bit bit
)
1308 * Prove that the new dependency does not connect a hardirq-safe
1309 * lock with a hardirq-unsafe lock - to achieve this we search
1310 * the backwards-subgraph starting at <prev>, and the
1311 * forwards-subgraph starting at <next>:
1313 if (!check_usage(curr
, prev
, next
, bit
,
1314 exclusive_bit(bit
), state_name(bit
)))
1320 * Prove that the new dependency does not connect a hardirq-safe-read
1321 * lock with a hardirq-unsafe lock - to achieve this we search
1322 * the backwards-subgraph starting at <prev>, and the
1323 * forwards-subgraph starting at <next>:
1325 if (!check_usage(curr
, prev
, next
, bit
,
1326 exclusive_bit(bit
), state_name(bit
)))
1333 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1334 struct held_lock
*next
)
1336 #define LOCKDEP_STATE(__STATE) \
1337 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1339 #include "lockdep_states.h"
1340 #undef LOCKDEP_STATE
1345 static void inc_chains(void)
1347 if (current
->hardirq_context
)
1348 nr_hardirq_chains
++;
1350 if (current
->softirq_context
)
1351 nr_softirq_chains
++;
1353 nr_process_chains
++;
1360 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1361 struct held_lock
*next
)
1366 static inline void inc_chains(void)
1368 nr_process_chains
++;
1374 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1375 struct held_lock
*next
)
1377 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1380 printk("\n=============================================\n");
1381 printk( "[ INFO: possible recursive locking detected ]\n");
1382 print_kernel_version();
1383 printk( "---------------------------------------------\n");
1384 printk("%s/%d is trying to acquire lock:\n",
1385 curr
->comm
, task_pid_nr(curr
));
1387 printk("\nbut task is already holding lock:\n");
1390 printk("\nother info that might help us debug this:\n");
1391 lockdep_print_held_locks(curr
);
1393 printk("\nstack backtrace:\n");
1400 * Check whether we are holding such a class already.
1402 * (Note that this has to be done separately, because the graph cannot
1403 * detect such classes of deadlocks.)
1405 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1408 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1409 struct lockdep_map
*next_instance
, int read
)
1411 struct held_lock
*prev
;
1412 struct held_lock
*nest
= NULL
;
1415 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1416 prev
= curr
->held_locks
+ i
;
1418 if (prev
->instance
== next
->nest_lock
)
1421 if (hlock_class(prev
) != hlock_class(next
))
1425 * Allow read-after-read recursion of the same
1426 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1428 if ((read
== 2) && prev
->read
)
1432 * We're holding the nest_lock, which serializes this lock's
1433 * nesting behaviour.
1438 return print_deadlock_bug(curr
, prev
, next
);
1444 * There was a chain-cache miss, and we are about to add a new dependency
1445 * to a previous lock. We recursively validate the following rules:
1447 * - would the adding of the <prev> -> <next> dependency create a
1448 * circular dependency in the graph? [== circular deadlock]
1450 * - does the new prev->next dependency connect any hardirq-safe lock
1451 * (in the full backwards-subgraph starting at <prev>) with any
1452 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1453 * <next>)? [== illegal lock inversion with hardirq contexts]
1455 * - does the new prev->next dependency connect any softirq-safe lock
1456 * (in the full backwards-subgraph starting at <prev>) with any
1457 * softirq-unsafe lock (in the full forwards-subgraph starting at
1458 * <next>)? [== illegal lock inversion with softirq contexts]
1460 * any of these scenarios could lead to a deadlock.
1462 * Then if all the validations pass, we add the forwards and backwards
1466 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1467 struct held_lock
*next
, int distance
)
1469 struct lock_list
*entry
;
1473 * Prove that the new <prev> -> <next> dependency would not
1474 * create a circular dependency in the graph. (We do this by
1475 * forward-recursing into the graph starting at <next>, and
1476 * checking whether we can reach <prev>.)
1478 * We are using global variables to control the recursion, to
1479 * keep the stackframe size of the recursive functions low:
1481 check_source
= next
;
1482 check_target
= prev
;
1483 if (!(check_noncircular(hlock_class(next
), 0)))
1484 return print_circular_bug_tail();
1486 if (!check_prev_add_irq(curr
, prev
, next
))
1490 * For recursive read-locks we do all the dependency checks,
1491 * but we dont store read-triggered dependencies (only
1492 * write-triggered dependencies). This ensures that only the
1493 * write-side dependencies matter, and that if for example a
1494 * write-lock never takes any other locks, then the reads are
1495 * equivalent to a NOP.
1497 if (next
->read
== 2 || prev
->read
== 2)
1500 * Is the <prev> -> <next> dependency already present?
1502 * (this may occur even though this is a new chain: consider
1503 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1504 * chains - the second one will be new, but L1 already has
1505 * L2 added to its dependency list, due to the first chain.)
1507 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1508 if (entry
->class == hlock_class(next
)) {
1510 entry
->distance
= 1;
1516 * Ok, all validations passed, add the new lock
1517 * to the previous lock's dependency list:
1519 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1520 &hlock_class(prev
)->locks_after
,
1521 next
->acquire_ip
, distance
);
1526 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1527 &hlock_class(next
)->locks_before
,
1528 next
->acquire_ip
, distance
);
1533 * Debugging printouts:
1535 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1537 printk("\n new dependency: ");
1538 print_lock_name(hlock_class(prev
));
1540 print_lock_name(hlock_class(next
));
1543 return graph_lock();
1549 * Add the dependency to all directly-previous locks that are 'relevant'.
1550 * The ones that are relevant are (in increasing distance from curr):
1551 * all consecutive trylock entries and the final non-trylock entry - or
1552 * the end of this context's lock-chain - whichever comes first.
1555 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1557 int depth
= curr
->lockdep_depth
;
1558 struct held_lock
*hlock
;
1563 * Depth must not be zero for a non-head lock:
1568 * At least two relevant locks must exist for this
1571 if (curr
->held_locks
[depth
].irq_context
!=
1572 curr
->held_locks
[depth
-1].irq_context
)
1576 int distance
= curr
->lockdep_depth
- depth
+ 1;
1577 hlock
= curr
->held_locks
+ depth
-1;
1579 * Only non-recursive-read entries get new dependencies
1582 if (hlock
->read
!= 2) {
1583 if (!check_prev_add(curr
, hlock
, next
, distance
))
1586 * Stop after the first non-trylock entry,
1587 * as non-trylock entries have added their
1588 * own direct dependencies already, so this
1589 * lock is connected to them indirectly:
1591 if (!hlock
->trylock
)
1596 * End of lock-stack?
1601 * Stop the search if we cross into another context:
1603 if (curr
->held_locks
[depth
].irq_context
!=
1604 curr
->held_locks
[depth
-1].irq_context
)
1609 if (!debug_locks_off_graph_unlock())
1617 unsigned long nr_lock_chains
;
1618 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1619 int nr_chain_hlocks
;
1620 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1622 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1624 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1628 * Look up a dependency chain. If the key is not present yet then
1629 * add it and return 1 - in this case the new dependency chain is
1630 * validated. If the key is already hashed, return 0.
1631 * (On return with 1 graph_lock is held.)
1633 static inline int lookup_chain_cache(struct task_struct
*curr
,
1634 struct held_lock
*hlock
,
1637 struct lock_class
*class = hlock_class(hlock
);
1638 struct list_head
*hash_head
= chainhashentry(chain_key
);
1639 struct lock_chain
*chain
;
1640 struct held_lock
*hlock_curr
, *hlock_next
;
1643 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1646 * We can walk it lock-free, because entries only get added
1649 list_for_each_entry(chain
, hash_head
, entry
) {
1650 if (chain
->chain_key
== chain_key
) {
1652 debug_atomic_inc(&chain_lookup_hits
);
1653 if (very_verbose(class))
1654 printk("\nhash chain already cached, key: "
1655 "%016Lx tail class: [%p] %s\n",
1656 (unsigned long long)chain_key
,
1657 class->key
, class->name
);
1661 if (very_verbose(class))
1662 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1663 (unsigned long long)chain_key
, class->key
, class->name
);
1665 * Allocate a new chain entry from the static array, and add
1671 * We have to walk the chain again locked - to avoid duplicates:
1673 list_for_each_entry(chain
, hash_head
, entry
) {
1674 if (chain
->chain_key
== chain_key
) {
1679 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1680 if (!debug_locks_off_graph_unlock())
1683 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1684 printk("turning off the locking correctness validator.\n");
1687 chain
= lock_chains
+ nr_lock_chains
++;
1688 chain
->chain_key
= chain_key
;
1689 chain
->irq_context
= hlock
->irq_context
;
1690 /* Find the first held_lock of current chain */
1692 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
1693 hlock_curr
= curr
->held_locks
+ i
;
1694 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
1699 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
1700 cn
= nr_chain_hlocks
;
1701 while (cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
) {
1702 n
= cmpxchg(&nr_chain_hlocks
, cn
, cn
+ chain
->depth
);
1707 if (likely(cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
1709 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
1710 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
1711 chain_hlocks
[chain
->base
+ j
] = lock_id
;
1713 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
1715 list_add_tail_rcu(&chain
->entry
, hash_head
);
1716 debug_atomic_inc(&chain_lookup_misses
);
1722 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1723 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
1726 * Trylock needs to maintain the stack of held locks, but it
1727 * does not add new dependencies, because trylock can be done
1730 * We look up the chain_key and do the O(N^2) check and update of
1731 * the dependencies only if this is a new dependency chain.
1732 * (If lookup_chain_cache() returns with 1 it acquires
1733 * graph_lock for us)
1735 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1736 lookup_chain_cache(curr
, hlock
, chain_key
)) {
1738 * Check whether last held lock:
1740 * - is irq-safe, if this lock is irq-unsafe
1741 * - is softirq-safe, if this lock is hardirq-unsafe
1743 * And check whether the new lock's dependency graph
1744 * could lead back to the previous lock.
1746 * any of these scenarios could lead to a deadlock. If
1749 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1754 * Mark recursive read, as we jump over it when
1755 * building dependencies (just like we jump over
1761 * Add dependency only if this lock is not the head
1762 * of the chain, and if it's not a secondary read-lock:
1764 if (!chain_head
&& ret
!= 2)
1765 if (!check_prevs_add(curr
, hlock
))
1769 /* after lookup_chain_cache(): */
1770 if (unlikely(!debug_locks
))
1776 static inline int validate_chain(struct task_struct
*curr
,
1777 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1778 int chain_head
, u64 chain_key
)
1785 * We are building curr_chain_key incrementally, so double-check
1786 * it from scratch, to make sure that it's done correctly:
1788 static void check_chain_key(struct task_struct
*curr
)
1790 #ifdef CONFIG_DEBUG_LOCKDEP
1791 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1795 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1796 hlock
= curr
->held_locks
+ i
;
1797 if (chain_key
!= hlock
->prev_chain_key
) {
1799 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1800 curr
->lockdep_depth
, i
,
1801 (unsigned long long)chain_key
,
1802 (unsigned long long)hlock
->prev_chain_key
);
1805 id
= hlock
->class_idx
- 1;
1806 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1809 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1810 hlock
->irq_context
))
1812 chain_key
= iterate_chain_key(chain_key
, id
);
1815 if (chain_key
!= curr
->curr_chain_key
) {
1817 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1818 curr
->lockdep_depth
, i
,
1819 (unsigned long long)chain_key
,
1820 (unsigned long long)curr
->curr_chain_key
);
1826 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1827 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1829 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1832 printk("\n=================================\n");
1833 printk( "[ INFO: inconsistent lock state ]\n");
1834 print_kernel_version();
1835 printk( "---------------------------------\n");
1837 printk("inconsistent {%s} -> {%s} usage.\n",
1838 usage_str
[prev_bit
], usage_str
[new_bit
]);
1840 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1841 curr
->comm
, task_pid_nr(curr
),
1842 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1843 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1844 trace_hardirqs_enabled(curr
),
1845 trace_softirqs_enabled(curr
));
1848 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1849 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
1851 print_irqtrace_events(curr
);
1852 printk("\nother info that might help us debug this:\n");
1853 lockdep_print_held_locks(curr
);
1855 printk("\nstack backtrace:\n");
1862 * Print out an error if an invalid bit is set:
1865 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1866 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1868 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
1869 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1873 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1874 enum lock_usage_bit new_bit
);
1876 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1879 * print irq inversion bug:
1882 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1883 struct held_lock
*this, int forwards
,
1884 const char *irqclass
)
1886 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1889 printk("\n=========================================================\n");
1890 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1891 print_kernel_version();
1892 printk( "---------------------------------------------------------\n");
1893 printk("%s/%d just changed the state of lock:\n",
1894 curr
->comm
, task_pid_nr(curr
));
1897 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
1899 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
1900 print_lock_name(other
);
1901 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1903 printk("\nother info that might help us debug this:\n");
1904 lockdep_print_held_locks(curr
);
1906 printk("\nthe first lock's dependencies:\n");
1907 print_lock_dependencies(hlock_class(this), 0);
1909 printk("\nthe second lock's dependencies:\n");
1910 print_lock_dependencies(other
, 0);
1912 printk("\nstack backtrace:\n");
1919 * Prove that in the forwards-direction subgraph starting at <this>
1920 * there is no lock matching <mask>:
1923 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1924 enum lock_usage_bit bit
, const char *irqclass
)
1928 find_usage_bit
= bit
;
1929 /* fills in <forwards_match> */
1930 ret
= find_usage_forwards(hlock_class(this), 0);
1931 if (!ret
|| ret
== 1)
1934 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1938 * Prove that in the backwards-direction subgraph starting at <this>
1939 * there is no lock matching <mask>:
1942 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1943 enum lock_usage_bit bit
, const char *irqclass
)
1947 find_usage_bit
= bit
;
1948 /* fills in <backwards_match> */
1949 ret
= find_usage_backwards(hlock_class(this), 0);
1950 if (!ret
|| ret
== 1)
1953 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1956 void print_irqtrace_events(struct task_struct
*curr
)
1958 printk("irq event stamp: %u\n", curr
->irq_events
);
1959 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1960 print_ip_sym(curr
->hardirq_enable_ip
);
1961 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1962 print_ip_sym(curr
->hardirq_disable_ip
);
1963 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1964 print_ip_sym(curr
->softirq_enable_ip
);
1965 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1966 print_ip_sym(curr
->softirq_disable_ip
);
1969 static int HARDIRQ_verbose(struct lock_class
*class)
1972 return class_filter(class);
1977 static int SOFTIRQ_verbose(struct lock_class
*class)
1980 return class_filter(class);
1985 static int RECLAIM_FS_verbose(struct lock_class
*class)
1988 return class_filter(class);
1993 #define STRICT_READ_CHECKS 1
1995 static int (*state_verbose_f
[])(struct lock_class
*class) = {
1996 #define LOCKDEP_STATE(__STATE) \
1998 #include "lockdep_states.h"
1999 #undef LOCKDEP_STATE
2002 static inline int state_verbose(enum lock_usage_bit bit
,
2003 struct lock_class
*class)
2005 return state_verbose_f
[bit
>> 2](class);
2008 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2009 enum lock_usage_bit bit
, const char *name
);
2012 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2013 enum lock_usage_bit new_bit
)
2015 int excl_bit
= exclusive_bit(new_bit
);
2016 int read
= new_bit
& 1;
2017 int dir
= new_bit
& 2;
2020 * mark USED_IN has to look forwards -- to ensure no dependency
2021 * has ENABLED state, which would allow recursion deadlocks.
2023 * mark ENABLED has to look backwards -- to ensure no dependee
2024 * has USED_IN state, which, again, would allow recursion deadlocks.
2026 check_usage_f usage
= dir
?
2027 check_usage_backwards
: check_usage_forwards
;
2030 * Validate that this particular lock does not have conflicting
2033 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2037 * Validate that the lock dependencies don't have conflicting usage
2040 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2041 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2045 * Check for read in write conflicts
2048 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2051 if (STRICT_READ_CHECKS
&&
2052 !usage(curr
, this, excl_bit
+ 1,
2053 state_name(new_bit
+ 1)))
2057 if (state_verbose(new_bit
, hlock_class(this)))
2064 #define LOCKDEP_STATE(__STATE) __STATE,
2065 #include "lockdep_states.h"
2066 #undef LOCKDEP_STATE
2070 * Mark all held locks with a usage bit:
2073 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2075 enum lock_usage_bit usage_bit
;
2076 struct held_lock
*hlock
;
2079 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2080 hlock
= curr
->held_locks
+ i
;
2082 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2084 usage_bit
+= 1; /* READ */
2086 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2088 if (!mark_lock(curr
, hlock
, usage_bit
))
2096 * Debugging helper: via this flag we know that we are in
2097 * 'early bootup code', and will warn about any invalid irqs-on event:
2099 static int early_boot_irqs_enabled
;
2101 void early_boot_irqs_off(void)
2103 early_boot_irqs_enabled
= 0;
2106 void early_boot_irqs_on(void)
2108 early_boot_irqs_enabled
= 1;
2112 * Hardirqs will be enabled:
2114 void trace_hardirqs_on_caller(unsigned long ip
)
2116 struct task_struct
*curr
= current
;
2118 time_hardirqs_on(CALLER_ADDR0
, ip
);
2120 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2123 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2126 if (unlikely(curr
->hardirqs_enabled
)) {
2127 debug_atomic_inc(&redundant_hardirqs_on
);
2130 /* we'll do an OFF -> ON transition: */
2131 curr
->hardirqs_enabled
= 1;
2133 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2135 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2138 * We are going to turn hardirqs on, so set the
2139 * usage bit for all held locks:
2141 if (!mark_held_locks(curr
, HARDIRQ
))
2144 * If we have softirqs enabled, then set the usage
2145 * bit for all held locks. (disabled hardirqs prevented
2146 * this bit from being set before)
2148 if (curr
->softirqs_enabled
)
2149 if (!mark_held_locks(curr
, SOFTIRQ
))
2152 curr
->hardirq_enable_ip
= ip
;
2153 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2154 debug_atomic_inc(&hardirqs_on_events
);
2156 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2158 void trace_hardirqs_on(void)
2160 trace_hardirqs_on_caller(CALLER_ADDR0
);
2162 EXPORT_SYMBOL(trace_hardirqs_on
);
2165 * Hardirqs were disabled:
2167 void trace_hardirqs_off_caller(unsigned long ip
)
2169 struct task_struct
*curr
= current
;
2171 time_hardirqs_off(CALLER_ADDR0
, ip
);
2173 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2176 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2179 if (curr
->hardirqs_enabled
) {
2181 * We have done an ON -> OFF transition:
2183 curr
->hardirqs_enabled
= 0;
2184 curr
->hardirq_disable_ip
= ip
;
2185 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2186 debug_atomic_inc(&hardirqs_off_events
);
2188 debug_atomic_inc(&redundant_hardirqs_off
);
2190 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2192 void trace_hardirqs_off(void)
2194 trace_hardirqs_off_caller(CALLER_ADDR0
);
2196 EXPORT_SYMBOL(trace_hardirqs_off
);
2199 * Softirqs will be enabled:
2201 void trace_softirqs_on(unsigned long ip
)
2203 struct task_struct
*curr
= current
;
2205 if (unlikely(!debug_locks
))
2208 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2211 if (curr
->softirqs_enabled
) {
2212 debug_atomic_inc(&redundant_softirqs_on
);
2217 * We'll do an OFF -> ON transition:
2219 curr
->softirqs_enabled
= 1;
2220 curr
->softirq_enable_ip
= ip
;
2221 curr
->softirq_enable_event
= ++curr
->irq_events
;
2222 debug_atomic_inc(&softirqs_on_events
);
2224 * We are going to turn softirqs on, so set the
2225 * usage bit for all held locks, if hardirqs are
2228 if (curr
->hardirqs_enabled
)
2229 mark_held_locks(curr
, SOFTIRQ
);
2233 * Softirqs were disabled:
2235 void trace_softirqs_off(unsigned long ip
)
2237 struct task_struct
*curr
= current
;
2239 if (unlikely(!debug_locks
))
2242 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2245 if (curr
->softirqs_enabled
) {
2247 * We have done an ON -> OFF transition:
2249 curr
->softirqs_enabled
= 0;
2250 curr
->softirq_disable_ip
= ip
;
2251 curr
->softirq_disable_event
= ++curr
->irq_events
;
2252 debug_atomic_inc(&softirqs_off_events
);
2253 DEBUG_LOCKS_WARN_ON(!softirq_count());
2255 debug_atomic_inc(&redundant_softirqs_off
);
2258 void lockdep_trace_alloc(gfp_t gfp_mask
)
2260 struct task_struct
*curr
= current
;
2262 if (unlikely(!debug_locks
))
2265 /* no reclaim without waiting on it */
2266 if (!(gfp_mask
& __GFP_WAIT
))
2269 /* this guy won't enter reclaim */
2270 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2273 /* We're only interested __GFP_FS allocations for now */
2274 if (!(gfp_mask
& __GFP_FS
))
2277 if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2280 mark_held_locks(curr
, RECLAIM_FS
);
2283 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2286 * If non-trylock use in a hardirq or softirq context, then
2287 * mark the lock as used in these contexts:
2289 if (!hlock
->trylock
) {
2291 if (curr
->hardirq_context
)
2292 if (!mark_lock(curr
, hlock
,
2293 LOCK_USED_IN_HARDIRQ_READ
))
2295 if (curr
->softirq_context
)
2296 if (!mark_lock(curr
, hlock
,
2297 LOCK_USED_IN_SOFTIRQ_READ
))
2300 if (curr
->hardirq_context
)
2301 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2303 if (curr
->softirq_context
)
2304 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2308 if (!hlock
->hardirqs_off
) {
2310 if (!mark_lock(curr
, hlock
,
2311 LOCK_ENABLED_HARDIRQ_READ
))
2313 if (curr
->softirqs_enabled
)
2314 if (!mark_lock(curr
, hlock
,
2315 LOCK_ENABLED_SOFTIRQ_READ
))
2318 if (!mark_lock(curr
, hlock
,
2319 LOCK_ENABLED_HARDIRQ
))
2321 if (curr
->softirqs_enabled
)
2322 if (!mark_lock(curr
, hlock
,
2323 LOCK_ENABLED_SOFTIRQ
))
2329 * We reuse the irq context infrastructure more broadly as a general
2330 * context checking code. This tests GFP_FS recursion (a lock taken
2331 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2334 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2336 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2339 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2347 static int separate_irq_context(struct task_struct
*curr
,
2348 struct held_lock
*hlock
)
2350 unsigned int depth
= curr
->lockdep_depth
;
2353 * Keep track of points where we cross into an interrupt context:
2355 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2356 curr
->softirq_context
;
2358 struct held_lock
*prev_hlock
;
2360 prev_hlock
= curr
->held_locks
+ depth
-1;
2362 * If we cross into another context, reset the
2363 * hash key (this also prevents the checking and the
2364 * adding of the dependency to 'prev'):
2366 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2375 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2376 enum lock_usage_bit new_bit
)
2382 static inline int mark_irqflags(struct task_struct
*curr
,
2383 struct held_lock
*hlock
)
2388 static inline int separate_irq_context(struct task_struct
*curr
,
2389 struct held_lock
*hlock
)
2394 void lockdep_trace_alloc(gfp_t gfp_mask
)
2401 * Mark a lock with a usage bit, and validate the state transition:
2403 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2404 enum lock_usage_bit new_bit
)
2406 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2409 * If already set then do not dirty the cacheline,
2410 * nor do any checks:
2412 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2418 * Make sure we didnt race:
2420 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2425 hlock_class(this)->usage_mask
|= new_mask
;
2427 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2431 #define LOCKDEP_STATE(__STATE) \
2432 case LOCK_USED_IN_##__STATE: \
2433 case LOCK_USED_IN_##__STATE##_READ: \
2434 case LOCK_ENABLED_##__STATE: \
2435 case LOCK_ENABLED_##__STATE##_READ:
2436 #include "lockdep_states.h"
2437 #undef LOCKDEP_STATE
2438 ret
= mark_lock_irq(curr
, this, new_bit
);
2443 debug_atomic_dec(&nr_unused_locks
);
2446 if (!debug_locks_off_graph_unlock())
2455 * We must printk outside of the graph_lock:
2458 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2460 print_irqtrace_events(curr
);
2468 * Initialize a lock instance's lock-class mapping info:
2470 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2471 struct lock_class_key
*key
, int subclass
)
2473 if (unlikely(!debug_locks
))
2476 if (DEBUG_LOCKS_WARN_ON(!key
))
2478 if (DEBUG_LOCKS_WARN_ON(!name
))
2481 * Sanity check, the lock-class key must be persistent:
2483 if (!static_obj(key
)) {
2484 printk("BUG: key %p not in .data!\n", key
);
2485 DEBUG_LOCKS_WARN_ON(1);
2490 lock
->class_cache
= NULL
;
2491 #ifdef CONFIG_LOCK_STAT
2492 lock
->cpu
= raw_smp_processor_id();
2495 register_lock_class(lock
, subclass
, 1);
2497 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2500 * This gets called for every mutex_lock*()/spin_lock*() operation.
2501 * We maintain the dependency maps and validate the locking attempt:
2503 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2504 int trylock
, int read
, int check
, int hardirqs_off
,
2505 struct lockdep_map
*nest_lock
, unsigned long ip
)
2507 struct task_struct
*curr
= current
;
2508 struct lock_class
*class = NULL
;
2509 struct held_lock
*hlock
;
2510 unsigned int depth
, id
;
2517 if (unlikely(!debug_locks
))
2520 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2523 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2525 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2526 printk("turning off the locking correctness validator.\n");
2531 class = lock
->class_cache
;
2533 * Not cached yet or subclass?
2535 if (unlikely(!class)) {
2536 class = register_lock_class(lock
, subclass
, 0);
2540 debug_atomic_inc((atomic_t
*)&class->ops
);
2541 if (very_verbose(class)) {
2542 printk("\nacquire class [%p] %s", class->key
, class->name
);
2543 if (class->name_version
> 1)
2544 printk("#%d", class->name_version
);
2550 * Add the lock to the list of currently held locks.
2551 * (we dont increase the depth just yet, up until the
2552 * dependency checks are done)
2554 depth
= curr
->lockdep_depth
;
2555 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2558 hlock
= curr
->held_locks
+ depth
;
2559 if (DEBUG_LOCKS_WARN_ON(!class))
2561 hlock
->class_idx
= class - lock_classes
+ 1;
2562 hlock
->acquire_ip
= ip
;
2563 hlock
->instance
= lock
;
2564 hlock
->nest_lock
= nest_lock
;
2565 hlock
->trylock
= trylock
;
2567 hlock
->check
= check
;
2568 hlock
->hardirqs_off
= !!hardirqs_off
;
2569 #ifdef CONFIG_LOCK_STAT
2570 hlock
->waittime_stamp
= 0;
2571 hlock
->holdtime_stamp
= sched_clock();
2574 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2577 /* mark it as used: */
2578 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2582 * Calculate the chain hash: it's the combined hash of all the
2583 * lock keys along the dependency chain. We save the hash value
2584 * at every step so that we can get the current hash easily
2585 * after unlock. The chain hash is then used to cache dependency
2588 * The 'key ID' is what is the most compact key value to drive
2589 * the hash, not class->key.
2591 id
= class - lock_classes
;
2592 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2595 chain_key
= curr
->curr_chain_key
;
2597 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2602 hlock
->prev_chain_key
= chain_key
;
2603 if (separate_irq_context(curr
, hlock
)) {
2607 chain_key
= iterate_chain_key(chain_key
, id
);
2609 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
2612 curr
->curr_chain_key
= chain_key
;
2613 curr
->lockdep_depth
++;
2614 check_chain_key(curr
);
2615 #ifdef CONFIG_DEBUG_LOCKDEP
2616 if (unlikely(!debug_locks
))
2619 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2621 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2622 printk("turning off the locking correctness validator.\n");
2626 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2627 max_lockdep_depth
= curr
->lockdep_depth
;
2633 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2636 if (!debug_locks_off())
2638 if (debug_locks_silent
)
2641 printk("\n=====================================\n");
2642 printk( "[ BUG: bad unlock balance detected! ]\n");
2643 printk( "-------------------------------------\n");
2644 printk("%s/%d is trying to release lock (",
2645 curr
->comm
, task_pid_nr(curr
));
2646 print_lockdep_cache(lock
);
2649 printk("but there are no more locks to release!\n");
2650 printk("\nother info that might help us debug this:\n");
2651 lockdep_print_held_locks(curr
);
2653 printk("\nstack backtrace:\n");
2660 * Common debugging checks for both nested and non-nested unlock:
2662 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2665 if (unlikely(!debug_locks
))
2667 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2670 if (curr
->lockdep_depth
<= 0)
2671 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2677 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
2678 struct lock_class_key
*key
, unsigned int subclass
,
2681 struct task_struct
*curr
= current
;
2682 struct held_lock
*hlock
, *prev_hlock
;
2683 struct lock_class
*class;
2687 depth
= curr
->lockdep_depth
;
2688 if (DEBUG_LOCKS_WARN_ON(!depth
))
2692 for (i
= depth
-1; i
>= 0; i
--) {
2693 hlock
= curr
->held_locks
+ i
;
2695 * We must not cross into another context:
2697 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2699 if (hlock
->instance
== lock
)
2703 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2706 lockdep_init_map(lock
, name
, key
, 0);
2707 class = register_lock_class(lock
, subclass
, 0);
2708 hlock
->class_idx
= class - lock_classes
+ 1;
2710 curr
->lockdep_depth
= i
;
2711 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2713 for (; i
< depth
; i
++) {
2714 hlock
= curr
->held_locks
+ i
;
2715 if (!__lock_acquire(hlock
->instance
,
2716 hlock_class(hlock
)->subclass
, hlock
->trylock
,
2717 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2718 hlock
->nest_lock
, hlock
->acquire_ip
))
2722 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
2728 * Remove the lock to the list of currently held locks in a
2729 * potentially non-nested (out of order) manner. This is a
2730 * relatively rare operation, as all the unlock APIs default
2731 * to nested mode (which uses lock_release()):
2734 lock_release_non_nested(struct task_struct
*curr
,
2735 struct lockdep_map
*lock
, unsigned long ip
)
2737 struct held_lock
*hlock
, *prev_hlock
;
2742 * Check whether the lock exists in the current stack
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 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2764 lock_release_holdtime(hlock
);
2767 * We have the right lock to unlock, 'hlock' points to it.
2768 * Now we remove it from the stack, and add back the other
2769 * entries (if any), recalculating the hash along the way:
2771 curr
->lockdep_depth
= i
;
2772 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2774 for (i
++; i
< depth
; i
++) {
2775 hlock
= curr
->held_locks
+ i
;
2776 if (!__lock_acquire(hlock
->instance
,
2777 hlock_class(hlock
)->subclass
, hlock
->trylock
,
2778 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2779 hlock
->nest_lock
, hlock
->acquire_ip
))
2783 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2789 * Remove the lock to the list of currently held locks - this gets
2790 * called on mutex_unlock()/spin_unlock*() (or on a failed
2791 * mutex_lock_interruptible()). This is done for unlocks that nest
2792 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2794 static int lock_release_nested(struct task_struct
*curr
,
2795 struct lockdep_map
*lock
, unsigned long ip
)
2797 struct held_lock
*hlock
;
2801 * Pop off the top of the lock stack:
2803 depth
= curr
->lockdep_depth
- 1;
2804 hlock
= curr
->held_locks
+ depth
;
2807 * Is the unlock non-nested:
2809 if (hlock
->instance
!= lock
)
2810 return lock_release_non_nested(curr
, lock
, ip
);
2811 curr
->lockdep_depth
--;
2813 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2816 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2818 lock_release_holdtime(hlock
);
2820 #ifdef CONFIG_DEBUG_LOCKDEP
2821 hlock
->prev_chain_key
= 0;
2822 hlock
->class_idx
= 0;
2823 hlock
->acquire_ip
= 0;
2824 hlock
->irq_context
= 0;
2830 * Remove the lock to the list of currently held locks - this gets
2831 * called on mutex_unlock()/spin_unlock*() (or on a failed
2832 * mutex_lock_interruptible()). This is done for unlocks that nest
2833 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2836 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2838 struct task_struct
*curr
= current
;
2840 if (!check_unlock(curr
, lock
, ip
))
2844 if (!lock_release_nested(curr
, lock
, ip
))
2847 if (!lock_release_non_nested(curr
, lock
, ip
))
2851 check_chain_key(curr
);
2855 * Check whether we follow the irq-flags state precisely:
2857 static void check_flags(unsigned long flags
)
2859 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2860 defined(CONFIG_TRACE_IRQFLAGS)
2864 if (irqs_disabled_flags(flags
)) {
2865 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
2866 printk("possible reason: unannotated irqs-off.\n");
2869 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
2870 printk("possible reason: unannotated irqs-on.\n");
2875 * We dont accurately track softirq state in e.g.
2876 * hardirq contexts (such as on 4KSTACKS), so only
2877 * check if not in hardirq contexts:
2879 if (!hardirq_count()) {
2880 if (softirq_count())
2881 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2883 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2887 print_irqtrace_events(current
);
2891 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
2892 struct lock_class_key
*key
, unsigned int subclass
,
2895 unsigned long flags
;
2897 if (unlikely(current
->lockdep_recursion
))
2900 raw_local_irq_save(flags
);
2901 current
->lockdep_recursion
= 1;
2903 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
2904 check_chain_key(current
);
2905 current
->lockdep_recursion
= 0;
2906 raw_local_irq_restore(flags
);
2908 EXPORT_SYMBOL_GPL(lock_set_class
);
2910 DEFINE_TRACE(lock_acquire
);
2913 * We are not always called with irqs disabled - do that here,
2914 * and also avoid lockdep recursion:
2916 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2917 int trylock
, int read
, int check
,
2918 struct lockdep_map
*nest_lock
, unsigned long ip
)
2920 unsigned long flags
;
2922 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
2924 if (unlikely(current
->lockdep_recursion
))
2927 raw_local_irq_save(flags
);
2930 current
->lockdep_recursion
= 1;
2931 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2932 irqs_disabled_flags(flags
), nest_lock
, ip
);
2933 current
->lockdep_recursion
= 0;
2934 raw_local_irq_restore(flags
);
2936 EXPORT_SYMBOL_GPL(lock_acquire
);
2938 DEFINE_TRACE(lock_release
);
2940 void lock_release(struct lockdep_map
*lock
, int nested
,
2943 unsigned long flags
;
2945 trace_lock_release(lock
, nested
, ip
);
2947 if (unlikely(current
->lockdep_recursion
))
2950 raw_local_irq_save(flags
);
2952 current
->lockdep_recursion
= 1;
2953 __lock_release(lock
, nested
, ip
);
2954 current
->lockdep_recursion
= 0;
2955 raw_local_irq_restore(flags
);
2957 EXPORT_SYMBOL_GPL(lock_release
);
2959 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
2961 current
->lockdep_reclaim_gfp
= gfp_mask
;
2964 void lockdep_clear_current_reclaim_state(void)
2966 current
->lockdep_reclaim_gfp
= 0;
2969 #ifdef CONFIG_LOCK_STAT
2971 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2974 if (!debug_locks_off())
2976 if (debug_locks_silent
)
2979 printk("\n=================================\n");
2980 printk( "[ BUG: bad contention detected! ]\n");
2981 printk( "---------------------------------\n");
2982 printk("%s/%d is trying to contend lock (",
2983 curr
->comm
, task_pid_nr(curr
));
2984 print_lockdep_cache(lock
);
2987 printk("but there are no locks held!\n");
2988 printk("\nother info that might help us debug this:\n");
2989 lockdep_print_held_locks(curr
);
2991 printk("\nstack backtrace:\n");
2998 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3000 struct task_struct
*curr
= current
;
3001 struct held_lock
*hlock
, *prev_hlock
;
3002 struct lock_class_stats
*stats
;
3004 int i
, contention_point
, contending_point
;
3006 depth
= curr
->lockdep_depth
;
3007 if (DEBUG_LOCKS_WARN_ON(!depth
))
3011 for (i
= depth
-1; i
>= 0; i
--) {
3012 hlock
= curr
->held_locks
+ i
;
3014 * We must not cross into another context:
3016 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3018 if (hlock
->instance
== lock
)
3022 print_lock_contention_bug(curr
, lock
, ip
);
3026 hlock
->waittime_stamp
= sched_clock();
3028 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3029 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3032 stats
= get_lock_stats(hlock_class(hlock
));
3033 if (contention_point
< LOCKSTAT_POINTS
)
3034 stats
->contention_point
[contention_point
]++;
3035 if (contending_point
< LOCKSTAT_POINTS
)
3036 stats
->contending_point
[contending_point
]++;
3037 if (lock
->cpu
!= smp_processor_id())
3038 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3039 put_lock_stats(stats
);
3043 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3045 struct task_struct
*curr
= current
;
3046 struct held_lock
*hlock
, *prev_hlock
;
3047 struct lock_class_stats
*stats
;
3053 depth
= curr
->lockdep_depth
;
3054 if (DEBUG_LOCKS_WARN_ON(!depth
))
3058 for (i
= depth
-1; i
>= 0; i
--) {
3059 hlock
= curr
->held_locks
+ i
;
3061 * We must not cross into another context:
3063 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3065 if (hlock
->instance
== lock
)
3069 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3073 cpu
= smp_processor_id();
3074 if (hlock
->waittime_stamp
) {
3075 now
= sched_clock();
3076 waittime
= now
- hlock
->waittime_stamp
;
3077 hlock
->holdtime_stamp
= now
;
3080 stats
= get_lock_stats(hlock_class(hlock
));
3083 lock_time_inc(&stats
->read_waittime
, waittime
);
3085 lock_time_inc(&stats
->write_waittime
, waittime
);
3087 if (lock
->cpu
!= cpu
)
3088 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3089 put_lock_stats(stats
);
3095 DEFINE_TRACE(lock_contended
);
3097 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3099 unsigned long flags
;
3101 trace_lock_contended(lock
, ip
);
3103 if (unlikely(!lock_stat
))
3106 if (unlikely(current
->lockdep_recursion
))
3109 raw_local_irq_save(flags
);
3111 current
->lockdep_recursion
= 1;
3112 __lock_contended(lock
, ip
);
3113 current
->lockdep_recursion
= 0;
3114 raw_local_irq_restore(flags
);
3116 EXPORT_SYMBOL_GPL(lock_contended
);
3118 DEFINE_TRACE(lock_acquired
);
3120 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3122 unsigned long flags
;
3124 trace_lock_acquired(lock
, ip
);
3126 if (unlikely(!lock_stat
))
3129 if (unlikely(current
->lockdep_recursion
))
3132 raw_local_irq_save(flags
);
3134 current
->lockdep_recursion
= 1;
3135 __lock_acquired(lock
, ip
);
3136 current
->lockdep_recursion
= 0;
3137 raw_local_irq_restore(flags
);
3139 EXPORT_SYMBOL_GPL(lock_acquired
);
3143 * Used by the testsuite, sanitize the validator state
3144 * after a simulated failure:
3147 void lockdep_reset(void)
3149 unsigned long flags
;
3152 raw_local_irq_save(flags
);
3153 current
->curr_chain_key
= 0;
3154 current
->lockdep_depth
= 0;
3155 current
->lockdep_recursion
= 0;
3156 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3157 nr_hardirq_chains
= 0;
3158 nr_softirq_chains
= 0;
3159 nr_process_chains
= 0;
3161 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3162 INIT_LIST_HEAD(chainhash_table
+ i
);
3163 raw_local_irq_restore(flags
);
3166 static void zap_class(struct lock_class
*class)
3171 * Remove all dependencies this lock is
3174 for (i
= 0; i
< nr_list_entries
; i
++) {
3175 if (list_entries
[i
].class == class)
3176 list_del_rcu(&list_entries
[i
].entry
);
3179 * Unhash the class and remove it from the all_lock_classes list:
3181 list_del_rcu(&class->hash_entry
);
3182 list_del_rcu(&class->lock_entry
);
3187 static inline int within(const void *addr
, void *start
, unsigned long size
)
3189 return addr
>= start
&& addr
< start
+ size
;
3192 void lockdep_free_key_range(void *start
, unsigned long size
)
3194 struct lock_class
*class, *next
;
3195 struct list_head
*head
;
3196 unsigned long flags
;
3200 raw_local_irq_save(flags
);
3201 locked
= graph_lock();
3204 * Unhash all classes that were created by this module:
3206 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3207 head
= classhash_table
+ i
;
3208 if (list_empty(head
))
3210 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3211 if (within(class->key
, start
, size
))
3213 else if (within(class->name
, start
, size
))
3220 raw_local_irq_restore(flags
);
3223 void lockdep_reset_lock(struct lockdep_map
*lock
)
3225 struct lock_class
*class, *next
;
3226 struct list_head
*head
;
3227 unsigned long flags
;
3231 raw_local_irq_save(flags
);
3234 * Remove all classes this lock might have:
3236 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3238 * If the class exists we look it up and zap it:
3240 class = look_up_lock_class(lock
, j
);
3245 * Debug check: in the end all mapped classes should
3248 locked
= graph_lock();
3249 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3250 head
= classhash_table
+ i
;
3251 if (list_empty(head
))
3253 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3254 if (unlikely(class == lock
->class_cache
)) {
3255 if (debug_locks_off_graph_unlock())
3265 raw_local_irq_restore(flags
);
3268 void lockdep_init(void)
3273 * Some architectures have their own start_kernel()
3274 * code which calls lockdep_init(), while we also
3275 * call lockdep_init() from the start_kernel() itself,
3276 * and we want to initialize the hashes only once:
3278 if (lockdep_initialized
)
3281 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3282 INIT_LIST_HEAD(classhash_table
+ i
);
3284 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3285 INIT_LIST_HEAD(chainhash_table
+ i
);
3287 lockdep_initialized
= 1;
3290 void __init
lockdep_info(void)
3292 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3294 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3295 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3296 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3297 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3298 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3299 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3300 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3302 printk(" memory used by lock dependency info: %lu kB\n",
3303 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3304 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3305 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3306 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3307 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
3309 printk(" per task-struct memory footprint: %lu bytes\n",
3310 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3312 #ifdef CONFIG_DEBUG_LOCKDEP
3313 if (lockdep_init_error
) {
3314 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3315 printk("Call stack leading to lockdep invocation was:\n");
3316 print_stack_trace(&lockdep_init_trace
, 0);
3322 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3323 const void *mem_to
, struct held_lock
*hlock
)
3325 if (!debug_locks_off())
3327 if (debug_locks_silent
)
3330 printk("\n=========================\n");
3331 printk( "[ BUG: held lock freed! ]\n");
3332 printk( "-------------------------\n");
3333 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3334 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3336 lockdep_print_held_locks(curr
);
3338 printk("\nstack backtrace:\n");
3342 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3343 const void* lock_from
, unsigned long lock_len
)
3345 return lock_from
+ lock_len
<= mem_from
||
3346 mem_from
+ mem_len
<= lock_from
;
3350 * Called when kernel memory is freed (or unmapped), or if a lock
3351 * is destroyed or reinitialized - this code checks whether there is
3352 * any held lock in the memory range of <from> to <to>:
3354 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3356 struct task_struct
*curr
= current
;
3357 struct held_lock
*hlock
;
3358 unsigned long flags
;
3361 if (unlikely(!debug_locks
))
3364 local_irq_save(flags
);
3365 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3366 hlock
= curr
->held_locks
+ i
;
3368 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3369 sizeof(*hlock
->instance
)))
3372 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3375 local_irq_restore(flags
);
3377 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3379 static void print_held_locks_bug(struct task_struct
*curr
)
3381 if (!debug_locks_off())
3383 if (debug_locks_silent
)
3386 printk("\n=====================================\n");
3387 printk( "[ BUG: lock held at task exit time! ]\n");
3388 printk( "-------------------------------------\n");
3389 printk("%s/%d is exiting with locks still held!\n",
3390 curr
->comm
, task_pid_nr(curr
));
3391 lockdep_print_held_locks(curr
);
3393 printk("\nstack backtrace:\n");
3397 void debug_check_no_locks_held(struct task_struct
*task
)
3399 if (unlikely(task
->lockdep_depth
> 0))
3400 print_held_locks_bug(task
);
3403 void debug_show_all_locks(void)
3405 struct task_struct
*g
, *p
;
3409 if (unlikely(!debug_locks
)) {
3410 printk("INFO: lockdep is turned off.\n");
3413 printk("\nShowing all locks held in the system:\n");
3416 * Here we try to get the tasklist_lock as hard as possible,
3417 * if not successful after 2 seconds we ignore it (but keep
3418 * trying). This is to enable a debug printout even if a
3419 * tasklist_lock-holding task deadlocks or crashes.
3422 if (!read_trylock(&tasklist_lock
)) {
3424 printk("hm, tasklist_lock locked, retrying... ");
3427 printk(" #%d", 10-count
);
3431 printk(" ignoring it.\n");
3435 printk(KERN_CONT
" locked it.\n");
3438 do_each_thread(g
, p
) {
3440 * It's not reliable to print a task's held locks
3441 * if it's not sleeping (or if it's not the current
3444 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3446 if (p
->lockdep_depth
)
3447 lockdep_print_held_locks(p
);
3449 if (read_trylock(&tasklist_lock
))
3451 } while_each_thread(g
, p
);
3454 printk("=============================================\n\n");
3457 read_unlock(&tasklist_lock
);
3459 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3462 * Careful: only use this function if you are sure that
3463 * the task cannot run in parallel!
3465 void __debug_show_held_locks(struct task_struct
*task
)
3467 if (unlikely(!debug_locks
)) {
3468 printk("INFO: lockdep is turned off.\n");
3471 lockdep_print_held_locks(task
);
3473 EXPORT_SYMBOL_GPL(__debug_show_held_locks
);
3475 void debug_show_held_locks(struct task_struct
*task
)
3477 __debug_show_held_locks(task
);
3479 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3481 void lockdep_sys_exit(void)
3483 struct task_struct
*curr
= current
;
3485 if (unlikely(curr
->lockdep_depth
)) {
3486 if (!debug_locks_off())
3488 printk("\n================================================\n");
3489 printk( "[ BUG: lock held when returning to user space! ]\n");
3490 printk( "------------------------------------------------\n");
3491 printk("%s/%d is leaving the kernel with locks still held!\n",
3492 curr
->comm
, curr
->pid
);
3493 lockdep_print_held_locks(curr
);