4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
28 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/spinlock.h>
36 #include <linux/kallsyms.h>
37 #include <linux/interrupt.h>
38 #include <linux/stacktrace.h>
39 #include <linux/debug_locks.h>
40 #include <linux/irqflags.h>
41 #include <linux/utsname.h>
42 #include <linux/hash.h>
43 #include <linux/ftrace.h>
44 #include <linux/stringify.h>
45 #include <linux/bitops.h>
46 #include <linux/gfp.h>
47 #include <linux/kmemcheck.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);
71 * lockdep_lock: protects the lockdep graph, the hashes and the
72 * class/list/hash allocators.
74 * This is one of the rare exceptions where it's justified
75 * to use a raw spinlock - we really dont want the spinlock
76 * code to recurse back into the lockdep code...
78 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
80 static int graph_lock(void)
82 arch_spin_lock(&lockdep_lock);
84 * Make sure that if another CPU detected a bug while
85 * walking the graph we dont change it (while the other
86 * CPU is busy printing out stuff with the graph lock
90 arch_spin_unlock(&lockdep_lock);
93 /* prevent any recursions within lockdep from causing deadlocks */
94 current->lockdep_recursion++;
98 static inline int graph_unlock(void)
100 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
102 * The lockdep graph lock isn't locked while we expect it to
103 * be, we're confused now, bye!
105 return DEBUG_LOCKS_WARN_ON(1);
108 current->lockdep_recursion--;
109 arch_spin_unlock(&lockdep_lock);
114 * Turn lock debugging off and return with 0 if it was off already,
115 * and also release the graph lock:
117 static inline int debug_locks_off_graph_unlock(void)
119 int ret = debug_locks_off();
121 arch_spin_unlock(&lockdep_lock);
126 static int lockdep_initialized;
128 unsigned long nr_list_entries;
129 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
132 * All data structures here are protected by the global debug_lock.
134 * Mutex key structs only get allocated, once during bootup, and never
135 * get freed - this significantly simplifies the debugging code.
137 unsigned long nr_lock_classes;
138 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
140 static inline struct lock_class *hlock_class(struct held_lock *hlock)
142 if (!hlock->class_idx) {
144 * Someone passed in garbage, we give up.
146 DEBUG_LOCKS_WARN_ON(1);
149 return lock_classes + hlock->class_idx - 1;
152 #ifdef CONFIG_LOCK_STAT
153 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
156 static inline u64 lockstat_clock(void)
158 return local_clock();
161 static int lock_point(unsigned long points[], unsigned long ip)
165 for (i = 0; i < LOCKSTAT_POINTS; i++) {
166 if (points[i] == 0) {
177 static void lock_time_inc(struct lock_time *lt, u64 time)
182 if (time < lt->min || !lt->nr)
189 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
194 if (src->max > dst->max)
197 if (src->min < dst->min || !dst->nr)
200 dst->total += src->total;
204 struct lock_class_stats lock_stats(struct lock_class *class)
206 struct lock_class_stats stats;
209 memset(&stats, 0, sizeof(struct lock_class_stats));
210 for_each_possible_cpu(cpu) {
211 struct lock_class_stats *pcs =
212 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
214 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
215 stats.contention_point[i] += pcs->contention_point[i];
217 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
218 stats.contending_point[i] += pcs->contending_point[i];
220 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
221 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
223 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
224 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
226 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
227 stats.bounces[i] += pcs->bounces[i];
233 void clear_lock_stats(struct lock_class *class)
237 for_each_possible_cpu(cpu) {
238 struct lock_class_stats *cpu_stats =
239 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
241 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
243 memset(class->contention_point, 0, sizeof(class->contention_point));
244 memset(class->contending_point, 0, sizeof(class->contending_point));
247 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
249 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
252 static void put_lock_stats(struct lock_class_stats *stats)
254 put_cpu_var(cpu_lock_stats);
257 static void lock_release_holdtime(struct held_lock *hlock)
259 struct lock_class_stats *stats;
265 holdtime = lockstat_clock() - hlock->holdtime_stamp;
267 stats = get_lock_stats(hlock_class(hlock));
269 lock_time_inc(&stats->read_holdtime, holdtime);
271 lock_time_inc(&stats->write_holdtime, holdtime);
272 put_lock_stats(stats);
275 static inline void lock_release_holdtime(struct held_lock *hlock)
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
285 LIST_HEAD(all_lock_classes);
288 * The lockdep classes are in a hash-table as well, for fast lookup:
290 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
292 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
293 #define classhashentry(key) (classhash_table + __classhashfn((key)))
295 static struct list_head classhash_table[CLASSHASH_SIZE];
298 * We put the lock dependency chains into a hash-table as well, to cache
301 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
303 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
304 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
306 static struct list_head chainhash_table[CHAINHASH_SIZE];
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
314 #define iterate_chain_key(key1, key2) \
315 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
316 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
319 void lockdep_off(void)
321 current->lockdep_recursion++;
323 EXPORT_SYMBOL(lockdep_off);
325 void lockdep_on(void)
327 current->lockdep_recursion--;
329 EXPORT_SYMBOL(lockdep_on);
332 * Debugging switches:
336 #define VERY_VERBOSE 0
339 # define HARDIRQ_VERBOSE 1
340 # define SOFTIRQ_VERBOSE 1
341 # define RECLAIM_VERBOSE 1
343 # define HARDIRQ_VERBOSE 0
344 # define SOFTIRQ_VERBOSE 0
345 # define RECLAIM_VERBOSE 0
348 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
350 * Quick filtering for interesting events:
352 static int class_filter(struct lock_class *class)
356 if (class->name_version == 1 &&
357 !strcmp(class->name, "lockname"))
359 if (class->name_version == 1 &&
360 !strcmp(class->name, "&struct->lockfield"))
363 /* Filter everything else. 1 would be to allow everything else */
368 static int verbose(struct lock_class *class)
371 return class_filter(class);
377 * Stack-trace: tightly packed array of stack backtrace
378 * addresses. Protected by the graph_lock.
380 unsigned long nr_stack_trace_entries;
381 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
383 static void print_lockdep_off(const char *bug_msg)
385 printk(KERN_DEBUG "%s\n", bug_msg);
386 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
387 #ifdef CONFIG_LOCK_STAT
388 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
392 static int save_trace(struct stack_trace *trace)
394 trace->nr_entries = 0;
395 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
396 trace->entries = stack_trace + nr_stack_trace_entries;
400 save_stack_trace(trace);
403 * Some daft arches put -1 at the end to indicate its a full trace.
405 * <rant> this is buggy anyway, since it takes a whole extra entry so a
406 * complete trace that maxes out the entries provided will be reported
407 * as incomplete, friggin useless </rant>
409 if (trace->nr_entries != 0 &&
410 trace->entries[trace->nr_entries-1] == ULONG_MAX)
413 trace->max_entries = trace->nr_entries;
415 nr_stack_trace_entries += trace->nr_entries;
417 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
418 if (!debug_locks_off_graph_unlock())
421 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
430 unsigned int nr_hardirq_chains;
431 unsigned int nr_softirq_chains;
432 unsigned int nr_process_chains;
433 unsigned int max_lockdep_depth;
435 #ifdef CONFIG_DEBUG_LOCKDEP
437 * We cannot printk in early bootup code. Not even early_printk()
438 * might work. So we mark any initialization errors and printk
439 * about it later on, in lockdep_info().
441 static int lockdep_init_error;
442 static const char *lock_init_error;
443 static unsigned long lockdep_init_trace_data[20];
444 static struct stack_trace lockdep_init_trace = {
445 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
446 .entries = lockdep_init_trace_data,
450 * Various lockdep statistics:
452 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
459 #define __USAGE(__STATE) \
460 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
461 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
462 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
463 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
465 static const char *usage_str[] =
467 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
468 #include "lockdep_states.h"
470 [LOCK_USED] = "INITIAL USE",
473 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
475 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
478 static inline unsigned long lock_flag(enum lock_usage_bit bit)
483 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
487 if (class->usage_mask & lock_flag(bit + 2))
489 if (class->usage_mask & lock_flag(bit)) {
491 if (class->usage_mask & lock_flag(bit + 2))
498 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
502 #define LOCKDEP_STATE(__STATE) \
503 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
504 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
505 #include "lockdep_states.h"
511 static void __print_lock_name(struct lock_class *class)
513 char str[KSYM_NAME_LEN];
518 name = __get_key_name(class->key, str);
522 if (class->name_version > 1)
523 printk("#%d", class->name_version);
525 printk("/%d", class->subclass);
529 static void print_lock_name(struct lock_class *class)
531 char usage[LOCK_USAGE_CHARS];
533 get_usage_chars(class, usage);
536 __print_lock_name(class);
537 printk("){%s}", usage);
540 static void print_lockdep_cache(struct lockdep_map *lock)
543 char str[KSYM_NAME_LEN];
547 name = __get_key_name(lock->key->subkeys, str);
552 static void print_lock(struct held_lock *hlock)
555 * We can be called locklessly through debug_show_all_locks() so be
556 * extra careful, the hlock might have been released and cleared.
558 unsigned int class_idx = hlock->class_idx;
560 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
563 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
564 printk("<RELEASED>\n");
568 print_lock_name(lock_classes + class_idx - 1);
570 print_ip_sym(hlock->acquire_ip);
573 static void lockdep_print_held_locks(struct task_struct *curr)
575 int i, depth = curr->lockdep_depth;
578 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
581 printk("%d lock%s held by %s/%d:\n",
582 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
584 for (i = 0; i < depth; i++) {
586 print_lock(curr->held_locks + i);
590 static void print_kernel_ident(void)
592 printk("%s %.*s %s\n", init_utsname()->release,
593 (int)strcspn(init_utsname()->version, " "),
594 init_utsname()->version,
598 static int very_verbose(struct lock_class *class)
601 return class_filter(class);
607 * Is this the address of a static object:
610 static int static_obj(void *obj)
612 unsigned long start = (unsigned long) &_stext,
613 end = (unsigned long) &_end,
614 addr = (unsigned long) obj;
619 if ((addr >= start) && (addr < end))
622 if (arch_is_kernel_data(addr))
626 * in-kernel percpu var?
628 if (is_kernel_percpu_address(addr))
632 * module static or percpu var?
634 return is_module_address(addr) || is_module_percpu_address(addr);
639 * To make lock name printouts unique, we calculate a unique
640 * class->name_version generation counter:
642 static int count_matching_names(struct lock_class *new_class)
644 struct lock_class *class;
647 if (!new_class->name)
650 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
651 if (new_class->key - new_class->subclass == class->key)
652 return class->name_version;
653 if (class->name && !strcmp(class->name, new_class->name))
654 count = max(count, class->name_version);
661 * Register a lock's class in the hash-table, if the class is not present
662 * yet. Otherwise we look it up. We cache the result in the lock object
663 * itself, so actual lookup of the hash should be once per lock object.
665 static inline struct lock_class *
666 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
668 struct lockdep_subclass_key *key;
669 struct list_head *hash_head;
670 struct lock_class *class;
672 #ifdef CONFIG_DEBUG_LOCKDEP
674 * If the architecture calls into lockdep before initializing
675 * the hashes then we'll warn about it later. (we cannot printk
678 if (unlikely(!lockdep_initialized)) {
680 lockdep_init_error = 1;
681 lock_init_error = lock->name;
682 save_stack_trace(&lockdep_init_trace);
686 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
689 "BUG: looking up invalid subclass: %u\n", subclass);
691 "turning off the locking correctness validator.\n");
697 * Static locks do not have their class-keys yet - for them the key
698 * is the lock object itself:
700 if (unlikely(!lock->key))
701 lock->key = (void *)lock;
704 * NOTE: the class-key must be unique. For dynamic locks, a static
705 * lock_class_key variable is passed in through the mutex_init()
706 * (or spin_lock_init()) call - which acts as the key. For static
707 * locks we use the lock object itself as the key.
709 BUILD_BUG_ON(sizeof(struct lock_class_key) >
710 sizeof(struct lockdep_map));
712 key = lock->key->subkeys + subclass;
714 hash_head = classhashentry(key);
717 * We do an RCU walk of the hash, see lockdep_free_key_range().
719 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
722 list_for_each_entry_rcu(class, hash_head, hash_entry) {
723 if (class->key == key) {
725 * Huh! same key, different name? Did someone trample
726 * on some memory? We're most confused.
728 WARN_ON_ONCE(class->name != lock->name);
737 * Register a lock's class in the hash-table, if the class is not present
738 * yet. Otherwise we look it up. We cache the result in the lock object
739 * itself, so actual lookup of the hash should be once per lock object.
741 static inline struct lock_class *
742 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
744 struct lockdep_subclass_key *key;
745 struct list_head *hash_head;
746 struct lock_class *class;
748 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
750 class = look_up_lock_class(lock, subclass);
752 goto out_set_class_cache;
755 * Debug-check: all keys must be persistent!
757 if (!static_obj(lock->key)) {
759 printk("INFO: trying to register non-static key.\n");
760 printk("the code is fine but needs lockdep annotation.\n");
761 printk("turning off the locking correctness validator.\n");
767 key = lock->key->subkeys + subclass;
768 hash_head = classhashentry(key);
774 * We have to do the hash-walk again, to avoid races
777 list_for_each_entry_rcu(class, hash_head, hash_entry) {
778 if (class->key == key)
783 * Allocate a new key from the static array, and add it to
786 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
787 if (!debug_locks_off_graph_unlock()) {
791 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
795 class = lock_classes + nr_lock_classes++;
796 debug_atomic_inc(nr_unused_locks);
798 class->name = lock->name;
799 class->subclass = subclass;
800 INIT_LIST_HEAD(&class->lock_entry);
801 INIT_LIST_HEAD(&class->locks_before);
802 INIT_LIST_HEAD(&class->locks_after);
803 class->name_version = count_matching_names(class);
805 * We use RCU's safe list-add method to make
806 * parallel walking of the hash-list safe:
808 list_add_tail_rcu(&class->hash_entry, hash_head);
810 * Add it to the global list of classes:
812 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
814 if (verbose(class)) {
817 printk("\nnew class %p: %s", class->key, class->name);
818 if (class->name_version > 1)
819 printk("#%d", class->name_version);
831 if (!subclass || force)
832 lock->class_cache[0] = class;
833 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
834 lock->class_cache[subclass] = class;
837 * Hash collision, did we smoke some? We found a class with a matching
838 * hash but the subclass -- which is hashed in -- didn't match.
840 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
846 #ifdef CONFIG_PROVE_LOCKING
848 * Allocate a lockdep entry. (assumes the graph_lock held, returns
849 * with NULL on failure)
851 static struct lock_list *alloc_list_entry(void)
853 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
854 if (!debug_locks_off_graph_unlock())
857 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
861 return list_entries + nr_list_entries++;
865 * Add a new dependency to the head of the list:
867 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
868 struct list_head *head, unsigned long ip,
869 int distance, struct stack_trace *trace)
871 struct lock_list *entry;
873 * Lock not present yet - get a new dependency struct and
874 * add it to the list:
876 entry = alloc_list_entry();
881 entry->distance = distance;
882 entry->trace = *trace;
884 * Both allocation and removal are done under the graph lock; but
885 * iteration is under RCU-sched; see look_up_lock_class() and
886 * lockdep_free_key_range().
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 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1039 list_for_each_entry_rcu(entry, head, entry) {
1040 if (!lock_accessed(entry)) {
1041 unsigned int cq_depth;
1042 mark_lock_accessed(entry, lock);
1043 if (match(entry, data)) {
1044 *target_entry = entry;
1049 if (__cq_enqueue(cq, (unsigned long)entry)) {
1053 cq_depth = __cq_get_elem_count(cq);
1054 if (max_bfs_queue_depth < cq_depth)
1055 max_bfs_queue_depth = cq_depth;
1063 static inline int __bfs_forwards(struct lock_list *src_entry,
1065 int (*match)(struct lock_list *entry, void *data),
1066 struct lock_list **target_entry)
1068 return __bfs(src_entry, data, match, target_entry, 1);
1072 static inline int __bfs_backwards(struct lock_list *src_entry,
1074 int (*match)(struct lock_list *entry, void *data),
1075 struct lock_list **target_entry)
1077 return __bfs(src_entry, data, match, target_entry, 0);
1082 * Recursive, forwards-direction lock-dependency checking, used for
1083 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1088 * Print a dependency chain entry (this is only done when a deadlock
1089 * has been detected):
1092 print_circular_bug_entry(struct lock_list *target, int depth)
1094 if (debug_locks_silent)
1096 printk("\n-> #%u", depth);
1097 print_lock_name(target->class);
1099 print_stack_trace(&target->trace, 6);
1105 print_circular_lock_scenario(struct held_lock *src,
1106 struct held_lock *tgt,
1107 struct lock_list *prt)
1109 struct lock_class *source = hlock_class(src);
1110 struct lock_class *target = hlock_class(tgt);
1111 struct lock_class *parent = prt->class;
1114 * A direct locking problem where unsafe_class lock is taken
1115 * directly by safe_class lock, then all we need to show
1116 * is the deadlock scenario, as it is obvious that the
1117 * unsafe lock is taken under the safe lock.
1119 * But if there is a chain instead, where the safe lock takes
1120 * an intermediate lock (middle_class) where this lock is
1121 * not the same as the safe lock, then the lock chain is
1122 * used to describe the problem. Otherwise we would need
1123 * to show a different CPU case for each link in the chain
1124 * from the safe_class lock to the unsafe_class lock.
1126 if (parent != source) {
1127 printk("Chain exists of:\n ");
1128 __print_lock_name(source);
1130 __print_lock_name(parent);
1132 __print_lock_name(target);
1136 printk(" Possible unsafe locking scenario:\n\n");
1137 printk(" CPU0 CPU1\n");
1138 printk(" ---- ----\n");
1140 __print_lock_name(target);
1143 __print_lock_name(parent);
1146 __print_lock_name(target);
1149 __print_lock_name(source);
1151 printk("\n *** DEADLOCK ***\n\n");
1155 * When a circular dependency is detected, print the
1159 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1160 struct held_lock *check_src,
1161 struct held_lock *check_tgt)
1163 struct task_struct *curr = current;
1165 if (debug_locks_silent)
1169 printk("======================================================\n");
1170 printk("[ 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 static 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 raw_local_irq_save(flags);
1268 arch_spin_lock(&lockdep_lock);
1269 ret = __lockdep_count_forward_deps(&this);
1270 arch_spin_unlock(&lockdep_lock);
1271 raw_local_irq_restore(flags);
1276 static 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 raw_local_irq_save(flags);
1295 arch_spin_lock(&lockdep_lock);
1296 ret = __lockdep_count_backward_deps(&this);
1297 arch_spin_unlock(&lockdep_lock);
1298 raw_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)
1507 printk("======================================================\n");
1508 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1509 irqclass, irqclass);
1510 print_kernel_ident();
1511 printk("------------------------------------------------------\n");
1512 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1513 curr->comm, task_pid_nr(curr),
1514 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1515 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1516 curr->hardirqs_enabled,
1517 curr->softirqs_enabled);
1520 printk("\nand this task is already holding:\n");
1522 printk("which would create a new lock dependency:\n");
1523 print_lock_name(hlock_class(prev));
1525 print_lock_name(hlock_class(next));
1528 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1530 print_lock_name(backwards_entry->class);
1531 printk("\n... which became %s-irq-safe at:\n", irqclass);
1533 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1535 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1536 print_lock_name(forwards_entry->class);
1537 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1540 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1542 printk("\nother info that might help us debug this:\n\n");
1543 print_irq_lock_scenario(backwards_entry, forwards_entry,
1544 hlock_class(prev), hlock_class(next));
1546 lockdep_print_held_locks(curr);
1548 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1549 printk(" and the holding lock:\n");
1550 if (!save_trace(&prev_root->trace))
1552 print_shortest_lock_dependencies(backwards_entry, prev_root);
1554 printk("\nthe dependencies between the lock to be acquired");
1555 printk(" and %s-irq-unsafe lock:\n", irqclass);
1556 if (!save_trace(&next_root->trace))
1558 print_shortest_lock_dependencies(forwards_entry, next_root);
1560 printk("\nstack backtrace:\n");
1567 check_usage(struct task_struct *curr, struct held_lock *prev,
1568 struct held_lock *next, enum lock_usage_bit bit_backwards,
1569 enum lock_usage_bit bit_forwards, const char *irqclass)
1572 struct lock_list this, that;
1573 struct lock_list *uninitialized_var(target_entry);
1574 struct lock_list *uninitialized_var(target_entry1);
1578 this.class = hlock_class(prev);
1579 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1581 return print_bfs_bug(ret);
1586 that.class = hlock_class(next);
1587 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1589 return print_bfs_bug(ret);
1593 return print_bad_irq_dependency(curr, &this, &that,
1594 target_entry, target_entry1,
1596 bit_backwards, bit_forwards, irqclass);
1599 static const char *state_names[] = {
1600 #define LOCKDEP_STATE(__STATE) \
1601 __stringify(__STATE),
1602 #include "lockdep_states.h"
1603 #undef LOCKDEP_STATE
1606 static const char *state_rnames[] = {
1607 #define LOCKDEP_STATE(__STATE) \
1608 __stringify(__STATE)"-READ",
1609 #include "lockdep_states.h"
1610 #undef LOCKDEP_STATE
1613 static inline const char *state_name(enum lock_usage_bit bit)
1615 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1618 static int exclusive_bit(int new_bit)
1626 * bit 0 - write/read
1627 * bit 1 - used_in/enabled
1631 int state = new_bit & ~3;
1632 int dir = new_bit & 2;
1635 * keep state, bit flip the direction and strip read.
1637 return state | (dir ^ 2);
1640 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1641 struct held_lock *next, enum lock_usage_bit bit)
1644 * Prove that the new dependency does not connect a hardirq-safe
1645 * lock with a hardirq-unsafe lock - to achieve this we search
1646 * the backwards-subgraph starting at <prev>, and the
1647 * forwards-subgraph starting at <next>:
1649 if (!check_usage(curr, prev, next, bit,
1650 exclusive_bit(bit), state_name(bit)))
1656 * Prove that the new dependency does not connect a hardirq-safe-read
1657 * lock with a hardirq-unsafe lock - to achieve this we search
1658 * the backwards-subgraph starting at <prev>, and the
1659 * forwards-subgraph starting at <next>:
1661 if (!check_usage(curr, prev, next, bit,
1662 exclusive_bit(bit), state_name(bit)))
1669 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1670 struct held_lock *next)
1672 #define LOCKDEP_STATE(__STATE) \
1673 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1675 #include "lockdep_states.h"
1676 #undef LOCKDEP_STATE
1681 static void inc_chains(void)
1683 if (current->hardirq_context)
1684 nr_hardirq_chains++;
1686 if (current->softirq_context)
1687 nr_softirq_chains++;
1689 nr_process_chains++;
1696 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1697 struct held_lock *next)
1702 static inline void inc_chains(void)
1704 nr_process_chains++;
1710 print_deadlock_scenario(struct held_lock *nxt,
1711 struct held_lock *prv)
1713 struct lock_class *next = hlock_class(nxt);
1714 struct lock_class *prev = hlock_class(prv);
1716 printk(" Possible unsafe locking scenario:\n\n");
1720 __print_lock_name(prev);
1723 __print_lock_name(next);
1725 printk("\n *** DEADLOCK ***\n\n");
1726 printk(" May be due to missing lock nesting notation\n\n");
1730 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1731 struct held_lock *next)
1733 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1737 printk("=============================================\n");
1738 printk("[ INFO: possible recursive locking detected ]\n");
1739 print_kernel_ident();
1740 printk("---------------------------------------------\n");
1741 printk("%s/%d is trying to acquire lock:\n",
1742 curr->comm, task_pid_nr(curr));
1744 printk("\nbut task is already holding lock:\n");
1747 printk("\nother info that might help us debug this:\n");
1748 print_deadlock_scenario(next, prev);
1749 lockdep_print_held_locks(curr);
1751 printk("\nstack backtrace:\n");
1758 * Check whether we are holding such a class already.
1760 * (Note that this has to be done separately, because the graph cannot
1761 * detect such classes of deadlocks.)
1763 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1766 check_deadlock(struct task_struct *curr, struct held_lock *next,
1767 struct lockdep_map *next_instance, int read)
1769 struct held_lock *prev;
1770 struct held_lock *nest = NULL;
1773 for (i = 0; i < curr->lockdep_depth; i++) {
1774 prev = curr->held_locks + i;
1776 if (prev->instance == next->nest_lock)
1779 if (hlock_class(prev) != hlock_class(next))
1783 * Allow read-after-read recursion of the same
1784 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1786 if ((read == 2) && prev->read)
1790 * We're holding the nest_lock, which serializes this lock's
1791 * nesting behaviour.
1796 return print_deadlock_bug(curr, prev, next);
1802 * There was a chain-cache miss, and we are about to add a new dependency
1803 * to a previous lock. We recursively validate the following rules:
1805 * - would the adding of the <prev> -> <next> dependency create a
1806 * circular dependency in the graph? [== circular deadlock]
1808 * - does the new prev->next dependency connect any hardirq-safe lock
1809 * (in the full backwards-subgraph starting at <prev>) with any
1810 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1811 * <next>)? [== illegal lock inversion with hardirq contexts]
1813 * - does the new prev->next dependency connect any softirq-safe lock
1814 * (in the full backwards-subgraph starting at <prev>) with any
1815 * softirq-unsafe lock (in the full forwards-subgraph starting at
1816 * <next>)? [== illegal lock inversion with softirq contexts]
1818 * any of these scenarios could lead to a deadlock.
1820 * Then if all the validations pass, we add the forwards and backwards
1824 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1825 struct held_lock *next, int distance, int trylock_loop)
1827 struct lock_list *entry;
1829 struct lock_list this;
1830 struct lock_list *uninitialized_var(target_entry);
1832 * Static variable, serialized by the graph_lock().
1834 * We use this static variable to save the stack trace in case
1835 * we call into this function multiple times due to encountering
1836 * trylocks in the held lock stack.
1838 static struct stack_trace trace;
1841 * Prove that the new <prev> -> <next> dependency would not
1842 * create a circular dependency in the graph. (We do this by
1843 * forward-recursing into the graph starting at <next>, and
1844 * checking whether we can reach <prev>.)
1846 * We are using global variables to control the recursion, to
1847 * keep the stackframe size of the recursive functions low:
1849 this.class = hlock_class(next);
1851 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1853 return print_circular_bug(&this, target_entry, next, prev);
1854 else if (unlikely(ret < 0))
1855 return print_bfs_bug(ret);
1857 if (!check_prev_add_irq(curr, prev, next))
1861 * For recursive read-locks we do all the dependency checks,
1862 * but we dont store read-triggered dependencies (only
1863 * write-triggered dependencies). This ensures that only the
1864 * write-side dependencies matter, and that if for example a
1865 * write-lock never takes any other locks, then the reads are
1866 * equivalent to a NOP.
1868 if (next->read == 2 || prev->read == 2)
1871 * Is the <prev> -> <next> dependency already present?
1873 * (this may occur even though this is a new chain: consider
1874 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1875 * chains - the second one will be new, but L1 already has
1876 * L2 added to its dependency list, due to the first chain.)
1878 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1879 if (entry->class == hlock_class(next)) {
1881 entry->distance = 1;
1886 if (!trylock_loop && !save_trace(&trace))
1890 * Ok, all validations passed, add the new lock
1891 * to the previous lock's dependency list:
1893 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1894 &hlock_class(prev)->locks_after,
1895 next->acquire_ip, distance, &trace);
1900 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1901 &hlock_class(next)->locks_before,
1902 next->acquire_ip, distance, &trace);
1907 * Debugging printouts:
1909 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1911 printk("\n new dependency: ");
1912 print_lock_name(hlock_class(prev));
1914 print_lock_name(hlock_class(next));
1917 return graph_lock();
1923 * Add the dependency to all directly-previous locks that are 'relevant'.
1924 * The ones that are relevant are (in increasing distance from curr):
1925 * all consecutive trylock entries and the final non-trylock entry - or
1926 * the end of this context's lock-chain - whichever comes first.
1929 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1931 int depth = curr->lockdep_depth;
1932 int trylock_loop = 0;
1933 struct held_lock *hlock;
1938 * Depth must not be zero for a non-head lock:
1943 * At least two relevant locks must exist for this
1946 if (curr->held_locks[depth].irq_context !=
1947 curr->held_locks[depth-1].irq_context)
1951 int distance = curr->lockdep_depth - depth + 1;
1952 hlock = curr->held_locks + depth - 1;
1954 * Only non-recursive-read entries get new dependencies
1957 if (hlock->read != 2 && hlock->check) {
1958 if (!check_prev_add(curr, hlock, next,
1959 distance, trylock_loop))
1962 * Stop after the first non-trylock entry,
1963 * as non-trylock entries have added their
1964 * own direct dependencies already, so this
1965 * lock is connected to them indirectly:
1967 if (!hlock->trylock)
1972 * End of lock-stack?
1977 * Stop the search if we cross into another context:
1979 if (curr->held_locks[depth].irq_context !=
1980 curr->held_locks[depth-1].irq_context)
1986 if (!debug_locks_off_graph_unlock())
1990 * Clearly we all shouldn't be here, but since we made it we
1991 * can reliable say we messed up our state. See the above two
1992 * gotos for reasons why we could possibly end up here.
1999 unsigned long nr_lock_chains;
2000 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2001 int nr_chain_hlocks;
2002 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2004 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2006 return lock_classes + chain_hlocks[chain->base + i];
2010 * Look up a dependency chain. If the key is not present yet then
2011 * add it and return 1 - in this case the new dependency chain is
2012 * validated. If the key is already hashed, return 0.
2013 * (On return with 1 graph_lock is held.)
2015 static inline int lookup_chain_cache(struct task_struct *curr,
2016 struct held_lock *hlock,
2019 struct lock_class *class = hlock_class(hlock);
2020 struct list_head *hash_head = chainhashentry(chain_key);
2021 struct lock_chain *chain;
2022 struct held_lock *hlock_curr;
2026 * We might need to take the graph lock, ensure we've got IRQs
2027 * disabled to make this an IRQ-safe lock.. for recursion reasons
2028 * lockdep won't complain about its own locking errors.
2030 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2033 * We can walk it lock-free, because entries only get added
2036 list_for_each_entry_rcu(chain, hash_head, entry) {
2037 if (chain->chain_key == chain_key) {
2039 debug_atomic_inc(chain_lookup_hits);
2040 if (very_verbose(class))
2041 printk("\nhash chain already cached, key: "
2042 "%016Lx tail class: [%p] %s\n",
2043 (unsigned long long)chain_key,
2044 class->key, class->name);
2048 if (very_verbose(class))
2049 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2050 (unsigned long long)chain_key, class->key, class->name);
2052 * Allocate a new chain entry from the static array, and add
2058 * We have to walk the chain again locked - to avoid duplicates:
2060 list_for_each_entry(chain, hash_head, entry) {
2061 if (chain->chain_key == chain_key) {
2066 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2067 if (!debug_locks_off_graph_unlock())
2070 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2074 chain = lock_chains + nr_lock_chains++;
2075 chain->chain_key = chain_key;
2076 chain->irq_context = hlock->irq_context;
2077 /* Find the first held_lock of current chain */
2078 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2079 hlock_curr = curr->held_locks + i;
2080 if (hlock_curr->irq_context != hlock->irq_context)
2084 chain->depth = curr->lockdep_depth + 1 - i;
2085 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2086 chain->base = nr_chain_hlocks;
2087 nr_chain_hlocks += chain->depth;
2088 for (j = 0; j < chain->depth - 1; j++, i++) {
2089 int lock_id = curr->held_locks[i].class_idx - 1;
2090 chain_hlocks[chain->base + j] = lock_id;
2092 chain_hlocks[chain->base + j] = class - lock_classes;
2094 list_add_tail_rcu(&chain->entry, hash_head);
2095 debug_atomic_inc(chain_lookup_misses);
2101 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2102 struct held_lock *hlock, int chain_head, u64 chain_key)
2105 * Trylock needs to maintain the stack of held locks, but it
2106 * does not add new dependencies, because trylock can be done
2109 * We look up the chain_key and do the O(N^2) check and update of
2110 * the dependencies only if this is a new dependency chain.
2111 * (If lookup_chain_cache() returns with 1 it acquires
2112 * graph_lock for us)
2114 if (!hlock->trylock && hlock->check &&
2115 lookup_chain_cache(curr, hlock, chain_key)) {
2117 * Check whether last held lock:
2119 * - is irq-safe, if this lock is irq-unsafe
2120 * - is softirq-safe, if this lock is hardirq-unsafe
2122 * And check whether the new lock's dependency graph
2123 * could lead back to the previous lock.
2125 * any of these scenarios could lead to a deadlock. If
2128 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2133 * Mark recursive read, as we jump over it when
2134 * building dependencies (just like we jump over
2140 * Add dependency only if this lock is not the head
2141 * of the chain, and if it's not a secondary read-lock:
2143 if (!chain_head && ret != 2)
2144 if (!check_prevs_add(curr, hlock))
2148 /* after lookup_chain_cache(): */
2149 if (unlikely(!debug_locks))
2155 static inline int validate_chain(struct task_struct *curr,
2156 struct lockdep_map *lock, struct held_lock *hlock,
2157 int chain_head, u64 chain_key)
2164 * We are building curr_chain_key incrementally, so double-check
2165 * it from scratch, to make sure that it's done correctly:
2167 static void check_chain_key(struct task_struct *curr)
2169 #ifdef CONFIG_DEBUG_LOCKDEP
2170 struct held_lock *hlock, *prev_hlock = NULL;
2174 for (i = 0; i < curr->lockdep_depth; i++) {
2175 hlock = curr->held_locks + i;
2176 if (chain_key != hlock->prev_chain_key) {
2179 * We got mighty confused, our chain keys don't match
2180 * with what we expect, someone trample on our task state?
2182 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2183 curr->lockdep_depth, i,
2184 (unsigned long long)chain_key,
2185 (unsigned long long)hlock->prev_chain_key);
2188 id = hlock->class_idx - 1;
2190 * Whoops ran out of static storage again?
2192 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2195 if (prev_hlock && (prev_hlock->irq_context !=
2196 hlock->irq_context))
2198 chain_key = iterate_chain_key(chain_key, id);
2201 if (chain_key != curr->curr_chain_key) {
2204 * More smoking hash instead of calculating it, damn see these
2205 * numbers float.. I bet that a pink elephant stepped on my memory.
2207 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2208 curr->lockdep_depth, i,
2209 (unsigned long long)chain_key,
2210 (unsigned long long)curr->curr_chain_key);
2216 print_usage_bug_scenario(struct held_lock *lock)
2218 struct lock_class *class = hlock_class(lock);
2220 printk(" Possible unsafe locking scenario:\n\n");
2224 __print_lock_name(class);
2226 printk(" <Interrupt>\n");
2228 __print_lock_name(class);
2230 printk("\n *** DEADLOCK ***\n\n");
2234 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2235 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2237 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2241 printk("=================================\n");
2242 printk("[ INFO: inconsistent lock state ]\n");
2243 print_kernel_ident();
2244 printk("---------------------------------\n");
2246 printk("inconsistent {%s} -> {%s} usage.\n",
2247 usage_str[prev_bit], usage_str[new_bit]);
2249 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2250 curr->comm, task_pid_nr(curr),
2251 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2252 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2253 trace_hardirqs_enabled(curr),
2254 trace_softirqs_enabled(curr));
2257 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
2258 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2260 print_irqtrace_events(curr);
2261 printk("\nother info that might help us debug this:\n");
2262 print_usage_bug_scenario(this);
2264 lockdep_print_held_locks(curr);
2266 printk("\nstack backtrace:\n");
2273 * Print out an error if an invalid bit is set:
2276 valid_state(struct task_struct *curr, struct held_lock *this,
2277 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2279 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2280 return print_usage_bug(curr, this, bad_bit, new_bit);
2284 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2285 enum lock_usage_bit new_bit);
2287 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2290 * print irq inversion bug:
2293 print_irq_inversion_bug(struct task_struct *curr,
2294 struct lock_list *root, struct lock_list *other,
2295 struct held_lock *this, int forwards,
2296 const char *irqclass)
2298 struct lock_list *entry = other;
2299 struct lock_list *middle = NULL;
2302 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2306 printk("=========================================================\n");
2307 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
2308 print_kernel_ident();
2309 printk("---------------------------------------------------------\n");
2310 printk("%s/%d just changed the state of lock:\n",
2311 curr->comm, task_pid_nr(curr));
2314 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2316 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2317 print_lock_name(other->class);
2318 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2320 printk("\nother info that might help us debug this:\n");
2322 /* Find a middle lock (if one exists) */
2323 depth = get_lock_depth(other);
2325 if (depth == 0 && (entry != root)) {
2326 printk("lockdep:%s bad path found in chain graph\n", __func__);
2330 entry = get_lock_parent(entry);
2332 } while (entry && entry != root && (depth >= 0));
2334 print_irq_lock_scenario(root, other,
2335 middle ? middle->class : root->class, other->class);
2337 print_irq_lock_scenario(other, root,
2338 middle ? middle->class : other->class, root->class);
2340 lockdep_print_held_locks(curr);
2342 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2343 if (!save_trace(&root->trace))
2345 print_shortest_lock_dependencies(other, root);
2347 printk("\nstack backtrace:\n");
2354 * Prove that in the forwards-direction subgraph starting at <this>
2355 * there is no lock matching <mask>:
2358 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2359 enum lock_usage_bit bit, const char *irqclass)
2362 struct lock_list root;
2363 struct lock_list *uninitialized_var(target_entry);
2366 root.class = hlock_class(this);
2367 ret = find_usage_forwards(&root, bit, &target_entry);
2369 return print_bfs_bug(ret);
2373 return print_irq_inversion_bug(curr, &root, target_entry,
2378 * Prove that in the backwards-direction subgraph starting at <this>
2379 * there is no lock matching <mask>:
2382 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2383 enum lock_usage_bit bit, const char *irqclass)
2386 struct lock_list root;
2387 struct lock_list *uninitialized_var(target_entry);
2390 root.class = hlock_class(this);
2391 ret = find_usage_backwards(&root, bit, &target_entry);
2393 return print_bfs_bug(ret);
2397 return print_irq_inversion_bug(curr, &root, target_entry,
2401 void print_irqtrace_events(struct task_struct *curr)
2403 printk("irq event stamp: %u\n", curr->irq_events);
2404 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2405 print_ip_sym(curr->hardirq_enable_ip);
2406 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2407 print_ip_sym(curr->hardirq_disable_ip);
2408 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2409 print_ip_sym(curr->softirq_enable_ip);
2410 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2411 print_ip_sym(curr->softirq_disable_ip);
2414 static int HARDIRQ_verbose(struct lock_class *class)
2417 return class_filter(class);
2422 static int SOFTIRQ_verbose(struct lock_class *class)
2425 return class_filter(class);
2430 static int RECLAIM_FS_verbose(struct lock_class *class)
2433 return class_filter(class);
2438 #define STRICT_READ_CHECKS 1
2440 static int (*state_verbose_f[])(struct lock_class *class) = {
2441 #define LOCKDEP_STATE(__STATE) \
2443 #include "lockdep_states.h"
2444 #undef LOCKDEP_STATE
2447 static inline int state_verbose(enum lock_usage_bit bit,
2448 struct lock_class *class)
2450 return state_verbose_f[bit >> 2](class);
2453 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2454 enum lock_usage_bit bit, const char *name);
2457 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2458 enum lock_usage_bit new_bit)
2460 int excl_bit = exclusive_bit(new_bit);
2461 int read = new_bit & 1;
2462 int dir = new_bit & 2;
2465 * mark USED_IN has to look forwards -- to ensure no dependency
2466 * has ENABLED state, which would allow recursion deadlocks.
2468 * mark ENABLED has to look backwards -- to ensure no dependee
2469 * has USED_IN state, which, again, would allow recursion deadlocks.
2471 check_usage_f usage = dir ?
2472 check_usage_backwards : check_usage_forwards;
2475 * Validate that this particular lock does not have conflicting
2478 if (!valid_state(curr, this, new_bit, excl_bit))
2482 * Validate that the lock dependencies don't have conflicting usage
2485 if ((!read || !dir || STRICT_READ_CHECKS) &&
2486 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2490 * Check for read in write conflicts
2493 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2496 if (STRICT_READ_CHECKS &&
2497 !usage(curr, this, excl_bit + 1,
2498 state_name(new_bit + 1)))
2502 if (state_verbose(new_bit, hlock_class(this)))
2509 #define LOCKDEP_STATE(__STATE) __STATE,
2510 #include "lockdep_states.h"
2511 #undef LOCKDEP_STATE
2515 * Mark all held locks with a usage bit:
2518 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2520 enum lock_usage_bit usage_bit;
2521 struct held_lock *hlock;
2524 for (i = 0; i < curr->lockdep_depth; i++) {
2525 hlock = curr->held_locks + i;
2527 usage_bit = 2 + (mark << 2); /* ENABLED */
2529 usage_bit += 1; /* READ */
2531 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2536 if (!mark_lock(curr, hlock, usage_bit))
2544 * Hardirqs will be enabled:
2546 static void __trace_hardirqs_on_caller(unsigned long ip)
2548 struct task_struct *curr = current;
2550 /* we'll do an OFF -> ON transition: */
2551 curr->hardirqs_enabled = 1;
2554 * We are going to turn hardirqs on, so set the
2555 * usage bit for all held locks:
2557 if (!mark_held_locks(curr, HARDIRQ))
2560 * If we have softirqs enabled, then set the usage
2561 * bit for all held locks. (disabled hardirqs prevented
2562 * this bit from being set before)
2564 if (curr->softirqs_enabled)
2565 if (!mark_held_locks(curr, SOFTIRQ))
2568 curr->hardirq_enable_ip = ip;
2569 curr->hardirq_enable_event = ++curr->irq_events;
2570 debug_atomic_inc(hardirqs_on_events);
2573 __visible void trace_hardirqs_on_caller(unsigned long ip)
2575 time_hardirqs_on(CALLER_ADDR0, ip);
2577 if (unlikely(!debug_locks || current->lockdep_recursion))
2580 if (unlikely(current->hardirqs_enabled)) {
2582 * Neither irq nor preemption are disabled here
2583 * so this is racy by nature but losing one hit
2584 * in a stat is not a big deal.
2586 __debug_atomic_inc(redundant_hardirqs_on);
2591 * We're enabling irqs and according to our state above irqs weren't
2592 * already enabled, yet we find the hardware thinks they are in fact
2593 * enabled.. someone messed up their IRQ state tracing.
2595 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2599 * See the fine text that goes along with this variable definition.
2601 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2605 * Can't allow enabling interrupts while in an interrupt handler,
2606 * that's general bad form and such. Recursion, limited stack etc..
2608 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2611 current->lockdep_recursion = 1;
2612 __trace_hardirqs_on_caller(ip);
2613 current->lockdep_recursion = 0;
2615 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2617 void trace_hardirqs_on(void)
2619 trace_hardirqs_on_caller(CALLER_ADDR0);
2621 EXPORT_SYMBOL(trace_hardirqs_on);
2624 * Hardirqs were disabled:
2626 __visible void trace_hardirqs_off_caller(unsigned long ip)
2628 struct task_struct *curr = current;
2630 time_hardirqs_off(CALLER_ADDR0, ip);
2632 if (unlikely(!debug_locks || current->lockdep_recursion))
2636 * So we're supposed to get called after you mask local IRQs, but for
2637 * some reason the hardware doesn't quite think you did a proper job.
2639 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2642 if (curr->hardirqs_enabled) {
2644 * We have done an ON -> OFF transition:
2646 curr->hardirqs_enabled = 0;
2647 curr->hardirq_disable_ip = ip;
2648 curr->hardirq_disable_event = ++curr->irq_events;
2649 debug_atomic_inc(hardirqs_off_events);
2651 debug_atomic_inc(redundant_hardirqs_off);
2653 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2655 void trace_hardirqs_off(void)
2657 trace_hardirqs_off_caller(CALLER_ADDR0);
2659 EXPORT_SYMBOL(trace_hardirqs_off);
2662 * Softirqs will be enabled:
2664 void trace_softirqs_on(unsigned long ip)
2666 struct task_struct *curr = current;
2668 if (unlikely(!debug_locks || current->lockdep_recursion))
2672 * We fancy IRQs being disabled here, see softirq.c, avoids
2673 * funny state and nesting things.
2675 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2678 if (curr->softirqs_enabled) {
2679 debug_atomic_inc(redundant_softirqs_on);
2683 current->lockdep_recursion = 1;
2685 * We'll do an OFF -> ON transition:
2687 curr->softirqs_enabled = 1;
2688 curr->softirq_enable_ip = ip;
2689 curr->softirq_enable_event = ++curr->irq_events;
2690 debug_atomic_inc(softirqs_on_events);
2692 * We are going to turn softirqs on, so set the
2693 * usage bit for all held locks, if hardirqs are
2696 if (curr->hardirqs_enabled)
2697 mark_held_locks(curr, SOFTIRQ);
2698 current->lockdep_recursion = 0;
2702 * Softirqs were disabled:
2704 void trace_softirqs_off(unsigned long ip)
2706 struct task_struct *curr = current;
2708 if (unlikely(!debug_locks || current->lockdep_recursion))
2712 * We fancy IRQs being disabled here, see softirq.c
2714 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2717 if (curr->softirqs_enabled) {
2719 * We have done an ON -> OFF transition:
2721 curr->softirqs_enabled = 0;
2722 curr->softirq_disable_ip = ip;
2723 curr->softirq_disable_event = ++curr->irq_events;
2724 debug_atomic_inc(softirqs_off_events);
2726 * Whoops, we wanted softirqs off, so why aren't they?
2728 DEBUG_LOCKS_WARN_ON(!softirq_count());
2730 debug_atomic_inc(redundant_softirqs_off);
2733 static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2735 struct task_struct *curr = current;
2737 if (unlikely(!debug_locks))
2740 /* no reclaim without waiting on it */
2741 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
2744 /* this guy won't enter reclaim */
2745 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2748 /* We're only interested __GFP_FS allocations for now */
2749 if (!(gfp_mask & __GFP_FS))
2753 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2755 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2758 mark_held_locks(curr, RECLAIM_FS);
2761 static void check_flags(unsigned long flags);
2763 void lockdep_trace_alloc(gfp_t gfp_mask)
2765 unsigned long flags;
2767 if (unlikely(current->lockdep_recursion))
2770 raw_local_irq_save(flags);
2772 current->lockdep_recursion = 1;
2773 __lockdep_trace_alloc(gfp_mask, flags);
2774 current->lockdep_recursion = 0;
2775 raw_local_irq_restore(flags);
2778 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2781 * If non-trylock use in a hardirq or softirq context, then
2782 * mark the lock as used in these contexts:
2784 if (!hlock->trylock) {
2786 if (curr->hardirq_context)
2787 if (!mark_lock(curr, hlock,
2788 LOCK_USED_IN_HARDIRQ_READ))
2790 if (curr->softirq_context)
2791 if (!mark_lock(curr, hlock,
2792 LOCK_USED_IN_SOFTIRQ_READ))
2795 if (curr->hardirq_context)
2796 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2798 if (curr->softirq_context)
2799 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2803 if (!hlock->hardirqs_off) {
2805 if (!mark_lock(curr, hlock,
2806 LOCK_ENABLED_HARDIRQ_READ))
2808 if (curr->softirqs_enabled)
2809 if (!mark_lock(curr, hlock,
2810 LOCK_ENABLED_SOFTIRQ_READ))
2813 if (!mark_lock(curr, hlock,
2814 LOCK_ENABLED_HARDIRQ))
2816 if (curr->softirqs_enabled)
2817 if (!mark_lock(curr, hlock,
2818 LOCK_ENABLED_SOFTIRQ))
2824 * We reuse the irq context infrastructure more broadly as a general
2825 * context checking code. This tests GFP_FS recursion (a lock taken
2826 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2829 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2831 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2834 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2842 static int separate_irq_context(struct task_struct *curr,
2843 struct held_lock *hlock)
2845 unsigned int depth = curr->lockdep_depth;
2848 * Keep track of points where we cross into an interrupt context:
2850 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2851 curr->softirq_context;
2853 struct held_lock *prev_hlock;
2855 prev_hlock = curr->held_locks + depth-1;
2857 * If we cross into another context, reset the
2858 * hash key (this also prevents the checking and the
2859 * adding of the dependency to 'prev'):
2861 if (prev_hlock->irq_context != hlock->irq_context)
2867 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2870 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2871 enum lock_usage_bit new_bit)
2873 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
2877 static inline int mark_irqflags(struct task_struct *curr,
2878 struct held_lock *hlock)
2883 static inline int separate_irq_context(struct task_struct *curr,
2884 struct held_lock *hlock)
2889 void lockdep_trace_alloc(gfp_t gfp_mask)
2893 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2896 * Mark a lock with a usage bit, and validate the state transition:
2898 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2899 enum lock_usage_bit new_bit)
2901 unsigned int new_mask = 1 << new_bit, ret = 1;
2904 * If already set then do not dirty the cacheline,
2905 * nor do any checks:
2907 if (likely(hlock_class(this)->usage_mask & new_mask))
2913 * Make sure we didn't race:
2915 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2920 hlock_class(this)->usage_mask |= new_mask;
2922 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2926 #define LOCKDEP_STATE(__STATE) \
2927 case LOCK_USED_IN_##__STATE: \
2928 case LOCK_USED_IN_##__STATE##_READ: \
2929 case LOCK_ENABLED_##__STATE: \
2930 case LOCK_ENABLED_##__STATE##_READ:
2931 #include "lockdep_states.h"
2932 #undef LOCKDEP_STATE
2933 ret = mark_lock_irq(curr, this, new_bit);
2938 debug_atomic_dec(nr_unused_locks);
2941 if (!debug_locks_off_graph_unlock())
2950 * We must printk outside of the graph_lock:
2953 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2955 print_irqtrace_events(curr);
2963 * Initialize a lock instance's lock-class mapping info:
2965 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2966 struct lock_class_key *key, int subclass)
2970 kmemcheck_mark_initialized(lock, sizeof(*lock));
2972 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2973 lock->class_cache[i] = NULL;
2975 #ifdef CONFIG_LOCK_STAT
2976 lock->cpu = raw_smp_processor_id();
2980 * Can't be having no nameless bastards around this place!
2982 if (DEBUG_LOCKS_WARN_ON(!name)) {
2983 lock->name = "NULL";
2990 * No key, no joy, we need to hash something.
2992 if (DEBUG_LOCKS_WARN_ON(!key))
2995 * Sanity check, the lock-class key must be persistent:
2997 if (!static_obj(key)) {
2998 printk("BUG: key %p not in .data!\n", key);
3000 * What it says above ^^^^^, I suggest you read it.
3002 DEBUG_LOCKS_WARN_ON(1);
3007 if (unlikely(!debug_locks))
3011 unsigned long flags;
3013 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3016 raw_local_irq_save(flags);
3017 current->lockdep_recursion = 1;
3018 register_lock_class(lock, subclass, 1);
3019 current->lockdep_recursion = 0;
3020 raw_local_irq_restore(flags);
3023 EXPORT_SYMBOL_GPL(lockdep_init_map);
3025 struct lock_class_key __lockdep_no_validate__;
3026 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3029 print_lock_nested_lock_not_held(struct task_struct *curr,
3030 struct held_lock *hlock,
3033 if (!debug_locks_off())
3035 if (debug_locks_silent)
3039 printk("==================================\n");
3040 printk("[ BUG: Nested lock was not taken ]\n");
3041 print_kernel_ident();
3042 printk("----------------------------------\n");
3044 printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3047 printk("\nbut this task is not holding:\n");
3048 printk("%s\n", hlock->nest_lock->name);
3050 printk("\nstack backtrace:\n");
3053 printk("\nother info that might help us debug this:\n");
3054 lockdep_print_held_locks(curr);
3056 printk("\nstack backtrace:\n");
3062 static int __lock_is_held(struct lockdep_map *lock);
3065 * This gets called for every mutex_lock*()/spin_lock*() operation.
3066 * We maintain the dependency maps and validate the locking attempt:
3068 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3069 int trylock, int read, int check, int hardirqs_off,
3070 struct lockdep_map *nest_lock, unsigned long ip,
3071 int references, int pin_count)
3073 struct task_struct *curr = current;
3074 struct lock_class *class = NULL;
3075 struct held_lock *hlock;
3076 unsigned int depth, id;
3081 if (unlikely(!debug_locks))
3085 * Lockdep should run with IRQs disabled, otherwise we could
3086 * get an interrupt which would want to take locks, which would
3087 * end up in lockdep and have you got a head-ache already?
3089 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3092 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3095 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3096 class = lock->class_cache[subclass];
3100 if (unlikely(!class)) {
3101 class = register_lock_class(lock, subclass, 0);
3105 atomic_inc((atomic_t *)&class->ops);
3106 if (very_verbose(class)) {
3107 printk("\nacquire class [%p] %s", class->key, class->name);
3108 if (class->name_version > 1)
3109 printk("#%d", class->name_version);
3115 * Add the lock to the list of currently held locks.
3116 * (we dont increase the depth just yet, up until the
3117 * dependency checks are done)
3119 depth = curr->lockdep_depth;
3121 * Ran out of static storage for our per-task lock stack again have we?
3123 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3126 class_idx = class - lock_classes + 1;
3129 hlock = curr->held_locks + depth - 1;
3130 if (hlock->class_idx == class_idx && nest_lock) {
3131 if (hlock->references) {
3133 * Check: unsigned int references:12, overflow.
3135 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3138 hlock->references++;
3140 hlock->references = 2;
3147 hlock = curr->held_locks + depth;
3149 * Plain impossible, we just registered it and checked it weren't no
3150 * NULL like.. I bet this mushroom I ate was good!
3152 if (DEBUG_LOCKS_WARN_ON(!class))
3154 hlock->class_idx = class_idx;
3155 hlock->acquire_ip = ip;
3156 hlock->instance = lock;
3157 hlock->nest_lock = nest_lock;
3158 hlock->trylock = trylock;
3160 hlock->check = check;
3161 hlock->hardirqs_off = !!hardirqs_off;
3162 hlock->references = references;
3163 #ifdef CONFIG_LOCK_STAT
3164 hlock->waittime_stamp = 0;
3165 hlock->holdtime_stamp = lockstat_clock();
3167 hlock->pin_count = pin_count;
3169 if (check && !mark_irqflags(curr, hlock))
3172 /* mark it as used: */
3173 if (!mark_lock(curr, hlock, LOCK_USED))
3177 * Calculate the chain hash: it's the combined hash of all the
3178 * lock keys along the dependency chain. We save the hash value
3179 * at every step so that we can get the current hash easily
3180 * after unlock. The chain hash is then used to cache dependency
3183 * The 'key ID' is what is the most compact key value to drive
3184 * the hash, not class->key.
3186 id = class - lock_classes;
3188 * Whoops, we did it again.. ran straight out of our static allocation.
3190 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3193 chain_key = curr->curr_chain_key;
3196 * How can we have a chain hash when we ain't got no keys?!
3198 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3203 hlock->prev_chain_key = chain_key;
3204 if (separate_irq_context(curr, hlock)) {
3208 chain_key = iterate_chain_key(chain_key, id);
3210 if (nest_lock && !__lock_is_held(nest_lock))
3211 return print_lock_nested_lock_not_held(curr, hlock, ip);
3213 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3216 curr->curr_chain_key = chain_key;
3217 curr->lockdep_depth++;
3218 check_chain_key(curr);
3219 #ifdef CONFIG_DEBUG_LOCKDEP
3220 if (unlikely(!debug_locks))
3223 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3225 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3226 printk(KERN_DEBUG "depth: %i max: %lu!\n",
3227 curr->lockdep_depth, MAX_LOCK_DEPTH);
3229 lockdep_print_held_locks(current);
3230 debug_show_all_locks();
3236 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3237 max_lockdep_depth = curr->lockdep_depth;
3243 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3246 if (!debug_locks_off())
3248 if (debug_locks_silent)
3252 printk("=====================================\n");
3253 printk("[ 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");
3271 static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3273 if (hlock->instance == lock)
3276 if (hlock->references) {
3277 struct lock_class *class = lock->class_cache[0];
3280 class = look_up_lock_class(lock, 0);
3283 * If look_up_lock_class() failed to find a class, we're trying
3284 * to test if we hold a lock that has never yet been acquired.
3285 * Clearly if the lock hasn't been acquired _ever_, we're not
3286 * holding it either, so report failure.
3292 * References, but not a lock we're actually ref-counting?
3293 * State got messed up, follow the sites that change ->references
3294 * and try to make sense of it.
3296 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3299 if (hlock->class_idx == class - lock_classes + 1)
3307 __lock_set_class(struct lockdep_map *lock, const char *name,
3308 struct lock_class_key *key, unsigned int subclass,
3311 struct task_struct *curr = current;
3312 struct held_lock *hlock, *prev_hlock;
3313 struct lock_class *class;
3317 depth = curr->lockdep_depth;
3319 * This function is about (re)setting the class of a held lock,
3320 * yet we're not actually holding any locks. Naughty user!
3322 if (DEBUG_LOCKS_WARN_ON(!depth))
3326 for (i = depth-1; i >= 0; i--) {
3327 hlock = curr->held_locks + i;
3329 * We must not cross into another context:
3331 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3333 if (match_held_lock(hlock, lock))
3337 return print_unlock_imbalance_bug(curr, lock, ip);
3340 lockdep_init_map(lock, name, key, 0);
3341 class = register_lock_class(lock, subclass, 0);
3342 hlock->class_idx = class - lock_classes + 1;
3344 curr->lockdep_depth = i;
3345 curr->curr_chain_key = hlock->prev_chain_key;
3347 for (; i < depth; i++) {
3348 hlock = curr->held_locks + i;
3349 if (!__lock_acquire(hlock->instance,
3350 hlock_class(hlock)->subclass, hlock->trylock,
3351 hlock->read, hlock->check, hlock->hardirqs_off,
3352 hlock->nest_lock, hlock->acquire_ip,
3353 hlock->references, hlock->pin_count))
3358 * I took it apart and put it back together again, except now I have
3359 * these 'spare' parts.. where shall I put them.
3361 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3367 * Remove the lock to the list of currently held locks - this gets
3368 * called on mutex_unlock()/spin_unlock*() (or on a failed
3369 * mutex_lock_interruptible()).
3371 * @nested is an hysterical artifact, needs a tree wide cleanup.
3374 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3376 struct task_struct *curr = current;
3377 struct held_lock *hlock, *prev_hlock;
3381 if (unlikely(!debug_locks))
3384 depth = curr->lockdep_depth;
3386 * So we're all set to release this lock.. wait what lock? We don't
3387 * own any locks, you've been drinking again?
3389 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3390 return print_unlock_imbalance_bug(curr, lock, ip);
3393 * Check whether the lock exists in the current stack
3397 for (i = depth-1; i >= 0; i--) {
3398 hlock = curr->held_locks + i;
3400 * We must not cross into another context:
3402 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3404 if (match_held_lock(hlock, lock))
3408 return print_unlock_imbalance_bug(curr, lock, ip);
3411 if (hlock->instance == lock)
3412 lock_release_holdtime(hlock);
3414 WARN(hlock->pin_count, "releasing a pinned lock\n");
3416 if (hlock->references) {
3417 hlock->references--;
3418 if (hlock->references) {
3420 * We had, and after removing one, still have
3421 * references, the current lock stack is still
3422 * valid. We're done!
3429 * We have the right lock to unlock, 'hlock' points to it.
3430 * Now we remove it from the stack, and add back the other
3431 * entries (if any), recalculating the hash along the way:
3434 curr->lockdep_depth = i;
3435 curr->curr_chain_key = hlock->prev_chain_key;
3437 for (i++; i < depth; i++) {
3438 hlock = curr->held_locks + i;
3439 if (!__lock_acquire(hlock->instance,
3440 hlock_class(hlock)->subclass, hlock->trylock,
3441 hlock->read, hlock->check, hlock->hardirqs_off,
3442 hlock->nest_lock, hlock->acquire_ip,
3443 hlock->references, hlock->pin_count))
3448 * We had N bottles of beer on the wall, we drank one, but now
3449 * there's not N-1 bottles of beer left on the wall...
3451 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3457 static int __lock_is_held(struct lockdep_map *lock)
3459 struct task_struct *curr = current;
3462 for (i = 0; i < curr->lockdep_depth; i++) {
3463 struct held_lock *hlock = curr->held_locks + i;
3465 if (match_held_lock(hlock, lock))
3472 static void __lock_pin_lock(struct lockdep_map *lock)
3474 struct task_struct *curr = current;
3477 if (unlikely(!debug_locks))
3480 for (i = 0; i < curr->lockdep_depth; i++) {
3481 struct held_lock *hlock = curr->held_locks + i;
3483 if (match_held_lock(hlock, lock)) {
3489 WARN(1, "pinning an unheld lock\n");
3492 static void __lock_unpin_lock(struct lockdep_map *lock)
3494 struct task_struct *curr = current;
3497 if (unlikely(!debug_locks))
3500 for (i = 0; i < curr->lockdep_depth; i++) {
3501 struct held_lock *hlock = curr->held_locks + i;
3503 if (match_held_lock(hlock, lock)) {
3504 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3512 WARN(1, "unpinning an unheld lock\n");
3516 * Check whether we follow the irq-flags state precisely:
3518 static void check_flags(unsigned long flags)
3520 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3521 defined(CONFIG_TRACE_IRQFLAGS)
3525 if (irqs_disabled_flags(flags)) {
3526 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3527 printk("possible reason: unannotated irqs-off.\n");
3530 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3531 printk("possible reason: unannotated irqs-on.\n");
3536 * We dont accurately track softirq state in e.g.
3537 * hardirq contexts (such as on 4KSTACKS), so only
3538 * check if not in hardirq contexts:
3540 if (!hardirq_count()) {
3541 if (softirq_count()) {
3542 /* like the above, but with softirqs */
3543 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3545 /* lick the above, does it taste good? */
3546 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3551 print_irqtrace_events(current);
3555 void lock_set_class(struct lockdep_map *lock, const char *name,
3556 struct lock_class_key *key, unsigned int subclass,
3559 unsigned long flags;
3561 if (unlikely(current->lockdep_recursion))
3564 raw_local_irq_save(flags);
3565 current->lockdep_recursion = 1;
3567 if (__lock_set_class(lock, name, key, subclass, ip))
3568 check_chain_key(current);
3569 current->lockdep_recursion = 0;
3570 raw_local_irq_restore(flags);
3572 EXPORT_SYMBOL_GPL(lock_set_class);
3575 * We are not always called with irqs disabled - do that here,
3576 * and also avoid lockdep recursion:
3578 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3579 int trylock, int read, int check,
3580 struct lockdep_map *nest_lock, unsigned long ip)
3582 unsigned long flags;
3584 if (unlikely(current->lockdep_recursion))
3587 raw_local_irq_save(flags);
3590 current->lockdep_recursion = 1;
3591 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3592 __lock_acquire(lock, subclass, trylock, read, check,
3593 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
3594 current->lockdep_recursion = 0;
3595 raw_local_irq_restore(flags);
3597 EXPORT_SYMBOL_GPL(lock_acquire);
3599 void lock_release(struct lockdep_map *lock, int nested,
3602 unsigned long flags;
3604 if (unlikely(current->lockdep_recursion))
3607 raw_local_irq_save(flags);
3609 current->lockdep_recursion = 1;
3610 trace_lock_release(lock, ip);
3611 if (__lock_release(lock, nested, ip))
3612 check_chain_key(current);
3613 current->lockdep_recursion = 0;
3614 raw_local_irq_restore(flags);
3616 EXPORT_SYMBOL_GPL(lock_release);
3618 int lock_is_held(struct lockdep_map *lock)
3620 unsigned long flags;
3623 if (unlikely(current->lockdep_recursion))
3624 return 1; /* avoid false negative lockdep_assert_held() */
3626 raw_local_irq_save(flags);
3629 current->lockdep_recursion = 1;
3630 ret = __lock_is_held(lock);
3631 current->lockdep_recursion = 0;
3632 raw_local_irq_restore(flags);
3636 EXPORT_SYMBOL_GPL(lock_is_held);
3638 void lock_pin_lock(struct lockdep_map *lock)
3640 unsigned long flags;
3642 if (unlikely(current->lockdep_recursion))
3645 raw_local_irq_save(flags);
3648 current->lockdep_recursion = 1;
3649 __lock_pin_lock(lock);
3650 current->lockdep_recursion = 0;
3651 raw_local_irq_restore(flags);
3653 EXPORT_SYMBOL_GPL(lock_pin_lock);
3655 void lock_unpin_lock(struct lockdep_map *lock)
3657 unsigned long flags;
3659 if (unlikely(current->lockdep_recursion))
3662 raw_local_irq_save(flags);
3665 current->lockdep_recursion = 1;
3666 __lock_unpin_lock(lock);
3667 current->lockdep_recursion = 0;
3668 raw_local_irq_restore(flags);
3670 EXPORT_SYMBOL_GPL(lock_unpin_lock);
3672 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3674 current->lockdep_reclaim_gfp = gfp_mask;
3677 void lockdep_clear_current_reclaim_state(void)
3679 current->lockdep_reclaim_gfp = 0;
3682 #ifdef CONFIG_LOCK_STAT
3684 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3687 if (!debug_locks_off())
3689 if (debug_locks_silent)
3693 printk("=================================\n");
3694 printk("[ BUG: bad contention detected! ]\n");
3695 print_kernel_ident();
3696 printk("---------------------------------\n");
3697 printk("%s/%d is trying to contend lock (",
3698 curr->comm, task_pid_nr(curr));
3699 print_lockdep_cache(lock);
3702 printk("but there are no locks held!\n");
3703 printk("\nother info that might help us debug this:\n");
3704 lockdep_print_held_locks(curr);
3706 printk("\nstack backtrace:\n");
3713 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3715 struct task_struct *curr = current;
3716 struct held_lock *hlock, *prev_hlock;
3717 struct lock_class_stats *stats;
3719 int i, contention_point, contending_point;
3721 depth = curr->lockdep_depth;
3723 * Whee, we contended on this lock, except it seems we're not
3724 * actually trying to acquire anything much at all..
3726 if (DEBUG_LOCKS_WARN_ON(!depth))
3730 for (i = depth-1; i >= 0; i--) {
3731 hlock = curr->held_locks + i;
3733 * We must not cross into another context:
3735 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3737 if (match_held_lock(hlock, lock))
3741 print_lock_contention_bug(curr, lock, ip);
3745 if (hlock->instance != lock)
3748 hlock->waittime_stamp = lockstat_clock();
3750 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3751 contending_point = lock_point(hlock_class(hlock)->contending_point,
3754 stats = get_lock_stats(hlock_class(hlock));
3755 if (contention_point < LOCKSTAT_POINTS)
3756 stats->contention_point[contention_point]++;
3757 if (contending_point < LOCKSTAT_POINTS)
3758 stats->contending_point[contending_point]++;
3759 if (lock->cpu != smp_processor_id())
3760 stats->bounces[bounce_contended + !!hlock->read]++;
3761 put_lock_stats(stats);
3765 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3767 struct task_struct *curr = current;
3768 struct held_lock *hlock, *prev_hlock;
3769 struct lock_class_stats *stats;
3771 u64 now, waittime = 0;
3774 depth = curr->lockdep_depth;
3776 * Yay, we acquired ownership of this lock we didn't try to
3777 * acquire, how the heck did that happen?
3779 if (DEBUG_LOCKS_WARN_ON(!depth))
3783 for (i = depth-1; i >= 0; i--) {
3784 hlock = curr->held_locks + i;
3786 * We must not cross into another context:
3788 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3790 if (match_held_lock(hlock, lock))
3794 print_lock_contention_bug(curr, lock, _RET_IP_);
3798 if (hlock->instance != lock)
3801 cpu = smp_processor_id();
3802 if (hlock->waittime_stamp) {
3803 now = lockstat_clock();
3804 waittime = now - hlock->waittime_stamp;
3805 hlock->holdtime_stamp = now;
3808 trace_lock_acquired(lock, ip);
3810 stats = get_lock_stats(hlock_class(hlock));
3813 lock_time_inc(&stats->read_waittime, waittime);
3815 lock_time_inc(&stats->write_waittime, waittime);
3817 if (lock->cpu != cpu)
3818 stats->bounces[bounce_acquired + !!hlock->read]++;
3819 put_lock_stats(stats);
3825 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3827 unsigned long flags;
3829 if (unlikely(!lock_stat || !debug_locks))
3832 if (unlikely(current->lockdep_recursion))
3835 raw_local_irq_save(flags);
3837 current->lockdep_recursion = 1;
3838 trace_lock_contended(lock, ip);
3839 __lock_contended(lock, ip);
3840 current->lockdep_recursion = 0;
3841 raw_local_irq_restore(flags);
3843 EXPORT_SYMBOL_GPL(lock_contended);
3845 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3847 unsigned long flags;
3849 if (unlikely(!lock_stat || !debug_locks))
3852 if (unlikely(current->lockdep_recursion))
3855 raw_local_irq_save(flags);
3857 current->lockdep_recursion = 1;
3858 __lock_acquired(lock, ip);
3859 current->lockdep_recursion = 0;
3860 raw_local_irq_restore(flags);
3862 EXPORT_SYMBOL_GPL(lock_acquired);
3866 * Used by the testsuite, sanitize the validator state
3867 * after a simulated failure:
3870 void lockdep_reset(void)
3872 unsigned long flags;
3875 raw_local_irq_save(flags);
3876 current->curr_chain_key = 0;
3877 current->lockdep_depth = 0;
3878 current->lockdep_recursion = 0;
3879 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3880 nr_hardirq_chains = 0;
3881 nr_softirq_chains = 0;
3882 nr_process_chains = 0;
3884 for (i = 0; i < CHAINHASH_SIZE; i++)
3885 INIT_LIST_HEAD(chainhash_table + i);
3886 raw_local_irq_restore(flags);
3889 static void zap_class(struct lock_class *class)
3894 * Remove all dependencies this lock is
3897 for (i = 0; i < nr_list_entries; i++) {
3898 if (list_entries[i].class == class)
3899 list_del_rcu(&list_entries[i].entry);
3902 * Unhash the class and remove it from the all_lock_classes list:
3904 list_del_rcu(&class->hash_entry);
3905 list_del_rcu(&class->lock_entry);
3907 RCU_INIT_POINTER(class->key, NULL);
3908 RCU_INIT_POINTER(class->name, NULL);
3911 static inline int within(const void *addr, void *start, unsigned long size)
3913 return addr >= start && addr < start + size;
3917 * Used in module.c to remove lock classes from memory that is going to be
3918 * freed; and possibly re-used by other modules.
3920 * We will have had one sync_sched() before getting here, so we're guaranteed
3921 * nobody will look up these exact classes -- they're properly dead but still
3924 void lockdep_free_key_range(void *start, unsigned long size)
3926 struct lock_class *class;
3927 struct list_head *head;
3928 unsigned long flags;
3932 raw_local_irq_save(flags);
3933 locked = graph_lock();
3936 * Unhash all classes that were created by this module:
3938 for (i = 0; i < CLASSHASH_SIZE; i++) {
3939 head = classhash_table + i;
3940 if (list_empty(head))
3942 list_for_each_entry_rcu(class, head, hash_entry) {
3943 if (within(class->key, start, size))
3945 else if (within(class->name, start, size))
3952 raw_local_irq_restore(flags);
3955 * Wait for any possible iterators from look_up_lock_class() to pass
3956 * before continuing to free the memory they refer to.
3958 * sync_sched() is sufficient because the read-side is IRQ disable.
3960 synchronize_sched();
3963 * XXX at this point we could return the resources to the pool;
3964 * instead we leak them. We would need to change to bitmap allocators
3965 * instead of the linear allocators we have now.
3969 void lockdep_reset_lock(struct lockdep_map *lock)
3971 struct lock_class *class;
3972 struct list_head *head;
3973 unsigned long flags;
3977 raw_local_irq_save(flags);
3980 * Remove all classes this lock might have:
3982 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3984 * If the class exists we look it up and zap it:
3986 class = look_up_lock_class(lock, j);
3991 * Debug check: in the end all mapped classes should
3994 locked = graph_lock();
3995 for (i = 0; i < CLASSHASH_SIZE; i++) {
3996 head = classhash_table + i;
3997 if (list_empty(head))
3999 list_for_each_entry_rcu(class, head, hash_entry) {
4002 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4003 match |= class == lock->class_cache[j];
4005 if (unlikely(match)) {
4006 if (debug_locks_off_graph_unlock()) {
4008 * We all just reset everything, how did it match?
4020 raw_local_irq_restore(flags);
4023 void lockdep_init(void)
4028 * Some architectures have their own start_kernel()
4029 * code which calls lockdep_init(), while we also
4030 * call lockdep_init() from the start_kernel() itself,
4031 * and we want to initialize the hashes only once:
4033 if (lockdep_initialized)
4036 for (i = 0; i < CLASSHASH_SIZE; i++)
4037 INIT_LIST_HEAD(classhash_table + i);
4039 for (i = 0; i < CHAINHASH_SIZE; i++)
4040 INIT_LIST_HEAD(chainhash_table + i);
4042 lockdep_initialized = 1;
4045 void __init lockdep_info(void)
4047 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4049 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
4050 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4051 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
4052 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
4053 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4054 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4055 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4057 printk(" memory used by lock dependency info: %lu kB\n",
4058 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4059 sizeof(struct list_head) * CLASSHASH_SIZE +
4060 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4061 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
4062 sizeof(struct list_head) * CHAINHASH_SIZE
4063 #ifdef CONFIG_PROVE_LOCKING
4064 + sizeof(struct circular_queue)
4069 printk(" per task-struct memory footprint: %lu bytes\n",
4070 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
4072 #ifdef CONFIG_DEBUG_LOCKDEP
4073 if (lockdep_init_error) {
4074 printk("WARNING: lockdep init error: lock '%s' was acquired before lockdep_init().\n", lock_init_error);
4075 printk("Call stack leading to lockdep invocation was:\n");
4076 print_stack_trace(&lockdep_init_trace, 0);
4082 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
4083 const void *mem_to, struct held_lock *hlock)
4085 if (!debug_locks_off())
4087 if (debug_locks_silent)
4091 printk("=========================\n");
4092 printk("[ BUG: held lock freed! ]\n");
4093 print_kernel_ident();
4094 printk("-------------------------\n");
4095 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4096 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
4098 lockdep_print_held_locks(curr);
4100 printk("\nstack backtrace:\n");
4104 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4105 const void* lock_from, unsigned long lock_len)
4107 return lock_from + lock_len <= mem_from ||
4108 mem_from + mem_len <= lock_from;
4112 * Called when kernel memory is freed (or unmapped), or if a lock
4113 * is destroyed or reinitialized - this code checks whether there is
4114 * any held lock in the memory range of <from> to <to>:
4116 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4118 struct task_struct *curr = current;
4119 struct held_lock *hlock;
4120 unsigned long flags;
4123 if (unlikely(!debug_locks))
4126 raw_local_irq_save(flags);
4127 for (i = 0; i < curr->lockdep_depth; i++) {
4128 hlock = curr->held_locks + i;
4130 if (not_in_range(mem_from, mem_len, hlock->instance,
4131 sizeof(*hlock->instance)))
4134 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
4137 raw_local_irq_restore(flags);
4139 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
4141 static void print_held_locks_bug(void)
4143 if (!debug_locks_off())
4145 if (debug_locks_silent)
4149 printk("=====================================\n");
4150 printk("[ BUG: %s/%d still has locks held! ]\n",
4151 current->comm, task_pid_nr(current));
4152 print_kernel_ident();
4153 printk("-------------------------------------\n");
4154 lockdep_print_held_locks(current);
4155 printk("\nstack backtrace:\n");
4159 void debug_check_no_locks_held(void)
4161 if (unlikely(current->lockdep_depth > 0))
4162 print_held_locks_bug();
4164 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
4167 void debug_show_all_locks(void)
4169 struct task_struct *g, *p;
4173 if (unlikely(!debug_locks)) {
4174 printk("INFO: lockdep is turned off.\n");
4177 printk("\nShowing all locks held in the system:\n");
4180 * Here we try to get the tasklist_lock as hard as possible,
4181 * if not successful after 2 seconds we ignore it (but keep
4182 * trying). This is to enable a debug printout even if a
4183 * tasklist_lock-holding task deadlocks or crashes.
4186 if (!read_trylock(&tasklist_lock)) {
4188 printk("hm, tasklist_lock locked, retrying... ");
4191 printk(" #%d", 10-count);
4195 printk(" ignoring it.\n");
4199 printk(KERN_CONT " locked it.\n");
4202 do_each_thread(g, p) {
4204 * It's not reliable to print a task's held locks
4205 * if it's not sleeping (or if it's not the current
4208 if (p->state == TASK_RUNNING && p != current)
4210 if (p->lockdep_depth)
4211 lockdep_print_held_locks(p);
4213 if (read_trylock(&tasklist_lock))
4215 } while_each_thread(g, p);
4218 printk("=============================================\n\n");
4221 read_unlock(&tasklist_lock);
4223 EXPORT_SYMBOL_GPL(debug_show_all_locks);
4227 * Careful: only use this function if you are sure that
4228 * the task cannot run in parallel!
4230 void debug_show_held_locks(struct task_struct *task)
4232 if (unlikely(!debug_locks)) {
4233 printk("INFO: lockdep is turned off.\n");
4236 lockdep_print_held_locks(task);
4238 EXPORT_SYMBOL_GPL(debug_show_held_locks);
4240 asmlinkage __visible void lockdep_sys_exit(void)
4242 struct task_struct *curr = current;
4244 if (unlikely(curr->lockdep_depth)) {
4245 if (!debug_locks_off())
4248 printk("================================================\n");
4249 printk("[ BUG: lock held when returning to user space! ]\n");
4250 print_kernel_ident();
4251 printk("------------------------------------------------\n");
4252 printk("%s/%d is leaving the kernel with locks still held!\n",
4253 curr->comm, curr->pid);
4254 lockdep_print_held_locks(curr);
4258 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
4260 struct task_struct *curr = current;
4262 #ifndef CONFIG_PROVE_RCU_REPEATEDLY
4263 if (!debug_locks_off())
4265 #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4266 /* Note: the following can be executed concurrently, so be careful. */
4268 printk("===============================\n");
4269 printk("[ INFO: suspicious RCU usage. ]\n");
4270 print_kernel_ident();
4271 printk("-------------------------------\n");
4272 printk("%s:%d %s!\n", file, line, s);
4273 printk("\nother info that might help us debug this:\n\n");
4274 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4275 !rcu_lockdep_current_cpu_online()
4276 ? "RCU used illegally from offline CPU!\n"
4277 : !rcu_is_watching()
4278 ? "RCU used illegally from idle CPU!\n"
4280 rcu_scheduler_active, debug_locks);
4283 * If a CPU is in the RCU-free window in idle (ie: in the section
4284 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4285 * considers that CPU to be in an "extended quiescent state",
4286 * which means that RCU will be completely ignoring that CPU.
4287 * Therefore, rcu_read_lock() and friends have absolutely no
4288 * effect on a CPU running in that state. In other words, even if
4289 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4290 * delete data structures out from under it. RCU really has no
4291 * choice here: we need to keep an RCU-free window in idle where
4292 * the CPU may possibly enter into low power mode. This way we can
4293 * notice an extended quiescent state to other CPUs that started a grace
4294 * period. Otherwise we would delay any grace period as long as we run
4297 * So complain bitterly if someone does call rcu_read_lock(),
4298 * rcu_read_lock_bh() and so on from extended quiescent states.
4300 if (!rcu_is_watching())
4301 printk("RCU used illegally from extended quiescent state!\n");
4303 lockdep_print_held_locks(curr);
4304 printk("\nstack backtrace:\n");
4307 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);