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 <linux/bitops.h>
47 #include <asm/sections.h>
49 #include "lockdep_internals.h"
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/lock.h>
54 #ifdef CONFIG_PROVE_LOCKING
55 int prove_locking
= 1;
56 module_param(prove_locking
, int, 0644);
58 #define prove_locking 0
61 #ifdef CONFIG_LOCK_STAT
63 module_param(lock_stat
, int, 0644);
69 * lockdep_lock: protects the lockdep graph, the hashes and the
70 * class/list/hash allocators.
72 * This is one of the rare exceptions where it's justified
73 * to use a raw spinlock - we really dont want the spinlock
74 * code to recurse back into the lockdep code...
76 static raw_spinlock_t lockdep_lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
78 static int graph_lock(void)
80 __raw_spin_lock(&lockdep_lock
);
82 * Make sure that if another CPU detected a bug while
83 * walking the graph we dont change it (while the other
84 * CPU is busy printing out stuff with the graph lock
88 __raw_spin_unlock(&lockdep_lock
);
91 /* prevent any recursions within lockdep from causing deadlocks */
92 current
->lockdep_recursion
++;
96 static inline int graph_unlock(void)
98 if (debug_locks
&& !__raw_spin_is_locked(&lockdep_lock
))
99 return DEBUG_LOCKS_WARN_ON(1);
101 current
->lockdep_recursion
--;
102 __raw_spin_unlock(&lockdep_lock
);
107 * Turn lock debugging off and return with 0 if it was off already,
108 * and also release the graph lock:
110 static inline int debug_locks_off_graph_unlock(void)
112 int ret
= debug_locks_off();
114 __raw_spin_unlock(&lockdep_lock
);
119 static int lockdep_initialized
;
121 unsigned long nr_list_entries
;
122 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
125 * All data structures here are protected by the global debug_lock.
127 * Mutex key structs only get allocated, once during bootup, and never
128 * get freed - this significantly simplifies the debugging code.
130 unsigned long nr_lock_classes
;
131 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
133 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
135 if (!hlock
->class_idx
) {
136 DEBUG_LOCKS_WARN_ON(1);
139 return lock_classes
+ hlock
->class_idx
- 1;
142 #ifdef CONFIG_LOCK_STAT
143 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], lock_stats
);
145 static inline u64
lockstat_clock(void)
147 return cpu_clock(smp_processor_id());
150 static int lock_point(unsigned long points
[], unsigned long ip
)
154 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
155 if (points
[i
] == 0) {
166 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
171 if (time
< lt
->min
|| !lt
->nr
)
178 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
183 if (src
->max
> dst
->max
)
186 if (src
->min
< dst
->min
|| !dst
->nr
)
189 dst
->total
+= src
->total
;
193 struct lock_class_stats
lock_stats(struct lock_class
*class)
195 struct lock_class_stats stats
;
198 memset(&stats
, 0, sizeof(struct lock_class_stats
));
199 for_each_possible_cpu(cpu
) {
200 struct lock_class_stats
*pcs
=
201 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
203 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
204 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
206 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
207 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
209 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
210 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
212 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
213 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
215 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
216 stats
.bounces
[i
] += pcs
->bounces
[i
];
222 void clear_lock_stats(struct lock_class
*class)
226 for_each_possible_cpu(cpu
) {
227 struct lock_class_stats
*cpu_stats
=
228 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
230 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
232 memset(class->contention_point
, 0, sizeof(class->contention_point
));
233 memset(class->contending_point
, 0, sizeof(class->contending_point
));
236 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
238 return &get_cpu_var(lock_stats
)[class - lock_classes
];
241 static void put_lock_stats(struct lock_class_stats
*stats
)
243 put_cpu_var(lock_stats
);
246 static void lock_release_holdtime(struct held_lock
*hlock
)
248 struct lock_class_stats
*stats
;
254 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
256 stats
= get_lock_stats(hlock_class(hlock
));
258 lock_time_inc(&stats
->read_holdtime
, holdtime
);
260 lock_time_inc(&stats
->write_holdtime
, holdtime
);
261 put_lock_stats(stats
);
264 static inline void lock_release_holdtime(struct held_lock
*hlock
)
270 * We keep a global list of all lock classes. The list only grows,
271 * never shrinks. The list is only accessed with the lockdep
272 * spinlock lock held.
274 LIST_HEAD(all_lock_classes
);
277 * The lockdep classes are in a hash-table as well, for fast lookup:
279 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
280 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
281 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
282 #define classhashentry(key) (classhash_table + __classhashfn((key)))
284 static struct list_head classhash_table
[CLASSHASH_SIZE
];
287 * We put the lock dependency chains into a hash-table as well, to cache
290 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
291 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
292 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
293 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
295 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
298 * The hash key of the lock dependency chains is a hash itself too:
299 * it's a hash of all locks taken up to that lock, including that lock.
300 * It's a 64-bit hash, because it's important for the keys to be
303 #define iterate_chain_key(key1, key2) \
304 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
305 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
308 void lockdep_off(void)
310 current
->lockdep_recursion
++;
312 EXPORT_SYMBOL(lockdep_off
);
314 void lockdep_on(void)
316 current
->lockdep_recursion
--;
318 EXPORT_SYMBOL(lockdep_on
);
321 * Debugging switches:
325 #define VERY_VERBOSE 0
328 # define HARDIRQ_VERBOSE 1
329 # define SOFTIRQ_VERBOSE 1
330 # define RECLAIM_VERBOSE 1
332 # define HARDIRQ_VERBOSE 0
333 # define SOFTIRQ_VERBOSE 0
334 # define RECLAIM_VERBOSE 0
337 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
339 * Quick filtering for interesting events:
341 static int class_filter(struct lock_class
*class)
345 if (class->name_version
== 1 &&
346 !strcmp(class->name
, "lockname"))
348 if (class->name_version
== 1 &&
349 !strcmp(class->name
, "&struct->lockfield"))
352 /* Filter everything else. 1 would be to allow everything else */
357 static int verbose(struct lock_class
*class)
360 return class_filter(class);
366 * Stack-trace: tightly packed array of stack backtrace
367 * addresses. Protected by the graph_lock.
369 unsigned long nr_stack_trace_entries
;
370 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
372 static int save_trace(struct stack_trace
*trace
)
374 trace
->nr_entries
= 0;
375 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
376 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
380 save_stack_trace(trace
);
383 * Some daft arches put -1 at the end to indicate its a full trace.
385 * <rant> this is buggy anyway, since it takes a whole extra entry so a
386 * complete trace that maxes out the entries provided will be reported
387 * as incomplete, friggin useless </rant>
389 if (trace
->nr_entries
!= 0 &&
390 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
393 trace
->max_entries
= trace
->nr_entries
;
395 nr_stack_trace_entries
+= trace
->nr_entries
;
397 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
398 if (!debug_locks_off_graph_unlock())
401 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
402 printk("turning off the locking correctness validator.\n");
411 unsigned int nr_hardirq_chains
;
412 unsigned int nr_softirq_chains
;
413 unsigned int nr_process_chains
;
414 unsigned int max_lockdep_depth
;
416 #ifdef CONFIG_DEBUG_LOCKDEP
418 * We cannot printk in early bootup code. Not even early_printk()
419 * might work. So we mark any initialization errors and printk
420 * about it later on, in lockdep_info().
422 static int lockdep_init_error
;
423 static unsigned long lockdep_init_trace_data
[20];
424 static struct stack_trace lockdep_init_trace
= {
425 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
426 .entries
= lockdep_init_trace_data
,
430 * Various lockdep statistics:
432 atomic_t chain_lookup_hits
;
433 atomic_t chain_lookup_misses
;
434 atomic_t hardirqs_on_events
;
435 atomic_t hardirqs_off_events
;
436 atomic_t redundant_hardirqs_on
;
437 atomic_t redundant_hardirqs_off
;
438 atomic_t softirqs_on_events
;
439 atomic_t softirqs_off_events
;
440 atomic_t redundant_softirqs_on
;
441 atomic_t redundant_softirqs_off
;
442 atomic_t nr_unused_locks
;
443 atomic_t nr_cyclic_checks
;
444 atomic_t nr_find_usage_forwards_checks
;
445 atomic_t nr_find_usage_backwards_checks
;
452 #define __USAGE(__STATE) \
453 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
454 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
455 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
456 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
458 static const char *usage_str
[] =
460 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
461 #include "lockdep_states.h"
463 [LOCK_USED
] = "INITIAL USE",
466 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
468 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
471 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
476 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
480 if (class->usage_mask
& lock_flag(bit
+ 2))
482 if (class->usage_mask
& lock_flag(bit
)) {
484 if (class->usage_mask
& lock_flag(bit
+ 2))
491 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
495 #define LOCKDEP_STATE(__STATE) \
496 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
497 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
498 #include "lockdep_states.h"
504 static void print_lock_name(struct lock_class
*class)
506 char str
[KSYM_NAME_LEN
], usage
[LOCK_USAGE_CHARS
];
509 get_usage_chars(class, usage
);
513 name
= __get_key_name(class->key
, str
);
514 printk(" (%s", name
);
516 printk(" (%s", name
);
517 if (class->name_version
> 1)
518 printk("#%d", class->name_version
);
520 printk("/%d", class->subclass
);
522 printk("){%s}", usage
);
525 static void print_lockdep_cache(struct lockdep_map
*lock
)
528 char str
[KSYM_NAME_LEN
];
532 name
= __get_key_name(lock
->key
->subkeys
, str
);
537 static void print_lock(struct held_lock
*hlock
)
539 print_lock_name(hlock_class(hlock
));
541 print_ip_sym(hlock
->acquire_ip
);
544 static void lockdep_print_held_locks(struct task_struct
*curr
)
546 int i
, depth
= curr
->lockdep_depth
;
549 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
552 printk("%d lock%s held by %s/%d:\n",
553 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
555 for (i
= 0; i
< depth
; i
++) {
557 print_lock(curr
->held_locks
+ i
);
561 static void print_kernel_version(void)
563 printk("%s %.*s\n", init_utsname()->release
,
564 (int)strcspn(init_utsname()->version
, " "),
565 init_utsname()->version
);
568 static int very_verbose(struct lock_class
*class)
571 return class_filter(class);
577 * Is this the address of a static object:
579 static int static_obj(void *obj
)
581 unsigned long start
= (unsigned long) &_stext
,
582 end
= (unsigned long) &_end
,
583 addr
= (unsigned long) obj
;
591 if ((addr
>= start
) && (addr
< end
))
594 if (arch_is_kernel_data(addr
))
601 for_each_possible_cpu(i
) {
602 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
603 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
606 if ((addr
>= start
) && (addr
< end
))
614 return is_module_address(addr
);
618 * To make lock name printouts unique, we calculate a unique
619 * class->name_version generation counter:
621 static int count_matching_names(struct lock_class
*new_class
)
623 struct lock_class
*class;
626 if (!new_class
->name
)
629 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
630 if (new_class
->key
- new_class
->subclass
== class->key
)
631 return class->name_version
;
632 if (class->name
&& !strcmp(class->name
, new_class
->name
))
633 count
= max(count
, class->name_version
);
640 * Register a lock's class in the hash-table, if the class is not present
641 * yet. Otherwise we look it up. We cache the result in the lock object
642 * itself, so actual lookup of the hash should be once per lock object.
644 static inline struct lock_class
*
645 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
647 struct lockdep_subclass_key
*key
;
648 struct list_head
*hash_head
;
649 struct lock_class
*class;
651 #ifdef CONFIG_DEBUG_LOCKDEP
653 * If the architecture calls into lockdep before initializing
654 * the hashes then we'll warn about it later. (we cannot printk
657 if (unlikely(!lockdep_initialized
)) {
659 lockdep_init_error
= 1;
660 save_stack_trace(&lockdep_init_trace
);
665 * Static locks do not have their class-keys yet - for them the key
666 * is the lock object itself:
668 if (unlikely(!lock
->key
))
669 lock
->key
= (void *)lock
;
672 * NOTE: the class-key must be unique. For dynamic locks, a static
673 * lock_class_key variable is passed in through the mutex_init()
674 * (or spin_lock_init()) call - which acts as the key. For static
675 * locks we use the lock object itself as the key.
677 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
678 sizeof(struct lockdep_map
));
680 key
= lock
->key
->subkeys
+ subclass
;
682 hash_head
= classhashentry(key
);
685 * We can walk the hash lockfree, because the hash only
686 * grows, and we are careful when adding entries to the end:
688 list_for_each_entry(class, hash_head
, hash_entry
) {
689 if (class->key
== key
) {
690 WARN_ON_ONCE(class->name
!= lock
->name
);
699 * Register a lock's class in the hash-table, if the class is not present
700 * yet. Otherwise we look it up. We cache the result in the lock object
701 * itself, so actual lookup of the hash should be once per lock object.
703 static inline struct lock_class
*
704 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
706 struct lockdep_subclass_key
*key
;
707 struct list_head
*hash_head
;
708 struct lock_class
*class;
711 class = look_up_lock_class(lock
, subclass
);
716 * Debug-check: all keys must be persistent!
718 if (!static_obj(lock
->key
)) {
720 printk("INFO: trying to register non-static key.\n");
721 printk("the code is fine but needs lockdep annotation.\n");
722 printk("turning off the locking correctness validator.\n");
728 key
= lock
->key
->subkeys
+ subclass
;
729 hash_head
= classhashentry(key
);
731 raw_local_irq_save(flags
);
733 raw_local_irq_restore(flags
);
737 * We have to do the hash-walk again, to avoid races
740 list_for_each_entry(class, hash_head
, hash_entry
)
741 if (class->key
== key
)
744 * Allocate a new key from the static array, and add it to
747 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
748 if (!debug_locks_off_graph_unlock()) {
749 raw_local_irq_restore(flags
);
752 raw_local_irq_restore(flags
);
754 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
755 printk("turning off the locking correctness validator.\n");
759 class = lock_classes
+ nr_lock_classes
++;
760 debug_atomic_inc(&nr_unused_locks
);
762 class->name
= lock
->name
;
763 class->subclass
= subclass
;
764 INIT_LIST_HEAD(&class->lock_entry
);
765 INIT_LIST_HEAD(&class->locks_before
);
766 INIT_LIST_HEAD(&class->locks_after
);
767 class->name_version
= count_matching_names(class);
769 * We use RCU's safe list-add method to make
770 * parallel walking of the hash-list safe:
772 list_add_tail_rcu(&class->hash_entry
, hash_head
);
774 * Add it to the global list of classes:
776 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
778 if (verbose(class)) {
780 raw_local_irq_restore(flags
);
782 printk("\nnew class %p: %s", class->key
, class->name
);
783 if (class->name_version
> 1)
784 printk("#%d", class->name_version
);
788 raw_local_irq_save(flags
);
790 raw_local_irq_restore(flags
);
796 raw_local_irq_restore(flags
);
798 if (!subclass
|| force
)
799 lock
->class_cache
= class;
801 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
807 #ifdef CONFIG_PROVE_LOCKING
809 * Allocate a lockdep entry. (assumes the graph_lock held, returns
810 * with NULL on failure)
812 static struct lock_list
*alloc_list_entry(void)
814 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
815 if (!debug_locks_off_graph_unlock())
818 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
819 printk("turning off the locking correctness validator.\n");
823 return list_entries
+ nr_list_entries
++;
827 * Add a new dependency to the head of the list:
829 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
830 struct list_head
*head
, unsigned long ip
, int distance
)
832 struct lock_list
*entry
;
834 * Lock not present yet - get a new dependency struct and
835 * add it to the list:
837 entry
= alloc_list_entry();
841 if (!save_trace(&entry
->trace
))
845 entry
->distance
= distance
;
847 * Since we never remove from the dependency list, the list can
848 * be walked lockless by other CPUs, it's only allocation
849 * that must be protected by the spinlock. But this also means
850 * we must make new entries visible only once writes to the
851 * entry become visible - hence the RCU op:
853 list_add_tail_rcu(&entry
->entry
, head
);
859 * For good efficiency of modular, we use power of 2
861 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
862 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
865 * The circular_queue and helpers is used to implement the
866 * breadth-first search(BFS)algorithem, by which we can build
867 * the shortest path from the next lock to be acquired to the
868 * previous held lock if there is a circular between them.
870 struct circular_queue
{
871 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
872 unsigned int front
, rear
;
875 static struct circular_queue lock_cq
;
877 unsigned int max_bfs_queue_depth
;
879 static unsigned int lockdep_dependency_gen_id
;
881 static inline void __cq_init(struct circular_queue
*cq
)
883 cq
->front
= cq
->rear
= 0;
884 lockdep_dependency_gen_id
++;
887 static inline int __cq_empty(struct circular_queue
*cq
)
889 return (cq
->front
== cq
->rear
);
892 static inline int __cq_full(struct circular_queue
*cq
)
894 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
897 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
902 cq
->element
[cq
->rear
] = elem
;
903 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
907 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
912 *elem
= cq
->element
[cq
->front
];
913 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
917 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
919 return (cq
->rear
- cq
->front
) & CQ_MASK
;
922 static inline void mark_lock_accessed(struct lock_list
*lock
,
923 struct lock_list
*parent
)
927 nr
= lock
- list_entries
;
928 WARN_ON(nr
>= nr_list_entries
);
929 lock
->parent
= parent
;
930 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
933 static inline unsigned long lock_accessed(struct lock_list
*lock
)
937 nr
= lock
- list_entries
;
938 WARN_ON(nr
>= nr_list_entries
);
939 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
942 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
944 return child
->parent
;
947 static inline int get_lock_depth(struct lock_list
*child
)
950 struct lock_list
*parent
;
952 while ((parent
= get_lock_parent(child
))) {
959 static int __bfs(struct lock_list
*source_entry
,
961 int (*match
)(struct lock_list
*entry
, void *data
),
962 struct lock_list
**target_entry
,
965 struct lock_list
*entry
;
966 struct list_head
*head
;
967 struct circular_queue
*cq
= &lock_cq
;
970 if (match(source_entry
, data
)) {
971 *target_entry
= source_entry
;
977 head
= &source_entry
->class->locks_after
;
979 head
= &source_entry
->class->locks_before
;
981 if (list_empty(head
))
985 __cq_enqueue(cq
, (unsigned long)source_entry
);
987 while (!__cq_empty(cq
)) {
988 struct lock_list
*lock
;
990 __cq_dequeue(cq
, (unsigned long *)&lock
);
998 head
= &lock
->class->locks_after
;
1000 head
= &lock
->class->locks_before
;
1002 list_for_each_entry(entry
, head
, entry
) {
1003 if (!lock_accessed(entry
)) {
1004 unsigned int cq_depth
;
1005 mark_lock_accessed(entry
, lock
);
1006 if (match(entry
, data
)) {
1007 *target_entry
= entry
;
1012 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1016 cq_depth
= __cq_get_elem_count(cq
);
1017 if (max_bfs_queue_depth
< cq_depth
)
1018 max_bfs_queue_depth
= cq_depth
;
1026 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1028 int (*match
)(struct lock_list
*entry
, void *data
),
1029 struct lock_list
**target_entry
)
1031 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1035 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1037 int (*match
)(struct lock_list
*entry
, void *data
),
1038 struct lock_list
**target_entry
)
1040 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1045 * Recursive, forwards-direction lock-dependency checking, used for
1046 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1051 * Print a dependency chain entry (this is only done when a deadlock
1052 * has been detected):
1055 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1057 if (debug_locks_silent
)
1059 printk("\n-> #%u", depth
);
1060 print_lock_name(target
->class);
1062 print_stack_trace(&target
->trace
, 6);
1068 * When a circular dependency is detected, print the
1072 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1073 struct held_lock
*check_src
,
1074 struct held_lock
*check_tgt
)
1076 struct task_struct
*curr
= current
;
1078 if (debug_locks_silent
)
1081 printk("\n=======================================================\n");
1082 printk( "[ INFO: possible circular locking dependency detected ]\n");
1083 print_kernel_version();
1084 printk( "-------------------------------------------------------\n");
1085 printk("%s/%d is trying to acquire lock:\n",
1086 curr
->comm
, task_pid_nr(curr
));
1087 print_lock(check_src
);
1088 printk("\nbut task is already holding lock:\n");
1089 print_lock(check_tgt
);
1090 printk("\nwhich lock already depends on the new lock.\n\n");
1091 printk("\nthe existing dependency chain (in reverse order) is:\n");
1093 print_circular_bug_entry(entry
, depth
);
1098 static inline int class_equal(struct lock_list
*entry
, void *data
)
1100 return entry
->class == data
;
1103 static noinline
int print_circular_bug(struct lock_list
*this,
1104 struct lock_list
*target
,
1105 struct held_lock
*check_src
,
1106 struct held_lock
*check_tgt
)
1108 struct task_struct
*curr
= current
;
1109 struct lock_list
*parent
;
1112 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1115 if (!save_trace(&this->trace
))
1118 depth
= get_lock_depth(target
);
1120 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1122 parent
= get_lock_parent(target
);
1125 print_circular_bug_entry(parent
, --depth
);
1126 parent
= get_lock_parent(parent
);
1129 printk("\nother info that might help us debug this:\n\n");
1130 lockdep_print_held_locks(curr
);
1132 printk("\nstack backtrace:\n");
1138 static noinline
int print_bfs_bug(int ret
)
1140 if (!debug_locks_off_graph_unlock())
1143 WARN(1, "lockdep bfs error:%d\n", ret
);
1148 static int noop_count(struct lock_list
*entry
, void *data
)
1150 (*(unsigned long *)data
)++;
1154 unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1156 unsigned long count
= 0;
1157 struct lock_list
*uninitialized_var(target_entry
);
1159 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1163 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1165 unsigned long ret
, flags
;
1166 struct lock_list
this;
1171 local_irq_save(flags
);
1172 __raw_spin_lock(&lockdep_lock
);
1173 ret
= __lockdep_count_forward_deps(&this);
1174 __raw_spin_unlock(&lockdep_lock
);
1175 local_irq_restore(flags
);
1180 unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1182 unsigned long count
= 0;
1183 struct lock_list
*uninitialized_var(target_entry
);
1185 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1190 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1192 unsigned long ret
, flags
;
1193 struct lock_list
this;
1198 local_irq_save(flags
);
1199 __raw_spin_lock(&lockdep_lock
);
1200 ret
= __lockdep_count_backward_deps(&this);
1201 __raw_spin_unlock(&lockdep_lock
);
1202 local_irq_restore(flags
);
1208 * Prove that the dependency graph starting at <entry> can not
1209 * lead to <target>. Print an error and return 0 if it does.
1212 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1213 struct lock_list
**target_entry
)
1217 debug_atomic_inc(&nr_cyclic_checks
);
1219 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1224 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1226 * Forwards and backwards subgraph searching, for the purposes of
1227 * proving that two subgraphs can be connected by a new dependency
1228 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1231 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1233 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1239 * Find a node in the forwards-direction dependency sub-graph starting
1240 * at @root->class that matches @bit.
1242 * Return 0 if such a node exists in the subgraph, and put that node
1243 * into *@target_entry.
1245 * Return 1 otherwise and keep *@target_entry unchanged.
1246 * Return <0 on error.
1249 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1250 struct lock_list
**target_entry
)
1254 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1256 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1262 * Find a node in the backwards-direction dependency sub-graph starting
1263 * at @root->class that matches @bit.
1265 * Return 0 if such a node exists in the subgraph, and put that node
1266 * into *@target_entry.
1268 * Return 1 otherwise and keep *@target_entry unchanged.
1269 * Return <0 on error.
1272 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1273 struct lock_list
**target_entry
)
1277 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1279 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1284 static void print_lock_class_header(struct lock_class
*class, int depth
)
1288 printk("%*s->", depth
, "");
1289 print_lock_name(class);
1290 printk(" ops: %lu", class->ops
);
1293 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1294 if (class->usage_mask
& (1 << bit
)) {
1297 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1298 len
+= printk(" at:\n");
1299 print_stack_trace(class->usage_traces
+ bit
, len
);
1302 printk("%*s }\n", depth
, "");
1304 printk("%*s ... key at: ",depth
,"");
1305 print_ip_sym((unsigned long)class->key
);
1309 * printk the shortest lock dependencies from @start to @end in reverse order:
1312 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1313 struct lock_list
*root
)
1315 struct lock_list
*entry
= leaf
;
1318 /*compute depth from generated tree by BFS*/
1319 depth
= get_lock_depth(leaf
);
1322 print_lock_class_header(entry
->class, depth
);
1323 printk("%*s ... acquired at:\n", depth
, "");
1324 print_stack_trace(&entry
->trace
, 2);
1327 if (depth
== 0 && (entry
!= root
)) {
1328 printk("lockdep:%s bad BFS generated tree\n", __func__
);
1332 entry
= get_lock_parent(entry
);
1334 } while (entry
&& (depth
>= 0));
1340 print_bad_irq_dependency(struct task_struct
*curr
,
1341 struct lock_list
*prev_root
,
1342 struct lock_list
*next_root
,
1343 struct lock_list
*backwards_entry
,
1344 struct lock_list
*forwards_entry
,
1345 struct held_lock
*prev
,
1346 struct held_lock
*next
,
1347 enum lock_usage_bit bit1
,
1348 enum lock_usage_bit bit2
,
1349 const char *irqclass
)
1351 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1354 printk("\n======================================================\n");
1355 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1356 irqclass
, irqclass
);
1357 print_kernel_version();
1358 printk( "------------------------------------------------------\n");
1359 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1360 curr
->comm
, task_pid_nr(curr
),
1361 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1362 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1363 curr
->hardirqs_enabled
,
1364 curr
->softirqs_enabled
);
1367 printk("\nand this task is already holding:\n");
1369 printk("which would create a new lock dependency:\n");
1370 print_lock_name(hlock_class(prev
));
1372 print_lock_name(hlock_class(next
));
1375 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1377 print_lock_name(backwards_entry
->class);
1378 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1380 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1382 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1383 print_lock_name(forwards_entry
->class);
1384 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1387 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1389 printk("\nother info that might help us debug this:\n\n");
1390 lockdep_print_held_locks(curr
);
1392 printk("\nthe dependencies between %s-irq-safe lock", irqclass
);
1393 printk(" and the holding lock:\n");
1394 if (!save_trace(&prev_root
->trace
))
1396 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1398 printk("\nthe dependencies between the lock to be acquired");
1399 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1400 if (!save_trace(&next_root
->trace
))
1402 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1404 printk("\nstack backtrace:\n");
1411 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1412 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1413 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1416 struct lock_list
this, that
;
1417 struct lock_list
*uninitialized_var(target_entry
);
1418 struct lock_list
*uninitialized_var(target_entry1
);
1422 this.class = hlock_class(prev
);
1423 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1425 return print_bfs_bug(ret
);
1430 that
.class = hlock_class(next
);
1431 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1433 return print_bfs_bug(ret
);
1437 return print_bad_irq_dependency(curr
, &this, &that
,
1438 target_entry
, target_entry1
,
1440 bit_backwards
, bit_forwards
, irqclass
);
1443 static const char *state_names
[] = {
1444 #define LOCKDEP_STATE(__STATE) \
1445 __stringify(__STATE),
1446 #include "lockdep_states.h"
1447 #undef LOCKDEP_STATE
1450 static const char *state_rnames
[] = {
1451 #define LOCKDEP_STATE(__STATE) \
1452 __stringify(__STATE)"-READ",
1453 #include "lockdep_states.h"
1454 #undef LOCKDEP_STATE
1457 static inline const char *state_name(enum lock_usage_bit bit
)
1459 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1462 static int exclusive_bit(int new_bit
)
1470 * bit 0 - write/read
1471 * bit 1 - used_in/enabled
1475 int state
= new_bit
& ~3;
1476 int dir
= new_bit
& 2;
1479 * keep state, bit flip the direction and strip read.
1481 return state
| (dir
^ 2);
1484 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1485 struct held_lock
*next
, enum lock_usage_bit bit
)
1488 * Prove that the new dependency does not connect a hardirq-safe
1489 * lock with a hardirq-unsafe lock - to achieve this we search
1490 * the backwards-subgraph starting at <prev>, and the
1491 * forwards-subgraph starting at <next>:
1493 if (!check_usage(curr
, prev
, next
, bit
,
1494 exclusive_bit(bit
), state_name(bit
)))
1500 * Prove that the new dependency does not connect a hardirq-safe-read
1501 * lock with a hardirq-unsafe lock - to achieve this we search
1502 * the backwards-subgraph starting at <prev>, and the
1503 * forwards-subgraph starting at <next>:
1505 if (!check_usage(curr
, prev
, next
, bit
,
1506 exclusive_bit(bit
), state_name(bit
)))
1513 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1514 struct held_lock
*next
)
1516 #define LOCKDEP_STATE(__STATE) \
1517 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1519 #include "lockdep_states.h"
1520 #undef LOCKDEP_STATE
1525 static void inc_chains(void)
1527 if (current
->hardirq_context
)
1528 nr_hardirq_chains
++;
1530 if (current
->softirq_context
)
1531 nr_softirq_chains
++;
1533 nr_process_chains
++;
1540 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1541 struct held_lock
*next
)
1546 static inline void inc_chains(void)
1548 nr_process_chains
++;
1554 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1555 struct held_lock
*next
)
1557 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1560 printk("\n=============================================\n");
1561 printk( "[ INFO: possible recursive locking detected ]\n");
1562 print_kernel_version();
1563 printk( "---------------------------------------------\n");
1564 printk("%s/%d is trying to acquire lock:\n",
1565 curr
->comm
, task_pid_nr(curr
));
1567 printk("\nbut task is already holding lock:\n");
1570 printk("\nother info that might help us debug this:\n");
1571 lockdep_print_held_locks(curr
);
1573 printk("\nstack backtrace:\n");
1580 * Check whether we are holding such a class already.
1582 * (Note that this has to be done separately, because the graph cannot
1583 * detect such classes of deadlocks.)
1585 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1588 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1589 struct lockdep_map
*next_instance
, int read
)
1591 struct held_lock
*prev
;
1592 struct held_lock
*nest
= NULL
;
1595 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1596 prev
= curr
->held_locks
+ i
;
1598 if (prev
->instance
== next
->nest_lock
)
1601 if (hlock_class(prev
) != hlock_class(next
))
1605 * Allow read-after-read recursion of the same
1606 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1608 if ((read
== 2) && prev
->read
)
1612 * We're holding the nest_lock, which serializes this lock's
1613 * nesting behaviour.
1618 return print_deadlock_bug(curr
, prev
, next
);
1624 * There was a chain-cache miss, and we are about to add a new dependency
1625 * to a previous lock. We recursively validate the following rules:
1627 * - would the adding of the <prev> -> <next> dependency create a
1628 * circular dependency in the graph? [== circular deadlock]
1630 * - does the new prev->next dependency connect any hardirq-safe lock
1631 * (in the full backwards-subgraph starting at <prev>) with any
1632 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1633 * <next>)? [== illegal lock inversion with hardirq contexts]
1635 * - does the new prev->next dependency connect any softirq-safe lock
1636 * (in the full backwards-subgraph starting at <prev>) with any
1637 * softirq-unsafe lock (in the full forwards-subgraph starting at
1638 * <next>)? [== illegal lock inversion with softirq contexts]
1640 * any of these scenarios could lead to a deadlock.
1642 * Then if all the validations pass, we add the forwards and backwards
1646 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1647 struct held_lock
*next
, int distance
)
1649 struct lock_list
*entry
;
1651 struct lock_list
this;
1652 struct lock_list
*uninitialized_var(target_entry
);
1655 * Prove that the new <prev> -> <next> dependency would not
1656 * create a circular dependency in the graph. (We do this by
1657 * forward-recursing into the graph starting at <next>, and
1658 * checking whether we can reach <prev>.)
1660 * We are using global variables to control the recursion, to
1661 * keep the stackframe size of the recursive functions low:
1663 this.class = hlock_class(next
);
1665 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1667 return print_circular_bug(&this, target_entry
, next
, prev
);
1668 else if (unlikely(ret
< 0))
1669 return print_bfs_bug(ret
);
1671 if (!check_prev_add_irq(curr
, prev
, next
))
1675 * For recursive read-locks we do all the dependency checks,
1676 * but we dont store read-triggered dependencies (only
1677 * write-triggered dependencies). This ensures that only the
1678 * write-side dependencies matter, and that if for example a
1679 * write-lock never takes any other locks, then the reads are
1680 * equivalent to a NOP.
1682 if (next
->read
== 2 || prev
->read
== 2)
1685 * Is the <prev> -> <next> dependency already present?
1687 * (this may occur even though this is a new chain: consider
1688 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1689 * chains - the second one will be new, but L1 already has
1690 * L2 added to its dependency list, due to the first chain.)
1692 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1693 if (entry
->class == hlock_class(next
)) {
1695 entry
->distance
= 1;
1701 * Ok, all validations passed, add the new lock
1702 * to the previous lock's dependency list:
1704 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1705 &hlock_class(prev
)->locks_after
,
1706 next
->acquire_ip
, distance
);
1711 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1712 &hlock_class(next
)->locks_before
,
1713 next
->acquire_ip
, distance
);
1718 * Debugging printouts:
1720 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1722 printk("\n new dependency: ");
1723 print_lock_name(hlock_class(prev
));
1725 print_lock_name(hlock_class(next
));
1728 return graph_lock();
1734 * Add the dependency to all directly-previous locks that are 'relevant'.
1735 * The ones that are relevant are (in increasing distance from curr):
1736 * all consecutive trylock entries and the final non-trylock entry - or
1737 * the end of this context's lock-chain - whichever comes first.
1740 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1742 int depth
= curr
->lockdep_depth
;
1743 struct held_lock
*hlock
;
1748 * Depth must not be zero for a non-head lock:
1753 * At least two relevant locks must exist for this
1756 if (curr
->held_locks
[depth
].irq_context
!=
1757 curr
->held_locks
[depth
-1].irq_context
)
1761 int distance
= curr
->lockdep_depth
- depth
+ 1;
1762 hlock
= curr
->held_locks
+ depth
-1;
1764 * Only non-recursive-read entries get new dependencies
1767 if (hlock
->read
!= 2) {
1768 if (!check_prev_add(curr
, hlock
, next
, distance
))
1771 * Stop after the first non-trylock entry,
1772 * as non-trylock entries have added their
1773 * own direct dependencies already, so this
1774 * lock is connected to them indirectly:
1776 if (!hlock
->trylock
)
1781 * End of lock-stack?
1786 * Stop the search if we cross into another context:
1788 if (curr
->held_locks
[depth
].irq_context
!=
1789 curr
->held_locks
[depth
-1].irq_context
)
1794 if (!debug_locks_off_graph_unlock())
1802 unsigned long nr_lock_chains
;
1803 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1804 int nr_chain_hlocks
;
1805 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1807 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1809 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1813 * Look up a dependency chain. If the key is not present yet then
1814 * add it and return 1 - in this case the new dependency chain is
1815 * validated. If the key is already hashed, return 0.
1816 * (On return with 1 graph_lock is held.)
1818 static inline int lookup_chain_cache(struct task_struct
*curr
,
1819 struct held_lock
*hlock
,
1822 struct lock_class
*class = hlock_class(hlock
);
1823 struct list_head
*hash_head
= chainhashentry(chain_key
);
1824 struct lock_chain
*chain
;
1825 struct held_lock
*hlock_curr
, *hlock_next
;
1828 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1831 * We can walk it lock-free, because entries only get added
1834 list_for_each_entry(chain
, hash_head
, entry
) {
1835 if (chain
->chain_key
== chain_key
) {
1837 debug_atomic_inc(&chain_lookup_hits
);
1838 if (very_verbose(class))
1839 printk("\nhash chain already cached, key: "
1840 "%016Lx tail class: [%p] %s\n",
1841 (unsigned long long)chain_key
,
1842 class->key
, class->name
);
1846 if (very_verbose(class))
1847 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1848 (unsigned long long)chain_key
, class->key
, class->name
);
1850 * Allocate a new chain entry from the static array, and add
1856 * We have to walk the chain again locked - to avoid duplicates:
1858 list_for_each_entry(chain
, hash_head
, entry
) {
1859 if (chain
->chain_key
== chain_key
) {
1864 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1865 if (!debug_locks_off_graph_unlock())
1868 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1869 printk("turning off the locking correctness validator.\n");
1873 chain
= lock_chains
+ nr_lock_chains
++;
1874 chain
->chain_key
= chain_key
;
1875 chain
->irq_context
= hlock
->irq_context
;
1876 /* Find the first held_lock of current chain */
1878 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
1879 hlock_curr
= curr
->held_locks
+ i
;
1880 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
1885 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
1886 cn
= nr_chain_hlocks
;
1887 while (cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
) {
1888 n
= cmpxchg(&nr_chain_hlocks
, cn
, cn
+ chain
->depth
);
1893 if (likely(cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
1895 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
1896 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
1897 chain_hlocks
[chain
->base
+ j
] = lock_id
;
1899 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
1901 list_add_tail_rcu(&chain
->entry
, hash_head
);
1902 debug_atomic_inc(&chain_lookup_misses
);
1908 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1909 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
1912 * Trylock needs to maintain the stack of held locks, but it
1913 * does not add new dependencies, because trylock can be done
1916 * We look up the chain_key and do the O(N^2) check and update of
1917 * the dependencies only if this is a new dependency chain.
1918 * (If lookup_chain_cache() returns with 1 it acquires
1919 * graph_lock for us)
1921 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1922 lookup_chain_cache(curr
, hlock
, chain_key
)) {
1924 * Check whether last held lock:
1926 * - is irq-safe, if this lock is irq-unsafe
1927 * - is softirq-safe, if this lock is hardirq-unsafe
1929 * And check whether the new lock's dependency graph
1930 * could lead back to the previous lock.
1932 * any of these scenarios could lead to a deadlock. If
1935 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1940 * Mark recursive read, as we jump over it when
1941 * building dependencies (just like we jump over
1947 * Add dependency only if this lock is not the head
1948 * of the chain, and if it's not a secondary read-lock:
1950 if (!chain_head
&& ret
!= 2)
1951 if (!check_prevs_add(curr
, hlock
))
1955 /* after lookup_chain_cache(): */
1956 if (unlikely(!debug_locks
))
1962 static inline int validate_chain(struct task_struct
*curr
,
1963 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1964 int chain_head
, u64 chain_key
)
1971 * We are building curr_chain_key incrementally, so double-check
1972 * it from scratch, to make sure that it's done correctly:
1974 static void check_chain_key(struct task_struct
*curr
)
1976 #ifdef CONFIG_DEBUG_LOCKDEP
1977 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1981 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1982 hlock
= curr
->held_locks
+ i
;
1983 if (chain_key
!= hlock
->prev_chain_key
) {
1985 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1986 curr
->lockdep_depth
, i
,
1987 (unsigned long long)chain_key
,
1988 (unsigned long long)hlock
->prev_chain_key
);
1991 id
= hlock
->class_idx
- 1;
1992 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1995 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1996 hlock
->irq_context
))
1998 chain_key
= iterate_chain_key(chain_key
, id
);
2001 if (chain_key
!= curr
->curr_chain_key
) {
2003 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2004 curr
->lockdep_depth
, i
,
2005 (unsigned long long)chain_key
,
2006 (unsigned long long)curr
->curr_chain_key
);
2012 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2013 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2015 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2018 printk("\n=================================\n");
2019 printk( "[ INFO: inconsistent lock state ]\n");
2020 print_kernel_version();
2021 printk( "---------------------------------\n");
2023 printk("inconsistent {%s} -> {%s} usage.\n",
2024 usage_str
[prev_bit
], usage_str
[new_bit
]);
2026 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2027 curr
->comm
, task_pid_nr(curr
),
2028 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2029 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2030 trace_hardirqs_enabled(curr
),
2031 trace_softirqs_enabled(curr
));
2034 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2035 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2037 print_irqtrace_events(curr
);
2038 printk("\nother info that might help us debug this:\n");
2039 lockdep_print_held_locks(curr
);
2041 printk("\nstack backtrace:\n");
2048 * Print out an error if an invalid bit is set:
2051 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2052 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2054 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2055 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2059 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2060 enum lock_usage_bit new_bit
);
2062 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2065 * print irq inversion bug:
2068 print_irq_inversion_bug(struct task_struct
*curr
,
2069 struct lock_list
*root
, struct lock_list
*other
,
2070 struct held_lock
*this, int forwards
,
2071 const char *irqclass
)
2073 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2076 printk("\n=========================================================\n");
2077 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2078 print_kernel_version();
2079 printk( "---------------------------------------------------------\n");
2080 printk("%s/%d just changed the state of lock:\n",
2081 curr
->comm
, task_pid_nr(curr
));
2084 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2086 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2087 print_lock_name(other
->class);
2088 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2090 printk("\nother info that might help us debug this:\n");
2091 lockdep_print_held_locks(curr
);
2093 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2094 if (!save_trace(&root
->trace
))
2096 print_shortest_lock_dependencies(other
, root
);
2098 printk("\nstack backtrace:\n");
2105 * Prove that in the forwards-direction subgraph starting at <this>
2106 * there is no lock matching <mask>:
2109 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2110 enum lock_usage_bit bit
, const char *irqclass
)
2113 struct lock_list root
;
2114 struct lock_list
*uninitialized_var(target_entry
);
2117 root
.class = hlock_class(this);
2118 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2120 return print_bfs_bug(ret
);
2124 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2129 * Prove that in the backwards-direction subgraph starting at <this>
2130 * there is no lock matching <mask>:
2133 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2134 enum lock_usage_bit bit
, const char *irqclass
)
2137 struct lock_list root
;
2138 struct lock_list
*uninitialized_var(target_entry
);
2141 root
.class = hlock_class(this);
2142 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2144 return print_bfs_bug(ret
);
2148 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2152 void print_irqtrace_events(struct task_struct
*curr
)
2154 printk("irq event stamp: %u\n", curr
->irq_events
);
2155 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
2156 print_ip_sym(curr
->hardirq_enable_ip
);
2157 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
2158 print_ip_sym(curr
->hardirq_disable_ip
);
2159 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
2160 print_ip_sym(curr
->softirq_enable_ip
);
2161 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
2162 print_ip_sym(curr
->softirq_disable_ip
);
2165 static int HARDIRQ_verbose(struct lock_class
*class)
2168 return class_filter(class);
2173 static int SOFTIRQ_verbose(struct lock_class
*class)
2176 return class_filter(class);
2181 static int RECLAIM_FS_verbose(struct lock_class
*class)
2184 return class_filter(class);
2189 #define STRICT_READ_CHECKS 1
2191 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2192 #define LOCKDEP_STATE(__STATE) \
2194 #include "lockdep_states.h"
2195 #undef LOCKDEP_STATE
2198 static inline int state_verbose(enum lock_usage_bit bit
,
2199 struct lock_class
*class)
2201 return state_verbose_f
[bit
>> 2](class);
2204 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2205 enum lock_usage_bit bit
, const char *name
);
2208 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2209 enum lock_usage_bit new_bit
)
2211 int excl_bit
= exclusive_bit(new_bit
);
2212 int read
= new_bit
& 1;
2213 int dir
= new_bit
& 2;
2216 * mark USED_IN has to look forwards -- to ensure no dependency
2217 * has ENABLED state, which would allow recursion deadlocks.
2219 * mark ENABLED has to look backwards -- to ensure no dependee
2220 * has USED_IN state, which, again, would allow recursion deadlocks.
2222 check_usage_f usage
= dir
?
2223 check_usage_backwards
: check_usage_forwards
;
2226 * Validate that this particular lock does not have conflicting
2229 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2233 * Validate that the lock dependencies don't have conflicting usage
2236 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2237 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2241 * Check for read in write conflicts
2244 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2247 if (STRICT_READ_CHECKS
&&
2248 !usage(curr
, this, excl_bit
+ 1,
2249 state_name(new_bit
+ 1)))
2253 if (state_verbose(new_bit
, hlock_class(this)))
2260 #define LOCKDEP_STATE(__STATE) __STATE,
2261 #include "lockdep_states.h"
2262 #undef LOCKDEP_STATE
2266 * Mark all held locks with a usage bit:
2269 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2271 enum lock_usage_bit usage_bit
;
2272 struct held_lock
*hlock
;
2275 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2276 hlock
= curr
->held_locks
+ i
;
2278 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2280 usage_bit
+= 1; /* READ */
2282 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2284 if (!mark_lock(curr
, hlock
, usage_bit
))
2292 * Debugging helper: via this flag we know that we are in
2293 * 'early bootup code', and will warn about any invalid irqs-on event:
2295 static int early_boot_irqs_enabled
;
2297 void early_boot_irqs_off(void)
2299 early_boot_irqs_enabled
= 0;
2302 void early_boot_irqs_on(void)
2304 early_boot_irqs_enabled
= 1;
2308 * Hardirqs will be enabled:
2310 void trace_hardirqs_on_caller(unsigned long ip
)
2312 struct task_struct
*curr
= current
;
2314 time_hardirqs_on(CALLER_ADDR0
, ip
);
2316 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2319 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2322 if (unlikely(curr
->hardirqs_enabled
)) {
2323 debug_atomic_inc(&redundant_hardirqs_on
);
2326 /* we'll do an OFF -> ON transition: */
2327 curr
->hardirqs_enabled
= 1;
2329 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2331 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2334 * We are going to turn hardirqs on, so set the
2335 * usage bit for all held locks:
2337 if (!mark_held_locks(curr
, HARDIRQ
))
2340 * If we have softirqs enabled, then set the usage
2341 * bit for all held locks. (disabled hardirqs prevented
2342 * this bit from being set before)
2344 if (curr
->softirqs_enabled
)
2345 if (!mark_held_locks(curr
, SOFTIRQ
))
2348 curr
->hardirq_enable_ip
= ip
;
2349 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2350 debug_atomic_inc(&hardirqs_on_events
);
2352 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2354 void trace_hardirqs_on(void)
2356 trace_hardirqs_on_caller(CALLER_ADDR0
);
2358 EXPORT_SYMBOL(trace_hardirqs_on
);
2361 * Hardirqs were disabled:
2363 void trace_hardirqs_off_caller(unsigned long ip
)
2365 struct task_struct
*curr
= current
;
2367 time_hardirqs_off(CALLER_ADDR0
, ip
);
2369 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2372 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2375 if (curr
->hardirqs_enabled
) {
2377 * We have done an ON -> OFF transition:
2379 curr
->hardirqs_enabled
= 0;
2380 curr
->hardirq_disable_ip
= ip
;
2381 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2382 debug_atomic_inc(&hardirqs_off_events
);
2384 debug_atomic_inc(&redundant_hardirqs_off
);
2386 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2388 void trace_hardirqs_off(void)
2390 trace_hardirqs_off_caller(CALLER_ADDR0
);
2392 EXPORT_SYMBOL(trace_hardirqs_off
);
2395 * Softirqs will be enabled:
2397 void trace_softirqs_on(unsigned long ip
)
2399 struct task_struct
*curr
= current
;
2401 if (unlikely(!debug_locks
))
2404 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2407 if (curr
->softirqs_enabled
) {
2408 debug_atomic_inc(&redundant_softirqs_on
);
2413 * We'll do an OFF -> ON transition:
2415 curr
->softirqs_enabled
= 1;
2416 curr
->softirq_enable_ip
= ip
;
2417 curr
->softirq_enable_event
= ++curr
->irq_events
;
2418 debug_atomic_inc(&softirqs_on_events
);
2420 * We are going to turn softirqs on, so set the
2421 * usage bit for all held locks, if hardirqs are
2424 if (curr
->hardirqs_enabled
)
2425 mark_held_locks(curr
, SOFTIRQ
);
2429 * Softirqs were disabled:
2431 void trace_softirqs_off(unsigned long ip
)
2433 struct task_struct
*curr
= current
;
2435 if (unlikely(!debug_locks
))
2438 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2441 if (curr
->softirqs_enabled
) {
2443 * We have done an ON -> OFF transition:
2445 curr
->softirqs_enabled
= 0;
2446 curr
->softirq_disable_ip
= ip
;
2447 curr
->softirq_disable_event
= ++curr
->irq_events
;
2448 debug_atomic_inc(&softirqs_off_events
);
2449 DEBUG_LOCKS_WARN_ON(!softirq_count());
2451 debug_atomic_inc(&redundant_softirqs_off
);
2454 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2456 struct task_struct
*curr
= current
;
2458 if (unlikely(!debug_locks
))
2461 /* no reclaim without waiting on it */
2462 if (!(gfp_mask
& __GFP_WAIT
))
2465 /* this guy won't enter reclaim */
2466 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2469 /* We're only interested __GFP_FS allocations for now */
2470 if (!(gfp_mask
& __GFP_FS
))
2473 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2476 mark_held_locks(curr
, RECLAIM_FS
);
2479 static void check_flags(unsigned long flags
);
2481 void lockdep_trace_alloc(gfp_t gfp_mask
)
2483 unsigned long flags
;
2485 if (unlikely(current
->lockdep_recursion
))
2488 raw_local_irq_save(flags
);
2490 current
->lockdep_recursion
= 1;
2491 __lockdep_trace_alloc(gfp_mask
, flags
);
2492 current
->lockdep_recursion
= 0;
2493 raw_local_irq_restore(flags
);
2496 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2499 * If non-trylock use in a hardirq or softirq context, then
2500 * mark the lock as used in these contexts:
2502 if (!hlock
->trylock
) {
2504 if (curr
->hardirq_context
)
2505 if (!mark_lock(curr
, hlock
,
2506 LOCK_USED_IN_HARDIRQ_READ
))
2508 if (curr
->softirq_context
)
2509 if (!mark_lock(curr
, hlock
,
2510 LOCK_USED_IN_SOFTIRQ_READ
))
2513 if (curr
->hardirq_context
)
2514 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2516 if (curr
->softirq_context
)
2517 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2521 if (!hlock
->hardirqs_off
) {
2523 if (!mark_lock(curr
, hlock
,
2524 LOCK_ENABLED_HARDIRQ_READ
))
2526 if (curr
->softirqs_enabled
)
2527 if (!mark_lock(curr
, hlock
,
2528 LOCK_ENABLED_SOFTIRQ_READ
))
2531 if (!mark_lock(curr
, hlock
,
2532 LOCK_ENABLED_HARDIRQ
))
2534 if (curr
->softirqs_enabled
)
2535 if (!mark_lock(curr
, hlock
,
2536 LOCK_ENABLED_SOFTIRQ
))
2542 * We reuse the irq context infrastructure more broadly as a general
2543 * context checking code. This tests GFP_FS recursion (a lock taken
2544 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2547 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2549 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2552 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2560 static int separate_irq_context(struct task_struct
*curr
,
2561 struct held_lock
*hlock
)
2563 unsigned int depth
= curr
->lockdep_depth
;
2566 * Keep track of points where we cross into an interrupt context:
2568 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2569 curr
->softirq_context
;
2571 struct held_lock
*prev_hlock
;
2573 prev_hlock
= curr
->held_locks
+ depth
-1;
2575 * If we cross into another context, reset the
2576 * hash key (this also prevents the checking and the
2577 * adding of the dependency to 'prev'):
2579 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2588 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2589 enum lock_usage_bit new_bit
)
2595 static inline int mark_irqflags(struct task_struct
*curr
,
2596 struct held_lock
*hlock
)
2601 static inline int separate_irq_context(struct task_struct
*curr
,
2602 struct held_lock
*hlock
)
2607 void lockdep_trace_alloc(gfp_t gfp_mask
)
2614 * Mark a lock with a usage bit, and validate the state transition:
2616 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2617 enum lock_usage_bit new_bit
)
2619 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2622 * If already set then do not dirty the cacheline,
2623 * nor do any checks:
2625 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2631 * Make sure we didnt race:
2633 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2638 hlock_class(this)->usage_mask
|= new_mask
;
2640 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2644 #define LOCKDEP_STATE(__STATE) \
2645 case LOCK_USED_IN_##__STATE: \
2646 case LOCK_USED_IN_##__STATE##_READ: \
2647 case LOCK_ENABLED_##__STATE: \
2648 case LOCK_ENABLED_##__STATE##_READ:
2649 #include "lockdep_states.h"
2650 #undef LOCKDEP_STATE
2651 ret
= mark_lock_irq(curr
, this, new_bit
);
2656 debug_atomic_dec(&nr_unused_locks
);
2659 if (!debug_locks_off_graph_unlock())
2668 * We must printk outside of the graph_lock:
2671 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2673 print_irqtrace_events(curr
);
2681 * Initialize a lock instance's lock-class mapping info:
2683 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2684 struct lock_class_key
*key
, int subclass
)
2686 lock
->class_cache
= NULL
;
2687 #ifdef CONFIG_LOCK_STAT
2688 lock
->cpu
= raw_smp_processor_id();
2691 if (DEBUG_LOCKS_WARN_ON(!name
)) {
2692 lock
->name
= "NULL";
2698 if (DEBUG_LOCKS_WARN_ON(!key
))
2701 * Sanity check, the lock-class key must be persistent:
2703 if (!static_obj(key
)) {
2704 printk("BUG: key %p not in .data!\n", key
);
2705 DEBUG_LOCKS_WARN_ON(1);
2710 if (unlikely(!debug_locks
))
2714 register_lock_class(lock
, subclass
, 1);
2716 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2719 * This gets called for every mutex_lock*()/spin_lock*() operation.
2720 * We maintain the dependency maps and validate the locking attempt:
2722 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2723 int trylock
, int read
, int check
, int hardirqs_off
,
2724 struct lockdep_map
*nest_lock
, unsigned long ip
,
2727 struct task_struct
*curr
= current
;
2728 struct lock_class
*class = NULL
;
2729 struct held_lock
*hlock
;
2730 unsigned int depth
, id
;
2738 if (unlikely(!debug_locks
))
2741 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2744 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2746 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2747 printk("turning off the locking correctness validator.\n");
2753 class = lock
->class_cache
;
2755 * Not cached yet or subclass?
2757 if (unlikely(!class)) {
2758 class = register_lock_class(lock
, subclass
, 0);
2762 debug_atomic_inc((atomic_t
*)&class->ops
);
2763 if (very_verbose(class)) {
2764 printk("\nacquire class [%p] %s", class->key
, class->name
);
2765 if (class->name_version
> 1)
2766 printk("#%d", class->name_version
);
2772 * Add the lock to the list of currently held locks.
2773 * (we dont increase the depth just yet, up until the
2774 * dependency checks are done)
2776 depth
= curr
->lockdep_depth
;
2777 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2780 class_idx
= class - lock_classes
+ 1;
2783 hlock
= curr
->held_locks
+ depth
- 1;
2784 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
2785 if (hlock
->references
)
2786 hlock
->references
++;
2788 hlock
->references
= 2;
2794 hlock
= curr
->held_locks
+ depth
;
2795 if (DEBUG_LOCKS_WARN_ON(!class))
2797 hlock
->class_idx
= class_idx
;
2798 hlock
->acquire_ip
= ip
;
2799 hlock
->instance
= lock
;
2800 hlock
->nest_lock
= nest_lock
;
2801 hlock
->trylock
= trylock
;
2803 hlock
->check
= check
;
2804 hlock
->hardirqs_off
= !!hardirqs_off
;
2805 hlock
->references
= references
;
2806 #ifdef CONFIG_LOCK_STAT
2807 hlock
->waittime_stamp
= 0;
2808 hlock
->holdtime_stamp
= lockstat_clock();
2811 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2814 /* mark it as used: */
2815 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2819 * Calculate the chain hash: it's the combined hash of all the
2820 * lock keys along the dependency chain. We save the hash value
2821 * at every step so that we can get the current hash easily
2822 * after unlock. The chain hash is then used to cache dependency
2825 * The 'key ID' is what is the most compact key value to drive
2826 * the hash, not class->key.
2828 id
= class - lock_classes
;
2829 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2832 chain_key
= curr
->curr_chain_key
;
2834 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2839 hlock
->prev_chain_key
= chain_key
;
2840 if (separate_irq_context(curr
, hlock
)) {
2844 chain_key
= iterate_chain_key(chain_key
, id
);
2846 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
2849 curr
->curr_chain_key
= chain_key
;
2850 curr
->lockdep_depth
++;
2851 check_chain_key(curr
);
2852 #ifdef CONFIG_DEBUG_LOCKDEP
2853 if (unlikely(!debug_locks
))
2856 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2858 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2859 printk("turning off the locking correctness validator.\n");
2864 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2865 max_lockdep_depth
= curr
->lockdep_depth
;
2871 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2874 if (!debug_locks_off())
2876 if (debug_locks_silent
)
2879 printk("\n=====================================\n");
2880 printk( "[ BUG: bad unlock balance detected! ]\n");
2881 printk( "-------------------------------------\n");
2882 printk("%s/%d is trying to release lock (",
2883 curr
->comm
, task_pid_nr(curr
));
2884 print_lockdep_cache(lock
);
2887 printk("but there are no more locks to release!\n");
2888 printk("\nother info that might help us debug this:\n");
2889 lockdep_print_held_locks(curr
);
2891 printk("\nstack backtrace:\n");
2898 * Common debugging checks for both nested and non-nested unlock:
2900 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2903 if (unlikely(!debug_locks
))
2905 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2908 if (curr
->lockdep_depth
<= 0)
2909 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2914 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
2916 if (hlock
->instance
== lock
)
2919 if (hlock
->references
) {
2920 struct lock_class
*class = lock
->class_cache
;
2923 class = look_up_lock_class(lock
, 0);
2925 if (DEBUG_LOCKS_WARN_ON(!class))
2928 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
2931 if (hlock
->class_idx
== class - lock_classes
+ 1)
2939 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
2940 struct lock_class_key
*key
, unsigned int subclass
,
2943 struct task_struct
*curr
= current
;
2944 struct held_lock
*hlock
, *prev_hlock
;
2945 struct lock_class
*class;
2949 depth
= curr
->lockdep_depth
;
2950 if (DEBUG_LOCKS_WARN_ON(!depth
))
2954 for (i
= depth
-1; i
>= 0; i
--) {
2955 hlock
= curr
->held_locks
+ i
;
2957 * We must not cross into another context:
2959 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2961 if (match_held_lock(hlock
, lock
))
2965 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2968 lockdep_init_map(lock
, name
, key
, 0);
2969 class = register_lock_class(lock
, subclass
, 0);
2970 hlock
->class_idx
= class - lock_classes
+ 1;
2972 curr
->lockdep_depth
= i
;
2973 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2975 for (; i
< depth
; i
++) {
2976 hlock
= curr
->held_locks
+ i
;
2977 if (!__lock_acquire(hlock
->instance
,
2978 hlock_class(hlock
)->subclass
, hlock
->trylock
,
2979 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2980 hlock
->nest_lock
, hlock
->acquire_ip
,
2985 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
2991 * Remove the lock to the list of currently held locks in a
2992 * potentially non-nested (out of order) manner. This is a
2993 * relatively rare operation, as all the unlock APIs default
2994 * to nested mode (which uses lock_release()):
2997 lock_release_non_nested(struct task_struct
*curr
,
2998 struct lockdep_map
*lock
, unsigned long ip
)
3000 struct held_lock
*hlock
, *prev_hlock
;
3005 * Check whether the lock exists in the current stack
3008 depth
= curr
->lockdep_depth
;
3009 if (DEBUG_LOCKS_WARN_ON(!depth
))
3013 for (i
= depth
-1; i
>= 0; i
--) {
3014 hlock
= curr
->held_locks
+ i
;
3016 * We must not cross into another context:
3018 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3020 if (match_held_lock(hlock
, lock
))
3024 return print_unlock_inbalance_bug(curr
, lock
, ip
);
3027 if (hlock
->instance
== lock
)
3028 lock_release_holdtime(hlock
);
3030 if (hlock
->references
) {
3031 hlock
->references
--;
3032 if (hlock
->references
) {
3034 * We had, and after removing one, still have
3035 * references, the current lock stack is still
3036 * valid. We're done!
3043 * We have the right lock to unlock, 'hlock' points to it.
3044 * Now we remove it from the stack, and add back the other
3045 * entries (if any), recalculating the hash along the way:
3048 curr
->lockdep_depth
= i
;
3049 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3051 for (i
++; i
< depth
; i
++) {
3052 hlock
= curr
->held_locks
+ i
;
3053 if (!__lock_acquire(hlock
->instance
,
3054 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3055 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3056 hlock
->nest_lock
, hlock
->acquire_ip
,
3061 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3067 * Remove the lock to the list of currently held locks - this gets
3068 * called on mutex_unlock()/spin_unlock*() (or on a failed
3069 * mutex_lock_interruptible()). This is done for unlocks that nest
3070 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3072 static int lock_release_nested(struct task_struct
*curr
,
3073 struct lockdep_map
*lock
, unsigned long ip
)
3075 struct held_lock
*hlock
;
3079 * Pop off the top of the lock stack:
3081 depth
= curr
->lockdep_depth
- 1;
3082 hlock
= curr
->held_locks
+ depth
;
3085 * Is the unlock non-nested:
3087 if (hlock
->instance
!= lock
|| hlock
->references
)
3088 return lock_release_non_nested(curr
, lock
, ip
);
3089 curr
->lockdep_depth
--;
3091 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
3094 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3096 lock_release_holdtime(hlock
);
3098 #ifdef CONFIG_DEBUG_LOCKDEP
3099 hlock
->prev_chain_key
= 0;
3100 hlock
->class_idx
= 0;
3101 hlock
->acquire_ip
= 0;
3102 hlock
->irq_context
= 0;
3108 * Remove the lock to the list of currently held locks - this gets
3109 * called on mutex_unlock()/spin_unlock*() (or on a failed
3110 * mutex_lock_interruptible()). This is done for unlocks that nest
3111 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3114 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3116 struct task_struct
*curr
= current
;
3118 if (!check_unlock(curr
, lock
, ip
))
3122 if (!lock_release_nested(curr
, lock
, ip
))
3125 if (!lock_release_non_nested(curr
, lock
, ip
))
3129 check_chain_key(curr
);
3132 static int __lock_is_held(struct lockdep_map
*lock
)
3134 struct task_struct
*curr
= current
;
3137 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3138 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3140 if (match_held_lock(hlock
, lock
))
3148 * Check whether we follow the irq-flags state precisely:
3150 static void check_flags(unsigned long flags
)
3152 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3153 defined(CONFIG_TRACE_IRQFLAGS)
3157 if (irqs_disabled_flags(flags
)) {
3158 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3159 printk("possible reason: unannotated irqs-off.\n");
3162 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3163 printk("possible reason: unannotated irqs-on.\n");
3168 * We dont accurately track softirq state in e.g.
3169 * hardirq contexts (such as on 4KSTACKS), so only
3170 * check if not in hardirq contexts:
3172 if (!hardirq_count()) {
3173 if (softirq_count())
3174 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3176 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3180 print_irqtrace_events(current
);
3184 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3185 struct lock_class_key
*key
, unsigned int subclass
,
3188 unsigned long flags
;
3190 if (unlikely(current
->lockdep_recursion
))
3193 raw_local_irq_save(flags
);
3194 current
->lockdep_recursion
= 1;
3196 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3197 check_chain_key(current
);
3198 current
->lockdep_recursion
= 0;
3199 raw_local_irq_restore(flags
);
3201 EXPORT_SYMBOL_GPL(lock_set_class
);
3204 * We are not always called with irqs disabled - do that here,
3205 * and also avoid lockdep recursion:
3207 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3208 int trylock
, int read
, int check
,
3209 struct lockdep_map
*nest_lock
, unsigned long ip
)
3211 unsigned long flags
;
3213 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3215 if (unlikely(current
->lockdep_recursion
))
3218 raw_local_irq_save(flags
);
3221 current
->lockdep_recursion
= 1;
3222 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3223 irqs_disabled_flags(flags
), nest_lock
, ip
, 0);
3224 current
->lockdep_recursion
= 0;
3225 raw_local_irq_restore(flags
);
3227 EXPORT_SYMBOL_GPL(lock_acquire
);
3229 void lock_release(struct lockdep_map
*lock
, int nested
,
3232 unsigned long flags
;
3234 trace_lock_release(lock
, nested
, ip
);
3236 if (unlikely(current
->lockdep_recursion
))
3239 raw_local_irq_save(flags
);
3241 current
->lockdep_recursion
= 1;
3242 __lock_release(lock
, nested
, ip
);
3243 current
->lockdep_recursion
= 0;
3244 raw_local_irq_restore(flags
);
3246 EXPORT_SYMBOL_GPL(lock_release
);
3248 int lock_is_held(struct lockdep_map
*lock
)
3250 unsigned long flags
;
3253 if (unlikely(current
->lockdep_recursion
))
3256 raw_local_irq_save(flags
);
3259 current
->lockdep_recursion
= 1;
3260 ret
= __lock_is_held(lock
);
3261 current
->lockdep_recursion
= 0;
3262 raw_local_irq_restore(flags
);
3266 EXPORT_SYMBOL_GPL(lock_is_held
);
3268 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3270 current
->lockdep_reclaim_gfp
= gfp_mask
;
3273 void lockdep_clear_current_reclaim_state(void)
3275 current
->lockdep_reclaim_gfp
= 0;
3278 #ifdef CONFIG_LOCK_STAT
3280 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3283 if (!debug_locks_off())
3285 if (debug_locks_silent
)
3288 printk("\n=================================\n");
3289 printk( "[ BUG: bad contention detected! ]\n");
3290 printk( "---------------------------------\n");
3291 printk("%s/%d is trying to contend lock (",
3292 curr
->comm
, task_pid_nr(curr
));
3293 print_lockdep_cache(lock
);
3296 printk("but there are no locks held!\n");
3297 printk("\nother info that might help us debug this:\n");
3298 lockdep_print_held_locks(curr
);
3300 printk("\nstack backtrace:\n");
3307 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3309 struct task_struct
*curr
= current
;
3310 struct held_lock
*hlock
, *prev_hlock
;
3311 struct lock_class_stats
*stats
;
3313 int i
, contention_point
, contending_point
;
3315 depth
= curr
->lockdep_depth
;
3316 if (DEBUG_LOCKS_WARN_ON(!depth
))
3320 for (i
= depth
-1; i
>= 0; i
--) {
3321 hlock
= curr
->held_locks
+ i
;
3323 * We must not cross into another context:
3325 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3327 if (match_held_lock(hlock
, lock
))
3331 print_lock_contention_bug(curr
, lock
, ip
);
3335 if (hlock
->instance
!= lock
)
3338 hlock
->waittime_stamp
= lockstat_clock();
3340 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3341 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3344 stats
= get_lock_stats(hlock_class(hlock
));
3345 if (contention_point
< LOCKSTAT_POINTS
)
3346 stats
->contention_point
[contention_point
]++;
3347 if (contending_point
< LOCKSTAT_POINTS
)
3348 stats
->contending_point
[contending_point
]++;
3349 if (lock
->cpu
!= smp_processor_id())
3350 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3351 put_lock_stats(stats
);
3355 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3357 struct task_struct
*curr
= current
;
3358 struct held_lock
*hlock
, *prev_hlock
;
3359 struct lock_class_stats
*stats
;
3361 u64 now
, waittime
= 0;
3364 depth
= curr
->lockdep_depth
;
3365 if (DEBUG_LOCKS_WARN_ON(!depth
))
3369 for (i
= depth
-1; i
>= 0; i
--) {
3370 hlock
= curr
->held_locks
+ i
;
3372 * We must not cross into another context:
3374 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3376 if (match_held_lock(hlock
, lock
))
3380 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3384 if (hlock
->instance
!= lock
)
3387 cpu
= smp_processor_id();
3388 if (hlock
->waittime_stamp
) {
3389 now
= lockstat_clock();
3390 waittime
= now
- hlock
->waittime_stamp
;
3391 hlock
->holdtime_stamp
= now
;
3394 trace_lock_acquired(lock
, ip
, waittime
);
3396 stats
= get_lock_stats(hlock_class(hlock
));
3399 lock_time_inc(&stats
->read_waittime
, waittime
);
3401 lock_time_inc(&stats
->write_waittime
, waittime
);
3403 if (lock
->cpu
!= cpu
)
3404 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3405 put_lock_stats(stats
);
3411 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3413 unsigned long flags
;
3415 trace_lock_contended(lock
, ip
);
3417 if (unlikely(!lock_stat
))
3420 if (unlikely(current
->lockdep_recursion
))
3423 raw_local_irq_save(flags
);
3425 current
->lockdep_recursion
= 1;
3426 __lock_contended(lock
, ip
);
3427 current
->lockdep_recursion
= 0;
3428 raw_local_irq_restore(flags
);
3430 EXPORT_SYMBOL_GPL(lock_contended
);
3432 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3434 unsigned long flags
;
3436 if (unlikely(!lock_stat
))
3439 if (unlikely(current
->lockdep_recursion
))
3442 raw_local_irq_save(flags
);
3444 current
->lockdep_recursion
= 1;
3445 __lock_acquired(lock
, ip
);
3446 current
->lockdep_recursion
= 0;
3447 raw_local_irq_restore(flags
);
3449 EXPORT_SYMBOL_GPL(lock_acquired
);
3453 * Used by the testsuite, sanitize the validator state
3454 * after a simulated failure:
3457 void lockdep_reset(void)
3459 unsigned long flags
;
3462 raw_local_irq_save(flags
);
3463 current
->curr_chain_key
= 0;
3464 current
->lockdep_depth
= 0;
3465 current
->lockdep_recursion
= 0;
3466 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3467 nr_hardirq_chains
= 0;
3468 nr_softirq_chains
= 0;
3469 nr_process_chains
= 0;
3471 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3472 INIT_LIST_HEAD(chainhash_table
+ i
);
3473 raw_local_irq_restore(flags
);
3476 static void zap_class(struct lock_class
*class)
3481 * Remove all dependencies this lock is
3484 for (i
= 0; i
< nr_list_entries
; i
++) {
3485 if (list_entries
[i
].class == class)
3486 list_del_rcu(&list_entries
[i
].entry
);
3489 * Unhash the class and remove it from the all_lock_classes list:
3491 list_del_rcu(&class->hash_entry
);
3492 list_del_rcu(&class->lock_entry
);
3497 static inline int within(const void *addr
, void *start
, unsigned long size
)
3499 return addr
>= start
&& addr
< start
+ size
;
3502 void lockdep_free_key_range(void *start
, unsigned long size
)
3504 struct lock_class
*class, *next
;
3505 struct list_head
*head
;
3506 unsigned long flags
;
3510 raw_local_irq_save(flags
);
3511 locked
= graph_lock();
3514 * Unhash all classes that were created by this module:
3516 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3517 head
= classhash_table
+ i
;
3518 if (list_empty(head
))
3520 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3521 if (within(class->key
, start
, size
))
3523 else if (within(class->name
, start
, size
))
3530 raw_local_irq_restore(flags
);
3533 void lockdep_reset_lock(struct lockdep_map
*lock
)
3535 struct lock_class
*class, *next
;
3536 struct list_head
*head
;
3537 unsigned long flags
;
3541 raw_local_irq_save(flags
);
3544 * Remove all classes this lock might have:
3546 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3548 * If the class exists we look it up and zap it:
3550 class = look_up_lock_class(lock
, j
);
3555 * Debug check: in the end all mapped classes should
3558 locked
= graph_lock();
3559 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3560 head
= classhash_table
+ i
;
3561 if (list_empty(head
))
3563 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3564 if (unlikely(class == lock
->class_cache
)) {
3565 if (debug_locks_off_graph_unlock())
3575 raw_local_irq_restore(flags
);
3578 void lockdep_init(void)
3583 * Some architectures have their own start_kernel()
3584 * code which calls lockdep_init(), while we also
3585 * call lockdep_init() from the start_kernel() itself,
3586 * and we want to initialize the hashes only once:
3588 if (lockdep_initialized
)
3591 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3592 INIT_LIST_HEAD(classhash_table
+ i
);
3594 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3595 INIT_LIST_HEAD(chainhash_table
+ i
);
3597 lockdep_initialized
= 1;
3600 void __init
lockdep_info(void)
3602 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3604 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3605 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3606 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3607 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3608 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3609 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3610 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3612 printk(" memory used by lock dependency info: %lu kB\n",
3613 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3614 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3615 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3616 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3617 sizeof(struct list_head
) * CHAINHASH_SIZE
3618 #ifdef CONFIG_PROVE_LOCKING
3619 + sizeof(struct circular_queue
)
3624 printk(" per task-struct memory footprint: %lu bytes\n",
3625 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3627 #ifdef CONFIG_DEBUG_LOCKDEP
3628 if (lockdep_init_error
) {
3629 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3630 printk("Call stack leading to lockdep invocation was:\n");
3631 print_stack_trace(&lockdep_init_trace
, 0);
3637 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3638 const void *mem_to
, struct held_lock
*hlock
)
3640 if (!debug_locks_off())
3642 if (debug_locks_silent
)
3645 printk("\n=========================\n");
3646 printk( "[ BUG: held lock freed! ]\n");
3647 printk( "-------------------------\n");
3648 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3649 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3651 lockdep_print_held_locks(curr
);
3653 printk("\nstack backtrace:\n");
3657 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3658 const void* lock_from
, unsigned long lock_len
)
3660 return lock_from
+ lock_len
<= mem_from
||
3661 mem_from
+ mem_len
<= lock_from
;
3665 * Called when kernel memory is freed (or unmapped), or if a lock
3666 * is destroyed or reinitialized - this code checks whether there is
3667 * any held lock in the memory range of <from> to <to>:
3669 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3671 struct task_struct
*curr
= current
;
3672 struct held_lock
*hlock
;
3673 unsigned long flags
;
3676 if (unlikely(!debug_locks
))
3679 local_irq_save(flags
);
3680 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3681 hlock
= curr
->held_locks
+ i
;
3683 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3684 sizeof(*hlock
->instance
)))
3687 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3690 local_irq_restore(flags
);
3692 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3694 static void print_held_locks_bug(struct task_struct
*curr
)
3696 if (!debug_locks_off())
3698 if (debug_locks_silent
)
3701 printk("\n=====================================\n");
3702 printk( "[ BUG: lock held at task exit time! ]\n");
3703 printk( "-------------------------------------\n");
3704 printk("%s/%d is exiting with locks still held!\n",
3705 curr
->comm
, task_pid_nr(curr
));
3706 lockdep_print_held_locks(curr
);
3708 printk("\nstack backtrace:\n");
3712 void debug_check_no_locks_held(struct task_struct
*task
)
3714 if (unlikely(task
->lockdep_depth
> 0))
3715 print_held_locks_bug(task
);
3718 void debug_show_all_locks(void)
3720 struct task_struct
*g
, *p
;
3724 if (unlikely(!debug_locks
)) {
3725 printk("INFO: lockdep is turned off.\n");
3728 printk("\nShowing all locks held in the system:\n");
3731 * Here we try to get the tasklist_lock as hard as possible,
3732 * if not successful after 2 seconds we ignore it (but keep
3733 * trying). This is to enable a debug printout even if a
3734 * tasklist_lock-holding task deadlocks or crashes.
3737 if (!read_trylock(&tasklist_lock
)) {
3739 printk("hm, tasklist_lock locked, retrying... ");
3742 printk(" #%d", 10-count
);
3746 printk(" ignoring it.\n");
3750 printk(KERN_CONT
" locked it.\n");
3753 do_each_thread(g
, p
) {
3755 * It's not reliable to print a task's held locks
3756 * if it's not sleeping (or if it's not the current
3759 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3761 if (p
->lockdep_depth
)
3762 lockdep_print_held_locks(p
);
3764 if (read_trylock(&tasklist_lock
))
3766 } while_each_thread(g
, p
);
3769 printk("=============================================\n\n");
3772 read_unlock(&tasklist_lock
);
3774 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3777 * Careful: only use this function if you are sure that
3778 * the task cannot run in parallel!
3780 void __debug_show_held_locks(struct task_struct
*task
)
3782 if (unlikely(!debug_locks
)) {
3783 printk("INFO: lockdep is turned off.\n");
3786 lockdep_print_held_locks(task
);
3788 EXPORT_SYMBOL_GPL(__debug_show_held_locks
);
3790 void debug_show_held_locks(struct task_struct
*task
)
3792 __debug_show_held_locks(task
);
3794 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3796 void lockdep_sys_exit(void)
3798 struct task_struct
*curr
= current
;
3800 if (unlikely(curr
->lockdep_depth
)) {
3801 if (!debug_locks_off())
3803 printk("\n================================================\n");
3804 printk( "[ BUG: lock held when returning to user space! ]\n");
3805 printk( "------------------------------------------------\n");
3806 printk("%s/%d is leaving the kernel with locks still held!\n",
3807 curr
->comm
, curr
->pid
);
3808 lockdep_print_held_locks(curr
);