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 arch_spinlock_t lockdep_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
78 static int graph_lock(void)
80 arch_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 arch_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
&& !arch_spin_is_locked(&lockdep_lock
))
99 return DEBUG_LOCKS_WARN_ON(1);
101 current
->lockdep_recursion
--;
102 arch_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 arch_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
],
146 static inline u64
lockstat_clock(void)
148 return cpu_clock(smp_processor_id());
151 static int lock_point(unsigned long points
[], unsigned long ip
)
155 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
156 if (points
[i
] == 0) {
167 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
172 if (time
< lt
->min
|| !lt
->nr
)
179 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
184 if (src
->max
> dst
->max
)
187 if (src
->min
< dst
->min
|| !dst
->nr
)
190 dst
->total
+= src
->total
;
194 struct lock_class_stats
lock_stats(struct lock_class
*class)
196 struct lock_class_stats stats
;
199 memset(&stats
, 0, sizeof(struct lock_class_stats
));
200 for_each_possible_cpu(cpu
) {
201 struct lock_class_stats
*pcs
=
202 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
204 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
205 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
207 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
208 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
210 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
211 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
213 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
214 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
216 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
217 stats
.bounces
[i
] += pcs
->bounces
[i
];
223 void clear_lock_stats(struct lock_class
*class)
227 for_each_possible_cpu(cpu
) {
228 struct lock_class_stats
*cpu_stats
=
229 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
231 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
233 memset(class->contention_point
, 0, sizeof(class->contention_point
));
234 memset(class->contending_point
, 0, sizeof(class->contending_point
));
237 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
239 return &get_cpu_var(cpu_lock_stats
)[class - lock_classes
];
242 static void put_lock_stats(struct lock_class_stats
*stats
)
244 put_cpu_var(cpu_lock_stats
);
247 static void lock_release_holdtime(struct held_lock
*hlock
)
249 struct lock_class_stats
*stats
;
255 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
257 stats
= get_lock_stats(hlock_class(hlock
));
259 lock_time_inc(&stats
->read_holdtime
, holdtime
);
261 lock_time_inc(&stats
->write_holdtime
, holdtime
);
262 put_lock_stats(stats
);
265 static inline void lock_release_holdtime(struct held_lock
*hlock
)
271 * We keep a global list of all lock classes. The list only grows,
272 * never shrinks. The list is only accessed with the lockdep
273 * spinlock lock held.
275 LIST_HEAD(all_lock_classes
);
278 * The lockdep classes are in a hash-table as well, for fast lookup:
280 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
281 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
282 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
283 #define classhashentry(key) (classhash_table + __classhashfn((key)))
285 static struct list_head classhash_table
[CLASSHASH_SIZE
];
288 * We put the lock dependency chains into a hash-table as well, to cache
291 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
292 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
293 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
294 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
296 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
299 * The hash key of the lock dependency chains is a hash itself too:
300 * it's a hash of all locks taken up to that lock, including that lock.
301 * It's a 64-bit hash, because it's important for the keys to be
304 #define iterate_chain_key(key1, key2) \
305 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
306 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
309 void lockdep_off(void)
311 current
->lockdep_recursion
++;
313 EXPORT_SYMBOL(lockdep_off
);
315 void lockdep_on(void)
317 current
->lockdep_recursion
--;
319 EXPORT_SYMBOL(lockdep_on
);
322 * Debugging switches:
326 #define VERY_VERBOSE 0
329 # define HARDIRQ_VERBOSE 1
330 # define SOFTIRQ_VERBOSE 1
331 # define RECLAIM_VERBOSE 1
333 # define HARDIRQ_VERBOSE 0
334 # define SOFTIRQ_VERBOSE 0
335 # define RECLAIM_VERBOSE 0
338 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
340 * Quick filtering for interesting events:
342 static int class_filter(struct lock_class
*class)
346 if (class->name_version
== 1 &&
347 !strcmp(class->name
, "lockname"))
349 if (class->name_version
== 1 &&
350 !strcmp(class->name
, "&struct->lockfield"))
353 /* Filter everything else. 1 would be to allow everything else */
358 static int verbose(struct lock_class
*class)
361 return class_filter(class);
367 * Stack-trace: tightly packed array of stack backtrace
368 * addresses. Protected by the graph_lock.
370 unsigned long nr_stack_trace_entries
;
371 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
373 static int save_trace(struct stack_trace
*trace
)
375 trace
->nr_entries
= 0;
376 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
377 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
381 save_stack_trace(trace
);
384 * Some daft arches put -1 at the end to indicate its a full trace.
386 * <rant> this is buggy anyway, since it takes a whole extra entry so a
387 * complete trace that maxes out the entries provided will be reported
388 * as incomplete, friggin useless </rant>
390 if (trace
->nr_entries
!= 0 &&
391 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
394 trace
->max_entries
= trace
->nr_entries
;
396 nr_stack_trace_entries
+= trace
->nr_entries
;
398 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
399 if (!debug_locks_off_graph_unlock())
402 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
403 printk("turning off the locking correctness validator.\n");
412 unsigned int nr_hardirq_chains
;
413 unsigned int nr_softirq_chains
;
414 unsigned int nr_process_chains
;
415 unsigned int max_lockdep_depth
;
417 #ifdef CONFIG_DEBUG_LOCKDEP
419 * We cannot printk in early bootup code. Not even early_printk()
420 * might work. So we mark any initialization errors and printk
421 * about it later on, in lockdep_info().
423 static int lockdep_init_error
;
424 static unsigned long lockdep_init_trace_data
[20];
425 static struct stack_trace lockdep_init_trace
= {
426 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
427 .entries
= lockdep_init_trace_data
,
431 * Various lockdep statistics:
433 atomic_t chain_lookup_hits
;
434 atomic_t chain_lookup_misses
;
435 atomic_t hardirqs_on_events
;
436 atomic_t hardirqs_off_events
;
437 atomic_t redundant_hardirqs_on
;
438 atomic_t redundant_hardirqs_off
;
439 atomic_t softirqs_on_events
;
440 atomic_t softirqs_off_events
;
441 atomic_t redundant_softirqs_on
;
442 atomic_t redundant_softirqs_off
;
443 atomic_t nr_unused_locks
;
444 atomic_t nr_cyclic_checks
;
445 atomic_t nr_find_usage_forwards_checks
;
446 atomic_t nr_find_usage_backwards_checks
;
453 #define __USAGE(__STATE) \
454 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
455 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
456 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
457 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
459 static const char *usage_str
[] =
461 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
462 #include "lockdep_states.h"
464 [LOCK_USED
] = "INITIAL USE",
467 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
469 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
472 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
477 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
481 if (class->usage_mask
& lock_flag(bit
+ 2))
483 if (class->usage_mask
& lock_flag(bit
)) {
485 if (class->usage_mask
& lock_flag(bit
+ 2))
492 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
496 #define LOCKDEP_STATE(__STATE) \
497 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
498 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
499 #include "lockdep_states.h"
505 static void print_lock_name(struct lock_class
*class)
507 char str
[KSYM_NAME_LEN
], usage
[LOCK_USAGE_CHARS
];
510 get_usage_chars(class, usage
);
514 name
= __get_key_name(class->key
, str
);
515 printk(" (%s", name
);
517 printk(" (%s", name
);
518 if (class->name_version
> 1)
519 printk("#%d", class->name_version
);
521 printk("/%d", class->subclass
);
523 printk("){%s}", usage
);
526 static void print_lockdep_cache(struct lockdep_map
*lock
)
529 char str
[KSYM_NAME_LEN
];
533 name
= __get_key_name(lock
->key
->subkeys
, str
);
538 static void print_lock(struct held_lock
*hlock
)
540 print_lock_name(hlock_class(hlock
));
542 print_ip_sym(hlock
->acquire_ip
);
545 static void lockdep_print_held_locks(struct task_struct
*curr
)
547 int i
, depth
= curr
->lockdep_depth
;
550 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
553 printk("%d lock%s held by %s/%d:\n",
554 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
556 for (i
= 0; i
< depth
; i
++) {
558 print_lock(curr
->held_locks
+ i
);
562 static void print_kernel_version(void)
564 printk("%s %.*s\n", init_utsname()->release
,
565 (int)strcspn(init_utsname()->version
, " "),
566 init_utsname()->version
);
569 static int very_verbose(struct lock_class
*class)
572 return class_filter(class);
578 * Is this the address of a static object:
580 static int static_obj(void *obj
)
582 unsigned long start
= (unsigned long) &_stext
,
583 end
= (unsigned long) &_end
,
584 addr
= (unsigned long) obj
;
589 if ((addr
>= start
) && (addr
< end
))
592 if (arch_is_kernel_data(addr
))
596 * in-kernel percpu var?
598 if (is_kernel_percpu_address(addr
))
602 * module static or percpu var?
604 return is_module_address(addr
) || is_module_percpu_address(addr
);
608 * To make lock name printouts unique, we calculate a unique
609 * class->name_version generation counter:
611 static int count_matching_names(struct lock_class
*new_class
)
613 struct lock_class
*class;
616 if (!new_class
->name
)
619 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
620 if (new_class
->key
- new_class
->subclass
== class->key
)
621 return class->name_version
;
622 if (class->name
&& !strcmp(class->name
, new_class
->name
))
623 count
= max(count
, class->name_version
);
630 * Register a lock's class in the hash-table, if the class is not present
631 * yet. Otherwise we look it up. We cache the result in the lock object
632 * itself, so actual lookup of the hash should be once per lock object.
634 static inline struct lock_class
*
635 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
637 struct lockdep_subclass_key
*key
;
638 struct list_head
*hash_head
;
639 struct lock_class
*class;
641 #ifdef CONFIG_DEBUG_LOCKDEP
643 * If the architecture calls into lockdep before initializing
644 * the hashes then we'll warn about it later. (we cannot printk
647 if (unlikely(!lockdep_initialized
)) {
649 lockdep_init_error
= 1;
650 save_stack_trace(&lockdep_init_trace
);
655 * Static locks do not have their class-keys yet - for them the key
656 * is the lock object itself:
658 if (unlikely(!lock
->key
))
659 lock
->key
= (void *)lock
;
662 * NOTE: the class-key must be unique. For dynamic locks, a static
663 * lock_class_key variable is passed in through the mutex_init()
664 * (or spin_lock_init()) call - which acts as the key. For static
665 * locks we use the lock object itself as the key.
667 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
668 sizeof(struct lockdep_map
));
670 key
= lock
->key
->subkeys
+ subclass
;
672 hash_head
= classhashentry(key
);
675 * We can walk the hash lockfree, because the hash only
676 * grows, and we are careful when adding entries to the end:
678 list_for_each_entry(class, hash_head
, hash_entry
) {
679 if (class->key
== key
) {
680 WARN_ON_ONCE(class->name
!= lock
->name
);
689 * Register a lock's class in the hash-table, if the class is not present
690 * yet. Otherwise we look it up. We cache the result in the lock object
691 * itself, so actual lookup of the hash should be once per lock object.
693 static inline struct lock_class
*
694 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
696 struct lockdep_subclass_key
*key
;
697 struct list_head
*hash_head
;
698 struct lock_class
*class;
701 class = look_up_lock_class(lock
, subclass
);
706 * Debug-check: all keys must be persistent!
708 if (!static_obj(lock
->key
)) {
710 printk("INFO: trying to register non-static key.\n");
711 printk("the code is fine but needs lockdep annotation.\n");
712 printk("turning off the locking correctness validator.\n");
718 key
= lock
->key
->subkeys
+ subclass
;
719 hash_head
= classhashentry(key
);
721 raw_local_irq_save(flags
);
723 raw_local_irq_restore(flags
);
727 * We have to do the hash-walk again, to avoid races
730 list_for_each_entry(class, hash_head
, hash_entry
)
731 if (class->key
== key
)
734 * Allocate a new key from the static array, and add it to
737 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
738 if (!debug_locks_off_graph_unlock()) {
739 raw_local_irq_restore(flags
);
742 raw_local_irq_restore(flags
);
744 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
745 printk("turning off the locking correctness validator.\n");
749 class = lock_classes
+ nr_lock_classes
++;
750 debug_atomic_inc(&nr_unused_locks
);
752 class->name
= lock
->name
;
753 class->subclass
= subclass
;
754 INIT_LIST_HEAD(&class->lock_entry
);
755 INIT_LIST_HEAD(&class->locks_before
);
756 INIT_LIST_HEAD(&class->locks_after
);
757 class->name_version
= count_matching_names(class);
759 * We use RCU's safe list-add method to make
760 * parallel walking of the hash-list safe:
762 list_add_tail_rcu(&class->hash_entry
, hash_head
);
764 * Add it to the global list of classes:
766 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
768 if (verbose(class)) {
770 raw_local_irq_restore(flags
);
772 printk("\nnew class %p: %s", class->key
, class->name
);
773 if (class->name_version
> 1)
774 printk("#%d", class->name_version
);
778 raw_local_irq_save(flags
);
780 raw_local_irq_restore(flags
);
786 raw_local_irq_restore(flags
);
788 if (!subclass
|| force
)
789 lock
->class_cache
= class;
791 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
797 #ifdef CONFIG_PROVE_LOCKING
799 * Allocate a lockdep entry. (assumes the graph_lock held, returns
800 * with NULL on failure)
802 static struct lock_list
*alloc_list_entry(void)
804 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
805 if (!debug_locks_off_graph_unlock())
808 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
809 printk("turning off the locking correctness validator.\n");
813 return list_entries
+ nr_list_entries
++;
817 * Add a new dependency to the head of the list:
819 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
820 struct list_head
*head
, unsigned long ip
, int distance
)
822 struct lock_list
*entry
;
824 * Lock not present yet - get a new dependency struct and
825 * add it to the list:
827 entry
= alloc_list_entry();
831 if (!save_trace(&entry
->trace
))
835 entry
->distance
= distance
;
837 * Since we never remove from the dependency list, the list can
838 * be walked lockless by other CPUs, it's only allocation
839 * that must be protected by the spinlock. But this also means
840 * we must make new entries visible only once writes to the
841 * entry become visible - hence the RCU op:
843 list_add_tail_rcu(&entry
->entry
, head
);
849 * For good efficiency of modular, we use power of 2
851 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
852 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
855 * The circular_queue and helpers is used to implement the
856 * breadth-first search(BFS)algorithem, by which we can build
857 * the shortest path from the next lock to be acquired to the
858 * previous held lock if there is a circular between them.
860 struct circular_queue
{
861 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
862 unsigned int front
, rear
;
865 static struct circular_queue lock_cq
;
867 unsigned int max_bfs_queue_depth
;
869 static unsigned int lockdep_dependency_gen_id
;
871 static inline void __cq_init(struct circular_queue
*cq
)
873 cq
->front
= cq
->rear
= 0;
874 lockdep_dependency_gen_id
++;
877 static inline int __cq_empty(struct circular_queue
*cq
)
879 return (cq
->front
== cq
->rear
);
882 static inline int __cq_full(struct circular_queue
*cq
)
884 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
887 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
892 cq
->element
[cq
->rear
] = elem
;
893 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
897 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
902 *elem
= cq
->element
[cq
->front
];
903 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
907 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
909 return (cq
->rear
- cq
->front
) & CQ_MASK
;
912 static inline void mark_lock_accessed(struct lock_list
*lock
,
913 struct lock_list
*parent
)
917 nr
= lock
- list_entries
;
918 WARN_ON(nr
>= nr_list_entries
);
919 lock
->parent
= parent
;
920 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
923 static inline unsigned long lock_accessed(struct lock_list
*lock
)
927 nr
= lock
- list_entries
;
928 WARN_ON(nr
>= nr_list_entries
);
929 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
932 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
934 return child
->parent
;
937 static inline int get_lock_depth(struct lock_list
*child
)
940 struct lock_list
*parent
;
942 while ((parent
= get_lock_parent(child
))) {
949 static int __bfs(struct lock_list
*source_entry
,
951 int (*match
)(struct lock_list
*entry
, void *data
),
952 struct lock_list
**target_entry
,
955 struct lock_list
*entry
;
956 struct list_head
*head
;
957 struct circular_queue
*cq
= &lock_cq
;
960 if (match(source_entry
, data
)) {
961 *target_entry
= source_entry
;
967 head
= &source_entry
->class->locks_after
;
969 head
= &source_entry
->class->locks_before
;
971 if (list_empty(head
))
975 __cq_enqueue(cq
, (unsigned long)source_entry
);
977 while (!__cq_empty(cq
)) {
978 struct lock_list
*lock
;
980 __cq_dequeue(cq
, (unsigned long *)&lock
);
988 head
= &lock
->class->locks_after
;
990 head
= &lock
->class->locks_before
;
992 list_for_each_entry(entry
, head
, entry
) {
993 if (!lock_accessed(entry
)) {
994 unsigned int cq_depth
;
995 mark_lock_accessed(entry
, lock
);
996 if (match(entry
, data
)) {
997 *target_entry
= entry
;
1002 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1006 cq_depth
= __cq_get_elem_count(cq
);
1007 if (max_bfs_queue_depth
< cq_depth
)
1008 max_bfs_queue_depth
= cq_depth
;
1016 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1018 int (*match
)(struct lock_list
*entry
, void *data
),
1019 struct lock_list
**target_entry
)
1021 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1025 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1027 int (*match
)(struct lock_list
*entry
, void *data
),
1028 struct lock_list
**target_entry
)
1030 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1035 * Recursive, forwards-direction lock-dependency checking, used for
1036 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1041 * Print a dependency chain entry (this is only done when a deadlock
1042 * has been detected):
1045 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1047 if (debug_locks_silent
)
1049 printk("\n-> #%u", depth
);
1050 print_lock_name(target
->class);
1052 print_stack_trace(&target
->trace
, 6);
1058 * When a circular dependency is detected, print the
1062 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1063 struct held_lock
*check_src
,
1064 struct held_lock
*check_tgt
)
1066 struct task_struct
*curr
= current
;
1068 if (debug_locks_silent
)
1071 printk("\n=======================================================\n");
1072 printk( "[ INFO: possible circular locking dependency detected ]\n");
1073 print_kernel_version();
1074 printk( "-------------------------------------------------------\n");
1075 printk("%s/%d is trying to acquire lock:\n",
1076 curr
->comm
, task_pid_nr(curr
));
1077 print_lock(check_src
);
1078 printk("\nbut task is already holding lock:\n");
1079 print_lock(check_tgt
);
1080 printk("\nwhich lock already depends on the new lock.\n\n");
1081 printk("\nthe existing dependency chain (in reverse order) is:\n");
1083 print_circular_bug_entry(entry
, depth
);
1088 static inline int class_equal(struct lock_list
*entry
, void *data
)
1090 return entry
->class == data
;
1093 static noinline
int print_circular_bug(struct lock_list
*this,
1094 struct lock_list
*target
,
1095 struct held_lock
*check_src
,
1096 struct held_lock
*check_tgt
)
1098 struct task_struct
*curr
= current
;
1099 struct lock_list
*parent
;
1102 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1105 if (!save_trace(&this->trace
))
1108 depth
= get_lock_depth(target
);
1110 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1112 parent
= get_lock_parent(target
);
1115 print_circular_bug_entry(parent
, --depth
);
1116 parent
= get_lock_parent(parent
);
1119 printk("\nother info that might help us debug this:\n\n");
1120 lockdep_print_held_locks(curr
);
1122 printk("\nstack backtrace:\n");
1128 static noinline
int print_bfs_bug(int ret
)
1130 if (!debug_locks_off_graph_unlock())
1133 WARN(1, "lockdep bfs error:%d\n", ret
);
1138 static int noop_count(struct lock_list
*entry
, void *data
)
1140 (*(unsigned long *)data
)++;
1144 unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1146 unsigned long count
= 0;
1147 struct lock_list
*uninitialized_var(target_entry
);
1149 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1153 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1155 unsigned long ret
, flags
;
1156 struct lock_list
this;
1161 local_irq_save(flags
);
1162 arch_spin_lock(&lockdep_lock
);
1163 ret
= __lockdep_count_forward_deps(&this);
1164 arch_spin_unlock(&lockdep_lock
);
1165 local_irq_restore(flags
);
1170 unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1172 unsigned long count
= 0;
1173 struct lock_list
*uninitialized_var(target_entry
);
1175 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1180 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1182 unsigned long ret
, flags
;
1183 struct lock_list
this;
1188 local_irq_save(flags
);
1189 arch_spin_lock(&lockdep_lock
);
1190 ret
= __lockdep_count_backward_deps(&this);
1191 arch_spin_unlock(&lockdep_lock
);
1192 local_irq_restore(flags
);
1198 * Prove that the dependency graph starting at <entry> can not
1199 * lead to <target>. Print an error and return 0 if it does.
1202 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1203 struct lock_list
**target_entry
)
1207 debug_atomic_inc(&nr_cyclic_checks
);
1209 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1214 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1216 * Forwards and backwards subgraph searching, for the purposes of
1217 * proving that two subgraphs can be connected by a new dependency
1218 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1221 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1223 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1229 * Find a node in the forwards-direction dependency sub-graph starting
1230 * at @root->class that matches @bit.
1232 * Return 0 if such a node exists in the subgraph, and put that node
1233 * into *@target_entry.
1235 * Return 1 otherwise and keep *@target_entry unchanged.
1236 * Return <0 on error.
1239 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1240 struct lock_list
**target_entry
)
1244 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1246 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1252 * Find a node in the backwards-direction dependency sub-graph starting
1253 * at @root->class that matches @bit.
1255 * Return 0 if such a node exists in the subgraph, and put that node
1256 * into *@target_entry.
1258 * Return 1 otherwise and keep *@target_entry unchanged.
1259 * Return <0 on error.
1262 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1263 struct lock_list
**target_entry
)
1267 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1269 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1274 static void print_lock_class_header(struct lock_class
*class, int depth
)
1278 printk("%*s->", depth
, "");
1279 print_lock_name(class);
1280 printk(" ops: %lu", class->ops
);
1283 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1284 if (class->usage_mask
& (1 << bit
)) {
1287 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1288 len
+= printk(" at:\n");
1289 print_stack_trace(class->usage_traces
+ bit
, len
);
1292 printk("%*s }\n", depth
, "");
1294 printk("%*s ... key at: ",depth
,"");
1295 print_ip_sym((unsigned long)class->key
);
1299 * printk the shortest lock dependencies from @start to @end in reverse order:
1302 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1303 struct lock_list
*root
)
1305 struct lock_list
*entry
= leaf
;
1308 /*compute depth from generated tree by BFS*/
1309 depth
= get_lock_depth(leaf
);
1312 print_lock_class_header(entry
->class, depth
);
1313 printk("%*s ... acquired at:\n", depth
, "");
1314 print_stack_trace(&entry
->trace
, 2);
1317 if (depth
== 0 && (entry
!= root
)) {
1318 printk("lockdep:%s bad BFS generated tree\n", __func__
);
1322 entry
= get_lock_parent(entry
);
1324 } while (entry
&& (depth
>= 0));
1330 print_bad_irq_dependency(struct task_struct
*curr
,
1331 struct lock_list
*prev_root
,
1332 struct lock_list
*next_root
,
1333 struct lock_list
*backwards_entry
,
1334 struct lock_list
*forwards_entry
,
1335 struct held_lock
*prev
,
1336 struct held_lock
*next
,
1337 enum lock_usage_bit bit1
,
1338 enum lock_usage_bit bit2
,
1339 const char *irqclass
)
1341 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1344 printk("\n======================================================\n");
1345 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1346 irqclass
, irqclass
);
1347 print_kernel_version();
1348 printk( "------------------------------------------------------\n");
1349 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1350 curr
->comm
, task_pid_nr(curr
),
1351 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1352 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1353 curr
->hardirqs_enabled
,
1354 curr
->softirqs_enabled
);
1357 printk("\nand this task is already holding:\n");
1359 printk("which would create a new lock dependency:\n");
1360 print_lock_name(hlock_class(prev
));
1362 print_lock_name(hlock_class(next
));
1365 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1367 print_lock_name(backwards_entry
->class);
1368 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1370 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1372 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1373 print_lock_name(forwards_entry
->class);
1374 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1377 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1379 printk("\nother info that might help us debug this:\n\n");
1380 lockdep_print_held_locks(curr
);
1382 printk("\nthe dependencies between %s-irq-safe lock", irqclass
);
1383 printk(" and the holding lock:\n");
1384 if (!save_trace(&prev_root
->trace
))
1386 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1388 printk("\nthe dependencies between the lock to be acquired");
1389 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1390 if (!save_trace(&next_root
->trace
))
1392 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1394 printk("\nstack backtrace:\n");
1401 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1402 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1403 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1406 struct lock_list
this, that
;
1407 struct lock_list
*uninitialized_var(target_entry
);
1408 struct lock_list
*uninitialized_var(target_entry1
);
1412 this.class = hlock_class(prev
);
1413 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1415 return print_bfs_bug(ret
);
1420 that
.class = hlock_class(next
);
1421 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1423 return print_bfs_bug(ret
);
1427 return print_bad_irq_dependency(curr
, &this, &that
,
1428 target_entry
, target_entry1
,
1430 bit_backwards
, bit_forwards
, irqclass
);
1433 static const char *state_names
[] = {
1434 #define LOCKDEP_STATE(__STATE) \
1435 __stringify(__STATE),
1436 #include "lockdep_states.h"
1437 #undef LOCKDEP_STATE
1440 static const char *state_rnames
[] = {
1441 #define LOCKDEP_STATE(__STATE) \
1442 __stringify(__STATE)"-READ",
1443 #include "lockdep_states.h"
1444 #undef LOCKDEP_STATE
1447 static inline const char *state_name(enum lock_usage_bit bit
)
1449 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1452 static int exclusive_bit(int new_bit
)
1460 * bit 0 - write/read
1461 * bit 1 - used_in/enabled
1465 int state
= new_bit
& ~3;
1466 int dir
= new_bit
& 2;
1469 * keep state, bit flip the direction and strip read.
1471 return state
| (dir
^ 2);
1474 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1475 struct held_lock
*next
, enum lock_usage_bit bit
)
1478 * Prove that the new dependency does not connect a hardirq-safe
1479 * lock with a hardirq-unsafe lock - to achieve this we search
1480 * the backwards-subgraph starting at <prev>, and the
1481 * forwards-subgraph starting at <next>:
1483 if (!check_usage(curr
, prev
, next
, bit
,
1484 exclusive_bit(bit
), state_name(bit
)))
1490 * Prove that the new dependency does not connect a hardirq-safe-read
1491 * lock with a hardirq-unsafe lock - to achieve this we search
1492 * the backwards-subgraph starting at <prev>, and the
1493 * forwards-subgraph starting at <next>:
1495 if (!check_usage(curr
, prev
, next
, bit
,
1496 exclusive_bit(bit
), state_name(bit
)))
1503 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1504 struct held_lock
*next
)
1506 #define LOCKDEP_STATE(__STATE) \
1507 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1509 #include "lockdep_states.h"
1510 #undef LOCKDEP_STATE
1515 static void inc_chains(void)
1517 if (current
->hardirq_context
)
1518 nr_hardirq_chains
++;
1520 if (current
->softirq_context
)
1521 nr_softirq_chains
++;
1523 nr_process_chains
++;
1530 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1531 struct held_lock
*next
)
1536 static inline void inc_chains(void)
1538 nr_process_chains
++;
1544 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1545 struct held_lock
*next
)
1547 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1550 printk("\n=============================================\n");
1551 printk( "[ INFO: possible recursive locking detected ]\n");
1552 print_kernel_version();
1553 printk( "---------------------------------------------\n");
1554 printk("%s/%d is trying to acquire lock:\n",
1555 curr
->comm
, task_pid_nr(curr
));
1557 printk("\nbut task is already holding lock:\n");
1560 printk("\nother info that might help us debug this:\n");
1561 lockdep_print_held_locks(curr
);
1563 printk("\nstack backtrace:\n");
1570 * Check whether we are holding such a class already.
1572 * (Note that this has to be done separately, because the graph cannot
1573 * detect such classes of deadlocks.)
1575 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1578 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1579 struct lockdep_map
*next_instance
, int read
)
1581 struct held_lock
*prev
;
1582 struct held_lock
*nest
= NULL
;
1585 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1586 prev
= curr
->held_locks
+ i
;
1588 if (prev
->instance
== next
->nest_lock
)
1591 if (hlock_class(prev
) != hlock_class(next
))
1595 * Allow read-after-read recursion of the same
1596 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1598 if ((read
== 2) && prev
->read
)
1602 * We're holding the nest_lock, which serializes this lock's
1603 * nesting behaviour.
1608 return print_deadlock_bug(curr
, prev
, next
);
1614 * There was a chain-cache miss, and we are about to add a new dependency
1615 * to a previous lock. We recursively validate the following rules:
1617 * - would the adding of the <prev> -> <next> dependency create a
1618 * circular dependency in the graph? [== circular deadlock]
1620 * - does the new prev->next dependency connect any hardirq-safe lock
1621 * (in the full backwards-subgraph starting at <prev>) with any
1622 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1623 * <next>)? [== illegal lock inversion with hardirq contexts]
1625 * - does the new prev->next dependency connect any softirq-safe lock
1626 * (in the full backwards-subgraph starting at <prev>) with any
1627 * softirq-unsafe lock (in the full forwards-subgraph starting at
1628 * <next>)? [== illegal lock inversion with softirq contexts]
1630 * any of these scenarios could lead to a deadlock.
1632 * Then if all the validations pass, we add the forwards and backwards
1636 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1637 struct held_lock
*next
, int distance
)
1639 struct lock_list
*entry
;
1641 struct lock_list
this;
1642 struct lock_list
*uninitialized_var(target_entry
);
1645 * Prove that the new <prev> -> <next> dependency would not
1646 * create a circular dependency in the graph. (We do this by
1647 * forward-recursing into the graph starting at <next>, and
1648 * checking whether we can reach <prev>.)
1650 * We are using global variables to control the recursion, to
1651 * keep the stackframe size of the recursive functions low:
1653 this.class = hlock_class(next
);
1655 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1657 return print_circular_bug(&this, target_entry
, next
, prev
);
1658 else if (unlikely(ret
< 0))
1659 return print_bfs_bug(ret
);
1661 if (!check_prev_add_irq(curr
, prev
, next
))
1665 * For recursive read-locks we do all the dependency checks,
1666 * but we dont store read-triggered dependencies (only
1667 * write-triggered dependencies). This ensures that only the
1668 * write-side dependencies matter, and that if for example a
1669 * write-lock never takes any other locks, then the reads are
1670 * equivalent to a NOP.
1672 if (next
->read
== 2 || prev
->read
== 2)
1675 * Is the <prev> -> <next> dependency already present?
1677 * (this may occur even though this is a new chain: consider
1678 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1679 * chains - the second one will be new, but L1 already has
1680 * L2 added to its dependency list, due to the first chain.)
1682 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1683 if (entry
->class == hlock_class(next
)) {
1685 entry
->distance
= 1;
1691 * Ok, all validations passed, add the new lock
1692 * to the previous lock's dependency list:
1694 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1695 &hlock_class(prev
)->locks_after
,
1696 next
->acquire_ip
, distance
);
1701 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1702 &hlock_class(next
)->locks_before
,
1703 next
->acquire_ip
, distance
);
1708 * Debugging printouts:
1710 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1712 printk("\n new dependency: ");
1713 print_lock_name(hlock_class(prev
));
1715 print_lock_name(hlock_class(next
));
1718 return graph_lock();
1724 * Add the dependency to all directly-previous locks that are 'relevant'.
1725 * The ones that are relevant are (in increasing distance from curr):
1726 * all consecutive trylock entries and the final non-trylock entry - or
1727 * the end of this context's lock-chain - whichever comes first.
1730 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1732 int depth
= curr
->lockdep_depth
;
1733 struct held_lock
*hlock
;
1738 * Depth must not be zero for a non-head lock:
1743 * At least two relevant locks must exist for this
1746 if (curr
->held_locks
[depth
].irq_context
!=
1747 curr
->held_locks
[depth
-1].irq_context
)
1751 int distance
= curr
->lockdep_depth
- depth
+ 1;
1752 hlock
= curr
->held_locks
+ depth
-1;
1754 * Only non-recursive-read entries get new dependencies
1757 if (hlock
->read
!= 2) {
1758 if (!check_prev_add(curr
, hlock
, next
, distance
))
1761 * Stop after the first non-trylock entry,
1762 * as non-trylock entries have added their
1763 * own direct dependencies already, so this
1764 * lock is connected to them indirectly:
1766 if (!hlock
->trylock
)
1771 * End of lock-stack?
1776 * Stop the search if we cross into another context:
1778 if (curr
->held_locks
[depth
].irq_context
!=
1779 curr
->held_locks
[depth
-1].irq_context
)
1784 if (!debug_locks_off_graph_unlock())
1792 unsigned long nr_lock_chains
;
1793 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1794 int nr_chain_hlocks
;
1795 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1797 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1799 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1803 * Look up a dependency chain. If the key is not present yet then
1804 * add it and return 1 - in this case the new dependency chain is
1805 * validated. If the key is already hashed, return 0.
1806 * (On return with 1 graph_lock is held.)
1808 static inline int lookup_chain_cache(struct task_struct
*curr
,
1809 struct held_lock
*hlock
,
1812 struct lock_class
*class = hlock_class(hlock
);
1813 struct list_head
*hash_head
= chainhashentry(chain_key
);
1814 struct lock_chain
*chain
;
1815 struct held_lock
*hlock_curr
, *hlock_next
;
1818 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1821 * We can walk it lock-free, because entries only get added
1824 list_for_each_entry(chain
, hash_head
, entry
) {
1825 if (chain
->chain_key
== chain_key
) {
1827 debug_atomic_inc(&chain_lookup_hits
);
1828 if (very_verbose(class))
1829 printk("\nhash chain already cached, key: "
1830 "%016Lx tail class: [%p] %s\n",
1831 (unsigned long long)chain_key
,
1832 class->key
, class->name
);
1836 if (very_verbose(class))
1837 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1838 (unsigned long long)chain_key
, class->key
, class->name
);
1840 * Allocate a new chain entry from the static array, and add
1846 * We have to walk the chain again locked - to avoid duplicates:
1848 list_for_each_entry(chain
, hash_head
, entry
) {
1849 if (chain
->chain_key
== chain_key
) {
1854 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1855 if (!debug_locks_off_graph_unlock())
1858 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1859 printk("turning off the locking correctness validator.\n");
1863 chain
= lock_chains
+ nr_lock_chains
++;
1864 chain
->chain_key
= chain_key
;
1865 chain
->irq_context
= hlock
->irq_context
;
1866 /* Find the first held_lock of current chain */
1868 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
1869 hlock_curr
= curr
->held_locks
+ i
;
1870 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
1875 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
1876 cn
= nr_chain_hlocks
;
1877 while (cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
) {
1878 n
= cmpxchg(&nr_chain_hlocks
, cn
, cn
+ chain
->depth
);
1883 if (likely(cn
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
1885 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
1886 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
1887 chain_hlocks
[chain
->base
+ j
] = lock_id
;
1889 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
1891 list_add_tail_rcu(&chain
->entry
, hash_head
);
1892 debug_atomic_inc(&chain_lookup_misses
);
1898 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1899 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
1902 * Trylock needs to maintain the stack of held locks, but it
1903 * does not add new dependencies, because trylock can be done
1906 * We look up the chain_key and do the O(N^2) check and update of
1907 * the dependencies only if this is a new dependency chain.
1908 * (If lookup_chain_cache() returns with 1 it acquires
1909 * graph_lock for us)
1911 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1912 lookup_chain_cache(curr
, hlock
, chain_key
)) {
1914 * Check whether last held lock:
1916 * - is irq-safe, if this lock is irq-unsafe
1917 * - is softirq-safe, if this lock is hardirq-unsafe
1919 * And check whether the new lock's dependency graph
1920 * could lead back to the previous lock.
1922 * any of these scenarios could lead to a deadlock. If
1925 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1930 * Mark recursive read, as we jump over it when
1931 * building dependencies (just like we jump over
1937 * Add dependency only if this lock is not the head
1938 * of the chain, and if it's not a secondary read-lock:
1940 if (!chain_head
&& ret
!= 2)
1941 if (!check_prevs_add(curr
, hlock
))
1945 /* after lookup_chain_cache(): */
1946 if (unlikely(!debug_locks
))
1952 static inline int validate_chain(struct task_struct
*curr
,
1953 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1954 int chain_head
, u64 chain_key
)
1961 * We are building curr_chain_key incrementally, so double-check
1962 * it from scratch, to make sure that it's done correctly:
1964 static void check_chain_key(struct task_struct
*curr
)
1966 #ifdef CONFIG_DEBUG_LOCKDEP
1967 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1971 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1972 hlock
= curr
->held_locks
+ i
;
1973 if (chain_key
!= hlock
->prev_chain_key
) {
1975 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1976 curr
->lockdep_depth
, i
,
1977 (unsigned long long)chain_key
,
1978 (unsigned long long)hlock
->prev_chain_key
);
1981 id
= hlock
->class_idx
- 1;
1982 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1985 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1986 hlock
->irq_context
))
1988 chain_key
= iterate_chain_key(chain_key
, id
);
1991 if (chain_key
!= curr
->curr_chain_key
) {
1993 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1994 curr
->lockdep_depth
, i
,
1995 (unsigned long long)chain_key
,
1996 (unsigned long long)curr
->curr_chain_key
);
2002 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2003 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2005 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2008 printk("\n=================================\n");
2009 printk( "[ INFO: inconsistent lock state ]\n");
2010 print_kernel_version();
2011 printk( "---------------------------------\n");
2013 printk("inconsistent {%s} -> {%s} usage.\n",
2014 usage_str
[prev_bit
], usage_str
[new_bit
]);
2016 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2017 curr
->comm
, task_pid_nr(curr
),
2018 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2019 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2020 trace_hardirqs_enabled(curr
),
2021 trace_softirqs_enabled(curr
));
2024 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2025 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2027 print_irqtrace_events(curr
);
2028 printk("\nother info that might help us debug this:\n");
2029 lockdep_print_held_locks(curr
);
2031 printk("\nstack backtrace:\n");
2038 * Print out an error if an invalid bit is set:
2041 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2042 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2044 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2045 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2049 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2050 enum lock_usage_bit new_bit
);
2052 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2055 * print irq inversion bug:
2058 print_irq_inversion_bug(struct task_struct
*curr
,
2059 struct lock_list
*root
, struct lock_list
*other
,
2060 struct held_lock
*this, int forwards
,
2061 const char *irqclass
)
2063 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2066 printk("\n=========================================================\n");
2067 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2068 print_kernel_version();
2069 printk( "---------------------------------------------------------\n");
2070 printk("%s/%d just changed the state of lock:\n",
2071 curr
->comm
, task_pid_nr(curr
));
2074 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2076 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2077 print_lock_name(other
->class);
2078 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2080 printk("\nother info that might help us debug this:\n");
2081 lockdep_print_held_locks(curr
);
2083 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2084 if (!save_trace(&root
->trace
))
2086 print_shortest_lock_dependencies(other
, root
);
2088 printk("\nstack backtrace:\n");
2095 * Prove that in the forwards-direction subgraph starting at <this>
2096 * there is no lock matching <mask>:
2099 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2100 enum lock_usage_bit bit
, const char *irqclass
)
2103 struct lock_list root
;
2104 struct lock_list
*uninitialized_var(target_entry
);
2107 root
.class = hlock_class(this);
2108 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2110 return print_bfs_bug(ret
);
2114 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2119 * Prove that in the backwards-direction subgraph starting at <this>
2120 * there is no lock matching <mask>:
2123 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2124 enum lock_usage_bit bit
, const char *irqclass
)
2127 struct lock_list root
;
2128 struct lock_list
*uninitialized_var(target_entry
);
2131 root
.class = hlock_class(this);
2132 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2134 return print_bfs_bug(ret
);
2138 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2142 void print_irqtrace_events(struct task_struct
*curr
)
2144 printk("irq event stamp: %u\n", curr
->irq_events
);
2145 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
2146 print_ip_sym(curr
->hardirq_enable_ip
);
2147 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
2148 print_ip_sym(curr
->hardirq_disable_ip
);
2149 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
2150 print_ip_sym(curr
->softirq_enable_ip
);
2151 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
2152 print_ip_sym(curr
->softirq_disable_ip
);
2155 static int HARDIRQ_verbose(struct lock_class
*class)
2158 return class_filter(class);
2163 static int SOFTIRQ_verbose(struct lock_class
*class)
2166 return class_filter(class);
2171 static int RECLAIM_FS_verbose(struct lock_class
*class)
2174 return class_filter(class);
2179 #define STRICT_READ_CHECKS 1
2181 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2182 #define LOCKDEP_STATE(__STATE) \
2184 #include "lockdep_states.h"
2185 #undef LOCKDEP_STATE
2188 static inline int state_verbose(enum lock_usage_bit bit
,
2189 struct lock_class
*class)
2191 return state_verbose_f
[bit
>> 2](class);
2194 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2195 enum lock_usage_bit bit
, const char *name
);
2198 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2199 enum lock_usage_bit new_bit
)
2201 int excl_bit
= exclusive_bit(new_bit
);
2202 int read
= new_bit
& 1;
2203 int dir
= new_bit
& 2;
2206 * mark USED_IN has to look forwards -- to ensure no dependency
2207 * has ENABLED state, which would allow recursion deadlocks.
2209 * mark ENABLED has to look backwards -- to ensure no dependee
2210 * has USED_IN state, which, again, would allow recursion deadlocks.
2212 check_usage_f usage
= dir
?
2213 check_usage_backwards
: check_usage_forwards
;
2216 * Validate that this particular lock does not have conflicting
2219 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2223 * Validate that the lock dependencies don't have conflicting usage
2226 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2227 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2231 * Check for read in write conflicts
2234 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2237 if (STRICT_READ_CHECKS
&&
2238 !usage(curr
, this, excl_bit
+ 1,
2239 state_name(new_bit
+ 1)))
2243 if (state_verbose(new_bit
, hlock_class(this)))
2250 #define LOCKDEP_STATE(__STATE) __STATE,
2251 #include "lockdep_states.h"
2252 #undef LOCKDEP_STATE
2256 * Mark all held locks with a usage bit:
2259 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2261 enum lock_usage_bit usage_bit
;
2262 struct held_lock
*hlock
;
2265 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2266 hlock
= curr
->held_locks
+ i
;
2268 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2270 usage_bit
+= 1; /* READ */
2272 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2274 if (!mark_lock(curr
, hlock
, usage_bit
))
2282 * Debugging helper: via this flag we know that we are in
2283 * 'early bootup code', and will warn about any invalid irqs-on event:
2285 static int early_boot_irqs_enabled
;
2287 void early_boot_irqs_off(void)
2289 early_boot_irqs_enabled
= 0;
2292 void early_boot_irqs_on(void)
2294 early_boot_irqs_enabled
= 1;
2298 * Hardirqs will be enabled:
2300 void trace_hardirqs_on_caller(unsigned long ip
)
2302 struct task_struct
*curr
= current
;
2304 time_hardirqs_on(CALLER_ADDR0
, ip
);
2306 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2309 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2312 if (unlikely(curr
->hardirqs_enabled
)) {
2313 debug_atomic_inc(&redundant_hardirqs_on
);
2316 /* we'll do an OFF -> ON transition: */
2317 curr
->hardirqs_enabled
= 1;
2319 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2321 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2324 * We are going to turn hardirqs on, so set the
2325 * usage bit for all held locks:
2327 if (!mark_held_locks(curr
, HARDIRQ
))
2330 * If we have softirqs enabled, then set the usage
2331 * bit for all held locks. (disabled hardirqs prevented
2332 * this bit from being set before)
2334 if (curr
->softirqs_enabled
)
2335 if (!mark_held_locks(curr
, SOFTIRQ
))
2338 curr
->hardirq_enable_ip
= ip
;
2339 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2340 debug_atomic_inc(&hardirqs_on_events
);
2342 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2344 void trace_hardirqs_on(void)
2346 trace_hardirqs_on_caller(CALLER_ADDR0
);
2348 EXPORT_SYMBOL(trace_hardirqs_on
);
2351 * Hardirqs were disabled:
2353 void trace_hardirqs_off_caller(unsigned long ip
)
2355 struct task_struct
*curr
= current
;
2357 time_hardirqs_off(CALLER_ADDR0
, ip
);
2359 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2362 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2365 if (curr
->hardirqs_enabled
) {
2367 * We have done an ON -> OFF transition:
2369 curr
->hardirqs_enabled
= 0;
2370 curr
->hardirq_disable_ip
= ip
;
2371 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2372 debug_atomic_inc(&hardirqs_off_events
);
2374 debug_atomic_inc(&redundant_hardirqs_off
);
2376 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2378 void trace_hardirqs_off(void)
2380 trace_hardirqs_off_caller(CALLER_ADDR0
);
2382 EXPORT_SYMBOL(trace_hardirqs_off
);
2385 * Softirqs will be enabled:
2387 void trace_softirqs_on(unsigned long ip
)
2389 struct task_struct
*curr
= current
;
2391 if (unlikely(!debug_locks
))
2394 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2397 if (curr
->softirqs_enabled
) {
2398 debug_atomic_inc(&redundant_softirqs_on
);
2403 * We'll do an OFF -> ON transition:
2405 curr
->softirqs_enabled
= 1;
2406 curr
->softirq_enable_ip
= ip
;
2407 curr
->softirq_enable_event
= ++curr
->irq_events
;
2408 debug_atomic_inc(&softirqs_on_events
);
2410 * We are going to turn softirqs on, so set the
2411 * usage bit for all held locks, if hardirqs are
2414 if (curr
->hardirqs_enabled
)
2415 mark_held_locks(curr
, SOFTIRQ
);
2419 * Softirqs were disabled:
2421 void trace_softirqs_off(unsigned long ip
)
2423 struct task_struct
*curr
= current
;
2425 if (unlikely(!debug_locks
))
2428 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2431 if (curr
->softirqs_enabled
) {
2433 * We have done an ON -> OFF transition:
2435 curr
->softirqs_enabled
= 0;
2436 curr
->softirq_disable_ip
= ip
;
2437 curr
->softirq_disable_event
= ++curr
->irq_events
;
2438 debug_atomic_inc(&softirqs_off_events
);
2439 DEBUG_LOCKS_WARN_ON(!softirq_count());
2441 debug_atomic_inc(&redundant_softirqs_off
);
2444 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2446 struct task_struct
*curr
= current
;
2448 if (unlikely(!debug_locks
))
2451 /* no reclaim without waiting on it */
2452 if (!(gfp_mask
& __GFP_WAIT
))
2455 /* this guy won't enter reclaim */
2456 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2459 /* We're only interested __GFP_FS allocations for now */
2460 if (!(gfp_mask
& __GFP_FS
))
2463 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2466 mark_held_locks(curr
, RECLAIM_FS
);
2469 static void check_flags(unsigned long flags
);
2471 void lockdep_trace_alloc(gfp_t gfp_mask
)
2473 unsigned long flags
;
2475 if (unlikely(current
->lockdep_recursion
))
2478 raw_local_irq_save(flags
);
2480 current
->lockdep_recursion
= 1;
2481 __lockdep_trace_alloc(gfp_mask
, flags
);
2482 current
->lockdep_recursion
= 0;
2483 raw_local_irq_restore(flags
);
2486 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2489 * If non-trylock use in a hardirq or softirq context, then
2490 * mark the lock as used in these contexts:
2492 if (!hlock
->trylock
) {
2494 if (curr
->hardirq_context
)
2495 if (!mark_lock(curr
, hlock
,
2496 LOCK_USED_IN_HARDIRQ_READ
))
2498 if (curr
->softirq_context
)
2499 if (!mark_lock(curr
, hlock
,
2500 LOCK_USED_IN_SOFTIRQ_READ
))
2503 if (curr
->hardirq_context
)
2504 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2506 if (curr
->softirq_context
)
2507 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2511 if (!hlock
->hardirqs_off
) {
2513 if (!mark_lock(curr
, hlock
,
2514 LOCK_ENABLED_HARDIRQ_READ
))
2516 if (curr
->softirqs_enabled
)
2517 if (!mark_lock(curr
, hlock
,
2518 LOCK_ENABLED_SOFTIRQ_READ
))
2521 if (!mark_lock(curr
, hlock
,
2522 LOCK_ENABLED_HARDIRQ
))
2524 if (curr
->softirqs_enabled
)
2525 if (!mark_lock(curr
, hlock
,
2526 LOCK_ENABLED_SOFTIRQ
))
2532 * We reuse the irq context infrastructure more broadly as a general
2533 * context checking code. This tests GFP_FS recursion (a lock taken
2534 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2537 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2539 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2542 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2550 static int separate_irq_context(struct task_struct
*curr
,
2551 struct held_lock
*hlock
)
2553 unsigned int depth
= curr
->lockdep_depth
;
2556 * Keep track of points where we cross into an interrupt context:
2558 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2559 curr
->softirq_context
;
2561 struct held_lock
*prev_hlock
;
2563 prev_hlock
= curr
->held_locks
+ depth
-1;
2565 * If we cross into another context, reset the
2566 * hash key (this also prevents the checking and the
2567 * adding of the dependency to 'prev'):
2569 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2578 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2579 enum lock_usage_bit new_bit
)
2585 static inline int mark_irqflags(struct task_struct
*curr
,
2586 struct held_lock
*hlock
)
2591 static inline int separate_irq_context(struct task_struct
*curr
,
2592 struct held_lock
*hlock
)
2597 void lockdep_trace_alloc(gfp_t gfp_mask
)
2604 * Mark a lock with a usage bit, and validate the state transition:
2606 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2607 enum lock_usage_bit new_bit
)
2609 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2612 * If already set then do not dirty the cacheline,
2613 * nor do any checks:
2615 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2621 * Make sure we didnt race:
2623 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2628 hlock_class(this)->usage_mask
|= new_mask
;
2630 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2634 #define LOCKDEP_STATE(__STATE) \
2635 case LOCK_USED_IN_##__STATE: \
2636 case LOCK_USED_IN_##__STATE##_READ: \
2637 case LOCK_ENABLED_##__STATE: \
2638 case LOCK_ENABLED_##__STATE##_READ:
2639 #include "lockdep_states.h"
2640 #undef LOCKDEP_STATE
2641 ret
= mark_lock_irq(curr
, this, new_bit
);
2646 debug_atomic_dec(&nr_unused_locks
);
2649 if (!debug_locks_off_graph_unlock())
2658 * We must printk outside of the graph_lock:
2661 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2663 print_irqtrace_events(curr
);
2671 * Initialize a lock instance's lock-class mapping info:
2673 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2674 struct lock_class_key
*key
, int subclass
)
2676 lock
->class_cache
= NULL
;
2677 #ifdef CONFIG_LOCK_STAT
2678 lock
->cpu
= raw_smp_processor_id();
2681 if (DEBUG_LOCKS_WARN_ON(!name
)) {
2682 lock
->name
= "NULL";
2688 if (DEBUG_LOCKS_WARN_ON(!key
))
2691 * Sanity check, the lock-class key must be persistent:
2693 if (!static_obj(key
)) {
2694 printk("BUG: key %p not in .data!\n", key
);
2695 DEBUG_LOCKS_WARN_ON(1);
2700 if (unlikely(!debug_locks
))
2704 register_lock_class(lock
, subclass
, 1);
2706 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2709 * This gets called for every mutex_lock*()/spin_lock*() operation.
2710 * We maintain the dependency maps and validate the locking attempt:
2712 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2713 int trylock
, int read
, int check
, int hardirqs_off
,
2714 struct lockdep_map
*nest_lock
, unsigned long ip
,
2717 struct task_struct
*curr
= current
;
2718 struct lock_class
*class = NULL
;
2719 struct held_lock
*hlock
;
2720 unsigned int depth
, id
;
2728 if (unlikely(!debug_locks
))
2731 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2734 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2736 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2737 printk("turning off the locking correctness validator.\n");
2743 class = lock
->class_cache
;
2745 * Not cached yet or subclass?
2747 if (unlikely(!class)) {
2748 class = register_lock_class(lock
, subclass
, 0);
2752 debug_atomic_inc((atomic_t
*)&class->ops
);
2753 if (very_verbose(class)) {
2754 printk("\nacquire class [%p] %s", class->key
, class->name
);
2755 if (class->name_version
> 1)
2756 printk("#%d", class->name_version
);
2762 * Add the lock to the list of currently held locks.
2763 * (we dont increase the depth just yet, up until the
2764 * dependency checks are done)
2766 depth
= curr
->lockdep_depth
;
2767 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2770 class_idx
= class - lock_classes
+ 1;
2773 hlock
= curr
->held_locks
+ depth
- 1;
2774 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
2775 if (hlock
->references
)
2776 hlock
->references
++;
2778 hlock
->references
= 2;
2784 hlock
= curr
->held_locks
+ depth
;
2785 if (DEBUG_LOCKS_WARN_ON(!class))
2787 hlock
->class_idx
= class_idx
;
2788 hlock
->acquire_ip
= ip
;
2789 hlock
->instance
= lock
;
2790 hlock
->nest_lock
= nest_lock
;
2791 hlock
->trylock
= trylock
;
2793 hlock
->check
= check
;
2794 hlock
->hardirqs_off
= !!hardirqs_off
;
2795 hlock
->references
= references
;
2796 #ifdef CONFIG_LOCK_STAT
2797 hlock
->waittime_stamp
= 0;
2798 hlock
->holdtime_stamp
= lockstat_clock();
2801 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2804 /* mark it as used: */
2805 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2809 * Calculate the chain hash: it's the combined hash of all the
2810 * lock keys along the dependency chain. We save the hash value
2811 * at every step so that we can get the current hash easily
2812 * after unlock. The chain hash is then used to cache dependency
2815 * The 'key ID' is what is the most compact key value to drive
2816 * the hash, not class->key.
2818 id
= class - lock_classes
;
2819 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2822 chain_key
= curr
->curr_chain_key
;
2824 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2829 hlock
->prev_chain_key
= chain_key
;
2830 if (separate_irq_context(curr
, hlock
)) {
2834 chain_key
= iterate_chain_key(chain_key
, id
);
2836 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
2839 curr
->curr_chain_key
= chain_key
;
2840 curr
->lockdep_depth
++;
2841 check_chain_key(curr
);
2842 #ifdef CONFIG_DEBUG_LOCKDEP
2843 if (unlikely(!debug_locks
))
2846 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2848 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2849 printk("turning off the locking correctness validator.\n");
2854 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2855 max_lockdep_depth
= curr
->lockdep_depth
;
2861 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2864 if (!debug_locks_off())
2866 if (debug_locks_silent
)
2869 printk("\n=====================================\n");
2870 printk( "[ BUG: bad unlock balance detected! ]\n");
2871 printk( "-------------------------------------\n");
2872 printk("%s/%d is trying to release lock (",
2873 curr
->comm
, task_pid_nr(curr
));
2874 print_lockdep_cache(lock
);
2877 printk("but there are no more locks to release!\n");
2878 printk("\nother info that might help us debug this:\n");
2879 lockdep_print_held_locks(curr
);
2881 printk("\nstack backtrace:\n");
2888 * Common debugging checks for both nested and non-nested unlock:
2890 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2893 if (unlikely(!debug_locks
))
2895 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2898 if (curr
->lockdep_depth
<= 0)
2899 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2904 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
2906 if (hlock
->instance
== lock
)
2909 if (hlock
->references
) {
2910 struct lock_class
*class = lock
->class_cache
;
2913 class = look_up_lock_class(lock
, 0);
2915 if (DEBUG_LOCKS_WARN_ON(!class))
2918 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
2921 if (hlock
->class_idx
== class - lock_classes
+ 1)
2929 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
2930 struct lock_class_key
*key
, unsigned int subclass
,
2933 struct task_struct
*curr
= current
;
2934 struct held_lock
*hlock
, *prev_hlock
;
2935 struct lock_class
*class;
2939 depth
= curr
->lockdep_depth
;
2940 if (DEBUG_LOCKS_WARN_ON(!depth
))
2944 for (i
= depth
-1; i
>= 0; i
--) {
2945 hlock
= curr
->held_locks
+ i
;
2947 * We must not cross into another context:
2949 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2951 if (match_held_lock(hlock
, lock
))
2955 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2958 lockdep_init_map(lock
, name
, key
, 0);
2959 class = register_lock_class(lock
, subclass
, 0);
2960 hlock
->class_idx
= class - lock_classes
+ 1;
2962 curr
->lockdep_depth
= i
;
2963 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2965 for (; i
< depth
; i
++) {
2966 hlock
= curr
->held_locks
+ i
;
2967 if (!__lock_acquire(hlock
->instance
,
2968 hlock_class(hlock
)->subclass
, hlock
->trylock
,
2969 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2970 hlock
->nest_lock
, hlock
->acquire_ip
,
2975 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
2981 * Remove the lock to the list of currently held locks in a
2982 * potentially non-nested (out of order) manner. This is a
2983 * relatively rare operation, as all the unlock APIs default
2984 * to nested mode (which uses lock_release()):
2987 lock_release_non_nested(struct task_struct
*curr
,
2988 struct lockdep_map
*lock
, unsigned long ip
)
2990 struct held_lock
*hlock
, *prev_hlock
;
2995 * Check whether the lock exists in the current stack
2998 depth
= curr
->lockdep_depth
;
2999 if (DEBUG_LOCKS_WARN_ON(!depth
))
3003 for (i
= depth
-1; i
>= 0; i
--) {
3004 hlock
= curr
->held_locks
+ i
;
3006 * We must not cross into another context:
3008 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3010 if (match_held_lock(hlock
, lock
))
3014 return print_unlock_inbalance_bug(curr
, lock
, ip
);
3017 if (hlock
->instance
== lock
)
3018 lock_release_holdtime(hlock
);
3020 if (hlock
->references
) {
3021 hlock
->references
--;
3022 if (hlock
->references
) {
3024 * We had, and after removing one, still have
3025 * references, the current lock stack is still
3026 * valid. We're done!
3033 * We have the right lock to unlock, 'hlock' points to it.
3034 * Now we remove it from the stack, and add back the other
3035 * entries (if any), recalculating the hash along the way:
3038 curr
->lockdep_depth
= i
;
3039 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3041 for (i
++; i
< depth
; i
++) {
3042 hlock
= curr
->held_locks
+ i
;
3043 if (!__lock_acquire(hlock
->instance
,
3044 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3045 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3046 hlock
->nest_lock
, hlock
->acquire_ip
,
3051 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3057 * Remove the lock to the list of currently held locks - this gets
3058 * called on mutex_unlock()/spin_unlock*() (or on a failed
3059 * mutex_lock_interruptible()). This is done for unlocks that nest
3060 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3062 static int lock_release_nested(struct task_struct
*curr
,
3063 struct lockdep_map
*lock
, unsigned long ip
)
3065 struct held_lock
*hlock
;
3069 * Pop off the top of the lock stack:
3071 depth
= curr
->lockdep_depth
- 1;
3072 hlock
= curr
->held_locks
+ depth
;
3075 * Is the unlock non-nested:
3077 if (hlock
->instance
!= lock
|| hlock
->references
)
3078 return lock_release_non_nested(curr
, lock
, ip
);
3079 curr
->lockdep_depth
--;
3081 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
3084 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3086 lock_release_holdtime(hlock
);
3088 #ifdef CONFIG_DEBUG_LOCKDEP
3089 hlock
->prev_chain_key
= 0;
3090 hlock
->class_idx
= 0;
3091 hlock
->acquire_ip
= 0;
3092 hlock
->irq_context
= 0;
3098 * Remove the lock to the list of currently held locks - this gets
3099 * called on mutex_unlock()/spin_unlock*() (or on a failed
3100 * mutex_lock_interruptible()). This is done for unlocks that nest
3101 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3104 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3106 struct task_struct
*curr
= current
;
3108 if (!check_unlock(curr
, lock
, ip
))
3112 if (!lock_release_nested(curr
, lock
, ip
))
3115 if (!lock_release_non_nested(curr
, lock
, ip
))
3119 check_chain_key(curr
);
3122 static int __lock_is_held(struct lockdep_map
*lock
)
3124 struct task_struct
*curr
= current
;
3127 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3128 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3130 if (match_held_lock(hlock
, lock
))
3138 * Check whether we follow the irq-flags state precisely:
3140 static void check_flags(unsigned long flags
)
3142 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3143 defined(CONFIG_TRACE_IRQFLAGS)
3147 if (irqs_disabled_flags(flags
)) {
3148 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3149 printk("possible reason: unannotated irqs-off.\n");
3152 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3153 printk("possible reason: unannotated irqs-on.\n");
3158 * We dont accurately track softirq state in e.g.
3159 * hardirq contexts (such as on 4KSTACKS), so only
3160 * check if not in hardirq contexts:
3162 if (!hardirq_count()) {
3163 if (softirq_count())
3164 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3166 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3170 print_irqtrace_events(current
);
3174 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3175 struct lock_class_key
*key
, unsigned int subclass
,
3178 unsigned long flags
;
3180 if (unlikely(current
->lockdep_recursion
))
3183 raw_local_irq_save(flags
);
3184 current
->lockdep_recursion
= 1;
3186 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3187 check_chain_key(current
);
3188 current
->lockdep_recursion
= 0;
3189 raw_local_irq_restore(flags
);
3191 EXPORT_SYMBOL_GPL(lock_set_class
);
3194 * We are not always called with irqs disabled - do that here,
3195 * and also avoid lockdep recursion:
3197 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3198 int trylock
, int read
, int check
,
3199 struct lockdep_map
*nest_lock
, unsigned long ip
)
3201 unsigned long flags
;
3203 if (unlikely(current
->lockdep_recursion
))
3206 raw_local_irq_save(flags
);
3209 current
->lockdep_recursion
= 1;
3210 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3211 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3212 irqs_disabled_flags(flags
), nest_lock
, ip
, 0);
3213 current
->lockdep_recursion
= 0;
3214 raw_local_irq_restore(flags
);
3216 EXPORT_SYMBOL_GPL(lock_acquire
);
3218 void lock_release(struct lockdep_map
*lock
, int nested
,
3221 unsigned long flags
;
3223 if (unlikely(current
->lockdep_recursion
))
3226 raw_local_irq_save(flags
);
3228 current
->lockdep_recursion
= 1;
3229 trace_lock_release(lock
, nested
, ip
);
3230 __lock_release(lock
, nested
, ip
);
3231 current
->lockdep_recursion
= 0;
3232 raw_local_irq_restore(flags
);
3234 EXPORT_SYMBOL_GPL(lock_release
);
3236 int lock_is_held(struct lockdep_map
*lock
)
3238 unsigned long flags
;
3241 if (unlikely(current
->lockdep_recursion
))
3244 raw_local_irq_save(flags
);
3247 current
->lockdep_recursion
= 1;
3248 ret
= __lock_is_held(lock
);
3249 current
->lockdep_recursion
= 0;
3250 raw_local_irq_restore(flags
);
3254 EXPORT_SYMBOL_GPL(lock_is_held
);
3256 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3258 current
->lockdep_reclaim_gfp
= gfp_mask
;
3261 void lockdep_clear_current_reclaim_state(void)
3263 current
->lockdep_reclaim_gfp
= 0;
3266 #ifdef CONFIG_LOCK_STAT
3268 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3271 if (!debug_locks_off())
3273 if (debug_locks_silent
)
3276 printk("\n=================================\n");
3277 printk( "[ BUG: bad contention detected! ]\n");
3278 printk( "---------------------------------\n");
3279 printk("%s/%d is trying to contend lock (",
3280 curr
->comm
, task_pid_nr(curr
));
3281 print_lockdep_cache(lock
);
3284 printk("but there are no locks held!\n");
3285 printk("\nother info that might help us debug this:\n");
3286 lockdep_print_held_locks(curr
);
3288 printk("\nstack backtrace:\n");
3295 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3297 struct task_struct
*curr
= current
;
3298 struct held_lock
*hlock
, *prev_hlock
;
3299 struct lock_class_stats
*stats
;
3301 int i
, contention_point
, contending_point
;
3303 depth
= curr
->lockdep_depth
;
3304 if (DEBUG_LOCKS_WARN_ON(!depth
))
3308 for (i
= depth
-1; i
>= 0; i
--) {
3309 hlock
= curr
->held_locks
+ i
;
3311 * We must not cross into another context:
3313 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3315 if (match_held_lock(hlock
, lock
))
3319 print_lock_contention_bug(curr
, lock
, ip
);
3323 if (hlock
->instance
!= lock
)
3326 hlock
->waittime_stamp
= lockstat_clock();
3328 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3329 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3332 stats
= get_lock_stats(hlock_class(hlock
));
3333 if (contention_point
< LOCKSTAT_POINTS
)
3334 stats
->contention_point
[contention_point
]++;
3335 if (contending_point
< LOCKSTAT_POINTS
)
3336 stats
->contending_point
[contending_point
]++;
3337 if (lock
->cpu
!= smp_processor_id())
3338 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3339 put_lock_stats(stats
);
3343 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3345 struct task_struct
*curr
= current
;
3346 struct held_lock
*hlock
, *prev_hlock
;
3347 struct lock_class_stats
*stats
;
3349 u64 now
, waittime
= 0;
3352 depth
= curr
->lockdep_depth
;
3353 if (DEBUG_LOCKS_WARN_ON(!depth
))
3357 for (i
= depth
-1; i
>= 0; i
--) {
3358 hlock
= curr
->held_locks
+ i
;
3360 * We must not cross into another context:
3362 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3364 if (match_held_lock(hlock
, lock
))
3368 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3372 if (hlock
->instance
!= lock
)
3375 cpu
= smp_processor_id();
3376 if (hlock
->waittime_stamp
) {
3377 now
= lockstat_clock();
3378 waittime
= now
- hlock
->waittime_stamp
;
3379 hlock
->holdtime_stamp
= now
;
3382 trace_lock_acquired(lock
, ip
, waittime
);
3384 stats
= get_lock_stats(hlock_class(hlock
));
3387 lock_time_inc(&stats
->read_waittime
, waittime
);
3389 lock_time_inc(&stats
->write_waittime
, waittime
);
3391 if (lock
->cpu
!= cpu
)
3392 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3393 put_lock_stats(stats
);
3399 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3401 unsigned long flags
;
3403 if (unlikely(!lock_stat
))
3406 if (unlikely(current
->lockdep_recursion
))
3409 raw_local_irq_save(flags
);
3411 current
->lockdep_recursion
= 1;
3412 trace_lock_contended(lock
, ip
);
3413 __lock_contended(lock
, ip
);
3414 current
->lockdep_recursion
= 0;
3415 raw_local_irq_restore(flags
);
3417 EXPORT_SYMBOL_GPL(lock_contended
);
3419 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3421 unsigned long flags
;
3423 if (unlikely(!lock_stat
))
3426 if (unlikely(current
->lockdep_recursion
))
3429 raw_local_irq_save(flags
);
3431 current
->lockdep_recursion
= 1;
3432 __lock_acquired(lock
, ip
);
3433 current
->lockdep_recursion
= 0;
3434 raw_local_irq_restore(flags
);
3436 EXPORT_SYMBOL_GPL(lock_acquired
);
3440 * Used by the testsuite, sanitize the validator state
3441 * after a simulated failure:
3444 void lockdep_reset(void)
3446 unsigned long flags
;
3449 raw_local_irq_save(flags
);
3450 current
->curr_chain_key
= 0;
3451 current
->lockdep_depth
= 0;
3452 current
->lockdep_recursion
= 0;
3453 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3454 nr_hardirq_chains
= 0;
3455 nr_softirq_chains
= 0;
3456 nr_process_chains
= 0;
3458 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3459 INIT_LIST_HEAD(chainhash_table
+ i
);
3460 raw_local_irq_restore(flags
);
3463 static void zap_class(struct lock_class
*class)
3468 * Remove all dependencies this lock is
3471 for (i
= 0; i
< nr_list_entries
; i
++) {
3472 if (list_entries
[i
].class == class)
3473 list_del_rcu(&list_entries
[i
].entry
);
3476 * Unhash the class and remove it from the all_lock_classes list:
3478 list_del_rcu(&class->hash_entry
);
3479 list_del_rcu(&class->lock_entry
);
3484 static inline int within(const void *addr
, void *start
, unsigned long size
)
3486 return addr
>= start
&& addr
< start
+ size
;
3489 void lockdep_free_key_range(void *start
, unsigned long size
)
3491 struct lock_class
*class, *next
;
3492 struct list_head
*head
;
3493 unsigned long flags
;
3497 raw_local_irq_save(flags
);
3498 locked
= graph_lock();
3501 * Unhash all classes that were created by this module:
3503 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3504 head
= classhash_table
+ i
;
3505 if (list_empty(head
))
3507 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3508 if (within(class->key
, start
, size
))
3510 else if (within(class->name
, start
, size
))
3517 raw_local_irq_restore(flags
);
3520 void lockdep_reset_lock(struct lockdep_map
*lock
)
3522 struct lock_class
*class, *next
;
3523 struct list_head
*head
;
3524 unsigned long flags
;
3528 raw_local_irq_save(flags
);
3531 * Remove all classes this lock might have:
3533 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3535 * If the class exists we look it up and zap it:
3537 class = look_up_lock_class(lock
, j
);
3542 * Debug check: in the end all mapped classes should
3545 locked
= graph_lock();
3546 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3547 head
= classhash_table
+ i
;
3548 if (list_empty(head
))
3550 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3551 if (unlikely(class == lock
->class_cache
)) {
3552 if (debug_locks_off_graph_unlock())
3562 raw_local_irq_restore(flags
);
3565 void lockdep_init(void)
3570 * Some architectures have their own start_kernel()
3571 * code which calls lockdep_init(), while we also
3572 * call lockdep_init() from the start_kernel() itself,
3573 * and we want to initialize the hashes only once:
3575 if (lockdep_initialized
)
3578 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3579 INIT_LIST_HEAD(classhash_table
+ i
);
3581 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3582 INIT_LIST_HEAD(chainhash_table
+ i
);
3584 lockdep_initialized
= 1;
3587 void __init
lockdep_info(void)
3589 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3591 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3592 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3593 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3594 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3595 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3596 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3597 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3599 printk(" memory used by lock dependency info: %lu kB\n",
3600 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3601 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3602 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3603 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3604 sizeof(struct list_head
) * CHAINHASH_SIZE
3605 #ifdef CONFIG_PROVE_LOCKING
3606 + sizeof(struct circular_queue
)
3611 printk(" per task-struct memory footprint: %lu bytes\n",
3612 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3614 #ifdef CONFIG_DEBUG_LOCKDEP
3615 if (lockdep_init_error
) {
3616 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3617 printk("Call stack leading to lockdep invocation was:\n");
3618 print_stack_trace(&lockdep_init_trace
, 0);
3624 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3625 const void *mem_to
, struct held_lock
*hlock
)
3627 if (!debug_locks_off())
3629 if (debug_locks_silent
)
3632 printk("\n=========================\n");
3633 printk( "[ BUG: held lock freed! ]\n");
3634 printk( "-------------------------\n");
3635 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3636 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3638 lockdep_print_held_locks(curr
);
3640 printk("\nstack backtrace:\n");
3644 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3645 const void* lock_from
, unsigned long lock_len
)
3647 return lock_from
+ lock_len
<= mem_from
||
3648 mem_from
+ mem_len
<= lock_from
;
3652 * Called when kernel memory is freed (or unmapped), or if a lock
3653 * is destroyed or reinitialized - this code checks whether there is
3654 * any held lock in the memory range of <from> to <to>:
3656 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3658 struct task_struct
*curr
= current
;
3659 struct held_lock
*hlock
;
3660 unsigned long flags
;
3663 if (unlikely(!debug_locks
))
3666 local_irq_save(flags
);
3667 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3668 hlock
= curr
->held_locks
+ i
;
3670 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3671 sizeof(*hlock
->instance
)))
3674 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3677 local_irq_restore(flags
);
3679 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3681 static void print_held_locks_bug(struct task_struct
*curr
)
3683 if (!debug_locks_off())
3685 if (debug_locks_silent
)
3688 printk("\n=====================================\n");
3689 printk( "[ BUG: lock held at task exit time! ]\n");
3690 printk( "-------------------------------------\n");
3691 printk("%s/%d is exiting with locks still held!\n",
3692 curr
->comm
, task_pid_nr(curr
));
3693 lockdep_print_held_locks(curr
);
3695 printk("\nstack backtrace:\n");
3699 void debug_check_no_locks_held(struct task_struct
*task
)
3701 if (unlikely(task
->lockdep_depth
> 0))
3702 print_held_locks_bug(task
);
3705 void debug_show_all_locks(void)
3707 struct task_struct
*g
, *p
;
3711 if (unlikely(!debug_locks
)) {
3712 printk("INFO: lockdep is turned off.\n");
3715 printk("\nShowing all locks held in the system:\n");
3718 * Here we try to get the tasklist_lock as hard as possible,
3719 * if not successful after 2 seconds we ignore it (but keep
3720 * trying). This is to enable a debug printout even if a
3721 * tasklist_lock-holding task deadlocks or crashes.
3724 if (!read_trylock(&tasklist_lock
)) {
3726 printk("hm, tasklist_lock locked, retrying... ");
3729 printk(" #%d", 10-count
);
3733 printk(" ignoring it.\n");
3737 printk(KERN_CONT
" locked it.\n");
3740 do_each_thread(g
, p
) {
3742 * It's not reliable to print a task's held locks
3743 * if it's not sleeping (or if it's not the current
3746 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3748 if (p
->lockdep_depth
)
3749 lockdep_print_held_locks(p
);
3751 if (read_trylock(&tasklist_lock
))
3753 } while_each_thread(g
, p
);
3756 printk("=============================================\n\n");
3759 read_unlock(&tasklist_lock
);
3761 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3764 * Careful: only use this function if you are sure that
3765 * the task cannot run in parallel!
3767 void __debug_show_held_locks(struct task_struct
*task
)
3769 if (unlikely(!debug_locks
)) {
3770 printk("INFO: lockdep is turned off.\n");
3773 lockdep_print_held_locks(task
);
3775 EXPORT_SYMBOL_GPL(__debug_show_held_locks
);
3777 void debug_show_held_locks(struct task_struct
*task
)
3779 __debug_show_held_locks(task
);
3781 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3783 void lockdep_sys_exit(void)
3785 struct task_struct
*curr
= current
;
3787 if (unlikely(curr
->lockdep_depth
)) {
3788 if (!debug_locks_off())
3790 printk("\n================================================\n");
3791 printk( "[ BUG: lock held when returning to user space! ]\n");
3792 printk( "------------------------------------------------\n");
3793 printk("%s/%d is leaving the kernel with locks still held!\n",
3794 curr
->comm
, curr
->pid
);
3795 lockdep_print_held_locks(curr
);
3799 void lockdep_rcu_dereference(const char *file
, const int line
)
3801 struct task_struct
*curr
= current
;
3803 if (!debug_locks_off())
3805 printk("\n===================================================\n");
3806 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
3807 printk( "---------------------------------------------------\n");
3808 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
3810 printk("\nother info that might help us debug this:\n\n");
3811 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active
, debug_locks
);
3812 lockdep_print_held_locks(curr
);
3813 printk("\nstack backtrace:\n");
3816 EXPORT_SYMBOL_GPL(lockdep_rcu_dereference
);