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>
52 #include <linux/nmi.h>
54 #include <asm/sections.h>
56 #include "lockdep_internals.h"
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/lock.h>
61 #ifdef CONFIG_PROVE_LOCKING
62 int prove_locking
= 1;
63 module_param(prove_locking
, int, 0644);
65 #define prove_locking 0
68 #ifdef CONFIG_LOCK_STAT
70 module_param(lock_stat
, int, 0644);
76 * lockdep_lock: protects the lockdep graph, the hashes and the
77 * class/list/hash allocators.
79 * This is one of the rare exceptions where it's justified
80 * to use a raw spinlock - we really dont want the spinlock
81 * code to recurse back into the lockdep code...
83 static arch_spinlock_t lockdep_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
84 static struct task_struct
*lockdep_selftest_task_struct
;
86 static int graph_lock(void)
88 arch_spin_lock(&lockdep_lock
);
90 * Make sure that if another CPU detected a bug while
91 * walking the graph we dont change it (while the other
92 * CPU is busy printing out stuff with the graph lock
96 arch_spin_unlock(&lockdep_lock
);
99 /* prevent any recursions within lockdep from causing deadlocks */
100 current
->lockdep_recursion
++;
104 static inline int graph_unlock(void)
106 if (debug_locks
&& !arch_spin_is_locked(&lockdep_lock
)) {
108 * The lockdep graph lock isn't locked while we expect it to
109 * be, we're confused now, bye!
111 return DEBUG_LOCKS_WARN_ON(1);
114 current
->lockdep_recursion
--;
115 arch_spin_unlock(&lockdep_lock
);
120 * Turn lock debugging off and return with 0 if it was off already,
121 * and also release the graph lock:
123 static inline int debug_locks_off_graph_unlock(void)
125 int ret
= debug_locks_off();
127 arch_spin_unlock(&lockdep_lock
);
132 unsigned long nr_list_entries
;
133 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
136 * All data structures here are protected by the global debug_lock.
138 * Mutex key structs only get allocated, once during bootup, and never
139 * get freed - this significantly simplifies the debugging code.
141 unsigned long nr_lock_classes
;
142 #ifndef CONFIG_DEBUG_LOCKDEP
145 struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
147 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
149 if (!hlock
->class_idx
) {
151 * Someone passed in garbage, we give up.
153 DEBUG_LOCKS_WARN_ON(1);
156 return lock_classes
+ hlock
->class_idx
- 1;
159 #ifdef CONFIG_LOCK_STAT
160 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], cpu_lock_stats
);
162 static inline u64
lockstat_clock(void)
164 return local_clock();
167 static int lock_point(unsigned long points
[], unsigned long ip
)
171 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
172 if (points
[i
] == 0) {
183 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
188 if (time
< lt
->min
|| !lt
->nr
)
195 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
200 if (src
->max
> dst
->max
)
203 if (src
->min
< dst
->min
|| !dst
->nr
)
206 dst
->total
+= src
->total
;
210 struct lock_class_stats
lock_stats(struct lock_class
*class)
212 struct lock_class_stats stats
;
215 memset(&stats
, 0, sizeof(struct lock_class_stats
));
216 for_each_possible_cpu(cpu
) {
217 struct lock_class_stats
*pcs
=
218 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
220 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
221 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
223 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
224 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
226 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
227 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
229 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
230 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
232 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
233 stats
.bounces
[i
] += pcs
->bounces
[i
];
239 void clear_lock_stats(struct lock_class
*class)
243 for_each_possible_cpu(cpu
) {
244 struct lock_class_stats
*cpu_stats
=
245 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
247 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
249 memset(class->contention_point
, 0, sizeof(class->contention_point
));
250 memset(class->contending_point
, 0, sizeof(class->contending_point
));
253 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
255 return &this_cpu_ptr(cpu_lock_stats
)[class - lock_classes
];
258 static void lock_release_holdtime(struct held_lock
*hlock
)
260 struct lock_class_stats
*stats
;
266 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
268 stats
= get_lock_stats(hlock_class(hlock
));
270 lock_time_inc(&stats
->read_holdtime
, holdtime
);
272 lock_time_inc(&stats
->write_holdtime
, holdtime
);
275 static inline void lock_release_holdtime(struct held_lock
*hlock
)
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
285 LIST_HEAD(all_lock_classes
);
288 * The lockdep classes are in a hash-table as well, for fast lookup:
290 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
292 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
293 #define classhashentry(key) (classhash_table + __classhashfn((key)))
295 static struct hlist_head classhash_table
[CLASSHASH_SIZE
];
298 * We put the lock dependency chains into a hash-table as well, to cache
301 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
303 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
304 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
306 static struct hlist_head chainhash_table
[CHAINHASH_SIZE
];
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
314 static inline u64
iterate_chain_key(u64 key
, u32 idx
)
316 u32 k0
= key
, k1
= key
>> 32;
318 __jhash_mix(idx
, k0
, k1
); /* Macro that modifies arguments! */
320 return k0
| (u64
)k1
<< 32;
323 void lockdep_off(void)
325 current
->lockdep_recursion
++;
327 EXPORT_SYMBOL(lockdep_off
);
329 void lockdep_on(void)
331 current
->lockdep_recursion
--;
333 EXPORT_SYMBOL(lockdep_on
);
335 void lockdep_set_selftest_task(struct task_struct
*task
)
337 lockdep_selftest_task_struct
= task
;
341 * Debugging switches:
345 #define VERY_VERBOSE 0
348 # define HARDIRQ_VERBOSE 1
349 # define SOFTIRQ_VERBOSE 1
351 # define HARDIRQ_VERBOSE 0
352 # define SOFTIRQ_VERBOSE 0
355 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
357 * Quick filtering for interesting events:
359 static int class_filter(struct lock_class
*class)
363 if (class->name_version
== 1 &&
364 !strcmp(class->name
, "lockname"))
366 if (class->name_version
== 1 &&
367 !strcmp(class->name
, "&struct->lockfield"))
370 /* Filter everything else. 1 would be to allow everything else */
375 static int verbose(struct lock_class
*class)
378 return class_filter(class);
384 * Stack-trace: tightly packed array of stack backtrace
385 * addresses. Protected by the graph_lock.
387 unsigned long nr_stack_trace_entries
;
388 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
390 static void print_lockdep_off(const char *bug_msg
)
392 printk(KERN_DEBUG
"%s\n", bug_msg
);
393 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
394 #ifdef CONFIG_LOCK_STAT
395 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
399 static int save_trace(struct stack_trace
*trace
)
401 trace
->nr_entries
= 0;
402 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
403 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
407 save_stack_trace(trace
);
410 * Some daft arches put -1 at the end to indicate its a full trace.
412 * <rant> this is buggy anyway, since it takes a whole extra entry so a
413 * complete trace that maxes out the entries provided will be reported
414 * as incomplete, friggin useless </rant>
416 if (trace
->nr_entries
!= 0 &&
417 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
420 trace
->max_entries
= trace
->nr_entries
;
422 nr_stack_trace_entries
+= trace
->nr_entries
;
424 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
425 if (!debug_locks_off_graph_unlock())
428 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
437 unsigned int nr_hardirq_chains
;
438 unsigned int nr_softirq_chains
;
439 unsigned int nr_process_chains
;
440 unsigned int max_lockdep_depth
;
442 #ifdef CONFIG_DEBUG_LOCKDEP
444 * Various lockdep statistics:
446 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
453 #define __USAGE(__STATE) \
454 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
455 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
456 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
457 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
459 static const char *usage_str
[] =
461 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
462 #include "lockdep_states.h"
464 [LOCK_USED
] = "INITIAL USE",
467 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
469 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
472 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
477 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
481 if (class->usage_mask
& lock_flag(bit
+ 2))
483 if (class->usage_mask
& lock_flag(bit
)) {
485 if (class->usage_mask
& lock_flag(bit
+ 2))
492 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
496 #define LOCKDEP_STATE(__STATE) \
497 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
498 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
499 #include "lockdep_states.h"
505 static void __print_lock_name(struct lock_class
*class)
507 char str
[KSYM_NAME_LEN
];
512 name
= __get_key_name(class->key
, str
);
513 printk(KERN_CONT
"%s", name
);
515 printk(KERN_CONT
"%s", name
);
516 if (class->name_version
> 1)
517 printk(KERN_CONT
"#%d", class->name_version
);
519 printk(KERN_CONT
"/%d", class->subclass
);
523 static void print_lock_name(struct lock_class
*class)
525 char usage
[LOCK_USAGE_CHARS
];
527 get_usage_chars(class, usage
);
529 printk(KERN_CONT
" (");
530 __print_lock_name(class);
531 printk(KERN_CONT
"){%s}", usage
);
534 static void print_lockdep_cache(struct lockdep_map
*lock
)
537 char str
[KSYM_NAME_LEN
];
541 name
= __get_key_name(lock
->key
->subkeys
, str
);
543 printk(KERN_CONT
"%s", name
);
546 static void print_lock(struct held_lock
*hlock
)
549 * We can be called locklessly through debug_show_all_locks() so be
550 * extra careful, the hlock might have been released and cleared.
552 unsigned int class_idx
= hlock
->class_idx
;
554 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
557 if (!class_idx
|| (class_idx
- 1) >= MAX_LOCKDEP_KEYS
) {
558 printk(KERN_CONT
"<RELEASED>\n");
562 printk(KERN_CONT
"%p", hlock
->instance
);
563 print_lock_name(lock_classes
+ class_idx
- 1);
564 printk(KERN_CONT
", at: %pS\n", (void *)hlock
->acquire_ip
);
567 static void lockdep_print_held_locks(struct task_struct
*p
)
569 int i
, depth
= READ_ONCE(p
->lockdep_depth
);
572 printk("no locks held by %s/%d.\n", p
->comm
, task_pid_nr(p
));
574 printk("%d lock%s held by %s/%d:\n", depth
,
575 depth
> 1 ? "s" : "", p
->comm
, task_pid_nr(p
));
577 * It's not reliable to print a task's held locks if it's not sleeping
578 * and it's not the current task.
580 if (p
->state
== TASK_RUNNING
&& p
!= current
)
582 for (i
= 0; i
< depth
; i
++) {
584 print_lock(p
->held_locks
+ i
);
588 static void print_kernel_ident(void)
590 printk("%s %.*s %s\n", init_utsname()->release
,
591 (int)strcspn(init_utsname()->version
, " "),
592 init_utsname()->version
,
596 static int very_verbose(struct lock_class
*class)
599 return class_filter(class);
605 * Is this the address of a static object:
608 static int static_obj(void *obj
)
610 unsigned long start
= (unsigned long) &_stext
,
611 end
= (unsigned long) &_end
,
612 addr
= (unsigned long) obj
;
617 if ((addr
>= start
) && (addr
< end
))
620 if (arch_is_kernel_data(addr
))
624 * in-kernel percpu var?
626 if (is_kernel_percpu_address(addr
))
630 * module static or percpu var?
632 return is_module_address(addr
) || is_module_percpu_address(addr
);
637 * To make lock name printouts unique, we calculate a unique
638 * class->name_version generation counter. The caller must hold the graph
641 static int count_matching_names(struct lock_class
*new_class
)
643 struct lock_class
*class;
646 if (!new_class
->name
)
649 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
650 if (new_class
->key
- new_class
->subclass
== class->key
)
651 return class->name_version
;
652 if (class->name
&& !strcmp(class->name
, new_class
->name
))
653 count
= max(count
, class->name_version
);
659 static inline struct lock_class
*
660 look_up_lock_class(const struct lockdep_map
*lock
, unsigned int subclass
)
662 struct lockdep_subclass_key
*key
;
663 struct hlist_head
*hash_head
;
664 struct lock_class
*class;
666 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
669 "BUG: looking up invalid subclass: %u\n", subclass
);
671 "turning off the locking correctness validator.\n");
677 * If it is not initialised then it has never been locked,
678 * so it won't be present in the hash table.
680 if (unlikely(!lock
->key
))
684 * NOTE: the class-key must be unique. For dynamic locks, a static
685 * lock_class_key variable is passed in through the mutex_init()
686 * (or spin_lock_init()) call - which acts as the key. For static
687 * locks we use the lock object itself as the key.
689 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
690 sizeof(struct lockdep_map
));
692 key
= lock
->key
->subkeys
+ subclass
;
694 hash_head
= classhashentry(key
);
697 * We do an RCU walk of the hash, see lockdep_free_key_range().
699 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
702 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
703 if (class->key
== key
) {
705 * Huh! same key, different name? Did someone trample
706 * on some memory? We're most confused.
708 WARN_ON_ONCE(class->name
!= lock
->name
);
717 * Static locks do not have their class-keys yet - for them the key is
718 * the lock object itself. If the lock is in the per cpu area, the
719 * canonical address of the lock (per cpu offset removed) is used.
721 static bool assign_lock_key(struct lockdep_map
*lock
)
723 unsigned long can_addr
, addr
= (unsigned long)lock
;
725 if (__is_kernel_percpu_address(addr
, &can_addr
))
726 lock
->key
= (void *)can_addr
;
727 else if (__is_module_percpu_address(addr
, &can_addr
))
728 lock
->key
= (void *)can_addr
;
729 else if (static_obj(lock
))
730 lock
->key
= (void *)lock
;
732 /* Debug-check: all keys must be persistent! */
734 pr_err("INFO: trying to register non-static key.\n");
735 pr_err("the code is fine but needs lockdep annotation.\n");
736 pr_err("turning off the locking correctness validator.\n");
745 * Initialize the lock_classes[] array elements.
747 static void init_data_structures_once(void)
749 static bool initialization_happened
;
752 if (likely(initialization_happened
))
755 initialization_happened
= true;
757 for (i
= 0; i
< ARRAY_SIZE(lock_classes
); i
++) {
758 INIT_LIST_HEAD(&lock_classes
[i
].locks_after
);
759 INIT_LIST_HEAD(&lock_classes
[i
].locks_before
);
764 * Register a lock's class in the hash-table, if the class is not present
765 * yet. Otherwise we look it up. We cache the result in the lock object
766 * itself, so actual lookup of the hash should be once per lock object.
768 static struct lock_class
*
769 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
771 struct lockdep_subclass_key
*key
;
772 struct hlist_head
*hash_head
;
773 struct lock_class
*class;
775 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
777 class = look_up_lock_class(lock
, subclass
);
779 goto out_set_class_cache
;
782 if (!assign_lock_key(lock
))
784 } else if (!static_obj(lock
->key
)) {
788 key
= lock
->key
->subkeys
+ subclass
;
789 hash_head
= classhashentry(key
);
795 * We have to do the hash-walk again, to avoid races
798 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
799 if (class->key
== key
)
803 init_data_structures_once();
806 * Allocate a new key from the static array, and add it to
809 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
810 if (!debug_locks_off_graph_unlock()) {
814 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
818 class = lock_classes
+ nr_lock_classes
++;
819 debug_atomic_inc(nr_unused_locks
);
821 class->name
= lock
->name
;
822 class->subclass
= subclass
;
823 WARN_ON_ONCE(!list_empty(&class->locks_before
));
824 WARN_ON_ONCE(!list_empty(&class->locks_after
));
825 class->name_version
= count_matching_names(class);
827 * We use RCU's safe list-add method to make
828 * parallel walking of the hash-list safe:
830 hlist_add_head_rcu(&class->hash_entry
, hash_head
);
832 * Add it to the global list of classes:
834 list_add_tail(&class->lock_entry
, &all_lock_classes
);
836 if (verbose(class)) {
839 printk("\nnew class %px: %s", class->key
, class->name
);
840 if (class->name_version
> 1)
841 printk(KERN_CONT
"#%d", class->name_version
);
842 printk(KERN_CONT
"\n");
853 if (!subclass
|| force
)
854 lock
->class_cache
[0] = class;
855 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
856 lock
->class_cache
[subclass
] = class;
859 * Hash collision, did we smoke some? We found a class with a matching
860 * hash but the subclass -- which is hashed in -- didn't match.
862 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
868 #ifdef CONFIG_PROVE_LOCKING
870 * Allocate a lockdep entry. (assumes the graph_lock held, returns
871 * with NULL on failure)
873 static struct lock_list
*alloc_list_entry(void)
875 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
876 if (!debug_locks_off_graph_unlock())
879 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
883 return list_entries
+ nr_list_entries
++;
887 * Add a new dependency to the head of the list:
889 static int add_lock_to_list(struct lock_class
*this,
890 struct lock_class
*links_to
, struct list_head
*head
,
891 unsigned long ip
, int distance
,
892 struct stack_trace
*trace
)
894 struct lock_list
*entry
;
896 * Lock not present yet - get a new dependency struct and
897 * add it to the list:
899 entry
= alloc_list_entry();
904 entry
->links_to
= links_to
;
905 entry
->distance
= distance
;
906 entry
->trace
= *trace
;
908 * Both allocation and removal are done under the graph lock; but
909 * iteration is under RCU-sched; see look_up_lock_class() and
910 * lockdep_free_key_range().
912 list_add_tail_rcu(&entry
->entry
, head
);
918 * For good efficiency of modular, we use power of 2
920 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
921 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
924 * The circular_queue and helpers is used to implement the
925 * breadth-first search(BFS)algorithem, by which we can build
926 * the shortest path from the next lock to be acquired to the
927 * previous held lock if there is a circular between them.
929 struct circular_queue
{
930 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
931 unsigned int front
, rear
;
934 static struct circular_queue lock_cq
;
936 unsigned int max_bfs_queue_depth
;
938 static unsigned int lockdep_dependency_gen_id
;
940 static inline void __cq_init(struct circular_queue
*cq
)
942 cq
->front
= cq
->rear
= 0;
943 lockdep_dependency_gen_id
++;
946 static inline int __cq_empty(struct circular_queue
*cq
)
948 return (cq
->front
== cq
->rear
);
951 static inline int __cq_full(struct circular_queue
*cq
)
953 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
956 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
961 cq
->element
[cq
->rear
] = elem
;
962 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
966 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
971 *elem
= cq
->element
[cq
->front
];
972 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
976 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
978 return (cq
->rear
- cq
->front
) & CQ_MASK
;
981 static inline void mark_lock_accessed(struct lock_list
*lock
,
982 struct lock_list
*parent
)
986 nr
= lock
- list_entries
;
987 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
988 lock
->parent
= parent
;
989 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
992 static inline unsigned long lock_accessed(struct lock_list
*lock
)
996 nr
= lock
- list_entries
;
997 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
998 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
1001 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
1003 return child
->parent
;
1006 static inline int get_lock_depth(struct lock_list
*child
)
1009 struct lock_list
*parent
;
1011 while ((parent
= get_lock_parent(child
))) {
1018 static int __bfs(struct lock_list
*source_entry
,
1020 int (*match
)(struct lock_list
*entry
, void *data
),
1021 struct lock_list
**target_entry
,
1024 struct lock_list
*entry
;
1025 struct list_head
*head
;
1026 struct circular_queue
*cq
= &lock_cq
;
1029 if (match(source_entry
, data
)) {
1030 *target_entry
= source_entry
;
1036 head
= &source_entry
->class->locks_after
;
1038 head
= &source_entry
->class->locks_before
;
1040 if (list_empty(head
))
1044 __cq_enqueue(cq
, (unsigned long)source_entry
);
1046 while (!__cq_empty(cq
)) {
1047 struct lock_list
*lock
;
1049 __cq_dequeue(cq
, (unsigned long *)&lock
);
1057 head
= &lock
->class->locks_after
;
1059 head
= &lock
->class->locks_before
;
1061 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1063 list_for_each_entry_rcu(entry
, head
, entry
) {
1064 if (!lock_accessed(entry
)) {
1065 unsigned int cq_depth
;
1066 mark_lock_accessed(entry
, lock
);
1067 if (match(entry
, data
)) {
1068 *target_entry
= entry
;
1073 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1077 cq_depth
= __cq_get_elem_count(cq
);
1078 if (max_bfs_queue_depth
< cq_depth
)
1079 max_bfs_queue_depth
= cq_depth
;
1087 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1089 int (*match
)(struct lock_list
*entry
, void *data
),
1090 struct lock_list
**target_entry
)
1092 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1096 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1098 int (*match
)(struct lock_list
*entry
, void *data
),
1099 struct lock_list
**target_entry
)
1101 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1106 * Recursive, forwards-direction lock-dependency checking, used for
1107 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1112 * Print a dependency chain entry (this is only done when a deadlock
1113 * has been detected):
1116 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1118 if (debug_locks_silent
)
1120 printk("\n-> #%u", depth
);
1121 print_lock_name(target
->class);
1122 printk(KERN_CONT
":\n");
1123 print_stack_trace(&target
->trace
, 6);
1129 print_circular_lock_scenario(struct held_lock
*src
,
1130 struct held_lock
*tgt
,
1131 struct lock_list
*prt
)
1133 struct lock_class
*source
= hlock_class(src
);
1134 struct lock_class
*target
= hlock_class(tgt
);
1135 struct lock_class
*parent
= prt
->class;
1138 * A direct locking problem where unsafe_class lock is taken
1139 * directly by safe_class lock, then all we need to show
1140 * is the deadlock scenario, as it is obvious that the
1141 * unsafe lock is taken under the safe lock.
1143 * But if there is a chain instead, where the safe lock takes
1144 * an intermediate lock (middle_class) where this lock is
1145 * not the same as the safe lock, then the lock chain is
1146 * used to describe the problem. Otherwise we would need
1147 * to show a different CPU case for each link in the chain
1148 * from the safe_class lock to the unsafe_class lock.
1150 if (parent
!= source
) {
1151 printk("Chain exists of:\n ");
1152 __print_lock_name(source
);
1153 printk(KERN_CONT
" --> ");
1154 __print_lock_name(parent
);
1155 printk(KERN_CONT
" --> ");
1156 __print_lock_name(target
);
1157 printk(KERN_CONT
"\n\n");
1160 printk(" Possible unsafe locking scenario:\n\n");
1161 printk(" CPU0 CPU1\n");
1162 printk(" ---- ----\n");
1164 __print_lock_name(target
);
1165 printk(KERN_CONT
");\n");
1167 __print_lock_name(parent
);
1168 printk(KERN_CONT
");\n");
1170 __print_lock_name(target
);
1171 printk(KERN_CONT
");\n");
1173 __print_lock_name(source
);
1174 printk(KERN_CONT
");\n");
1175 printk("\n *** DEADLOCK ***\n\n");
1179 * When a circular dependency is detected, print the
1183 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1184 struct held_lock
*check_src
,
1185 struct held_lock
*check_tgt
)
1187 struct task_struct
*curr
= current
;
1189 if (debug_locks_silent
)
1193 pr_warn("======================================================\n");
1194 pr_warn("WARNING: possible circular locking dependency detected\n");
1195 print_kernel_ident();
1196 pr_warn("------------------------------------------------------\n");
1197 pr_warn("%s/%d is trying to acquire lock:\n",
1198 curr
->comm
, task_pid_nr(curr
));
1199 print_lock(check_src
);
1201 pr_warn("\nbut task is already holding lock:\n");
1203 print_lock(check_tgt
);
1204 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1205 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1207 print_circular_bug_entry(entry
, depth
);
1212 static inline int class_equal(struct lock_list
*entry
, void *data
)
1214 return entry
->class == data
;
1217 static noinline
int print_circular_bug(struct lock_list
*this,
1218 struct lock_list
*target
,
1219 struct held_lock
*check_src
,
1220 struct held_lock
*check_tgt
,
1221 struct stack_trace
*trace
)
1223 struct task_struct
*curr
= current
;
1224 struct lock_list
*parent
;
1225 struct lock_list
*first_parent
;
1228 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1231 if (!save_trace(&this->trace
))
1234 depth
= get_lock_depth(target
);
1236 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1238 parent
= get_lock_parent(target
);
1239 first_parent
= parent
;
1242 print_circular_bug_entry(parent
, --depth
);
1243 parent
= get_lock_parent(parent
);
1246 printk("\nother info that might help us debug this:\n\n");
1247 print_circular_lock_scenario(check_src
, check_tgt
,
1250 lockdep_print_held_locks(curr
);
1252 printk("\nstack backtrace:\n");
1258 static noinline
int print_bfs_bug(int ret
)
1260 if (!debug_locks_off_graph_unlock())
1264 * Breadth-first-search failed, graph got corrupted?
1266 WARN(1, "lockdep bfs error:%d\n", ret
);
1271 static int noop_count(struct lock_list
*entry
, void *data
)
1273 (*(unsigned long *)data
)++;
1277 static unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1279 unsigned long count
= 0;
1280 struct lock_list
*uninitialized_var(target_entry
);
1282 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1286 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1288 unsigned long ret
, flags
;
1289 struct lock_list
this;
1294 raw_local_irq_save(flags
);
1295 arch_spin_lock(&lockdep_lock
);
1296 ret
= __lockdep_count_forward_deps(&this);
1297 arch_spin_unlock(&lockdep_lock
);
1298 raw_local_irq_restore(flags
);
1303 static unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1305 unsigned long count
= 0;
1306 struct lock_list
*uninitialized_var(target_entry
);
1308 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1313 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1315 unsigned long ret
, flags
;
1316 struct lock_list
this;
1321 raw_local_irq_save(flags
);
1322 arch_spin_lock(&lockdep_lock
);
1323 ret
= __lockdep_count_backward_deps(&this);
1324 arch_spin_unlock(&lockdep_lock
);
1325 raw_local_irq_restore(flags
);
1331 * Prove that the dependency graph starting at <entry> can not
1332 * lead to <target>. Print an error and return 0 if it does.
1335 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1336 struct lock_list
**target_entry
)
1340 debug_atomic_inc(nr_cyclic_checks
);
1342 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1348 check_redundant(struct lock_list
*root
, struct lock_class
*target
,
1349 struct lock_list
**target_entry
)
1353 debug_atomic_inc(nr_redundant_checks
);
1355 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1360 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1362 * Forwards and backwards subgraph searching, for the purposes of
1363 * proving that two subgraphs can be connected by a new dependency
1364 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1367 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1369 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1375 * Find a node in the forwards-direction dependency sub-graph starting
1376 * at @root->class that matches @bit.
1378 * Return 0 if such a node exists in the subgraph, and put that node
1379 * into *@target_entry.
1381 * Return 1 otherwise and keep *@target_entry unchanged.
1382 * Return <0 on error.
1385 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1386 struct lock_list
**target_entry
)
1390 debug_atomic_inc(nr_find_usage_forwards_checks
);
1392 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1398 * Find a node in the backwards-direction dependency sub-graph starting
1399 * at @root->class that matches @bit.
1401 * Return 0 if such a node exists in the subgraph, and put that node
1402 * into *@target_entry.
1404 * Return 1 otherwise and keep *@target_entry unchanged.
1405 * Return <0 on error.
1408 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1409 struct lock_list
**target_entry
)
1413 debug_atomic_inc(nr_find_usage_backwards_checks
);
1415 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1420 static void print_lock_class_header(struct lock_class
*class, int depth
)
1424 printk("%*s->", depth
, "");
1425 print_lock_name(class);
1426 #ifdef CONFIG_DEBUG_LOCKDEP
1427 printk(KERN_CONT
" ops: %lu", debug_class_ops_read(class));
1429 printk(KERN_CONT
" {\n");
1431 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1432 if (class->usage_mask
& (1 << bit
)) {
1435 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1436 len
+= printk(KERN_CONT
" at:\n");
1437 print_stack_trace(class->usage_traces
+ bit
, len
);
1440 printk("%*s }\n", depth
, "");
1442 printk("%*s ... key at: [<%px>] %pS\n",
1443 depth
, "", class->key
, class->key
);
1447 * printk the shortest lock dependencies from @start to @end in reverse order:
1450 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1451 struct lock_list
*root
)
1453 struct lock_list
*entry
= leaf
;
1456 /*compute depth from generated tree by BFS*/
1457 depth
= get_lock_depth(leaf
);
1460 print_lock_class_header(entry
->class, depth
);
1461 printk("%*s ... acquired at:\n", depth
, "");
1462 print_stack_trace(&entry
->trace
, 2);
1465 if (depth
== 0 && (entry
!= root
)) {
1466 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1470 entry
= get_lock_parent(entry
);
1472 } while (entry
&& (depth
>= 0));
1478 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1479 struct lock_list
*unsafe_entry
,
1480 struct lock_class
*prev_class
,
1481 struct lock_class
*next_class
)
1483 struct lock_class
*safe_class
= safe_entry
->class;
1484 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1485 struct lock_class
*middle_class
= prev_class
;
1487 if (middle_class
== safe_class
)
1488 middle_class
= next_class
;
1491 * A direct locking problem where unsafe_class lock is taken
1492 * directly by safe_class lock, then all we need to show
1493 * is the deadlock scenario, as it is obvious that the
1494 * unsafe lock is taken under the safe lock.
1496 * But if there is a chain instead, where the safe lock takes
1497 * an intermediate lock (middle_class) where this lock is
1498 * not the same as the safe lock, then the lock chain is
1499 * used to describe the problem. Otherwise we would need
1500 * to show a different CPU case for each link in the chain
1501 * from the safe_class lock to the unsafe_class lock.
1503 if (middle_class
!= unsafe_class
) {
1504 printk("Chain exists of:\n ");
1505 __print_lock_name(safe_class
);
1506 printk(KERN_CONT
" --> ");
1507 __print_lock_name(middle_class
);
1508 printk(KERN_CONT
" --> ");
1509 __print_lock_name(unsafe_class
);
1510 printk(KERN_CONT
"\n\n");
1513 printk(" Possible interrupt unsafe locking scenario:\n\n");
1514 printk(" CPU0 CPU1\n");
1515 printk(" ---- ----\n");
1517 __print_lock_name(unsafe_class
);
1518 printk(KERN_CONT
");\n");
1519 printk(" local_irq_disable();\n");
1521 __print_lock_name(safe_class
);
1522 printk(KERN_CONT
");\n");
1524 __print_lock_name(middle_class
);
1525 printk(KERN_CONT
");\n");
1526 printk(" <Interrupt>\n");
1528 __print_lock_name(safe_class
);
1529 printk(KERN_CONT
");\n");
1530 printk("\n *** DEADLOCK ***\n\n");
1534 print_bad_irq_dependency(struct task_struct
*curr
,
1535 struct lock_list
*prev_root
,
1536 struct lock_list
*next_root
,
1537 struct lock_list
*backwards_entry
,
1538 struct lock_list
*forwards_entry
,
1539 struct held_lock
*prev
,
1540 struct held_lock
*next
,
1541 enum lock_usage_bit bit1
,
1542 enum lock_usage_bit bit2
,
1543 const char *irqclass
)
1545 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1549 pr_warn("=====================================================\n");
1550 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1551 irqclass
, irqclass
);
1552 print_kernel_ident();
1553 pr_warn("-----------------------------------------------------\n");
1554 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1555 curr
->comm
, task_pid_nr(curr
),
1556 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1557 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1558 curr
->hardirqs_enabled
,
1559 curr
->softirqs_enabled
);
1562 pr_warn("\nand this task is already holding:\n");
1564 pr_warn("which would create a new lock dependency:\n");
1565 print_lock_name(hlock_class(prev
));
1567 print_lock_name(hlock_class(next
));
1570 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1572 print_lock_name(backwards_entry
->class);
1573 pr_warn("\n... which became %s-irq-safe at:\n", irqclass
);
1575 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1577 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass
);
1578 print_lock_name(forwards_entry
->class);
1579 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass
);
1582 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1584 pr_warn("\nother info that might help us debug this:\n\n");
1585 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1586 hlock_class(prev
), hlock_class(next
));
1588 lockdep_print_held_locks(curr
);
1590 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass
);
1591 if (!save_trace(&prev_root
->trace
))
1593 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1595 pr_warn("\nthe dependencies between the lock to be acquired");
1596 pr_warn(" and %s-irq-unsafe lock:\n", irqclass
);
1597 if (!save_trace(&next_root
->trace
))
1599 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1601 pr_warn("\nstack backtrace:\n");
1608 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1609 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1610 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1613 struct lock_list
this, that
;
1614 struct lock_list
*uninitialized_var(target_entry
);
1615 struct lock_list
*uninitialized_var(target_entry1
);
1619 this.class = hlock_class(prev
);
1620 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1622 return print_bfs_bug(ret
);
1627 that
.class = hlock_class(next
);
1628 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1630 return print_bfs_bug(ret
);
1634 return print_bad_irq_dependency(curr
, &this, &that
,
1635 target_entry
, target_entry1
,
1637 bit_backwards
, bit_forwards
, irqclass
);
1640 static const char *state_names
[] = {
1641 #define LOCKDEP_STATE(__STATE) \
1642 __stringify(__STATE),
1643 #include "lockdep_states.h"
1644 #undef LOCKDEP_STATE
1647 static const char *state_rnames
[] = {
1648 #define LOCKDEP_STATE(__STATE) \
1649 __stringify(__STATE)"-READ",
1650 #include "lockdep_states.h"
1651 #undef LOCKDEP_STATE
1654 static inline const char *state_name(enum lock_usage_bit bit
)
1656 return (bit
& LOCK_USAGE_READ_MASK
) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1659 static int exclusive_bit(int new_bit
)
1661 int state
= new_bit
& LOCK_USAGE_STATE_MASK
;
1662 int dir
= new_bit
& LOCK_USAGE_DIR_MASK
;
1665 * keep state, bit flip the direction and strip read.
1667 return state
| (dir
^ LOCK_USAGE_DIR_MASK
);
1670 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1671 struct held_lock
*next
, enum lock_usage_bit bit
)
1674 * Prove that the new dependency does not connect a hardirq-safe
1675 * lock with a hardirq-unsafe lock - to achieve this we search
1676 * the backwards-subgraph starting at <prev>, and the
1677 * forwards-subgraph starting at <next>:
1679 if (!check_usage(curr
, prev
, next
, bit
,
1680 exclusive_bit(bit
), state_name(bit
)))
1686 * Prove that the new dependency does not connect a hardirq-safe-read
1687 * lock with a hardirq-unsafe lock - to achieve this we search
1688 * the backwards-subgraph starting at <prev>, and the
1689 * forwards-subgraph starting at <next>:
1691 if (!check_usage(curr
, prev
, next
, bit
,
1692 exclusive_bit(bit
), state_name(bit
)))
1699 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1700 struct held_lock
*next
)
1702 #define LOCKDEP_STATE(__STATE) \
1703 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1705 #include "lockdep_states.h"
1706 #undef LOCKDEP_STATE
1711 static void inc_chains(void)
1713 if (current
->hardirq_context
)
1714 nr_hardirq_chains
++;
1716 if (current
->softirq_context
)
1717 nr_softirq_chains
++;
1719 nr_process_chains
++;
1726 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1727 struct held_lock
*next
)
1732 static inline void inc_chains(void)
1734 nr_process_chains
++;
1740 print_deadlock_scenario(struct held_lock
*nxt
,
1741 struct held_lock
*prv
)
1743 struct lock_class
*next
= hlock_class(nxt
);
1744 struct lock_class
*prev
= hlock_class(prv
);
1746 printk(" Possible unsafe locking scenario:\n\n");
1750 __print_lock_name(prev
);
1751 printk(KERN_CONT
");\n");
1753 __print_lock_name(next
);
1754 printk(KERN_CONT
");\n");
1755 printk("\n *** DEADLOCK ***\n\n");
1756 printk(" May be due to missing lock nesting notation\n\n");
1760 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1761 struct held_lock
*next
)
1763 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1767 pr_warn("============================================\n");
1768 pr_warn("WARNING: possible recursive locking detected\n");
1769 print_kernel_ident();
1770 pr_warn("--------------------------------------------\n");
1771 pr_warn("%s/%d is trying to acquire lock:\n",
1772 curr
->comm
, task_pid_nr(curr
));
1774 pr_warn("\nbut task is already holding lock:\n");
1777 pr_warn("\nother info that might help us debug this:\n");
1778 print_deadlock_scenario(next
, prev
);
1779 lockdep_print_held_locks(curr
);
1781 pr_warn("\nstack backtrace:\n");
1788 * Check whether we are holding such a class already.
1790 * (Note that this has to be done separately, because the graph cannot
1791 * detect such classes of deadlocks.)
1793 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1796 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1797 struct lockdep_map
*next_instance
, int read
)
1799 struct held_lock
*prev
;
1800 struct held_lock
*nest
= NULL
;
1803 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1804 prev
= curr
->held_locks
+ i
;
1806 if (prev
->instance
== next
->nest_lock
)
1809 if (hlock_class(prev
) != hlock_class(next
))
1813 * Allow read-after-read recursion of the same
1814 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1816 if ((read
== 2) && prev
->read
)
1820 * We're holding the nest_lock, which serializes this lock's
1821 * nesting behaviour.
1826 return print_deadlock_bug(curr
, prev
, next
);
1832 * There was a chain-cache miss, and we are about to add a new dependency
1833 * to a previous lock. We recursively validate the following rules:
1835 * - would the adding of the <prev> -> <next> dependency create a
1836 * circular dependency in the graph? [== circular deadlock]
1838 * - does the new prev->next dependency connect any hardirq-safe lock
1839 * (in the full backwards-subgraph starting at <prev>) with any
1840 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1841 * <next>)? [== illegal lock inversion with hardirq contexts]
1843 * - does the new prev->next dependency connect any softirq-safe lock
1844 * (in the full backwards-subgraph starting at <prev>) with any
1845 * softirq-unsafe lock (in the full forwards-subgraph starting at
1846 * <next>)? [== illegal lock inversion with softirq contexts]
1848 * any of these scenarios could lead to a deadlock.
1850 * Then if all the validations pass, we add the forwards and backwards
1854 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1855 struct held_lock
*next
, int distance
, struct stack_trace
*trace
,
1856 int (*save
)(struct stack_trace
*trace
))
1858 struct lock_list
*uninitialized_var(target_entry
);
1859 struct lock_list
*entry
;
1860 struct lock_list
this;
1864 * Prove that the new <prev> -> <next> dependency would not
1865 * create a circular dependency in the graph. (We do this by
1866 * forward-recursing into the graph starting at <next>, and
1867 * checking whether we can reach <prev>.)
1869 * We are using global variables to control the recursion, to
1870 * keep the stackframe size of the recursive functions low:
1872 this.class = hlock_class(next
);
1874 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1875 if (unlikely(!ret
)) {
1876 if (!trace
->entries
) {
1878 * If @save fails here, the printing might trigger
1879 * a WARN but because of the !nr_entries it should
1880 * not do bad things.
1884 return print_circular_bug(&this, target_entry
, next
, prev
, trace
);
1886 else if (unlikely(ret
< 0))
1887 return print_bfs_bug(ret
);
1889 if (!check_prev_add_irq(curr
, prev
, next
))
1893 * For recursive read-locks we do all the dependency checks,
1894 * but we dont store read-triggered dependencies (only
1895 * write-triggered dependencies). This ensures that only the
1896 * write-side dependencies matter, and that if for example a
1897 * write-lock never takes any other locks, then the reads are
1898 * equivalent to a NOP.
1900 if (next
->read
== 2 || prev
->read
== 2)
1903 * Is the <prev> -> <next> dependency already present?
1905 * (this may occur even though this is a new chain: consider
1906 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1907 * chains - the second one will be new, but L1 already has
1908 * L2 added to its dependency list, due to the first chain.)
1910 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1911 if (entry
->class == hlock_class(next
)) {
1913 entry
->distance
= 1;
1919 * Is the <prev> -> <next> link redundant?
1921 this.class = hlock_class(prev
);
1923 ret
= check_redundant(&this, hlock_class(next
), &target_entry
);
1925 debug_atomic_inc(nr_redundant
);
1929 return print_bfs_bug(ret
);
1932 if (!trace
->entries
&& !save(trace
))
1936 * Ok, all validations passed, add the new lock
1937 * to the previous lock's dependency list:
1939 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1940 &hlock_class(prev
)->locks_after
,
1941 next
->acquire_ip
, distance
, trace
);
1946 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1947 &hlock_class(next
)->locks_before
,
1948 next
->acquire_ip
, distance
, trace
);
1956 * Add the dependency to all directly-previous locks that are 'relevant'.
1957 * The ones that are relevant are (in increasing distance from curr):
1958 * all consecutive trylock entries and the final non-trylock entry - or
1959 * the end of this context's lock-chain - whichever comes first.
1962 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1964 int depth
= curr
->lockdep_depth
;
1965 struct held_lock
*hlock
;
1966 struct stack_trace trace
= {
1976 * Depth must not be zero for a non-head lock:
1981 * At least two relevant locks must exist for this
1984 if (curr
->held_locks
[depth
].irq_context
!=
1985 curr
->held_locks
[depth
-1].irq_context
)
1989 int distance
= curr
->lockdep_depth
- depth
+ 1;
1990 hlock
= curr
->held_locks
+ depth
- 1;
1993 * Only non-recursive-read entries get new dependencies
1996 if (hlock
->read
!= 2 && hlock
->check
) {
1997 int ret
= check_prev_add(curr
, hlock
, next
, distance
, &trace
, save_trace
);
2002 * Stop after the first non-trylock entry,
2003 * as non-trylock entries have added their
2004 * own direct dependencies already, so this
2005 * lock is connected to them indirectly:
2007 if (!hlock
->trylock
)
2013 * End of lock-stack?
2018 * Stop the search if we cross into another context:
2020 if (curr
->held_locks
[depth
].irq_context
!=
2021 curr
->held_locks
[depth
-1].irq_context
)
2026 if (!debug_locks_off_graph_unlock())
2030 * Clearly we all shouldn't be here, but since we made it we
2031 * can reliable say we messed up our state. See the above two
2032 * gotos for reasons why we could possibly end up here.
2039 unsigned long nr_lock_chains
;
2040 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
2041 int nr_chain_hlocks
;
2042 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
2044 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
2046 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
2050 * Returns the index of the first held_lock of the current chain
2052 static inline int get_first_held_lock(struct task_struct
*curr
,
2053 struct held_lock
*hlock
)
2056 struct held_lock
*hlock_curr
;
2058 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2059 hlock_curr
= curr
->held_locks
+ i
;
2060 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2068 #ifdef CONFIG_DEBUG_LOCKDEP
2070 * Returns the next chain_key iteration
2072 static u64
print_chain_key_iteration(int class_idx
, u64 chain_key
)
2074 u64 new_chain_key
= iterate_chain_key(chain_key
, class_idx
);
2076 printk(" class_idx:%d -> chain_key:%016Lx",
2078 (unsigned long long)new_chain_key
);
2079 return new_chain_key
;
2083 print_chain_keys_held_locks(struct task_struct
*curr
, struct held_lock
*hlock_next
)
2085 struct held_lock
*hlock
;
2087 int depth
= curr
->lockdep_depth
;
2090 printk("depth: %u\n", depth
+ 1);
2091 for (i
= get_first_held_lock(curr
, hlock_next
); i
< depth
; i
++) {
2092 hlock
= curr
->held_locks
+ i
;
2093 chain_key
= print_chain_key_iteration(hlock
->class_idx
, chain_key
);
2098 print_chain_key_iteration(hlock_next
->class_idx
, chain_key
);
2099 print_lock(hlock_next
);
2102 static void print_chain_keys_chain(struct lock_chain
*chain
)
2108 printk("depth: %u\n", chain
->depth
);
2109 for (i
= 0; i
< chain
->depth
; i
++) {
2110 class_id
= chain_hlocks
[chain
->base
+ i
];
2111 chain_key
= print_chain_key_iteration(class_id
+ 1, chain_key
);
2113 print_lock_name(lock_classes
+ class_id
);
2118 static void print_collision(struct task_struct
*curr
,
2119 struct held_lock
*hlock_next
,
2120 struct lock_chain
*chain
)
2123 pr_warn("============================\n");
2124 pr_warn("WARNING: chain_key collision\n");
2125 print_kernel_ident();
2126 pr_warn("----------------------------\n");
2127 pr_warn("%s/%d: ", current
->comm
, task_pid_nr(current
));
2128 pr_warn("Hash chain already cached but the contents don't match!\n");
2130 pr_warn("Held locks:");
2131 print_chain_keys_held_locks(curr
, hlock_next
);
2133 pr_warn("Locks in cached chain:");
2134 print_chain_keys_chain(chain
);
2136 pr_warn("\nstack backtrace:\n");
2142 * Checks whether the chain and the current held locks are consistent
2143 * in depth and also in content. If they are not it most likely means
2144 * that there was a collision during the calculation of the chain_key.
2145 * Returns: 0 not passed, 1 passed
2147 static int check_no_collision(struct task_struct
*curr
,
2148 struct held_lock
*hlock
,
2149 struct lock_chain
*chain
)
2151 #ifdef CONFIG_DEBUG_LOCKDEP
2154 i
= get_first_held_lock(curr
, hlock
);
2156 if (DEBUG_LOCKS_WARN_ON(chain
->depth
!= curr
->lockdep_depth
- (i
- 1))) {
2157 print_collision(curr
, hlock
, chain
);
2161 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2162 id
= curr
->held_locks
[i
].class_idx
- 1;
2164 if (DEBUG_LOCKS_WARN_ON(chain_hlocks
[chain
->base
+ j
] != id
)) {
2165 print_collision(curr
, hlock
, chain
);
2174 * Adds a dependency chain into chain hashtable. And must be called with
2177 * Return 0 if fail, and graph_lock is released.
2178 * Return 1 if succeed, with graph_lock held.
2180 static inline int add_chain_cache(struct task_struct
*curr
,
2181 struct held_lock
*hlock
,
2184 struct lock_class
*class = hlock_class(hlock
);
2185 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2186 struct lock_chain
*chain
;
2190 * Allocate a new chain entry from the static array, and add
2195 * We might need to take the graph lock, ensure we've got IRQs
2196 * disabled to make this an IRQ-safe lock.. for recursion reasons
2197 * lockdep won't complain about its own locking errors.
2199 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2202 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2203 if (!debug_locks_off_graph_unlock())
2206 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2210 chain
= lock_chains
+ nr_lock_chains
++;
2211 chain
->chain_key
= chain_key
;
2212 chain
->irq_context
= hlock
->irq_context
;
2213 i
= get_first_held_lock(curr
, hlock
);
2214 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2216 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks
));
2217 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr
->held_locks
));
2218 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks
[0])) <= ARRAY_SIZE(lock_classes
));
2220 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2221 chain
->base
= nr_chain_hlocks
;
2222 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2223 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2224 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2226 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2227 nr_chain_hlocks
+= chain
->depth
;
2229 if (!debug_locks_off_graph_unlock())
2232 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2237 hlist_add_head_rcu(&chain
->entry
, hash_head
);
2238 debug_atomic_inc(chain_lookup_misses
);
2245 * Look up a dependency chain.
2247 static inline struct lock_chain
*lookup_chain_cache(u64 chain_key
)
2249 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2250 struct lock_chain
*chain
;
2253 * We can walk it lock-free, because entries only get added
2256 hlist_for_each_entry_rcu(chain
, hash_head
, entry
) {
2257 if (chain
->chain_key
== chain_key
) {
2258 debug_atomic_inc(chain_lookup_hits
);
2266 * If the key is not present yet in dependency chain cache then
2267 * add it and return 1 - in this case the new dependency chain is
2268 * validated. If the key is already hashed, return 0.
2269 * (On return with 1 graph_lock is held.)
2271 static inline int lookup_chain_cache_add(struct task_struct
*curr
,
2272 struct held_lock
*hlock
,
2275 struct lock_class
*class = hlock_class(hlock
);
2276 struct lock_chain
*chain
= lookup_chain_cache(chain_key
);
2280 if (!check_no_collision(curr
, hlock
, chain
))
2283 if (very_verbose(class)) {
2284 printk("\nhash chain already cached, key: "
2285 "%016Lx tail class: [%px] %s\n",
2286 (unsigned long long)chain_key
,
2287 class->key
, class->name
);
2293 if (very_verbose(class)) {
2294 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
2295 (unsigned long long)chain_key
, class->key
, class->name
);
2302 * We have to walk the chain again locked - to avoid duplicates:
2304 chain
= lookup_chain_cache(chain_key
);
2310 if (!add_chain_cache(curr
, hlock
, chain_key
))
2316 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2317 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2320 * Trylock needs to maintain the stack of held locks, but it
2321 * does not add new dependencies, because trylock can be done
2324 * We look up the chain_key and do the O(N^2) check and update of
2325 * the dependencies only if this is a new dependency chain.
2326 * (If lookup_chain_cache_add() return with 1 it acquires
2327 * graph_lock for us)
2329 if (!hlock
->trylock
&& hlock
->check
&&
2330 lookup_chain_cache_add(curr
, hlock
, chain_key
)) {
2332 * Check whether last held lock:
2334 * - is irq-safe, if this lock is irq-unsafe
2335 * - is softirq-safe, if this lock is hardirq-unsafe
2337 * And check whether the new lock's dependency graph
2338 * could lead back to the previous lock.
2340 * any of these scenarios could lead to a deadlock. If
2343 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2348 * Mark recursive read, as we jump over it when
2349 * building dependencies (just like we jump over
2355 * Add dependency only if this lock is not the head
2356 * of the chain, and if it's not a secondary read-lock:
2358 if (!chain_head
&& ret
!= 2) {
2359 if (!check_prevs_add(curr
, hlock
))
2365 /* after lookup_chain_cache_add(): */
2366 if (unlikely(!debug_locks
))
2373 static inline int validate_chain(struct task_struct
*curr
,
2374 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2375 int chain_head
, u64 chain_key
)
2382 * We are building curr_chain_key incrementally, so double-check
2383 * it from scratch, to make sure that it's done correctly:
2385 static void check_chain_key(struct task_struct
*curr
)
2387 #ifdef CONFIG_DEBUG_LOCKDEP
2388 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2392 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2393 hlock
= curr
->held_locks
+ i
;
2394 if (chain_key
!= hlock
->prev_chain_key
) {
2397 * We got mighty confused, our chain keys don't match
2398 * with what we expect, someone trample on our task state?
2400 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2401 curr
->lockdep_depth
, i
,
2402 (unsigned long long)chain_key
,
2403 (unsigned long long)hlock
->prev_chain_key
);
2407 * Whoops ran out of static storage again?
2409 if (DEBUG_LOCKS_WARN_ON(hlock
->class_idx
> MAX_LOCKDEP_KEYS
))
2412 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2413 hlock
->irq_context
))
2415 chain_key
= iterate_chain_key(chain_key
, hlock
->class_idx
);
2418 if (chain_key
!= curr
->curr_chain_key
) {
2421 * More smoking hash instead of calculating it, damn see these
2422 * numbers float.. I bet that a pink elephant stepped on my memory.
2424 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2425 curr
->lockdep_depth
, i
,
2426 (unsigned long long)chain_key
,
2427 (unsigned long long)curr
->curr_chain_key
);
2433 print_usage_bug_scenario(struct held_lock
*lock
)
2435 struct lock_class
*class = hlock_class(lock
);
2437 printk(" Possible unsafe locking scenario:\n\n");
2441 __print_lock_name(class);
2442 printk(KERN_CONT
");\n");
2443 printk(" <Interrupt>\n");
2445 __print_lock_name(class);
2446 printk(KERN_CONT
");\n");
2447 printk("\n *** DEADLOCK ***\n\n");
2451 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2452 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2454 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2458 pr_warn("================================\n");
2459 pr_warn("WARNING: inconsistent lock state\n");
2460 print_kernel_ident();
2461 pr_warn("--------------------------------\n");
2463 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2464 usage_str
[prev_bit
], usage_str
[new_bit
]);
2466 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2467 curr
->comm
, task_pid_nr(curr
),
2468 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2469 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2470 trace_hardirqs_enabled(curr
),
2471 trace_softirqs_enabled(curr
));
2474 pr_warn("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2475 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2477 print_irqtrace_events(curr
);
2478 pr_warn("\nother info that might help us debug this:\n");
2479 print_usage_bug_scenario(this);
2481 lockdep_print_held_locks(curr
);
2483 pr_warn("\nstack backtrace:\n");
2490 * Print out an error if an invalid bit is set:
2493 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2494 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2496 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2497 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2501 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2502 enum lock_usage_bit new_bit
);
2504 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2507 * print irq inversion bug:
2510 print_irq_inversion_bug(struct task_struct
*curr
,
2511 struct lock_list
*root
, struct lock_list
*other
,
2512 struct held_lock
*this, int forwards
,
2513 const char *irqclass
)
2515 struct lock_list
*entry
= other
;
2516 struct lock_list
*middle
= NULL
;
2519 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2523 pr_warn("========================================================\n");
2524 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2525 print_kernel_ident();
2526 pr_warn("--------------------------------------------------------\n");
2527 pr_warn("%s/%d just changed the state of lock:\n",
2528 curr
->comm
, task_pid_nr(curr
));
2531 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2533 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2534 print_lock_name(other
->class);
2535 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2537 pr_warn("\nother info that might help us debug this:\n");
2539 /* Find a middle lock (if one exists) */
2540 depth
= get_lock_depth(other
);
2542 if (depth
== 0 && (entry
!= root
)) {
2543 pr_warn("lockdep:%s bad path found in chain graph\n", __func__
);
2547 entry
= get_lock_parent(entry
);
2549 } while (entry
&& entry
!= root
&& (depth
>= 0));
2551 print_irq_lock_scenario(root
, other
,
2552 middle
? middle
->class : root
->class, other
->class);
2554 print_irq_lock_scenario(other
, root
,
2555 middle
? middle
->class : other
->class, root
->class);
2557 lockdep_print_held_locks(curr
);
2559 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2560 if (!save_trace(&root
->trace
))
2562 print_shortest_lock_dependencies(other
, root
);
2564 pr_warn("\nstack backtrace:\n");
2571 * Prove that in the forwards-direction subgraph starting at <this>
2572 * there is no lock matching <mask>:
2575 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2576 enum lock_usage_bit bit
, const char *irqclass
)
2579 struct lock_list root
;
2580 struct lock_list
*uninitialized_var(target_entry
);
2583 root
.class = hlock_class(this);
2584 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2586 return print_bfs_bug(ret
);
2590 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2595 * Prove that in the backwards-direction subgraph starting at <this>
2596 * there is no lock matching <mask>:
2599 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2600 enum lock_usage_bit bit
, const char *irqclass
)
2603 struct lock_list root
;
2604 struct lock_list
*uninitialized_var(target_entry
);
2607 root
.class = hlock_class(this);
2608 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2610 return print_bfs_bug(ret
);
2614 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2618 void print_irqtrace_events(struct task_struct
*curr
)
2620 printk("irq event stamp: %u\n", curr
->irq_events
);
2621 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
2622 curr
->hardirq_enable_event
, (void *)curr
->hardirq_enable_ip
,
2623 (void *)curr
->hardirq_enable_ip
);
2624 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
2625 curr
->hardirq_disable_event
, (void *)curr
->hardirq_disable_ip
,
2626 (void *)curr
->hardirq_disable_ip
);
2627 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
2628 curr
->softirq_enable_event
, (void *)curr
->softirq_enable_ip
,
2629 (void *)curr
->softirq_enable_ip
);
2630 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
2631 curr
->softirq_disable_event
, (void *)curr
->softirq_disable_ip
,
2632 (void *)curr
->softirq_disable_ip
);
2635 static int HARDIRQ_verbose(struct lock_class
*class)
2638 return class_filter(class);
2643 static int SOFTIRQ_verbose(struct lock_class
*class)
2646 return class_filter(class);
2651 #define STRICT_READ_CHECKS 1
2653 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2654 #define LOCKDEP_STATE(__STATE) \
2656 #include "lockdep_states.h"
2657 #undef LOCKDEP_STATE
2660 static inline int state_verbose(enum lock_usage_bit bit
,
2661 struct lock_class
*class)
2663 return state_verbose_f
[bit
>> 2](class);
2666 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2667 enum lock_usage_bit bit
, const char *name
);
2670 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2671 enum lock_usage_bit new_bit
)
2673 int excl_bit
= exclusive_bit(new_bit
);
2674 int read
= new_bit
& LOCK_USAGE_READ_MASK
;
2675 int dir
= new_bit
& LOCK_USAGE_DIR_MASK
;
2678 * mark USED_IN has to look forwards -- to ensure no dependency
2679 * has ENABLED state, which would allow recursion deadlocks.
2681 * mark ENABLED has to look backwards -- to ensure no dependee
2682 * has USED_IN state, which, again, would allow recursion deadlocks.
2684 check_usage_f usage
= dir
?
2685 check_usage_backwards
: check_usage_forwards
;
2688 * Validate that this particular lock does not have conflicting
2691 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2695 * Validate that the lock dependencies don't have conflicting usage
2698 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2699 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~LOCK_USAGE_READ_MASK
)))
2703 * Check for read in write conflicts
2706 if (!valid_state(curr
, this, new_bit
, excl_bit
+ LOCK_USAGE_READ_MASK
))
2709 if (STRICT_READ_CHECKS
&&
2710 !usage(curr
, this, excl_bit
+ LOCK_USAGE_READ_MASK
,
2711 state_name(new_bit
+ LOCK_USAGE_READ_MASK
)))
2715 if (state_verbose(new_bit
, hlock_class(this)))
2722 * Mark all held locks with a usage bit:
2725 mark_held_locks(struct task_struct
*curr
, enum lock_usage_bit base_bit
)
2727 struct held_lock
*hlock
;
2730 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2731 enum lock_usage_bit hlock_bit
= base_bit
;
2732 hlock
= curr
->held_locks
+ i
;
2735 hlock_bit
+= LOCK_USAGE_READ_MASK
;
2737 BUG_ON(hlock_bit
>= LOCK_USAGE_STATES
);
2742 if (!mark_lock(curr
, hlock
, hlock_bit
))
2750 * Hardirqs will be enabled:
2752 static void __trace_hardirqs_on_caller(unsigned long ip
)
2754 struct task_struct
*curr
= current
;
2756 /* we'll do an OFF -> ON transition: */
2757 curr
->hardirqs_enabled
= 1;
2760 * We are going to turn hardirqs on, so set the
2761 * usage bit for all held locks:
2763 if (!mark_held_locks(curr
, LOCK_ENABLED_HARDIRQ
))
2766 * If we have softirqs enabled, then set the usage
2767 * bit for all held locks. (disabled hardirqs prevented
2768 * this bit from being set before)
2770 if (curr
->softirqs_enabled
)
2771 if (!mark_held_locks(curr
, LOCK_ENABLED_SOFTIRQ
))
2774 curr
->hardirq_enable_ip
= ip
;
2775 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2776 debug_atomic_inc(hardirqs_on_events
);
2779 void lockdep_hardirqs_on(unsigned long ip
)
2781 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2784 if (unlikely(current
->hardirqs_enabled
)) {
2786 * Neither irq nor preemption are disabled here
2787 * so this is racy by nature but losing one hit
2788 * in a stat is not a big deal.
2790 __debug_atomic_inc(redundant_hardirqs_on
);
2795 * We're enabling irqs and according to our state above irqs weren't
2796 * already enabled, yet we find the hardware thinks they are in fact
2797 * enabled.. someone messed up their IRQ state tracing.
2799 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2803 * See the fine text that goes along with this variable definition.
2805 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2809 * Can't allow enabling interrupts while in an interrupt handler,
2810 * that's general bad form and such. Recursion, limited stack etc..
2812 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2815 current
->lockdep_recursion
= 1;
2816 __trace_hardirqs_on_caller(ip
);
2817 current
->lockdep_recursion
= 0;
2821 * Hardirqs were disabled:
2823 void lockdep_hardirqs_off(unsigned long ip
)
2825 struct task_struct
*curr
= current
;
2827 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2831 * So we're supposed to get called after you mask local IRQs, but for
2832 * some reason the hardware doesn't quite think you did a proper job.
2834 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2837 if (curr
->hardirqs_enabled
) {
2839 * We have done an ON -> OFF transition:
2841 curr
->hardirqs_enabled
= 0;
2842 curr
->hardirq_disable_ip
= ip
;
2843 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2844 debug_atomic_inc(hardirqs_off_events
);
2846 debug_atomic_inc(redundant_hardirqs_off
);
2850 * Softirqs will be enabled:
2852 void trace_softirqs_on(unsigned long ip
)
2854 struct task_struct
*curr
= current
;
2856 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2860 * We fancy IRQs being disabled here, see softirq.c, avoids
2861 * funny state and nesting things.
2863 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2866 if (curr
->softirqs_enabled
) {
2867 debug_atomic_inc(redundant_softirqs_on
);
2871 current
->lockdep_recursion
= 1;
2873 * We'll do an OFF -> ON transition:
2875 curr
->softirqs_enabled
= 1;
2876 curr
->softirq_enable_ip
= ip
;
2877 curr
->softirq_enable_event
= ++curr
->irq_events
;
2878 debug_atomic_inc(softirqs_on_events
);
2880 * We are going to turn softirqs on, so set the
2881 * usage bit for all held locks, if hardirqs are
2884 if (curr
->hardirqs_enabled
)
2885 mark_held_locks(curr
, LOCK_ENABLED_SOFTIRQ
);
2886 current
->lockdep_recursion
= 0;
2890 * Softirqs were disabled:
2892 void trace_softirqs_off(unsigned long ip
)
2894 struct task_struct
*curr
= current
;
2896 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2900 * We fancy IRQs being disabled here, see softirq.c
2902 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2905 if (curr
->softirqs_enabled
) {
2907 * We have done an ON -> OFF transition:
2909 curr
->softirqs_enabled
= 0;
2910 curr
->softirq_disable_ip
= ip
;
2911 curr
->softirq_disable_event
= ++curr
->irq_events
;
2912 debug_atomic_inc(softirqs_off_events
);
2914 * Whoops, we wanted softirqs off, so why aren't they?
2916 DEBUG_LOCKS_WARN_ON(!softirq_count());
2918 debug_atomic_inc(redundant_softirqs_off
);
2921 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2924 * If non-trylock use in a hardirq or softirq context, then
2925 * mark the lock as used in these contexts:
2927 if (!hlock
->trylock
) {
2929 if (curr
->hardirq_context
)
2930 if (!mark_lock(curr
, hlock
,
2931 LOCK_USED_IN_HARDIRQ_READ
))
2933 if (curr
->softirq_context
)
2934 if (!mark_lock(curr
, hlock
,
2935 LOCK_USED_IN_SOFTIRQ_READ
))
2938 if (curr
->hardirq_context
)
2939 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2941 if (curr
->softirq_context
)
2942 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2946 if (!hlock
->hardirqs_off
) {
2948 if (!mark_lock(curr
, hlock
,
2949 LOCK_ENABLED_HARDIRQ_READ
))
2951 if (curr
->softirqs_enabled
)
2952 if (!mark_lock(curr
, hlock
,
2953 LOCK_ENABLED_SOFTIRQ_READ
))
2956 if (!mark_lock(curr
, hlock
,
2957 LOCK_ENABLED_HARDIRQ
))
2959 if (curr
->softirqs_enabled
)
2960 if (!mark_lock(curr
, hlock
,
2961 LOCK_ENABLED_SOFTIRQ
))
2969 static inline unsigned int task_irq_context(struct task_struct
*task
)
2971 return 2 * !!task
->hardirq_context
+ !!task
->softirq_context
;
2974 static int separate_irq_context(struct task_struct
*curr
,
2975 struct held_lock
*hlock
)
2977 unsigned int depth
= curr
->lockdep_depth
;
2980 * Keep track of points where we cross into an interrupt context:
2983 struct held_lock
*prev_hlock
;
2985 prev_hlock
= curr
->held_locks
+ depth
-1;
2987 * If we cross into another context, reset the
2988 * hash key (this also prevents the checking and the
2989 * adding of the dependency to 'prev'):
2991 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2997 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3000 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
3001 enum lock_usage_bit new_bit
)
3003 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3007 static inline int mark_irqflags(struct task_struct
*curr
,
3008 struct held_lock
*hlock
)
3013 static inline unsigned int task_irq_context(struct task_struct
*task
)
3018 static inline int separate_irq_context(struct task_struct
*curr
,
3019 struct held_lock
*hlock
)
3024 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3027 * Mark a lock with a usage bit, and validate the state transition:
3029 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
3030 enum lock_usage_bit new_bit
)
3032 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
3035 * If already set then do not dirty the cacheline,
3036 * nor do any checks:
3038 if (likely(hlock_class(this)->usage_mask
& new_mask
))
3044 * Make sure we didn't race:
3046 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
3051 hlock_class(this)->usage_mask
|= new_mask
;
3053 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
3057 #define LOCKDEP_STATE(__STATE) \
3058 case LOCK_USED_IN_##__STATE: \
3059 case LOCK_USED_IN_##__STATE##_READ: \
3060 case LOCK_ENABLED_##__STATE: \
3061 case LOCK_ENABLED_##__STATE##_READ:
3062 #include "lockdep_states.h"
3063 #undef LOCKDEP_STATE
3064 ret
= mark_lock_irq(curr
, this, new_bit
);
3069 debug_atomic_dec(nr_unused_locks
);
3072 if (!debug_locks_off_graph_unlock())
3081 * We must printk outside of the graph_lock:
3084 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
3086 print_irqtrace_events(curr
);
3094 * Initialize a lock instance's lock-class mapping info:
3096 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
3097 struct lock_class_key
*key
, int subclass
)
3101 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
3102 lock
->class_cache
[i
] = NULL
;
3104 #ifdef CONFIG_LOCK_STAT
3105 lock
->cpu
= raw_smp_processor_id();
3109 * Can't be having no nameless bastards around this place!
3111 if (DEBUG_LOCKS_WARN_ON(!name
)) {
3112 lock
->name
= "NULL";
3119 * No key, no joy, we need to hash something.
3121 if (DEBUG_LOCKS_WARN_ON(!key
))
3124 * Sanity check, the lock-class key must be persistent:
3126 if (!static_obj(key
)) {
3127 printk("BUG: key %px not in .data!\n", key
);
3129 * What it says above ^^^^^, I suggest you read it.
3131 DEBUG_LOCKS_WARN_ON(1);
3136 if (unlikely(!debug_locks
))
3140 unsigned long flags
;
3142 if (DEBUG_LOCKS_WARN_ON(current
->lockdep_recursion
))
3145 raw_local_irq_save(flags
);
3146 current
->lockdep_recursion
= 1;
3147 register_lock_class(lock
, subclass
, 1);
3148 current
->lockdep_recursion
= 0;
3149 raw_local_irq_restore(flags
);
3152 EXPORT_SYMBOL_GPL(lockdep_init_map
);
3154 struct lock_class_key __lockdep_no_validate__
;
3155 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
3158 print_lock_nested_lock_not_held(struct task_struct
*curr
,
3159 struct held_lock
*hlock
,
3162 if (!debug_locks_off())
3164 if (debug_locks_silent
)
3168 pr_warn("==================================\n");
3169 pr_warn("WARNING: Nested lock was not taken\n");
3170 print_kernel_ident();
3171 pr_warn("----------------------------------\n");
3173 pr_warn("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3176 pr_warn("\nbut this task is not holding:\n");
3177 pr_warn("%s\n", hlock
->nest_lock
->name
);
3179 pr_warn("\nstack backtrace:\n");
3182 pr_warn("\nother info that might help us debug this:\n");
3183 lockdep_print_held_locks(curr
);
3185 pr_warn("\nstack backtrace:\n");
3191 static int __lock_is_held(const struct lockdep_map
*lock
, int read
);
3194 * This gets called for every mutex_lock*()/spin_lock*() operation.
3195 * We maintain the dependency maps and validate the locking attempt:
3197 * The callers must make sure that IRQs are disabled before calling it,
3198 * otherwise we could get an interrupt which would want to take locks,
3199 * which would end up in lockdep again.
3201 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3202 int trylock
, int read
, int check
, int hardirqs_off
,
3203 struct lockdep_map
*nest_lock
, unsigned long ip
,
3204 int references
, int pin_count
)
3206 struct task_struct
*curr
= current
;
3207 struct lock_class
*class = NULL
;
3208 struct held_lock
*hlock
;
3214 if (unlikely(!debug_locks
))
3217 if (!prove_locking
|| lock
->key
== &__lockdep_no_validate__
)
3220 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
3221 class = lock
->class_cache
[subclass
];
3225 if (unlikely(!class)) {
3226 class = register_lock_class(lock
, subclass
, 0);
3231 debug_class_ops_inc(class);
3233 if (very_verbose(class)) {
3234 printk("\nacquire class [%px] %s", class->key
, class->name
);
3235 if (class->name_version
> 1)
3236 printk(KERN_CONT
"#%d", class->name_version
);
3237 printk(KERN_CONT
"\n");
3242 * Add the lock to the list of currently held locks.
3243 * (we dont increase the depth just yet, up until the
3244 * dependency checks are done)
3246 depth
= curr
->lockdep_depth
;
3248 * Ran out of static storage for our per-task lock stack again have we?
3250 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
3253 class_idx
= class - lock_classes
+ 1;
3256 hlock
= curr
->held_locks
+ depth
- 1;
3257 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
3258 if (hlock
->references
) {
3260 * Check: unsigned int references:12, overflow.
3262 if (DEBUG_LOCKS_WARN_ON(hlock
->references
== (1 << 12)-1))
3265 hlock
->references
++;
3267 hlock
->references
= 2;
3274 hlock
= curr
->held_locks
+ depth
;
3276 * Plain impossible, we just registered it and checked it weren't no
3277 * NULL like.. I bet this mushroom I ate was good!
3279 if (DEBUG_LOCKS_WARN_ON(!class))
3281 hlock
->class_idx
= class_idx
;
3282 hlock
->acquire_ip
= ip
;
3283 hlock
->instance
= lock
;
3284 hlock
->nest_lock
= nest_lock
;
3285 hlock
->irq_context
= task_irq_context(curr
);
3286 hlock
->trylock
= trylock
;
3288 hlock
->check
= check
;
3289 hlock
->hardirqs_off
= !!hardirqs_off
;
3290 hlock
->references
= references
;
3291 #ifdef CONFIG_LOCK_STAT
3292 hlock
->waittime_stamp
= 0;
3293 hlock
->holdtime_stamp
= lockstat_clock();
3295 hlock
->pin_count
= pin_count
;
3297 if (check
&& !mark_irqflags(curr
, hlock
))
3300 /* mark it as used: */
3301 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3305 * Calculate the chain hash: it's the combined hash of all the
3306 * lock keys along the dependency chain. We save the hash value
3307 * at every step so that we can get the current hash easily
3308 * after unlock. The chain hash is then used to cache dependency
3311 * The 'key ID' is what is the most compact key value to drive
3312 * the hash, not class->key.
3315 * Whoops, we did it again.. ran straight out of our static allocation.
3317 if (DEBUG_LOCKS_WARN_ON(class_idx
> MAX_LOCKDEP_KEYS
))
3320 chain_key
= curr
->curr_chain_key
;
3323 * How can we have a chain hash when we ain't got no keys?!
3325 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3330 hlock
->prev_chain_key
= chain_key
;
3331 if (separate_irq_context(curr
, hlock
)) {
3335 chain_key
= iterate_chain_key(chain_key
, class_idx
);
3337 if (nest_lock
&& !__lock_is_held(nest_lock
, -1))
3338 return print_lock_nested_lock_not_held(curr
, hlock
, ip
);
3340 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3343 curr
->curr_chain_key
= chain_key
;
3344 curr
->lockdep_depth
++;
3345 check_chain_key(curr
);
3346 #ifdef CONFIG_DEBUG_LOCKDEP
3347 if (unlikely(!debug_locks
))
3350 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3352 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3353 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
3354 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
3356 lockdep_print_held_locks(current
);
3357 debug_show_all_locks();
3363 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3364 max_lockdep_depth
= curr
->lockdep_depth
;
3370 print_unlock_imbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3373 if (!debug_locks_off())
3375 if (debug_locks_silent
)
3379 pr_warn("=====================================\n");
3380 pr_warn("WARNING: bad unlock balance detected!\n");
3381 print_kernel_ident();
3382 pr_warn("-------------------------------------\n");
3383 pr_warn("%s/%d is trying to release lock (",
3384 curr
->comm
, task_pid_nr(curr
));
3385 print_lockdep_cache(lock
);
3388 pr_warn("but there are no more locks to release!\n");
3389 pr_warn("\nother info that might help us debug this:\n");
3390 lockdep_print_held_locks(curr
);
3392 pr_warn("\nstack backtrace:\n");
3398 static int match_held_lock(const struct held_lock
*hlock
,
3399 const struct lockdep_map
*lock
)
3401 if (hlock
->instance
== lock
)
3404 if (hlock
->references
) {
3405 const struct lock_class
*class = lock
->class_cache
[0];
3408 class = look_up_lock_class(lock
, 0);
3411 * If look_up_lock_class() failed to find a class, we're trying
3412 * to test if we hold a lock that has never yet been acquired.
3413 * Clearly if the lock hasn't been acquired _ever_, we're not
3414 * holding it either, so report failure.
3420 * References, but not a lock we're actually ref-counting?
3421 * State got messed up, follow the sites that change ->references
3422 * and try to make sense of it.
3424 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3427 if (hlock
->class_idx
== class - lock_classes
+ 1)
3434 /* @depth must not be zero */
3435 static struct held_lock
*find_held_lock(struct task_struct
*curr
,
3436 struct lockdep_map
*lock
,
3437 unsigned int depth
, int *idx
)
3439 struct held_lock
*ret
, *hlock
, *prev_hlock
;
3443 hlock
= curr
->held_locks
+ i
;
3445 if (match_held_lock(hlock
, lock
))
3449 for (i
--, prev_hlock
= hlock
--;
3451 i
--, prev_hlock
= hlock
--) {
3453 * We must not cross into another context:
3455 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
3459 if (match_held_lock(hlock
, lock
)) {
3470 static int reacquire_held_locks(struct task_struct
*curr
, unsigned int depth
,
3473 struct held_lock
*hlock
;
3475 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3478 for (hlock
= curr
->held_locks
+ idx
; idx
< depth
; idx
++, hlock
++) {
3479 if (!__lock_acquire(hlock
->instance
,
3480 hlock_class(hlock
)->subclass
,
3482 hlock
->read
, hlock
->check
,
3483 hlock
->hardirqs_off
,
3484 hlock
->nest_lock
, hlock
->acquire_ip
,
3485 hlock
->references
, hlock
->pin_count
))
3492 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3493 struct lock_class_key
*key
, unsigned int subclass
,
3496 struct task_struct
*curr
= current
;
3497 struct held_lock
*hlock
;
3498 struct lock_class
*class;
3502 if (unlikely(!debug_locks
))
3505 depth
= curr
->lockdep_depth
;
3507 * This function is about (re)setting the class of a held lock,
3508 * yet we're not actually holding any locks. Naughty user!
3510 if (DEBUG_LOCKS_WARN_ON(!depth
))
3513 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3515 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3517 lockdep_init_map(lock
, name
, key
, 0);
3518 class = register_lock_class(lock
, subclass
, 0);
3519 hlock
->class_idx
= class - lock_classes
+ 1;
3521 curr
->lockdep_depth
= i
;
3522 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3524 if (reacquire_held_locks(curr
, depth
, i
))
3528 * I took it apart and put it back together again, except now I have
3529 * these 'spare' parts.. where shall I put them.
3531 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3536 static int __lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
3538 struct task_struct
*curr
= current
;
3539 struct held_lock
*hlock
;
3543 if (unlikely(!debug_locks
))
3546 depth
= curr
->lockdep_depth
;
3548 * This function is about (re)setting the class of a held lock,
3549 * yet we're not actually holding any locks. Naughty user!
3551 if (DEBUG_LOCKS_WARN_ON(!depth
))
3554 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3556 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3558 curr
->lockdep_depth
= i
;
3559 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3561 WARN(hlock
->read
, "downgrading a read lock");
3563 hlock
->acquire_ip
= ip
;
3565 if (reacquire_held_locks(curr
, depth
, i
))
3569 * I took it apart and put it back together again, except now I have
3570 * these 'spare' parts.. where shall I put them.
3572 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3578 * Remove the lock to the list of currently held locks - this gets
3579 * called on mutex_unlock()/spin_unlock*() (or on a failed
3580 * mutex_lock_interruptible()).
3582 * @nested is an hysterical artifact, needs a tree wide cleanup.
3585 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3587 struct task_struct
*curr
= current
;
3588 struct held_lock
*hlock
;
3592 if (unlikely(!debug_locks
))
3595 depth
= curr
->lockdep_depth
;
3597 * So we're all set to release this lock.. wait what lock? We don't
3598 * own any locks, you've been drinking again?
3600 if (DEBUG_LOCKS_WARN_ON(depth
<= 0))
3601 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3604 * Check whether the lock exists in the current stack
3607 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3609 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3611 if (hlock
->instance
== lock
)
3612 lock_release_holdtime(hlock
);
3614 WARN(hlock
->pin_count
, "releasing a pinned lock\n");
3616 if (hlock
->references
) {
3617 hlock
->references
--;
3618 if (hlock
->references
) {
3620 * We had, and after removing one, still have
3621 * references, the current lock stack is still
3622 * valid. We're done!
3629 * We have the right lock to unlock, 'hlock' points to it.
3630 * Now we remove it from the stack, and add back the other
3631 * entries (if any), recalculating the hash along the way:
3634 curr
->lockdep_depth
= i
;
3635 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3638 * The most likely case is when the unlock is on the innermost
3639 * lock. In this case, we are done!
3644 if (reacquire_held_locks(curr
, depth
, i
+ 1))
3648 * We had N bottles of beer on the wall, we drank one, but now
3649 * there's not N-1 bottles of beer left on the wall...
3651 DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
-1);
3654 * Since reacquire_held_locks() would have called check_chain_key()
3655 * indirectly via __lock_acquire(), we don't need to do it again
3661 static int __lock_is_held(const struct lockdep_map
*lock
, int read
)
3663 struct task_struct
*curr
= current
;
3666 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3667 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3669 if (match_held_lock(hlock
, lock
)) {
3670 if (read
== -1 || hlock
->read
== read
)
3680 static struct pin_cookie
__lock_pin_lock(struct lockdep_map
*lock
)
3682 struct pin_cookie cookie
= NIL_COOKIE
;
3683 struct task_struct
*curr
= current
;
3686 if (unlikely(!debug_locks
))
3689 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3690 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3692 if (match_held_lock(hlock
, lock
)) {
3694 * Grab 16bits of randomness; this is sufficient to not
3695 * be guessable and still allows some pin nesting in
3696 * our u32 pin_count.
3698 cookie
.val
= 1 + (prandom_u32() >> 16);
3699 hlock
->pin_count
+= cookie
.val
;
3704 WARN(1, "pinning an unheld lock\n");
3708 static void __lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3710 struct task_struct
*curr
= current
;
3713 if (unlikely(!debug_locks
))
3716 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3717 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3719 if (match_held_lock(hlock
, lock
)) {
3720 hlock
->pin_count
+= cookie
.val
;
3725 WARN(1, "pinning an unheld lock\n");
3728 static void __lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3730 struct task_struct
*curr
= current
;
3733 if (unlikely(!debug_locks
))
3736 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3737 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3739 if (match_held_lock(hlock
, lock
)) {
3740 if (WARN(!hlock
->pin_count
, "unpinning an unpinned lock\n"))
3743 hlock
->pin_count
-= cookie
.val
;
3745 if (WARN((int)hlock
->pin_count
< 0, "pin count corrupted\n"))
3746 hlock
->pin_count
= 0;
3752 WARN(1, "unpinning an unheld lock\n");
3756 * Check whether we follow the irq-flags state precisely:
3758 static void check_flags(unsigned long flags
)
3760 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3761 defined(CONFIG_TRACE_IRQFLAGS)
3765 if (irqs_disabled_flags(flags
)) {
3766 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3767 printk("possible reason: unannotated irqs-off.\n");
3770 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3771 printk("possible reason: unannotated irqs-on.\n");
3776 * We dont accurately track softirq state in e.g.
3777 * hardirq contexts (such as on 4KSTACKS), so only
3778 * check if not in hardirq contexts:
3780 if (!hardirq_count()) {
3781 if (softirq_count()) {
3782 /* like the above, but with softirqs */
3783 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3785 /* lick the above, does it taste good? */
3786 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3791 print_irqtrace_events(current
);
3795 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3796 struct lock_class_key
*key
, unsigned int subclass
,
3799 unsigned long flags
;
3801 if (unlikely(current
->lockdep_recursion
))
3804 raw_local_irq_save(flags
);
3805 current
->lockdep_recursion
= 1;
3807 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3808 check_chain_key(current
);
3809 current
->lockdep_recursion
= 0;
3810 raw_local_irq_restore(flags
);
3812 EXPORT_SYMBOL_GPL(lock_set_class
);
3814 void lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
3816 unsigned long flags
;
3818 if (unlikely(current
->lockdep_recursion
))
3821 raw_local_irq_save(flags
);
3822 current
->lockdep_recursion
= 1;
3824 if (__lock_downgrade(lock
, ip
))
3825 check_chain_key(current
);
3826 current
->lockdep_recursion
= 0;
3827 raw_local_irq_restore(flags
);
3829 EXPORT_SYMBOL_GPL(lock_downgrade
);
3832 * We are not always called with irqs disabled - do that here,
3833 * and also avoid lockdep recursion:
3835 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3836 int trylock
, int read
, int check
,
3837 struct lockdep_map
*nest_lock
, unsigned long ip
)
3839 unsigned long flags
;
3841 if (unlikely(current
->lockdep_recursion
))
3844 raw_local_irq_save(flags
);
3847 current
->lockdep_recursion
= 1;
3848 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3849 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3850 irqs_disabled_flags(flags
), nest_lock
, ip
, 0, 0);
3851 current
->lockdep_recursion
= 0;
3852 raw_local_irq_restore(flags
);
3854 EXPORT_SYMBOL_GPL(lock_acquire
);
3856 void lock_release(struct lockdep_map
*lock
, int nested
,
3859 unsigned long flags
;
3861 if (unlikely(current
->lockdep_recursion
))
3864 raw_local_irq_save(flags
);
3866 current
->lockdep_recursion
= 1;
3867 trace_lock_release(lock
, ip
);
3868 if (__lock_release(lock
, nested
, ip
))
3869 check_chain_key(current
);
3870 current
->lockdep_recursion
= 0;
3871 raw_local_irq_restore(flags
);
3873 EXPORT_SYMBOL_GPL(lock_release
);
3875 int lock_is_held_type(const struct lockdep_map
*lock
, int read
)
3877 unsigned long flags
;
3880 if (unlikely(current
->lockdep_recursion
))
3881 return 1; /* avoid false negative lockdep_assert_held() */
3883 raw_local_irq_save(flags
);
3886 current
->lockdep_recursion
= 1;
3887 ret
= __lock_is_held(lock
, read
);
3888 current
->lockdep_recursion
= 0;
3889 raw_local_irq_restore(flags
);
3893 EXPORT_SYMBOL_GPL(lock_is_held_type
);
3895 struct pin_cookie
lock_pin_lock(struct lockdep_map
*lock
)
3897 struct pin_cookie cookie
= NIL_COOKIE
;
3898 unsigned long flags
;
3900 if (unlikely(current
->lockdep_recursion
))
3903 raw_local_irq_save(flags
);
3906 current
->lockdep_recursion
= 1;
3907 cookie
= __lock_pin_lock(lock
);
3908 current
->lockdep_recursion
= 0;
3909 raw_local_irq_restore(flags
);
3913 EXPORT_SYMBOL_GPL(lock_pin_lock
);
3915 void lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3917 unsigned long flags
;
3919 if (unlikely(current
->lockdep_recursion
))
3922 raw_local_irq_save(flags
);
3925 current
->lockdep_recursion
= 1;
3926 __lock_repin_lock(lock
, cookie
);
3927 current
->lockdep_recursion
= 0;
3928 raw_local_irq_restore(flags
);
3930 EXPORT_SYMBOL_GPL(lock_repin_lock
);
3932 void lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3934 unsigned long flags
;
3936 if (unlikely(current
->lockdep_recursion
))
3939 raw_local_irq_save(flags
);
3942 current
->lockdep_recursion
= 1;
3943 __lock_unpin_lock(lock
, cookie
);
3944 current
->lockdep_recursion
= 0;
3945 raw_local_irq_restore(flags
);
3947 EXPORT_SYMBOL_GPL(lock_unpin_lock
);
3949 #ifdef CONFIG_LOCK_STAT
3951 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3954 if (!debug_locks_off())
3956 if (debug_locks_silent
)
3960 pr_warn("=================================\n");
3961 pr_warn("WARNING: bad contention detected!\n");
3962 print_kernel_ident();
3963 pr_warn("---------------------------------\n");
3964 pr_warn("%s/%d is trying to contend lock (",
3965 curr
->comm
, task_pid_nr(curr
));
3966 print_lockdep_cache(lock
);
3969 pr_warn("but there are no locks held!\n");
3970 pr_warn("\nother info that might help us debug this:\n");
3971 lockdep_print_held_locks(curr
);
3973 pr_warn("\nstack backtrace:\n");
3980 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3982 struct task_struct
*curr
= current
;
3983 struct held_lock
*hlock
;
3984 struct lock_class_stats
*stats
;
3986 int i
, contention_point
, contending_point
;
3988 depth
= curr
->lockdep_depth
;
3990 * Whee, we contended on this lock, except it seems we're not
3991 * actually trying to acquire anything much at all..
3993 if (DEBUG_LOCKS_WARN_ON(!depth
))
3996 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3998 print_lock_contention_bug(curr
, lock
, ip
);
4002 if (hlock
->instance
!= lock
)
4005 hlock
->waittime_stamp
= lockstat_clock();
4007 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
4008 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
4011 stats
= get_lock_stats(hlock_class(hlock
));
4012 if (contention_point
< LOCKSTAT_POINTS
)
4013 stats
->contention_point
[contention_point
]++;
4014 if (contending_point
< LOCKSTAT_POINTS
)
4015 stats
->contending_point
[contending_point
]++;
4016 if (lock
->cpu
!= smp_processor_id())
4017 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
4021 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4023 struct task_struct
*curr
= current
;
4024 struct held_lock
*hlock
;
4025 struct lock_class_stats
*stats
;
4027 u64 now
, waittime
= 0;
4030 depth
= curr
->lockdep_depth
;
4032 * Yay, we acquired ownership of this lock we didn't try to
4033 * acquire, how the heck did that happen?
4035 if (DEBUG_LOCKS_WARN_ON(!depth
))
4038 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4040 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
4044 if (hlock
->instance
!= lock
)
4047 cpu
= smp_processor_id();
4048 if (hlock
->waittime_stamp
) {
4049 now
= lockstat_clock();
4050 waittime
= now
- hlock
->waittime_stamp
;
4051 hlock
->holdtime_stamp
= now
;
4054 trace_lock_acquired(lock
, ip
);
4056 stats
= get_lock_stats(hlock_class(hlock
));
4059 lock_time_inc(&stats
->read_waittime
, waittime
);
4061 lock_time_inc(&stats
->write_waittime
, waittime
);
4063 if (lock
->cpu
!= cpu
)
4064 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
4070 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
4072 unsigned long flags
;
4074 if (unlikely(!lock_stat
|| !debug_locks
))
4077 if (unlikely(current
->lockdep_recursion
))
4080 raw_local_irq_save(flags
);
4082 current
->lockdep_recursion
= 1;
4083 trace_lock_contended(lock
, ip
);
4084 __lock_contended(lock
, ip
);
4085 current
->lockdep_recursion
= 0;
4086 raw_local_irq_restore(flags
);
4088 EXPORT_SYMBOL_GPL(lock_contended
);
4090 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4092 unsigned long flags
;
4094 if (unlikely(!lock_stat
|| !debug_locks
))
4097 if (unlikely(current
->lockdep_recursion
))
4100 raw_local_irq_save(flags
);
4102 current
->lockdep_recursion
= 1;
4103 __lock_acquired(lock
, ip
);
4104 current
->lockdep_recursion
= 0;
4105 raw_local_irq_restore(flags
);
4107 EXPORT_SYMBOL_GPL(lock_acquired
);
4111 * Used by the testsuite, sanitize the validator state
4112 * after a simulated failure:
4115 void lockdep_reset(void)
4117 unsigned long flags
;
4120 raw_local_irq_save(flags
);
4121 current
->curr_chain_key
= 0;
4122 current
->lockdep_depth
= 0;
4123 current
->lockdep_recursion
= 0;
4124 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
4125 nr_hardirq_chains
= 0;
4126 nr_softirq_chains
= 0;
4127 nr_process_chains
= 0;
4129 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
4130 INIT_HLIST_HEAD(chainhash_table
+ i
);
4131 raw_local_irq_restore(flags
);
4135 * Remove all references to a lock class. The caller must hold the graph lock.
4137 static void zap_class(struct lock_class
*class)
4139 struct lock_list
*entry
;
4143 * Remove all dependencies this lock is
4146 for (i
= 0, entry
= list_entries
; i
< nr_list_entries
; i
++, entry
++) {
4147 if (entry
->class != class && entry
->links_to
!= class)
4149 list_del_rcu(&entry
->entry
);
4150 /* Clear .class and .links_to to avoid double removal. */
4151 WRITE_ONCE(entry
->class, NULL
);
4152 WRITE_ONCE(entry
->links_to
, NULL
);
4155 * Unhash the class and remove it from the all_lock_classes list:
4157 hlist_del_rcu(&class->hash_entry
);
4158 list_del(&class->lock_entry
);
4160 RCU_INIT_POINTER(class->key
, NULL
);
4161 RCU_INIT_POINTER(class->name
, NULL
);
4164 static inline int within(const void *addr
, void *start
, unsigned long size
)
4166 return addr
>= start
&& addr
< start
+ size
;
4169 static void __lockdep_free_key_range(void *start
, unsigned long size
)
4171 struct lock_class
*class;
4172 struct hlist_head
*head
;
4175 /* Unhash all classes that were created by a module. */
4176 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4177 head
= classhash_table
+ i
;
4178 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4179 if (!within(class->key
, start
, size
) &&
4180 !within(class->name
, start
, size
))
4188 * Used in module.c to remove lock classes from memory that is going to be
4189 * freed; and possibly re-used by other modules.
4191 * We will have had one sync_sched() before getting here, so we're guaranteed
4192 * nobody will look up these exact classes -- they're properly dead but still
4195 void lockdep_free_key_range(void *start
, unsigned long size
)
4197 unsigned long flags
;
4200 init_data_structures_once();
4202 raw_local_irq_save(flags
);
4203 locked
= graph_lock();
4204 __lockdep_free_key_range(start
, size
);
4207 raw_local_irq_restore(flags
);
4210 * Wait for any possible iterators from look_up_lock_class() to pass
4211 * before continuing to free the memory they refer to.
4213 * sync_sched() is sufficient because the read-side is IRQ disable.
4218 * XXX at this point we could return the resources to the pool;
4219 * instead we leak them. We would need to change to bitmap allocators
4220 * instead of the linear allocators we have now.
4225 * Check whether any element of the @lock->class_cache[] array refers to a
4226 * registered lock class. The caller must hold either the graph lock or the
4229 static bool lock_class_cache_is_registered(struct lockdep_map
*lock
)
4231 struct lock_class
*class;
4232 struct hlist_head
*head
;
4235 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4236 head
= classhash_table
+ i
;
4237 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4238 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
4239 if (lock
->class_cache
[j
] == class)
4246 /* The caller must hold the graph lock. Does not sleep. */
4247 static void __lockdep_reset_lock(struct lockdep_map
*lock
)
4249 struct lock_class
*class;
4253 * Remove all classes this lock might have:
4255 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
4257 * If the class exists we look it up and zap it:
4259 class = look_up_lock_class(lock
, j
);
4264 * Debug check: in the end all mapped classes should
4267 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock
)))
4271 void lockdep_reset_lock(struct lockdep_map
*lock
)
4273 unsigned long flags
;
4276 init_data_structures_once();
4278 raw_local_irq_save(flags
);
4279 locked
= graph_lock();
4280 __lockdep_reset_lock(lock
);
4283 raw_local_irq_restore(flags
);
4286 void __init
lockdep_init(void)
4288 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4290 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
4291 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
4292 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
4293 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
4294 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
4295 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
4296 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
4298 printk(" memory used by lock dependency info: %zu kB\n",
4299 (sizeof(lock_classes
) +
4300 sizeof(classhash_table
) +
4301 sizeof(list_entries
) +
4302 sizeof(chainhash_table
)
4303 #ifdef CONFIG_PROVE_LOCKING
4305 + sizeof(lock_chains
)
4306 + sizeof(chain_hlocks
)
4311 printk(" per task-struct memory footprint: %zu bytes\n",
4312 sizeof(((struct task_struct
*)NULL
)->held_locks
));
4316 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
4317 const void *mem_to
, struct held_lock
*hlock
)
4319 if (!debug_locks_off())
4321 if (debug_locks_silent
)
4325 pr_warn("=========================\n");
4326 pr_warn("WARNING: held lock freed!\n");
4327 print_kernel_ident();
4328 pr_warn("-------------------------\n");
4329 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
4330 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
4332 lockdep_print_held_locks(curr
);
4334 pr_warn("\nstack backtrace:\n");
4338 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
4339 const void* lock_from
, unsigned long lock_len
)
4341 return lock_from
+ lock_len
<= mem_from
||
4342 mem_from
+ mem_len
<= lock_from
;
4346 * Called when kernel memory is freed (or unmapped), or if a lock
4347 * is destroyed or reinitialized - this code checks whether there is
4348 * any held lock in the memory range of <from> to <to>:
4350 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
4352 struct task_struct
*curr
= current
;
4353 struct held_lock
*hlock
;
4354 unsigned long flags
;
4357 if (unlikely(!debug_locks
))
4360 raw_local_irq_save(flags
);
4361 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4362 hlock
= curr
->held_locks
+ i
;
4364 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
4365 sizeof(*hlock
->instance
)))
4368 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
4371 raw_local_irq_restore(flags
);
4373 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
4375 static void print_held_locks_bug(void)
4377 if (!debug_locks_off())
4379 if (debug_locks_silent
)
4383 pr_warn("====================================\n");
4384 pr_warn("WARNING: %s/%d still has locks held!\n",
4385 current
->comm
, task_pid_nr(current
));
4386 print_kernel_ident();
4387 pr_warn("------------------------------------\n");
4388 lockdep_print_held_locks(current
);
4389 pr_warn("\nstack backtrace:\n");
4393 void debug_check_no_locks_held(void)
4395 if (unlikely(current
->lockdep_depth
> 0))
4396 print_held_locks_bug();
4398 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
4401 void debug_show_all_locks(void)
4403 struct task_struct
*g
, *p
;
4405 if (unlikely(!debug_locks
)) {
4406 pr_warn("INFO: lockdep is turned off.\n");
4409 pr_warn("\nShowing all locks held in the system:\n");
4412 for_each_process_thread(g
, p
) {
4413 if (!p
->lockdep_depth
)
4415 lockdep_print_held_locks(p
);
4416 touch_nmi_watchdog();
4417 touch_all_softlockup_watchdogs();
4422 pr_warn("=============================================\n\n");
4424 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
4428 * Careful: only use this function if you are sure that
4429 * the task cannot run in parallel!
4431 void debug_show_held_locks(struct task_struct
*task
)
4433 if (unlikely(!debug_locks
)) {
4434 printk("INFO: lockdep is turned off.\n");
4437 lockdep_print_held_locks(task
);
4439 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
4441 asmlinkage __visible
void lockdep_sys_exit(void)
4443 struct task_struct
*curr
= current
;
4445 if (unlikely(curr
->lockdep_depth
)) {
4446 if (!debug_locks_off())
4449 pr_warn("================================================\n");
4450 pr_warn("WARNING: lock held when returning to user space!\n");
4451 print_kernel_ident();
4452 pr_warn("------------------------------------------------\n");
4453 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4454 curr
->comm
, curr
->pid
);
4455 lockdep_print_held_locks(curr
);
4459 * The lock history for each syscall should be independent. So wipe the
4460 * slate clean on return to userspace.
4462 lockdep_invariant_state(false);
4465 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
4467 struct task_struct
*curr
= current
;
4469 /* Note: the following can be executed concurrently, so be careful. */
4471 pr_warn("=============================\n");
4472 pr_warn("WARNING: suspicious RCU usage\n");
4473 print_kernel_ident();
4474 pr_warn("-----------------------------\n");
4475 pr_warn("%s:%d %s!\n", file
, line
, s
);
4476 pr_warn("\nother info that might help us debug this:\n\n");
4477 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4478 !rcu_lockdep_current_cpu_online()
4479 ? "RCU used illegally from offline CPU!\n"
4480 : !rcu_is_watching()
4481 ? "RCU used illegally from idle CPU!\n"
4483 rcu_scheduler_active
, debug_locks
);
4486 * If a CPU is in the RCU-free window in idle (ie: in the section
4487 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4488 * considers that CPU to be in an "extended quiescent state",
4489 * which means that RCU will be completely ignoring that CPU.
4490 * Therefore, rcu_read_lock() and friends have absolutely no
4491 * effect on a CPU running in that state. In other words, even if
4492 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4493 * delete data structures out from under it. RCU really has no
4494 * choice here: we need to keep an RCU-free window in idle where
4495 * the CPU may possibly enter into low power mode. This way we can
4496 * notice an extended quiescent state to other CPUs that started a grace
4497 * period. Otherwise we would delay any grace period as long as we run
4500 * So complain bitterly if someone does call rcu_read_lock(),
4501 * rcu_read_lock_bh() and so on from extended quiescent states.
4503 if (!rcu_is_watching())
4504 pr_warn("RCU used illegally from extended quiescent state!\n");
4506 lockdep_print_held_locks(curr
);
4507 pr_warn("\nstack backtrace:\n");
4510 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);