4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
28 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/sched/clock.h>
32 #include <linux/sched/task.h>
33 #include <linux/sched/mm.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39 #include <linux/kallsyms.h>
40 #include <linux/interrupt.h>
41 #include <linux/stacktrace.h>
42 #include <linux/debug_locks.h>
43 #include <linux/irqflags.h>
44 #include <linux/utsname.h>
45 #include <linux/hash.h>
46 #include <linux/ftrace.h>
47 #include <linux/stringify.h>
48 #include <linux/bitops.h>
49 #include <linux/gfp.h>
50 #include <linux/random.h>
51 #include <linux/jhash.h>
53 #include <asm/sections.h>
55 #include "lockdep_internals.h"
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/lock.h>
60 #ifdef CONFIG_PROVE_LOCKING
61 int prove_locking
= 1;
62 module_param(prove_locking
, int, 0644);
64 #define prove_locking 0
67 #ifdef CONFIG_LOCK_STAT
69 module_param(lock_stat
, int, 0644);
75 * lockdep_lock: protects the lockdep graph, the hashes and the
76 * class/list/hash allocators.
78 * This is one of the rare exceptions where it's justified
79 * to use a raw spinlock - we really dont want the spinlock
80 * code to recurse back into the lockdep code...
82 static arch_spinlock_t lockdep_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
84 static int graph_lock(void)
86 arch_spin_lock(&lockdep_lock
);
88 * Make sure that if another CPU detected a bug while
89 * walking the graph we dont change it (while the other
90 * CPU is busy printing out stuff with the graph lock
94 arch_spin_unlock(&lockdep_lock
);
97 /* prevent any recursions within lockdep from causing deadlocks */
98 current
->lockdep_recursion
++;
102 static inline int graph_unlock(void)
104 if (debug_locks
&& !arch_spin_is_locked(&lockdep_lock
)) {
106 * The lockdep graph lock isn't locked while we expect it to
107 * be, we're confused now, bye!
109 return DEBUG_LOCKS_WARN_ON(1);
112 current
->lockdep_recursion
--;
113 arch_spin_unlock(&lockdep_lock
);
118 * Turn lock debugging off and return with 0 if it was off already,
119 * and also release the graph lock:
121 static inline int debug_locks_off_graph_unlock(void)
123 int ret
= debug_locks_off();
125 arch_spin_unlock(&lockdep_lock
);
130 unsigned long nr_list_entries
;
131 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
134 * All data structures here are protected by the global debug_lock.
136 * Mutex key structs only get allocated, once during bootup, and never
137 * get freed - this significantly simplifies the debugging code.
139 unsigned long nr_lock_classes
;
140 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
142 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
144 if (!hlock
->class_idx
) {
146 * Someone passed in garbage, we give up.
148 DEBUG_LOCKS_WARN_ON(1);
151 return lock_classes
+ hlock
->class_idx
- 1;
154 #ifdef CONFIG_LOCK_STAT
155 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], cpu_lock_stats
);
157 static inline u64
lockstat_clock(void)
159 return local_clock();
162 static int lock_point(unsigned long points
[], unsigned long ip
)
166 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
167 if (points
[i
] == 0) {
178 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
183 if (time
< lt
->min
|| !lt
->nr
)
190 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
195 if (src
->max
> dst
->max
)
198 if (src
->min
< dst
->min
|| !dst
->nr
)
201 dst
->total
+= src
->total
;
205 struct lock_class_stats
lock_stats(struct lock_class
*class)
207 struct lock_class_stats stats
;
210 memset(&stats
, 0, sizeof(struct lock_class_stats
));
211 for_each_possible_cpu(cpu
) {
212 struct lock_class_stats
*pcs
=
213 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
215 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
216 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
218 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
219 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
221 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
222 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
224 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
225 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
227 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
228 stats
.bounces
[i
] += pcs
->bounces
[i
];
234 void clear_lock_stats(struct lock_class
*class)
238 for_each_possible_cpu(cpu
) {
239 struct lock_class_stats
*cpu_stats
=
240 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
242 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
244 memset(class->contention_point
, 0, sizeof(class->contention_point
));
245 memset(class->contending_point
, 0, sizeof(class->contending_point
));
248 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
250 return &get_cpu_var(cpu_lock_stats
)[class - lock_classes
];
253 static void put_lock_stats(struct lock_class_stats
*stats
)
255 put_cpu_var(cpu_lock_stats
);
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
);
273 put_lock_stats(stats
);
276 static inline void lock_release_holdtime(struct held_lock
*hlock
)
282 * We keep a global list of all lock classes. The list only grows,
283 * never shrinks. The list is only accessed with the lockdep
284 * spinlock lock held.
286 LIST_HEAD(all_lock_classes
);
289 * The lockdep classes are in a hash-table as well, for fast lookup:
291 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
292 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
293 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
294 #define classhashentry(key) (classhash_table + __classhashfn((key)))
296 static struct hlist_head classhash_table
[CLASSHASH_SIZE
];
299 * We put the lock dependency chains into a hash-table as well, to cache
302 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
303 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
304 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
305 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
307 static struct hlist_head chainhash_table
[CHAINHASH_SIZE
];
310 * The hash key of the lock dependency chains is a hash itself too:
311 * it's a hash of all locks taken up to that lock, including that lock.
312 * It's a 64-bit hash, because it's important for the keys to be
315 static inline u64
iterate_chain_key(u64 key
, u32 idx
)
317 u32 k0
= key
, k1
= key
>> 32;
319 __jhash_mix(idx
, k0
, k1
); /* Macro that modifies arguments! */
321 return k0
| (u64
)k1
<< 32;
324 void lockdep_off(void)
326 current
->lockdep_recursion
++;
328 EXPORT_SYMBOL(lockdep_off
);
330 void lockdep_on(void)
332 current
->lockdep_recursion
--;
334 EXPORT_SYMBOL(lockdep_on
);
337 * Debugging switches:
341 #define VERY_VERBOSE 0
344 # define HARDIRQ_VERBOSE 1
345 # define SOFTIRQ_VERBOSE 1
347 # define HARDIRQ_VERBOSE 0
348 # define SOFTIRQ_VERBOSE 0
351 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
353 * Quick filtering for interesting events:
355 static int class_filter(struct lock_class
*class)
359 if (class->name_version
== 1 &&
360 !strcmp(class->name
, "lockname"))
362 if (class->name_version
== 1 &&
363 !strcmp(class->name
, "&struct->lockfield"))
366 /* Filter everything else. 1 would be to allow everything else */
371 static int verbose(struct lock_class
*class)
374 return class_filter(class);
380 * Stack-trace: tightly packed array of stack backtrace
381 * addresses. Protected by the graph_lock.
383 unsigned long nr_stack_trace_entries
;
384 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
386 static void print_lockdep_off(const char *bug_msg
)
388 printk(KERN_DEBUG
"%s\n", bug_msg
);
389 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
390 #ifdef CONFIG_LOCK_STAT
391 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
395 static int save_trace(struct stack_trace
*trace
)
397 trace
->nr_entries
= 0;
398 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
399 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
403 save_stack_trace(trace
);
406 * Some daft arches put -1 at the end to indicate its a full trace.
408 * <rant> this is buggy anyway, since it takes a whole extra entry so a
409 * complete trace that maxes out the entries provided will be reported
410 * as incomplete, friggin useless </rant>
412 if (trace
->nr_entries
!= 0 &&
413 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
416 trace
->max_entries
= trace
->nr_entries
;
418 nr_stack_trace_entries
+= trace
->nr_entries
;
420 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
421 if (!debug_locks_off_graph_unlock())
424 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
433 unsigned int nr_hardirq_chains
;
434 unsigned int nr_softirq_chains
;
435 unsigned int nr_process_chains
;
436 unsigned int max_lockdep_depth
;
438 #ifdef CONFIG_DEBUG_LOCKDEP
440 * Various lockdep statistics:
442 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
449 #define __USAGE(__STATE) \
450 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
451 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
452 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
453 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
455 static const char *usage_str
[] =
457 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
458 #include "lockdep_states.h"
460 [LOCK_USED
] = "INITIAL USE",
463 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
465 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
468 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
473 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
477 if (class->usage_mask
& lock_flag(bit
+ 2))
479 if (class->usage_mask
& lock_flag(bit
)) {
481 if (class->usage_mask
& lock_flag(bit
+ 2))
488 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
492 #define LOCKDEP_STATE(__STATE) \
493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
495 #include "lockdep_states.h"
501 static void __print_lock_name(struct lock_class
*class)
503 char str
[KSYM_NAME_LEN
];
508 name
= __get_key_name(class->key
, str
);
509 printk(KERN_CONT
"%s", name
);
511 printk(KERN_CONT
"%s", name
);
512 if (class->name_version
> 1)
513 printk(KERN_CONT
"#%d", class->name_version
);
515 printk(KERN_CONT
"/%d", class->subclass
);
519 static void print_lock_name(struct lock_class
*class)
521 char usage
[LOCK_USAGE_CHARS
];
523 get_usage_chars(class, usage
);
525 printk(KERN_CONT
" (");
526 __print_lock_name(class);
527 printk(KERN_CONT
"){%s}", usage
);
530 static void print_lockdep_cache(struct lockdep_map
*lock
)
533 char str
[KSYM_NAME_LEN
];
537 name
= __get_key_name(lock
->key
->subkeys
, str
);
539 printk(KERN_CONT
"%s", name
);
542 static void print_lock(struct held_lock
*hlock
)
545 * We can be called locklessly through debug_show_all_locks() so be
546 * extra careful, the hlock might have been released and cleared.
548 unsigned int class_idx
= hlock
->class_idx
;
550 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
553 if (!class_idx
|| (class_idx
- 1) >= MAX_LOCKDEP_KEYS
) {
554 printk(KERN_CONT
"<RELEASED>\n");
558 print_lock_name(lock_classes
+ class_idx
- 1);
559 printk(KERN_CONT
", at: [<%p>] %pS\n",
560 (void *)hlock
->acquire_ip
, (void *)hlock
->acquire_ip
);
563 static void lockdep_print_held_locks(struct task_struct
*curr
)
565 int i
, depth
= curr
->lockdep_depth
;
568 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
571 printk("%d lock%s held by %s/%d:\n",
572 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
574 for (i
= 0; i
< depth
; i
++) {
576 print_lock(curr
->held_locks
+ i
);
580 static void print_kernel_ident(void)
582 printk("%s %.*s %s\n", init_utsname()->release
,
583 (int)strcspn(init_utsname()->version
, " "),
584 init_utsname()->version
,
588 static int very_verbose(struct lock_class
*class)
591 return class_filter(class);
597 * Is this the address of a static object:
600 static int static_obj(void *obj
)
602 unsigned long start
= (unsigned long) &_stext
,
603 end
= (unsigned long) &_end
,
604 addr
= (unsigned long) obj
;
609 if ((addr
>= start
) && (addr
< end
))
612 if (arch_is_kernel_data(addr
))
616 * in-kernel percpu var?
618 if (is_kernel_percpu_address(addr
))
622 * module static or percpu var?
624 return is_module_address(addr
) || is_module_percpu_address(addr
);
629 * To make lock name printouts unique, we calculate a unique
630 * class->name_version generation counter:
632 static int count_matching_names(struct lock_class
*new_class
)
634 struct lock_class
*class;
637 if (!new_class
->name
)
640 list_for_each_entry_rcu(class, &all_lock_classes
, lock_entry
) {
641 if (new_class
->key
- new_class
->subclass
== class->key
)
642 return class->name_version
;
643 if (class->name
&& !strcmp(class->name
, new_class
->name
))
644 count
= max(count
, class->name_version
);
651 * Register a lock's class in the hash-table, if the class is not present
652 * yet. Otherwise we look it up. We cache the result in the lock object
653 * itself, so actual lookup of the hash should be once per lock object.
655 static inline struct lock_class
*
656 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
658 struct lockdep_subclass_key
*key
;
659 struct hlist_head
*hash_head
;
660 struct lock_class
*class;
661 bool is_static
= false;
663 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
666 "BUG: looking up invalid subclass: %u\n", subclass
);
668 "turning off the locking correctness validator.\n");
674 * Static locks do not have their class-keys yet - for them the key
675 * is the lock object itself. If the lock is in the per cpu area,
676 * the canonical address of the lock (per cpu offset removed) is
679 if (unlikely(!lock
->key
)) {
680 unsigned long can_addr
, addr
= (unsigned long)lock
;
682 if (__is_kernel_percpu_address(addr
, &can_addr
))
683 lock
->key
= (void *)can_addr
;
684 else if (__is_module_percpu_address(addr
, &can_addr
))
685 lock
->key
= (void *)can_addr
;
686 else if (static_obj(lock
))
687 lock
->key
= (void *)lock
;
689 return ERR_PTR(-EINVAL
);
694 * NOTE: the class-key must be unique. For dynamic locks, a static
695 * lock_class_key variable is passed in through the mutex_init()
696 * (or spin_lock_init()) call - which acts as the key. For static
697 * locks we use the lock object itself as the key.
699 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
700 sizeof(struct lockdep_map
));
702 key
= lock
->key
->subkeys
+ subclass
;
704 hash_head
= classhashentry(key
);
707 * We do an RCU walk of the hash, see lockdep_free_key_range().
709 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
712 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
713 if (class->key
== key
) {
715 * Huh! same key, different name? Did someone trample
716 * on some memory? We're most confused.
718 WARN_ON_ONCE(class->name
!= lock
->name
);
723 return is_static
|| static_obj(lock
->key
) ? NULL
: ERR_PTR(-EINVAL
);
727 * Register a lock's class in the hash-table, if the class is not present
728 * yet. Otherwise we look it up. We cache the result in the lock object
729 * itself, so actual lookup of the hash should be once per lock object.
731 static struct lock_class
*
732 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
734 struct lockdep_subclass_key
*key
;
735 struct hlist_head
*hash_head
;
736 struct lock_class
*class;
738 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
740 class = look_up_lock_class(lock
, subclass
);
741 if (likely(!IS_ERR_OR_NULL(class)))
742 goto out_set_class_cache
;
745 * Debug-check: all keys must be persistent!
749 printk("INFO: trying to register non-static key.\n");
750 printk("the code is fine but needs lockdep annotation.\n");
751 printk("turning off the locking correctness validator.\n");
756 key
= lock
->key
->subkeys
+ subclass
;
757 hash_head
= classhashentry(key
);
763 * We have to do the hash-walk again, to avoid races
766 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
767 if (class->key
== key
)
772 * Allocate a new key from the static array, and add it to
775 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
776 if (!debug_locks_off_graph_unlock()) {
780 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
784 class = lock_classes
+ nr_lock_classes
++;
785 debug_atomic_inc(nr_unused_locks
);
787 class->name
= lock
->name
;
788 class->subclass
= subclass
;
789 INIT_LIST_HEAD(&class->lock_entry
);
790 INIT_LIST_HEAD(&class->locks_before
);
791 INIT_LIST_HEAD(&class->locks_after
);
792 class->name_version
= count_matching_names(class);
794 * We use RCU's safe list-add method to make
795 * parallel walking of the hash-list safe:
797 hlist_add_head_rcu(&class->hash_entry
, hash_head
);
799 * Add it to the global list of classes:
801 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
803 if (verbose(class)) {
806 printk("\nnew class %p: %s", class->key
, class->name
);
807 if (class->name_version
> 1)
808 printk(KERN_CONT
"#%d", class->name_version
);
809 printk(KERN_CONT
"\n");
820 if (!subclass
|| force
)
821 lock
->class_cache
[0] = class;
822 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
823 lock
->class_cache
[subclass
] = class;
826 * Hash collision, did we smoke some? We found a class with a matching
827 * hash but the subclass -- which is hashed in -- didn't match.
829 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
835 #ifdef CONFIG_PROVE_LOCKING
837 * Allocate a lockdep entry. (assumes the graph_lock held, returns
838 * with NULL on failure)
840 static struct lock_list
*alloc_list_entry(void)
842 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
843 if (!debug_locks_off_graph_unlock())
846 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
850 return list_entries
+ nr_list_entries
++;
854 * Add a new dependency to the head of the list:
856 static int add_lock_to_list(struct lock_class
*this, struct list_head
*head
,
857 unsigned long ip
, int distance
,
858 struct stack_trace
*trace
)
860 struct lock_list
*entry
;
862 * Lock not present yet - get a new dependency struct and
863 * add it to the list:
865 entry
= alloc_list_entry();
870 entry
->distance
= distance
;
871 entry
->trace
= *trace
;
873 * Both allocation and removal are done under the graph lock; but
874 * iteration is under RCU-sched; see look_up_lock_class() and
875 * lockdep_free_key_range().
877 list_add_tail_rcu(&entry
->entry
, head
);
883 * For good efficiency of modular, we use power of 2
885 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
886 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
889 * The circular_queue and helpers is used to implement the
890 * breadth-first search(BFS)algorithem, by which we can build
891 * the shortest path from the next lock to be acquired to the
892 * previous held lock if there is a circular between them.
894 struct circular_queue
{
895 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
896 unsigned int front
, rear
;
899 static struct circular_queue lock_cq
;
901 unsigned int max_bfs_queue_depth
;
903 static unsigned int lockdep_dependency_gen_id
;
905 static inline void __cq_init(struct circular_queue
*cq
)
907 cq
->front
= cq
->rear
= 0;
908 lockdep_dependency_gen_id
++;
911 static inline int __cq_empty(struct circular_queue
*cq
)
913 return (cq
->front
== cq
->rear
);
916 static inline int __cq_full(struct circular_queue
*cq
)
918 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
921 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
926 cq
->element
[cq
->rear
] = elem
;
927 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
931 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
936 *elem
= cq
->element
[cq
->front
];
937 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
941 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
943 return (cq
->rear
- cq
->front
) & CQ_MASK
;
946 static inline void mark_lock_accessed(struct lock_list
*lock
,
947 struct lock_list
*parent
)
951 nr
= lock
- list_entries
;
952 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
953 lock
->parent
= parent
;
954 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
957 static inline unsigned long lock_accessed(struct lock_list
*lock
)
961 nr
= lock
- list_entries
;
962 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
963 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
966 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
968 return child
->parent
;
971 static inline int get_lock_depth(struct lock_list
*child
)
974 struct lock_list
*parent
;
976 while ((parent
= get_lock_parent(child
))) {
983 static int __bfs(struct lock_list
*source_entry
,
985 int (*match
)(struct lock_list
*entry
, void *data
),
986 struct lock_list
**target_entry
,
989 struct lock_list
*entry
;
990 struct list_head
*head
;
991 struct circular_queue
*cq
= &lock_cq
;
994 if (match(source_entry
, data
)) {
995 *target_entry
= source_entry
;
1001 head
= &source_entry
->class->locks_after
;
1003 head
= &source_entry
->class->locks_before
;
1005 if (list_empty(head
))
1009 __cq_enqueue(cq
, (unsigned long)source_entry
);
1011 while (!__cq_empty(cq
)) {
1012 struct lock_list
*lock
;
1014 __cq_dequeue(cq
, (unsigned long *)&lock
);
1022 head
= &lock
->class->locks_after
;
1024 head
= &lock
->class->locks_before
;
1026 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1028 list_for_each_entry_rcu(entry
, head
, entry
) {
1029 if (!lock_accessed(entry
)) {
1030 unsigned int cq_depth
;
1031 mark_lock_accessed(entry
, lock
);
1032 if (match(entry
, data
)) {
1033 *target_entry
= entry
;
1038 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1042 cq_depth
= __cq_get_elem_count(cq
);
1043 if (max_bfs_queue_depth
< cq_depth
)
1044 max_bfs_queue_depth
= cq_depth
;
1052 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1054 int (*match
)(struct lock_list
*entry
, void *data
),
1055 struct lock_list
**target_entry
)
1057 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1061 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1063 int (*match
)(struct lock_list
*entry
, void *data
),
1064 struct lock_list
**target_entry
)
1066 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1071 * Recursive, forwards-direction lock-dependency checking, used for
1072 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1077 * Print a dependency chain entry (this is only done when a deadlock
1078 * has been detected):
1081 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1083 if (debug_locks_silent
)
1085 printk("\n-> #%u", depth
);
1086 print_lock_name(target
->class);
1087 printk(KERN_CONT
":\n");
1088 print_stack_trace(&target
->trace
, 6);
1094 print_circular_lock_scenario(struct held_lock
*src
,
1095 struct held_lock
*tgt
,
1096 struct lock_list
*prt
)
1098 struct lock_class
*source
= hlock_class(src
);
1099 struct lock_class
*target
= hlock_class(tgt
);
1100 struct lock_class
*parent
= prt
->class;
1103 * A direct locking problem where unsafe_class lock is taken
1104 * directly by safe_class lock, then all we need to show
1105 * is the deadlock scenario, as it is obvious that the
1106 * unsafe lock is taken under the safe lock.
1108 * But if there is a chain instead, where the safe lock takes
1109 * an intermediate lock (middle_class) where this lock is
1110 * not the same as the safe lock, then the lock chain is
1111 * used to describe the problem. Otherwise we would need
1112 * to show a different CPU case for each link in the chain
1113 * from the safe_class lock to the unsafe_class lock.
1115 if (parent
!= source
) {
1116 printk("Chain exists of:\n ");
1117 __print_lock_name(source
);
1118 printk(KERN_CONT
" --> ");
1119 __print_lock_name(parent
);
1120 printk(KERN_CONT
" --> ");
1121 __print_lock_name(target
);
1122 printk(KERN_CONT
"\n\n");
1125 printk(" Possible unsafe locking scenario:\n\n");
1126 printk(" CPU0 CPU1\n");
1127 printk(" ---- ----\n");
1129 __print_lock_name(target
);
1130 printk(KERN_CONT
");\n");
1132 __print_lock_name(parent
);
1133 printk(KERN_CONT
");\n");
1135 __print_lock_name(target
);
1136 printk(KERN_CONT
");\n");
1138 __print_lock_name(source
);
1139 printk(KERN_CONT
");\n");
1140 printk("\n *** DEADLOCK ***\n\n");
1144 * When a circular dependency is detected, print the
1148 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1149 struct held_lock
*check_src
,
1150 struct held_lock
*check_tgt
)
1152 struct task_struct
*curr
= current
;
1154 if (debug_locks_silent
)
1158 pr_warn("======================================================\n");
1159 pr_warn("WARNING: possible circular locking dependency detected\n");
1160 print_kernel_ident();
1161 pr_warn("------------------------------------------------------\n");
1162 pr_warn("%s/%d is trying to acquire lock:\n",
1163 curr
->comm
, task_pid_nr(curr
));
1164 print_lock(check_src
);
1166 pr_warn("\nbut task is already holding lock:\n");
1168 print_lock(check_tgt
);
1169 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1170 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1172 print_circular_bug_entry(entry
, depth
);
1177 static inline int class_equal(struct lock_list
*entry
, void *data
)
1179 return entry
->class == data
;
1182 static noinline
int print_circular_bug(struct lock_list
*this,
1183 struct lock_list
*target
,
1184 struct held_lock
*check_src
,
1185 struct held_lock
*check_tgt
,
1186 struct stack_trace
*trace
)
1188 struct task_struct
*curr
= current
;
1189 struct lock_list
*parent
;
1190 struct lock_list
*first_parent
;
1193 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1196 if (!save_trace(&this->trace
))
1199 depth
= get_lock_depth(target
);
1201 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1203 parent
= get_lock_parent(target
);
1204 first_parent
= parent
;
1207 print_circular_bug_entry(parent
, --depth
);
1208 parent
= get_lock_parent(parent
);
1211 printk("\nother info that might help us debug this:\n\n");
1212 print_circular_lock_scenario(check_src
, check_tgt
,
1215 lockdep_print_held_locks(curr
);
1217 printk("\nstack backtrace:\n");
1223 static noinline
int print_bfs_bug(int ret
)
1225 if (!debug_locks_off_graph_unlock())
1229 * Breadth-first-search failed, graph got corrupted?
1231 WARN(1, "lockdep bfs error:%d\n", ret
);
1236 static int noop_count(struct lock_list
*entry
, void *data
)
1238 (*(unsigned long *)data
)++;
1242 static unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1244 unsigned long count
= 0;
1245 struct lock_list
*uninitialized_var(target_entry
);
1247 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1251 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1253 unsigned long ret
, flags
;
1254 struct lock_list
this;
1259 local_irq_save(flags
);
1260 arch_spin_lock(&lockdep_lock
);
1261 ret
= __lockdep_count_forward_deps(&this);
1262 arch_spin_unlock(&lockdep_lock
);
1263 local_irq_restore(flags
);
1268 static unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1270 unsigned long count
= 0;
1271 struct lock_list
*uninitialized_var(target_entry
);
1273 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1278 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1280 unsigned long ret
, flags
;
1281 struct lock_list
this;
1286 local_irq_save(flags
);
1287 arch_spin_lock(&lockdep_lock
);
1288 ret
= __lockdep_count_backward_deps(&this);
1289 arch_spin_unlock(&lockdep_lock
);
1290 local_irq_restore(flags
);
1296 * Prove that the dependency graph starting at <entry> can not
1297 * lead to <target>. Print an error and return 0 if it does.
1300 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1301 struct lock_list
**target_entry
)
1305 debug_atomic_inc(nr_cyclic_checks
);
1307 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1313 check_redundant(struct lock_list
*root
, struct lock_class
*target
,
1314 struct lock_list
**target_entry
)
1318 debug_atomic_inc(nr_redundant_checks
);
1320 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1325 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1327 * Forwards and backwards subgraph searching, for the purposes of
1328 * proving that two subgraphs can be connected by a new dependency
1329 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1332 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1334 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1340 * Find a node in the forwards-direction dependency sub-graph starting
1341 * at @root->class that matches @bit.
1343 * Return 0 if such a node exists in the subgraph, and put that node
1344 * into *@target_entry.
1346 * Return 1 otherwise and keep *@target_entry unchanged.
1347 * Return <0 on error.
1350 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1351 struct lock_list
**target_entry
)
1355 debug_atomic_inc(nr_find_usage_forwards_checks
);
1357 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1363 * Find a node in the backwards-direction dependency sub-graph starting
1364 * at @root->class that matches @bit.
1366 * Return 0 if such a node exists in the subgraph, and put that node
1367 * into *@target_entry.
1369 * Return 1 otherwise and keep *@target_entry unchanged.
1370 * Return <0 on error.
1373 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1374 struct lock_list
**target_entry
)
1378 debug_atomic_inc(nr_find_usage_backwards_checks
);
1380 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1385 static void print_lock_class_header(struct lock_class
*class, int depth
)
1389 printk("%*s->", depth
, "");
1390 print_lock_name(class);
1391 printk(KERN_CONT
" ops: %lu", class->ops
);
1392 printk(KERN_CONT
" {\n");
1394 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1395 if (class->usage_mask
& (1 << bit
)) {
1398 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1399 len
+= printk(KERN_CONT
" at:\n");
1400 print_stack_trace(class->usage_traces
+ bit
, len
);
1403 printk("%*s }\n", depth
, "");
1405 printk("%*s ... key at: [<%p>] %pS\n",
1406 depth
, "", class->key
, class->key
);
1410 * printk the shortest lock dependencies from @start to @end in reverse order:
1413 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1414 struct lock_list
*root
)
1416 struct lock_list
*entry
= leaf
;
1419 /*compute depth from generated tree by BFS*/
1420 depth
= get_lock_depth(leaf
);
1423 print_lock_class_header(entry
->class, depth
);
1424 printk("%*s ... acquired at:\n", depth
, "");
1425 print_stack_trace(&entry
->trace
, 2);
1428 if (depth
== 0 && (entry
!= root
)) {
1429 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1433 entry
= get_lock_parent(entry
);
1435 } while (entry
&& (depth
>= 0));
1441 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1442 struct lock_list
*unsafe_entry
,
1443 struct lock_class
*prev_class
,
1444 struct lock_class
*next_class
)
1446 struct lock_class
*safe_class
= safe_entry
->class;
1447 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1448 struct lock_class
*middle_class
= prev_class
;
1450 if (middle_class
== safe_class
)
1451 middle_class
= next_class
;
1454 * A direct locking problem where unsafe_class lock is taken
1455 * directly by safe_class lock, then all we need to show
1456 * is the deadlock scenario, as it is obvious that the
1457 * unsafe lock is taken under the safe lock.
1459 * But if there is a chain instead, where the safe lock takes
1460 * an intermediate lock (middle_class) where this lock is
1461 * not the same as the safe lock, then the lock chain is
1462 * used to describe the problem. Otherwise we would need
1463 * to show a different CPU case for each link in the chain
1464 * from the safe_class lock to the unsafe_class lock.
1466 if (middle_class
!= unsafe_class
) {
1467 printk("Chain exists of:\n ");
1468 __print_lock_name(safe_class
);
1469 printk(KERN_CONT
" --> ");
1470 __print_lock_name(middle_class
);
1471 printk(KERN_CONT
" --> ");
1472 __print_lock_name(unsafe_class
);
1473 printk(KERN_CONT
"\n\n");
1476 printk(" Possible interrupt unsafe locking scenario:\n\n");
1477 printk(" CPU0 CPU1\n");
1478 printk(" ---- ----\n");
1480 __print_lock_name(unsafe_class
);
1481 printk(KERN_CONT
");\n");
1482 printk(" local_irq_disable();\n");
1484 __print_lock_name(safe_class
);
1485 printk(KERN_CONT
");\n");
1487 __print_lock_name(middle_class
);
1488 printk(KERN_CONT
");\n");
1489 printk(" <Interrupt>\n");
1491 __print_lock_name(safe_class
);
1492 printk(KERN_CONT
");\n");
1493 printk("\n *** DEADLOCK ***\n\n");
1497 print_bad_irq_dependency(struct task_struct
*curr
,
1498 struct lock_list
*prev_root
,
1499 struct lock_list
*next_root
,
1500 struct lock_list
*backwards_entry
,
1501 struct lock_list
*forwards_entry
,
1502 struct held_lock
*prev
,
1503 struct held_lock
*next
,
1504 enum lock_usage_bit bit1
,
1505 enum lock_usage_bit bit2
,
1506 const char *irqclass
)
1508 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1512 pr_warn("=====================================================\n");
1513 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1514 irqclass
, irqclass
);
1515 print_kernel_ident();
1516 pr_warn("-----------------------------------------------------\n");
1517 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1518 curr
->comm
, task_pid_nr(curr
),
1519 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1520 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1521 curr
->hardirqs_enabled
,
1522 curr
->softirqs_enabled
);
1525 pr_warn("\nand this task is already holding:\n");
1527 pr_warn("which would create a new lock dependency:\n");
1528 print_lock_name(hlock_class(prev
));
1530 print_lock_name(hlock_class(next
));
1533 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1535 print_lock_name(backwards_entry
->class);
1536 pr_warn("\n... which became %s-irq-safe at:\n", irqclass
);
1538 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1540 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass
);
1541 print_lock_name(forwards_entry
->class);
1542 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass
);
1545 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1547 pr_warn("\nother info that might help us debug this:\n\n");
1548 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1549 hlock_class(prev
), hlock_class(next
));
1551 lockdep_print_held_locks(curr
);
1553 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass
);
1554 if (!save_trace(&prev_root
->trace
))
1556 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1558 pr_warn("\nthe dependencies between the lock to be acquired");
1559 pr_warn(" and %s-irq-unsafe lock:\n", irqclass
);
1560 if (!save_trace(&next_root
->trace
))
1562 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1564 pr_warn("\nstack backtrace:\n");
1571 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1572 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1573 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1576 struct lock_list
this, that
;
1577 struct lock_list
*uninitialized_var(target_entry
);
1578 struct lock_list
*uninitialized_var(target_entry1
);
1582 this.class = hlock_class(prev
);
1583 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1585 return print_bfs_bug(ret
);
1590 that
.class = hlock_class(next
);
1591 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1593 return print_bfs_bug(ret
);
1597 return print_bad_irq_dependency(curr
, &this, &that
,
1598 target_entry
, target_entry1
,
1600 bit_backwards
, bit_forwards
, irqclass
);
1603 static const char *state_names
[] = {
1604 #define LOCKDEP_STATE(__STATE) \
1605 __stringify(__STATE),
1606 #include "lockdep_states.h"
1607 #undef LOCKDEP_STATE
1610 static const char *state_rnames
[] = {
1611 #define LOCKDEP_STATE(__STATE) \
1612 __stringify(__STATE)"-READ",
1613 #include "lockdep_states.h"
1614 #undef LOCKDEP_STATE
1617 static inline const char *state_name(enum lock_usage_bit bit
)
1619 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1622 static int exclusive_bit(int new_bit
)
1630 * bit 0 - write/read
1631 * bit 1 - used_in/enabled
1635 int state
= new_bit
& ~3;
1636 int dir
= new_bit
& 2;
1639 * keep state, bit flip the direction and strip read.
1641 return state
| (dir
^ 2);
1644 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1645 struct held_lock
*next
, enum lock_usage_bit bit
)
1648 * Prove that the new dependency does not connect a hardirq-safe
1649 * lock with a hardirq-unsafe lock - to achieve this we search
1650 * the backwards-subgraph starting at <prev>, and the
1651 * forwards-subgraph starting at <next>:
1653 if (!check_usage(curr
, prev
, next
, bit
,
1654 exclusive_bit(bit
), state_name(bit
)))
1660 * Prove that the new dependency does not connect a hardirq-safe-read
1661 * lock with a hardirq-unsafe lock - to achieve this we search
1662 * the backwards-subgraph starting at <prev>, and the
1663 * forwards-subgraph starting at <next>:
1665 if (!check_usage(curr
, prev
, next
, bit
,
1666 exclusive_bit(bit
), state_name(bit
)))
1673 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1674 struct held_lock
*next
)
1676 #define LOCKDEP_STATE(__STATE) \
1677 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1679 #include "lockdep_states.h"
1680 #undef LOCKDEP_STATE
1685 static void inc_chains(void)
1687 if (current
->hardirq_context
)
1688 nr_hardirq_chains
++;
1690 if (current
->softirq_context
)
1691 nr_softirq_chains
++;
1693 nr_process_chains
++;
1700 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1701 struct held_lock
*next
)
1706 static inline void inc_chains(void)
1708 nr_process_chains
++;
1714 print_deadlock_scenario(struct held_lock
*nxt
,
1715 struct held_lock
*prv
)
1717 struct lock_class
*next
= hlock_class(nxt
);
1718 struct lock_class
*prev
= hlock_class(prv
);
1720 printk(" Possible unsafe locking scenario:\n\n");
1724 __print_lock_name(prev
);
1725 printk(KERN_CONT
");\n");
1727 __print_lock_name(next
);
1728 printk(KERN_CONT
");\n");
1729 printk("\n *** DEADLOCK ***\n\n");
1730 printk(" May be due to missing lock nesting notation\n\n");
1734 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1735 struct held_lock
*next
)
1737 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1741 pr_warn("============================================\n");
1742 pr_warn("WARNING: possible recursive locking detected\n");
1743 print_kernel_ident();
1744 pr_warn("--------------------------------------------\n");
1745 pr_warn("%s/%d is trying to acquire lock:\n",
1746 curr
->comm
, task_pid_nr(curr
));
1748 pr_warn("\nbut task is already holding lock:\n");
1751 pr_warn("\nother info that might help us debug this:\n");
1752 print_deadlock_scenario(next
, prev
);
1753 lockdep_print_held_locks(curr
);
1755 pr_warn("\nstack backtrace:\n");
1762 * Check whether we are holding such a class already.
1764 * (Note that this has to be done separately, because the graph cannot
1765 * detect such classes of deadlocks.)
1767 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1770 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1771 struct lockdep_map
*next_instance
, int read
)
1773 struct held_lock
*prev
;
1774 struct held_lock
*nest
= NULL
;
1777 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1778 prev
= curr
->held_locks
+ i
;
1780 if (prev
->instance
== next
->nest_lock
)
1783 if (hlock_class(prev
) != hlock_class(next
))
1787 * Allow read-after-read recursion of the same
1788 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1790 if ((read
== 2) && prev
->read
)
1794 * We're holding the nest_lock, which serializes this lock's
1795 * nesting behaviour.
1800 return print_deadlock_bug(curr
, prev
, next
);
1806 * There was a chain-cache miss, and we are about to add a new dependency
1807 * to a previous lock. We recursively validate the following rules:
1809 * - would the adding of the <prev> -> <next> dependency create a
1810 * circular dependency in the graph? [== circular deadlock]
1812 * - does the new prev->next dependency connect any hardirq-safe lock
1813 * (in the full backwards-subgraph starting at <prev>) with any
1814 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1815 * <next>)? [== illegal lock inversion with hardirq contexts]
1817 * - does the new prev->next dependency connect any softirq-safe lock
1818 * (in the full backwards-subgraph starting at <prev>) with any
1819 * softirq-unsafe lock (in the full forwards-subgraph starting at
1820 * <next>)? [== illegal lock inversion with softirq contexts]
1822 * any of these scenarios could lead to a deadlock.
1824 * Then if all the validations pass, we add the forwards and backwards
1828 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1829 struct held_lock
*next
, int distance
, struct stack_trace
*trace
,
1830 int (*save
)(struct stack_trace
*trace
))
1832 struct lock_list
*uninitialized_var(target_entry
);
1833 struct lock_list
*entry
;
1834 struct lock_list
this;
1838 * Prove that the new <prev> -> <next> dependency would not
1839 * create a circular dependency in the graph. (We do this by
1840 * forward-recursing into the graph starting at <next>, and
1841 * checking whether we can reach <prev>.)
1843 * We are using global variables to control the recursion, to
1844 * keep the stackframe size of the recursive functions low:
1846 this.class = hlock_class(next
);
1848 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1849 if (unlikely(!ret
)) {
1850 if (!trace
->entries
) {
1852 * If @save fails here, the printing might trigger
1853 * a WARN but because of the !nr_entries it should
1854 * not do bad things.
1858 return print_circular_bug(&this, target_entry
, next
, prev
, trace
);
1860 else if (unlikely(ret
< 0))
1861 return print_bfs_bug(ret
);
1863 if (!check_prev_add_irq(curr
, prev
, next
))
1867 * For recursive read-locks we do all the dependency checks,
1868 * but we dont store read-triggered dependencies (only
1869 * write-triggered dependencies). This ensures that only the
1870 * write-side dependencies matter, and that if for example a
1871 * write-lock never takes any other locks, then the reads are
1872 * equivalent to a NOP.
1874 if (next
->read
== 2 || prev
->read
== 2)
1877 * Is the <prev> -> <next> dependency already present?
1879 * (this may occur even though this is a new chain: consider
1880 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1881 * chains - the second one will be new, but L1 already has
1882 * L2 added to its dependency list, due to the first chain.)
1884 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1885 if (entry
->class == hlock_class(next
)) {
1887 entry
->distance
= 1;
1893 * Is the <prev> -> <next> link redundant?
1895 this.class = hlock_class(prev
);
1897 ret
= check_redundant(&this, hlock_class(next
), &target_entry
);
1899 debug_atomic_inc(nr_redundant
);
1903 return print_bfs_bug(ret
);
1906 if (!trace
->entries
&& !save(trace
))
1910 * Ok, all validations passed, add the new lock
1911 * to the previous lock's dependency list:
1913 ret
= add_lock_to_list(hlock_class(next
),
1914 &hlock_class(prev
)->locks_after
,
1915 next
->acquire_ip
, distance
, trace
);
1920 ret
= add_lock_to_list(hlock_class(prev
),
1921 &hlock_class(next
)->locks_before
,
1922 next
->acquire_ip
, distance
, trace
);
1930 * Add the dependency to all directly-previous locks that are 'relevant'.
1931 * The ones that are relevant are (in increasing distance from curr):
1932 * all consecutive trylock entries and the final non-trylock entry - or
1933 * the end of this context's lock-chain - whichever comes first.
1936 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1938 int depth
= curr
->lockdep_depth
;
1939 struct held_lock
*hlock
;
1940 struct stack_trace trace
= {
1950 * Depth must not be zero for a non-head lock:
1955 * At least two relevant locks must exist for this
1958 if (curr
->held_locks
[depth
].irq_context
!=
1959 curr
->held_locks
[depth
-1].irq_context
)
1963 int distance
= curr
->lockdep_depth
- depth
+ 1;
1964 hlock
= curr
->held_locks
+ depth
- 1;
1967 * Only non-recursive-read entries get new dependencies
1970 if (hlock
->read
!= 2 && hlock
->check
) {
1971 int ret
= check_prev_add(curr
, hlock
, next
, distance
, &trace
, save_trace
);
1976 * Stop after the first non-trylock entry,
1977 * as non-trylock entries have added their
1978 * own direct dependencies already, so this
1979 * lock is connected to them indirectly:
1981 if (!hlock
->trylock
)
1987 * End of lock-stack?
1992 * Stop the search if we cross into another context:
1994 if (curr
->held_locks
[depth
].irq_context
!=
1995 curr
->held_locks
[depth
-1].irq_context
)
2000 if (!debug_locks_off_graph_unlock())
2004 * Clearly we all shouldn't be here, but since we made it we
2005 * can reliable say we messed up our state. See the above two
2006 * gotos for reasons why we could possibly end up here.
2013 unsigned long nr_lock_chains
;
2014 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
2015 int nr_chain_hlocks
;
2016 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
2018 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
2020 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
2024 * Returns the index of the first held_lock of the current chain
2026 static inline int get_first_held_lock(struct task_struct
*curr
,
2027 struct held_lock
*hlock
)
2030 struct held_lock
*hlock_curr
;
2032 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2033 hlock_curr
= curr
->held_locks
+ i
;
2034 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2042 #ifdef CONFIG_DEBUG_LOCKDEP
2044 * Returns the next chain_key iteration
2046 static u64
print_chain_key_iteration(int class_idx
, u64 chain_key
)
2048 u64 new_chain_key
= iterate_chain_key(chain_key
, class_idx
);
2050 printk(" class_idx:%d -> chain_key:%016Lx",
2052 (unsigned long long)new_chain_key
);
2053 return new_chain_key
;
2057 print_chain_keys_held_locks(struct task_struct
*curr
, struct held_lock
*hlock_next
)
2059 struct held_lock
*hlock
;
2061 int depth
= curr
->lockdep_depth
;
2064 printk("depth: %u\n", depth
+ 1);
2065 for (i
= get_first_held_lock(curr
, hlock_next
); i
< depth
; i
++) {
2066 hlock
= curr
->held_locks
+ i
;
2067 chain_key
= print_chain_key_iteration(hlock
->class_idx
, chain_key
);
2072 print_chain_key_iteration(hlock_next
->class_idx
, chain_key
);
2073 print_lock(hlock_next
);
2076 static void print_chain_keys_chain(struct lock_chain
*chain
)
2082 printk("depth: %u\n", chain
->depth
);
2083 for (i
= 0; i
< chain
->depth
; i
++) {
2084 class_id
= chain_hlocks
[chain
->base
+ i
];
2085 chain_key
= print_chain_key_iteration(class_id
+ 1, chain_key
);
2087 print_lock_name(lock_classes
+ class_id
);
2092 static void print_collision(struct task_struct
*curr
,
2093 struct held_lock
*hlock_next
,
2094 struct lock_chain
*chain
)
2097 pr_warn("============================\n");
2098 pr_warn("WARNING: chain_key collision\n");
2099 print_kernel_ident();
2100 pr_warn("----------------------------\n");
2101 pr_warn("%s/%d: ", current
->comm
, task_pid_nr(current
));
2102 pr_warn("Hash chain already cached but the contents don't match!\n");
2104 pr_warn("Held locks:");
2105 print_chain_keys_held_locks(curr
, hlock_next
);
2107 pr_warn("Locks in cached chain:");
2108 print_chain_keys_chain(chain
);
2110 pr_warn("\nstack backtrace:\n");
2116 * Checks whether the chain and the current held locks are consistent
2117 * in depth and also in content. If they are not it most likely means
2118 * that there was a collision during the calculation of the chain_key.
2119 * Returns: 0 not passed, 1 passed
2121 static int check_no_collision(struct task_struct
*curr
,
2122 struct held_lock
*hlock
,
2123 struct lock_chain
*chain
)
2125 #ifdef CONFIG_DEBUG_LOCKDEP
2128 i
= get_first_held_lock(curr
, hlock
);
2130 if (DEBUG_LOCKS_WARN_ON(chain
->depth
!= curr
->lockdep_depth
- (i
- 1))) {
2131 print_collision(curr
, hlock
, chain
);
2135 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2136 id
= curr
->held_locks
[i
].class_idx
- 1;
2138 if (DEBUG_LOCKS_WARN_ON(chain_hlocks
[chain
->base
+ j
] != id
)) {
2139 print_collision(curr
, hlock
, chain
);
2148 * This is for building a chain between just two different classes,
2149 * instead of adding a new hlock upon current, which is done by
2150 * add_chain_cache().
2152 * This can be called in any context with two classes, while
2153 * add_chain_cache() must be done within the lock owener's context
2154 * since it uses hlock which might be racy in another context.
2156 static inline int add_chain_cache_classes(unsigned int prev
,
2158 unsigned int irq_context
,
2161 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2162 struct lock_chain
*chain
;
2165 * Allocate a new chain entry from the static array, and add
2170 * We might need to take the graph lock, ensure we've got IRQs
2171 * disabled to make this an IRQ-safe lock.. for recursion reasons
2172 * lockdep won't complain about its own locking errors.
2174 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2177 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2178 if (!debug_locks_off_graph_unlock())
2181 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2186 chain
= lock_chains
+ nr_lock_chains
++;
2187 chain
->chain_key
= chain_key
;
2188 chain
->irq_context
= irq_context
;
2190 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2191 chain
->base
= nr_chain_hlocks
;
2192 nr_chain_hlocks
+= chain
->depth
;
2193 chain_hlocks
[chain
->base
] = prev
- 1;
2194 chain_hlocks
[chain
->base
+ 1] = next
-1;
2196 #ifdef CONFIG_DEBUG_LOCKDEP
2198 * Important for check_no_collision().
2201 if (!debug_locks_off_graph_unlock())
2204 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2210 hlist_add_head_rcu(&chain
->entry
, hash_head
);
2211 debug_atomic_inc(chain_lookup_misses
);
2218 * Adds a dependency chain into chain hashtable. And must be called with
2221 * Return 0 if fail, and graph_lock is released.
2222 * Return 1 if succeed, with graph_lock held.
2224 static inline int add_chain_cache(struct task_struct
*curr
,
2225 struct held_lock
*hlock
,
2228 struct lock_class
*class = hlock_class(hlock
);
2229 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2230 struct lock_chain
*chain
;
2234 * Allocate a new chain entry from the static array, and add
2239 * We might need to take the graph lock, ensure we've got IRQs
2240 * disabled to make this an IRQ-safe lock.. for recursion reasons
2241 * lockdep won't complain about its own locking errors.
2243 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2246 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2247 if (!debug_locks_off_graph_unlock())
2250 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2254 chain
= lock_chains
+ nr_lock_chains
++;
2255 chain
->chain_key
= chain_key
;
2256 chain
->irq_context
= hlock
->irq_context
;
2257 i
= get_first_held_lock(curr
, hlock
);
2258 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2260 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks
));
2261 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr
->held_locks
));
2262 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks
[0])) <= ARRAY_SIZE(lock_classes
));
2264 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2265 chain
->base
= nr_chain_hlocks
;
2266 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2267 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2268 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2270 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2273 if (nr_chain_hlocks
< MAX_LOCKDEP_CHAIN_HLOCKS
)
2274 nr_chain_hlocks
+= chain
->depth
;
2276 #ifdef CONFIG_DEBUG_LOCKDEP
2278 * Important for check_no_collision().
2280 if (unlikely(nr_chain_hlocks
> MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2281 if (!debug_locks_off_graph_unlock())
2284 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2290 hlist_add_head_rcu(&chain
->entry
, hash_head
);
2291 debug_atomic_inc(chain_lookup_misses
);
2298 * Look up a dependency chain.
2300 static inline struct lock_chain
*lookup_chain_cache(u64 chain_key
)
2302 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2303 struct lock_chain
*chain
;
2306 * We can walk it lock-free, because entries only get added
2309 hlist_for_each_entry_rcu(chain
, hash_head
, entry
) {
2310 if (chain
->chain_key
== chain_key
) {
2311 debug_atomic_inc(chain_lookup_hits
);
2319 * If the key is not present yet in dependency chain cache then
2320 * add it and return 1 - in this case the new dependency chain is
2321 * validated. If the key is already hashed, return 0.
2322 * (On return with 1 graph_lock is held.)
2324 static inline int lookup_chain_cache_add(struct task_struct
*curr
,
2325 struct held_lock
*hlock
,
2328 struct lock_class
*class = hlock_class(hlock
);
2329 struct lock_chain
*chain
= lookup_chain_cache(chain_key
);
2333 if (!check_no_collision(curr
, hlock
, chain
))
2336 if (very_verbose(class)) {
2337 printk("\nhash chain already cached, key: "
2338 "%016Lx tail class: [%p] %s\n",
2339 (unsigned long long)chain_key
,
2340 class->key
, class->name
);
2346 if (very_verbose(class)) {
2347 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2348 (unsigned long long)chain_key
, class->key
, class->name
);
2355 * We have to walk the chain again locked - to avoid duplicates:
2357 chain
= lookup_chain_cache(chain_key
);
2363 if (!add_chain_cache(curr
, hlock
, chain_key
))
2369 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2370 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2373 * Trylock needs to maintain the stack of held locks, but it
2374 * does not add new dependencies, because trylock can be done
2377 * We look up the chain_key and do the O(N^2) check and update of
2378 * the dependencies only if this is a new dependency chain.
2379 * (If lookup_chain_cache_add() return with 1 it acquires
2380 * graph_lock for us)
2382 if (!hlock
->trylock
&& hlock
->check
&&
2383 lookup_chain_cache_add(curr
, hlock
, chain_key
)) {
2385 * Check whether last held lock:
2387 * - is irq-safe, if this lock is irq-unsafe
2388 * - is softirq-safe, if this lock is hardirq-unsafe
2390 * And check whether the new lock's dependency graph
2391 * could lead back to the previous lock.
2393 * any of these scenarios could lead to a deadlock. If
2396 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2401 * Mark recursive read, as we jump over it when
2402 * building dependencies (just like we jump over
2408 * Add dependency only if this lock is not the head
2409 * of the chain, and if it's not a secondary read-lock:
2411 if (!chain_head
&& ret
!= 2) {
2412 if (!check_prevs_add(curr
, hlock
))
2418 /* after lookup_chain_cache_add(): */
2419 if (unlikely(!debug_locks
))
2426 static inline int validate_chain(struct task_struct
*curr
,
2427 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2428 int chain_head
, u64 chain_key
)
2435 * We are building curr_chain_key incrementally, so double-check
2436 * it from scratch, to make sure that it's done correctly:
2438 static void check_chain_key(struct task_struct
*curr
)
2440 #ifdef CONFIG_DEBUG_LOCKDEP
2441 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2445 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2446 hlock
= curr
->held_locks
+ i
;
2447 if (chain_key
!= hlock
->prev_chain_key
) {
2450 * We got mighty confused, our chain keys don't match
2451 * with what we expect, someone trample on our task state?
2453 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2454 curr
->lockdep_depth
, i
,
2455 (unsigned long long)chain_key
,
2456 (unsigned long long)hlock
->prev_chain_key
);
2460 * Whoops ran out of static storage again?
2462 if (DEBUG_LOCKS_WARN_ON(hlock
->class_idx
> MAX_LOCKDEP_KEYS
))
2465 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2466 hlock
->irq_context
))
2468 chain_key
= iterate_chain_key(chain_key
, hlock
->class_idx
);
2471 if (chain_key
!= curr
->curr_chain_key
) {
2474 * More smoking hash instead of calculating it, damn see these
2475 * numbers float.. I bet that a pink elephant stepped on my memory.
2477 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2478 curr
->lockdep_depth
, i
,
2479 (unsigned long long)chain_key
,
2480 (unsigned long long)curr
->curr_chain_key
);
2486 print_usage_bug_scenario(struct held_lock
*lock
)
2488 struct lock_class
*class = hlock_class(lock
);
2490 printk(" Possible unsafe locking scenario:\n\n");
2494 __print_lock_name(class);
2495 printk(KERN_CONT
");\n");
2496 printk(" <Interrupt>\n");
2498 __print_lock_name(class);
2499 printk(KERN_CONT
");\n");
2500 printk("\n *** DEADLOCK ***\n\n");
2504 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2505 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2507 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2511 pr_warn("================================\n");
2512 pr_warn("WARNING: inconsistent lock state\n");
2513 print_kernel_ident();
2514 pr_warn("--------------------------------\n");
2516 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2517 usage_str
[prev_bit
], usage_str
[new_bit
]);
2519 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2520 curr
->comm
, task_pid_nr(curr
),
2521 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2522 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2523 trace_hardirqs_enabled(curr
),
2524 trace_softirqs_enabled(curr
));
2527 pr_warn("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2528 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2530 print_irqtrace_events(curr
);
2531 pr_warn("\nother info that might help us debug this:\n");
2532 print_usage_bug_scenario(this);
2534 lockdep_print_held_locks(curr
);
2536 pr_warn("\nstack backtrace:\n");
2543 * Print out an error if an invalid bit is set:
2546 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2547 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2549 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2550 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2554 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2555 enum lock_usage_bit new_bit
);
2557 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2560 * print irq inversion bug:
2563 print_irq_inversion_bug(struct task_struct
*curr
,
2564 struct lock_list
*root
, struct lock_list
*other
,
2565 struct held_lock
*this, int forwards
,
2566 const char *irqclass
)
2568 struct lock_list
*entry
= other
;
2569 struct lock_list
*middle
= NULL
;
2572 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2576 pr_warn("========================================================\n");
2577 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2578 print_kernel_ident();
2579 pr_warn("--------------------------------------------------------\n");
2580 pr_warn("%s/%d just changed the state of lock:\n",
2581 curr
->comm
, task_pid_nr(curr
));
2584 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2586 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2587 print_lock_name(other
->class);
2588 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2590 pr_warn("\nother info that might help us debug this:\n");
2592 /* Find a middle lock (if one exists) */
2593 depth
= get_lock_depth(other
);
2595 if (depth
== 0 && (entry
!= root
)) {
2596 pr_warn("lockdep:%s bad path found in chain graph\n", __func__
);
2600 entry
= get_lock_parent(entry
);
2602 } while (entry
&& entry
!= root
&& (depth
>= 0));
2604 print_irq_lock_scenario(root
, other
,
2605 middle
? middle
->class : root
->class, other
->class);
2607 print_irq_lock_scenario(other
, root
,
2608 middle
? middle
->class : other
->class, root
->class);
2610 lockdep_print_held_locks(curr
);
2612 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2613 if (!save_trace(&root
->trace
))
2615 print_shortest_lock_dependencies(other
, root
);
2617 pr_warn("\nstack backtrace:\n");
2624 * Prove that in the forwards-direction subgraph starting at <this>
2625 * there is no lock matching <mask>:
2628 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2629 enum lock_usage_bit bit
, const char *irqclass
)
2632 struct lock_list root
;
2633 struct lock_list
*uninitialized_var(target_entry
);
2636 root
.class = hlock_class(this);
2637 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2639 return print_bfs_bug(ret
);
2643 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2648 * Prove that in the backwards-direction subgraph starting at <this>
2649 * there is no lock matching <mask>:
2652 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2653 enum lock_usage_bit bit
, const char *irqclass
)
2656 struct lock_list root
;
2657 struct lock_list
*uninitialized_var(target_entry
);
2660 root
.class = hlock_class(this);
2661 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2663 return print_bfs_bug(ret
);
2667 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2671 void print_irqtrace_events(struct task_struct
*curr
)
2673 printk("irq event stamp: %u\n", curr
->irq_events
);
2674 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2675 curr
->hardirq_enable_event
, (void *)curr
->hardirq_enable_ip
,
2676 (void *)curr
->hardirq_enable_ip
);
2677 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2678 curr
->hardirq_disable_event
, (void *)curr
->hardirq_disable_ip
,
2679 (void *)curr
->hardirq_disable_ip
);
2680 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2681 curr
->softirq_enable_event
, (void *)curr
->softirq_enable_ip
,
2682 (void *)curr
->softirq_enable_ip
);
2683 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2684 curr
->softirq_disable_event
, (void *)curr
->softirq_disable_ip
,
2685 (void *)curr
->softirq_disable_ip
);
2688 static int HARDIRQ_verbose(struct lock_class
*class)
2691 return class_filter(class);
2696 static int SOFTIRQ_verbose(struct lock_class
*class)
2699 return class_filter(class);
2704 #define STRICT_READ_CHECKS 1
2706 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2707 #define LOCKDEP_STATE(__STATE) \
2709 #include "lockdep_states.h"
2710 #undef LOCKDEP_STATE
2713 static inline int state_verbose(enum lock_usage_bit bit
,
2714 struct lock_class
*class)
2716 return state_verbose_f
[bit
>> 2](class);
2719 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2720 enum lock_usage_bit bit
, const char *name
);
2723 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2724 enum lock_usage_bit new_bit
)
2726 int excl_bit
= exclusive_bit(new_bit
);
2727 int read
= new_bit
& 1;
2728 int dir
= new_bit
& 2;
2731 * mark USED_IN has to look forwards -- to ensure no dependency
2732 * has ENABLED state, which would allow recursion deadlocks.
2734 * mark ENABLED has to look backwards -- to ensure no dependee
2735 * has USED_IN state, which, again, would allow recursion deadlocks.
2737 check_usage_f usage
= dir
?
2738 check_usage_backwards
: check_usage_forwards
;
2741 * Validate that this particular lock does not have conflicting
2744 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2748 * Validate that the lock dependencies don't have conflicting usage
2751 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2752 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2756 * Check for read in write conflicts
2759 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2762 if (STRICT_READ_CHECKS
&&
2763 !usage(curr
, this, excl_bit
+ 1,
2764 state_name(new_bit
+ 1)))
2768 if (state_verbose(new_bit
, hlock_class(this)))
2775 #define LOCKDEP_STATE(__STATE) __STATE,
2776 #include "lockdep_states.h"
2777 #undef LOCKDEP_STATE
2781 * Mark all held locks with a usage bit:
2784 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2786 enum lock_usage_bit usage_bit
;
2787 struct held_lock
*hlock
;
2790 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2791 hlock
= curr
->held_locks
+ i
;
2793 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2795 usage_bit
+= 1; /* READ */
2797 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2802 if (!mark_lock(curr
, hlock
, usage_bit
))
2810 * Hardirqs will be enabled:
2812 static void __trace_hardirqs_on_caller(unsigned long ip
)
2814 struct task_struct
*curr
= current
;
2816 /* we'll do an OFF -> ON transition: */
2817 curr
->hardirqs_enabled
= 1;
2820 * We are going to turn hardirqs on, so set the
2821 * usage bit for all held locks:
2823 if (!mark_held_locks(curr
, HARDIRQ
))
2826 * If we have softirqs enabled, then set the usage
2827 * bit for all held locks. (disabled hardirqs prevented
2828 * this bit from being set before)
2830 if (curr
->softirqs_enabled
)
2831 if (!mark_held_locks(curr
, SOFTIRQ
))
2834 curr
->hardirq_enable_ip
= ip
;
2835 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2836 debug_atomic_inc(hardirqs_on_events
);
2839 __visible
void trace_hardirqs_on_caller(unsigned long ip
)
2841 time_hardirqs_on(CALLER_ADDR0
, ip
);
2843 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2846 if (unlikely(current
->hardirqs_enabled
)) {
2848 * Neither irq nor preemption are disabled here
2849 * so this is racy by nature but losing one hit
2850 * in a stat is not a big deal.
2852 __debug_atomic_inc(redundant_hardirqs_on
);
2857 * We're enabling irqs and according to our state above irqs weren't
2858 * already enabled, yet we find the hardware thinks they are in fact
2859 * enabled.. someone messed up their IRQ state tracing.
2861 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2865 * See the fine text that goes along with this variable definition.
2867 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2871 * Can't allow enabling interrupts while in an interrupt handler,
2872 * that's general bad form and such. Recursion, limited stack etc..
2874 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2877 current
->lockdep_recursion
= 1;
2878 __trace_hardirqs_on_caller(ip
);
2879 current
->lockdep_recursion
= 0;
2881 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2883 void trace_hardirqs_on(void)
2885 trace_hardirqs_on_caller(CALLER_ADDR0
);
2887 EXPORT_SYMBOL(trace_hardirqs_on
);
2890 * Hardirqs were disabled:
2892 __visible
void trace_hardirqs_off_caller(unsigned long ip
)
2894 struct task_struct
*curr
= current
;
2896 time_hardirqs_off(CALLER_ADDR0
, ip
);
2898 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2902 * So we're supposed to get called after you mask local IRQs, but for
2903 * some reason the hardware doesn't quite think you did a proper job.
2905 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2908 if (curr
->hardirqs_enabled
) {
2910 * We have done an ON -> OFF transition:
2912 curr
->hardirqs_enabled
= 0;
2913 curr
->hardirq_disable_ip
= ip
;
2914 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2915 debug_atomic_inc(hardirqs_off_events
);
2917 debug_atomic_inc(redundant_hardirqs_off
);
2919 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2921 void trace_hardirqs_off(void)
2923 trace_hardirqs_off_caller(CALLER_ADDR0
);
2925 EXPORT_SYMBOL(trace_hardirqs_off
);
2928 * Softirqs will be enabled:
2930 void trace_softirqs_on(unsigned long ip
)
2932 struct task_struct
*curr
= current
;
2934 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2938 * We fancy IRQs being disabled here, see softirq.c, avoids
2939 * funny state and nesting things.
2941 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2944 if (curr
->softirqs_enabled
) {
2945 debug_atomic_inc(redundant_softirqs_on
);
2949 current
->lockdep_recursion
= 1;
2951 * We'll do an OFF -> ON transition:
2953 curr
->softirqs_enabled
= 1;
2954 curr
->softirq_enable_ip
= ip
;
2955 curr
->softirq_enable_event
= ++curr
->irq_events
;
2956 debug_atomic_inc(softirqs_on_events
);
2958 * We are going to turn softirqs on, so set the
2959 * usage bit for all held locks, if hardirqs are
2962 if (curr
->hardirqs_enabled
)
2963 mark_held_locks(curr
, SOFTIRQ
);
2964 current
->lockdep_recursion
= 0;
2968 * Softirqs were disabled:
2970 void trace_softirqs_off(unsigned long ip
)
2972 struct task_struct
*curr
= current
;
2974 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2978 * We fancy IRQs being disabled here, see softirq.c
2980 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2983 if (curr
->softirqs_enabled
) {
2985 * We have done an ON -> OFF transition:
2987 curr
->softirqs_enabled
= 0;
2988 curr
->softirq_disable_ip
= ip
;
2989 curr
->softirq_disable_event
= ++curr
->irq_events
;
2990 debug_atomic_inc(softirqs_off_events
);
2992 * Whoops, we wanted softirqs off, so why aren't they?
2994 DEBUG_LOCKS_WARN_ON(!softirq_count());
2996 debug_atomic_inc(redundant_softirqs_off
);
2999 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
3002 * If non-trylock use in a hardirq or softirq context, then
3003 * mark the lock as used in these contexts:
3005 if (!hlock
->trylock
) {
3007 if (curr
->hardirq_context
)
3008 if (!mark_lock(curr
, hlock
,
3009 LOCK_USED_IN_HARDIRQ_READ
))
3011 if (curr
->softirq_context
)
3012 if (!mark_lock(curr
, hlock
,
3013 LOCK_USED_IN_SOFTIRQ_READ
))
3016 if (curr
->hardirq_context
)
3017 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
3019 if (curr
->softirq_context
)
3020 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
3024 if (!hlock
->hardirqs_off
) {
3026 if (!mark_lock(curr
, hlock
,
3027 LOCK_ENABLED_HARDIRQ_READ
))
3029 if (curr
->softirqs_enabled
)
3030 if (!mark_lock(curr
, hlock
,
3031 LOCK_ENABLED_SOFTIRQ_READ
))
3034 if (!mark_lock(curr
, hlock
,
3035 LOCK_ENABLED_HARDIRQ
))
3037 if (curr
->softirqs_enabled
)
3038 if (!mark_lock(curr
, hlock
,
3039 LOCK_ENABLED_SOFTIRQ
))
3047 static inline unsigned int task_irq_context(struct task_struct
*task
)
3049 return 2 * !!task
->hardirq_context
+ !!task
->softirq_context
;
3052 static int separate_irq_context(struct task_struct
*curr
,
3053 struct held_lock
*hlock
)
3055 unsigned int depth
= curr
->lockdep_depth
;
3058 * Keep track of points where we cross into an interrupt context:
3061 struct held_lock
*prev_hlock
;
3063 prev_hlock
= curr
->held_locks
+ depth
-1;
3065 * If we cross into another context, reset the
3066 * hash key (this also prevents the checking and the
3067 * adding of the dependency to 'prev'):
3069 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
3075 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3078 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
3079 enum lock_usage_bit new_bit
)
3081 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3085 static inline int mark_irqflags(struct task_struct
*curr
,
3086 struct held_lock
*hlock
)
3091 static inline unsigned int task_irq_context(struct task_struct
*task
)
3096 static inline int separate_irq_context(struct task_struct
*curr
,
3097 struct held_lock
*hlock
)
3102 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3105 * Mark a lock with a usage bit, and validate the state transition:
3107 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
3108 enum lock_usage_bit new_bit
)
3110 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
3113 * If already set then do not dirty the cacheline,
3114 * nor do any checks:
3116 if (likely(hlock_class(this)->usage_mask
& new_mask
))
3122 * Make sure we didn't race:
3124 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
3129 hlock_class(this)->usage_mask
|= new_mask
;
3131 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
3135 #define LOCKDEP_STATE(__STATE) \
3136 case LOCK_USED_IN_##__STATE: \
3137 case LOCK_USED_IN_##__STATE##_READ: \
3138 case LOCK_ENABLED_##__STATE: \
3139 case LOCK_ENABLED_##__STATE##_READ:
3140 #include "lockdep_states.h"
3141 #undef LOCKDEP_STATE
3142 ret
= mark_lock_irq(curr
, this, new_bit
);
3147 debug_atomic_dec(nr_unused_locks
);
3150 if (!debug_locks_off_graph_unlock())
3159 * We must printk outside of the graph_lock:
3162 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
3164 print_irqtrace_events(curr
);
3172 * Initialize a lock instance's lock-class mapping info:
3174 static void __lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
3175 struct lock_class_key
*key
, int subclass
)
3179 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
3180 lock
->class_cache
[i
] = NULL
;
3182 #ifdef CONFIG_LOCK_STAT
3183 lock
->cpu
= raw_smp_processor_id();
3187 * Can't be having no nameless bastards around this place!
3189 if (DEBUG_LOCKS_WARN_ON(!name
)) {
3190 lock
->name
= "NULL";
3197 * No key, no joy, we need to hash something.
3199 if (DEBUG_LOCKS_WARN_ON(!key
))
3202 * Sanity check, the lock-class key must be persistent:
3204 if (!static_obj(key
)) {
3205 printk("BUG: key %p not in .data!\n", key
);
3207 * What it says above ^^^^^, I suggest you read it.
3209 DEBUG_LOCKS_WARN_ON(1);
3214 if (unlikely(!debug_locks
))
3218 unsigned long flags
;
3220 if (DEBUG_LOCKS_WARN_ON(current
->lockdep_recursion
))
3223 raw_local_irq_save(flags
);
3224 current
->lockdep_recursion
= 1;
3225 register_lock_class(lock
, subclass
, 1);
3226 current
->lockdep_recursion
= 0;
3227 raw_local_irq_restore(flags
);
3231 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
3232 struct lock_class_key
*key
, int subclass
)
3234 __lockdep_init_map(lock
, name
, key
, subclass
);
3236 EXPORT_SYMBOL_GPL(lockdep_init_map
);
3238 struct lock_class_key __lockdep_no_validate__
;
3239 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
3242 print_lock_nested_lock_not_held(struct task_struct
*curr
,
3243 struct held_lock
*hlock
,
3246 if (!debug_locks_off())
3248 if (debug_locks_silent
)
3252 pr_warn("==================================\n");
3253 pr_warn("WARNING: Nested lock was not taken\n");
3254 print_kernel_ident();
3255 pr_warn("----------------------------------\n");
3257 pr_warn("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3260 pr_warn("\nbut this task is not holding:\n");
3261 pr_warn("%s\n", hlock
->nest_lock
->name
);
3263 pr_warn("\nstack backtrace:\n");
3266 pr_warn("\nother info that might help us debug this:\n");
3267 lockdep_print_held_locks(curr
);
3269 pr_warn("\nstack backtrace:\n");
3275 static int __lock_is_held(struct lockdep_map
*lock
, int read
);
3278 * This gets called for every mutex_lock*()/spin_lock*() operation.
3279 * We maintain the dependency maps and validate the locking attempt:
3281 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3282 int trylock
, int read
, int check
, int hardirqs_off
,
3283 struct lockdep_map
*nest_lock
, unsigned long ip
,
3284 int references
, int pin_count
)
3286 struct task_struct
*curr
= current
;
3287 struct lock_class
*class = NULL
;
3288 struct held_lock
*hlock
;
3294 if (unlikely(!debug_locks
))
3298 * Lockdep should run with IRQs disabled, otherwise we could
3299 * get an interrupt which would want to take locks, which would
3300 * end up in lockdep and have you got a head-ache already?
3302 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3305 if (!prove_locking
|| lock
->key
== &__lockdep_no_validate__
)
3308 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
3309 class = lock
->class_cache
[subclass
];
3313 if (unlikely(!class)) {
3314 class = register_lock_class(lock
, subclass
, 0);
3318 atomic_inc((atomic_t
*)&class->ops
);
3319 if (very_verbose(class)) {
3320 printk("\nacquire class [%p] %s", class->key
, class->name
);
3321 if (class->name_version
> 1)
3322 printk(KERN_CONT
"#%d", class->name_version
);
3323 printk(KERN_CONT
"\n");
3328 * Add the lock to the list of currently held locks.
3329 * (we dont increase the depth just yet, up until the
3330 * dependency checks are done)
3332 depth
= curr
->lockdep_depth
;
3334 * Ran out of static storage for our per-task lock stack again have we?
3336 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
3339 class_idx
= class - lock_classes
+ 1;
3342 hlock
= curr
->held_locks
+ depth
- 1;
3343 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
3344 if (hlock
->references
) {
3346 * Check: unsigned int references:12, overflow.
3348 if (DEBUG_LOCKS_WARN_ON(hlock
->references
== (1 << 12)-1))
3351 hlock
->references
++;
3353 hlock
->references
= 2;
3360 hlock
= curr
->held_locks
+ depth
;
3362 * Plain impossible, we just registered it and checked it weren't no
3363 * NULL like.. I bet this mushroom I ate was good!
3365 if (DEBUG_LOCKS_WARN_ON(!class))
3367 hlock
->class_idx
= class_idx
;
3368 hlock
->acquire_ip
= ip
;
3369 hlock
->instance
= lock
;
3370 hlock
->nest_lock
= nest_lock
;
3371 hlock
->irq_context
= task_irq_context(curr
);
3372 hlock
->trylock
= trylock
;
3374 hlock
->check
= check
;
3375 hlock
->hardirqs_off
= !!hardirqs_off
;
3376 hlock
->references
= references
;
3377 #ifdef CONFIG_LOCK_STAT
3378 hlock
->waittime_stamp
= 0;
3379 hlock
->holdtime_stamp
= lockstat_clock();
3381 hlock
->pin_count
= pin_count
;
3383 if (check
&& !mark_irqflags(curr
, hlock
))
3386 /* mark it as used: */
3387 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3391 * Calculate the chain hash: it's the combined hash of all the
3392 * lock keys along the dependency chain. We save the hash value
3393 * at every step so that we can get the current hash easily
3394 * after unlock. The chain hash is then used to cache dependency
3397 * The 'key ID' is what is the most compact key value to drive
3398 * the hash, not class->key.
3401 * Whoops, we did it again.. ran straight out of our static allocation.
3403 if (DEBUG_LOCKS_WARN_ON(class_idx
> MAX_LOCKDEP_KEYS
))
3406 chain_key
= curr
->curr_chain_key
;
3409 * How can we have a chain hash when we ain't got no keys?!
3411 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3416 hlock
->prev_chain_key
= chain_key
;
3417 if (separate_irq_context(curr
, hlock
)) {
3421 chain_key
= iterate_chain_key(chain_key
, class_idx
);
3423 if (nest_lock
&& !__lock_is_held(nest_lock
, -1))
3424 return print_lock_nested_lock_not_held(curr
, hlock
, ip
);
3426 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3429 curr
->curr_chain_key
= chain_key
;
3430 curr
->lockdep_depth
++;
3431 check_chain_key(curr
);
3432 #ifdef CONFIG_DEBUG_LOCKDEP
3433 if (unlikely(!debug_locks
))
3436 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3438 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3439 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
3440 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
3442 lockdep_print_held_locks(current
);
3443 debug_show_all_locks();
3449 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3450 max_lockdep_depth
= curr
->lockdep_depth
;
3456 print_unlock_imbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3459 if (!debug_locks_off())
3461 if (debug_locks_silent
)
3465 pr_warn("=====================================\n");
3466 pr_warn("WARNING: bad unlock balance detected!\n");
3467 print_kernel_ident();
3468 pr_warn("-------------------------------------\n");
3469 pr_warn("%s/%d is trying to release lock (",
3470 curr
->comm
, task_pid_nr(curr
));
3471 print_lockdep_cache(lock
);
3474 pr_warn("but there are no more locks to release!\n");
3475 pr_warn("\nother info that might help us debug this:\n");
3476 lockdep_print_held_locks(curr
);
3478 pr_warn("\nstack backtrace:\n");
3484 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
3486 if (hlock
->instance
== lock
)
3489 if (hlock
->references
) {
3490 struct lock_class
*class = lock
->class_cache
[0];
3493 class = look_up_lock_class(lock
, 0);
3496 * If look_up_lock_class() failed to find a class, we're trying
3497 * to test if we hold a lock that has never yet been acquired.
3498 * Clearly if the lock hasn't been acquired _ever_, we're not
3499 * holding it either, so report failure.
3501 if (IS_ERR_OR_NULL(class))
3505 * References, but not a lock we're actually ref-counting?
3506 * State got messed up, follow the sites that change ->references
3507 * and try to make sense of it.
3509 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3512 if (hlock
->class_idx
== class - lock_classes
+ 1)
3519 /* @depth must not be zero */
3520 static struct held_lock
*find_held_lock(struct task_struct
*curr
,
3521 struct lockdep_map
*lock
,
3522 unsigned int depth
, int *idx
)
3524 struct held_lock
*ret
, *hlock
, *prev_hlock
;
3528 hlock
= curr
->held_locks
+ i
;
3530 if (match_held_lock(hlock
, lock
))
3534 for (i
--, prev_hlock
= hlock
--;
3536 i
--, prev_hlock
= hlock
--) {
3538 * We must not cross into another context:
3540 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
3544 if (match_held_lock(hlock
, lock
)) {
3555 static int reacquire_held_locks(struct task_struct
*curr
, unsigned int depth
,
3558 struct held_lock
*hlock
;
3560 for (hlock
= curr
->held_locks
+ idx
; idx
< depth
; idx
++, hlock
++) {
3561 if (!__lock_acquire(hlock
->instance
,
3562 hlock_class(hlock
)->subclass
,
3564 hlock
->read
, hlock
->check
,
3565 hlock
->hardirqs_off
,
3566 hlock
->nest_lock
, hlock
->acquire_ip
,
3567 hlock
->references
, hlock
->pin_count
))
3574 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3575 struct lock_class_key
*key
, unsigned int subclass
,
3578 struct task_struct
*curr
= current
;
3579 struct held_lock
*hlock
;
3580 struct lock_class
*class;
3584 depth
= curr
->lockdep_depth
;
3586 * This function is about (re)setting the class of a held lock,
3587 * yet we're not actually holding any locks. Naughty user!
3589 if (DEBUG_LOCKS_WARN_ON(!depth
))
3592 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3594 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3596 lockdep_init_map(lock
, name
, key
, 0);
3597 class = register_lock_class(lock
, subclass
, 0);
3598 hlock
->class_idx
= class - lock_classes
+ 1;
3600 curr
->lockdep_depth
= i
;
3601 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3603 if (reacquire_held_locks(curr
, depth
, i
))
3607 * I took it apart and put it back together again, except now I have
3608 * these 'spare' parts.. where shall I put them.
3610 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3615 static int __lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
3617 struct task_struct
*curr
= current
;
3618 struct held_lock
*hlock
;
3622 depth
= curr
->lockdep_depth
;
3624 * This function is about (re)setting the class of a held lock,
3625 * yet we're not actually holding any locks. Naughty user!
3627 if (DEBUG_LOCKS_WARN_ON(!depth
))
3630 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3632 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3634 curr
->lockdep_depth
= i
;
3635 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3637 WARN(hlock
->read
, "downgrading a read lock");
3639 hlock
->acquire_ip
= ip
;
3641 if (reacquire_held_locks(curr
, depth
, i
))
3645 * I took it apart and put it back together again, except now I have
3646 * these 'spare' parts.. where shall I put them.
3648 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3654 * Remove the lock to the list of currently held locks - this gets
3655 * called on mutex_unlock()/spin_unlock*() (or on a failed
3656 * mutex_lock_interruptible()).
3658 * @nested is an hysterical artifact, needs a tree wide cleanup.
3661 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3663 struct task_struct
*curr
= current
;
3664 struct held_lock
*hlock
;
3668 if (unlikely(!debug_locks
))
3671 depth
= curr
->lockdep_depth
;
3673 * So we're all set to release this lock.. wait what lock? We don't
3674 * own any locks, you've been drinking again?
3676 if (DEBUG_LOCKS_WARN_ON(depth
<= 0))
3677 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3680 * Check whether the lock exists in the current stack
3683 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
3685 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3687 if (hlock
->instance
== lock
)
3688 lock_release_holdtime(hlock
);
3690 WARN(hlock
->pin_count
, "releasing a pinned lock\n");
3692 if (hlock
->references
) {
3693 hlock
->references
--;
3694 if (hlock
->references
) {
3696 * We had, and after removing one, still have
3697 * references, the current lock stack is still
3698 * valid. We're done!
3705 * We have the right lock to unlock, 'hlock' points to it.
3706 * Now we remove it from the stack, and add back the other
3707 * entries (if any), recalculating the hash along the way:
3710 curr
->lockdep_depth
= i
;
3711 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3713 if (reacquire_held_locks(curr
, depth
, i
+ 1))
3717 * We had N bottles of beer on the wall, we drank one, but now
3718 * there's not N-1 bottles of beer left on the wall...
3720 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3726 static int __lock_is_held(struct lockdep_map
*lock
, int read
)
3728 struct task_struct
*curr
= current
;
3731 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3732 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3734 if (match_held_lock(hlock
, lock
)) {
3735 if (read
== -1 || hlock
->read
== read
)
3745 static struct pin_cookie
__lock_pin_lock(struct lockdep_map
*lock
)
3747 struct pin_cookie cookie
= NIL_COOKIE
;
3748 struct task_struct
*curr
= current
;
3751 if (unlikely(!debug_locks
))
3754 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3755 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3757 if (match_held_lock(hlock
, lock
)) {
3759 * Grab 16bits of randomness; this is sufficient to not
3760 * be guessable and still allows some pin nesting in
3761 * our u32 pin_count.
3763 cookie
.val
= 1 + (prandom_u32() >> 16);
3764 hlock
->pin_count
+= cookie
.val
;
3769 WARN(1, "pinning an unheld lock\n");
3773 static void __lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3775 struct task_struct
*curr
= current
;
3778 if (unlikely(!debug_locks
))
3781 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3782 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3784 if (match_held_lock(hlock
, lock
)) {
3785 hlock
->pin_count
+= cookie
.val
;
3790 WARN(1, "pinning an unheld lock\n");
3793 static void __lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3795 struct task_struct
*curr
= current
;
3798 if (unlikely(!debug_locks
))
3801 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3802 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3804 if (match_held_lock(hlock
, lock
)) {
3805 if (WARN(!hlock
->pin_count
, "unpinning an unpinned lock\n"))
3808 hlock
->pin_count
-= cookie
.val
;
3810 if (WARN((int)hlock
->pin_count
< 0, "pin count corrupted\n"))
3811 hlock
->pin_count
= 0;
3817 WARN(1, "unpinning an unheld lock\n");
3821 * Check whether we follow the irq-flags state precisely:
3823 static void check_flags(unsigned long flags
)
3825 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3826 defined(CONFIG_TRACE_IRQFLAGS)
3830 if (irqs_disabled_flags(flags
)) {
3831 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3832 printk("possible reason: unannotated irqs-off.\n");
3835 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3836 printk("possible reason: unannotated irqs-on.\n");
3841 * We dont accurately track softirq state in e.g.
3842 * hardirq contexts (such as on 4KSTACKS), so only
3843 * check if not in hardirq contexts:
3845 if (!hardirq_count()) {
3846 if (softirq_count()) {
3847 /* like the above, but with softirqs */
3848 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3850 /* lick the above, does it taste good? */
3851 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3856 print_irqtrace_events(current
);
3860 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3861 struct lock_class_key
*key
, unsigned int subclass
,
3864 unsigned long flags
;
3866 if (unlikely(current
->lockdep_recursion
))
3869 raw_local_irq_save(flags
);
3870 current
->lockdep_recursion
= 1;
3872 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3873 check_chain_key(current
);
3874 current
->lockdep_recursion
= 0;
3875 raw_local_irq_restore(flags
);
3877 EXPORT_SYMBOL_GPL(lock_set_class
);
3879 void lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
3881 unsigned long flags
;
3883 if (unlikely(current
->lockdep_recursion
))
3886 raw_local_irq_save(flags
);
3887 current
->lockdep_recursion
= 1;
3889 if (__lock_downgrade(lock
, ip
))
3890 check_chain_key(current
);
3891 current
->lockdep_recursion
= 0;
3892 raw_local_irq_restore(flags
);
3894 EXPORT_SYMBOL_GPL(lock_downgrade
);
3897 * We are not always called with irqs disabled - do that here,
3898 * and also avoid lockdep recursion:
3900 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3901 int trylock
, int read
, int check
,
3902 struct lockdep_map
*nest_lock
, unsigned long ip
)
3904 unsigned long flags
;
3906 if (unlikely(current
->lockdep_recursion
))
3909 raw_local_irq_save(flags
);
3912 current
->lockdep_recursion
= 1;
3913 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3914 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3915 irqs_disabled_flags(flags
), nest_lock
, ip
, 0, 0);
3916 current
->lockdep_recursion
= 0;
3917 raw_local_irq_restore(flags
);
3919 EXPORT_SYMBOL_GPL(lock_acquire
);
3921 void lock_release(struct lockdep_map
*lock
, int nested
,
3924 unsigned long flags
;
3926 if (unlikely(current
->lockdep_recursion
))
3929 raw_local_irq_save(flags
);
3931 current
->lockdep_recursion
= 1;
3932 trace_lock_release(lock
, ip
);
3933 if (__lock_release(lock
, nested
, ip
))
3934 check_chain_key(current
);
3935 current
->lockdep_recursion
= 0;
3936 raw_local_irq_restore(flags
);
3938 EXPORT_SYMBOL_GPL(lock_release
);
3940 int lock_is_held_type(struct lockdep_map
*lock
, int read
)
3942 unsigned long flags
;
3945 if (unlikely(current
->lockdep_recursion
))
3946 return 1; /* avoid false negative lockdep_assert_held() */
3948 raw_local_irq_save(flags
);
3951 current
->lockdep_recursion
= 1;
3952 ret
= __lock_is_held(lock
, read
);
3953 current
->lockdep_recursion
= 0;
3954 raw_local_irq_restore(flags
);
3958 EXPORT_SYMBOL_GPL(lock_is_held_type
);
3960 struct pin_cookie
lock_pin_lock(struct lockdep_map
*lock
)
3962 struct pin_cookie cookie
= NIL_COOKIE
;
3963 unsigned long flags
;
3965 if (unlikely(current
->lockdep_recursion
))
3968 raw_local_irq_save(flags
);
3971 current
->lockdep_recursion
= 1;
3972 cookie
= __lock_pin_lock(lock
);
3973 current
->lockdep_recursion
= 0;
3974 raw_local_irq_restore(flags
);
3978 EXPORT_SYMBOL_GPL(lock_pin_lock
);
3980 void lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3982 unsigned long flags
;
3984 if (unlikely(current
->lockdep_recursion
))
3987 raw_local_irq_save(flags
);
3990 current
->lockdep_recursion
= 1;
3991 __lock_repin_lock(lock
, cookie
);
3992 current
->lockdep_recursion
= 0;
3993 raw_local_irq_restore(flags
);
3995 EXPORT_SYMBOL_GPL(lock_repin_lock
);
3997 void lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3999 unsigned long flags
;
4001 if (unlikely(current
->lockdep_recursion
))
4004 raw_local_irq_save(flags
);
4007 current
->lockdep_recursion
= 1;
4008 __lock_unpin_lock(lock
, cookie
);
4009 current
->lockdep_recursion
= 0;
4010 raw_local_irq_restore(flags
);
4012 EXPORT_SYMBOL_GPL(lock_unpin_lock
);
4014 #ifdef CONFIG_LOCK_STAT
4016 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
4019 if (!debug_locks_off())
4021 if (debug_locks_silent
)
4025 pr_warn("=================================\n");
4026 pr_warn("WARNING: bad contention detected!\n");
4027 print_kernel_ident();
4028 pr_warn("---------------------------------\n");
4029 pr_warn("%s/%d is trying to contend lock (",
4030 curr
->comm
, task_pid_nr(curr
));
4031 print_lockdep_cache(lock
);
4034 pr_warn("but there are no locks held!\n");
4035 pr_warn("\nother info that might help us debug this:\n");
4036 lockdep_print_held_locks(curr
);
4038 pr_warn("\nstack backtrace:\n");
4045 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
4047 struct task_struct
*curr
= current
;
4048 struct held_lock
*hlock
;
4049 struct lock_class_stats
*stats
;
4051 int i
, contention_point
, contending_point
;
4053 depth
= curr
->lockdep_depth
;
4055 * Whee, we contended on this lock, except it seems we're not
4056 * actually trying to acquire anything much at all..
4058 if (DEBUG_LOCKS_WARN_ON(!depth
))
4061 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4063 print_lock_contention_bug(curr
, lock
, ip
);
4067 if (hlock
->instance
!= lock
)
4070 hlock
->waittime_stamp
= lockstat_clock();
4072 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
4073 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
4076 stats
= get_lock_stats(hlock_class(hlock
));
4077 if (contention_point
< LOCKSTAT_POINTS
)
4078 stats
->contention_point
[contention_point
]++;
4079 if (contending_point
< LOCKSTAT_POINTS
)
4080 stats
->contending_point
[contending_point
]++;
4081 if (lock
->cpu
!= smp_processor_id())
4082 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
4083 put_lock_stats(stats
);
4087 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4089 struct task_struct
*curr
= current
;
4090 struct held_lock
*hlock
;
4091 struct lock_class_stats
*stats
;
4093 u64 now
, waittime
= 0;
4096 depth
= curr
->lockdep_depth
;
4098 * Yay, we acquired ownership of this lock we didn't try to
4099 * acquire, how the heck did that happen?
4101 if (DEBUG_LOCKS_WARN_ON(!depth
))
4104 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4106 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
4110 if (hlock
->instance
!= lock
)
4113 cpu
= smp_processor_id();
4114 if (hlock
->waittime_stamp
) {
4115 now
= lockstat_clock();
4116 waittime
= now
- hlock
->waittime_stamp
;
4117 hlock
->holdtime_stamp
= now
;
4120 trace_lock_acquired(lock
, ip
);
4122 stats
= get_lock_stats(hlock_class(hlock
));
4125 lock_time_inc(&stats
->read_waittime
, waittime
);
4127 lock_time_inc(&stats
->write_waittime
, waittime
);
4129 if (lock
->cpu
!= cpu
)
4130 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
4131 put_lock_stats(stats
);
4137 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
4139 unsigned long flags
;
4141 if (unlikely(!lock_stat
))
4144 if (unlikely(current
->lockdep_recursion
))
4147 raw_local_irq_save(flags
);
4149 current
->lockdep_recursion
= 1;
4150 trace_lock_contended(lock
, ip
);
4151 __lock_contended(lock
, ip
);
4152 current
->lockdep_recursion
= 0;
4153 raw_local_irq_restore(flags
);
4155 EXPORT_SYMBOL_GPL(lock_contended
);
4157 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4159 unsigned long flags
;
4161 if (unlikely(!lock_stat
))
4164 if (unlikely(current
->lockdep_recursion
))
4167 raw_local_irq_save(flags
);
4169 current
->lockdep_recursion
= 1;
4170 __lock_acquired(lock
, ip
);
4171 current
->lockdep_recursion
= 0;
4172 raw_local_irq_restore(flags
);
4174 EXPORT_SYMBOL_GPL(lock_acquired
);
4178 * Used by the testsuite, sanitize the validator state
4179 * after a simulated failure:
4182 void lockdep_reset(void)
4184 unsigned long flags
;
4187 raw_local_irq_save(flags
);
4188 current
->curr_chain_key
= 0;
4189 current
->lockdep_depth
= 0;
4190 current
->lockdep_recursion
= 0;
4191 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
4192 nr_hardirq_chains
= 0;
4193 nr_softirq_chains
= 0;
4194 nr_process_chains
= 0;
4196 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
4197 INIT_HLIST_HEAD(chainhash_table
+ i
);
4198 raw_local_irq_restore(flags
);
4201 static void zap_class(struct lock_class
*class)
4206 * Remove all dependencies this lock is
4209 for (i
= 0; i
< nr_list_entries
; i
++) {
4210 if (list_entries
[i
].class == class)
4211 list_del_rcu(&list_entries
[i
].entry
);
4214 * Unhash the class and remove it from the all_lock_classes list:
4216 hlist_del_rcu(&class->hash_entry
);
4217 list_del_rcu(&class->lock_entry
);
4219 RCU_INIT_POINTER(class->key
, NULL
);
4220 RCU_INIT_POINTER(class->name
, NULL
);
4223 static inline int within(const void *addr
, void *start
, unsigned long size
)
4225 return addr
>= start
&& addr
< start
+ size
;
4229 * Used in module.c to remove lock classes from memory that is going to be
4230 * freed; and possibly re-used by other modules.
4232 * We will have had one sync_sched() before getting here, so we're guaranteed
4233 * nobody will look up these exact classes -- they're properly dead but still
4236 void lockdep_free_key_range(void *start
, unsigned long size
)
4238 struct lock_class
*class;
4239 struct hlist_head
*head
;
4240 unsigned long flags
;
4244 raw_local_irq_save(flags
);
4245 locked
= graph_lock();
4248 * Unhash all classes that were created by this module:
4250 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4251 head
= classhash_table
+ i
;
4252 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4253 if (within(class->key
, start
, size
))
4255 else if (within(class->name
, start
, size
))
4262 raw_local_irq_restore(flags
);
4265 * Wait for any possible iterators from look_up_lock_class() to pass
4266 * before continuing to free the memory they refer to.
4268 * sync_sched() is sufficient because the read-side is IRQ disable.
4270 synchronize_sched();
4273 * XXX at this point we could return the resources to the pool;
4274 * instead we leak them. We would need to change to bitmap allocators
4275 * instead of the linear allocators we have now.
4279 void lockdep_reset_lock(struct lockdep_map
*lock
)
4281 struct lock_class
*class;
4282 struct hlist_head
*head
;
4283 unsigned long flags
;
4287 raw_local_irq_save(flags
);
4290 * Remove all classes this lock might have:
4292 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
4294 * If the class exists we look it up and zap it:
4296 class = look_up_lock_class(lock
, j
);
4297 if (!IS_ERR_OR_NULL(class))
4301 * Debug check: in the end all mapped classes should
4304 locked
= graph_lock();
4305 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4306 head
= classhash_table
+ i
;
4307 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4310 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
4311 match
|= class == lock
->class_cache
[j
];
4313 if (unlikely(match
)) {
4314 if (debug_locks_off_graph_unlock()) {
4316 * We all just reset everything, how did it match?
4328 raw_local_irq_restore(flags
);
4331 void __init
lockdep_info(void)
4333 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4335 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
4336 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
4337 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
4338 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
4339 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
4340 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
4341 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
4343 printk(" memory used by lock dependency info: %lu kB\n",
4344 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
4345 sizeof(struct list_head
) * CLASSHASH_SIZE
+
4346 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
4347 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
4348 sizeof(struct list_head
) * CHAINHASH_SIZE
4349 #ifdef CONFIG_PROVE_LOCKING
4350 + sizeof(struct circular_queue
)
4355 printk(" per task-struct memory footprint: %lu bytes\n",
4356 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
4360 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
4361 const void *mem_to
, struct held_lock
*hlock
)
4363 if (!debug_locks_off())
4365 if (debug_locks_silent
)
4369 pr_warn("=========================\n");
4370 pr_warn("WARNING: held lock freed!\n");
4371 print_kernel_ident();
4372 pr_warn("-------------------------\n");
4373 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4374 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
4376 lockdep_print_held_locks(curr
);
4378 pr_warn("\nstack backtrace:\n");
4382 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
4383 const void* lock_from
, unsigned long lock_len
)
4385 return lock_from
+ lock_len
<= mem_from
||
4386 mem_from
+ mem_len
<= lock_from
;
4390 * Called when kernel memory is freed (or unmapped), or if a lock
4391 * is destroyed or reinitialized - this code checks whether there is
4392 * any held lock in the memory range of <from> to <to>:
4394 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
4396 struct task_struct
*curr
= current
;
4397 struct held_lock
*hlock
;
4398 unsigned long flags
;
4401 if (unlikely(!debug_locks
))
4404 local_irq_save(flags
);
4405 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4406 hlock
= curr
->held_locks
+ i
;
4408 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
4409 sizeof(*hlock
->instance
)))
4412 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
4415 local_irq_restore(flags
);
4417 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
4419 static void print_held_locks_bug(void)
4421 if (!debug_locks_off())
4423 if (debug_locks_silent
)
4427 pr_warn("====================================\n");
4428 pr_warn("WARNING: %s/%d still has locks held!\n",
4429 current
->comm
, task_pid_nr(current
));
4430 print_kernel_ident();
4431 pr_warn("------------------------------------\n");
4432 lockdep_print_held_locks(current
);
4433 pr_warn("\nstack backtrace:\n");
4437 void debug_check_no_locks_held(void)
4439 if (unlikely(current
->lockdep_depth
> 0))
4440 print_held_locks_bug();
4442 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
4445 void debug_show_all_locks(void)
4447 struct task_struct
*g
, *p
;
4451 if (unlikely(!debug_locks
)) {
4452 pr_warn("INFO: lockdep is turned off.\n");
4455 pr_warn("\nShowing all locks held in the system:\n");
4458 * Here we try to get the tasklist_lock as hard as possible,
4459 * if not successful after 2 seconds we ignore it (but keep
4460 * trying). This is to enable a debug printout even if a
4461 * tasklist_lock-holding task deadlocks or crashes.
4464 if (!read_trylock(&tasklist_lock
)) {
4466 pr_warn("hm, tasklist_lock locked, retrying... ");
4469 pr_cont(" #%d", 10-count
);
4473 pr_cont(" ignoring it.\n");
4477 pr_cont(" locked it.\n");
4480 do_each_thread(g
, p
) {
4482 * It's not reliable to print a task's held locks
4483 * if it's not sleeping (or if it's not the current
4486 if (p
->state
== TASK_RUNNING
&& p
!= current
)
4488 if (p
->lockdep_depth
)
4489 lockdep_print_held_locks(p
);
4491 if (read_trylock(&tasklist_lock
))
4493 } while_each_thread(g
, p
);
4496 pr_warn("=============================================\n\n");
4499 read_unlock(&tasklist_lock
);
4501 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
4505 * Careful: only use this function if you are sure that
4506 * the task cannot run in parallel!
4508 void debug_show_held_locks(struct task_struct
*task
)
4510 if (unlikely(!debug_locks
)) {
4511 printk("INFO: lockdep is turned off.\n");
4514 lockdep_print_held_locks(task
);
4516 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
4518 asmlinkage __visible
void lockdep_sys_exit(void)
4520 struct task_struct
*curr
= current
;
4522 if (unlikely(curr
->lockdep_depth
)) {
4523 if (!debug_locks_off())
4526 pr_warn("================================================\n");
4527 pr_warn("WARNING: lock held when returning to user space!\n");
4528 print_kernel_ident();
4529 pr_warn("------------------------------------------------\n");
4530 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4531 curr
->comm
, curr
->pid
);
4532 lockdep_print_held_locks(curr
);
4536 * The lock history for each syscall should be independent. So wipe the
4537 * slate clean on return to userspace.
4539 lockdep_invariant_state(false);
4542 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
4544 struct task_struct
*curr
= current
;
4546 /* Note: the following can be executed concurrently, so be careful. */
4548 pr_warn("=============================\n");
4549 pr_warn("WARNING: suspicious RCU usage\n");
4550 print_kernel_ident();
4551 pr_warn("-----------------------------\n");
4552 pr_warn("%s:%d %s!\n", file
, line
, s
);
4553 pr_warn("\nother info that might help us debug this:\n\n");
4554 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4555 !rcu_lockdep_current_cpu_online()
4556 ? "RCU used illegally from offline CPU!\n"
4557 : !rcu_is_watching()
4558 ? "RCU used illegally from idle CPU!\n"
4560 rcu_scheduler_active
, debug_locks
);
4563 * If a CPU is in the RCU-free window in idle (ie: in the section
4564 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4565 * considers that CPU to be in an "extended quiescent state",
4566 * which means that RCU will be completely ignoring that CPU.
4567 * Therefore, rcu_read_lock() and friends have absolutely no
4568 * effect on a CPU running in that state. In other words, even if
4569 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4570 * delete data structures out from under it. RCU really has no
4571 * choice here: we need to keep an RCU-free window in idle where
4572 * the CPU may possibly enter into low power mode. This way we can
4573 * notice an extended quiescent state to other CPUs that started a grace
4574 * period. Otherwise we would delay any grace period as long as we run
4577 * So complain bitterly if someone does call rcu_read_lock(),
4578 * rcu_read_lock_bh() and so on from extended quiescent states.
4580 if (!rcu_is_watching())
4581 pr_warn("RCU used illegally from extended quiescent state!\n");
4583 lockdep_print_held_locks(curr
);
4584 pr_warn("\nstack backtrace:\n");
4587 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);