Revert "Revert "ide: try to use PIO Mode 0 during probe if possible""
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / lockdep.c
blobf74d2d7aa605801f70209cc61f434bdfbc3a2663
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>
47 #include <asm/sections.h>
49 #include "lockdep_internals.h"
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/lockdep.h>
54 #ifdef CONFIG_PROVE_LOCKING
55 int prove_locking = 1;
56 module_param(prove_locking, int, 0644);
57 #else
58 #define prove_locking 0
59 #endif
61 #ifdef CONFIG_LOCK_STAT
62 int lock_stat = 1;
63 module_param(lock_stat, int, 0644);
64 #else
65 #define lock_stat 0
66 #endif
69 * lockdep_lock: protects the lockdep graph, the hashes and the
70 * class/list/hash allocators.
72 * This is one of the rare exceptions where it's justified
73 * to use a raw spinlock - we really dont want the spinlock
74 * code to recurse back into the lockdep code...
76 static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
78 static int graph_lock(void)
80 __raw_spin_lock(&lockdep_lock);
82 * Make sure that if another CPU detected a bug while
83 * walking the graph we dont change it (while the other
84 * CPU is busy printing out stuff with the graph lock
85 * dropped already)
87 if (!debug_locks) {
88 __raw_spin_unlock(&lockdep_lock);
89 return 0;
91 /* prevent any recursions within lockdep from causing deadlocks */
92 current->lockdep_recursion++;
93 return 1;
96 static inline int graph_unlock(void)
98 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
99 return DEBUG_LOCKS_WARN_ON(1);
101 current->lockdep_recursion--;
102 __raw_spin_unlock(&lockdep_lock);
103 return 0;
107 * Turn lock debugging off and return with 0 if it was off already,
108 * and also release the graph lock:
110 static inline int debug_locks_off_graph_unlock(void)
112 int ret = debug_locks_off();
114 __raw_spin_unlock(&lockdep_lock);
116 return ret;
119 static int lockdep_initialized;
121 unsigned long nr_list_entries;
122 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
125 * All data structures here are protected by the global debug_lock.
127 * Mutex key structs only get allocated, once during bootup, and never
128 * get freed - this significantly simplifies the debugging code.
130 unsigned long nr_lock_classes;
131 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
133 static inline struct lock_class *hlock_class(struct held_lock *hlock)
135 if (!hlock->class_idx) {
136 DEBUG_LOCKS_WARN_ON(1);
137 return NULL;
139 return lock_classes + hlock->class_idx - 1;
142 #ifdef CONFIG_LOCK_STAT
143 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
145 static int lock_point(unsigned long points[], unsigned long ip)
147 int i;
149 for (i = 0; i < LOCKSTAT_POINTS; i++) {
150 if (points[i] == 0) {
151 points[i] = ip;
152 break;
154 if (points[i] == ip)
155 break;
158 return i;
161 static void lock_time_inc(struct lock_time *lt, s64 time)
163 if (time > lt->max)
164 lt->max = time;
166 if (time < lt->min || !lt->min)
167 lt->min = time;
169 lt->total += time;
170 lt->nr++;
173 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
175 dst->min += src->min;
176 dst->max += src->max;
177 dst->total += src->total;
178 dst->nr += src->nr;
181 struct lock_class_stats lock_stats(struct lock_class *class)
183 struct lock_class_stats stats;
184 int cpu, i;
186 memset(&stats, 0, sizeof(struct lock_class_stats));
187 for_each_possible_cpu(cpu) {
188 struct lock_class_stats *pcs =
189 &per_cpu(lock_stats, cpu)[class - lock_classes];
191 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
192 stats.contention_point[i] += pcs->contention_point[i];
194 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
195 stats.contending_point[i] += pcs->contending_point[i];
197 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
198 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
200 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
201 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
203 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
204 stats.bounces[i] += pcs->bounces[i];
207 return stats;
210 void clear_lock_stats(struct lock_class *class)
212 int cpu;
214 for_each_possible_cpu(cpu) {
215 struct lock_class_stats *cpu_stats =
216 &per_cpu(lock_stats, cpu)[class - lock_classes];
218 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
220 memset(class->contention_point, 0, sizeof(class->contention_point));
221 memset(class->contending_point, 0, sizeof(class->contending_point));
224 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
226 return &get_cpu_var(lock_stats)[class - lock_classes];
229 static void put_lock_stats(struct lock_class_stats *stats)
231 put_cpu_var(lock_stats);
234 static void lock_release_holdtime(struct held_lock *hlock)
236 struct lock_class_stats *stats;
237 s64 holdtime;
239 if (!lock_stat)
240 return;
242 holdtime = sched_clock() - hlock->holdtime_stamp;
244 stats = get_lock_stats(hlock_class(hlock));
245 if (hlock->read)
246 lock_time_inc(&stats->read_holdtime, holdtime);
247 else
248 lock_time_inc(&stats->write_holdtime, holdtime);
249 put_lock_stats(stats);
251 #else
252 static inline void lock_release_holdtime(struct held_lock *hlock)
255 #endif
258 * We keep a global list of all lock classes. The list only grows,
259 * never shrinks. The list is only accessed with the lockdep
260 * spinlock lock held.
262 LIST_HEAD(all_lock_classes);
265 * The lockdep classes are in a hash-table as well, for fast lookup:
267 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
268 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
269 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
270 #define classhashentry(key) (classhash_table + __classhashfn((key)))
272 static struct list_head classhash_table[CLASSHASH_SIZE];
275 * We put the lock dependency chains into a hash-table as well, to cache
276 * their existence:
278 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
279 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
280 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
281 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
283 static struct list_head chainhash_table[CHAINHASH_SIZE];
286 * The hash key of the lock dependency chains is a hash itself too:
287 * it's a hash of all locks taken up to that lock, including that lock.
288 * It's a 64-bit hash, because it's important for the keys to be
289 * unique.
291 #define iterate_chain_key(key1, key2) \
292 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
293 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
294 (key2))
296 void lockdep_off(void)
298 current->lockdep_recursion++;
300 EXPORT_SYMBOL(lockdep_off);
302 void lockdep_on(void)
304 current->lockdep_recursion--;
306 EXPORT_SYMBOL(lockdep_on);
309 * Debugging switches:
312 #define VERBOSE 0
313 #define VERY_VERBOSE 0
315 #if VERBOSE
316 # define HARDIRQ_VERBOSE 1
317 # define SOFTIRQ_VERBOSE 1
318 # define RECLAIM_VERBOSE 1
319 #else
320 # define HARDIRQ_VERBOSE 0
321 # define SOFTIRQ_VERBOSE 0
322 # define RECLAIM_VERBOSE 0
323 #endif
325 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
327 * Quick filtering for interesting events:
329 static int class_filter(struct lock_class *class)
331 #if 0
332 /* Example */
333 if (class->name_version == 1 &&
334 !strcmp(class->name, "lockname"))
335 return 1;
336 if (class->name_version == 1 &&
337 !strcmp(class->name, "&struct->lockfield"))
338 return 1;
339 #endif
340 /* Filter everything else. 1 would be to allow everything else */
341 return 0;
343 #endif
345 static int verbose(struct lock_class *class)
347 #if VERBOSE
348 return class_filter(class);
349 #endif
350 return 0;
354 * Stack-trace: tightly packed array of stack backtrace
355 * addresses. Protected by the graph_lock.
357 unsigned long nr_stack_trace_entries;
358 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
360 static int save_trace(struct stack_trace *trace)
362 trace->nr_entries = 0;
363 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
364 trace->entries = stack_trace + nr_stack_trace_entries;
366 trace->skip = 3;
368 save_stack_trace(trace);
371 * Some daft arches put -1 at the end to indicate its a full trace.
373 * <rant> this is buggy anyway, since it takes a whole extra entry so a
374 * complete trace that maxes out the entries provided will be reported
375 * as incomplete, friggin useless </rant>
377 if (trace->entries[trace->nr_entries-1] == ULONG_MAX)
378 trace->nr_entries--;
380 trace->max_entries = trace->nr_entries;
382 nr_stack_trace_entries += trace->nr_entries;
384 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
385 if (!debug_locks_off_graph_unlock())
386 return 0;
388 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
389 printk("turning off the locking correctness validator.\n");
390 dump_stack();
392 return 0;
395 return 1;
398 unsigned int nr_hardirq_chains;
399 unsigned int nr_softirq_chains;
400 unsigned int nr_process_chains;
401 unsigned int max_lockdep_depth;
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_find_usage_forwards_checks;
432 atomic_t nr_find_usage_backwards_checks;
433 #endif
436 * Locking printouts:
439 #define __USAGE(__STATE) \
440 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
441 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
442 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
443 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
445 static const char *usage_str[] =
447 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
448 #include "lockdep_states.h"
449 #undef LOCKDEP_STATE
450 [LOCK_USED] = "INITIAL USE",
453 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
455 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
458 static inline unsigned long lock_flag(enum lock_usage_bit bit)
460 return 1UL << bit;
463 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
465 char c = '.';
467 if (class->usage_mask & lock_flag(bit + 2))
468 c = '+';
469 if (class->usage_mask & lock_flag(bit)) {
470 c = '-';
471 if (class->usage_mask & lock_flag(bit + 2))
472 c = '?';
475 return c;
478 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
480 int i = 0;
482 #define LOCKDEP_STATE(__STATE) \
483 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
484 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
485 #include "lockdep_states.h"
486 #undef LOCKDEP_STATE
488 usage[i] = '\0';
491 static void print_lock_name(struct lock_class *class)
493 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
494 const char *name;
496 get_usage_chars(class, usage);
498 name = class->name;
499 if (!name) {
500 name = __get_key_name(class->key, str);
501 printk(" (%s", name);
502 } else {
503 printk(" (%s", name);
504 if (class->name_version > 1)
505 printk("#%d", class->name_version);
506 if (class->subclass)
507 printk("/%d", class->subclass);
509 printk("){%s}", usage);
512 static void print_lockdep_cache(struct lockdep_map *lock)
514 const char *name;
515 char str[KSYM_NAME_LEN];
517 name = lock->name;
518 if (!name)
519 name = __get_key_name(lock->key->subkeys, str);
521 printk("%s", name);
524 static void print_lock(struct held_lock *hlock)
526 print_lock_name(hlock_class(hlock));
527 printk(", at: ");
528 print_ip_sym(hlock->acquire_ip);
531 static void lockdep_print_held_locks(struct task_struct *curr)
533 int i, depth = curr->lockdep_depth;
535 if (!depth) {
536 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
537 return;
539 printk("%d lock%s held by %s/%d:\n",
540 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
542 for (i = 0; i < depth; i++) {
543 printk(" #%d: ", i);
544 print_lock(curr->held_locks + i);
548 static void print_kernel_version(void)
550 printk("%s %.*s\n", init_utsname()->release,
551 (int)strcspn(init_utsname()->version, " "),
552 init_utsname()->version);
555 static int very_verbose(struct lock_class *class)
557 #if VERY_VERBOSE
558 return class_filter(class);
559 #endif
560 return 0;
564 * Is this the address of a static object:
566 static int static_obj(void *obj)
568 unsigned long start = (unsigned long) &_stext,
569 end = (unsigned long) &_end,
570 addr = (unsigned long) obj;
571 #ifdef CONFIG_SMP
572 int i;
573 #endif
576 * static variable?
578 if ((addr >= start) && (addr < end))
579 return 1;
581 #ifdef CONFIG_SMP
583 * percpu var?
585 for_each_possible_cpu(i) {
586 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
587 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
588 + per_cpu_offset(i);
590 if ((addr >= start) && (addr < end))
591 return 1;
593 #endif
596 * module var?
598 return is_module_address(addr);
602 * To make lock name printouts unique, we calculate a unique
603 * class->name_version generation counter:
605 static int count_matching_names(struct lock_class *new_class)
607 struct lock_class *class;
608 int count = 0;
610 if (!new_class->name)
611 return 0;
613 list_for_each_entry(class, &all_lock_classes, lock_entry) {
614 if (new_class->key - new_class->subclass == class->key)
615 return class->name_version;
616 if (class->name && !strcmp(class->name, new_class->name))
617 count = max(count, class->name_version);
620 return count + 1;
624 * Register a lock's class in the hash-table, if the class is not present
625 * yet. Otherwise we look it up. We cache the result in the lock object
626 * itself, so actual lookup of the hash should be once per lock object.
628 static inline struct lock_class *
629 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
631 struct lockdep_subclass_key *key;
632 struct list_head *hash_head;
633 struct lock_class *class;
635 #ifdef CONFIG_DEBUG_LOCKDEP
637 * If the architecture calls into lockdep before initializing
638 * the hashes then we'll warn about it later. (we cannot printk
639 * right now)
641 if (unlikely(!lockdep_initialized)) {
642 lockdep_init();
643 lockdep_init_error = 1;
644 save_stack_trace(&lockdep_init_trace);
646 #endif
649 * Static locks do not have their class-keys yet - for them the key
650 * is the lock object itself:
652 if (unlikely(!lock->key))
653 lock->key = (void *)lock;
656 * NOTE: the class-key must be unique. For dynamic locks, a static
657 * lock_class_key variable is passed in through the mutex_init()
658 * (or spin_lock_init()) call - which acts as the key. For static
659 * locks we use the lock object itself as the key.
661 BUILD_BUG_ON(sizeof(struct lock_class_key) >
662 sizeof(struct lockdep_map));
664 key = lock->key->subkeys + subclass;
666 hash_head = classhashentry(key);
669 * We can walk the hash lockfree, because the hash only
670 * grows, and we are careful when adding entries to the end:
672 list_for_each_entry(class, hash_head, hash_entry) {
673 if (class->key == key) {
674 WARN_ON_ONCE(class->name != lock->name);
675 return class;
679 return NULL;
683 * Register a lock's class in the hash-table, if the class is not present
684 * yet. Otherwise we look it up. We cache the result in the lock object
685 * itself, so actual lookup of the hash should be once per lock object.
687 static inline struct lock_class *
688 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
690 struct lockdep_subclass_key *key;
691 struct list_head *hash_head;
692 struct lock_class *class;
693 unsigned long flags;
695 class = look_up_lock_class(lock, subclass);
696 if (likely(class))
697 return class;
700 * Debug-check: all keys must be persistent!
702 if (!static_obj(lock->key)) {
703 debug_locks_off();
704 printk("INFO: trying to register non-static key.\n");
705 printk("the code is fine but needs lockdep annotation.\n");
706 printk("turning off the locking correctness validator.\n");
707 dump_stack();
709 return NULL;
712 key = lock->key->subkeys + subclass;
713 hash_head = classhashentry(key);
715 raw_local_irq_save(flags);
716 if (!graph_lock()) {
717 raw_local_irq_restore(flags);
718 return NULL;
721 * We have to do the hash-walk again, to avoid races
722 * with another CPU:
724 list_for_each_entry(class, hash_head, hash_entry)
725 if (class->key == key)
726 goto out_unlock_set;
728 * Allocate a new key from the static array, and add it to
729 * the hash:
731 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
732 if (!debug_locks_off_graph_unlock()) {
733 raw_local_irq_restore(flags);
734 return NULL;
736 raw_local_irq_restore(flags);
738 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
739 printk("turning off the locking correctness validator.\n");
740 dump_stack();
741 return NULL;
743 class = lock_classes + nr_lock_classes++;
744 debug_atomic_inc(&nr_unused_locks);
745 class->key = key;
746 class->name = lock->name;
747 class->subclass = subclass;
748 INIT_LIST_HEAD(&class->lock_entry);
749 INIT_LIST_HEAD(&class->locks_before);
750 INIT_LIST_HEAD(&class->locks_after);
751 class->name_version = count_matching_names(class);
753 * We use RCU's safe list-add method to make
754 * parallel walking of the hash-list safe:
756 list_add_tail_rcu(&class->hash_entry, hash_head);
758 * Add it to the global list of classes:
760 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
762 if (verbose(class)) {
763 graph_unlock();
764 raw_local_irq_restore(flags);
766 printk("\nnew class %p: %s", class->key, class->name);
767 if (class->name_version > 1)
768 printk("#%d", class->name_version);
769 printk("\n");
770 dump_stack();
772 raw_local_irq_save(flags);
773 if (!graph_lock()) {
774 raw_local_irq_restore(flags);
775 return NULL;
778 out_unlock_set:
779 graph_unlock();
780 raw_local_irq_restore(flags);
782 if (!subclass || force)
783 lock->class_cache = class;
785 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
786 return NULL;
788 return class;
791 #ifdef CONFIG_PROVE_LOCKING
793 * Allocate a lockdep entry. (assumes the graph_lock held, returns
794 * with NULL on failure)
796 static struct lock_list *alloc_list_entry(void)
798 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
799 if (!debug_locks_off_graph_unlock())
800 return NULL;
802 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
803 printk("turning off the locking correctness validator.\n");
804 dump_stack();
805 return NULL;
807 return list_entries + nr_list_entries++;
811 * Add a new dependency to the head of the list:
813 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
814 struct list_head *head, unsigned long ip, int distance)
816 struct lock_list *entry;
818 * Lock not present yet - get a new dependency struct and
819 * add it to the list:
821 entry = alloc_list_entry();
822 if (!entry)
823 return 0;
825 if (!save_trace(&entry->trace))
826 return 0;
828 entry->class = this;
829 entry->distance = distance;
831 * Since we never remove from the dependency list, the list can
832 * be walked lockless by other CPUs, it's only allocation
833 * that must be protected by the spinlock. But this also means
834 * we must make new entries visible only once writes to the
835 * entry become visible - hence the RCU op:
837 list_add_tail_rcu(&entry->entry, head);
839 return 1;
843 * For good efficiency of modular, we use power of 2
845 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
846 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
849 * The circular_queue and helpers is used to implement the
850 * breadth-first search(BFS)algorithem, by which we can build
851 * the shortest path from the next lock to be acquired to the
852 * previous held lock if there is a circular between them.
854 struct circular_queue {
855 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
856 unsigned int front, rear;
859 static struct circular_queue lock_cq;
861 unsigned int max_bfs_queue_depth;
863 static unsigned int lockdep_dependency_gen_id;
865 static inline void __cq_init(struct circular_queue *cq)
867 cq->front = cq->rear = 0;
868 lockdep_dependency_gen_id++;
871 static inline int __cq_empty(struct circular_queue *cq)
873 return (cq->front == cq->rear);
876 static inline int __cq_full(struct circular_queue *cq)
878 return ((cq->rear + 1) & CQ_MASK) == cq->front;
881 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
883 if (__cq_full(cq))
884 return -1;
886 cq->element[cq->rear] = elem;
887 cq->rear = (cq->rear + 1) & CQ_MASK;
888 return 0;
891 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
893 if (__cq_empty(cq))
894 return -1;
896 *elem = cq->element[cq->front];
897 cq->front = (cq->front + 1) & CQ_MASK;
898 return 0;
901 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
903 return (cq->rear - cq->front) & CQ_MASK;
906 static inline void mark_lock_accessed(struct lock_list *lock,
907 struct lock_list *parent)
909 unsigned long nr;
911 nr = lock - list_entries;
912 WARN_ON(nr >= nr_list_entries);
913 lock->parent = parent;
914 lock->class->dep_gen_id = lockdep_dependency_gen_id;
917 static inline unsigned long lock_accessed(struct lock_list *lock)
919 unsigned long nr;
921 nr = lock - list_entries;
922 WARN_ON(nr >= nr_list_entries);
923 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
926 static inline struct lock_list *get_lock_parent(struct lock_list *child)
928 return child->parent;
931 static inline int get_lock_depth(struct lock_list *child)
933 int depth = 0;
934 struct lock_list *parent;
936 while ((parent = get_lock_parent(child))) {
937 child = parent;
938 depth++;
940 return depth;
943 static int __bfs(struct lock_list *source_entry,
944 void *data,
945 int (*match)(struct lock_list *entry, void *data),
946 struct lock_list **target_entry,
947 int forward)
949 struct lock_list *entry;
950 struct list_head *head;
951 struct circular_queue *cq = &lock_cq;
952 int ret = 1;
954 if (match(source_entry, data)) {
955 *target_entry = source_entry;
956 ret = 0;
957 goto exit;
960 if (forward)
961 head = &source_entry->class->locks_after;
962 else
963 head = &source_entry->class->locks_before;
965 if (list_empty(head))
966 goto exit;
968 __cq_init(cq);
969 __cq_enqueue(cq, (unsigned long)source_entry);
971 while (!__cq_empty(cq)) {
972 struct lock_list *lock;
974 __cq_dequeue(cq, (unsigned long *)&lock);
976 if (!lock->class) {
977 ret = -2;
978 goto exit;
981 if (forward)
982 head = &lock->class->locks_after;
983 else
984 head = &lock->class->locks_before;
986 list_for_each_entry(entry, head, entry) {
987 if (!lock_accessed(entry)) {
988 unsigned int cq_depth;
989 mark_lock_accessed(entry, lock);
990 if (match(entry, data)) {
991 *target_entry = entry;
992 ret = 0;
993 goto exit;
996 if (__cq_enqueue(cq, (unsigned long)entry)) {
997 ret = -1;
998 goto exit;
1000 cq_depth = __cq_get_elem_count(cq);
1001 if (max_bfs_queue_depth < cq_depth)
1002 max_bfs_queue_depth = cq_depth;
1006 exit:
1007 return ret;
1010 static inline int __bfs_forwards(struct lock_list *src_entry,
1011 void *data,
1012 int (*match)(struct lock_list *entry, void *data),
1013 struct lock_list **target_entry)
1015 return __bfs(src_entry, data, match, target_entry, 1);
1019 static inline int __bfs_backwards(struct lock_list *src_entry,
1020 void *data,
1021 int (*match)(struct lock_list *entry, void *data),
1022 struct lock_list **target_entry)
1024 return __bfs(src_entry, data, match, target_entry, 0);
1029 * Recursive, forwards-direction lock-dependency checking, used for
1030 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1031 * checking.
1035 * Print a dependency chain entry (this is only done when a deadlock
1036 * has been detected):
1038 static noinline int
1039 print_circular_bug_entry(struct lock_list *target, int depth)
1041 if (debug_locks_silent)
1042 return 0;
1043 printk("\n-> #%u", depth);
1044 print_lock_name(target->class);
1045 printk(":\n");
1046 print_stack_trace(&target->trace, 6);
1048 return 0;
1052 * When a circular dependency is detected, print the
1053 * header first:
1055 static noinline int
1056 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1057 struct held_lock *check_src,
1058 struct held_lock *check_tgt)
1060 struct task_struct *curr = current;
1062 if (debug_locks_silent)
1063 return 0;
1065 printk("\n=======================================================\n");
1066 printk( "[ INFO: possible circular locking dependency detected ]\n");
1067 print_kernel_version();
1068 printk( "-------------------------------------------------------\n");
1069 printk("%s/%d is trying to acquire lock:\n",
1070 curr->comm, task_pid_nr(curr));
1071 print_lock(check_src);
1072 printk("\nbut task is already holding lock:\n");
1073 print_lock(check_tgt);
1074 printk("\nwhich lock already depends on the new lock.\n\n");
1075 printk("\nthe existing dependency chain (in reverse order) is:\n");
1077 print_circular_bug_entry(entry, depth);
1079 return 0;
1082 static inline int class_equal(struct lock_list *entry, void *data)
1084 return entry->class == data;
1087 static noinline int print_circular_bug(struct lock_list *this,
1088 struct lock_list *target,
1089 struct held_lock *check_src,
1090 struct held_lock *check_tgt)
1092 struct task_struct *curr = current;
1093 struct lock_list *parent;
1094 int depth;
1096 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1097 return 0;
1099 if (!save_trace(&this->trace))
1100 return 0;
1102 depth = get_lock_depth(target);
1104 print_circular_bug_header(target, depth, check_src, check_tgt);
1106 parent = get_lock_parent(target);
1108 while (parent) {
1109 print_circular_bug_entry(parent, --depth);
1110 parent = get_lock_parent(parent);
1113 printk("\nother info that might help us debug this:\n\n");
1114 lockdep_print_held_locks(curr);
1116 printk("\nstack backtrace:\n");
1117 dump_stack();
1119 return 0;
1122 static noinline int print_bfs_bug(int ret)
1124 if (!debug_locks_off_graph_unlock())
1125 return 0;
1127 WARN(1, "lockdep bfs error:%d\n", ret);
1129 return 0;
1132 static int noop_count(struct lock_list *entry, void *data)
1134 (*(unsigned long *)data)++;
1135 return 0;
1138 unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1140 unsigned long count = 0;
1141 struct lock_list *uninitialized_var(target_entry);
1143 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1145 return count;
1147 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1149 unsigned long ret, flags;
1150 struct lock_list this;
1152 this.parent = NULL;
1153 this.class = class;
1155 local_irq_save(flags);
1156 __raw_spin_lock(&lockdep_lock);
1157 ret = __lockdep_count_forward_deps(&this);
1158 __raw_spin_unlock(&lockdep_lock);
1159 local_irq_restore(flags);
1161 return ret;
1164 unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1166 unsigned long count = 0;
1167 struct lock_list *uninitialized_var(target_entry);
1169 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1171 return count;
1174 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1176 unsigned long ret, flags;
1177 struct lock_list this;
1179 this.parent = NULL;
1180 this.class = class;
1182 local_irq_save(flags);
1183 __raw_spin_lock(&lockdep_lock);
1184 ret = __lockdep_count_backward_deps(&this);
1185 __raw_spin_unlock(&lockdep_lock);
1186 local_irq_restore(flags);
1188 return ret;
1192 * Prove that the dependency graph starting at <entry> can not
1193 * lead to <target>. Print an error and return 0 if it does.
1195 static noinline int
1196 check_noncircular(struct lock_list *root, struct lock_class *target,
1197 struct lock_list **target_entry)
1199 int result;
1201 debug_atomic_inc(&nr_cyclic_checks);
1203 result = __bfs_forwards(root, target, class_equal, target_entry);
1205 return result;
1208 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1210 * Forwards and backwards subgraph searching, for the purposes of
1211 * proving that two subgraphs can be connected by a new dependency
1212 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1215 static inline int usage_match(struct lock_list *entry, void *bit)
1217 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1223 * Find a node in the forwards-direction dependency sub-graph starting
1224 * at @root->class that matches @bit.
1226 * Return 0 if such a node exists in the subgraph, and put that node
1227 * into *@target_entry.
1229 * Return 1 otherwise and keep *@target_entry unchanged.
1230 * Return <0 on error.
1232 static int
1233 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1234 struct lock_list **target_entry)
1236 int result;
1238 debug_atomic_inc(&nr_find_usage_forwards_checks);
1240 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1242 return result;
1246 * Find a node in the backwards-direction dependency sub-graph starting
1247 * at @root->class that matches @bit.
1249 * Return 0 if such a node exists in the subgraph, and put that node
1250 * into *@target_entry.
1252 * Return 1 otherwise and keep *@target_entry unchanged.
1253 * Return <0 on error.
1255 static int
1256 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1257 struct lock_list **target_entry)
1259 int result;
1261 debug_atomic_inc(&nr_find_usage_backwards_checks);
1263 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1265 return result;
1268 static void print_lock_class_header(struct lock_class *class, int depth)
1270 int bit;
1272 printk("%*s->", depth, "");
1273 print_lock_name(class);
1274 printk(" ops: %lu", class->ops);
1275 printk(" {\n");
1277 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1278 if (class->usage_mask & (1 << bit)) {
1279 int len = depth;
1281 len += printk("%*s %s", depth, "", usage_str[bit]);
1282 len += printk(" at:\n");
1283 print_stack_trace(class->usage_traces + bit, len);
1286 printk("%*s }\n", depth, "");
1288 printk("%*s ... key at: ",depth,"");
1289 print_ip_sym((unsigned long)class->key);
1293 * printk the shortest lock dependencies from @start to @end in reverse order:
1295 static void __used
1296 print_shortest_lock_dependencies(struct lock_list *leaf,
1297 struct lock_list *root)
1299 struct lock_list *entry = leaf;
1300 int depth;
1302 /*compute depth from generated tree by BFS*/
1303 depth = get_lock_depth(leaf);
1305 do {
1306 print_lock_class_header(entry->class, depth);
1307 printk("%*s ... acquired at:\n", depth, "");
1308 print_stack_trace(&entry->trace, 2);
1309 printk("\n");
1311 if (depth == 0 && (entry != root)) {
1312 printk("lockdep:%s bad BFS generated tree\n", __func__);
1313 break;
1316 entry = get_lock_parent(entry);
1317 depth--;
1318 } while (entry && (depth >= 0));
1320 return;
1323 static int
1324 print_bad_irq_dependency(struct task_struct *curr,
1325 struct lock_list *prev_root,
1326 struct lock_list *next_root,
1327 struct lock_list *backwards_entry,
1328 struct lock_list *forwards_entry,
1329 struct held_lock *prev,
1330 struct held_lock *next,
1331 enum lock_usage_bit bit1,
1332 enum lock_usage_bit bit2,
1333 const char *irqclass)
1335 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1336 return 0;
1338 printk("\n======================================================\n");
1339 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1340 irqclass, irqclass);
1341 print_kernel_version();
1342 printk( "------------------------------------------------------\n");
1343 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1344 curr->comm, task_pid_nr(curr),
1345 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1346 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1347 curr->hardirqs_enabled,
1348 curr->softirqs_enabled);
1349 print_lock(next);
1351 printk("\nand this task is already holding:\n");
1352 print_lock(prev);
1353 printk("which would create a new lock dependency:\n");
1354 print_lock_name(hlock_class(prev));
1355 printk(" ->");
1356 print_lock_name(hlock_class(next));
1357 printk("\n");
1359 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1360 irqclass);
1361 print_lock_name(backwards_entry->class);
1362 printk("\n... which became %s-irq-safe at:\n", irqclass);
1364 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1366 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1367 print_lock_name(forwards_entry->class);
1368 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1369 printk("...");
1371 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1373 printk("\nother info that might help us debug this:\n\n");
1374 lockdep_print_held_locks(curr);
1376 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1377 printk(" and the holding lock:\n");
1378 if (!save_trace(&prev_root->trace))
1379 return 0;
1380 print_shortest_lock_dependencies(backwards_entry, prev_root);
1382 printk("\nthe dependencies between the lock to be acquired");
1383 printk(" and %s-irq-unsafe lock:\n", irqclass);
1384 if (!save_trace(&next_root->trace))
1385 return 0;
1386 print_shortest_lock_dependencies(forwards_entry, next_root);
1388 printk("\nstack backtrace:\n");
1389 dump_stack();
1391 return 0;
1394 static int
1395 check_usage(struct task_struct *curr, struct held_lock *prev,
1396 struct held_lock *next, enum lock_usage_bit bit_backwards,
1397 enum lock_usage_bit bit_forwards, const char *irqclass)
1399 int ret;
1400 struct lock_list this, that;
1401 struct lock_list *uninitialized_var(target_entry);
1402 struct lock_list *uninitialized_var(target_entry1);
1404 this.parent = NULL;
1406 this.class = hlock_class(prev);
1407 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1408 if (ret < 0)
1409 return print_bfs_bug(ret);
1410 if (ret == 1)
1411 return ret;
1413 that.parent = NULL;
1414 that.class = hlock_class(next);
1415 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1416 if (ret < 0)
1417 return print_bfs_bug(ret);
1418 if (ret == 1)
1419 return ret;
1421 return print_bad_irq_dependency(curr, &this, &that,
1422 target_entry, target_entry1,
1423 prev, next,
1424 bit_backwards, bit_forwards, irqclass);
1427 static const char *state_names[] = {
1428 #define LOCKDEP_STATE(__STATE) \
1429 __stringify(__STATE),
1430 #include "lockdep_states.h"
1431 #undef LOCKDEP_STATE
1434 static const char *state_rnames[] = {
1435 #define LOCKDEP_STATE(__STATE) \
1436 __stringify(__STATE)"-READ",
1437 #include "lockdep_states.h"
1438 #undef LOCKDEP_STATE
1441 static inline const char *state_name(enum lock_usage_bit bit)
1443 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1446 static int exclusive_bit(int new_bit)
1449 * USED_IN
1450 * USED_IN_READ
1451 * ENABLED
1452 * ENABLED_READ
1454 * bit 0 - write/read
1455 * bit 1 - used_in/enabled
1456 * bit 2+ state
1459 int state = new_bit & ~3;
1460 int dir = new_bit & 2;
1463 * keep state, bit flip the direction and strip read.
1465 return state | (dir ^ 2);
1468 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1469 struct held_lock *next, enum lock_usage_bit bit)
1472 * Prove that the new dependency does not connect a hardirq-safe
1473 * lock with a hardirq-unsafe lock - to achieve this we search
1474 * the backwards-subgraph starting at <prev>, and the
1475 * forwards-subgraph starting at <next>:
1477 if (!check_usage(curr, prev, next, bit,
1478 exclusive_bit(bit), state_name(bit)))
1479 return 0;
1481 bit++; /* _READ */
1484 * Prove that the new dependency does not connect a hardirq-safe-read
1485 * lock with a hardirq-unsafe lock - to achieve this we search
1486 * the backwards-subgraph starting at <prev>, and the
1487 * forwards-subgraph starting at <next>:
1489 if (!check_usage(curr, prev, next, bit,
1490 exclusive_bit(bit), state_name(bit)))
1491 return 0;
1493 return 1;
1496 static int
1497 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1498 struct held_lock *next)
1500 #define LOCKDEP_STATE(__STATE) \
1501 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1502 return 0;
1503 #include "lockdep_states.h"
1504 #undef LOCKDEP_STATE
1506 return 1;
1509 static void inc_chains(void)
1511 if (current->hardirq_context)
1512 nr_hardirq_chains++;
1513 else {
1514 if (current->softirq_context)
1515 nr_softirq_chains++;
1516 else
1517 nr_process_chains++;
1521 #else
1523 static inline int
1524 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1525 struct held_lock *next)
1527 return 1;
1530 static inline void inc_chains(void)
1532 nr_process_chains++;
1535 #endif
1537 static int
1538 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1539 struct held_lock *next)
1541 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1542 return 0;
1544 printk("\n=============================================\n");
1545 printk( "[ INFO: possible recursive locking detected ]\n");
1546 print_kernel_version();
1547 printk( "---------------------------------------------\n");
1548 printk("%s/%d is trying to acquire lock:\n",
1549 curr->comm, task_pid_nr(curr));
1550 print_lock(next);
1551 printk("\nbut task is already holding lock:\n");
1552 print_lock(prev);
1554 printk("\nother info that might help us debug this:\n");
1555 lockdep_print_held_locks(curr);
1557 printk("\nstack backtrace:\n");
1558 dump_stack();
1560 return 0;
1564 * Check whether we are holding such a class already.
1566 * (Note that this has to be done separately, because the graph cannot
1567 * detect such classes of deadlocks.)
1569 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1571 static int
1572 check_deadlock(struct task_struct *curr, struct held_lock *next,
1573 struct lockdep_map *next_instance, int read)
1575 struct held_lock *prev;
1576 struct held_lock *nest = NULL;
1577 int i;
1579 for (i = 0; i < curr->lockdep_depth; i++) {
1580 prev = curr->held_locks + i;
1582 if (prev->instance == next->nest_lock)
1583 nest = prev;
1585 if (hlock_class(prev) != hlock_class(next))
1586 continue;
1589 * Allow read-after-read recursion of the same
1590 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1592 if ((read == 2) && prev->read)
1593 return 2;
1596 * We're holding the nest_lock, which serializes this lock's
1597 * nesting behaviour.
1599 if (nest)
1600 return 2;
1602 return print_deadlock_bug(curr, prev, next);
1604 return 1;
1608 * There was a chain-cache miss, and we are about to add a new dependency
1609 * to a previous lock. We recursively validate the following rules:
1611 * - would the adding of the <prev> -> <next> dependency create a
1612 * circular dependency in the graph? [== circular deadlock]
1614 * - does the new prev->next dependency connect any hardirq-safe lock
1615 * (in the full backwards-subgraph starting at <prev>) with any
1616 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1617 * <next>)? [== illegal lock inversion with hardirq contexts]
1619 * - does the new prev->next dependency connect any softirq-safe lock
1620 * (in the full backwards-subgraph starting at <prev>) with any
1621 * softirq-unsafe lock (in the full forwards-subgraph starting at
1622 * <next>)? [== illegal lock inversion with softirq contexts]
1624 * any of these scenarios could lead to a deadlock.
1626 * Then if all the validations pass, we add the forwards and backwards
1627 * dependency.
1629 static int
1630 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1631 struct held_lock *next, int distance)
1633 struct lock_list *entry;
1634 int ret;
1635 struct lock_list this;
1636 struct lock_list *uninitialized_var(target_entry);
1639 * Prove that the new <prev> -> <next> dependency would not
1640 * create a circular dependency in the graph. (We do this by
1641 * forward-recursing into the graph starting at <next>, and
1642 * checking whether we can reach <prev>.)
1644 * We are using global variables to control the recursion, to
1645 * keep the stackframe size of the recursive functions low:
1647 this.class = hlock_class(next);
1648 this.parent = NULL;
1649 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1650 if (unlikely(!ret))
1651 return print_circular_bug(&this, target_entry, next, prev);
1652 else if (unlikely(ret < 0))
1653 return print_bfs_bug(ret);
1655 if (!check_prev_add_irq(curr, prev, next))
1656 return 0;
1659 * For recursive read-locks we do all the dependency checks,
1660 * but we dont store read-triggered dependencies (only
1661 * write-triggered dependencies). This ensures that only the
1662 * write-side dependencies matter, and that if for example a
1663 * write-lock never takes any other locks, then the reads are
1664 * equivalent to a NOP.
1666 if (next->read == 2 || prev->read == 2)
1667 return 1;
1669 * Is the <prev> -> <next> dependency already present?
1671 * (this may occur even though this is a new chain: consider
1672 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1673 * chains - the second one will be new, but L1 already has
1674 * L2 added to its dependency list, due to the first chain.)
1676 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1677 if (entry->class == hlock_class(next)) {
1678 if (distance == 1)
1679 entry->distance = 1;
1680 return 2;
1685 * Ok, all validations passed, add the new lock
1686 * to the previous lock's dependency list:
1688 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1689 &hlock_class(prev)->locks_after,
1690 next->acquire_ip, distance);
1692 if (!ret)
1693 return 0;
1695 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1696 &hlock_class(next)->locks_before,
1697 next->acquire_ip, distance);
1698 if (!ret)
1699 return 0;
1702 * Debugging printouts:
1704 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1705 graph_unlock();
1706 printk("\n new dependency: ");
1707 print_lock_name(hlock_class(prev));
1708 printk(" => ");
1709 print_lock_name(hlock_class(next));
1710 printk("\n");
1711 dump_stack();
1712 return graph_lock();
1714 return 1;
1718 * Add the dependency to all directly-previous locks that are 'relevant'.
1719 * The ones that are relevant are (in increasing distance from curr):
1720 * all consecutive trylock entries and the final non-trylock entry - or
1721 * the end of this context's lock-chain - whichever comes first.
1723 static int
1724 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1726 int depth = curr->lockdep_depth;
1727 struct held_lock *hlock;
1730 * Debugging checks.
1732 * Depth must not be zero for a non-head lock:
1734 if (!depth)
1735 goto out_bug;
1737 * At least two relevant locks must exist for this
1738 * to be a head:
1740 if (curr->held_locks[depth].irq_context !=
1741 curr->held_locks[depth-1].irq_context)
1742 goto out_bug;
1744 for (;;) {
1745 int distance = curr->lockdep_depth - depth + 1;
1746 hlock = curr->held_locks + depth-1;
1748 * Only non-recursive-read entries get new dependencies
1749 * added:
1751 if (hlock->read != 2) {
1752 if (!check_prev_add(curr, hlock, next, distance))
1753 return 0;
1755 * Stop after the first non-trylock entry,
1756 * as non-trylock entries have added their
1757 * own direct dependencies already, so this
1758 * lock is connected to them indirectly:
1760 if (!hlock->trylock)
1761 break;
1763 depth--;
1765 * End of lock-stack?
1767 if (!depth)
1768 break;
1770 * Stop the search if we cross into another context:
1772 if (curr->held_locks[depth].irq_context !=
1773 curr->held_locks[depth-1].irq_context)
1774 break;
1776 return 1;
1777 out_bug:
1778 if (!debug_locks_off_graph_unlock())
1779 return 0;
1781 WARN_ON(1);
1783 return 0;
1786 unsigned long nr_lock_chains;
1787 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1788 int nr_chain_hlocks;
1789 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1791 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1793 return lock_classes + chain_hlocks[chain->base + i];
1797 * Look up a dependency chain. If the key is not present yet then
1798 * add it and return 1 - in this case the new dependency chain is
1799 * validated. If the key is already hashed, return 0.
1800 * (On return with 1 graph_lock is held.)
1802 static inline int lookup_chain_cache(struct task_struct *curr,
1803 struct held_lock *hlock,
1804 u64 chain_key)
1806 struct lock_class *class = hlock_class(hlock);
1807 struct list_head *hash_head = chainhashentry(chain_key);
1808 struct lock_chain *chain;
1809 struct held_lock *hlock_curr, *hlock_next;
1810 int i, j, n, cn;
1812 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1813 return 0;
1815 * We can walk it lock-free, because entries only get added
1816 * to the hash:
1818 list_for_each_entry(chain, hash_head, entry) {
1819 if (chain->chain_key == chain_key) {
1820 cache_hit:
1821 debug_atomic_inc(&chain_lookup_hits);
1822 if (very_verbose(class))
1823 printk("\nhash chain already cached, key: "
1824 "%016Lx tail class: [%p] %s\n",
1825 (unsigned long long)chain_key,
1826 class->key, class->name);
1827 return 0;
1830 if (very_verbose(class))
1831 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1832 (unsigned long long)chain_key, class->key, class->name);
1834 * Allocate a new chain entry from the static array, and add
1835 * it to the hash:
1837 if (!graph_lock())
1838 return 0;
1840 * We have to walk the chain again locked - to avoid duplicates:
1842 list_for_each_entry(chain, hash_head, entry) {
1843 if (chain->chain_key == chain_key) {
1844 graph_unlock();
1845 goto cache_hit;
1848 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1849 if (!debug_locks_off_graph_unlock())
1850 return 0;
1852 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1853 printk("turning off the locking correctness validator.\n");
1854 dump_stack();
1855 return 0;
1857 chain = lock_chains + nr_lock_chains++;
1858 chain->chain_key = chain_key;
1859 chain->irq_context = hlock->irq_context;
1860 /* Find the first held_lock of current chain */
1861 hlock_next = hlock;
1862 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1863 hlock_curr = curr->held_locks + i;
1864 if (hlock_curr->irq_context != hlock_next->irq_context)
1865 break;
1866 hlock_next = hlock;
1868 i++;
1869 chain->depth = curr->lockdep_depth + 1 - i;
1870 cn = nr_chain_hlocks;
1871 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1872 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1873 if (n == cn)
1874 break;
1875 cn = n;
1877 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1878 chain->base = cn;
1879 for (j = 0; j < chain->depth - 1; j++, i++) {
1880 int lock_id = curr->held_locks[i].class_idx - 1;
1881 chain_hlocks[chain->base + j] = lock_id;
1883 chain_hlocks[chain->base + j] = class - lock_classes;
1885 list_add_tail_rcu(&chain->entry, hash_head);
1886 debug_atomic_inc(&chain_lookup_misses);
1887 inc_chains();
1889 return 1;
1892 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1893 struct held_lock *hlock, int chain_head, u64 chain_key)
1896 * Trylock needs to maintain the stack of held locks, but it
1897 * does not add new dependencies, because trylock can be done
1898 * in any order.
1900 * We look up the chain_key and do the O(N^2) check and update of
1901 * the dependencies only if this is a new dependency chain.
1902 * (If lookup_chain_cache() returns with 1 it acquires
1903 * graph_lock for us)
1905 if (!hlock->trylock && (hlock->check == 2) &&
1906 lookup_chain_cache(curr, hlock, chain_key)) {
1908 * Check whether last held lock:
1910 * - is irq-safe, if this lock is irq-unsafe
1911 * - is softirq-safe, if this lock is hardirq-unsafe
1913 * And check whether the new lock's dependency graph
1914 * could lead back to the previous lock.
1916 * any of these scenarios could lead to a deadlock. If
1917 * All validations
1919 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1921 if (!ret)
1922 return 0;
1924 * Mark recursive read, as we jump over it when
1925 * building dependencies (just like we jump over
1926 * trylock entries):
1928 if (ret == 2)
1929 hlock->read = 2;
1931 * Add dependency only if this lock is not the head
1932 * of the chain, and if it's not a secondary read-lock:
1934 if (!chain_head && ret != 2)
1935 if (!check_prevs_add(curr, hlock))
1936 return 0;
1937 graph_unlock();
1938 } else
1939 /* after lookup_chain_cache(): */
1940 if (unlikely(!debug_locks))
1941 return 0;
1943 return 1;
1945 #else
1946 static inline int validate_chain(struct task_struct *curr,
1947 struct lockdep_map *lock, struct held_lock *hlock,
1948 int chain_head, u64 chain_key)
1950 return 1;
1952 #endif
1955 * We are building curr_chain_key incrementally, so double-check
1956 * it from scratch, to make sure that it's done correctly:
1958 static void check_chain_key(struct task_struct *curr)
1960 #ifdef CONFIG_DEBUG_LOCKDEP
1961 struct held_lock *hlock, *prev_hlock = NULL;
1962 unsigned int i, id;
1963 u64 chain_key = 0;
1965 for (i = 0; i < curr->lockdep_depth; i++) {
1966 hlock = curr->held_locks + i;
1967 if (chain_key != hlock->prev_chain_key) {
1968 debug_locks_off();
1969 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1970 curr->lockdep_depth, i,
1971 (unsigned long long)chain_key,
1972 (unsigned long long)hlock->prev_chain_key);
1973 return;
1975 id = hlock->class_idx - 1;
1976 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1977 return;
1979 if (prev_hlock && (prev_hlock->irq_context !=
1980 hlock->irq_context))
1981 chain_key = 0;
1982 chain_key = iterate_chain_key(chain_key, id);
1983 prev_hlock = hlock;
1985 if (chain_key != curr->curr_chain_key) {
1986 debug_locks_off();
1987 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1988 curr->lockdep_depth, i,
1989 (unsigned long long)chain_key,
1990 (unsigned long long)curr->curr_chain_key);
1992 #endif
1995 static int
1996 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1997 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1999 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2000 return 0;
2002 printk("\n=================================\n");
2003 printk( "[ INFO: inconsistent lock state ]\n");
2004 print_kernel_version();
2005 printk( "---------------------------------\n");
2007 printk("inconsistent {%s} -> {%s} usage.\n",
2008 usage_str[prev_bit], usage_str[new_bit]);
2010 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2011 curr->comm, task_pid_nr(curr),
2012 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2013 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2014 trace_hardirqs_enabled(curr),
2015 trace_softirqs_enabled(curr));
2016 print_lock(this);
2018 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
2019 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2021 print_irqtrace_events(curr);
2022 printk("\nother info that might help us debug this:\n");
2023 lockdep_print_held_locks(curr);
2025 printk("\nstack backtrace:\n");
2026 dump_stack();
2028 return 0;
2032 * Print out an error if an invalid bit is set:
2034 static inline int
2035 valid_state(struct task_struct *curr, struct held_lock *this,
2036 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2038 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2039 return print_usage_bug(curr, this, bad_bit, new_bit);
2040 return 1;
2043 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2044 enum lock_usage_bit new_bit);
2046 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2049 * print irq inversion bug:
2051 static int
2052 print_irq_inversion_bug(struct task_struct *curr,
2053 struct lock_list *root, struct lock_list *other,
2054 struct held_lock *this, int forwards,
2055 const char *irqclass)
2057 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2058 return 0;
2060 printk("\n=========================================================\n");
2061 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2062 print_kernel_version();
2063 printk( "---------------------------------------------------------\n");
2064 printk("%s/%d just changed the state of lock:\n",
2065 curr->comm, task_pid_nr(curr));
2066 print_lock(this);
2067 if (forwards)
2068 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2069 else
2070 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2071 print_lock_name(other->class);
2072 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2074 printk("\nother info that might help us debug this:\n");
2075 lockdep_print_held_locks(curr);
2077 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2078 if (!save_trace(&root->trace))
2079 return 0;
2080 print_shortest_lock_dependencies(other, root);
2082 printk("\nstack backtrace:\n");
2083 dump_stack();
2085 return 0;
2089 * Prove that in the forwards-direction subgraph starting at <this>
2090 * there is no lock matching <mask>:
2092 static int
2093 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2094 enum lock_usage_bit bit, const char *irqclass)
2096 int ret;
2097 struct lock_list root;
2098 struct lock_list *uninitialized_var(target_entry);
2100 root.parent = NULL;
2101 root.class = hlock_class(this);
2102 ret = find_usage_forwards(&root, bit, &target_entry);
2103 if (ret < 0)
2104 return print_bfs_bug(ret);
2105 if (ret == 1)
2106 return ret;
2108 return print_irq_inversion_bug(curr, &root, target_entry,
2109 this, 1, irqclass);
2113 * Prove that in the backwards-direction subgraph starting at <this>
2114 * there is no lock matching <mask>:
2116 static int
2117 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2118 enum lock_usage_bit bit, const char *irqclass)
2120 int ret;
2121 struct lock_list root;
2122 struct lock_list *uninitialized_var(target_entry);
2124 root.parent = NULL;
2125 root.class = hlock_class(this);
2126 ret = find_usage_backwards(&root, bit, &target_entry);
2127 if (ret < 0)
2128 return print_bfs_bug(ret);
2129 if (ret == 1)
2130 return ret;
2132 return print_irq_inversion_bug(curr, &root, target_entry,
2133 this, 1, irqclass);
2136 void print_irqtrace_events(struct task_struct *curr)
2138 printk("irq event stamp: %u\n", curr->irq_events);
2139 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2140 print_ip_sym(curr->hardirq_enable_ip);
2141 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2142 print_ip_sym(curr->hardirq_disable_ip);
2143 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2144 print_ip_sym(curr->softirq_enable_ip);
2145 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2146 print_ip_sym(curr->softirq_disable_ip);
2149 static int HARDIRQ_verbose(struct lock_class *class)
2151 #if HARDIRQ_VERBOSE
2152 return class_filter(class);
2153 #endif
2154 return 0;
2157 static int SOFTIRQ_verbose(struct lock_class *class)
2159 #if SOFTIRQ_VERBOSE
2160 return class_filter(class);
2161 #endif
2162 return 0;
2165 static int RECLAIM_FS_verbose(struct lock_class *class)
2167 #if RECLAIM_VERBOSE
2168 return class_filter(class);
2169 #endif
2170 return 0;
2173 #define STRICT_READ_CHECKS 1
2175 static int (*state_verbose_f[])(struct lock_class *class) = {
2176 #define LOCKDEP_STATE(__STATE) \
2177 __STATE##_verbose,
2178 #include "lockdep_states.h"
2179 #undef LOCKDEP_STATE
2182 static inline int state_verbose(enum lock_usage_bit bit,
2183 struct lock_class *class)
2185 return state_verbose_f[bit >> 2](class);
2188 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2189 enum lock_usage_bit bit, const char *name);
2191 static int
2192 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2193 enum lock_usage_bit new_bit)
2195 int excl_bit = exclusive_bit(new_bit);
2196 int read = new_bit & 1;
2197 int dir = new_bit & 2;
2200 * mark USED_IN has to look forwards -- to ensure no dependency
2201 * has ENABLED state, which would allow recursion deadlocks.
2203 * mark ENABLED has to look backwards -- to ensure no dependee
2204 * has USED_IN state, which, again, would allow recursion deadlocks.
2206 check_usage_f usage = dir ?
2207 check_usage_backwards : check_usage_forwards;
2210 * Validate that this particular lock does not have conflicting
2211 * usage states.
2213 if (!valid_state(curr, this, new_bit, excl_bit))
2214 return 0;
2217 * Validate that the lock dependencies don't have conflicting usage
2218 * states.
2220 if ((!read || !dir || STRICT_READ_CHECKS) &&
2221 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2222 return 0;
2225 * Check for read in write conflicts
2227 if (!read) {
2228 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2229 return 0;
2231 if (STRICT_READ_CHECKS &&
2232 !usage(curr, this, excl_bit + 1,
2233 state_name(new_bit + 1)))
2234 return 0;
2237 if (state_verbose(new_bit, hlock_class(this)))
2238 return 2;
2240 return 1;
2243 enum mark_type {
2244 #define LOCKDEP_STATE(__STATE) __STATE,
2245 #include "lockdep_states.h"
2246 #undef LOCKDEP_STATE
2250 * Mark all held locks with a usage bit:
2252 static int
2253 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2255 enum lock_usage_bit usage_bit;
2256 struct held_lock *hlock;
2257 int i;
2259 for (i = 0; i < curr->lockdep_depth; i++) {
2260 hlock = curr->held_locks + i;
2262 usage_bit = 2 + (mark << 2); /* ENABLED */
2263 if (hlock->read)
2264 usage_bit += 1; /* READ */
2266 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2268 if (!mark_lock(curr, hlock, usage_bit))
2269 return 0;
2272 return 1;
2276 * Debugging helper: via this flag we know that we are in
2277 * 'early bootup code', and will warn about any invalid irqs-on event:
2279 static int early_boot_irqs_enabled;
2281 void early_boot_irqs_off(void)
2283 early_boot_irqs_enabled = 0;
2286 void early_boot_irqs_on(void)
2288 early_boot_irqs_enabled = 1;
2292 * Hardirqs will be enabled:
2294 void trace_hardirqs_on_caller(unsigned long ip)
2296 struct task_struct *curr = current;
2298 time_hardirqs_on(CALLER_ADDR0, ip);
2300 if (unlikely(!debug_locks || current->lockdep_recursion))
2301 return;
2303 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2304 return;
2306 if (unlikely(curr->hardirqs_enabled)) {
2307 debug_atomic_inc(&redundant_hardirqs_on);
2308 return;
2310 /* we'll do an OFF -> ON transition: */
2311 curr->hardirqs_enabled = 1;
2313 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2314 return;
2315 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2316 return;
2318 * We are going to turn hardirqs on, so set the
2319 * usage bit for all held locks:
2321 if (!mark_held_locks(curr, HARDIRQ))
2322 return;
2324 * If we have softirqs enabled, then set the usage
2325 * bit for all held locks. (disabled hardirqs prevented
2326 * this bit from being set before)
2328 if (curr->softirqs_enabled)
2329 if (!mark_held_locks(curr, SOFTIRQ))
2330 return;
2332 curr->hardirq_enable_ip = ip;
2333 curr->hardirq_enable_event = ++curr->irq_events;
2334 debug_atomic_inc(&hardirqs_on_events);
2336 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2338 void trace_hardirqs_on(void)
2340 trace_hardirqs_on_caller(CALLER_ADDR0);
2342 EXPORT_SYMBOL(trace_hardirqs_on);
2345 * Hardirqs were disabled:
2347 void trace_hardirqs_off_caller(unsigned long ip)
2349 struct task_struct *curr = current;
2351 time_hardirqs_off(CALLER_ADDR0, ip);
2353 if (unlikely(!debug_locks || current->lockdep_recursion))
2354 return;
2356 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2357 return;
2359 if (curr->hardirqs_enabled) {
2361 * We have done an ON -> OFF transition:
2363 curr->hardirqs_enabled = 0;
2364 curr->hardirq_disable_ip = ip;
2365 curr->hardirq_disable_event = ++curr->irq_events;
2366 debug_atomic_inc(&hardirqs_off_events);
2367 } else
2368 debug_atomic_inc(&redundant_hardirqs_off);
2370 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2372 void trace_hardirqs_off(void)
2374 trace_hardirqs_off_caller(CALLER_ADDR0);
2376 EXPORT_SYMBOL(trace_hardirqs_off);
2379 * Softirqs will be enabled:
2381 void trace_softirqs_on(unsigned long ip)
2383 struct task_struct *curr = current;
2385 if (unlikely(!debug_locks))
2386 return;
2388 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2389 return;
2391 if (curr->softirqs_enabled) {
2392 debug_atomic_inc(&redundant_softirqs_on);
2393 return;
2397 * We'll do an OFF -> ON transition:
2399 curr->softirqs_enabled = 1;
2400 curr->softirq_enable_ip = ip;
2401 curr->softirq_enable_event = ++curr->irq_events;
2402 debug_atomic_inc(&softirqs_on_events);
2404 * We are going to turn softirqs on, so set the
2405 * usage bit for all held locks, if hardirqs are
2406 * enabled too:
2408 if (curr->hardirqs_enabled)
2409 mark_held_locks(curr, SOFTIRQ);
2413 * Softirqs were disabled:
2415 void trace_softirqs_off(unsigned long ip)
2417 struct task_struct *curr = current;
2419 if (unlikely(!debug_locks))
2420 return;
2422 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2423 return;
2425 if (curr->softirqs_enabled) {
2427 * We have done an ON -> OFF transition:
2429 curr->softirqs_enabled = 0;
2430 curr->softirq_disable_ip = ip;
2431 curr->softirq_disable_event = ++curr->irq_events;
2432 debug_atomic_inc(&softirqs_off_events);
2433 DEBUG_LOCKS_WARN_ON(!softirq_count());
2434 } else
2435 debug_atomic_inc(&redundant_softirqs_off);
2438 static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2440 struct task_struct *curr = current;
2442 if (unlikely(!debug_locks))
2443 return;
2445 /* no reclaim without waiting on it */
2446 if (!(gfp_mask & __GFP_WAIT))
2447 return;
2449 /* this guy won't enter reclaim */
2450 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2451 return;
2453 /* We're only interested __GFP_FS allocations for now */
2454 if (!(gfp_mask & __GFP_FS))
2455 return;
2457 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2458 return;
2460 mark_held_locks(curr, RECLAIM_FS);
2463 static void check_flags(unsigned long flags);
2465 void lockdep_trace_alloc(gfp_t gfp_mask)
2467 unsigned long flags;
2469 if (unlikely(current->lockdep_recursion))
2470 return;
2472 raw_local_irq_save(flags);
2473 check_flags(flags);
2474 current->lockdep_recursion = 1;
2475 __lockdep_trace_alloc(gfp_mask, flags);
2476 current->lockdep_recursion = 0;
2477 raw_local_irq_restore(flags);
2480 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2483 * If non-trylock use in a hardirq or softirq context, then
2484 * mark the lock as used in these contexts:
2486 if (!hlock->trylock) {
2487 if (hlock->read) {
2488 if (curr->hardirq_context)
2489 if (!mark_lock(curr, hlock,
2490 LOCK_USED_IN_HARDIRQ_READ))
2491 return 0;
2492 if (curr->softirq_context)
2493 if (!mark_lock(curr, hlock,
2494 LOCK_USED_IN_SOFTIRQ_READ))
2495 return 0;
2496 } else {
2497 if (curr->hardirq_context)
2498 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2499 return 0;
2500 if (curr->softirq_context)
2501 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2502 return 0;
2505 if (!hlock->hardirqs_off) {
2506 if (hlock->read) {
2507 if (!mark_lock(curr, hlock,
2508 LOCK_ENABLED_HARDIRQ_READ))
2509 return 0;
2510 if (curr->softirqs_enabled)
2511 if (!mark_lock(curr, hlock,
2512 LOCK_ENABLED_SOFTIRQ_READ))
2513 return 0;
2514 } else {
2515 if (!mark_lock(curr, hlock,
2516 LOCK_ENABLED_HARDIRQ))
2517 return 0;
2518 if (curr->softirqs_enabled)
2519 if (!mark_lock(curr, hlock,
2520 LOCK_ENABLED_SOFTIRQ))
2521 return 0;
2526 * We reuse the irq context infrastructure more broadly as a general
2527 * context checking code. This tests GFP_FS recursion (a lock taken
2528 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2529 * allocation).
2531 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2532 if (hlock->read) {
2533 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2534 return 0;
2535 } else {
2536 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2537 return 0;
2541 return 1;
2544 static int separate_irq_context(struct task_struct *curr,
2545 struct held_lock *hlock)
2547 unsigned int depth = curr->lockdep_depth;
2550 * Keep track of points where we cross into an interrupt context:
2552 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2553 curr->softirq_context;
2554 if (depth) {
2555 struct held_lock *prev_hlock;
2557 prev_hlock = curr->held_locks + depth-1;
2559 * If we cross into another context, reset the
2560 * hash key (this also prevents the checking and the
2561 * adding of the dependency to 'prev'):
2563 if (prev_hlock->irq_context != hlock->irq_context)
2564 return 1;
2566 return 0;
2569 #else
2571 static inline
2572 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2573 enum lock_usage_bit new_bit)
2575 WARN_ON(1);
2576 return 1;
2579 static inline int mark_irqflags(struct task_struct *curr,
2580 struct held_lock *hlock)
2582 return 1;
2585 static inline int separate_irq_context(struct task_struct *curr,
2586 struct held_lock *hlock)
2588 return 0;
2591 void lockdep_trace_alloc(gfp_t gfp_mask)
2595 #endif
2598 * Mark a lock with a usage bit, and validate the state transition:
2600 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2601 enum lock_usage_bit new_bit)
2603 unsigned int new_mask = 1 << new_bit, ret = 1;
2606 * If already set then do not dirty the cacheline,
2607 * nor do any checks:
2609 if (likely(hlock_class(this)->usage_mask & new_mask))
2610 return 1;
2612 if (!graph_lock())
2613 return 0;
2615 * Make sure we didnt race:
2617 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2618 graph_unlock();
2619 return 1;
2622 hlock_class(this)->usage_mask |= new_mask;
2624 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2625 return 0;
2627 switch (new_bit) {
2628 #define LOCKDEP_STATE(__STATE) \
2629 case LOCK_USED_IN_##__STATE: \
2630 case LOCK_USED_IN_##__STATE##_READ: \
2631 case LOCK_ENABLED_##__STATE: \
2632 case LOCK_ENABLED_##__STATE##_READ:
2633 #include "lockdep_states.h"
2634 #undef LOCKDEP_STATE
2635 ret = mark_lock_irq(curr, this, new_bit);
2636 if (!ret)
2637 return 0;
2638 break;
2639 case LOCK_USED:
2640 debug_atomic_dec(&nr_unused_locks);
2641 break;
2642 default:
2643 if (!debug_locks_off_graph_unlock())
2644 return 0;
2645 WARN_ON(1);
2646 return 0;
2649 graph_unlock();
2652 * We must printk outside of the graph_lock:
2654 if (ret == 2) {
2655 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2656 print_lock(this);
2657 print_irqtrace_events(curr);
2658 dump_stack();
2661 return ret;
2665 * Initialize a lock instance's lock-class mapping info:
2667 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2668 struct lock_class_key *key, int subclass)
2670 lock->class_cache = NULL;
2671 #ifdef CONFIG_LOCK_STAT
2672 lock->cpu = raw_smp_processor_id();
2673 #endif
2675 if (DEBUG_LOCKS_WARN_ON(!name)) {
2676 lock->name = "NULL";
2677 return;
2680 lock->name = name;
2682 if (DEBUG_LOCKS_WARN_ON(!key))
2683 return;
2685 * Sanity check, the lock-class key must be persistent:
2687 if (!static_obj(key)) {
2688 printk("BUG: key %p not in .data!\n", key);
2689 DEBUG_LOCKS_WARN_ON(1);
2690 return;
2692 lock->key = key;
2694 if (unlikely(!debug_locks))
2695 return;
2697 if (subclass)
2698 register_lock_class(lock, subclass, 1);
2700 EXPORT_SYMBOL_GPL(lockdep_init_map);
2703 * This gets called for every mutex_lock*()/spin_lock*() operation.
2704 * We maintain the dependency maps and validate the locking attempt:
2706 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2707 int trylock, int read, int check, int hardirqs_off,
2708 struct lockdep_map *nest_lock, unsigned long ip,
2709 int references)
2711 struct task_struct *curr = current;
2712 struct lock_class *class = NULL;
2713 struct held_lock *hlock;
2714 unsigned int depth, id;
2715 int chain_head = 0;
2716 int class_idx;
2717 u64 chain_key;
2719 if (!prove_locking)
2720 check = 1;
2722 if (unlikely(!debug_locks))
2723 return 0;
2725 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2726 return 0;
2728 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2729 debug_locks_off();
2730 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2731 printk("turning off the locking correctness validator.\n");
2732 dump_stack();
2733 return 0;
2736 if (!subclass)
2737 class = lock->class_cache;
2739 * Not cached yet or subclass?
2741 if (unlikely(!class)) {
2742 class = register_lock_class(lock, subclass, 0);
2743 if (!class)
2744 return 0;
2746 debug_atomic_inc((atomic_t *)&class->ops);
2747 if (very_verbose(class)) {
2748 printk("\nacquire class [%p] %s", class->key, class->name);
2749 if (class->name_version > 1)
2750 printk("#%d", class->name_version);
2751 printk("\n");
2752 dump_stack();
2756 * Add the lock to the list of currently held locks.
2757 * (we dont increase the depth just yet, up until the
2758 * dependency checks are done)
2760 depth = curr->lockdep_depth;
2761 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2762 return 0;
2764 class_idx = class - lock_classes + 1;
2766 if (depth) {
2767 hlock = curr->held_locks + depth - 1;
2768 if (hlock->class_idx == class_idx && nest_lock) {
2769 if (hlock->references)
2770 hlock->references++;
2771 else
2772 hlock->references = 2;
2774 return 1;
2778 hlock = curr->held_locks + depth;
2779 if (DEBUG_LOCKS_WARN_ON(!class))
2780 return 0;
2781 hlock->class_idx = class_idx;
2782 hlock->acquire_ip = ip;
2783 hlock->instance = lock;
2784 hlock->nest_lock = nest_lock;
2785 hlock->trylock = trylock;
2786 hlock->read = read;
2787 hlock->check = check;
2788 hlock->hardirqs_off = !!hardirqs_off;
2789 hlock->references = references;
2790 #ifdef CONFIG_LOCK_STAT
2791 hlock->waittime_stamp = 0;
2792 hlock->holdtime_stamp = sched_clock();
2793 #endif
2795 if (check == 2 && !mark_irqflags(curr, hlock))
2796 return 0;
2798 /* mark it as used: */
2799 if (!mark_lock(curr, hlock, LOCK_USED))
2800 return 0;
2803 * Calculate the chain hash: it's the combined hash of all the
2804 * lock keys along the dependency chain. We save the hash value
2805 * at every step so that we can get the current hash easily
2806 * after unlock. The chain hash is then used to cache dependency
2807 * results.
2809 * The 'key ID' is what is the most compact key value to drive
2810 * the hash, not class->key.
2812 id = class - lock_classes;
2813 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2814 return 0;
2816 chain_key = curr->curr_chain_key;
2817 if (!depth) {
2818 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2819 return 0;
2820 chain_head = 1;
2823 hlock->prev_chain_key = chain_key;
2824 if (separate_irq_context(curr, hlock)) {
2825 chain_key = 0;
2826 chain_head = 1;
2828 chain_key = iterate_chain_key(chain_key, id);
2830 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2831 return 0;
2833 curr->curr_chain_key = chain_key;
2834 curr->lockdep_depth++;
2835 check_chain_key(curr);
2836 #ifdef CONFIG_DEBUG_LOCKDEP
2837 if (unlikely(!debug_locks))
2838 return 0;
2839 #endif
2840 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2841 debug_locks_off();
2842 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2843 printk("turning off the locking correctness validator.\n");
2844 dump_stack();
2845 return 0;
2848 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2849 max_lockdep_depth = curr->lockdep_depth;
2851 return 1;
2854 static int
2855 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2856 unsigned long ip)
2858 if (!debug_locks_off())
2859 return 0;
2860 if (debug_locks_silent)
2861 return 0;
2863 printk("\n=====================================\n");
2864 printk( "[ BUG: bad unlock balance detected! ]\n");
2865 printk( "-------------------------------------\n");
2866 printk("%s/%d is trying to release lock (",
2867 curr->comm, task_pid_nr(curr));
2868 print_lockdep_cache(lock);
2869 printk(") at:\n");
2870 print_ip_sym(ip);
2871 printk("but there are no more locks to release!\n");
2872 printk("\nother info that might help us debug this:\n");
2873 lockdep_print_held_locks(curr);
2875 printk("\nstack backtrace:\n");
2876 dump_stack();
2878 return 0;
2882 * Common debugging checks for both nested and non-nested unlock:
2884 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2885 unsigned long ip)
2887 if (unlikely(!debug_locks))
2888 return 0;
2889 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2890 return 0;
2892 if (curr->lockdep_depth <= 0)
2893 return print_unlock_inbalance_bug(curr, lock, ip);
2895 return 1;
2898 static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
2900 if (hlock->instance == lock)
2901 return 1;
2903 if (hlock->references) {
2904 struct lock_class *class = lock->class_cache;
2906 if (!class)
2907 class = look_up_lock_class(lock, 0);
2909 if (DEBUG_LOCKS_WARN_ON(!class))
2910 return 0;
2912 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
2913 return 0;
2915 if (hlock->class_idx == class - lock_classes + 1)
2916 return 1;
2919 return 0;
2922 static int
2923 __lock_set_class(struct lockdep_map *lock, const char *name,
2924 struct lock_class_key *key, unsigned int subclass,
2925 unsigned long ip)
2927 struct task_struct *curr = current;
2928 struct held_lock *hlock, *prev_hlock;
2929 struct lock_class *class;
2930 unsigned int depth;
2931 int i;
2933 depth = curr->lockdep_depth;
2934 if (DEBUG_LOCKS_WARN_ON(!depth))
2935 return 0;
2937 prev_hlock = NULL;
2938 for (i = depth-1; i >= 0; i--) {
2939 hlock = curr->held_locks + i;
2941 * We must not cross into another context:
2943 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2944 break;
2945 if (match_held_lock(hlock, lock))
2946 goto found_it;
2947 prev_hlock = hlock;
2949 return print_unlock_inbalance_bug(curr, lock, ip);
2951 found_it:
2952 lockdep_init_map(lock, name, key, 0);
2953 class = register_lock_class(lock, subclass, 0);
2954 hlock->class_idx = class - lock_classes + 1;
2956 curr->lockdep_depth = i;
2957 curr->curr_chain_key = hlock->prev_chain_key;
2959 for (; i < depth; i++) {
2960 hlock = curr->held_locks + i;
2961 if (!__lock_acquire(hlock->instance,
2962 hlock_class(hlock)->subclass, hlock->trylock,
2963 hlock->read, hlock->check, hlock->hardirqs_off,
2964 hlock->nest_lock, hlock->acquire_ip,
2965 hlock->references))
2966 return 0;
2969 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2970 return 0;
2971 return 1;
2975 * Remove the lock to the list of currently held locks in a
2976 * potentially non-nested (out of order) manner. This is a
2977 * relatively rare operation, as all the unlock APIs default
2978 * to nested mode (which uses lock_release()):
2980 static int
2981 lock_release_non_nested(struct task_struct *curr,
2982 struct lockdep_map *lock, unsigned long ip)
2984 struct held_lock *hlock, *prev_hlock;
2985 unsigned int depth;
2986 int i;
2989 * Check whether the lock exists in the current stack
2990 * of held locks:
2992 depth = curr->lockdep_depth;
2993 if (DEBUG_LOCKS_WARN_ON(!depth))
2994 return 0;
2996 prev_hlock = NULL;
2997 for (i = depth-1; i >= 0; i--) {
2998 hlock = curr->held_locks + i;
3000 * We must not cross into another context:
3002 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3003 break;
3004 if (match_held_lock(hlock, lock))
3005 goto found_it;
3006 prev_hlock = hlock;
3008 return print_unlock_inbalance_bug(curr, lock, ip);
3010 found_it:
3011 if (hlock->instance == lock)
3012 lock_release_holdtime(hlock);
3014 if (hlock->references) {
3015 hlock->references--;
3016 if (hlock->references) {
3018 * We had, and after removing one, still have
3019 * references, the current lock stack is still
3020 * valid. We're done!
3022 return 1;
3027 * We have the right lock to unlock, 'hlock' points to it.
3028 * Now we remove it from the stack, and add back the other
3029 * entries (if any), recalculating the hash along the way:
3032 curr->lockdep_depth = i;
3033 curr->curr_chain_key = hlock->prev_chain_key;
3035 for (i++; i < depth; i++) {
3036 hlock = curr->held_locks + i;
3037 if (!__lock_acquire(hlock->instance,
3038 hlock_class(hlock)->subclass, hlock->trylock,
3039 hlock->read, hlock->check, hlock->hardirqs_off,
3040 hlock->nest_lock, hlock->acquire_ip,
3041 hlock->references))
3042 return 0;
3045 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3046 return 0;
3047 return 1;
3051 * Remove the lock to the list of currently held locks - this gets
3052 * called on mutex_unlock()/spin_unlock*() (or on a failed
3053 * mutex_lock_interruptible()). This is done for unlocks that nest
3054 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3056 static int lock_release_nested(struct task_struct *curr,
3057 struct lockdep_map *lock, unsigned long ip)
3059 struct held_lock *hlock;
3060 unsigned int depth;
3063 * Pop off the top of the lock stack:
3065 depth = curr->lockdep_depth - 1;
3066 hlock = curr->held_locks + depth;
3069 * Is the unlock non-nested:
3071 if (hlock->instance != lock || hlock->references)
3072 return lock_release_non_nested(curr, lock, ip);
3073 curr->lockdep_depth--;
3075 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3076 return 0;
3078 curr->curr_chain_key = hlock->prev_chain_key;
3080 lock_release_holdtime(hlock);
3082 #ifdef CONFIG_DEBUG_LOCKDEP
3083 hlock->prev_chain_key = 0;
3084 hlock->class_idx = 0;
3085 hlock->acquire_ip = 0;
3086 hlock->irq_context = 0;
3087 #endif
3088 return 1;
3092 * Remove the lock to the list of currently held locks - this gets
3093 * called on mutex_unlock()/spin_unlock*() (or on a failed
3094 * mutex_lock_interruptible()). This is done for unlocks that nest
3095 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3097 static void
3098 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3100 struct task_struct *curr = current;
3102 if (!check_unlock(curr, lock, ip))
3103 return;
3105 if (nested) {
3106 if (!lock_release_nested(curr, lock, ip))
3107 return;
3108 } else {
3109 if (!lock_release_non_nested(curr, lock, ip))
3110 return;
3113 check_chain_key(curr);
3116 static int __lock_is_held(struct lockdep_map *lock)
3118 struct task_struct *curr = current;
3119 int i;
3121 for (i = 0; i < curr->lockdep_depth; i++) {
3122 struct held_lock *hlock = curr->held_locks + i;
3124 if (match_held_lock(hlock, lock))
3125 return 1;
3128 return 0;
3132 * Check whether we follow the irq-flags state precisely:
3134 static void check_flags(unsigned long flags)
3136 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3137 defined(CONFIG_TRACE_IRQFLAGS)
3138 if (!debug_locks)
3139 return;
3141 if (irqs_disabled_flags(flags)) {
3142 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3143 printk("possible reason: unannotated irqs-off.\n");
3145 } else {
3146 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3147 printk("possible reason: unannotated irqs-on.\n");
3152 * We dont accurately track softirq state in e.g.
3153 * hardirq contexts (such as on 4KSTACKS), so only
3154 * check if not in hardirq contexts:
3156 if (!hardirq_count()) {
3157 if (softirq_count())
3158 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3159 else
3160 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3163 if (!debug_locks)
3164 print_irqtrace_events(current);
3165 #endif
3168 void lock_set_class(struct lockdep_map *lock, const char *name,
3169 struct lock_class_key *key, unsigned int subclass,
3170 unsigned long ip)
3172 unsigned long flags;
3174 if (unlikely(current->lockdep_recursion))
3175 return;
3177 raw_local_irq_save(flags);
3178 current->lockdep_recursion = 1;
3179 check_flags(flags);
3180 if (__lock_set_class(lock, name, key, subclass, ip))
3181 check_chain_key(current);
3182 current->lockdep_recursion = 0;
3183 raw_local_irq_restore(flags);
3185 EXPORT_SYMBOL_GPL(lock_set_class);
3188 * We are not always called with irqs disabled - do that here,
3189 * and also avoid lockdep recursion:
3191 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3192 int trylock, int read, int check,
3193 struct lockdep_map *nest_lock, unsigned long ip)
3195 unsigned long flags;
3197 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3199 if (unlikely(current->lockdep_recursion))
3200 return;
3202 raw_local_irq_save(flags);
3203 check_flags(flags);
3205 current->lockdep_recursion = 1;
3206 __lock_acquire(lock, subclass, trylock, read, check,
3207 irqs_disabled_flags(flags), nest_lock, ip, 0);
3208 current->lockdep_recursion = 0;
3209 raw_local_irq_restore(flags);
3211 EXPORT_SYMBOL_GPL(lock_acquire);
3213 void lock_release(struct lockdep_map *lock, int nested,
3214 unsigned long ip)
3216 unsigned long flags;
3218 trace_lock_release(lock, nested, ip);
3220 if (unlikely(current->lockdep_recursion))
3221 return;
3223 raw_local_irq_save(flags);
3224 check_flags(flags);
3225 current->lockdep_recursion = 1;
3226 __lock_release(lock, nested, ip);
3227 current->lockdep_recursion = 0;
3228 raw_local_irq_restore(flags);
3230 EXPORT_SYMBOL_GPL(lock_release);
3232 int lock_is_held(struct lockdep_map *lock)
3234 unsigned long flags;
3235 int ret = 0;
3237 if (unlikely(current->lockdep_recursion))
3238 return ret;
3240 raw_local_irq_save(flags);
3241 check_flags(flags);
3243 current->lockdep_recursion = 1;
3244 ret = __lock_is_held(lock);
3245 current->lockdep_recursion = 0;
3246 raw_local_irq_restore(flags);
3248 return ret;
3250 EXPORT_SYMBOL_GPL(lock_is_held);
3252 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3254 current->lockdep_reclaim_gfp = gfp_mask;
3257 void lockdep_clear_current_reclaim_state(void)
3259 current->lockdep_reclaim_gfp = 0;
3262 #ifdef CONFIG_LOCK_STAT
3263 static int
3264 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3265 unsigned long ip)
3267 if (!debug_locks_off())
3268 return 0;
3269 if (debug_locks_silent)
3270 return 0;
3272 printk("\n=================================\n");
3273 printk( "[ BUG: bad contention detected! ]\n");
3274 printk( "---------------------------------\n");
3275 printk("%s/%d is trying to contend lock (",
3276 curr->comm, task_pid_nr(curr));
3277 print_lockdep_cache(lock);
3278 printk(") at:\n");
3279 print_ip_sym(ip);
3280 printk("but there are no locks held!\n");
3281 printk("\nother info that might help us debug this:\n");
3282 lockdep_print_held_locks(curr);
3284 printk("\nstack backtrace:\n");
3285 dump_stack();
3287 return 0;
3290 static void
3291 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3293 struct task_struct *curr = current;
3294 struct held_lock *hlock, *prev_hlock;
3295 struct lock_class_stats *stats;
3296 unsigned int depth;
3297 int i, contention_point, contending_point;
3299 depth = curr->lockdep_depth;
3300 if (DEBUG_LOCKS_WARN_ON(!depth))
3301 return;
3303 prev_hlock = NULL;
3304 for (i = depth-1; i >= 0; i--) {
3305 hlock = curr->held_locks + i;
3307 * We must not cross into another context:
3309 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3310 break;
3311 if (match_held_lock(hlock, lock))
3312 goto found_it;
3313 prev_hlock = hlock;
3315 print_lock_contention_bug(curr, lock, ip);
3316 return;
3318 found_it:
3319 if (hlock->instance != lock)
3320 return;
3322 hlock->waittime_stamp = sched_clock();
3324 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3325 contending_point = lock_point(hlock_class(hlock)->contending_point,
3326 lock->ip);
3328 stats = get_lock_stats(hlock_class(hlock));
3329 if (contention_point < LOCKSTAT_POINTS)
3330 stats->contention_point[contention_point]++;
3331 if (contending_point < LOCKSTAT_POINTS)
3332 stats->contending_point[contending_point]++;
3333 if (lock->cpu != smp_processor_id())
3334 stats->bounces[bounce_contended + !!hlock->read]++;
3335 put_lock_stats(stats);
3338 static void
3339 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3341 struct task_struct *curr = current;
3342 struct held_lock *hlock, *prev_hlock;
3343 struct lock_class_stats *stats;
3344 unsigned int depth;
3345 u64 now;
3346 s64 waittime = 0;
3347 int i, cpu;
3349 depth = curr->lockdep_depth;
3350 if (DEBUG_LOCKS_WARN_ON(!depth))
3351 return;
3353 prev_hlock = NULL;
3354 for (i = depth-1; i >= 0; i--) {
3355 hlock = curr->held_locks + i;
3357 * We must not cross into another context:
3359 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3360 break;
3361 if (match_held_lock(hlock, lock))
3362 goto found_it;
3363 prev_hlock = hlock;
3365 print_lock_contention_bug(curr, lock, _RET_IP_);
3366 return;
3368 found_it:
3369 if (hlock->instance != lock)
3370 return;
3372 cpu = smp_processor_id();
3373 if (hlock->waittime_stamp) {
3374 now = sched_clock();
3375 waittime = now - hlock->waittime_stamp;
3376 hlock->holdtime_stamp = now;
3379 trace_lock_acquired(lock, ip, waittime);
3381 stats = get_lock_stats(hlock_class(hlock));
3382 if (waittime) {
3383 if (hlock->read)
3384 lock_time_inc(&stats->read_waittime, waittime);
3385 else
3386 lock_time_inc(&stats->write_waittime, waittime);
3388 if (lock->cpu != cpu)
3389 stats->bounces[bounce_acquired + !!hlock->read]++;
3390 put_lock_stats(stats);
3392 lock->cpu = cpu;
3393 lock->ip = ip;
3396 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3398 unsigned long flags;
3400 trace_lock_contended(lock, ip);
3402 if (unlikely(!lock_stat))
3403 return;
3405 if (unlikely(current->lockdep_recursion))
3406 return;
3408 raw_local_irq_save(flags);
3409 check_flags(flags);
3410 current->lockdep_recursion = 1;
3411 __lock_contended(lock, ip);
3412 current->lockdep_recursion = 0;
3413 raw_local_irq_restore(flags);
3415 EXPORT_SYMBOL_GPL(lock_contended);
3417 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3419 unsigned long flags;
3421 if (unlikely(!lock_stat))
3422 return;
3424 if (unlikely(current->lockdep_recursion))
3425 return;
3427 raw_local_irq_save(flags);
3428 check_flags(flags);
3429 current->lockdep_recursion = 1;
3430 __lock_acquired(lock, ip);
3431 current->lockdep_recursion = 0;
3432 raw_local_irq_restore(flags);
3434 EXPORT_SYMBOL_GPL(lock_acquired);
3435 #endif
3438 * Used by the testsuite, sanitize the validator state
3439 * after a simulated failure:
3442 void lockdep_reset(void)
3444 unsigned long flags;
3445 int i;
3447 raw_local_irq_save(flags);
3448 current->curr_chain_key = 0;
3449 current->lockdep_depth = 0;
3450 current->lockdep_recursion = 0;
3451 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3452 nr_hardirq_chains = 0;
3453 nr_softirq_chains = 0;
3454 nr_process_chains = 0;
3455 debug_locks = 1;
3456 for (i = 0; i < CHAINHASH_SIZE; i++)
3457 INIT_LIST_HEAD(chainhash_table + i);
3458 raw_local_irq_restore(flags);
3461 static void zap_class(struct lock_class *class)
3463 int i;
3466 * Remove all dependencies this lock is
3467 * involved in:
3469 for (i = 0; i < nr_list_entries; i++) {
3470 if (list_entries[i].class == class)
3471 list_del_rcu(&list_entries[i].entry);
3474 * Unhash the class and remove it from the all_lock_classes list:
3476 list_del_rcu(&class->hash_entry);
3477 list_del_rcu(&class->lock_entry);
3479 class->key = NULL;
3482 static inline int within(const void *addr, void *start, unsigned long size)
3484 return addr >= start && addr < start + size;
3487 void lockdep_free_key_range(void *start, unsigned long size)
3489 struct lock_class *class, *next;
3490 struct list_head *head;
3491 unsigned long flags;
3492 int i;
3493 int locked;
3495 raw_local_irq_save(flags);
3496 locked = graph_lock();
3499 * Unhash all classes that were created by this module:
3501 for (i = 0; i < CLASSHASH_SIZE; i++) {
3502 head = classhash_table + i;
3503 if (list_empty(head))
3504 continue;
3505 list_for_each_entry_safe(class, next, head, hash_entry) {
3506 if (within(class->key, start, size))
3507 zap_class(class);
3508 else if (within(class->name, start, size))
3509 zap_class(class);
3513 if (locked)
3514 graph_unlock();
3515 raw_local_irq_restore(flags);
3518 void lockdep_reset_lock(struct lockdep_map *lock)
3520 struct lock_class *class, *next;
3521 struct list_head *head;
3522 unsigned long flags;
3523 int i, j;
3524 int locked;
3526 raw_local_irq_save(flags);
3529 * Remove all classes this lock might have:
3531 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3533 * If the class exists we look it up and zap it:
3535 class = look_up_lock_class(lock, j);
3536 if (class)
3537 zap_class(class);
3540 * Debug check: in the end all mapped classes should
3541 * be gone.
3543 locked = graph_lock();
3544 for (i = 0; i < CLASSHASH_SIZE; i++) {
3545 head = classhash_table + i;
3546 if (list_empty(head))
3547 continue;
3548 list_for_each_entry_safe(class, next, head, hash_entry) {
3549 if (unlikely(class == lock->class_cache)) {
3550 if (debug_locks_off_graph_unlock())
3551 WARN_ON(1);
3552 goto out_restore;
3556 if (locked)
3557 graph_unlock();
3559 out_restore:
3560 raw_local_irq_restore(flags);
3563 void lockdep_init(void)
3565 int i;
3568 * Some architectures have their own start_kernel()
3569 * code which calls lockdep_init(), while we also
3570 * call lockdep_init() from the start_kernel() itself,
3571 * and we want to initialize the hashes only once:
3573 if (lockdep_initialized)
3574 return;
3576 for (i = 0; i < CLASSHASH_SIZE; i++)
3577 INIT_LIST_HEAD(classhash_table + i);
3579 for (i = 0; i < CHAINHASH_SIZE; i++)
3580 INIT_LIST_HEAD(chainhash_table + i);
3582 lockdep_initialized = 1;
3585 void __init lockdep_info(void)
3587 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3589 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3590 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3591 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3592 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3593 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3594 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3595 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3597 printk(" memory used by lock dependency info: %lu kB\n",
3598 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3599 sizeof(struct list_head) * CLASSHASH_SIZE +
3600 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3601 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3602 sizeof(struct list_head) * CHAINHASH_SIZE
3603 #ifdef CONFIG_PROVE_LOCKING
3604 + sizeof(struct circular_queue)
3605 #endif
3606 ) / 1024
3609 printk(" per task-struct memory footprint: %lu bytes\n",
3610 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3612 #ifdef CONFIG_DEBUG_LOCKDEP
3613 if (lockdep_init_error) {
3614 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3615 printk("Call stack leading to lockdep invocation was:\n");
3616 print_stack_trace(&lockdep_init_trace, 0);
3618 #endif
3621 static void
3622 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3623 const void *mem_to, struct held_lock *hlock)
3625 if (!debug_locks_off())
3626 return;
3627 if (debug_locks_silent)
3628 return;
3630 printk("\n=========================\n");
3631 printk( "[ BUG: held lock freed! ]\n");
3632 printk( "-------------------------\n");
3633 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3634 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3635 print_lock(hlock);
3636 lockdep_print_held_locks(curr);
3638 printk("\nstack backtrace:\n");
3639 dump_stack();
3642 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3643 const void* lock_from, unsigned long lock_len)
3645 return lock_from + lock_len <= mem_from ||
3646 mem_from + mem_len <= lock_from;
3650 * Called when kernel memory is freed (or unmapped), or if a lock
3651 * is destroyed or reinitialized - this code checks whether there is
3652 * any held lock in the memory range of <from> to <to>:
3654 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3656 struct task_struct *curr = current;
3657 struct held_lock *hlock;
3658 unsigned long flags;
3659 int i;
3661 if (unlikely(!debug_locks))
3662 return;
3664 local_irq_save(flags);
3665 for (i = 0; i < curr->lockdep_depth; i++) {
3666 hlock = curr->held_locks + i;
3668 if (not_in_range(mem_from, mem_len, hlock->instance,
3669 sizeof(*hlock->instance)))
3670 continue;
3672 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3673 break;
3675 local_irq_restore(flags);
3677 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3679 static void print_held_locks_bug(struct task_struct *curr)
3681 if (!debug_locks_off())
3682 return;
3683 if (debug_locks_silent)
3684 return;
3686 printk("\n=====================================\n");
3687 printk( "[ BUG: lock held at task exit time! ]\n");
3688 printk( "-------------------------------------\n");
3689 printk("%s/%d is exiting with locks still held!\n",
3690 curr->comm, task_pid_nr(curr));
3691 lockdep_print_held_locks(curr);
3693 printk("\nstack backtrace:\n");
3694 dump_stack();
3697 void debug_check_no_locks_held(struct task_struct *task)
3699 if (unlikely(task->lockdep_depth > 0))
3700 print_held_locks_bug(task);
3703 void debug_show_all_locks(void)
3705 struct task_struct *g, *p;
3706 int count = 10;
3707 int unlock = 1;
3709 if (unlikely(!debug_locks)) {
3710 printk("INFO: lockdep is turned off.\n");
3711 return;
3713 printk("\nShowing all locks held in the system:\n");
3716 * Here we try to get the tasklist_lock as hard as possible,
3717 * if not successful after 2 seconds we ignore it (but keep
3718 * trying). This is to enable a debug printout even if a
3719 * tasklist_lock-holding task deadlocks or crashes.
3721 retry:
3722 if (!read_trylock(&tasklist_lock)) {
3723 if (count == 10)
3724 printk("hm, tasklist_lock locked, retrying... ");
3725 if (count) {
3726 count--;
3727 printk(" #%d", 10-count);
3728 mdelay(200);
3729 goto retry;
3731 printk(" ignoring it.\n");
3732 unlock = 0;
3733 } else {
3734 if (count != 10)
3735 printk(KERN_CONT " locked it.\n");
3738 do_each_thread(g, p) {
3740 * It's not reliable to print a task's held locks
3741 * if it's not sleeping (or if it's not the current
3742 * task):
3744 if (p->state == TASK_RUNNING && p != current)
3745 continue;
3746 if (p->lockdep_depth)
3747 lockdep_print_held_locks(p);
3748 if (!unlock)
3749 if (read_trylock(&tasklist_lock))
3750 unlock = 1;
3751 } while_each_thread(g, p);
3753 printk("\n");
3754 printk("=============================================\n\n");
3756 if (unlock)
3757 read_unlock(&tasklist_lock);
3759 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3762 * Careful: only use this function if you are sure that
3763 * the task cannot run in parallel!
3765 void __debug_show_held_locks(struct task_struct *task)
3767 if (unlikely(!debug_locks)) {
3768 printk("INFO: lockdep is turned off.\n");
3769 return;
3771 lockdep_print_held_locks(task);
3773 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3775 void debug_show_held_locks(struct task_struct *task)
3777 __debug_show_held_locks(task);
3779 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3781 void lockdep_sys_exit(void)
3783 struct task_struct *curr = current;
3785 if (unlikely(curr->lockdep_depth)) {
3786 if (!debug_locks_off())
3787 return;
3788 printk("\n================================================\n");
3789 printk( "[ BUG: lock held when returning to user space! ]\n");
3790 printk( "------------------------------------------------\n");
3791 printk("%s/%d is leaving the kernel with locks still held!\n",
3792 curr->comm, curr->pid);
3793 lockdep_print_held_locks(curr);