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
)) {
102 * The lockdep graph lock isn't locked while we expect it to
103 * be, we're confused now, bye!
105 return DEBUG_LOCKS_WARN_ON(1);
108 current
->lockdep_recursion
--;
109 arch_spin_unlock(&lockdep_lock
);
114 * Turn lock debugging off and return with 0 if it was off already,
115 * and also release the graph lock:
117 static inline int debug_locks_off_graph_unlock(void)
119 int ret
= debug_locks_off();
121 arch_spin_unlock(&lockdep_lock
);
126 static int lockdep_initialized
;
128 unsigned long nr_list_entries
;
129 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
132 * All data structures here are protected by the global debug_lock.
134 * Mutex key structs only get allocated, once during bootup, and never
135 * get freed - this significantly simplifies the debugging code.
137 unsigned long nr_lock_classes
;
138 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
140 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
142 if (!hlock
->class_idx
) {
144 * Someone passed in garbage, we give up.
146 DEBUG_LOCKS_WARN_ON(1);
149 return lock_classes
+ hlock
->class_idx
- 1;
152 #ifdef CONFIG_LOCK_STAT
153 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
],
156 static inline u64
lockstat_clock(void)
158 return local_clock();
161 static int lock_point(unsigned long points
[], unsigned long ip
)
165 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
166 if (points
[i
] == 0) {
177 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
182 if (time
< lt
->min
|| !lt
->nr
)
189 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
194 if (src
->max
> dst
->max
)
197 if (src
->min
< dst
->min
|| !dst
->nr
)
200 dst
->total
+= src
->total
;
204 struct lock_class_stats
lock_stats(struct lock_class
*class)
206 struct lock_class_stats stats
;
209 memset(&stats
, 0, sizeof(struct lock_class_stats
));
210 for_each_possible_cpu(cpu
) {
211 struct lock_class_stats
*pcs
=
212 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
214 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
215 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
217 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
218 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
220 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
221 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
223 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
224 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
226 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
227 stats
.bounces
[i
] += pcs
->bounces
[i
];
233 void clear_lock_stats(struct lock_class
*class)
237 for_each_possible_cpu(cpu
) {
238 struct lock_class_stats
*cpu_stats
=
239 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
241 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
243 memset(class->contention_point
, 0, sizeof(class->contention_point
));
244 memset(class->contending_point
, 0, sizeof(class->contending_point
));
247 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
249 return &get_cpu_var(cpu_lock_stats
)[class - lock_classes
];
252 static void put_lock_stats(struct lock_class_stats
*stats
)
254 put_cpu_var(cpu_lock_stats
);
257 static void lock_release_holdtime(struct held_lock
*hlock
)
259 struct lock_class_stats
*stats
;
265 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
267 stats
= get_lock_stats(hlock_class(hlock
));
269 lock_time_inc(&stats
->read_holdtime
, holdtime
);
271 lock_time_inc(&stats
->write_holdtime
, holdtime
);
272 put_lock_stats(stats
);
275 static inline void lock_release_holdtime(struct held_lock
*hlock
)
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
285 LIST_HEAD(all_lock_classes
);
288 * The lockdep classes are in a hash-table as well, for fast lookup:
290 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
292 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
293 #define classhashentry(key) (classhash_table + __classhashfn((key)))
295 static struct list_head classhash_table
[CLASSHASH_SIZE
];
298 * We put the lock dependency chains into a hash-table as well, to cache
301 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
303 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
304 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
306 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
314 #define iterate_chain_key(key1, key2) \
315 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
316 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
319 void lockdep_off(void)
321 current
->lockdep_recursion
++;
323 EXPORT_SYMBOL(lockdep_off
);
325 void lockdep_on(void)
327 current
->lockdep_recursion
--;
329 EXPORT_SYMBOL(lockdep_on
);
332 * Debugging switches:
336 #define VERY_VERBOSE 0
339 # define HARDIRQ_VERBOSE 1
340 # define SOFTIRQ_VERBOSE 1
341 # define RECLAIM_VERBOSE 1
343 # define HARDIRQ_VERBOSE 0
344 # define SOFTIRQ_VERBOSE 0
345 # define RECLAIM_VERBOSE 0
348 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
350 * Quick filtering for interesting events:
352 static int class_filter(struct lock_class
*class)
356 if (class->name_version
== 1 &&
357 !strcmp(class->name
, "lockname"))
359 if (class->name_version
== 1 &&
360 !strcmp(class->name
, "&struct->lockfield"))
363 /* Filter everything else. 1 would be to allow everything else */
368 static int verbose(struct lock_class
*class)
371 return class_filter(class);
377 * Stack-trace: tightly packed array of stack backtrace
378 * addresses. Protected by the graph_lock.
380 unsigned long nr_stack_trace_entries
;
381 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
383 static void print_lockdep_off(const char *bug_msg
)
385 printk(KERN_DEBUG
"%s\n", bug_msg
);
386 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
387 #ifdef CONFIG_LOCK_STAT
388 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
392 static int save_trace(struct stack_trace
*trace
)
394 trace
->nr_entries
= 0;
395 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
396 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
400 save_stack_trace(trace
);
403 * Some daft arches put -1 at the end to indicate its a full trace.
405 * <rant> this is buggy anyway, since it takes a whole extra entry so a
406 * complete trace that maxes out the entries provided will be reported
407 * as incomplete, friggin useless </rant>
409 if (trace
->nr_entries
!= 0 &&
410 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
413 trace
->max_entries
= trace
->nr_entries
;
415 nr_stack_trace_entries
+= trace
->nr_entries
;
417 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
418 if (!debug_locks_off_graph_unlock())
421 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
430 unsigned int nr_hardirq_chains
;
431 unsigned int nr_softirq_chains
;
432 unsigned int nr_process_chains
;
433 unsigned int max_lockdep_depth
;
435 #ifdef CONFIG_DEBUG_LOCKDEP
437 * We cannot printk in early bootup code. Not even early_printk()
438 * might work. So we mark any initialization errors and printk
439 * about it later on, in lockdep_info().
441 static int lockdep_init_error
;
442 static const char *lock_init_error
;
443 static unsigned long lockdep_init_trace_data
[20];
444 static struct stack_trace lockdep_init_trace
= {
445 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
446 .entries
= lockdep_init_trace_data
,
450 * Various lockdep statistics:
452 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
459 #define __USAGE(__STATE) \
460 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
461 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
462 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
463 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
465 static const char *usage_str
[] =
467 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
468 #include "lockdep_states.h"
470 [LOCK_USED
] = "INITIAL USE",
473 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
475 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
478 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
483 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
487 if (class->usage_mask
& lock_flag(bit
+ 2))
489 if (class->usage_mask
& lock_flag(bit
)) {
491 if (class->usage_mask
& lock_flag(bit
+ 2))
498 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
502 #define LOCKDEP_STATE(__STATE) \
503 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
504 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
505 #include "lockdep_states.h"
511 static void __print_lock_name(struct lock_class
*class)
513 char str
[KSYM_NAME_LEN
];
518 name
= __get_key_name(class->key
, str
);
522 if (class->name_version
> 1)
523 printk("#%d", class->name_version
);
525 printk("/%d", class->subclass
);
529 static void print_lock_name(struct lock_class
*class)
531 char usage
[LOCK_USAGE_CHARS
];
533 get_usage_chars(class, usage
);
536 __print_lock_name(class);
537 printk("){%s}", usage
);
540 static void print_lockdep_cache(struct lockdep_map
*lock
)
543 char str
[KSYM_NAME_LEN
];
547 name
= __get_key_name(lock
->key
->subkeys
, str
);
552 static void print_lock(struct held_lock
*hlock
)
554 print_lock_name(hlock_class(hlock
));
556 print_ip_sym(hlock
->acquire_ip
);
559 static void lockdep_print_held_locks(struct task_struct
*curr
)
561 int i
, depth
= curr
->lockdep_depth
;
564 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
567 printk("%d lock%s held by %s/%d:\n",
568 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
570 for (i
= 0; i
< depth
; i
++) {
572 print_lock(curr
->held_locks
+ i
);
576 static void print_kernel_ident(void)
578 printk("%s %.*s %s\n", init_utsname()->release
,
579 (int)strcspn(init_utsname()->version
, " "),
580 init_utsname()->version
,
584 static int very_verbose(struct lock_class
*class)
587 return class_filter(class);
593 * Is this the address of a static object:
596 static int static_obj(void *obj
)
598 unsigned long start
= (unsigned long) &_stext
,
599 end
= (unsigned long) &_end
,
600 addr
= (unsigned long) obj
;
605 if ((addr
>= start
) && (addr
< end
))
608 if (arch_is_kernel_data(addr
))
612 * in-kernel percpu var?
614 if (is_kernel_percpu_address(addr
))
618 * module static or percpu var?
620 return is_module_address(addr
) || is_module_percpu_address(addr
);
625 * To make lock name printouts unique, we calculate a unique
626 * class->name_version generation counter:
628 static int count_matching_names(struct lock_class
*new_class
)
630 struct lock_class
*class;
633 if (!new_class
->name
)
636 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
637 if (new_class
->key
- new_class
->subclass
== class->key
)
638 return class->name_version
;
639 if (class->name
&& !strcmp(class->name
, new_class
->name
))
640 count
= max(count
, class->name_version
);
647 * Register a lock's class in the hash-table, if the class is not present
648 * yet. Otherwise we look it up. We cache the result in the lock object
649 * itself, so actual lookup of the hash should be once per lock object.
651 static inline struct lock_class
*
652 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
654 struct lockdep_subclass_key
*key
;
655 struct list_head
*hash_head
;
656 struct lock_class
*class;
658 #ifdef CONFIG_DEBUG_LOCKDEP
660 * If the architecture calls into lockdep before initializing
661 * the hashes then we'll warn about it later. (we cannot printk
664 if (unlikely(!lockdep_initialized
)) {
666 lockdep_init_error
= 1;
667 lock_init_error
= lock
->name
;
668 save_stack_trace(&lockdep_init_trace
);
672 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
675 "BUG: looking up invalid subclass: %u\n", subclass
);
677 "turning off the locking correctness validator.\n");
683 * Static locks do not have their class-keys yet - for them the key
684 * is the lock object itself:
686 if (unlikely(!lock
->key
))
687 lock
->key
= (void *)lock
;
690 * NOTE: the class-key must be unique. For dynamic locks, a static
691 * lock_class_key variable is passed in through the mutex_init()
692 * (or spin_lock_init()) call - which acts as the key. For static
693 * locks we use the lock object itself as the key.
695 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
696 sizeof(struct lockdep_map
));
698 key
= lock
->key
->subkeys
+ subclass
;
700 hash_head
= classhashentry(key
);
703 * We can walk the hash lockfree, because the hash only
704 * grows, and we are careful when adding entries to the end:
706 list_for_each_entry(class, hash_head
, hash_entry
) {
707 if (class->key
== key
) {
709 * Huh! same key, different name? Did someone trample
710 * on some memory? We're most confused.
712 WARN_ON_ONCE(class->name
!= lock
->name
);
721 * Register a lock's class in the hash-table, if the class is not present
722 * yet. Otherwise we look it up. We cache the result in the lock object
723 * itself, so actual lookup of the hash should be once per lock object.
725 static inline struct lock_class
*
726 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
728 struct lockdep_subclass_key
*key
;
729 struct list_head
*hash_head
;
730 struct lock_class
*class;
733 class = look_up_lock_class(lock
, subclass
);
735 goto out_set_class_cache
;
738 * Debug-check: all keys must be persistent!
740 if (!static_obj(lock
->key
)) {
742 printk("INFO: trying to register non-static key.\n");
743 printk("the code is fine but needs lockdep annotation.\n");
744 printk("turning off the locking correctness validator.\n");
750 key
= lock
->key
->subkeys
+ subclass
;
751 hash_head
= classhashentry(key
);
753 raw_local_irq_save(flags
);
755 raw_local_irq_restore(flags
);
759 * We have to do the hash-walk again, to avoid races
762 list_for_each_entry(class, hash_head
, hash_entry
)
763 if (class->key
== key
)
766 * Allocate a new key from the static array, and add it to
769 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
770 if (!debug_locks_off_graph_unlock()) {
771 raw_local_irq_restore(flags
);
774 raw_local_irq_restore(flags
);
776 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
780 class = lock_classes
+ nr_lock_classes
++;
781 debug_atomic_inc(nr_unused_locks
);
783 class->name
= lock
->name
;
784 class->subclass
= subclass
;
785 INIT_LIST_HEAD(&class->lock_entry
);
786 INIT_LIST_HEAD(&class->locks_before
);
787 INIT_LIST_HEAD(&class->locks_after
);
788 class->name_version
= count_matching_names(class);
790 * We use RCU's safe list-add method to make
791 * parallel walking of the hash-list safe:
793 list_add_tail_rcu(&class->hash_entry
, hash_head
);
795 * Add it to the global list of classes:
797 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
799 if (verbose(class)) {
801 raw_local_irq_restore(flags
);
803 printk("\nnew class %p: %s", class->key
, class->name
);
804 if (class->name_version
> 1)
805 printk("#%d", class->name_version
);
809 raw_local_irq_save(flags
);
811 raw_local_irq_restore(flags
);
817 raw_local_irq_restore(flags
);
820 if (!subclass
|| force
)
821 lock
->class_cache
[0] = class;
822 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
823 lock
->class_cache
[subclass
] = class;
826 * Hash collision, did we smoke some? We found a class with a matching
827 * hash but the subclass -- which is hashed in -- didn't match.
829 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
835 #ifdef CONFIG_PROVE_LOCKING
837 * Allocate a lockdep entry. (assumes the graph_lock held, returns
838 * with NULL on failure)
840 static struct lock_list
*alloc_list_entry(void)
842 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
843 if (!debug_locks_off_graph_unlock())
846 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
850 return list_entries
+ nr_list_entries
++;
854 * Add a new dependency to the head of the list:
856 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
857 struct list_head
*head
, unsigned long ip
,
858 int distance
, struct stack_trace
*trace
)
860 struct lock_list
*entry
;
862 * Lock not present yet - get a new dependency struct and
863 * add it to the list:
865 entry
= alloc_list_entry();
870 entry
->distance
= distance
;
871 entry
->trace
= *trace
;
873 * Since we never remove from the dependency list, the list can
874 * be walked lockless by other CPUs, it's only allocation
875 * that must be protected by the spinlock. But this also means
876 * we must make new entries visible only once writes to the
877 * entry become visible - hence the RCU op:
879 list_add_tail_rcu(&entry
->entry
, head
);
885 * For good efficiency of modular, we use power of 2
887 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
888 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
891 * The circular_queue and helpers is used to implement the
892 * breadth-first search(BFS)algorithem, by which we can build
893 * the shortest path from the next lock to be acquired to the
894 * previous held lock if there is a circular between them.
896 struct circular_queue
{
897 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
898 unsigned int front
, rear
;
901 static struct circular_queue lock_cq
;
903 unsigned int max_bfs_queue_depth
;
905 static unsigned int lockdep_dependency_gen_id
;
907 static inline void __cq_init(struct circular_queue
*cq
)
909 cq
->front
= cq
->rear
= 0;
910 lockdep_dependency_gen_id
++;
913 static inline int __cq_empty(struct circular_queue
*cq
)
915 return (cq
->front
== cq
->rear
);
918 static inline int __cq_full(struct circular_queue
*cq
)
920 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
923 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
928 cq
->element
[cq
->rear
] = elem
;
929 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
933 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
938 *elem
= cq
->element
[cq
->front
];
939 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
943 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
945 return (cq
->rear
- cq
->front
) & CQ_MASK
;
948 static inline void mark_lock_accessed(struct lock_list
*lock
,
949 struct lock_list
*parent
)
953 nr
= lock
- list_entries
;
954 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
955 lock
->parent
= parent
;
956 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
959 static inline unsigned long lock_accessed(struct lock_list
*lock
)
963 nr
= lock
- list_entries
;
964 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
965 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
968 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
970 return child
->parent
;
973 static inline int get_lock_depth(struct lock_list
*child
)
976 struct lock_list
*parent
;
978 while ((parent
= get_lock_parent(child
))) {
985 static int __bfs(struct lock_list
*source_entry
,
987 int (*match
)(struct lock_list
*entry
, void *data
),
988 struct lock_list
**target_entry
,
991 struct lock_list
*entry
;
992 struct list_head
*head
;
993 struct circular_queue
*cq
= &lock_cq
;
996 if (match(source_entry
, data
)) {
997 *target_entry
= source_entry
;
1003 head
= &source_entry
->class->locks_after
;
1005 head
= &source_entry
->class->locks_before
;
1007 if (list_empty(head
))
1011 __cq_enqueue(cq
, (unsigned long)source_entry
);
1013 while (!__cq_empty(cq
)) {
1014 struct lock_list
*lock
;
1016 __cq_dequeue(cq
, (unsigned long *)&lock
);
1024 head
= &lock
->class->locks_after
;
1026 head
= &lock
->class->locks_before
;
1028 list_for_each_entry(entry
, head
, entry
) {
1029 if (!lock_accessed(entry
)) {
1030 unsigned int cq_depth
;
1031 mark_lock_accessed(entry
, lock
);
1032 if (match(entry
, data
)) {
1033 *target_entry
= entry
;
1038 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1042 cq_depth
= __cq_get_elem_count(cq
);
1043 if (max_bfs_queue_depth
< cq_depth
)
1044 max_bfs_queue_depth
= cq_depth
;
1052 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1054 int (*match
)(struct lock_list
*entry
, void *data
),
1055 struct lock_list
**target_entry
)
1057 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1061 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1063 int (*match
)(struct lock_list
*entry
, void *data
),
1064 struct lock_list
**target_entry
)
1066 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1071 * Recursive, forwards-direction lock-dependency checking, used for
1072 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1077 * Print a dependency chain entry (this is only done when a deadlock
1078 * has been detected):
1081 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1083 if (debug_locks_silent
)
1085 printk("\n-> #%u", depth
);
1086 print_lock_name(target
->class);
1088 print_stack_trace(&target
->trace
, 6);
1094 print_circular_lock_scenario(struct held_lock
*src
,
1095 struct held_lock
*tgt
,
1096 struct lock_list
*prt
)
1098 struct lock_class
*source
= hlock_class(src
);
1099 struct lock_class
*target
= hlock_class(tgt
);
1100 struct lock_class
*parent
= prt
->class;
1103 * A direct locking problem where unsafe_class lock is taken
1104 * directly by safe_class lock, then all we need to show
1105 * is the deadlock scenario, as it is obvious that the
1106 * unsafe lock is taken under the safe lock.
1108 * But if there is a chain instead, where the safe lock takes
1109 * an intermediate lock (middle_class) where this lock is
1110 * not the same as the safe lock, then the lock chain is
1111 * used to describe the problem. Otherwise we would need
1112 * to show a different CPU case for each link in the chain
1113 * from the safe_class lock to the unsafe_class lock.
1115 if (parent
!= source
) {
1116 printk("Chain exists of:\n ");
1117 __print_lock_name(source
);
1119 __print_lock_name(parent
);
1121 __print_lock_name(target
);
1125 printk(" Possible unsafe locking scenario:\n\n");
1126 printk(" CPU0 CPU1\n");
1127 printk(" ---- ----\n");
1129 __print_lock_name(target
);
1132 __print_lock_name(parent
);
1135 __print_lock_name(target
);
1138 __print_lock_name(source
);
1140 printk("\n *** DEADLOCK ***\n\n");
1144 * When a circular dependency is detected, print the
1148 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1149 struct held_lock
*check_src
,
1150 struct held_lock
*check_tgt
)
1152 struct task_struct
*curr
= current
;
1154 if (debug_locks_silent
)
1158 printk("======================================================\n");
1159 printk("[ INFO: possible circular locking dependency detected ]\n");
1160 print_kernel_ident();
1161 printk("-------------------------------------------------------\n");
1162 printk("%s/%d is trying to acquire lock:\n",
1163 curr
->comm
, task_pid_nr(curr
));
1164 print_lock(check_src
);
1165 printk("\nbut task is already holding lock:\n");
1166 print_lock(check_tgt
);
1167 printk("\nwhich lock already depends on the new lock.\n\n");
1168 printk("\nthe existing dependency chain (in reverse order) is:\n");
1170 print_circular_bug_entry(entry
, depth
);
1175 static inline int class_equal(struct lock_list
*entry
, void *data
)
1177 return entry
->class == data
;
1180 static noinline
int print_circular_bug(struct lock_list
*this,
1181 struct lock_list
*target
,
1182 struct held_lock
*check_src
,
1183 struct held_lock
*check_tgt
)
1185 struct task_struct
*curr
= current
;
1186 struct lock_list
*parent
;
1187 struct lock_list
*first_parent
;
1190 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1193 if (!save_trace(&this->trace
))
1196 depth
= get_lock_depth(target
);
1198 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1200 parent
= get_lock_parent(target
);
1201 first_parent
= parent
;
1204 print_circular_bug_entry(parent
, --depth
);
1205 parent
= get_lock_parent(parent
);
1208 printk("\nother info that might help us debug this:\n\n");
1209 print_circular_lock_scenario(check_src
, check_tgt
,
1212 lockdep_print_held_locks(curr
);
1214 printk("\nstack backtrace:\n");
1220 static noinline
int print_bfs_bug(int ret
)
1222 if (!debug_locks_off_graph_unlock())
1226 * Breadth-first-search failed, graph got corrupted?
1228 WARN(1, "lockdep bfs error:%d\n", ret
);
1233 static int noop_count(struct lock_list
*entry
, void *data
)
1235 (*(unsigned long *)data
)++;
1239 static unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1241 unsigned long count
= 0;
1242 struct lock_list
*uninitialized_var(target_entry
);
1244 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1248 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1250 unsigned long ret
, flags
;
1251 struct lock_list
this;
1256 local_irq_save(flags
);
1257 arch_spin_lock(&lockdep_lock
);
1258 ret
= __lockdep_count_forward_deps(&this);
1259 arch_spin_unlock(&lockdep_lock
);
1260 local_irq_restore(flags
);
1265 static unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1267 unsigned long count
= 0;
1268 struct lock_list
*uninitialized_var(target_entry
);
1270 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1275 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1277 unsigned long ret
, flags
;
1278 struct lock_list
this;
1283 local_irq_save(flags
);
1284 arch_spin_lock(&lockdep_lock
);
1285 ret
= __lockdep_count_backward_deps(&this);
1286 arch_spin_unlock(&lockdep_lock
);
1287 local_irq_restore(flags
);
1293 * Prove that the dependency graph starting at <entry> can not
1294 * lead to <target>. Print an error and return 0 if it does.
1297 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1298 struct lock_list
**target_entry
)
1302 debug_atomic_inc(nr_cyclic_checks
);
1304 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1309 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1311 * Forwards and backwards subgraph searching, for the purposes of
1312 * proving that two subgraphs can be connected by a new dependency
1313 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1316 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1318 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1324 * Find a node in the forwards-direction dependency sub-graph starting
1325 * at @root->class that matches @bit.
1327 * Return 0 if such a node exists in the subgraph, and put that node
1328 * into *@target_entry.
1330 * Return 1 otherwise and keep *@target_entry unchanged.
1331 * Return <0 on error.
1334 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1335 struct lock_list
**target_entry
)
1339 debug_atomic_inc(nr_find_usage_forwards_checks
);
1341 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1347 * Find a node in the backwards-direction dependency sub-graph starting
1348 * at @root->class that matches @bit.
1350 * Return 0 if such a node exists in the subgraph, and put that node
1351 * into *@target_entry.
1353 * Return 1 otherwise and keep *@target_entry unchanged.
1354 * Return <0 on error.
1357 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1358 struct lock_list
**target_entry
)
1362 debug_atomic_inc(nr_find_usage_backwards_checks
);
1364 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1369 static void print_lock_class_header(struct lock_class
*class, int depth
)
1373 printk("%*s->", depth
, "");
1374 print_lock_name(class);
1375 printk(" ops: %lu", class->ops
);
1378 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1379 if (class->usage_mask
& (1 << bit
)) {
1382 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1383 len
+= printk(" at:\n");
1384 print_stack_trace(class->usage_traces
+ bit
, len
);
1387 printk("%*s }\n", depth
, "");
1389 printk("%*s ... key at: ",depth
,"");
1390 print_ip_sym((unsigned long)class->key
);
1394 * printk the shortest lock dependencies from @start to @end in reverse order:
1397 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1398 struct lock_list
*root
)
1400 struct lock_list
*entry
= leaf
;
1403 /*compute depth from generated tree by BFS*/
1404 depth
= get_lock_depth(leaf
);
1407 print_lock_class_header(entry
->class, depth
);
1408 printk("%*s ... acquired at:\n", depth
, "");
1409 print_stack_trace(&entry
->trace
, 2);
1412 if (depth
== 0 && (entry
!= root
)) {
1413 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1417 entry
= get_lock_parent(entry
);
1419 } while (entry
&& (depth
>= 0));
1425 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1426 struct lock_list
*unsafe_entry
,
1427 struct lock_class
*prev_class
,
1428 struct lock_class
*next_class
)
1430 struct lock_class
*safe_class
= safe_entry
->class;
1431 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1432 struct lock_class
*middle_class
= prev_class
;
1434 if (middle_class
== safe_class
)
1435 middle_class
= next_class
;
1438 * A direct locking problem where unsafe_class lock is taken
1439 * directly by safe_class lock, then all we need to show
1440 * is the deadlock scenario, as it is obvious that the
1441 * unsafe lock is taken under the safe lock.
1443 * But if there is a chain instead, where the safe lock takes
1444 * an intermediate lock (middle_class) where this lock is
1445 * not the same as the safe lock, then the lock chain is
1446 * used to describe the problem. Otherwise we would need
1447 * to show a different CPU case for each link in the chain
1448 * from the safe_class lock to the unsafe_class lock.
1450 if (middle_class
!= unsafe_class
) {
1451 printk("Chain exists of:\n ");
1452 __print_lock_name(safe_class
);
1454 __print_lock_name(middle_class
);
1456 __print_lock_name(unsafe_class
);
1460 printk(" Possible interrupt unsafe locking scenario:\n\n");
1461 printk(" CPU0 CPU1\n");
1462 printk(" ---- ----\n");
1464 __print_lock_name(unsafe_class
);
1466 printk(" local_irq_disable();\n");
1468 __print_lock_name(safe_class
);
1471 __print_lock_name(middle_class
);
1473 printk(" <Interrupt>\n");
1475 __print_lock_name(safe_class
);
1477 printk("\n *** DEADLOCK ***\n\n");
1481 print_bad_irq_dependency(struct task_struct
*curr
,
1482 struct lock_list
*prev_root
,
1483 struct lock_list
*next_root
,
1484 struct lock_list
*backwards_entry
,
1485 struct lock_list
*forwards_entry
,
1486 struct held_lock
*prev
,
1487 struct held_lock
*next
,
1488 enum lock_usage_bit bit1
,
1489 enum lock_usage_bit bit2
,
1490 const char *irqclass
)
1492 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1496 printk("======================================================\n");
1497 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1498 irqclass
, irqclass
);
1499 print_kernel_ident();
1500 printk("------------------------------------------------------\n");
1501 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1502 curr
->comm
, task_pid_nr(curr
),
1503 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1504 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1505 curr
->hardirqs_enabled
,
1506 curr
->softirqs_enabled
);
1509 printk("\nand this task is already holding:\n");
1511 printk("which would create a new lock dependency:\n");
1512 print_lock_name(hlock_class(prev
));
1514 print_lock_name(hlock_class(next
));
1517 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1519 print_lock_name(backwards_entry
->class);
1520 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1522 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1524 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1525 print_lock_name(forwards_entry
->class);
1526 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1529 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1531 printk("\nother info that might help us debug this:\n\n");
1532 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1533 hlock_class(prev
), hlock_class(next
));
1535 lockdep_print_held_locks(curr
);
1537 printk("\nthe dependencies between %s-irq-safe lock", irqclass
);
1538 printk(" and the holding lock:\n");
1539 if (!save_trace(&prev_root
->trace
))
1541 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1543 printk("\nthe dependencies between the lock to be acquired");
1544 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1545 if (!save_trace(&next_root
->trace
))
1547 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1549 printk("\nstack backtrace:\n");
1556 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1557 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1558 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1561 struct lock_list
this, that
;
1562 struct lock_list
*uninitialized_var(target_entry
);
1563 struct lock_list
*uninitialized_var(target_entry1
);
1567 this.class = hlock_class(prev
);
1568 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1570 return print_bfs_bug(ret
);
1575 that
.class = hlock_class(next
);
1576 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1578 return print_bfs_bug(ret
);
1582 return print_bad_irq_dependency(curr
, &this, &that
,
1583 target_entry
, target_entry1
,
1585 bit_backwards
, bit_forwards
, irqclass
);
1588 static const char *state_names
[] = {
1589 #define LOCKDEP_STATE(__STATE) \
1590 __stringify(__STATE),
1591 #include "lockdep_states.h"
1592 #undef LOCKDEP_STATE
1595 static const char *state_rnames
[] = {
1596 #define LOCKDEP_STATE(__STATE) \
1597 __stringify(__STATE)"-READ",
1598 #include "lockdep_states.h"
1599 #undef LOCKDEP_STATE
1602 static inline const char *state_name(enum lock_usage_bit bit
)
1604 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1607 static int exclusive_bit(int new_bit
)
1615 * bit 0 - write/read
1616 * bit 1 - used_in/enabled
1620 int state
= new_bit
& ~3;
1621 int dir
= new_bit
& 2;
1624 * keep state, bit flip the direction and strip read.
1626 return state
| (dir
^ 2);
1629 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1630 struct held_lock
*next
, enum lock_usage_bit bit
)
1633 * Prove that the new dependency does not connect a hardirq-safe
1634 * lock with a hardirq-unsafe lock - to achieve this we search
1635 * the backwards-subgraph starting at <prev>, and the
1636 * forwards-subgraph starting at <next>:
1638 if (!check_usage(curr
, prev
, next
, bit
,
1639 exclusive_bit(bit
), state_name(bit
)))
1645 * Prove that the new dependency does not connect a hardirq-safe-read
1646 * lock with a hardirq-unsafe lock - to achieve this we search
1647 * the backwards-subgraph starting at <prev>, and the
1648 * forwards-subgraph starting at <next>:
1650 if (!check_usage(curr
, prev
, next
, bit
,
1651 exclusive_bit(bit
), state_name(bit
)))
1658 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1659 struct held_lock
*next
)
1661 #define LOCKDEP_STATE(__STATE) \
1662 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1664 #include "lockdep_states.h"
1665 #undef LOCKDEP_STATE
1670 static void inc_chains(void)
1672 if (current
->hardirq_context
)
1673 nr_hardirq_chains
++;
1675 if (current
->softirq_context
)
1676 nr_softirq_chains
++;
1678 nr_process_chains
++;
1685 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1686 struct held_lock
*next
)
1691 static inline void inc_chains(void)
1693 nr_process_chains
++;
1699 print_deadlock_scenario(struct held_lock
*nxt
,
1700 struct held_lock
*prv
)
1702 struct lock_class
*next
= hlock_class(nxt
);
1703 struct lock_class
*prev
= hlock_class(prv
);
1705 printk(" Possible unsafe locking scenario:\n\n");
1709 __print_lock_name(prev
);
1712 __print_lock_name(next
);
1714 printk("\n *** DEADLOCK ***\n\n");
1715 printk(" May be due to missing lock nesting notation\n\n");
1719 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1720 struct held_lock
*next
)
1722 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1726 printk("=============================================\n");
1727 printk("[ INFO: possible recursive locking detected ]\n");
1728 print_kernel_ident();
1729 printk("---------------------------------------------\n");
1730 printk("%s/%d is trying to acquire lock:\n",
1731 curr
->comm
, task_pid_nr(curr
));
1733 printk("\nbut task is already holding lock:\n");
1736 printk("\nother info that might help us debug this:\n");
1737 print_deadlock_scenario(next
, prev
);
1738 lockdep_print_held_locks(curr
);
1740 printk("\nstack backtrace:\n");
1747 * Check whether we are holding such a class already.
1749 * (Note that this has to be done separately, because the graph cannot
1750 * detect such classes of deadlocks.)
1752 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1755 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1756 struct lockdep_map
*next_instance
, int read
)
1758 struct held_lock
*prev
;
1759 struct held_lock
*nest
= NULL
;
1762 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1763 prev
= curr
->held_locks
+ i
;
1765 if (prev
->instance
== next
->nest_lock
)
1768 if (hlock_class(prev
) != hlock_class(next
))
1772 * Allow read-after-read recursion of the same
1773 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1775 if ((read
== 2) && prev
->read
)
1779 * We're holding the nest_lock, which serializes this lock's
1780 * nesting behaviour.
1785 return print_deadlock_bug(curr
, prev
, next
);
1791 * There was a chain-cache miss, and we are about to add a new dependency
1792 * to a previous lock. We recursively validate the following rules:
1794 * - would the adding of the <prev> -> <next> dependency create a
1795 * circular dependency in the graph? [== circular deadlock]
1797 * - does the new prev->next dependency connect any hardirq-safe lock
1798 * (in the full backwards-subgraph starting at <prev>) with any
1799 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1800 * <next>)? [== illegal lock inversion with hardirq contexts]
1802 * - does the new prev->next dependency connect any softirq-safe lock
1803 * (in the full backwards-subgraph starting at <prev>) with any
1804 * softirq-unsafe lock (in the full forwards-subgraph starting at
1805 * <next>)? [== illegal lock inversion with softirq contexts]
1807 * any of these scenarios could lead to a deadlock.
1809 * Then if all the validations pass, we add the forwards and backwards
1813 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1814 struct held_lock
*next
, int distance
, int trylock_loop
)
1816 struct lock_list
*entry
;
1818 struct lock_list
this;
1819 struct lock_list
*uninitialized_var(target_entry
);
1821 * Static variable, serialized by the graph_lock().
1823 * We use this static variable to save the stack trace in case
1824 * we call into this function multiple times due to encountering
1825 * trylocks in the held lock stack.
1827 static struct stack_trace trace
;
1830 * Prove that the new <prev> -> <next> dependency would not
1831 * create a circular dependency in the graph. (We do this by
1832 * forward-recursing into the graph starting at <next>, and
1833 * checking whether we can reach <prev>.)
1835 * We are using global variables to control the recursion, to
1836 * keep the stackframe size of the recursive functions low:
1838 this.class = hlock_class(next
);
1840 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1842 return print_circular_bug(&this, target_entry
, next
, prev
);
1843 else if (unlikely(ret
< 0))
1844 return print_bfs_bug(ret
);
1846 if (!check_prev_add_irq(curr
, prev
, next
))
1850 * For recursive read-locks we do all the dependency checks,
1851 * but we dont store read-triggered dependencies (only
1852 * write-triggered dependencies). This ensures that only the
1853 * write-side dependencies matter, and that if for example a
1854 * write-lock never takes any other locks, then the reads are
1855 * equivalent to a NOP.
1857 if (next
->read
== 2 || prev
->read
== 2)
1860 * Is the <prev> -> <next> dependency already present?
1862 * (this may occur even though this is a new chain: consider
1863 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1864 * chains - the second one will be new, but L1 already has
1865 * L2 added to its dependency list, due to the first chain.)
1867 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1868 if (entry
->class == hlock_class(next
)) {
1870 entry
->distance
= 1;
1875 if (!trylock_loop
&& !save_trace(&trace
))
1879 * Ok, all validations passed, add the new lock
1880 * to the previous lock's dependency list:
1882 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1883 &hlock_class(prev
)->locks_after
,
1884 next
->acquire_ip
, distance
, &trace
);
1889 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1890 &hlock_class(next
)->locks_before
,
1891 next
->acquire_ip
, distance
, &trace
);
1896 * Debugging printouts:
1898 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1900 printk("\n new dependency: ");
1901 print_lock_name(hlock_class(prev
));
1903 print_lock_name(hlock_class(next
));
1906 return graph_lock();
1912 * Add the dependency to all directly-previous locks that are 'relevant'.
1913 * The ones that are relevant are (in increasing distance from curr):
1914 * all consecutive trylock entries and the final non-trylock entry - or
1915 * the end of this context's lock-chain - whichever comes first.
1918 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1920 int depth
= curr
->lockdep_depth
;
1921 int trylock_loop
= 0;
1922 struct held_lock
*hlock
;
1927 * Depth must not be zero for a non-head lock:
1932 * At least two relevant locks must exist for this
1935 if (curr
->held_locks
[depth
].irq_context
!=
1936 curr
->held_locks
[depth
-1].irq_context
)
1940 int distance
= curr
->lockdep_depth
- depth
+ 1;
1941 hlock
= curr
->held_locks
+ depth
- 1;
1943 * Only non-recursive-read entries get new dependencies
1946 if (hlock
->read
!= 2 && hlock
->check
) {
1947 if (!check_prev_add(curr
, hlock
, next
,
1948 distance
, trylock_loop
))
1951 * Stop after the first non-trylock entry,
1952 * as non-trylock entries have added their
1953 * own direct dependencies already, so this
1954 * lock is connected to them indirectly:
1956 if (!hlock
->trylock
)
1961 * End of lock-stack?
1966 * Stop the search if we cross into another context:
1968 if (curr
->held_locks
[depth
].irq_context
!=
1969 curr
->held_locks
[depth
-1].irq_context
)
1975 if (!debug_locks_off_graph_unlock())
1979 * Clearly we all shouldn't be here, but since we made it we
1980 * can reliable say we messed up our state. See the above two
1981 * gotos for reasons why we could possibly end up here.
1988 unsigned long nr_lock_chains
;
1989 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1990 int nr_chain_hlocks
;
1991 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1993 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1995 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1999 * Look up a dependency chain. If the key is not present yet then
2000 * add it and return 1 - in this case the new dependency chain is
2001 * validated. If the key is already hashed, return 0.
2002 * (On return with 1 graph_lock is held.)
2004 static inline int lookup_chain_cache(struct task_struct
*curr
,
2005 struct held_lock
*hlock
,
2008 struct lock_class
*class = hlock_class(hlock
);
2009 struct list_head
*hash_head
= chainhashentry(chain_key
);
2010 struct lock_chain
*chain
;
2011 struct held_lock
*hlock_curr
;
2015 * We might need to take the graph lock, ensure we've got IRQs
2016 * disabled to make this an IRQ-safe lock.. for recursion reasons
2017 * lockdep won't complain about its own locking errors.
2019 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2022 * We can walk it lock-free, because entries only get added
2025 list_for_each_entry(chain
, hash_head
, entry
) {
2026 if (chain
->chain_key
== chain_key
) {
2028 debug_atomic_inc(chain_lookup_hits
);
2029 if (very_verbose(class))
2030 printk("\nhash chain already cached, key: "
2031 "%016Lx tail class: [%p] %s\n",
2032 (unsigned long long)chain_key
,
2033 class->key
, class->name
);
2037 if (very_verbose(class))
2038 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2039 (unsigned long long)chain_key
, class->key
, class->name
);
2041 * Allocate a new chain entry from the static array, and add
2047 * We have to walk the chain again locked - to avoid duplicates:
2049 list_for_each_entry(chain
, hash_head
, entry
) {
2050 if (chain
->chain_key
== chain_key
) {
2055 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2056 if (!debug_locks_off_graph_unlock())
2059 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2063 chain
= lock_chains
+ nr_lock_chains
++;
2064 chain
->chain_key
= chain_key
;
2065 chain
->irq_context
= hlock
->irq_context
;
2066 /* Find the first held_lock of current chain */
2067 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2068 hlock_curr
= curr
->held_locks
+ i
;
2069 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2073 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2074 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2075 chain
->base
= nr_chain_hlocks
;
2076 nr_chain_hlocks
+= chain
->depth
;
2077 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2078 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2079 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2081 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2083 list_add_tail_rcu(&chain
->entry
, hash_head
);
2084 debug_atomic_inc(chain_lookup_misses
);
2090 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2091 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2094 * Trylock needs to maintain the stack of held locks, but it
2095 * does not add new dependencies, because trylock can be done
2098 * We look up the chain_key and do the O(N^2) check and update of
2099 * the dependencies only if this is a new dependency chain.
2100 * (If lookup_chain_cache() returns with 1 it acquires
2101 * graph_lock for us)
2103 if (!hlock
->trylock
&& hlock
->check
&&
2104 lookup_chain_cache(curr
, hlock
, chain_key
)) {
2106 * Check whether last held lock:
2108 * - is irq-safe, if this lock is irq-unsafe
2109 * - is softirq-safe, if this lock is hardirq-unsafe
2111 * And check whether the new lock's dependency graph
2112 * could lead back to the previous lock.
2114 * any of these scenarios could lead to a deadlock. If
2117 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2122 * Mark recursive read, as we jump over it when
2123 * building dependencies (just like we jump over
2129 * Add dependency only if this lock is not the head
2130 * of the chain, and if it's not a secondary read-lock:
2132 if (!chain_head
&& ret
!= 2)
2133 if (!check_prevs_add(curr
, hlock
))
2137 /* after lookup_chain_cache(): */
2138 if (unlikely(!debug_locks
))
2144 static inline int validate_chain(struct task_struct
*curr
,
2145 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2146 int chain_head
, u64 chain_key
)
2153 * We are building curr_chain_key incrementally, so double-check
2154 * it from scratch, to make sure that it's done correctly:
2156 static void check_chain_key(struct task_struct
*curr
)
2158 #ifdef CONFIG_DEBUG_LOCKDEP
2159 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2163 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2164 hlock
= curr
->held_locks
+ i
;
2165 if (chain_key
!= hlock
->prev_chain_key
) {
2168 * We got mighty confused, our chain keys don't match
2169 * with what we expect, someone trample on our task state?
2171 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2172 curr
->lockdep_depth
, i
,
2173 (unsigned long long)chain_key
,
2174 (unsigned long long)hlock
->prev_chain_key
);
2177 id
= hlock
->class_idx
- 1;
2179 * Whoops ran out of static storage again?
2181 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2184 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2185 hlock
->irq_context
))
2187 chain_key
= iterate_chain_key(chain_key
, id
);
2190 if (chain_key
!= curr
->curr_chain_key
) {
2193 * More smoking hash instead of calculating it, damn see these
2194 * numbers float.. I bet that a pink elephant stepped on my memory.
2196 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2197 curr
->lockdep_depth
, i
,
2198 (unsigned long long)chain_key
,
2199 (unsigned long long)curr
->curr_chain_key
);
2205 print_usage_bug_scenario(struct held_lock
*lock
)
2207 struct lock_class
*class = hlock_class(lock
);
2209 printk(" Possible unsafe locking scenario:\n\n");
2213 __print_lock_name(class);
2215 printk(" <Interrupt>\n");
2217 __print_lock_name(class);
2219 printk("\n *** DEADLOCK ***\n\n");
2223 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2224 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2226 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2230 printk("=================================\n");
2231 printk("[ INFO: inconsistent lock state ]\n");
2232 print_kernel_ident();
2233 printk("---------------------------------\n");
2235 printk("inconsistent {%s} -> {%s} usage.\n",
2236 usage_str
[prev_bit
], usage_str
[new_bit
]);
2238 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2239 curr
->comm
, task_pid_nr(curr
),
2240 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2241 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2242 trace_hardirqs_enabled(curr
),
2243 trace_softirqs_enabled(curr
));
2246 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2247 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2249 print_irqtrace_events(curr
);
2250 printk("\nother info that might help us debug this:\n");
2251 print_usage_bug_scenario(this);
2253 lockdep_print_held_locks(curr
);
2255 printk("\nstack backtrace:\n");
2262 * Print out an error if an invalid bit is set:
2265 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2266 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2268 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2269 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2273 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2274 enum lock_usage_bit new_bit
);
2276 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2279 * print irq inversion bug:
2282 print_irq_inversion_bug(struct task_struct
*curr
,
2283 struct lock_list
*root
, struct lock_list
*other
,
2284 struct held_lock
*this, int forwards
,
2285 const char *irqclass
)
2287 struct lock_list
*entry
= other
;
2288 struct lock_list
*middle
= NULL
;
2291 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2295 printk("=========================================================\n");
2296 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
2297 print_kernel_ident();
2298 printk("---------------------------------------------------------\n");
2299 printk("%s/%d just changed the state of lock:\n",
2300 curr
->comm
, task_pid_nr(curr
));
2303 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2305 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2306 print_lock_name(other
->class);
2307 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2309 printk("\nother info that might help us debug this:\n");
2311 /* Find a middle lock (if one exists) */
2312 depth
= get_lock_depth(other
);
2314 if (depth
== 0 && (entry
!= root
)) {
2315 printk("lockdep:%s bad path found in chain graph\n", __func__
);
2319 entry
= get_lock_parent(entry
);
2321 } while (entry
&& entry
!= root
&& (depth
>= 0));
2323 print_irq_lock_scenario(root
, other
,
2324 middle
? middle
->class : root
->class, other
->class);
2326 print_irq_lock_scenario(other
, root
,
2327 middle
? middle
->class : other
->class, root
->class);
2329 lockdep_print_held_locks(curr
);
2331 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2332 if (!save_trace(&root
->trace
))
2334 print_shortest_lock_dependencies(other
, root
);
2336 printk("\nstack backtrace:\n");
2343 * Prove that in the forwards-direction subgraph starting at <this>
2344 * there is no lock matching <mask>:
2347 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2348 enum lock_usage_bit bit
, const char *irqclass
)
2351 struct lock_list root
;
2352 struct lock_list
*uninitialized_var(target_entry
);
2355 root
.class = hlock_class(this);
2356 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2358 return print_bfs_bug(ret
);
2362 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2367 * Prove that in the backwards-direction subgraph starting at <this>
2368 * there is no lock matching <mask>:
2371 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2372 enum lock_usage_bit bit
, const char *irqclass
)
2375 struct lock_list root
;
2376 struct lock_list
*uninitialized_var(target_entry
);
2379 root
.class = hlock_class(this);
2380 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2382 return print_bfs_bug(ret
);
2386 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2390 void print_irqtrace_events(struct task_struct
*curr
)
2392 printk("irq event stamp: %u\n", curr
->irq_events
);
2393 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
2394 print_ip_sym(curr
->hardirq_enable_ip
);
2395 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
2396 print_ip_sym(curr
->hardirq_disable_ip
);
2397 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
2398 print_ip_sym(curr
->softirq_enable_ip
);
2399 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
2400 print_ip_sym(curr
->softirq_disable_ip
);
2403 static int HARDIRQ_verbose(struct lock_class
*class)
2406 return class_filter(class);
2411 static int SOFTIRQ_verbose(struct lock_class
*class)
2414 return class_filter(class);
2419 static int RECLAIM_FS_verbose(struct lock_class
*class)
2422 return class_filter(class);
2427 #define STRICT_READ_CHECKS 1
2429 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2430 #define LOCKDEP_STATE(__STATE) \
2432 #include "lockdep_states.h"
2433 #undef LOCKDEP_STATE
2436 static inline int state_verbose(enum lock_usage_bit bit
,
2437 struct lock_class
*class)
2439 return state_verbose_f
[bit
>> 2](class);
2442 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2443 enum lock_usage_bit bit
, const char *name
);
2446 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2447 enum lock_usage_bit new_bit
)
2449 int excl_bit
= exclusive_bit(new_bit
);
2450 int read
= new_bit
& 1;
2451 int dir
= new_bit
& 2;
2454 * mark USED_IN has to look forwards -- to ensure no dependency
2455 * has ENABLED state, which would allow recursion deadlocks.
2457 * mark ENABLED has to look backwards -- to ensure no dependee
2458 * has USED_IN state, which, again, would allow recursion deadlocks.
2460 check_usage_f usage
= dir
?
2461 check_usage_backwards
: check_usage_forwards
;
2464 * Validate that this particular lock does not have conflicting
2467 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2471 * Validate that the lock dependencies don't have conflicting usage
2474 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2475 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2479 * Check for read in write conflicts
2482 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2485 if (STRICT_READ_CHECKS
&&
2486 !usage(curr
, this, excl_bit
+ 1,
2487 state_name(new_bit
+ 1)))
2491 if (state_verbose(new_bit
, hlock_class(this)))
2498 #define LOCKDEP_STATE(__STATE) __STATE,
2499 #include "lockdep_states.h"
2500 #undef LOCKDEP_STATE
2504 * Mark all held locks with a usage bit:
2507 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2509 enum lock_usage_bit usage_bit
;
2510 struct held_lock
*hlock
;
2513 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2514 hlock
= curr
->held_locks
+ i
;
2516 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2518 usage_bit
+= 1; /* READ */
2520 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2525 if (!mark_lock(curr
, hlock
, usage_bit
))
2533 * Hardirqs will be enabled:
2535 static void __trace_hardirqs_on_caller(unsigned long ip
)
2537 struct task_struct
*curr
= current
;
2539 /* we'll do an OFF -> ON transition: */
2540 curr
->hardirqs_enabled
= 1;
2543 * We are going to turn hardirqs on, so set the
2544 * usage bit for all held locks:
2546 if (!mark_held_locks(curr
, HARDIRQ
))
2549 * If we have softirqs enabled, then set the usage
2550 * bit for all held locks. (disabled hardirqs prevented
2551 * this bit from being set before)
2553 if (curr
->softirqs_enabled
)
2554 if (!mark_held_locks(curr
, SOFTIRQ
))
2557 curr
->hardirq_enable_ip
= ip
;
2558 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2559 debug_atomic_inc(hardirqs_on_events
);
2562 __visible
void trace_hardirqs_on_caller(unsigned long ip
)
2564 time_hardirqs_on(CALLER_ADDR0
, ip
);
2566 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2569 if (unlikely(current
->hardirqs_enabled
)) {
2571 * Neither irq nor preemption are disabled here
2572 * so this is racy by nature but losing one hit
2573 * in a stat is not a big deal.
2575 __debug_atomic_inc(redundant_hardirqs_on
);
2580 * We're enabling irqs and according to our state above irqs weren't
2581 * already enabled, yet we find the hardware thinks they are in fact
2582 * enabled.. someone messed up their IRQ state tracing.
2584 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2588 * See the fine text that goes along with this variable definition.
2590 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2594 * Can't allow enabling interrupts while in an interrupt handler,
2595 * that's general bad form and such. Recursion, limited stack etc..
2597 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2600 current
->lockdep_recursion
= 1;
2601 __trace_hardirqs_on_caller(ip
);
2602 current
->lockdep_recursion
= 0;
2604 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2606 void trace_hardirqs_on(void)
2608 trace_hardirqs_on_caller(CALLER_ADDR0
);
2610 EXPORT_SYMBOL(trace_hardirqs_on
);
2613 * Hardirqs were disabled:
2615 __visible
void trace_hardirqs_off_caller(unsigned long ip
)
2617 struct task_struct
*curr
= current
;
2619 time_hardirqs_off(CALLER_ADDR0
, ip
);
2621 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2625 * So we're supposed to get called after you mask local IRQs, but for
2626 * some reason the hardware doesn't quite think you did a proper job.
2628 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2631 if (curr
->hardirqs_enabled
) {
2633 * We have done an ON -> OFF transition:
2635 curr
->hardirqs_enabled
= 0;
2636 curr
->hardirq_disable_ip
= ip
;
2637 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2638 debug_atomic_inc(hardirqs_off_events
);
2640 debug_atomic_inc(redundant_hardirqs_off
);
2642 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2644 void trace_hardirqs_off(void)
2646 trace_hardirqs_off_caller(CALLER_ADDR0
);
2648 EXPORT_SYMBOL(trace_hardirqs_off
);
2651 * Softirqs will be enabled:
2653 void trace_softirqs_on(unsigned long ip
)
2655 struct task_struct
*curr
= current
;
2657 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2661 * We fancy IRQs being disabled here, see softirq.c, avoids
2662 * funny state and nesting things.
2664 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2667 if (curr
->softirqs_enabled
) {
2668 debug_atomic_inc(redundant_softirqs_on
);
2672 current
->lockdep_recursion
= 1;
2674 * We'll do an OFF -> ON transition:
2676 curr
->softirqs_enabled
= 1;
2677 curr
->softirq_enable_ip
= ip
;
2678 curr
->softirq_enable_event
= ++curr
->irq_events
;
2679 debug_atomic_inc(softirqs_on_events
);
2681 * We are going to turn softirqs on, so set the
2682 * usage bit for all held locks, if hardirqs are
2685 if (curr
->hardirqs_enabled
)
2686 mark_held_locks(curr
, SOFTIRQ
);
2687 current
->lockdep_recursion
= 0;
2691 * Softirqs were disabled:
2693 void trace_softirqs_off(unsigned long ip
)
2695 struct task_struct
*curr
= current
;
2697 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2701 * We fancy IRQs being disabled here, see softirq.c
2703 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2706 if (curr
->softirqs_enabled
) {
2708 * We have done an ON -> OFF transition:
2710 curr
->softirqs_enabled
= 0;
2711 curr
->softirq_disable_ip
= ip
;
2712 curr
->softirq_disable_event
= ++curr
->irq_events
;
2713 debug_atomic_inc(softirqs_off_events
);
2715 * Whoops, we wanted softirqs off, so why aren't they?
2717 DEBUG_LOCKS_WARN_ON(!softirq_count());
2719 debug_atomic_inc(redundant_softirqs_off
);
2722 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2724 struct task_struct
*curr
= current
;
2726 if (unlikely(!debug_locks
))
2729 /* no reclaim without waiting on it */
2730 if (!(gfp_mask
& __GFP_WAIT
))
2733 /* this guy won't enter reclaim */
2734 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2737 /* We're only interested __GFP_FS allocations for now */
2738 if (!(gfp_mask
& __GFP_FS
))
2742 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2744 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2747 mark_held_locks(curr
, RECLAIM_FS
);
2750 static void check_flags(unsigned long flags
);
2752 void lockdep_trace_alloc(gfp_t gfp_mask
)
2754 unsigned long flags
;
2756 if (unlikely(current
->lockdep_recursion
))
2759 raw_local_irq_save(flags
);
2761 current
->lockdep_recursion
= 1;
2762 __lockdep_trace_alloc(gfp_mask
, flags
);
2763 current
->lockdep_recursion
= 0;
2764 raw_local_irq_restore(flags
);
2767 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2770 * If non-trylock use in a hardirq or softirq context, then
2771 * mark the lock as used in these contexts:
2773 if (!hlock
->trylock
) {
2775 if (curr
->hardirq_context
)
2776 if (!mark_lock(curr
, hlock
,
2777 LOCK_USED_IN_HARDIRQ_READ
))
2779 if (curr
->softirq_context
)
2780 if (!mark_lock(curr
, hlock
,
2781 LOCK_USED_IN_SOFTIRQ_READ
))
2784 if (curr
->hardirq_context
)
2785 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2787 if (curr
->softirq_context
)
2788 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2792 if (!hlock
->hardirqs_off
) {
2794 if (!mark_lock(curr
, hlock
,
2795 LOCK_ENABLED_HARDIRQ_READ
))
2797 if (curr
->softirqs_enabled
)
2798 if (!mark_lock(curr
, hlock
,
2799 LOCK_ENABLED_SOFTIRQ_READ
))
2802 if (!mark_lock(curr
, hlock
,
2803 LOCK_ENABLED_HARDIRQ
))
2805 if (curr
->softirqs_enabled
)
2806 if (!mark_lock(curr
, hlock
,
2807 LOCK_ENABLED_SOFTIRQ
))
2813 * We reuse the irq context infrastructure more broadly as a general
2814 * context checking code. This tests GFP_FS recursion (a lock taken
2815 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2818 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2820 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2823 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2831 static int separate_irq_context(struct task_struct
*curr
,
2832 struct held_lock
*hlock
)
2834 unsigned int depth
= curr
->lockdep_depth
;
2837 * Keep track of points where we cross into an interrupt context:
2839 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2840 curr
->softirq_context
;
2842 struct held_lock
*prev_hlock
;
2844 prev_hlock
= curr
->held_locks
+ depth
-1;
2846 * If we cross into another context, reset the
2847 * hash key (this also prevents the checking and the
2848 * adding of the dependency to 'prev'):
2850 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2856 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2859 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2860 enum lock_usage_bit new_bit
)
2862 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
2866 static inline int mark_irqflags(struct task_struct
*curr
,
2867 struct held_lock
*hlock
)
2872 static inline int separate_irq_context(struct task_struct
*curr
,
2873 struct held_lock
*hlock
)
2878 void lockdep_trace_alloc(gfp_t gfp_mask
)
2882 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2885 * Mark a lock with a usage bit, and validate the state transition:
2887 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2888 enum lock_usage_bit new_bit
)
2890 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2893 * If already set then do not dirty the cacheline,
2894 * nor do any checks:
2896 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2902 * Make sure we didn't race:
2904 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2909 hlock_class(this)->usage_mask
|= new_mask
;
2911 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2915 #define LOCKDEP_STATE(__STATE) \
2916 case LOCK_USED_IN_##__STATE: \
2917 case LOCK_USED_IN_##__STATE##_READ: \
2918 case LOCK_ENABLED_##__STATE: \
2919 case LOCK_ENABLED_##__STATE##_READ:
2920 #include "lockdep_states.h"
2921 #undef LOCKDEP_STATE
2922 ret
= mark_lock_irq(curr
, this, new_bit
);
2927 debug_atomic_dec(nr_unused_locks
);
2930 if (!debug_locks_off_graph_unlock())
2939 * We must printk outside of the graph_lock:
2942 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2944 print_irqtrace_events(curr
);
2952 * Initialize a lock instance's lock-class mapping info:
2954 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2955 struct lock_class_key
*key
, int subclass
)
2959 kmemcheck_mark_initialized(lock
, sizeof(*lock
));
2961 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
2962 lock
->class_cache
[i
] = NULL
;
2964 #ifdef CONFIG_LOCK_STAT
2965 lock
->cpu
= raw_smp_processor_id();
2969 * Can't be having no nameless bastards around this place!
2971 if (DEBUG_LOCKS_WARN_ON(!name
)) {
2972 lock
->name
= "NULL";
2979 * No key, no joy, we need to hash something.
2981 if (DEBUG_LOCKS_WARN_ON(!key
))
2984 * Sanity check, the lock-class key must be persistent:
2986 if (!static_obj(key
)) {
2987 printk("BUG: key %p not in .data!\n", key
);
2989 * What it says above ^^^^^, I suggest you read it.
2991 DEBUG_LOCKS_WARN_ON(1);
2996 if (unlikely(!debug_locks
))
3000 register_lock_class(lock
, subclass
, 1);
3002 EXPORT_SYMBOL_GPL(lockdep_init_map
);
3004 struct lock_class_key __lockdep_no_validate__
;
3005 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
3008 print_lock_nested_lock_not_held(struct task_struct
*curr
,
3009 struct held_lock
*hlock
,
3012 if (!debug_locks_off())
3014 if (debug_locks_silent
)
3018 printk("==================================\n");
3019 printk("[ BUG: Nested lock was not taken ]\n");
3020 print_kernel_ident();
3021 printk("----------------------------------\n");
3023 printk("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3026 printk("\nbut this task is not holding:\n");
3027 printk("%s\n", hlock
->nest_lock
->name
);
3029 printk("\nstack backtrace:\n");
3032 printk("\nother info that might help us debug this:\n");
3033 lockdep_print_held_locks(curr
);
3035 printk("\nstack backtrace:\n");
3041 static int __lock_is_held(struct lockdep_map
*lock
);
3044 * This gets called for every mutex_lock*()/spin_lock*() operation.
3045 * We maintain the dependency maps and validate the locking attempt:
3047 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3048 int trylock
, int read
, int check
, int hardirqs_off
,
3049 struct lockdep_map
*nest_lock
, unsigned long ip
,
3052 struct task_struct
*curr
= current
;
3053 struct lock_class
*class = NULL
;
3054 struct held_lock
*hlock
;
3055 unsigned int depth
, id
;
3060 if (unlikely(!debug_locks
))
3064 * Lockdep should run with IRQs disabled, otherwise we could
3065 * get an interrupt which would want to take locks, which would
3066 * end up in lockdep and have you got a head-ache already?
3068 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3071 if (!prove_locking
|| lock
->key
== &__lockdep_no_validate__
)
3074 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
3075 class = lock
->class_cache
[subclass
];
3079 if (unlikely(!class)) {
3080 class = register_lock_class(lock
, subclass
, 0);
3084 atomic_inc((atomic_t
*)&class->ops
);
3085 if (very_verbose(class)) {
3086 printk("\nacquire class [%p] %s", class->key
, class->name
);
3087 if (class->name_version
> 1)
3088 printk("#%d", class->name_version
);
3094 * Add the lock to the list of currently held locks.
3095 * (we dont increase the depth just yet, up until the
3096 * dependency checks are done)
3098 depth
= curr
->lockdep_depth
;
3100 * Ran out of static storage for our per-task lock stack again have we?
3102 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
3105 class_idx
= class - lock_classes
+ 1;
3108 hlock
= curr
->held_locks
+ depth
- 1;
3109 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
3110 if (hlock
->references
) {
3112 * Check: unsigned int references:12, overflow.
3114 if (DEBUG_LOCKS_WARN_ON(hlock
->references
== (1 << 12)-1))
3117 hlock
->references
++;
3119 hlock
->references
= 2;
3126 hlock
= curr
->held_locks
+ depth
;
3128 * Plain impossible, we just registered it and checked it weren't no
3129 * NULL like.. I bet this mushroom I ate was good!
3131 if (DEBUG_LOCKS_WARN_ON(!class))
3133 hlock
->class_idx
= class_idx
;
3134 hlock
->acquire_ip
= ip
;
3135 hlock
->instance
= lock
;
3136 hlock
->nest_lock
= nest_lock
;
3137 hlock
->trylock
= trylock
;
3139 hlock
->check
= check
;
3140 hlock
->hardirqs_off
= !!hardirqs_off
;
3141 hlock
->references
= references
;
3142 #ifdef CONFIG_LOCK_STAT
3143 hlock
->waittime_stamp
= 0;
3144 hlock
->holdtime_stamp
= lockstat_clock();
3147 if (check
&& !mark_irqflags(curr
, hlock
))
3150 /* mark it as used: */
3151 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3155 * Calculate the chain hash: it's the combined hash of all the
3156 * lock keys along the dependency chain. We save the hash value
3157 * at every step so that we can get the current hash easily
3158 * after unlock. The chain hash is then used to cache dependency
3161 * The 'key ID' is what is the most compact key value to drive
3162 * the hash, not class->key.
3164 id
= class - lock_classes
;
3166 * Whoops, we did it again.. ran straight out of our static allocation.
3168 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
3171 chain_key
= curr
->curr_chain_key
;
3174 * How can we have a chain hash when we ain't got no keys?!
3176 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3181 hlock
->prev_chain_key
= chain_key
;
3182 if (separate_irq_context(curr
, hlock
)) {
3186 chain_key
= iterate_chain_key(chain_key
, id
);
3188 if (nest_lock
&& !__lock_is_held(nest_lock
))
3189 return print_lock_nested_lock_not_held(curr
, hlock
, ip
);
3191 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3194 curr
->curr_chain_key
= chain_key
;
3195 curr
->lockdep_depth
++;
3196 check_chain_key(curr
);
3197 #ifdef CONFIG_DEBUG_LOCKDEP
3198 if (unlikely(!debug_locks
))
3201 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3203 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3204 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
3205 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
3207 lockdep_print_held_locks(current
);
3208 debug_show_all_locks();
3214 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3215 max_lockdep_depth
= curr
->lockdep_depth
;
3221 print_unlock_imbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3224 if (!debug_locks_off())
3226 if (debug_locks_silent
)
3230 printk("=====================================\n");
3231 printk("[ BUG: bad unlock balance detected! ]\n");
3232 print_kernel_ident();
3233 printk("-------------------------------------\n");
3234 printk("%s/%d is trying to release lock (",
3235 curr
->comm
, task_pid_nr(curr
));
3236 print_lockdep_cache(lock
);
3239 printk("but there are no more locks to release!\n");
3240 printk("\nother info that might help us debug this:\n");
3241 lockdep_print_held_locks(curr
);
3243 printk("\nstack backtrace:\n");
3250 * Common debugging checks for both nested and non-nested unlock:
3252 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
3255 if (unlikely(!debug_locks
))
3258 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3260 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3263 if (curr
->lockdep_depth
<= 0)
3264 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3269 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
3271 if (hlock
->instance
== lock
)
3274 if (hlock
->references
) {
3275 struct lock_class
*class = lock
->class_cache
[0];
3278 class = look_up_lock_class(lock
, 0);
3281 * If look_up_lock_class() failed to find a class, we're trying
3282 * to test if we hold a lock that has never yet been acquired.
3283 * Clearly if the lock hasn't been acquired _ever_, we're not
3284 * holding it either, so report failure.
3290 * References, but not a lock we're actually ref-counting?
3291 * State got messed up, follow the sites that change ->references
3292 * and try to make sense of it.
3294 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3297 if (hlock
->class_idx
== class - lock_classes
+ 1)
3305 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3306 struct lock_class_key
*key
, unsigned int subclass
,
3309 struct task_struct
*curr
= current
;
3310 struct held_lock
*hlock
, *prev_hlock
;
3311 struct lock_class
*class;
3315 depth
= curr
->lockdep_depth
;
3317 * This function is about (re)setting the class of a held lock,
3318 * yet we're not actually holding any locks. Naughty user!
3320 if (DEBUG_LOCKS_WARN_ON(!depth
))
3324 for (i
= depth
-1; i
>= 0; i
--) {
3325 hlock
= curr
->held_locks
+ i
;
3327 * We must not cross into another context:
3329 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3331 if (match_held_lock(hlock
, lock
))
3335 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3338 lockdep_init_map(lock
, name
, key
, 0);
3339 class = register_lock_class(lock
, subclass
, 0);
3340 hlock
->class_idx
= class - lock_classes
+ 1;
3342 curr
->lockdep_depth
= i
;
3343 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3345 for (; i
< depth
; i
++) {
3346 hlock
= curr
->held_locks
+ i
;
3347 if (!__lock_acquire(hlock
->instance
,
3348 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3349 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3350 hlock
->nest_lock
, hlock
->acquire_ip
,
3356 * I took it apart and put it back together again, except now I have
3357 * these 'spare' parts.. where shall I put them.
3359 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3365 * Remove the lock to the list of currently held locks in a
3366 * potentially non-nested (out of order) manner. This is a
3367 * relatively rare operation, as all the unlock APIs default
3368 * to nested mode (which uses lock_release()):
3371 lock_release_non_nested(struct task_struct
*curr
,
3372 struct lockdep_map
*lock
, unsigned long ip
)
3374 struct held_lock
*hlock
, *prev_hlock
;
3379 * Check whether the lock exists in the current stack
3382 depth
= curr
->lockdep_depth
;
3384 * So we're all set to release this lock.. wait what lock? We don't
3385 * own any locks, you've been drinking again?
3387 if (DEBUG_LOCKS_WARN_ON(!depth
))
3391 for (i
= depth
-1; i
>= 0; i
--) {
3392 hlock
= curr
->held_locks
+ i
;
3394 * We must not cross into another context:
3396 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3398 if (match_held_lock(hlock
, lock
))
3402 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3405 if (hlock
->instance
== lock
)
3406 lock_release_holdtime(hlock
);
3408 if (hlock
->references
) {
3409 hlock
->references
--;
3410 if (hlock
->references
) {
3412 * We had, and after removing one, still have
3413 * references, the current lock stack is still
3414 * valid. We're done!
3421 * We have the right lock to unlock, 'hlock' points to it.
3422 * Now we remove it from the stack, and add back the other
3423 * entries (if any), recalculating the hash along the way:
3426 curr
->lockdep_depth
= i
;
3427 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3429 for (i
++; i
< depth
; i
++) {
3430 hlock
= curr
->held_locks
+ i
;
3431 if (!__lock_acquire(hlock
->instance
,
3432 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3433 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3434 hlock
->nest_lock
, hlock
->acquire_ip
,
3440 * We had N bottles of beer on the wall, we drank one, but now
3441 * there's not N-1 bottles of beer left on the wall...
3443 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3449 * Remove the lock to the list of currently held locks - this gets
3450 * called on mutex_unlock()/spin_unlock*() (or on a failed
3451 * mutex_lock_interruptible()). This is done for unlocks that nest
3452 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3454 static int lock_release_nested(struct task_struct
*curr
,
3455 struct lockdep_map
*lock
, unsigned long ip
)
3457 struct held_lock
*hlock
;
3461 * Pop off the top of the lock stack:
3463 depth
= curr
->lockdep_depth
- 1;
3464 hlock
= curr
->held_locks
+ depth
;
3467 * Is the unlock non-nested:
3469 if (hlock
->instance
!= lock
|| hlock
->references
)
3470 return lock_release_non_nested(curr
, lock
, ip
);
3471 curr
->lockdep_depth
--;
3474 * No more locks, but somehow we've got hash left over, who left it?
3476 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
3479 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3481 lock_release_holdtime(hlock
);
3483 #ifdef CONFIG_DEBUG_LOCKDEP
3484 hlock
->prev_chain_key
= 0;
3485 hlock
->class_idx
= 0;
3486 hlock
->acquire_ip
= 0;
3487 hlock
->irq_context
= 0;
3493 * Remove the lock to the list of currently held locks - this gets
3494 * called on mutex_unlock()/spin_unlock*() (or on a failed
3495 * mutex_lock_interruptible()). This is done for unlocks that nest
3496 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3499 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3501 struct task_struct
*curr
= current
;
3503 if (!check_unlock(curr
, lock
, ip
))
3507 if (!lock_release_nested(curr
, lock
, ip
))
3510 if (!lock_release_non_nested(curr
, lock
, ip
))
3514 check_chain_key(curr
);
3517 static int __lock_is_held(struct lockdep_map
*lock
)
3519 struct task_struct
*curr
= current
;
3522 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3523 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3525 if (match_held_lock(hlock
, lock
))
3533 * Check whether we follow the irq-flags state precisely:
3535 static void check_flags(unsigned long flags
)
3537 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3538 defined(CONFIG_TRACE_IRQFLAGS)
3542 if (irqs_disabled_flags(flags
)) {
3543 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3544 printk("possible reason: unannotated irqs-off.\n");
3547 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3548 printk("possible reason: unannotated irqs-on.\n");
3553 * We dont accurately track softirq state in e.g.
3554 * hardirq contexts (such as on 4KSTACKS), so only
3555 * check if not in hardirq contexts:
3557 if (!hardirq_count()) {
3558 if (softirq_count()) {
3559 /* like the above, but with softirqs */
3560 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3562 /* lick the above, does it taste good? */
3563 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3568 print_irqtrace_events(current
);
3572 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3573 struct lock_class_key
*key
, unsigned int subclass
,
3576 unsigned long flags
;
3578 if (unlikely(current
->lockdep_recursion
))
3581 raw_local_irq_save(flags
);
3582 current
->lockdep_recursion
= 1;
3584 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3585 check_chain_key(current
);
3586 current
->lockdep_recursion
= 0;
3587 raw_local_irq_restore(flags
);
3589 EXPORT_SYMBOL_GPL(lock_set_class
);
3592 * We are not always called with irqs disabled - do that here,
3593 * and also avoid lockdep recursion:
3595 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3596 int trylock
, int read
, int check
,
3597 struct lockdep_map
*nest_lock
, unsigned long ip
)
3599 unsigned long flags
;
3601 if (unlikely(current
->lockdep_recursion
))
3604 raw_local_irq_save(flags
);
3607 current
->lockdep_recursion
= 1;
3608 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3609 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3610 irqs_disabled_flags(flags
), nest_lock
, ip
, 0);
3611 current
->lockdep_recursion
= 0;
3612 raw_local_irq_restore(flags
);
3614 EXPORT_SYMBOL_GPL(lock_acquire
);
3616 void lock_release(struct lockdep_map
*lock
, int nested
,
3619 unsigned long flags
;
3621 if (unlikely(current
->lockdep_recursion
))
3624 raw_local_irq_save(flags
);
3626 current
->lockdep_recursion
= 1;
3627 trace_lock_release(lock
, ip
);
3628 __lock_release(lock
, nested
, ip
);
3629 current
->lockdep_recursion
= 0;
3630 raw_local_irq_restore(flags
);
3632 EXPORT_SYMBOL_GPL(lock_release
);
3634 int lock_is_held(struct lockdep_map
*lock
)
3636 unsigned long flags
;
3639 if (unlikely(current
->lockdep_recursion
))
3640 return 1; /* avoid false negative lockdep_assert_held() */
3642 raw_local_irq_save(flags
);
3645 current
->lockdep_recursion
= 1;
3646 ret
= __lock_is_held(lock
);
3647 current
->lockdep_recursion
= 0;
3648 raw_local_irq_restore(flags
);
3652 EXPORT_SYMBOL_GPL(lock_is_held
);
3654 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3656 current
->lockdep_reclaim_gfp
= gfp_mask
;
3659 void lockdep_clear_current_reclaim_state(void)
3661 current
->lockdep_reclaim_gfp
= 0;
3664 #ifdef CONFIG_LOCK_STAT
3666 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3669 if (!debug_locks_off())
3671 if (debug_locks_silent
)
3675 printk("=================================\n");
3676 printk("[ BUG: bad contention detected! ]\n");
3677 print_kernel_ident();
3678 printk("---------------------------------\n");
3679 printk("%s/%d is trying to contend lock (",
3680 curr
->comm
, task_pid_nr(curr
));
3681 print_lockdep_cache(lock
);
3684 printk("but there are no locks held!\n");
3685 printk("\nother info that might help us debug this:\n");
3686 lockdep_print_held_locks(curr
);
3688 printk("\nstack backtrace:\n");
3695 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3697 struct task_struct
*curr
= current
;
3698 struct held_lock
*hlock
, *prev_hlock
;
3699 struct lock_class_stats
*stats
;
3701 int i
, contention_point
, contending_point
;
3703 depth
= curr
->lockdep_depth
;
3705 * Whee, we contended on this lock, except it seems we're not
3706 * actually trying to acquire anything much at all..
3708 if (DEBUG_LOCKS_WARN_ON(!depth
))
3712 for (i
= depth
-1; i
>= 0; i
--) {
3713 hlock
= curr
->held_locks
+ i
;
3715 * We must not cross into another context:
3717 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3719 if (match_held_lock(hlock
, lock
))
3723 print_lock_contention_bug(curr
, lock
, ip
);
3727 if (hlock
->instance
!= lock
)
3730 hlock
->waittime_stamp
= lockstat_clock();
3732 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3733 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3736 stats
= get_lock_stats(hlock_class(hlock
));
3737 if (contention_point
< LOCKSTAT_POINTS
)
3738 stats
->contention_point
[contention_point
]++;
3739 if (contending_point
< LOCKSTAT_POINTS
)
3740 stats
->contending_point
[contending_point
]++;
3741 if (lock
->cpu
!= smp_processor_id())
3742 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3743 put_lock_stats(stats
);
3747 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3749 struct task_struct
*curr
= current
;
3750 struct held_lock
*hlock
, *prev_hlock
;
3751 struct lock_class_stats
*stats
;
3753 u64 now
, waittime
= 0;
3756 depth
= curr
->lockdep_depth
;
3758 * Yay, we acquired ownership of this lock we didn't try to
3759 * acquire, how the heck did that happen?
3761 if (DEBUG_LOCKS_WARN_ON(!depth
))
3765 for (i
= depth
-1; i
>= 0; i
--) {
3766 hlock
= curr
->held_locks
+ i
;
3768 * We must not cross into another context:
3770 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3772 if (match_held_lock(hlock
, lock
))
3776 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3780 if (hlock
->instance
!= lock
)
3783 cpu
= smp_processor_id();
3784 if (hlock
->waittime_stamp
) {
3785 now
= lockstat_clock();
3786 waittime
= now
- hlock
->waittime_stamp
;
3787 hlock
->holdtime_stamp
= now
;
3790 trace_lock_acquired(lock
, ip
);
3792 stats
= get_lock_stats(hlock_class(hlock
));
3795 lock_time_inc(&stats
->read_waittime
, waittime
);
3797 lock_time_inc(&stats
->write_waittime
, waittime
);
3799 if (lock
->cpu
!= cpu
)
3800 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3801 put_lock_stats(stats
);
3807 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3809 unsigned long flags
;
3811 if (unlikely(!lock_stat
))
3814 if (unlikely(current
->lockdep_recursion
))
3817 raw_local_irq_save(flags
);
3819 current
->lockdep_recursion
= 1;
3820 trace_lock_contended(lock
, ip
);
3821 __lock_contended(lock
, ip
);
3822 current
->lockdep_recursion
= 0;
3823 raw_local_irq_restore(flags
);
3825 EXPORT_SYMBOL_GPL(lock_contended
);
3827 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3829 unsigned long flags
;
3831 if (unlikely(!lock_stat
))
3834 if (unlikely(current
->lockdep_recursion
))
3837 raw_local_irq_save(flags
);
3839 current
->lockdep_recursion
= 1;
3840 __lock_acquired(lock
, ip
);
3841 current
->lockdep_recursion
= 0;
3842 raw_local_irq_restore(flags
);
3844 EXPORT_SYMBOL_GPL(lock_acquired
);
3848 * Used by the testsuite, sanitize the validator state
3849 * after a simulated failure:
3852 void lockdep_reset(void)
3854 unsigned long flags
;
3857 raw_local_irq_save(flags
);
3858 current
->curr_chain_key
= 0;
3859 current
->lockdep_depth
= 0;
3860 current
->lockdep_recursion
= 0;
3861 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3862 nr_hardirq_chains
= 0;
3863 nr_softirq_chains
= 0;
3864 nr_process_chains
= 0;
3866 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3867 INIT_LIST_HEAD(chainhash_table
+ i
);
3868 raw_local_irq_restore(flags
);
3871 static void zap_class(struct lock_class
*class)
3876 * Remove all dependencies this lock is
3879 for (i
= 0; i
< nr_list_entries
; i
++) {
3880 if (list_entries
[i
].class == class)
3881 list_del_rcu(&list_entries
[i
].entry
);
3884 * Unhash the class and remove it from the all_lock_classes list:
3886 list_del_rcu(&class->hash_entry
);
3887 list_del_rcu(&class->lock_entry
);
3892 static inline int within(const void *addr
, void *start
, unsigned long size
)
3894 return addr
>= start
&& addr
< start
+ size
;
3897 void lockdep_free_key_range(void *start
, unsigned long size
)
3899 struct lock_class
*class, *next
;
3900 struct list_head
*head
;
3901 unsigned long flags
;
3905 raw_local_irq_save(flags
);
3906 locked
= graph_lock();
3909 * Unhash all classes that were created by this module:
3911 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3912 head
= classhash_table
+ i
;
3913 if (list_empty(head
))
3915 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3916 if (within(class->key
, start
, size
))
3918 else if (within(class->name
, start
, size
))
3925 raw_local_irq_restore(flags
);
3928 void lockdep_reset_lock(struct lockdep_map
*lock
)
3930 struct lock_class
*class, *next
;
3931 struct list_head
*head
;
3932 unsigned long flags
;
3936 raw_local_irq_save(flags
);
3939 * Remove all classes this lock might have:
3941 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3943 * If the class exists we look it up and zap it:
3945 class = look_up_lock_class(lock
, j
);
3950 * Debug check: in the end all mapped classes should
3953 locked
= graph_lock();
3954 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3955 head
= classhash_table
+ i
;
3956 if (list_empty(head
))
3958 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3961 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
3962 match
|= class == lock
->class_cache
[j
];
3964 if (unlikely(match
)) {
3965 if (debug_locks_off_graph_unlock()) {
3967 * We all just reset everything, how did it match?
3979 raw_local_irq_restore(flags
);
3982 void lockdep_init(void)
3987 * Some architectures have their own start_kernel()
3988 * code which calls lockdep_init(), while we also
3989 * call lockdep_init() from the start_kernel() itself,
3990 * and we want to initialize the hashes only once:
3992 if (lockdep_initialized
)
3995 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
3996 INIT_LIST_HEAD(classhash_table
+ i
);
3998 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3999 INIT_LIST_HEAD(chainhash_table
+ i
);
4001 lockdep_initialized
= 1;
4004 void __init
lockdep_info(void)
4006 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4008 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
4009 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
4010 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
4011 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
4012 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
4013 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
4014 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
4016 printk(" memory used by lock dependency info: %lu kB\n",
4017 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
4018 sizeof(struct list_head
) * CLASSHASH_SIZE
+
4019 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
4020 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
4021 sizeof(struct list_head
) * CHAINHASH_SIZE
4022 #ifdef CONFIG_PROVE_LOCKING
4023 + sizeof(struct circular_queue
)
4028 printk(" per task-struct memory footprint: %lu bytes\n",
4029 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
4031 #ifdef CONFIG_DEBUG_LOCKDEP
4032 if (lockdep_init_error
) {
4033 printk("WARNING: lockdep init error! lock-%s was acquired"
4034 "before lockdep_init\n", lock_init_error
);
4035 printk("Call stack leading to lockdep invocation was:\n");
4036 print_stack_trace(&lockdep_init_trace
, 0);
4042 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
4043 const void *mem_to
, struct held_lock
*hlock
)
4045 if (!debug_locks_off())
4047 if (debug_locks_silent
)
4051 printk("=========================\n");
4052 printk("[ BUG: held lock freed! ]\n");
4053 print_kernel_ident();
4054 printk("-------------------------\n");
4055 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4056 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
4058 lockdep_print_held_locks(curr
);
4060 printk("\nstack backtrace:\n");
4064 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
4065 const void* lock_from
, unsigned long lock_len
)
4067 return lock_from
+ lock_len
<= mem_from
||
4068 mem_from
+ mem_len
<= lock_from
;
4072 * Called when kernel memory is freed (or unmapped), or if a lock
4073 * is destroyed or reinitialized - this code checks whether there is
4074 * any held lock in the memory range of <from> to <to>:
4076 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
4078 struct task_struct
*curr
= current
;
4079 struct held_lock
*hlock
;
4080 unsigned long flags
;
4083 if (unlikely(!debug_locks
))
4086 local_irq_save(flags
);
4087 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4088 hlock
= curr
->held_locks
+ i
;
4090 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
4091 sizeof(*hlock
->instance
)))
4094 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
4097 local_irq_restore(flags
);
4099 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
4101 static void print_held_locks_bug(void)
4103 if (!debug_locks_off())
4105 if (debug_locks_silent
)
4109 printk("=====================================\n");
4110 printk("[ BUG: %s/%d still has locks held! ]\n",
4111 current
->comm
, task_pid_nr(current
));
4112 print_kernel_ident();
4113 printk("-------------------------------------\n");
4114 lockdep_print_held_locks(current
);
4115 printk("\nstack backtrace:\n");
4119 void debug_check_no_locks_held(void)
4121 if (unlikely(current
->lockdep_depth
> 0))
4122 print_held_locks_bug();
4124 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
4127 void debug_show_all_locks(void)
4129 struct task_struct
*g
, *p
;
4133 if (unlikely(!debug_locks
)) {
4134 printk("INFO: lockdep is turned off.\n");
4137 printk("\nShowing all locks held in the system:\n");
4140 * Here we try to get the tasklist_lock as hard as possible,
4141 * if not successful after 2 seconds we ignore it (but keep
4142 * trying). This is to enable a debug printout even if a
4143 * tasklist_lock-holding task deadlocks or crashes.
4146 if (!read_trylock(&tasklist_lock
)) {
4148 printk("hm, tasklist_lock locked, retrying... ");
4151 printk(" #%d", 10-count
);
4155 printk(" ignoring it.\n");
4159 printk(KERN_CONT
" locked it.\n");
4162 do_each_thread(g
, p
) {
4164 * It's not reliable to print a task's held locks
4165 * if it's not sleeping (or if it's not the current
4168 if (p
->state
== TASK_RUNNING
&& p
!= current
)
4170 if (p
->lockdep_depth
)
4171 lockdep_print_held_locks(p
);
4173 if (read_trylock(&tasklist_lock
))
4175 } while_each_thread(g
, p
);
4178 printk("=============================================\n\n");
4181 read_unlock(&tasklist_lock
);
4183 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
4187 * Careful: only use this function if you are sure that
4188 * the task cannot run in parallel!
4190 void debug_show_held_locks(struct task_struct
*task
)
4192 if (unlikely(!debug_locks
)) {
4193 printk("INFO: lockdep is turned off.\n");
4196 lockdep_print_held_locks(task
);
4198 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
4200 asmlinkage __visible
void lockdep_sys_exit(void)
4202 struct task_struct
*curr
= current
;
4204 if (unlikely(curr
->lockdep_depth
)) {
4205 if (!debug_locks_off())
4208 printk("================================================\n");
4209 printk("[ BUG: lock held when returning to user space! ]\n");
4210 print_kernel_ident();
4211 printk("------------------------------------------------\n");
4212 printk("%s/%d is leaving the kernel with locks still held!\n",
4213 curr
->comm
, curr
->pid
);
4214 lockdep_print_held_locks(curr
);
4218 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
4220 struct task_struct
*curr
= current
;
4222 #ifndef CONFIG_PROVE_RCU_REPEATEDLY
4223 if (!debug_locks_off())
4225 #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4226 /* Note: the following can be executed concurrently, so be careful. */
4228 printk("===============================\n");
4229 printk("[ INFO: suspicious RCU usage. ]\n");
4230 print_kernel_ident();
4231 printk("-------------------------------\n");
4232 printk("%s:%d %s!\n", file
, line
, s
);
4233 printk("\nother info that might help us debug this:\n\n");
4234 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4235 !rcu_lockdep_current_cpu_online()
4236 ? "RCU used illegally from offline CPU!\n"
4237 : !rcu_is_watching()
4238 ? "RCU used illegally from idle CPU!\n"
4240 rcu_scheduler_active
, debug_locks
);
4243 * If a CPU is in the RCU-free window in idle (ie: in the section
4244 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4245 * considers that CPU to be in an "extended quiescent state",
4246 * which means that RCU will be completely ignoring that CPU.
4247 * Therefore, rcu_read_lock() and friends have absolutely no
4248 * effect on a CPU running in that state. In other words, even if
4249 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4250 * delete data structures out from under it. RCU really has no
4251 * choice here: we need to keep an RCU-free window in idle where
4252 * the CPU may possibly enter into low power mode. This way we can
4253 * notice an extended quiescent state to other CPUs that started a grace
4254 * period. Otherwise we would delay any grace period as long as we run
4257 * So complain bitterly if someone does call rcu_read_lock(),
4258 * rcu_read_lock_bh() and so on from extended quiescent states.
4260 if (!rcu_is_watching())
4261 printk("RCU used illegally from extended quiescent state!\n");
4263 lockdep_print_held_locks(curr
);
4264 printk("\nstack backtrace:\n");
4267 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);