lockdep: BFS cleanup
[linux-2.6/cjktty.git] / kernel / lockdep.c
blob1cedb00e3e7a9d664165af50c8c1f7d5574d5265
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);
370 trace->max_entries = trace->nr_entries;
372 nr_stack_trace_entries += trace->nr_entries;
374 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
375 if (!debug_locks_off_graph_unlock())
376 return 0;
378 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
379 printk("turning off the locking correctness validator.\n");
380 dump_stack();
382 return 0;
385 return 1;
388 unsigned int nr_hardirq_chains;
389 unsigned int nr_softirq_chains;
390 unsigned int nr_process_chains;
391 unsigned int max_lockdep_depth;
392 unsigned int max_recursion_depth;
394 #ifdef CONFIG_DEBUG_LOCKDEP
396 * We cannot printk in early bootup code. Not even early_printk()
397 * might work. So we mark any initialization errors and printk
398 * about it later on, in lockdep_info().
400 static int lockdep_init_error;
401 static unsigned long lockdep_init_trace_data[20];
402 static struct stack_trace lockdep_init_trace = {
403 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
404 .entries = lockdep_init_trace_data,
408 * Various lockdep statistics:
410 atomic_t chain_lookup_hits;
411 atomic_t chain_lookup_misses;
412 atomic_t hardirqs_on_events;
413 atomic_t hardirqs_off_events;
414 atomic_t redundant_hardirqs_on;
415 atomic_t redundant_hardirqs_off;
416 atomic_t softirqs_on_events;
417 atomic_t softirqs_off_events;
418 atomic_t redundant_softirqs_on;
419 atomic_t redundant_softirqs_off;
420 atomic_t nr_unused_locks;
421 atomic_t nr_cyclic_checks;
422 atomic_t nr_cyclic_check_recursions;
423 atomic_t nr_find_usage_forwards_checks;
424 atomic_t nr_find_usage_forwards_recursions;
425 atomic_t nr_find_usage_backwards_checks;
426 atomic_t nr_find_usage_backwards_recursions;
427 #endif
430 * Locking printouts:
433 #define __USAGE(__STATE) \
434 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
435 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
436 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
437 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
439 static const char *usage_str[] =
441 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
442 #include "lockdep_states.h"
443 #undef LOCKDEP_STATE
444 [LOCK_USED] = "INITIAL USE",
447 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
449 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
452 static inline unsigned long lock_flag(enum lock_usage_bit bit)
454 return 1UL << bit;
457 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
459 char c = '.';
461 if (class->usage_mask & lock_flag(bit + 2))
462 c = '+';
463 if (class->usage_mask & lock_flag(bit)) {
464 c = '-';
465 if (class->usage_mask & lock_flag(bit + 2))
466 c = '?';
469 return c;
472 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
474 int i = 0;
476 #define LOCKDEP_STATE(__STATE) \
477 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
478 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
479 #include "lockdep_states.h"
480 #undef LOCKDEP_STATE
482 usage[i] = '\0';
485 static void print_lock_name(struct lock_class *class)
487 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
488 const char *name;
490 get_usage_chars(class, usage);
492 name = class->name;
493 if (!name) {
494 name = __get_key_name(class->key, str);
495 printk(" (%s", name);
496 } else {
497 printk(" (%s", name);
498 if (class->name_version > 1)
499 printk("#%d", class->name_version);
500 if (class->subclass)
501 printk("/%d", class->subclass);
503 printk("){%s}", usage);
506 static void print_lockdep_cache(struct lockdep_map *lock)
508 const char *name;
509 char str[KSYM_NAME_LEN];
511 name = lock->name;
512 if (!name)
513 name = __get_key_name(lock->key->subkeys, str);
515 printk("%s", name);
518 static void print_lock(struct held_lock *hlock)
520 print_lock_name(hlock_class(hlock));
521 printk(", at: ");
522 print_ip_sym(hlock->acquire_ip);
525 static void lockdep_print_held_locks(struct task_struct *curr)
527 int i, depth = curr->lockdep_depth;
529 if (!depth) {
530 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
531 return;
533 printk("%d lock%s held by %s/%d:\n",
534 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
536 for (i = 0; i < depth; i++) {
537 printk(" #%d: ", i);
538 print_lock(curr->held_locks + i);
542 static void print_kernel_version(void)
544 printk("%s %.*s\n", init_utsname()->release,
545 (int)strcspn(init_utsname()->version, " "),
546 init_utsname()->version);
549 static int very_verbose(struct lock_class *class)
551 #if VERY_VERBOSE
552 return class_filter(class);
553 #endif
554 return 0;
558 * Is this the address of a static object:
560 static int static_obj(void *obj)
562 unsigned long start = (unsigned long) &_stext,
563 end = (unsigned long) &_end,
564 addr = (unsigned long) obj;
565 #ifdef CONFIG_SMP
566 int i;
567 #endif
570 * static variable?
572 if ((addr >= start) && (addr < end))
573 return 1;
575 #ifdef CONFIG_SMP
577 * percpu var?
579 for_each_possible_cpu(i) {
580 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
581 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
582 + per_cpu_offset(i);
584 if ((addr >= start) && (addr < end))
585 return 1;
587 #endif
590 * module var?
592 return is_module_address(addr);
596 * To make lock name printouts unique, we calculate a unique
597 * class->name_version generation counter:
599 static int count_matching_names(struct lock_class *new_class)
601 struct lock_class *class;
602 int count = 0;
604 if (!new_class->name)
605 return 0;
607 list_for_each_entry(class, &all_lock_classes, lock_entry) {
608 if (new_class->key - new_class->subclass == class->key)
609 return class->name_version;
610 if (class->name && !strcmp(class->name, new_class->name))
611 count = max(count, class->name_version);
614 return count + 1;
618 * Register a lock's class in the hash-table, if the class is not present
619 * yet. Otherwise we look it up. We cache the result in the lock object
620 * itself, so actual lookup of the hash should be once per lock object.
622 static inline struct lock_class *
623 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
625 struct lockdep_subclass_key *key;
626 struct list_head *hash_head;
627 struct lock_class *class;
629 #ifdef CONFIG_DEBUG_LOCKDEP
631 * If the architecture calls into lockdep before initializing
632 * the hashes then we'll warn about it later. (we cannot printk
633 * right now)
635 if (unlikely(!lockdep_initialized)) {
636 lockdep_init();
637 lockdep_init_error = 1;
638 save_stack_trace(&lockdep_init_trace);
640 #endif
643 * Static locks do not have their class-keys yet - for them the key
644 * is the lock object itself:
646 if (unlikely(!lock->key))
647 lock->key = (void *)lock;
650 * NOTE: the class-key must be unique. For dynamic locks, a static
651 * lock_class_key variable is passed in through the mutex_init()
652 * (or spin_lock_init()) call - which acts as the key. For static
653 * locks we use the lock object itself as the key.
655 BUILD_BUG_ON(sizeof(struct lock_class_key) >
656 sizeof(struct lockdep_map));
658 key = lock->key->subkeys + subclass;
660 hash_head = classhashentry(key);
663 * We can walk the hash lockfree, because the hash only
664 * grows, and we are careful when adding entries to the end:
666 list_for_each_entry(class, hash_head, hash_entry) {
667 if (class->key == key) {
668 WARN_ON_ONCE(class->name != lock->name);
669 return class;
673 return NULL;
677 * Register a lock's class in the hash-table, if the class is not present
678 * yet. Otherwise we look it up. We cache the result in the lock object
679 * itself, so actual lookup of the hash should be once per lock object.
681 static inline struct lock_class *
682 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
684 struct lockdep_subclass_key *key;
685 struct list_head *hash_head;
686 struct lock_class *class;
687 unsigned long flags;
689 class = look_up_lock_class(lock, subclass);
690 if (likely(class))
691 return class;
694 * Debug-check: all keys must be persistent!
696 if (!static_obj(lock->key)) {
697 debug_locks_off();
698 printk("INFO: trying to register non-static key.\n");
699 printk("the code is fine but needs lockdep annotation.\n");
700 printk("turning off the locking correctness validator.\n");
701 dump_stack();
703 return NULL;
706 key = lock->key->subkeys + subclass;
707 hash_head = classhashentry(key);
709 raw_local_irq_save(flags);
710 if (!graph_lock()) {
711 raw_local_irq_restore(flags);
712 return NULL;
715 * We have to do the hash-walk again, to avoid races
716 * with another CPU:
718 list_for_each_entry(class, hash_head, hash_entry)
719 if (class->key == key)
720 goto out_unlock_set;
722 * Allocate a new key from the static array, and add it to
723 * the hash:
725 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
726 if (!debug_locks_off_graph_unlock()) {
727 raw_local_irq_restore(flags);
728 return NULL;
730 raw_local_irq_restore(flags);
732 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
733 printk("turning off the locking correctness validator.\n");
734 dump_stack();
735 return NULL;
737 class = lock_classes + nr_lock_classes++;
738 debug_atomic_inc(&nr_unused_locks);
739 class->key = key;
740 class->name = lock->name;
741 class->subclass = subclass;
742 INIT_LIST_HEAD(&class->lock_entry);
743 INIT_LIST_HEAD(&class->locks_before);
744 INIT_LIST_HEAD(&class->locks_after);
745 class->name_version = count_matching_names(class);
747 * We use RCU's safe list-add method to make
748 * parallel walking of the hash-list safe:
750 list_add_tail_rcu(&class->hash_entry, hash_head);
752 * Add it to the global list of classes:
754 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
756 if (verbose(class)) {
757 graph_unlock();
758 raw_local_irq_restore(flags);
760 printk("\nnew class %p: %s", class->key, class->name);
761 if (class->name_version > 1)
762 printk("#%d", class->name_version);
763 printk("\n");
764 dump_stack();
766 raw_local_irq_save(flags);
767 if (!graph_lock()) {
768 raw_local_irq_restore(flags);
769 return NULL;
772 out_unlock_set:
773 graph_unlock();
774 raw_local_irq_restore(flags);
776 if (!subclass || force)
777 lock->class_cache = class;
779 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
780 return NULL;
782 return class;
785 #ifdef CONFIG_PROVE_LOCKING
787 * Allocate a lockdep entry. (assumes the graph_lock held, returns
788 * with NULL on failure)
790 static struct lock_list *alloc_list_entry(void)
792 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
793 if (!debug_locks_off_graph_unlock())
794 return NULL;
796 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
797 printk("turning off the locking correctness validator.\n");
798 dump_stack();
799 return NULL;
801 return list_entries + nr_list_entries++;
805 * Add a new dependency to the head of the list:
807 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
808 struct list_head *head, unsigned long ip, int distance)
810 struct lock_list *entry;
812 * Lock not present yet - get a new dependency struct and
813 * add it to the list:
815 entry = alloc_list_entry();
816 if (!entry)
817 return 0;
819 if (!save_trace(&entry->trace))
820 return 0;
822 entry->class = this;
823 entry->distance = distance;
825 * Since we never remove from the dependency list, the list can
826 * be walked lockless by other CPUs, it's only allocation
827 * that must be protected by the spinlock. But this also means
828 * we must make new entries visible only once writes to the
829 * entry become visible - hence the RCU op:
831 list_add_tail_rcu(&entry->entry, head);
833 return 1;
836 /*For good efficiency of modular, we use power of 2*/
837 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
838 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
840 /* The circular_queue and helpers is used to implement the
841 * breadth-first search(BFS)algorithem, by which we can build
842 * the shortest path from the next lock to be acquired to the
843 * previous held lock if there is a circular between them.
844 * */
845 struct circular_queue {
846 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
847 unsigned int front, rear;
850 static struct circular_queue lock_cq;
851 static unsigned long bfs_accessed[BITS_TO_LONGS(MAX_LOCKDEP_ENTRIES)];
853 unsigned int max_bfs_queue_depth;
855 static inline void __cq_init(struct circular_queue *cq)
857 cq->front = cq->rear = 0;
858 bitmap_zero(bfs_accessed, MAX_LOCKDEP_ENTRIES);
861 static inline int __cq_empty(struct circular_queue *cq)
863 return (cq->front == cq->rear);
866 static inline int __cq_full(struct circular_queue *cq)
868 return ((cq->rear + 1) & CQ_MASK) == cq->front;
871 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
873 if (__cq_full(cq))
874 return -1;
876 cq->element[cq->rear] = elem;
877 cq->rear = (cq->rear + 1) & CQ_MASK;
878 return 0;
881 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
883 if (__cq_empty(cq))
884 return -1;
886 *elem = cq->element[cq->front];
887 cq->front = (cq->front + 1) & CQ_MASK;
888 return 0;
891 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
893 return (cq->rear - cq->front) & CQ_MASK;
896 static inline void mark_lock_accessed(struct lock_list *lock,
897 struct lock_list *parent)
899 unsigned long nr;
900 nr = lock - list_entries;
901 WARN_ON(nr >= nr_list_entries);
902 lock->parent = parent;
903 set_bit(nr, bfs_accessed);
906 static inline unsigned long lock_accessed(struct lock_list *lock)
908 unsigned long nr;
909 nr = lock - list_entries;
910 WARN_ON(nr >= nr_list_entries);
911 return test_bit(nr, bfs_accessed);
914 static inline struct lock_list *get_lock_parent(struct lock_list *child)
916 return child->parent;
919 static inline int get_lock_depth(struct lock_list *child)
921 int depth = 0;
922 struct lock_list *parent;
924 while ((parent = get_lock_parent(child))) {
925 child = parent;
926 depth++;
928 return depth;
931 static int __bfs(struct lock_list *source_entry,
932 void *data,
933 int (*match)(struct lock_list *entry, void *data),
934 struct lock_list **target_entry,
935 int forward)
937 struct lock_list *entry;
938 struct list_head *head;
939 struct circular_queue *cq = &lock_cq;
940 int ret = 1;
942 if (match(source_entry, data)) {
943 *target_entry = source_entry;
944 ret = 0;
945 goto exit;
948 if (forward)
949 head = &source_entry->class->locks_after;
950 else
951 head = &source_entry->class->locks_before;
953 if (list_empty(head))
954 goto exit;
956 __cq_init(cq);
957 __cq_enqueue(cq, (unsigned long)source_entry);
959 while (!__cq_empty(cq)) {
960 struct lock_list *lock;
962 __cq_dequeue(cq, (unsigned long *)&lock);
964 if (!lock->class) {
965 ret = -2;
966 goto exit;
969 if (forward)
970 head = &lock->class->locks_after;
971 else
972 head = &lock->class->locks_before;
974 list_for_each_entry(entry, head, entry) {
975 if (!lock_accessed(entry)) {
976 unsigned int cq_depth;
977 mark_lock_accessed(entry, lock);
978 if (match(entry, data)) {
979 *target_entry = entry;
980 ret = 0;
981 goto exit;
984 if (__cq_enqueue(cq, (unsigned long)entry)) {
985 ret = -1;
986 goto exit;
988 cq_depth = __cq_get_elem_count(cq);
989 if (max_bfs_queue_depth < cq_depth)
990 max_bfs_queue_depth = cq_depth;
994 exit:
995 return ret;
998 static inline int __bfs_forwards(struct lock_list *src_entry,
999 void *data,
1000 int (*match)(struct lock_list *entry, void *data),
1001 struct lock_list **target_entry)
1003 return __bfs(src_entry, data, match, target_entry, 1);
1007 static inline int __bfs_backwards(struct lock_list *src_entry,
1008 void *data,
1009 int (*match)(struct lock_list *entry, void *data),
1010 struct lock_list **target_entry)
1012 return __bfs(src_entry, data, match, target_entry, 0);
1017 * Recursive, forwards-direction lock-dependency checking, used for
1018 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1019 * checking.
1023 * Print a dependency chain entry (this is only done when a deadlock
1024 * has been detected):
1026 static noinline int
1027 print_circular_bug_entry(struct lock_list *target, int depth)
1029 if (debug_locks_silent)
1030 return 0;
1031 printk("\n-> #%u", depth);
1032 print_lock_name(target->class);
1033 printk(":\n");
1034 print_stack_trace(&target->trace, 6);
1036 return 0;
1040 * When a circular dependency is detected, print the
1041 * header first:
1043 static noinline int
1044 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1045 struct held_lock *check_src,
1046 struct held_lock *check_tgt)
1048 struct task_struct *curr = current;
1050 if (debug_locks_silent)
1051 return 0;
1053 printk("\n=======================================================\n");
1054 printk( "[ INFO: possible circular locking dependency detected ]\n");
1055 print_kernel_version();
1056 printk( "-------------------------------------------------------\n");
1057 printk("%s/%d is trying to acquire lock:\n",
1058 curr->comm, task_pid_nr(curr));
1059 print_lock(check_src);
1060 printk("\nbut task is already holding lock:\n");
1061 print_lock(check_tgt);
1062 printk("\nwhich lock already depends on the new lock.\n\n");
1063 printk("\nthe existing dependency chain (in reverse order) is:\n");
1065 print_circular_bug_entry(entry, depth);
1067 return 0;
1070 static inline int class_equal(struct lock_list *entry, void *data)
1072 return entry->class == data;
1075 static noinline int print_circular_bug(struct lock_list *this,
1076 struct lock_list *target,
1077 struct held_lock *check_src,
1078 struct held_lock *check_tgt)
1080 struct task_struct *curr = current;
1081 struct lock_list *parent;
1082 int depth;
1084 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1085 return 0;
1087 if (!save_trace(&this->trace))
1088 return 0;
1090 depth = get_lock_depth(target);
1092 print_circular_bug_header(target, depth, check_src, check_tgt);
1094 parent = get_lock_parent(target);
1096 while (parent) {
1097 print_circular_bug_entry(parent, --depth);
1098 parent = get_lock_parent(parent);
1101 printk("\nother info that might help us debug this:\n\n");
1102 lockdep_print_held_locks(curr);
1104 printk("\nstack backtrace:\n");
1105 dump_stack();
1107 return 0;
1110 static noinline int print_bfs_bug(int ret)
1112 if (!debug_locks_off_graph_unlock())
1113 return 0;
1115 WARN(1, "lockdep bfs error:%d\n", ret);
1117 return 0;
1120 static int noop_count(struct lock_list *entry, void *data)
1122 (*(unsigned long *)data)++;
1123 return 0;
1126 unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1128 unsigned long count = 0;
1129 struct lock_list *uninitialized_var(target_entry);
1131 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1133 return count;
1135 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1137 unsigned long ret, flags;
1138 struct lock_list this;
1140 this.parent = NULL;
1141 this.class = class;
1143 local_irq_save(flags);
1144 __raw_spin_lock(&lockdep_lock);
1145 ret = __lockdep_count_forward_deps(&this);
1146 __raw_spin_unlock(&lockdep_lock);
1147 local_irq_restore(flags);
1149 return ret;
1152 unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1154 unsigned long count = 0;
1155 struct lock_list *uninitialized_var(target_entry);
1157 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1159 return count;
1162 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1164 unsigned long ret, flags;
1165 struct lock_list this;
1167 this.parent = NULL;
1168 this.class = class;
1170 local_irq_save(flags);
1171 __raw_spin_lock(&lockdep_lock);
1172 ret = __lockdep_count_backward_deps(&this);
1173 __raw_spin_unlock(&lockdep_lock);
1174 local_irq_restore(flags);
1176 return ret;
1180 * Prove that the dependency graph starting at <entry> can not
1181 * lead to <target>. Print an error and return 0 if it does.
1183 static noinline int
1184 check_noncircular(struct lock_list *root, struct lock_class *target,
1185 struct lock_list **target_entry)
1187 int result;
1189 debug_atomic_inc(&nr_cyclic_checks);
1191 result = __bfs_forwards(root, target, class_equal, target_entry);
1193 return result;
1196 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1198 * Forwards and backwards subgraph searching, for the purposes of
1199 * proving that two subgraphs can be connected by a new dependency
1200 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1203 static inline int usage_match(struct lock_list *entry, void *bit)
1205 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1211 * Find a node in the forwards-direction dependency sub-graph starting
1212 * at @root->class that matches @bit.
1214 * Return 0 if such a node exists in the subgraph, and put that node
1215 * into *@target_entry.
1217 * Return 1 otherwise and keep *@target_entry unchanged.
1218 * Return <0 on error.
1220 static int
1221 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1222 struct lock_list **target_entry)
1224 int result;
1226 debug_atomic_inc(&nr_find_usage_forwards_checks);
1228 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1230 return result;
1234 * Find a node in the backwards-direction dependency sub-graph starting
1235 * at @root->class that matches @bit.
1237 * Return 0 if such a node exists in the subgraph, and put that node
1238 * into *@target_entry.
1240 * Return 1 otherwise and keep *@target_entry unchanged.
1241 * Return <0 on error.
1243 static int
1244 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1245 struct lock_list **target_entry)
1247 int result;
1249 debug_atomic_inc(&nr_find_usage_backwards_checks);
1251 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1253 return result;
1256 static void print_lock_class_header(struct lock_class *class, int depth)
1258 int bit;
1260 printk("%*s->", depth, "");
1261 print_lock_name(class);
1262 printk(" ops: %lu", class->ops);
1263 printk(" {\n");
1265 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1266 if (class->usage_mask & (1 << bit)) {
1267 int len = depth;
1269 len += printk("%*s %s", depth, "", usage_str[bit]);
1270 len += printk(" at:\n");
1271 print_stack_trace(class->usage_traces + bit, len);
1274 printk("%*s }\n", depth, "");
1276 printk("%*s ... key at: ",depth,"");
1277 print_ip_sym((unsigned long)class->key);
1281 * printk the shortest lock dependencies from @start to @end in reverse order:
1283 static void __used
1284 print_shortest_lock_dependencies(struct lock_list *leaf,
1285 struct lock_list *root)
1287 struct lock_list *entry = leaf;
1288 int depth;
1290 /*compute depth from generated tree by BFS*/
1291 depth = get_lock_depth(leaf);
1293 do {
1294 print_lock_class_header(entry->class, depth);
1295 printk("%*s ... acquired at:\n", depth, "");
1296 print_stack_trace(&entry->trace, 2);
1297 printk("\n");
1299 if (depth == 0 && (entry != root)) {
1300 printk("lockdep:%s bad BFS generated tree\n", __func__);
1301 break;
1304 entry = get_lock_parent(entry);
1305 depth--;
1306 } while (entry && (depth >= 0));
1308 return;
1311 static int
1312 print_bad_irq_dependency(struct task_struct *curr,
1313 struct lock_list *prev_root,
1314 struct lock_list *next_root,
1315 struct lock_list *backwards_entry,
1316 struct lock_list *forwards_entry,
1317 struct held_lock *prev,
1318 struct held_lock *next,
1319 enum lock_usage_bit bit1,
1320 enum lock_usage_bit bit2,
1321 const char *irqclass)
1323 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1324 return 0;
1326 printk("\n======================================================\n");
1327 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1328 irqclass, irqclass);
1329 print_kernel_version();
1330 printk( "------------------------------------------------------\n");
1331 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1332 curr->comm, task_pid_nr(curr),
1333 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1334 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1335 curr->hardirqs_enabled,
1336 curr->softirqs_enabled);
1337 print_lock(next);
1339 printk("\nand this task is already holding:\n");
1340 print_lock(prev);
1341 printk("which would create a new lock dependency:\n");
1342 print_lock_name(hlock_class(prev));
1343 printk(" ->");
1344 print_lock_name(hlock_class(next));
1345 printk("\n");
1347 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1348 irqclass);
1349 print_lock_name(backwards_entry->class);
1350 printk("\n... which became %s-irq-safe at:\n", irqclass);
1352 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1354 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1355 print_lock_name(forwards_entry->class);
1356 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1357 printk("...");
1359 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1361 printk("\nother info that might help us debug this:\n\n");
1362 lockdep_print_held_locks(curr);
1364 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1365 printk(" and the holding lock:\n");
1366 if (!save_trace(&prev_root->trace))
1367 return 0;
1368 print_shortest_lock_dependencies(backwards_entry, prev_root);
1370 printk("\nthe dependencies between the lock to be acquired");
1371 printk(" and %s-irq-unsafe lock:\n", irqclass);
1372 if (!save_trace(&next_root->trace))
1373 return 0;
1374 print_shortest_lock_dependencies(forwards_entry, next_root);
1376 printk("\nstack backtrace:\n");
1377 dump_stack();
1379 return 0;
1382 static int
1383 check_usage(struct task_struct *curr, struct held_lock *prev,
1384 struct held_lock *next, enum lock_usage_bit bit_backwards,
1385 enum lock_usage_bit bit_forwards, const char *irqclass)
1387 int ret;
1388 struct lock_list this, that;
1389 struct lock_list *uninitialized_var(target_entry);
1390 struct lock_list *uninitialized_var(target_entry1);
1392 this.parent = NULL;
1394 this.class = hlock_class(prev);
1395 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1396 if (ret < 0)
1397 return print_bfs_bug(ret);
1398 if (ret == 1)
1399 return ret;
1401 that.parent = NULL;
1402 that.class = hlock_class(next);
1403 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1404 if (ret < 0)
1405 return print_bfs_bug(ret);
1406 if (ret == 1)
1407 return ret;
1409 return print_bad_irq_dependency(curr, &this, &that,
1410 target_entry, target_entry1,
1411 prev, next,
1412 bit_backwards, bit_forwards, irqclass);
1415 static const char *state_names[] = {
1416 #define LOCKDEP_STATE(__STATE) \
1417 __stringify(__STATE),
1418 #include "lockdep_states.h"
1419 #undef LOCKDEP_STATE
1422 static const char *state_rnames[] = {
1423 #define LOCKDEP_STATE(__STATE) \
1424 __stringify(__STATE)"-READ",
1425 #include "lockdep_states.h"
1426 #undef LOCKDEP_STATE
1429 static inline const char *state_name(enum lock_usage_bit bit)
1431 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1434 static int exclusive_bit(int new_bit)
1437 * USED_IN
1438 * USED_IN_READ
1439 * ENABLED
1440 * ENABLED_READ
1442 * bit 0 - write/read
1443 * bit 1 - used_in/enabled
1444 * bit 2+ state
1447 int state = new_bit & ~3;
1448 int dir = new_bit & 2;
1451 * keep state, bit flip the direction and strip read.
1453 return state | (dir ^ 2);
1456 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1457 struct held_lock *next, enum lock_usage_bit bit)
1460 * Prove that the new dependency does not connect a hardirq-safe
1461 * lock with a hardirq-unsafe lock - to achieve this we search
1462 * the backwards-subgraph starting at <prev>, and the
1463 * forwards-subgraph starting at <next>:
1465 if (!check_usage(curr, prev, next, bit,
1466 exclusive_bit(bit), state_name(bit)))
1467 return 0;
1469 bit++; /* _READ */
1472 * Prove that the new dependency does not connect a hardirq-safe-read
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 return 1;
1484 static int
1485 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1486 struct held_lock *next)
1488 #define LOCKDEP_STATE(__STATE) \
1489 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1490 return 0;
1491 #include "lockdep_states.h"
1492 #undef LOCKDEP_STATE
1494 return 1;
1497 static void inc_chains(void)
1499 if (current->hardirq_context)
1500 nr_hardirq_chains++;
1501 else {
1502 if (current->softirq_context)
1503 nr_softirq_chains++;
1504 else
1505 nr_process_chains++;
1509 #else
1511 static inline int
1512 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1513 struct held_lock *next)
1515 return 1;
1518 static inline void inc_chains(void)
1520 nr_process_chains++;
1523 #endif
1525 static int
1526 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1527 struct held_lock *next)
1529 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1530 return 0;
1532 printk("\n=============================================\n");
1533 printk( "[ INFO: possible recursive locking detected ]\n");
1534 print_kernel_version();
1535 printk( "---------------------------------------------\n");
1536 printk("%s/%d is trying to acquire lock:\n",
1537 curr->comm, task_pid_nr(curr));
1538 print_lock(next);
1539 printk("\nbut task is already holding lock:\n");
1540 print_lock(prev);
1542 printk("\nother info that might help us debug this:\n");
1543 lockdep_print_held_locks(curr);
1545 printk("\nstack backtrace:\n");
1546 dump_stack();
1548 return 0;
1552 * Check whether we are holding such a class already.
1554 * (Note that this has to be done separately, because the graph cannot
1555 * detect such classes of deadlocks.)
1557 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1559 static int
1560 check_deadlock(struct task_struct *curr, struct held_lock *next,
1561 struct lockdep_map *next_instance, int read)
1563 struct held_lock *prev;
1564 struct held_lock *nest = NULL;
1565 int i;
1567 for (i = 0; i < curr->lockdep_depth; i++) {
1568 prev = curr->held_locks + i;
1570 if (prev->instance == next->nest_lock)
1571 nest = prev;
1573 if (hlock_class(prev) != hlock_class(next))
1574 continue;
1577 * Allow read-after-read recursion of the same
1578 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1580 if ((read == 2) && prev->read)
1581 return 2;
1584 * We're holding the nest_lock, which serializes this lock's
1585 * nesting behaviour.
1587 if (nest)
1588 return 2;
1590 return print_deadlock_bug(curr, prev, next);
1592 return 1;
1596 * There was a chain-cache miss, and we are about to add a new dependency
1597 * to a previous lock. We recursively validate the following rules:
1599 * - would the adding of the <prev> -> <next> dependency create a
1600 * circular dependency in the graph? [== circular deadlock]
1602 * - does the new prev->next dependency connect any hardirq-safe lock
1603 * (in the full backwards-subgraph starting at <prev>) with any
1604 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1605 * <next>)? [== illegal lock inversion with hardirq contexts]
1607 * - does the new prev->next dependency connect any softirq-safe lock
1608 * (in the full backwards-subgraph starting at <prev>) with any
1609 * softirq-unsafe lock (in the full forwards-subgraph starting at
1610 * <next>)? [== illegal lock inversion with softirq contexts]
1612 * any of these scenarios could lead to a deadlock.
1614 * Then if all the validations pass, we add the forwards and backwards
1615 * dependency.
1617 static int
1618 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1619 struct held_lock *next, int distance)
1621 struct lock_list *entry;
1622 int ret;
1623 struct lock_list this;
1624 struct lock_list *uninitialized_var(target_entry);
1627 * Prove that the new <prev> -> <next> dependency would not
1628 * create a circular dependency in the graph. (We do this by
1629 * forward-recursing into the graph starting at <next>, and
1630 * checking whether we can reach <prev>.)
1632 * We are using global variables to control the recursion, to
1633 * keep the stackframe size of the recursive functions low:
1635 this.class = hlock_class(next);
1636 this.parent = NULL;
1637 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1638 if (unlikely(!ret))
1639 return print_circular_bug(&this, target_entry, next, prev);
1640 else if (unlikely(ret < 0))
1641 return print_bfs_bug(ret);
1643 if (!check_prev_add_irq(curr, prev, next))
1644 return 0;
1647 * For recursive read-locks we do all the dependency checks,
1648 * but we dont store read-triggered dependencies (only
1649 * write-triggered dependencies). This ensures that only the
1650 * write-side dependencies matter, and that if for example a
1651 * write-lock never takes any other locks, then the reads are
1652 * equivalent to a NOP.
1654 if (next->read == 2 || prev->read == 2)
1655 return 1;
1657 * Is the <prev> -> <next> dependency already present?
1659 * (this may occur even though this is a new chain: consider
1660 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1661 * chains - the second one will be new, but L1 already has
1662 * L2 added to its dependency list, due to the first chain.)
1664 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1665 if (entry->class == hlock_class(next)) {
1666 if (distance == 1)
1667 entry->distance = 1;
1668 return 2;
1673 * Ok, all validations passed, add the new lock
1674 * to the previous lock's dependency list:
1676 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1677 &hlock_class(prev)->locks_after,
1678 next->acquire_ip, distance);
1680 if (!ret)
1681 return 0;
1683 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1684 &hlock_class(next)->locks_before,
1685 next->acquire_ip, distance);
1686 if (!ret)
1687 return 0;
1690 * Debugging printouts:
1692 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1693 graph_unlock();
1694 printk("\n new dependency: ");
1695 print_lock_name(hlock_class(prev));
1696 printk(" => ");
1697 print_lock_name(hlock_class(next));
1698 printk("\n");
1699 dump_stack();
1700 return graph_lock();
1702 return 1;
1706 * Add the dependency to all directly-previous locks that are 'relevant'.
1707 * The ones that are relevant are (in increasing distance from curr):
1708 * all consecutive trylock entries and the final non-trylock entry - or
1709 * the end of this context's lock-chain - whichever comes first.
1711 static int
1712 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1714 int depth = curr->lockdep_depth;
1715 struct held_lock *hlock;
1718 * Debugging checks.
1720 * Depth must not be zero for a non-head lock:
1722 if (!depth)
1723 goto out_bug;
1725 * At least two relevant locks must exist for this
1726 * to be a head:
1728 if (curr->held_locks[depth].irq_context !=
1729 curr->held_locks[depth-1].irq_context)
1730 goto out_bug;
1732 for (;;) {
1733 int distance = curr->lockdep_depth - depth + 1;
1734 hlock = curr->held_locks + depth-1;
1736 * Only non-recursive-read entries get new dependencies
1737 * added:
1739 if (hlock->read != 2) {
1740 if (!check_prev_add(curr, hlock, next, distance))
1741 return 0;
1743 * Stop after the first non-trylock entry,
1744 * as non-trylock entries have added their
1745 * own direct dependencies already, so this
1746 * lock is connected to them indirectly:
1748 if (!hlock->trylock)
1749 break;
1751 depth--;
1753 * End of lock-stack?
1755 if (!depth)
1756 break;
1758 * Stop the search if we cross into another context:
1760 if (curr->held_locks[depth].irq_context !=
1761 curr->held_locks[depth-1].irq_context)
1762 break;
1764 return 1;
1765 out_bug:
1766 if (!debug_locks_off_graph_unlock())
1767 return 0;
1769 WARN_ON(1);
1771 return 0;
1774 unsigned long nr_lock_chains;
1775 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1776 int nr_chain_hlocks;
1777 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1779 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1781 return lock_classes + chain_hlocks[chain->base + i];
1785 * Look up a dependency chain. If the key is not present yet then
1786 * add it and return 1 - in this case the new dependency chain is
1787 * validated. If the key is already hashed, return 0.
1788 * (On return with 1 graph_lock is held.)
1790 static inline int lookup_chain_cache(struct task_struct *curr,
1791 struct held_lock *hlock,
1792 u64 chain_key)
1794 struct lock_class *class = hlock_class(hlock);
1795 struct list_head *hash_head = chainhashentry(chain_key);
1796 struct lock_chain *chain;
1797 struct held_lock *hlock_curr, *hlock_next;
1798 int i, j, n, cn;
1800 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1801 return 0;
1803 * We can walk it lock-free, because entries only get added
1804 * to the hash:
1806 list_for_each_entry(chain, hash_head, entry) {
1807 if (chain->chain_key == chain_key) {
1808 cache_hit:
1809 debug_atomic_inc(&chain_lookup_hits);
1810 if (very_verbose(class))
1811 printk("\nhash chain already cached, key: "
1812 "%016Lx tail class: [%p] %s\n",
1813 (unsigned long long)chain_key,
1814 class->key, class->name);
1815 return 0;
1818 if (very_verbose(class))
1819 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1820 (unsigned long long)chain_key, class->key, class->name);
1822 * Allocate a new chain entry from the static array, and add
1823 * it to the hash:
1825 if (!graph_lock())
1826 return 0;
1828 * We have to walk the chain again locked - to avoid duplicates:
1830 list_for_each_entry(chain, hash_head, entry) {
1831 if (chain->chain_key == chain_key) {
1832 graph_unlock();
1833 goto cache_hit;
1836 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1837 if (!debug_locks_off_graph_unlock())
1838 return 0;
1840 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1841 printk("turning off the locking correctness validator.\n");
1842 dump_stack();
1843 return 0;
1845 chain = lock_chains + nr_lock_chains++;
1846 chain->chain_key = chain_key;
1847 chain->irq_context = hlock->irq_context;
1848 /* Find the first held_lock of current chain */
1849 hlock_next = hlock;
1850 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1851 hlock_curr = curr->held_locks + i;
1852 if (hlock_curr->irq_context != hlock_next->irq_context)
1853 break;
1854 hlock_next = hlock;
1856 i++;
1857 chain->depth = curr->lockdep_depth + 1 - i;
1858 cn = nr_chain_hlocks;
1859 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1860 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1861 if (n == cn)
1862 break;
1863 cn = n;
1865 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1866 chain->base = cn;
1867 for (j = 0; j < chain->depth - 1; j++, i++) {
1868 int lock_id = curr->held_locks[i].class_idx - 1;
1869 chain_hlocks[chain->base + j] = lock_id;
1871 chain_hlocks[chain->base + j] = class - lock_classes;
1873 list_add_tail_rcu(&chain->entry, hash_head);
1874 debug_atomic_inc(&chain_lookup_misses);
1875 inc_chains();
1877 return 1;
1880 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1881 struct held_lock *hlock, int chain_head, u64 chain_key)
1884 * Trylock needs to maintain the stack of held locks, but it
1885 * does not add new dependencies, because trylock can be done
1886 * in any order.
1888 * We look up the chain_key and do the O(N^2) check and update of
1889 * the dependencies only if this is a new dependency chain.
1890 * (If lookup_chain_cache() returns with 1 it acquires
1891 * graph_lock for us)
1893 if (!hlock->trylock && (hlock->check == 2) &&
1894 lookup_chain_cache(curr, hlock, chain_key)) {
1896 * Check whether last held lock:
1898 * - is irq-safe, if this lock is irq-unsafe
1899 * - is softirq-safe, if this lock is hardirq-unsafe
1901 * And check whether the new lock's dependency graph
1902 * could lead back to the previous lock.
1904 * any of these scenarios could lead to a deadlock. If
1905 * All validations
1907 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1909 if (!ret)
1910 return 0;
1912 * Mark recursive read, as we jump over it when
1913 * building dependencies (just like we jump over
1914 * trylock entries):
1916 if (ret == 2)
1917 hlock->read = 2;
1919 * Add dependency only if this lock is not the head
1920 * of the chain, and if it's not a secondary read-lock:
1922 if (!chain_head && ret != 2)
1923 if (!check_prevs_add(curr, hlock))
1924 return 0;
1925 graph_unlock();
1926 } else
1927 /* after lookup_chain_cache(): */
1928 if (unlikely(!debug_locks))
1929 return 0;
1931 return 1;
1933 #else
1934 static inline int validate_chain(struct task_struct *curr,
1935 struct lockdep_map *lock, struct held_lock *hlock,
1936 int chain_head, u64 chain_key)
1938 return 1;
1940 #endif
1943 * We are building curr_chain_key incrementally, so double-check
1944 * it from scratch, to make sure that it's done correctly:
1946 static void check_chain_key(struct task_struct *curr)
1948 #ifdef CONFIG_DEBUG_LOCKDEP
1949 struct held_lock *hlock, *prev_hlock = NULL;
1950 unsigned int i, id;
1951 u64 chain_key = 0;
1953 for (i = 0; i < curr->lockdep_depth; i++) {
1954 hlock = curr->held_locks + i;
1955 if (chain_key != hlock->prev_chain_key) {
1956 debug_locks_off();
1957 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1958 curr->lockdep_depth, i,
1959 (unsigned long long)chain_key,
1960 (unsigned long long)hlock->prev_chain_key);
1961 return;
1963 id = hlock->class_idx - 1;
1964 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1965 return;
1967 if (prev_hlock && (prev_hlock->irq_context !=
1968 hlock->irq_context))
1969 chain_key = 0;
1970 chain_key = iterate_chain_key(chain_key, id);
1971 prev_hlock = hlock;
1973 if (chain_key != curr->curr_chain_key) {
1974 debug_locks_off();
1975 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1976 curr->lockdep_depth, i,
1977 (unsigned long long)chain_key,
1978 (unsigned long long)curr->curr_chain_key);
1980 #endif
1983 static int
1984 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1985 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1987 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1988 return 0;
1990 printk("\n=================================\n");
1991 printk( "[ INFO: inconsistent lock state ]\n");
1992 print_kernel_version();
1993 printk( "---------------------------------\n");
1995 printk("inconsistent {%s} -> {%s} usage.\n",
1996 usage_str[prev_bit], usage_str[new_bit]);
1998 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1999 curr->comm, task_pid_nr(curr),
2000 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2001 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2002 trace_hardirqs_enabled(curr),
2003 trace_softirqs_enabled(curr));
2004 print_lock(this);
2006 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
2007 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2009 print_irqtrace_events(curr);
2010 printk("\nother info that might help us debug this:\n");
2011 lockdep_print_held_locks(curr);
2013 printk("\nstack backtrace:\n");
2014 dump_stack();
2016 return 0;
2020 * Print out an error if an invalid bit is set:
2022 static inline int
2023 valid_state(struct task_struct *curr, struct held_lock *this,
2024 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2026 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2027 return print_usage_bug(curr, this, bad_bit, new_bit);
2028 return 1;
2031 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2032 enum lock_usage_bit new_bit);
2034 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2037 * print irq inversion bug:
2039 static int
2040 print_irq_inversion_bug(struct task_struct *curr,
2041 struct lock_list *root, struct lock_list *other,
2042 struct held_lock *this, int forwards,
2043 const char *irqclass)
2045 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2046 return 0;
2048 printk("\n=========================================================\n");
2049 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2050 print_kernel_version();
2051 printk( "---------------------------------------------------------\n");
2052 printk("%s/%d just changed the state of lock:\n",
2053 curr->comm, task_pid_nr(curr));
2054 print_lock(this);
2055 if (forwards)
2056 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2057 else
2058 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2059 print_lock_name(other->class);
2060 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2062 printk("\nother info that might help us debug this:\n");
2063 lockdep_print_held_locks(curr);
2065 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2066 if (!save_trace(&root->trace))
2067 return 0;
2068 print_shortest_lock_dependencies(other, root);
2070 printk("\nstack backtrace:\n");
2071 dump_stack();
2073 return 0;
2077 * Prove that in the forwards-direction subgraph starting at <this>
2078 * there is no lock matching <mask>:
2080 static int
2081 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2082 enum lock_usage_bit bit, const char *irqclass)
2084 int ret;
2085 struct lock_list root;
2086 struct lock_list *uninitialized_var(target_entry);
2088 root.parent = NULL;
2089 root.class = hlock_class(this);
2090 ret = find_usage_forwards(&root, bit, &target_entry);
2091 if (ret < 0)
2092 return print_bfs_bug(ret);
2093 if (ret == 1)
2094 return ret;
2096 return print_irq_inversion_bug(curr, &root, target_entry,
2097 this, 1, irqclass);
2101 * Prove that in the backwards-direction subgraph starting at <this>
2102 * there is no lock matching <mask>:
2104 static int
2105 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2106 enum lock_usage_bit bit, const char *irqclass)
2108 int ret;
2109 struct lock_list root;
2110 struct lock_list *uninitialized_var(target_entry);
2112 root.parent = NULL;
2113 root.class = hlock_class(this);
2114 ret = find_usage_backwards(&root, bit, &target_entry);
2115 if (ret < 0)
2116 return print_bfs_bug(ret);
2117 if (ret == 1)
2118 return ret;
2120 return print_irq_inversion_bug(curr, &root, target_entry,
2121 this, 1, irqclass);
2124 void print_irqtrace_events(struct task_struct *curr)
2126 printk("irq event stamp: %u\n", curr->irq_events);
2127 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2128 print_ip_sym(curr->hardirq_enable_ip);
2129 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2130 print_ip_sym(curr->hardirq_disable_ip);
2131 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2132 print_ip_sym(curr->softirq_enable_ip);
2133 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2134 print_ip_sym(curr->softirq_disable_ip);
2137 static int HARDIRQ_verbose(struct lock_class *class)
2139 #if HARDIRQ_VERBOSE
2140 return class_filter(class);
2141 #endif
2142 return 0;
2145 static int SOFTIRQ_verbose(struct lock_class *class)
2147 #if SOFTIRQ_VERBOSE
2148 return class_filter(class);
2149 #endif
2150 return 0;
2153 static int RECLAIM_FS_verbose(struct lock_class *class)
2155 #if RECLAIM_VERBOSE
2156 return class_filter(class);
2157 #endif
2158 return 0;
2161 #define STRICT_READ_CHECKS 1
2163 static int (*state_verbose_f[])(struct lock_class *class) = {
2164 #define LOCKDEP_STATE(__STATE) \
2165 __STATE##_verbose,
2166 #include "lockdep_states.h"
2167 #undef LOCKDEP_STATE
2170 static inline int state_verbose(enum lock_usage_bit bit,
2171 struct lock_class *class)
2173 return state_verbose_f[bit >> 2](class);
2176 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2177 enum lock_usage_bit bit, const char *name);
2179 static int
2180 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2181 enum lock_usage_bit new_bit)
2183 int excl_bit = exclusive_bit(new_bit);
2184 int read = new_bit & 1;
2185 int dir = new_bit & 2;
2188 * mark USED_IN has to look forwards -- to ensure no dependency
2189 * has ENABLED state, which would allow recursion deadlocks.
2191 * mark ENABLED has to look backwards -- to ensure no dependee
2192 * has USED_IN state, which, again, would allow recursion deadlocks.
2194 check_usage_f usage = dir ?
2195 check_usage_backwards : check_usage_forwards;
2198 * Validate that this particular lock does not have conflicting
2199 * usage states.
2201 if (!valid_state(curr, this, new_bit, excl_bit))
2202 return 0;
2205 * Validate that the lock dependencies don't have conflicting usage
2206 * states.
2208 if ((!read || !dir || STRICT_READ_CHECKS) &&
2209 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2210 return 0;
2213 * Check for read in write conflicts
2215 if (!read) {
2216 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2217 return 0;
2219 if (STRICT_READ_CHECKS &&
2220 !usage(curr, this, excl_bit + 1,
2221 state_name(new_bit + 1)))
2222 return 0;
2225 if (state_verbose(new_bit, hlock_class(this)))
2226 return 2;
2228 return 1;
2231 enum mark_type {
2232 #define LOCKDEP_STATE(__STATE) __STATE,
2233 #include "lockdep_states.h"
2234 #undef LOCKDEP_STATE
2238 * Mark all held locks with a usage bit:
2240 static int
2241 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2243 enum lock_usage_bit usage_bit;
2244 struct held_lock *hlock;
2245 int i;
2247 for (i = 0; i < curr->lockdep_depth; i++) {
2248 hlock = curr->held_locks + i;
2250 usage_bit = 2 + (mark << 2); /* ENABLED */
2251 if (hlock->read)
2252 usage_bit += 1; /* READ */
2254 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2256 if (!mark_lock(curr, hlock, usage_bit))
2257 return 0;
2260 return 1;
2264 * Debugging helper: via this flag we know that we are in
2265 * 'early bootup code', and will warn about any invalid irqs-on event:
2267 static int early_boot_irqs_enabled;
2269 void early_boot_irqs_off(void)
2271 early_boot_irqs_enabled = 0;
2274 void early_boot_irqs_on(void)
2276 early_boot_irqs_enabled = 1;
2280 * Hardirqs will be enabled:
2282 void trace_hardirqs_on_caller(unsigned long ip)
2284 struct task_struct *curr = current;
2286 time_hardirqs_on(CALLER_ADDR0, ip);
2288 if (unlikely(!debug_locks || current->lockdep_recursion))
2289 return;
2291 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2292 return;
2294 if (unlikely(curr->hardirqs_enabled)) {
2295 debug_atomic_inc(&redundant_hardirqs_on);
2296 return;
2298 /* we'll do an OFF -> ON transition: */
2299 curr->hardirqs_enabled = 1;
2301 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2302 return;
2303 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2304 return;
2306 * We are going to turn hardirqs on, so set the
2307 * usage bit for all held locks:
2309 if (!mark_held_locks(curr, HARDIRQ))
2310 return;
2312 * If we have softirqs enabled, then set the usage
2313 * bit for all held locks. (disabled hardirqs prevented
2314 * this bit from being set before)
2316 if (curr->softirqs_enabled)
2317 if (!mark_held_locks(curr, SOFTIRQ))
2318 return;
2320 curr->hardirq_enable_ip = ip;
2321 curr->hardirq_enable_event = ++curr->irq_events;
2322 debug_atomic_inc(&hardirqs_on_events);
2324 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2326 void trace_hardirqs_on(void)
2328 trace_hardirqs_on_caller(CALLER_ADDR0);
2330 EXPORT_SYMBOL(trace_hardirqs_on);
2333 * Hardirqs were disabled:
2335 void trace_hardirqs_off_caller(unsigned long ip)
2337 struct task_struct *curr = current;
2339 time_hardirqs_off(CALLER_ADDR0, ip);
2341 if (unlikely(!debug_locks || current->lockdep_recursion))
2342 return;
2344 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2345 return;
2347 if (curr->hardirqs_enabled) {
2349 * We have done an ON -> OFF transition:
2351 curr->hardirqs_enabled = 0;
2352 curr->hardirq_disable_ip = ip;
2353 curr->hardirq_disable_event = ++curr->irq_events;
2354 debug_atomic_inc(&hardirqs_off_events);
2355 } else
2356 debug_atomic_inc(&redundant_hardirqs_off);
2358 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2360 void trace_hardirqs_off(void)
2362 trace_hardirqs_off_caller(CALLER_ADDR0);
2364 EXPORT_SYMBOL(trace_hardirqs_off);
2367 * Softirqs will be enabled:
2369 void trace_softirqs_on(unsigned long ip)
2371 struct task_struct *curr = current;
2373 if (unlikely(!debug_locks))
2374 return;
2376 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2377 return;
2379 if (curr->softirqs_enabled) {
2380 debug_atomic_inc(&redundant_softirqs_on);
2381 return;
2385 * We'll do an OFF -> ON transition:
2387 curr->softirqs_enabled = 1;
2388 curr->softirq_enable_ip = ip;
2389 curr->softirq_enable_event = ++curr->irq_events;
2390 debug_atomic_inc(&softirqs_on_events);
2392 * We are going to turn softirqs on, so set the
2393 * usage bit for all held locks, if hardirqs are
2394 * enabled too:
2396 if (curr->hardirqs_enabled)
2397 mark_held_locks(curr, SOFTIRQ);
2401 * Softirqs were disabled:
2403 void trace_softirqs_off(unsigned long ip)
2405 struct task_struct *curr = current;
2407 if (unlikely(!debug_locks))
2408 return;
2410 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2411 return;
2413 if (curr->softirqs_enabled) {
2415 * We have done an ON -> OFF transition:
2417 curr->softirqs_enabled = 0;
2418 curr->softirq_disable_ip = ip;
2419 curr->softirq_disable_event = ++curr->irq_events;
2420 debug_atomic_inc(&softirqs_off_events);
2421 DEBUG_LOCKS_WARN_ON(!softirq_count());
2422 } else
2423 debug_atomic_inc(&redundant_softirqs_off);
2426 static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2428 struct task_struct *curr = current;
2430 if (unlikely(!debug_locks))
2431 return;
2433 /* no reclaim without waiting on it */
2434 if (!(gfp_mask & __GFP_WAIT))
2435 return;
2437 /* this guy won't enter reclaim */
2438 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2439 return;
2441 /* We're only interested __GFP_FS allocations for now */
2442 if (!(gfp_mask & __GFP_FS))
2443 return;
2445 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2446 return;
2448 mark_held_locks(curr, RECLAIM_FS);
2451 static void check_flags(unsigned long flags);
2453 void lockdep_trace_alloc(gfp_t gfp_mask)
2455 unsigned long flags;
2457 if (unlikely(current->lockdep_recursion))
2458 return;
2460 raw_local_irq_save(flags);
2461 check_flags(flags);
2462 current->lockdep_recursion = 1;
2463 __lockdep_trace_alloc(gfp_mask, flags);
2464 current->lockdep_recursion = 0;
2465 raw_local_irq_restore(flags);
2468 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2471 * If non-trylock use in a hardirq or softirq context, then
2472 * mark the lock as used in these contexts:
2474 if (!hlock->trylock) {
2475 if (hlock->read) {
2476 if (curr->hardirq_context)
2477 if (!mark_lock(curr, hlock,
2478 LOCK_USED_IN_HARDIRQ_READ))
2479 return 0;
2480 if (curr->softirq_context)
2481 if (!mark_lock(curr, hlock,
2482 LOCK_USED_IN_SOFTIRQ_READ))
2483 return 0;
2484 } else {
2485 if (curr->hardirq_context)
2486 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2487 return 0;
2488 if (curr->softirq_context)
2489 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2490 return 0;
2493 if (!hlock->hardirqs_off) {
2494 if (hlock->read) {
2495 if (!mark_lock(curr, hlock,
2496 LOCK_ENABLED_HARDIRQ_READ))
2497 return 0;
2498 if (curr->softirqs_enabled)
2499 if (!mark_lock(curr, hlock,
2500 LOCK_ENABLED_SOFTIRQ_READ))
2501 return 0;
2502 } else {
2503 if (!mark_lock(curr, hlock,
2504 LOCK_ENABLED_HARDIRQ))
2505 return 0;
2506 if (curr->softirqs_enabled)
2507 if (!mark_lock(curr, hlock,
2508 LOCK_ENABLED_SOFTIRQ))
2509 return 0;
2514 * We reuse the irq context infrastructure more broadly as a general
2515 * context checking code. This tests GFP_FS recursion (a lock taken
2516 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2517 * allocation).
2519 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2520 if (hlock->read) {
2521 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2522 return 0;
2523 } else {
2524 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2525 return 0;
2529 return 1;
2532 static int separate_irq_context(struct task_struct *curr,
2533 struct held_lock *hlock)
2535 unsigned int depth = curr->lockdep_depth;
2538 * Keep track of points where we cross into an interrupt context:
2540 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2541 curr->softirq_context;
2542 if (depth) {
2543 struct held_lock *prev_hlock;
2545 prev_hlock = curr->held_locks + depth-1;
2547 * If we cross into another context, reset the
2548 * hash key (this also prevents the checking and the
2549 * adding of the dependency to 'prev'):
2551 if (prev_hlock->irq_context != hlock->irq_context)
2552 return 1;
2554 return 0;
2557 #else
2559 static inline
2560 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2561 enum lock_usage_bit new_bit)
2563 WARN_ON(1);
2564 return 1;
2567 static inline int mark_irqflags(struct task_struct *curr,
2568 struct held_lock *hlock)
2570 return 1;
2573 static inline int separate_irq_context(struct task_struct *curr,
2574 struct held_lock *hlock)
2576 return 0;
2579 void lockdep_trace_alloc(gfp_t gfp_mask)
2583 #endif
2586 * Mark a lock with a usage bit, and validate the state transition:
2588 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2589 enum lock_usage_bit new_bit)
2591 unsigned int new_mask = 1 << new_bit, ret = 1;
2594 * If already set then do not dirty the cacheline,
2595 * nor do any checks:
2597 if (likely(hlock_class(this)->usage_mask & new_mask))
2598 return 1;
2600 if (!graph_lock())
2601 return 0;
2603 * Make sure we didnt race:
2605 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2606 graph_unlock();
2607 return 1;
2610 hlock_class(this)->usage_mask |= new_mask;
2612 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2613 return 0;
2615 switch (new_bit) {
2616 #define LOCKDEP_STATE(__STATE) \
2617 case LOCK_USED_IN_##__STATE: \
2618 case LOCK_USED_IN_##__STATE##_READ: \
2619 case LOCK_ENABLED_##__STATE: \
2620 case LOCK_ENABLED_##__STATE##_READ:
2621 #include "lockdep_states.h"
2622 #undef LOCKDEP_STATE
2623 ret = mark_lock_irq(curr, this, new_bit);
2624 if (!ret)
2625 return 0;
2626 break;
2627 case LOCK_USED:
2628 debug_atomic_dec(&nr_unused_locks);
2629 break;
2630 default:
2631 if (!debug_locks_off_graph_unlock())
2632 return 0;
2633 WARN_ON(1);
2634 return 0;
2637 graph_unlock();
2640 * We must printk outside of the graph_lock:
2642 if (ret == 2) {
2643 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2644 print_lock(this);
2645 print_irqtrace_events(curr);
2646 dump_stack();
2649 return ret;
2653 * Initialize a lock instance's lock-class mapping info:
2655 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2656 struct lock_class_key *key, int subclass)
2658 lock->class_cache = NULL;
2659 #ifdef CONFIG_LOCK_STAT
2660 lock->cpu = raw_smp_processor_id();
2661 #endif
2663 if (DEBUG_LOCKS_WARN_ON(!name)) {
2664 lock->name = "NULL";
2665 return;
2668 lock->name = name;
2670 if (DEBUG_LOCKS_WARN_ON(!key))
2671 return;
2673 * Sanity check, the lock-class key must be persistent:
2675 if (!static_obj(key)) {
2676 printk("BUG: key %p not in .data!\n", key);
2677 DEBUG_LOCKS_WARN_ON(1);
2678 return;
2680 lock->key = key;
2682 if (unlikely(!debug_locks))
2683 return;
2685 if (subclass)
2686 register_lock_class(lock, subclass, 1);
2688 EXPORT_SYMBOL_GPL(lockdep_init_map);
2691 * This gets called for every mutex_lock*()/spin_lock*() operation.
2692 * We maintain the dependency maps and validate the locking attempt:
2694 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2695 int trylock, int read, int check, int hardirqs_off,
2696 struct lockdep_map *nest_lock, unsigned long ip)
2698 struct task_struct *curr = current;
2699 struct lock_class *class = NULL;
2700 struct held_lock *hlock;
2701 unsigned int depth, id;
2702 int chain_head = 0;
2703 u64 chain_key;
2705 if (!prove_locking)
2706 check = 1;
2708 if (unlikely(!debug_locks))
2709 return 0;
2711 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2712 return 0;
2714 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2715 debug_locks_off();
2716 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2717 printk("turning off the locking correctness validator.\n");
2718 dump_stack();
2719 return 0;
2722 if (!subclass)
2723 class = lock->class_cache;
2725 * Not cached yet or subclass?
2727 if (unlikely(!class)) {
2728 class = register_lock_class(lock, subclass, 0);
2729 if (!class)
2730 return 0;
2732 debug_atomic_inc((atomic_t *)&class->ops);
2733 if (very_verbose(class)) {
2734 printk("\nacquire class [%p] %s", class->key, class->name);
2735 if (class->name_version > 1)
2736 printk("#%d", class->name_version);
2737 printk("\n");
2738 dump_stack();
2742 * Add the lock to the list of currently held locks.
2743 * (we dont increase the depth just yet, up until the
2744 * dependency checks are done)
2746 depth = curr->lockdep_depth;
2747 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2748 return 0;
2750 hlock = curr->held_locks + depth;
2751 if (DEBUG_LOCKS_WARN_ON(!class))
2752 return 0;
2753 hlock->class_idx = class - lock_classes + 1;
2754 hlock->acquire_ip = ip;
2755 hlock->instance = lock;
2756 hlock->nest_lock = nest_lock;
2757 hlock->trylock = trylock;
2758 hlock->read = read;
2759 hlock->check = check;
2760 hlock->hardirqs_off = !!hardirqs_off;
2761 #ifdef CONFIG_LOCK_STAT
2762 hlock->waittime_stamp = 0;
2763 hlock->holdtime_stamp = sched_clock();
2764 #endif
2766 if (check == 2 && !mark_irqflags(curr, hlock))
2767 return 0;
2769 /* mark it as used: */
2770 if (!mark_lock(curr, hlock, LOCK_USED))
2771 return 0;
2774 * Calculate the chain hash: it's the combined hash of all the
2775 * lock keys along the dependency chain. We save the hash value
2776 * at every step so that we can get the current hash easily
2777 * after unlock. The chain hash is then used to cache dependency
2778 * results.
2780 * The 'key ID' is what is the most compact key value to drive
2781 * the hash, not class->key.
2783 id = class - lock_classes;
2784 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2785 return 0;
2787 chain_key = curr->curr_chain_key;
2788 if (!depth) {
2789 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2790 return 0;
2791 chain_head = 1;
2794 hlock->prev_chain_key = chain_key;
2795 if (separate_irq_context(curr, hlock)) {
2796 chain_key = 0;
2797 chain_head = 1;
2799 chain_key = iterate_chain_key(chain_key, id);
2801 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2802 return 0;
2804 curr->curr_chain_key = chain_key;
2805 curr->lockdep_depth++;
2806 check_chain_key(curr);
2807 #ifdef CONFIG_DEBUG_LOCKDEP
2808 if (unlikely(!debug_locks))
2809 return 0;
2810 #endif
2811 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2812 debug_locks_off();
2813 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2814 printk("turning off the locking correctness validator.\n");
2815 dump_stack();
2816 return 0;
2819 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2820 max_lockdep_depth = curr->lockdep_depth;
2822 return 1;
2825 static int
2826 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2827 unsigned long ip)
2829 if (!debug_locks_off())
2830 return 0;
2831 if (debug_locks_silent)
2832 return 0;
2834 printk("\n=====================================\n");
2835 printk( "[ BUG: bad unlock balance detected! ]\n");
2836 printk( "-------------------------------------\n");
2837 printk("%s/%d is trying to release lock (",
2838 curr->comm, task_pid_nr(curr));
2839 print_lockdep_cache(lock);
2840 printk(") at:\n");
2841 print_ip_sym(ip);
2842 printk("but there are no more locks to release!\n");
2843 printk("\nother info that might help us debug this:\n");
2844 lockdep_print_held_locks(curr);
2846 printk("\nstack backtrace:\n");
2847 dump_stack();
2849 return 0;
2853 * Common debugging checks for both nested and non-nested unlock:
2855 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2856 unsigned long ip)
2858 if (unlikely(!debug_locks))
2859 return 0;
2860 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2861 return 0;
2863 if (curr->lockdep_depth <= 0)
2864 return print_unlock_inbalance_bug(curr, lock, ip);
2866 return 1;
2869 static int
2870 __lock_set_class(struct lockdep_map *lock, const char *name,
2871 struct lock_class_key *key, unsigned int subclass,
2872 unsigned long ip)
2874 struct task_struct *curr = current;
2875 struct held_lock *hlock, *prev_hlock;
2876 struct lock_class *class;
2877 unsigned int depth;
2878 int i;
2880 depth = curr->lockdep_depth;
2881 if (DEBUG_LOCKS_WARN_ON(!depth))
2882 return 0;
2884 prev_hlock = NULL;
2885 for (i = depth-1; i >= 0; i--) {
2886 hlock = curr->held_locks + i;
2888 * We must not cross into another context:
2890 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2891 break;
2892 if (hlock->instance == lock)
2893 goto found_it;
2894 prev_hlock = hlock;
2896 return print_unlock_inbalance_bug(curr, lock, ip);
2898 found_it:
2899 lockdep_init_map(lock, name, key, 0);
2900 class = register_lock_class(lock, subclass, 0);
2901 hlock->class_idx = class - lock_classes + 1;
2903 curr->lockdep_depth = i;
2904 curr->curr_chain_key = hlock->prev_chain_key;
2906 for (; i < depth; i++) {
2907 hlock = curr->held_locks + i;
2908 if (!__lock_acquire(hlock->instance,
2909 hlock_class(hlock)->subclass, hlock->trylock,
2910 hlock->read, hlock->check, hlock->hardirqs_off,
2911 hlock->nest_lock, hlock->acquire_ip))
2912 return 0;
2915 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2916 return 0;
2917 return 1;
2921 * Remove the lock to the list of currently held locks in a
2922 * potentially non-nested (out of order) manner. This is a
2923 * relatively rare operation, as all the unlock APIs default
2924 * to nested mode (which uses lock_release()):
2926 static int
2927 lock_release_non_nested(struct task_struct *curr,
2928 struct lockdep_map *lock, unsigned long ip)
2930 struct held_lock *hlock, *prev_hlock;
2931 unsigned int depth;
2932 int i;
2935 * Check whether the lock exists in the current stack
2936 * of held locks:
2938 depth = curr->lockdep_depth;
2939 if (DEBUG_LOCKS_WARN_ON(!depth))
2940 return 0;
2942 prev_hlock = NULL;
2943 for (i = depth-1; i >= 0; i--) {
2944 hlock = curr->held_locks + i;
2946 * We must not cross into another context:
2948 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2949 break;
2950 if (hlock->instance == lock)
2951 goto found_it;
2952 prev_hlock = hlock;
2954 return print_unlock_inbalance_bug(curr, lock, ip);
2956 found_it:
2957 lock_release_holdtime(hlock);
2960 * We have the right lock to unlock, 'hlock' points to it.
2961 * Now we remove it from the stack, and add back the other
2962 * entries (if any), recalculating the hash along the way:
2964 curr->lockdep_depth = i;
2965 curr->curr_chain_key = hlock->prev_chain_key;
2967 for (i++; i < depth; i++) {
2968 hlock = curr->held_locks + i;
2969 if (!__lock_acquire(hlock->instance,
2970 hlock_class(hlock)->subclass, hlock->trylock,
2971 hlock->read, hlock->check, hlock->hardirqs_off,
2972 hlock->nest_lock, hlock->acquire_ip))
2973 return 0;
2976 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2977 return 0;
2978 return 1;
2982 * Remove the lock to the list of currently held locks - this gets
2983 * called on mutex_unlock()/spin_unlock*() (or on a failed
2984 * mutex_lock_interruptible()). This is done for unlocks that nest
2985 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2987 static int lock_release_nested(struct task_struct *curr,
2988 struct lockdep_map *lock, unsigned long ip)
2990 struct held_lock *hlock;
2991 unsigned int depth;
2994 * Pop off the top of the lock stack:
2996 depth = curr->lockdep_depth - 1;
2997 hlock = curr->held_locks + depth;
3000 * Is the unlock non-nested:
3002 if (hlock->instance != lock)
3003 return lock_release_non_nested(curr, lock, ip);
3004 curr->lockdep_depth--;
3006 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3007 return 0;
3009 curr->curr_chain_key = hlock->prev_chain_key;
3011 lock_release_holdtime(hlock);
3013 #ifdef CONFIG_DEBUG_LOCKDEP
3014 hlock->prev_chain_key = 0;
3015 hlock->class_idx = 0;
3016 hlock->acquire_ip = 0;
3017 hlock->irq_context = 0;
3018 #endif
3019 return 1;
3023 * Remove the lock to the list of currently held locks - this gets
3024 * called on mutex_unlock()/spin_unlock*() (or on a failed
3025 * mutex_lock_interruptible()). This is done for unlocks that nest
3026 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3028 static void
3029 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3031 struct task_struct *curr = current;
3033 if (!check_unlock(curr, lock, ip))
3034 return;
3036 if (nested) {
3037 if (!lock_release_nested(curr, lock, ip))
3038 return;
3039 } else {
3040 if (!lock_release_non_nested(curr, lock, ip))
3041 return;
3044 check_chain_key(curr);
3048 * Check whether we follow the irq-flags state precisely:
3050 static void check_flags(unsigned long flags)
3052 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3053 defined(CONFIG_TRACE_IRQFLAGS)
3054 if (!debug_locks)
3055 return;
3057 if (irqs_disabled_flags(flags)) {
3058 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3059 printk("possible reason: unannotated irqs-off.\n");
3061 } else {
3062 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3063 printk("possible reason: unannotated irqs-on.\n");
3068 * We dont accurately track softirq state in e.g.
3069 * hardirq contexts (such as on 4KSTACKS), so only
3070 * check if not in hardirq contexts:
3072 if (!hardirq_count()) {
3073 if (softirq_count())
3074 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3075 else
3076 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3079 if (!debug_locks)
3080 print_irqtrace_events(current);
3081 #endif
3084 void lock_set_class(struct lockdep_map *lock, const char *name,
3085 struct lock_class_key *key, unsigned int subclass,
3086 unsigned long ip)
3088 unsigned long flags;
3090 if (unlikely(current->lockdep_recursion))
3091 return;
3093 raw_local_irq_save(flags);
3094 current->lockdep_recursion = 1;
3095 check_flags(flags);
3096 if (__lock_set_class(lock, name, key, subclass, ip))
3097 check_chain_key(current);
3098 current->lockdep_recursion = 0;
3099 raw_local_irq_restore(flags);
3101 EXPORT_SYMBOL_GPL(lock_set_class);
3104 * We are not always called with irqs disabled - do that here,
3105 * and also avoid lockdep recursion:
3107 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3108 int trylock, int read, int check,
3109 struct lockdep_map *nest_lock, unsigned long ip)
3111 unsigned long flags;
3113 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3115 if (unlikely(current->lockdep_recursion))
3116 return;
3118 raw_local_irq_save(flags);
3119 check_flags(flags);
3121 current->lockdep_recursion = 1;
3122 __lock_acquire(lock, subclass, trylock, read, check,
3123 irqs_disabled_flags(flags), nest_lock, ip);
3124 current->lockdep_recursion = 0;
3125 raw_local_irq_restore(flags);
3127 EXPORT_SYMBOL_GPL(lock_acquire);
3129 void lock_release(struct lockdep_map *lock, int nested,
3130 unsigned long ip)
3132 unsigned long flags;
3134 trace_lock_release(lock, nested, ip);
3136 if (unlikely(current->lockdep_recursion))
3137 return;
3139 raw_local_irq_save(flags);
3140 check_flags(flags);
3141 current->lockdep_recursion = 1;
3142 __lock_release(lock, nested, ip);
3143 current->lockdep_recursion = 0;
3144 raw_local_irq_restore(flags);
3146 EXPORT_SYMBOL_GPL(lock_release);
3148 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3150 current->lockdep_reclaim_gfp = gfp_mask;
3153 void lockdep_clear_current_reclaim_state(void)
3155 current->lockdep_reclaim_gfp = 0;
3158 #ifdef CONFIG_LOCK_STAT
3159 static int
3160 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3161 unsigned long ip)
3163 if (!debug_locks_off())
3164 return 0;
3165 if (debug_locks_silent)
3166 return 0;
3168 printk("\n=================================\n");
3169 printk( "[ BUG: bad contention detected! ]\n");
3170 printk( "---------------------------------\n");
3171 printk("%s/%d is trying to contend lock (",
3172 curr->comm, task_pid_nr(curr));
3173 print_lockdep_cache(lock);
3174 printk(") at:\n");
3175 print_ip_sym(ip);
3176 printk("but there are no locks held!\n");
3177 printk("\nother info that might help us debug this:\n");
3178 lockdep_print_held_locks(curr);
3180 printk("\nstack backtrace:\n");
3181 dump_stack();
3183 return 0;
3186 static void
3187 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3189 struct task_struct *curr = current;
3190 struct held_lock *hlock, *prev_hlock;
3191 struct lock_class_stats *stats;
3192 unsigned int depth;
3193 int i, contention_point, contending_point;
3195 depth = curr->lockdep_depth;
3196 if (DEBUG_LOCKS_WARN_ON(!depth))
3197 return;
3199 prev_hlock = NULL;
3200 for (i = depth-1; i >= 0; i--) {
3201 hlock = curr->held_locks + i;
3203 * We must not cross into another context:
3205 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3206 break;
3207 if (hlock->instance == lock)
3208 goto found_it;
3209 prev_hlock = hlock;
3211 print_lock_contention_bug(curr, lock, ip);
3212 return;
3214 found_it:
3215 hlock->waittime_stamp = sched_clock();
3217 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3218 contending_point = lock_point(hlock_class(hlock)->contending_point,
3219 lock->ip);
3221 stats = get_lock_stats(hlock_class(hlock));
3222 if (contention_point < LOCKSTAT_POINTS)
3223 stats->contention_point[contention_point]++;
3224 if (contending_point < LOCKSTAT_POINTS)
3225 stats->contending_point[contending_point]++;
3226 if (lock->cpu != smp_processor_id())
3227 stats->bounces[bounce_contended + !!hlock->read]++;
3228 put_lock_stats(stats);
3231 static void
3232 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3234 struct task_struct *curr = current;
3235 struct held_lock *hlock, *prev_hlock;
3236 struct lock_class_stats *stats;
3237 unsigned int depth;
3238 u64 now;
3239 s64 waittime = 0;
3240 int i, cpu;
3242 depth = curr->lockdep_depth;
3243 if (DEBUG_LOCKS_WARN_ON(!depth))
3244 return;
3246 prev_hlock = NULL;
3247 for (i = depth-1; i >= 0; i--) {
3248 hlock = curr->held_locks + i;
3250 * We must not cross into another context:
3252 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3253 break;
3254 if (hlock->instance == lock)
3255 goto found_it;
3256 prev_hlock = hlock;
3258 print_lock_contention_bug(curr, lock, _RET_IP_);
3259 return;
3261 found_it:
3262 cpu = smp_processor_id();
3263 if (hlock->waittime_stamp) {
3264 now = sched_clock();
3265 waittime = now - hlock->waittime_stamp;
3266 hlock->holdtime_stamp = now;
3269 trace_lock_acquired(lock, ip, waittime);
3271 stats = get_lock_stats(hlock_class(hlock));
3272 if (waittime) {
3273 if (hlock->read)
3274 lock_time_inc(&stats->read_waittime, waittime);
3275 else
3276 lock_time_inc(&stats->write_waittime, waittime);
3278 if (lock->cpu != cpu)
3279 stats->bounces[bounce_acquired + !!hlock->read]++;
3280 put_lock_stats(stats);
3282 lock->cpu = cpu;
3283 lock->ip = ip;
3286 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3288 unsigned long flags;
3290 trace_lock_contended(lock, ip);
3292 if (unlikely(!lock_stat))
3293 return;
3295 if (unlikely(current->lockdep_recursion))
3296 return;
3298 raw_local_irq_save(flags);
3299 check_flags(flags);
3300 current->lockdep_recursion = 1;
3301 __lock_contended(lock, ip);
3302 current->lockdep_recursion = 0;
3303 raw_local_irq_restore(flags);
3305 EXPORT_SYMBOL_GPL(lock_contended);
3307 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3309 unsigned long flags;
3311 if (unlikely(!lock_stat))
3312 return;
3314 if (unlikely(current->lockdep_recursion))
3315 return;
3317 raw_local_irq_save(flags);
3318 check_flags(flags);
3319 current->lockdep_recursion = 1;
3320 __lock_acquired(lock, ip);
3321 current->lockdep_recursion = 0;
3322 raw_local_irq_restore(flags);
3324 EXPORT_SYMBOL_GPL(lock_acquired);
3325 #endif
3328 * Used by the testsuite, sanitize the validator state
3329 * after a simulated failure:
3332 void lockdep_reset(void)
3334 unsigned long flags;
3335 int i;
3337 raw_local_irq_save(flags);
3338 current->curr_chain_key = 0;
3339 current->lockdep_depth = 0;
3340 current->lockdep_recursion = 0;
3341 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3342 nr_hardirq_chains = 0;
3343 nr_softirq_chains = 0;
3344 nr_process_chains = 0;
3345 debug_locks = 1;
3346 for (i = 0; i < CHAINHASH_SIZE; i++)
3347 INIT_LIST_HEAD(chainhash_table + i);
3348 raw_local_irq_restore(flags);
3351 static void zap_class(struct lock_class *class)
3353 int i;
3356 * Remove all dependencies this lock is
3357 * involved in:
3359 for (i = 0; i < nr_list_entries; i++) {
3360 if (list_entries[i].class == class)
3361 list_del_rcu(&list_entries[i].entry);
3364 * Unhash the class and remove it from the all_lock_classes list:
3366 list_del_rcu(&class->hash_entry);
3367 list_del_rcu(&class->lock_entry);
3369 class->key = NULL;
3372 static inline int within(const void *addr, void *start, unsigned long size)
3374 return addr >= start && addr < start + size;
3377 void lockdep_free_key_range(void *start, unsigned long size)
3379 struct lock_class *class, *next;
3380 struct list_head *head;
3381 unsigned long flags;
3382 int i;
3383 int locked;
3385 raw_local_irq_save(flags);
3386 locked = graph_lock();
3389 * Unhash all classes that were created by this module:
3391 for (i = 0; i < CLASSHASH_SIZE; i++) {
3392 head = classhash_table + i;
3393 if (list_empty(head))
3394 continue;
3395 list_for_each_entry_safe(class, next, head, hash_entry) {
3396 if (within(class->key, start, size))
3397 zap_class(class);
3398 else if (within(class->name, start, size))
3399 zap_class(class);
3403 if (locked)
3404 graph_unlock();
3405 raw_local_irq_restore(flags);
3408 void lockdep_reset_lock(struct lockdep_map *lock)
3410 struct lock_class *class, *next;
3411 struct list_head *head;
3412 unsigned long flags;
3413 int i, j;
3414 int locked;
3416 raw_local_irq_save(flags);
3419 * Remove all classes this lock might have:
3421 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3423 * If the class exists we look it up and zap it:
3425 class = look_up_lock_class(lock, j);
3426 if (class)
3427 zap_class(class);
3430 * Debug check: in the end all mapped classes should
3431 * be gone.
3433 locked = graph_lock();
3434 for (i = 0; i < CLASSHASH_SIZE; i++) {
3435 head = classhash_table + i;
3436 if (list_empty(head))
3437 continue;
3438 list_for_each_entry_safe(class, next, head, hash_entry) {
3439 if (unlikely(class == lock->class_cache)) {
3440 if (debug_locks_off_graph_unlock())
3441 WARN_ON(1);
3442 goto out_restore;
3446 if (locked)
3447 graph_unlock();
3449 out_restore:
3450 raw_local_irq_restore(flags);
3453 void lockdep_init(void)
3455 int i;
3458 * Some architectures have their own start_kernel()
3459 * code which calls lockdep_init(), while we also
3460 * call lockdep_init() from the start_kernel() itself,
3461 * and we want to initialize the hashes only once:
3463 if (lockdep_initialized)
3464 return;
3466 for (i = 0; i < CLASSHASH_SIZE; i++)
3467 INIT_LIST_HEAD(classhash_table + i);
3469 for (i = 0; i < CHAINHASH_SIZE; i++)
3470 INIT_LIST_HEAD(chainhash_table + i);
3472 lockdep_initialized = 1;
3475 void __init lockdep_info(void)
3477 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3479 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3480 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3481 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3482 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3483 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3484 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3485 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3487 printk(" memory used by lock dependency info: %lu kB\n",
3488 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3489 sizeof(struct list_head) * CLASSHASH_SIZE +
3490 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3491 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3492 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024
3493 #ifdef CONFIG_PROVE_LOCKING
3494 + sizeof(struct circular_queue) + sizeof(bfs_accessed)
3495 #endif
3498 printk(" per task-struct memory footprint: %lu bytes\n",
3499 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3501 #ifdef CONFIG_DEBUG_LOCKDEP
3502 if (lockdep_init_error) {
3503 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3504 printk("Call stack leading to lockdep invocation was:\n");
3505 print_stack_trace(&lockdep_init_trace, 0);
3507 #endif
3510 static void
3511 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3512 const void *mem_to, struct held_lock *hlock)
3514 if (!debug_locks_off())
3515 return;
3516 if (debug_locks_silent)
3517 return;
3519 printk("\n=========================\n");
3520 printk( "[ BUG: held lock freed! ]\n");
3521 printk( "-------------------------\n");
3522 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3523 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3524 print_lock(hlock);
3525 lockdep_print_held_locks(curr);
3527 printk("\nstack backtrace:\n");
3528 dump_stack();
3531 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3532 const void* lock_from, unsigned long lock_len)
3534 return lock_from + lock_len <= mem_from ||
3535 mem_from + mem_len <= lock_from;
3539 * Called when kernel memory is freed (or unmapped), or if a lock
3540 * is destroyed or reinitialized - this code checks whether there is
3541 * any held lock in the memory range of <from> to <to>:
3543 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3545 struct task_struct *curr = current;
3546 struct held_lock *hlock;
3547 unsigned long flags;
3548 int i;
3550 if (unlikely(!debug_locks))
3551 return;
3553 local_irq_save(flags);
3554 for (i = 0; i < curr->lockdep_depth; i++) {
3555 hlock = curr->held_locks + i;
3557 if (not_in_range(mem_from, mem_len, hlock->instance,
3558 sizeof(*hlock->instance)))
3559 continue;
3561 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3562 break;
3564 local_irq_restore(flags);
3566 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3568 static void print_held_locks_bug(struct task_struct *curr)
3570 if (!debug_locks_off())
3571 return;
3572 if (debug_locks_silent)
3573 return;
3575 printk("\n=====================================\n");
3576 printk( "[ BUG: lock held at task exit time! ]\n");
3577 printk( "-------------------------------------\n");
3578 printk("%s/%d is exiting with locks still held!\n",
3579 curr->comm, task_pid_nr(curr));
3580 lockdep_print_held_locks(curr);
3582 printk("\nstack backtrace:\n");
3583 dump_stack();
3586 void debug_check_no_locks_held(struct task_struct *task)
3588 if (unlikely(task->lockdep_depth > 0))
3589 print_held_locks_bug(task);
3592 void debug_show_all_locks(void)
3594 struct task_struct *g, *p;
3595 int count = 10;
3596 int unlock = 1;
3598 if (unlikely(!debug_locks)) {
3599 printk("INFO: lockdep is turned off.\n");
3600 return;
3602 printk("\nShowing all locks held in the system:\n");
3605 * Here we try to get the tasklist_lock as hard as possible,
3606 * if not successful after 2 seconds we ignore it (but keep
3607 * trying). This is to enable a debug printout even if a
3608 * tasklist_lock-holding task deadlocks or crashes.
3610 retry:
3611 if (!read_trylock(&tasklist_lock)) {
3612 if (count == 10)
3613 printk("hm, tasklist_lock locked, retrying... ");
3614 if (count) {
3615 count--;
3616 printk(" #%d", 10-count);
3617 mdelay(200);
3618 goto retry;
3620 printk(" ignoring it.\n");
3621 unlock = 0;
3622 } else {
3623 if (count != 10)
3624 printk(KERN_CONT " locked it.\n");
3627 do_each_thread(g, p) {
3629 * It's not reliable to print a task's held locks
3630 * if it's not sleeping (or if it's not the current
3631 * task):
3633 if (p->state == TASK_RUNNING && p != current)
3634 continue;
3635 if (p->lockdep_depth)
3636 lockdep_print_held_locks(p);
3637 if (!unlock)
3638 if (read_trylock(&tasklist_lock))
3639 unlock = 1;
3640 } while_each_thread(g, p);
3642 printk("\n");
3643 printk("=============================================\n\n");
3645 if (unlock)
3646 read_unlock(&tasklist_lock);
3648 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3651 * Careful: only use this function if you are sure that
3652 * the task cannot run in parallel!
3654 void __debug_show_held_locks(struct task_struct *task)
3656 if (unlikely(!debug_locks)) {
3657 printk("INFO: lockdep is turned off.\n");
3658 return;
3660 lockdep_print_held_locks(task);
3662 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3664 void debug_show_held_locks(struct task_struct *task)
3666 __debug_show_held_locks(task);
3668 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3670 void lockdep_sys_exit(void)
3672 struct task_struct *curr = current;
3674 if (unlikely(curr->lockdep_depth)) {
3675 if (!debug_locks_off())
3676 return;
3677 printk("\n================================================\n");
3678 printk( "[ BUG: lock held when returning to user space! ]\n");
3679 printk( "------------------------------------------------\n");
3680 printk("%s/%d is leaving the kernel with locks still held!\n",
3681 curr->comm, curr->pid);
3682 lockdep_print_held_locks(curr);