lockdep: Comment all warnings
[linux-2.6/libata-dev.git] / kernel / lockdep.c
blobc081fa967c8f32166b329b702bf6ab11887000fe
1 /*
2 * kernel/lockdep.c
4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
14 * - lock inversion scenarios
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>
48 #include <asm/sections.h>
50 #include "lockdep_internals.h"
52 #define CREATE_TRACE_POINTS
53 #include <trace/events/lock.h>
55 #ifdef CONFIG_PROVE_LOCKING
56 int prove_locking = 1;
57 module_param(prove_locking, int, 0644);
58 #else
59 #define prove_locking 0
60 #endif
62 #ifdef CONFIG_LOCK_STAT
63 int lock_stat = 1;
64 module_param(lock_stat, int, 0644);
65 #else
66 #define lock_stat 0
67 #endif
70 * lockdep_lock: protects the lockdep graph, the hashes and the
71 * class/list/hash allocators.
73 * This is one of the rare exceptions where it's justified
74 * to use a raw spinlock - we really dont want the spinlock
75 * code to recurse back into the lockdep code...
77 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
79 static int graph_lock(void)
81 arch_spin_lock(&lockdep_lock);
83 * Make sure that if another CPU detected a bug while
84 * walking the graph we dont change it (while the other
85 * CPU is busy printing out stuff with the graph lock
86 * dropped already)
88 if (!debug_locks) {
89 arch_spin_unlock(&lockdep_lock);
90 return 0;
92 /* prevent any recursions within lockdep from causing deadlocks */
93 current->lockdep_recursion++;
94 return 1;
97 static inline int graph_unlock(void)
99 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
101 * The lockdep graph lock isn't locked while we expect it to
102 * be, we're confused now, bye!
104 return DEBUG_LOCKS_WARN_ON(1);
107 current->lockdep_recursion--;
108 arch_spin_unlock(&lockdep_lock);
109 return 0;
113 * Turn lock debugging off and return with 0 if it was off already,
114 * and also release the graph lock:
116 static inline int debug_locks_off_graph_unlock(void)
118 int ret = debug_locks_off();
120 arch_spin_unlock(&lockdep_lock);
122 return ret;
125 static int lockdep_initialized;
127 unsigned long nr_list_entries;
128 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
131 * All data structures here are protected by the global debug_lock.
133 * Mutex key structs only get allocated, once during bootup, and never
134 * get freed - this significantly simplifies the debugging code.
136 unsigned long nr_lock_classes;
137 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
139 static inline struct lock_class *hlock_class(struct held_lock *hlock)
141 if (!hlock->class_idx) {
143 * Someone passed in garbage, we give up.
145 DEBUG_LOCKS_WARN_ON(1);
146 return NULL;
148 return lock_classes + hlock->class_idx - 1;
151 #ifdef CONFIG_LOCK_STAT
152 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
153 cpu_lock_stats);
155 static inline u64 lockstat_clock(void)
157 return local_clock();
160 static int lock_point(unsigned long points[], unsigned long ip)
162 int i;
164 for (i = 0; i < LOCKSTAT_POINTS; i++) {
165 if (points[i] == 0) {
166 points[i] = ip;
167 break;
169 if (points[i] == ip)
170 break;
173 return i;
176 static void lock_time_inc(struct lock_time *lt, u64 time)
178 if (time > lt->max)
179 lt->max = time;
181 if (time < lt->min || !lt->nr)
182 lt->min = time;
184 lt->total += time;
185 lt->nr++;
188 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
190 if (!src->nr)
191 return;
193 if (src->max > dst->max)
194 dst->max = src->max;
196 if (src->min < dst->min || !dst->nr)
197 dst->min = src->min;
199 dst->total += src->total;
200 dst->nr += src->nr;
203 struct lock_class_stats lock_stats(struct lock_class *class)
205 struct lock_class_stats stats;
206 int cpu, i;
208 memset(&stats, 0, sizeof(struct lock_class_stats));
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *pcs =
211 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
213 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
214 stats.contention_point[i] += pcs->contention_point[i];
216 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
217 stats.contending_point[i] += pcs->contending_point[i];
219 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
220 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
222 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
223 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
225 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
226 stats.bounces[i] += pcs->bounces[i];
229 return stats;
232 void clear_lock_stats(struct lock_class *class)
234 int cpu;
236 for_each_possible_cpu(cpu) {
237 struct lock_class_stats *cpu_stats =
238 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
240 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
242 memset(class->contention_point, 0, sizeof(class->contention_point));
243 memset(class->contending_point, 0, sizeof(class->contending_point));
246 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
248 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
251 static void put_lock_stats(struct lock_class_stats *stats)
253 put_cpu_var(cpu_lock_stats);
256 static void lock_release_holdtime(struct held_lock *hlock)
258 struct lock_class_stats *stats;
259 u64 holdtime;
261 if (!lock_stat)
262 return;
264 holdtime = lockstat_clock() - hlock->holdtime_stamp;
266 stats = get_lock_stats(hlock_class(hlock));
267 if (hlock->read)
268 lock_time_inc(&stats->read_holdtime, holdtime);
269 else
270 lock_time_inc(&stats->write_holdtime, holdtime);
271 put_lock_stats(stats);
273 #else
274 static inline void lock_release_holdtime(struct held_lock *hlock)
277 #endif
280 * We keep a global list of all lock classes. The list only grows,
281 * never shrinks. The list is only accessed with the lockdep
282 * spinlock lock held.
284 LIST_HEAD(all_lock_classes);
287 * The lockdep classes are in a hash-table as well, for fast lookup:
289 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
290 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
291 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
292 #define classhashentry(key) (classhash_table + __classhashfn((key)))
294 static struct list_head classhash_table[CLASSHASH_SIZE];
297 * We put the lock dependency chains into a hash-table as well, to cache
298 * their existence:
300 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
301 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
302 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
303 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
305 static struct list_head chainhash_table[CHAINHASH_SIZE];
308 * The hash key of the lock dependency chains is a hash itself too:
309 * it's a hash of all locks taken up to that lock, including that lock.
310 * It's a 64-bit hash, because it's important for the keys to be
311 * unique.
313 #define iterate_chain_key(key1, key2) \
314 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
315 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
316 (key2))
318 void lockdep_off(void)
320 current->lockdep_recursion++;
322 EXPORT_SYMBOL(lockdep_off);
324 void lockdep_on(void)
326 current->lockdep_recursion--;
328 EXPORT_SYMBOL(lockdep_on);
331 * Debugging switches:
334 #define VERBOSE 0
335 #define VERY_VERBOSE 0
337 #if VERBOSE
338 # define HARDIRQ_VERBOSE 1
339 # define SOFTIRQ_VERBOSE 1
340 # define RECLAIM_VERBOSE 1
341 #else
342 # define HARDIRQ_VERBOSE 0
343 # define SOFTIRQ_VERBOSE 0
344 # define RECLAIM_VERBOSE 0
345 #endif
347 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
349 * Quick filtering for interesting events:
351 static int class_filter(struct lock_class *class)
353 #if 0
354 /* Example */
355 if (class->name_version == 1 &&
356 !strcmp(class->name, "lockname"))
357 return 1;
358 if (class->name_version == 1 &&
359 !strcmp(class->name, "&struct->lockfield"))
360 return 1;
361 #endif
362 /* Filter everything else. 1 would be to allow everything else */
363 return 0;
365 #endif
367 static int verbose(struct lock_class *class)
369 #if VERBOSE
370 return class_filter(class);
371 #endif
372 return 0;
376 * Stack-trace: tightly packed array of stack backtrace
377 * addresses. Protected by the graph_lock.
379 unsigned long nr_stack_trace_entries;
380 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
382 static int save_trace(struct stack_trace *trace)
384 trace->nr_entries = 0;
385 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
386 trace->entries = stack_trace + nr_stack_trace_entries;
388 trace->skip = 3;
390 save_stack_trace(trace);
393 * Some daft arches put -1 at the end to indicate its a full trace.
395 * <rant> this is buggy anyway, since it takes a whole extra entry so a
396 * complete trace that maxes out the entries provided will be reported
397 * as incomplete, friggin useless </rant>
399 if (trace->nr_entries != 0 &&
400 trace->entries[trace->nr_entries-1] == ULONG_MAX)
401 trace->nr_entries--;
403 trace->max_entries = trace->nr_entries;
405 nr_stack_trace_entries += trace->nr_entries;
407 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
408 if (!debug_locks_off_graph_unlock())
409 return 0;
411 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
412 printk("turning off the locking correctness validator.\n");
413 dump_stack();
415 return 0;
418 return 1;
421 unsigned int nr_hardirq_chains;
422 unsigned int nr_softirq_chains;
423 unsigned int nr_process_chains;
424 unsigned int max_lockdep_depth;
426 #ifdef CONFIG_DEBUG_LOCKDEP
428 * We cannot printk in early bootup code. Not even early_printk()
429 * might work. So we mark any initialization errors and printk
430 * about it later on, in lockdep_info().
432 static int lockdep_init_error;
433 static unsigned long lockdep_init_trace_data[20];
434 static struct stack_trace lockdep_init_trace = {
435 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
436 .entries = lockdep_init_trace_data,
440 * Various lockdep statistics:
442 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
443 #endif
446 * Locking printouts:
449 #define __USAGE(__STATE) \
450 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
451 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
452 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
453 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
455 static const char *usage_str[] =
457 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
458 #include "lockdep_states.h"
459 #undef LOCKDEP_STATE
460 [LOCK_USED] = "INITIAL USE",
463 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
465 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
468 static inline unsigned long lock_flag(enum lock_usage_bit bit)
470 return 1UL << bit;
473 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
475 char c = '.';
477 if (class->usage_mask & lock_flag(bit + 2))
478 c = '+';
479 if (class->usage_mask & lock_flag(bit)) {
480 c = '-';
481 if (class->usage_mask & lock_flag(bit + 2))
482 c = '?';
485 return c;
488 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
490 int i = 0;
492 #define LOCKDEP_STATE(__STATE) \
493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
495 #include "lockdep_states.h"
496 #undef LOCKDEP_STATE
498 usage[i] = '\0';
501 static int __print_lock_name(struct lock_class *class)
503 char str[KSYM_NAME_LEN];
504 const char *name;
506 name = class->name;
507 if (!name)
508 name = __get_key_name(class->key, str);
510 return printk("%s", name);
513 static void print_lock_name(struct lock_class *class)
515 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
516 const char *name;
518 get_usage_chars(class, usage);
520 name = class->name;
521 if (!name) {
522 name = __get_key_name(class->key, str);
523 printk(" (%s", name);
524 } else {
525 printk(" (%s", name);
526 if (class->name_version > 1)
527 printk("#%d", class->name_version);
528 if (class->subclass)
529 printk("/%d", class->subclass);
531 printk("){%s}", usage);
534 static void print_lockdep_cache(struct lockdep_map *lock)
536 const char *name;
537 char str[KSYM_NAME_LEN];
539 name = lock->name;
540 if (!name)
541 name = __get_key_name(lock->key->subkeys, str);
543 printk("%s", name);
546 static void print_lock(struct held_lock *hlock)
548 print_lock_name(hlock_class(hlock));
549 printk(", at: ");
550 print_ip_sym(hlock->acquire_ip);
553 static void lockdep_print_held_locks(struct task_struct *curr)
555 int i, depth = curr->lockdep_depth;
557 if (!depth) {
558 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
559 return;
561 printk("%d lock%s held by %s/%d:\n",
562 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
564 for (i = 0; i < depth; i++) {
565 printk(" #%d: ", i);
566 print_lock(curr->held_locks + i);
570 static void print_kernel_version(void)
572 printk("%s %.*s\n", init_utsname()->release,
573 (int)strcspn(init_utsname()->version, " "),
574 init_utsname()->version);
577 static int very_verbose(struct lock_class *class)
579 #if VERY_VERBOSE
580 return class_filter(class);
581 #endif
582 return 0;
586 * Is this the address of a static object:
588 static int static_obj(void *obj)
590 unsigned long start = (unsigned long) &_stext,
591 end = (unsigned long) &_end,
592 addr = (unsigned long) obj;
595 * static variable?
597 if ((addr >= start) && (addr < end))
598 return 1;
600 if (arch_is_kernel_data(addr))
601 return 1;
604 * in-kernel percpu var?
606 if (is_kernel_percpu_address(addr))
607 return 1;
610 * module static or percpu var?
612 return is_module_address(addr) || is_module_percpu_address(addr);
616 * To make lock name printouts unique, we calculate a unique
617 * class->name_version generation counter:
619 static int count_matching_names(struct lock_class *new_class)
621 struct lock_class *class;
622 int count = 0;
624 if (!new_class->name)
625 return 0;
627 list_for_each_entry(class, &all_lock_classes, lock_entry) {
628 if (new_class->key - new_class->subclass == class->key)
629 return class->name_version;
630 if (class->name && !strcmp(class->name, new_class->name))
631 count = max(count, class->name_version);
634 return count + 1;
638 * Register a lock's class in the hash-table, if the class is not present
639 * yet. Otherwise we look it up. We cache the result in the lock object
640 * itself, so actual lookup of the hash should be once per lock object.
642 static inline struct lock_class *
643 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
645 struct lockdep_subclass_key *key;
646 struct list_head *hash_head;
647 struct lock_class *class;
649 #ifdef CONFIG_DEBUG_LOCKDEP
651 * If the architecture calls into lockdep before initializing
652 * the hashes then we'll warn about it later. (we cannot printk
653 * right now)
655 if (unlikely(!lockdep_initialized)) {
656 lockdep_init();
657 lockdep_init_error = 1;
658 save_stack_trace(&lockdep_init_trace);
660 #endif
662 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
663 debug_locks_off();
664 printk(KERN_ERR
665 "BUG: looking up invalid subclass: %u\n", subclass);
666 printk(KERN_ERR
667 "turning off the locking correctness validator.\n");
668 dump_stack();
669 return NULL;
673 * Static locks do not have their class-keys yet - for them the key
674 * is the lock object itself:
676 if (unlikely(!lock->key))
677 lock->key = (void *)lock;
680 * NOTE: the class-key must be unique. For dynamic locks, a static
681 * lock_class_key variable is passed in through the mutex_init()
682 * (or spin_lock_init()) call - which acts as the key. For static
683 * locks we use the lock object itself as the key.
685 BUILD_BUG_ON(sizeof(struct lock_class_key) >
686 sizeof(struct lockdep_map));
688 key = lock->key->subkeys + subclass;
690 hash_head = classhashentry(key);
693 * We can walk the hash lockfree, because the hash only
694 * grows, and we are careful when adding entries to the end:
696 list_for_each_entry(class, hash_head, hash_entry) {
697 if (class->key == key) {
699 * Huh! same key, different name? Did someone trample
700 * on some memory? We're most confused.
702 WARN_ON_ONCE(class->name != lock->name);
703 return class;
707 return NULL;
711 * Register a lock's class in the hash-table, if the class is not present
712 * yet. Otherwise we look it up. We cache the result in the lock object
713 * itself, so actual lookup of the hash should be once per lock object.
715 static inline struct lock_class *
716 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
718 struct lockdep_subclass_key *key;
719 struct list_head *hash_head;
720 struct lock_class *class;
721 unsigned long flags;
723 class = look_up_lock_class(lock, subclass);
724 if (likely(class))
725 return class;
728 * Debug-check: all keys must be persistent!
730 if (!static_obj(lock->key)) {
731 debug_locks_off();
732 printk("INFO: trying to register non-static key.\n");
733 printk("the code is fine but needs lockdep annotation.\n");
734 printk("turning off the locking correctness validator.\n");
735 dump_stack();
737 return NULL;
740 key = lock->key->subkeys + subclass;
741 hash_head = classhashentry(key);
743 raw_local_irq_save(flags);
744 if (!graph_lock()) {
745 raw_local_irq_restore(flags);
746 return NULL;
749 * We have to do the hash-walk again, to avoid races
750 * with another CPU:
752 list_for_each_entry(class, hash_head, hash_entry)
753 if (class->key == key)
754 goto out_unlock_set;
756 * Allocate a new key from the static array, and add it to
757 * the hash:
759 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
760 if (!debug_locks_off_graph_unlock()) {
761 raw_local_irq_restore(flags);
762 return NULL;
764 raw_local_irq_restore(flags);
766 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
767 printk("turning off the locking correctness validator.\n");
768 dump_stack();
769 return NULL;
771 class = lock_classes + nr_lock_classes++;
772 debug_atomic_inc(nr_unused_locks);
773 class->key = key;
774 class->name = lock->name;
775 class->subclass = subclass;
776 INIT_LIST_HEAD(&class->lock_entry);
777 INIT_LIST_HEAD(&class->locks_before);
778 INIT_LIST_HEAD(&class->locks_after);
779 class->name_version = count_matching_names(class);
781 * We use RCU's safe list-add method to make
782 * parallel walking of the hash-list safe:
784 list_add_tail_rcu(&class->hash_entry, hash_head);
786 * Add it to the global list of classes:
788 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
790 if (verbose(class)) {
791 graph_unlock();
792 raw_local_irq_restore(flags);
794 printk("\nnew class %p: %s", class->key, class->name);
795 if (class->name_version > 1)
796 printk("#%d", class->name_version);
797 printk("\n");
798 dump_stack();
800 raw_local_irq_save(flags);
801 if (!graph_lock()) {
802 raw_local_irq_restore(flags);
803 return NULL;
806 out_unlock_set:
807 graph_unlock();
808 raw_local_irq_restore(flags);
810 if (!subclass || force)
811 lock->class_cache[0] = class;
812 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
813 lock->class_cache[subclass] = class;
816 * Hash collision, did we smoke some? We found a class with a matching
817 * hash but the subclass -- which is hashed in -- didn't match.
819 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
820 return NULL;
822 return class;
825 #ifdef CONFIG_PROVE_LOCKING
827 * Allocate a lockdep entry. (assumes the graph_lock held, returns
828 * with NULL on failure)
830 static struct lock_list *alloc_list_entry(void)
832 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
833 if (!debug_locks_off_graph_unlock())
834 return NULL;
836 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
837 printk("turning off the locking correctness validator.\n");
838 dump_stack();
839 return NULL;
841 return list_entries + nr_list_entries++;
845 * Add a new dependency to the head of the list:
847 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
848 struct list_head *head, unsigned long ip,
849 int distance, struct stack_trace *trace)
851 struct lock_list *entry;
853 * Lock not present yet - get a new dependency struct and
854 * add it to the list:
856 entry = alloc_list_entry();
857 if (!entry)
858 return 0;
860 entry->class = this;
861 entry->distance = distance;
862 entry->trace = *trace;
864 * Since we never remove from the dependency list, the list can
865 * be walked lockless by other CPUs, it's only allocation
866 * that must be protected by the spinlock. But this also means
867 * we must make new entries visible only once writes to the
868 * entry become visible - hence the RCU op:
870 list_add_tail_rcu(&entry->entry, head);
872 return 1;
876 * For good efficiency of modular, we use power of 2
878 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
879 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
882 * The circular_queue and helpers is used to implement the
883 * breadth-first search(BFS)algorithem, by which we can build
884 * the shortest path from the next lock to be acquired to the
885 * previous held lock if there is a circular between them.
887 struct circular_queue {
888 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
889 unsigned int front, rear;
892 static struct circular_queue lock_cq;
894 unsigned int max_bfs_queue_depth;
896 static unsigned int lockdep_dependency_gen_id;
898 static inline void __cq_init(struct circular_queue *cq)
900 cq->front = cq->rear = 0;
901 lockdep_dependency_gen_id++;
904 static inline int __cq_empty(struct circular_queue *cq)
906 return (cq->front == cq->rear);
909 static inline int __cq_full(struct circular_queue *cq)
911 return ((cq->rear + 1) & CQ_MASK) == cq->front;
914 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
916 if (__cq_full(cq))
917 return -1;
919 cq->element[cq->rear] = elem;
920 cq->rear = (cq->rear + 1) & CQ_MASK;
921 return 0;
924 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
926 if (__cq_empty(cq))
927 return -1;
929 *elem = cq->element[cq->front];
930 cq->front = (cq->front + 1) & CQ_MASK;
931 return 0;
934 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
936 return (cq->rear - cq->front) & CQ_MASK;
939 static inline void mark_lock_accessed(struct lock_list *lock,
940 struct lock_list *parent)
942 unsigned long nr;
944 nr = lock - list_entries;
945 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
946 lock->parent = parent;
947 lock->class->dep_gen_id = lockdep_dependency_gen_id;
950 static inline unsigned long lock_accessed(struct lock_list *lock)
952 unsigned long nr;
954 nr = lock - list_entries;
955 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
956 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
959 static inline struct lock_list *get_lock_parent(struct lock_list *child)
961 return child->parent;
964 static inline int get_lock_depth(struct lock_list *child)
966 int depth = 0;
967 struct lock_list *parent;
969 while ((parent = get_lock_parent(child))) {
970 child = parent;
971 depth++;
973 return depth;
976 static int __bfs(struct lock_list *source_entry,
977 void *data,
978 int (*match)(struct lock_list *entry, void *data),
979 struct lock_list **target_entry,
980 int forward)
982 struct lock_list *entry;
983 struct list_head *head;
984 struct circular_queue *cq = &lock_cq;
985 int ret = 1;
987 if (match(source_entry, data)) {
988 *target_entry = source_entry;
989 ret = 0;
990 goto exit;
993 if (forward)
994 head = &source_entry->class->locks_after;
995 else
996 head = &source_entry->class->locks_before;
998 if (list_empty(head))
999 goto exit;
1001 __cq_init(cq);
1002 __cq_enqueue(cq, (unsigned long)source_entry);
1004 while (!__cq_empty(cq)) {
1005 struct lock_list *lock;
1007 __cq_dequeue(cq, (unsigned long *)&lock);
1009 if (!lock->class) {
1010 ret = -2;
1011 goto exit;
1014 if (forward)
1015 head = &lock->class->locks_after;
1016 else
1017 head = &lock->class->locks_before;
1019 list_for_each_entry(entry, head, entry) {
1020 if (!lock_accessed(entry)) {
1021 unsigned int cq_depth;
1022 mark_lock_accessed(entry, lock);
1023 if (match(entry, data)) {
1024 *target_entry = entry;
1025 ret = 0;
1026 goto exit;
1029 if (__cq_enqueue(cq, (unsigned long)entry)) {
1030 ret = -1;
1031 goto exit;
1033 cq_depth = __cq_get_elem_count(cq);
1034 if (max_bfs_queue_depth < cq_depth)
1035 max_bfs_queue_depth = cq_depth;
1039 exit:
1040 return ret;
1043 static inline int __bfs_forwards(struct lock_list *src_entry,
1044 void *data,
1045 int (*match)(struct lock_list *entry, void *data),
1046 struct lock_list **target_entry)
1048 return __bfs(src_entry, data, match, target_entry, 1);
1052 static inline int __bfs_backwards(struct lock_list *src_entry,
1053 void *data,
1054 int (*match)(struct lock_list *entry, void *data),
1055 struct lock_list **target_entry)
1057 return __bfs(src_entry, data, match, target_entry, 0);
1062 * Recursive, forwards-direction lock-dependency checking, used for
1063 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1064 * checking.
1068 * Print a dependency chain entry (this is only done when a deadlock
1069 * has been detected):
1071 static noinline int
1072 print_circular_bug_entry(struct lock_list *target, int depth)
1074 if (debug_locks_silent)
1075 return 0;
1076 printk("\n-> #%u", depth);
1077 print_lock_name(target->class);
1078 printk(":\n");
1079 print_stack_trace(&target->trace, 6);
1081 return 0;
1084 static void
1085 print_circular_lock_scenario(struct held_lock *src,
1086 struct held_lock *tgt,
1087 struct lock_list *prt)
1089 struct lock_class *source = hlock_class(src);
1090 struct lock_class *target = hlock_class(tgt);
1091 struct lock_class *parent = prt->class;
1094 * A direct locking problem where unsafe_class lock is taken
1095 * directly by safe_class lock, then all we need to show
1096 * is the deadlock scenario, as it is obvious that the
1097 * unsafe lock is taken under the safe lock.
1099 * But if there is a chain instead, where the safe lock takes
1100 * an intermediate lock (middle_class) where this lock is
1101 * not the same as the safe lock, then the lock chain is
1102 * used to describe the problem. Otherwise we would need
1103 * to show a different CPU case for each link in the chain
1104 * from the safe_class lock to the unsafe_class lock.
1106 if (parent != source) {
1107 printk("Chain exists of:\n ");
1108 __print_lock_name(source);
1109 printk(" --> ");
1110 __print_lock_name(parent);
1111 printk(" --> ");
1112 __print_lock_name(target);
1113 printk("\n\n");
1116 printk(" Possible unsafe locking scenario:\n\n");
1117 printk(" CPU0 CPU1\n");
1118 printk(" ---- ----\n");
1119 printk(" lock(");
1120 __print_lock_name(target);
1121 printk(");\n");
1122 printk(" lock(");
1123 __print_lock_name(parent);
1124 printk(");\n");
1125 printk(" lock(");
1126 __print_lock_name(target);
1127 printk(");\n");
1128 printk(" lock(");
1129 __print_lock_name(source);
1130 printk(");\n");
1131 printk("\n *** DEADLOCK ***\n\n");
1135 * When a circular dependency is detected, print the
1136 * header first:
1138 static noinline int
1139 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1140 struct held_lock *check_src,
1141 struct held_lock *check_tgt)
1143 struct task_struct *curr = current;
1145 if (debug_locks_silent)
1146 return 0;
1148 printk("\n=======================================================\n");
1149 printk( "[ INFO: possible circular locking dependency detected ]\n");
1150 print_kernel_version();
1151 printk( "-------------------------------------------------------\n");
1152 printk("%s/%d is trying to acquire lock:\n",
1153 curr->comm, task_pid_nr(curr));
1154 print_lock(check_src);
1155 printk("\nbut task is already holding lock:\n");
1156 print_lock(check_tgt);
1157 printk("\nwhich lock already depends on the new lock.\n\n");
1158 printk("\nthe existing dependency chain (in reverse order) is:\n");
1160 print_circular_bug_entry(entry, depth);
1162 return 0;
1165 static inline int class_equal(struct lock_list *entry, void *data)
1167 return entry->class == data;
1170 static noinline int print_circular_bug(struct lock_list *this,
1171 struct lock_list *target,
1172 struct held_lock *check_src,
1173 struct held_lock *check_tgt)
1175 struct task_struct *curr = current;
1176 struct lock_list *parent;
1177 struct lock_list *first_parent;
1178 int depth;
1180 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1181 return 0;
1183 if (!save_trace(&this->trace))
1184 return 0;
1186 depth = get_lock_depth(target);
1188 print_circular_bug_header(target, depth, check_src, check_tgt);
1190 parent = get_lock_parent(target);
1191 first_parent = parent;
1193 while (parent) {
1194 print_circular_bug_entry(parent, --depth);
1195 parent = get_lock_parent(parent);
1198 printk("\nother info that might help us debug this:\n\n");
1199 print_circular_lock_scenario(check_src, check_tgt,
1200 first_parent);
1202 lockdep_print_held_locks(curr);
1204 printk("\nstack backtrace:\n");
1205 dump_stack();
1207 return 0;
1210 static noinline int print_bfs_bug(int ret)
1212 if (!debug_locks_off_graph_unlock())
1213 return 0;
1216 * Breadth-first-search failed, graph got corrupted?
1218 WARN(1, "lockdep bfs error:%d\n", ret);
1220 return 0;
1223 static int noop_count(struct lock_list *entry, void *data)
1225 (*(unsigned long *)data)++;
1226 return 0;
1229 unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1231 unsigned long count = 0;
1232 struct lock_list *uninitialized_var(target_entry);
1234 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1236 return count;
1238 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1240 unsigned long ret, flags;
1241 struct lock_list this;
1243 this.parent = NULL;
1244 this.class = class;
1246 local_irq_save(flags);
1247 arch_spin_lock(&lockdep_lock);
1248 ret = __lockdep_count_forward_deps(&this);
1249 arch_spin_unlock(&lockdep_lock);
1250 local_irq_restore(flags);
1252 return ret;
1255 unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1257 unsigned long count = 0;
1258 struct lock_list *uninitialized_var(target_entry);
1260 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1262 return count;
1265 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1267 unsigned long ret, flags;
1268 struct lock_list this;
1270 this.parent = NULL;
1271 this.class = class;
1273 local_irq_save(flags);
1274 arch_spin_lock(&lockdep_lock);
1275 ret = __lockdep_count_backward_deps(&this);
1276 arch_spin_unlock(&lockdep_lock);
1277 local_irq_restore(flags);
1279 return ret;
1283 * Prove that the dependency graph starting at <entry> can not
1284 * lead to <target>. Print an error and return 0 if it does.
1286 static noinline int
1287 check_noncircular(struct lock_list *root, struct lock_class *target,
1288 struct lock_list **target_entry)
1290 int result;
1292 debug_atomic_inc(nr_cyclic_checks);
1294 result = __bfs_forwards(root, target, class_equal, target_entry);
1296 return result;
1299 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1301 * Forwards and backwards subgraph searching, for the purposes of
1302 * proving that two subgraphs can be connected by a new dependency
1303 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1306 static inline int usage_match(struct lock_list *entry, void *bit)
1308 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1314 * Find a node in the forwards-direction dependency sub-graph starting
1315 * at @root->class that matches @bit.
1317 * Return 0 if such a node exists in the subgraph, and put that node
1318 * into *@target_entry.
1320 * Return 1 otherwise and keep *@target_entry unchanged.
1321 * Return <0 on error.
1323 static int
1324 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1325 struct lock_list **target_entry)
1327 int result;
1329 debug_atomic_inc(nr_find_usage_forwards_checks);
1331 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1333 return result;
1337 * Find a node in the backwards-direction dependency sub-graph starting
1338 * at @root->class that matches @bit.
1340 * Return 0 if such a node exists in the subgraph, and put that node
1341 * into *@target_entry.
1343 * Return 1 otherwise and keep *@target_entry unchanged.
1344 * Return <0 on error.
1346 static int
1347 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1348 struct lock_list **target_entry)
1350 int result;
1352 debug_atomic_inc(nr_find_usage_backwards_checks);
1354 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1356 return result;
1359 static void print_lock_class_header(struct lock_class *class, int depth)
1361 int bit;
1363 printk("%*s->", depth, "");
1364 print_lock_name(class);
1365 printk(" ops: %lu", class->ops);
1366 printk(" {\n");
1368 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1369 if (class->usage_mask & (1 << bit)) {
1370 int len = depth;
1372 len += printk("%*s %s", depth, "", usage_str[bit]);
1373 len += printk(" at:\n");
1374 print_stack_trace(class->usage_traces + bit, len);
1377 printk("%*s }\n", depth, "");
1379 printk("%*s ... key at: ",depth,"");
1380 print_ip_sym((unsigned long)class->key);
1384 * printk the shortest lock dependencies from @start to @end in reverse order:
1386 static void __used
1387 print_shortest_lock_dependencies(struct lock_list *leaf,
1388 struct lock_list *root)
1390 struct lock_list *entry = leaf;
1391 int depth;
1393 /*compute depth from generated tree by BFS*/
1394 depth = get_lock_depth(leaf);
1396 do {
1397 print_lock_class_header(entry->class, depth);
1398 printk("%*s ... acquired at:\n", depth, "");
1399 print_stack_trace(&entry->trace, 2);
1400 printk("\n");
1402 if (depth == 0 && (entry != root)) {
1403 printk("lockdep:%s bad path found in chain graph\n", __func__);
1404 break;
1407 entry = get_lock_parent(entry);
1408 depth--;
1409 } while (entry && (depth >= 0));
1411 return;
1414 static void
1415 print_irq_lock_scenario(struct lock_list *safe_entry,
1416 struct lock_list *unsafe_entry,
1417 struct lock_class *prev_class,
1418 struct lock_class *next_class)
1420 struct lock_class *safe_class = safe_entry->class;
1421 struct lock_class *unsafe_class = unsafe_entry->class;
1422 struct lock_class *middle_class = prev_class;
1424 if (middle_class == safe_class)
1425 middle_class = next_class;
1428 * A direct locking problem where unsafe_class lock is taken
1429 * directly by safe_class lock, then all we need to show
1430 * is the deadlock scenario, as it is obvious that the
1431 * unsafe lock is taken under the safe lock.
1433 * But if there is a chain instead, where the safe lock takes
1434 * an intermediate lock (middle_class) where this lock is
1435 * not the same as the safe lock, then the lock chain is
1436 * used to describe the problem. Otherwise we would need
1437 * to show a different CPU case for each link in the chain
1438 * from the safe_class lock to the unsafe_class lock.
1440 if (middle_class != unsafe_class) {
1441 printk("Chain exists of:\n ");
1442 __print_lock_name(safe_class);
1443 printk(" --> ");
1444 __print_lock_name(middle_class);
1445 printk(" --> ");
1446 __print_lock_name(unsafe_class);
1447 printk("\n\n");
1450 printk(" Possible interrupt unsafe locking scenario:\n\n");
1451 printk(" CPU0 CPU1\n");
1452 printk(" ---- ----\n");
1453 printk(" lock(");
1454 __print_lock_name(unsafe_class);
1455 printk(");\n");
1456 printk(" local_irq_disable();\n");
1457 printk(" lock(");
1458 __print_lock_name(safe_class);
1459 printk(");\n");
1460 printk(" lock(");
1461 __print_lock_name(middle_class);
1462 printk(");\n");
1463 printk(" <Interrupt>\n");
1464 printk(" lock(");
1465 __print_lock_name(safe_class);
1466 printk(");\n");
1467 printk("\n *** DEADLOCK ***\n\n");
1470 static int
1471 print_bad_irq_dependency(struct task_struct *curr,
1472 struct lock_list *prev_root,
1473 struct lock_list *next_root,
1474 struct lock_list *backwards_entry,
1475 struct lock_list *forwards_entry,
1476 struct held_lock *prev,
1477 struct held_lock *next,
1478 enum lock_usage_bit bit1,
1479 enum lock_usage_bit bit2,
1480 const char *irqclass)
1482 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1483 return 0;
1485 printk("\n======================================================\n");
1486 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1487 irqclass, irqclass);
1488 print_kernel_version();
1489 printk( "------------------------------------------------------\n");
1490 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1491 curr->comm, task_pid_nr(curr),
1492 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1493 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1494 curr->hardirqs_enabled,
1495 curr->softirqs_enabled);
1496 print_lock(next);
1498 printk("\nand this task is already holding:\n");
1499 print_lock(prev);
1500 printk("which would create a new lock dependency:\n");
1501 print_lock_name(hlock_class(prev));
1502 printk(" ->");
1503 print_lock_name(hlock_class(next));
1504 printk("\n");
1506 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1507 irqclass);
1508 print_lock_name(backwards_entry->class);
1509 printk("\n... which became %s-irq-safe at:\n", irqclass);
1511 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1513 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1514 print_lock_name(forwards_entry->class);
1515 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1516 printk("...");
1518 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1520 printk("\nother info that might help us debug this:\n\n");
1521 print_irq_lock_scenario(backwards_entry, forwards_entry,
1522 hlock_class(prev), hlock_class(next));
1524 lockdep_print_held_locks(curr);
1526 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1527 printk(" and the holding lock:\n");
1528 if (!save_trace(&prev_root->trace))
1529 return 0;
1530 print_shortest_lock_dependencies(backwards_entry, prev_root);
1532 printk("\nthe dependencies between the lock to be acquired");
1533 printk(" and %s-irq-unsafe lock:\n", irqclass);
1534 if (!save_trace(&next_root->trace))
1535 return 0;
1536 print_shortest_lock_dependencies(forwards_entry, next_root);
1538 printk("\nstack backtrace:\n");
1539 dump_stack();
1541 return 0;
1544 static int
1545 check_usage(struct task_struct *curr, struct held_lock *prev,
1546 struct held_lock *next, enum lock_usage_bit bit_backwards,
1547 enum lock_usage_bit bit_forwards, const char *irqclass)
1549 int ret;
1550 struct lock_list this, that;
1551 struct lock_list *uninitialized_var(target_entry);
1552 struct lock_list *uninitialized_var(target_entry1);
1554 this.parent = NULL;
1556 this.class = hlock_class(prev);
1557 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1558 if (ret < 0)
1559 return print_bfs_bug(ret);
1560 if (ret == 1)
1561 return ret;
1563 that.parent = NULL;
1564 that.class = hlock_class(next);
1565 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1566 if (ret < 0)
1567 return print_bfs_bug(ret);
1568 if (ret == 1)
1569 return ret;
1571 return print_bad_irq_dependency(curr, &this, &that,
1572 target_entry, target_entry1,
1573 prev, next,
1574 bit_backwards, bit_forwards, irqclass);
1577 static const char *state_names[] = {
1578 #define LOCKDEP_STATE(__STATE) \
1579 __stringify(__STATE),
1580 #include "lockdep_states.h"
1581 #undef LOCKDEP_STATE
1584 static const char *state_rnames[] = {
1585 #define LOCKDEP_STATE(__STATE) \
1586 __stringify(__STATE)"-READ",
1587 #include "lockdep_states.h"
1588 #undef LOCKDEP_STATE
1591 static inline const char *state_name(enum lock_usage_bit bit)
1593 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1596 static int exclusive_bit(int new_bit)
1599 * USED_IN
1600 * USED_IN_READ
1601 * ENABLED
1602 * ENABLED_READ
1604 * bit 0 - write/read
1605 * bit 1 - used_in/enabled
1606 * bit 2+ state
1609 int state = new_bit & ~3;
1610 int dir = new_bit & 2;
1613 * keep state, bit flip the direction and strip read.
1615 return state | (dir ^ 2);
1618 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1619 struct held_lock *next, enum lock_usage_bit bit)
1622 * Prove that the new dependency does not connect a hardirq-safe
1623 * lock with a hardirq-unsafe lock - to achieve this we search
1624 * the backwards-subgraph starting at <prev>, and the
1625 * forwards-subgraph starting at <next>:
1627 if (!check_usage(curr, prev, next, bit,
1628 exclusive_bit(bit), state_name(bit)))
1629 return 0;
1631 bit++; /* _READ */
1634 * Prove that the new dependency does not connect a hardirq-safe-read
1635 * lock with a hardirq-unsafe lock - to achieve this we search
1636 * the backwards-subgraph starting at <prev>, and the
1637 * forwards-subgraph starting at <next>:
1639 if (!check_usage(curr, prev, next, bit,
1640 exclusive_bit(bit), state_name(bit)))
1641 return 0;
1643 return 1;
1646 static int
1647 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1648 struct held_lock *next)
1650 #define LOCKDEP_STATE(__STATE) \
1651 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1652 return 0;
1653 #include "lockdep_states.h"
1654 #undef LOCKDEP_STATE
1656 return 1;
1659 static void inc_chains(void)
1661 if (current->hardirq_context)
1662 nr_hardirq_chains++;
1663 else {
1664 if (current->softirq_context)
1665 nr_softirq_chains++;
1666 else
1667 nr_process_chains++;
1671 #else
1673 static inline int
1674 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1675 struct held_lock *next)
1677 return 1;
1680 static inline void inc_chains(void)
1682 nr_process_chains++;
1685 #endif
1687 static void
1688 print_deadlock_scenario(struct held_lock *nxt,
1689 struct held_lock *prv)
1691 struct lock_class *next = hlock_class(nxt);
1692 struct lock_class *prev = hlock_class(prv);
1694 printk(" Possible unsafe locking scenario:\n\n");
1695 printk(" CPU0\n");
1696 printk(" ----\n");
1697 printk(" lock(");
1698 __print_lock_name(prev);
1699 printk(");\n");
1700 printk(" lock(");
1701 __print_lock_name(next);
1702 printk(");\n");
1703 printk("\n *** DEADLOCK ***\n\n");
1704 printk(" May be due to missing lock nesting notation\n\n");
1707 static int
1708 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1709 struct held_lock *next)
1711 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1712 return 0;
1714 printk("\n=============================================\n");
1715 printk( "[ INFO: possible recursive locking detected ]\n");
1716 print_kernel_version();
1717 printk( "---------------------------------------------\n");
1718 printk("%s/%d is trying to acquire lock:\n",
1719 curr->comm, task_pid_nr(curr));
1720 print_lock(next);
1721 printk("\nbut task is already holding lock:\n");
1722 print_lock(prev);
1724 printk("\nother info that might help us debug this:\n");
1725 print_deadlock_scenario(next, prev);
1726 lockdep_print_held_locks(curr);
1728 printk("\nstack backtrace:\n");
1729 dump_stack();
1731 return 0;
1735 * Check whether we are holding such a class already.
1737 * (Note that this has to be done separately, because the graph cannot
1738 * detect such classes of deadlocks.)
1740 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1742 static int
1743 check_deadlock(struct task_struct *curr, struct held_lock *next,
1744 struct lockdep_map *next_instance, int read)
1746 struct held_lock *prev;
1747 struct held_lock *nest = NULL;
1748 int i;
1750 for (i = 0; i < curr->lockdep_depth; i++) {
1751 prev = curr->held_locks + i;
1753 if (prev->instance == next->nest_lock)
1754 nest = prev;
1756 if (hlock_class(prev) != hlock_class(next))
1757 continue;
1760 * Allow read-after-read recursion of the same
1761 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1763 if ((read == 2) && prev->read)
1764 return 2;
1767 * We're holding the nest_lock, which serializes this lock's
1768 * nesting behaviour.
1770 if (nest)
1771 return 2;
1773 return print_deadlock_bug(curr, prev, next);
1775 return 1;
1779 * There was a chain-cache miss, and we are about to add a new dependency
1780 * to a previous lock. We recursively validate the following rules:
1782 * - would the adding of the <prev> -> <next> dependency create a
1783 * circular dependency in the graph? [== circular deadlock]
1785 * - does the new prev->next dependency connect any hardirq-safe lock
1786 * (in the full backwards-subgraph starting at <prev>) with any
1787 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1788 * <next>)? [== illegal lock inversion with hardirq contexts]
1790 * - does the new prev->next dependency connect any softirq-safe lock
1791 * (in the full backwards-subgraph starting at <prev>) with any
1792 * softirq-unsafe lock (in the full forwards-subgraph starting at
1793 * <next>)? [== illegal lock inversion with softirq contexts]
1795 * any of these scenarios could lead to a deadlock.
1797 * Then if all the validations pass, we add the forwards and backwards
1798 * dependency.
1800 static int
1801 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1802 struct held_lock *next, int distance, int trylock_loop)
1804 struct lock_list *entry;
1805 int ret;
1806 struct lock_list this;
1807 struct lock_list *uninitialized_var(target_entry);
1809 * Static variable, serialized by the graph_lock().
1811 * We use this static variable to save the stack trace in case
1812 * we call into this function multiple times due to encountering
1813 * trylocks in the held lock stack.
1815 static struct stack_trace trace;
1818 * Prove that the new <prev> -> <next> dependency would not
1819 * create a circular dependency in the graph. (We do this by
1820 * forward-recursing into the graph starting at <next>, and
1821 * checking whether we can reach <prev>.)
1823 * We are using global variables to control the recursion, to
1824 * keep the stackframe size of the recursive functions low:
1826 this.class = hlock_class(next);
1827 this.parent = NULL;
1828 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1829 if (unlikely(!ret))
1830 return print_circular_bug(&this, target_entry, next, prev);
1831 else if (unlikely(ret < 0))
1832 return print_bfs_bug(ret);
1834 if (!check_prev_add_irq(curr, prev, next))
1835 return 0;
1838 * For recursive read-locks we do all the dependency checks,
1839 * but we dont store read-triggered dependencies (only
1840 * write-triggered dependencies). This ensures that only the
1841 * write-side dependencies matter, and that if for example a
1842 * write-lock never takes any other locks, then the reads are
1843 * equivalent to a NOP.
1845 if (next->read == 2 || prev->read == 2)
1846 return 1;
1848 * Is the <prev> -> <next> dependency already present?
1850 * (this may occur even though this is a new chain: consider
1851 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1852 * chains - the second one will be new, but L1 already has
1853 * L2 added to its dependency list, due to the first chain.)
1855 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1856 if (entry->class == hlock_class(next)) {
1857 if (distance == 1)
1858 entry->distance = 1;
1859 return 2;
1863 if (!trylock_loop && !save_trace(&trace))
1864 return 0;
1867 * Ok, all validations passed, add the new lock
1868 * to the previous lock's dependency list:
1870 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1871 &hlock_class(prev)->locks_after,
1872 next->acquire_ip, distance, &trace);
1874 if (!ret)
1875 return 0;
1877 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1878 &hlock_class(next)->locks_before,
1879 next->acquire_ip, distance, &trace);
1880 if (!ret)
1881 return 0;
1884 * Debugging printouts:
1886 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1887 graph_unlock();
1888 printk("\n new dependency: ");
1889 print_lock_name(hlock_class(prev));
1890 printk(" => ");
1891 print_lock_name(hlock_class(next));
1892 printk("\n");
1893 dump_stack();
1894 return graph_lock();
1896 return 1;
1900 * Add the dependency to all directly-previous locks that are 'relevant'.
1901 * The ones that are relevant are (in increasing distance from curr):
1902 * all consecutive trylock entries and the final non-trylock entry - or
1903 * the end of this context's lock-chain - whichever comes first.
1905 static int
1906 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1908 int depth = curr->lockdep_depth;
1909 int trylock_loop = 0;
1910 struct held_lock *hlock;
1913 * Debugging checks.
1915 * Depth must not be zero for a non-head lock:
1917 if (!depth)
1918 goto out_bug;
1920 * At least two relevant locks must exist for this
1921 * to be a head:
1923 if (curr->held_locks[depth].irq_context !=
1924 curr->held_locks[depth-1].irq_context)
1925 goto out_bug;
1927 for (;;) {
1928 int distance = curr->lockdep_depth - depth + 1;
1929 hlock = curr->held_locks + depth-1;
1931 * Only non-recursive-read entries get new dependencies
1932 * added:
1934 if (hlock->read != 2) {
1935 if (!check_prev_add(curr, hlock, next,
1936 distance, trylock_loop))
1937 return 0;
1939 * Stop after the first non-trylock entry,
1940 * as non-trylock entries have added their
1941 * own direct dependencies already, so this
1942 * lock is connected to them indirectly:
1944 if (!hlock->trylock)
1945 break;
1947 depth--;
1949 * End of lock-stack?
1951 if (!depth)
1952 break;
1954 * Stop the search if we cross into another context:
1956 if (curr->held_locks[depth].irq_context !=
1957 curr->held_locks[depth-1].irq_context)
1958 break;
1959 trylock_loop = 1;
1961 return 1;
1962 out_bug:
1963 if (!debug_locks_off_graph_unlock())
1964 return 0;
1967 * Clearly we all shouldn't be here, but since we made it we
1968 * can reliable say we messed up our state. See the above two
1969 * gotos for reasons why we could possibly end up here.
1971 WARN_ON(1);
1973 return 0;
1976 unsigned long nr_lock_chains;
1977 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1978 int nr_chain_hlocks;
1979 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1981 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1983 return lock_classes + chain_hlocks[chain->base + i];
1987 * Look up a dependency chain. If the key is not present yet then
1988 * add it and return 1 - in this case the new dependency chain is
1989 * validated. If the key is already hashed, return 0.
1990 * (On return with 1 graph_lock is held.)
1992 static inline int lookup_chain_cache(struct task_struct *curr,
1993 struct held_lock *hlock,
1994 u64 chain_key)
1996 struct lock_class *class = hlock_class(hlock);
1997 struct list_head *hash_head = chainhashentry(chain_key);
1998 struct lock_chain *chain;
1999 struct held_lock *hlock_curr, *hlock_next;
2000 int i, j;
2003 * We might need to take the graph lock, ensure we've got IRQs
2004 * disabled to make this an IRQ-safe lock.. for recursion reasons
2005 * lockdep won't complain about its own locking errors.
2007 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2008 return 0;
2010 * We can walk it lock-free, because entries only get added
2011 * to the hash:
2013 list_for_each_entry(chain, hash_head, entry) {
2014 if (chain->chain_key == chain_key) {
2015 cache_hit:
2016 debug_atomic_inc(chain_lookup_hits);
2017 if (very_verbose(class))
2018 printk("\nhash chain already cached, key: "
2019 "%016Lx tail class: [%p] %s\n",
2020 (unsigned long long)chain_key,
2021 class->key, class->name);
2022 return 0;
2025 if (very_verbose(class))
2026 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2027 (unsigned long long)chain_key, class->key, class->name);
2029 * Allocate a new chain entry from the static array, and add
2030 * it to the hash:
2032 if (!graph_lock())
2033 return 0;
2035 * We have to walk the chain again locked - to avoid duplicates:
2037 list_for_each_entry(chain, hash_head, entry) {
2038 if (chain->chain_key == chain_key) {
2039 graph_unlock();
2040 goto cache_hit;
2043 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2044 if (!debug_locks_off_graph_unlock())
2045 return 0;
2047 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
2048 printk("turning off the locking correctness validator.\n");
2049 dump_stack();
2050 return 0;
2052 chain = lock_chains + nr_lock_chains++;
2053 chain->chain_key = chain_key;
2054 chain->irq_context = hlock->irq_context;
2055 /* Find the first held_lock of current chain */
2056 hlock_next = hlock;
2057 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2058 hlock_curr = curr->held_locks + i;
2059 if (hlock_curr->irq_context != hlock_next->irq_context)
2060 break;
2061 hlock_next = hlock;
2063 i++;
2064 chain->depth = curr->lockdep_depth + 1 - i;
2065 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2066 chain->base = nr_chain_hlocks;
2067 nr_chain_hlocks += chain->depth;
2068 for (j = 0; j < chain->depth - 1; j++, i++) {
2069 int lock_id = curr->held_locks[i].class_idx - 1;
2070 chain_hlocks[chain->base + j] = lock_id;
2072 chain_hlocks[chain->base + j] = class - lock_classes;
2074 list_add_tail_rcu(&chain->entry, hash_head);
2075 debug_atomic_inc(chain_lookup_misses);
2076 inc_chains();
2078 return 1;
2081 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2082 struct held_lock *hlock, int chain_head, u64 chain_key)
2085 * Trylock needs to maintain the stack of held locks, but it
2086 * does not add new dependencies, because trylock can be done
2087 * in any order.
2089 * We look up the chain_key and do the O(N^2) check and update of
2090 * the dependencies only if this is a new dependency chain.
2091 * (If lookup_chain_cache() returns with 1 it acquires
2092 * graph_lock for us)
2094 if (!hlock->trylock && (hlock->check == 2) &&
2095 lookup_chain_cache(curr, hlock, chain_key)) {
2097 * Check whether last held lock:
2099 * - is irq-safe, if this lock is irq-unsafe
2100 * - is softirq-safe, if this lock is hardirq-unsafe
2102 * And check whether the new lock's dependency graph
2103 * could lead back to the previous lock.
2105 * any of these scenarios could lead to a deadlock. If
2106 * All validations
2108 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2110 if (!ret)
2111 return 0;
2113 * Mark recursive read, as we jump over it when
2114 * building dependencies (just like we jump over
2115 * trylock entries):
2117 if (ret == 2)
2118 hlock->read = 2;
2120 * Add dependency only if this lock is not the head
2121 * of the chain, and if it's not a secondary read-lock:
2123 if (!chain_head && ret != 2)
2124 if (!check_prevs_add(curr, hlock))
2125 return 0;
2126 graph_unlock();
2127 } else
2128 /* after lookup_chain_cache(): */
2129 if (unlikely(!debug_locks))
2130 return 0;
2132 return 1;
2134 #else
2135 static inline int validate_chain(struct task_struct *curr,
2136 struct lockdep_map *lock, struct held_lock *hlock,
2137 int chain_head, u64 chain_key)
2139 return 1;
2141 #endif
2144 * We are building curr_chain_key incrementally, so double-check
2145 * it from scratch, to make sure that it's done correctly:
2147 static void check_chain_key(struct task_struct *curr)
2149 #ifdef CONFIG_DEBUG_LOCKDEP
2150 struct held_lock *hlock, *prev_hlock = NULL;
2151 unsigned int i, id;
2152 u64 chain_key = 0;
2154 for (i = 0; i < curr->lockdep_depth; i++) {
2155 hlock = curr->held_locks + i;
2156 if (chain_key != hlock->prev_chain_key) {
2157 debug_locks_off();
2159 * We got mighty confused, our chain keys don't match
2160 * with what we expect, someone trample on our task state?
2162 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2163 curr->lockdep_depth, i,
2164 (unsigned long long)chain_key,
2165 (unsigned long long)hlock->prev_chain_key);
2166 return;
2168 id = hlock->class_idx - 1;
2170 * Whoops ran out of static storage again?
2172 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2173 return;
2175 if (prev_hlock && (prev_hlock->irq_context !=
2176 hlock->irq_context))
2177 chain_key = 0;
2178 chain_key = iterate_chain_key(chain_key, id);
2179 prev_hlock = hlock;
2181 if (chain_key != curr->curr_chain_key) {
2182 debug_locks_off();
2184 * More smoking hash instead of calculating it, damn see these
2185 * numbers float.. I bet that a pink elephant stepped on my memory.
2187 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2188 curr->lockdep_depth, i,
2189 (unsigned long long)chain_key,
2190 (unsigned long long)curr->curr_chain_key);
2192 #endif
2195 static void
2196 print_usage_bug_scenario(struct held_lock *lock)
2198 struct lock_class *class = hlock_class(lock);
2200 printk(" Possible unsafe locking scenario:\n\n");
2201 printk(" CPU0\n");
2202 printk(" ----\n");
2203 printk(" lock(");
2204 __print_lock_name(class);
2205 printk(");\n");
2206 printk(" <Interrupt>\n");
2207 printk(" lock(");
2208 __print_lock_name(class);
2209 printk(");\n");
2210 printk("\n *** DEADLOCK ***\n\n");
2213 static int
2214 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2215 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2217 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2218 return 0;
2220 printk("\n=================================\n");
2221 printk( "[ INFO: inconsistent lock state ]\n");
2222 print_kernel_version();
2223 printk( "---------------------------------\n");
2225 printk("inconsistent {%s} -> {%s} usage.\n",
2226 usage_str[prev_bit], usage_str[new_bit]);
2228 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2229 curr->comm, task_pid_nr(curr),
2230 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2231 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2232 trace_hardirqs_enabled(curr),
2233 trace_softirqs_enabled(curr));
2234 print_lock(this);
2236 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
2237 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2239 print_irqtrace_events(curr);
2240 printk("\nother info that might help us debug this:\n");
2241 print_usage_bug_scenario(this);
2243 lockdep_print_held_locks(curr);
2245 printk("\nstack backtrace:\n");
2246 dump_stack();
2248 return 0;
2252 * Print out an error if an invalid bit is set:
2254 static inline int
2255 valid_state(struct task_struct *curr, struct held_lock *this,
2256 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2258 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2259 return print_usage_bug(curr, this, bad_bit, new_bit);
2260 return 1;
2263 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2264 enum lock_usage_bit new_bit);
2266 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2269 * print irq inversion bug:
2271 static int
2272 print_irq_inversion_bug(struct task_struct *curr,
2273 struct lock_list *root, struct lock_list *other,
2274 struct held_lock *this, int forwards,
2275 const char *irqclass)
2277 struct lock_list *entry = other;
2278 struct lock_list *middle = NULL;
2279 int depth;
2281 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2282 return 0;
2284 printk("\n=========================================================\n");
2285 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2286 print_kernel_version();
2287 printk( "---------------------------------------------------------\n");
2288 printk("%s/%d just changed the state of lock:\n",
2289 curr->comm, task_pid_nr(curr));
2290 print_lock(this);
2291 if (forwards)
2292 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2293 else
2294 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2295 print_lock_name(other->class);
2296 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2298 printk("\nother info that might help us debug this:\n");
2300 /* Find a middle lock (if one exists) */
2301 depth = get_lock_depth(other);
2302 do {
2303 if (depth == 0 && (entry != root)) {
2304 printk("lockdep:%s bad path found in chain graph\n", __func__);
2305 break;
2307 middle = entry;
2308 entry = get_lock_parent(entry);
2309 depth--;
2310 } while (entry && entry != root && (depth >= 0));
2311 if (forwards)
2312 print_irq_lock_scenario(root, other,
2313 middle ? middle->class : root->class, other->class);
2314 else
2315 print_irq_lock_scenario(other, root,
2316 middle ? middle->class : other->class, root->class);
2318 lockdep_print_held_locks(curr);
2320 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2321 if (!save_trace(&root->trace))
2322 return 0;
2323 print_shortest_lock_dependencies(other, root);
2325 printk("\nstack backtrace:\n");
2326 dump_stack();
2328 return 0;
2332 * Prove that in the forwards-direction subgraph starting at <this>
2333 * there is no lock matching <mask>:
2335 static int
2336 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2337 enum lock_usage_bit bit, const char *irqclass)
2339 int ret;
2340 struct lock_list root;
2341 struct lock_list *uninitialized_var(target_entry);
2343 root.parent = NULL;
2344 root.class = hlock_class(this);
2345 ret = find_usage_forwards(&root, bit, &target_entry);
2346 if (ret < 0)
2347 return print_bfs_bug(ret);
2348 if (ret == 1)
2349 return ret;
2351 return print_irq_inversion_bug(curr, &root, target_entry,
2352 this, 1, irqclass);
2356 * Prove that in the backwards-direction subgraph starting at <this>
2357 * there is no lock matching <mask>:
2359 static int
2360 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2361 enum lock_usage_bit bit, const char *irqclass)
2363 int ret;
2364 struct lock_list root;
2365 struct lock_list *uninitialized_var(target_entry);
2367 root.parent = NULL;
2368 root.class = hlock_class(this);
2369 ret = find_usage_backwards(&root, bit, &target_entry);
2370 if (ret < 0)
2371 return print_bfs_bug(ret);
2372 if (ret == 1)
2373 return ret;
2375 return print_irq_inversion_bug(curr, &root, target_entry,
2376 this, 0, irqclass);
2379 void print_irqtrace_events(struct task_struct *curr)
2381 printk("irq event stamp: %u\n", curr->irq_events);
2382 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2383 print_ip_sym(curr->hardirq_enable_ip);
2384 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2385 print_ip_sym(curr->hardirq_disable_ip);
2386 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2387 print_ip_sym(curr->softirq_enable_ip);
2388 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2389 print_ip_sym(curr->softirq_disable_ip);
2392 static int HARDIRQ_verbose(struct lock_class *class)
2394 #if HARDIRQ_VERBOSE
2395 return class_filter(class);
2396 #endif
2397 return 0;
2400 static int SOFTIRQ_verbose(struct lock_class *class)
2402 #if SOFTIRQ_VERBOSE
2403 return class_filter(class);
2404 #endif
2405 return 0;
2408 static int RECLAIM_FS_verbose(struct lock_class *class)
2410 #if RECLAIM_VERBOSE
2411 return class_filter(class);
2412 #endif
2413 return 0;
2416 #define STRICT_READ_CHECKS 1
2418 static int (*state_verbose_f[])(struct lock_class *class) = {
2419 #define LOCKDEP_STATE(__STATE) \
2420 __STATE##_verbose,
2421 #include "lockdep_states.h"
2422 #undef LOCKDEP_STATE
2425 static inline int state_verbose(enum lock_usage_bit bit,
2426 struct lock_class *class)
2428 return state_verbose_f[bit >> 2](class);
2431 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2432 enum lock_usage_bit bit, const char *name);
2434 static int
2435 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2436 enum lock_usage_bit new_bit)
2438 int excl_bit = exclusive_bit(new_bit);
2439 int read = new_bit & 1;
2440 int dir = new_bit & 2;
2443 * mark USED_IN has to look forwards -- to ensure no dependency
2444 * has ENABLED state, which would allow recursion deadlocks.
2446 * mark ENABLED has to look backwards -- to ensure no dependee
2447 * has USED_IN state, which, again, would allow recursion deadlocks.
2449 check_usage_f usage = dir ?
2450 check_usage_backwards : check_usage_forwards;
2453 * Validate that this particular lock does not have conflicting
2454 * usage states.
2456 if (!valid_state(curr, this, new_bit, excl_bit))
2457 return 0;
2460 * Validate that the lock dependencies don't have conflicting usage
2461 * states.
2463 if ((!read || !dir || STRICT_READ_CHECKS) &&
2464 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2465 return 0;
2468 * Check for read in write conflicts
2470 if (!read) {
2471 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2472 return 0;
2474 if (STRICT_READ_CHECKS &&
2475 !usage(curr, this, excl_bit + 1,
2476 state_name(new_bit + 1)))
2477 return 0;
2480 if (state_verbose(new_bit, hlock_class(this)))
2481 return 2;
2483 return 1;
2486 enum mark_type {
2487 #define LOCKDEP_STATE(__STATE) __STATE,
2488 #include "lockdep_states.h"
2489 #undef LOCKDEP_STATE
2493 * Mark all held locks with a usage bit:
2495 static int
2496 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2498 enum lock_usage_bit usage_bit;
2499 struct held_lock *hlock;
2500 int i;
2502 for (i = 0; i < curr->lockdep_depth; i++) {
2503 hlock = curr->held_locks + i;
2505 usage_bit = 2 + (mark << 2); /* ENABLED */
2506 if (hlock->read)
2507 usage_bit += 1; /* READ */
2509 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2511 if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
2512 continue;
2514 if (!mark_lock(curr, hlock, usage_bit))
2515 return 0;
2518 return 1;
2522 * Hardirqs will be enabled:
2524 static void __trace_hardirqs_on_caller(unsigned long ip)
2526 struct task_struct *curr = current;
2528 /* we'll do an OFF -> ON transition: */
2529 curr->hardirqs_enabled = 1;
2532 * We are going to turn hardirqs on, so set the
2533 * usage bit for all held locks:
2535 if (!mark_held_locks(curr, HARDIRQ))
2536 return;
2538 * If we have softirqs enabled, then set the usage
2539 * bit for all held locks. (disabled hardirqs prevented
2540 * this bit from being set before)
2542 if (curr->softirqs_enabled)
2543 if (!mark_held_locks(curr, SOFTIRQ))
2544 return;
2546 curr->hardirq_enable_ip = ip;
2547 curr->hardirq_enable_event = ++curr->irq_events;
2548 debug_atomic_inc(hardirqs_on_events);
2551 void trace_hardirqs_on_caller(unsigned long ip)
2553 time_hardirqs_on(CALLER_ADDR0, ip);
2555 if (unlikely(!debug_locks || current->lockdep_recursion))
2556 return;
2558 if (unlikely(current->hardirqs_enabled)) {
2560 * Neither irq nor preemption are disabled here
2561 * so this is racy by nature but losing one hit
2562 * in a stat is not a big deal.
2564 __debug_atomic_inc(redundant_hardirqs_on);
2565 return;
2569 * We're enabling irqs and according to our state above irqs weren't
2570 * already enabled, yet we find the hardware thinks they are in fact
2571 * enabled.. someone messed up their IRQ state tracing.
2573 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2574 return;
2577 * See the fine text that goes along with this variable definition.
2579 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2580 return;
2583 * Can't allow enabling interrupts while in an interrupt handler,
2584 * that's general bad form and such. Recursion, limited stack etc..
2586 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2587 return;
2589 current->lockdep_recursion = 1;
2590 __trace_hardirqs_on_caller(ip);
2591 current->lockdep_recursion = 0;
2593 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2595 void trace_hardirqs_on(void)
2597 trace_hardirqs_on_caller(CALLER_ADDR0);
2599 EXPORT_SYMBOL(trace_hardirqs_on);
2602 * Hardirqs were disabled:
2604 void trace_hardirqs_off_caller(unsigned long ip)
2606 struct task_struct *curr = current;
2608 time_hardirqs_off(CALLER_ADDR0, ip);
2610 if (unlikely(!debug_locks || current->lockdep_recursion))
2611 return;
2614 * So we're supposed to get called after you mask local IRQs, but for
2615 * some reason the hardware doesn't quite think you did a proper job.
2617 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2618 return;
2620 if (curr->hardirqs_enabled) {
2622 * We have done an ON -> OFF transition:
2624 curr->hardirqs_enabled = 0;
2625 curr->hardirq_disable_ip = ip;
2626 curr->hardirq_disable_event = ++curr->irq_events;
2627 debug_atomic_inc(hardirqs_off_events);
2628 } else
2629 debug_atomic_inc(redundant_hardirqs_off);
2631 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2633 void trace_hardirqs_off(void)
2635 trace_hardirqs_off_caller(CALLER_ADDR0);
2637 EXPORT_SYMBOL(trace_hardirqs_off);
2640 * Softirqs will be enabled:
2642 void trace_softirqs_on(unsigned long ip)
2644 struct task_struct *curr = current;
2646 if (unlikely(!debug_locks || current->lockdep_recursion))
2647 return;
2650 * We fancy IRQs being disabled here, see softirq.c, avoids
2651 * funny state and nesting things.
2653 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2654 return;
2656 if (curr->softirqs_enabled) {
2657 debug_atomic_inc(redundant_softirqs_on);
2658 return;
2661 current->lockdep_recursion = 1;
2663 * We'll do an OFF -> ON transition:
2665 curr->softirqs_enabled = 1;
2666 curr->softirq_enable_ip = ip;
2667 curr->softirq_enable_event = ++curr->irq_events;
2668 debug_atomic_inc(softirqs_on_events);
2670 * We are going to turn softirqs on, so set the
2671 * usage bit for all held locks, if hardirqs are
2672 * enabled too:
2674 if (curr->hardirqs_enabled)
2675 mark_held_locks(curr, SOFTIRQ);
2676 current->lockdep_recursion = 0;
2680 * Softirqs were disabled:
2682 void trace_softirqs_off(unsigned long ip)
2684 struct task_struct *curr = current;
2686 if (unlikely(!debug_locks || current->lockdep_recursion))
2687 return;
2690 * We fancy IRQs being disabled here, see softirq.c
2692 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2693 return;
2695 if (curr->softirqs_enabled) {
2697 * We have done an ON -> OFF transition:
2699 curr->softirqs_enabled = 0;
2700 curr->softirq_disable_ip = ip;
2701 curr->softirq_disable_event = ++curr->irq_events;
2702 debug_atomic_inc(softirqs_off_events);
2704 * Whoops, we wanted softirqs off, so why aren't they?
2706 DEBUG_LOCKS_WARN_ON(!softirq_count());
2707 } else
2708 debug_atomic_inc(redundant_softirqs_off);
2711 static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2713 struct task_struct *curr = current;
2715 if (unlikely(!debug_locks))
2716 return;
2718 /* no reclaim without waiting on it */
2719 if (!(gfp_mask & __GFP_WAIT))
2720 return;
2722 /* this guy won't enter reclaim */
2723 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2724 return;
2726 /* We're only interested __GFP_FS allocations for now */
2727 if (!(gfp_mask & __GFP_FS))
2728 return;
2731 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2733 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2734 return;
2736 mark_held_locks(curr, RECLAIM_FS);
2739 static void check_flags(unsigned long flags);
2741 void lockdep_trace_alloc(gfp_t gfp_mask)
2743 unsigned long flags;
2745 if (unlikely(current->lockdep_recursion))
2746 return;
2748 raw_local_irq_save(flags);
2749 check_flags(flags);
2750 current->lockdep_recursion = 1;
2751 __lockdep_trace_alloc(gfp_mask, flags);
2752 current->lockdep_recursion = 0;
2753 raw_local_irq_restore(flags);
2756 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2759 * If non-trylock use in a hardirq or softirq context, then
2760 * mark the lock as used in these contexts:
2762 if (!hlock->trylock) {
2763 if (hlock->read) {
2764 if (curr->hardirq_context)
2765 if (!mark_lock(curr, hlock,
2766 LOCK_USED_IN_HARDIRQ_READ))
2767 return 0;
2768 if (curr->softirq_context)
2769 if (!mark_lock(curr, hlock,
2770 LOCK_USED_IN_SOFTIRQ_READ))
2771 return 0;
2772 } else {
2773 if (curr->hardirq_context)
2774 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2775 return 0;
2776 if (curr->softirq_context)
2777 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2778 return 0;
2781 if (!hlock->hardirqs_off) {
2782 if (hlock->read) {
2783 if (!mark_lock(curr, hlock,
2784 LOCK_ENABLED_HARDIRQ_READ))
2785 return 0;
2786 if (curr->softirqs_enabled)
2787 if (!mark_lock(curr, hlock,
2788 LOCK_ENABLED_SOFTIRQ_READ))
2789 return 0;
2790 } else {
2791 if (!mark_lock(curr, hlock,
2792 LOCK_ENABLED_HARDIRQ))
2793 return 0;
2794 if (curr->softirqs_enabled)
2795 if (!mark_lock(curr, hlock,
2796 LOCK_ENABLED_SOFTIRQ))
2797 return 0;
2802 * We reuse the irq context infrastructure more broadly as a general
2803 * context checking code. This tests GFP_FS recursion (a lock taken
2804 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2805 * allocation).
2807 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2808 if (hlock->read) {
2809 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2810 return 0;
2811 } else {
2812 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2813 return 0;
2817 return 1;
2820 static int separate_irq_context(struct task_struct *curr,
2821 struct held_lock *hlock)
2823 unsigned int depth = curr->lockdep_depth;
2826 * Keep track of points where we cross into an interrupt context:
2828 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2829 curr->softirq_context;
2830 if (depth) {
2831 struct held_lock *prev_hlock;
2833 prev_hlock = curr->held_locks + depth-1;
2835 * If we cross into another context, reset the
2836 * hash key (this also prevents the checking and the
2837 * adding of the dependency to 'prev'):
2839 if (prev_hlock->irq_context != hlock->irq_context)
2840 return 1;
2842 return 0;
2845 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2847 static inline
2848 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2849 enum lock_usage_bit new_bit)
2851 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
2852 return 1;
2855 static inline int mark_irqflags(struct task_struct *curr,
2856 struct held_lock *hlock)
2858 return 1;
2861 static inline int separate_irq_context(struct task_struct *curr,
2862 struct held_lock *hlock)
2864 return 0;
2867 void lockdep_trace_alloc(gfp_t gfp_mask)
2871 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2874 * Mark a lock with a usage bit, and validate the state transition:
2876 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2877 enum lock_usage_bit new_bit)
2879 unsigned int new_mask = 1 << new_bit, ret = 1;
2882 * If already set then do not dirty the cacheline,
2883 * nor do any checks:
2885 if (likely(hlock_class(this)->usage_mask & new_mask))
2886 return 1;
2888 if (!graph_lock())
2889 return 0;
2891 * Make sure we didn't race:
2893 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2894 graph_unlock();
2895 return 1;
2898 hlock_class(this)->usage_mask |= new_mask;
2900 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2901 return 0;
2903 switch (new_bit) {
2904 #define LOCKDEP_STATE(__STATE) \
2905 case LOCK_USED_IN_##__STATE: \
2906 case LOCK_USED_IN_##__STATE##_READ: \
2907 case LOCK_ENABLED_##__STATE: \
2908 case LOCK_ENABLED_##__STATE##_READ:
2909 #include "lockdep_states.h"
2910 #undef LOCKDEP_STATE
2911 ret = mark_lock_irq(curr, this, new_bit);
2912 if (!ret)
2913 return 0;
2914 break;
2915 case LOCK_USED:
2916 debug_atomic_dec(nr_unused_locks);
2917 break;
2918 default:
2919 if (!debug_locks_off_graph_unlock())
2920 return 0;
2921 WARN_ON(1);
2922 return 0;
2925 graph_unlock();
2928 * We must printk outside of the graph_lock:
2930 if (ret == 2) {
2931 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2932 print_lock(this);
2933 print_irqtrace_events(curr);
2934 dump_stack();
2937 return ret;
2941 * Initialize a lock instance's lock-class mapping info:
2943 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2944 struct lock_class_key *key, int subclass)
2946 memset(lock, 0, sizeof(*lock));
2948 #ifdef CONFIG_LOCK_STAT
2949 lock->cpu = raw_smp_processor_id();
2950 #endif
2953 * Can't be having no nameless bastards around this place!
2955 if (DEBUG_LOCKS_WARN_ON(!name)) {
2956 lock->name = "NULL";
2957 return;
2960 lock->name = name;
2963 * No key, no joy, we need to hash something.
2965 if (DEBUG_LOCKS_WARN_ON(!key))
2966 return;
2968 * Sanity check, the lock-class key must be persistent:
2970 if (!static_obj(key)) {
2971 printk("BUG: key %p not in .data!\n", key);
2973 * What it says above ^^^^^, I suggest you read it.
2975 DEBUG_LOCKS_WARN_ON(1);
2976 return;
2978 lock->key = key;
2980 if (unlikely(!debug_locks))
2981 return;
2983 if (subclass)
2984 register_lock_class(lock, subclass, 1);
2986 EXPORT_SYMBOL_GPL(lockdep_init_map);
2988 struct lock_class_key __lockdep_no_validate__;
2991 * This gets called for every mutex_lock*()/spin_lock*() operation.
2992 * We maintain the dependency maps and validate the locking attempt:
2994 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2995 int trylock, int read, int check, int hardirqs_off,
2996 struct lockdep_map *nest_lock, unsigned long ip,
2997 int references)
2999 struct task_struct *curr = current;
3000 struct lock_class *class = NULL;
3001 struct held_lock *hlock;
3002 unsigned int depth, id;
3003 int chain_head = 0;
3004 int class_idx;
3005 u64 chain_key;
3007 if (!prove_locking)
3008 check = 1;
3010 if (unlikely(!debug_locks))
3011 return 0;
3014 * Lockdep should run with IRQs disabled, otherwise we could
3015 * get an interrupt which would want to take locks, which would
3016 * end up in lockdep and have you got a head-ache already?
3018 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3019 return 0;
3021 if (lock->key == &__lockdep_no_validate__)
3022 check = 1;
3024 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3025 class = lock->class_cache[subclass];
3027 * Not cached?
3029 if (unlikely(!class)) {
3030 class = register_lock_class(lock, subclass, 0);
3031 if (!class)
3032 return 0;
3034 atomic_inc((atomic_t *)&class->ops);
3035 if (very_verbose(class)) {
3036 printk("\nacquire class [%p] %s", class->key, class->name);
3037 if (class->name_version > 1)
3038 printk("#%d", class->name_version);
3039 printk("\n");
3040 dump_stack();
3044 * Add the lock to the list of currently held locks.
3045 * (we dont increase the depth just yet, up until the
3046 * dependency checks are done)
3048 depth = curr->lockdep_depth;
3050 * Ran out of static storage for our per-task lock stack again have we?
3052 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3053 return 0;
3055 class_idx = class - lock_classes + 1;
3057 if (depth) {
3058 hlock = curr->held_locks + depth - 1;
3059 if (hlock->class_idx == class_idx && nest_lock) {
3060 if (hlock->references)
3061 hlock->references++;
3062 else
3063 hlock->references = 2;
3065 return 1;
3069 hlock = curr->held_locks + depth;
3071 * Plain impossible, we just registered it and checked it weren't no
3072 * NULL like.. I bet this mushroom I ate was good!
3074 if (DEBUG_LOCKS_WARN_ON(!class))
3075 return 0;
3076 hlock->class_idx = class_idx;
3077 hlock->acquire_ip = ip;
3078 hlock->instance = lock;
3079 hlock->nest_lock = nest_lock;
3080 hlock->trylock = trylock;
3081 hlock->read = read;
3082 hlock->check = check;
3083 hlock->hardirqs_off = !!hardirqs_off;
3084 hlock->references = references;
3085 #ifdef CONFIG_LOCK_STAT
3086 hlock->waittime_stamp = 0;
3087 hlock->holdtime_stamp = lockstat_clock();
3088 #endif
3090 if (check == 2 && !mark_irqflags(curr, hlock))
3091 return 0;
3093 /* mark it as used: */
3094 if (!mark_lock(curr, hlock, LOCK_USED))
3095 return 0;
3098 * Calculate the chain hash: it's the combined hash of all the
3099 * lock keys along the dependency chain. We save the hash value
3100 * at every step so that we can get the current hash easily
3101 * after unlock. The chain hash is then used to cache dependency
3102 * results.
3104 * The 'key ID' is what is the most compact key value to drive
3105 * the hash, not class->key.
3107 id = class - lock_classes;
3109 * Whoops, we did it again.. ran straight out of our static allocation.
3111 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3112 return 0;
3114 chain_key = curr->curr_chain_key;
3115 if (!depth) {
3117 * How can we have a chain hash when we ain't got no keys?!
3119 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3120 return 0;
3121 chain_head = 1;
3124 hlock->prev_chain_key = chain_key;
3125 if (separate_irq_context(curr, hlock)) {
3126 chain_key = 0;
3127 chain_head = 1;
3129 chain_key = iterate_chain_key(chain_key, id);
3131 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3132 return 0;
3134 curr->curr_chain_key = chain_key;
3135 curr->lockdep_depth++;
3136 check_chain_key(curr);
3137 #ifdef CONFIG_DEBUG_LOCKDEP
3138 if (unlikely(!debug_locks))
3139 return 0;
3140 #endif
3141 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3142 debug_locks_off();
3143 printk("BUG: MAX_LOCK_DEPTH too low!\n");
3144 printk("turning off the locking correctness validator.\n");
3145 dump_stack();
3146 return 0;
3149 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3150 max_lockdep_depth = curr->lockdep_depth;
3152 return 1;
3155 static int
3156 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3157 unsigned long ip)
3159 if (!debug_locks_off())
3160 return 0;
3161 if (debug_locks_silent)
3162 return 0;
3164 printk("\n=====================================\n");
3165 printk( "[ BUG: bad unlock balance detected! ]\n");
3166 printk( "-------------------------------------\n");
3167 printk("%s/%d is trying to release lock (",
3168 curr->comm, task_pid_nr(curr));
3169 print_lockdep_cache(lock);
3170 printk(") at:\n");
3171 print_ip_sym(ip);
3172 printk("but there are no more locks to release!\n");
3173 printk("\nother info that might help us debug this:\n");
3174 lockdep_print_held_locks(curr);
3176 printk("\nstack backtrace:\n");
3177 dump_stack();
3179 return 0;
3183 * Common debugging checks for both nested and non-nested unlock:
3185 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
3186 unsigned long ip)
3188 if (unlikely(!debug_locks))
3189 return 0;
3191 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3193 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3194 return 0;
3196 if (curr->lockdep_depth <= 0)
3197 return print_unlock_inbalance_bug(curr, lock, ip);
3199 return 1;
3202 static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3204 if (hlock->instance == lock)
3205 return 1;
3207 if (hlock->references) {
3208 struct lock_class *class = lock->class_cache[0];
3210 if (!class)
3211 class = look_up_lock_class(lock, 0);
3214 * If look_up_lock_class() failed to find a class, we're trying
3215 * to test if we hold a lock that has never yet been acquired.
3216 * Clearly if the lock hasn't been acquired _ever_, we're not
3217 * holding it either, so report failure.
3219 if (!class)
3220 return 0;
3223 * References, but not a lock we're actually ref-counting?
3224 * State got messed up, follow the sites that change ->references
3225 * and try to make sense of it.
3227 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3228 return 0;
3230 if (hlock->class_idx == class - lock_classes + 1)
3231 return 1;
3234 return 0;
3237 static int
3238 __lock_set_class(struct lockdep_map *lock, const char *name,
3239 struct lock_class_key *key, unsigned int subclass,
3240 unsigned long ip)
3242 struct task_struct *curr = current;
3243 struct held_lock *hlock, *prev_hlock;
3244 struct lock_class *class;
3245 unsigned int depth;
3246 int i;
3248 depth = curr->lockdep_depth;
3250 * This function is about (re)setting the class of a held lock,
3251 * yet we're not actually holding any locks. Naughty user!
3253 if (DEBUG_LOCKS_WARN_ON(!depth))
3254 return 0;
3256 prev_hlock = NULL;
3257 for (i = depth-1; i >= 0; i--) {
3258 hlock = curr->held_locks + i;
3260 * We must not cross into another context:
3262 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3263 break;
3264 if (match_held_lock(hlock, lock))
3265 goto found_it;
3266 prev_hlock = hlock;
3268 return print_unlock_inbalance_bug(curr, lock, ip);
3270 found_it:
3271 lockdep_init_map(lock, name, key, 0);
3272 class = register_lock_class(lock, subclass, 0);
3273 hlock->class_idx = class - lock_classes + 1;
3275 curr->lockdep_depth = i;
3276 curr->curr_chain_key = hlock->prev_chain_key;
3278 for (; i < depth; i++) {
3279 hlock = curr->held_locks + i;
3280 if (!__lock_acquire(hlock->instance,
3281 hlock_class(hlock)->subclass, hlock->trylock,
3282 hlock->read, hlock->check, hlock->hardirqs_off,
3283 hlock->nest_lock, hlock->acquire_ip,
3284 hlock->references))
3285 return 0;
3289 * I took it apart and put it back together again, except now I have
3290 * these 'spare' parts.. where shall I put them.
3292 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3293 return 0;
3294 return 1;
3298 * Remove the lock to the list of currently held locks in a
3299 * potentially non-nested (out of order) manner. This is a
3300 * relatively rare operation, as all the unlock APIs default
3301 * to nested mode (which uses lock_release()):
3303 static int
3304 lock_release_non_nested(struct task_struct *curr,
3305 struct lockdep_map *lock, unsigned long ip)
3307 struct held_lock *hlock, *prev_hlock;
3308 unsigned int depth;
3309 int i;
3312 * Check whether the lock exists in the current stack
3313 * of held locks:
3315 depth = curr->lockdep_depth;
3317 * So we're all set to release this lock.. wait what lock? We don't
3318 * own any locks, you've been drinking again?
3320 if (DEBUG_LOCKS_WARN_ON(!depth))
3321 return 0;
3323 prev_hlock = NULL;
3324 for (i = depth-1; i >= 0; i--) {
3325 hlock = curr->held_locks + i;
3327 * We must not cross into another context:
3329 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3330 break;
3331 if (match_held_lock(hlock, lock))
3332 goto found_it;
3333 prev_hlock = hlock;
3335 return print_unlock_inbalance_bug(curr, lock, ip);
3337 found_it:
3338 if (hlock->instance == lock)
3339 lock_release_holdtime(hlock);
3341 if (hlock->references) {
3342 hlock->references--;
3343 if (hlock->references) {
3345 * We had, and after removing one, still have
3346 * references, the current lock stack is still
3347 * valid. We're done!
3349 return 1;
3354 * We have the right lock to unlock, 'hlock' points to it.
3355 * Now we remove it from the stack, and add back the other
3356 * entries (if any), recalculating the hash along the way:
3359 curr->lockdep_depth = i;
3360 curr->curr_chain_key = hlock->prev_chain_key;
3362 for (i++; i < depth; i++) {
3363 hlock = curr->held_locks + i;
3364 if (!__lock_acquire(hlock->instance,
3365 hlock_class(hlock)->subclass, hlock->trylock,
3366 hlock->read, hlock->check, hlock->hardirqs_off,
3367 hlock->nest_lock, hlock->acquire_ip,
3368 hlock->references))
3369 return 0;
3373 * We had N bottles of beer on the wall, we drank one, but now
3374 * there's not N-1 bottles of beer left on the wall...
3376 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3377 return 0;
3378 return 1;
3382 * Remove the lock to the list of currently held locks - this gets
3383 * called on mutex_unlock()/spin_unlock*() (or on a failed
3384 * mutex_lock_interruptible()). This is done for unlocks that nest
3385 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3387 static int lock_release_nested(struct task_struct *curr,
3388 struct lockdep_map *lock, unsigned long ip)
3390 struct held_lock *hlock;
3391 unsigned int depth;
3394 * Pop off the top of the lock stack:
3396 depth = curr->lockdep_depth - 1;
3397 hlock = curr->held_locks + depth;
3400 * Is the unlock non-nested:
3402 if (hlock->instance != lock || hlock->references)
3403 return lock_release_non_nested(curr, lock, ip);
3404 curr->lockdep_depth--;
3407 * No more locks, but somehow we've got hash left over, who left it?
3409 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3410 return 0;
3412 curr->curr_chain_key = hlock->prev_chain_key;
3414 lock_release_holdtime(hlock);
3416 #ifdef CONFIG_DEBUG_LOCKDEP
3417 hlock->prev_chain_key = 0;
3418 hlock->class_idx = 0;
3419 hlock->acquire_ip = 0;
3420 hlock->irq_context = 0;
3421 #endif
3422 return 1;
3426 * Remove the lock to the list of currently held locks - this gets
3427 * called on mutex_unlock()/spin_unlock*() (or on a failed
3428 * mutex_lock_interruptible()). This is done for unlocks that nest
3429 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3431 static void
3432 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3434 struct task_struct *curr = current;
3436 if (!check_unlock(curr, lock, ip))
3437 return;
3439 if (nested) {
3440 if (!lock_release_nested(curr, lock, ip))
3441 return;
3442 } else {
3443 if (!lock_release_non_nested(curr, lock, ip))
3444 return;
3447 check_chain_key(curr);
3450 static int __lock_is_held(struct lockdep_map *lock)
3452 struct task_struct *curr = current;
3453 int i;
3455 for (i = 0; i < curr->lockdep_depth; i++) {
3456 struct held_lock *hlock = curr->held_locks + i;
3458 if (match_held_lock(hlock, lock))
3459 return 1;
3462 return 0;
3466 * Check whether we follow the irq-flags state precisely:
3468 static void check_flags(unsigned long flags)
3470 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3471 defined(CONFIG_TRACE_IRQFLAGS)
3472 if (!debug_locks)
3473 return;
3475 if (irqs_disabled_flags(flags)) {
3476 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3477 printk("possible reason: unannotated irqs-off.\n");
3479 } else {
3480 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3481 printk("possible reason: unannotated irqs-on.\n");
3486 * We dont accurately track softirq state in e.g.
3487 * hardirq contexts (such as on 4KSTACKS), so only
3488 * check if not in hardirq contexts:
3490 if (!hardirq_count()) {
3491 if (softirq_count()) {
3492 /* like the above, but with softirqs */
3493 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3494 } else {
3495 /* lick the above, does it taste good? */
3496 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3500 if (!debug_locks)
3501 print_irqtrace_events(current);
3502 #endif
3505 void lock_set_class(struct lockdep_map *lock, const char *name,
3506 struct lock_class_key *key, unsigned int subclass,
3507 unsigned long ip)
3509 unsigned long flags;
3511 if (unlikely(current->lockdep_recursion))
3512 return;
3514 raw_local_irq_save(flags);
3515 current->lockdep_recursion = 1;
3516 check_flags(flags);
3517 if (__lock_set_class(lock, name, key, subclass, ip))
3518 check_chain_key(current);
3519 current->lockdep_recursion = 0;
3520 raw_local_irq_restore(flags);
3522 EXPORT_SYMBOL_GPL(lock_set_class);
3525 * We are not always called with irqs disabled - do that here,
3526 * and also avoid lockdep recursion:
3528 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3529 int trylock, int read, int check,
3530 struct lockdep_map *nest_lock, unsigned long ip)
3532 unsigned long flags;
3534 if (unlikely(current->lockdep_recursion))
3535 return;
3537 raw_local_irq_save(flags);
3538 check_flags(flags);
3540 current->lockdep_recursion = 1;
3541 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3542 __lock_acquire(lock, subclass, trylock, read, check,
3543 irqs_disabled_flags(flags), nest_lock, ip, 0);
3544 current->lockdep_recursion = 0;
3545 raw_local_irq_restore(flags);
3547 EXPORT_SYMBOL_GPL(lock_acquire);
3549 void lock_release(struct lockdep_map *lock, int nested,
3550 unsigned long ip)
3552 unsigned long flags;
3554 if (unlikely(current->lockdep_recursion))
3555 return;
3557 raw_local_irq_save(flags);
3558 check_flags(flags);
3559 current->lockdep_recursion = 1;
3560 trace_lock_release(lock, ip);
3561 __lock_release(lock, nested, ip);
3562 current->lockdep_recursion = 0;
3563 raw_local_irq_restore(flags);
3565 EXPORT_SYMBOL_GPL(lock_release);
3567 int lock_is_held(struct lockdep_map *lock)
3569 unsigned long flags;
3570 int ret = 0;
3572 if (unlikely(current->lockdep_recursion))
3573 return 1; /* avoid false negative lockdep_assert_held() */
3575 raw_local_irq_save(flags);
3576 check_flags(flags);
3578 current->lockdep_recursion = 1;
3579 ret = __lock_is_held(lock);
3580 current->lockdep_recursion = 0;
3581 raw_local_irq_restore(flags);
3583 return ret;
3585 EXPORT_SYMBOL_GPL(lock_is_held);
3587 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3589 current->lockdep_reclaim_gfp = gfp_mask;
3592 void lockdep_clear_current_reclaim_state(void)
3594 current->lockdep_reclaim_gfp = 0;
3597 #ifdef CONFIG_LOCK_STAT
3598 static int
3599 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3600 unsigned long ip)
3602 if (!debug_locks_off())
3603 return 0;
3604 if (debug_locks_silent)
3605 return 0;
3607 printk("\n=================================\n");
3608 printk( "[ BUG: bad contention detected! ]\n");
3609 printk( "---------------------------------\n");
3610 printk("%s/%d is trying to contend lock (",
3611 curr->comm, task_pid_nr(curr));
3612 print_lockdep_cache(lock);
3613 printk(") at:\n");
3614 print_ip_sym(ip);
3615 printk("but there are no locks held!\n");
3616 printk("\nother info that might help us debug this:\n");
3617 lockdep_print_held_locks(curr);
3619 printk("\nstack backtrace:\n");
3620 dump_stack();
3622 return 0;
3625 static void
3626 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3628 struct task_struct *curr = current;
3629 struct held_lock *hlock, *prev_hlock;
3630 struct lock_class_stats *stats;
3631 unsigned int depth;
3632 int i, contention_point, contending_point;
3634 depth = curr->lockdep_depth;
3636 * Whee, we contended on this lock, except it seems we're not
3637 * actually trying to acquire anything much at all..
3639 if (DEBUG_LOCKS_WARN_ON(!depth))
3640 return;
3642 prev_hlock = NULL;
3643 for (i = depth-1; i >= 0; i--) {
3644 hlock = curr->held_locks + i;
3646 * We must not cross into another context:
3648 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3649 break;
3650 if (match_held_lock(hlock, lock))
3651 goto found_it;
3652 prev_hlock = hlock;
3654 print_lock_contention_bug(curr, lock, ip);
3655 return;
3657 found_it:
3658 if (hlock->instance != lock)
3659 return;
3661 hlock->waittime_stamp = lockstat_clock();
3663 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3664 contending_point = lock_point(hlock_class(hlock)->contending_point,
3665 lock->ip);
3667 stats = get_lock_stats(hlock_class(hlock));
3668 if (contention_point < LOCKSTAT_POINTS)
3669 stats->contention_point[contention_point]++;
3670 if (contending_point < LOCKSTAT_POINTS)
3671 stats->contending_point[contending_point]++;
3672 if (lock->cpu != smp_processor_id())
3673 stats->bounces[bounce_contended + !!hlock->read]++;
3674 put_lock_stats(stats);
3677 static void
3678 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3680 struct task_struct *curr = current;
3681 struct held_lock *hlock, *prev_hlock;
3682 struct lock_class_stats *stats;
3683 unsigned int depth;
3684 u64 now, waittime = 0;
3685 int i, cpu;
3687 depth = curr->lockdep_depth;
3689 * Yay, we acquired ownership of this lock we didn't try to
3690 * acquire, how the heck did that happen?
3692 if (DEBUG_LOCKS_WARN_ON(!depth))
3693 return;
3695 prev_hlock = NULL;
3696 for (i = depth-1; i >= 0; i--) {
3697 hlock = curr->held_locks + i;
3699 * We must not cross into another context:
3701 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3702 break;
3703 if (match_held_lock(hlock, lock))
3704 goto found_it;
3705 prev_hlock = hlock;
3707 print_lock_contention_bug(curr, lock, _RET_IP_);
3708 return;
3710 found_it:
3711 if (hlock->instance != lock)
3712 return;
3714 cpu = smp_processor_id();
3715 if (hlock->waittime_stamp) {
3716 now = lockstat_clock();
3717 waittime = now - hlock->waittime_stamp;
3718 hlock->holdtime_stamp = now;
3721 trace_lock_acquired(lock, ip);
3723 stats = get_lock_stats(hlock_class(hlock));
3724 if (waittime) {
3725 if (hlock->read)
3726 lock_time_inc(&stats->read_waittime, waittime);
3727 else
3728 lock_time_inc(&stats->write_waittime, waittime);
3730 if (lock->cpu != cpu)
3731 stats->bounces[bounce_acquired + !!hlock->read]++;
3732 put_lock_stats(stats);
3734 lock->cpu = cpu;
3735 lock->ip = ip;
3738 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3740 unsigned long flags;
3742 if (unlikely(!lock_stat))
3743 return;
3745 if (unlikely(current->lockdep_recursion))
3746 return;
3748 raw_local_irq_save(flags);
3749 check_flags(flags);
3750 current->lockdep_recursion = 1;
3751 trace_lock_contended(lock, ip);
3752 __lock_contended(lock, ip);
3753 current->lockdep_recursion = 0;
3754 raw_local_irq_restore(flags);
3756 EXPORT_SYMBOL_GPL(lock_contended);
3758 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3760 unsigned long flags;
3762 if (unlikely(!lock_stat))
3763 return;
3765 if (unlikely(current->lockdep_recursion))
3766 return;
3768 raw_local_irq_save(flags);
3769 check_flags(flags);
3770 current->lockdep_recursion = 1;
3771 __lock_acquired(lock, ip);
3772 current->lockdep_recursion = 0;
3773 raw_local_irq_restore(flags);
3775 EXPORT_SYMBOL_GPL(lock_acquired);
3776 #endif
3779 * Used by the testsuite, sanitize the validator state
3780 * after a simulated failure:
3783 void lockdep_reset(void)
3785 unsigned long flags;
3786 int i;
3788 raw_local_irq_save(flags);
3789 current->curr_chain_key = 0;
3790 current->lockdep_depth = 0;
3791 current->lockdep_recursion = 0;
3792 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3793 nr_hardirq_chains = 0;
3794 nr_softirq_chains = 0;
3795 nr_process_chains = 0;
3796 debug_locks = 1;
3797 for (i = 0; i < CHAINHASH_SIZE; i++)
3798 INIT_LIST_HEAD(chainhash_table + i);
3799 raw_local_irq_restore(flags);
3802 static void zap_class(struct lock_class *class)
3804 int i;
3807 * Remove all dependencies this lock is
3808 * involved in:
3810 for (i = 0; i < nr_list_entries; i++) {
3811 if (list_entries[i].class == class)
3812 list_del_rcu(&list_entries[i].entry);
3815 * Unhash the class and remove it from the all_lock_classes list:
3817 list_del_rcu(&class->hash_entry);
3818 list_del_rcu(&class->lock_entry);
3820 class->key = NULL;
3823 static inline int within(const void *addr, void *start, unsigned long size)
3825 return addr >= start && addr < start + size;
3828 void lockdep_free_key_range(void *start, unsigned long size)
3830 struct lock_class *class, *next;
3831 struct list_head *head;
3832 unsigned long flags;
3833 int i;
3834 int locked;
3836 raw_local_irq_save(flags);
3837 locked = graph_lock();
3840 * Unhash all classes that were created by this module:
3842 for (i = 0; i < CLASSHASH_SIZE; i++) {
3843 head = classhash_table + i;
3844 if (list_empty(head))
3845 continue;
3846 list_for_each_entry_safe(class, next, head, hash_entry) {
3847 if (within(class->key, start, size))
3848 zap_class(class);
3849 else if (within(class->name, start, size))
3850 zap_class(class);
3854 if (locked)
3855 graph_unlock();
3856 raw_local_irq_restore(flags);
3859 void lockdep_reset_lock(struct lockdep_map *lock)
3861 struct lock_class *class, *next;
3862 struct list_head *head;
3863 unsigned long flags;
3864 int i, j;
3865 int locked;
3867 raw_local_irq_save(flags);
3870 * Remove all classes this lock might have:
3872 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3874 * If the class exists we look it up and zap it:
3876 class = look_up_lock_class(lock, j);
3877 if (class)
3878 zap_class(class);
3881 * Debug check: in the end all mapped classes should
3882 * be gone.
3884 locked = graph_lock();
3885 for (i = 0; i < CLASSHASH_SIZE; i++) {
3886 head = classhash_table + i;
3887 if (list_empty(head))
3888 continue;
3889 list_for_each_entry_safe(class, next, head, hash_entry) {
3890 int match = 0;
3892 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3893 match |= class == lock->class_cache[j];
3895 if (unlikely(match)) {
3896 if (debug_locks_off_graph_unlock()) {
3898 * We all just reset everything, how did it match?
3900 WARN_ON(1);
3902 goto out_restore;
3906 if (locked)
3907 graph_unlock();
3909 out_restore:
3910 raw_local_irq_restore(flags);
3913 void lockdep_init(void)
3915 int i;
3918 * Some architectures have their own start_kernel()
3919 * code which calls lockdep_init(), while we also
3920 * call lockdep_init() from the start_kernel() itself,
3921 * and we want to initialize the hashes only once:
3923 if (lockdep_initialized)
3924 return;
3926 for (i = 0; i < CLASSHASH_SIZE; i++)
3927 INIT_LIST_HEAD(classhash_table + i);
3929 for (i = 0; i < CHAINHASH_SIZE; i++)
3930 INIT_LIST_HEAD(chainhash_table + i);
3932 lockdep_initialized = 1;
3935 void __init lockdep_info(void)
3937 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3939 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3940 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3941 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3942 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3943 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3944 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3945 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3947 printk(" memory used by lock dependency info: %lu kB\n",
3948 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3949 sizeof(struct list_head) * CLASSHASH_SIZE +
3950 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3951 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3952 sizeof(struct list_head) * CHAINHASH_SIZE
3953 #ifdef CONFIG_PROVE_LOCKING
3954 + sizeof(struct circular_queue)
3955 #endif
3956 ) / 1024
3959 printk(" per task-struct memory footprint: %lu bytes\n",
3960 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3962 #ifdef CONFIG_DEBUG_LOCKDEP
3963 if (lockdep_init_error) {
3964 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3965 printk("Call stack leading to lockdep invocation was:\n");
3966 print_stack_trace(&lockdep_init_trace, 0);
3968 #endif
3971 static void
3972 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3973 const void *mem_to, struct held_lock *hlock)
3975 if (!debug_locks_off())
3976 return;
3977 if (debug_locks_silent)
3978 return;
3980 printk("\n=========================\n");
3981 printk( "[ BUG: held lock freed! ]\n");
3982 printk( "-------------------------\n");
3983 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3984 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3985 print_lock(hlock);
3986 lockdep_print_held_locks(curr);
3988 printk("\nstack backtrace:\n");
3989 dump_stack();
3992 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3993 const void* lock_from, unsigned long lock_len)
3995 return lock_from + lock_len <= mem_from ||
3996 mem_from + mem_len <= lock_from;
4000 * Called when kernel memory is freed (or unmapped), or if a lock
4001 * is destroyed or reinitialized - this code checks whether there is
4002 * any held lock in the memory range of <from> to <to>:
4004 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4006 struct task_struct *curr = current;
4007 struct held_lock *hlock;
4008 unsigned long flags;
4009 int i;
4011 if (unlikely(!debug_locks))
4012 return;
4014 local_irq_save(flags);
4015 for (i = 0; i < curr->lockdep_depth; i++) {
4016 hlock = curr->held_locks + i;
4018 if (not_in_range(mem_from, mem_len, hlock->instance,
4019 sizeof(*hlock->instance)))
4020 continue;
4022 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
4023 break;
4025 local_irq_restore(flags);
4027 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
4029 static void print_held_locks_bug(struct task_struct *curr)
4031 if (!debug_locks_off())
4032 return;
4033 if (debug_locks_silent)
4034 return;
4036 printk("\n=====================================\n");
4037 printk( "[ BUG: lock held at task exit time! ]\n");
4038 printk( "-------------------------------------\n");
4039 printk("%s/%d is exiting with locks still held!\n",
4040 curr->comm, task_pid_nr(curr));
4041 lockdep_print_held_locks(curr);
4043 printk("\nstack backtrace:\n");
4044 dump_stack();
4047 void debug_check_no_locks_held(struct task_struct *task)
4049 if (unlikely(task->lockdep_depth > 0))
4050 print_held_locks_bug(task);
4053 void debug_show_all_locks(void)
4055 struct task_struct *g, *p;
4056 int count = 10;
4057 int unlock = 1;
4059 if (unlikely(!debug_locks)) {
4060 printk("INFO: lockdep is turned off.\n");
4061 return;
4063 printk("\nShowing all locks held in the system:\n");
4066 * Here we try to get the tasklist_lock as hard as possible,
4067 * if not successful after 2 seconds we ignore it (but keep
4068 * trying). This is to enable a debug printout even if a
4069 * tasklist_lock-holding task deadlocks or crashes.
4071 retry:
4072 if (!read_trylock(&tasklist_lock)) {
4073 if (count == 10)
4074 printk("hm, tasklist_lock locked, retrying... ");
4075 if (count) {
4076 count--;
4077 printk(" #%d", 10-count);
4078 mdelay(200);
4079 goto retry;
4081 printk(" ignoring it.\n");
4082 unlock = 0;
4083 } else {
4084 if (count != 10)
4085 printk(KERN_CONT " locked it.\n");
4088 do_each_thread(g, p) {
4090 * It's not reliable to print a task's held locks
4091 * if it's not sleeping (or if it's not the current
4092 * task):
4094 if (p->state == TASK_RUNNING && p != current)
4095 continue;
4096 if (p->lockdep_depth)
4097 lockdep_print_held_locks(p);
4098 if (!unlock)
4099 if (read_trylock(&tasklist_lock))
4100 unlock = 1;
4101 } while_each_thread(g, p);
4103 printk("\n");
4104 printk("=============================================\n\n");
4106 if (unlock)
4107 read_unlock(&tasklist_lock);
4109 EXPORT_SYMBOL_GPL(debug_show_all_locks);
4112 * Careful: only use this function if you are sure that
4113 * the task cannot run in parallel!
4115 void debug_show_held_locks(struct task_struct *task)
4117 if (unlikely(!debug_locks)) {
4118 printk("INFO: lockdep is turned off.\n");
4119 return;
4121 lockdep_print_held_locks(task);
4123 EXPORT_SYMBOL_GPL(debug_show_held_locks);
4125 void lockdep_sys_exit(void)
4127 struct task_struct *curr = current;
4129 if (unlikely(curr->lockdep_depth)) {
4130 if (!debug_locks_off())
4131 return;
4132 printk("\n================================================\n");
4133 printk( "[ BUG: lock held when returning to user space! ]\n");
4134 printk( "------------------------------------------------\n");
4135 printk("%s/%d is leaving the kernel with locks still held!\n",
4136 curr->comm, curr->pid);
4137 lockdep_print_held_locks(curr);
4141 void lockdep_rcu_dereference(const char *file, const int line)
4143 struct task_struct *curr = current;
4145 #ifndef CONFIG_PROVE_RCU_REPEATEDLY
4146 if (!debug_locks_off())
4147 return;
4148 #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4149 /* Note: the following can be executed concurrently, so be careful. */
4150 printk("\n===================================================\n");
4151 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
4152 printk( "---------------------------------------------------\n");
4153 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
4154 file, line);
4155 printk("\nother info that might help us debug this:\n\n");
4156 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
4157 lockdep_print_held_locks(curr);
4158 printk("\nstack backtrace:\n");
4159 dump_stack();
4161 EXPORT_SYMBOL_GPL(lockdep_rcu_dereference);