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>
46 #include <linux/gfp.h>
47 #include <linux/kmemcheck.h>
49 #include <asm/sections.h>
51 #include "lockdep_internals.h"
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/lock.h>
56 #ifdef CONFIG_PROVE_LOCKING
57 int prove_locking
= 1;
58 module_param(prove_locking
, int, 0644);
60 #define prove_locking 0
63 #ifdef CONFIG_LOCK_STAT
65 module_param(lock_stat
, int, 0644);
71 * lockdep_lock: protects the lockdep graph, the hashes and the
72 * class/list/hash allocators.
74 * This is one of the rare exceptions where it's justified
75 * to use a raw spinlock - we really dont want the spinlock
76 * code to recurse back into the lockdep code...
78 static arch_spinlock_t lockdep_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
80 static int graph_lock(void)
82 arch_spin_lock(&lockdep_lock
);
84 * Make sure that if another CPU detected a bug while
85 * walking the graph we dont change it (while the other
86 * CPU is busy printing out stuff with the graph lock
90 arch_spin_unlock(&lockdep_lock
);
93 /* prevent any recursions within lockdep from causing deadlocks */
94 current
->lockdep_recursion
++;
98 static inline int graph_unlock(void)
100 if (debug_locks
&& !arch_spin_is_locked(&lockdep_lock
))
101 return DEBUG_LOCKS_WARN_ON(1);
103 current
->lockdep_recursion
--;
104 arch_spin_unlock(&lockdep_lock
);
109 * Turn lock debugging off and return with 0 if it was off already,
110 * and also release the graph lock:
112 static inline int debug_locks_off_graph_unlock(void)
114 int ret
= debug_locks_off();
116 arch_spin_unlock(&lockdep_lock
);
121 static int lockdep_initialized
;
123 unsigned long nr_list_entries
;
124 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
127 * All data structures here are protected by the global debug_lock.
129 * Mutex key structs only get allocated, once during bootup, and never
130 * get freed - this significantly simplifies the debugging code.
132 unsigned long nr_lock_classes
;
133 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
135 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
137 if (!hlock
->class_idx
) {
138 DEBUG_LOCKS_WARN_ON(1);
141 return lock_classes
+ hlock
->class_idx
- 1;
144 #ifdef CONFIG_LOCK_STAT
145 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
],
148 static inline u64
lockstat_clock(void)
150 return local_clock();
153 static int lock_point(unsigned long points
[], unsigned long ip
)
157 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
158 if (points
[i
] == 0) {
169 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
174 if (time
< lt
->min
|| !lt
->nr
)
181 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
186 if (src
->max
> dst
->max
)
189 if (src
->min
< dst
->min
|| !dst
->nr
)
192 dst
->total
+= src
->total
;
196 struct lock_class_stats
lock_stats(struct lock_class
*class)
198 struct lock_class_stats stats
;
201 memset(&stats
, 0, sizeof(struct lock_class_stats
));
202 for_each_possible_cpu(cpu
) {
203 struct lock_class_stats
*pcs
=
204 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
206 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
207 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
209 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
210 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
212 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
213 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
215 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
216 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
218 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
219 stats
.bounces
[i
] += pcs
->bounces
[i
];
225 void clear_lock_stats(struct lock_class
*class)
229 for_each_possible_cpu(cpu
) {
230 struct lock_class_stats
*cpu_stats
=
231 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
233 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
235 memset(class->contention_point
, 0, sizeof(class->contention_point
));
236 memset(class->contending_point
, 0, sizeof(class->contending_point
));
239 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
241 return &get_cpu_var(cpu_lock_stats
)[class - lock_classes
];
244 static void put_lock_stats(struct lock_class_stats
*stats
)
246 put_cpu_var(cpu_lock_stats
);
249 static void lock_release_holdtime(struct held_lock
*hlock
)
251 struct lock_class_stats
*stats
;
257 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
259 stats
= get_lock_stats(hlock_class(hlock
));
261 lock_time_inc(&stats
->read_holdtime
, holdtime
);
263 lock_time_inc(&stats
->write_holdtime
, holdtime
);
264 put_lock_stats(stats
);
267 static inline void lock_release_holdtime(struct held_lock
*hlock
)
273 * We keep a global list of all lock classes. The list only grows,
274 * never shrinks. The list is only accessed with the lockdep
275 * spinlock lock held.
277 LIST_HEAD(all_lock_classes
);
280 * The lockdep classes are in a hash-table as well, for fast lookup:
282 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
283 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
284 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
285 #define classhashentry(key) (classhash_table + __classhashfn((key)))
287 static struct list_head classhash_table
[CLASSHASH_SIZE
];
290 * We put the lock dependency chains into a hash-table as well, to cache
293 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
294 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
295 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
296 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
298 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
301 * The hash key of the lock dependency chains is a hash itself too:
302 * it's a hash of all locks taken up to that lock, including that lock.
303 * It's a 64-bit hash, because it's important for the keys to be
306 #define iterate_chain_key(key1, key2) \
307 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
308 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
311 void lockdep_off(void)
313 current
->lockdep_recursion
++;
315 EXPORT_SYMBOL(lockdep_off
);
317 void lockdep_on(void)
319 current
->lockdep_recursion
--;
321 EXPORT_SYMBOL(lockdep_on
);
324 * Debugging switches:
328 #define VERY_VERBOSE 0
331 # define HARDIRQ_VERBOSE 1
332 # define SOFTIRQ_VERBOSE 1
333 # define RECLAIM_VERBOSE 1
335 # define HARDIRQ_VERBOSE 0
336 # define SOFTIRQ_VERBOSE 0
337 # define RECLAIM_VERBOSE 0
340 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
342 * Quick filtering for interesting events:
344 static int class_filter(struct lock_class
*class)
348 if (class->name_version
== 1 &&
349 !strcmp(class->name
, "lockname"))
351 if (class->name_version
== 1 &&
352 !strcmp(class->name
, "&struct->lockfield"))
355 /* Filter everything else. 1 would be to allow everything else */
360 static int verbose(struct lock_class
*class)
363 return class_filter(class);
369 * Stack-trace: tightly packed array of stack backtrace
370 * addresses. Protected by the graph_lock.
372 unsigned long nr_stack_trace_entries
;
373 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
375 static int save_trace(struct stack_trace
*trace
)
377 trace
->nr_entries
= 0;
378 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
379 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
383 save_stack_trace(trace
);
386 * Some daft arches put -1 at the end to indicate its a full trace.
388 * <rant> this is buggy anyway, since it takes a whole extra entry so a
389 * complete trace that maxes out the entries provided will be reported
390 * as incomplete, friggin useless </rant>
392 if (trace
->nr_entries
!= 0 &&
393 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
396 trace
->max_entries
= trace
->nr_entries
;
398 nr_stack_trace_entries
+= trace
->nr_entries
;
400 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
401 if (!debug_locks_off_graph_unlock())
404 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
405 printk("turning off the locking correctness validator.\n");
414 unsigned int nr_hardirq_chains
;
415 unsigned int nr_softirq_chains
;
416 unsigned int nr_process_chains
;
417 unsigned int max_lockdep_depth
;
419 #ifdef CONFIG_DEBUG_LOCKDEP
421 * We cannot printk in early bootup code. Not even early_printk()
422 * might work. So we mark any initialization errors and printk
423 * about it later on, in lockdep_info().
425 static int lockdep_init_error
;
426 static unsigned long lockdep_init_trace_data
[20];
427 static struct stack_trace lockdep_init_trace
= {
428 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
429 .entries
= lockdep_init_trace_data
,
433 * Various lockdep statistics:
435 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
442 #define __USAGE(__STATE) \
443 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
444 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
445 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
446 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
448 static const char *usage_str
[] =
450 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
451 #include "lockdep_states.h"
453 [LOCK_USED
] = "INITIAL USE",
456 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
458 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
461 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
466 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
470 if (class->usage_mask
& lock_flag(bit
+ 2))
472 if (class->usage_mask
& lock_flag(bit
)) {
474 if (class->usage_mask
& lock_flag(bit
+ 2))
481 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
485 #define LOCKDEP_STATE(__STATE) \
486 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
488 #include "lockdep_states.h"
494 static int __print_lock_name(struct lock_class
*class)
496 char str
[KSYM_NAME_LEN
];
501 name
= __get_key_name(class->key
, str
);
503 return printk("%s", name
);
506 static void print_lock_name(struct lock_class
*class)
508 char str
[KSYM_NAME_LEN
], usage
[LOCK_USAGE_CHARS
];
511 get_usage_chars(class, usage
);
515 name
= __get_key_name(class->key
, str
);
516 printk(" (%s", name
);
518 printk(" (%s", name
);
519 if (class->name_version
> 1)
520 printk("#%d", class->name_version
);
522 printk("/%d", class->subclass
);
524 printk("){%s}", usage
);
527 static void print_lockdep_cache(struct lockdep_map
*lock
)
530 char str
[KSYM_NAME_LEN
];
534 name
= __get_key_name(lock
->key
->subkeys
, str
);
539 static void print_lock(struct held_lock
*hlock
)
541 print_lock_name(hlock_class(hlock
));
543 print_ip_sym(hlock
->acquire_ip
);
546 static void lockdep_print_held_locks(struct task_struct
*curr
)
548 int i
, depth
= curr
->lockdep_depth
;
551 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
554 printk("%d lock%s held by %s/%d:\n",
555 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
557 for (i
= 0; i
< depth
; i
++) {
559 print_lock(curr
->held_locks
+ i
);
563 static void print_kernel_version(void)
565 printk("%s %.*s\n", init_utsname()->release
,
566 (int)strcspn(init_utsname()->version
, " "),
567 init_utsname()->version
);
570 static int very_verbose(struct lock_class
*class)
573 return class_filter(class);
579 * Is this the address of a static object:
581 static int static_obj(void *obj
)
583 unsigned long start
= (unsigned long) &_stext
,
584 end
= (unsigned long) &_end
,
585 addr
= (unsigned long) obj
;
590 if ((addr
>= start
) && (addr
< end
))
593 if (arch_is_kernel_data(addr
))
597 * in-kernel percpu var?
599 if (is_kernel_percpu_address(addr
))
603 * module static or percpu var?
605 return is_module_address(addr
) || is_module_percpu_address(addr
);
609 * To make lock name printouts unique, we calculate a unique
610 * class->name_version generation counter:
612 static int count_matching_names(struct lock_class
*new_class
)
614 struct lock_class
*class;
617 if (!new_class
->name
)
620 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
621 if (new_class
->key
- new_class
->subclass
== class->key
)
622 return class->name_version
;
623 if (class->name
&& !strcmp(class->name
, new_class
->name
))
624 count
= max(count
, class->name_version
);
631 * Register a lock's class in the hash-table, if the class is not present
632 * yet. Otherwise we look it up. We cache the result in the lock object
633 * itself, so actual lookup of the hash should be once per lock object.
635 static inline struct lock_class
*
636 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
638 struct lockdep_subclass_key
*key
;
639 struct list_head
*hash_head
;
640 struct lock_class
*class;
642 #ifdef CONFIG_DEBUG_LOCKDEP
644 * If the architecture calls into lockdep before initializing
645 * the hashes then we'll warn about it later. (we cannot printk
648 if (unlikely(!lockdep_initialized
)) {
650 lockdep_init_error
= 1;
651 save_stack_trace(&lockdep_init_trace
);
655 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
658 "BUG: looking up invalid subclass: %u\n", subclass
);
660 "turning off the locking correctness validator.\n");
666 * Static locks do not have their class-keys yet - for them the key
667 * is the lock object itself:
669 if (unlikely(!lock
->key
))
670 lock
->key
= (void *)lock
;
673 * NOTE: the class-key must be unique. For dynamic locks, a static
674 * lock_class_key variable is passed in through the mutex_init()
675 * (or spin_lock_init()) call - which acts as the key. For static
676 * locks we use the lock object itself as the key.
678 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
679 sizeof(struct lockdep_map
));
681 key
= lock
->key
->subkeys
+ subclass
;
683 hash_head
= classhashentry(key
);
686 * We can walk the hash lockfree, because the hash only
687 * grows, and we are careful when adding entries to the end:
689 list_for_each_entry(class, hash_head
, hash_entry
) {
690 if (class->key
== key
) {
691 WARN_ON_ONCE(class->name
!= lock
->name
);
700 * Register a lock's class in the hash-table, if the class is not present
701 * yet. Otherwise we look it up. We cache the result in the lock object
702 * itself, so actual lookup of the hash should be once per lock object.
704 static inline struct lock_class
*
705 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
707 struct lockdep_subclass_key
*key
;
708 struct list_head
*hash_head
;
709 struct lock_class
*class;
712 class = look_up_lock_class(lock
, subclass
);
717 * Debug-check: all keys must be persistent!
719 if (!static_obj(lock
->key
)) {
721 printk("INFO: trying to register non-static key.\n");
722 printk("the code is fine but needs lockdep annotation.\n");
723 printk("turning off the locking correctness validator.\n");
729 key
= lock
->key
->subkeys
+ subclass
;
730 hash_head
= classhashentry(key
);
732 raw_local_irq_save(flags
);
734 raw_local_irq_restore(flags
);
738 * We have to do the hash-walk again, to avoid races
741 list_for_each_entry(class, hash_head
, hash_entry
)
742 if (class->key
== key
)
745 * Allocate a new key from the static array, and add it to
748 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
749 if (!debug_locks_off_graph_unlock()) {
750 raw_local_irq_restore(flags
);
753 raw_local_irq_restore(flags
);
755 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
756 printk("turning off the locking correctness validator.\n");
760 class = lock_classes
+ nr_lock_classes
++;
761 debug_atomic_inc(nr_unused_locks
);
763 class->name
= lock
->name
;
764 class->subclass
= subclass
;
765 INIT_LIST_HEAD(&class->lock_entry
);
766 INIT_LIST_HEAD(&class->locks_before
);
767 INIT_LIST_HEAD(&class->locks_after
);
768 class->name_version
= count_matching_names(class);
770 * We use RCU's safe list-add method to make
771 * parallel walking of the hash-list safe:
773 list_add_tail_rcu(&class->hash_entry
, hash_head
);
775 * Add it to the global list of classes:
777 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
779 if (verbose(class)) {
781 raw_local_irq_restore(flags
);
783 printk("\nnew class %p: %s", class->key
, class->name
);
784 if (class->name_version
> 1)
785 printk("#%d", class->name_version
);
789 raw_local_irq_save(flags
);
791 raw_local_irq_restore(flags
);
797 raw_local_irq_restore(flags
);
799 if (!subclass
|| force
)
800 lock
->class_cache
[0] = class;
801 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
802 lock
->class_cache
[subclass
] = class;
804 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
810 #ifdef CONFIG_PROVE_LOCKING
812 * Allocate a lockdep entry. (assumes the graph_lock held, returns
813 * with NULL on failure)
815 static struct lock_list
*alloc_list_entry(void)
817 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
818 if (!debug_locks_off_graph_unlock())
821 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
822 printk("turning off the locking correctness validator.\n");
826 return list_entries
+ nr_list_entries
++;
830 * Add a new dependency to the head of the list:
832 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
833 struct list_head
*head
, unsigned long ip
,
834 int distance
, struct stack_trace
*trace
)
836 struct lock_list
*entry
;
838 * Lock not present yet - get a new dependency struct and
839 * add it to the list:
841 entry
= alloc_list_entry();
846 entry
->distance
= distance
;
847 entry
->trace
= *trace
;
849 * Since we never remove from the dependency list, the list can
850 * be walked lockless by other CPUs, it's only allocation
851 * that must be protected by the spinlock. But this also means
852 * we must make new entries visible only once writes to the
853 * entry become visible - hence the RCU op:
855 list_add_tail_rcu(&entry
->entry
, head
);
861 * For good efficiency of modular, we use power of 2
863 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
864 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
867 * The circular_queue and helpers is used to implement the
868 * breadth-first search(BFS)algorithem, by which we can build
869 * the shortest path from the next lock to be acquired to the
870 * previous held lock if there is a circular between them.
872 struct circular_queue
{
873 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
874 unsigned int front
, rear
;
877 static struct circular_queue lock_cq
;
879 unsigned int max_bfs_queue_depth
;
881 static unsigned int lockdep_dependency_gen_id
;
883 static inline void __cq_init(struct circular_queue
*cq
)
885 cq
->front
= cq
->rear
= 0;
886 lockdep_dependency_gen_id
++;
889 static inline int __cq_empty(struct circular_queue
*cq
)
891 return (cq
->front
== cq
->rear
);
894 static inline int __cq_full(struct circular_queue
*cq
)
896 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
899 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
904 cq
->element
[cq
->rear
] = elem
;
905 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
909 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
914 *elem
= cq
->element
[cq
->front
];
915 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
919 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
921 return (cq
->rear
- cq
->front
) & CQ_MASK
;
924 static inline void mark_lock_accessed(struct lock_list
*lock
,
925 struct lock_list
*parent
)
929 nr
= lock
- list_entries
;
930 WARN_ON(nr
>= nr_list_entries
);
931 lock
->parent
= parent
;
932 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
935 static inline unsigned long lock_accessed(struct lock_list
*lock
)
939 nr
= lock
- list_entries
;
940 WARN_ON(nr
>= nr_list_entries
);
941 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
944 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
946 return child
->parent
;
949 static inline int get_lock_depth(struct lock_list
*child
)
952 struct lock_list
*parent
;
954 while ((parent
= get_lock_parent(child
))) {
961 static int __bfs(struct lock_list
*source_entry
,
963 int (*match
)(struct lock_list
*entry
, void *data
),
964 struct lock_list
**target_entry
,
967 struct lock_list
*entry
;
968 struct list_head
*head
;
969 struct circular_queue
*cq
= &lock_cq
;
972 if (match(source_entry
, data
)) {
973 *target_entry
= source_entry
;
979 head
= &source_entry
->class->locks_after
;
981 head
= &source_entry
->class->locks_before
;
983 if (list_empty(head
))
987 __cq_enqueue(cq
, (unsigned long)source_entry
);
989 while (!__cq_empty(cq
)) {
990 struct lock_list
*lock
;
992 __cq_dequeue(cq
, (unsigned long *)&lock
);
1000 head
= &lock
->class->locks_after
;
1002 head
= &lock
->class->locks_before
;
1004 list_for_each_entry(entry
, head
, entry
) {
1005 if (!lock_accessed(entry
)) {
1006 unsigned int cq_depth
;
1007 mark_lock_accessed(entry
, lock
);
1008 if (match(entry
, data
)) {
1009 *target_entry
= entry
;
1014 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1018 cq_depth
= __cq_get_elem_count(cq
);
1019 if (max_bfs_queue_depth
< cq_depth
)
1020 max_bfs_queue_depth
= cq_depth
;
1028 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1030 int (*match
)(struct lock_list
*entry
, void *data
),
1031 struct lock_list
**target_entry
)
1033 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1037 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1039 int (*match
)(struct lock_list
*entry
, void *data
),
1040 struct lock_list
**target_entry
)
1042 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1047 * Recursive, forwards-direction lock-dependency checking, used for
1048 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1053 * Print a dependency chain entry (this is only done when a deadlock
1054 * has been detected):
1057 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1059 if (debug_locks_silent
)
1061 printk("\n-> #%u", depth
);
1062 print_lock_name(target
->class);
1064 print_stack_trace(&target
->trace
, 6);
1070 print_circular_lock_scenario(struct held_lock
*src
,
1071 struct held_lock
*tgt
,
1072 struct lock_list
*prt
)
1074 struct lock_class
*source
= hlock_class(src
);
1075 struct lock_class
*target
= hlock_class(tgt
);
1076 struct lock_class
*parent
= prt
->class;
1079 * A direct locking problem where unsafe_class lock is taken
1080 * directly by safe_class lock, then all we need to show
1081 * is the deadlock scenario, as it is obvious that the
1082 * unsafe lock is taken under the safe lock.
1084 * But if there is a chain instead, where the safe lock takes
1085 * an intermediate lock (middle_class) where this lock is
1086 * not the same as the safe lock, then the lock chain is
1087 * used to describe the problem. Otherwise we would need
1088 * to show a different CPU case for each link in the chain
1089 * from the safe_class lock to the unsafe_class lock.
1091 if (parent
!= source
) {
1092 printk("Chain exists of:\n ");
1093 __print_lock_name(source
);
1095 __print_lock_name(parent
);
1097 __print_lock_name(target
);
1101 printk(" Possible unsafe locking scenario:\n\n");
1102 printk(" CPU0 CPU1\n");
1103 printk(" ---- ----\n");
1105 __print_lock_name(target
);
1108 __print_lock_name(parent
);
1111 __print_lock_name(target
);
1114 __print_lock_name(source
);
1116 printk("\n *** DEADLOCK ***\n\n");
1120 * When a circular dependency is detected, print the
1124 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1125 struct held_lock
*check_src
,
1126 struct held_lock
*check_tgt
)
1128 struct task_struct
*curr
= current
;
1130 if (debug_locks_silent
)
1133 printk("\n=======================================================\n");
1134 printk( "[ INFO: possible circular locking dependency detected ]\n");
1135 print_kernel_version();
1136 printk( "-------------------------------------------------------\n");
1137 printk("%s/%d is trying to acquire lock:\n",
1138 curr
->comm
, task_pid_nr(curr
));
1139 print_lock(check_src
);
1140 printk("\nbut task is already holding lock:\n");
1141 print_lock(check_tgt
);
1142 printk("\nwhich lock already depends on the new lock.\n\n");
1143 printk("\nthe existing dependency chain (in reverse order) is:\n");
1145 print_circular_bug_entry(entry
, depth
);
1150 static inline int class_equal(struct lock_list
*entry
, void *data
)
1152 return entry
->class == data
;
1155 static noinline
int print_circular_bug(struct lock_list
*this,
1156 struct lock_list
*target
,
1157 struct held_lock
*check_src
,
1158 struct held_lock
*check_tgt
)
1160 struct task_struct
*curr
= current
;
1161 struct lock_list
*parent
;
1162 struct lock_list
*first_parent
;
1165 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1168 if (!save_trace(&this->trace
))
1171 depth
= get_lock_depth(target
);
1173 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1175 parent
= get_lock_parent(target
);
1176 first_parent
= parent
;
1179 print_circular_bug_entry(parent
, --depth
);
1180 parent
= get_lock_parent(parent
);
1183 printk("\nother info that might help us debug this:\n\n");
1184 print_circular_lock_scenario(check_src
, check_tgt
,
1187 lockdep_print_held_locks(curr
);
1189 printk("\nstack backtrace:\n");
1195 static noinline
int print_bfs_bug(int ret
)
1197 if (!debug_locks_off_graph_unlock())
1200 WARN(1, "lockdep bfs error:%d\n", ret
);
1205 static int noop_count(struct lock_list
*entry
, void *data
)
1207 (*(unsigned long *)data
)++;
1211 unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1213 unsigned long count
= 0;
1214 struct lock_list
*uninitialized_var(target_entry
);
1216 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1220 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1222 unsigned long ret
, flags
;
1223 struct lock_list
this;
1228 local_irq_save(flags
);
1229 arch_spin_lock(&lockdep_lock
);
1230 ret
= __lockdep_count_forward_deps(&this);
1231 arch_spin_unlock(&lockdep_lock
);
1232 local_irq_restore(flags
);
1237 unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1239 unsigned long count
= 0;
1240 struct lock_list
*uninitialized_var(target_entry
);
1242 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1247 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1249 unsigned long ret
, flags
;
1250 struct lock_list
this;
1255 local_irq_save(flags
);
1256 arch_spin_lock(&lockdep_lock
);
1257 ret
= __lockdep_count_backward_deps(&this);
1258 arch_spin_unlock(&lockdep_lock
);
1259 local_irq_restore(flags
);
1265 * Prove that the dependency graph starting at <entry> can not
1266 * lead to <target>. Print an error and return 0 if it does.
1269 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1270 struct lock_list
**target_entry
)
1274 debug_atomic_inc(nr_cyclic_checks
);
1276 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1281 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1283 * Forwards and backwards subgraph searching, for the purposes of
1284 * proving that two subgraphs can be connected by a new dependency
1285 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1288 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1290 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1296 * Find a node in the forwards-direction dependency sub-graph starting
1297 * at @root->class that matches @bit.
1299 * Return 0 if such a node exists in the subgraph, and put that node
1300 * into *@target_entry.
1302 * Return 1 otherwise and keep *@target_entry unchanged.
1303 * Return <0 on error.
1306 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1307 struct lock_list
**target_entry
)
1311 debug_atomic_inc(nr_find_usage_forwards_checks
);
1313 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1319 * Find a node in the backwards-direction dependency sub-graph starting
1320 * at @root->class that matches @bit.
1322 * Return 0 if such a node exists in the subgraph, and put that node
1323 * into *@target_entry.
1325 * Return 1 otherwise and keep *@target_entry unchanged.
1326 * Return <0 on error.
1329 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1330 struct lock_list
**target_entry
)
1334 debug_atomic_inc(nr_find_usage_backwards_checks
);
1336 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1341 static void print_lock_class_header(struct lock_class
*class, int depth
)
1345 printk("%*s->", depth
, "");
1346 print_lock_name(class);
1347 printk(" ops: %lu", class->ops
);
1350 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1351 if (class->usage_mask
& (1 << bit
)) {
1354 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1355 len
+= printk(" at:\n");
1356 print_stack_trace(class->usage_traces
+ bit
, len
);
1359 printk("%*s }\n", depth
, "");
1361 printk("%*s ... key at: ",depth
,"");
1362 print_ip_sym((unsigned long)class->key
);
1366 * printk the shortest lock dependencies from @start to @end in reverse order:
1369 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1370 struct lock_list
*root
)
1372 struct lock_list
*entry
= leaf
;
1375 /*compute depth from generated tree by BFS*/
1376 depth
= get_lock_depth(leaf
);
1379 print_lock_class_header(entry
->class, depth
);
1380 printk("%*s ... acquired at:\n", depth
, "");
1381 print_stack_trace(&entry
->trace
, 2);
1384 if (depth
== 0 && (entry
!= root
)) {
1385 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1389 entry
= get_lock_parent(entry
);
1391 } while (entry
&& (depth
>= 0));
1397 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1398 struct lock_list
*unsafe_entry
,
1399 struct lock_class
*prev_class
,
1400 struct lock_class
*next_class
)
1402 struct lock_class
*safe_class
= safe_entry
->class;
1403 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1404 struct lock_class
*middle_class
= prev_class
;
1406 if (middle_class
== safe_class
)
1407 middle_class
= next_class
;
1410 * A direct locking problem where unsafe_class lock is taken
1411 * directly by safe_class lock, then all we need to show
1412 * is the deadlock scenario, as it is obvious that the
1413 * unsafe lock is taken under the safe lock.
1415 * But if there is a chain instead, where the safe lock takes
1416 * an intermediate lock (middle_class) where this lock is
1417 * not the same as the safe lock, then the lock chain is
1418 * used to describe the problem. Otherwise we would need
1419 * to show a different CPU case for each link in the chain
1420 * from the safe_class lock to the unsafe_class lock.
1422 if (middle_class
!= unsafe_class
) {
1423 printk("Chain exists of:\n ");
1424 __print_lock_name(safe_class
);
1426 __print_lock_name(middle_class
);
1428 __print_lock_name(unsafe_class
);
1432 printk(" Possible interrupt unsafe locking scenario:\n\n");
1433 printk(" CPU0 CPU1\n");
1434 printk(" ---- ----\n");
1436 __print_lock_name(unsafe_class
);
1438 printk(" local_irq_disable();\n");
1440 __print_lock_name(safe_class
);
1443 __print_lock_name(middle_class
);
1445 printk(" <Interrupt>\n");
1447 __print_lock_name(safe_class
);
1449 printk("\n *** DEADLOCK ***\n\n");
1453 print_bad_irq_dependency(struct task_struct
*curr
,
1454 struct lock_list
*prev_root
,
1455 struct lock_list
*next_root
,
1456 struct lock_list
*backwards_entry
,
1457 struct lock_list
*forwards_entry
,
1458 struct held_lock
*prev
,
1459 struct held_lock
*next
,
1460 enum lock_usage_bit bit1
,
1461 enum lock_usage_bit bit2
,
1462 const char *irqclass
)
1464 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1467 printk("\n======================================================\n");
1468 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1469 irqclass
, irqclass
);
1470 print_kernel_version();
1471 printk( "------------------------------------------------------\n");
1472 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1473 curr
->comm
, task_pid_nr(curr
),
1474 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1475 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1476 curr
->hardirqs_enabled
,
1477 curr
->softirqs_enabled
);
1480 printk("\nand this task is already holding:\n");
1482 printk("which would create a new lock dependency:\n");
1483 print_lock_name(hlock_class(prev
));
1485 print_lock_name(hlock_class(next
));
1488 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1490 print_lock_name(backwards_entry
->class);
1491 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1493 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1495 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1496 print_lock_name(forwards_entry
->class);
1497 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1500 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1502 printk("\nother info that might help us debug this:\n\n");
1503 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1504 hlock_class(prev
), hlock_class(next
));
1506 lockdep_print_held_locks(curr
);
1508 printk("\nthe dependencies between %s-irq-safe lock", irqclass
);
1509 printk(" and the holding lock:\n");
1510 if (!save_trace(&prev_root
->trace
))
1512 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1514 printk("\nthe dependencies between the lock to be acquired");
1515 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1516 if (!save_trace(&next_root
->trace
))
1518 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1520 printk("\nstack backtrace:\n");
1527 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1528 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1529 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1532 struct lock_list
this, that
;
1533 struct lock_list
*uninitialized_var(target_entry
);
1534 struct lock_list
*uninitialized_var(target_entry1
);
1538 this.class = hlock_class(prev
);
1539 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1541 return print_bfs_bug(ret
);
1546 that
.class = hlock_class(next
);
1547 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1549 return print_bfs_bug(ret
);
1553 return print_bad_irq_dependency(curr
, &this, &that
,
1554 target_entry
, target_entry1
,
1556 bit_backwards
, bit_forwards
, irqclass
);
1559 static const char *state_names
[] = {
1560 #define LOCKDEP_STATE(__STATE) \
1561 __stringify(__STATE),
1562 #include "lockdep_states.h"
1563 #undef LOCKDEP_STATE
1566 static const char *state_rnames
[] = {
1567 #define LOCKDEP_STATE(__STATE) \
1568 __stringify(__STATE)"-READ",
1569 #include "lockdep_states.h"
1570 #undef LOCKDEP_STATE
1573 static inline const char *state_name(enum lock_usage_bit bit
)
1575 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1578 static int exclusive_bit(int new_bit
)
1586 * bit 0 - write/read
1587 * bit 1 - used_in/enabled
1591 int state
= new_bit
& ~3;
1592 int dir
= new_bit
& 2;
1595 * keep state, bit flip the direction and strip read.
1597 return state
| (dir
^ 2);
1600 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1601 struct held_lock
*next
, enum lock_usage_bit bit
)
1604 * Prove that the new dependency does not connect a hardirq-safe
1605 * lock with a hardirq-unsafe lock - to achieve this we search
1606 * the backwards-subgraph starting at <prev>, and the
1607 * forwards-subgraph starting at <next>:
1609 if (!check_usage(curr
, prev
, next
, bit
,
1610 exclusive_bit(bit
), state_name(bit
)))
1616 * Prove that the new dependency does not connect a hardirq-safe-read
1617 * lock with a hardirq-unsafe lock - to achieve this we search
1618 * the backwards-subgraph starting at <prev>, and the
1619 * forwards-subgraph starting at <next>:
1621 if (!check_usage(curr
, prev
, next
, bit
,
1622 exclusive_bit(bit
), state_name(bit
)))
1629 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1630 struct held_lock
*next
)
1632 #define LOCKDEP_STATE(__STATE) \
1633 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1635 #include "lockdep_states.h"
1636 #undef LOCKDEP_STATE
1641 static void inc_chains(void)
1643 if (current
->hardirq_context
)
1644 nr_hardirq_chains
++;
1646 if (current
->softirq_context
)
1647 nr_softirq_chains
++;
1649 nr_process_chains
++;
1656 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1657 struct held_lock
*next
)
1662 static inline void inc_chains(void)
1664 nr_process_chains
++;
1670 print_deadlock_scenario(struct held_lock
*nxt
,
1671 struct held_lock
*prv
)
1673 struct lock_class
*next
= hlock_class(nxt
);
1674 struct lock_class
*prev
= hlock_class(prv
);
1676 printk(" Possible unsafe locking scenario:\n\n");
1680 __print_lock_name(prev
);
1683 __print_lock_name(next
);
1685 printk("\n *** DEADLOCK ***\n\n");
1686 printk(" May be due to missing lock nesting notation\n\n");
1690 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1691 struct held_lock
*next
)
1693 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1696 printk("\n=============================================\n");
1697 printk( "[ INFO: possible recursive locking detected ]\n");
1698 print_kernel_version();
1699 printk( "---------------------------------------------\n");
1700 printk("%s/%d is trying to acquire lock:\n",
1701 curr
->comm
, task_pid_nr(curr
));
1703 printk("\nbut task is already holding lock:\n");
1706 printk("\nother info that might help us debug this:\n");
1707 print_deadlock_scenario(next
, prev
);
1708 lockdep_print_held_locks(curr
);
1710 printk("\nstack backtrace:\n");
1717 * Check whether we are holding such a class already.
1719 * (Note that this has to be done separately, because the graph cannot
1720 * detect such classes of deadlocks.)
1722 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1725 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1726 struct lockdep_map
*next_instance
, int read
)
1728 struct held_lock
*prev
;
1729 struct held_lock
*nest
= NULL
;
1732 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1733 prev
= curr
->held_locks
+ i
;
1735 if (prev
->instance
== next
->nest_lock
)
1738 if (hlock_class(prev
) != hlock_class(next
))
1742 * Allow read-after-read recursion of the same
1743 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1745 if ((read
== 2) && prev
->read
)
1749 * We're holding the nest_lock, which serializes this lock's
1750 * nesting behaviour.
1755 return print_deadlock_bug(curr
, prev
, next
);
1761 * There was a chain-cache miss, and we are about to add a new dependency
1762 * to a previous lock. We recursively validate the following rules:
1764 * - would the adding of the <prev> -> <next> dependency create a
1765 * circular dependency in the graph? [== circular deadlock]
1767 * - does the new prev->next dependency connect any hardirq-safe lock
1768 * (in the full backwards-subgraph starting at <prev>) with any
1769 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1770 * <next>)? [== illegal lock inversion with hardirq contexts]
1772 * - does the new prev->next dependency connect any softirq-safe lock
1773 * (in the full backwards-subgraph starting at <prev>) with any
1774 * softirq-unsafe lock (in the full forwards-subgraph starting at
1775 * <next>)? [== illegal lock inversion with softirq contexts]
1777 * any of these scenarios could lead to a deadlock.
1779 * Then if all the validations pass, we add the forwards and backwards
1783 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1784 struct held_lock
*next
, int distance
, int trylock_loop
)
1786 struct lock_list
*entry
;
1788 struct lock_list
this;
1789 struct lock_list
*uninitialized_var(target_entry
);
1791 * Static variable, serialized by the graph_lock().
1793 * We use this static variable to save the stack trace in case
1794 * we call into this function multiple times due to encountering
1795 * trylocks in the held lock stack.
1797 static struct stack_trace trace
;
1800 * Prove that the new <prev> -> <next> dependency would not
1801 * create a circular dependency in the graph. (We do this by
1802 * forward-recursing into the graph starting at <next>, and
1803 * checking whether we can reach <prev>.)
1805 * We are using global variables to control the recursion, to
1806 * keep the stackframe size of the recursive functions low:
1808 this.class = hlock_class(next
);
1810 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1812 return print_circular_bug(&this, target_entry
, next
, prev
);
1813 else if (unlikely(ret
< 0))
1814 return print_bfs_bug(ret
);
1816 if (!check_prev_add_irq(curr
, prev
, next
))
1820 * For recursive read-locks we do all the dependency checks,
1821 * but we dont store read-triggered dependencies (only
1822 * write-triggered dependencies). This ensures that only the
1823 * write-side dependencies matter, and that if for example a
1824 * write-lock never takes any other locks, then the reads are
1825 * equivalent to a NOP.
1827 if (next
->read
== 2 || prev
->read
== 2)
1830 * Is the <prev> -> <next> dependency already present?
1832 * (this may occur even though this is a new chain: consider
1833 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1834 * chains - the second one will be new, but L1 already has
1835 * L2 added to its dependency list, due to the first chain.)
1837 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1838 if (entry
->class == hlock_class(next
)) {
1840 entry
->distance
= 1;
1845 if (!trylock_loop
&& !save_trace(&trace
))
1849 * Ok, all validations passed, add the new lock
1850 * to the previous lock's dependency list:
1852 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1853 &hlock_class(prev
)->locks_after
,
1854 next
->acquire_ip
, distance
, &trace
);
1859 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1860 &hlock_class(next
)->locks_before
,
1861 next
->acquire_ip
, distance
, &trace
);
1866 * Debugging printouts:
1868 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1870 printk("\n new dependency: ");
1871 print_lock_name(hlock_class(prev
));
1873 print_lock_name(hlock_class(next
));
1876 return graph_lock();
1882 * Add the dependency to all directly-previous locks that are 'relevant'.
1883 * The ones that are relevant are (in increasing distance from curr):
1884 * all consecutive trylock entries and the final non-trylock entry - or
1885 * the end of this context's lock-chain - whichever comes first.
1888 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1890 int depth
= curr
->lockdep_depth
;
1891 int trylock_loop
= 0;
1892 struct held_lock
*hlock
;
1897 * Depth must not be zero for a non-head lock:
1902 * At least two relevant locks must exist for this
1905 if (curr
->held_locks
[depth
].irq_context
!=
1906 curr
->held_locks
[depth
-1].irq_context
)
1910 int distance
= curr
->lockdep_depth
- depth
+ 1;
1911 hlock
= curr
->held_locks
+ depth
-1;
1913 * Only non-recursive-read entries get new dependencies
1916 if (hlock
->read
!= 2) {
1917 if (!check_prev_add(curr
, hlock
, next
,
1918 distance
, trylock_loop
))
1921 * Stop after the first non-trylock entry,
1922 * as non-trylock entries have added their
1923 * own direct dependencies already, so this
1924 * lock is connected to them indirectly:
1926 if (!hlock
->trylock
)
1931 * End of lock-stack?
1936 * Stop the search if we cross into another context:
1938 if (curr
->held_locks
[depth
].irq_context
!=
1939 curr
->held_locks
[depth
-1].irq_context
)
1945 if (!debug_locks_off_graph_unlock())
1953 unsigned long nr_lock_chains
;
1954 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1955 int nr_chain_hlocks
;
1956 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1958 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1960 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1964 * Look up a dependency chain. If the key is not present yet then
1965 * add it and return 1 - in this case the new dependency chain is
1966 * validated. If the key is already hashed, return 0.
1967 * (On return with 1 graph_lock is held.)
1969 static inline int lookup_chain_cache(struct task_struct
*curr
,
1970 struct held_lock
*hlock
,
1973 struct lock_class
*class = hlock_class(hlock
);
1974 struct list_head
*hash_head
= chainhashentry(chain_key
);
1975 struct lock_chain
*chain
;
1976 struct held_lock
*hlock_curr
, *hlock_next
;
1979 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1982 * We can walk it lock-free, because entries only get added
1985 list_for_each_entry(chain
, hash_head
, entry
) {
1986 if (chain
->chain_key
== chain_key
) {
1988 debug_atomic_inc(chain_lookup_hits
);
1989 if (very_verbose(class))
1990 printk("\nhash chain already cached, key: "
1991 "%016Lx tail class: [%p] %s\n",
1992 (unsigned long long)chain_key
,
1993 class->key
, class->name
);
1997 if (very_verbose(class))
1998 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1999 (unsigned long long)chain_key
, class->key
, class->name
);
2001 * Allocate a new chain entry from the static array, and add
2007 * We have to walk the chain again locked - to avoid duplicates:
2009 list_for_each_entry(chain
, hash_head
, entry
) {
2010 if (chain
->chain_key
== chain_key
) {
2015 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2016 if (!debug_locks_off_graph_unlock())
2019 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
2020 printk("turning off the locking correctness validator.\n");
2024 chain
= lock_chains
+ nr_lock_chains
++;
2025 chain
->chain_key
= chain_key
;
2026 chain
->irq_context
= hlock
->irq_context
;
2027 /* Find the first held_lock of current chain */
2029 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2030 hlock_curr
= curr
->held_locks
+ i
;
2031 if (hlock_curr
->irq_context
!= hlock_next
->irq_context
)
2036 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2037 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2038 chain
->base
= nr_chain_hlocks
;
2039 nr_chain_hlocks
+= chain
->depth
;
2040 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2041 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2042 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2044 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2046 list_add_tail_rcu(&chain
->entry
, hash_head
);
2047 debug_atomic_inc(chain_lookup_misses
);
2053 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2054 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2057 * Trylock needs to maintain the stack of held locks, but it
2058 * does not add new dependencies, because trylock can be done
2061 * We look up the chain_key and do the O(N^2) check and update of
2062 * the dependencies only if this is a new dependency chain.
2063 * (If lookup_chain_cache() returns with 1 it acquires
2064 * graph_lock for us)
2066 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
2067 lookup_chain_cache(curr
, hlock
, chain_key
)) {
2069 * Check whether last held lock:
2071 * - is irq-safe, if this lock is irq-unsafe
2072 * - is softirq-safe, if this lock is hardirq-unsafe
2074 * And check whether the new lock's dependency graph
2075 * could lead back to the previous lock.
2077 * any of these scenarios could lead to a deadlock. If
2080 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2085 * Mark recursive read, as we jump over it when
2086 * building dependencies (just like we jump over
2092 * Add dependency only if this lock is not the head
2093 * of the chain, and if it's not a secondary read-lock:
2095 if (!chain_head
&& ret
!= 2)
2096 if (!check_prevs_add(curr
, hlock
))
2100 /* after lookup_chain_cache(): */
2101 if (unlikely(!debug_locks
))
2107 static inline int validate_chain(struct task_struct
*curr
,
2108 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2109 int chain_head
, u64 chain_key
)
2116 * We are building curr_chain_key incrementally, so double-check
2117 * it from scratch, to make sure that it's done correctly:
2119 static void check_chain_key(struct task_struct
*curr
)
2121 #ifdef CONFIG_DEBUG_LOCKDEP
2122 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2126 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2127 hlock
= curr
->held_locks
+ i
;
2128 if (chain_key
!= hlock
->prev_chain_key
) {
2130 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2131 curr
->lockdep_depth
, i
,
2132 (unsigned long long)chain_key
,
2133 (unsigned long long)hlock
->prev_chain_key
);
2136 id
= hlock
->class_idx
- 1;
2137 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2140 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2141 hlock
->irq_context
))
2143 chain_key
= iterate_chain_key(chain_key
, id
);
2146 if (chain_key
!= curr
->curr_chain_key
) {
2148 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2149 curr
->lockdep_depth
, i
,
2150 (unsigned long long)chain_key
,
2151 (unsigned long long)curr
->curr_chain_key
);
2157 print_usage_bug_scenario(struct held_lock
*lock
)
2159 struct lock_class
*class = hlock_class(lock
);
2161 printk(" Possible unsafe locking scenario:\n\n");
2165 __print_lock_name(class);
2167 printk(" <Interrupt>\n");
2169 __print_lock_name(class);
2171 printk("\n *** DEADLOCK ***\n\n");
2175 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2176 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2178 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2181 printk("\n=================================\n");
2182 printk( "[ INFO: inconsistent lock state ]\n");
2183 print_kernel_version();
2184 printk( "---------------------------------\n");
2186 printk("inconsistent {%s} -> {%s} usage.\n",
2187 usage_str
[prev_bit
], usage_str
[new_bit
]);
2189 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2190 curr
->comm
, task_pid_nr(curr
),
2191 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2192 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2193 trace_hardirqs_enabled(curr
),
2194 trace_softirqs_enabled(curr
));
2197 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2198 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2200 print_irqtrace_events(curr
);
2201 printk("\nother info that might help us debug this:\n");
2202 print_usage_bug_scenario(this);
2204 lockdep_print_held_locks(curr
);
2206 printk("\nstack backtrace:\n");
2213 * Print out an error if an invalid bit is set:
2216 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2217 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2219 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2220 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2224 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2225 enum lock_usage_bit new_bit
);
2227 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2230 * print irq inversion bug:
2233 print_irq_inversion_bug(struct task_struct
*curr
,
2234 struct lock_list
*root
, struct lock_list
*other
,
2235 struct held_lock
*this, int forwards
,
2236 const char *irqclass
)
2238 struct lock_list
*entry
= other
;
2239 struct lock_list
*middle
= NULL
;
2242 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2245 printk("\n=========================================================\n");
2246 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2247 print_kernel_version();
2248 printk( "---------------------------------------------------------\n");
2249 printk("%s/%d just changed the state of lock:\n",
2250 curr
->comm
, task_pid_nr(curr
));
2253 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2255 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2256 print_lock_name(other
->class);
2257 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2259 printk("\nother info that might help us debug this:\n");
2261 /* Find a middle lock (if one exists) */
2262 depth
= get_lock_depth(other
);
2264 if (depth
== 0 && (entry
!= root
)) {
2265 printk("lockdep:%s bad path found in chain graph\n", __func__
);
2269 entry
= get_lock_parent(entry
);
2271 } while (entry
&& entry
!= root
&& (depth
>= 0));
2273 print_irq_lock_scenario(root
, other
,
2274 middle
? middle
->class : root
->class, other
->class);
2276 print_irq_lock_scenario(other
, root
,
2277 middle
? middle
->class : other
->class, root
->class);
2279 lockdep_print_held_locks(curr
);
2281 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2282 if (!save_trace(&root
->trace
))
2284 print_shortest_lock_dependencies(other
, root
);
2286 printk("\nstack backtrace:\n");
2293 * Prove that in the forwards-direction subgraph starting at <this>
2294 * there is no lock matching <mask>:
2297 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2298 enum lock_usage_bit bit
, const char *irqclass
)
2301 struct lock_list root
;
2302 struct lock_list
*uninitialized_var(target_entry
);
2305 root
.class = hlock_class(this);
2306 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2308 return print_bfs_bug(ret
);
2312 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2317 * Prove that in the backwards-direction subgraph starting at <this>
2318 * there is no lock matching <mask>:
2321 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2322 enum lock_usage_bit bit
, const char *irqclass
)
2325 struct lock_list root
;
2326 struct lock_list
*uninitialized_var(target_entry
);
2329 root
.class = hlock_class(this);
2330 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2332 return print_bfs_bug(ret
);
2336 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2340 void print_irqtrace_events(struct task_struct
*curr
)
2342 printk("irq event stamp: %u\n", curr
->irq_events
);
2343 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
2344 print_ip_sym(curr
->hardirq_enable_ip
);
2345 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
2346 print_ip_sym(curr
->hardirq_disable_ip
);
2347 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
2348 print_ip_sym(curr
->softirq_enable_ip
);
2349 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
2350 print_ip_sym(curr
->softirq_disable_ip
);
2353 static int HARDIRQ_verbose(struct lock_class
*class)
2356 return class_filter(class);
2361 static int SOFTIRQ_verbose(struct lock_class
*class)
2364 return class_filter(class);
2369 static int RECLAIM_FS_verbose(struct lock_class
*class)
2372 return class_filter(class);
2377 #define STRICT_READ_CHECKS 1
2379 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2380 #define LOCKDEP_STATE(__STATE) \
2382 #include "lockdep_states.h"
2383 #undef LOCKDEP_STATE
2386 static inline int state_verbose(enum lock_usage_bit bit
,
2387 struct lock_class
*class)
2389 return state_verbose_f
[bit
>> 2](class);
2392 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2393 enum lock_usage_bit bit
, const char *name
);
2396 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2397 enum lock_usage_bit new_bit
)
2399 int excl_bit
= exclusive_bit(new_bit
);
2400 int read
= new_bit
& 1;
2401 int dir
= new_bit
& 2;
2404 * mark USED_IN has to look forwards -- to ensure no dependency
2405 * has ENABLED state, which would allow recursion deadlocks.
2407 * mark ENABLED has to look backwards -- to ensure no dependee
2408 * has USED_IN state, which, again, would allow recursion deadlocks.
2410 check_usage_f usage
= dir
?
2411 check_usage_backwards
: check_usage_forwards
;
2414 * Validate that this particular lock does not have conflicting
2417 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2421 * Validate that the lock dependencies don't have conflicting usage
2424 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2425 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2429 * Check for read in write conflicts
2432 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2435 if (STRICT_READ_CHECKS
&&
2436 !usage(curr
, this, excl_bit
+ 1,
2437 state_name(new_bit
+ 1)))
2441 if (state_verbose(new_bit
, hlock_class(this)))
2448 #define LOCKDEP_STATE(__STATE) __STATE,
2449 #include "lockdep_states.h"
2450 #undef LOCKDEP_STATE
2454 * Mark all held locks with a usage bit:
2457 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2459 enum lock_usage_bit usage_bit
;
2460 struct held_lock
*hlock
;
2463 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2464 hlock
= curr
->held_locks
+ i
;
2466 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2468 usage_bit
+= 1; /* READ */
2470 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2472 if (hlock_class(hlock
)->key
== __lockdep_no_validate__
.subkeys
)
2475 if (!mark_lock(curr
, hlock
, usage_bit
))
2483 * Hardirqs will be enabled:
2485 static void __trace_hardirqs_on_caller(unsigned long ip
)
2487 struct task_struct
*curr
= current
;
2489 /* we'll do an OFF -> ON transition: */
2490 curr
->hardirqs_enabled
= 1;
2493 * We are going to turn hardirqs on, so set the
2494 * usage bit for all held locks:
2496 if (!mark_held_locks(curr
, HARDIRQ
))
2499 * If we have softirqs enabled, then set the usage
2500 * bit for all held locks. (disabled hardirqs prevented
2501 * this bit from being set before)
2503 if (curr
->softirqs_enabled
)
2504 if (!mark_held_locks(curr
, SOFTIRQ
))
2507 curr
->hardirq_enable_ip
= ip
;
2508 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2509 debug_atomic_inc(hardirqs_on_events
);
2512 void trace_hardirqs_on_caller(unsigned long ip
)
2514 time_hardirqs_on(CALLER_ADDR0
, ip
);
2516 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2519 if (unlikely(current
->hardirqs_enabled
)) {
2521 * Neither irq nor preemption are disabled here
2522 * so this is racy by nature but losing one hit
2523 * in a stat is not a big deal.
2525 __debug_atomic_inc(redundant_hardirqs_on
);
2529 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2532 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2535 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2538 current
->lockdep_recursion
= 1;
2539 __trace_hardirqs_on_caller(ip
);
2540 current
->lockdep_recursion
= 0;
2542 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2544 void trace_hardirqs_on(void)
2546 trace_hardirqs_on_caller(CALLER_ADDR0
);
2548 EXPORT_SYMBOL(trace_hardirqs_on
);
2551 * Hardirqs were disabled:
2553 void trace_hardirqs_off_caller(unsigned long ip
)
2555 struct task_struct
*curr
= current
;
2557 time_hardirqs_off(CALLER_ADDR0
, ip
);
2559 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2562 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2565 if (curr
->hardirqs_enabled
) {
2567 * We have done an ON -> OFF transition:
2569 curr
->hardirqs_enabled
= 0;
2570 curr
->hardirq_disable_ip
= ip
;
2571 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2572 debug_atomic_inc(hardirqs_off_events
);
2574 debug_atomic_inc(redundant_hardirqs_off
);
2576 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2578 void trace_hardirqs_off(void)
2580 trace_hardirqs_off_caller(CALLER_ADDR0
);
2582 EXPORT_SYMBOL(trace_hardirqs_off
);
2585 * Softirqs will be enabled:
2587 void trace_softirqs_on(unsigned long ip
)
2589 struct task_struct
*curr
= current
;
2591 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2594 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2597 if (curr
->softirqs_enabled
) {
2598 debug_atomic_inc(redundant_softirqs_on
);
2602 current
->lockdep_recursion
= 1;
2604 * We'll do an OFF -> ON transition:
2606 curr
->softirqs_enabled
= 1;
2607 curr
->softirq_enable_ip
= ip
;
2608 curr
->softirq_enable_event
= ++curr
->irq_events
;
2609 debug_atomic_inc(softirqs_on_events
);
2611 * We are going to turn softirqs on, so set the
2612 * usage bit for all held locks, if hardirqs are
2615 if (curr
->hardirqs_enabled
)
2616 mark_held_locks(curr
, SOFTIRQ
);
2617 current
->lockdep_recursion
= 0;
2621 * Softirqs were disabled:
2623 void trace_softirqs_off(unsigned long ip
)
2625 struct task_struct
*curr
= current
;
2627 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2630 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2633 if (curr
->softirqs_enabled
) {
2635 * We have done an ON -> OFF transition:
2637 curr
->softirqs_enabled
= 0;
2638 curr
->softirq_disable_ip
= ip
;
2639 curr
->softirq_disable_event
= ++curr
->irq_events
;
2640 debug_atomic_inc(softirqs_off_events
);
2641 DEBUG_LOCKS_WARN_ON(!softirq_count());
2643 debug_atomic_inc(redundant_softirqs_off
);
2646 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2648 struct task_struct
*curr
= current
;
2650 if (unlikely(!debug_locks
))
2653 /* no reclaim without waiting on it */
2654 if (!(gfp_mask
& __GFP_WAIT
))
2657 /* this guy won't enter reclaim */
2658 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2661 /* We're only interested __GFP_FS allocations for now */
2662 if (!(gfp_mask
& __GFP_FS
))
2665 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2668 mark_held_locks(curr
, RECLAIM_FS
);
2671 static void check_flags(unsigned long flags
);
2673 void lockdep_trace_alloc(gfp_t gfp_mask
)
2675 unsigned long flags
;
2677 if (unlikely(current
->lockdep_recursion
))
2680 raw_local_irq_save(flags
);
2682 current
->lockdep_recursion
= 1;
2683 __lockdep_trace_alloc(gfp_mask
, flags
);
2684 current
->lockdep_recursion
= 0;
2685 raw_local_irq_restore(flags
);
2688 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2691 * If non-trylock use in a hardirq or softirq context, then
2692 * mark the lock as used in these contexts:
2694 if (!hlock
->trylock
) {
2696 if (curr
->hardirq_context
)
2697 if (!mark_lock(curr
, hlock
,
2698 LOCK_USED_IN_HARDIRQ_READ
))
2700 if (curr
->softirq_context
)
2701 if (!mark_lock(curr
, hlock
,
2702 LOCK_USED_IN_SOFTIRQ_READ
))
2705 if (curr
->hardirq_context
)
2706 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2708 if (curr
->softirq_context
)
2709 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2713 if (!hlock
->hardirqs_off
) {
2715 if (!mark_lock(curr
, hlock
,
2716 LOCK_ENABLED_HARDIRQ_READ
))
2718 if (curr
->softirqs_enabled
)
2719 if (!mark_lock(curr
, hlock
,
2720 LOCK_ENABLED_SOFTIRQ_READ
))
2723 if (!mark_lock(curr
, hlock
,
2724 LOCK_ENABLED_HARDIRQ
))
2726 if (curr
->softirqs_enabled
)
2727 if (!mark_lock(curr
, hlock
,
2728 LOCK_ENABLED_SOFTIRQ
))
2734 * We reuse the irq context infrastructure more broadly as a general
2735 * context checking code. This tests GFP_FS recursion (a lock taken
2736 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2739 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2741 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2744 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2752 static int separate_irq_context(struct task_struct
*curr
,
2753 struct held_lock
*hlock
)
2755 unsigned int depth
= curr
->lockdep_depth
;
2758 * Keep track of points where we cross into an interrupt context:
2760 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2761 curr
->softirq_context
;
2763 struct held_lock
*prev_hlock
;
2765 prev_hlock
= curr
->held_locks
+ depth
-1;
2767 * If we cross into another context, reset the
2768 * hash key (this also prevents the checking and the
2769 * adding of the dependency to 'prev'):
2771 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2780 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2781 enum lock_usage_bit new_bit
)
2787 static inline int mark_irqflags(struct task_struct
*curr
,
2788 struct held_lock
*hlock
)
2793 static inline int separate_irq_context(struct task_struct
*curr
,
2794 struct held_lock
*hlock
)
2799 void lockdep_trace_alloc(gfp_t gfp_mask
)
2806 * Mark a lock with a usage bit, and validate the state transition:
2808 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2809 enum lock_usage_bit new_bit
)
2811 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2814 * If already set then do not dirty the cacheline,
2815 * nor do any checks:
2817 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2823 * Make sure we didn't race:
2825 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2830 hlock_class(this)->usage_mask
|= new_mask
;
2832 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2836 #define LOCKDEP_STATE(__STATE) \
2837 case LOCK_USED_IN_##__STATE: \
2838 case LOCK_USED_IN_##__STATE##_READ: \
2839 case LOCK_ENABLED_##__STATE: \
2840 case LOCK_ENABLED_##__STATE##_READ:
2841 #include "lockdep_states.h"
2842 #undef LOCKDEP_STATE
2843 ret
= mark_lock_irq(curr
, this, new_bit
);
2848 debug_atomic_dec(nr_unused_locks
);
2851 if (!debug_locks_off_graph_unlock())
2860 * We must printk outside of the graph_lock:
2863 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2865 print_irqtrace_events(curr
);
2873 * Initialize a lock instance's lock-class mapping info:
2875 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2876 struct lock_class_key
*key
, int subclass
)
2880 kmemcheck_mark_initialized(lock
, sizeof(*lock
));
2882 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
2883 lock
->class_cache
[i
] = NULL
;
2885 #ifdef CONFIG_LOCK_STAT
2886 lock
->cpu
= raw_smp_processor_id();
2889 if (DEBUG_LOCKS_WARN_ON(!name
)) {
2890 lock
->name
= "NULL";
2896 if (DEBUG_LOCKS_WARN_ON(!key
))
2899 * Sanity check, the lock-class key must be persistent:
2901 if (!static_obj(key
)) {
2902 printk("BUG: key %p not in .data!\n", key
);
2903 DEBUG_LOCKS_WARN_ON(1);
2908 if (unlikely(!debug_locks
))
2912 register_lock_class(lock
, subclass
, 1);
2914 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2916 struct lock_class_key __lockdep_no_validate__
;
2919 * This gets called for every mutex_lock*()/spin_lock*() operation.
2920 * We maintain the dependency maps and validate the locking attempt:
2922 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2923 int trylock
, int read
, int check
, int hardirqs_off
,
2924 struct lockdep_map
*nest_lock
, unsigned long ip
,
2927 struct task_struct
*curr
= current
;
2928 struct lock_class
*class = NULL
;
2929 struct held_lock
*hlock
;
2930 unsigned int depth
, id
;
2938 if (unlikely(!debug_locks
))
2941 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2944 if (lock
->key
== &__lockdep_no_validate__
)
2947 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
2948 class = lock
->class_cache
[subclass
];
2952 if (unlikely(!class)) {
2953 class = register_lock_class(lock
, subclass
, 0);
2957 atomic_inc((atomic_t
*)&class->ops
);
2958 if (very_verbose(class)) {
2959 printk("\nacquire class [%p] %s", class->key
, class->name
);
2960 if (class->name_version
> 1)
2961 printk("#%d", class->name_version
);
2967 * Add the lock to the list of currently held locks.
2968 * (we dont increase the depth just yet, up until the
2969 * dependency checks are done)
2971 depth
= curr
->lockdep_depth
;
2972 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2975 class_idx
= class - lock_classes
+ 1;
2978 hlock
= curr
->held_locks
+ depth
- 1;
2979 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
2980 if (hlock
->references
)
2981 hlock
->references
++;
2983 hlock
->references
= 2;
2989 hlock
= curr
->held_locks
+ depth
;
2990 if (DEBUG_LOCKS_WARN_ON(!class))
2992 hlock
->class_idx
= class_idx
;
2993 hlock
->acquire_ip
= ip
;
2994 hlock
->instance
= lock
;
2995 hlock
->nest_lock
= nest_lock
;
2996 hlock
->trylock
= trylock
;
2998 hlock
->check
= check
;
2999 hlock
->hardirqs_off
= !!hardirqs_off
;
3000 hlock
->references
= references
;
3001 #ifdef CONFIG_LOCK_STAT
3002 hlock
->waittime_stamp
= 0;
3003 hlock
->holdtime_stamp
= lockstat_clock();
3006 if (check
== 2 && !mark_irqflags(curr
, hlock
))
3009 /* mark it as used: */
3010 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3014 * Calculate the chain hash: it's the combined hash of all the
3015 * lock keys along the dependency chain. We save the hash value
3016 * at every step so that we can get the current hash easily
3017 * after unlock. The chain hash is then used to cache dependency
3020 * The 'key ID' is what is the most compact key value to drive
3021 * the hash, not class->key.
3023 id
= class - lock_classes
;
3024 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
3027 chain_key
= curr
->curr_chain_key
;
3029 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3034 hlock
->prev_chain_key
= chain_key
;
3035 if (separate_irq_context(curr
, hlock
)) {
3039 chain_key
= iterate_chain_key(chain_key
, id
);
3041 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3044 curr
->curr_chain_key
= chain_key
;
3045 curr
->lockdep_depth
++;
3046 check_chain_key(curr
);
3047 #ifdef CONFIG_DEBUG_LOCKDEP
3048 if (unlikely(!debug_locks
))
3051 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3053 printk("BUG: MAX_LOCK_DEPTH too low!\n");
3054 printk("turning off the locking correctness validator.\n");
3059 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3060 max_lockdep_depth
= curr
->lockdep_depth
;
3066 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3069 if (!debug_locks_off())
3071 if (debug_locks_silent
)
3074 printk("\n=====================================\n");
3075 printk( "[ BUG: bad unlock balance detected! ]\n");
3076 printk( "-------------------------------------\n");
3077 printk("%s/%d is trying to release lock (",
3078 curr
->comm
, task_pid_nr(curr
));
3079 print_lockdep_cache(lock
);
3082 printk("but there are no more locks to release!\n");
3083 printk("\nother info that might help us debug this:\n");
3084 lockdep_print_held_locks(curr
);
3086 printk("\nstack backtrace:\n");
3093 * Common debugging checks for both nested and non-nested unlock:
3095 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
3098 if (unlikely(!debug_locks
))
3100 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3103 if (curr
->lockdep_depth
<= 0)
3104 return print_unlock_inbalance_bug(curr
, lock
, ip
);
3109 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
3111 if (hlock
->instance
== lock
)
3114 if (hlock
->references
) {
3115 struct lock_class
*class = lock
->class_cache
[0];
3118 class = look_up_lock_class(lock
, 0);
3121 * If look_up_lock_class() failed to find a class, we're trying
3122 * to test if we hold a lock that has never yet been acquired.
3123 * Clearly if the lock hasn't been acquired _ever_, we're not
3124 * holding it either, so report failure.
3129 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3132 if (hlock
->class_idx
== class - lock_classes
+ 1)
3140 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3141 struct lock_class_key
*key
, unsigned int subclass
,
3144 struct task_struct
*curr
= current
;
3145 struct held_lock
*hlock
, *prev_hlock
;
3146 struct lock_class
*class;
3150 depth
= curr
->lockdep_depth
;
3151 if (DEBUG_LOCKS_WARN_ON(!depth
))
3155 for (i
= depth
-1; i
>= 0; i
--) {
3156 hlock
= curr
->held_locks
+ i
;
3158 * We must not cross into another context:
3160 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3162 if (match_held_lock(hlock
, lock
))
3166 return print_unlock_inbalance_bug(curr
, lock
, ip
);
3169 lockdep_init_map(lock
, name
, key
, 0);
3170 class = register_lock_class(lock
, subclass
, 0);
3171 hlock
->class_idx
= class - lock_classes
+ 1;
3173 curr
->lockdep_depth
= i
;
3174 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3176 for (; i
< depth
; i
++) {
3177 hlock
= curr
->held_locks
+ i
;
3178 if (!__lock_acquire(hlock
->instance
,
3179 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3180 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3181 hlock
->nest_lock
, hlock
->acquire_ip
,
3186 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3192 * Remove the lock to the list of currently held locks in a
3193 * potentially non-nested (out of order) manner. This is a
3194 * relatively rare operation, as all the unlock APIs default
3195 * to nested mode (which uses lock_release()):
3198 lock_release_non_nested(struct task_struct
*curr
,
3199 struct lockdep_map
*lock
, unsigned long ip
)
3201 struct held_lock
*hlock
, *prev_hlock
;
3206 * Check whether the lock exists in the current stack
3209 depth
= curr
->lockdep_depth
;
3210 if (DEBUG_LOCKS_WARN_ON(!depth
))
3214 for (i
= depth
-1; i
>= 0; i
--) {
3215 hlock
= curr
->held_locks
+ i
;
3217 * We must not cross into another context:
3219 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3221 if (match_held_lock(hlock
, lock
))
3225 return print_unlock_inbalance_bug(curr
, lock
, ip
);
3228 if (hlock
->instance
== lock
)
3229 lock_release_holdtime(hlock
);
3231 if (hlock
->references
) {
3232 hlock
->references
--;
3233 if (hlock
->references
) {
3235 * We had, and after removing one, still have
3236 * references, the current lock stack is still
3237 * valid. We're done!
3244 * We have the right lock to unlock, 'hlock' points to it.
3245 * Now we remove it from the stack, and add back the other
3246 * entries (if any), recalculating the hash along the way:
3249 curr
->lockdep_depth
= i
;
3250 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3252 for (i
++; i
< depth
; i
++) {
3253 hlock
= curr
->held_locks
+ i
;
3254 if (!__lock_acquire(hlock
->instance
,
3255 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3256 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3257 hlock
->nest_lock
, hlock
->acquire_ip
,
3262 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3268 * Remove the lock to the list of currently held locks - this gets
3269 * called on mutex_unlock()/spin_unlock*() (or on a failed
3270 * mutex_lock_interruptible()). This is done for unlocks that nest
3271 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3273 static int lock_release_nested(struct task_struct
*curr
,
3274 struct lockdep_map
*lock
, unsigned long ip
)
3276 struct held_lock
*hlock
;
3280 * Pop off the top of the lock stack:
3282 depth
= curr
->lockdep_depth
- 1;
3283 hlock
= curr
->held_locks
+ depth
;
3286 * Is the unlock non-nested:
3288 if (hlock
->instance
!= lock
|| hlock
->references
)
3289 return lock_release_non_nested(curr
, lock
, ip
);
3290 curr
->lockdep_depth
--;
3292 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
3295 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3297 lock_release_holdtime(hlock
);
3299 #ifdef CONFIG_DEBUG_LOCKDEP
3300 hlock
->prev_chain_key
= 0;
3301 hlock
->class_idx
= 0;
3302 hlock
->acquire_ip
= 0;
3303 hlock
->irq_context
= 0;
3309 * Remove the lock to the list of currently held locks - this gets
3310 * called on mutex_unlock()/spin_unlock*() (or on a failed
3311 * mutex_lock_interruptible()). This is done for unlocks that nest
3312 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3315 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3317 struct task_struct
*curr
= current
;
3319 if (!check_unlock(curr
, lock
, ip
))
3323 if (!lock_release_nested(curr
, lock
, ip
))
3326 if (!lock_release_non_nested(curr
, lock
, ip
))
3330 check_chain_key(curr
);
3333 static int __lock_is_held(struct lockdep_map
*lock
)
3335 struct task_struct
*curr
= current
;
3338 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3339 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3341 if (match_held_lock(hlock
, lock
))
3349 * Check whether we follow the irq-flags state precisely:
3351 static void check_flags(unsigned long flags
)
3353 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3354 defined(CONFIG_TRACE_IRQFLAGS)
3358 if (irqs_disabled_flags(flags
)) {
3359 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3360 printk("possible reason: unannotated irqs-off.\n");
3363 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3364 printk("possible reason: unannotated irqs-on.\n");
3369 * We dont accurately track softirq state in e.g.
3370 * hardirq contexts (such as on 4KSTACKS), so only
3371 * check if not in hardirq contexts:
3373 if (!hardirq_count()) {
3374 if (softirq_count())
3375 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3377 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3381 print_irqtrace_events(current
);
3385 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3386 struct lock_class_key
*key
, unsigned int subclass
,
3389 unsigned long flags
;
3391 if (unlikely(current
->lockdep_recursion
))
3394 raw_local_irq_save(flags
);
3395 current
->lockdep_recursion
= 1;
3397 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3398 check_chain_key(current
);
3399 current
->lockdep_recursion
= 0;
3400 raw_local_irq_restore(flags
);
3402 EXPORT_SYMBOL_GPL(lock_set_class
);
3405 * We are not always called with irqs disabled - do that here,
3406 * and also avoid lockdep recursion:
3408 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3409 int trylock
, int read
, int check
,
3410 struct lockdep_map
*nest_lock
, unsigned long ip
)
3412 unsigned long flags
;
3414 if (unlikely(current
->lockdep_recursion
))
3417 raw_local_irq_save(flags
);
3420 current
->lockdep_recursion
= 1;
3421 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3422 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3423 irqs_disabled_flags(flags
), nest_lock
, ip
, 0);
3424 current
->lockdep_recursion
= 0;
3425 raw_local_irq_restore(flags
);
3427 EXPORT_SYMBOL_GPL(lock_acquire
);
3429 void lock_release(struct lockdep_map
*lock
, int nested
,
3432 unsigned long flags
;
3434 if (unlikely(current
->lockdep_recursion
))
3437 raw_local_irq_save(flags
);
3439 current
->lockdep_recursion
= 1;
3440 trace_lock_release(lock
, ip
);
3441 __lock_release(lock
, nested
, ip
);
3442 current
->lockdep_recursion
= 0;
3443 raw_local_irq_restore(flags
);
3445 EXPORT_SYMBOL_GPL(lock_release
);
3447 int lock_is_held(struct lockdep_map
*lock
)
3449 unsigned long flags
;
3452 if (unlikely(current
->lockdep_recursion
))
3453 return 1; /* avoid false negative lockdep_assert_held() */
3455 raw_local_irq_save(flags
);
3458 current
->lockdep_recursion
= 1;
3459 ret
= __lock_is_held(lock
);
3460 current
->lockdep_recursion
= 0;
3461 raw_local_irq_restore(flags
);
3465 EXPORT_SYMBOL_GPL(lock_is_held
);
3467 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3469 current
->lockdep_reclaim_gfp
= gfp_mask
;
3472 void lockdep_clear_current_reclaim_state(void)
3474 current
->lockdep_reclaim_gfp
= 0;
3477 #ifdef CONFIG_LOCK_STAT
3479 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3482 if (!debug_locks_off())
3484 if (debug_locks_silent
)
3487 printk("\n=================================\n");
3488 printk( "[ BUG: bad contention detected! ]\n");
3489 printk( "---------------------------------\n");
3490 printk("%s/%d is trying to contend lock (",
3491 curr
->comm
, task_pid_nr(curr
));
3492 print_lockdep_cache(lock
);
3495 printk("but there are no locks held!\n");
3496 printk("\nother info that might help us debug this:\n");
3497 lockdep_print_held_locks(curr
);
3499 printk("\nstack backtrace:\n");
3506 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3508 struct task_struct
*curr
= current
;
3509 struct held_lock
*hlock
, *prev_hlock
;
3510 struct lock_class_stats
*stats
;
3512 int i
, contention_point
, contending_point
;
3514 depth
= curr
->lockdep_depth
;
3515 if (DEBUG_LOCKS_WARN_ON(!depth
))
3519 for (i
= depth
-1; i
>= 0; i
--) {
3520 hlock
= curr
->held_locks
+ i
;
3522 * We must not cross into another context:
3524 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3526 if (match_held_lock(hlock
, lock
))
3530 print_lock_contention_bug(curr
, lock
, ip
);
3534 if (hlock
->instance
!= lock
)
3537 hlock
->waittime_stamp
= lockstat_clock();
3539 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3540 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3543 stats
= get_lock_stats(hlock_class(hlock
));
3544 if (contention_point
< LOCKSTAT_POINTS
)
3545 stats
->contention_point
[contention_point
]++;
3546 if (contending_point
< LOCKSTAT_POINTS
)
3547 stats
->contending_point
[contending_point
]++;
3548 if (lock
->cpu
!= smp_processor_id())
3549 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3550 put_lock_stats(stats
);
3554 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3556 struct task_struct
*curr
= current
;
3557 struct held_lock
*hlock
, *prev_hlock
;
3558 struct lock_class_stats
*stats
;
3560 u64 now
, waittime
= 0;
3563 depth
= curr
->lockdep_depth
;
3564 if (DEBUG_LOCKS_WARN_ON(!depth
))
3568 for (i
= depth
-1; i
>= 0; i
--) {
3569 hlock
= curr
->held_locks
+ i
;
3571 * We must not cross into another context:
3573 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3575 if (match_held_lock(hlock
, lock
))
3579 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3583 if (hlock
->instance
!= lock
)
3586 cpu
= smp_processor_id();
3587 if (hlock
->waittime_stamp
) {
3588 now
= lockstat_clock();
3589 waittime
= now
- hlock
->waittime_stamp
;
3590 hlock
->holdtime_stamp
= now
;
3593 trace_lock_acquired(lock
, ip
);
3595 stats
= get_lock_stats(hlock_class(hlock
));
3598 lock_time_inc(&stats
->read_waittime
, waittime
);
3600 lock_time_inc(&stats
->write_waittime
, waittime
);
3602 if (lock
->cpu
!= cpu
)
3603 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3604 put_lock_stats(stats
);
3610 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3612 unsigned long flags
;
3614 if (unlikely(!lock_stat
))
3617 if (unlikely(current
->lockdep_recursion
))
3620 raw_local_irq_save(flags
);
3622 current
->lockdep_recursion
= 1;
3623 trace_lock_contended(lock
, ip
);
3624 __lock_contended(lock
, ip
);
3625 current
->lockdep_recursion
= 0;
3626 raw_local_irq_restore(flags
);
3628 EXPORT_SYMBOL_GPL(lock_contended
);
3630 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3632 unsigned long flags
;
3634 if (unlikely(!lock_stat
))
3637 if (unlikely(current
->lockdep_recursion
))
3640 raw_local_irq_save(flags
);
3642 current
->lockdep_recursion
= 1;
3643 __lock_acquired(lock
, ip
);
3644 current
->lockdep_recursion
= 0;
3645 raw_local_irq_restore(flags
);
3647 EXPORT_SYMBOL_GPL(lock_acquired
);
3651 * Used by the testsuite, sanitize the validator state
3652 * after a simulated failure:
3655 void lockdep_reset(void)
3657 unsigned long flags
;
3660 raw_local_irq_save(flags
);
3661 current
->curr_chain_key
= 0;
3662 current
->lockdep_depth
= 0;
3663 current
->lockdep_recursion
= 0;
3664 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3665 nr_hardirq_chains
= 0;
3666 nr_softirq_chains
= 0;
3667 nr_process_chains
= 0;
3669 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3670 INIT_LIST_HEAD(chainhash_table
+ i
);
3671 raw_local_irq_restore(flags
);
3674 static void zap_class(struct lock_class
*class)
3679 * Remove all dependencies this lock is
3682 for (i
= 0; i
< nr_list_entries
; i
++) {
3683 if (list_entries
[i
].class == class)
3684 list_del_rcu(&list_entries
[i
].entry
);
3687 * Unhash the class and remove it from the all_lock_classes list:
3689 list_del_rcu(&class->hash_entry
);
3690 list_del_rcu(&class->lock_entry
);
3695 static inline int within(const void *addr
, void *start
, unsigned long size
)
3697 return addr
>= start
&& addr
< start
+ size
;
3700 void lockdep_free_key_range(void *start
, unsigned long size
)
3702 struct lock_class
*class, *next
;
3703 struct list_head
*head
;
3704 unsigned long flags
;
3708 raw_local_irq_save(flags
);
3709 locked
= graph_lock();
3712 * Unhash all classes that were created by this module:
3714 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3715 head
= classhash_table
+ i
;
3716 if (list_empty(head
))
3718 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3719 if (within(class->key
, start
, size
))
3721 else if (within(class->name
, start
, size
))
3728 raw_local_irq_restore(flags
);
3731 void lockdep_reset_lock(struct lockdep_map
*lock
)
3733 struct lock_class
*class, *next
;
3734 struct list_head
*head
;
3735 unsigned long flags
;
3739 raw_local_irq_save(flags
);
3742 * Remove all classes this lock might have:
3744 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3746 * If the class exists we look it up and zap it:
3748 class = look_up_lock_class(lock
, j
);
3753 * Debug check: in the end all mapped classes should
3756 locked
= graph_lock();
3757 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3758 head
= classhash_table
+ i
;
3759 if (list_empty(head
))
3761 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3764 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
3765 match
|= class == lock
->class_cache
[j
];
3767 if (unlikely(match
)) {
3768 if (debug_locks_off_graph_unlock())
3778 raw_local_irq_restore(flags
);
3781 void lockdep_init(void)
3786 * Some architectures have their own start_kernel()
3787 * code which calls lockdep_init(), while we also
3788 * call lockdep_init() from the start_kernel() itself,
3789 * and we want to initialize the hashes only once:
3791 if (lockdep_initialized
)
3794 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3795 INIT_LIST_HEAD(classhash_table
+ i
);
3797 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3798 INIT_LIST_HEAD(chainhash_table
+ i
);
3800 lockdep_initialized
= 1;
3803 void __init
lockdep_info(void)
3805 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3807 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3808 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3809 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3810 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3811 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3812 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3813 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3815 printk(" memory used by lock dependency info: %lu kB\n",
3816 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3817 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3818 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3819 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3820 sizeof(struct list_head
) * CHAINHASH_SIZE
3821 #ifdef CONFIG_PROVE_LOCKING
3822 + sizeof(struct circular_queue
)
3827 printk(" per task-struct memory footprint: %lu bytes\n",
3828 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3830 #ifdef CONFIG_DEBUG_LOCKDEP
3831 if (lockdep_init_error
) {
3832 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3833 printk("Call stack leading to lockdep invocation was:\n");
3834 print_stack_trace(&lockdep_init_trace
, 0);
3840 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3841 const void *mem_to
, struct held_lock
*hlock
)
3843 if (!debug_locks_off())
3845 if (debug_locks_silent
)
3848 printk("\n=========================\n");
3849 printk( "[ BUG: held lock freed! ]\n");
3850 printk( "-------------------------\n");
3851 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3852 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
3854 lockdep_print_held_locks(curr
);
3856 printk("\nstack backtrace:\n");
3860 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
3861 const void* lock_from
, unsigned long lock_len
)
3863 return lock_from
+ lock_len
<= mem_from
||
3864 mem_from
+ mem_len
<= lock_from
;
3868 * Called when kernel memory is freed (or unmapped), or if a lock
3869 * is destroyed or reinitialized - this code checks whether there is
3870 * any held lock in the memory range of <from> to <to>:
3872 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3874 struct task_struct
*curr
= current
;
3875 struct held_lock
*hlock
;
3876 unsigned long flags
;
3879 if (unlikely(!debug_locks
))
3882 local_irq_save(flags
);
3883 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3884 hlock
= curr
->held_locks
+ i
;
3886 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
3887 sizeof(*hlock
->instance
)))
3890 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
3893 local_irq_restore(flags
);
3895 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3897 static void print_held_locks_bug(struct task_struct
*curr
)
3899 if (!debug_locks_off())
3901 if (debug_locks_silent
)
3904 printk("\n=====================================\n");
3905 printk( "[ BUG: lock held at task exit time! ]\n");
3906 printk( "-------------------------------------\n");
3907 printk("%s/%d is exiting with locks still held!\n",
3908 curr
->comm
, task_pid_nr(curr
));
3909 lockdep_print_held_locks(curr
);
3911 printk("\nstack backtrace:\n");
3915 void debug_check_no_locks_held(struct task_struct
*task
)
3917 if (unlikely(task
->lockdep_depth
> 0))
3918 print_held_locks_bug(task
);
3921 void debug_show_all_locks(void)
3923 struct task_struct
*g
, *p
;
3927 if (unlikely(!debug_locks
)) {
3928 printk("INFO: lockdep is turned off.\n");
3931 printk("\nShowing all locks held in the system:\n");
3934 * Here we try to get the tasklist_lock as hard as possible,
3935 * if not successful after 2 seconds we ignore it (but keep
3936 * trying). This is to enable a debug printout even if a
3937 * tasklist_lock-holding task deadlocks or crashes.
3940 if (!read_trylock(&tasklist_lock
)) {
3942 printk("hm, tasklist_lock locked, retrying... ");
3945 printk(" #%d", 10-count
);
3949 printk(" ignoring it.\n");
3953 printk(KERN_CONT
" locked it.\n");
3956 do_each_thread(g
, p
) {
3958 * It's not reliable to print a task's held locks
3959 * if it's not sleeping (or if it's not the current
3962 if (p
->state
== TASK_RUNNING
&& p
!= current
)
3964 if (p
->lockdep_depth
)
3965 lockdep_print_held_locks(p
);
3967 if (read_trylock(&tasklist_lock
))
3969 } while_each_thread(g
, p
);
3972 printk("=============================================\n\n");
3975 read_unlock(&tasklist_lock
);
3977 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3980 * Careful: only use this function if you are sure that
3981 * the task cannot run in parallel!
3983 void debug_show_held_locks(struct task_struct
*task
)
3985 if (unlikely(!debug_locks
)) {
3986 printk("INFO: lockdep is turned off.\n");
3989 lockdep_print_held_locks(task
);
3991 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
3993 void lockdep_sys_exit(void)
3995 struct task_struct
*curr
= current
;
3997 if (unlikely(curr
->lockdep_depth
)) {
3998 if (!debug_locks_off())
4000 printk("\n================================================\n");
4001 printk( "[ BUG: lock held when returning to user space! ]\n");
4002 printk( "------------------------------------------------\n");
4003 printk("%s/%d is leaving the kernel with locks still held!\n",
4004 curr
->comm
, curr
->pid
);
4005 lockdep_print_held_locks(curr
);
4009 void lockdep_rcu_dereference(const char *file
, const int line
)
4011 struct task_struct
*curr
= current
;
4013 #ifndef CONFIG_PROVE_RCU_REPEATEDLY
4014 if (!debug_locks_off())
4016 #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4017 /* Note: the following can be executed concurrently, so be careful. */
4018 printk("\n===================================================\n");
4019 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
4020 printk( "---------------------------------------------------\n");
4021 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
4023 printk("\nother info that might help us debug this:\n\n");
4024 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active
, debug_locks
);
4025 lockdep_print_held_locks(curr
);
4026 printk("\nstack backtrace:\n");
4029 EXPORT_SYMBOL_GPL(lockdep_rcu_dereference
);