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/delay.h>
34 #include <linux/module.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/spinlock.h>
38 #include <linux/kallsyms.h>
39 #include <linux/interrupt.h>
40 #include <linux/stacktrace.h>
41 #include <linux/debug_locks.h>
42 #include <linux/irqflags.h>
43 #include <linux/utsname.h>
44 #include <linux/hash.h>
45 #include <linux/ftrace.h>
46 #include <linux/stringify.h>
47 #include <linux/bitops.h>
48 #include <linux/gfp.h>
49 #include <linux/kmemcheck.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
346 # define RECLAIM_VERBOSE 1
348 # define HARDIRQ_VERBOSE 0
349 # define SOFTIRQ_VERBOSE 0
350 # define RECLAIM_VERBOSE 0
353 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
355 * Quick filtering for interesting events:
357 static int class_filter(struct lock_class
*class)
361 if (class->name_version
== 1 &&
362 !strcmp(class->name
, "lockname"))
364 if (class->name_version
== 1 &&
365 !strcmp(class->name
, "&struct->lockfield"))
368 /* Filter everything else. 1 would be to allow everything else */
373 static int verbose(struct lock_class
*class)
376 return class_filter(class);
382 * Stack-trace: tightly packed array of stack backtrace
383 * addresses. Protected by the graph_lock.
385 unsigned long nr_stack_trace_entries
;
386 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
388 static void print_lockdep_off(const char *bug_msg
)
390 printk(KERN_DEBUG
"%s\n", bug_msg
);
391 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
392 #ifdef CONFIG_LOCK_STAT
393 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
397 static int save_trace(struct stack_trace
*trace
)
399 trace
->nr_entries
= 0;
400 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
401 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
405 save_stack_trace(trace
);
408 * Some daft arches put -1 at the end to indicate its a full trace.
410 * <rant> this is buggy anyway, since it takes a whole extra entry so a
411 * complete trace that maxes out the entries provided will be reported
412 * as incomplete, friggin useless </rant>
414 if (trace
->nr_entries
!= 0 &&
415 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
418 trace
->max_entries
= trace
->nr_entries
;
420 nr_stack_trace_entries
+= trace
->nr_entries
;
422 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
423 if (!debug_locks_off_graph_unlock())
426 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
435 unsigned int nr_hardirq_chains
;
436 unsigned int nr_softirq_chains
;
437 unsigned int nr_process_chains
;
438 unsigned int max_lockdep_depth
;
440 #ifdef CONFIG_DEBUG_LOCKDEP
442 * Various lockdep statistics:
444 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
451 #define __USAGE(__STATE) \
452 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
453 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
454 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
455 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
457 static const char *usage_str
[] =
459 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460 #include "lockdep_states.h"
462 [LOCK_USED
] = "INITIAL USE",
465 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
467 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
470 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
475 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
479 if (class->usage_mask
& lock_flag(bit
+ 2))
481 if (class->usage_mask
& lock_flag(bit
)) {
483 if (class->usage_mask
& lock_flag(bit
+ 2))
490 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
494 #define LOCKDEP_STATE(__STATE) \
495 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
496 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
497 #include "lockdep_states.h"
503 static void __print_lock_name(struct lock_class
*class)
505 char str
[KSYM_NAME_LEN
];
510 name
= __get_key_name(class->key
, str
);
511 printk(KERN_CONT
"%s", name
);
513 printk(KERN_CONT
"%s", name
);
514 if (class->name_version
> 1)
515 printk(KERN_CONT
"#%d", class->name_version
);
517 printk(KERN_CONT
"/%d", class->subclass
);
521 static void print_lock_name(struct lock_class
*class)
523 char usage
[LOCK_USAGE_CHARS
];
525 get_usage_chars(class, usage
);
527 printk(KERN_CONT
" (");
528 __print_lock_name(class);
529 printk(KERN_CONT
"){%s}", usage
);
532 static void print_lockdep_cache(struct lockdep_map
*lock
)
535 char str
[KSYM_NAME_LEN
];
539 name
= __get_key_name(lock
->key
->subkeys
, str
);
541 printk(KERN_CONT
"%s", name
);
544 static void print_lock(struct held_lock
*hlock
)
547 * We can be called locklessly through debug_show_all_locks() so be
548 * extra careful, the hlock might have been released and cleared.
550 unsigned int class_idx
= hlock
->class_idx
;
552 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
555 if (!class_idx
|| (class_idx
- 1) >= MAX_LOCKDEP_KEYS
) {
556 printk(KERN_CONT
"<RELEASED>\n");
560 print_lock_name(lock_classes
+ class_idx
- 1);
561 printk(KERN_CONT
", at: [<%p>] %pS\n",
562 (void *)hlock
->acquire_ip
, (void *)hlock
->acquire_ip
);
565 static void lockdep_print_held_locks(struct task_struct
*curr
)
567 int i
, depth
= curr
->lockdep_depth
;
570 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
573 printk("%d lock%s held by %s/%d:\n",
574 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
576 for (i
= 0; i
< depth
; i
++) {
578 print_lock(curr
->held_locks
+ i
);
582 static void print_kernel_ident(void)
584 printk("%s %.*s %s\n", init_utsname()->release
,
585 (int)strcspn(init_utsname()->version
, " "),
586 init_utsname()->version
,
590 static int very_verbose(struct lock_class
*class)
593 return class_filter(class);
599 * Is this the address of a static object:
602 static int static_obj(void *obj
)
604 unsigned long start
= (unsigned long) &_stext
,
605 end
= (unsigned long) &_end
,
606 addr
= (unsigned long) obj
;
611 if ((addr
>= start
) && (addr
< end
))
614 if (arch_is_kernel_data(addr
))
618 * in-kernel percpu var?
620 if (is_kernel_percpu_address(addr
))
624 * module static or percpu var?
626 return is_module_address(addr
) || is_module_percpu_address(addr
);
631 * To make lock name printouts unique, we calculate a unique
632 * class->name_version generation counter:
634 static int count_matching_names(struct lock_class
*new_class
)
636 struct lock_class
*class;
639 if (!new_class
->name
)
642 list_for_each_entry_rcu(class, &all_lock_classes
, lock_entry
) {
643 if (new_class
->key
- new_class
->subclass
== class->key
)
644 return class->name_version
;
645 if (class->name
&& !strcmp(class->name
, new_class
->name
))
646 count
= max(count
, class->name_version
);
653 * Register a lock's class in the hash-table, if the class is not present
654 * yet. Otherwise we look it up. We cache the result in the lock object
655 * itself, so actual lookup of the hash should be once per lock object.
657 static inline struct lock_class
*
658 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
660 struct lockdep_subclass_key
*key
;
661 struct hlist_head
*hash_head
;
662 struct lock_class
*class;
664 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
667 "BUG: looking up invalid subclass: %u\n", subclass
);
669 "turning off the locking correctness validator.\n");
675 * Static locks do not have their class-keys yet - for them the key
676 * is the lock object itself:
678 if (unlikely(!lock
->key
))
679 lock
->key
= (void *)lock
;
682 * NOTE: the class-key must be unique. For dynamic locks, a static
683 * lock_class_key variable is passed in through the mutex_init()
684 * (or spin_lock_init()) call - which acts as the key. For static
685 * locks we use the lock object itself as the key.
687 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
688 sizeof(struct lockdep_map
));
690 key
= lock
->key
->subkeys
+ subclass
;
692 hash_head
= classhashentry(key
);
695 * We do an RCU walk of the hash, see lockdep_free_key_range().
697 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
700 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
701 if (class->key
== key
) {
703 * Huh! same key, different name? Did someone trample
704 * on some memory? We're most confused.
706 WARN_ON_ONCE(class->name
!= lock
->name
);
715 * Register a lock's class in the hash-table, if the class is not present
716 * yet. Otherwise we look it up. We cache the result in the lock object
717 * itself, so actual lookup of the hash should be once per lock object.
719 static struct lock_class
*
720 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
722 struct lockdep_subclass_key
*key
;
723 struct hlist_head
*hash_head
;
724 struct lock_class
*class;
726 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
728 class = look_up_lock_class(lock
, subclass
);
730 goto out_set_class_cache
;
733 * Debug-check: all keys must be persistent!
735 if (!static_obj(lock
->key
)) {
737 printk("INFO: trying to register non-static key.\n");
738 printk("the code is fine but needs lockdep annotation.\n");
739 printk("turning off the locking correctness validator.\n");
745 key
= lock
->key
->subkeys
+ subclass
;
746 hash_head
= classhashentry(key
);
752 * We have to do the hash-walk again, to avoid races
755 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
756 if (class->key
== key
)
761 * Allocate a new key from the static array, and add it to
764 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
765 if (!debug_locks_off_graph_unlock()) {
769 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
773 class = lock_classes
+ nr_lock_classes
++;
774 debug_atomic_inc(nr_unused_locks
);
776 class->name
= lock
->name
;
777 class->subclass
= subclass
;
778 INIT_LIST_HEAD(&class->lock_entry
);
779 INIT_LIST_HEAD(&class->locks_before
);
780 INIT_LIST_HEAD(&class->locks_after
);
781 class->name_version
= count_matching_names(class);
783 * We use RCU's safe list-add method to make
784 * parallel walking of the hash-list safe:
786 hlist_add_head_rcu(&class->hash_entry
, hash_head
);
788 * Add it to the global list of classes:
790 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
792 if (verbose(class)) {
795 printk("\nnew class %p: %s", class->key
, class->name
);
796 if (class->name_version
> 1)
797 printk(KERN_CONT
"#%d", class->name_version
);
798 printk(KERN_CONT
"\n");
809 if (!subclass
|| force
)
810 lock
->class_cache
[0] = class;
811 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
812 lock
->class_cache
[subclass
] = class;
815 * Hash collision, did we smoke some? We found a class with a matching
816 * hash but the subclass -- which is hashed in -- didn't match.
818 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
824 #ifdef CONFIG_PROVE_LOCKING
826 * Allocate a lockdep entry. (assumes the graph_lock held, returns
827 * with NULL on failure)
829 static struct lock_list
*alloc_list_entry(void)
831 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
832 if (!debug_locks_off_graph_unlock())
835 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
839 return list_entries
+ nr_list_entries
++;
843 * Add a new dependency to the head of the list:
845 static int add_lock_to_list(struct lock_class
*this, struct list_head
*head
,
846 unsigned long ip
, int distance
,
847 struct stack_trace
*trace
)
849 struct lock_list
*entry
;
851 * Lock not present yet - get a new dependency struct and
852 * add it to the list:
854 entry
= alloc_list_entry();
859 entry
->distance
= distance
;
860 entry
->trace
= *trace
;
862 * Both allocation and removal are done under the graph lock; but
863 * iteration is under RCU-sched; see look_up_lock_class() and
864 * lockdep_free_key_range().
866 list_add_tail_rcu(&entry
->entry
, head
);
872 * For good efficiency of modular, we use power of 2
874 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
875 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
878 * The circular_queue and helpers is used to implement the
879 * breadth-first search(BFS)algorithem, by which we can build
880 * the shortest path from the next lock to be acquired to the
881 * previous held lock if there is a circular between them.
883 struct circular_queue
{
884 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
885 unsigned int front
, rear
;
888 static struct circular_queue lock_cq
;
890 unsigned int max_bfs_queue_depth
;
892 static unsigned int lockdep_dependency_gen_id
;
894 static inline void __cq_init(struct circular_queue
*cq
)
896 cq
->front
= cq
->rear
= 0;
897 lockdep_dependency_gen_id
++;
900 static inline int __cq_empty(struct circular_queue
*cq
)
902 return (cq
->front
== cq
->rear
);
905 static inline int __cq_full(struct circular_queue
*cq
)
907 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
910 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
915 cq
->element
[cq
->rear
] = elem
;
916 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
920 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
925 *elem
= cq
->element
[cq
->front
];
926 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
930 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
932 return (cq
->rear
- cq
->front
) & CQ_MASK
;
935 static inline void mark_lock_accessed(struct lock_list
*lock
,
936 struct lock_list
*parent
)
940 nr
= lock
- list_entries
;
941 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
942 lock
->parent
= parent
;
943 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
946 static inline unsigned long lock_accessed(struct lock_list
*lock
)
950 nr
= lock
- list_entries
;
951 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
952 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
955 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
957 return child
->parent
;
960 static inline int get_lock_depth(struct lock_list
*child
)
963 struct lock_list
*parent
;
965 while ((parent
= get_lock_parent(child
))) {
972 static int __bfs(struct lock_list
*source_entry
,
974 int (*match
)(struct lock_list
*entry
, void *data
),
975 struct lock_list
**target_entry
,
978 struct lock_list
*entry
;
979 struct list_head
*head
;
980 struct circular_queue
*cq
= &lock_cq
;
983 if (match(source_entry
, data
)) {
984 *target_entry
= source_entry
;
990 head
= &source_entry
->class->locks_after
;
992 head
= &source_entry
->class->locks_before
;
994 if (list_empty(head
))
998 __cq_enqueue(cq
, (unsigned long)source_entry
);
1000 while (!__cq_empty(cq
)) {
1001 struct lock_list
*lock
;
1003 __cq_dequeue(cq
, (unsigned long *)&lock
);
1011 head
= &lock
->class->locks_after
;
1013 head
= &lock
->class->locks_before
;
1015 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1017 list_for_each_entry_rcu(entry
, head
, entry
) {
1018 if (!lock_accessed(entry
)) {
1019 unsigned int cq_depth
;
1020 mark_lock_accessed(entry
, lock
);
1021 if (match(entry
, data
)) {
1022 *target_entry
= entry
;
1027 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1031 cq_depth
= __cq_get_elem_count(cq
);
1032 if (max_bfs_queue_depth
< cq_depth
)
1033 max_bfs_queue_depth
= cq_depth
;
1041 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1043 int (*match
)(struct lock_list
*entry
, void *data
),
1044 struct lock_list
**target_entry
)
1046 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1050 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1052 int (*match
)(struct lock_list
*entry
, void *data
),
1053 struct lock_list
**target_entry
)
1055 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1060 * Recursive, forwards-direction lock-dependency checking, used for
1061 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1066 * Print a dependency chain entry (this is only done when a deadlock
1067 * has been detected):
1070 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1072 if (debug_locks_silent
)
1074 printk("\n-> #%u", depth
);
1075 print_lock_name(target
->class);
1076 printk(KERN_CONT
":\n");
1077 print_stack_trace(&target
->trace
, 6);
1083 print_circular_lock_scenario(struct held_lock
*src
,
1084 struct held_lock
*tgt
,
1085 struct lock_list
*prt
)
1087 struct lock_class
*source
= hlock_class(src
);
1088 struct lock_class
*target
= hlock_class(tgt
);
1089 struct lock_class
*parent
= prt
->class;
1092 * A direct locking problem where unsafe_class lock is taken
1093 * directly by safe_class lock, then all we need to show
1094 * is the deadlock scenario, as it is obvious that the
1095 * unsafe lock is taken under the safe lock.
1097 * But if there is a chain instead, where the safe lock takes
1098 * an intermediate lock (middle_class) where this lock is
1099 * not the same as the safe lock, then the lock chain is
1100 * used to describe the problem. Otherwise we would need
1101 * to show a different CPU case for each link in the chain
1102 * from the safe_class lock to the unsafe_class lock.
1104 if (parent
!= source
) {
1105 printk("Chain exists of:\n ");
1106 __print_lock_name(source
);
1107 printk(KERN_CONT
" --> ");
1108 __print_lock_name(parent
);
1109 printk(KERN_CONT
" --> ");
1110 __print_lock_name(target
);
1111 printk(KERN_CONT
"\n\n");
1114 printk(" Possible unsafe locking scenario:\n\n");
1115 printk(" CPU0 CPU1\n");
1116 printk(" ---- ----\n");
1118 __print_lock_name(target
);
1119 printk(KERN_CONT
");\n");
1121 __print_lock_name(parent
);
1122 printk(KERN_CONT
");\n");
1124 __print_lock_name(target
);
1125 printk(KERN_CONT
");\n");
1127 __print_lock_name(source
);
1128 printk(KERN_CONT
");\n");
1129 printk("\n *** DEADLOCK ***\n\n");
1133 * When a circular dependency is detected, print the
1137 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1138 struct held_lock
*check_src
,
1139 struct held_lock
*check_tgt
)
1141 struct task_struct
*curr
= current
;
1143 if (debug_locks_silent
)
1147 printk("======================================================\n");
1148 printk("[ INFO: possible circular locking dependency detected ]\n");
1149 print_kernel_ident();
1150 printk("-------------------------------------------------------\n");
1151 printk("%s/%d is trying to acquire lock:\n",
1152 curr
->comm
, task_pid_nr(curr
));
1153 print_lock(check_src
);
1154 printk("\nbut task is already holding lock:\n");
1155 print_lock(check_tgt
);
1156 printk("\nwhich lock already depends on the new lock.\n\n");
1157 printk("\nthe existing dependency chain (in reverse order) is:\n");
1159 print_circular_bug_entry(entry
, depth
);
1164 static inline int class_equal(struct lock_list
*entry
, void *data
)
1166 return entry
->class == data
;
1169 static noinline
int print_circular_bug(struct lock_list
*this,
1170 struct lock_list
*target
,
1171 struct held_lock
*check_src
,
1172 struct held_lock
*check_tgt
)
1174 struct task_struct
*curr
= current
;
1175 struct lock_list
*parent
;
1176 struct lock_list
*first_parent
;
1179 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1182 if (!save_trace(&this->trace
))
1185 depth
= get_lock_depth(target
);
1187 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1189 parent
= get_lock_parent(target
);
1190 first_parent
= parent
;
1193 print_circular_bug_entry(parent
, --depth
);
1194 parent
= get_lock_parent(parent
);
1197 printk("\nother info that might help us debug this:\n\n");
1198 print_circular_lock_scenario(check_src
, check_tgt
,
1201 lockdep_print_held_locks(curr
);
1203 printk("\nstack backtrace:\n");
1209 static noinline
int print_bfs_bug(int ret
)
1211 if (!debug_locks_off_graph_unlock())
1215 * Breadth-first-search failed, graph got corrupted?
1217 WARN(1, "lockdep bfs error:%d\n", ret
);
1222 static int noop_count(struct lock_list
*entry
, void *data
)
1224 (*(unsigned long *)data
)++;
1228 static unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1230 unsigned long count
= 0;
1231 struct lock_list
*uninitialized_var(target_entry
);
1233 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1237 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1239 unsigned long ret
, flags
;
1240 struct lock_list
this;
1245 local_irq_save(flags
);
1246 arch_spin_lock(&lockdep_lock
);
1247 ret
= __lockdep_count_forward_deps(&this);
1248 arch_spin_unlock(&lockdep_lock
);
1249 local_irq_restore(flags
);
1254 static unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1256 unsigned long count
= 0;
1257 struct lock_list
*uninitialized_var(target_entry
);
1259 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1264 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1266 unsigned long ret
, flags
;
1267 struct lock_list
this;
1272 local_irq_save(flags
);
1273 arch_spin_lock(&lockdep_lock
);
1274 ret
= __lockdep_count_backward_deps(&this);
1275 arch_spin_unlock(&lockdep_lock
);
1276 local_irq_restore(flags
);
1282 * Prove that the dependency graph starting at <entry> can not
1283 * lead to <target>. Print an error and return 0 if it does.
1286 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1287 struct lock_list
**target_entry
)
1291 debug_atomic_inc(nr_cyclic_checks
);
1293 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1298 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1300 * Forwards and backwards subgraph searching, for the purposes of
1301 * proving that two subgraphs can be connected by a new dependency
1302 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1305 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1307 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1313 * Find a node in the forwards-direction dependency sub-graph starting
1314 * at @root->class that matches @bit.
1316 * Return 0 if such a node exists in the subgraph, and put that node
1317 * into *@target_entry.
1319 * Return 1 otherwise and keep *@target_entry unchanged.
1320 * Return <0 on error.
1323 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1324 struct lock_list
**target_entry
)
1328 debug_atomic_inc(nr_find_usage_forwards_checks
);
1330 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1336 * Find a node in the backwards-direction dependency sub-graph starting
1337 * at @root->class that matches @bit.
1339 * Return 0 if such a node exists in the subgraph, and put that node
1340 * into *@target_entry.
1342 * Return 1 otherwise and keep *@target_entry unchanged.
1343 * Return <0 on error.
1346 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1347 struct lock_list
**target_entry
)
1351 debug_atomic_inc(nr_find_usage_backwards_checks
);
1353 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1358 static void print_lock_class_header(struct lock_class
*class, int depth
)
1362 printk("%*s->", depth
, "");
1363 print_lock_name(class);
1364 printk(KERN_CONT
" ops: %lu", class->ops
);
1365 printk(KERN_CONT
" {\n");
1367 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1368 if (class->usage_mask
& (1 << bit
)) {
1371 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1372 len
+= printk(KERN_CONT
" at:\n");
1373 print_stack_trace(class->usage_traces
+ bit
, len
);
1376 printk("%*s }\n", depth
, "");
1378 printk("%*s ... key at: [<%p>] %pS\n",
1379 depth
, "", class->key
, class->key
);
1383 * printk the shortest lock dependencies from @start to @end in reverse order:
1386 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1387 struct lock_list
*root
)
1389 struct lock_list
*entry
= leaf
;
1392 /*compute depth from generated tree by BFS*/
1393 depth
= get_lock_depth(leaf
);
1396 print_lock_class_header(entry
->class, depth
);
1397 printk("%*s ... acquired at:\n", depth
, "");
1398 print_stack_trace(&entry
->trace
, 2);
1401 if (depth
== 0 && (entry
!= root
)) {
1402 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1406 entry
= get_lock_parent(entry
);
1408 } while (entry
&& (depth
>= 0));
1414 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1415 struct lock_list
*unsafe_entry
,
1416 struct lock_class
*prev_class
,
1417 struct lock_class
*next_class
)
1419 struct lock_class
*safe_class
= safe_entry
->class;
1420 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1421 struct lock_class
*middle_class
= prev_class
;
1423 if (middle_class
== safe_class
)
1424 middle_class
= next_class
;
1427 * A direct locking problem where unsafe_class lock is taken
1428 * directly by safe_class lock, then all we need to show
1429 * is the deadlock scenario, as it is obvious that the
1430 * unsafe lock is taken under the safe lock.
1432 * But if there is a chain instead, where the safe lock takes
1433 * an intermediate lock (middle_class) where this lock is
1434 * not the same as the safe lock, then the lock chain is
1435 * used to describe the problem. Otherwise we would need
1436 * to show a different CPU case for each link in the chain
1437 * from the safe_class lock to the unsafe_class lock.
1439 if (middle_class
!= unsafe_class
) {
1440 printk("Chain exists of:\n ");
1441 __print_lock_name(safe_class
);
1442 printk(KERN_CONT
" --> ");
1443 __print_lock_name(middle_class
);
1444 printk(KERN_CONT
" --> ");
1445 __print_lock_name(unsafe_class
);
1446 printk(KERN_CONT
"\n\n");
1449 printk(" Possible interrupt unsafe locking scenario:\n\n");
1450 printk(" CPU0 CPU1\n");
1451 printk(" ---- ----\n");
1453 __print_lock_name(unsafe_class
);
1454 printk(KERN_CONT
");\n");
1455 printk(" local_irq_disable();\n");
1457 __print_lock_name(safe_class
);
1458 printk(KERN_CONT
");\n");
1460 __print_lock_name(middle_class
);
1461 printk(KERN_CONT
");\n");
1462 printk(" <Interrupt>\n");
1464 __print_lock_name(safe_class
);
1465 printk(KERN_CONT
");\n");
1466 printk("\n *** DEADLOCK ***\n\n");
1470 print_bad_irq_dependency(struct task_struct
*curr
,
1471 struct lock_list
*prev_root
,
1472 struct lock_list
*next_root
,
1473 struct lock_list
*backwards_entry
,
1474 struct lock_list
*forwards_entry
,
1475 struct held_lock
*prev
,
1476 struct held_lock
*next
,
1477 enum lock_usage_bit bit1
,
1478 enum lock_usage_bit bit2
,
1479 const char *irqclass
)
1481 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1485 printk("======================================================\n");
1486 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1487 irqclass
, irqclass
);
1488 print_kernel_ident();
1489 printk("------------------------------------------------------\n");
1490 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1491 curr
->comm
, task_pid_nr(curr
),
1492 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1493 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1494 curr
->hardirqs_enabled
,
1495 curr
->softirqs_enabled
);
1498 printk("\nand this task is already holding:\n");
1500 printk("which would create a new lock dependency:\n");
1501 print_lock_name(hlock_class(prev
));
1502 printk(KERN_CONT
" ->");
1503 print_lock_name(hlock_class(next
));
1504 printk(KERN_CONT
"\n");
1506 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1508 print_lock_name(backwards_entry
->class);
1509 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1511 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1513 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1514 print_lock_name(forwards_entry
->class);
1515 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1518 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1520 printk("\nother info that might help us debug this:\n\n");
1521 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1522 hlock_class(prev
), hlock_class(next
));
1524 lockdep_print_held_locks(curr
);
1526 printk("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass
);
1527 if (!save_trace(&prev_root
->trace
))
1529 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1531 printk("\nthe dependencies between the lock to be acquired");
1532 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1533 if (!save_trace(&next_root
->trace
))
1535 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1537 printk("\nstack backtrace:\n");
1544 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1545 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1546 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1549 struct lock_list
this, that
;
1550 struct lock_list
*uninitialized_var(target_entry
);
1551 struct lock_list
*uninitialized_var(target_entry1
);
1555 this.class = hlock_class(prev
);
1556 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1558 return print_bfs_bug(ret
);
1563 that
.class = hlock_class(next
);
1564 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1566 return print_bfs_bug(ret
);
1570 return print_bad_irq_dependency(curr
, &this, &that
,
1571 target_entry
, target_entry1
,
1573 bit_backwards
, bit_forwards
, irqclass
);
1576 static const char *state_names
[] = {
1577 #define LOCKDEP_STATE(__STATE) \
1578 __stringify(__STATE),
1579 #include "lockdep_states.h"
1580 #undef LOCKDEP_STATE
1583 static const char *state_rnames
[] = {
1584 #define LOCKDEP_STATE(__STATE) \
1585 __stringify(__STATE)"-READ",
1586 #include "lockdep_states.h"
1587 #undef LOCKDEP_STATE
1590 static inline const char *state_name(enum lock_usage_bit bit
)
1592 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1595 static int exclusive_bit(int new_bit
)
1603 * bit 0 - write/read
1604 * bit 1 - used_in/enabled
1608 int state
= new_bit
& ~3;
1609 int dir
= new_bit
& 2;
1612 * keep state, bit flip the direction and strip read.
1614 return state
| (dir
^ 2);
1617 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1618 struct held_lock
*next
, enum lock_usage_bit bit
)
1621 * Prove that the new dependency does not connect a hardirq-safe
1622 * lock with a hardirq-unsafe lock - to achieve this we search
1623 * the backwards-subgraph starting at <prev>, and the
1624 * forwards-subgraph starting at <next>:
1626 if (!check_usage(curr
, prev
, next
, bit
,
1627 exclusive_bit(bit
), state_name(bit
)))
1633 * Prove that the new dependency does not connect a hardirq-safe-read
1634 * lock with a hardirq-unsafe lock - to achieve this we search
1635 * the backwards-subgraph starting at <prev>, and the
1636 * forwards-subgraph starting at <next>:
1638 if (!check_usage(curr
, prev
, next
, bit
,
1639 exclusive_bit(bit
), state_name(bit
)))
1646 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1647 struct held_lock
*next
)
1649 #define LOCKDEP_STATE(__STATE) \
1650 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1652 #include "lockdep_states.h"
1653 #undef LOCKDEP_STATE
1658 static void inc_chains(void)
1660 if (current
->hardirq_context
)
1661 nr_hardirq_chains
++;
1663 if (current
->softirq_context
)
1664 nr_softirq_chains
++;
1666 nr_process_chains
++;
1673 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1674 struct held_lock
*next
)
1679 static inline void inc_chains(void)
1681 nr_process_chains
++;
1687 print_deadlock_scenario(struct held_lock
*nxt
,
1688 struct held_lock
*prv
)
1690 struct lock_class
*next
= hlock_class(nxt
);
1691 struct lock_class
*prev
= hlock_class(prv
);
1693 printk(" Possible unsafe locking scenario:\n\n");
1697 __print_lock_name(prev
);
1698 printk(KERN_CONT
");\n");
1700 __print_lock_name(next
);
1701 printk(KERN_CONT
");\n");
1702 printk("\n *** DEADLOCK ***\n\n");
1703 printk(" May be due to missing lock nesting notation\n\n");
1707 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1708 struct held_lock
*next
)
1710 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1714 printk("=============================================\n");
1715 printk("[ INFO: possible recursive locking detected ]\n");
1716 print_kernel_ident();
1717 printk("---------------------------------------------\n");
1718 printk("%s/%d is trying to acquire lock:\n",
1719 curr
->comm
, task_pid_nr(curr
));
1721 printk("\nbut task is already holding lock:\n");
1724 printk("\nother info that might help us debug this:\n");
1725 print_deadlock_scenario(next
, prev
);
1726 lockdep_print_held_locks(curr
);
1728 printk("\nstack backtrace:\n");
1735 * Check whether we are holding such a class already.
1737 * (Note that this has to be done separately, because the graph cannot
1738 * detect such classes of deadlocks.)
1740 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1743 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1744 struct lockdep_map
*next_instance
, int read
)
1746 struct held_lock
*prev
;
1747 struct held_lock
*nest
= NULL
;
1750 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1751 prev
= curr
->held_locks
+ i
;
1753 if (prev
->instance
== next
->nest_lock
)
1756 if (hlock_class(prev
) != hlock_class(next
))
1760 * Allow read-after-read recursion of the same
1761 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1763 if ((read
== 2) && prev
->read
)
1767 * We're holding the nest_lock, which serializes this lock's
1768 * nesting behaviour.
1773 return print_deadlock_bug(curr
, prev
, next
);
1779 * There was a chain-cache miss, and we are about to add a new dependency
1780 * to a previous lock. We recursively validate the following rules:
1782 * - would the adding of the <prev> -> <next> dependency create a
1783 * circular dependency in the graph? [== circular deadlock]
1785 * - does the new prev->next dependency connect any hardirq-safe lock
1786 * (in the full backwards-subgraph starting at <prev>) with any
1787 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1788 * <next>)? [== illegal lock inversion with hardirq contexts]
1790 * - does the new prev->next dependency connect any softirq-safe lock
1791 * (in the full backwards-subgraph starting at <prev>) with any
1792 * softirq-unsafe lock (in the full forwards-subgraph starting at
1793 * <next>)? [== illegal lock inversion with softirq contexts]
1795 * any of these scenarios could lead to a deadlock.
1797 * Then if all the validations pass, we add the forwards and backwards
1801 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1802 struct held_lock
*next
, int distance
, int *stack_saved
)
1804 struct lock_list
*entry
;
1806 struct lock_list
this;
1807 struct lock_list
*uninitialized_var(target_entry
);
1809 * Static variable, serialized by the graph_lock().
1811 * We use this static variable to save the stack trace in case
1812 * we call into this function multiple times due to encountering
1813 * trylocks in the held lock stack.
1815 static struct stack_trace trace
;
1818 * Prove that the new <prev> -> <next> dependency would not
1819 * create a circular dependency in the graph. (We do this by
1820 * forward-recursing into the graph starting at <next>, and
1821 * checking whether we can reach <prev>.)
1823 * We are using global variables to control the recursion, to
1824 * keep the stackframe size of the recursive functions low:
1826 this.class = hlock_class(next
);
1828 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1830 return print_circular_bug(&this, target_entry
, next
, prev
);
1831 else if (unlikely(ret
< 0))
1832 return print_bfs_bug(ret
);
1834 if (!check_prev_add_irq(curr
, prev
, next
))
1838 * For recursive read-locks we do all the dependency checks,
1839 * but we dont store read-triggered dependencies (only
1840 * write-triggered dependencies). This ensures that only the
1841 * write-side dependencies matter, and that if for example a
1842 * write-lock never takes any other locks, then the reads are
1843 * equivalent to a NOP.
1845 if (next
->read
== 2 || prev
->read
== 2)
1848 * Is the <prev> -> <next> dependency already present?
1850 * (this may occur even though this is a new chain: consider
1851 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1852 * chains - the second one will be new, but L1 already has
1853 * L2 added to its dependency list, due to the first chain.)
1855 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1856 if (entry
->class == hlock_class(next
)) {
1858 entry
->distance
= 1;
1863 if (!*stack_saved
) {
1864 if (!save_trace(&trace
))
1870 * Ok, all validations passed, add the new lock
1871 * to the previous lock's dependency list:
1873 ret
= add_lock_to_list(hlock_class(next
),
1874 &hlock_class(prev
)->locks_after
,
1875 next
->acquire_ip
, distance
, &trace
);
1880 ret
= add_lock_to_list(hlock_class(prev
),
1881 &hlock_class(next
)->locks_before
,
1882 next
->acquire_ip
, distance
, &trace
);
1887 * Debugging printouts:
1889 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1890 /* We drop graph lock, so another thread can overwrite trace. */
1893 printk("\n new dependency: ");
1894 print_lock_name(hlock_class(prev
));
1895 printk(KERN_CONT
" => ");
1896 print_lock_name(hlock_class(next
));
1897 printk(KERN_CONT
"\n");
1899 return graph_lock();
1905 * Add the dependency to all directly-previous locks that are 'relevant'.
1906 * The ones that are relevant are (in increasing distance from curr):
1907 * all consecutive trylock entries and the final non-trylock entry - or
1908 * the end of this context's lock-chain - whichever comes first.
1911 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1913 int depth
= curr
->lockdep_depth
;
1914 int stack_saved
= 0;
1915 struct held_lock
*hlock
;
1920 * Depth must not be zero for a non-head lock:
1925 * At least two relevant locks must exist for this
1928 if (curr
->held_locks
[depth
].irq_context
!=
1929 curr
->held_locks
[depth
-1].irq_context
)
1933 int distance
= curr
->lockdep_depth
- depth
+ 1;
1934 hlock
= curr
->held_locks
+ depth
- 1;
1936 * Only non-recursive-read entries get new dependencies
1939 if (hlock
->read
!= 2 && hlock
->check
) {
1940 if (!check_prev_add(curr
, hlock
, next
,
1941 distance
, &stack_saved
))
1944 * Stop after the first non-trylock entry,
1945 * as non-trylock entries have added their
1946 * own direct dependencies already, so this
1947 * lock is connected to them indirectly:
1949 if (!hlock
->trylock
)
1954 * End of lock-stack?
1959 * Stop the search if we cross into another context:
1961 if (curr
->held_locks
[depth
].irq_context
!=
1962 curr
->held_locks
[depth
-1].irq_context
)
1967 if (!debug_locks_off_graph_unlock())
1971 * Clearly we all shouldn't be here, but since we made it we
1972 * can reliable say we messed up our state. See the above two
1973 * gotos for reasons why we could possibly end up here.
1980 unsigned long nr_lock_chains
;
1981 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1982 int nr_chain_hlocks
;
1983 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
1985 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
1987 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
1991 * Returns the index of the first held_lock of the current chain
1993 static inline int get_first_held_lock(struct task_struct
*curr
,
1994 struct held_lock
*hlock
)
1997 struct held_lock
*hlock_curr
;
1999 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2000 hlock_curr
= curr
->held_locks
+ i
;
2001 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2009 #ifdef CONFIG_DEBUG_LOCKDEP
2011 * Returns the next chain_key iteration
2013 static u64
print_chain_key_iteration(int class_idx
, u64 chain_key
)
2015 u64 new_chain_key
= iterate_chain_key(chain_key
, class_idx
);
2017 printk(" class_idx:%d -> chain_key:%016Lx",
2019 (unsigned long long)new_chain_key
);
2020 return new_chain_key
;
2024 print_chain_keys_held_locks(struct task_struct
*curr
, struct held_lock
*hlock_next
)
2026 struct held_lock
*hlock
;
2028 int depth
= curr
->lockdep_depth
;
2031 printk("depth: %u\n", depth
+ 1);
2032 for (i
= get_first_held_lock(curr
, hlock_next
); i
< depth
; i
++) {
2033 hlock
= curr
->held_locks
+ i
;
2034 chain_key
= print_chain_key_iteration(hlock
->class_idx
, chain_key
);
2039 print_chain_key_iteration(hlock_next
->class_idx
, chain_key
);
2040 print_lock(hlock_next
);
2043 static void print_chain_keys_chain(struct lock_chain
*chain
)
2049 printk("depth: %u\n", chain
->depth
);
2050 for (i
= 0; i
< chain
->depth
; i
++) {
2051 class_id
= chain_hlocks
[chain
->base
+ i
];
2052 chain_key
= print_chain_key_iteration(class_id
+ 1, chain_key
);
2054 print_lock_name(lock_classes
+ class_id
);
2059 static void print_collision(struct task_struct
*curr
,
2060 struct held_lock
*hlock_next
,
2061 struct lock_chain
*chain
)
2064 printk("======================\n");
2065 printk("[chain_key collision ]\n");
2066 print_kernel_ident();
2067 printk("----------------------\n");
2068 printk("%s/%d: ", current
->comm
, task_pid_nr(current
));
2069 printk("Hash chain already cached but the contents don't match!\n");
2071 printk("Held locks:");
2072 print_chain_keys_held_locks(curr
, hlock_next
);
2074 printk("Locks in cached chain:");
2075 print_chain_keys_chain(chain
);
2077 printk("\nstack backtrace:\n");
2083 * Checks whether the chain and the current held locks are consistent
2084 * in depth and also in content. If they are not it most likely means
2085 * that there was a collision during the calculation of the chain_key.
2086 * Returns: 0 not passed, 1 passed
2088 static int check_no_collision(struct task_struct
*curr
,
2089 struct held_lock
*hlock
,
2090 struct lock_chain
*chain
)
2092 #ifdef CONFIG_DEBUG_LOCKDEP
2095 i
= get_first_held_lock(curr
, hlock
);
2097 if (DEBUG_LOCKS_WARN_ON(chain
->depth
!= curr
->lockdep_depth
- (i
- 1))) {
2098 print_collision(curr
, hlock
, chain
);
2102 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2103 id
= curr
->held_locks
[i
].class_idx
- 1;
2105 if (DEBUG_LOCKS_WARN_ON(chain_hlocks
[chain
->base
+ j
] != id
)) {
2106 print_collision(curr
, hlock
, chain
);
2115 * Look up a dependency chain. If the key is not present yet then
2116 * add it and return 1 - in this case the new dependency chain is
2117 * validated. If the key is already hashed, return 0.
2118 * (On return with 1 graph_lock is held.)
2120 static inline int lookup_chain_cache(struct task_struct
*curr
,
2121 struct held_lock
*hlock
,
2124 struct lock_class
*class = hlock_class(hlock
);
2125 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
2126 struct lock_chain
*chain
;
2130 * We might need to take the graph lock, ensure we've got IRQs
2131 * disabled to make this an IRQ-safe lock.. for recursion reasons
2132 * lockdep won't complain about its own locking errors.
2134 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2137 * We can walk it lock-free, because entries only get added
2140 hlist_for_each_entry_rcu(chain
, hash_head
, entry
) {
2141 if (chain
->chain_key
== chain_key
) {
2143 debug_atomic_inc(chain_lookup_hits
);
2144 if (!check_no_collision(curr
, hlock
, chain
))
2147 if (very_verbose(class))
2148 printk("\nhash chain already cached, key: "
2149 "%016Lx tail class: [%p] %s\n",
2150 (unsigned long long)chain_key
,
2151 class->key
, class->name
);
2155 if (very_verbose(class))
2156 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2157 (unsigned long long)chain_key
, class->key
, class->name
);
2159 * Allocate a new chain entry from the static array, and add
2165 * We have to walk the chain again locked - to avoid duplicates:
2167 hlist_for_each_entry(chain
, hash_head
, entry
) {
2168 if (chain
->chain_key
== chain_key
) {
2173 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2174 if (!debug_locks_off_graph_unlock())
2177 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2181 chain
= lock_chains
+ nr_lock_chains
++;
2182 chain
->chain_key
= chain_key
;
2183 chain
->irq_context
= hlock
->irq_context
;
2184 i
= get_first_held_lock(curr
, hlock
);
2185 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2187 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks
));
2188 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr
->held_locks
));
2189 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks
[0])) <= ARRAY_SIZE(lock_classes
));
2191 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2192 chain
->base
= nr_chain_hlocks
;
2193 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2194 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2195 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2197 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2200 if (nr_chain_hlocks
< MAX_LOCKDEP_CHAIN_HLOCKS
)
2201 nr_chain_hlocks
+= chain
->depth
;
2203 #ifdef CONFIG_DEBUG_LOCKDEP
2205 * Important for check_no_collision().
2207 if (unlikely(nr_chain_hlocks
> MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2208 if (!debug_locks_off_graph_unlock())
2211 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2217 hlist_add_head_rcu(&chain
->entry
, hash_head
);
2218 debug_atomic_inc(chain_lookup_misses
);
2224 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2225 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2228 * Trylock needs to maintain the stack of held locks, but it
2229 * does not add new dependencies, because trylock can be done
2232 * We look up the chain_key and do the O(N^2) check and update of
2233 * the dependencies only if this is a new dependency chain.
2234 * (If lookup_chain_cache() returns with 1 it acquires
2235 * graph_lock for us)
2237 if (!hlock
->trylock
&& hlock
->check
&&
2238 lookup_chain_cache(curr
, hlock
, chain_key
)) {
2240 * Check whether last held lock:
2242 * - is irq-safe, if this lock is irq-unsafe
2243 * - is softirq-safe, if this lock is hardirq-unsafe
2245 * And check whether the new lock's dependency graph
2246 * could lead back to the previous lock.
2248 * any of these scenarios could lead to a deadlock. If
2251 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2256 * Mark recursive read, as we jump over it when
2257 * building dependencies (just like we jump over
2263 * Add dependency only if this lock is not the head
2264 * of the chain, and if it's not a secondary read-lock:
2266 if (!chain_head
&& ret
!= 2)
2267 if (!check_prevs_add(curr
, hlock
))
2271 /* after lookup_chain_cache(): */
2272 if (unlikely(!debug_locks
))
2278 static inline int validate_chain(struct task_struct
*curr
,
2279 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2280 int chain_head
, u64 chain_key
)
2287 * We are building curr_chain_key incrementally, so double-check
2288 * it from scratch, to make sure that it's done correctly:
2290 static void check_chain_key(struct task_struct
*curr
)
2292 #ifdef CONFIG_DEBUG_LOCKDEP
2293 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2297 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2298 hlock
= curr
->held_locks
+ i
;
2299 if (chain_key
!= hlock
->prev_chain_key
) {
2302 * We got mighty confused, our chain keys don't match
2303 * with what we expect, someone trample on our task state?
2305 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2306 curr
->lockdep_depth
, i
,
2307 (unsigned long long)chain_key
,
2308 (unsigned long long)hlock
->prev_chain_key
);
2312 * Whoops ran out of static storage again?
2314 if (DEBUG_LOCKS_WARN_ON(hlock
->class_idx
> MAX_LOCKDEP_KEYS
))
2317 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2318 hlock
->irq_context
))
2320 chain_key
= iterate_chain_key(chain_key
, hlock
->class_idx
);
2323 if (chain_key
!= curr
->curr_chain_key
) {
2326 * More smoking hash instead of calculating it, damn see these
2327 * numbers float.. I bet that a pink elephant stepped on my memory.
2329 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2330 curr
->lockdep_depth
, i
,
2331 (unsigned long long)chain_key
,
2332 (unsigned long long)curr
->curr_chain_key
);
2338 print_usage_bug_scenario(struct held_lock
*lock
)
2340 struct lock_class
*class = hlock_class(lock
);
2342 printk(" Possible unsafe locking scenario:\n\n");
2346 __print_lock_name(class);
2347 printk(KERN_CONT
");\n");
2348 printk(" <Interrupt>\n");
2350 __print_lock_name(class);
2351 printk(KERN_CONT
");\n");
2352 printk("\n *** DEADLOCK ***\n\n");
2356 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2357 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2359 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2363 printk("=================================\n");
2364 printk("[ INFO: inconsistent lock state ]\n");
2365 print_kernel_ident();
2366 printk("---------------------------------\n");
2368 printk("inconsistent {%s} -> {%s} usage.\n",
2369 usage_str
[prev_bit
], usage_str
[new_bit
]);
2371 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2372 curr
->comm
, task_pid_nr(curr
),
2373 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2374 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2375 trace_hardirqs_enabled(curr
),
2376 trace_softirqs_enabled(curr
));
2379 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2380 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2382 print_irqtrace_events(curr
);
2383 printk("\nother info that might help us debug this:\n");
2384 print_usage_bug_scenario(this);
2386 lockdep_print_held_locks(curr
);
2388 printk("\nstack backtrace:\n");
2395 * Print out an error if an invalid bit is set:
2398 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2399 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2401 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2402 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2406 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2407 enum lock_usage_bit new_bit
);
2409 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2412 * print irq inversion bug:
2415 print_irq_inversion_bug(struct task_struct
*curr
,
2416 struct lock_list
*root
, struct lock_list
*other
,
2417 struct held_lock
*this, int forwards
,
2418 const char *irqclass
)
2420 struct lock_list
*entry
= other
;
2421 struct lock_list
*middle
= NULL
;
2424 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2428 printk("=========================================================\n");
2429 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
2430 print_kernel_ident();
2431 printk("---------------------------------------------------------\n");
2432 printk("%s/%d just changed the state of lock:\n",
2433 curr
->comm
, task_pid_nr(curr
));
2436 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2438 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2439 print_lock_name(other
->class);
2440 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2442 printk("\nother info that might help us debug this:\n");
2444 /* Find a middle lock (if one exists) */
2445 depth
= get_lock_depth(other
);
2447 if (depth
== 0 && (entry
!= root
)) {
2448 printk("lockdep:%s bad path found in chain graph\n", __func__
);
2452 entry
= get_lock_parent(entry
);
2454 } while (entry
&& entry
!= root
&& (depth
>= 0));
2456 print_irq_lock_scenario(root
, other
,
2457 middle
? middle
->class : root
->class, other
->class);
2459 print_irq_lock_scenario(other
, root
,
2460 middle
? middle
->class : other
->class, root
->class);
2462 lockdep_print_held_locks(curr
);
2464 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2465 if (!save_trace(&root
->trace
))
2467 print_shortest_lock_dependencies(other
, root
);
2469 printk("\nstack backtrace:\n");
2476 * Prove that in the forwards-direction subgraph starting at <this>
2477 * there is no lock matching <mask>:
2480 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2481 enum lock_usage_bit bit
, const char *irqclass
)
2484 struct lock_list root
;
2485 struct lock_list
*uninitialized_var(target_entry
);
2488 root
.class = hlock_class(this);
2489 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2491 return print_bfs_bug(ret
);
2495 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2500 * Prove that in the backwards-direction subgraph starting at <this>
2501 * there is no lock matching <mask>:
2504 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2505 enum lock_usage_bit bit
, const char *irqclass
)
2508 struct lock_list root
;
2509 struct lock_list
*uninitialized_var(target_entry
);
2512 root
.class = hlock_class(this);
2513 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2515 return print_bfs_bug(ret
);
2519 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2523 void print_irqtrace_events(struct task_struct
*curr
)
2525 printk("irq event stamp: %u\n", curr
->irq_events
);
2526 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2527 curr
->hardirq_enable_event
, (void *)curr
->hardirq_enable_ip
,
2528 (void *)curr
->hardirq_enable_ip
);
2529 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2530 curr
->hardirq_disable_event
, (void *)curr
->hardirq_disable_ip
,
2531 (void *)curr
->hardirq_disable_ip
);
2532 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2533 curr
->softirq_enable_event
, (void *)curr
->softirq_enable_ip
,
2534 (void *)curr
->softirq_enable_ip
);
2535 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2536 curr
->softirq_disable_event
, (void *)curr
->softirq_disable_ip
,
2537 (void *)curr
->softirq_disable_ip
);
2540 static int HARDIRQ_verbose(struct lock_class
*class)
2543 return class_filter(class);
2548 static int SOFTIRQ_verbose(struct lock_class
*class)
2551 return class_filter(class);
2556 static int RECLAIM_FS_verbose(struct lock_class
*class)
2559 return class_filter(class);
2564 #define STRICT_READ_CHECKS 1
2566 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2567 #define LOCKDEP_STATE(__STATE) \
2569 #include "lockdep_states.h"
2570 #undef LOCKDEP_STATE
2573 static inline int state_verbose(enum lock_usage_bit bit
,
2574 struct lock_class
*class)
2576 return state_verbose_f
[bit
>> 2](class);
2579 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2580 enum lock_usage_bit bit
, const char *name
);
2583 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2584 enum lock_usage_bit new_bit
)
2586 int excl_bit
= exclusive_bit(new_bit
);
2587 int read
= new_bit
& 1;
2588 int dir
= new_bit
& 2;
2591 * mark USED_IN has to look forwards -- to ensure no dependency
2592 * has ENABLED state, which would allow recursion deadlocks.
2594 * mark ENABLED has to look backwards -- to ensure no dependee
2595 * has USED_IN state, which, again, would allow recursion deadlocks.
2597 check_usage_f usage
= dir
?
2598 check_usage_backwards
: check_usage_forwards
;
2601 * Validate that this particular lock does not have conflicting
2604 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2608 * Validate that the lock dependencies don't have conflicting usage
2611 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2612 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2616 * Check for read in write conflicts
2619 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2622 if (STRICT_READ_CHECKS
&&
2623 !usage(curr
, this, excl_bit
+ 1,
2624 state_name(new_bit
+ 1)))
2628 if (state_verbose(new_bit
, hlock_class(this)))
2635 #define LOCKDEP_STATE(__STATE) __STATE,
2636 #include "lockdep_states.h"
2637 #undef LOCKDEP_STATE
2641 * Mark all held locks with a usage bit:
2644 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2646 enum lock_usage_bit usage_bit
;
2647 struct held_lock
*hlock
;
2650 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2651 hlock
= curr
->held_locks
+ i
;
2653 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2655 usage_bit
+= 1; /* READ */
2657 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2662 if (!mark_lock(curr
, hlock
, usage_bit
))
2670 * Hardirqs will be enabled:
2672 static void __trace_hardirqs_on_caller(unsigned long ip
)
2674 struct task_struct
*curr
= current
;
2676 /* we'll do an OFF -> ON transition: */
2677 curr
->hardirqs_enabled
= 1;
2680 * We are going to turn hardirqs on, so set the
2681 * usage bit for all held locks:
2683 if (!mark_held_locks(curr
, HARDIRQ
))
2686 * If we have softirqs enabled, then set the usage
2687 * bit for all held locks. (disabled hardirqs prevented
2688 * this bit from being set before)
2690 if (curr
->softirqs_enabled
)
2691 if (!mark_held_locks(curr
, SOFTIRQ
))
2694 curr
->hardirq_enable_ip
= ip
;
2695 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2696 debug_atomic_inc(hardirqs_on_events
);
2699 __visible
void trace_hardirqs_on_caller(unsigned long ip
)
2701 time_hardirqs_on(CALLER_ADDR0
, ip
);
2703 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2706 if (unlikely(current
->hardirqs_enabled
)) {
2708 * Neither irq nor preemption are disabled here
2709 * so this is racy by nature but losing one hit
2710 * in a stat is not a big deal.
2712 __debug_atomic_inc(redundant_hardirqs_on
);
2717 * We're enabling irqs and according to our state above irqs weren't
2718 * already enabled, yet we find the hardware thinks they are in fact
2719 * enabled.. someone messed up their IRQ state tracing.
2721 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2725 * See the fine text that goes along with this variable definition.
2727 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2731 * Can't allow enabling interrupts while in an interrupt handler,
2732 * that's general bad form and such. Recursion, limited stack etc..
2734 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2737 current
->lockdep_recursion
= 1;
2738 __trace_hardirqs_on_caller(ip
);
2739 current
->lockdep_recursion
= 0;
2741 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2743 void trace_hardirqs_on(void)
2745 trace_hardirqs_on_caller(CALLER_ADDR0
);
2747 EXPORT_SYMBOL(trace_hardirqs_on
);
2750 * Hardirqs were disabled:
2752 __visible
void trace_hardirqs_off_caller(unsigned long ip
)
2754 struct task_struct
*curr
= current
;
2756 time_hardirqs_off(CALLER_ADDR0
, ip
);
2758 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2762 * So we're supposed to get called after you mask local IRQs, but for
2763 * some reason the hardware doesn't quite think you did a proper job.
2765 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2768 if (curr
->hardirqs_enabled
) {
2770 * We have done an ON -> OFF transition:
2772 curr
->hardirqs_enabled
= 0;
2773 curr
->hardirq_disable_ip
= ip
;
2774 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2775 debug_atomic_inc(hardirqs_off_events
);
2777 debug_atomic_inc(redundant_hardirqs_off
);
2779 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2781 void trace_hardirqs_off(void)
2783 trace_hardirqs_off_caller(CALLER_ADDR0
);
2785 EXPORT_SYMBOL(trace_hardirqs_off
);
2788 * Softirqs will be enabled:
2790 void trace_softirqs_on(unsigned long ip
)
2792 struct task_struct
*curr
= current
;
2794 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2798 * We fancy IRQs being disabled here, see softirq.c, avoids
2799 * funny state and nesting things.
2801 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2804 if (curr
->softirqs_enabled
) {
2805 debug_atomic_inc(redundant_softirqs_on
);
2809 current
->lockdep_recursion
= 1;
2811 * We'll do an OFF -> ON transition:
2813 curr
->softirqs_enabled
= 1;
2814 curr
->softirq_enable_ip
= ip
;
2815 curr
->softirq_enable_event
= ++curr
->irq_events
;
2816 debug_atomic_inc(softirqs_on_events
);
2818 * We are going to turn softirqs on, so set the
2819 * usage bit for all held locks, if hardirqs are
2822 if (curr
->hardirqs_enabled
)
2823 mark_held_locks(curr
, SOFTIRQ
);
2824 current
->lockdep_recursion
= 0;
2828 * Softirqs were disabled:
2830 void trace_softirqs_off(unsigned long ip
)
2832 struct task_struct
*curr
= current
;
2834 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2838 * We fancy IRQs being disabled here, see softirq.c
2840 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2843 if (curr
->softirqs_enabled
) {
2845 * We have done an ON -> OFF transition:
2847 curr
->softirqs_enabled
= 0;
2848 curr
->softirq_disable_ip
= ip
;
2849 curr
->softirq_disable_event
= ++curr
->irq_events
;
2850 debug_atomic_inc(softirqs_off_events
);
2852 * Whoops, we wanted softirqs off, so why aren't they?
2854 DEBUG_LOCKS_WARN_ON(!softirq_count());
2856 debug_atomic_inc(redundant_softirqs_off
);
2859 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2861 struct task_struct
*curr
= current
;
2863 if (unlikely(!debug_locks
))
2866 /* no reclaim without waiting on it */
2867 if (!(gfp_mask
& __GFP_DIRECT_RECLAIM
))
2870 /* this guy won't enter reclaim */
2871 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2874 /* We're only interested __GFP_FS allocations for now */
2875 if (!(gfp_mask
& __GFP_FS
))
2879 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2881 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2884 mark_held_locks(curr
, RECLAIM_FS
);
2887 static void check_flags(unsigned long flags
);
2889 void lockdep_trace_alloc(gfp_t gfp_mask
)
2891 unsigned long flags
;
2893 if (unlikely(current
->lockdep_recursion
))
2896 raw_local_irq_save(flags
);
2898 current
->lockdep_recursion
= 1;
2899 __lockdep_trace_alloc(gfp_mask
, flags
);
2900 current
->lockdep_recursion
= 0;
2901 raw_local_irq_restore(flags
);
2904 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2907 * If non-trylock use in a hardirq or softirq context, then
2908 * mark the lock as used in these contexts:
2910 if (!hlock
->trylock
) {
2912 if (curr
->hardirq_context
)
2913 if (!mark_lock(curr
, hlock
,
2914 LOCK_USED_IN_HARDIRQ_READ
))
2916 if (curr
->softirq_context
)
2917 if (!mark_lock(curr
, hlock
,
2918 LOCK_USED_IN_SOFTIRQ_READ
))
2921 if (curr
->hardirq_context
)
2922 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2924 if (curr
->softirq_context
)
2925 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2929 if (!hlock
->hardirqs_off
) {
2931 if (!mark_lock(curr
, hlock
,
2932 LOCK_ENABLED_HARDIRQ_READ
))
2934 if (curr
->softirqs_enabled
)
2935 if (!mark_lock(curr
, hlock
,
2936 LOCK_ENABLED_SOFTIRQ_READ
))
2939 if (!mark_lock(curr
, hlock
,
2940 LOCK_ENABLED_HARDIRQ
))
2942 if (curr
->softirqs_enabled
)
2943 if (!mark_lock(curr
, hlock
,
2944 LOCK_ENABLED_SOFTIRQ
))
2950 * We reuse the irq context infrastructure more broadly as a general
2951 * context checking code. This tests GFP_FS recursion (a lock taken
2952 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2955 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2957 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2960 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2968 static inline unsigned int task_irq_context(struct task_struct
*task
)
2970 return 2 * !!task
->hardirq_context
+ !!task
->softirq_context
;
2973 static int separate_irq_context(struct task_struct
*curr
,
2974 struct held_lock
*hlock
)
2976 unsigned int depth
= curr
->lockdep_depth
;
2979 * Keep track of points where we cross into an interrupt context:
2982 struct held_lock
*prev_hlock
;
2984 prev_hlock
= curr
->held_locks
+ depth
-1;
2986 * If we cross into another context, reset the
2987 * hash key (this also prevents the checking and the
2988 * adding of the dependency to 'prev'):
2990 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2996 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2999 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
3000 enum lock_usage_bit new_bit
)
3002 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3006 static inline int mark_irqflags(struct task_struct
*curr
,
3007 struct held_lock
*hlock
)
3012 static inline unsigned int task_irq_context(struct task_struct
*task
)
3017 static inline int separate_irq_context(struct task_struct
*curr
,
3018 struct held_lock
*hlock
)
3023 void lockdep_trace_alloc(gfp_t gfp_mask
)
3027 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3030 * Mark a lock with a usage bit, and validate the state transition:
3032 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
3033 enum lock_usage_bit new_bit
)
3035 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
3038 * If already set then do not dirty the cacheline,
3039 * nor do any checks:
3041 if (likely(hlock_class(this)->usage_mask
& new_mask
))
3047 * Make sure we didn't race:
3049 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
3054 hlock_class(this)->usage_mask
|= new_mask
;
3056 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
3060 #define LOCKDEP_STATE(__STATE) \
3061 case LOCK_USED_IN_##__STATE: \
3062 case LOCK_USED_IN_##__STATE##_READ: \
3063 case LOCK_ENABLED_##__STATE: \
3064 case LOCK_ENABLED_##__STATE##_READ:
3065 #include "lockdep_states.h"
3066 #undef LOCKDEP_STATE
3067 ret
= mark_lock_irq(curr
, this, new_bit
);
3072 debug_atomic_dec(nr_unused_locks
);
3075 if (!debug_locks_off_graph_unlock())
3084 * We must printk outside of the graph_lock:
3087 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
3089 print_irqtrace_events(curr
);
3097 * Initialize a lock instance's lock-class mapping info:
3099 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
3100 struct lock_class_key
*key
, int subclass
)
3104 kmemcheck_mark_initialized(lock
, sizeof(*lock
));
3106 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
3107 lock
->class_cache
[i
] = NULL
;
3109 #ifdef CONFIG_LOCK_STAT
3110 lock
->cpu
= raw_smp_processor_id();
3114 * Can't be having no nameless bastards around this place!
3116 if (DEBUG_LOCKS_WARN_ON(!name
)) {
3117 lock
->name
= "NULL";
3124 * No key, no joy, we need to hash something.
3126 if (DEBUG_LOCKS_WARN_ON(!key
))
3129 * Sanity check, the lock-class key must be persistent:
3131 if (!static_obj(key
)) {
3132 printk("BUG: key %p not in .data!\n", key
);
3134 * What it says above ^^^^^, I suggest you read it.
3136 DEBUG_LOCKS_WARN_ON(1);
3141 if (unlikely(!debug_locks
))
3145 unsigned long flags
;
3147 if (DEBUG_LOCKS_WARN_ON(current
->lockdep_recursion
))
3150 raw_local_irq_save(flags
);
3151 current
->lockdep_recursion
= 1;
3152 register_lock_class(lock
, subclass
, 1);
3153 current
->lockdep_recursion
= 0;
3154 raw_local_irq_restore(flags
);
3157 EXPORT_SYMBOL_GPL(lockdep_init_map
);
3159 struct lock_class_key __lockdep_no_validate__
;
3160 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
3163 print_lock_nested_lock_not_held(struct task_struct
*curr
,
3164 struct held_lock
*hlock
,
3167 if (!debug_locks_off())
3169 if (debug_locks_silent
)
3173 printk("==================================\n");
3174 printk("[ BUG: Nested lock was not taken ]\n");
3175 print_kernel_ident();
3176 printk("----------------------------------\n");
3178 printk("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3181 printk("\nbut this task is not holding:\n");
3182 printk("%s\n", hlock
->nest_lock
->name
);
3184 printk("\nstack backtrace:\n");
3187 printk("\nother info that might help us debug this:\n");
3188 lockdep_print_held_locks(curr
);
3190 printk("\nstack backtrace:\n");
3196 static int __lock_is_held(struct lockdep_map
*lock
, int read
);
3199 * This gets called for every mutex_lock*()/spin_lock*() operation.
3200 * We maintain the dependency maps and validate the locking attempt:
3202 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3203 int trylock
, int read
, int check
, int hardirqs_off
,
3204 struct lockdep_map
*nest_lock
, unsigned long ip
,
3205 int references
, int pin_count
)
3207 struct task_struct
*curr
= current
;
3208 struct lock_class
*class = NULL
;
3209 struct held_lock
*hlock
;
3215 if (unlikely(!debug_locks
))
3219 * Lockdep should run with IRQs disabled, otherwise we could
3220 * get an interrupt which would want to take locks, which would
3221 * end up in lockdep and have you got a head-ache already?
3223 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3226 if (!prove_locking
|| lock
->key
== &__lockdep_no_validate__
)
3229 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
3230 class = lock
->class_cache
[subclass
];
3234 if (unlikely(!class)) {
3235 class = register_lock_class(lock
, subclass
, 0);
3239 atomic_inc((atomic_t
*)&class->ops
);
3240 if (very_verbose(class)) {
3241 printk("\nacquire class [%p] %s", class->key
, class->name
);
3242 if (class->name_version
> 1)
3243 printk(KERN_CONT
"#%d", class->name_version
);
3244 printk(KERN_CONT
"\n");
3249 * Add the lock to the list of currently held locks.
3250 * (we dont increase the depth just yet, up until the
3251 * dependency checks are done)
3253 depth
= curr
->lockdep_depth
;
3255 * Ran out of static storage for our per-task lock stack again have we?
3257 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
3260 class_idx
= class - lock_classes
+ 1;
3263 hlock
= curr
->held_locks
+ depth
- 1;
3264 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
3265 if (hlock
->references
) {
3267 * Check: unsigned int references:12, overflow.
3269 if (DEBUG_LOCKS_WARN_ON(hlock
->references
== (1 << 12)-1))
3272 hlock
->references
++;
3274 hlock
->references
= 2;
3281 hlock
= curr
->held_locks
+ depth
;
3283 * Plain impossible, we just registered it and checked it weren't no
3284 * NULL like.. I bet this mushroom I ate was good!
3286 if (DEBUG_LOCKS_WARN_ON(!class))
3288 hlock
->class_idx
= class_idx
;
3289 hlock
->acquire_ip
= ip
;
3290 hlock
->instance
= lock
;
3291 hlock
->nest_lock
= nest_lock
;
3292 hlock
->irq_context
= task_irq_context(curr
);
3293 hlock
->trylock
= trylock
;
3295 hlock
->check
= check
;
3296 hlock
->hardirqs_off
= !!hardirqs_off
;
3297 hlock
->references
= references
;
3298 #ifdef CONFIG_LOCK_STAT
3299 hlock
->waittime_stamp
= 0;
3300 hlock
->holdtime_stamp
= lockstat_clock();
3302 hlock
->pin_count
= pin_count
;
3304 if (check
&& !mark_irqflags(curr
, hlock
))
3307 /* mark it as used: */
3308 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3312 * Calculate the chain hash: it's the combined hash of all the
3313 * lock keys along the dependency chain. We save the hash value
3314 * at every step so that we can get the current hash easily
3315 * after unlock. The chain hash is then used to cache dependency
3318 * The 'key ID' is what is the most compact key value to drive
3319 * the hash, not class->key.
3322 * Whoops, we did it again.. ran straight out of our static allocation.
3324 if (DEBUG_LOCKS_WARN_ON(class_idx
> MAX_LOCKDEP_KEYS
))
3327 chain_key
= curr
->curr_chain_key
;
3330 * How can we have a chain hash when we ain't got no keys?!
3332 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3337 hlock
->prev_chain_key
= chain_key
;
3338 if (separate_irq_context(curr
, hlock
)) {
3342 chain_key
= iterate_chain_key(chain_key
, class_idx
);
3344 if (nest_lock
&& !__lock_is_held(nest_lock
, -1))
3345 return print_lock_nested_lock_not_held(curr
, hlock
, ip
);
3347 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3350 curr
->curr_chain_key
= chain_key
;
3351 curr
->lockdep_depth
++;
3352 check_chain_key(curr
);
3353 #ifdef CONFIG_DEBUG_LOCKDEP
3354 if (unlikely(!debug_locks
))
3357 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3359 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3360 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
3361 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
3363 lockdep_print_held_locks(current
);
3364 debug_show_all_locks();
3370 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3371 max_lockdep_depth
= curr
->lockdep_depth
;
3377 print_unlock_imbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3380 if (!debug_locks_off())
3382 if (debug_locks_silent
)
3386 printk("=====================================\n");
3387 printk("[ BUG: bad unlock balance detected! ]\n");
3388 print_kernel_ident();
3389 printk("-------------------------------------\n");
3390 printk("%s/%d is trying to release lock (",
3391 curr
->comm
, task_pid_nr(curr
));
3392 print_lockdep_cache(lock
);
3393 printk(KERN_CONT
") at:\n");
3395 printk("but there are no more locks to release!\n");
3396 printk("\nother info that might help us debug this:\n");
3397 lockdep_print_held_locks(curr
);
3399 printk("\nstack backtrace:\n");
3405 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
3407 if (hlock
->instance
== lock
)
3410 if (hlock
->references
) {
3411 struct lock_class
*class = lock
->class_cache
[0];
3414 class = look_up_lock_class(lock
, 0);
3417 * If look_up_lock_class() failed to find a class, we're trying
3418 * to test if we hold a lock that has never yet been acquired.
3419 * Clearly if the lock hasn't been acquired _ever_, we're not
3420 * holding it either, so report failure.
3426 * References, but not a lock we're actually ref-counting?
3427 * State got messed up, follow the sites that change ->references
3428 * and try to make sense of it.
3430 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3433 if (hlock
->class_idx
== class - lock_classes
+ 1)
3441 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3442 struct lock_class_key
*key
, unsigned int subclass
,
3445 struct task_struct
*curr
= current
;
3446 struct held_lock
*hlock
, *prev_hlock
;
3447 struct lock_class
*class;
3451 depth
= curr
->lockdep_depth
;
3453 * This function is about (re)setting the class of a held lock,
3454 * yet we're not actually holding any locks. Naughty user!
3456 if (DEBUG_LOCKS_WARN_ON(!depth
))
3460 for (i
= depth
-1; i
>= 0; i
--) {
3461 hlock
= curr
->held_locks
+ i
;
3463 * We must not cross into another context:
3465 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3467 if (match_held_lock(hlock
, lock
))
3471 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3474 lockdep_init_map(lock
, name
, key
, 0);
3475 class = register_lock_class(lock
, subclass
, 0);
3476 hlock
->class_idx
= class - lock_classes
+ 1;
3478 curr
->lockdep_depth
= i
;
3479 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3481 for (; i
< depth
; i
++) {
3482 hlock
= curr
->held_locks
+ i
;
3483 if (!__lock_acquire(hlock
->instance
,
3484 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3485 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3486 hlock
->nest_lock
, hlock
->acquire_ip
,
3487 hlock
->references
, hlock
->pin_count
))
3492 * I took it apart and put it back together again, except now I have
3493 * these 'spare' parts.. where shall I put them.
3495 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3501 * Remove the lock to the list of currently held locks - this gets
3502 * called on mutex_unlock()/spin_unlock*() (or on a failed
3503 * mutex_lock_interruptible()).
3505 * @nested is an hysterical artifact, needs a tree wide cleanup.
3508 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3510 struct task_struct
*curr
= current
;
3511 struct held_lock
*hlock
, *prev_hlock
;
3515 if (unlikely(!debug_locks
))
3518 depth
= curr
->lockdep_depth
;
3520 * So we're all set to release this lock.. wait what lock? We don't
3521 * own any locks, you've been drinking again?
3523 if (DEBUG_LOCKS_WARN_ON(depth
<= 0))
3524 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3527 * Check whether the lock exists in the current stack
3531 for (i
= depth
-1; i
>= 0; i
--) {
3532 hlock
= curr
->held_locks
+ i
;
3534 * We must not cross into another context:
3536 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3538 if (match_held_lock(hlock
, lock
))
3542 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3545 if (hlock
->instance
== lock
)
3546 lock_release_holdtime(hlock
);
3548 WARN(hlock
->pin_count
, "releasing a pinned lock\n");
3550 if (hlock
->references
) {
3551 hlock
->references
--;
3552 if (hlock
->references
) {
3554 * We had, and after removing one, still have
3555 * references, the current lock stack is still
3556 * valid. We're done!
3563 * We have the right lock to unlock, 'hlock' points to it.
3564 * Now we remove it from the stack, and add back the other
3565 * entries (if any), recalculating the hash along the way:
3568 curr
->lockdep_depth
= i
;
3569 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3571 for (i
++; i
< depth
; i
++) {
3572 hlock
= curr
->held_locks
+ i
;
3573 if (!__lock_acquire(hlock
->instance
,
3574 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3575 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3576 hlock
->nest_lock
, hlock
->acquire_ip
,
3577 hlock
->references
, hlock
->pin_count
))
3582 * We had N bottles of beer on the wall, we drank one, but now
3583 * there's not N-1 bottles of beer left on the wall...
3585 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3591 static int __lock_is_held(struct lockdep_map
*lock
, int read
)
3593 struct task_struct
*curr
= current
;
3596 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3597 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3599 if (match_held_lock(hlock
, lock
)) {
3600 if (read
== -1 || hlock
->read
== read
)
3610 static struct pin_cookie
__lock_pin_lock(struct lockdep_map
*lock
)
3612 struct pin_cookie cookie
= NIL_COOKIE
;
3613 struct task_struct
*curr
= current
;
3616 if (unlikely(!debug_locks
))
3619 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3620 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3622 if (match_held_lock(hlock
, lock
)) {
3624 * Grab 16bits of randomness; this is sufficient to not
3625 * be guessable and still allows some pin nesting in
3626 * our u32 pin_count.
3628 cookie
.val
= 1 + (prandom_u32() >> 16);
3629 hlock
->pin_count
+= cookie
.val
;
3634 WARN(1, "pinning an unheld lock\n");
3638 static void __lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3640 struct task_struct
*curr
= current
;
3643 if (unlikely(!debug_locks
))
3646 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3647 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3649 if (match_held_lock(hlock
, lock
)) {
3650 hlock
->pin_count
+= cookie
.val
;
3655 WARN(1, "pinning an unheld lock\n");
3658 static void __lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3660 struct task_struct
*curr
= current
;
3663 if (unlikely(!debug_locks
))
3666 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3667 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3669 if (match_held_lock(hlock
, lock
)) {
3670 if (WARN(!hlock
->pin_count
, "unpinning an unpinned lock\n"))
3673 hlock
->pin_count
-= cookie
.val
;
3675 if (WARN((int)hlock
->pin_count
< 0, "pin count corrupted\n"))
3676 hlock
->pin_count
= 0;
3682 WARN(1, "unpinning an unheld lock\n");
3686 * Check whether we follow the irq-flags state precisely:
3688 static void check_flags(unsigned long flags
)
3690 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3691 defined(CONFIG_TRACE_IRQFLAGS)
3695 if (irqs_disabled_flags(flags
)) {
3696 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3697 printk("possible reason: unannotated irqs-off.\n");
3700 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3701 printk("possible reason: unannotated irqs-on.\n");
3706 * We dont accurately track softirq state in e.g.
3707 * hardirq contexts (such as on 4KSTACKS), so only
3708 * check if not in hardirq contexts:
3710 if (!hardirq_count()) {
3711 if (softirq_count()) {
3712 /* like the above, but with softirqs */
3713 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3715 /* lick the above, does it taste good? */
3716 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3721 print_irqtrace_events(current
);
3725 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3726 struct lock_class_key
*key
, unsigned int subclass
,
3729 unsigned long flags
;
3731 if (unlikely(current
->lockdep_recursion
))
3734 raw_local_irq_save(flags
);
3735 current
->lockdep_recursion
= 1;
3737 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3738 check_chain_key(current
);
3739 current
->lockdep_recursion
= 0;
3740 raw_local_irq_restore(flags
);
3742 EXPORT_SYMBOL_GPL(lock_set_class
);
3745 * We are not always called with irqs disabled - do that here,
3746 * and also avoid lockdep recursion:
3748 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3749 int trylock
, int read
, int check
,
3750 struct lockdep_map
*nest_lock
, unsigned long ip
)
3752 unsigned long flags
;
3754 if (unlikely(current
->lockdep_recursion
))
3757 raw_local_irq_save(flags
);
3760 current
->lockdep_recursion
= 1;
3761 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3762 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3763 irqs_disabled_flags(flags
), nest_lock
, ip
, 0, 0);
3764 current
->lockdep_recursion
= 0;
3765 raw_local_irq_restore(flags
);
3767 EXPORT_SYMBOL_GPL(lock_acquire
);
3769 void lock_release(struct lockdep_map
*lock
, int nested
,
3772 unsigned long flags
;
3774 if (unlikely(current
->lockdep_recursion
))
3777 raw_local_irq_save(flags
);
3779 current
->lockdep_recursion
= 1;
3780 trace_lock_release(lock
, ip
);
3781 if (__lock_release(lock
, nested
, ip
))
3782 check_chain_key(current
);
3783 current
->lockdep_recursion
= 0;
3784 raw_local_irq_restore(flags
);
3786 EXPORT_SYMBOL_GPL(lock_release
);
3788 int lock_is_held_type(struct lockdep_map
*lock
, int read
)
3790 unsigned long flags
;
3793 if (unlikely(current
->lockdep_recursion
))
3794 return 1; /* avoid false negative lockdep_assert_held() */
3796 raw_local_irq_save(flags
);
3799 current
->lockdep_recursion
= 1;
3800 ret
= __lock_is_held(lock
, read
);
3801 current
->lockdep_recursion
= 0;
3802 raw_local_irq_restore(flags
);
3806 EXPORT_SYMBOL_GPL(lock_is_held_type
);
3808 struct pin_cookie
lock_pin_lock(struct lockdep_map
*lock
)
3810 struct pin_cookie cookie
= NIL_COOKIE
;
3811 unsigned long flags
;
3813 if (unlikely(current
->lockdep_recursion
))
3816 raw_local_irq_save(flags
);
3819 current
->lockdep_recursion
= 1;
3820 cookie
= __lock_pin_lock(lock
);
3821 current
->lockdep_recursion
= 0;
3822 raw_local_irq_restore(flags
);
3826 EXPORT_SYMBOL_GPL(lock_pin_lock
);
3828 void lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3830 unsigned long flags
;
3832 if (unlikely(current
->lockdep_recursion
))
3835 raw_local_irq_save(flags
);
3838 current
->lockdep_recursion
= 1;
3839 __lock_repin_lock(lock
, cookie
);
3840 current
->lockdep_recursion
= 0;
3841 raw_local_irq_restore(flags
);
3843 EXPORT_SYMBOL_GPL(lock_repin_lock
);
3845 void lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
3847 unsigned long flags
;
3849 if (unlikely(current
->lockdep_recursion
))
3852 raw_local_irq_save(flags
);
3855 current
->lockdep_recursion
= 1;
3856 __lock_unpin_lock(lock
, cookie
);
3857 current
->lockdep_recursion
= 0;
3858 raw_local_irq_restore(flags
);
3860 EXPORT_SYMBOL_GPL(lock_unpin_lock
);
3862 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3864 current
->lockdep_reclaim_gfp
= gfp_mask
;
3867 void lockdep_clear_current_reclaim_state(void)
3869 current
->lockdep_reclaim_gfp
= 0;
3872 #ifdef CONFIG_LOCK_STAT
3874 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3877 if (!debug_locks_off())
3879 if (debug_locks_silent
)
3883 printk("=================================\n");
3884 printk("[ BUG: bad contention detected! ]\n");
3885 print_kernel_ident();
3886 printk("---------------------------------\n");
3887 printk("%s/%d is trying to contend lock (",
3888 curr
->comm
, task_pid_nr(curr
));
3889 print_lockdep_cache(lock
);
3890 printk(KERN_CONT
") at:\n");
3892 printk("but there are no locks held!\n");
3893 printk("\nother info that might help us debug this:\n");
3894 lockdep_print_held_locks(curr
);
3896 printk("\nstack backtrace:\n");
3903 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3905 struct task_struct
*curr
= current
;
3906 struct held_lock
*hlock
, *prev_hlock
;
3907 struct lock_class_stats
*stats
;
3909 int i
, contention_point
, contending_point
;
3911 depth
= curr
->lockdep_depth
;
3913 * Whee, we contended on this lock, except it seems we're not
3914 * actually trying to acquire anything much at all..
3916 if (DEBUG_LOCKS_WARN_ON(!depth
))
3920 for (i
= depth
-1; i
>= 0; i
--) {
3921 hlock
= curr
->held_locks
+ i
;
3923 * We must not cross into another context:
3925 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3927 if (match_held_lock(hlock
, lock
))
3931 print_lock_contention_bug(curr
, lock
, ip
);
3935 if (hlock
->instance
!= lock
)
3938 hlock
->waittime_stamp
= lockstat_clock();
3940 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3941 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3944 stats
= get_lock_stats(hlock_class(hlock
));
3945 if (contention_point
< LOCKSTAT_POINTS
)
3946 stats
->contention_point
[contention_point
]++;
3947 if (contending_point
< LOCKSTAT_POINTS
)
3948 stats
->contending_point
[contending_point
]++;
3949 if (lock
->cpu
!= smp_processor_id())
3950 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3951 put_lock_stats(stats
);
3955 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3957 struct task_struct
*curr
= current
;
3958 struct held_lock
*hlock
, *prev_hlock
;
3959 struct lock_class_stats
*stats
;
3961 u64 now
, waittime
= 0;
3964 depth
= curr
->lockdep_depth
;
3966 * Yay, we acquired ownership of this lock we didn't try to
3967 * acquire, how the heck did that happen?
3969 if (DEBUG_LOCKS_WARN_ON(!depth
))
3973 for (i
= depth
-1; i
>= 0; i
--) {
3974 hlock
= curr
->held_locks
+ i
;
3976 * We must not cross into another context:
3978 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3980 if (match_held_lock(hlock
, lock
))
3984 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3988 if (hlock
->instance
!= lock
)
3991 cpu
= smp_processor_id();
3992 if (hlock
->waittime_stamp
) {
3993 now
= lockstat_clock();
3994 waittime
= now
- hlock
->waittime_stamp
;
3995 hlock
->holdtime_stamp
= now
;
3998 trace_lock_acquired(lock
, ip
);
4000 stats
= get_lock_stats(hlock_class(hlock
));
4003 lock_time_inc(&stats
->read_waittime
, waittime
);
4005 lock_time_inc(&stats
->write_waittime
, waittime
);
4007 if (lock
->cpu
!= cpu
)
4008 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
4009 put_lock_stats(stats
);
4015 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
4017 unsigned long flags
;
4019 if (unlikely(!lock_stat
))
4022 if (unlikely(current
->lockdep_recursion
))
4025 raw_local_irq_save(flags
);
4027 current
->lockdep_recursion
= 1;
4028 trace_lock_contended(lock
, ip
);
4029 __lock_contended(lock
, ip
);
4030 current
->lockdep_recursion
= 0;
4031 raw_local_irq_restore(flags
);
4033 EXPORT_SYMBOL_GPL(lock_contended
);
4035 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
4037 unsigned long flags
;
4039 if (unlikely(!lock_stat
))
4042 if (unlikely(current
->lockdep_recursion
))
4045 raw_local_irq_save(flags
);
4047 current
->lockdep_recursion
= 1;
4048 __lock_acquired(lock
, ip
);
4049 current
->lockdep_recursion
= 0;
4050 raw_local_irq_restore(flags
);
4052 EXPORT_SYMBOL_GPL(lock_acquired
);
4056 * Used by the testsuite, sanitize the validator state
4057 * after a simulated failure:
4060 void lockdep_reset(void)
4062 unsigned long flags
;
4065 raw_local_irq_save(flags
);
4066 current
->curr_chain_key
= 0;
4067 current
->lockdep_depth
= 0;
4068 current
->lockdep_recursion
= 0;
4069 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
4070 nr_hardirq_chains
= 0;
4071 nr_softirq_chains
= 0;
4072 nr_process_chains
= 0;
4074 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
4075 INIT_HLIST_HEAD(chainhash_table
+ i
);
4076 raw_local_irq_restore(flags
);
4079 static void zap_class(struct lock_class
*class)
4084 * Remove all dependencies this lock is
4087 for (i
= 0; i
< nr_list_entries
; i
++) {
4088 if (list_entries
[i
].class == class)
4089 list_del_rcu(&list_entries
[i
].entry
);
4092 * Unhash the class and remove it from the all_lock_classes list:
4094 hlist_del_rcu(&class->hash_entry
);
4095 list_del_rcu(&class->lock_entry
);
4097 RCU_INIT_POINTER(class->key
, NULL
);
4098 RCU_INIT_POINTER(class->name
, NULL
);
4101 static inline int within(const void *addr
, void *start
, unsigned long size
)
4103 return addr
>= start
&& addr
< start
+ size
;
4107 * Used in module.c to remove lock classes from memory that is going to be
4108 * freed; and possibly re-used by other modules.
4110 * We will have had one sync_sched() before getting here, so we're guaranteed
4111 * nobody will look up these exact classes -- they're properly dead but still
4114 void lockdep_free_key_range(void *start
, unsigned long size
)
4116 struct lock_class
*class;
4117 struct hlist_head
*head
;
4118 unsigned long flags
;
4122 raw_local_irq_save(flags
);
4123 locked
= graph_lock();
4126 * Unhash all classes that were created by this module:
4128 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4129 head
= classhash_table
+ i
;
4130 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4131 if (within(class->key
, start
, size
))
4133 else if (within(class->name
, start
, size
))
4140 raw_local_irq_restore(flags
);
4143 * Wait for any possible iterators from look_up_lock_class() to pass
4144 * before continuing to free the memory they refer to.
4146 * sync_sched() is sufficient because the read-side is IRQ disable.
4148 synchronize_sched();
4151 * XXX at this point we could return the resources to the pool;
4152 * instead we leak them. We would need to change to bitmap allocators
4153 * instead of the linear allocators we have now.
4157 void lockdep_reset_lock(struct lockdep_map
*lock
)
4159 struct lock_class
*class;
4160 struct hlist_head
*head
;
4161 unsigned long flags
;
4165 raw_local_irq_save(flags
);
4168 * Remove all classes this lock might have:
4170 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
4172 * If the class exists we look it up and zap it:
4174 class = look_up_lock_class(lock
, j
);
4179 * Debug check: in the end all mapped classes should
4182 locked
= graph_lock();
4183 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
4184 head
= classhash_table
+ i
;
4185 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
4188 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
4189 match
|= class == lock
->class_cache
[j
];
4191 if (unlikely(match
)) {
4192 if (debug_locks_off_graph_unlock()) {
4194 * We all just reset everything, how did it match?
4206 raw_local_irq_restore(flags
);
4209 void __init
lockdep_info(void)
4211 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4213 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
4214 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
4215 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
4216 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
4217 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
4218 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
4219 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
4221 printk(" memory used by lock dependency info: %lu kB\n",
4222 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
4223 sizeof(struct list_head
) * CLASSHASH_SIZE
+
4224 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
4225 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
4226 sizeof(struct list_head
) * CHAINHASH_SIZE
4227 #ifdef CONFIG_PROVE_LOCKING
4228 + sizeof(struct circular_queue
)
4233 printk(" per task-struct memory footprint: %lu bytes\n",
4234 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
4238 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
4239 const void *mem_to
, struct held_lock
*hlock
)
4241 if (!debug_locks_off())
4243 if (debug_locks_silent
)
4247 printk("=========================\n");
4248 printk("[ BUG: held lock freed! ]\n");
4249 print_kernel_ident();
4250 printk("-------------------------\n");
4251 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4252 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
4254 lockdep_print_held_locks(curr
);
4256 printk("\nstack backtrace:\n");
4260 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
4261 const void* lock_from
, unsigned long lock_len
)
4263 return lock_from
+ lock_len
<= mem_from
||
4264 mem_from
+ mem_len
<= lock_from
;
4268 * Called when kernel memory is freed (or unmapped), or if a lock
4269 * is destroyed or reinitialized - this code checks whether there is
4270 * any held lock in the memory range of <from> to <to>:
4272 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
4274 struct task_struct
*curr
= current
;
4275 struct held_lock
*hlock
;
4276 unsigned long flags
;
4279 if (unlikely(!debug_locks
))
4282 local_irq_save(flags
);
4283 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4284 hlock
= curr
->held_locks
+ i
;
4286 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
4287 sizeof(*hlock
->instance
)))
4290 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
4293 local_irq_restore(flags
);
4295 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
4297 static void print_held_locks_bug(void)
4299 if (!debug_locks_off())
4301 if (debug_locks_silent
)
4305 printk("=====================================\n");
4306 printk("[ BUG: %s/%d still has locks held! ]\n",
4307 current
->comm
, task_pid_nr(current
));
4308 print_kernel_ident();
4309 printk("-------------------------------------\n");
4310 lockdep_print_held_locks(current
);
4311 printk("\nstack backtrace:\n");
4315 void debug_check_no_locks_held(void)
4317 if (unlikely(current
->lockdep_depth
> 0))
4318 print_held_locks_bug();
4320 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
4323 void debug_show_all_locks(void)
4325 struct task_struct
*g
, *p
;
4329 if (unlikely(!debug_locks
)) {
4330 printk("INFO: lockdep is turned off.\n");
4333 printk("\nShowing all locks held in the system:\n");
4336 * Here we try to get the tasklist_lock as hard as possible,
4337 * if not successful after 2 seconds we ignore it (but keep
4338 * trying). This is to enable a debug printout even if a
4339 * tasklist_lock-holding task deadlocks or crashes.
4342 if (!read_trylock(&tasklist_lock
)) {
4344 printk("hm, tasklist_lock locked, retrying... ");
4347 printk(" #%d", 10-count
);
4351 printk(" ignoring it.\n");
4355 printk(KERN_CONT
" locked it.\n");
4358 do_each_thread(g
, p
) {
4360 * It's not reliable to print a task's held locks
4361 * if it's not sleeping (or if it's not the current
4364 if (p
->state
== TASK_RUNNING
&& p
!= current
)
4366 if (p
->lockdep_depth
)
4367 lockdep_print_held_locks(p
);
4369 if (read_trylock(&tasklist_lock
))
4371 } while_each_thread(g
, p
);
4374 printk("=============================================\n\n");
4377 read_unlock(&tasklist_lock
);
4379 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
4383 * Careful: only use this function if you are sure that
4384 * the task cannot run in parallel!
4386 void debug_show_held_locks(struct task_struct
*task
)
4388 if (unlikely(!debug_locks
)) {
4389 printk("INFO: lockdep is turned off.\n");
4392 lockdep_print_held_locks(task
);
4394 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
4396 asmlinkage __visible
void lockdep_sys_exit(void)
4398 struct task_struct
*curr
= current
;
4400 if (unlikely(curr
->lockdep_depth
)) {
4401 if (!debug_locks_off())
4404 printk("================================================\n");
4405 printk("[ BUG: lock held when returning to user space! ]\n");
4406 print_kernel_ident();
4407 printk("------------------------------------------------\n");
4408 printk("%s/%d is leaving the kernel with locks still held!\n",
4409 curr
->comm
, curr
->pid
);
4410 lockdep_print_held_locks(curr
);
4414 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
4416 struct task_struct
*curr
= current
;
4418 #ifndef CONFIG_PROVE_RCU_REPEATEDLY
4419 if (!debug_locks_off())
4421 #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4422 /* Note: the following can be executed concurrently, so be careful. */
4424 pr_err("===============================\n");
4425 pr_err("[ ERR: suspicious RCU usage. ]\n");
4426 print_kernel_ident();
4427 pr_err("-------------------------------\n");
4428 pr_err("%s:%d %s!\n", file
, line
, s
);
4429 pr_err("\nother info that might help us debug this:\n\n");
4430 pr_err("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4431 !rcu_lockdep_current_cpu_online()
4432 ? "RCU used illegally from offline CPU!\n"
4433 : !rcu_is_watching()
4434 ? "RCU used illegally from idle CPU!\n"
4436 rcu_scheduler_active
, debug_locks
);
4439 * If a CPU is in the RCU-free window in idle (ie: in the section
4440 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4441 * considers that CPU to be in an "extended quiescent state",
4442 * which means that RCU will be completely ignoring that CPU.
4443 * Therefore, rcu_read_lock() and friends have absolutely no
4444 * effect on a CPU running in that state. In other words, even if
4445 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4446 * delete data structures out from under it. RCU really has no
4447 * choice here: we need to keep an RCU-free window in idle where
4448 * the CPU may possibly enter into low power mode. This way we can
4449 * notice an extended quiescent state to other CPUs that started a grace
4450 * period. Otherwise we would delay any grace period as long as we run
4453 * So complain bitterly if someone does call rcu_read_lock(),
4454 * rcu_read_lock_bh() and so on from extended quiescent states.
4456 if (!rcu_is_watching())
4457 printk("RCU used illegally from extended quiescent state!\n");
4459 lockdep_print_held_locks(curr
);
4460 printk("\nstack backtrace:\n");
4463 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);