4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
14 * - lock inversion scenarios * - circular lock dependencies
15 * - hardirq/softirq safe/unsafe locking bugs
17 * Bugs are reported even if the current locking scenario does not cause
18 * any deadlock at this point.
20 * I.e. if anytime in the past two locks were taken in a different order,
21 * even if it happened for another task, even if those were different
22 * locks (but of the same class as this lock), this code will detect it.
24 * Thanks to Arjan van de Ven for coming up with the initial idea of
25 * mapping lock dependencies runtime.
27 #define DISABLE_BRANCH_PROFILING
28 #include <linux/mutex.h>
29 #include <linux/sched.h>
30 #include <linux/delay.h>
31 #include <linux/module.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/spinlock.h>
35 #include <linux/kallsyms.h>
36 #include <linux/interrupt.h>
37 #include <linux/stacktrace.h>
38 #include <linux/debug_locks.h>
39 #include <linux/irqflags.h>
40 #include <linux/utsname.h>
41 #include <linux/hash.h>
42 #include <linux/ftrace.h>
43 #include <linux/stringify.h>
44 #include <linux/bitops.h>
45 #include <linux/gfp.h>
46 #include <linux/kmemcheck.h>
47 #include <linux/aee.h>
49 #include <asm/sections.h>
51 #include "lockdep_internals.h"
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/lock.h>
56 #ifdef CONFIG_PROVE_LOCKING
57 int prove_locking
= 1;
58 module_param(prove_locking
, int, 0644);
60 #define prove_locking 0
63 #ifdef CONFIG_LOCK_STAT
65 module_param(lock_stat
, int, 0644);
70 static void lockdep_aee(void)
73 snprintf( aee_str
, 40, "[%s]LockProve Warning", current
->comm
);
74 aee_kernel_warning_api(__FILE__
, __LINE__
, DB_OPT_DUMMY_DUMP
| DB_OPT_FTRACE
, aee_str
,"LockProve Debug\n");
79 * lockdep_lock: protects the lockdep graph, the hashes and the
80 * class/list/hash allocators.
82 * This is one of the rare exceptions where it's justified
83 * to use a raw spinlock - we really dont want the spinlock
84 * code to recurse back into the lockdep code...
86 static arch_spinlock_t lockdep_lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
88 static int graph_lock(void)
90 arch_spin_lock(&lockdep_lock
);
92 * Make sure that if another CPU detected a bug while
93 * walking the graph we dont change it (while the other
94 * CPU is busy printing out stuff with the graph lock
98 arch_spin_unlock(&lockdep_lock
);
101 /* prevent any recursions within lockdep from causing deadlocks */
102 current
->lockdep_recursion
++;
106 static inline int graph_unlock(void)
108 if (debug_locks
&& !arch_spin_is_locked(&lockdep_lock
)) {
110 * The lockdep graph lock isn't locked while we expect it to
111 * be, we're confused now, bye!
113 return DEBUG_LOCKS_WARN_ON(1);
116 current
->lockdep_recursion
--;
117 arch_spin_unlock(&lockdep_lock
);
122 * Turn lock debugging off and return with 0 if it was off already,
123 * and also release the graph lock:
125 static inline int debug_locks_off_graph_unlock(void)
127 int ret
= debug_locks_off();
129 arch_spin_unlock(&lockdep_lock
);
134 static int lockdep_initialized
;
136 unsigned long nr_list_entries
;
137 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
140 * All data structures here are protected by the global debug_lock.
142 * Mutex key structs only get allocated, once during bootup, and never
143 * get freed - this significantly simplifies the debugging code.
145 unsigned long nr_lock_classes
;
146 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
148 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
150 if (!hlock
->class_idx
) {
152 * Someone passed in garbage, we give up.
154 DEBUG_LOCKS_WARN_ON(1);
157 return lock_classes
+ hlock
->class_idx
- 1;
160 #ifdef CONFIG_LOCK_STAT
161 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
],
164 static inline u64
lockstat_clock(void)
166 return local_clock();
169 static int lock_point(unsigned long points
[], unsigned long ip
)
173 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
174 if (points
[i
] == 0) {
185 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
190 if (time
< lt
->min
|| !lt
->nr
)
197 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
202 if (src
->max
> dst
->max
)
205 if (src
->min
< dst
->min
|| !dst
->nr
)
208 dst
->total
+= src
->total
;
212 struct lock_class_stats
lock_stats(struct lock_class
*class)
214 struct lock_class_stats stats
;
217 memset(&stats
, 0, sizeof(struct lock_class_stats
));
218 for_each_possible_cpu(cpu
) {
219 struct lock_class_stats
*pcs
=
220 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
222 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
223 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
225 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
226 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
228 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
229 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
231 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
232 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
234 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
235 stats
.bounces
[i
] += pcs
->bounces
[i
];
241 void clear_lock_stats(struct lock_class
*class)
245 for_each_possible_cpu(cpu
) {
246 struct lock_class_stats
*cpu_stats
=
247 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
249 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
251 memset(class->contention_point
, 0, sizeof(class->contention_point
));
252 memset(class->contending_point
, 0, sizeof(class->contending_point
));
255 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
257 return &get_cpu_var(cpu_lock_stats
)[class - lock_classes
];
260 static void put_lock_stats(struct lock_class_stats
*stats
)
262 put_cpu_var(cpu_lock_stats
);
265 static void lock_release_holdtime(struct held_lock
*hlock
)
267 struct lock_class_stats
*stats
;
273 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
275 stats
= get_lock_stats(hlock_class(hlock
));
277 lock_time_inc(&stats
->read_holdtime
, holdtime
);
279 lock_time_inc(&stats
->write_holdtime
, holdtime
);
280 put_lock_stats(stats
);
283 static inline void lock_release_holdtime(struct held_lock
*hlock
)
289 * We keep a global list of all lock classes. The list only grows,
290 * never shrinks. The list is only accessed with the lockdep
291 * spinlock lock held.
293 LIST_HEAD(all_lock_classes
);
296 * The lockdep classes are in a hash-table as well, for fast lookup:
298 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
299 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
300 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
301 #define classhashentry(key) (classhash_table + __classhashfn((key)))
303 static struct list_head classhash_table
[CLASSHASH_SIZE
];
306 * We put the lock dependency chains into a hash-table as well, to cache
309 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
310 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
311 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
312 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
314 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
317 * The hash key of the lock dependency chains is a hash itself too:
318 * it's a hash of all locks taken up to that lock, including that lock.
319 * It's a 64-bit hash, because it's important for the keys to be
322 #define iterate_chain_key(key1, key2) \
323 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
324 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
327 void lockdep_off(void)
329 current
->lockdep_recursion
++;
331 EXPORT_SYMBOL(lockdep_off
);
333 void lockdep_on(void)
335 current
->lockdep_recursion
--;
337 EXPORT_SYMBOL(lockdep_on
);
340 * Debugging switches:
344 #define VERY_VERBOSE 0
347 # define HARDIRQ_VERBOSE 1
348 # define SOFTIRQ_VERBOSE 1
349 # define RECLAIM_VERBOSE 1
351 # define HARDIRQ_VERBOSE 0
352 # define SOFTIRQ_VERBOSE 0
353 # define RECLAIM_VERBOSE 0
356 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
358 * Quick filtering for interesting events:
360 static int class_filter(struct lock_class
*class)
364 if (class->name_version
== 1 &&
365 !strcmp(class->name
, "lockname"))
367 if (class->name_version
== 1 &&
368 !strcmp(class->name
, "&struct->lockfield"))
371 /* Filter everything else. 1 would be to allow everything else */
376 static int verbose(struct lock_class
*class)
379 return class_filter(class);
385 * Stack-trace: tightly packed array of stack backtrace
386 * addresses. Protected by the graph_lock.
388 unsigned long nr_stack_trace_entries
;
389 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
391 static void print_lockdep_off(const char *bug_msg
)
393 printk(KERN_DEBUG
"%s\n", bug_msg
);
394 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
395 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
398 static int save_trace(struct stack_trace
*trace
)
400 trace
->nr_entries
= 0;
401 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
402 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
406 save_stack_trace(trace
);
409 * Some daft arches put -1 at the end to indicate its a full trace.
411 * <rant> this is buggy anyway, since it takes a whole extra entry so a
412 * complete trace that maxes out the entries provided will be reported
413 * as incomplete, friggin useless </rant>
415 if (trace
->nr_entries
!= 0 &&
416 trace
->entries
[trace
->nr_entries
-1] == ULONG_MAX
)
419 trace
->max_entries
= trace
->nr_entries
;
421 nr_stack_trace_entries
+= trace
->nr_entries
;
423 if (nr_stack_trace_entries
>= MAX_STACK_TRACE_ENTRIES
-1) {
424 if (!debug_locks_off_graph_unlock())
427 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
436 unsigned int nr_hardirq_chains
;
437 unsigned int nr_softirq_chains
;
438 unsigned int nr_process_chains
;
439 unsigned int max_lockdep_depth
;
441 #ifdef CONFIG_DEBUG_LOCKDEP
443 * We cannot printk in early bootup code. Not even early_printk()
444 * might work. So we mark any initialization errors and printk
445 * about it later on, in lockdep_info().
447 static int lockdep_init_error
;
448 static const char *lock_init_error
;
449 static unsigned long lockdep_init_trace_data
[20];
450 static struct stack_trace lockdep_init_trace
= {
451 .max_entries
= ARRAY_SIZE(lockdep_init_trace_data
),
452 .entries
= lockdep_init_trace_data
,
456 * Various lockdep statistics:
458 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
465 #define __USAGE(__STATE) \
466 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
467 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
468 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
469 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
471 static const char *usage_str
[] =
473 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
474 #include "lockdep_states.h"
476 [LOCK_USED
] = "INITIAL USE",
479 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
481 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
484 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
489 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
493 if (class->usage_mask
& lock_flag(bit
+ 2))
495 if (class->usage_mask
& lock_flag(bit
)) {
497 if (class->usage_mask
& lock_flag(bit
+ 2))
504 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
508 #define LOCKDEP_STATE(__STATE) \
509 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
510 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
511 #include "lockdep_states.h"
517 static void __print_lock_name(struct lock_class
*class)
519 char str
[KSYM_NAME_LEN
];
524 name
= __get_key_name(class->key
, str
);
528 if (class->name_version
> 1)
529 printk("#%d", class->name_version
);
531 printk("/%d", class->subclass
);
535 static void print_lock_name(struct lock_class
*class)
537 char usage
[LOCK_USAGE_CHARS
];
539 get_usage_chars(class, usage
);
542 __print_lock_name(class);
543 printk("){%s}", usage
);
546 static void print_lockdep_cache(struct lockdep_map
*lock
)
549 char str
[KSYM_NAME_LEN
];
553 name
= __get_key_name(lock
->key
->subkeys
, str
);
558 static void print_lock(struct held_lock
*hlock
)
560 struct lock_class
*lock
= hlock_class(hlock
);
562 print_lock_name(lock
);
564 print_ip_sym(hlock
->acquire_ip
);
568 static void lockdep_print_held_locks(struct task_struct
*curr
)
570 int i
, depth
= curr
->lockdep_depth
;
573 printk("no locks held by %s/%d.\n", curr
->comm
, task_pid_nr(curr
));
576 if (curr
->state
== TASK_RUNNING
)
577 printk("[Caution!] %s/%d is runable state\n", curr
->comm
, curr
->pid
);
578 printk("%d lock%s held by %s/%d:\n",
579 depth
, depth
> 1 ? "s" : "", curr
->comm
, task_pid_nr(curr
));
581 for (i
= 0; i
< depth
; i
++) {
583 print_lock(curr
->held_locks
+ i
);
587 static void print_kernel_ident(void)
589 printk("%s %.*s %s\n", init_utsname()->release
,
590 (int)strcspn(init_utsname()->version
, " "),
591 init_utsname()->version
,
595 static int very_verbose(struct lock_class
*class)
598 return class_filter(class);
604 * Is this the address of a static object:
606 static int static_obj(void *obj
)
608 unsigned long start
= (unsigned long) &_stext
,
609 end
= (unsigned long) &_end
,
610 addr
= (unsigned long) obj
;
615 if ((addr
>= start
) && (addr
< end
))
618 if (arch_is_kernel_data(addr
))
622 * in-kernel percpu var?
624 if (is_kernel_percpu_address(addr
))
628 * module static or percpu var?
630 return is_module_address(addr
) || is_module_percpu_address(addr
);
634 * To make lock name printouts unique, we calculate a unique
635 * class->name_version generation counter:
637 static int count_matching_names(struct lock_class
*new_class
)
639 struct lock_class
*class;
642 if (!new_class
->name
)
645 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
646 if (new_class
->key
- new_class
->subclass
== class->key
)
647 return class->name_version
;
648 if (class->name
&& !strcmp(class->name
, new_class
->name
))
649 count
= max(count
, class->name_version
);
656 * Register a lock's class in the hash-table, if the class is not present
657 * yet. Otherwise we look it up. We cache the result in the lock object
658 * itself, so actual lookup of the hash should be once per lock object.
660 static inline struct lock_class
*
661 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
663 struct lockdep_subclass_key
*key
;
664 struct list_head
*hash_head
;
665 struct lock_class
*class;
667 #ifdef CONFIG_DEBUG_LOCKDEP
669 * If the architecture calls into lockdep before initializing
670 * the hashes then we'll warn about it later. (we cannot printk
673 if (unlikely(!lockdep_initialized
)) {
675 lockdep_init_error
= 1;
676 lock_init_error
= lock
->name
;
677 save_stack_trace(&lockdep_init_trace
);
681 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
684 "BUG: looking up invalid subclass: %u\n", subclass
);
686 "turning off the locking correctness validator.\n");
692 * Static locks do not have their class-keys yet - for them the key
693 * is the lock object itself:
695 if (unlikely(!lock
->key
))
696 lock
->key
= (void *)lock
;
699 * NOTE: the class-key must be unique. For dynamic locks, a static
700 * lock_class_key variable is passed in through the mutex_init()
701 * (or spin_lock_init()) call - which acts as the key. For static
702 * locks we use the lock object itself as the key.
704 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
705 sizeof(struct lockdep_map
));
707 key
= lock
->key
->subkeys
+ subclass
;
709 hash_head
= classhashentry(key
);
712 * We can walk the hash lockfree, because the hash only
713 * grows, and we are careful when adding entries to the end:
715 list_for_each_entry(class, hash_head
, hash_entry
) {
716 if (class->key
== key
) {
718 * Huh! same key, different name? Did someone trample
719 * on some memory? We're most confused.
721 WARN_ON_ONCE(class->name
!= lock
->name
);
730 * Register a lock's class in the hash-table, if the class is not present
731 * yet. Otherwise we look it up. We cache the result in the lock object
732 * itself, so actual lookup of the hash should be once per lock object.
734 static inline struct lock_class
*
735 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
737 struct lockdep_subclass_key
*key
;
738 struct list_head
*hash_head
;
739 struct lock_class
*class;
742 class = look_up_lock_class(lock
, subclass
);
744 goto out_set_class_cache
;
747 * Debug-check: all keys must be persistent!
749 if (!static_obj(lock
->key
)) {
751 printk("INFO: trying to register non-static key.\n");
752 printk("the code is fine but needs lockdep annotation.\n");
753 printk("turning off the locking correctness validator.\n");
759 key
= lock
->key
->subkeys
+ subclass
;
760 hash_head
= classhashentry(key
);
762 raw_local_irq_save(flags
);
764 raw_local_irq_restore(flags
);
768 * We have to do the hash-walk again, to avoid races
771 list_for_each_entry(class, hash_head
, hash_entry
)
772 if (class->key
== key
)
775 * Allocate a new key from the static array, and add it to
778 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
779 if (!debug_locks_off_graph_unlock()) {
780 raw_local_irq_restore(flags
);
783 raw_local_irq_restore(flags
);
785 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
789 class = lock_classes
+ nr_lock_classes
++;
790 debug_atomic_inc(nr_unused_locks
);
792 class->name
= lock
->name
;
793 class->subclass
= subclass
;
794 INIT_LIST_HEAD(&class->lock_entry
);
795 INIT_LIST_HEAD(&class->locks_before
);
796 INIT_LIST_HEAD(&class->locks_after
);
797 class->name_version
= count_matching_names(class);
799 * We use RCU's safe list-add method to make
800 * parallel walking of the hash-list safe:
802 list_add_tail_rcu(&class->hash_entry
, hash_head
);
804 * Add it to the global list of classes:
806 list_add_tail_rcu(&class->lock_entry
, &all_lock_classes
);
808 if (verbose(class)) {
810 raw_local_irq_restore(flags
);
812 printk("\nnew class %p: %s", class->key
, class->name
);
813 if (class->name_version
> 1)
814 printk("#%d", class->name_version
);
818 raw_local_irq_save(flags
);
820 raw_local_irq_restore(flags
);
826 raw_local_irq_restore(flags
);
829 if (!subclass
|| force
)
830 lock
->class_cache
[0] = class;
831 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
832 lock
->class_cache
[subclass
] = class;
835 * Hash collision, did we smoke some? We found a class with a matching
836 * hash but the subclass -- which is hashed in -- didn't match.
838 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
844 #ifdef CONFIG_PROVE_LOCKING
846 * Allocate a lockdep entry. (assumes the graph_lock held, returns
847 * with NULL on failure)
849 static struct lock_list
*alloc_list_entry(void)
851 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
852 if (!debug_locks_off_graph_unlock())
855 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
859 return list_entries
+ nr_list_entries
++;
863 * Add a new dependency to the head of the list:
865 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
866 struct list_head
*head
, unsigned long ip
,
867 int distance
, struct stack_trace
*trace
)
869 struct lock_list
*entry
;
871 * Lock not present yet - get a new dependency struct and
872 * add it to the list:
874 entry
= alloc_list_entry();
879 entry
->distance
= distance
;
880 entry
->trace
= *trace
;
882 * Since we never remove from the dependency list, the list can
883 * be walked lockless by other CPUs, it's only allocation
884 * that must be protected by the spinlock. But this also means
885 * we must make new entries visible only once writes to the
886 * entry become visible - hence the RCU op:
888 list_add_tail_rcu(&entry
->entry
, head
);
894 * For good efficiency of modular, we use power of 2
896 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
897 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
900 * The circular_queue and helpers is used to implement the
901 * breadth-first search(BFS)algorithem, by which we can build
902 * the shortest path from the next lock to be acquired to the
903 * previous held lock if there is a circular between them.
905 struct circular_queue
{
906 unsigned long element
[MAX_CIRCULAR_QUEUE_SIZE
];
907 unsigned int front
, rear
;
910 static struct circular_queue lock_cq
;
912 unsigned int max_bfs_queue_depth
;
914 static unsigned int lockdep_dependency_gen_id
;
916 static inline void __cq_init(struct circular_queue
*cq
)
918 cq
->front
= cq
->rear
= 0;
919 lockdep_dependency_gen_id
++;
922 static inline int __cq_empty(struct circular_queue
*cq
)
924 return (cq
->front
== cq
->rear
);
927 static inline int __cq_full(struct circular_queue
*cq
)
929 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
932 static inline int __cq_enqueue(struct circular_queue
*cq
, unsigned long elem
)
937 cq
->element
[cq
->rear
] = elem
;
938 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
942 static inline int __cq_dequeue(struct circular_queue
*cq
, unsigned long *elem
)
947 *elem
= cq
->element
[cq
->front
];
948 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
952 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
954 return (cq
->rear
- cq
->front
) & CQ_MASK
;
957 static inline void mark_lock_accessed(struct lock_list
*lock
,
958 struct lock_list
*parent
)
962 nr
= lock
- list_entries
;
963 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
964 lock
->parent
= parent
;
965 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
968 static inline unsigned long lock_accessed(struct lock_list
*lock
)
972 nr
= lock
- list_entries
;
973 WARN_ON(nr
>= nr_list_entries
); /* Out-of-bounds, input fail */
974 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
977 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
979 return child
->parent
;
982 static inline int get_lock_depth(struct lock_list
*child
)
985 struct lock_list
*parent
;
987 while ((parent
= get_lock_parent(child
))) {
994 static int __bfs(struct lock_list
*source_entry
,
996 int (*match
)(struct lock_list
*entry
, void *data
),
997 struct lock_list
**target_entry
,
1000 struct lock_list
*entry
;
1001 struct list_head
*head
;
1002 struct circular_queue
*cq
= &lock_cq
;
1005 if (match(source_entry
, data
)) {
1006 *target_entry
= source_entry
;
1012 head
= &source_entry
->class->locks_after
;
1014 head
= &source_entry
->class->locks_before
;
1016 if (list_empty(head
))
1020 __cq_enqueue(cq
, (unsigned long)source_entry
);
1022 while (!__cq_empty(cq
)) {
1023 struct lock_list
*lock
;
1025 __cq_dequeue(cq
, (unsigned long *)&lock
);
1033 head
= &lock
->class->locks_after
;
1035 head
= &lock
->class->locks_before
;
1037 list_for_each_entry(entry
, head
, entry
) {
1038 if (!lock_accessed(entry
)) {
1039 unsigned int cq_depth
;
1040 mark_lock_accessed(entry
, lock
);
1041 if (match(entry
, data
)) {
1042 *target_entry
= entry
;
1047 if (__cq_enqueue(cq
, (unsigned long)entry
)) {
1051 cq_depth
= __cq_get_elem_count(cq
);
1052 if (max_bfs_queue_depth
< cq_depth
)
1053 max_bfs_queue_depth
= cq_depth
;
1061 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1063 int (*match
)(struct lock_list
*entry
, void *data
),
1064 struct lock_list
**target_entry
)
1066 return __bfs(src_entry
, data
, match
, target_entry
, 1);
1070 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1072 int (*match
)(struct lock_list
*entry
, void *data
),
1073 struct lock_list
**target_entry
)
1075 return __bfs(src_entry
, data
, match
, target_entry
, 0);
1080 * Recursive, forwards-direction lock-dependency checking, used for
1081 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1086 * Print a dependency chain entry (this is only done when a deadlock
1087 * has been detected):
1090 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1092 if (debug_locks_silent
)
1094 printk("\n-> #%u", depth
);
1095 print_lock_name(target
->class);
1097 print_stack_trace(&target
->trace
, 6);
1103 print_circular_lock_scenario(struct held_lock
*src
,
1104 struct held_lock
*tgt
,
1105 struct lock_list
*prt
)
1107 struct lock_class
*source
= hlock_class(src
);
1108 struct lock_class
*target
= hlock_class(tgt
);
1109 struct lock_class
*parent
= prt
->class;
1112 * A direct locking problem where unsafe_class lock is taken
1113 * directly by safe_class lock, then all we need to show
1114 * is the deadlock scenario, as it is obvious that the
1115 * unsafe lock is taken under the safe lock.
1117 * But if there is a chain instead, where the safe lock takes
1118 * an intermediate lock (middle_class) where this lock is
1119 * not the same as the safe lock, then the lock chain is
1120 * used to describe the problem. Otherwise we would need
1121 * to show a different CPU case for each link in the chain
1122 * from the safe_class lock to the unsafe_class lock.
1124 if (parent
!= source
) {
1125 printk("Chain exists of:\n ");
1126 __print_lock_name(source
);
1128 __print_lock_name(parent
);
1130 __print_lock_name(target
);
1134 printk(" Possible unsafe locking scenario:\n\n");
1135 printk(" CPU0 CPU1\n");
1136 printk(" ---- ----\n");
1138 __print_lock_name(target
);
1141 __print_lock_name(parent
);
1144 __print_lock_name(target
);
1147 __print_lock_name(source
);
1149 printk("\n *** DEADLOCK ***\n\n");
1153 * When a circular dependency is detected, print the
1157 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1158 struct held_lock
*check_src
,
1159 struct held_lock
*check_tgt
)
1161 struct task_struct
*curr
= current
;
1163 if (debug_locks_silent
)
1169 printk("======================================================\n");
1170 printk("[ ProveLock INFO: possible circular locking dependency detected ]\n");
1171 print_kernel_ident();
1172 printk("-------------------------------------------------------\n");
1173 printk("%s/%d is trying to acquire lock:\n",
1174 curr
->comm
, task_pid_nr(curr
));
1175 print_lock(check_src
);
1176 printk("\nbut task is already holding lock:\n");
1177 print_lock(check_tgt
);
1178 printk("\nwhich lock already depends on the new lock.\n\n");
1179 printk("\nthe existing dependency chain (in reverse order) is:\n");
1181 print_circular_bug_entry(entry
, depth
);
1186 static inline int class_equal(struct lock_list
*entry
, void *data
)
1188 return entry
->class == data
;
1191 static noinline
int print_circular_bug(struct lock_list
*this,
1192 struct lock_list
*target
,
1193 struct held_lock
*check_src
,
1194 struct held_lock
*check_tgt
)
1196 struct task_struct
*curr
= current
;
1197 struct lock_list
*parent
;
1198 struct lock_list
*first_parent
;
1201 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1204 if (!save_trace(&this->trace
))
1207 depth
= get_lock_depth(target
);
1209 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1211 parent
= get_lock_parent(target
);
1212 first_parent
= parent
;
1215 print_circular_bug_entry(parent
, --depth
);
1216 parent
= get_lock_parent(parent
);
1219 printk("\nother info that might help us debug this:\n\n");
1220 print_circular_lock_scenario(check_src
, check_tgt
,
1223 lockdep_print_held_locks(curr
);
1225 printk("\nstack backtrace:\n");
1231 static noinline
int print_bfs_bug(int ret
)
1233 if (!debug_locks_off_graph_unlock())
1237 * Breadth-first-search failed, graph got corrupted?
1239 WARN(1, "lockdep bfs error:%d\n", ret
);
1244 static int noop_count(struct lock_list
*entry
, void *data
)
1246 (*(unsigned long *)data
)++;
1250 unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1252 unsigned long count
= 0;
1253 struct lock_list
*uninitialized_var(target_entry
);
1255 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1259 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1261 unsigned long ret
, flags
;
1262 struct lock_list
this;
1267 local_irq_save(flags
);
1268 arch_spin_lock(&lockdep_lock
);
1269 ret
= __lockdep_count_forward_deps(&this);
1270 arch_spin_unlock(&lockdep_lock
);
1271 local_irq_restore(flags
);
1276 unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1278 unsigned long count
= 0;
1279 struct lock_list
*uninitialized_var(target_entry
);
1281 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1286 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1288 unsigned long ret
, flags
;
1289 struct lock_list
this;
1294 local_irq_save(flags
);
1295 arch_spin_lock(&lockdep_lock
);
1296 ret
= __lockdep_count_backward_deps(&this);
1297 arch_spin_unlock(&lockdep_lock
);
1298 local_irq_restore(flags
);
1304 * Prove that the dependency graph starting at <entry> can not
1305 * lead to <target>. Print an error and return 0 if it does.
1308 check_noncircular(struct lock_list
*root
, struct lock_class
*target
,
1309 struct lock_list
**target_entry
)
1313 debug_atomic_inc(nr_cyclic_checks
);
1315 result
= __bfs_forwards(root
, target
, class_equal
, target_entry
);
1320 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1322 * Forwards and backwards subgraph searching, for the purposes of
1323 * proving that two subgraphs can be connected by a new dependency
1324 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1327 static inline int usage_match(struct lock_list
*entry
, void *bit
)
1329 return entry
->class->usage_mask
& (1 << (enum lock_usage_bit
)bit
);
1335 * Find a node in the forwards-direction dependency sub-graph starting
1336 * at @root->class that matches @bit.
1338 * Return 0 if such a node exists in the subgraph, and put that node
1339 * into *@target_entry.
1341 * Return 1 otherwise and keep *@target_entry unchanged.
1342 * Return <0 on error.
1345 find_usage_forwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1346 struct lock_list
**target_entry
)
1350 debug_atomic_inc(nr_find_usage_forwards_checks
);
1352 result
= __bfs_forwards(root
, (void *)bit
, usage_match
, target_entry
);
1358 * Find a node in the backwards-direction dependency sub-graph starting
1359 * at @root->class that matches @bit.
1361 * Return 0 if such a node exists in the subgraph, and put that node
1362 * into *@target_entry.
1364 * Return 1 otherwise and keep *@target_entry unchanged.
1365 * Return <0 on error.
1368 find_usage_backwards(struct lock_list
*root
, enum lock_usage_bit bit
,
1369 struct lock_list
**target_entry
)
1373 debug_atomic_inc(nr_find_usage_backwards_checks
);
1375 result
= __bfs_backwards(root
, (void *)bit
, usage_match
, target_entry
);
1380 static void print_lock_class_header(struct lock_class
*class, int depth
)
1384 printk("%*s->", depth
, "");
1385 print_lock_name(class);
1386 printk(" ops: %lu", class->ops
);
1389 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1390 if (class->usage_mask
& (1 << bit
)) {
1393 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1394 len
+= printk(" at:\n");
1395 print_stack_trace(class->usage_traces
+ bit
, len
);
1398 printk("%*s }\n", depth
, "");
1400 printk("%*s ... key at: ",depth
,"");
1401 print_ip_sym((unsigned long)class->key
);
1405 * printk the shortest lock dependencies from @start to @end in reverse order:
1408 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1409 struct lock_list
*root
)
1411 struct lock_list
*entry
= leaf
;
1414 /*compute depth from generated tree by BFS*/
1415 depth
= get_lock_depth(leaf
);
1418 print_lock_class_header(entry
->class, depth
);
1419 printk("%*s ... acquired at:\n", depth
, "");
1420 print_stack_trace(&entry
->trace
, 2);
1423 if (depth
== 0 && (entry
!= root
)) {
1424 printk("lockdep:%s bad path found in chain graph\n", __func__
);
1428 entry
= get_lock_parent(entry
);
1430 } while (entry
&& (depth
>= 0));
1436 print_irq_lock_scenario(struct lock_list
*safe_entry
,
1437 struct lock_list
*unsafe_entry
,
1438 struct lock_class
*prev_class
,
1439 struct lock_class
*next_class
)
1441 struct lock_class
*safe_class
= safe_entry
->class;
1442 struct lock_class
*unsafe_class
= unsafe_entry
->class;
1443 struct lock_class
*middle_class
= prev_class
;
1445 if (middle_class
== safe_class
)
1446 middle_class
= next_class
;
1449 * A direct locking problem where unsafe_class lock is taken
1450 * directly by safe_class lock, then all we need to show
1451 * is the deadlock scenario, as it is obvious that the
1452 * unsafe lock is taken under the safe lock.
1454 * But if there is a chain instead, where the safe lock takes
1455 * an intermediate lock (middle_class) where this lock is
1456 * not the same as the safe lock, then the lock chain is
1457 * used to describe the problem. Otherwise we would need
1458 * to show a different CPU case for each link in the chain
1459 * from the safe_class lock to the unsafe_class lock.
1461 if (middle_class
!= unsafe_class
) {
1462 printk("Chain exists of:\n ");
1463 __print_lock_name(safe_class
);
1465 __print_lock_name(middle_class
);
1467 __print_lock_name(unsafe_class
);
1471 printk(" Possible interrupt unsafe locking scenario:\n\n");
1472 printk(" CPU0 CPU1\n");
1473 printk(" ---- ----\n");
1475 __print_lock_name(unsafe_class
);
1477 printk(" local_irq_disable();\n");
1479 __print_lock_name(safe_class
);
1482 __print_lock_name(middle_class
);
1484 printk(" <Interrupt>\n");
1486 __print_lock_name(safe_class
);
1488 printk("\n *** DEADLOCK ***\n\n");
1492 print_bad_irq_dependency(struct task_struct
*curr
,
1493 struct lock_list
*prev_root
,
1494 struct lock_list
*next_root
,
1495 struct lock_list
*backwards_entry
,
1496 struct lock_list
*forwards_entry
,
1497 struct held_lock
*prev
,
1498 struct held_lock
*next
,
1499 enum lock_usage_bit bit1
,
1500 enum lock_usage_bit bit2
,
1501 const char *irqclass
)
1503 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1510 printk("======================================================\n");
1511 printk("[ ProveLock INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1512 irqclass
, irqclass
);
1513 print_kernel_ident();
1514 printk("------------------------------------------------------\n");
1515 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1516 curr
->comm
, task_pid_nr(curr
),
1517 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1518 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1519 curr
->hardirqs_enabled
,
1520 curr
->softirqs_enabled
);
1523 printk("\nand this task is already holding:\n");
1525 printk("which would create a new lock dependency:\n");
1526 print_lock_name(hlock_class(prev
));
1528 print_lock_name(hlock_class(next
));
1531 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1533 print_lock_name(backwards_entry
->class);
1534 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1536 print_stack_trace(backwards_entry
->class->usage_traces
+ bit1
, 1);
1538 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1539 print_lock_name(forwards_entry
->class);
1540 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1543 print_stack_trace(forwards_entry
->class->usage_traces
+ bit2
, 1);
1545 printk("\nother info that might help us debug this:\n\n");
1546 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
1547 hlock_class(prev
), hlock_class(next
));
1549 lockdep_print_held_locks(curr
);
1551 printk("\nthe dependencies between %s-irq-safe lock", irqclass
);
1552 printk(" and the holding lock:\n");
1553 if (!save_trace(&prev_root
->trace
))
1555 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
1557 printk("\nthe dependencies between the lock to be acquired");
1558 printk(" and %s-irq-unsafe lock:\n", irqclass
);
1559 if (!save_trace(&next_root
->trace
))
1561 print_shortest_lock_dependencies(forwards_entry
, next_root
);
1563 printk("\nstack backtrace:\n");
1570 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1571 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1572 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1575 struct lock_list
this, that
;
1576 struct lock_list
*uninitialized_var(target_entry
);
1577 struct lock_list
*uninitialized_var(target_entry1
);
1581 this.class = hlock_class(prev
);
1582 ret
= find_usage_backwards(&this, bit_backwards
, &target_entry
);
1584 return print_bfs_bug(ret
);
1589 that
.class = hlock_class(next
);
1590 ret
= find_usage_forwards(&that
, bit_forwards
, &target_entry1
);
1592 return print_bfs_bug(ret
);
1596 return print_bad_irq_dependency(curr
, &this, &that
,
1597 target_entry
, target_entry1
,
1599 bit_backwards
, bit_forwards
, irqclass
);
1602 static const char *state_names
[] = {
1603 #define LOCKDEP_STATE(__STATE) \
1604 __stringify(__STATE),
1605 #include "lockdep_states.h"
1606 #undef LOCKDEP_STATE
1609 static const char *state_rnames
[] = {
1610 #define LOCKDEP_STATE(__STATE) \
1611 __stringify(__STATE)"-READ",
1612 #include "lockdep_states.h"
1613 #undef LOCKDEP_STATE
1616 static inline const char *state_name(enum lock_usage_bit bit
)
1618 return (bit
& 1) ? state_rnames
[bit
>> 2] : state_names
[bit
>> 2];
1621 static int exclusive_bit(int new_bit
)
1629 * bit 0 - write/read
1630 * bit 1 - used_in/enabled
1634 int state
= new_bit
& ~3;
1635 int dir
= new_bit
& 2;
1638 * keep state, bit flip the direction and strip read.
1640 return state
| (dir
^ 2);
1643 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1644 struct held_lock
*next
, enum lock_usage_bit bit
)
1647 * Prove that the new dependency does not connect a hardirq-safe
1648 * lock with a hardirq-unsafe lock - to achieve this we search
1649 * the backwards-subgraph starting at <prev>, and the
1650 * forwards-subgraph starting at <next>:
1652 if (!check_usage(curr
, prev
, next
, bit
,
1653 exclusive_bit(bit
), state_name(bit
)))
1659 * Prove that the new dependency does not connect a hardirq-safe-read
1660 * lock with a hardirq-unsafe lock - to achieve this we search
1661 * the backwards-subgraph starting at <prev>, and the
1662 * forwards-subgraph starting at <next>:
1664 if (!check_usage(curr
, prev
, next
, bit
,
1665 exclusive_bit(bit
), state_name(bit
)))
1672 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1673 struct held_lock
*next
)
1675 #define LOCKDEP_STATE(__STATE) \
1676 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1678 #include "lockdep_states.h"
1679 #undef LOCKDEP_STATE
1684 static void inc_chains(void)
1686 if (current
->hardirq_context
)
1687 nr_hardirq_chains
++;
1689 if (current
->softirq_context
)
1690 nr_softirq_chains
++;
1692 nr_process_chains
++;
1699 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1700 struct held_lock
*next
)
1705 static inline void inc_chains(void)
1707 nr_process_chains
++;
1713 print_deadlock_scenario(struct held_lock
*nxt
,
1714 struct held_lock
*prv
)
1716 struct lock_class
*next
= hlock_class(nxt
);
1717 struct lock_class
*prev
= hlock_class(prv
);
1719 printk(" Possible unsafe locking scenario:\n\n");
1723 __print_lock_name(prev
);
1726 __print_lock_name(next
);
1728 printk("\n *** DEADLOCK ***\n\n");
1729 printk(" May be due to missing lock nesting notation\n\n");
1733 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1734 struct held_lock
*next
)
1736 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1743 printk("=============================================\n");
1744 printk("[ ProveLock INFO: possible recursive locking detected ]\n");
1745 print_kernel_ident();
1746 printk("---------------------------------------------\n");
1747 printk("%s/%d is trying to acquire lock:\n",
1748 curr
->comm
, task_pid_nr(curr
));
1750 printk("\nbut task is already holding lock:\n");
1753 printk("\nother info that might help us debug this:\n");
1754 print_deadlock_scenario(next
, prev
);
1755 lockdep_print_held_locks(curr
);
1757 printk("\nstack backtrace:\n");
1764 * Check whether we are holding such a class already.
1766 * (Note that this has to be done separately, because the graph cannot
1767 * detect such classes of deadlocks.)
1769 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1772 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1773 struct lockdep_map
*next_instance
, int read
)
1775 struct held_lock
*prev
;
1776 struct held_lock
*nest
= NULL
;
1779 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1780 prev
= curr
->held_locks
+ i
;
1782 if (prev
->instance
== next
->nest_lock
)
1785 if (hlock_class(prev
) != hlock_class(next
))
1789 * Allow read-after-read recursion of the same
1790 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1792 if ((read
== 2) && prev
->read
)
1796 * We're holding the nest_lock, which serializes this lock's
1797 * nesting behaviour.
1802 return print_deadlock_bug(curr
, prev
, next
);
1808 * There was a chain-cache miss, and we are about to add a new dependency
1809 * to a previous lock. We recursively validate the following rules:
1811 * - would the adding of the <prev> -> <next> dependency create a
1812 * circular dependency in the graph? [== circular deadlock]
1814 * - does the new prev->next dependency connect any hardirq-safe lock
1815 * (in the full backwards-subgraph starting at <prev>) with any
1816 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1817 * <next>)? [== illegal lock inversion with hardirq contexts]
1819 * - does the new prev->next dependency connect any softirq-safe lock
1820 * (in the full backwards-subgraph starting at <prev>) with any
1821 * softirq-unsafe lock (in the full forwards-subgraph starting at
1822 * <next>)? [== illegal lock inversion with softirq contexts]
1824 * any of these scenarios could lead to a deadlock.
1826 * Then if all the validations pass, we add the forwards and backwards
1830 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1831 struct held_lock
*next
, int distance
, int trylock_loop
)
1833 struct lock_list
*entry
;
1835 struct lock_list
this;
1836 struct lock_list
*uninitialized_var(target_entry
);
1838 * Static variable, serialized by the graph_lock().
1840 * We use this static variable to save the stack trace in case
1841 * we call into this function multiple times due to encountering
1842 * trylocks in the held lock stack.
1844 static struct stack_trace trace
;
1847 * Prove that the new <prev> -> <next> dependency would not
1848 * create a circular dependency in the graph. (We do this by
1849 * forward-recursing into the graph starting at <next>, and
1850 * checking whether we can reach <prev>.)
1852 * We are using global variables to control the recursion, to
1853 * keep the stackframe size of the recursive functions low:
1855 this.class = hlock_class(next
);
1857 ret
= check_noncircular(&this, hlock_class(prev
), &target_entry
);
1859 return print_circular_bug(&this, target_entry
, next
, prev
);
1860 else if (unlikely(ret
< 0))
1861 return print_bfs_bug(ret
);
1863 if (!check_prev_add_irq(curr
, prev
, next
))
1867 * For recursive read-locks we do all the dependency checks,
1868 * but we dont store read-triggered dependencies (only
1869 * write-triggered dependencies). This ensures that only the
1870 * write-side dependencies matter, and that if for example a
1871 * write-lock never takes any other locks, then the reads are
1872 * equivalent to a NOP.
1874 if (next
->read
== 2 || prev
->read
== 2)
1877 * Is the <prev> -> <next> dependency already present?
1879 * (this may occur even though this is a new chain: consider
1880 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1881 * chains - the second one will be new, but L1 already has
1882 * L2 added to its dependency list, due to the first chain.)
1884 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
1885 if (entry
->class == hlock_class(next
)) {
1887 entry
->distance
= 1;
1892 if (!trylock_loop
&& !save_trace(&trace
))
1896 * Ok, all validations passed, add the new lock
1897 * to the previous lock's dependency list:
1899 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
1900 &hlock_class(prev
)->locks_after
,
1901 next
->acquire_ip
, distance
, &trace
);
1906 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
1907 &hlock_class(next
)->locks_before
,
1908 next
->acquire_ip
, distance
, &trace
);
1913 * Debugging printouts:
1915 if (verbose(hlock_class(prev
)) || verbose(hlock_class(next
))) {
1917 printk("\n new dependency: ");
1918 print_lock_name(hlock_class(prev
));
1920 print_lock_name(hlock_class(next
));
1923 return graph_lock();
1929 * Add the dependency to all directly-previous locks that are 'relevant'.
1930 * The ones that are relevant are (in increasing distance from curr):
1931 * all consecutive trylock entries and the final non-trylock entry - or
1932 * the end of this context's lock-chain - whichever comes first.
1935 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1937 int depth
= curr
->lockdep_depth
;
1938 int trylock_loop
= 0;
1939 struct held_lock
*hlock
;
1944 * Depth must not be zero for a non-head lock:
1949 * At least two relevant locks must exist for this
1952 if (curr
->held_locks
[depth
].irq_context
!=
1953 curr
->held_locks
[depth
-1].irq_context
)
1957 int distance
= curr
->lockdep_depth
- depth
+ 1;
1958 hlock
= curr
->held_locks
+ depth
-1;
1960 * Only non-recursive-read entries get new dependencies
1963 if (hlock
->read
!= 2) {
1964 if (!check_prev_add(curr
, hlock
, next
,
1965 distance
, trylock_loop
))
1968 * Stop after the first non-trylock entry,
1969 * as non-trylock entries have added their
1970 * own direct dependencies already, so this
1971 * lock is connected to them indirectly:
1973 if (!hlock
->trylock
)
1978 * End of lock-stack?
1983 * Stop the search if we cross into another context:
1985 if (curr
->held_locks
[depth
].irq_context
!=
1986 curr
->held_locks
[depth
-1].irq_context
)
1992 if (!debug_locks_off_graph_unlock())
1996 * Clearly we all shouldn't be here, but since we made it we
1997 * can reliable say we messed up our state. See the above two
1998 * gotos for reasons why we could possibly end up here.
2005 unsigned long nr_lock_chains
;
2006 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
2007 int nr_chain_hlocks
;
2008 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
2010 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
2012 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
2016 * Look up a dependency chain. If the key is not present yet then
2017 * add it and return 1 - in this case the new dependency chain is
2018 * validated. If the key is already hashed, return 0.
2019 * (On return with 1 graph_lock is held.)
2021 static inline int lookup_chain_cache(struct task_struct
*curr
,
2022 struct held_lock
*hlock
,
2025 struct lock_class
*class = hlock_class(hlock
);
2026 struct list_head
*hash_head
= chainhashentry(chain_key
);
2027 struct lock_chain
*chain
;
2028 struct held_lock
*hlock_curr
;
2032 * We might need to take the graph lock, ensure we've got IRQs
2033 * disabled to make this an IRQ-safe lock.. for recursion reasons
2034 * lockdep won't complain about its own locking errors.
2036 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2039 * We can walk it lock-free, because entries only get added
2042 list_for_each_entry(chain
, hash_head
, entry
) {
2043 if (chain
->chain_key
== chain_key
) {
2045 debug_atomic_inc(chain_lookup_hits
);
2046 if (very_verbose(class))
2047 printk("\nhash chain already cached, key: "
2048 "%016Lx tail class: [%p] %s\n",
2049 (unsigned long long)chain_key
,
2050 class->key
, class->name
);
2054 if (very_verbose(class))
2055 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2056 (unsigned long long)chain_key
, class->key
, class->name
);
2058 * Allocate a new chain entry from the static array, and add
2064 * We have to walk the chain again locked - to avoid duplicates:
2066 list_for_each_entry(chain
, hash_head
, entry
) {
2067 if (chain
->chain_key
== chain_key
) {
2072 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
2073 if (!debug_locks_off_graph_unlock())
2076 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2080 chain
= lock_chains
+ nr_lock_chains
++;
2081 chain
->chain_key
= chain_key
;
2082 chain
->irq_context
= hlock
->irq_context
;
2083 /* Find the first held_lock of current chain */
2084 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2085 hlock_curr
= curr
->held_locks
+ i
;
2086 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2090 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
2091 if (likely(nr_chain_hlocks
+ chain
->depth
<= MAX_LOCKDEP_CHAIN_HLOCKS
)) {
2092 chain
->base
= nr_chain_hlocks
;
2093 nr_chain_hlocks
+= chain
->depth
;
2094 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
2095 int lock_id
= curr
->held_locks
[i
].class_idx
- 1;
2096 chain_hlocks
[chain
->base
+ j
] = lock_id
;
2098 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
2100 list_add_tail_rcu(&chain
->entry
, hash_head
);
2101 debug_atomic_inc(chain_lookup_misses
);
2107 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
2108 struct held_lock
*hlock
, int chain_head
, u64 chain_key
)
2111 * Trylock needs to maintain the stack of held locks, but it
2112 * does not add new dependencies, because trylock can be done
2115 * We look up the chain_key and do the O(N^2) check and update of
2116 * the dependencies only if this is a new dependency chain.
2117 * (If lookup_chain_cache() returns with 1 it acquires
2118 * graph_lock for us)
2120 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
2121 lookup_chain_cache(curr
, hlock
, chain_key
)) {
2123 * Check whether last held lock:
2125 * - is irq-safe, if this lock is irq-unsafe
2126 * - is softirq-safe, if this lock is hardirq-unsafe
2128 * And check whether the new lock's dependency graph
2129 * could lead back to the previous lock.
2131 * any of these scenarios could lead to a deadlock. If
2134 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
2139 * Mark recursive read, as we jump over it when
2140 * building dependencies (just like we jump over
2146 * Add dependency only if this lock is not the head
2147 * of the chain, and if it's not a secondary read-lock:
2149 if (!chain_head
&& ret
!= 2)
2150 if (!check_prevs_add(curr
, hlock
))
2154 /* after lookup_chain_cache(): */
2155 if (unlikely(!debug_locks
))
2161 static inline int validate_chain(struct task_struct
*curr
,
2162 struct lockdep_map
*lock
, struct held_lock
*hlock
,
2163 int chain_head
, u64 chain_key
)
2170 * We are building curr_chain_key incrementally, so double-check
2171 * it from scratch, to make sure that it's done correctly:
2173 static void check_chain_key(struct task_struct
*curr
)
2175 #ifdef CONFIG_DEBUG_LOCKDEP
2176 struct held_lock
*hlock
, *prev_hlock
= NULL
;
2180 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2181 hlock
= curr
->held_locks
+ i
;
2182 if (chain_key
!= hlock
->prev_chain_key
) {
2185 * We got mighty confused, our chain keys don't match
2186 * with what we expect, someone trample on our task state?
2188 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2189 curr
->lockdep_depth
, i
,
2190 (unsigned long long)chain_key
,
2191 (unsigned long long)hlock
->prev_chain_key
);
2194 id
= hlock
->class_idx
- 1;
2196 * Whoops ran out of static storage again?
2198 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2201 if (prev_hlock
&& (prev_hlock
->irq_context
!=
2202 hlock
->irq_context
))
2204 chain_key
= iterate_chain_key(chain_key
, id
);
2207 if (chain_key
!= curr
->curr_chain_key
) {
2210 * More smoking hash instead of calculating it, damn see these
2211 * numbers float.. I bet that a pink elephant stepped on my memory.
2213 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2214 curr
->lockdep_depth
, i
,
2215 (unsigned long long)chain_key
,
2216 (unsigned long long)curr
->curr_chain_key
);
2222 print_usage_bug_scenario(struct held_lock
*lock
)
2224 struct lock_class
*class = hlock_class(lock
);
2226 printk(" Possible unsafe locking scenario:\n\n");
2230 __print_lock_name(class);
2232 printk(" <Interrupt>\n");
2234 __print_lock_name(class);
2236 printk("\n *** DEADLOCK ***\n\n");
2240 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
2241 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
2243 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2250 printk("=================================\n");
2251 printk("[ ProveLock INFO: inconsistent lock state ]\n");
2252 print_kernel_ident();
2253 printk("---------------------------------\n");
2255 printk("inconsistent {%s} -> {%s} usage.\n",
2256 usage_str
[prev_bit
], usage_str
[new_bit
]);
2258 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2259 curr
->comm
, task_pid_nr(curr
),
2260 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
2261 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
2262 trace_hardirqs_enabled(curr
),
2263 trace_softirqs_enabled(curr
));
2266 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
2267 print_stack_trace(hlock_class(this)->usage_traces
+ prev_bit
, 1);
2269 print_irqtrace_events(curr
);
2270 printk("\nother info that might help us debug this:\n");
2271 print_usage_bug_scenario(this);
2273 lockdep_print_held_locks(curr
);
2275 printk("\nstack backtrace:\n");
2282 * Print out an error if an invalid bit is set:
2285 valid_state(struct task_struct
*curr
, struct held_lock
*this,
2286 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
2288 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
)))
2289 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
2293 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2294 enum lock_usage_bit new_bit
);
2296 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2299 * print irq inversion bug:
2302 print_irq_inversion_bug(struct task_struct
*curr
,
2303 struct lock_list
*root
, struct lock_list
*other
,
2304 struct held_lock
*this, int forwards
,
2305 const char *irqclass
)
2307 struct lock_list
*entry
= other
;
2308 struct lock_list
*middle
= NULL
;
2311 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2318 printk("=========================================================\n");
2319 printk("[ ProveLock INFO: possible irq lock inversion dependency detected ]\n");
2320 print_kernel_ident();
2321 printk("---------------------------------------------------------\n");
2322 printk("%s/%d just changed the state of lock:\n",
2323 curr
->comm
, task_pid_nr(curr
));
2326 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
2328 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
2329 print_lock_name(other
->class);
2330 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2332 printk("\nother info that might help us debug this:\n");
2334 /* Find a middle lock (if one exists) */
2335 depth
= get_lock_depth(other
);
2337 if (depth
== 0 && (entry
!= root
)) {
2338 printk("lockdep:%s bad path found in chain graph\n", __func__
);
2342 entry
= get_lock_parent(entry
);
2344 } while (entry
&& entry
!= root
&& (depth
>= 0));
2346 print_irq_lock_scenario(root
, other
,
2347 middle
? middle
->class : root
->class, other
->class);
2349 print_irq_lock_scenario(other
, root
,
2350 middle
? middle
->class : other
->class, root
->class);
2352 lockdep_print_held_locks(curr
);
2354 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2355 if (!save_trace(&root
->trace
))
2357 print_shortest_lock_dependencies(other
, root
);
2359 printk("\nstack backtrace:\n");
2366 * Prove that in the forwards-direction subgraph starting at <this>
2367 * there is no lock matching <mask>:
2370 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
2371 enum lock_usage_bit bit
, const char *irqclass
)
2374 struct lock_list root
;
2375 struct lock_list
*uninitialized_var(target_entry
);
2378 root
.class = hlock_class(this);
2379 ret
= find_usage_forwards(&root
, bit
, &target_entry
);
2381 return print_bfs_bug(ret
);
2385 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2390 * Prove that in the backwards-direction subgraph starting at <this>
2391 * there is no lock matching <mask>:
2394 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
2395 enum lock_usage_bit bit
, const char *irqclass
)
2398 struct lock_list root
;
2399 struct lock_list
*uninitialized_var(target_entry
);
2402 root
.class = hlock_class(this);
2403 ret
= find_usage_backwards(&root
, bit
, &target_entry
);
2405 return print_bfs_bug(ret
);
2409 return print_irq_inversion_bug(curr
, &root
, target_entry
,
2413 void print_irqtrace_events(struct task_struct
*curr
)
2415 printk("irq event stamp: %u\n", curr
->irq_events
);
2416 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
2417 print_ip_sym(curr
->hardirq_enable_ip
);
2418 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
2419 print_ip_sym(curr
->hardirq_disable_ip
);
2420 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
2421 print_ip_sym(curr
->softirq_enable_ip
);
2422 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
2423 print_ip_sym(curr
->softirq_disable_ip
);
2426 static int HARDIRQ_verbose(struct lock_class
*class)
2429 return class_filter(class);
2434 static int SOFTIRQ_verbose(struct lock_class
*class)
2437 return class_filter(class);
2442 static int RECLAIM_FS_verbose(struct lock_class
*class)
2445 return class_filter(class);
2450 #define STRICT_READ_CHECKS 1
2452 static int (*state_verbose_f
[])(struct lock_class
*class) = {
2453 #define LOCKDEP_STATE(__STATE) \
2455 #include "lockdep_states.h"
2456 #undef LOCKDEP_STATE
2459 static inline int state_verbose(enum lock_usage_bit bit
,
2460 struct lock_class
*class)
2462 return state_verbose_f
[bit
>> 2](class);
2465 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
2466 enum lock_usage_bit bit
, const char *name
);
2469 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2470 enum lock_usage_bit new_bit
)
2472 int excl_bit
= exclusive_bit(new_bit
);
2473 int read
= new_bit
& 1;
2474 int dir
= new_bit
& 2;
2477 * mark USED_IN has to look forwards -- to ensure no dependency
2478 * has ENABLED state, which would allow recursion deadlocks.
2480 * mark ENABLED has to look backwards -- to ensure no dependee
2481 * has USED_IN state, which, again, would allow recursion deadlocks.
2483 check_usage_f usage
= dir
?
2484 check_usage_backwards
: check_usage_forwards
;
2487 * Validate that this particular lock does not have conflicting
2490 if (!valid_state(curr
, this, new_bit
, excl_bit
))
2494 * Validate that the lock dependencies don't have conflicting usage
2497 if ((!read
|| !dir
|| STRICT_READ_CHECKS
) &&
2498 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~1)))
2502 * Check for read in write conflicts
2505 if (!valid_state(curr
, this, new_bit
, excl_bit
+ 1))
2508 if (STRICT_READ_CHECKS
&&
2509 !usage(curr
, this, excl_bit
+ 1,
2510 state_name(new_bit
+ 1)))
2514 if (state_verbose(new_bit
, hlock_class(this)))
2521 #define LOCKDEP_STATE(__STATE) __STATE,
2522 #include "lockdep_states.h"
2523 #undef LOCKDEP_STATE
2527 * Mark all held locks with a usage bit:
2530 mark_held_locks(struct task_struct
*curr
, enum mark_type mark
)
2532 enum lock_usage_bit usage_bit
;
2533 struct held_lock
*hlock
;
2536 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2537 hlock
= curr
->held_locks
+ i
;
2539 usage_bit
= 2 + (mark
<< 2); /* ENABLED */
2541 usage_bit
+= 1; /* READ */
2543 BUG_ON(usage_bit
>= LOCK_USAGE_STATES
);
2545 if (hlock_class(hlock
)->key
== __lockdep_no_validate__
.subkeys
)
2548 if (!mark_lock(curr
, hlock
, usage_bit
))
2556 * Hardirqs will be enabled:
2558 static void __trace_hardirqs_on_caller(unsigned long ip
)
2560 struct task_struct
*curr
= current
;
2562 /* we'll do an OFF -> ON transition: */
2563 curr
->hardirqs_enabled
= 1;
2566 * We are going to turn hardirqs on, so set the
2567 * usage bit for all held locks:
2569 if (!mark_held_locks(curr
, HARDIRQ
))
2572 * If we have softirqs enabled, then set the usage
2573 * bit for all held locks. (disabled hardirqs prevented
2574 * this bit from being set before)
2576 if (curr
->softirqs_enabled
)
2577 if (!mark_held_locks(curr
, SOFTIRQ
))
2580 curr
->hardirq_enable_ip
= ip
;
2581 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2582 debug_atomic_inc(hardirqs_on_events
);
2585 void trace_hardirqs_on_caller(unsigned long ip
)
2587 time_hardirqs_on(CALLER_ADDR0
, ip
);
2589 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2592 if (unlikely(current
->hardirqs_enabled
)) {
2594 * Neither irq nor preemption are disabled here
2595 * so this is racy by nature but losing one hit
2596 * in a stat is not a big deal.
2598 __debug_atomic_inc(redundant_hardirqs_on
);
2603 * We're enabling irqs and according to our state above irqs weren't
2604 * already enabled, yet we find the hardware thinks they are in fact
2605 * enabled.. someone messed up their IRQ state tracing.
2607 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2611 * See the fine text that goes along with this variable definition.
2613 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled
)))
2617 * Can't allow enabling interrupts while in an interrupt handler,
2618 * that's general bad form and such. Recursion, limited stack etc..
2620 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2623 current
->lockdep_recursion
= 1;
2624 __trace_hardirqs_on_caller(ip
);
2625 current
->lockdep_recursion
= 0;
2627 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
2629 void trace_hardirqs_on(void)
2631 trace_hardirqs_on_caller(CALLER_ADDR0
);
2633 EXPORT_SYMBOL(trace_hardirqs_on
);
2636 * Hardirqs were disabled:
2638 void trace_hardirqs_off_caller(unsigned long ip
)
2640 struct task_struct
*curr
= current
;
2642 time_hardirqs_off(CALLER_ADDR0
, ip
);
2644 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2648 * So we're supposed to get called after you mask local IRQs, but for
2649 * some reason the hardware doesn't quite think you did a proper job.
2651 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2654 if (curr
->hardirqs_enabled
) {
2656 * We have done an ON -> OFF transition:
2658 curr
->hardirqs_enabled
= 0;
2659 curr
->hardirq_disable_ip
= ip
;
2660 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2661 debug_atomic_inc(hardirqs_off_events
);
2663 debug_atomic_inc(redundant_hardirqs_off
);
2665 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
2667 void trace_hardirqs_off(void)
2669 trace_hardirqs_off_caller(CALLER_ADDR0
);
2671 EXPORT_SYMBOL(trace_hardirqs_off
);
2674 * Softirqs will be enabled:
2676 void trace_softirqs_on(unsigned long ip
)
2678 struct task_struct
*curr
= current
;
2680 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2684 * We fancy IRQs being disabled here, see softirq.c, avoids
2685 * funny state and nesting things.
2687 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2690 if (curr
->softirqs_enabled
) {
2691 debug_atomic_inc(redundant_softirqs_on
);
2695 current
->lockdep_recursion
= 1;
2697 * We'll do an OFF -> ON transition:
2699 curr
->softirqs_enabled
= 1;
2700 curr
->softirq_enable_ip
= ip
;
2701 curr
->softirq_enable_event
= ++curr
->irq_events
;
2702 debug_atomic_inc(softirqs_on_events
);
2704 * We are going to turn softirqs on, so set the
2705 * usage bit for all held locks, if hardirqs are
2708 if (curr
->hardirqs_enabled
)
2709 mark_held_locks(curr
, SOFTIRQ
);
2710 current
->lockdep_recursion
= 0;
2714 * Softirqs were disabled:
2716 void trace_softirqs_off(unsigned long ip
)
2718 struct task_struct
*curr
= current
;
2720 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2724 * We fancy IRQs being disabled here, see softirq.c
2726 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2729 if (curr
->softirqs_enabled
) {
2731 * We have done an ON -> OFF transition:
2733 curr
->softirqs_enabled
= 0;
2734 curr
->softirq_disable_ip
= ip
;
2735 curr
->softirq_disable_event
= ++curr
->irq_events
;
2736 debug_atomic_inc(softirqs_off_events
);
2738 * Whoops, we wanted softirqs off, so why aren't they?
2740 DEBUG_LOCKS_WARN_ON(!softirq_count());
2742 debug_atomic_inc(redundant_softirqs_off
);
2745 static void __lockdep_trace_alloc(gfp_t gfp_mask
, unsigned long flags
)
2747 struct task_struct
*curr
= current
;
2749 if (unlikely(!debug_locks
))
2752 /* no reclaim without waiting on it */
2753 if (!(gfp_mask
& __GFP_WAIT
))
2756 /* this guy won't enter reclaim */
2757 if ((curr
->flags
& PF_MEMALLOC
) && !(gfp_mask
& __GFP_NOMEMALLOC
))
2760 /* We're only interested __GFP_FS allocations for now */
2761 if (!(gfp_mask
& __GFP_FS
))
2765 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2767 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags
)))
2770 mark_held_locks(curr
, RECLAIM_FS
);
2773 static void check_flags(unsigned long flags
);
2775 void lockdep_trace_alloc(gfp_t gfp_mask
)
2777 unsigned long flags
;
2779 if (unlikely(current
->lockdep_recursion
))
2782 raw_local_irq_save(flags
);
2784 current
->lockdep_recursion
= 1;
2785 __lockdep_trace_alloc(gfp_mask
, flags
);
2786 current
->lockdep_recursion
= 0;
2787 raw_local_irq_restore(flags
);
2790 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2793 * If non-trylock use in a hardirq or softirq context, then
2794 * mark the lock as used in these contexts:
2796 if (!hlock
->trylock
) {
2798 if (curr
->hardirq_context
)
2799 if (!mark_lock(curr
, hlock
,
2800 LOCK_USED_IN_HARDIRQ_READ
))
2802 if (curr
->softirq_context
)
2803 if (!mark_lock(curr
, hlock
,
2804 LOCK_USED_IN_SOFTIRQ_READ
))
2807 if (curr
->hardirq_context
)
2808 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2810 if (curr
->softirq_context
)
2811 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2815 if (!hlock
->hardirqs_off
) {
2817 if (!mark_lock(curr
, hlock
,
2818 LOCK_ENABLED_HARDIRQ_READ
))
2820 if (curr
->softirqs_enabled
)
2821 if (!mark_lock(curr
, hlock
,
2822 LOCK_ENABLED_SOFTIRQ_READ
))
2825 if (!mark_lock(curr
, hlock
,
2826 LOCK_ENABLED_HARDIRQ
))
2828 if (curr
->softirqs_enabled
)
2829 if (!mark_lock(curr
, hlock
,
2830 LOCK_ENABLED_SOFTIRQ
))
2836 * We reuse the irq context infrastructure more broadly as a general
2837 * context checking code. This tests GFP_FS recursion (a lock taken
2838 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2841 if (!hlock
->trylock
&& (curr
->lockdep_reclaim_gfp
& __GFP_FS
)) {
2843 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS_READ
))
2846 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_RECLAIM_FS
))
2854 static int separate_irq_context(struct task_struct
*curr
,
2855 struct held_lock
*hlock
)
2857 unsigned int depth
= curr
->lockdep_depth
;
2860 * Keep track of points where we cross into an interrupt context:
2862 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2863 curr
->softirq_context
;
2865 struct held_lock
*prev_hlock
;
2867 prev_hlock
= curr
->held_locks
+ depth
-1;
2869 * If we cross into another context, reset the
2870 * hash key (this also prevents the checking and the
2871 * adding of the dependency to 'prev'):
2873 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2879 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2882 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2883 enum lock_usage_bit new_bit
)
2885 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
2889 static inline int mark_irqflags(struct task_struct
*curr
,
2890 struct held_lock
*hlock
)
2895 static inline int separate_irq_context(struct task_struct
*curr
,
2896 struct held_lock
*hlock
)
2901 void lockdep_trace_alloc(gfp_t gfp_mask
)
2905 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2908 * Mark a lock with a usage bit, and validate the state transition:
2910 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2911 enum lock_usage_bit new_bit
)
2913 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2916 * If already set then do not dirty the cacheline,
2917 * nor do any checks:
2919 if (likely(hlock_class(this)->usage_mask
& new_mask
))
2925 * Make sure we didn't race:
2927 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
2932 hlock_class(this)->usage_mask
|= new_mask
;
2934 if (!save_trace(hlock_class(this)->usage_traces
+ new_bit
))
2938 #define LOCKDEP_STATE(__STATE) \
2939 case LOCK_USED_IN_##__STATE: \
2940 case LOCK_USED_IN_##__STATE##_READ: \
2941 case LOCK_ENABLED_##__STATE: \
2942 case LOCK_ENABLED_##__STATE##_READ:
2943 #include "lockdep_states.h"
2944 #undef LOCKDEP_STATE
2945 ret
= mark_lock_irq(curr
, this, new_bit
);
2950 debug_atomic_dec(nr_unused_locks
);
2953 if (!debug_locks_off_graph_unlock())
2962 * We must printk outside of the graph_lock:
2965 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2967 print_irqtrace_events(curr
);
2975 * Initialize a lock instance's lock-class mapping info:
2977 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2978 struct lock_class_key
*key
, int subclass
)
2982 kmemcheck_mark_initialized(lock
, sizeof(*lock
));
2984 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
2985 lock
->class_cache
[i
] = NULL
;
2987 #ifdef CONFIG_LOCK_STAT
2988 lock
->cpu
= raw_smp_processor_id();
2992 * Can't be having no nameless bastards around this place!
2994 if (DEBUG_LOCKS_WARN_ON(!name
)) {
2995 lock
->name
= "NULL";
3002 * No key, no joy, we need to hash something.
3004 if (DEBUG_LOCKS_WARN_ON(!key
))
3007 * Sanity check, the lock-class key must be persistent:
3009 if (!static_obj(key
)) {
3010 printk("BUG: key %p not in .data!\n", key
);
3012 * What it says above ^^^^^, I suggest you read it.
3014 DEBUG_LOCKS_WARN_ON(1);
3019 if (unlikely(!debug_locks
))
3023 register_lock_class(lock
, subclass
, 1);
3025 EXPORT_SYMBOL_GPL(lockdep_init_map
);
3027 struct lock_class_key __lockdep_no_validate__
;
3028 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
3031 print_lock_nested_lock_not_held(struct task_struct
*curr
,
3032 struct held_lock
*hlock
,
3035 if (!debug_locks_off())
3037 if (debug_locks_silent
)
3041 printk("==================================\n");
3042 printk("[ BUG: Nested lock was not taken ]\n");
3043 print_kernel_ident();
3044 printk("----------------------------------\n");
3046 printk("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3049 printk("\nbut this task is not holding:\n");
3050 printk("%s\n", hlock
->nest_lock
->name
);
3052 printk("\nstack backtrace:\n");
3055 printk("\nother info that might help us debug this:\n");
3056 lockdep_print_held_locks(curr
);
3058 printk("\nstack backtrace:\n");
3064 static int __lock_is_held(struct lockdep_map
*lock
);
3067 * This gets called for every mutex_lock*()/spin_lock*() operation.
3068 * We maintain the dependency maps and validate the locking attempt:
3070 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3071 int trylock
, int read
, int check
, int hardirqs_off
,
3072 struct lockdep_map
*nest_lock
, unsigned long ip
,
3075 struct task_struct
*curr
= current
;
3076 struct lock_class
*class = NULL
;
3077 struct held_lock
*hlock
;
3078 unsigned int depth
, id
;
3086 if (unlikely(!debug_locks
))
3090 * Lockdep should run with IRQs disabled, otherwise we could
3091 * get an interrupt which would want to take locks, which would
3092 * end up in lockdep and have you got a head-ache already?
3094 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3097 if (lock
->key
== &__lockdep_no_validate__
)
3100 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
3101 class = lock
->class_cache
[subclass
];
3105 if (unlikely(!class)) {
3106 class = register_lock_class(lock
, subclass
, 0);
3110 atomic_inc((atomic_t
*)&class->ops
);
3111 if (very_verbose(class)) {
3112 printk("\nacquire class [%p] %s", class->key
, class->name
);
3113 if (class->name_version
> 1)
3114 printk("#%d", class->name_version
);
3120 * Add the lock to the list of currently held locks.
3121 * (we dont increase the depth just yet, up until the
3122 * dependency checks are done)
3124 depth
= curr
->lockdep_depth
;
3126 * Ran out of static storage for our per-task lock stack again have we?
3128 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
3131 class_idx
= class - lock_classes
+ 1;
3134 hlock
= curr
->held_locks
+ depth
- 1;
3135 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
3136 if (hlock
->references
)
3137 hlock
->references
++;
3139 hlock
->references
= 2;
3145 hlock
= curr
->held_locks
+ depth
;
3147 * Plain impossible, we just registered it and checked it weren't no
3148 * NULL like.. I bet this mushroom I ate was good!
3150 if (DEBUG_LOCKS_WARN_ON(!class))
3152 hlock
->class_idx
= class_idx
;
3153 hlock
->acquire_ip
= ip
;
3154 hlock
->instance
= lock
;
3155 hlock
->nest_lock
= nest_lock
;
3156 hlock
->trylock
= trylock
;
3158 hlock
->check
= check
;
3159 hlock
->hardirqs_off
= !!hardirqs_off
;
3160 hlock
->references
= references
;
3161 #ifdef CONFIG_LOCK_STAT
3162 hlock
->waittime_stamp
= 0;
3163 hlock
->holdtime_stamp
= lockstat_clock();
3166 if (check
== 2 && !mark_irqflags(curr
, hlock
))
3169 /* mark it as used: */
3170 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3174 * Calculate the chain hash: it's the combined hash of all the
3175 * lock keys along the dependency chain. We save the hash value
3176 * at every step so that we can get the current hash easily
3177 * after unlock. The chain hash is then used to cache dependency
3180 * The 'key ID' is what is the most compact key value to drive
3181 * the hash, not class->key.
3183 id
= class - lock_classes
;
3185 * Whoops, we did it again.. ran straight out of our static allocation.
3187 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
3190 chain_key
= curr
->curr_chain_key
;
3193 * How can we have a chain hash when we ain't got no keys?!
3195 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
3200 hlock
->prev_chain_key
= chain_key
;
3201 if (separate_irq_context(curr
, hlock
)) {
3205 chain_key
= iterate_chain_key(chain_key
, id
);
3207 if (nest_lock
&& !__lock_is_held(nest_lock
))
3208 return print_lock_nested_lock_not_held(curr
, hlock
, ip
);
3210 if (!validate_chain(curr
, lock
, hlock
, chain_head
, chain_key
))
3213 curr
->curr_chain_key
= chain_key
;
3214 curr
->lockdep_depth
++;
3215 check_chain_key(curr
);
3216 #ifdef CONFIG_DEBUG_LOCKDEP
3217 if (unlikely(!debug_locks
))
3220 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
3222 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3223 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
3224 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
3226 lockdep_print_held_locks(current
);
3227 debug_show_all_locks();
3233 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
3234 max_lockdep_depth
= curr
->lockdep_depth
;
3240 print_unlock_imbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3243 if (!debug_locks_off())
3245 if (debug_locks_silent
)
3252 printk("=====================================\n");
3253 printk("[ ProveLock BUG: bad unlock balance detected! ]\n");
3254 print_kernel_ident();
3255 printk("-------------------------------------\n");
3256 printk("%s/%d is trying to release lock (",
3257 curr
->comm
, task_pid_nr(curr
));
3258 print_lockdep_cache(lock
);
3261 printk("but there are no more locks to release!\n");
3262 printk("\nother info that might help us debug this:\n");
3263 lockdep_print_held_locks(curr
);
3265 printk("\nstack backtrace:\n");
3272 * Common debugging checks for both nested and non-nested unlock:
3274 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
3277 if (unlikely(!debug_locks
))
3280 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3282 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3285 if (curr
->lockdep_depth
<= 0)
3286 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3291 static int match_held_lock(struct held_lock
*hlock
, struct lockdep_map
*lock
)
3293 if (hlock
->instance
== lock
)
3296 if (hlock
->references
) {
3297 struct lock_class
*class = lock
->class_cache
[0];
3300 class = look_up_lock_class(lock
, 0);
3303 * If look_up_lock_class() failed to find a class, we're trying
3304 * to test if we hold a lock that has never yet been acquired.
3305 * Clearly if the lock hasn't been acquired _ever_, we're not
3306 * holding it either, so report failure.
3312 * References, but not a lock we're actually ref-counting?
3313 * State got messed up, follow the sites that change ->references
3314 * and try to make sense of it.
3316 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
3319 if (hlock
->class_idx
== class - lock_classes
+ 1)
3327 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
3328 struct lock_class_key
*key
, unsigned int subclass
,
3331 struct task_struct
*curr
= current
;
3332 struct held_lock
*hlock
, *prev_hlock
;
3333 struct lock_class
*class;
3337 depth
= curr
->lockdep_depth
;
3339 * This function is about (re)setting the class of a held lock,
3340 * yet we're not actually holding any locks. Naughty user!
3342 if (DEBUG_LOCKS_WARN_ON(!depth
))
3346 for (i
= depth
-1; i
>= 0; i
--) {
3347 hlock
= curr
->held_locks
+ i
;
3349 * We must not cross into another context:
3351 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3353 if (match_held_lock(hlock
, lock
))
3357 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3360 lockdep_init_map(lock
, name
, key
, 0);
3361 class = register_lock_class(lock
, subclass
, 0);
3362 hlock
->class_idx
= class - lock_classes
+ 1;
3364 curr
->lockdep_depth
= i
;
3365 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3367 for (; i
< depth
; i
++) {
3368 hlock
= curr
->held_locks
+ i
;
3369 if (!__lock_acquire(hlock
->instance
,
3370 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3371 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3372 hlock
->nest_lock
, hlock
->acquire_ip
,
3378 * I took it apart and put it back together again, except now I have
3379 * these 'spare' parts.. where shall I put them.
3381 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
3387 * Remove the lock to the list of currently held locks in a
3388 * potentially non-nested (out of order) manner. This is a
3389 * relatively rare operation, as all the unlock APIs default
3390 * to nested mode (which uses lock_release()):
3393 lock_release_non_nested(struct task_struct
*curr
,
3394 struct lockdep_map
*lock
, unsigned long ip
)
3396 struct held_lock
*hlock
, *prev_hlock
;
3401 * Check whether the lock exists in the current stack
3404 depth
= curr
->lockdep_depth
;
3406 * So we're all set to release this lock.. wait what lock? We don't
3407 * own any locks, you've been drinking again?
3409 if (DEBUG_LOCKS_WARN_ON(!depth
))
3413 for (i
= depth
-1; i
>= 0; i
--) {
3414 hlock
= curr
->held_locks
+ i
;
3416 * We must not cross into another context:
3418 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3420 if (match_held_lock(hlock
, lock
))
3424 return print_unlock_imbalance_bug(curr
, lock
, ip
);
3427 if (hlock
->instance
== lock
)
3428 lock_release_holdtime(hlock
);
3430 if (hlock
->references
) {
3431 hlock
->references
--;
3432 if (hlock
->references
) {
3434 * We had, and after removing one, still have
3435 * references, the current lock stack is still
3436 * valid. We're done!
3443 * We have the right lock to unlock, 'hlock' points to it.
3444 * Now we remove it from the stack, and add back the other
3445 * entries (if any), recalculating the hash along the way:
3448 curr
->lockdep_depth
= i
;
3449 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3451 for (i
++; i
< depth
; i
++) {
3452 hlock
= curr
->held_locks
+ i
;
3453 if (!__lock_acquire(hlock
->instance
,
3454 hlock_class(hlock
)->subclass
, hlock
->trylock
,
3455 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
3456 hlock
->nest_lock
, hlock
->acquire_ip
,
3462 * We had N bottles of beer on the wall, we drank one, but now
3463 * there's not N-1 bottles of beer left on the wall...
3465 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
3471 * Remove the lock to the list of currently held locks - this gets
3472 * called on mutex_unlock()/spin_unlock*() (or on a failed
3473 * mutex_lock_interruptible()). This is done for unlocks that nest
3474 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3476 static int lock_release_nested(struct task_struct
*curr
,
3477 struct lockdep_map
*lock
, unsigned long ip
)
3479 struct held_lock
*hlock
;
3483 * Pop off the top of the lock stack:
3485 depth
= curr
->lockdep_depth
- 1;
3486 hlock
= curr
->held_locks
+ depth
;
3489 * Is the unlock non-nested:
3491 if (hlock
->instance
!= lock
|| hlock
->references
)
3492 return lock_release_non_nested(curr
, lock
, ip
);
3493 curr
->lockdep_depth
--;
3496 * No more locks, but somehow we've got hash left over, who left it?
3498 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
3501 curr
->curr_chain_key
= hlock
->prev_chain_key
;
3503 lock_release_holdtime(hlock
);
3505 #ifdef CONFIG_DEBUG_LOCKDEP
3506 hlock
->prev_chain_key
= 0;
3507 hlock
->class_idx
= 0;
3508 hlock
->acquire_ip
= 0;
3509 hlock
->irq_context
= 0;
3515 * Remove the lock to the list of currently held locks - this gets
3516 * called on mutex_unlock()/spin_unlock*() (or on a failed
3517 * mutex_lock_interruptible()). This is done for unlocks that nest
3518 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3521 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
3523 struct task_struct
*curr
= current
;
3525 if (!check_unlock(curr
, lock
, ip
))
3529 if (!lock_release_nested(curr
, lock
, ip
))
3532 if (!lock_release_non_nested(curr
, lock
, ip
))
3536 check_chain_key(curr
);
3539 static int __lock_is_held(struct lockdep_map
*lock
)
3541 struct task_struct
*curr
= current
;
3544 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3545 struct held_lock
*hlock
= curr
->held_locks
+ i
;
3547 if (match_held_lock(hlock
, lock
))
3555 * Check whether we follow the irq-flags state precisely:
3557 static void check_flags(unsigned long flags
)
3559 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3560 defined(CONFIG_TRACE_IRQFLAGS)
3564 if (irqs_disabled_flags(flags
)) {
3565 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
3566 printk("possible reason: unannotated irqs-off.\n");
3569 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
3570 printk("possible reason: unannotated irqs-on.\n");
3575 * We dont accurately track softirq state in e.g.
3576 * hardirq contexts (such as on 4KSTACKS), so only
3577 * check if not in hardirq contexts:
3579 if (!hardirq_count()) {
3580 if (softirq_count()) {
3581 /* like the above, but with softirqs */
3582 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
3584 /* lick the above, does it taste good? */
3585 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
3590 print_irqtrace_events(current
);
3594 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
3595 struct lock_class_key
*key
, unsigned int subclass
,
3598 unsigned long flags
;
3600 if (unlikely(current
->lockdep_recursion
))
3603 raw_local_irq_save(flags
);
3604 current
->lockdep_recursion
= 1;
3606 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
3607 check_chain_key(current
);
3608 current
->lockdep_recursion
= 0;
3609 raw_local_irq_restore(flags
);
3611 EXPORT_SYMBOL_GPL(lock_set_class
);
3614 * We are not always called with irqs disabled - do that here,
3615 * and also avoid lockdep recursion:
3617 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
3618 int trylock
, int read
, int check
,
3619 struct lockdep_map
*nest_lock
, unsigned long ip
)
3621 unsigned long flags
;
3622 if (unlikely(!debug_locks
))
3624 if (unlikely(current
->lockdep_recursion
))
3627 if (unlikely(lock
->skip
==1))
3630 raw_local_irq_save(flags
);
3633 current
->lockdep_recursion
= 1;
3634 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
3635 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
3636 irqs_disabled_flags(flags
), nest_lock
, ip
, 0);
3637 current
->lockdep_recursion
= 0;
3638 raw_local_irq_restore(flags
);
3640 EXPORT_SYMBOL_GPL(lock_acquire
);
3642 void lock_release(struct lockdep_map
*lock
, int nested
,
3645 unsigned long flags
;
3646 if (unlikely(!debug_locks
))
3649 if (unlikely(current
->lockdep_recursion
))
3652 if (unlikely(lock
->skip
==1))
3655 raw_local_irq_save(flags
);
3657 current
->lockdep_recursion
= 1;
3658 trace_lock_release(lock
, ip
);
3659 __lock_release(lock
, nested
, ip
);
3660 current
->lockdep_recursion
= 0;
3661 raw_local_irq_restore(flags
);
3663 EXPORT_SYMBOL_GPL(lock_release
);
3665 int lock_is_held(struct lockdep_map
*lock
)
3667 unsigned long flags
;
3670 if (unlikely(current
->lockdep_recursion
))
3671 return 1; /* avoid false negative lockdep_assert_held() */
3673 raw_local_irq_save(flags
);
3676 current
->lockdep_recursion
= 1;
3677 ret
= __lock_is_held(lock
);
3678 current
->lockdep_recursion
= 0;
3679 raw_local_irq_restore(flags
);
3683 EXPORT_SYMBOL_GPL(lock_is_held
);
3685 void lockdep_set_current_reclaim_state(gfp_t gfp_mask
)
3687 current
->lockdep_reclaim_gfp
= gfp_mask
;
3690 void lockdep_clear_current_reclaim_state(void)
3692 current
->lockdep_reclaim_gfp
= 0;
3695 #ifdef CONFIG_LOCK_STAT
3697 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
3700 if (!debug_locks_off())
3702 if (debug_locks_silent
)
3709 printk("=================================\n");
3710 printk("[ ProveLock BUG: bad contention detected! ]\n");
3711 print_kernel_ident();
3712 printk("---------------------------------\n");
3713 printk("%s/%d is trying to contend lock (",
3714 curr
->comm
, task_pid_nr(curr
));
3715 print_lockdep_cache(lock
);
3718 printk("but there are no locks held!\n");
3719 printk("\nother info that might help us debug this:\n");
3720 lockdep_print_held_locks(curr
);
3722 printk("\nstack backtrace:\n");
3729 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3731 struct task_struct
*curr
= current
;
3732 struct held_lock
*hlock
, *prev_hlock
;
3733 struct lock_class_stats
*stats
;
3735 int i
, contention_point
, contending_point
;
3737 depth
= curr
->lockdep_depth
;
3739 * Whee, we contended on this lock, except it seems we're not
3740 * actually trying to acquire anything much at all..
3742 if (DEBUG_LOCKS_WARN_ON(!depth
))
3746 for (i
= depth
-1; i
>= 0; i
--) {
3747 hlock
= curr
->held_locks
+ i
;
3749 * We must not cross into another context:
3751 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3753 if (match_held_lock(hlock
, lock
))
3757 print_lock_contention_bug(curr
, lock
, ip
);
3761 if (hlock
->instance
!= lock
)
3764 hlock
->waittime_stamp
= lockstat_clock();
3766 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
3767 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
3770 stats
= get_lock_stats(hlock_class(hlock
));
3771 if (contention_point
< LOCKSTAT_POINTS
)
3772 stats
->contention_point
[contention_point
]++;
3773 if (contending_point
< LOCKSTAT_POINTS
)
3774 stats
->contending_point
[contending_point
]++;
3775 if (lock
->cpu
!= smp_processor_id())
3776 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
3777 put_lock_stats(stats
);
3781 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3783 struct task_struct
*curr
= current
;
3784 struct held_lock
*hlock
, *prev_hlock
;
3785 struct lock_class_stats
*stats
;
3787 u64 now
, waittime
= 0;
3790 depth
= curr
->lockdep_depth
;
3792 * Yay, we acquired ownership of this lock we didn't try to
3793 * acquire, how the heck did that happen?
3795 if (DEBUG_LOCKS_WARN_ON(!depth
))
3799 for (i
= depth
-1; i
>= 0; i
--) {
3800 hlock
= curr
->held_locks
+ i
;
3802 * We must not cross into another context:
3804 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
3806 if (match_held_lock(hlock
, lock
))
3810 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
3814 if (hlock
->instance
!= lock
)
3817 cpu
= smp_processor_id();
3818 if (hlock
->waittime_stamp
) {
3819 now
= lockstat_clock();
3820 waittime
= now
- hlock
->waittime_stamp
;
3821 hlock
->holdtime_stamp
= now
;
3824 trace_lock_acquired(lock
, ip
);
3826 stats
= get_lock_stats(hlock_class(hlock
));
3829 lock_time_inc(&stats
->read_waittime
, waittime
);
3831 lock_time_inc(&stats
->write_waittime
, waittime
);
3833 if (lock
->cpu
!= cpu
)
3834 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
3835 put_lock_stats(stats
);
3841 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
3843 unsigned long flags
;
3845 if (unlikely(!lock_stat
))
3848 if (unlikely(current
->lockdep_recursion
))
3851 raw_local_irq_save(flags
);
3853 current
->lockdep_recursion
= 1;
3854 trace_lock_contended(lock
, ip
);
3855 __lock_contended(lock
, ip
);
3856 current
->lockdep_recursion
= 0;
3857 raw_local_irq_restore(flags
);
3859 EXPORT_SYMBOL_GPL(lock_contended
);
3861 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
3863 unsigned long flags
;
3865 if (unlikely(!lock_stat
))
3868 if (unlikely(current
->lockdep_recursion
))
3871 raw_local_irq_save(flags
);
3873 current
->lockdep_recursion
= 1;
3874 __lock_acquired(lock
, ip
);
3875 current
->lockdep_recursion
= 0;
3876 raw_local_irq_restore(flags
);
3878 EXPORT_SYMBOL_GPL(lock_acquired
);
3882 * Used by the testsuite, sanitize the validator state
3883 * after a simulated failure:
3886 void lockdep_reset(void)
3888 unsigned long flags
;
3891 raw_local_irq_save(flags
);
3892 current
->curr_chain_key
= 0;
3893 current
->lockdep_depth
= 0;
3894 current
->lockdep_recursion
= 0;
3895 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
3896 nr_hardirq_chains
= 0;
3897 nr_softirq_chains
= 0;
3898 nr_process_chains
= 0;
3900 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3901 INIT_LIST_HEAD(chainhash_table
+ i
);
3902 raw_local_irq_restore(flags
);
3905 static void zap_class(struct lock_class
*class)
3910 * Remove all dependencies this lock is
3913 for (i
= 0; i
< nr_list_entries
; i
++) {
3914 if (list_entries
[i
].class == class)
3915 list_del_rcu(&list_entries
[i
].entry
);
3918 * Unhash the class and remove it from the all_lock_classes list:
3920 list_del_rcu(&class->hash_entry
);
3921 list_del_rcu(&class->lock_entry
);
3926 static inline int within(const void *addr
, void *start
, unsigned long size
)
3928 return addr
>= start
&& addr
< start
+ size
;
3931 void lockdep_free_key_range(void *start
, unsigned long size
)
3933 struct lock_class
*class, *next
;
3934 struct list_head
*head
;
3935 unsigned long flags
;
3939 raw_local_irq_save(flags
);
3940 locked
= graph_lock();
3943 * Unhash all classes that were created by this module:
3945 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3946 head
= classhash_table
+ i
;
3947 if (list_empty(head
))
3949 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3950 if (within(class->key
, start
, size
))
3952 else if (within(class->name
, start
, size
))
3959 raw_local_irq_restore(flags
);
3962 void lockdep_reset_lock(struct lockdep_map
*lock
)
3964 struct lock_class
*class, *next
;
3965 struct list_head
*head
;
3966 unsigned long flags
;
3970 raw_local_irq_save(flags
);
3973 * Remove all classes this lock might have:
3975 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
3977 * If the class exists we look it up and zap it:
3979 class = look_up_lock_class(lock
, j
);
3984 * Debug check: in the end all mapped classes should
3987 locked
= graph_lock();
3988 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
3989 head
= classhash_table
+ i
;
3990 if (list_empty(head
))
3992 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
3995 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
3996 match
|= class == lock
->class_cache
[j
];
3998 if (unlikely(match
)) {
3999 if (debug_locks_off_graph_unlock()) {
4001 * We all just reset everything, how did it match?
4013 raw_local_irq_restore(flags
);
4016 void lockdep_init(void)
4021 * Some architectures have their own start_kernel()
4022 * code which calls lockdep_init(), while we also
4023 * call lockdep_init() from the start_kernel() itself,
4024 * and we want to initialize the hashes only once:
4026 if (lockdep_initialized
)
4029 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
4030 INIT_LIST_HEAD(classhash_table
+ i
);
4032 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
4033 INIT_LIST_HEAD(chainhash_table
+ i
);
4035 lockdep_initialized
= 1;
4038 void __init
lockdep_info(void)
4040 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4042 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
4043 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
4044 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
4045 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
4046 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
4047 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
4048 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
4050 printk(" memory used by lock dependency info: %lu kB\n",
4051 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
4052 sizeof(struct list_head
) * CLASSHASH_SIZE
+
4053 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
4054 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
4055 sizeof(struct list_head
) * CHAINHASH_SIZE
4056 #ifdef CONFIG_PROVE_LOCKING
4057 + sizeof(struct circular_queue
)
4062 printk(" per task-struct memory footprint: %lu bytes\n",
4063 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
4065 #ifdef CONFIG_DEBUG_LOCKDEP
4066 if (lockdep_init_error
) {
4067 printk("WARNING: lockdep init error! lock-%s was acquired"
4068 "before lockdep_init\n", lock_init_error
);
4069 printk("Call stack leading to lockdep invocation was:\n");
4070 print_stack_trace(&lockdep_init_trace
, 0);
4076 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
4077 const void *mem_to
, struct held_lock
*hlock
)
4079 if (!debug_locks_off())
4081 if (debug_locks_silent
)
4088 printk("=========================\n");
4089 printk("[ ProveLock BUG: held lock freed! ]\n");
4090 print_kernel_ident();
4091 printk("-------------------------\n");
4092 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4093 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
4095 lockdep_print_held_locks(curr
);
4097 printk("\nstack backtrace:\n");
4101 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
4102 const void* lock_from
, unsigned long lock_len
)
4104 return lock_from
+ lock_len
<= mem_from
||
4105 mem_from
+ mem_len
<= lock_from
;
4109 * Called when kernel memory is freed (or unmapped), or if a lock
4110 * is destroyed or reinitialized - this code checks whether there is
4111 * any held lock in the memory range of <from> to <to>:
4113 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
4115 struct task_struct
*curr
= current
;
4116 struct held_lock
*hlock
;
4117 unsigned long flags
;
4120 if (unlikely(!debug_locks
))
4123 local_irq_save(flags
);
4124 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4125 hlock
= curr
->held_locks
+ i
;
4127 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
4128 sizeof(*hlock
->instance
)))
4131 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
4134 local_irq_restore(flags
);
4136 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
4138 static void print_held_locks_bug(void)
4140 if (!debug_locks_off())
4142 if (debug_locks_silent
)
4144 printk("[ ProveLock BUG: %s/%d still has locks held! ]\n",
4145 current
->comm
, task_pid_nr(current
));
4152 printk("=====================================\n");
4153 printk("[ ProveLock BUG: %s/%d still has locks held! ]\n",
4154 current
->comm
, task_pid_nr(current
));
4155 print_kernel_ident();
4156 printk("-------------------------------------\n");
4157 lockdep_print_held_locks(current
);
4158 printk("\nstack backtrace:\n");
4162 void debug_check_no_locks_held(void)
4164 if (unlikely(current
->lockdep_depth
> 0))
4165 print_held_locks_bug();
4167 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
4169 void debug_show_all_locks(void)
4171 struct task_struct
*g
, *p
;
4175 if (unlikely(!debug_locks
)) {
4176 printk("INFO: lockdep is turned off.\n");
4179 printk("\nShowing all locks held in the system:\n");
4182 * Here we try to get the tasklist_lock as hard as possible,
4183 * if not successful after 2 seconds we ignore it (but keep
4184 * trying). This is to enable a debug printout even if a
4185 * tasklist_lock-holding task deadlocks or crashes.
4188 if (!read_trylock(&tasklist_lock
)) {
4190 printk("hm, tasklist_lock locked, retrying... ");
4193 printk(" #%d", 10-count
);
4197 printk(" ignoring it.\n");
4201 printk(KERN_CONT
" locked it.\n");
4204 do_each_thread(g
, p
) {
4206 * It's not reliable to print a task's held locks
4207 * if it's not sleeping (or if it's not the current
4210 if (p
->state
== TASK_RUNNING
&& p
!= current
){
4211 printk("[Caution!] %s/%d is running now\n", p
->comm
, p
->pid
);
4214 if (p
->lockdep_depth
)
4215 lockdep_print_held_locks(p
);
4217 if (read_trylock(&tasklist_lock
))
4219 } while_each_thread(g
, p
);
4222 printk("=============================================\n\n");
4225 read_unlock(&tasklist_lock
);
4227 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
4230 * Careful: only use this function if you are sure that
4231 * the task cannot run in parallel!
4233 void debug_show_held_locks(struct task_struct
*task
)
4235 if (unlikely(!debug_locks
)) {
4236 printk("INFO: lockdep is turned off.\n");
4239 lockdep_print_held_locks(task
);
4241 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
4243 void lockdep_sys_exit(void)
4245 struct task_struct
*curr
= current
;
4247 if (unlikely(curr
->lockdep_depth
)) {
4248 if (!debug_locks_off())
4251 printk("================================================\n");
4252 printk("[ BUG: lock held when returning to user space! ]\n");
4253 print_kernel_ident();
4254 printk("------------------------------------------------\n");
4255 printk("%s/%d is leaving the kernel with locks still held!\n",
4256 curr
->comm
, curr
->pid
);
4257 lockdep_print_held_locks(curr
);
4261 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
4263 struct task_struct
*curr
= current
;
4265 #ifndef CONFIG_PROVE_RCU_REPEATEDLY
4266 if (!debug_locks_off())
4269 #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4270 /* Note: the following can be executed concurrently, so be careful. */
4275 printk("===============================\n");
4276 printk("[ ProveLock INFO: suspicious RCU usage. ]\n");
4277 print_kernel_ident();
4278 printk("-------------------------------\n");
4279 printk("%s:%d %s!\n", file
, line
, s
);
4280 printk("\nother info that might help us debug this:\n\n");
4281 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4282 !rcu_lockdep_current_cpu_online()
4283 ? "RCU used illegally from offline CPU!\n"
4285 ? "RCU used illegally from idle CPU!\n"
4287 rcu_scheduler_active
, debug_locks
);
4290 * If a CPU is in the RCU-free window in idle (ie: in the section
4291 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4292 * considers that CPU to be in an "extended quiescent state",
4293 * which means that RCU will be completely ignoring that CPU.
4294 * Therefore, rcu_read_lock() and friends have absolutely no
4295 * effect on a CPU running in that state. In other words, even if
4296 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4297 * delete data structures out from under it. RCU really has no
4298 * choice here: we need to keep an RCU-free window in idle where
4299 * the CPU may possibly enter into low power mode. This way we can
4300 * notice an extended quiescent state to other CPUs that started a grace
4301 * period. Otherwise we would delay any grace period as long as we run
4304 * So complain bitterly if someone does call rcu_read_lock(),
4305 * rcu_read_lock_bh() and so on from extended quiescent states.
4307 if (rcu_is_cpu_idle())
4308 printk("RCU used illegally from extended quiescent state!\n");
4310 lockdep_print_held_locks(curr
);
4311 printk("\nstack backtrace:\n");
4314 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);