udf: Fix oops when invalid character in filename occurs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / lockdep.c
blob3673a3f44d9d445f7cdf42f2ee5592731a4c0db7
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>
46 #include <asm/sections.h>
48 #include "lockdep_internals.h"
50 #ifdef CONFIG_PROVE_LOCKING
51 int prove_locking = 1;
52 module_param(prove_locking, int, 0644);
53 #else
54 #define prove_locking 0
55 #endif
57 #ifdef CONFIG_LOCK_STAT
58 int lock_stat = 1;
59 module_param(lock_stat, int, 0644);
60 #else
61 #define lock_stat 0
62 #endif
65 * lockdep_lock: protects the lockdep graph, the hashes and the
66 * class/list/hash allocators.
68 * This is one of the rare exceptions where it's justified
69 * to use a raw spinlock - we really dont want the spinlock
70 * code to recurse back into the lockdep code...
72 static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
74 static int graph_lock(void)
76 __raw_spin_lock(&lockdep_lock);
78 * Make sure that if another CPU detected a bug while
79 * walking the graph we dont change it (while the other
80 * CPU is busy printing out stuff with the graph lock
81 * dropped already)
83 if (!debug_locks) {
84 __raw_spin_unlock(&lockdep_lock);
85 return 0;
87 /* prevent any recursions within lockdep from causing deadlocks */
88 current->lockdep_recursion++;
89 return 1;
92 static inline int graph_unlock(void)
94 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
95 return DEBUG_LOCKS_WARN_ON(1);
97 current->lockdep_recursion--;
98 __raw_spin_unlock(&lockdep_lock);
99 return 0;
103 * Turn lock debugging off and return with 0 if it was off already,
104 * and also release the graph lock:
106 static inline int debug_locks_off_graph_unlock(void)
108 int ret = debug_locks_off();
110 __raw_spin_unlock(&lockdep_lock);
112 return ret;
115 static int lockdep_initialized;
117 unsigned long nr_list_entries;
118 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
121 * All data structures here are protected by the global debug_lock.
123 * Mutex key structs only get allocated, once during bootup, and never
124 * get freed - this significantly simplifies the debugging code.
126 unsigned long nr_lock_classes;
127 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
129 static inline struct lock_class *hlock_class(struct held_lock *hlock)
131 if (!hlock->class_idx) {
132 DEBUG_LOCKS_WARN_ON(1);
133 return NULL;
135 return lock_classes + hlock->class_idx - 1;
138 #ifdef CONFIG_LOCK_STAT
139 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
141 static int lock_point(unsigned long points[], unsigned long ip)
143 int i;
145 for (i = 0; i < LOCKSTAT_POINTS; i++) {
146 if (points[i] == 0) {
147 points[i] = ip;
148 break;
150 if (points[i] == ip)
151 break;
154 return i;
157 static void lock_time_inc(struct lock_time *lt, s64 time)
159 if (time > lt->max)
160 lt->max = time;
162 if (time < lt->min || !lt->min)
163 lt->min = time;
165 lt->total += time;
166 lt->nr++;
169 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
171 dst->min += src->min;
172 dst->max += src->max;
173 dst->total += src->total;
174 dst->nr += src->nr;
177 struct lock_class_stats lock_stats(struct lock_class *class)
179 struct lock_class_stats stats;
180 int cpu, i;
182 memset(&stats, 0, sizeof(struct lock_class_stats));
183 for_each_possible_cpu(cpu) {
184 struct lock_class_stats *pcs =
185 &per_cpu(lock_stats, cpu)[class - lock_classes];
187 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
188 stats.contention_point[i] += pcs->contention_point[i];
190 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
191 stats.contending_point[i] += pcs->contending_point[i];
193 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
194 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
196 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
197 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
199 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
200 stats.bounces[i] += pcs->bounces[i];
203 return stats;
206 void clear_lock_stats(struct lock_class *class)
208 int cpu;
210 for_each_possible_cpu(cpu) {
211 struct lock_class_stats *cpu_stats =
212 &per_cpu(lock_stats, cpu)[class - lock_classes];
214 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
216 memset(class->contention_point, 0, sizeof(class->contention_point));
217 memset(class->contending_point, 0, sizeof(class->contending_point));
220 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
222 return &get_cpu_var(lock_stats)[class - lock_classes];
225 static void put_lock_stats(struct lock_class_stats *stats)
227 put_cpu_var(lock_stats);
230 static void lock_release_holdtime(struct held_lock *hlock)
232 struct lock_class_stats *stats;
233 s64 holdtime;
235 if (!lock_stat)
236 return;
238 holdtime = sched_clock() - hlock->holdtime_stamp;
240 stats = get_lock_stats(hlock_class(hlock));
241 if (hlock->read)
242 lock_time_inc(&stats->read_holdtime, holdtime);
243 else
244 lock_time_inc(&stats->write_holdtime, holdtime);
245 put_lock_stats(stats);
247 #else
248 static inline void lock_release_holdtime(struct held_lock *hlock)
251 #endif
254 * We keep a global list of all lock classes. The list only grows,
255 * never shrinks. The list is only accessed with the lockdep
256 * spinlock lock held.
258 LIST_HEAD(all_lock_classes);
261 * The lockdep classes are in a hash-table as well, for fast lookup:
263 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
264 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
265 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
266 #define classhashentry(key) (classhash_table + __classhashfn((key)))
268 static struct list_head classhash_table[CLASSHASH_SIZE];
271 * We put the lock dependency chains into a hash-table as well, to cache
272 * their existence:
274 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
275 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
276 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
277 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
279 static struct list_head chainhash_table[CHAINHASH_SIZE];
282 * The hash key of the lock dependency chains is a hash itself too:
283 * it's a hash of all locks taken up to that lock, including that lock.
284 * It's a 64-bit hash, because it's important for the keys to be
285 * unique.
287 #define iterate_chain_key(key1, key2) \
288 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
289 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
290 (key2))
292 void lockdep_off(void)
294 current->lockdep_recursion++;
296 EXPORT_SYMBOL(lockdep_off);
298 void lockdep_on(void)
300 current->lockdep_recursion--;
302 EXPORT_SYMBOL(lockdep_on);
305 * Debugging switches:
308 #define VERBOSE 0
309 #define VERY_VERBOSE 0
311 #if VERBOSE
312 # define HARDIRQ_VERBOSE 1
313 # define SOFTIRQ_VERBOSE 1
314 # define RECLAIM_VERBOSE 1
315 #else
316 # define HARDIRQ_VERBOSE 0
317 # define SOFTIRQ_VERBOSE 0
318 # define RECLAIM_VERBOSE 0
319 #endif
321 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
323 * Quick filtering for interesting events:
325 static int class_filter(struct lock_class *class)
327 #if 0
328 /* Example */
329 if (class->name_version == 1 &&
330 !strcmp(class->name, "lockname"))
331 return 1;
332 if (class->name_version == 1 &&
333 !strcmp(class->name, "&struct->lockfield"))
334 return 1;
335 #endif
336 /* Filter everything else. 1 would be to allow everything else */
337 return 0;
339 #endif
341 static int verbose(struct lock_class *class)
343 #if VERBOSE
344 return class_filter(class);
345 #endif
346 return 0;
350 * Stack-trace: tightly packed array of stack backtrace
351 * addresses. Protected by the graph_lock.
353 unsigned long nr_stack_trace_entries;
354 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
356 static int save_trace(struct stack_trace *trace)
358 trace->nr_entries = 0;
359 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
360 trace->entries = stack_trace + nr_stack_trace_entries;
362 trace->skip = 3;
364 save_stack_trace(trace);
366 trace->max_entries = trace->nr_entries;
368 nr_stack_trace_entries += trace->nr_entries;
370 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
371 if (!debug_locks_off_graph_unlock())
372 return 0;
374 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
375 printk("turning off the locking correctness validator.\n");
376 dump_stack();
378 return 0;
381 return 1;
384 unsigned int nr_hardirq_chains;
385 unsigned int nr_softirq_chains;
386 unsigned int nr_process_chains;
387 unsigned int max_lockdep_depth;
388 unsigned int max_recursion_depth;
390 static unsigned int lockdep_dependency_gen_id;
392 static bool lockdep_dependency_visit(struct lock_class *source,
393 unsigned int depth)
395 if (!depth)
396 lockdep_dependency_gen_id++;
397 if (source->dep_gen_id == lockdep_dependency_gen_id)
398 return true;
399 source->dep_gen_id = lockdep_dependency_gen_id;
400 return false;
403 #ifdef CONFIG_DEBUG_LOCKDEP
405 * We cannot printk in early bootup code. Not even early_printk()
406 * might work. So we mark any initialization errors and printk
407 * about it later on, in lockdep_info().
409 static int lockdep_init_error;
410 static unsigned long lockdep_init_trace_data[20];
411 static struct stack_trace lockdep_init_trace = {
412 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
413 .entries = lockdep_init_trace_data,
417 * Various lockdep statistics:
419 atomic_t chain_lookup_hits;
420 atomic_t chain_lookup_misses;
421 atomic_t hardirqs_on_events;
422 atomic_t hardirqs_off_events;
423 atomic_t redundant_hardirqs_on;
424 atomic_t redundant_hardirqs_off;
425 atomic_t softirqs_on_events;
426 atomic_t softirqs_off_events;
427 atomic_t redundant_softirqs_on;
428 atomic_t redundant_softirqs_off;
429 atomic_t nr_unused_locks;
430 atomic_t nr_cyclic_checks;
431 atomic_t nr_cyclic_check_recursions;
432 atomic_t nr_find_usage_forwards_checks;
433 atomic_t nr_find_usage_forwards_recursions;
434 atomic_t nr_find_usage_backwards_checks;
435 atomic_t nr_find_usage_backwards_recursions;
436 # define debug_atomic_inc(ptr) atomic_inc(ptr)
437 # define debug_atomic_dec(ptr) atomic_dec(ptr)
438 # define debug_atomic_read(ptr) atomic_read(ptr)
439 #else
440 # define debug_atomic_inc(ptr) do { } while (0)
441 # define debug_atomic_dec(ptr) do { } while (0)
442 # define debug_atomic_read(ptr) 0
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 void print_lock_name(struct lock_class *class)
503 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
504 const char *name;
506 get_usage_chars(class, usage);
508 name = class->name;
509 if (!name) {
510 name = __get_key_name(class->key, str);
511 printk(" (%s", name);
512 } else {
513 printk(" (%s", name);
514 if (class->name_version > 1)
515 printk("#%d", class->name_version);
516 if (class->subclass)
517 printk("/%d", class->subclass);
519 printk("){%s}", usage);
522 static void print_lockdep_cache(struct lockdep_map *lock)
524 const char *name;
525 char str[KSYM_NAME_LEN];
527 name = lock->name;
528 if (!name)
529 name = __get_key_name(lock->key->subkeys, str);
531 printk("%s", name);
534 static void print_lock(struct held_lock *hlock)
536 print_lock_name(hlock_class(hlock));
537 printk(", at: ");
538 print_ip_sym(hlock->acquire_ip);
541 static void lockdep_print_held_locks(struct task_struct *curr)
543 int i, depth = curr->lockdep_depth;
545 if (!depth) {
546 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
547 return;
549 printk("%d lock%s held by %s/%d:\n",
550 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
552 for (i = 0; i < depth; i++) {
553 printk(" #%d: ", i);
554 print_lock(curr->held_locks + i);
558 static void print_lock_class_header(struct lock_class *class, int depth)
560 int bit;
562 printk("%*s->", depth, "");
563 print_lock_name(class);
564 printk(" ops: %lu", class->ops);
565 printk(" {\n");
567 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
568 if (class->usage_mask & (1 << bit)) {
569 int len = depth;
571 len += printk("%*s %s", depth, "", usage_str[bit]);
572 len += printk(" at:\n");
573 print_stack_trace(class->usage_traces + bit, len);
576 printk("%*s }\n", depth, "");
578 printk("%*s ... key at: ",depth,"");
579 print_ip_sym((unsigned long)class->key);
583 * printk all lock dependencies starting at <entry>:
585 static void __used
586 print_lock_dependencies(struct lock_class *class, int depth)
588 struct lock_list *entry;
590 if (lockdep_dependency_visit(class, depth))
591 return;
593 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
594 return;
596 print_lock_class_header(class, depth);
598 list_for_each_entry(entry, &class->locks_after, entry) {
599 if (DEBUG_LOCKS_WARN_ON(!entry->class))
600 return;
602 print_lock_dependencies(entry->class, depth + 1);
604 printk("%*s ... acquired at:\n",depth,"");
605 print_stack_trace(&entry->trace, 2);
606 printk("\n");
610 static void print_kernel_version(void)
612 printk("%s %.*s\n", init_utsname()->release,
613 (int)strcspn(init_utsname()->version, " "),
614 init_utsname()->version);
617 static int very_verbose(struct lock_class *class)
619 #if VERY_VERBOSE
620 return class_filter(class);
621 #endif
622 return 0;
626 * Is this the address of a static object:
628 static int static_obj(void *obj)
630 unsigned long start = (unsigned long) &_stext,
631 end = (unsigned long) &_end,
632 addr = (unsigned long) obj;
633 #ifdef CONFIG_SMP
634 int i;
635 #endif
638 * static variable?
640 if ((addr >= start) && (addr < end))
641 return 1;
643 #ifdef CONFIG_SMP
645 * percpu var?
647 for_each_possible_cpu(i) {
648 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
649 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
650 + per_cpu_offset(i);
652 if ((addr >= start) && (addr < end))
653 return 1;
655 #endif
658 * module var?
660 return is_module_address(addr);
664 * To make lock name printouts unique, we calculate a unique
665 * class->name_version generation counter:
667 static int count_matching_names(struct lock_class *new_class)
669 struct lock_class *class;
670 int count = 0;
672 if (!new_class->name)
673 return 0;
675 list_for_each_entry(class, &all_lock_classes, lock_entry) {
676 if (new_class->key - new_class->subclass == class->key)
677 return class->name_version;
678 if (class->name && !strcmp(class->name, new_class->name))
679 count = max(count, class->name_version);
682 return count + 1;
686 * Register a lock's class in the hash-table, if the class is not present
687 * yet. Otherwise we look it up. We cache the result in the lock object
688 * itself, so actual lookup of the hash should be once per lock object.
690 static inline struct lock_class *
691 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
693 struct lockdep_subclass_key *key;
694 struct list_head *hash_head;
695 struct lock_class *class;
697 #ifdef CONFIG_DEBUG_LOCKDEP
699 * If the architecture calls into lockdep before initializing
700 * the hashes then we'll warn about it later. (we cannot printk
701 * right now)
703 if (unlikely(!lockdep_initialized)) {
704 lockdep_init();
705 lockdep_init_error = 1;
706 save_stack_trace(&lockdep_init_trace);
708 #endif
711 * Static locks do not have their class-keys yet - for them the key
712 * is the lock object itself:
714 if (unlikely(!lock->key))
715 lock->key = (void *)lock;
718 * NOTE: the class-key must be unique. For dynamic locks, a static
719 * lock_class_key variable is passed in through the mutex_init()
720 * (or spin_lock_init()) call - which acts as the key. For static
721 * locks we use the lock object itself as the key.
723 BUILD_BUG_ON(sizeof(struct lock_class_key) >
724 sizeof(struct lockdep_map));
726 key = lock->key->subkeys + subclass;
728 hash_head = classhashentry(key);
731 * We can walk the hash lockfree, because the hash only
732 * grows, and we are careful when adding entries to the end:
734 list_for_each_entry(class, hash_head, hash_entry) {
735 if (class->key == key) {
736 WARN_ON_ONCE(class->name != lock->name);
737 return class;
741 return NULL;
745 * Register a lock's class in the hash-table, if the class is not present
746 * yet. Otherwise we look it up. We cache the result in the lock object
747 * itself, so actual lookup of the hash should be once per lock object.
749 static inline struct lock_class *
750 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
752 struct lockdep_subclass_key *key;
753 struct list_head *hash_head;
754 struct lock_class *class;
755 unsigned long flags;
757 class = look_up_lock_class(lock, subclass);
758 if (likely(class))
759 return class;
762 * Debug-check: all keys must be persistent!
764 if (!static_obj(lock->key)) {
765 debug_locks_off();
766 printk("INFO: trying to register non-static key.\n");
767 printk("the code is fine but needs lockdep annotation.\n");
768 printk("turning off the locking correctness validator.\n");
769 dump_stack();
771 return NULL;
774 key = lock->key->subkeys + subclass;
775 hash_head = classhashentry(key);
777 raw_local_irq_save(flags);
778 if (!graph_lock()) {
779 raw_local_irq_restore(flags);
780 return NULL;
783 * We have to do the hash-walk again, to avoid races
784 * with another CPU:
786 list_for_each_entry(class, hash_head, hash_entry)
787 if (class->key == key)
788 goto out_unlock_set;
790 * Allocate a new key from the static array, and add it to
791 * the hash:
793 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
794 if (!debug_locks_off_graph_unlock()) {
795 raw_local_irq_restore(flags);
796 return NULL;
798 raw_local_irq_restore(flags);
800 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
801 printk("turning off the locking correctness validator.\n");
802 return NULL;
804 class = lock_classes + nr_lock_classes++;
805 debug_atomic_inc(&nr_unused_locks);
806 class->key = key;
807 class->name = lock->name;
808 class->subclass = subclass;
809 INIT_LIST_HEAD(&class->lock_entry);
810 INIT_LIST_HEAD(&class->locks_before);
811 INIT_LIST_HEAD(&class->locks_after);
812 class->name_version = count_matching_names(class);
814 * We use RCU's safe list-add method to make
815 * parallel walking of the hash-list safe:
817 list_add_tail_rcu(&class->hash_entry, hash_head);
819 * Add it to the global list of classes:
821 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
823 if (verbose(class)) {
824 graph_unlock();
825 raw_local_irq_restore(flags);
827 printk("\nnew class %p: %s", class->key, class->name);
828 if (class->name_version > 1)
829 printk("#%d", class->name_version);
830 printk("\n");
831 dump_stack();
833 raw_local_irq_save(flags);
834 if (!graph_lock()) {
835 raw_local_irq_restore(flags);
836 return NULL;
839 out_unlock_set:
840 graph_unlock();
841 raw_local_irq_restore(flags);
843 if (!subclass || force)
844 lock->class_cache = class;
846 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
847 return NULL;
849 return class;
852 #ifdef CONFIG_PROVE_LOCKING
854 * Allocate a lockdep entry. (assumes the graph_lock held, returns
855 * with NULL on failure)
857 static struct lock_list *alloc_list_entry(void)
859 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
860 if (!debug_locks_off_graph_unlock())
861 return NULL;
863 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
864 printk("turning off the locking correctness validator.\n");
865 return NULL;
867 return list_entries + nr_list_entries++;
871 * Add a new dependency to the head of the list:
873 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
874 struct list_head *head, unsigned long ip, int distance)
876 struct lock_list *entry;
878 * Lock not present yet - get a new dependency struct and
879 * add it to the list:
881 entry = alloc_list_entry();
882 if (!entry)
883 return 0;
885 if (!save_trace(&entry->trace))
886 return 0;
888 entry->class = this;
889 entry->distance = distance;
891 * Since we never remove from the dependency list, the list can
892 * be walked lockless by other CPUs, it's only allocation
893 * that must be protected by the spinlock. But this also means
894 * we must make new entries visible only once writes to the
895 * entry become visible - hence the RCU op:
897 list_add_tail_rcu(&entry->entry, head);
899 return 1;
903 * Recursive, forwards-direction lock-dependency checking, used for
904 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
905 * checking.
907 * (to keep the stackframe of the recursive functions small we
908 * use these global variables, and we also mark various helper
909 * functions as noinline.)
911 static struct held_lock *check_source, *check_target;
914 * Print a dependency chain entry (this is only done when a deadlock
915 * has been detected):
917 static noinline int
918 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
920 if (debug_locks_silent)
921 return 0;
922 printk("\n-> #%u", depth);
923 print_lock_name(target->class);
924 printk(":\n");
925 print_stack_trace(&target->trace, 6);
927 return 0;
931 * When a circular dependency is detected, print the
932 * header first:
934 static noinline int
935 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
937 struct task_struct *curr = current;
939 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
940 return 0;
942 printk("\n=======================================================\n");
943 printk( "[ INFO: possible circular locking dependency detected ]\n");
944 print_kernel_version();
945 printk( "-------------------------------------------------------\n");
946 printk("%s/%d is trying to acquire lock:\n",
947 curr->comm, task_pid_nr(curr));
948 print_lock(check_source);
949 printk("\nbut task is already holding lock:\n");
950 print_lock(check_target);
951 printk("\nwhich lock already depends on the new lock.\n\n");
952 printk("\nthe existing dependency chain (in reverse order) is:\n");
954 print_circular_bug_entry(entry, depth);
956 return 0;
959 static noinline int print_circular_bug_tail(void)
961 struct task_struct *curr = current;
962 struct lock_list this;
964 if (debug_locks_silent)
965 return 0;
967 this.class = hlock_class(check_source);
968 if (!save_trace(&this.trace))
969 return 0;
971 print_circular_bug_entry(&this, 0);
973 printk("\nother info that might help us debug this:\n\n");
974 lockdep_print_held_locks(curr);
976 printk("\nstack backtrace:\n");
977 dump_stack();
979 return 0;
982 #define RECURSION_LIMIT 40
984 static int noinline print_infinite_recursion_bug(void)
986 if (!debug_locks_off_graph_unlock())
987 return 0;
989 WARN_ON(1);
991 return 0;
994 unsigned long __lockdep_count_forward_deps(struct lock_class *class,
995 unsigned int depth)
997 struct lock_list *entry;
998 unsigned long ret = 1;
1000 if (lockdep_dependency_visit(class, depth))
1001 return 0;
1004 * Recurse this class's dependency list:
1006 list_for_each_entry(entry, &class->locks_after, entry)
1007 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1009 return ret;
1012 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1014 unsigned long ret, flags;
1016 local_irq_save(flags);
1017 __raw_spin_lock(&lockdep_lock);
1018 ret = __lockdep_count_forward_deps(class, 0);
1019 __raw_spin_unlock(&lockdep_lock);
1020 local_irq_restore(flags);
1022 return ret;
1025 unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1026 unsigned int depth)
1028 struct lock_list *entry;
1029 unsigned long ret = 1;
1031 if (lockdep_dependency_visit(class, depth))
1032 return 0;
1034 * Recurse this class's dependency list:
1036 list_for_each_entry(entry, &class->locks_before, entry)
1037 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1039 return ret;
1042 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1044 unsigned long ret, flags;
1046 local_irq_save(flags);
1047 __raw_spin_lock(&lockdep_lock);
1048 ret = __lockdep_count_backward_deps(class, 0);
1049 __raw_spin_unlock(&lockdep_lock);
1050 local_irq_restore(flags);
1052 return ret;
1056 * Prove that the dependency graph starting at <entry> can not
1057 * lead to <target>. Print an error and return 0 if it does.
1059 static noinline int
1060 check_noncircular(struct lock_class *source, unsigned int depth)
1062 struct lock_list *entry;
1064 if (lockdep_dependency_visit(source, depth))
1065 return 1;
1067 debug_atomic_inc(&nr_cyclic_check_recursions);
1068 if (depth > max_recursion_depth)
1069 max_recursion_depth = depth;
1070 if (depth >= RECURSION_LIMIT)
1071 return print_infinite_recursion_bug();
1073 * Check this lock's dependency list:
1075 list_for_each_entry(entry, &source->locks_after, entry) {
1076 if (entry->class == hlock_class(check_target))
1077 return print_circular_bug_header(entry, depth+1);
1078 debug_atomic_inc(&nr_cyclic_checks);
1079 if (!check_noncircular(entry->class, depth+1))
1080 return print_circular_bug_entry(entry, depth+1);
1082 return 1;
1085 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1087 * Forwards and backwards subgraph searching, for the purposes of
1088 * proving that two subgraphs can be connected by a new dependency
1089 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1091 static enum lock_usage_bit find_usage_bit;
1092 static struct lock_class *forwards_match, *backwards_match;
1095 * Find a node in the forwards-direction dependency sub-graph starting
1096 * at <source> that matches <find_usage_bit>.
1098 * Return 2 if such a node exists in the subgraph, and put that node
1099 * into <forwards_match>.
1101 * Return 1 otherwise and keep <forwards_match> unchanged.
1102 * Return 0 on error.
1104 static noinline int
1105 find_usage_forwards(struct lock_class *source, unsigned int depth)
1107 struct lock_list *entry;
1108 int ret;
1110 if (lockdep_dependency_visit(source, depth))
1111 return 1;
1113 if (depth > max_recursion_depth)
1114 max_recursion_depth = depth;
1115 if (depth >= RECURSION_LIMIT)
1116 return print_infinite_recursion_bug();
1118 debug_atomic_inc(&nr_find_usage_forwards_checks);
1119 if (source->usage_mask & (1 << find_usage_bit)) {
1120 forwards_match = source;
1121 return 2;
1125 * Check this lock's dependency list:
1127 list_for_each_entry(entry, &source->locks_after, entry) {
1128 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1129 ret = find_usage_forwards(entry->class, depth+1);
1130 if (ret == 2 || ret == 0)
1131 return ret;
1133 return 1;
1137 * Find a node in the backwards-direction dependency sub-graph starting
1138 * at <source> that matches <find_usage_bit>.
1140 * Return 2 if such a node exists in the subgraph, and put that node
1141 * into <backwards_match>.
1143 * Return 1 otherwise and keep <backwards_match> unchanged.
1144 * Return 0 on error.
1146 static noinline int
1147 find_usage_backwards(struct lock_class *source, unsigned int depth)
1149 struct lock_list *entry;
1150 int ret;
1152 if (lockdep_dependency_visit(source, depth))
1153 return 1;
1155 if (!__raw_spin_is_locked(&lockdep_lock))
1156 return DEBUG_LOCKS_WARN_ON(1);
1158 if (depth > max_recursion_depth)
1159 max_recursion_depth = depth;
1160 if (depth >= RECURSION_LIMIT)
1161 return print_infinite_recursion_bug();
1163 debug_atomic_inc(&nr_find_usage_backwards_checks);
1164 if (source->usage_mask & (1 << find_usage_bit)) {
1165 backwards_match = source;
1166 return 2;
1169 if (!source && debug_locks_off_graph_unlock()) {
1170 WARN_ON(1);
1171 return 0;
1175 * Check this lock's dependency list:
1177 list_for_each_entry(entry, &source->locks_before, entry) {
1178 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1179 ret = find_usage_backwards(entry->class, depth+1);
1180 if (ret == 2 || ret == 0)
1181 return ret;
1183 return 1;
1186 static int
1187 print_bad_irq_dependency(struct task_struct *curr,
1188 struct held_lock *prev,
1189 struct held_lock *next,
1190 enum lock_usage_bit bit1,
1191 enum lock_usage_bit bit2,
1192 const char *irqclass)
1194 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1195 return 0;
1197 printk("\n======================================================\n");
1198 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1199 irqclass, irqclass);
1200 print_kernel_version();
1201 printk( "------------------------------------------------------\n");
1202 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1203 curr->comm, task_pid_nr(curr),
1204 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1205 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1206 curr->hardirqs_enabled,
1207 curr->softirqs_enabled);
1208 print_lock(next);
1210 printk("\nand this task is already holding:\n");
1211 print_lock(prev);
1212 printk("which would create a new lock dependency:\n");
1213 print_lock_name(hlock_class(prev));
1214 printk(" ->");
1215 print_lock_name(hlock_class(next));
1216 printk("\n");
1218 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1219 irqclass);
1220 print_lock_name(backwards_match);
1221 printk("\n... which became %s-irq-safe at:\n", irqclass);
1223 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1225 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1226 print_lock_name(forwards_match);
1227 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1228 printk("...");
1230 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1232 printk("\nother info that might help us debug this:\n\n");
1233 lockdep_print_held_locks(curr);
1235 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1236 print_lock_dependencies(backwards_match, 0);
1238 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1239 print_lock_dependencies(forwards_match, 0);
1241 printk("\nstack backtrace:\n");
1242 dump_stack();
1244 return 0;
1247 static int
1248 check_usage(struct task_struct *curr, struct held_lock *prev,
1249 struct held_lock *next, enum lock_usage_bit bit_backwards,
1250 enum lock_usage_bit bit_forwards, const char *irqclass)
1252 int ret;
1254 find_usage_bit = bit_backwards;
1255 /* fills in <backwards_match> */
1256 ret = find_usage_backwards(hlock_class(prev), 0);
1257 if (!ret || ret == 1)
1258 return ret;
1260 find_usage_bit = bit_forwards;
1261 ret = find_usage_forwards(hlock_class(next), 0);
1262 if (!ret || ret == 1)
1263 return ret;
1264 /* ret == 2 */
1265 return print_bad_irq_dependency(curr, prev, next,
1266 bit_backwards, bit_forwards, irqclass);
1269 static const char *state_names[] = {
1270 #define LOCKDEP_STATE(__STATE) \
1271 __stringify(__STATE),
1272 #include "lockdep_states.h"
1273 #undef LOCKDEP_STATE
1276 static const char *state_rnames[] = {
1277 #define LOCKDEP_STATE(__STATE) \
1278 __stringify(__STATE)"-READ",
1279 #include "lockdep_states.h"
1280 #undef LOCKDEP_STATE
1283 static inline const char *state_name(enum lock_usage_bit bit)
1285 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1288 static int exclusive_bit(int new_bit)
1291 * USED_IN
1292 * USED_IN_READ
1293 * ENABLED
1294 * ENABLED_READ
1296 * bit 0 - write/read
1297 * bit 1 - used_in/enabled
1298 * bit 2+ state
1301 int state = new_bit & ~3;
1302 int dir = new_bit & 2;
1305 * keep state, bit flip the direction and strip read.
1307 return state | (dir ^ 2);
1310 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1311 struct held_lock *next, enum lock_usage_bit bit)
1314 * Prove that the new dependency does not connect a hardirq-safe
1315 * lock with a hardirq-unsafe lock - to achieve this we search
1316 * the backwards-subgraph starting at <prev>, and the
1317 * forwards-subgraph starting at <next>:
1319 if (!check_usage(curr, prev, next, bit,
1320 exclusive_bit(bit), state_name(bit)))
1321 return 0;
1323 bit++; /* _READ */
1326 * Prove that the new dependency does not connect a hardirq-safe-read
1327 * lock with a hardirq-unsafe lock - to achieve this we search
1328 * the backwards-subgraph starting at <prev>, and the
1329 * forwards-subgraph starting at <next>:
1331 if (!check_usage(curr, prev, next, bit,
1332 exclusive_bit(bit), state_name(bit)))
1333 return 0;
1335 return 1;
1338 static int
1339 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1340 struct held_lock *next)
1342 #define LOCKDEP_STATE(__STATE) \
1343 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1344 return 0;
1345 #include "lockdep_states.h"
1346 #undef LOCKDEP_STATE
1348 return 1;
1351 static void inc_chains(void)
1353 if (current->hardirq_context)
1354 nr_hardirq_chains++;
1355 else {
1356 if (current->softirq_context)
1357 nr_softirq_chains++;
1358 else
1359 nr_process_chains++;
1363 #else
1365 static inline int
1366 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1367 struct held_lock *next)
1369 return 1;
1372 static inline void inc_chains(void)
1374 nr_process_chains++;
1377 #endif
1379 static int
1380 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1381 struct held_lock *next)
1383 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1384 return 0;
1386 printk("\n=============================================\n");
1387 printk( "[ INFO: possible recursive locking detected ]\n");
1388 print_kernel_version();
1389 printk( "---------------------------------------------\n");
1390 printk("%s/%d is trying to acquire lock:\n",
1391 curr->comm, task_pid_nr(curr));
1392 print_lock(next);
1393 printk("\nbut task is already holding lock:\n");
1394 print_lock(prev);
1396 printk("\nother info that might help us debug this:\n");
1397 lockdep_print_held_locks(curr);
1399 printk("\nstack backtrace:\n");
1400 dump_stack();
1402 return 0;
1406 * Check whether we are holding such a class already.
1408 * (Note that this has to be done separately, because the graph cannot
1409 * detect such classes of deadlocks.)
1411 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1413 static int
1414 check_deadlock(struct task_struct *curr, struct held_lock *next,
1415 struct lockdep_map *next_instance, int read)
1417 struct held_lock *prev;
1418 struct held_lock *nest = NULL;
1419 int i;
1421 for (i = 0; i < curr->lockdep_depth; i++) {
1422 prev = curr->held_locks + i;
1424 if (prev->instance == next->nest_lock)
1425 nest = prev;
1427 if (hlock_class(prev) != hlock_class(next))
1428 continue;
1431 * Allow read-after-read recursion of the same
1432 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1434 if ((read == 2) && prev->read)
1435 return 2;
1438 * We're holding the nest_lock, which serializes this lock's
1439 * nesting behaviour.
1441 if (nest)
1442 return 2;
1444 return print_deadlock_bug(curr, prev, next);
1446 return 1;
1450 * There was a chain-cache miss, and we are about to add a new dependency
1451 * to a previous lock. We recursively validate the following rules:
1453 * - would the adding of the <prev> -> <next> dependency create a
1454 * circular dependency in the graph? [== circular deadlock]
1456 * - does the new prev->next dependency connect any hardirq-safe lock
1457 * (in the full backwards-subgraph starting at <prev>) with any
1458 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1459 * <next>)? [== illegal lock inversion with hardirq contexts]
1461 * - does the new prev->next dependency connect any softirq-safe lock
1462 * (in the full backwards-subgraph starting at <prev>) with any
1463 * softirq-unsafe lock (in the full forwards-subgraph starting at
1464 * <next>)? [== illegal lock inversion with softirq contexts]
1466 * any of these scenarios could lead to a deadlock.
1468 * Then if all the validations pass, we add the forwards and backwards
1469 * dependency.
1471 static int
1472 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1473 struct held_lock *next, int distance)
1475 struct lock_list *entry;
1476 int ret;
1479 * Prove that the new <prev> -> <next> dependency would not
1480 * create a circular dependency in the graph. (We do this by
1481 * forward-recursing into the graph starting at <next>, and
1482 * checking whether we can reach <prev>.)
1484 * We are using global variables to control the recursion, to
1485 * keep the stackframe size of the recursive functions low:
1487 check_source = next;
1488 check_target = prev;
1489 if (!(check_noncircular(hlock_class(next), 0)))
1490 return print_circular_bug_tail();
1492 if (!check_prev_add_irq(curr, prev, next))
1493 return 0;
1496 * For recursive read-locks we do all the dependency checks,
1497 * but we dont store read-triggered dependencies (only
1498 * write-triggered dependencies). This ensures that only the
1499 * write-side dependencies matter, and that if for example a
1500 * write-lock never takes any other locks, then the reads are
1501 * equivalent to a NOP.
1503 if (next->read == 2 || prev->read == 2)
1504 return 1;
1506 * Is the <prev> -> <next> dependency already present?
1508 * (this may occur even though this is a new chain: consider
1509 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1510 * chains - the second one will be new, but L1 already has
1511 * L2 added to its dependency list, due to the first chain.)
1513 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1514 if (entry->class == hlock_class(next)) {
1515 if (distance == 1)
1516 entry->distance = 1;
1517 return 2;
1522 * Ok, all validations passed, add the new lock
1523 * to the previous lock's dependency list:
1525 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1526 &hlock_class(prev)->locks_after,
1527 next->acquire_ip, distance);
1529 if (!ret)
1530 return 0;
1532 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1533 &hlock_class(next)->locks_before,
1534 next->acquire_ip, distance);
1535 if (!ret)
1536 return 0;
1539 * Debugging printouts:
1541 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1542 graph_unlock();
1543 printk("\n new dependency: ");
1544 print_lock_name(hlock_class(prev));
1545 printk(" => ");
1546 print_lock_name(hlock_class(next));
1547 printk("\n");
1548 dump_stack();
1549 return graph_lock();
1551 return 1;
1555 * Add the dependency to all directly-previous locks that are 'relevant'.
1556 * The ones that are relevant are (in increasing distance from curr):
1557 * all consecutive trylock entries and the final non-trylock entry - or
1558 * the end of this context's lock-chain - whichever comes first.
1560 static int
1561 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1563 int depth = curr->lockdep_depth;
1564 struct held_lock *hlock;
1567 * Debugging checks.
1569 * Depth must not be zero for a non-head lock:
1571 if (!depth)
1572 goto out_bug;
1574 * At least two relevant locks must exist for this
1575 * to be a head:
1577 if (curr->held_locks[depth].irq_context !=
1578 curr->held_locks[depth-1].irq_context)
1579 goto out_bug;
1581 for (;;) {
1582 int distance = curr->lockdep_depth - depth + 1;
1583 hlock = curr->held_locks + depth-1;
1585 * Only non-recursive-read entries get new dependencies
1586 * added:
1588 if (hlock->read != 2) {
1589 if (!check_prev_add(curr, hlock, next, distance))
1590 return 0;
1592 * Stop after the first non-trylock entry,
1593 * as non-trylock entries have added their
1594 * own direct dependencies already, so this
1595 * lock is connected to them indirectly:
1597 if (!hlock->trylock)
1598 break;
1600 depth--;
1602 * End of lock-stack?
1604 if (!depth)
1605 break;
1607 * Stop the search if we cross into another context:
1609 if (curr->held_locks[depth].irq_context !=
1610 curr->held_locks[depth-1].irq_context)
1611 break;
1613 return 1;
1614 out_bug:
1615 if (!debug_locks_off_graph_unlock())
1616 return 0;
1618 WARN_ON(1);
1620 return 0;
1623 unsigned long nr_lock_chains;
1624 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1625 int nr_chain_hlocks;
1626 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1628 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1630 return lock_classes + chain_hlocks[chain->base + i];
1634 * Look up a dependency chain. If the key is not present yet then
1635 * add it and return 1 - in this case the new dependency chain is
1636 * validated. If the key is already hashed, return 0.
1637 * (On return with 1 graph_lock is held.)
1639 static inline int lookup_chain_cache(struct task_struct *curr,
1640 struct held_lock *hlock,
1641 u64 chain_key)
1643 struct lock_class *class = hlock_class(hlock);
1644 struct list_head *hash_head = chainhashentry(chain_key);
1645 struct lock_chain *chain;
1646 struct held_lock *hlock_curr, *hlock_next;
1647 int i, j, n, cn;
1649 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1650 return 0;
1652 * We can walk it lock-free, because entries only get added
1653 * to the hash:
1655 list_for_each_entry(chain, hash_head, entry) {
1656 if (chain->chain_key == chain_key) {
1657 cache_hit:
1658 debug_atomic_inc(&chain_lookup_hits);
1659 if (very_verbose(class))
1660 printk("\nhash chain already cached, key: "
1661 "%016Lx tail class: [%p] %s\n",
1662 (unsigned long long)chain_key,
1663 class->key, class->name);
1664 return 0;
1667 if (very_verbose(class))
1668 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1669 (unsigned long long)chain_key, class->key, class->name);
1671 * Allocate a new chain entry from the static array, and add
1672 * it to the hash:
1674 if (!graph_lock())
1675 return 0;
1677 * We have to walk the chain again locked - to avoid duplicates:
1679 list_for_each_entry(chain, hash_head, entry) {
1680 if (chain->chain_key == chain_key) {
1681 graph_unlock();
1682 goto cache_hit;
1685 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1686 if (!debug_locks_off_graph_unlock())
1687 return 0;
1689 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1690 printk("turning off the locking correctness validator.\n");
1691 return 0;
1693 chain = lock_chains + nr_lock_chains++;
1694 chain->chain_key = chain_key;
1695 chain->irq_context = hlock->irq_context;
1696 /* Find the first held_lock of current chain */
1697 hlock_next = hlock;
1698 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1699 hlock_curr = curr->held_locks + i;
1700 if (hlock_curr->irq_context != hlock_next->irq_context)
1701 break;
1702 hlock_next = hlock;
1704 i++;
1705 chain->depth = curr->lockdep_depth + 1 - i;
1706 cn = nr_chain_hlocks;
1707 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1708 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1709 if (n == cn)
1710 break;
1711 cn = n;
1713 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1714 chain->base = cn;
1715 for (j = 0; j < chain->depth - 1; j++, i++) {
1716 int lock_id = curr->held_locks[i].class_idx - 1;
1717 chain_hlocks[chain->base + j] = lock_id;
1719 chain_hlocks[chain->base + j] = class - lock_classes;
1721 list_add_tail_rcu(&chain->entry, hash_head);
1722 debug_atomic_inc(&chain_lookup_misses);
1723 inc_chains();
1725 return 1;
1728 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1729 struct held_lock *hlock, int chain_head, u64 chain_key)
1732 * Trylock needs to maintain the stack of held locks, but it
1733 * does not add new dependencies, because trylock can be done
1734 * in any order.
1736 * We look up the chain_key and do the O(N^2) check and update of
1737 * the dependencies only if this is a new dependency chain.
1738 * (If lookup_chain_cache() returns with 1 it acquires
1739 * graph_lock for us)
1741 if (!hlock->trylock && (hlock->check == 2) &&
1742 lookup_chain_cache(curr, hlock, chain_key)) {
1744 * Check whether last held lock:
1746 * - is irq-safe, if this lock is irq-unsafe
1747 * - is softirq-safe, if this lock is hardirq-unsafe
1749 * And check whether the new lock's dependency graph
1750 * could lead back to the previous lock.
1752 * any of these scenarios could lead to a deadlock. If
1753 * All validations
1755 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1757 if (!ret)
1758 return 0;
1760 * Mark recursive read, as we jump over it when
1761 * building dependencies (just like we jump over
1762 * trylock entries):
1764 if (ret == 2)
1765 hlock->read = 2;
1767 * Add dependency only if this lock is not the head
1768 * of the chain, and if it's not a secondary read-lock:
1770 if (!chain_head && ret != 2)
1771 if (!check_prevs_add(curr, hlock))
1772 return 0;
1773 graph_unlock();
1774 } else
1775 /* after lookup_chain_cache(): */
1776 if (unlikely(!debug_locks))
1777 return 0;
1779 return 1;
1781 #else
1782 static inline int validate_chain(struct task_struct *curr,
1783 struct lockdep_map *lock, struct held_lock *hlock,
1784 int chain_head, u64 chain_key)
1786 return 1;
1788 #endif
1791 * We are building curr_chain_key incrementally, so double-check
1792 * it from scratch, to make sure that it's done correctly:
1794 static void check_chain_key(struct task_struct *curr)
1796 #ifdef CONFIG_DEBUG_LOCKDEP
1797 struct held_lock *hlock, *prev_hlock = NULL;
1798 unsigned int i, id;
1799 u64 chain_key = 0;
1801 for (i = 0; i < curr->lockdep_depth; i++) {
1802 hlock = curr->held_locks + i;
1803 if (chain_key != hlock->prev_chain_key) {
1804 debug_locks_off();
1805 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1806 curr->lockdep_depth, i,
1807 (unsigned long long)chain_key,
1808 (unsigned long long)hlock->prev_chain_key);
1809 return;
1811 id = hlock->class_idx - 1;
1812 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1813 return;
1815 if (prev_hlock && (prev_hlock->irq_context !=
1816 hlock->irq_context))
1817 chain_key = 0;
1818 chain_key = iterate_chain_key(chain_key, id);
1819 prev_hlock = hlock;
1821 if (chain_key != curr->curr_chain_key) {
1822 debug_locks_off();
1823 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1824 curr->lockdep_depth, i,
1825 (unsigned long long)chain_key,
1826 (unsigned long long)curr->curr_chain_key);
1828 #endif
1831 static int
1832 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1833 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1835 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1836 return 0;
1838 printk("\n=================================\n");
1839 printk( "[ INFO: inconsistent lock state ]\n");
1840 print_kernel_version();
1841 printk( "---------------------------------\n");
1843 printk("inconsistent {%s} -> {%s} usage.\n",
1844 usage_str[prev_bit], usage_str[new_bit]);
1846 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1847 curr->comm, task_pid_nr(curr),
1848 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1849 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1850 trace_hardirqs_enabled(curr),
1851 trace_softirqs_enabled(curr));
1852 print_lock(this);
1854 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1855 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
1857 print_irqtrace_events(curr);
1858 printk("\nother info that might help us debug this:\n");
1859 lockdep_print_held_locks(curr);
1861 printk("\nstack backtrace:\n");
1862 dump_stack();
1864 return 0;
1868 * Print out an error if an invalid bit is set:
1870 static inline int
1871 valid_state(struct task_struct *curr, struct held_lock *this,
1872 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1874 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
1875 return print_usage_bug(curr, this, bad_bit, new_bit);
1876 return 1;
1879 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1880 enum lock_usage_bit new_bit);
1882 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1885 * print irq inversion bug:
1887 static int
1888 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1889 struct held_lock *this, int forwards,
1890 const char *irqclass)
1892 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1893 return 0;
1895 printk("\n=========================================================\n");
1896 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1897 print_kernel_version();
1898 printk( "---------------------------------------------------------\n");
1899 printk("%s/%d just changed the state of lock:\n",
1900 curr->comm, task_pid_nr(curr));
1901 print_lock(this);
1902 if (forwards)
1903 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1904 else
1905 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1906 print_lock_name(other);
1907 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1909 printk("\nother info that might help us debug this:\n");
1910 lockdep_print_held_locks(curr);
1912 printk("\nthe first lock's dependencies:\n");
1913 print_lock_dependencies(hlock_class(this), 0);
1915 printk("\nthe second lock's dependencies:\n");
1916 print_lock_dependencies(other, 0);
1918 printk("\nstack backtrace:\n");
1919 dump_stack();
1921 return 0;
1925 * Prove that in the forwards-direction subgraph starting at <this>
1926 * there is no lock matching <mask>:
1928 static int
1929 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1930 enum lock_usage_bit bit, const char *irqclass)
1932 int ret;
1934 find_usage_bit = bit;
1935 /* fills in <forwards_match> */
1936 ret = find_usage_forwards(hlock_class(this), 0);
1937 if (!ret || ret == 1)
1938 return ret;
1940 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1944 * Prove that in the backwards-direction subgraph starting at <this>
1945 * there is no lock matching <mask>:
1947 static int
1948 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1949 enum lock_usage_bit bit, const char *irqclass)
1951 int ret;
1953 find_usage_bit = bit;
1954 /* fills in <backwards_match> */
1955 ret = find_usage_backwards(hlock_class(this), 0);
1956 if (!ret || ret == 1)
1957 return ret;
1959 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1962 void print_irqtrace_events(struct task_struct *curr)
1964 printk("irq event stamp: %u\n", curr->irq_events);
1965 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1966 print_ip_sym(curr->hardirq_enable_ip);
1967 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1968 print_ip_sym(curr->hardirq_disable_ip);
1969 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1970 print_ip_sym(curr->softirq_enable_ip);
1971 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1972 print_ip_sym(curr->softirq_disable_ip);
1975 static int HARDIRQ_verbose(struct lock_class *class)
1977 #if HARDIRQ_VERBOSE
1978 return class_filter(class);
1979 #endif
1980 return 0;
1983 static int SOFTIRQ_verbose(struct lock_class *class)
1985 #if SOFTIRQ_VERBOSE
1986 return class_filter(class);
1987 #endif
1988 return 0;
1991 static int RECLAIM_FS_verbose(struct lock_class *class)
1993 #if RECLAIM_VERBOSE
1994 return class_filter(class);
1995 #endif
1996 return 0;
1999 #define STRICT_READ_CHECKS 1
2001 static int (*state_verbose_f[])(struct lock_class *class) = {
2002 #define LOCKDEP_STATE(__STATE) \
2003 __STATE##_verbose,
2004 #include "lockdep_states.h"
2005 #undef LOCKDEP_STATE
2008 static inline int state_verbose(enum lock_usage_bit bit,
2009 struct lock_class *class)
2011 return state_verbose_f[bit >> 2](class);
2014 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2015 enum lock_usage_bit bit, const char *name);
2017 static int
2018 mark_lock_irq(struct task_struct *curr, struct held_lock *this, int new_bit)
2020 int excl_bit = exclusive_bit(new_bit);
2021 int read = new_bit & 1;
2022 int dir = new_bit & 2;
2025 * mark USED_IN has to look forwards -- to ensure no dependency
2026 * has ENABLED state, which would allow recursion deadlocks.
2028 * mark ENABLED has to look backwards -- to ensure no dependee
2029 * has USED_IN state, which, again, would allow recursion deadlocks.
2031 check_usage_f usage = dir ?
2032 check_usage_backwards : check_usage_forwards;
2035 * Validate that this particular lock does not have conflicting
2036 * usage states.
2038 if (!valid_state(curr, this, new_bit, excl_bit))
2039 return 0;
2042 * Validate that the lock dependencies don't have conflicting usage
2043 * states.
2045 if ((!read || !dir || STRICT_READ_CHECKS) &&
2046 !usage(curr, this, excl_bit, state_name(new_bit)))
2047 return 0;
2050 * Check for read in write conflicts
2052 if (!read) {
2053 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2054 return 0;
2056 if (STRICT_READ_CHECKS &&
2057 !usage(curr, this, excl_bit + 1,
2058 state_name(new_bit + 1)))
2059 return 0;
2062 if (state_verbose(new_bit, hlock_class(this)))
2063 return 2;
2065 return 1;
2068 enum mark_type {
2069 #define LOCKDEP_STATE(__STATE) __STATE,
2070 #include "lockdep_states.h"
2071 #undef LOCKDEP_STATE
2075 * Mark all held locks with a usage bit:
2077 static int
2078 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2080 enum lock_usage_bit usage_bit;
2081 struct held_lock *hlock;
2082 int i;
2084 for (i = 0; i < curr->lockdep_depth; i++) {
2085 hlock = curr->held_locks + i;
2087 usage_bit = 2 + (mark << 2); /* ENABLED */
2088 if (hlock->read)
2089 usage_bit += 1; /* READ */
2091 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2093 if (!mark_lock(curr, hlock, usage_bit))
2094 return 0;
2097 return 1;
2101 * Debugging helper: via this flag we know that we are in
2102 * 'early bootup code', and will warn about any invalid irqs-on event:
2104 static int early_boot_irqs_enabled;
2106 void early_boot_irqs_off(void)
2108 early_boot_irqs_enabled = 0;
2111 void early_boot_irqs_on(void)
2113 early_boot_irqs_enabled = 1;
2117 * Hardirqs will be enabled:
2119 void trace_hardirqs_on_caller(unsigned long ip)
2121 struct task_struct *curr = current;
2123 time_hardirqs_on(CALLER_ADDR0, ip);
2125 if (unlikely(!debug_locks || current->lockdep_recursion))
2126 return;
2128 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2129 return;
2131 if (unlikely(curr->hardirqs_enabled)) {
2132 debug_atomic_inc(&redundant_hardirqs_on);
2133 return;
2135 /* we'll do an OFF -> ON transition: */
2136 curr->hardirqs_enabled = 1;
2138 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2139 return;
2140 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2141 return;
2143 * We are going to turn hardirqs on, so set the
2144 * usage bit for all held locks:
2146 if (!mark_held_locks(curr, HARDIRQ))
2147 return;
2149 * If we have softirqs enabled, then set the usage
2150 * bit for all held locks. (disabled hardirqs prevented
2151 * this bit from being set before)
2153 if (curr->softirqs_enabled)
2154 if (!mark_held_locks(curr, SOFTIRQ))
2155 return;
2157 curr->hardirq_enable_ip = ip;
2158 curr->hardirq_enable_event = ++curr->irq_events;
2159 debug_atomic_inc(&hardirqs_on_events);
2161 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2163 void trace_hardirqs_on(void)
2165 trace_hardirqs_on_caller(CALLER_ADDR0);
2167 EXPORT_SYMBOL(trace_hardirqs_on);
2170 * Hardirqs were disabled:
2172 void trace_hardirqs_off_caller(unsigned long ip)
2174 struct task_struct *curr = current;
2176 time_hardirqs_off(CALLER_ADDR0, ip);
2178 if (unlikely(!debug_locks || current->lockdep_recursion))
2179 return;
2181 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2182 return;
2184 if (curr->hardirqs_enabled) {
2186 * We have done an ON -> OFF transition:
2188 curr->hardirqs_enabled = 0;
2189 curr->hardirq_disable_ip = ip;
2190 curr->hardirq_disable_event = ++curr->irq_events;
2191 debug_atomic_inc(&hardirqs_off_events);
2192 } else
2193 debug_atomic_inc(&redundant_hardirqs_off);
2195 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2197 void trace_hardirqs_off(void)
2199 trace_hardirqs_off_caller(CALLER_ADDR0);
2201 EXPORT_SYMBOL(trace_hardirqs_off);
2204 * Softirqs will be enabled:
2206 void trace_softirqs_on(unsigned long ip)
2208 struct task_struct *curr = current;
2210 if (unlikely(!debug_locks))
2211 return;
2213 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2214 return;
2216 if (curr->softirqs_enabled) {
2217 debug_atomic_inc(&redundant_softirqs_on);
2218 return;
2222 * We'll do an OFF -> ON transition:
2224 curr->softirqs_enabled = 1;
2225 curr->softirq_enable_ip = ip;
2226 curr->softirq_enable_event = ++curr->irq_events;
2227 debug_atomic_inc(&softirqs_on_events);
2229 * We are going to turn softirqs on, so set the
2230 * usage bit for all held locks, if hardirqs are
2231 * enabled too:
2233 if (curr->hardirqs_enabled)
2234 mark_held_locks(curr, SOFTIRQ);
2238 * Softirqs were disabled:
2240 void trace_softirqs_off(unsigned long ip)
2242 struct task_struct *curr = current;
2244 if (unlikely(!debug_locks))
2245 return;
2247 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2248 return;
2250 if (curr->softirqs_enabled) {
2252 * We have done an ON -> OFF transition:
2254 curr->softirqs_enabled = 0;
2255 curr->softirq_disable_ip = ip;
2256 curr->softirq_disable_event = ++curr->irq_events;
2257 debug_atomic_inc(&softirqs_off_events);
2258 DEBUG_LOCKS_WARN_ON(!softirq_count());
2259 } else
2260 debug_atomic_inc(&redundant_softirqs_off);
2263 static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2265 struct task_struct *curr = current;
2267 if (unlikely(!debug_locks))
2268 return;
2270 /* no reclaim without waiting on it */
2271 if (!(gfp_mask & __GFP_WAIT))
2272 return;
2274 /* this guy won't enter reclaim */
2275 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2276 return;
2278 /* We're only interested __GFP_FS allocations for now */
2279 if (!(gfp_mask & __GFP_FS))
2280 return;
2282 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2283 return;
2285 mark_held_locks(curr, RECLAIM_FS);
2288 static void check_flags(unsigned long flags);
2290 void lockdep_trace_alloc(gfp_t gfp_mask)
2292 unsigned long flags;
2294 if (unlikely(current->lockdep_recursion))
2295 return;
2297 raw_local_irq_save(flags);
2298 check_flags(flags);
2299 current->lockdep_recursion = 1;
2300 __lockdep_trace_alloc(gfp_mask, flags);
2301 current->lockdep_recursion = 0;
2302 raw_local_irq_restore(flags);
2305 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2308 * If non-trylock use in a hardirq or softirq context, then
2309 * mark the lock as used in these contexts:
2311 if (!hlock->trylock) {
2312 if (hlock->read) {
2313 if (curr->hardirq_context)
2314 if (!mark_lock(curr, hlock,
2315 LOCK_USED_IN_HARDIRQ_READ))
2316 return 0;
2317 if (curr->softirq_context)
2318 if (!mark_lock(curr, hlock,
2319 LOCK_USED_IN_SOFTIRQ_READ))
2320 return 0;
2321 } else {
2322 if (curr->hardirq_context)
2323 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2324 return 0;
2325 if (curr->softirq_context)
2326 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2327 return 0;
2330 if (!hlock->hardirqs_off) {
2331 if (hlock->read) {
2332 if (!mark_lock(curr, hlock,
2333 LOCK_ENABLED_HARDIRQ_READ))
2334 return 0;
2335 if (curr->softirqs_enabled)
2336 if (!mark_lock(curr, hlock,
2337 LOCK_ENABLED_SOFTIRQ_READ))
2338 return 0;
2339 } else {
2340 if (!mark_lock(curr, hlock,
2341 LOCK_ENABLED_HARDIRQ))
2342 return 0;
2343 if (curr->softirqs_enabled)
2344 if (!mark_lock(curr, hlock,
2345 LOCK_ENABLED_SOFTIRQ))
2346 return 0;
2351 * We reuse the irq context infrastructure more broadly as a general
2352 * context checking code. This tests GFP_FS recursion (a lock taken
2353 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2354 * allocation).
2356 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2357 if (hlock->read) {
2358 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2359 return 0;
2360 } else {
2361 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2362 return 0;
2366 return 1;
2369 static int separate_irq_context(struct task_struct *curr,
2370 struct held_lock *hlock)
2372 unsigned int depth = curr->lockdep_depth;
2375 * Keep track of points where we cross into an interrupt context:
2377 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2378 curr->softirq_context;
2379 if (depth) {
2380 struct held_lock *prev_hlock;
2382 prev_hlock = curr->held_locks + depth-1;
2384 * If we cross into another context, reset the
2385 * hash key (this also prevents the checking and the
2386 * adding of the dependency to 'prev'):
2388 if (prev_hlock->irq_context != hlock->irq_context)
2389 return 1;
2391 return 0;
2394 #else
2396 static inline
2397 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2398 enum lock_usage_bit new_bit)
2400 WARN_ON(1);
2401 return 1;
2404 static inline int mark_irqflags(struct task_struct *curr,
2405 struct held_lock *hlock)
2407 return 1;
2410 static inline int separate_irq_context(struct task_struct *curr,
2411 struct held_lock *hlock)
2413 return 0;
2416 void lockdep_trace_alloc(gfp_t gfp_mask)
2420 #endif
2423 * Mark a lock with a usage bit, and validate the state transition:
2425 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2426 enum lock_usage_bit new_bit)
2428 unsigned int new_mask = 1 << new_bit, ret = 1;
2431 * If already set then do not dirty the cacheline,
2432 * nor do any checks:
2434 if (likely(hlock_class(this)->usage_mask & new_mask))
2435 return 1;
2437 if (!graph_lock())
2438 return 0;
2440 * Make sure we didnt race:
2442 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2443 graph_unlock();
2444 return 1;
2447 hlock_class(this)->usage_mask |= new_mask;
2449 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2450 return 0;
2452 switch (new_bit) {
2453 #define LOCKDEP_STATE(__STATE) \
2454 case LOCK_USED_IN_##__STATE: \
2455 case LOCK_USED_IN_##__STATE##_READ: \
2456 case LOCK_ENABLED_##__STATE: \
2457 case LOCK_ENABLED_##__STATE##_READ:
2458 #include "lockdep_states.h"
2459 #undef LOCKDEP_STATE
2460 ret = mark_lock_irq(curr, this, new_bit);
2461 if (!ret)
2462 return 0;
2463 break;
2464 case LOCK_USED:
2465 debug_atomic_dec(&nr_unused_locks);
2466 break;
2467 default:
2468 if (!debug_locks_off_graph_unlock())
2469 return 0;
2470 WARN_ON(1);
2471 return 0;
2474 graph_unlock();
2477 * We must printk outside of the graph_lock:
2479 if (ret == 2) {
2480 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2481 print_lock(this);
2482 print_irqtrace_events(curr);
2483 dump_stack();
2486 return ret;
2490 * Initialize a lock instance's lock-class mapping info:
2492 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2493 struct lock_class_key *key, int subclass)
2495 if (unlikely(!debug_locks))
2496 return;
2498 if (DEBUG_LOCKS_WARN_ON(!key))
2499 return;
2500 if (DEBUG_LOCKS_WARN_ON(!name))
2501 return;
2503 * Sanity check, the lock-class key must be persistent:
2505 if (!static_obj(key)) {
2506 printk("BUG: key %p not in .data!\n", key);
2507 DEBUG_LOCKS_WARN_ON(1);
2508 return;
2510 lock->name = name;
2511 lock->key = key;
2512 lock->class_cache = NULL;
2513 #ifdef CONFIG_LOCK_STAT
2514 lock->cpu = raw_smp_processor_id();
2515 #endif
2516 if (subclass)
2517 register_lock_class(lock, subclass, 1);
2519 EXPORT_SYMBOL_GPL(lockdep_init_map);
2522 * This gets called for every mutex_lock*()/spin_lock*() operation.
2523 * We maintain the dependency maps and validate the locking attempt:
2525 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2526 int trylock, int read, int check, int hardirqs_off,
2527 struct lockdep_map *nest_lock, unsigned long ip)
2529 struct task_struct *curr = current;
2530 struct lock_class *class = NULL;
2531 struct held_lock *hlock;
2532 unsigned int depth, id;
2533 int chain_head = 0;
2534 u64 chain_key;
2536 if (!prove_locking)
2537 check = 1;
2539 if (unlikely(!debug_locks))
2540 return 0;
2542 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2543 return 0;
2545 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2546 debug_locks_off();
2547 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2548 printk("turning off the locking correctness validator.\n");
2549 return 0;
2552 if (!subclass)
2553 class = lock->class_cache;
2555 * Not cached yet or subclass?
2557 if (unlikely(!class)) {
2558 class = register_lock_class(lock, subclass, 0);
2559 if (!class)
2560 return 0;
2562 debug_atomic_inc((atomic_t *)&class->ops);
2563 if (very_verbose(class)) {
2564 printk("\nacquire class [%p] %s", class->key, class->name);
2565 if (class->name_version > 1)
2566 printk("#%d", class->name_version);
2567 printk("\n");
2568 dump_stack();
2572 * Add the lock to the list of currently held locks.
2573 * (we dont increase the depth just yet, up until the
2574 * dependency checks are done)
2576 depth = curr->lockdep_depth;
2577 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2578 return 0;
2580 hlock = curr->held_locks + depth;
2581 if (DEBUG_LOCKS_WARN_ON(!class))
2582 return 0;
2583 hlock->class_idx = class - lock_classes + 1;
2584 hlock->acquire_ip = ip;
2585 hlock->instance = lock;
2586 hlock->nest_lock = nest_lock;
2587 hlock->trylock = trylock;
2588 hlock->read = read;
2589 hlock->check = check;
2590 hlock->hardirqs_off = !!hardirqs_off;
2591 #ifdef CONFIG_LOCK_STAT
2592 hlock->waittime_stamp = 0;
2593 hlock->holdtime_stamp = sched_clock();
2594 #endif
2596 if (check == 2 && !mark_irqflags(curr, hlock))
2597 return 0;
2599 /* mark it as used: */
2600 if (!mark_lock(curr, hlock, LOCK_USED))
2601 return 0;
2604 * Calculate the chain hash: it's the combined hash of all the
2605 * lock keys along the dependency chain. We save the hash value
2606 * at every step so that we can get the current hash easily
2607 * after unlock. The chain hash is then used to cache dependency
2608 * results.
2610 * The 'key ID' is what is the most compact key value to drive
2611 * the hash, not class->key.
2613 id = class - lock_classes;
2614 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2615 return 0;
2617 chain_key = curr->curr_chain_key;
2618 if (!depth) {
2619 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2620 return 0;
2621 chain_head = 1;
2624 hlock->prev_chain_key = chain_key;
2625 if (separate_irq_context(curr, hlock)) {
2626 chain_key = 0;
2627 chain_head = 1;
2629 chain_key = iterate_chain_key(chain_key, id);
2631 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2632 return 0;
2634 curr->curr_chain_key = chain_key;
2635 curr->lockdep_depth++;
2636 check_chain_key(curr);
2637 #ifdef CONFIG_DEBUG_LOCKDEP
2638 if (unlikely(!debug_locks))
2639 return 0;
2640 #endif
2641 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2642 debug_locks_off();
2643 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2644 printk("turning off the locking correctness validator.\n");
2645 return 0;
2648 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2649 max_lockdep_depth = curr->lockdep_depth;
2651 return 1;
2654 static int
2655 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2656 unsigned long ip)
2658 if (!debug_locks_off())
2659 return 0;
2660 if (debug_locks_silent)
2661 return 0;
2663 printk("\n=====================================\n");
2664 printk( "[ BUG: bad unlock balance detected! ]\n");
2665 printk( "-------------------------------------\n");
2666 printk("%s/%d is trying to release lock (",
2667 curr->comm, task_pid_nr(curr));
2668 print_lockdep_cache(lock);
2669 printk(") at:\n");
2670 print_ip_sym(ip);
2671 printk("but there are no more locks to release!\n");
2672 printk("\nother info that might help us debug this:\n");
2673 lockdep_print_held_locks(curr);
2675 printk("\nstack backtrace:\n");
2676 dump_stack();
2678 return 0;
2682 * Common debugging checks for both nested and non-nested unlock:
2684 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2685 unsigned long ip)
2687 if (unlikely(!debug_locks))
2688 return 0;
2689 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2690 return 0;
2692 if (curr->lockdep_depth <= 0)
2693 return print_unlock_inbalance_bug(curr, lock, ip);
2695 return 1;
2698 static int
2699 __lock_set_class(struct lockdep_map *lock, const char *name,
2700 struct lock_class_key *key, unsigned int subclass,
2701 unsigned long ip)
2703 struct task_struct *curr = current;
2704 struct held_lock *hlock, *prev_hlock;
2705 struct lock_class *class;
2706 unsigned int depth;
2707 int i;
2709 depth = curr->lockdep_depth;
2710 if (DEBUG_LOCKS_WARN_ON(!depth))
2711 return 0;
2713 prev_hlock = NULL;
2714 for (i = depth-1; i >= 0; i--) {
2715 hlock = curr->held_locks + i;
2717 * We must not cross into another context:
2719 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2720 break;
2721 if (hlock->instance == lock)
2722 goto found_it;
2723 prev_hlock = hlock;
2725 return print_unlock_inbalance_bug(curr, lock, ip);
2727 found_it:
2728 lockdep_init_map(lock, name, key, 0);
2729 class = register_lock_class(lock, subclass, 0);
2730 hlock->class_idx = class - lock_classes + 1;
2732 curr->lockdep_depth = i;
2733 curr->curr_chain_key = hlock->prev_chain_key;
2735 for (; i < depth; i++) {
2736 hlock = curr->held_locks + i;
2737 if (!__lock_acquire(hlock->instance,
2738 hlock_class(hlock)->subclass, hlock->trylock,
2739 hlock->read, hlock->check, hlock->hardirqs_off,
2740 hlock->nest_lock, hlock->acquire_ip))
2741 return 0;
2744 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2745 return 0;
2746 return 1;
2750 * Remove the lock to the list of currently held locks in a
2751 * potentially non-nested (out of order) manner. This is a
2752 * relatively rare operation, as all the unlock APIs default
2753 * to nested mode (which uses lock_release()):
2755 static int
2756 lock_release_non_nested(struct task_struct *curr,
2757 struct lockdep_map *lock, unsigned long ip)
2759 struct held_lock *hlock, *prev_hlock;
2760 unsigned int depth;
2761 int i;
2764 * Check whether the lock exists in the current stack
2765 * of held locks:
2767 depth = curr->lockdep_depth;
2768 if (DEBUG_LOCKS_WARN_ON(!depth))
2769 return 0;
2771 prev_hlock = NULL;
2772 for (i = depth-1; i >= 0; i--) {
2773 hlock = curr->held_locks + i;
2775 * We must not cross into another context:
2777 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2778 break;
2779 if (hlock->instance == lock)
2780 goto found_it;
2781 prev_hlock = hlock;
2783 return print_unlock_inbalance_bug(curr, lock, ip);
2785 found_it:
2786 lock_release_holdtime(hlock);
2789 * We have the right lock to unlock, 'hlock' points to it.
2790 * Now we remove it from the stack, and add back the other
2791 * entries (if any), recalculating the hash along the way:
2793 curr->lockdep_depth = i;
2794 curr->curr_chain_key = hlock->prev_chain_key;
2796 for (i++; i < depth; i++) {
2797 hlock = curr->held_locks + i;
2798 if (!__lock_acquire(hlock->instance,
2799 hlock_class(hlock)->subclass, hlock->trylock,
2800 hlock->read, hlock->check, hlock->hardirqs_off,
2801 hlock->nest_lock, hlock->acquire_ip))
2802 return 0;
2805 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2806 return 0;
2807 return 1;
2811 * Remove the lock to the list of currently held locks - this gets
2812 * called on mutex_unlock()/spin_unlock*() (or on a failed
2813 * mutex_lock_interruptible()). This is done for unlocks that nest
2814 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2816 static int lock_release_nested(struct task_struct *curr,
2817 struct lockdep_map *lock, unsigned long ip)
2819 struct held_lock *hlock;
2820 unsigned int depth;
2823 * Pop off the top of the lock stack:
2825 depth = curr->lockdep_depth - 1;
2826 hlock = curr->held_locks + depth;
2829 * Is the unlock non-nested:
2831 if (hlock->instance != lock)
2832 return lock_release_non_nested(curr, lock, ip);
2833 curr->lockdep_depth--;
2835 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2836 return 0;
2838 curr->curr_chain_key = hlock->prev_chain_key;
2840 lock_release_holdtime(hlock);
2842 #ifdef CONFIG_DEBUG_LOCKDEP
2843 hlock->prev_chain_key = 0;
2844 hlock->class_idx = 0;
2845 hlock->acquire_ip = 0;
2846 hlock->irq_context = 0;
2847 #endif
2848 return 1;
2852 * Remove the lock to the list of currently held locks - this gets
2853 * called on mutex_unlock()/spin_unlock*() (or on a failed
2854 * mutex_lock_interruptible()). This is done for unlocks that nest
2855 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2857 static void
2858 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2860 struct task_struct *curr = current;
2862 if (!check_unlock(curr, lock, ip))
2863 return;
2865 if (nested) {
2866 if (!lock_release_nested(curr, lock, ip))
2867 return;
2868 } else {
2869 if (!lock_release_non_nested(curr, lock, ip))
2870 return;
2873 check_chain_key(curr);
2877 * Check whether we follow the irq-flags state precisely:
2879 static void check_flags(unsigned long flags)
2881 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2882 defined(CONFIG_TRACE_IRQFLAGS)
2883 if (!debug_locks)
2884 return;
2886 if (irqs_disabled_flags(flags)) {
2887 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2888 printk("possible reason: unannotated irqs-off.\n");
2890 } else {
2891 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2892 printk("possible reason: unannotated irqs-on.\n");
2897 * We dont accurately track softirq state in e.g.
2898 * hardirq contexts (such as on 4KSTACKS), so only
2899 * check if not in hardirq contexts:
2901 if (!hardirq_count()) {
2902 if (softirq_count())
2903 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2904 else
2905 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2908 if (!debug_locks)
2909 print_irqtrace_events(current);
2910 #endif
2913 void lock_set_class(struct lockdep_map *lock, const char *name,
2914 struct lock_class_key *key, unsigned int subclass,
2915 unsigned long ip)
2917 unsigned long flags;
2919 if (unlikely(current->lockdep_recursion))
2920 return;
2922 raw_local_irq_save(flags);
2923 current->lockdep_recursion = 1;
2924 check_flags(flags);
2925 if (__lock_set_class(lock, name, key, subclass, ip))
2926 check_chain_key(current);
2927 current->lockdep_recursion = 0;
2928 raw_local_irq_restore(flags);
2930 EXPORT_SYMBOL_GPL(lock_set_class);
2933 * We are not always called with irqs disabled - do that here,
2934 * and also avoid lockdep recursion:
2936 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2937 int trylock, int read, int check,
2938 struct lockdep_map *nest_lock, unsigned long ip)
2940 unsigned long flags;
2942 if (unlikely(current->lockdep_recursion))
2943 return;
2945 raw_local_irq_save(flags);
2946 check_flags(flags);
2948 current->lockdep_recursion = 1;
2949 __lock_acquire(lock, subclass, trylock, read, check,
2950 irqs_disabled_flags(flags), nest_lock, ip);
2951 current->lockdep_recursion = 0;
2952 raw_local_irq_restore(flags);
2954 EXPORT_SYMBOL_GPL(lock_acquire);
2956 void lock_release(struct lockdep_map *lock, int nested,
2957 unsigned long ip)
2959 unsigned long flags;
2961 if (unlikely(current->lockdep_recursion))
2962 return;
2964 raw_local_irq_save(flags);
2965 check_flags(flags);
2966 current->lockdep_recursion = 1;
2967 __lock_release(lock, nested, ip);
2968 current->lockdep_recursion = 0;
2969 raw_local_irq_restore(flags);
2971 EXPORT_SYMBOL_GPL(lock_release);
2973 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2975 current->lockdep_reclaim_gfp = gfp_mask;
2978 void lockdep_clear_current_reclaim_state(void)
2980 current->lockdep_reclaim_gfp = 0;
2983 #ifdef CONFIG_LOCK_STAT
2984 static int
2985 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2986 unsigned long ip)
2988 if (!debug_locks_off())
2989 return 0;
2990 if (debug_locks_silent)
2991 return 0;
2993 printk("\n=================================\n");
2994 printk( "[ BUG: bad contention detected! ]\n");
2995 printk( "---------------------------------\n");
2996 printk("%s/%d is trying to contend lock (",
2997 curr->comm, task_pid_nr(curr));
2998 print_lockdep_cache(lock);
2999 printk(") at:\n");
3000 print_ip_sym(ip);
3001 printk("but there are no locks held!\n");
3002 printk("\nother info that might help us debug this:\n");
3003 lockdep_print_held_locks(curr);
3005 printk("\nstack backtrace:\n");
3006 dump_stack();
3008 return 0;
3011 static void
3012 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3014 struct task_struct *curr = current;
3015 struct held_lock *hlock, *prev_hlock;
3016 struct lock_class_stats *stats;
3017 unsigned int depth;
3018 int i, contention_point, contending_point;
3020 depth = curr->lockdep_depth;
3021 if (DEBUG_LOCKS_WARN_ON(!depth))
3022 return;
3024 prev_hlock = NULL;
3025 for (i = depth-1; i >= 0; i--) {
3026 hlock = curr->held_locks + i;
3028 * We must not cross into another context:
3030 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3031 break;
3032 if (hlock->instance == lock)
3033 goto found_it;
3034 prev_hlock = hlock;
3036 print_lock_contention_bug(curr, lock, ip);
3037 return;
3039 found_it:
3040 hlock->waittime_stamp = sched_clock();
3042 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3043 contending_point = lock_point(hlock_class(hlock)->contending_point,
3044 lock->ip);
3046 stats = get_lock_stats(hlock_class(hlock));
3047 if (contention_point < LOCKSTAT_POINTS)
3048 stats->contention_point[contention_point]++;
3049 if (contending_point < LOCKSTAT_POINTS)
3050 stats->contending_point[contending_point]++;
3051 if (lock->cpu != smp_processor_id())
3052 stats->bounces[bounce_contended + !!hlock->read]++;
3053 put_lock_stats(stats);
3056 static void
3057 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3059 struct task_struct *curr = current;
3060 struct held_lock *hlock, *prev_hlock;
3061 struct lock_class_stats *stats;
3062 unsigned int depth;
3063 u64 now;
3064 s64 waittime = 0;
3065 int i, cpu;
3067 depth = curr->lockdep_depth;
3068 if (DEBUG_LOCKS_WARN_ON(!depth))
3069 return;
3071 prev_hlock = NULL;
3072 for (i = depth-1; i >= 0; i--) {
3073 hlock = curr->held_locks + i;
3075 * We must not cross into another context:
3077 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3078 break;
3079 if (hlock->instance == lock)
3080 goto found_it;
3081 prev_hlock = hlock;
3083 print_lock_contention_bug(curr, lock, _RET_IP_);
3084 return;
3086 found_it:
3087 cpu = smp_processor_id();
3088 if (hlock->waittime_stamp) {
3089 now = sched_clock();
3090 waittime = now - hlock->waittime_stamp;
3091 hlock->holdtime_stamp = now;
3094 stats = get_lock_stats(hlock_class(hlock));
3095 if (waittime) {
3096 if (hlock->read)
3097 lock_time_inc(&stats->read_waittime, waittime);
3098 else
3099 lock_time_inc(&stats->write_waittime, waittime);
3101 if (lock->cpu != cpu)
3102 stats->bounces[bounce_acquired + !!hlock->read]++;
3103 put_lock_stats(stats);
3105 lock->cpu = cpu;
3106 lock->ip = ip;
3109 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3111 unsigned long flags;
3113 if (unlikely(!lock_stat))
3114 return;
3116 if (unlikely(current->lockdep_recursion))
3117 return;
3119 raw_local_irq_save(flags);
3120 check_flags(flags);
3121 current->lockdep_recursion = 1;
3122 __lock_contended(lock, ip);
3123 current->lockdep_recursion = 0;
3124 raw_local_irq_restore(flags);
3126 EXPORT_SYMBOL_GPL(lock_contended);
3128 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3130 unsigned long flags;
3132 if (unlikely(!lock_stat))
3133 return;
3135 if (unlikely(current->lockdep_recursion))
3136 return;
3138 raw_local_irq_save(flags);
3139 check_flags(flags);
3140 current->lockdep_recursion = 1;
3141 __lock_acquired(lock, ip);
3142 current->lockdep_recursion = 0;
3143 raw_local_irq_restore(flags);
3145 EXPORT_SYMBOL_GPL(lock_acquired);
3146 #endif
3149 * Used by the testsuite, sanitize the validator state
3150 * after a simulated failure:
3153 void lockdep_reset(void)
3155 unsigned long flags;
3156 int i;
3158 raw_local_irq_save(flags);
3159 current->curr_chain_key = 0;
3160 current->lockdep_depth = 0;
3161 current->lockdep_recursion = 0;
3162 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3163 nr_hardirq_chains = 0;
3164 nr_softirq_chains = 0;
3165 nr_process_chains = 0;
3166 debug_locks = 1;
3167 for (i = 0; i < CHAINHASH_SIZE; i++)
3168 INIT_LIST_HEAD(chainhash_table + i);
3169 raw_local_irq_restore(flags);
3172 static void zap_class(struct lock_class *class)
3174 int i;
3177 * Remove all dependencies this lock is
3178 * involved in:
3180 for (i = 0; i < nr_list_entries; i++) {
3181 if (list_entries[i].class == class)
3182 list_del_rcu(&list_entries[i].entry);
3185 * Unhash the class and remove it from the all_lock_classes list:
3187 list_del_rcu(&class->hash_entry);
3188 list_del_rcu(&class->lock_entry);
3190 class->key = NULL;
3193 static inline int within(const void *addr, void *start, unsigned long size)
3195 return addr >= start && addr < start + size;
3198 void lockdep_free_key_range(void *start, unsigned long size)
3200 struct lock_class *class, *next;
3201 struct list_head *head;
3202 unsigned long flags;
3203 int i;
3204 int locked;
3206 raw_local_irq_save(flags);
3207 locked = graph_lock();
3210 * Unhash all classes that were created by this module:
3212 for (i = 0; i < CLASSHASH_SIZE; i++) {
3213 head = classhash_table + i;
3214 if (list_empty(head))
3215 continue;
3216 list_for_each_entry_safe(class, next, head, hash_entry) {
3217 if (within(class->key, start, size))
3218 zap_class(class);
3219 else if (within(class->name, start, size))
3220 zap_class(class);
3224 if (locked)
3225 graph_unlock();
3226 raw_local_irq_restore(flags);
3229 void lockdep_reset_lock(struct lockdep_map *lock)
3231 struct lock_class *class, *next;
3232 struct list_head *head;
3233 unsigned long flags;
3234 int i, j;
3235 int locked;
3237 raw_local_irq_save(flags);
3240 * Remove all classes this lock might have:
3242 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3244 * If the class exists we look it up and zap it:
3246 class = look_up_lock_class(lock, j);
3247 if (class)
3248 zap_class(class);
3251 * Debug check: in the end all mapped classes should
3252 * be gone.
3254 locked = graph_lock();
3255 for (i = 0; i < CLASSHASH_SIZE; i++) {
3256 head = classhash_table + i;
3257 if (list_empty(head))
3258 continue;
3259 list_for_each_entry_safe(class, next, head, hash_entry) {
3260 if (unlikely(class == lock->class_cache)) {
3261 if (debug_locks_off_graph_unlock())
3262 WARN_ON(1);
3263 goto out_restore;
3267 if (locked)
3268 graph_unlock();
3270 out_restore:
3271 raw_local_irq_restore(flags);
3274 void lockdep_init(void)
3276 int i;
3279 * Some architectures have their own start_kernel()
3280 * code which calls lockdep_init(), while we also
3281 * call lockdep_init() from the start_kernel() itself,
3282 * and we want to initialize the hashes only once:
3284 if (lockdep_initialized)
3285 return;
3287 for (i = 0; i < CLASSHASH_SIZE; i++)
3288 INIT_LIST_HEAD(classhash_table + i);
3290 for (i = 0; i < CHAINHASH_SIZE; i++)
3291 INIT_LIST_HEAD(chainhash_table + i);
3293 lockdep_initialized = 1;
3296 void __init lockdep_info(void)
3298 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3300 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3301 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3302 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3303 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3304 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3305 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3306 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3308 printk(" memory used by lock dependency info: %lu kB\n",
3309 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3310 sizeof(struct list_head) * CLASSHASH_SIZE +
3311 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3312 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3313 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3315 printk(" per task-struct memory footprint: %lu bytes\n",
3316 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3318 #ifdef CONFIG_DEBUG_LOCKDEP
3319 if (lockdep_init_error) {
3320 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3321 printk("Call stack leading to lockdep invocation was:\n");
3322 print_stack_trace(&lockdep_init_trace, 0);
3324 #endif
3327 static void
3328 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3329 const void *mem_to, struct held_lock *hlock)
3331 if (!debug_locks_off())
3332 return;
3333 if (debug_locks_silent)
3334 return;
3336 printk("\n=========================\n");
3337 printk( "[ BUG: held lock freed! ]\n");
3338 printk( "-------------------------\n");
3339 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3340 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3341 print_lock(hlock);
3342 lockdep_print_held_locks(curr);
3344 printk("\nstack backtrace:\n");
3345 dump_stack();
3348 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3349 const void* lock_from, unsigned long lock_len)
3351 return lock_from + lock_len <= mem_from ||
3352 mem_from + mem_len <= lock_from;
3356 * Called when kernel memory is freed (or unmapped), or if a lock
3357 * is destroyed or reinitialized - this code checks whether there is
3358 * any held lock in the memory range of <from> to <to>:
3360 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3362 struct task_struct *curr = current;
3363 struct held_lock *hlock;
3364 unsigned long flags;
3365 int i;
3367 if (unlikely(!debug_locks))
3368 return;
3370 local_irq_save(flags);
3371 for (i = 0; i < curr->lockdep_depth; i++) {
3372 hlock = curr->held_locks + i;
3374 if (not_in_range(mem_from, mem_len, hlock->instance,
3375 sizeof(*hlock->instance)))
3376 continue;
3378 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3379 break;
3381 local_irq_restore(flags);
3383 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3385 static void print_held_locks_bug(struct task_struct *curr)
3387 if (!debug_locks_off())
3388 return;
3389 if (debug_locks_silent)
3390 return;
3392 printk("\n=====================================\n");
3393 printk( "[ BUG: lock held at task exit time! ]\n");
3394 printk( "-------------------------------------\n");
3395 printk("%s/%d is exiting with locks still held!\n",
3396 curr->comm, task_pid_nr(curr));
3397 lockdep_print_held_locks(curr);
3399 printk("\nstack backtrace:\n");
3400 dump_stack();
3403 void debug_check_no_locks_held(struct task_struct *task)
3405 if (unlikely(task->lockdep_depth > 0))
3406 print_held_locks_bug(task);
3409 void debug_show_all_locks(void)
3411 struct task_struct *g, *p;
3412 int count = 10;
3413 int unlock = 1;
3415 if (unlikely(!debug_locks)) {
3416 printk("INFO: lockdep is turned off.\n");
3417 return;
3419 printk("\nShowing all locks held in the system:\n");
3422 * Here we try to get the tasklist_lock as hard as possible,
3423 * if not successful after 2 seconds we ignore it (but keep
3424 * trying). This is to enable a debug printout even if a
3425 * tasklist_lock-holding task deadlocks or crashes.
3427 retry:
3428 if (!read_trylock(&tasklist_lock)) {
3429 if (count == 10)
3430 printk("hm, tasklist_lock locked, retrying... ");
3431 if (count) {
3432 count--;
3433 printk(" #%d", 10-count);
3434 mdelay(200);
3435 goto retry;
3437 printk(" ignoring it.\n");
3438 unlock = 0;
3439 } else {
3440 if (count != 10)
3441 printk(KERN_CONT " locked it.\n");
3444 do_each_thread(g, p) {
3446 * It's not reliable to print a task's held locks
3447 * if it's not sleeping (or if it's not the current
3448 * task):
3450 if (p->state == TASK_RUNNING && p != current)
3451 continue;
3452 if (p->lockdep_depth)
3453 lockdep_print_held_locks(p);
3454 if (!unlock)
3455 if (read_trylock(&tasklist_lock))
3456 unlock = 1;
3457 } while_each_thread(g, p);
3459 printk("\n");
3460 printk("=============================================\n\n");
3462 if (unlock)
3463 read_unlock(&tasklist_lock);
3465 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3468 * Careful: only use this function if you are sure that
3469 * the task cannot run in parallel!
3471 void __debug_show_held_locks(struct task_struct *task)
3473 if (unlikely(!debug_locks)) {
3474 printk("INFO: lockdep is turned off.\n");
3475 return;
3477 lockdep_print_held_locks(task);
3479 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3481 void debug_show_held_locks(struct task_struct *task)
3483 __debug_show_held_locks(task);
3485 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3487 void lockdep_sys_exit(void)
3489 struct task_struct *curr = current;
3491 if (unlikely(curr->lockdep_depth)) {
3492 if (!debug_locks_off())
3493 return;
3494 printk("\n================================================\n");
3495 printk( "[ BUG: lock held when returning to user space! ]\n");
3496 printk( "------------------------------------------------\n");
3497 printk("%s/%d is leaving the kernel with locks still held!\n",
3498 curr->comm, curr->pid);
3499 lockdep_print_held_locks(curr);