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
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/sched/clock.h>
32 #include <linux/sched/task.h>
33 #include <linux/sched/mm.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39 #include <linux/kallsyms.h>
40 #include <linux/interrupt.h>
41 #include <linux/stacktrace.h>
42 #include <linux/debug_locks.h>
43 #include <linux/irqflags.h>
44 #include <linux/utsname.h>
45 #include <linux/hash.h>
46 #include <linux/ftrace.h>
47 #include <linux/stringify.h>
48 #include <linux/bitops.h>
49 #include <linux/gfp.h>
50 #include <linux/random.h>
51 #include <linux/jhash.h>
53 #include <asm/sections.h>
55 #include "lockdep_internals.h"
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/lock.h>
60 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
61 #include <linux/slab.h>
64 #ifdef CONFIG_PROVE_LOCKING
65 int prove_locking
= 1;
66 module_param(prove_locking
, int, 0644);
68 #define prove_locking 0
71 #ifdef CONFIG_LOCK_STAT
73 module_param(lock_stat
, int, 0644);
78 #ifdef CONFIG_BOOTPARAM_LOCKDEP_CROSSRELEASE_FULLSTACK
79 static int crossrelease_fullstack
= 1;
81 static int crossrelease_fullstack
;
83 static int __init
allow_crossrelease_fullstack(char *str
)
85 crossrelease_fullstack
= 1;
89 early_param("crossrelease_fullstack", allow_crossrelease_fullstack
);
92 * lockdep_lock: protects the lockdep graph, the hashes and the
93 * class/list/hash allocators.
95 * This is one of the rare exceptions where it's justified
96 * to use a raw spinlock - we really dont want the spinlock
97 * code to recurse back into the lockdep code...
99 static arch_spinlock_t lockdep_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
101 static int graph_lock(void)
103 arch_spin_lock(&lockdep_lock
);
105 * Make sure that if another CPU detected a bug while
106 * walking the graph we dont change it (while the other
107 * CPU is busy printing out stuff with the graph lock
111 arch_spin_unlock(&lockdep_lock
);
114 /* prevent any recursions within lockdep from causing deadlocks */
115 current
->lockdep_recursion
++;
119 static inline int graph_unlock(void)
121 if (debug_locks
&& !arch_spin_is_locked(&lockdep_lock
)) {
123 * The lockdep graph lock isn't locked while we expect it to
124 * be, we're confused now, bye!
126 return DEBUG_LOCKS_WARN_ON(1);
129 current
->lockdep_recursion
--;
130 arch_spin_unlock(&lockdep_lock
);
135 * Turn lock debugging off and return with 0 if it was off already,
136 * and also release the graph lock:
138 static inline int debug_locks_off_graph_unlock(void)
140 int ret
= debug_locks_off();
142 arch_spin_unlock(&lockdep_lock
);
147 unsigned long nr_list_entries
;
148 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
151 * All data structures here are protected by the global debug_lock.
153 * Mutex key structs only get allocated, once during bootup, and never
154 * get freed - this significantly simplifies the debugging code.
156 unsigned long nr_lock_classes
;
157 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
159 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
161 if (!hlock
->class_idx
) {
163 * Someone passed in garbage, we give up.
165 DEBUG_LOCKS_WARN_ON(1);
168 return lock_classes
+ hlock
->class_idx
- 1;
171 #ifdef CONFIG_LOCK_STAT
172 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], cpu_lock_stats
);
174 static inline u64
lockstat_clock(void)
176 return local_clock();
179 static int lock_point(unsigned long points
[], unsigned long ip
)
183 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
184 if (points
[i
] == 0) {
195 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
200 if (time
< lt
->min
|| !lt
->nr
)
207 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
212 if (src
->max
> dst
->max
)
215 if (src
->min
< dst
->min
|| !dst
->nr
)
218 dst
->total
+= src
->total
;
222 struct lock_class_stats
lock_stats(struct lock_class
*class)
224 struct lock_class_stats stats
;
227 memset(&stats
, 0, sizeof(struct lock_class_stats
));
228 for_each_possible_cpu(cpu
) {
229 struct lock_class_stats
*pcs
=
230 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
232 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
233 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
235 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
236 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
238 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
239 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
241 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
242 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
244 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
245 stats
.bounces
[i
] += pcs
->bounces
[i
];
251 void clear_lock_stats(struct lock_class
*class)
255 for_each_possible_cpu(cpu
) {
256 struct lock_class_stats
*cpu_stats
=
257 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
259 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
261 memset(class->contention_point
, 0, sizeof(class->contention_point
));
262 memset(class->contending_point
, 0, sizeof(class->contending_point
));
265 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
267 return &get_cpu_var(cpu_lock_stats
)[class - lock_classes
];
270 static void put_lock_stats(struct lock_class_stats
*stats
)
272 put_cpu_var(cpu_lock_stats
);
275 static void lock_release_holdtime(struct held_lock
*hlock
)
277 struct lock_class_stats
*stats
;
283 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
285 stats
= get_lock_stats(hlock_class(hlock
));
287 lock_time_inc(&stats
->read_holdtime
, holdtime
);
289 lock_time_inc(&stats
->write_holdtime
, holdtime
);
290 put_lock_stats(stats
);
293 static inline void lock_release_holdtime(struct held_lock
*hlock
)
299 * We keep a global list of all lock classes. The list only grows,
300 * never shrinks. The list is only accessed with the lockdep
301 * spinlock lock held.
303 LIST_HEAD(all_lock_classes
);
306 * The lockdep classes are in a hash-table as well, for fast lookup:
308 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
309 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
310 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
311 #define classhashentry(key) (classhash_table + __classhashfn((key)))
313 static struct hlist_head classhash_table
[CLASSHASH_SIZE
];
316 * We put the lock dependency chains into a hash-table as well, to cache
319 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
320 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
321 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
322 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
324 static struct hlist_head chainhash_table
[CHAINHASH_SIZE
];
327 * The hash key of the lock dependency chains is a hash itself too:
328 * it's a hash of all locks taken up to that lock, including that lock.
329 * It's a 64-bit hash, because it's important for the keys to be
332 static inline u64
iterate_chain_key(u64 key
, u32 idx
)
334 u32 k0
= key
, k1
= key
>> 32;
336 __jhash_mix(idx
, k0
, k1
); /* Macro that modifies arguments! */
338 return k0
| (u64
)k1
<< 32;
341 void lockdep_off(void)
343 current
->lockdep_recursion
++;
345 EXPORT_SYMBOL(lockdep_off
);
347 void lockdep_on(void)
349 current
->lockdep_recursion
--;
351 EXPORT_SYMBOL(lockdep_on
);
354 * Debugging switches:
358 #define VERY_VERBOSE 0
361 # define HARDIRQ_VERBOSE 1
362 # define SOFTIRQ_VERBOSE 1
364 # define HARDIRQ_VERBOSE 0
365 # define SOFTIRQ_VERBOSE 0
368 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
370 * Quick filtering for interesting events:
372 static int class_filter(struct lock_class
*class)
376 if (class->name_version
== 1 &&
377 !strcmp(class->name
, "lockname"))
379 if (class->name_version
== 1 &&
380 !strcmp(class->name
, "&struct->lockfield"))
383 /* Filter everything else. 1 would be to allow everything else */
388 static int verbose(struct lock_class
*class)
391 return class_filter(class);
397 * Stack-trace: tightly packed array of stack backtrace
398 * addresses. Protected by the graph_lock.
400 unsigned long nr_stack_trace_entries
;
401 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
403 static void print_lockdep_off(const char *bug_msg
)
405 printk(KERN_DEBUG
"%s\n", bug_msg
);
406 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
407 #ifdef CONFIG_LOCK_STAT
408 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
412 static int save_trace(struct stack_trace
*trace
)
414 trace
->nr_entries
= 0;
415 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
416 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
420 save_stack_trace(trace
);
423 * Some daft arches put -1 at the end to indicate its a full trace.
425 * <rant> this is buggy anyway, since it takes a whole extra entry so a
426 * complete trace that maxes out the entries provided will be reported
427 * as incomplete, friggin useless </rant>
429 if (trace
->nr_entries
!= 0 &&
430 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
433 trace
->max_entries
= trace
->nr_entries
;
435 nr_stack_trace_entries
+= trace
->nr_entries
;
437 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
438 if (!debug_locks_off_graph_unlock())
441 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
450 unsigned int nr_hardirq_chains
;
451 unsigned int nr_softirq_chains
;
452 unsigned int nr_process_chains
;
453 unsigned int max_lockdep_depth
;
455 #ifdef CONFIG_DEBUG_LOCKDEP
457 * Various lockdep statistics:
459 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
466 #define __USAGE(__STATE) \
467 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
468 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
469 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
470 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
472 static const char *usage_str
[] =
474 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
475 #include "lockdep_states.h"
477 [LOCK_USED
] = "INITIAL USE",
480 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
482 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
485 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
490 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
494 if (class->usage_mask
& lock_flag(bit
+ 2))
496 if (class->usage_mask
& lock_flag(bit
)) {
498 if (class->usage_mask
& lock_flag(bit
+ 2))
505 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
509 #define LOCKDEP_STATE(__STATE) \
510 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
511 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
512 #include "lockdep_states.h"
518 static void __print_lock_name(struct lock_class
*class)
520 char str
[KSYM_NAME_LEN
];
525 name
= __get_key_name(class->key
, str
);
526 printk(KERN_CONT
"%s", name
);
528 printk(KERN_CONT
"%s", name
);
529 if (class->name_version
> 1)
530 printk(KERN_CONT
"#%d", class->name_version
);
532 printk(KERN_CONT
"/%d", class->subclass
);
536 static void print_lock_name(struct lock_class
*class)
538 char usage
[LOCK_USAGE_CHARS
];
540 get_usage_chars(class, usage
);
542 printk(KERN_CONT
" (");
543 __print_lock_name(class);
544 printk(KERN_CONT
"){%s}", usage
);
547 static void print_lockdep_cache(struct lockdep_map
*lock
)
550 char str
[KSYM_NAME_LEN
];
554 name
= __get_key_name(lock
->key
->subkeys
, str
);
556 printk(KERN_CONT
"%s", name
);
559 static void print_lock(struct held_lock
*hlock
)
562 * We can be called locklessly through debug_show_all_locks() so be
563 * extra careful, the hlock might have been released and cleared.
565 unsigned int class_idx
= hlock
->class_idx
;
567 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
570 if (!class_idx
|| (class_idx
- 1) >= MAX_LOCKDEP_KEYS
) {
571 printk(KERN_CONT
"<RELEASED>\n");
575 print_lock_name(lock_classes
+ class_idx
- 1);
576 printk(KERN_CONT
", at: [<%p>] %pS\n",
577 (void *)hlock
->acquire_ip
, (void *)hlock
->acquire_ip
);
580 static void lockdep_print_held_locks(struct task_struct
*curr
)
582 int i
, depth
= curr
->lockdep_depth
;
585 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
588 printk("%d lock%s held by %s/%d:\n",
589 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
591 for (i
= 0; i
< depth
; i
++) {
593 print_lock(curr
->held_locks
+ i
);
597 static void print_kernel_ident(void)
599 printk("%s %.*s %s\n", init_utsname()->release
,
600 (int)strcspn(init_utsname()->version
, " "),
601 init_utsname()->version
,
605 static int very_verbose(struct lock_class
*class)
608 return class_filter(class);
614 * Is this the address of a static object:
617 static int static_obj(void *obj
)
619 unsigned long start
= (unsigned long) &_stext
,
620 end
= (unsigned long) &_end
,
621 addr
= (unsigned long) obj
;
626 if ((addr
>= start
) && (addr
< end
))
629 if (arch_is_kernel_data(addr
))
633 * in-kernel percpu var?
635 if (is_kernel_percpu_address(addr
))
639 * module static or percpu var?
641 return is_module_address(addr
) || is_module_percpu_address(addr
);
646 * To make lock name printouts unique, we calculate a unique
647 * class->name_version generation counter:
649 static int count_matching_names(struct lock_class
*new_class
)
651 struct lock_class
*class;
654 if (!new_class
->name
)
657 list_for_each_entry_rcu(class, &all_lock_classes
, lock_entry
) {
658 if (new_class
->key
- new_class
->subclass
== class->key
)
659 return class->name_version
;
660 if (class->name
&& !strcmp(class->name
, new_class
->name
))
661 count
= max(count
, class->name_version
);
668 * Register a lock's class in the hash-table, if the class is not present
669 * yet. Otherwise we look it up. We cache the result in the lock object
670 * itself, so actual lookup of the hash should be once per lock object.
672 static inline struct lock_class
*
673 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
675 struct lockdep_subclass_key
*key
;
676 struct hlist_head
*hash_head
;
677 struct lock_class
*class;
678 bool is_static
= false;
680 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
683 "BUG: looking up invalid subclass: %u\n", subclass
);
685 "turning off the locking correctness validator.\n");
691 * Static locks do not have their class-keys yet - for them the key
692 * is the lock object itself. If the lock is in the per cpu area,
693 * the canonical address of the lock (per cpu offset removed) is
696 if (unlikely(!lock
->key
)) {
697 unsigned long can_addr
, addr
= (unsigned long)lock
;
699 if (__is_kernel_percpu_address(addr
, &can_addr
))
700 lock
->key
= (void *)can_addr
;
701 else if (__is_module_percpu_address(addr
, &can_addr
))
702 lock
->key
= (void *)can_addr
;
703 else if (static_obj(lock
))
704 lock
->key
= (void *)lock
;
706 return ERR_PTR(-EINVAL
);
711 * NOTE: the class-key must be unique. For dynamic locks, a static
712 * lock_class_key variable is passed in through the mutex_init()
713 * (or spin_lock_init()) call - which acts as the key. For static
714 * locks we use the lock object itself as the key.
716 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
717 sizeof(struct lockdep_map
));
719 key
= lock
->key
->subkeys
+ subclass
;
721 hash_head
= classhashentry(key
);
724 * We do an RCU walk of the hash, see lockdep_free_key_range().
726 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
729 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
730 if (class->key
== key
) {
732 * Huh! same key, different name? Did someone trample
733 * on some memory? We're most confused.
735 WARN_ON_ONCE(class->name
!= lock
->name
);
740 return is_static
|| static_obj(lock
->key
) ? NULL
: ERR_PTR(-EINVAL
);
743 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
744 static void cross_init(struct lockdep_map
*lock
, int cross
);
745 static int cross_lock(struct lockdep_map
*lock
);
746 static int lock_acquire_crosslock(struct held_lock
*hlock
);
747 static int lock_release_crosslock(struct lockdep_map
*lock
);
749 static inline void cross_init(struct lockdep_map
*lock
, int cross
) {}
750 static inline int cross_lock(struct lockdep_map
*lock
) { return 0; }
751 static inline int lock_acquire_crosslock(struct held_lock
*hlock
) { return 2; }
752 static inline int lock_release_crosslock(struct lockdep_map
*lock
) { return 2; }
756 * Register a lock's class in the hash-table, if the class is not present
757 * yet. Otherwise we look it up. We cache the result in the lock object
758 * itself, so actual lookup of the hash should be once per lock object.
760 static struct lock_class
*
761 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
763 struct lockdep_subclass_key
*key
;
764 struct hlist_head
*hash_head
;
765 struct lock_class
*class;
767 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
769 class = look_up_lock_class(lock
, subclass
);
770 if (likely(!IS_ERR_OR_NULL(class)))
771 goto out_set_class_cache
;
774 * Debug-check: all keys must be persistent!
778 printk("INFO: trying to register non-static key.\n");
779 printk("the code is fine but needs lockdep annotation.\n");
780 printk("turning off the locking correctness validator.\n");
785 key
= lock
->key
->subkeys
+ subclass
;
786 hash_head
= classhashentry(key
);
792 * We have to do the hash-walk again, to avoid races
795 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
796 if (class->key
== key
)
801 * Allocate a new key from the static array, and add it to
804 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
805 if (!debug_locks_off_graph_unlock()) {
809 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
813 class = lock_classes
+ nr_lock_classes
++;
814 debug_atomic_inc(nr_unused_locks
);
816 class->name
= lock
->name
;
817 class->subclass
= subclass
;
818 INIT_LIST_HEAD(&class->lock_entry
);
819 INIT_LIST_HEAD(&class->locks_before
);
820 INIT_LIST_HEAD(&class->locks_after
);
821 class->name_version
= count_matching_names(class);
823 * We use RCU's safe list-add method to make
824 * parallel walking of the hash-list safe:
826 hlist_add_head_rcu(&class->hash_entry
, hash_head
);
828 * Add it to the global list of classes:
830 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
832 if (verbose(class)) {
835 printk("\nnew class %p: %s", class->key
, class->name
);
836 if (class->name_version
> 1)
837 printk(KERN_CONT
"#%d", class->name_version
);
838 printk(KERN_CONT
"\n");
849 if (!subclass
|| force
)
850 lock
->class_cache
[0] = class;
851 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
852 lock
->class_cache
[subclass
] = class;
855 * Hash collision, did we smoke some? We found a class with a matching
856 * hash but the subclass -- which is hashed in -- didn't match.
858 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
864 #ifdef CONFIG_PROVE_LOCKING
866 * Allocate a lockdep entry. (assumes the graph_lock held, returns
867 * with NULL on failure)
869 static struct lock_list
*alloc_list_entry(void)
871 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
872 if (!debug_locks_off_graph_unlock())
875 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
879 return list_entries
+ nr_list_entries
++;
883 * Add a new dependency to the head of the list:
885 static int add_lock_to_list(struct lock_class
*this, struct list_head
*head
,
886 unsigned long ip
, int distance
,
887 struct stack_trace
*trace
)
889 struct lock_list
*entry
;
891 * Lock not present yet - get a new dependency struct and
892 * add it to the list:
894 entry
= alloc_list_entry();
899 entry
->distance
= distance
;
900 entry
->trace
= *trace
;
902 * Both allocation and removal are done under the graph lock; but
903 * iteration is under RCU-sched; see look_up_lock_class() and
904 * lockdep_free_key_range().
906 list_add_tail_rcu(&entry
->entry
, head
);
912 * For good efficiency of modular, we use power of 2
914 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
915 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
918 * The circular_queue and helpers is used to implement the
919 * breadth-first search(BFS)algorithem, by which we can build
920 * the shortest path from the next lock to be acquired to the
921 * previous held lock if there is a circular between them.
923 struct circular_queue
{
924 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
925 unsigned int front
, rear
;
928 static struct circular_queue lock_cq
;
930 unsigned int max_bfs_queue_depth
;
932 static unsigned int lockdep_dependency_gen_id
;
934 static inline void __cq_init(struct circular_queue
*cq
)
936 cq
->front
= cq
->rear
= 0;
937 lockdep_dependency_gen_id
++;
940 static inline int __cq_empty(struct circular_queue
*cq
)
942 return (cq
->front
== cq
->rear
);
945 static inline int __cq_full(struct circular_queue
*cq
)
947 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
950 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
955 cq
->element
[cq
->rear
] = elem
;
956 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
960 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
965 *elem
= cq
->element
[cq
->front
];
966 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
970 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
972 return (cq
->rear
- cq
->front
) & CQ_MASK
;
975 static inline void mark_lock_accessed(struct lock_list
*lock
,
976 struct lock_list
*parent
)
980 nr
= lock
- list_entries
;
981 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
982 lock
->parent
= parent
;
983 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
986 static inline unsigned long lock_accessed(struct lock_list
*lock
)
990 nr
= lock
- list_entries
;
991 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
992 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
995 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
997 return child
->parent
;
1000 static inline int get_lock_depth(struct lock_list
*child
)
1003 struct lock_list
*parent
;
1005 while ((parent
= get_lock_parent(child
))) {
1012 static int __bfs(struct lock_list
*source_entry
,
1014 int (*match
)(struct lock_list
*entry
, void *data
),
1015 struct lock_list
**target_entry
,
1018 struct lock_list
*entry
;
1019 struct list_head
*head
;
1020 struct circular_queue
*cq
= &lock_cq
;
1023 if (match(source_entry
, data
)) {
1024 *target_entry
= source_entry
;
1030 head
= &source_entry
->class->locks_after
;
1032 head
= &source_entry
->class->locks_before
;
1034 if (list_empty(head
))
1038 __cq_enqueue(cq
, (unsigned long)source_entry
);
1040 while (!__cq_empty(cq
)) {
1041 struct lock_list
*lock
;
1043 __cq_dequeue(cq
, (unsigned long *)&lock
);
1051 head
= &lock
->class->locks_after
;
1053 head
= &lock
->class->locks_before
;
1055 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1057 list_for_each_entry_rcu(entry
, head
, entry
) {
1058 if (!lock_accessed(entry
)) {
1059 unsigned int cq_depth
;
1060 mark_lock_accessed(entry
, lock
);
1061 if (match(entry
, data
)) {
1062 *target_entry
= entry
;
1067 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1071 cq_depth
= __cq_get_elem_count(cq
);
1072 if (max_bfs_queue_depth
< cq_depth
)
1073 max_bfs_queue_depth
= cq_depth
;
1081 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1083 int (*match
)(struct lock_list
*entry
, void *data
),
1084 struct lock_list
**target_entry
)
1086 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1090 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1092 int (*match
)(struct lock_list
*entry
, void *data
),
1093 struct lock_list
**target_entry
)
1095 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1100 * Recursive, forwards-direction lock-dependency checking, used for
1101 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1106 * Print a dependency chain entry (this is only done when a deadlock
1107 * has been detected):
1110 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1112 if (debug_locks_silent
)
1114 printk("\n-> #%u", depth
);
1115 print_lock_name(target
->class);
1116 printk(KERN_CONT
":\n");
1117 print_stack_trace(&target
->trace
, 6);
1123 print_circular_lock_scenario(struct held_lock
*src
,
1124 struct held_lock
*tgt
,
1125 struct lock_list
*prt
)
1127 struct lock_class
*source
= hlock_class(src
);
1128 struct lock_class
*target
= hlock_class(tgt
);
1129 struct lock_class
*parent
= prt
->class;
1132 * A direct locking problem where unsafe_class lock is taken
1133 * directly by safe_class lock, then all we need to show
1134 * is the deadlock scenario, as it is obvious that the
1135 * unsafe lock is taken under the safe lock.
1137 * But if there is a chain instead, where the safe lock takes
1138 * an intermediate lock (middle_class) where this lock is
1139 * not the same as the safe lock, then the lock chain is
1140 * used to describe the problem. Otherwise we would need
1141 * to show a different CPU case for each link in the chain
1142 * from the safe_class lock to the unsafe_class lock.
1144 if (parent
!= source
) {
1145 printk("Chain exists of:\n ");
1146 __print_lock_name(source
);
1147 printk(KERN_CONT
" --> ");
1148 __print_lock_name(parent
);
1149 printk(KERN_CONT
" --> ");
1150 __print_lock_name(target
);
1151 printk(KERN_CONT
"\n\n");
1154 if (cross_lock(tgt
->instance
)) {
1155 printk(" Possible unsafe locking scenario by crosslock:\n\n");
1156 printk(" CPU0 CPU1\n");
1157 printk(" ---- ----\n");
1159 __print_lock_name(parent
);
1160 printk(KERN_CONT
");\n");
1162 __print_lock_name(target
);
1163 printk(KERN_CONT
");\n");
1165 __print_lock_name(source
);
1166 printk(KERN_CONT
");\n");
1168 __print_lock_name(target
);
1169 printk(KERN_CONT
");\n");
1170 printk("\n *** DEADLOCK ***\n\n");
1172 printk(" Possible unsafe locking scenario:\n\n");
1173 printk(" CPU0 CPU1\n");
1174 printk(" ---- ----\n");
1176 __print_lock_name(target
);
1177 printk(KERN_CONT
");\n");
1179 __print_lock_name(parent
);
1180 printk(KERN_CONT
");\n");
1182 __print_lock_name(target
);
1183 printk(KERN_CONT
");\n");
1185 __print_lock_name(source
);
1186 printk(KERN_CONT
");\n");
1187 printk("\n *** DEADLOCK ***\n\n");
1192 * When a circular dependency is detected, print the
1196 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1197 struct held_lock
*check_src
,
1198 struct held_lock
*check_tgt
)
1200 struct task_struct
*curr
= current
;
1202 if (debug_locks_silent
)
1206 pr_warn("======================================================\n");
1207 pr_warn("WARNING: possible circular locking dependency detected\n");
1208 print_kernel_ident();
1209 pr_warn("------------------------------------------------------\n");
1210 pr_warn("%s/%d is trying to acquire lock:\n",
1211 curr
->comm
, task_pid_nr(curr
));
1212 print_lock(check_src
);
1214 if (cross_lock(check_tgt
->instance
))
1215 pr_warn("\nbut now in release context of a crosslock acquired at the following:\n");
1217 pr_warn("\nbut task is already holding lock:\n");
1219 print_lock(check_tgt
);
1220 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1221 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1223 print_circular_bug_entry(entry
, depth
);
1228 static inline int class_equal(struct lock_list
*entry
, void *data
)
1230 return entry
->class == data
;
1233 static noinline
int print_circular_bug(struct lock_list
*this,
1234 struct lock_list
*target
,
1235 struct held_lock
*check_src
,
1236 struct held_lock
*check_tgt
,
1237 struct stack_trace
*trace
)
1239 struct task_struct
*curr
= current
;
1240 struct lock_list
*parent
;
1241 struct lock_list
*first_parent
;
1244 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1247 if (cross_lock(check_tgt
->instance
))
1248 this->trace
= *trace
;
1249 else if (!save_trace(&this->trace
))
1252 depth
= get_lock_depth(target
);
1254 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1256 parent
= get_lock_parent(target
);
1257 first_parent
= parent
;
1260 print_circular_bug_entry(parent
, --depth
);
1261 parent
= get_lock_parent(parent
);
1264 printk("\nother info that might help us debug this:\n\n");
1265 print_circular_lock_scenario(check_src
, check_tgt
,
1268 lockdep_print_held_locks(curr
);
1270 printk("\nstack backtrace:\n");
1276 static noinline
int print_bfs_bug(int ret
)
1278 if (!debug_locks_off_graph_unlock())
1282 * Breadth-first-search failed, graph got corrupted?
1284 WARN(1, "lockdep bfs error:%d\n", ret
);
1289 static int noop_count(struct lock_list
*entry
, void *data
)
1291 (*(unsigned long *)data
)++;
1295 static unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1297 unsigned long count
= 0;
1298 struct lock_list
*uninitialized_var(target_entry
);
1300 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1304 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1306 unsigned long ret
, flags
;
1307 struct lock_list
this;
1312 local_irq_save(flags
);
1313 arch_spin_lock(&lockdep_lock
);
1314 ret
= __lockdep_count_forward_deps(&this);
1315 arch_spin_unlock(&lockdep_lock
);
1316 local_irq_restore(flags
);
1321 static unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1323 unsigned long count
= 0;
1324 struct lock_list
*uninitialized_var(target_entry
);
1326 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1331 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1333 unsigned long ret
, flags
;
1334 struct lock_list
this;
1339 local_irq_save(flags
);
1340 arch_spin_lock(&lockdep_lock
);
1341 ret
= __lockdep_count_backward_deps(&this);
1342 arch_spin_unlock(&lockdep_lock
);
1343 local_irq_restore(flags
);
1349 * Prove that the dependency graph starting at <entry> can not
1350 * lead to <target>. Print an error and return 0 if it does.
1353 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1354 struct lock_list
**target_entry
)
1358 debug_atomic_inc(nr_cyclic_checks
);
1360 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1366 check_redundant(struct lock_list
*root
, struct lock_class
*target
,
1367 struct lock_list
**target_entry
)
1371 debug_atomic_inc(nr_redundant_checks
);
1373 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1378 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1380 * Forwards and backwards subgraph searching, for the purposes of
1381 * proving that two subgraphs can be connected by a new dependency
1382 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1385 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1387 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1393 * Find a node in the forwards-direction dependency sub-graph starting
1394 * at @root->class that matches @bit.
1396 * Return 0 if such a node exists in the subgraph, and put that node
1397 * into *@target_entry.
1399 * Return 1 otherwise and keep *@target_entry unchanged.
1400 * Return <0 on error.
1403 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1404 struct lock_list
**target_entry
)
1408 debug_atomic_inc(nr_find_usage_forwards_checks
);
1410 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1416 * Find a node in the backwards-direction dependency sub-graph starting
1417 * at @root->class that matches @bit.
1419 * Return 0 if such a node exists in the subgraph, and put that node
1420 * into *@target_entry.
1422 * Return 1 otherwise and keep *@target_entry unchanged.
1423 * Return <0 on error.
1426 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1427 struct lock_list
**target_entry
)
1431 debug_atomic_inc(nr_find_usage_backwards_checks
);
1433 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1438 static void print_lock_class_header(struct lock_class
*class, int depth
)
1442 printk("%*s->", depth
, "");
1443 print_lock_name(class);
1444 printk(KERN_CONT
" ops: %lu", class->ops
);
1445 printk(KERN_CONT
" {\n");
1447 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1448 if (class->usage_mask
& (1 << bit
)) {
1451 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1452 len
+= printk(KERN_CONT
" at:\n");
1453 print_stack_trace(class->usage_traces
+ bit
, len
);
1456 printk("%*s }\n", depth
, "");
1458 printk("%*s ... key at: [<%p>] %pS\n",
1459 depth
, "", class->key
, class->key
);
1463 * printk the shortest lock dependencies from @start to @end in reverse order:
1466 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1467 struct lock_list
*root
)
1469 struct lock_list
*entry
= leaf
;
1472 /*compute depth from generated tree by BFS*/
1473 depth
= get_lock_depth(leaf
);
1476 print_lock_class_header(entry
->class, depth
);
1477 printk("%*s ... acquired at:\n", depth
, "");
1478 print_stack_trace(&entry
->trace
, 2);
1481 if (depth
== 0 && (entry
!= root
)) {
1482 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1486 entry
= get_lock_parent(entry
);
1488 } while (entry
&& (depth
>= 0));
1494 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1495 struct lock_list
*unsafe_entry
,
1496 struct lock_class
*prev_class
,
1497 struct lock_class
*next_class
)
1499 struct lock_class
*safe_class
= safe_entry
->class;
1500 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1501 struct lock_class
*middle_class
= prev_class
;
1503 if (middle_class
== safe_class
)
1504 middle_class
= next_class
;
1507 * A direct locking problem where unsafe_class lock is taken
1508 * directly by safe_class lock, then all we need to show
1509 * is the deadlock scenario, as it is obvious that the
1510 * unsafe lock is taken under the safe lock.
1512 * But if there is a chain instead, where the safe lock takes
1513 * an intermediate lock (middle_class) where this lock is
1514 * not the same as the safe lock, then the lock chain is
1515 * used to describe the problem. Otherwise we would need
1516 * to show a different CPU case for each link in the chain
1517 * from the safe_class lock to the unsafe_class lock.
1519 if (middle_class
!= unsafe_class
) {
1520 printk("Chain exists of:\n ");
1521 __print_lock_name(safe_class
);
1522 printk(KERN_CONT
" --> ");
1523 __print_lock_name(middle_class
);
1524 printk(KERN_CONT
" --> ");
1525 __print_lock_name(unsafe_class
);
1526 printk(KERN_CONT
"\n\n");
1529 printk(" Possible interrupt unsafe locking scenario:\n\n");
1530 printk(" CPU0 CPU1\n");
1531 printk(" ---- ----\n");
1533 __print_lock_name(unsafe_class
);
1534 printk(KERN_CONT
");\n");
1535 printk(" local_irq_disable();\n");
1537 __print_lock_name(safe_class
);
1538 printk(KERN_CONT
");\n");
1540 __print_lock_name(middle_class
);
1541 printk(KERN_CONT
");\n");
1542 printk(" <Interrupt>\n");
1544 __print_lock_name(safe_class
);
1545 printk(KERN_CONT
");\n");
1546 printk("\n *** DEADLOCK ***\n\n");
1550 print_bad_irq_dependency(struct task_struct
*curr
,
1551 struct lock_list
*prev_root
,
1552 struct lock_list
*next_root
,
1553 struct lock_list
*backwards_entry
,
1554 struct lock_list
*forwards_entry
,
1555 struct held_lock
*prev
,
1556 struct held_lock
*next
,
1557 enum lock_usage_bit bit1
,
1558 enum lock_usage_bit bit2
,
1559 const char *irqclass
)
1561 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1565 pr_warn("=====================================================\n");
1566 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1567 irqclass
, irqclass
);
1568 print_kernel_ident();
1569 pr_warn("-----------------------------------------------------\n");
1570 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1571 curr
->comm
, task_pid_nr(curr
),
1572 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1573 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1574 curr
->hardirqs_enabled
,
1575 curr
->softirqs_enabled
);
1578 pr_warn("\nand this task is already holding:\n");
1580 pr_warn("which would create a new lock dependency:\n");
1581 print_lock_name(hlock_class(prev
));
1583 print_lock_name(hlock_class(next
));
1586 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1588 print_lock_name(backwards_entry
->class);
1589 pr_warn("\n... which became %s-irq-safe at:\n", irqclass
);
1591 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1593 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass
);
1594 print_lock_name(forwards_entry
->class);
1595 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass
);
1598 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1600 pr_warn("\nother info that might help us debug this:\n\n");
1601 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1602 hlock_class(prev
), hlock_class(next
));
1604 lockdep_print_held_locks(curr
);
1606 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass
);
1607 if (!save_trace(&prev_root
->trace
))
1609 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1611 pr_warn("\nthe dependencies between the lock to be acquired");
1612 pr_warn(" and %s-irq-unsafe lock:\n", irqclass
);
1613 if (!save_trace(&next_root
->trace
))
1615 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1617 pr_warn("\nstack backtrace:\n");
1624 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1625 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1626 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1629 struct lock_list
this, that
;
1630 struct lock_list
*uninitialized_var(target_entry
);
1631 struct lock_list
*uninitialized_var(target_entry1
);
1635 this.class = hlock_class(prev
);
1636 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1638 return print_bfs_bug(ret
);
1643 that
.class = hlock_class(next
);
1644 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1646 return print_bfs_bug(ret
);
1650 return print_bad_irq_dependency(curr
, &this, &that
,
1651 target_entry
, target_entry1
,
1653 bit_backwards
, bit_forwards
, irqclass
);
1656 static const char *state_names
[] = {
1657 #define LOCKDEP_STATE(__STATE) \
1658 __stringify(__STATE),
1659 #include "lockdep_states.h"
1660 #undef LOCKDEP_STATE
1663 static const char *state_rnames
[] = {
1664 #define LOCKDEP_STATE(__STATE) \
1665 __stringify(__STATE)"-READ",
1666 #include "lockdep_states.h"
1667 #undef LOCKDEP_STATE
1670 static inline const char *state_name(enum lock_usage_bit bit
)
1672 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1675 static int exclusive_bit(int new_bit
)
1683 * bit 0 - write/read
1684 * bit 1 - used_in/enabled
1688 int state
= new_bit
& ~3;
1689 int dir
= new_bit
& 2;
1692 * keep state, bit flip the direction and strip read.
1694 return state
| (dir
^ 2);
1697 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1698 struct held_lock
*next
, enum lock_usage_bit bit
)
1701 * Prove that the new dependency does not connect a hardirq-safe
1702 * lock with a hardirq-unsafe lock - to achieve this we search
1703 * the backwards-subgraph starting at <prev>, and the
1704 * forwards-subgraph starting at <next>:
1706 if (!check_usage(curr
, prev
, next
, bit
,
1707 exclusive_bit(bit
), state_name(bit
)))
1713 * Prove that the new dependency does not connect a hardirq-safe-read
1714 * lock with a hardirq-unsafe lock - to achieve this we search
1715 * the backwards-subgraph starting at <prev>, and the
1716 * forwards-subgraph starting at <next>:
1718 if (!check_usage(curr
, prev
, next
, bit
,
1719 exclusive_bit(bit
), state_name(bit
)))
1726 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1727 struct held_lock
*next
)
1729 #define LOCKDEP_STATE(__STATE) \
1730 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1732 #include "lockdep_states.h"
1733 #undef LOCKDEP_STATE
1738 static void inc_chains(void)
1740 if (current
->hardirq_context
)
1741 nr_hardirq_chains
++;
1743 if (current
->softirq_context
)
1744 nr_softirq_chains
++;
1746 nr_process_chains
++;
1753 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1754 struct held_lock
*next
)
1759 static inline void inc_chains(void)
1761 nr_process_chains
++;
1767 print_deadlock_scenario(struct held_lock
*nxt
,
1768 struct held_lock
*prv
)
1770 struct lock_class
*next
= hlock_class(nxt
);
1771 struct lock_class
*prev
= hlock_class(prv
);
1773 printk(" Possible unsafe locking scenario:\n\n");
1777 __print_lock_name(prev
);
1778 printk(KERN_CONT
");\n");
1780 __print_lock_name(next
);
1781 printk(KERN_CONT
");\n");
1782 printk("\n *** DEADLOCK ***\n\n");
1783 printk(" May be due to missing lock nesting notation\n\n");
1787 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1788 struct held_lock
*next
)
1790 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1794 pr_warn("============================================\n");
1795 pr_warn("WARNING: possible recursive locking detected\n");
1796 print_kernel_ident();
1797 pr_warn("--------------------------------------------\n");
1798 pr_warn("%s/%d is trying to acquire lock:\n",
1799 curr
->comm
, task_pid_nr(curr
));
1801 pr_warn("\nbut task is already holding lock:\n");
1804 pr_warn("\nother info that might help us debug this:\n");
1805 print_deadlock_scenario(next
, prev
);
1806 lockdep_print_held_locks(curr
);
1808 pr_warn("\nstack backtrace:\n");
1815 * Check whether we are holding such a class already.
1817 * (Note that this has to be done separately, because the graph cannot
1818 * detect such classes of deadlocks.)
1820 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1823 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1824 struct lockdep_map
*next_instance
, int read
)
1826 struct held_lock
*prev
;
1827 struct held_lock
*nest
= NULL
;
1830 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1831 prev
= curr
->held_locks
+ i
;
1833 if (prev
->instance
== next
->nest_lock
)
1836 if (hlock_class(prev
) != hlock_class(next
))
1840 * Allow read-after-read recursion of the same
1841 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1843 if ((read
== 2) && prev
->read
)
1847 * We're holding the nest_lock, which serializes this lock's
1848 * nesting behaviour.
1853 if (cross_lock(prev
->instance
))
1856 return print_deadlock_bug(curr
, prev
, next
);
1862 * There was a chain-cache miss, and we are about to add a new dependency
1863 * to a previous lock. We recursively validate the following rules:
1865 * - would the adding of the <prev> -> <next> dependency create a
1866 * circular dependency in the graph? [== circular deadlock]
1868 * - does the new prev->next dependency connect any hardirq-safe lock
1869 * (in the full backwards-subgraph starting at <prev>) with any
1870 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1871 * <next>)? [== illegal lock inversion with hardirq contexts]
1873 * - does the new prev->next dependency connect any softirq-safe lock
1874 * (in the full backwards-subgraph starting at <prev>) with any
1875 * softirq-unsafe lock (in the full forwards-subgraph starting at
1876 * <next>)? [== illegal lock inversion with softirq contexts]
1878 * any of these scenarios could lead to a deadlock.
1880 * Then if all the validations pass, we add the forwards and backwards
1884 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1885 struct held_lock
*next
, int distance
, struct stack_trace
*trace
,
1886 int (*save
)(struct stack_trace
*trace
))
1888 struct lock_list
*uninitialized_var(target_entry
);
1889 struct lock_list
*entry
;
1890 struct lock_list
this;
1894 * Prove that the new <prev> -> <next> dependency would not
1895 * create a circular dependency in the graph. (We do this by
1896 * forward-recursing into the graph starting at <next>, and
1897 * checking whether we can reach <prev>.)
1899 * We are using global variables to control the recursion, to
1900 * keep the stackframe size of the recursive functions low:
1902 this.class = hlock_class(next
);
1904 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1905 if (unlikely(!ret
)) {
1906 if (!trace
->entries
) {
1908 * If @save fails here, the printing might trigger
1909 * a WARN but because of the !nr_entries it should
1910 * not do bad things.
1914 return print_circular_bug(&this, target_entry
, next
, prev
, trace
);
1916 else if (unlikely(ret
< 0))
1917 return print_bfs_bug(ret
);
1919 if (!check_prev_add_irq(curr
, prev
, next
))
1923 * For recursive read-locks we do all the dependency checks,
1924 * but we dont store read-triggered dependencies (only
1925 * write-triggered dependencies). This ensures that only the
1926 * write-side dependencies matter, and that if for example a
1927 * write-lock never takes any other locks, then the reads are
1928 * equivalent to a NOP.
1930 if (next
->read
== 2 || prev
->read
== 2)
1933 * Is the <prev> -> <next> dependency already present?
1935 * (this may occur even though this is a new chain: consider
1936 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1937 * chains - the second one will be new, but L1 already has
1938 * L2 added to its dependency list, due to the first chain.)
1940 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1941 if (entry
->class == hlock_class(next
)) {
1943 entry
->distance
= 1;
1949 * Is the <prev> -> <next> link redundant?
1951 this.class = hlock_class(prev
);
1953 ret
= check_redundant(&this, hlock_class(next
), &target_entry
);
1955 debug_atomic_inc(nr_redundant
);
1959 return print_bfs_bug(ret
);
1962 if (!trace
->entries
&& !save(trace
))
1966 * Ok, all validations passed, add the new lock
1967 * to the previous lock's dependency list:
1969 ret
= add_lock_to_list(hlock_class(next
),
1970 &hlock_class(prev
)->locks_after
,
1971 next
->acquire_ip
, distance
, trace
);
1976 ret
= add_lock_to_list(hlock_class(prev
),
1977 &hlock_class(next
)->locks_before
,
1978 next
->acquire_ip
, distance
, trace
);
1986 * Add the dependency to all directly-previous locks that are 'relevant'.
1987 * The ones that are relevant are (in increasing distance from curr):
1988 * all consecutive trylock entries and the final non-trylock entry - or
1989 * the end of this context's lock-chain - whichever comes first.
1992 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1994 int depth
= curr
->lockdep_depth
;
1995 struct held_lock
*hlock
;
1996 struct stack_trace trace
= {
2006 * Depth must not be zero for a non-head lock:
2011 * At least two relevant locks must exist for this
2014 if (curr
->held_locks
[depth
].irq_context
!=
2015 curr
->held_locks
[depth
-1].irq_context
)
2019 int distance
= curr
->lockdep_depth
- depth
+ 1;
2020 hlock
= curr
->held_locks
+ depth
- 1;
2022 * Only non-crosslock entries get new dependencies added.
2023 * Crosslock entries will be added by commit later:
2025 if (!cross_lock(hlock
->instance
)) {
2027 * Only non-recursive-read entries get new dependencies
2030 if (hlock
->read
!= 2 && hlock
->check
) {
2031 int ret
= check_prev_add(curr
, hlock
, next
,
2032 distance
, &trace
, save_trace
);
2037 * Stop after the first non-trylock entry,
2038 * as non-trylock entries have added their
2039 * own direct dependencies already, so this
2040 * lock is connected to them indirectly:
2042 if (!hlock
->trylock
)
2048 * End of lock-stack?
2053 * Stop the search if we cross into another context:
2055 if (curr
->held_locks
[depth
].irq_context
!=
2056 curr
->held_locks
[depth
-1].irq_context
)
2061 if (!debug_locks_off_graph_unlock())
2065 * Clearly we all shouldn't be here, but since we made it we
2066 * can reliable say we messed up our state. See the above two
2067 * gotos for reasons why we could possibly end up here.
2074 unsigned long nr_lock_chains
;
2075 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
2076 int nr_chain_hlocks
;
2077 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
2079 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
2081 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
2085 * Returns the index of the first held_lock of the current chain
2087 static inline int get_first_held_lock(struct task_struct
*curr
,
2088 struct held_lock
*hlock
)
2091 struct held_lock
*hlock_curr
;
2093 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2094 hlock_curr
= curr
->held_locks
+ i
;
2095 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2103 #ifdef CONFIG_DEBUG_LOCKDEP
2105 * Returns the next chain_key iteration
2107 static u64
print_chain_key_iteration(int class_idx
, u64 chain_key
)
2109 u64 new_chain_key
= iterate_chain_key(chain_key
, class_idx
);
2111 printk(" class_idx:%d -> chain_key:%016Lx",
2113 (unsigned long long)new_chain_key
);
2114 return new_chain_key
;
2118 print_chain_keys_held_locks(struct task_struct
*curr
, struct held_lock
*hlock_next
)
2120 struct held_lock
*hlock
;
2122 int depth
= curr
->lockdep_depth
;
2125 printk("depth: %u\n", depth
+ 1);
2126 for (i
= get_first_held_lock(curr
, hlock_next
); i
< depth
; i
++) {
2127 hlock
= curr
->held_locks
+ i
;
2128 chain_key
= print_chain_key_iteration(hlock
->class_idx
, chain_key
);
2133 print_chain_key_iteration(hlock_next
->class_idx
, chain_key
);
2134 print_lock(hlock_next
);
2137 static void print_chain_keys_chain(struct lock_chain
*chain
)
2143 printk("depth: %u\n", chain
->depth
);
2144 for (i
= 0; i
< chain
->depth
; i
++) {
2145 class_id
= chain_hlocks
[chain
->base
+ i
];
2146 chain_key
= print_chain_key_iteration(class_id
+ 1, chain_key
);
2148 print_lock_name(lock_classes
+ class_id
);
2153 static void print_collision(struct task_struct
*curr
,
2154 struct held_lock
*hlock_next
,
2155 struct lock_chain
*chain
)
2158 pr_warn("============================\n");
2159 pr_warn("WARNING: chain_key collision\n");
2160 print_kernel_ident();
2161 pr_warn("----------------------------\n");
2162 pr_warn("%s/%d: ", current
->comm
, task_pid_nr(current
));
2163 pr_warn("Hash chain already cached but the contents don't match!\n");
2165 pr_warn("Held locks:");
2166 print_chain_keys_held_locks(curr
, hlock_next
);
2168 pr_warn("Locks in cached chain:");
2169 print_chain_keys_chain(chain
);
2171 pr_warn("\nstack backtrace:\n");
2177 * Checks whether the chain and the current held locks are consistent
2178 * in depth and also in content. If they are not it most likely means
2179 * that there was a collision during the calculation of the chain_key.
2180 * Returns: 0 not passed, 1 passed
2182 static int check_no_collision(struct task_struct
*curr
,
2183 struct held_lock
*hlock
,
2184 struct lock_chain
*chain
)
2186 #ifdef CONFIG_DEBUG_LOCKDEP
2189 i
= get_first_held_lock(curr
, hlock
);
2191 if (DEBUG_LOCKS_WARN_ON(chain
->depth
!= curr
->lockdep_depth
- (i
- 1))) {
2192 print_collision(curr
, hlock
, chain
);
2196 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2197 id
= curr
->held_locks
[i
].class_idx
- 1;
2199 if (DEBUG_LOCKS_WARN_ON(chain_hlocks
[chain
->base
+ j
] != id
)) {
2200 print_collision(curr
, hlock
, chain
);
2209 * This is for building a chain between just two different classes,
2210 * instead of adding a new hlock upon current, which is done by
2211 * add_chain_cache().
2213 * This can be called in any context with two classes, while
2214 * add_chain_cache() must be done within the lock owener's context
2215 * since it uses hlock which might be racy in another context.
2217 static inline int add_chain_cache_classes(unsigned int prev
,
2219 unsigned int irq_context
,
2222 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2223 struct lock_chain
*chain
;
2226 * Allocate a new chain entry from the static array, and add
2231 * We might need to take the graph lock, ensure we've got IRQs
2232 * disabled to make this an IRQ-safe lock.. for recursion reasons
2233 * lockdep won't complain about its own locking errors.
2235 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2238 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2239 if (!debug_locks_off_graph_unlock())
2242 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2247 chain
= lock_chains
+ nr_lock_chains
++;
2248 chain
->chain_key
= chain_key
;
2249 chain
->irq_context
= irq_context
;
2251 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2252 chain
->base
= nr_chain_hlocks
;
2253 nr_chain_hlocks
+= chain
->depth
;
2254 chain_hlocks
[chain
->base
] = prev
- 1;
2255 chain_hlocks
[chain
->base
+ 1] = next
-1;
2257 #ifdef CONFIG_DEBUG_LOCKDEP
2259 * Important for check_no_collision().
2262 if (!debug_locks_off_graph_unlock())
2265 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2271 hlist_add_head_rcu(&chain
->entry
, hash_head
);
2272 debug_atomic_inc(chain_lookup_misses
);
2279 * Adds a dependency chain into chain hashtable. And must be called with
2282 * Return 0 if fail, and graph_lock is released.
2283 * Return 1 if succeed, with graph_lock held.
2285 static inline int add_chain_cache(struct task_struct
*curr
,
2286 struct held_lock
*hlock
,
2289 struct lock_class
*class = hlock_class(hlock
);
2290 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2291 struct lock_chain
*chain
;
2295 * Allocate a new chain entry from the static array, and add
2300 * We might need to take the graph lock, ensure we've got IRQs
2301 * disabled to make this an IRQ-safe lock.. for recursion reasons
2302 * lockdep won't complain about its own locking errors.
2304 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2307 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2308 if (!debug_locks_off_graph_unlock())
2311 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2315 chain
= lock_chains
+ nr_lock_chains
++;
2316 chain
->chain_key
= chain_key
;
2317 chain
->irq_context
= hlock
->irq_context
;
2318 i
= get_first_held_lock(curr
, hlock
);
2319 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2321 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks
));
2322 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr
->held_locks
));
2323 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks
[0])) <= ARRAY_SIZE(lock_classes
));
2325 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2326 chain
->base
= nr_chain_hlocks
;
2327 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2328 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2329 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2331 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2334 if (nr_chain_hlocks
< MAX_LOCKDEP_CHAIN_HLOCKS
)
2335 nr_chain_hlocks
+= chain
->depth
;
2337 #ifdef CONFIG_DEBUG_LOCKDEP
2339 * Important for check_no_collision().
2341 if (unlikely(nr_chain_hlocks
> MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2342 if (!debug_locks_off_graph_unlock())
2345 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2351 hlist_add_head_rcu(&chain
->entry
, hash_head
);
2352 debug_atomic_inc(chain_lookup_misses
);
2359 * Look up a dependency chain.
2361 static inline struct lock_chain
*lookup_chain_cache(u64 chain_key
)
2363 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2364 struct lock_chain
*chain
;
2367 * We can walk it lock-free, because entries only get added
2370 hlist_for_each_entry_rcu(chain
, hash_head
, entry
) {
2371 if (chain
->chain_key
== chain_key
) {
2372 debug_atomic_inc(chain_lookup_hits
);
2380 * If the key is not present yet in dependency chain cache then
2381 * add it and return 1 - in this case the new dependency chain is
2382 * validated. If the key is already hashed, return 0.
2383 * (On return with 1 graph_lock is held.)
2385 static inline int lookup_chain_cache_add(struct task_struct
*curr
,
2386 struct held_lock
*hlock
,
2389 struct lock_class
*class = hlock_class(hlock
);
2390 struct lock_chain
*chain
= lookup_chain_cache(chain_key
);
2394 if (!check_no_collision(curr
, hlock
, chain
))
2397 if (very_verbose(class)) {
2398 printk("\nhash chain already cached, key: "
2399 "%016Lx tail class: [%p] %s\n",
2400 (unsigned long long)chain_key
,
2401 class->key
, class->name
);
2407 if (very_verbose(class)) {
2408 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2409 (unsigned long long)chain_key
, class->key
, class->name
);
2416 * We have to walk the chain again locked - to avoid duplicates:
2418 chain
= lookup_chain_cache(chain_key
);
2424 if (!add_chain_cache(curr
, hlock
, chain_key
))
2430 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2431 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2434 * Trylock needs to maintain the stack of held locks, but it
2435 * does not add new dependencies, because trylock can be done
2438 * We look up the chain_key and do the O(N^2) check and update of
2439 * the dependencies only if this is a new dependency chain.
2440 * (If lookup_chain_cache_add() return with 1 it acquires
2441 * graph_lock for us)
2443 if (!hlock
->trylock
&& hlock
->check
&&
2444 lookup_chain_cache_add(curr
, hlock
, chain_key
)) {
2446 * Check whether last held lock:
2448 * - is irq-safe, if this lock is irq-unsafe
2449 * - is softirq-safe, if this lock is hardirq-unsafe
2451 * And check whether the new lock's dependency graph
2452 * could lead back to the previous lock.
2454 * any of these scenarios could lead to a deadlock. If
2457 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2462 * Mark recursive read, as we jump over it when
2463 * building dependencies (just like we jump over
2469 * Add dependency only if this lock is not the head
2470 * of the chain, and if it's not a secondary read-lock:
2472 if (!chain_head
&& ret
!= 2) {
2473 if (!check_prevs_add(curr
, hlock
))
2479 /* after lookup_chain_cache_add(): */
2480 if (unlikely(!debug_locks
))
2487 static inline int validate_chain(struct task_struct
*curr
,
2488 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2489 int chain_head
, u64 chain_key
)
2496 * We are building curr_chain_key incrementally, so double-check
2497 * it from scratch, to make sure that it's done correctly:
2499 static void check_chain_key(struct task_struct
*curr
)
2501 #ifdef CONFIG_DEBUG_LOCKDEP
2502 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2506 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2507 hlock
= curr
->held_locks
+ i
;
2508 if (chain_key
!= hlock
->prev_chain_key
) {
2511 * We got mighty confused, our chain keys don't match
2512 * with what we expect, someone trample on our task state?
2514 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2515 curr
->lockdep_depth
, i
,
2516 (unsigned long long)chain_key
,
2517 (unsigned long long)hlock
->prev_chain_key
);
2521 * Whoops ran out of static storage again?
2523 if (DEBUG_LOCKS_WARN_ON(hlock
->class_idx
> MAX_LOCKDEP_KEYS
))
2526 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2527 hlock
->irq_context
))
2529 chain_key
= iterate_chain_key(chain_key
, hlock
->class_idx
);
2532 if (chain_key
!= curr
->curr_chain_key
) {
2535 * More smoking hash instead of calculating it, damn see these
2536 * numbers float.. I bet that a pink elephant stepped on my memory.
2538 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2539 curr
->lockdep_depth
, i
,
2540 (unsigned long long)chain_key
,
2541 (unsigned long long)curr
->curr_chain_key
);
2547 print_usage_bug_scenario(struct held_lock
*lock
)
2549 struct lock_class
*class = hlock_class(lock
);
2551 printk(" Possible unsafe locking scenario:\n\n");
2555 __print_lock_name(class);
2556 printk(KERN_CONT
");\n");
2557 printk(" <Interrupt>\n");
2559 __print_lock_name(class);
2560 printk(KERN_CONT
");\n");
2561 printk("\n *** DEADLOCK ***\n\n");
2565 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2566 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2568 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2572 pr_warn("================================\n");
2573 pr_warn("WARNING: inconsistent lock state\n");
2574 print_kernel_ident();
2575 pr_warn("--------------------------------\n");
2577 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2578 usage_str
[prev_bit
], usage_str
[new_bit
]);
2580 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2581 curr
->comm
, task_pid_nr(curr
),
2582 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2583 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2584 trace_hardirqs_enabled(curr
),
2585 trace_softirqs_enabled(curr
));
2588 pr_warn("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2589 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2591 print_irqtrace_events(curr
);
2592 pr_warn("\nother info that might help us debug this:\n");
2593 print_usage_bug_scenario(this);
2595 lockdep_print_held_locks(curr
);
2597 pr_warn("\nstack backtrace:\n");
2604 * Print out an error if an invalid bit is set:
2607 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2608 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2610 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2611 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2615 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2616 enum lock_usage_bit new_bit
);
2618 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2621 * print irq inversion bug:
2624 print_irq_inversion_bug(struct task_struct
*curr
,
2625 struct lock_list
*root
, struct lock_list
*other
,
2626 struct held_lock
*this, int forwards
,
2627 const char *irqclass
)
2629 struct lock_list
*entry
= other
;
2630 struct lock_list
*middle
= NULL
;
2633 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2637 pr_warn("========================================================\n");
2638 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2639 print_kernel_ident();
2640 pr_warn("--------------------------------------------------------\n");
2641 pr_warn("%s/%d just changed the state of lock:\n",
2642 curr
->comm
, task_pid_nr(curr
));
2645 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2647 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2648 print_lock_name(other
->class);
2649 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2651 pr_warn("\nother info that might help us debug this:\n");
2653 /* Find a middle lock (if one exists) */
2654 depth
= get_lock_depth(other
);
2656 if (depth
== 0 && (entry
!= root
)) {
2657 pr_warn("lockdep:%s bad path found in chain graph\n", __func__
);
2661 entry
= get_lock_parent(entry
);
2663 } while (entry
&& entry
!= root
&& (depth
>= 0));
2665 print_irq_lock_scenario(root
, other
,
2666 middle
? middle
->class : root
->class, other
->class);
2668 print_irq_lock_scenario(other
, root
,
2669 middle
? middle
->class : other
->class, root
->class);
2671 lockdep_print_held_locks(curr
);
2673 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2674 if (!save_trace(&root
->trace
))
2676 print_shortest_lock_dependencies(other
, root
);
2678 pr_warn("\nstack backtrace:\n");
2685 * Prove that in the forwards-direction subgraph starting at <this>
2686 * there is no lock matching <mask>:
2689 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2690 enum lock_usage_bit bit
, const char *irqclass
)
2693 struct lock_list root
;
2694 struct lock_list
*uninitialized_var(target_entry
);
2697 root
.class = hlock_class(this);
2698 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2700 return print_bfs_bug(ret
);
2704 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2709 * Prove that in the backwards-direction subgraph starting at <this>
2710 * there is no lock matching <mask>:
2713 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2714 enum lock_usage_bit bit
, const char *irqclass
)
2717 struct lock_list root
;
2718 struct lock_list
*uninitialized_var(target_entry
);
2721 root
.class = hlock_class(this);
2722 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2724 return print_bfs_bug(ret
);
2728 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2732 void print_irqtrace_events(struct task_struct
*curr
)
2734 printk("irq event stamp: %u\n", curr
->irq_events
);
2735 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2736 curr
->hardirq_enable_event
, (void *)curr
->hardirq_enable_ip
,
2737 (void *)curr
->hardirq_enable_ip
);
2738 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2739 curr
->hardirq_disable_event
, (void *)curr
->hardirq_disable_ip
,
2740 (void *)curr
->hardirq_disable_ip
);
2741 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2742 curr
->softirq_enable_event
, (void *)curr
->softirq_enable_ip
,
2743 (void *)curr
->softirq_enable_ip
);
2744 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2745 curr
->softirq_disable_event
, (void *)curr
->softirq_disable_ip
,
2746 (void *)curr
->softirq_disable_ip
);
2749 static int HARDIRQ_verbose(struct lock_class
*class)
2752 return class_filter(class);
2757 static int SOFTIRQ_verbose(struct lock_class
*class)
2760 return class_filter(class);
2765 #define STRICT_READ_CHECKS 1
2767 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2768 #define LOCKDEP_STATE(__STATE) \
2770 #include "lockdep_states.h"
2771 #undef LOCKDEP_STATE
2774 static inline int state_verbose(enum lock_usage_bit bit
,
2775 struct lock_class
*class)
2777 return state_verbose_f
[bit
>> 2](class);
2780 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2781 enum lock_usage_bit bit
, const char *name
);
2784 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2785 enum lock_usage_bit new_bit
)
2787 int excl_bit
= exclusive_bit(new_bit
);
2788 int read
= new_bit
& 1;
2789 int dir
= new_bit
& 2;
2792 * mark USED_IN has to look forwards -- to ensure no dependency
2793 * has ENABLED state, which would allow recursion deadlocks.
2795 * mark ENABLED has to look backwards -- to ensure no dependee
2796 * has USED_IN state, which, again, would allow recursion deadlocks.
2798 check_usage_f usage
= dir
?
2799 check_usage_backwards
: check_usage_forwards
;
2802 * Validate that this particular lock does not have conflicting
2805 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2809 * Validate that the lock dependencies don't have conflicting usage
2812 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2813 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2817 * Check for read in write conflicts
2820 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2823 if (STRICT_READ_CHECKS
&&
2824 !usage(curr
, this, excl_bit
+ 1,
2825 state_name(new_bit
+ 1)))
2829 if (state_verbose(new_bit
, hlock_class(this)))
2836 #define LOCKDEP_STATE(__STATE) __STATE,
2837 #include "lockdep_states.h"
2838 #undef LOCKDEP_STATE
2842 * Mark all held locks with a usage bit:
2845 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2847 enum lock_usage_bit usage_bit
;
2848 struct held_lock
*hlock
;
2851 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2852 hlock
= curr
->held_locks
+ i
;
2854 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2856 usage_bit
+= 1; /* READ */
2858 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2863 if (!mark_lock(curr
, hlock
, usage_bit
))
2871 * Hardirqs will be enabled:
2873 static void __trace_hardirqs_on_caller(unsigned long ip
)
2875 struct task_struct
*curr
= current
;
2877 /* we'll do an OFF -> ON transition: */
2878 curr
->hardirqs_enabled
= 1;
2881 * We are going to turn hardirqs on, so set the
2882 * usage bit for all held locks:
2884 if (!mark_held_locks(curr
, HARDIRQ
))
2887 * If we have softirqs enabled, then set the usage
2888 * bit for all held locks. (disabled hardirqs prevented
2889 * this bit from being set before)
2891 if (curr
->softirqs_enabled
)
2892 if (!mark_held_locks(curr
, SOFTIRQ
))
2895 curr
->hardirq_enable_ip
= ip
;
2896 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2897 debug_atomic_inc(hardirqs_on_events
);
2900 __visible
void trace_hardirqs_on_caller(unsigned long ip
)
2902 time_hardirqs_on(CALLER_ADDR0
, ip
);
2904 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2907 if (unlikely(current
->hardirqs_enabled
)) {
2909 * Neither irq nor preemption are disabled here
2910 * so this is racy by nature but losing one hit
2911 * in a stat is not a big deal.
2913 __debug_atomic_inc(redundant_hardirqs_on
);
2918 * We're enabling irqs and according to our state above irqs weren't
2919 * already enabled, yet we find the hardware thinks they are in fact
2920 * enabled.. someone messed up their IRQ state tracing.
2922 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2926 * See the fine text that goes along with this variable definition.
2928 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2932 * Can't allow enabling interrupts while in an interrupt handler,
2933 * that's general bad form and such. Recursion, limited stack etc..
2935 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2938 current
->lockdep_recursion
= 1;
2939 __trace_hardirqs_on_caller(ip
);
2940 current
->lockdep_recursion
= 0;
2942 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2944 void trace_hardirqs_on(void)
2946 trace_hardirqs_on_caller(CALLER_ADDR0
);
2948 EXPORT_SYMBOL(trace_hardirqs_on
);
2951 * Hardirqs were disabled:
2953 __visible
void trace_hardirqs_off_caller(unsigned long ip
)
2955 struct task_struct
*curr
= current
;
2957 time_hardirqs_off(CALLER_ADDR0
, ip
);
2959 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2963 * So we're supposed to get called after you mask local IRQs, but for
2964 * some reason the hardware doesn't quite think you did a proper job.
2966 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2969 if (curr
->hardirqs_enabled
) {
2971 * We have done an ON -> OFF transition:
2973 curr
->hardirqs_enabled
= 0;
2974 curr
->hardirq_disable_ip
= ip
;
2975 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2976 debug_atomic_inc(hardirqs_off_events
);
2978 debug_atomic_inc(redundant_hardirqs_off
);
2980 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2982 void trace_hardirqs_off(void)
2984 trace_hardirqs_off_caller(CALLER_ADDR0
);
2986 EXPORT_SYMBOL(trace_hardirqs_off
);
2989 * Softirqs will be enabled:
2991 void trace_softirqs_on(unsigned long ip
)
2993 struct task_struct
*curr
= current
;
2995 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2999 * We fancy IRQs being disabled here, see softirq.c, avoids
3000 * funny state and nesting things.
3002 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3005 if (curr
->softirqs_enabled
) {
3006 debug_atomic_inc(redundant_softirqs_on
);
3010 current
->lockdep_recursion
= 1;
3012 * We'll do an OFF -> ON transition:
3014 curr
->softirqs_enabled
= 1;
3015 curr
->softirq_enable_ip
= ip
;
3016 curr
->softirq_enable_event
= ++curr
->irq_events
;
3017 debug_atomic_inc(softirqs_on_events
);
3019 * We are going to turn softirqs on, so set the
3020 * usage bit for all held locks, if hardirqs are
3023 if (curr
->hardirqs_enabled
)
3024 mark_held_locks(curr
, SOFTIRQ
);
3025 current
->lockdep_recursion
= 0;
3029 * Softirqs were disabled:
3031 void trace_softirqs_off(unsigned long ip
)
3033 struct task_struct
*curr
= current
;
3035 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
3039 * We fancy IRQs being disabled here, see softirq.c
3041 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3044 if (curr
->softirqs_enabled
) {
3046 * We have done an ON -> OFF transition:
3048 curr
->softirqs_enabled
= 0;
3049 curr
->softirq_disable_ip
= ip
;
3050 curr
->softirq_disable_event
= ++curr
->irq_events
;
3051 debug_atomic_inc(softirqs_off_events
);
3053 * Whoops, we wanted softirqs off, so why aren't they?
3055 DEBUG_LOCKS_WARN_ON(!softirq_count());
3057 debug_atomic_inc(redundant_softirqs_off
);
3060 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
3063 * If non-trylock use in a hardirq or softirq context, then
3064 * mark the lock as used in these contexts:
3066 if (!hlock
->trylock
) {
3068 if (curr
->hardirq_context
)
3069 if (!mark_lock(curr
, hlock
,
3070 LOCK_USED_IN_HARDIRQ_READ
))
3072 if (curr
->softirq_context
)
3073 if (!mark_lock(curr
, hlock
,
3074 LOCK_USED_IN_SOFTIRQ_READ
))
3077 if (curr
->hardirq_context
)
3078 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
3080 if (curr
->softirq_context
)
3081 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
3085 if (!hlock
->hardirqs_off
) {
3087 if (!mark_lock(curr
, hlock
,
3088 LOCK_ENABLED_HARDIRQ_READ
))
3090 if (curr
->softirqs_enabled
)
3091 if (!mark_lock(curr
, hlock
,
3092 LOCK_ENABLED_SOFTIRQ_READ
))
3095 if (!mark_lock(curr
, hlock
,
3096 LOCK_ENABLED_HARDIRQ
))
3098 if (curr
->softirqs_enabled
)
3099 if (!mark_lock(curr
, hlock
,
3100 LOCK_ENABLED_SOFTIRQ
))
3108 static inline unsigned int task_irq_context(struct task_struct
*task
)
3110 return 2 * !!task
->hardirq_context
+ !!task
->softirq_context
;
3113 static int separate_irq_context(struct task_struct
*curr
,
3114 struct held_lock
*hlock
)
3116 unsigned int depth
= curr
->lockdep_depth
;
3119 * Keep track of points where we cross into an interrupt context:
3122 struct held_lock
*prev_hlock
;
3124 prev_hlock
= curr
->held_locks
+ depth
-1;
3126 * If we cross into another context, reset the
3127 * hash key (this also prevents the checking and the
3128 * adding of the dependency to 'prev'):
3130 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
3136 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3139 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
3140 enum lock_usage_bit new_bit
)
3142 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3146 static inline int mark_irqflags(struct task_struct
*curr
,
3147 struct held_lock
*hlock
)
3152 static inline unsigned int task_irq_context(struct task_struct
*task
)
3157 static inline int separate_irq_context(struct task_struct
*curr
,
3158 struct held_lock
*hlock
)
3163 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3166 * Mark a lock with a usage bit, and validate the state transition:
3168 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
3169 enum lock_usage_bit new_bit
)
3171 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
3174 * If already set then do not dirty the cacheline,
3175 * nor do any checks:
3177 if (likely(hlock_class(this)->usage_mask
& new_mask
))
3183 * Make sure we didn't race:
3185 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
3190 hlock_class(this)->usage_mask
|= new_mask
;
3192 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
3196 #define LOCKDEP_STATE(__STATE) \
3197 case LOCK_USED_IN_##__STATE: \
3198 case LOCK_USED_IN_##__STATE##_READ: \
3199 case LOCK_ENABLED_##__STATE: \
3200 case LOCK_ENABLED_##__STATE##_READ:
3201 #include "lockdep_states.h"
3202 #undef LOCKDEP_STATE
3203 ret
= mark_lock_irq(curr
, this, new_bit
);
3208 debug_atomic_dec(nr_unused_locks
);
3211 if (!debug_locks_off_graph_unlock())
3220 * We must printk outside of the graph_lock:
3223 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
3225 print_irqtrace_events(curr
);
3233 * Initialize a lock instance's lock-class mapping info:
3235 static void __lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
3236 struct lock_class_key
*key
, int subclass
)
3240 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
3241 lock
->class_cache
[i
] = NULL
;
3243 #ifdef CONFIG_LOCK_STAT
3244 lock
->cpu
= raw_smp_processor_id();
3248 * Can't be having no nameless bastards around this place!
3250 if (DEBUG_LOCKS_WARN_ON(!name
)) {
3251 lock
->name
= "NULL";
3258 * No key, no joy, we need to hash something.
3260 if (DEBUG_LOCKS_WARN_ON(!key
))
3263 * Sanity check, the lock-class key must be persistent:
3265 if (!static_obj(key
)) {
3266 printk("BUG: key %p not in .data!\n", key
);
3268 * What it says above ^^^^^, I suggest you read it.
3270 DEBUG_LOCKS_WARN_ON(1);
3275 if (unlikely(!debug_locks
))
3279 unsigned long flags
;
3281 if (DEBUG_LOCKS_WARN_ON(current
->lockdep_recursion
))
3284 raw_local_irq_save(flags
);
3285 current
->lockdep_recursion
= 1;
3286 register_lock_class(lock
, subclass
, 1);
3287 current
->lockdep_recursion
= 0;
3288 raw_local_irq_restore(flags
);
3292 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
3293 struct lock_class_key
*key
, int subclass
)
3295 cross_init(lock
, 0);
3296 __lockdep_init_map(lock
, name
, key
, subclass
);
3298 EXPORT_SYMBOL_GPL(lockdep_init_map
);
3300 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
3301 void lockdep_init_map_crosslock(struct lockdep_map
*lock
, const char *name
,
3302 struct lock_class_key
*key
, int subclass
)
3304 cross_init(lock
, 1);
3305 __lockdep_init_map(lock
, name
, key
, subclass
);
3307 EXPORT_SYMBOL_GPL(lockdep_init_map_crosslock
);
3310 struct lock_class_key __lockdep_no_validate__
;
3311 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
3314 print_lock_nested_lock_not_held(struct task_struct
*curr
,
3315 struct held_lock
*hlock
,
3318 if (!debug_locks_off())
3320 if (debug_locks_silent
)
3324 pr_warn("==================================\n");
3325 pr_warn("WARNING: Nested lock was not taken\n");
3326 print_kernel_ident();
3327 pr_warn("----------------------------------\n");
3329 pr_warn("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3332 pr_warn("\nbut this task is not holding:\n");
3333 pr_warn("%s\n", hlock
->nest_lock
->name
);
3335 pr_warn("\nstack backtrace:\n");
3338 pr_warn("\nother info that might help us debug this:\n");
3339 lockdep_print_held_locks(curr
);
3341 pr_warn("\nstack backtrace:\n");
3347 static int __lock_is_held(struct lockdep_map
*lock
, int read
);
3350 * This gets called for every mutex_lock*()/spin_lock*() operation.
3351 * We maintain the dependency maps and validate the locking attempt:
3353 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3354 int trylock
, int read
, int check
, int hardirqs_off
,
3355 struct lockdep_map
*nest_lock
, unsigned long ip
,
3356 int references
, int pin_count
)
3358 struct task_struct
*curr
= current
;
3359 struct lock_class
*class = NULL
;
3360 struct held_lock
*hlock
;
3367 if (unlikely(!debug_locks
))
3371 * Lockdep should run with IRQs disabled, otherwise we could
3372 * get an interrupt which would want to take locks, which would
3373 * end up in lockdep and have you got a head-ache already?
3375 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3378 if (!prove_locking
|| lock
->key
== &__lockdep_no_validate__
)
3381 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
3382 class = lock
->class_cache
[subclass
];
3386 if (unlikely(!class)) {
3387 class = register_lock_class(lock
, subclass
, 0);
3391 atomic_inc((atomic_t
*)&class->ops
);
3392 if (very_verbose(class)) {
3393 printk("\nacquire class [%p] %s", class->key
, class->name
);
3394 if (class->name_version
> 1)
3395 printk(KERN_CONT
"#%d", class->name_version
);
3396 printk(KERN_CONT
"\n");
3401 * Add the lock to the list of currently held locks.
3402 * (we dont increase the depth just yet, up until the
3403 * dependency checks are done)
3405 depth
= curr
->lockdep_depth
;
3407 * Ran out of static storage for our per-task lock stack again have we?
3409 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
3412 class_idx
= class - lock_classes
+ 1;
3414 /* TODO: nest_lock is not implemented for crosslock yet. */
3415 if (depth
&& !cross_lock(lock
)) {
3416 hlock
= curr
->held_locks
+ depth
- 1;
3417 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
3418 if (hlock
->references
) {
3420 * Check: unsigned int references:12, overflow.
3422 if (DEBUG_LOCKS_WARN_ON(hlock
->references
== (1 << 12)-1))
3425 hlock
->references
++;
3427 hlock
->references
= 2;
3434 hlock
= curr
->held_locks
+ depth
;
3436 * Plain impossible, we just registered it and checked it weren't no
3437 * NULL like.. I bet this mushroom I ate was good!
3439 if (DEBUG_LOCKS_WARN_ON(!class))
3441 hlock
->class_idx
= class_idx
;
3442 hlock
->acquire_ip
= ip
;
3443 hlock
->instance
= lock
;
3444 hlock
->nest_lock
= nest_lock
;
3445 hlock
->irq_context
= task_irq_context(curr
);
3446 hlock
->trylock
= trylock
;
3448 hlock
->check
= check
;
3449 hlock
->hardirqs_off
= !!hardirqs_off
;
3450 hlock
->references
= references
;
3451 #ifdef CONFIG_LOCK_STAT
3452 hlock
->waittime_stamp
= 0;
3453 hlock
->holdtime_stamp
= lockstat_clock();
3455 hlock
->pin_count
= pin_count
;
3457 if (check
&& !mark_irqflags(curr
, hlock
))
3460 /* mark it as used: */
3461 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3465 * Calculate the chain hash: it's the combined hash of all the
3466 * lock keys along the dependency chain. We save the hash value
3467 * at every step so that we can get the current hash easily
3468 * after unlock. The chain hash is then used to cache dependency
3471 * The 'key ID' is what is the most compact key value to drive
3472 * the hash, not class->key.
3475 * Whoops, we did it again.. ran straight out of our static allocation.
3477 if (DEBUG_LOCKS_WARN_ON(class_idx
> MAX_LOCKDEP_KEYS
))
3480 chain_key
= curr
->curr_chain_key
;
3483 * How can we have a chain hash when we ain't got no keys?!
3485 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3490 hlock
->prev_chain_key
= chain_key
;
3491 if (separate_irq_context(curr
, hlock
)) {
3495 chain_key
= iterate_chain_key(chain_key
, class_idx
);
3497 if (nest_lock
&& !__lock_is_held(nest_lock
, -1))
3498 return print_lock_nested_lock_not_held(curr
, hlock
, ip
);
3500 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3503 ret
= lock_acquire_crosslock(hlock
);
3505 * 2 means normal acquire operations are needed. Otherwise, it's
3506 * ok just to return with '0:fail, 1:success'.
3511 curr
->curr_chain_key
= chain_key
;
3512 curr
->lockdep_depth
++;
3513 check_chain_key(curr
);
3514 #ifdef CONFIG_DEBUG_LOCKDEP
3515 if (unlikely(!debug_locks
))
3518 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3520 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3521 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
3522 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
3524 lockdep_print_held_locks(current
);
3525 debug_show_all_locks();
3531 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3532 max_lockdep_depth
= curr
->lockdep_depth
;
3538 print_unlock_imbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3541 if (!debug_locks_off())
3543 if (debug_locks_silent
)
3547 pr_warn("=====================================\n");
3548 pr_warn("WARNING: bad unlock balance detected!\n");
3549 print_kernel_ident();
3550 pr_warn("-------------------------------------\n");
3551 pr_warn("%s/%d is trying to release lock (",
3552 curr
->comm
, task_pid_nr(curr
));
3553 print_lockdep_cache(lock
);
3556 pr_warn("but there are no more locks to release!\n");
3557 pr_warn("\nother info that might help us debug this:\n");
3558 lockdep_print_held_locks(curr
);
3560 pr_warn("\nstack backtrace:\n");
3566 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
3568 if (hlock
->instance
== lock
)
3571 if (hlock
->references
) {
3572 struct lock_class
*class = lock
->class_cache
[0];
3575 class = look_up_lock_class(lock
, 0);
3578 * If look_up_lock_class() failed to find a class, we're trying
3579 * to test if we hold a lock that has never yet been acquired.
3580 * Clearly if the lock hasn't been acquired _ever_, we're not
3581 * holding it either, so report failure.
3583 if (IS_ERR_OR_NULL(class))
3587 * References, but not a lock we're actually ref-counting?
3588 * State got messed up, follow the sites that change ->references
3589 * and try to make sense of it.
3591 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3594 if (hlock
->class_idx
== class - lock_classes
+ 1)
3601 /* @depth must not be zero */
3602 static struct held_lock
*find_held_lock(struct task_struct
*curr
,
3603 struct lockdep_map
*lock
,
3604 unsigned int depth
, int *idx
)
3606 struct held_lock
*ret
, *hlock
, *prev_hlock
;
3610 hlock
= curr
->held_locks
+ i
;
3612 if (match_held_lock(hlock
, lock
))
3616 for (i
--, prev_hlock
= hlock
--;
3618 i
--, prev_hlock
= hlock
--) {
3620 * We must not cross into another context:
3622 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
3626 if (match_held_lock(hlock
, lock
)) {
3637 static int reacquire_held_locks(struct task_struct
*curr
, unsigned int depth
,
3640 struct held_lock
*hlock
;
3642 for (hlock
= curr
->held_locks
+ idx
; idx
< depth
; idx
++, hlock
++) {
3643 if (!__lock_acquire(hlock
->instance
,
3644 hlock_class(hlock
)->subclass
,
3646 hlock
->read
, hlock
->check
,
3647 hlock
->hardirqs_off
,
3648 hlock
->nest_lock
, hlock
->acquire_ip
,
3649 hlock
->references
, hlock
->pin_count
))
3656 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3657 struct lock_class_key
*key
, unsigned int subclass
,
3660 struct task_struct
*curr
= current
;
3661 struct held_lock
*hlock
;
3662 struct lock_class
*class;
3666 depth
= curr
->lockdep_depth
;
3668 * This function is about (re)setting the class of a held lock,
3669 * yet we're not actually holding any locks. Naughty user!
3671 if (DEBUG_LOCKS_WARN_ON(!depth
))
3674 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3676 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3678 lockdep_init_map(lock
, name
, key
, 0);
3679 class = register_lock_class(lock
, subclass
, 0);
3680 hlock
->class_idx
= class - lock_classes
+ 1;
3682 curr
->lockdep_depth
= i
;
3683 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3685 if (reacquire_held_locks(curr
, depth
, i
))
3689 * I took it apart and put it back together again, except now I have
3690 * these 'spare' parts.. where shall I put them.
3692 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3697 static int __lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
3699 struct task_struct
*curr
= current
;
3700 struct held_lock
*hlock
;
3704 depth
= curr
->lockdep_depth
;
3706 * This function is about (re)setting the class of a held lock,
3707 * yet we're not actually holding any locks. Naughty user!
3709 if (DEBUG_LOCKS_WARN_ON(!depth
))
3712 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3714 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3716 curr
->lockdep_depth
= i
;
3717 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3719 WARN(hlock
->read
, "downgrading a read lock");
3721 hlock
->acquire_ip
= ip
;
3723 if (reacquire_held_locks(curr
, depth
, i
))
3727 * I took it apart and put it back together again, except now I have
3728 * these 'spare' parts.. where shall I put them.
3730 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3736 * Remove the lock to the list of currently held locks - this gets
3737 * called on mutex_unlock()/spin_unlock*() (or on a failed
3738 * mutex_lock_interruptible()).
3740 * @nested is an hysterical artifact, needs a tree wide cleanup.
3743 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3745 struct task_struct
*curr
= current
;
3746 struct held_lock
*hlock
;
3750 if (unlikely(!debug_locks
))
3753 ret
= lock_release_crosslock(lock
);
3755 * 2 means normal release operations are needed. Otherwise, it's
3756 * ok just to return with '0:fail, 1:success'.
3761 depth
= curr
->lockdep_depth
;
3763 * So we're all set to release this lock.. wait what lock? We don't
3764 * own any locks, you've been drinking again?
3766 if (DEBUG_LOCKS_WARN_ON(depth
<= 0))
3767 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3770 * Check whether the lock exists in the current stack
3773 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3775 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3777 if (hlock
->instance
== lock
)
3778 lock_release_holdtime(hlock
);
3780 WARN(hlock
->pin_count
, "releasing a pinned lock\n");
3782 if (hlock
->references
) {
3783 hlock
->references
--;
3784 if (hlock
->references
) {
3786 * We had, and after removing one, still have
3787 * references, the current lock stack is still
3788 * valid. We're done!
3795 * We have the right lock to unlock, 'hlock' points to it.
3796 * Now we remove it from the stack, and add back the other
3797 * entries (if any), recalculating the hash along the way:
3800 curr
->lockdep_depth
= i
;
3801 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3803 if (reacquire_held_locks(curr
, depth
, i
+ 1))
3807 * We had N bottles of beer on the wall, we drank one, but now
3808 * there's not N-1 bottles of beer left on the wall...
3810 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3816 static int __lock_is_held(struct lockdep_map
*lock
, int read
)
3818 struct task_struct
*curr
= current
;
3821 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3822 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3824 if (match_held_lock(hlock
, lock
)) {
3825 if (read
== -1 || hlock
->read
== read
)
3835 static struct pin_cookie
__lock_pin_lock(struct lockdep_map
*lock
)
3837 struct pin_cookie cookie
= NIL_COOKIE
;
3838 struct task_struct
*curr
= current
;
3841 if (unlikely(!debug_locks
))
3844 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3845 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3847 if (match_held_lock(hlock
, lock
)) {
3849 * Grab 16bits of randomness; this is sufficient to not
3850 * be guessable and still allows some pin nesting in
3851 * our u32 pin_count.
3853 cookie
.val
= 1 + (prandom_u32() >> 16);
3854 hlock
->pin_count
+= cookie
.val
;
3859 WARN(1, "pinning an unheld lock\n");
3863 static void __lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3865 struct task_struct
*curr
= current
;
3868 if (unlikely(!debug_locks
))
3871 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3872 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3874 if (match_held_lock(hlock
, lock
)) {
3875 hlock
->pin_count
+= cookie
.val
;
3880 WARN(1, "pinning an unheld lock\n");
3883 static void __lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3885 struct task_struct
*curr
= current
;
3888 if (unlikely(!debug_locks
))
3891 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3892 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3894 if (match_held_lock(hlock
, lock
)) {
3895 if (WARN(!hlock
->pin_count
, "unpinning an unpinned lock\n"))
3898 hlock
->pin_count
-= cookie
.val
;
3900 if (WARN((int)hlock
->pin_count
< 0, "pin count corrupted\n"))
3901 hlock
->pin_count
= 0;
3907 WARN(1, "unpinning an unheld lock\n");
3911 * Check whether we follow the irq-flags state precisely:
3913 static void check_flags(unsigned long flags
)
3915 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3916 defined(CONFIG_TRACE_IRQFLAGS)
3920 if (irqs_disabled_flags(flags
)) {
3921 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3922 printk("possible reason: unannotated irqs-off.\n");
3925 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3926 printk("possible reason: unannotated irqs-on.\n");
3931 * We dont accurately track softirq state in e.g.
3932 * hardirq contexts (such as on 4KSTACKS), so only
3933 * check if not in hardirq contexts:
3935 if (!hardirq_count()) {
3936 if (softirq_count()) {
3937 /* like the above, but with softirqs */
3938 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3940 /* lick the above, does it taste good? */
3941 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3946 print_irqtrace_events(current
);
3950 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3951 struct lock_class_key
*key
, unsigned int subclass
,
3954 unsigned long flags
;
3956 if (unlikely(current
->lockdep_recursion
))
3959 raw_local_irq_save(flags
);
3960 current
->lockdep_recursion
= 1;
3962 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3963 check_chain_key(current
);
3964 current
->lockdep_recursion
= 0;
3965 raw_local_irq_restore(flags
);
3967 EXPORT_SYMBOL_GPL(lock_set_class
);
3969 void lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
3971 unsigned long flags
;
3973 if (unlikely(current
->lockdep_recursion
))
3976 raw_local_irq_save(flags
);
3977 current
->lockdep_recursion
= 1;
3979 if (__lock_downgrade(lock
, ip
))
3980 check_chain_key(current
);
3981 current
->lockdep_recursion
= 0;
3982 raw_local_irq_restore(flags
);
3984 EXPORT_SYMBOL_GPL(lock_downgrade
);
3987 * We are not always called with irqs disabled - do that here,
3988 * and also avoid lockdep recursion:
3990 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3991 int trylock
, int read
, int check
,
3992 struct lockdep_map
*nest_lock
, unsigned long ip
)
3994 unsigned long flags
;
3996 if (unlikely(current
->lockdep_recursion
))
3999 raw_local_irq_save(flags
);
4002 current
->lockdep_recursion
= 1;
4003 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
4004 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
4005 irqs_disabled_flags(flags
), nest_lock
, ip
, 0, 0);
4006 current
->lockdep_recursion
= 0;
4007 raw_local_irq_restore(flags
);
4009 EXPORT_SYMBOL_GPL(lock_acquire
);
4011 void lock_release(struct lockdep_map
*lock
, int nested
,
4014 unsigned long flags
;
4016 if (unlikely(current
->lockdep_recursion
))
4019 raw_local_irq_save(flags
);
4021 current
->lockdep_recursion
= 1;
4022 trace_lock_release(lock
, ip
);
4023 if (__lock_release(lock
, nested
, ip
))
4024 check_chain_key(current
);
4025 current
->lockdep_recursion
= 0;
4026 raw_local_irq_restore(flags
);
4028 EXPORT_SYMBOL_GPL(lock_release
);
4030 int lock_is_held_type(struct lockdep_map
*lock
, int read
)
4032 unsigned long flags
;
4035 if (unlikely(current
->lockdep_recursion
))
4036 return 1; /* avoid false negative lockdep_assert_held() */
4038 raw_local_irq_save(flags
);
4041 current
->lockdep_recursion
= 1;
4042 ret
= __lock_is_held(lock
, read
);
4043 current
->lockdep_recursion
= 0;
4044 raw_local_irq_restore(flags
);
4048 EXPORT_SYMBOL_GPL(lock_is_held_type
);
4050 struct pin_cookie
lock_pin_lock(struct lockdep_map
*lock
)
4052 struct pin_cookie cookie
= NIL_COOKIE
;
4053 unsigned long flags
;
4055 if (unlikely(current
->lockdep_recursion
))
4058 raw_local_irq_save(flags
);
4061 current
->lockdep_recursion
= 1;
4062 cookie
= __lock_pin_lock(lock
);
4063 current
->lockdep_recursion
= 0;
4064 raw_local_irq_restore(flags
);
4068 EXPORT_SYMBOL_GPL(lock_pin_lock
);
4070 void lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
4072 unsigned long flags
;
4074 if (unlikely(current
->lockdep_recursion
))
4077 raw_local_irq_save(flags
);
4080 current
->lockdep_recursion
= 1;
4081 __lock_repin_lock(lock
, cookie
);
4082 current
->lockdep_recursion
= 0;
4083 raw_local_irq_restore(flags
);
4085 EXPORT_SYMBOL_GPL(lock_repin_lock
);
4087 void lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
4089 unsigned long flags
;
4091 if (unlikely(current
->lockdep_recursion
))
4094 raw_local_irq_save(flags
);
4097 current
->lockdep_recursion
= 1;
4098 __lock_unpin_lock(lock
, cookie
);
4099 current
->lockdep_recursion
= 0;
4100 raw_local_irq_restore(flags
);
4102 EXPORT_SYMBOL_GPL(lock_unpin_lock
);
4104 #ifdef CONFIG_LOCK_STAT
4106 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
4109 if (!debug_locks_off())
4111 if (debug_locks_silent
)
4115 pr_warn("=================================\n");
4116 pr_warn("WARNING: bad contention detected!\n");
4117 print_kernel_ident();
4118 pr_warn("---------------------------------\n");
4119 pr_warn("%s/%d is trying to contend lock (",
4120 curr
->comm
, task_pid_nr(curr
));
4121 print_lockdep_cache(lock
);
4124 pr_warn("but there are no locks held!\n");
4125 pr_warn("\nother info that might help us debug this:\n");
4126 lockdep_print_held_locks(curr
);
4128 pr_warn("\nstack backtrace:\n");
4135 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
4137 struct task_struct
*curr
= current
;
4138 struct held_lock
*hlock
;
4139 struct lock_class_stats
*stats
;
4141 int i
, contention_point
, contending_point
;
4143 depth
= curr
->lockdep_depth
;
4145 * Whee, we contended on this lock, except it seems we're not
4146 * actually trying to acquire anything much at all..
4148 if (DEBUG_LOCKS_WARN_ON(!depth
))
4151 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4153 print_lock_contention_bug(curr
, lock
, ip
);
4157 if (hlock
->instance
!= lock
)
4160 hlock
->waittime_stamp
= lockstat_clock();
4162 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
4163 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
4166 stats
= get_lock_stats(hlock_class(hlock
));
4167 if (contention_point
< LOCKSTAT_POINTS
)
4168 stats
->contention_point
[contention_point
]++;
4169 if (contending_point
< LOCKSTAT_POINTS
)
4170 stats
->contending_point
[contending_point
]++;
4171 if (lock
->cpu
!= smp_processor_id())
4172 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
4173 put_lock_stats(stats
);
4177 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4179 struct task_struct
*curr
= current
;
4180 struct held_lock
*hlock
;
4181 struct lock_class_stats
*stats
;
4183 u64 now
, waittime
= 0;
4186 depth
= curr
->lockdep_depth
;
4188 * Yay, we acquired ownership of this lock we didn't try to
4189 * acquire, how the heck did that happen?
4191 if (DEBUG_LOCKS_WARN_ON(!depth
))
4194 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4196 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
4200 if (hlock
->instance
!= lock
)
4203 cpu
= smp_processor_id();
4204 if (hlock
->waittime_stamp
) {
4205 now
= lockstat_clock();
4206 waittime
= now
- hlock
->waittime_stamp
;
4207 hlock
->holdtime_stamp
= now
;
4210 trace_lock_acquired(lock
, ip
);
4212 stats
= get_lock_stats(hlock_class(hlock
));
4215 lock_time_inc(&stats
->read_waittime
, waittime
);
4217 lock_time_inc(&stats
->write_waittime
, waittime
);
4219 if (lock
->cpu
!= cpu
)
4220 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
4221 put_lock_stats(stats
);
4227 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
4229 unsigned long flags
;
4231 if (unlikely(!lock_stat
))
4234 if (unlikely(current
->lockdep_recursion
))
4237 raw_local_irq_save(flags
);
4239 current
->lockdep_recursion
= 1;
4240 trace_lock_contended(lock
, ip
);
4241 __lock_contended(lock
, ip
);
4242 current
->lockdep_recursion
= 0;
4243 raw_local_irq_restore(flags
);
4245 EXPORT_SYMBOL_GPL(lock_contended
);
4247 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4249 unsigned long flags
;
4251 if (unlikely(!lock_stat
))
4254 if (unlikely(current
->lockdep_recursion
))
4257 raw_local_irq_save(flags
);
4259 current
->lockdep_recursion
= 1;
4260 __lock_acquired(lock
, ip
);
4261 current
->lockdep_recursion
= 0;
4262 raw_local_irq_restore(flags
);
4264 EXPORT_SYMBOL_GPL(lock_acquired
);
4268 * Used by the testsuite, sanitize the validator state
4269 * after a simulated failure:
4272 void lockdep_reset(void)
4274 unsigned long flags
;
4277 raw_local_irq_save(flags
);
4278 current
->curr_chain_key
= 0;
4279 current
->lockdep_depth
= 0;
4280 current
->lockdep_recursion
= 0;
4281 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
4282 nr_hardirq_chains
= 0;
4283 nr_softirq_chains
= 0;
4284 nr_process_chains
= 0;
4286 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
4287 INIT_HLIST_HEAD(chainhash_table
+ i
);
4288 raw_local_irq_restore(flags
);
4291 static void zap_class(struct lock_class
*class)
4296 * Remove all dependencies this lock is
4299 for (i
= 0; i
< nr_list_entries
; i
++) {
4300 if (list_entries
[i
].class == class)
4301 list_del_rcu(&list_entries
[i
].entry
);
4304 * Unhash the class and remove it from the all_lock_classes list:
4306 hlist_del_rcu(&class->hash_entry
);
4307 list_del_rcu(&class->lock_entry
);
4309 RCU_INIT_POINTER(class->key
, NULL
);
4310 RCU_INIT_POINTER(class->name
, NULL
);
4313 static inline int within(const void *addr
, void *start
, unsigned long size
)
4315 return addr
>= start
&& addr
< start
+ size
;
4319 * Used in module.c to remove lock classes from memory that is going to be
4320 * freed; and possibly re-used by other modules.
4322 * We will have had one sync_sched() before getting here, so we're guaranteed
4323 * nobody will look up these exact classes -- they're properly dead but still
4326 void lockdep_free_key_range(void *start
, unsigned long size
)
4328 struct lock_class
*class;
4329 struct hlist_head
*head
;
4330 unsigned long flags
;
4334 raw_local_irq_save(flags
);
4335 locked
= graph_lock();
4338 * Unhash all classes that were created by this module:
4340 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4341 head
= classhash_table
+ i
;
4342 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4343 if (within(class->key
, start
, size
))
4345 else if (within(class->name
, start
, size
))
4352 raw_local_irq_restore(flags
);
4355 * Wait for any possible iterators from look_up_lock_class() to pass
4356 * before continuing to free the memory they refer to.
4358 * sync_sched() is sufficient because the read-side is IRQ disable.
4360 synchronize_sched();
4363 * XXX at this point we could return the resources to the pool;
4364 * instead we leak them. We would need to change to bitmap allocators
4365 * instead of the linear allocators we have now.
4369 void lockdep_reset_lock(struct lockdep_map
*lock
)
4371 struct lock_class
*class;
4372 struct hlist_head
*head
;
4373 unsigned long flags
;
4377 raw_local_irq_save(flags
);
4380 * Remove all classes this lock might have:
4382 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
4384 * If the class exists we look it up and zap it:
4386 class = look_up_lock_class(lock
, j
);
4387 if (!IS_ERR_OR_NULL(class))
4391 * Debug check: in the end all mapped classes should
4394 locked
= graph_lock();
4395 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4396 head
= classhash_table
+ i
;
4397 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4400 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
4401 match
|= class == lock
->class_cache
[j
];
4403 if (unlikely(match
)) {
4404 if (debug_locks_off_graph_unlock()) {
4406 * We all just reset everything, how did it match?
4418 raw_local_irq_restore(flags
);
4421 void __init
lockdep_info(void)
4423 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4425 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
4426 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
4427 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
4428 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
4429 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
4430 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
4431 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
4433 printk(" memory used by lock dependency info: %lu kB\n",
4434 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
4435 sizeof(struct list_head
) * CLASSHASH_SIZE
+
4436 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
4437 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
4438 sizeof(struct list_head
) * CHAINHASH_SIZE
4439 #ifdef CONFIG_PROVE_LOCKING
4440 + sizeof(struct circular_queue
)
4445 printk(" per task-struct memory footprint: %lu bytes\n",
4446 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
4450 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
4451 const void *mem_to
, struct held_lock
*hlock
)
4453 if (!debug_locks_off())
4455 if (debug_locks_silent
)
4459 pr_warn("=========================\n");
4460 pr_warn("WARNING: held lock freed!\n");
4461 print_kernel_ident();
4462 pr_warn("-------------------------\n");
4463 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4464 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
4466 lockdep_print_held_locks(curr
);
4468 pr_warn("\nstack backtrace:\n");
4472 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
4473 const void* lock_from
, unsigned long lock_len
)
4475 return lock_from
+ lock_len
<= mem_from
||
4476 mem_from
+ mem_len
<= lock_from
;
4480 * Called when kernel memory is freed (or unmapped), or if a lock
4481 * is destroyed or reinitialized - this code checks whether there is
4482 * any held lock in the memory range of <from> to <to>:
4484 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
4486 struct task_struct
*curr
= current
;
4487 struct held_lock
*hlock
;
4488 unsigned long flags
;
4491 if (unlikely(!debug_locks
))
4494 local_irq_save(flags
);
4495 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4496 hlock
= curr
->held_locks
+ i
;
4498 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
4499 sizeof(*hlock
->instance
)))
4502 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
4505 local_irq_restore(flags
);
4507 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
4509 static void print_held_locks_bug(void)
4511 if (!debug_locks_off())
4513 if (debug_locks_silent
)
4517 pr_warn("====================================\n");
4518 pr_warn("WARNING: %s/%d still has locks held!\n",
4519 current
->comm
, task_pid_nr(current
));
4520 print_kernel_ident();
4521 pr_warn("------------------------------------\n");
4522 lockdep_print_held_locks(current
);
4523 pr_warn("\nstack backtrace:\n");
4527 void debug_check_no_locks_held(void)
4529 if (unlikely(current
->lockdep_depth
> 0))
4530 print_held_locks_bug();
4532 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
4535 void debug_show_all_locks(void)
4537 struct task_struct
*g
, *p
;
4541 if (unlikely(!debug_locks
)) {
4542 pr_warn("INFO: lockdep is turned off.\n");
4545 pr_warn("\nShowing all locks held in the system:\n");
4548 * Here we try to get the tasklist_lock as hard as possible,
4549 * if not successful after 2 seconds we ignore it (but keep
4550 * trying). This is to enable a debug printout even if a
4551 * tasklist_lock-holding task deadlocks or crashes.
4554 if (!read_trylock(&tasklist_lock
)) {
4556 pr_warn("hm, tasklist_lock locked, retrying... ");
4559 pr_cont(" #%d", 10-count
);
4563 pr_cont(" ignoring it.\n");
4567 pr_cont(" locked it.\n");
4570 do_each_thread(g
, p
) {
4572 * It's not reliable to print a task's held locks
4573 * if it's not sleeping (or if it's not the current
4576 if (p
->state
== TASK_RUNNING
&& p
!= current
)
4578 if (p
->lockdep_depth
)
4579 lockdep_print_held_locks(p
);
4581 if (read_trylock(&tasklist_lock
))
4583 } while_each_thread(g
, p
);
4586 pr_warn("=============================================\n\n");
4589 read_unlock(&tasklist_lock
);
4591 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
4595 * Careful: only use this function if you are sure that
4596 * the task cannot run in parallel!
4598 void debug_show_held_locks(struct task_struct
*task
)
4600 if (unlikely(!debug_locks
)) {
4601 printk("INFO: lockdep is turned off.\n");
4604 lockdep_print_held_locks(task
);
4606 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
4608 asmlinkage __visible
void lockdep_sys_exit(void)
4610 struct task_struct
*curr
= current
;
4612 if (unlikely(curr
->lockdep_depth
)) {
4613 if (!debug_locks_off())
4616 pr_warn("================================================\n");
4617 pr_warn("WARNING: lock held when returning to user space!\n");
4618 print_kernel_ident();
4619 pr_warn("------------------------------------------------\n");
4620 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4621 curr
->comm
, curr
->pid
);
4622 lockdep_print_held_locks(curr
);
4626 * The lock history for each syscall should be independent. So wipe the
4627 * slate clean on return to userspace.
4629 lockdep_invariant_state(false);
4632 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
4634 struct task_struct
*curr
= current
;
4636 /* Note: the following can be executed concurrently, so be careful. */
4638 pr_warn("=============================\n");
4639 pr_warn("WARNING: suspicious RCU usage\n");
4640 print_kernel_ident();
4641 pr_warn("-----------------------------\n");
4642 pr_warn("%s:%d %s!\n", file
, line
, s
);
4643 pr_warn("\nother info that might help us debug this:\n\n");
4644 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4645 !rcu_lockdep_current_cpu_online()
4646 ? "RCU used illegally from offline CPU!\n"
4647 : !rcu_is_watching()
4648 ? "RCU used illegally from idle CPU!\n"
4650 rcu_scheduler_active
, debug_locks
);
4653 * If a CPU is in the RCU-free window in idle (ie: in the section
4654 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4655 * considers that CPU to be in an "extended quiescent state",
4656 * which means that RCU will be completely ignoring that CPU.
4657 * Therefore, rcu_read_lock() and friends have absolutely no
4658 * effect on a CPU running in that state. In other words, even if
4659 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4660 * delete data structures out from under it. RCU really has no
4661 * choice here: we need to keep an RCU-free window in idle where
4662 * the CPU may possibly enter into low power mode. This way we can
4663 * notice an extended quiescent state to other CPUs that started a grace
4664 * period. Otherwise we would delay any grace period as long as we run
4667 * So complain bitterly if someone does call rcu_read_lock(),
4668 * rcu_read_lock_bh() and so on from extended quiescent states.
4670 if (!rcu_is_watching())
4671 pr_warn("RCU used illegally from extended quiescent state!\n");
4673 lockdep_print_held_locks(curr
);
4674 pr_warn("\nstack backtrace:\n");
4677 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);
4679 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
4682 * Crossrelease works by recording a lock history for each thread and
4683 * connecting those historic locks that were taken after the
4684 * wait_for_completion() in the complete() context.
4691 * wait_for_completion(&C);
4692 * lock_acquire_crosslock();
4693 * atomic_inc_return(&cross_gen_id);
4696 * | mutex_unlock(&B);
4699 * `-- lock_commit_crosslock();
4701 * Which will then add a dependency between B and C.
4704 #define xhlock(i) (current->xhlocks[(i) % MAX_XHLOCKS_NR])
4707 * Whenever a crosslock is held, cross_gen_id will be increased.
4709 static atomic_t cross_gen_id
; /* Can be wrapped */
4712 * Make an entry of the ring buffer invalid.
4714 static inline void invalidate_xhlock(struct hist_lock
*xhlock
)
4717 * Normally, xhlock->hlock.instance must be !NULL.
4719 xhlock
->hlock
.instance
= NULL
;
4723 * Lock history stacks; we have 2 nested lock history stacks:
4728 * The thing is that once we complete a HARD/SOFT IRQ the future task locks
4729 * should not depend on any of the locks observed while running the IRQ. So
4730 * what we do is rewind the history buffer and erase all our knowledge of that
4734 void crossrelease_hist_start(enum xhlock_context_t c
)
4736 struct task_struct
*cur
= current
;
4741 cur
->xhlock_idx_hist
[c
] = cur
->xhlock_idx
;
4742 cur
->hist_id_save
[c
] = cur
->hist_id
;
4745 void crossrelease_hist_end(enum xhlock_context_t c
)
4747 struct task_struct
*cur
= current
;
4750 unsigned int idx
= cur
->xhlock_idx_hist
[c
];
4751 struct hist_lock
*h
= &xhlock(idx
);
4753 cur
->xhlock_idx
= idx
;
4755 /* Check if the ring was overwritten. */
4756 if (h
->hist_id
!= cur
->hist_id_save
[c
])
4757 invalidate_xhlock(h
);
4762 * lockdep_invariant_state() is used to annotate independence inside a task, to
4763 * make one task look like multiple independent 'tasks'.
4765 * Take for instance workqueues; each work is independent of the last. The
4766 * completion of a future work does not depend on the completion of a past work
4767 * (in general). Therefore we must not carry that (lock) dependency across
4770 * This is true for many things; pretty much all kthreads fall into this
4771 * pattern, where they have an invariant state and future completions do not
4772 * depend on past completions. Its just that since they all have the 'same'
4773 * form -- the kthread does the same over and over -- it doesn't typically
4776 * The same is true for system-calls, once a system call is completed (we've
4777 * returned to userspace) the next system call does not depend on the lock
4778 * history of the previous system call.
4780 * They key property for independence, this invariant state, is that it must be
4781 * a point where we hold no locks and have no history. Because if we were to
4782 * hold locks, the restore at _end() would not necessarily recover it's history
4783 * entry. Similarly, independence per-definition means it does not depend on
4786 void lockdep_invariant_state(bool force
)
4789 * We call this at an invariant point, no current state, no history.
4790 * Verify the former, enforce the latter.
4792 WARN_ON_ONCE(!force
&& current
->lockdep_depth
);
4793 if (current
->xhlocks
)
4794 invalidate_xhlock(&xhlock(current
->xhlock_idx
));
4797 static int cross_lock(struct lockdep_map
*lock
)
4799 return lock
? lock
->cross
: 0;
4803 * This is needed to decide the relationship between wrapable variables.
4805 static inline int before(unsigned int a
, unsigned int b
)
4807 return (int)(a
- b
) < 0;
4810 static inline struct lock_class
*xhlock_class(struct hist_lock
*xhlock
)
4812 return hlock_class(&xhlock
->hlock
);
4815 static inline struct lock_class
*xlock_class(struct cross_lock
*xlock
)
4817 return hlock_class(&xlock
->hlock
);
4821 * Should we check a dependency with previous one?
4823 static inline int depend_before(struct held_lock
*hlock
)
4825 return hlock
->read
!= 2 && hlock
->check
&& !hlock
->trylock
;
4829 * Should we check a dependency with next one?
4831 static inline int depend_after(struct held_lock
*hlock
)
4833 return hlock
->read
!= 2 && hlock
->check
;
4837 * Check if the xhlock is valid, which would be false if,
4839 * 1. Has not used after initializaion yet.
4840 * 2. Got invalidated.
4842 * Remind hist_lock is implemented as a ring buffer.
4844 static inline int xhlock_valid(struct hist_lock
*xhlock
)
4847 * xhlock->hlock.instance must be !NULL.
4849 return !!xhlock
->hlock
.instance
;
4853 * Record a hist_lock entry.
4855 * Irq disable is only required.
4857 static void add_xhlock(struct held_lock
*hlock
)
4859 unsigned int idx
= ++current
->xhlock_idx
;
4860 struct hist_lock
*xhlock
= &xhlock(idx
);
4862 #ifdef CONFIG_DEBUG_LOCKDEP
4864 * This can be done locklessly because they are all task-local
4865 * state, we must however ensure IRQs are disabled.
4867 WARN_ON_ONCE(!irqs_disabled());
4870 /* Initialize hist_lock's members */
4871 xhlock
->hlock
= *hlock
;
4872 xhlock
->hist_id
= ++current
->hist_id
;
4874 xhlock
->trace
.nr_entries
= 0;
4875 xhlock
->trace
.max_entries
= MAX_XHLOCK_TRACE_ENTRIES
;
4876 xhlock
->trace
.entries
= xhlock
->trace_entries
;
4878 if (crossrelease_fullstack
) {
4879 xhlock
->trace
.skip
= 3;
4880 save_stack_trace(&xhlock
->trace
);
4882 xhlock
->trace
.nr_entries
= 1;
4883 xhlock
->trace
.entries
[0] = hlock
->acquire_ip
;
4887 static inline int same_context_xhlock(struct hist_lock
*xhlock
)
4889 return xhlock
->hlock
.irq_context
== task_irq_context(current
);
4893 * This should be lockless as far as possible because this would be
4894 * called very frequently.
4896 static void check_add_xhlock(struct held_lock
*hlock
)
4899 * Record a hist_lock, only in case that acquisitions ahead
4900 * could depend on the held_lock. For example, if the held_lock
4901 * is trylock then acquisitions ahead never depends on that.
4902 * In that case, we don't need to record it. Just return.
4904 if (!current
->xhlocks
|| !depend_before(hlock
))
4913 static int add_xlock(struct held_lock
*hlock
)
4915 struct cross_lock
*xlock
;
4916 unsigned int gen_id
;
4921 xlock
= &((struct lockdep_map_cross
*)hlock
->instance
)->xlock
;
4924 * When acquisitions for a crosslock are overlapped, we use
4925 * nr_acquire to perform commit for them, based on cross_gen_id
4926 * of the first acquisition, which allows to add additional
4929 * Moreover, when no acquisition of a crosslock is in progress,
4930 * we should not perform commit because the lock might not exist
4931 * any more, which might cause incorrect memory access. So we
4932 * have to track the number of acquisitions of a crosslock.
4934 * depend_after() is necessary to initialize only the first
4935 * valid xlock so that the xlock can be used on its commit.
4937 if (xlock
->nr_acquire
++ && depend_after(&xlock
->hlock
))
4940 gen_id
= (unsigned int)atomic_inc_return(&cross_gen_id
);
4941 xlock
->hlock
= *hlock
;
4942 xlock
->hlock
.gen_id
= gen_id
;
4949 * Called for both normal and crosslock acquires. Normal locks will be
4950 * pushed on the hist_lock queue. Cross locks will record state and
4951 * stop regular lock_acquire() to avoid being placed on the held_lock
4954 * Return: 0 - failure;
4955 * 1 - crosslock, done;
4956 * 2 - normal lock, continue to held_lock[] ops.
4958 static int lock_acquire_crosslock(struct held_lock
*hlock
)
4961 * CONTEXT 1 CONTEXT 2
4962 * --------- ---------
4964 * X = atomic_inc_return(&cross_gen_id)
4965 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4966 * Y = atomic_read_acquire(&cross_gen_id)
4969 * atomic_read_acquire() is for ordering between A and B,
4970 * IOW, A happens before B, when CONTEXT 2 see Y >= X.
4972 * Pairs with atomic_inc_return() in add_xlock().
4974 hlock
->gen_id
= (unsigned int)atomic_read_acquire(&cross_gen_id
);
4976 if (cross_lock(hlock
->instance
))
4977 return add_xlock(hlock
);
4979 check_add_xhlock(hlock
);
4983 static int copy_trace(struct stack_trace
*trace
)
4985 unsigned long *buf
= stack_trace
+ nr_stack_trace_entries
;
4986 unsigned int max_nr
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
4987 unsigned int nr
= min(max_nr
, trace
->nr_entries
);
4989 trace
->nr_entries
= nr
;
4990 memcpy(buf
, trace
->entries
, nr
* sizeof(trace
->entries
[0]));
4991 trace
->entries
= buf
;
4992 nr_stack_trace_entries
+= nr
;
4994 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
4995 if (!debug_locks_off_graph_unlock())
4998 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
5007 static int commit_xhlock(struct cross_lock
*xlock
, struct hist_lock
*xhlock
)
5009 unsigned int xid
, pid
;
5012 xid
= xlock_class(xlock
) - lock_classes
;
5013 chain_key
= iterate_chain_key((u64
)0, xid
);
5014 pid
= xhlock_class(xhlock
) - lock_classes
;
5015 chain_key
= iterate_chain_key(chain_key
, pid
);
5017 if (lookup_chain_cache(chain_key
))
5020 if (!add_chain_cache_classes(xid
, pid
, xhlock
->hlock
.irq_context
,
5024 if (!check_prev_add(current
, &xlock
->hlock
, &xhlock
->hlock
, 1,
5025 &xhlock
->trace
, copy_trace
))
5031 static void commit_xhlocks(struct cross_lock
*xlock
)
5033 unsigned int cur
= current
->xhlock_idx
;
5034 unsigned int prev_hist_id
= xhlock(cur
).hist_id
;
5040 if (xlock
->nr_acquire
) {
5041 for (i
= 0; i
< MAX_XHLOCKS_NR
; i
++) {
5042 struct hist_lock
*xhlock
= &xhlock(cur
- i
);
5044 if (!xhlock_valid(xhlock
))
5047 if (before(xhlock
->hlock
.gen_id
, xlock
->hlock
.gen_id
))
5050 if (!same_context_xhlock(xhlock
))
5054 * Filter out the cases where the ring buffer was
5055 * overwritten and the current entry has a bigger
5056 * hist_id than the previous one, which is impossible
5059 if (unlikely(before(prev_hist_id
, xhlock
->hist_id
)))
5062 prev_hist_id
= xhlock
->hist_id
;
5065 * commit_xhlock() returns 0 with graph_lock already
5068 if (!commit_xhlock(xlock
, xhlock
))
5076 void lock_commit_crosslock(struct lockdep_map
*lock
)
5078 struct cross_lock
*xlock
;
5079 unsigned long flags
;
5081 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
5084 if (!current
->xhlocks
)
5088 * Do commit hist_locks with the cross_lock, only in case that
5089 * the cross_lock could depend on acquisitions after that.
5091 * For example, if the cross_lock does not have the 'check' flag
5092 * then we don't need to check dependencies and commit for that.
5093 * Just skip it. In that case, of course, the cross_lock does
5094 * not depend on acquisitions ahead, either.
5096 * WARNING: Don't do that in add_xlock() in advance. When an
5097 * acquisition context is different from the commit context,
5098 * invalid(skipped) cross_lock might be accessed.
5100 if (!depend_after(&((struct lockdep_map_cross
*)lock
)->xlock
.hlock
))
5103 raw_local_irq_save(flags
);
5105 current
->lockdep_recursion
= 1;
5106 xlock
= &((struct lockdep_map_cross
*)lock
)->xlock
;
5107 commit_xhlocks(xlock
);
5108 current
->lockdep_recursion
= 0;
5109 raw_local_irq_restore(flags
);
5111 EXPORT_SYMBOL_GPL(lock_commit_crosslock
);
5114 * Return: 0 - failure;
5115 * 1 - crosslock, done;
5116 * 2 - normal lock, continue to held_lock[] ops.
5118 static int lock_release_crosslock(struct lockdep_map
*lock
)
5120 if (cross_lock(lock
)) {
5123 ((struct lockdep_map_cross
*)lock
)->xlock
.nr_acquire
--;
5130 static void cross_init(struct lockdep_map
*lock
, int cross
)
5133 ((struct lockdep_map_cross
*)lock
)->xlock
.nr_acquire
= 0;
5135 lock
->cross
= cross
;
5138 * Crossrelease assumes that the ring buffer size of xhlocks
5139 * is aligned with power of 2. So force it on build.
5141 BUILD_BUG_ON(MAX_XHLOCKS_NR
& (MAX_XHLOCKS_NR
- 1));
5144 void lockdep_init_task(struct task_struct
*task
)
5148 task
->xhlock_idx
= UINT_MAX
;
5151 for (i
= 0; i
< XHLOCK_CTX_NR
; i
++) {
5152 task
->xhlock_idx_hist
[i
] = UINT_MAX
;
5153 task
->hist_id_save
[i
] = 0;
5156 task
->xhlocks
= kzalloc(sizeof(struct hist_lock
) * MAX_XHLOCKS_NR
,
5160 void lockdep_free_task(struct task_struct
*task
)
5162 if (task
->xhlocks
) {
5163 void *tmp
= task
->xhlocks
;
5164 /* Diable crossrelease for current */
5165 task
->xhlocks
= NULL
;