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/lockdep.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 int lock_point(unsigned long points
[], unsigned long ip
)
149 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
150 if (points
[i
] == 0) {
161 static void lock_time_inc(struct lock_time
*lt
, s64 time
)
166 if (time
< lt
->min
|| !lt
->min
)
173 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
175 dst
->min
+= src
->min
;
176 dst
->max
+= src
->max
;
177 dst
->total
+= src
->total
;
181 struct lock_class_stats
lock_stats(struct lock_class
*class)
183 struct lock_class_stats stats
;
186 memset(&stats
, 0, sizeof(struct lock_class_stats
));
187 for_each_possible_cpu(cpu
) {
188 struct lock_class_stats
*pcs
=
189 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
191 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
192 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
194 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
195 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
197 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
198 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
200 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
201 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
203 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
204 stats
.bounces
[i
] += pcs
->bounces
[i
];
210 void clear_lock_stats(struct lock_class
*class)
214 for_each_possible_cpu(cpu
) {
215 struct lock_class_stats
*cpu_stats
=
216 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
218 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
220 memset(class->contention_point
, 0, sizeof(class->contention_point
));
221 memset(class->contending_point
, 0, sizeof(class->contending_point
));
224 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
226 return &get_cpu_var(lock_stats
)[class - lock_classes
];
229 static void put_lock_stats(struct lock_class_stats
*stats
)
231 put_cpu_var(lock_stats
);
234 static void lock_release_holdtime(struct held_lock
*hlock
)
236 struct lock_class_stats
*stats
;
242 holdtime
= sched_clock() - hlock
->holdtime_stamp
;
244 stats
= get_lock_stats(hlock_class(hlock
));
246 lock_time_inc(&stats
->read_holdtime
, holdtime
);
248 lock_time_inc(&stats
->write_holdtime
, holdtime
);
249 put_lock_stats(stats
);
252 static inline void lock_release_holdtime(struct held_lock
*hlock
)
258 * We keep a global list of all lock classes. The list only grows,
259 * never shrinks. The list is only accessed with the lockdep
260 * spinlock lock held.
262 LIST_HEAD(all_lock_classes
);
265 * The lockdep classes are in a hash-table as well, for fast lookup:
267 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
268 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
269 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
270 #define classhashentry(key) (classhash_table + __classhashfn((key)))
272 static struct list_head classhash_table
[CLASSHASH_SIZE
];
275 * We put the lock dependency chains into a hash-table as well, to cache
278 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
279 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
280 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
281 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
283 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
286 * The hash key of the lock dependency chains is a hash itself too:
287 * it's a hash of all locks taken up to that lock, including that lock.
288 * It's a 64-bit hash, because it's important for the keys to be
291 #define iterate_chain_key(key1, key2) \
292 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
293 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
296 void lockdep_off(void)
298 current
->lockdep_recursion
++;
300 EXPORT_SYMBOL(lockdep_off
);
302 void lockdep_on(void)
304 current
->lockdep_recursion
--;
306 EXPORT_SYMBOL(lockdep_on
);
309 * Debugging switches:
313 #define VERY_VERBOSE 0
316 # define HARDIRQ_VERBOSE 1
317 # define SOFTIRQ_VERBOSE 1
318 # define RECLAIM_VERBOSE 1
320 # define HARDIRQ_VERBOSE 0
321 # define SOFTIRQ_VERBOSE 0
322 # define RECLAIM_VERBOSE 0
325 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
327 * Quick filtering for interesting events:
329 static int class_filter(struct lock_class
*class)
333 if (class->name_version
== 1 &&
334 !strcmp(class->name
, "lockname"))
336 if (class->name_version
== 1 &&
337 !strcmp(class->name
, "&struct->lockfield"))
340 /* Filter everything else. 1 would be to allow everything else */
345 static int verbose(struct lock_class
*class)
348 return class_filter(class);
354 * Stack-trace: tightly packed array of stack backtrace
355 * addresses. Protected by the graph_lock.
357 unsigned long nr_stack_trace_entries
;
358 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
360 static int save_trace(struct stack_trace
*trace
)
362 trace
->nr_entries
= 0;
363 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
364 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
368 save_stack_trace(trace
);
371 * Some daft arches put -1 at the end to indicate its a full trace.
373 * <rant> this is buggy anyway, since it takes a whole extra entry so a
374 * complete trace that maxes out the entries provided will be reported
375 * as incomplete, friggin useless </rant>
377 if (trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
380 trace
->max_entries
= trace
->nr_entries
;
382 nr_stack_trace_entries
+= trace
->nr_entries
;
384 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
385 if (!debug_locks_off_graph_unlock())
388 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
389 printk("turning off the locking correctness validator.\n");
398 unsigned int nr_hardirq_chains
;
399 unsigned int nr_softirq_chains
;
400 unsigned int nr_process_chains
;
401 unsigned int max_lockdep_depth
;
403 #ifdef CONFIG_DEBUG_LOCKDEP
405 * We cannot printk in early bootup code. Not even early_printk()
406 * might work. So we mark any initialization errors and printk
407 * about it later on, in lockdep_info().
409 static int lockdep_init_error
;
410 static unsigned long lockdep_init_trace_data
[20];
411 static struct stack_trace lockdep_init_trace
= {
412 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
413 .entries
= lockdep_init_trace_data
,
417 * Various lockdep statistics:
419 atomic_t chain_lookup_hits
;
420 atomic_t chain_lookup_misses
;
421 atomic_t hardirqs_on_events
;
422 atomic_t hardirqs_off_events
;
423 atomic_t redundant_hardirqs_on
;
424 atomic_t redundant_hardirqs_off
;
425 atomic_t softirqs_on_events
;
426 atomic_t softirqs_off_events
;
427 atomic_t redundant_softirqs_on
;
428 atomic_t redundant_softirqs_off
;
429 atomic_t nr_unused_locks
;
430 atomic_t nr_cyclic_checks
;
431 atomic_t nr_find_usage_forwards_checks
;
432 atomic_t nr_find_usage_backwards_checks
;
439 #define __USAGE(__STATE) \
440 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
441 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
442 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
443 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
445 static const char *usage_str
[] =
447 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
448 #include "lockdep_states.h"
450 [LOCK_USED
] = "INITIAL USE",
453 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
455 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
458 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
463 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
467 if (class->usage_mask
& lock_flag(bit
+ 2))
469 if (class->usage_mask
& lock_flag(bit
)) {
471 if (class->usage_mask
& lock_flag(bit
+ 2))
478 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
482 #define LOCKDEP_STATE(__STATE) \
483 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
484 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
485 #include "lockdep_states.h"
491 static void print_lock_name(struct lock_class
*class)
493 char str
[KSYM_NAME_LEN
], usage
[LOCK_USAGE_CHARS
];
496 get_usage_chars(class, usage
);
500 name
= __get_key_name(class->key
, str
);
501 printk(" (%s", name
);
503 printk(" (%s", name
);
504 if (class->name_version
> 1)
505 printk("#%d", class->name_version
);
507 printk("/%d", class->subclass
);
509 printk("){%s}", usage
);
512 static void print_lockdep_cache(struct lockdep_map
*lock
)
515 char str
[KSYM_NAME_LEN
];
519 name
= __get_key_name(lock
->key
->subkeys
, str
);
524 static void print_lock(struct held_lock
*hlock
)
526 print_lock_name(hlock_class(hlock
));
528 print_ip_sym(hlock
->acquire_ip
);
531 static void lockdep_print_held_locks(struct task_struct
*curr
)
533 int i
, depth
= curr
->lockdep_depth
;
536 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
539 printk("%d lock%s held by %s/%d:\n",
540 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
542 for (i
= 0; i
< depth
; i
++) {
544 print_lock(curr
->held_locks
+ i
);
548 static void print_kernel_version(void)
550 printk("%s %.*s\n", init_utsname()->release
,
551 (int)strcspn(init_utsname()->version
, " "),
552 init_utsname()->version
);
555 static int very_verbose(struct lock_class
*class)
558 return class_filter(class);
564 * Is this the address of a static object:
566 static int static_obj(void *obj
)
568 unsigned long start
= (unsigned long) &_stext
,
569 end
= (unsigned long) &_end
,
570 addr
= (unsigned long) obj
;
578 if ((addr
>= start
) && (addr
< end
))
585 for_each_possible_cpu(i
) {
586 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
587 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
590 if ((addr
>= start
) && (addr
< end
))
598 return is_module_address(addr
);
602 * To make lock name printouts unique, we calculate a unique
603 * class->name_version generation counter:
605 static int count_matching_names(struct lock_class
*new_class
)
607 struct lock_class
*class;
610 if (!new_class
->name
)
613 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
614 if (new_class
->key
- new_class
->subclass
== class->key
)
615 return class->name_version
;
616 if (class->name
&& !strcmp(class->name
, new_class
->name
))
617 count
= max(count
, class->name_version
);
624 * Register a lock's class in the hash-table, if the class is not present
625 * yet. Otherwise we look it up. We cache the result in the lock object
626 * itself, so actual lookup of the hash should be once per lock object.
628 static inline struct lock_class
*
629 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
631 struct lockdep_subclass_key
*key
;
632 struct list_head
*hash_head
;
633 struct lock_class
*class;
635 #ifdef CONFIG_DEBUG_LOCKDEP
637 * If the architecture calls into lockdep before initializing
638 * the hashes then we'll warn about it later. (we cannot printk
641 if (unlikely(!lockdep_initialized
)) {
643 lockdep_init_error
= 1;
644 save_stack_trace(&lockdep_init_trace
);
649 * Static locks do not have their class-keys yet - for them the key
650 * is the lock object itself:
652 if (unlikely(!lock
->key
))
653 lock
->key
= (void *)lock
;
656 * NOTE: the class-key must be unique. For dynamic locks, a static
657 * lock_class_key variable is passed in through the mutex_init()
658 * (or spin_lock_init()) call - which acts as the key. For static
659 * locks we use the lock object itself as the key.
661 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
662 sizeof(struct lockdep_map
));
664 key
= lock
->key
->subkeys
+ subclass
;
666 hash_head
= classhashentry(key
);
669 * We can walk the hash lockfree, because the hash only
670 * grows, and we are careful when adding entries to the end:
672 list_for_each_entry(class, hash_head
, hash_entry
) {
673 if (class->key
== key
) {
674 WARN_ON_ONCE(class->name
!= lock
->name
);
683 * Register a lock's class in the hash-table, if the class is not present
684 * yet. Otherwise we look it up. We cache the result in the lock object
685 * itself, so actual lookup of the hash should be once per lock object.
687 static inline struct lock_class
*
688 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
690 struct lockdep_subclass_key
*key
;
691 struct list_head
*hash_head
;
692 struct lock_class
*class;
695 class = look_up_lock_class(lock
, subclass
);
700 * Debug-check: all keys must be persistent!
702 if (!static_obj(lock
->key
)) {
704 printk("INFO: trying to register non-static key.\n");
705 printk("the code is fine but needs lockdep annotation.\n");
706 printk("turning off the locking correctness validator.\n");
712 key
= lock
->key
->subkeys
+ subclass
;
713 hash_head
= classhashentry(key
);
715 raw_local_irq_save(flags
);
717 raw_local_irq_restore(flags
);
721 * We have to do the hash-walk again, to avoid races
724 list_for_each_entry(class, hash_head
, hash_entry
)
725 if (class->key
== key
)
728 * Allocate a new key from the static array, and add it to
731 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
732 if (!debug_locks_off_graph_unlock()) {
733 raw_local_irq_restore(flags
);
736 raw_local_irq_restore(flags
);
738 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
739 printk("turning off the locking correctness validator.\n");
743 class = lock_classes
+ nr_lock_classes
++;
744 debug_atomic_inc(&nr_unused_locks
);
746 class->name
= lock
->name
;
747 class->subclass
= subclass
;
748 INIT_LIST_HEAD(&class->lock_entry
);
749 INIT_LIST_HEAD(&class->locks_before
);
750 INIT_LIST_HEAD(&class->locks_after
);
751 class->name_version
= count_matching_names(class);
753 * We use RCU's safe list-add method to make
754 * parallel walking of the hash-list safe:
756 list_add_tail_rcu(&class->hash_entry
, hash_head
);
758 * Add it to the global list of classes:
760 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
762 if (verbose(class)) {
764 raw_local_irq_restore(flags
);
766 printk("\nnew class %p: %s", class->key
, class->name
);
767 if (class->name_version
> 1)
768 printk("#%d", class->name_version
);
772 raw_local_irq_save(flags
);
774 raw_local_irq_restore(flags
);
780 raw_local_irq_restore(flags
);
782 if (!subclass
|| force
)
783 lock
->class_cache
= class;
785 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
791 #ifdef CONFIG_PROVE_LOCKING
793 * Allocate a lockdep entry. (assumes the graph_lock held, returns
794 * with NULL on failure)
796 static struct lock_list
*alloc_list_entry(void)
798 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
799 if (!debug_locks_off_graph_unlock())
802 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
803 printk("turning off the locking correctness validator.\n");
807 return list_entries
+ nr_list_entries
++;
811 * Add a new dependency to the head of the list:
813 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
814 struct list_head
*head
, unsigned long ip
, int distance
)
816 struct lock_list
*entry
;
818 * Lock not present yet - get a new dependency struct and
819 * add it to the list:
821 entry
= alloc_list_entry();
825 if (!save_trace(&entry
->trace
))
829 entry
->distance
= distance
;
831 * Since we never remove from the dependency list, the list can
832 * be walked lockless by other CPUs, it's only allocation
833 * that must be protected by the spinlock. But this also means
834 * we must make new entries visible only once writes to the
835 * entry become visible - hence the RCU op:
837 list_add_tail_rcu(&entry
->entry
, head
);
843 * For good efficiency of modular, we use power of 2
845 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
846 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
849 * The circular_queue and helpers is used to implement the
850 * breadth-first search(BFS)algorithem, by which we can build
851 * the shortest path from the next lock to be acquired to the
852 * previous held lock if there is a circular between them.
854 struct circular_queue
{
855 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
856 unsigned int front
, rear
;
859 static struct circular_queue lock_cq
;
861 unsigned int max_bfs_queue_depth
;
863 static unsigned int lockdep_dependency_gen_id
;
865 static inline void __cq_init(struct circular_queue
*cq
)
867 cq
->front
= cq
->rear
= 0;
868 lockdep_dependency_gen_id
++;
871 static inline int __cq_empty(struct circular_queue
*cq
)
873 return (cq
->front
== cq
->rear
);
876 static inline int __cq_full(struct circular_queue
*cq
)
878 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
881 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
886 cq
->element
[cq
->rear
] = elem
;
887 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
891 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
896 *elem
= cq
->element
[cq
->front
];
897 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
901 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
903 return (cq
->rear
- cq
->front
) & CQ_MASK
;
906 static inline void mark_lock_accessed(struct lock_list
*lock
,
907 struct lock_list
*parent
)
911 nr
= lock
- list_entries
;
912 WARN_ON(nr
>= nr_list_entries
);
913 lock
->parent
= parent
;
914 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
917 static inline unsigned long lock_accessed(struct lock_list
*lock
)
921 nr
= lock
- list_entries
;
922 WARN_ON(nr
>= nr_list_entries
);
923 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
926 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
928 return child
->parent
;
931 static inline int get_lock_depth(struct lock_list
*child
)
934 struct lock_list
*parent
;
936 while ((parent
= get_lock_parent(child
))) {
943 static int __bfs(struct lock_list
*source_entry
,
945 int (*match
)(struct lock_list
*entry
, void *data
),
946 struct lock_list
**target_entry
,
949 struct lock_list
*entry
;
950 struct list_head
*head
;
951 struct circular_queue
*cq
= &lock_cq
;
954 if (match(source_entry
, data
)) {
955 *target_entry
= source_entry
;
961 head
= &source_entry
->class->locks_after
;
963 head
= &source_entry
->class->locks_before
;
965 if (list_empty(head
))
969 __cq_enqueue(cq
, (unsigned long)source_entry
);
971 while (!__cq_empty(cq
)) {
972 struct lock_list
*lock
;
974 __cq_dequeue(cq
, (unsigned long *)&lock
);
982 head
= &lock
->class->locks_after
;
984 head
= &lock
->class->locks_before
;
986 list_for_each_entry(entry
, head
, entry
) {
987 if (!lock_accessed(entry
)) {
988 unsigned int cq_depth
;
989 mark_lock_accessed(entry
, lock
);
990 if (match(entry
, data
)) {
991 *target_entry
= entry
;
996 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1000 cq_depth
= __cq_get_elem_count(cq
);
1001 if (max_bfs_queue_depth
< cq_depth
)
1002 max_bfs_queue_depth
= cq_depth
;
1010 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1012 int (*match
)(struct lock_list
*entry
, void *data
),
1013 struct lock_list
**target_entry
)
1015 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1019 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1021 int (*match
)(struct lock_list
*entry
, void *data
),
1022 struct lock_list
**target_entry
)
1024 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1029 * Recursive, forwards-direction lock-dependency checking, used for
1030 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1035 * Print a dependency chain entry (this is only done when a deadlock
1036 * has been detected):
1039 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1041 if (debug_locks_silent
)
1043 printk("\n-> #%u", depth
);
1044 print_lock_name(target
->class);
1046 print_stack_trace(&target
->trace
, 6);
1052 * When a circular dependency is detected, print the
1056 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1057 struct held_lock
*check_src
,
1058 struct held_lock
*check_tgt
)
1060 struct task_struct
*curr
= current
;
1062 if (debug_locks_silent
)
1065 printk("\n=======================================================\n");
1066 printk( "[ INFO: possible circular locking dependency detected ]\n");
1067 print_kernel_version();
1068 printk( "-------------------------------------------------------\n");
1069 printk("%s/%d is trying to acquire lock:\n",
1070 curr
->comm
, task_pid_nr(curr
));
1071 print_lock(check_src
);
1072 printk("\nbut task is already holding lock:\n");
1073 print_lock(check_tgt
);
1074 printk("\nwhich lock already depends on the new lock.\n\n");
1075 printk("\nthe existing dependency chain (in reverse order) is:\n");
1077 print_circular_bug_entry(entry
, depth
);
1082 static inline int class_equal(struct lock_list
*entry
, void *data
)
1084 return entry
->class == data
;
1087 static noinline
int print_circular_bug(struct lock_list
*this,
1088 struct lock_list
*target
,
1089 struct held_lock
*check_src
,
1090 struct held_lock
*check_tgt
)
1092 struct task_struct
*curr
= current
;
1093 struct lock_list
*parent
;
1096 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1099 if (!save_trace(&this->trace
))
1102 depth
= get_lock_depth(target
);
1104 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1106 parent
= get_lock_parent(target
);
1109 print_circular_bug_entry(parent
, --depth
);
1110 parent
= get_lock_parent(parent
);
1113 printk("\nother info that might help us debug this:\n\n");
1114 lockdep_print_held_locks(curr
);
1116 printk("\nstack backtrace:\n");
1122 static noinline
int print_bfs_bug(int ret
)
1124 if (!debug_locks_off_graph_unlock())
1127 WARN(1, "lockdep bfs error:%d\n", ret
);
1132 static int noop_count(struct lock_list
*entry
, void *data
)
1134 (*(unsigned long *)data
)++;
1138 unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1140 unsigned long count
= 0;
1141 struct lock_list
*uninitialized_var(target_entry
);
1143 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1147 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1149 unsigned long ret
, flags
;
1150 struct lock_list
this;
1155 local_irq_save(flags
);
1156 __raw_spin_lock(&lockdep_lock
);
1157 ret
= __lockdep_count_forward_deps(&this);
1158 __raw_spin_unlock(&lockdep_lock
);
1159 local_irq_restore(flags
);
1164 unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1166 unsigned long count
= 0;
1167 struct lock_list
*uninitialized_var(target_entry
);
1169 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1174 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1176 unsigned long ret
, flags
;
1177 struct lock_list
this;
1182 local_irq_save(flags
);
1183 __raw_spin_lock(&lockdep_lock
);
1184 ret
= __lockdep_count_backward_deps(&this);
1185 __raw_spin_unlock(&lockdep_lock
);
1186 local_irq_restore(flags
);
1192 * Prove that the dependency graph starting at <entry> can not
1193 * lead to <target>. Print an error and return 0 if it does.
1196 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1197 struct lock_list
**target_entry
)
1201 debug_atomic_inc(&nr_cyclic_checks
);
1203 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1208 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1210 * Forwards and backwards subgraph searching, for the purposes of
1211 * proving that two subgraphs can be connected by a new dependency
1212 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1215 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1217 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1223 * Find a node in the forwards-direction dependency sub-graph starting
1224 * at @root->class that matches @bit.
1226 * Return 0 if such a node exists in the subgraph, and put that node
1227 * into *@target_entry.
1229 * Return 1 otherwise and keep *@target_entry unchanged.
1230 * Return <0 on error.
1233 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1234 struct lock_list
**target_entry
)
1238 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1240 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1246 * Find a node in the backwards-direction dependency sub-graph starting
1247 * at @root->class that matches @bit.
1249 * Return 0 if such a node exists in the subgraph, and put that node
1250 * into *@target_entry.
1252 * Return 1 otherwise and keep *@target_entry unchanged.
1253 * Return <0 on error.
1256 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1257 struct lock_list
**target_entry
)
1261 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1263 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1268 static void print_lock_class_header(struct lock_class
*class, int depth
)
1272 printk("%*s->", depth
, "");
1273 print_lock_name(class);
1274 printk(" ops: %lu", class->ops
);
1277 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1278 if (class->usage_mask
& (1 << bit
)) {
1281 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1282 len
+= printk(" at:\n");
1283 print_stack_trace(class->usage_traces
+ bit
, len
);
1286 printk("%*s }\n", depth
, "");
1288 printk("%*s ... key at: ",depth
,"");
1289 print_ip_sym((unsigned long)class->key
);
1293 * printk the shortest lock dependencies from @start to @end in reverse order:
1296 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1297 struct lock_list
*root
)
1299 struct lock_list
*entry
= leaf
;
1302 /*compute depth from generated tree by BFS*/
1303 depth
= get_lock_depth(leaf
);
1306 print_lock_class_header(entry
->class, depth
);
1307 printk("%*s ... acquired at:\n", depth
, "");
1308 print_stack_trace(&entry
->trace
, 2);
1311 if (depth
== 0 && (entry
!= root
)) {
1312 printk("lockdep:%s bad BFS generated tree\n", __func__
);
1316 entry
= get_lock_parent(entry
);
1318 } while (entry
&& (depth
>= 0));
1324 print_bad_irq_dependency(struct task_struct
*curr
,
1325 struct lock_list
*prev_root
,
1326 struct lock_list
*next_root
,
1327 struct lock_list
*backwards_entry
,
1328 struct lock_list
*forwards_entry
,
1329 struct held_lock
*prev
,
1330 struct held_lock
*next
,
1331 enum lock_usage_bit bit1
,
1332 enum lock_usage_bit bit2
,
1333 const char *irqclass
)
1335 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1338 printk("\n======================================================\n");
1339 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1340 irqclass
, irqclass
);
1341 print_kernel_version();
1342 printk( "------------------------------------------------------\n");
1343 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1344 curr
->comm
, task_pid_nr(curr
),
1345 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1346 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1347 curr
->hardirqs_enabled
,
1348 curr
->softirqs_enabled
);
1351 printk("\nand this task is already holding:\n");
1353 printk("which would create a new lock dependency:\n");
1354 print_lock_name(hlock_class(prev
));
1356 print_lock_name(hlock_class(next
));
1359 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1361 print_lock_name(backwards_entry
->class);
1362 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1364 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1366 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1367 print_lock_name(forwards_entry
->class);
1368 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1371 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1373 printk("\nother info that might help us debug this:\n\n");
1374 lockdep_print_held_locks(curr
);
1376 printk("\nthe dependencies between %s-irq-safe lock", irqclass
);
1377 printk(" and the holding lock:\n");
1378 if (!save_trace(&prev_root
->trace
))
1380 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1382 printk("\nthe dependencies between the lock to be acquired");
1383 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1384 if (!save_trace(&next_root
->trace
))
1386 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1388 printk("\nstack backtrace:\n");
1395 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1396 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1397 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1400 struct lock_list
this, that
;
1401 struct lock_list
*uninitialized_var(target_entry
);
1402 struct lock_list
*uninitialized_var(target_entry1
);
1406 this.class = hlock_class(prev
);
1407 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1409 return print_bfs_bug(ret
);
1414 that
.class = hlock_class(next
);
1415 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1417 return print_bfs_bug(ret
);
1421 return print_bad_irq_dependency(curr
, &this, &that
,
1422 target_entry
, target_entry1
,
1424 bit_backwards
, bit_forwards
, irqclass
);
1427 static const char *state_names
[] = {
1428 #define LOCKDEP_STATE(__STATE) \
1429 __stringify(__STATE),
1430 #include "lockdep_states.h"
1431 #undef LOCKDEP_STATE
1434 static const char *state_rnames
[] = {
1435 #define LOCKDEP_STATE(__STATE) \
1436 __stringify(__STATE)"-READ",
1437 #include "lockdep_states.h"
1438 #undef LOCKDEP_STATE
1441 static inline const char *state_name(enum lock_usage_bit bit
)
1443 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1446 static int exclusive_bit(int new_bit
)
1454 * bit 0 - write/read
1455 * bit 1 - used_in/enabled
1459 int state
= new_bit
& ~3;
1460 int dir
= new_bit
& 2;
1463 * keep state, bit flip the direction and strip read.
1465 return state
| (dir
^ 2);
1468 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1469 struct held_lock
*next
, enum lock_usage_bit bit
)
1472 * Prove that the new dependency does not connect a hardirq-safe
1473 * lock with a hardirq-unsafe lock - to achieve this we search
1474 * the backwards-subgraph starting at <prev>, and the
1475 * forwards-subgraph starting at <next>:
1477 if (!check_usage(curr
, prev
, next
, bit
,
1478 exclusive_bit(bit
), state_name(bit
)))
1484 * Prove that the new dependency does not connect a hardirq-safe-read
1485 * lock with a hardirq-unsafe lock - to achieve this we search
1486 * the backwards-subgraph starting at <prev>, and the
1487 * forwards-subgraph starting at <next>:
1489 if (!check_usage(curr
, prev
, next
, bit
,
1490 exclusive_bit(bit
), state_name(bit
)))
1497 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1498 struct held_lock
*next
)
1500 #define LOCKDEP_STATE(__STATE) \
1501 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1503 #include "lockdep_states.h"
1504 #undef LOCKDEP_STATE
1509 static void inc_chains(void)
1511 if (current
->hardirq_context
)
1512 nr_hardirq_chains
++;
1514 if (current
->softirq_context
)
1515 nr_softirq_chains
++;
1517 nr_process_chains
++;
1524 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1525 struct held_lock
*next
)
1530 static inline void inc_chains(void)
1532 nr_process_chains
++;
1538 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1539 struct held_lock
*next
)
1541 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1544 printk("\n=============================================\n");
1545 printk( "[ INFO: possible recursive locking detected ]\n");
1546 print_kernel_version();
1547 printk( "---------------------------------------------\n");
1548 printk("%s/%d is trying to acquire lock:\n",
1549 curr
->comm
, task_pid_nr(curr
));
1551 printk("\nbut task is already holding lock:\n");
1554 printk("\nother info that might help us debug this:\n");
1555 lockdep_print_held_locks(curr
);
1557 printk("\nstack backtrace:\n");
1564 * Check whether we are holding such a class already.
1566 * (Note that this has to be done separately, because the graph cannot
1567 * detect such classes of deadlocks.)
1569 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1572 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1573 struct lockdep_map
*next_instance
, int read
)
1575 struct held_lock
*prev
;
1576 struct held_lock
*nest
= NULL
;
1579 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1580 prev
= curr
->held_locks
+ i
;
1582 if (prev
->instance
== next
->nest_lock
)
1585 if (hlock_class(prev
) != hlock_class(next
))
1589 * Allow read-after-read recursion of the same
1590 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1592 if ((read
== 2) && prev
->read
)
1596 * We're holding the nest_lock, which serializes this lock's
1597 * nesting behaviour.
1602 return print_deadlock_bug(curr
, prev
, next
);
1608 * There was a chain-cache miss, and we are about to add a new dependency
1609 * to a previous lock. We recursively validate the following rules:
1611 * - would the adding of the <prev> -> <next> dependency create a
1612 * circular dependency in the graph? [== circular deadlock]
1614 * - does the new prev->next dependency connect any hardirq-safe lock
1615 * (in the full backwards-subgraph starting at <prev>) with any
1616 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1617 * <next>)? [== illegal lock inversion with hardirq contexts]
1619 * - does the new prev->next dependency connect any softirq-safe lock
1620 * (in the full backwards-subgraph starting at <prev>) with any
1621 * softirq-unsafe lock (in the full forwards-subgraph starting at
1622 * <next>)? [== illegal lock inversion with softirq contexts]
1624 * any of these scenarios could lead to a deadlock.
1626 * Then if all the validations pass, we add the forwards and backwards
1630 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1631 struct held_lock
*next
, int distance
)
1633 struct lock_list
*entry
;
1635 struct lock_list
this;
1636 struct lock_list
*uninitialized_var(target_entry
);
1639 * Prove that the new <prev> -> <next> dependency would not
1640 * create a circular dependency in the graph. (We do this by
1641 * forward-recursing into the graph starting at <next>, and
1642 * checking whether we can reach <prev>.)
1644 * We are using global variables to control the recursion, to
1645 * keep the stackframe size of the recursive functions low:
1647 this.class = hlock_class(next
);
1649 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1651 return print_circular_bug(&this, target_entry
, next
, prev
);
1652 else if (unlikely(ret
< 0))
1653 return print_bfs_bug(ret
);
1655 if (!check_prev_add_irq(curr
, prev
, next
))
1659 * For recursive read-locks we do all the dependency checks,
1660 * but we dont store read-triggered dependencies (only
1661 * write-triggered dependencies). This ensures that only the
1662 * write-side dependencies matter, and that if for example a
1663 * write-lock never takes any other locks, then the reads are
1664 * equivalent to a NOP.
1666 if (next
->read
== 2 || prev
->read
== 2)
1669 * Is the <prev> -> <next> dependency already present?
1671 * (this may occur even though this is a new chain: consider
1672 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1673 * chains - the second one will be new, but L1 already has
1674 * L2 added to its dependency list, due to the first chain.)
1676 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1677 if (entry
->class == hlock_class(next
)) {
1679 entry
->distance
= 1;
1685 * Ok, all validations passed, add the new lock
1686 * to the previous lock's dependency list:
1688 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1689 &hlock_class(prev
)->locks_after
,
1690 next
->acquire_ip
, distance
);
1695 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1696 &hlock_class(next
)->locks_before
,
1697 next
->acquire_ip
, distance
);
1702 * Debugging printouts:
1704 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1706 printk("\n new dependency: ");
1707 print_lock_name(hlock_class(prev
));
1709 print_lock_name(hlock_class(next
));
1712 return graph_lock();
1718 * Add the dependency to all directly-previous locks that are 'relevant'.
1719 * The ones that are relevant are (in increasing distance from curr):
1720 * all consecutive trylock entries and the final non-trylock entry - or
1721 * the end of this context's lock-chain - whichever comes first.
1724 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1726 int depth
= curr
->lockdep_depth
;
1727 struct held_lock
*hlock
;
1732 * Depth must not be zero for a non-head lock:
1737 * At least two relevant locks must exist for this
1740 if (curr
->held_locks
[depth
].irq_context
!=
1741 curr
->held_locks
[depth
-1].irq_context
)
1745 int distance
= curr
->lockdep_depth
- depth
+ 1;
1746 hlock
= curr
->held_locks
+ depth
-1;
1748 * Only non-recursive-read entries get new dependencies
1751 if (hlock
->read
!= 2) {
1752 if (!check_prev_add(curr
, hlock
, next
, distance
))
1755 * Stop after the first non-trylock entry,
1756 * as non-trylock entries have added their
1757 * own direct dependencies already, so this
1758 * lock is connected to them indirectly:
1760 if (!hlock
->trylock
)
1765 * End of lock-stack?
1770 * Stop the search if we cross into another context:
1772 if (curr
->held_locks
[depth
].irq_context
!=
1773 curr
->held_locks
[depth
-1].irq_context
)
1778 if (!debug_locks_off_graph_unlock())
1786 unsigned long nr_lock_chains
;
1787 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1788 int nr_chain_hlocks
;
1789 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1791 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1793 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1797 * Look up a dependency chain. If the key is not present yet then
1798 * add it and return 1 - in this case the new dependency chain is
1799 * validated. If the key is already hashed, return 0.
1800 * (On return with 1 graph_lock is held.)
1802 static inline int lookup_chain_cache(struct task_struct
*curr
,
1803 struct held_lock
*hlock
,
1806 struct lock_class
*class = hlock_class(hlock
);
1807 struct list_head
*hash_head
= chainhashentry(chain_key
);
1808 struct lock_chain
*chain
;
1809 struct held_lock
*hlock_curr
, *hlock_next
;
1812 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1815 * We can walk it lock-free, because entries only get added
1818 list_for_each_entry(chain
, hash_head
, entry
) {
1819 if (chain
->chain_key
== chain_key
) {
1821 debug_atomic_inc(&chain_lookup_hits
);
1822 if (very_verbose(class))
1823 printk("\nhash chain already cached, key: "
1824 "%016Lx tail class: [%p] %s\n",
1825 (unsigned long long)chain_key
,
1826 class->key
, class->name
);
1830 if (very_verbose(class))
1831 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1832 (unsigned long long)chain_key
, class->key
, class->name
);
1834 * Allocate a new chain entry from the static array, and add
1840 * We have to walk the chain again locked - to avoid duplicates:
1842 list_for_each_entry(chain
, hash_head
, entry
) {
1843 if (chain
->chain_key
== chain_key
) {
1848 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1849 if (!debug_locks_off_graph_unlock())
1852 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1853 printk("turning off the locking correctness validator.\n");
1857 chain
= lock_chains
+ nr_lock_chains
++;
1858 chain
->chain_key
= chain_key
;
1859 chain
->irq_context
= hlock
->irq_context
;
1860 /* Find the first held_lock of current chain */
1862 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
1863 hlock_curr
= curr
->held_locks
+ i
;
1864 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
1869 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
1870 cn
= nr_chain_hlocks
;
1871 while (cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
) {
1872 n
= cmpxchg(&nr_chain_hlocks
, cn
, cn
+ chain
->depth
);
1877 if (likely(cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
1879 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
1880 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
1881 chain_hlocks
[chain
->base
+ j
] = lock_id
;
1883 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
1885 list_add_tail_rcu(&chain
->entry
, hash_head
);
1886 debug_atomic_inc(&chain_lookup_misses
);
1892 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1893 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
1896 * Trylock needs to maintain the stack of held locks, but it
1897 * does not add new dependencies, because trylock can be done
1900 * We look up the chain_key and do the O(N^2) check and update of
1901 * the dependencies only if this is a new dependency chain.
1902 * (If lookup_chain_cache() returns with 1 it acquires
1903 * graph_lock for us)
1905 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1906 lookup_chain_cache(curr
, hlock
, chain_key
)) {
1908 * Check whether last held lock:
1910 * - is irq-safe, if this lock is irq-unsafe
1911 * - is softirq-safe, if this lock is hardirq-unsafe
1913 * And check whether the new lock's dependency graph
1914 * could lead back to the previous lock.
1916 * any of these scenarios could lead to a deadlock. If
1919 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1924 * Mark recursive read, as we jump over it when
1925 * building dependencies (just like we jump over
1931 * Add dependency only if this lock is not the head
1932 * of the chain, and if it's not a secondary read-lock:
1934 if (!chain_head
&& ret
!= 2)
1935 if (!check_prevs_add(curr
, hlock
))
1939 /* after lookup_chain_cache(): */
1940 if (unlikely(!debug_locks
))
1946 static inline int validate_chain(struct task_struct
*curr
,
1947 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1948 int chain_head
, u64 chain_key
)
1955 * We are building curr_chain_key incrementally, so double-check
1956 * it from scratch, to make sure that it's done correctly:
1958 static void check_chain_key(struct task_struct
*curr
)
1960 #ifdef CONFIG_DEBUG_LOCKDEP
1961 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1965 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1966 hlock
= curr
->held_locks
+ i
;
1967 if (chain_key
!= hlock
->prev_chain_key
) {
1969 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1970 curr
->lockdep_depth
, i
,
1971 (unsigned long long)chain_key
,
1972 (unsigned long long)hlock
->prev_chain_key
);
1975 id
= hlock
->class_idx
- 1;
1976 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1979 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1980 hlock
->irq_context
))
1982 chain_key
= iterate_chain_key(chain_key
, id
);
1985 if (chain_key
!= curr
->curr_chain_key
) {
1987 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1988 curr
->lockdep_depth
, i
,
1989 (unsigned long long)chain_key
,
1990 (unsigned long long)curr
->curr_chain_key
);
1996 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1997 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1999 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2002 printk("\n=================================\n");
2003 printk( "[ INFO: inconsistent lock state ]\n");
2004 print_kernel_version();
2005 printk( "---------------------------------\n");
2007 printk("inconsistent {%s} -> {%s} usage.\n",
2008 usage_str
[prev_bit
], usage_str
[new_bit
]);
2010 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2011 curr
->comm
, task_pid_nr(curr
),
2012 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2013 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2014 trace_hardirqs_enabled(curr
),
2015 trace_softirqs_enabled(curr
));
2018 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2019 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2021 print_irqtrace_events(curr
);
2022 printk("\nother info that might help us debug this:\n");
2023 lockdep_print_held_locks(curr
);
2025 printk("\nstack backtrace:\n");
2032 * Print out an error if an invalid bit is set:
2035 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2036 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2038 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2039 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2043 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2044 enum lock_usage_bit new_bit
);
2046 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2049 * print irq inversion bug:
2052 print_irq_inversion_bug(struct task_struct
*curr
,
2053 struct lock_list
*root
, struct lock_list
*other
,
2054 struct held_lock
*this, int forwards
,
2055 const char *irqclass
)
2057 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2060 printk("\n=========================================================\n");
2061 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2062 print_kernel_version();
2063 printk( "---------------------------------------------------------\n");
2064 printk("%s/%d just changed the state of lock:\n",
2065 curr
->comm
, task_pid_nr(curr
));
2068 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2070 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2071 print_lock_name(other
->class);
2072 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2074 printk("\nother info that might help us debug this:\n");
2075 lockdep_print_held_locks(curr
);
2077 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2078 if (!save_trace(&root
->trace
))
2080 print_shortest_lock_dependencies(other
, root
);
2082 printk("\nstack backtrace:\n");
2089 * Prove that in the forwards-direction subgraph starting at <this>
2090 * there is no lock matching <mask>:
2093 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2094 enum lock_usage_bit bit
, const char *irqclass
)
2097 struct lock_list root
;
2098 struct lock_list
*uninitialized_var(target_entry
);
2101 root
.class = hlock_class(this);
2102 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2104 return print_bfs_bug(ret
);
2108 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2113 * Prove that in the backwards-direction subgraph starting at <this>
2114 * there is no lock matching <mask>:
2117 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2118 enum lock_usage_bit bit
, const char *irqclass
)
2121 struct lock_list root
;
2122 struct lock_list
*uninitialized_var(target_entry
);
2125 root
.class = hlock_class(this);
2126 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2128 return print_bfs_bug(ret
);
2132 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2136 void print_irqtrace_events(struct task_struct
*curr
)
2138 printk("irq event stamp: %u\n", curr
->irq_events
);
2139 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
2140 print_ip_sym(curr
->hardirq_enable_ip
);
2141 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
2142 print_ip_sym(curr
->hardirq_disable_ip
);
2143 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
2144 print_ip_sym(curr
->softirq_enable_ip
);
2145 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
2146 print_ip_sym(curr
->softirq_disable_ip
);
2149 static int HARDIRQ_verbose(struct lock_class
*class)
2152 return class_filter(class);
2157 static int SOFTIRQ_verbose(struct lock_class
*class)
2160 return class_filter(class);
2165 static int RECLAIM_FS_verbose(struct lock_class
*class)
2168 return class_filter(class);
2173 #define STRICT_READ_CHECKS 1
2175 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2176 #define LOCKDEP_STATE(__STATE) \
2178 #include "lockdep_states.h"
2179 #undef LOCKDEP_STATE
2182 static inline int state_verbose(enum lock_usage_bit bit
,
2183 struct lock_class
*class)
2185 return state_verbose_f
[bit
>> 2](class);
2188 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2189 enum lock_usage_bit bit
, const char *name
);
2192 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2193 enum lock_usage_bit new_bit
)
2195 int excl_bit
= exclusive_bit(new_bit
);
2196 int read
= new_bit
& 1;
2197 int dir
= new_bit
& 2;
2200 * mark USED_IN has to look forwards -- to ensure no dependency
2201 * has ENABLED state, which would allow recursion deadlocks.
2203 * mark ENABLED has to look backwards -- to ensure no dependee
2204 * has USED_IN state, which, again, would allow recursion deadlocks.
2206 check_usage_f usage
= dir
?
2207 check_usage_backwards
: check_usage_forwards
;
2210 * Validate that this particular lock does not have conflicting
2213 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2217 * Validate that the lock dependencies don't have conflicting usage
2220 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2221 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2225 * Check for read in write conflicts
2228 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2231 if (STRICT_READ_CHECKS
&&
2232 !usage(curr
, this, excl_bit
+ 1,
2233 state_name(new_bit
+ 1)))
2237 if (state_verbose(new_bit
, hlock_class(this)))
2244 #define LOCKDEP_STATE(__STATE) __STATE,
2245 #include "lockdep_states.h"
2246 #undef LOCKDEP_STATE
2250 * Mark all held locks with a usage bit:
2253 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2255 enum lock_usage_bit usage_bit
;
2256 struct held_lock
*hlock
;
2259 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2260 hlock
= curr
->held_locks
+ i
;
2262 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2264 usage_bit
+= 1; /* READ */
2266 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2268 if (!mark_lock(curr
, hlock
, usage_bit
))
2276 * Debugging helper: via this flag we know that we are in
2277 * 'early bootup code', and will warn about any invalid irqs-on event:
2279 static int early_boot_irqs_enabled
;
2281 void early_boot_irqs_off(void)
2283 early_boot_irqs_enabled
= 0;
2286 void early_boot_irqs_on(void)
2288 early_boot_irqs_enabled
= 1;
2292 * Hardirqs will be enabled:
2294 void trace_hardirqs_on_caller(unsigned long ip
)
2296 struct task_struct
*curr
= current
;
2298 time_hardirqs_on(CALLER_ADDR0
, ip
);
2300 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2303 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2306 if (unlikely(curr
->hardirqs_enabled
)) {
2307 debug_atomic_inc(&redundant_hardirqs_on
);
2310 /* we'll do an OFF -> ON transition: */
2311 curr
->hardirqs_enabled
= 1;
2313 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2315 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2318 * We are going to turn hardirqs on, so set the
2319 * usage bit for all held locks:
2321 if (!mark_held_locks(curr
, HARDIRQ
))
2324 * If we have softirqs enabled, then set the usage
2325 * bit for all held locks. (disabled hardirqs prevented
2326 * this bit from being set before)
2328 if (curr
->softirqs_enabled
)
2329 if (!mark_held_locks(curr
, SOFTIRQ
))
2332 curr
->hardirq_enable_ip
= ip
;
2333 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2334 debug_atomic_inc(&hardirqs_on_events
);
2336 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2338 void trace_hardirqs_on(void)
2340 trace_hardirqs_on_caller(CALLER_ADDR0
);
2342 EXPORT_SYMBOL(trace_hardirqs_on
);
2345 * Hardirqs were disabled:
2347 void trace_hardirqs_off_caller(unsigned long ip
)
2349 struct task_struct
*curr
= current
;
2351 time_hardirqs_off(CALLER_ADDR0
, ip
);
2353 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2356 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2359 if (curr
->hardirqs_enabled
) {
2361 * We have done an ON -> OFF transition:
2363 curr
->hardirqs_enabled
= 0;
2364 curr
->hardirq_disable_ip
= ip
;
2365 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2366 debug_atomic_inc(&hardirqs_off_events
);
2368 debug_atomic_inc(&redundant_hardirqs_off
);
2370 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2372 void trace_hardirqs_off(void)
2374 trace_hardirqs_off_caller(CALLER_ADDR0
);
2376 EXPORT_SYMBOL(trace_hardirqs_off
);
2379 * Softirqs will be enabled:
2381 void trace_softirqs_on(unsigned long ip
)
2383 struct task_struct
*curr
= current
;
2385 if (unlikely(!debug_locks
))
2388 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2391 if (curr
->softirqs_enabled
) {
2392 debug_atomic_inc(&redundant_softirqs_on
);
2397 * We'll do an OFF -> ON transition:
2399 curr
->softirqs_enabled
= 1;
2400 curr
->softirq_enable_ip
= ip
;
2401 curr
->softirq_enable_event
= ++curr
->irq_events
;
2402 debug_atomic_inc(&softirqs_on_events
);
2404 * We are going to turn softirqs on, so set the
2405 * usage bit for all held locks, if hardirqs are
2408 if (curr
->hardirqs_enabled
)
2409 mark_held_locks(curr
, SOFTIRQ
);
2413 * Softirqs were disabled:
2415 void trace_softirqs_off(unsigned long ip
)
2417 struct task_struct
*curr
= current
;
2419 if (unlikely(!debug_locks
))
2422 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2425 if (curr
->softirqs_enabled
) {
2427 * We have done an ON -> OFF transition:
2429 curr
->softirqs_enabled
= 0;
2430 curr
->softirq_disable_ip
= ip
;
2431 curr
->softirq_disable_event
= ++curr
->irq_events
;
2432 debug_atomic_inc(&softirqs_off_events
);
2433 DEBUG_LOCKS_WARN_ON(!softirq_count());
2435 debug_atomic_inc(&redundant_softirqs_off
);
2438 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2440 struct task_struct
*curr
= current
;
2442 if (unlikely(!debug_locks
))
2445 /* no reclaim without waiting on it */
2446 if (!(gfp_mask
& __GFP_WAIT
))
2449 /* this guy won't enter reclaim */
2450 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2453 /* We're only interested __GFP_FS allocations for now */
2454 if (!(gfp_mask
& __GFP_FS
))
2457 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2460 mark_held_locks(curr
, RECLAIM_FS
);
2463 static void check_flags(unsigned long flags
);
2465 void lockdep_trace_alloc(gfp_t gfp_mask
)
2467 unsigned long flags
;
2469 if (unlikely(current
->lockdep_recursion
))
2472 raw_local_irq_save(flags
);
2474 current
->lockdep_recursion
= 1;
2475 __lockdep_trace_alloc(gfp_mask
, flags
);
2476 current
->lockdep_recursion
= 0;
2477 raw_local_irq_restore(flags
);
2480 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2483 * If non-trylock use in a hardirq or softirq context, then
2484 * mark the lock as used in these contexts:
2486 if (!hlock
->trylock
) {
2488 if (curr
->hardirq_context
)
2489 if (!mark_lock(curr
, hlock
,
2490 LOCK_USED_IN_HARDIRQ_READ
))
2492 if (curr
->softirq_context
)
2493 if (!mark_lock(curr
, hlock
,
2494 LOCK_USED_IN_SOFTIRQ_READ
))
2497 if (curr
->hardirq_context
)
2498 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2500 if (curr
->softirq_context
)
2501 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2505 if (!hlock
->hardirqs_off
) {
2507 if (!mark_lock(curr
, hlock
,
2508 LOCK_ENABLED_HARDIRQ_READ
))
2510 if (curr
->softirqs_enabled
)
2511 if (!mark_lock(curr
, hlock
,
2512 LOCK_ENABLED_SOFTIRQ_READ
))
2515 if (!mark_lock(curr
, hlock
,
2516 LOCK_ENABLED_HARDIRQ
))
2518 if (curr
->softirqs_enabled
)
2519 if (!mark_lock(curr
, hlock
,
2520 LOCK_ENABLED_SOFTIRQ
))
2526 * We reuse the irq context infrastructure more broadly as a general
2527 * context checking code. This tests GFP_FS recursion (a lock taken
2528 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2531 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2533 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2536 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2544 static int separate_irq_context(struct task_struct
*curr
,
2545 struct held_lock
*hlock
)
2547 unsigned int depth
= curr
->lockdep_depth
;
2550 * Keep track of points where we cross into an interrupt context:
2552 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2553 curr
->softirq_context
;
2555 struct held_lock
*prev_hlock
;
2557 prev_hlock
= curr
->held_locks
+ depth
-1;
2559 * If we cross into another context, reset the
2560 * hash key (this also prevents the checking and the
2561 * adding of the dependency to 'prev'):
2563 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2572 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2573 enum lock_usage_bit new_bit
)
2579 static inline int mark_irqflags(struct task_struct
*curr
,
2580 struct held_lock
*hlock
)
2585 static inline int separate_irq_context(struct task_struct
*curr
,
2586 struct held_lock
*hlock
)
2591 void lockdep_trace_alloc(gfp_t gfp_mask
)
2598 * Mark a lock with a usage bit, and validate the state transition:
2600 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2601 enum lock_usage_bit new_bit
)
2603 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2606 * If already set then do not dirty the cacheline,
2607 * nor do any checks:
2609 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2615 * Make sure we didnt race:
2617 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2622 hlock_class(this)->usage_mask
|= new_mask
;
2624 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2628 #define LOCKDEP_STATE(__STATE) \
2629 case LOCK_USED_IN_##__STATE: \
2630 case LOCK_USED_IN_##__STATE##_READ: \
2631 case LOCK_ENABLED_##__STATE: \
2632 case LOCK_ENABLED_##__STATE##_READ:
2633 #include "lockdep_states.h"
2634 #undef LOCKDEP_STATE
2635 ret
= mark_lock_irq(curr
, this, new_bit
);
2640 debug_atomic_dec(&nr_unused_locks
);
2643 if (!debug_locks_off_graph_unlock())
2652 * We must printk outside of the graph_lock:
2655 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2657 print_irqtrace_events(curr
);
2665 * Initialize a lock instance's lock-class mapping info:
2667 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2668 struct lock_class_key
*key
, int subclass
)
2670 lock
->class_cache
= NULL
;
2671 #ifdef CONFIG_LOCK_STAT
2672 lock
->cpu
= raw_smp_processor_id();
2675 if (DEBUG_LOCKS_WARN_ON(!name
)) {
2676 lock
->name
= "NULL";
2682 if (DEBUG_LOCKS_WARN_ON(!key
))
2685 * Sanity check, the lock-class key must be persistent:
2687 if (!static_obj(key
)) {
2688 printk("BUG: key %p not in .data!\n", key
);
2689 DEBUG_LOCKS_WARN_ON(1);
2694 if (unlikely(!debug_locks
))
2698 register_lock_class(lock
, subclass
, 1);
2700 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2703 * This gets called for every mutex_lock*()/spin_lock*() operation.
2704 * We maintain the dependency maps and validate the locking attempt:
2706 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2707 int trylock
, int read
, int check
, int hardirqs_off
,
2708 struct lockdep_map
*nest_lock
, unsigned long ip
,
2711 struct task_struct
*curr
= current
;
2712 struct lock_class
*class = NULL
;
2713 struct held_lock
*hlock
;
2714 unsigned int depth
, id
;
2722 if (unlikely(!debug_locks
))
2725 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2728 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2730 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2731 printk("turning off the locking correctness validator.\n");
2737 class = lock
->class_cache
;
2739 * Not cached yet or subclass?
2741 if (unlikely(!class)) {
2742 class = register_lock_class(lock
, subclass
, 0);
2746 debug_atomic_inc((atomic_t
*)&class->ops
);
2747 if (very_verbose(class)) {
2748 printk("\nacquire class [%p] %s", class->key
, class->name
);
2749 if (class->name_version
> 1)
2750 printk("#%d", class->name_version
);
2756 * Add the lock to the list of currently held locks.
2757 * (we dont increase the depth just yet, up until the
2758 * dependency checks are done)
2760 depth
= curr
->lockdep_depth
;
2761 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2764 class_idx
= class - lock_classes
+ 1;
2767 hlock
= curr
->held_locks
+ depth
- 1;
2768 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
2769 if (hlock
->references
)
2770 hlock
->references
++;
2772 hlock
->references
= 2;
2778 hlock
= curr
->held_locks
+ depth
;
2779 if (DEBUG_LOCKS_WARN_ON(!class))
2781 hlock
->class_idx
= class_idx
;
2782 hlock
->acquire_ip
= ip
;
2783 hlock
->instance
= lock
;
2784 hlock
->nest_lock
= nest_lock
;
2785 hlock
->trylock
= trylock
;
2787 hlock
->check
= check
;
2788 hlock
->hardirqs_off
= !!hardirqs_off
;
2789 hlock
->references
= references
;
2790 #ifdef CONFIG_LOCK_STAT
2791 hlock
->waittime_stamp
= 0;
2792 hlock
->holdtime_stamp
= sched_clock();
2795 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2798 /* mark it as used: */
2799 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2803 * Calculate the chain hash: it's the combined hash of all the
2804 * lock keys along the dependency chain. We save the hash value
2805 * at every step so that we can get the current hash easily
2806 * after unlock. The chain hash is then used to cache dependency
2809 * The 'key ID' is what is the most compact key value to drive
2810 * the hash, not class->key.
2812 id
= class - lock_classes
;
2813 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2816 chain_key
= curr
->curr_chain_key
;
2818 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2823 hlock
->prev_chain_key
= chain_key
;
2824 if (separate_irq_context(curr
, hlock
)) {
2828 chain_key
= iterate_chain_key(chain_key
, id
);
2830 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
2833 curr
->curr_chain_key
= chain_key
;
2834 curr
->lockdep_depth
++;
2835 check_chain_key(curr
);
2836 #ifdef CONFIG_DEBUG_LOCKDEP
2837 if (unlikely(!debug_locks
))
2840 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2842 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2843 printk("turning off the locking correctness validator.\n");
2848 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2849 max_lockdep_depth
= curr
->lockdep_depth
;
2855 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2858 if (!debug_locks_off())
2860 if (debug_locks_silent
)
2863 printk("\n=====================================\n");
2864 printk( "[ BUG: bad unlock balance detected! ]\n");
2865 printk( "-------------------------------------\n");
2866 printk("%s/%d is trying to release lock (",
2867 curr
->comm
, task_pid_nr(curr
));
2868 print_lockdep_cache(lock
);
2871 printk("but there are no more locks to release!\n");
2872 printk("\nother info that might help us debug this:\n");
2873 lockdep_print_held_locks(curr
);
2875 printk("\nstack backtrace:\n");
2882 * Common debugging checks for both nested and non-nested unlock:
2884 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2887 if (unlikely(!debug_locks
))
2889 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2892 if (curr
->lockdep_depth
<= 0)
2893 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2898 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
2900 if (hlock
->instance
== lock
)
2903 if (hlock
->references
) {
2904 struct lock_class
*class = lock
->class_cache
;
2907 class = look_up_lock_class(lock
, 0);
2909 if (DEBUG_LOCKS_WARN_ON(!class))
2912 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
2915 if (hlock
->class_idx
== class - lock_classes
+ 1)
2923 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
2924 struct lock_class_key
*key
, unsigned int subclass
,
2927 struct task_struct
*curr
= current
;
2928 struct held_lock
*hlock
, *prev_hlock
;
2929 struct lock_class
*class;
2933 depth
= curr
->lockdep_depth
;
2934 if (DEBUG_LOCKS_WARN_ON(!depth
))
2938 for (i
= depth
-1; i
>= 0; i
--) {
2939 hlock
= curr
->held_locks
+ i
;
2941 * We must not cross into another context:
2943 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2945 if (match_held_lock(hlock
, lock
))
2949 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2952 lockdep_init_map(lock
, name
, key
, 0);
2953 class = register_lock_class(lock
, subclass
, 0);
2954 hlock
->class_idx
= class - lock_classes
+ 1;
2956 curr
->lockdep_depth
= i
;
2957 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2959 for (; i
< depth
; i
++) {
2960 hlock
= curr
->held_locks
+ i
;
2961 if (!__lock_acquire(hlock
->instance
,
2962 hlock_class(hlock
)->subclass
, hlock
->trylock
,
2963 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2964 hlock
->nest_lock
, hlock
->acquire_ip
,
2969 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
2975 * Remove the lock to the list of currently held locks in a
2976 * potentially non-nested (out of order) manner. This is a
2977 * relatively rare operation, as all the unlock APIs default
2978 * to nested mode (which uses lock_release()):
2981 lock_release_non_nested(struct task_struct
*curr
,
2982 struct lockdep_map
*lock
, unsigned long ip
)
2984 struct held_lock
*hlock
, *prev_hlock
;
2989 * Check whether the lock exists in the current stack
2992 depth
= curr
->lockdep_depth
;
2993 if (DEBUG_LOCKS_WARN_ON(!depth
))
2997 for (i
= depth
-1; i
>= 0; i
--) {
2998 hlock
= curr
->held_locks
+ i
;
3000 * We must not cross into another context:
3002 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3004 if (match_held_lock(hlock
, lock
))
3008 return print_unlock_inbalance_bug(curr
, lock
, ip
);
3011 if (hlock
->instance
== lock
)
3012 lock_release_holdtime(hlock
);
3014 if (hlock
->references
) {
3015 hlock
->references
--;
3016 if (hlock
->references
) {
3018 * We had, and after removing one, still have
3019 * references, the current lock stack is still
3020 * valid. We're done!
3027 * We have the right lock to unlock, 'hlock' points to it.
3028 * Now we remove it from the stack, and add back the other
3029 * entries (if any), recalculating the hash along the way:
3032 curr
->lockdep_depth
= i
;
3033 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3035 for (i
++; i
< depth
; i
++) {
3036 hlock
= curr
->held_locks
+ i
;
3037 if (!__lock_acquire(hlock
->instance
,
3038 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3039 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3040 hlock
->nest_lock
, hlock
->acquire_ip
,
3045 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3051 * Remove the lock to the list of currently held locks - this gets
3052 * called on mutex_unlock()/spin_unlock*() (or on a failed
3053 * mutex_lock_interruptible()). This is done for unlocks that nest
3054 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3056 static int lock_release_nested(struct task_struct
*curr
,
3057 struct lockdep_map
*lock
, unsigned long ip
)
3059 struct held_lock
*hlock
;
3063 * Pop off the top of the lock stack:
3065 depth
= curr
->lockdep_depth
- 1;
3066 hlock
= curr
->held_locks
+ depth
;
3069 * Is the unlock non-nested:
3071 if (hlock
->instance
!= lock
|| hlock
->references
)
3072 return lock_release_non_nested(curr
, lock
, ip
);
3073 curr
->lockdep_depth
--;
3075 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
3078 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3080 lock_release_holdtime(hlock
);
3082 #ifdef CONFIG_DEBUG_LOCKDEP
3083 hlock
->prev_chain_key
= 0;
3084 hlock
->class_idx
= 0;
3085 hlock
->acquire_ip
= 0;
3086 hlock
->irq_context
= 0;
3092 * Remove the lock to the list of currently held locks - this gets
3093 * called on mutex_unlock()/spin_unlock*() (or on a failed
3094 * mutex_lock_interruptible()). This is done for unlocks that nest
3095 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3098 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3100 struct task_struct
*curr
= current
;
3102 if (!check_unlock(curr
, lock
, ip
))
3106 if (!lock_release_nested(curr
, lock
, ip
))
3109 if (!lock_release_non_nested(curr
, lock
, ip
))
3113 check_chain_key(curr
);
3116 static int __lock_is_held(struct lockdep_map
*lock
)
3118 struct task_struct
*curr
= current
;
3121 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3122 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3124 if (match_held_lock(hlock
, lock
))
3132 * Check whether we follow the irq-flags state precisely:
3134 static void check_flags(unsigned long flags
)
3136 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3137 defined(CONFIG_TRACE_IRQFLAGS)
3141 if (irqs_disabled_flags(flags
)) {
3142 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3143 printk("possible reason: unannotated irqs-off.\n");
3146 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3147 printk("possible reason: unannotated irqs-on.\n");
3152 * We dont accurately track softirq state in e.g.
3153 * hardirq contexts (such as on 4KSTACKS), so only
3154 * check if not in hardirq contexts:
3156 if (!hardirq_count()) {
3157 if (softirq_count())
3158 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3160 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3164 print_irqtrace_events(current
);
3168 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3169 struct lock_class_key
*key
, unsigned int subclass
,
3172 unsigned long flags
;
3174 if (unlikely(current
->lockdep_recursion
))
3177 raw_local_irq_save(flags
);
3178 current
->lockdep_recursion
= 1;
3180 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3181 check_chain_key(current
);
3182 current
->lockdep_recursion
= 0;
3183 raw_local_irq_restore(flags
);
3185 EXPORT_SYMBOL_GPL(lock_set_class
);
3188 * We are not always called with irqs disabled - do that here,
3189 * and also avoid lockdep recursion:
3191 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3192 int trylock
, int read
, int check
,
3193 struct lockdep_map
*nest_lock
, unsigned long ip
)
3195 unsigned long flags
;
3197 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3199 if (unlikely(current
->lockdep_recursion
))
3202 raw_local_irq_save(flags
);
3205 current
->lockdep_recursion
= 1;
3206 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3207 irqs_disabled_flags(flags
), nest_lock
, ip
, 0);
3208 current
->lockdep_recursion
= 0;
3209 raw_local_irq_restore(flags
);
3211 EXPORT_SYMBOL_GPL(lock_acquire
);
3213 void lock_release(struct lockdep_map
*lock
, int nested
,
3216 unsigned long flags
;
3218 trace_lock_release(lock
, nested
, ip
);
3220 if (unlikely(current
->lockdep_recursion
))
3223 raw_local_irq_save(flags
);
3225 current
->lockdep_recursion
= 1;
3226 __lock_release(lock
, nested
, ip
);
3227 current
->lockdep_recursion
= 0;
3228 raw_local_irq_restore(flags
);
3230 EXPORT_SYMBOL_GPL(lock_release
);
3232 int lock_is_held(struct lockdep_map
*lock
)
3234 unsigned long flags
;
3237 if (unlikely(current
->lockdep_recursion
))
3240 raw_local_irq_save(flags
);
3243 current
->lockdep_recursion
= 1;
3244 ret
= __lock_is_held(lock
);
3245 current
->lockdep_recursion
= 0;
3246 raw_local_irq_restore(flags
);
3250 EXPORT_SYMBOL_GPL(lock_is_held
);
3252 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3254 current
->lockdep_reclaim_gfp
= gfp_mask
;
3257 void lockdep_clear_current_reclaim_state(void)
3259 current
->lockdep_reclaim_gfp
= 0;
3262 #ifdef CONFIG_LOCK_STAT
3264 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3267 if (!debug_locks_off())
3269 if (debug_locks_silent
)
3272 printk("\n=================================\n");
3273 printk( "[ BUG: bad contention detected! ]\n");
3274 printk( "---------------------------------\n");
3275 printk("%s/%d is trying to contend lock (",
3276 curr
->comm
, task_pid_nr(curr
));
3277 print_lockdep_cache(lock
);
3280 printk("but there are no locks held!\n");
3281 printk("\nother info that might help us debug this:\n");
3282 lockdep_print_held_locks(curr
);
3284 printk("\nstack backtrace:\n");
3291 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3293 struct task_struct
*curr
= current
;
3294 struct held_lock
*hlock
, *prev_hlock
;
3295 struct lock_class_stats
*stats
;
3297 int i
, contention_point
, contending_point
;
3299 depth
= curr
->lockdep_depth
;
3300 if (DEBUG_LOCKS_WARN_ON(!depth
))
3304 for (i
= depth
-1; i
>= 0; i
--) {
3305 hlock
= curr
->held_locks
+ i
;
3307 * We must not cross into another context:
3309 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3311 if (match_held_lock(hlock
, lock
))
3315 print_lock_contention_bug(curr
, lock
, ip
);
3319 if (hlock
->instance
!= lock
)
3322 hlock
->waittime_stamp
= sched_clock();
3324 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3325 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3328 stats
= get_lock_stats(hlock_class(hlock
));
3329 if (contention_point
< LOCKSTAT_POINTS
)
3330 stats
->contention_point
[contention_point
]++;
3331 if (contending_point
< LOCKSTAT_POINTS
)
3332 stats
->contending_point
[contending_point
]++;
3333 if (lock
->cpu
!= smp_processor_id())
3334 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3335 put_lock_stats(stats
);
3339 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3341 struct task_struct
*curr
= current
;
3342 struct held_lock
*hlock
, *prev_hlock
;
3343 struct lock_class_stats
*stats
;
3349 depth
= curr
->lockdep_depth
;
3350 if (DEBUG_LOCKS_WARN_ON(!depth
))
3354 for (i
= depth
-1; i
>= 0; i
--) {
3355 hlock
= curr
->held_locks
+ i
;
3357 * We must not cross into another context:
3359 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3361 if (match_held_lock(hlock
, lock
))
3365 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3369 if (hlock
->instance
!= lock
)
3372 cpu
= smp_processor_id();
3373 if (hlock
->waittime_stamp
) {
3374 now
= sched_clock();
3375 waittime
= now
- hlock
->waittime_stamp
;
3376 hlock
->holdtime_stamp
= now
;
3379 trace_lock_acquired(lock
, ip
, waittime
);
3381 stats
= get_lock_stats(hlock_class(hlock
));
3384 lock_time_inc(&stats
->read_waittime
, waittime
);
3386 lock_time_inc(&stats
->write_waittime
, waittime
);
3388 if (lock
->cpu
!= cpu
)
3389 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3390 put_lock_stats(stats
);
3396 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3398 unsigned long flags
;
3400 trace_lock_contended(lock
, ip
);
3402 if (unlikely(!lock_stat
))
3405 if (unlikely(current
->lockdep_recursion
))
3408 raw_local_irq_save(flags
);
3410 current
->lockdep_recursion
= 1;
3411 __lock_contended(lock
, ip
);
3412 current
->lockdep_recursion
= 0;
3413 raw_local_irq_restore(flags
);
3415 EXPORT_SYMBOL_GPL(lock_contended
);
3417 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3419 unsigned long flags
;
3421 if (unlikely(!lock_stat
))
3424 if (unlikely(current
->lockdep_recursion
))
3427 raw_local_irq_save(flags
);
3429 current
->lockdep_recursion
= 1;
3430 __lock_acquired(lock
, ip
);
3431 current
->lockdep_recursion
= 0;
3432 raw_local_irq_restore(flags
);
3434 EXPORT_SYMBOL_GPL(lock_acquired
);
3438 * Used by the testsuite, sanitize the validator state
3439 * after a simulated failure:
3442 void lockdep_reset(void)
3444 unsigned long flags
;
3447 raw_local_irq_save(flags
);
3448 current
->curr_chain_key
= 0;
3449 current
->lockdep_depth
= 0;
3450 current
->lockdep_recursion
= 0;
3451 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3452 nr_hardirq_chains
= 0;
3453 nr_softirq_chains
= 0;
3454 nr_process_chains
= 0;
3456 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3457 INIT_LIST_HEAD(chainhash_table
+ i
);
3458 raw_local_irq_restore(flags
);
3461 static void zap_class(struct lock_class
*class)
3466 * Remove all dependencies this lock is
3469 for (i
= 0; i
< nr_list_entries
; i
++) {
3470 if (list_entries
[i
].class == class)
3471 list_del_rcu(&list_entries
[i
].entry
);
3474 * Unhash the class and remove it from the all_lock_classes list:
3476 list_del_rcu(&class->hash_entry
);
3477 list_del_rcu(&class->lock_entry
);
3482 static inline int within(const void *addr
, void *start
, unsigned long size
)
3484 return addr
>= start
&& addr
< start
+ size
;
3487 void lockdep_free_key_range(void *start
, unsigned long size
)
3489 struct lock_class
*class, *next
;
3490 struct list_head
*head
;
3491 unsigned long flags
;
3495 raw_local_irq_save(flags
);
3496 locked
= graph_lock();
3499 * Unhash all classes that were created by this module:
3501 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3502 head
= classhash_table
+ i
;
3503 if (list_empty(head
))
3505 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3506 if (within(class->key
, start
, size
))
3508 else if (within(class->name
, start
, size
))
3515 raw_local_irq_restore(flags
);
3518 void lockdep_reset_lock(struct lockdep_map
*lock
)
3520 struct lock_class
*class, *next
;
3521 struct list_head
*head
;
3522 unsigned long flags
;
3526 raw_local_irq_save(flags
);
3529 * Remove all classes this lock might have:
3531 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3533 * If the class exists we look it up and zap it:
3535 class = look_up_lock_class(lock
, j
);
3540 * Debug check: in the end all mapped classes should
3543 locked
= graph_lock();
3544 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3545 head
= classhash_table
+ i
;
3546 if (list_empty(head
))
3548 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3549 if (unlikely(class == lock
->class_cache
)) {
3550 if (debug_locks_off_graph_unlock())
3560 raw_local_irq_restore(flags
);
3563 void lockdep_init(void)
3568 * Some architectures have their own start_kernel()
3569 * code which calls lockdep_init(), while we also
3570 * call lockdep_init() from the start_kernel() itself,
3571 * and we want to initialize the hashes only once:
3573 if (lockdep_initialized
)
3576 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3577 INIT_LIST_HEAD(classhash_table
+ i
);
3579 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3580 INIT_LIST_HEAD(chainhash_table
+ i
);
3582 lockdep_initialized
= 1;
3585 void __init
lockdep_info(void)
3587 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3589 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3590 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3591 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3592 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3593 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3594 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3595 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3597 printk(" memory used by lock dependency info: %lu kB\n",
3598 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3599 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3600 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3601 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3602 sizeof(struct list_head
) * CHAINHASH_SIZE
3603 #ifdef CONFIG_PROVE_LOCKING
3604 + sizeof(struct circular_queue
)
3609 printk(" per task-struct memory footprint: %lu bytes\n",
3610 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3612 #ifdef CONFIG_DEBUG_LOCKDEP
3613 if (lockdep_init_error
) {
3614 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3615 printk("Call stack leading to lockdep invocation was:\n");
3616 print_stack_trace(&lockdep_init_trace
, 0);
3622 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3623 const void *mem_to
, struct held_lock
*hlock
)
3625 if (!debug_locks_off())
3627 if (debug_locks_silent
)
3630 printk("\n=========================\n");
3631 printk( "[ BUG: held lock freed! ]\n");
3632 printk( "-------------------------\n");
3633 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3634 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3636 lockdep_print_held_locks(curr
);
3638 printk("\nstack backtrace:\n");
3642 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3643 const void* lock_from
, unsigned long lock_len
)
3645 return lock_from
+ lock_len
<= mem_from
||
3646 mem_from
+ mem_len
<= lock_from
;
3650 * Called when kernel memory is freed (or unmapped), or if a lock
3651 * is destroyed or reinitialized - this code checks whether there is
3652 * any held lock in the memory range of <from> to <to>:
3654 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3656 struct task_struct
*curr
= current
;
3657 struct held_lock
*hlock
;
3658 unsigned long flags
;
3661 if (unlikely(!debug_locks
))
3664 local_irq_save(flags
);
3665 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3666 hlock
= curr
->held_locks
+ i
;
3668 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3669 sizeof(*hlock
->instance
)))
3672 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3675 local_irq_restore(flags
);
3677 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3679 static void print_held_locks_bug(struct task_struct
*curr
)
3681 if (!debug_locks_off())
3683 if (debug_locks_silent
)
3686 printk("\n=====================================\n");
3687 printk( "[ BUG: lock held at task exit time! ]\n");
3688 printk( "-------------------------------------\n");
3689 printk("%s/%d is exiting with locks still held!\n",
3690 curr
->comm
, task_pid_nr(curr
));
3691 lockdep_print_held_locks(curr
);
3693 printk("\nstack backtrace:\n");
3697 void debug_check_no_locks_held(struct task_struct
*task
)
3699 if (unlikely(task
->lockdep_depth
> 0))
3700 print_held_locks_bug(task
);
3703 void debug_show_all_locks(void)
3705 struct task_struct
*g
, *p
;
3709 if (unlikely(!debug_locks
)) {
3710 printk("INFO: lockdep is turned off.\n");
3713 printk("\nShowing all locks held in the system:\n");
3716 * Here we try to get the tasklist_lock as hard as possible,
3717 * if not successful after 2 seconds we ignore it (but keep
3718 * trying). This is to enable a debug printout even if a
3719 * tasklist_lock-holding task deadlocks or crashes.
3722 if (!read_trylock(&tasklist_lock
)) {
3724 printk("hm, tasklist_lock locked, retrying... ");
3727 printk(" #%d", 10-count
);
3731 printk(" ignoring it.\n");
3735 printk(KERN_CONT
" locked it.\n");
3738 do_each_thread(g
, p
) {
3740 * It's not reliable to print a task's held locks
3741 * if it's not sleeping (or if it's not the current
3744 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3746 if (p
->lockdep_depth
)
3747 lockdep_print_held_locks(p
);
3749 if (read_trylock(&tasklist_lock
))
3751 } while_each_thread(g
, p
);
3754 printk("=============================================\n\n");
3757 read_unlock(&tasklist_lock
);
3759 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3762 * Careful: only use this function if you are sure that
3763 * the task cannot run in parallel!
3765 void __debug_show_held_locks(struct task_struct
*task
)
3767 if (unlikely(!debug_locks
)) {
3768 printk("INFO: lockdep is turned off.\n");
3771 lockdep_print_held_locks(task
);
3773 EXPORT_SYMBOL_GPL(__debug_show_held_locks
);
3775 void debug_show_held_locks(struct task_struct
*task
)
3777 __debug_show_held_locks(task
);
3779 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3781 void lockdep_sys_exit(void)
3783 struct task_struct
*curr
= current
;
3785 if (unlikely(curr
->lockdep_depth
)) {
3786 if (!debug_locks_off())
3788 printk("\n================================================\n");
3789 printk( "[ BUG: lock held when returning to user space! ]\n");
3790 printk( "------------------------------------------------\n");
3791 printk("%s/%d is leaving the kernel with locks still held!\n",
3792 curr
->comm
, curr
->pid
);
3793 lockdep_print_held_locks(curr
);