4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
28 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/sched/clock.h>
32 #include <linux/sched/task.h>
33 #include <linux/sched/mm.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39 #include <linux/kallsyms.h>
40 #include <linux/interrupt.h>
41 #include <linux/stacktrace.h>
42 #include <linux/debug_locks.h>
43 #include <linux/irqflags.h>
44 #include <linux/utsname.h>
45 #include <linux/hash.h>
46 #include <linux/ftrace.h>
47 #include <linux/stringify.h>
48 #include <linux/bitops.h>
49 #include <linux/gfp.h>
50 #include <linux/random.h>
51 #include <linux/jhash.h>
52 #include <linux/nmi.h>
53 #include <linux/kprobes.h>
55 #include <asm/sections.h>
57 #include "lockdep_internals.h"
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/lock.h>
62 #ifdef CONFIG_PROVE_LOCKING
63 int prove_locking = 1;
64 module_param(prove_locking, int, 0644);
66 #define prove_locking 0
69 #ifdef CONFIG_LOCK_STAT
71 module_param(lock_stat, int, 0644);
77 * lockdep_lock: protects the lockdep graph, the hashes and the
78 * class/list/hash allocators.
80 * This is one of the rare exceptions where it's justified
81 * to use a raw spinlock - we really dont want the spinlock
82 * code to recurse back into the lockdep code...
84 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
86 static int graph_lock(void)
88 arch_spin_lock(&lockdep_lock);
90 * Make sure that if another CPU detected a bug while
91 * walking the graph we dont change it (while the other
92 * CPU is busy printing out stuff with the graph lock
96 arch_spin_unlock(&lockdep_lock);
99 /* prevent any recursions within lockdep from causing deadlocks */
100 current->lockdep_recursion++;
104 static inline int graph_unlock(void)
106 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
108 * The lockdep graph lock isn't locked while we expect it to
109 * be, we're confused now, bye!
111 return DEBUG_LOCKS_WARN_ON(1);
114 current->lockdep_recursion--;
115 arch_spin_unlock(&lockdep_lock);
120 * Turn lock debugging off and return with 0 if it was off already,
121 * and also release the graph lock:
123 static inline int debug_locks_off_graph_unlock(void)
125 int ret = debug_locks_off();
127 arch_spin_unlock(&lockdep_lock);
132 unsigned long nr_list_entries;
133 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
136 * All data structures here are protected by the global debug_lock.
138 * Mutex key structs only get allocated, once during bootup, and never
139 * get freed - this significantly simplifies the debugging code.
141 unsigned long nr_lock_classes;
142 #ifndef CONFIG_DEBUG_LOCKDEP
145 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
147 static inline struct lock_class *hlock_class(struct held_lock *hlock)
149 if (!hlock->class_idx) {
151 * Someone passed in garbage, we give up.
153 DEBUG_LOCKS_WARN_ON(1);
156 return lock_classes + hlock->class_idx - 1;
159 #ifdef CONFIG_LOCK_STAT
160 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
162 static inline u64 lockstat_clock(void)
164 return local_clock();
167 static int lock_point(unsigned long points[], unsigned long ip)
171 for (i = 0; i < LOCKSTAT_POINTS; i++) {
172 if (points[i] == 0) {
183 static void lock_time_inc(struct lock_time *lt, u64 time)
188 if (time < lt->min || !lt->nr)
195 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
200 if (src->max > dst->max)
203 if (src->min < dst->min || !dst->nr)
206 dst->total += src->total;
210 struct lock_class_stats lock_stats(struct lock_class *class)
212 struct lock_class_stats stats;
215 memset(&stats, 0, sizeof(struct lock_class_stats));
216 for_each_possible_cpu(cpu) {
217 struct lock_class_stats *pcs =
218 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
220 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
221 stats.contention_point[i] += pcs->contention_point[i];
223 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
224 stats.contending_point[i] += pcs->contending_point[i];
226 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
227 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
229 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
230 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
232 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
233 stats.bounces[i] += pcs->bounces[i];
239 void clear_lock_stats(struct lock_class *class)
243 for_each_possible_cpu(cpu) {
244 struct lock_class_stats *cpu_stats =
245 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
247 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
249 memset(class->contention_point, 0, sizeof(class->contention_point));
250 memset(class->contending_point, 0, sizeof(class->contending_point));
253 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
255 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
258 static void lock_release_holdtime(struct held_lock *hlock)
260 struct lock_class_stats *stats;
266 holdtime = lockstat_clock() - hlock->holdtime_stamp;
268 stats = get_lock_stats(hlock_class(hlock));
270 lock_time_inc(&stats->read_holdtime, holdtime);
272 lock_time_inc(&stats->write_holdtime, holdtime);
275 static inline void lock_release_holdtime(struct held_lock *hlock)
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
285 LIST_HEAD(all_lock_classes);
288 * The lockdep classes are in a hash-table as well, for fast lookup:
290 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
292 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
293 #define classhashentry(key) (classhash_table + __classhashfn((key)))
295 static struct hlist_head classhash_table[CLASSHASH_SIZE];
298 * We put the lock dependency chains into a hash-table as well, to cache
301 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
303 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
304 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
306 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
314 static inline u64 iterate_chain_key(u64 key, u32 idx)
316 u32 k0 = key, k1 = key >> 32;
318 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
320 return k0 | (u64)k1 << 32;
323 void lockdep_off(void)
325 current->lockdep_recursion++;
327 EXPORT_SYMBOL(lockdep_off);
329 void lockdep_on(void)
331 current->lockdep_recursion--;
333 EXPORT_SYMBOL(lockdep_on);
336 * Debugging switches:
340 #define VERY_VERBOSE 0
343 # define HARDIRQ_VERBOSE 1
344 # define SOFTIRQ_VERBOSE 1
346 # define HARDIRQ_VERBOSE 0
347 # define SOFTIRQ_VERBOSE 0
350 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
352 * Quick filtering for interesting events:
354 static int class_filter(struct lock_class *class)
358 if (class->name_version == 1 &&
359 !strcmp(class->name, "lockname"))
361 if (class->name_version == 1 &&
362 !strcmp(class->name, "&struct->lockfield"))
365 /* Filter everything else. 1 would be to allow everything else */
370 static int verbose(struct lock_class *class)
373 return class_filter(class);
379 * Stack-trace: tightly packed array of stack backtrace
380 * addresses. Protected by the graph_lock.
382 unsigned long nr_stack_trace_entries;
383 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
385 static void print_lockdep_off(const char *bug_msg)
387 printk(KERN_DEBUG "%s\n", bug_msg);
388 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
389 #ifdef CONFIG_LOCK_STAT
390 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
394 static int save_trace(struct stack_trace *trace)
396 trace->nr_entries = 0;
397 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
398 trace->entries = stack_trace + nr_stack_trace_entries;
402 save_stack_trace(trace);
405 * Some daft arches put -1 at the end to indicate its a full trace.
407 * <rant> this is buggy anyway, since it takes a whole extra entry so a
408 * complete trace that maxes out the entries provided will be reported
409 * as incomplete, friggin useless </rant>
411 if (trace->nr_entries != 0 &&
412 trace->entries[trace->nr_entries-1] == ULONG_MAX)
415 trace->max_entries = trace->nr_entries;
417 nr_stack_trace_entries += trace->nr_entries;
419 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
420 if (!debug_locks_off_graph_unlock())
423 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
432 unsigned int nr_hardirq_chains;
433 unsigned int nr_softirq_chains;
434 unsigned int nr_process_chains;
435 unsigned int max_lockdep_depth;
437 #ifdef CONFIG_DEBUG_LOCKDEP
439 * Various lockdep statistics:
441 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
448 #define __USAGE(__STATE) \
449 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
450 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
451 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
452 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
454 static const char *usage_str[] =
456 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
457 #include "lockdep_states.h"
459 [LOCK_USED] = "INITIAL USE",
462 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
464 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
467 static inline unsigned long lock_flag(enum lock_usage_bit bit)
472 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
476 if (class->usage_mask & lock_flag(bit + 2))
478 if (class->usage_mask & lock_flag(bit)) {
480 if (class->usage_mask & lock_flag(bit + 2))
487 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
491 #define LOCKDEP_STATE(__STATE) \
492 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
494 #include "lockdep_states.h"
500 static void __print_lock_name(struct lock_class *class)
502 char str[KSYM_NAME_LEN];
507 name = __get_key_name(class->key, str);
508 printk(KERN_CONT "%s", name);
510 printk(KERN_CONT "%s", name);
511 if (class->name_version > 1)
512 printk(KERN_CONT "#%d", class->name_version);
514 printk(KERN_CONT "/%d", class->subclass);
518 static void print_lock_name(struct lock_class *class)
520 char usage[LOCK_USAGE_CHARS];
522 get_usage_chars(class, usage);
524 printk(KERN_CONT " (");
525 __print_lock_name(class);
526 printk(KERN_CONT "){%s}", usage);
529 static void print_lockdep_cache(struct lockdep_map *lock)
532 char str[KSYM_NAME_LEN];
536 name = __get_key_name(lock->key->subkeys, str);
538 printk(KERN_CONT "%s", name);
541 static void print_lock(struct held_lock *hlock)
544 * We can be called locklessly through debug_show_all_locks() so be
545 * extra careful, the hlock might have been released and cleared.
547 unsigned int class_idx = hlock->class_idx;
549 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
552 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
553 printk(KERN_CONT "<RELEASED>\n");
557 printk(KERN_CONT "%p", hlock->instance);
558 print_lock_name(lock_classes + class_idx - 1);
559 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
562 static void lockdep_print_held_locks(struct task_struct *p)
564 int i, depth = READ_ONCE(p->lockdep_depth);
567 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
569 printk("%d lock%s held by %s/%d:\n", depth,
570 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
572 * It's not reliable to print a task's held locks if it's not sleeping
573 * and it's not the current task.
575 if (p->state == TASK_RUNNING && p != current)
577 for (i = 0; i < depth; i++) {
579 print_lock(p->held_locks + i);
583 static void print_kernel_ident(void)
585 printk("%s %.*s %s\n", init_utsname()->release,
586 (int)strcspn(init_utsname()->version, " "),
587 init_utsname()->version,
591 static int very_verbose(struct lock_class *class)
594 return class_filter(class);
600 * Is this the address of a static object:
603 static int static_obj(void *obj)
605 unsigned long start = (unsigned long) &_stext,
606 end = (unsigned long) &_end,
607 addr = (unsigned long) obj;
612 if ((addr >= start) && (addr < end))
615 if (arch_is_kernel_data(addr))
619 * in-kernel percpu var?
621 if (is_kernel_percpu_address(addr))
625 * module static or percpu var?
627 return is_module_address(addr) || is_module_percpu_address(addr);
632 * To make lock name printouts unique, we calculate a unique
633 * class->name_version generation counter. The caller must hold the graph
636 static int count_matching_names(struct lock_class *new_class)
638 struct lock_class *class;
641 if (!new_class->name)
644 list_for_each_entry(class, &all_lock_classes, lock_entry) {
645 if (new_class->key - new_class->subclass == class->key)
646 return class->name_version;
647 if (class->name && !strcmp(class->name, new_class->name))
648 count = max(count, class->name_version);
654 static inline struct lock_class *
655 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
657 struct lockdep_subclass_key *key;
658 struct hlist_head *hash_head;
659 struct lock_class *class;
661 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
664 "BUG: looking up invalid subclass: %u\n", subclass);
666 "turning off the locking correctness validator.\n");
672 * If it is not initialised then it has never been locked,
673 * so it won't be present in the hash table.
675 if (unlikely(!lock->key))
679 * NOTE: the class-key must be unique. For dynamic locks, a static
680 * lock_class_key variable is passed in through the mutex_init()
681 * (or spin_lock_init()) call - which acts as the key. For static
682 * locks we use the lock object itself as the key.
684 BUILD_BUG_ON(sizeof(struct lock_class_key) >
685 sizeof(struct lockdep_map));
687 key = lock->key->subkeys + subclass;
689 hash_head = classhashentry(key);
692 * We do an RCU walk of the hash, see lockdep_free_key_range().
694 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
697 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
698 if (class->key == key) {
700 * Huh! same key, different name? Did someone trample
701 * on some memory? We're most confused.
703 WARN_ON_ONCE(class->name != lock->name);
712 * Static locks do not have their class-keys yet - for them the key is
713 * the lock object itself. If the lock is in the per cpu area, the
714 * canonical address of the lock (per cpu offset removed) is used.
716 static bool assign_lock_key(struct lockdep_map *lock)
718 unsigned long can_addr, addr = (unsigned long)lock;
720 if (__is_kernel_percpu_address(addr, &can_addr))
721 lock->key = (void *)can_addr;
722 else if (__is_module_percpu_address(addr, &can_addr))
723 lock->key = (void *)can_addr;
724 else if (static_obj(lock))
725 lock->key = (void *)lock;
727 /* Debug-check: all keys must be persistent! */
729 pr_err("INFO: trying to register non-static key.\n");
730 pr_err("the code is fine but needs lockdep annotation.\n");
731 pr_err("turning off the locking correctness validator.\n");
740 * Register a lock's class in the hash-table, if the class is not present
741 * yet. Otherwise we look it up. We cache the result in the lock object
742 * itself, so actual lookup of the hash should be once per lock object.
744 static struct lock_class *
745 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
747 struct lockdep_subclass_key *key;
748 struct hlist_head *hash_head;
749 struct lock_class *class;
751 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
753 class = look_up_lock_class(lock, subclass);
755 goto out_set_class_cache;
758 if (!assign_lock_key(lock))
760 } else if (!static_obj(lock->key)) {
764 key = lock->key->subkeys + subclass;
765 hash_head = classhashentry(key);
771 * We have to do the hash-walk again, to avoid races
774 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
775 if (class->key == key)
780 * Allocate a new key from the static array, and add it to
783 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
784 if (!debug_locks_off_graph_unlock()) {
788 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
792 class = lock_classes + nr_lock_classes++;
793 debug_atomic_inc(nr_unused_locks);
795 class->name = lock->name;
796 class->subclass = subclass;
797 INIT_LIST_HEAD(&class->locks_before);
798 INIT_LIST_HEAD(&class->locks_after);
799 class->name_version = count_matching_names(class);
801 * We use RCU's safe list-add method to make
802 * parallel walking of the hash-list safe:
804 hlist_add_head_rcu(&class->hash_entry, hash_head);
806 * Add it to the global list of classes:
808 list_add_tail(&class->lock_entry, &all_lock_classes);
810 if (verbose(class)) {
813 printk("\nnew class %px: %s", class->key, class->name);
814 if (class->name_version > 1)
815 printk(KERN_CONT "#%d", class->name_version);
816 printk(KERN_CONT "\n");
827 if (!subclass || force)
828 lock->class_cache[0] = class;
829 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
830 lock->class_cache[subclass] = class;
833 * Hash collision, did we smoke some? We found a class with a matching
834 * hash but the subclass -- which is hashed in -- didn't match.
836 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
842 #ifdef CONFIG_PROVE_LOCKING
844 * Allocate a lockdep entry. (assumes the graph_lock held, returns
845 * with NULL on failure)
847 static struct lock_list *alloc_list_entry(void)
849 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
850 if (!debug_locks_off_graph_unlock())
853 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
857 return list_entries + nr_list_entries++;
861 * Add a new dependency to the head of the list:
863 static int add_lock_to_list(struct lock_class *this, struct list_head *head,
864 unsigned long ip, int distance,
865 struct stack_trace *trace)
867 struct lock_list *entry;
869 * Lock not present yet - get a new dependency struct and
870 * add it to the list:
872 entry = alloc_list_entry();
877 entry->distance = distance;
878 entry->trace = *trace;
880 * Both allocation and removal are done under the graph lock; but
881 * iteration is under RCU-sched; see look_up_lock_class() and
882 * lockdep_free_key_range().
884 list_add_tail_rcu(&entry->entry, head);
890 * For good efficiency of modular, we use power of 2
892 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
893 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
896 * The circular_queue and helpers is used to implement the
897 * breadth-first search(BFS)algorithem, by which we can build
898 * the shortest path from the next lock to be acquired to the
899 * previous held lock if there is a circular between them.
901 struct circular_queue {
902 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
903 unsigned int front, rear;
906 static struct circular_queue lock_cq;
908 unsigned int max_bfs_queue_depth;
910 static unsigned int lockdep_dependency_gen_id;
912 static inline void __cq_init(struct circular_queue *cq)
914 cq->front = cq->rear = 0;
915 lockdep_dependency_gen_id++;
918 static inline int __cq_empty(struct circular_queue *cq)
920 return (cq->front == cq->rear);
923 static inline int __cq_full(struct circular_queue *cq)
925 return ((cq->rear + 1) & CQ_MASK) == cq->front;
928 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
933 cq->element[cq->rear] = elem;
934 cq->rear = (cq->rear + 1) & CQ_MASK;
938 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
943 *elem = cq->element[cq->front];
944 cq->front = (cq->front + 1) & CQ_MASK;
948 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
950 return (cq->rear - cq->front) & CQ_MASK;
953 static inline void mark_lock_accessed(struct lock_list *lock,
954 struct lock_list *parent)
958 nr = lock - list_entries;
959 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
960 lock->parent = parent;
961 lock->class->dep_gen_id = lockdep_dependency_gen_id;
964 static inline unsigned long lock_accessed(struct lock_list *lock)
968 nr = lock - list_entries;
969 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
970 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
973 static inline struct lock_list *get_lock_parent(struct lock_list *child)
975 return child->parent;
978 static inline int get_lock_depth(struct lock_list *child)
981 struct lock_list *parent;
983 while ((parent = get_lock_parent(child))) {
990 static int __bfs(struct lock_list *source_entry,
992 int (*match)(struct lock_list *entry, void *data),
993 struct lock_list **target_entry,
996 struct lock_list *entry;
997 struct list_head *head;
998 struct circular_queue *cq = &lock_cq;
1001 if (match(source_entry, data)) {
1002 *target_entry = source_entry;
1008 head = &source_entry->class->locks_after;
1010 head = &source_entry->class->locks_before;
1012 if (list_empty(head))
1016 __cq_enqueue(cq, (unsigned long)source_entry);
1018 while (!__cq_empty(cq)) {
1019 struct lock_list *lock;
1021 __cq_dequeue(cq, (unsigned long *)&lock);
1029 head = &lock->class->locks_after;
1031 head = &lock->class->locks_before;
1033 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1035 list_for_each_entry_rcu(entry, head, entry) {
1036 if (!lock_accessed(entry)) {
1037 unsigned int cq_depth;
1038 mark_lock_accessed(entry, lock);
1039 if (match(entry, data)) {
1040 *target_entry = entry;
1045 if (__cq_enqueue(cq, (unsigned long)entry)) {
1049 cq_depth = __cq_get_elem_count(cq);
1050 if (max_bfs_queue_depth < cq_depth)
1051 max_bfs_queue_depth = cq_depth;
1059 static inline int __bfs_forwards(struct lock_list *src_entry,
1061 int (*match)(struct lock_list *entry, void *data),
1062 struct lock_list **target_entry)
1064 return __bfs(src_entry, data, match, target_entry, 1);
1068 static inline int __bfs_backwards(struct lock_list *src_entry,
1070 int (*match)(struct lock_list *entry, void *data),
1071 struct lock_list **target_entry)
1073 return __bfs(src_entry, data, match, target_entry, 0);
1078 * Recursive, forwards-direction lock-dependency checking, used for
1079 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1084 * Print a dependency chain entry (this is only done when a deadlock
1085 * has been detected):
1088 print_circular_bug_entry(struct lock_list *target, int depth)
1090 if (debug_locks_silent)
1092 printk("\n-> #%u", depth);
1093 print_lock_name(target->class);
1094 printk(KERN_CONT ":\n");
1095 print_stack_trace(&target->trace, 6);
1101 print_circular_lock_scenario(struct held_lock *src,
1102 struct held_lock *tgt,
1103 struct lock_list *prt)
1105 struct lock_class *source = hlock_class(src);
1106 struct lock_class *target = hlock_class(tgt);
1107 struct lock_class *parent = prt->class;
1110 * A direct locking problem where unsafe_class lock is taken
1111 * directly by safe_class lock, then all we need to show
1112 * is the deadlock scenario, as it is obvious that the
1113 * unsafe lock is taken under the safe lock.
1115 * But if there is a chain instead, where the safe lock takes
1116 * an intermediate lock (middle_class) where this lock is
1117 * not the same as the safe lock, then the lock chain is
1118 * used to describe the problem. Otherwise we would need
1119 * to show a different CPU case for each link in the chain
1120 * from the safe_class lock to the unsafe_class lock.
1122 if (parent != source) {
1123 printk("Chain exists of:\n ");
1124 __print_lock_name(source);
1125 printk(KERN_CONT " --> ");
1126 __print_lock_name(parent);
1127 printk(KERN_CONT " --> ");
1128 __print_lock_name(target);
1129 printk(KERN_CONT "\n\n");
1132 printk(" Possible unsafe locking scenario:\n\n");
1133 printk(" CPU0 CPU1\n");
1134 printk(" ---- ----\n");
1136 __print_lock_name(target);
1137 printk(KERN_CONT ");\n");
1139 __print_lock_name(parent);
1140 printk(KERN_CONT ");\n");
1142 __print_lock_name(target);
1143 printk(KERN_CONT ");\n");
1145 __print_lock_name(source);
1146 printk(KERN_CONT ");\n");
1147 printk("\n *** DEADLOCK ***\n\n");
1151 * When a circular dependency is detected, print the
1155 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1156 struct held_lock *check_src,
1157 struct held_lock *check_tgt)
1159 struct task_struct *curr = current;
1161 if (debug_locks_silent)
1165 pr_warn("======================================================\n");
1166 pr_warn("WARNING: possible circular locking dependency detected\n");
1167 print_kernel_ident();
1168 pr_warn("------------------------------------------------------\n");
1169 pr_warn("%s/%d is trying to acquire lock:\n",
1170 curr->comm, task_pid_nr(curr));
1171 print_lock(check_src);
1173 pr_warn("\nbut task is already holding lock:\n");
1175 print_lock(check_tgt);
1176 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1177 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1179 print_circular_bug_entry(entry, depth);
1184 static inline int class_equal(struct lock_list *entry, void *data)
1186 return entry->class == data;
1189 static noinline int print_circular_bug(struct lock_list *this,
1190 struct lock_list *target,
1191 struct held_lock *check_src,
1192 struct held_lock *check_tgt,
1193 struct stack_trace *trace)
1195 struct task_struct *curr = current;
1196 struct lock_list *parent;
1197 struct lock_list *first_parent;
1200 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1203 if (!save_trace(&this->trace))
1206 depth = get_lock_depth(target);
1208 print_circular_bug_header(target, depth, check_src, check_tgt);
1210 parent = get_lock_parent(target);
1211 first_parent = parent;
1214 print_circular_bug_entry(parent, --depth);
1215 parent = get_lock_parent(parent);
1218 printk("\nother info that might help us debug this:\n\n");
1219 print_circular_lock_scenario(check_src, check_tgt,
1222 lockdep_print_held_locks(curr);
1224 printk("\nstack backtrace:\n");
1230 static noinline int print_bfs_bug(int ret)
1232 if (!debug_locks_off_graph_unlock())
1236 * Breadth-first-search failed, graph got corrupted?
1238 WARN(1, "lockdep bfs error:%d\n", ret);
1243 static int noop_count(struct lock_list *entry, void *data)
1245 (*(unsigned long *)data)++;
1249 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1251 unsigned long count = 0;
1252 struct lock_list *uninitialized_var(target_entry);
1254 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1258 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1260 unsigned long ret, flags;
1261 struct lock_list this;
1266 raw_local_irq_save(flags);
1267 arch_spin_lock(&lockdep_lock);
1268 ret = __lockdep_count_forward_deps(&this);
1269 arch_spin_unlock(&lockdep_lock);
1270 raw_local_irq_restore(flags);
1275 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1277 unsigned long count = 0;
1278 struct lock_list *uninitialized_var(target_entry);
1280 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1285 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1287 unsigned long ret, flags;
1288 struct lock_list this;
1293 raw_local_irq_save(flags);
1294 arch_spin_lock(&lockdep_lock);
1295 ret = __lockdep_count_backward_deps(&this);
1296 arch_spin_unlock(&lockdep_lock);
1297 raw_local_irq_restore(flags);
1303 * Prove that the dependency graph starting at <entry> can not
1304 * lead to <target>. Print an error and return 0 if it does.
1307 check_noncircular(struct lock_list *root, struct lock_class *target,
1308 struct lock_list **target_entry)
1312 debug_atomic_inc(nr_cyclic_checks);
1314 result = __bfs_forwards(root, target, class_equal, target_entry);
1320 check_redundant(struct lock_list *root, struct lock_class *target,
1321 struct lock_list **target_entry)
1325 debug_atomic_inc(nr_redundant_checks);
1327 result = __bfs_forwards(root, target, class_equal, target_entry);
1332 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1334 * Forwards and backwards subgraph searching, for the purposes of
1335 * proving that two subgraphs can be connected by a new dependency
1336 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1339 static inline int usage_match(struct lock_list *entry, void *bit)
1341 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1347 * Find a node in the forwards-direction dependency sub-graph starting
1348 * at @root->class that matches @bit.
1350 * Return 0 if such a node exists in the subgraph, and put that node
1351 * into *@target_entry.
1353 * Return 1 otherwise and keep *@target_entry unchanged.
1354 * Return <0 on error.
1357 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1358 struct lock_list **target_entry)
1362 debug_atomic_inc(nr_find_usage_forwards_checks);
1364 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1370 * Find a node in the backwards-direction dependency sub-graph starting
1371 * at @root->class that matches @bit.
1373 * Return 0 if such a node exists in the subgraph, and put that node
1374 * into *@target_entry.
1376 * Return 1 otherwise and keep *@target_entry unchanged.
1377 * Return <0 on error.
1380 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1381 struct lock_list **target_entry)
1385 debug_atomic_inc(nr_find_usage_backwards_checks);
1387 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1392 static void print_lock_class_header(struct lock_class *class, int depth)
1396 printk("%*s->", depth, "");
1397 print_lock_name(class);
1398 #ifdef CONFIG_DEBUG_LOCKDEP
1399 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
1401 printk(KERN_CONT " {\n");
1403 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1404 if (class->usage_mask & (1 << bit)) {
1407 len += printk("%*s %s", depth, "", usage_str[bit]);
1408 len += printk(KERN_CONT " at:\n");
1409 print_stack_trace(class->usage_traces + bit, len);
1412 printk("%*s }\n", depth, "");
1414 printk("%*s ... key at: [<%px>] %pS\n",
1415 depth, "", class->key, class->key);
1419 * printk the shortest lock dependencies from @start to @end in reverse order:
1422 print_shortest_lock_dependencies(struct lock_list *leaf,
1423 struct lock_list *root)
1425 struct lock_list *entry = leaf;
1428 /*compute depth from generated tree by BFS*/
1429 depth = get_lock_depth(leaf);
1432 print_lock_class_header(entry->class, depth);
1433 printk("%*s ... acquired at:\n", depth, "");
1434 print_stack_trace(&entry->trace, 2);
1437 if (depth == 0 && (entry != root)) {
1438 printk("lockdep:%s bad path found in chain graph\n", __func__);
1442 entry = get_lock_parent(entry);
1444 } while (entry && (depth >= 0));
1450 print_irq_lock_scenario(struct lock_list *safe_entry,
1451 struct lock_list *unsafe_entry,
1452 struct lock_class *prev_class,
1453 struct lock_class *next_class)
1455 struct lock_class *safe_class = safe_entry->class;
1456 struct lock_class *unsafe_class = unsafe_entry->class;
1457 struct lock_class *middle_class = prev_class;
1459 if (middle_class == safe_class)
1460 middle_class = next_class;
1463 * A direct locking problem where unsafe_class lock is taken
1464 * directly by safe_class lock, then all we need to show
1465 * is the deadlock scenario, as it is obvious that the
1466 * unsafe lock is taken under the safe lock.
1468 * But if there is a chain instead, where the safe lock takes
1469 * an intermediate lock (middle_class) where this lock is
1470 * not the same as the safe lock, then the lock chain is
1471 * used to describe the problem. Otherwise we would need
1472 * to show a different CPU case for each link in the chain
1473 * from the safe_class lock to the unsafe_class lock.
1475 if (middle_class != unsafe_class) {
1476 printk("Chain exists of:\n ");
1477 __print_lock_name(safe_class);
1478 printk(KERN_CONT " --> ");
1479 __print_lock_name(middle_class);
1480 printk(KERN_CONT " --> ");
1481 __print_lock_name(unsafe_class);
1482 printk(KERN_CONT "\n\n");
1485 printk(" Possible interrupt unsafe locking scenario:\n\n");
1486 printk(" CPU0 CPU1\n");
1487 printk(" ---- ----\n");
1489 __print_lock_name(unsafe_class);
1490 printk(KERN_CONT ");\n");
1491 printk(" local_irq_disable();\n");
1493 __print_lock_name(safe_class);
1494 printk(KERN_CONT ");\n");
1496 __print_lock_name(middle_class);
1497 printk(KERN_CONT ");\n");
1498 printk(" <Interrupt>\n");
1500 __print_lock_name(safe_class);
1501 printk(KERN_CONT ");\n");
1502 printk("\n *** DEADLOCK ***\n\n");
1506 print_bad_irq_dependency(struct task_struct *curr,
1507 struct lock_list *prev_root,
1508 struct lock_list *next_root,
1509 struct lock_list *backwards_entry,
1510 struct lock_list *forwards_entry,
1511 struct held_lock *prev,
1512 struct held_lock *next,
1513 enum lock_usage_bit bit1,
1514 enum lock_usage_bit bit2,
1515 const char *irqclass)
1517 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1521 pr_warn("=====================================================\n");
1522 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1523 irqclass, irqclass);
1524 print_kernel_ident();
1525 pr_warn("-----------------------------------------------------\n");
1526 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1527 curr->comm, task_pid_nr(curr),
1528 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1529 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1530 curr->hardirqs_enabled,
1531 curr->softirqs_enabled);
1534 pr_warn("\nand this task is already holding:\n");
1536 pr_warn("which would create a new lock dependency:\n");
1537 print_lock_name(hlock_class(prev));
1539 print_lock_name(hlock_class(next));
1542 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1544 print_lock_name(backwards_entry->class);
1545 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
1547 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1549 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
1550 print_lock_name(forwards_entry->class);
1551 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1554 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1556 pr_warn("\nother info that might help us debug this:\n\n");
1557 print_irq_lock_scenario(backwards_entry, forwards_entry,
1558 hlock_class(prev), hlock_class(next));
1560 lockdep_print_held_locks(curr);
1562 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
1563 if (!save_trace(&prev_root->trace))
1565 print_shortest_lock_dependencies(backwards_entry, prev_root);
1567 pr_warn("\nthe dependencies between the lock to be acquired");
1568 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
1569 if (!save_trace(&next_root->trace))
1571 print_shortest_lock_dependencies(forwards_entry, next_root);
1573 pr_warn("\nstack backtrace:\n");
1580 check_usage(struct task_struct *curr, struct held_lock *prev,
1581 struct held_lock *next, enum lock_usage_bit bit_backwards,
1582 enum lock_usage_bit bit_forwards, const char *irqclass)
1585 struct lock_list this, that;
1586 struct lock_list *uninitialized_var(target_entry);
1587 struct lock_list *uninitialized_var(target_entry1);
1591 this.class = hlock_class(prev);
1592 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1594 return print_bfs_bug(ret);
1599 that.class = hlock_class(next);
1600 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1602 return print_bfs_bug(ret);
1606 return print_bad_irq_dependency(curr, &this, &that,
1607 target_entry, target_entry1,
1609 bit_backwards, bit_forwards, irqclass);
1612 static const char *state_names[] = {
1613 #define LOCKDEP_STATE(__STATE) \
1614 __stringify(__STATE),
1615 #include "lockdep_states.h"
1616 #undef LOCKDEP_STATE
1619 static const char *state_rnames[] = {
1620 #define LOCKDEP_STATE(__STATE) \
1621 __stringify(__STATE)"-READ",
1622 #include "lockdep_states.h"
1623 #undef LOCKDEP_STATE
1626 static inline const char *state_name(enum lock_usage_bit bit)
1628 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1631 static int exclusive_bit(int new_bit)
1639 * bit 0 - write/read
1640 * bit 1 - used_in/enabled
1644 int state = new_bit & ~3;
1645 int dir = new_bit & 2;
1648 * keep state, bit flip the direction and strip read.
1650 return state | (dir ^ 2);
1653 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1654 struct held_lock *next, enum lock_usage_bit bit)
1657 * Prove that the new dependency does not connect a hardirq-safe
1658 * lock with a hardirq-unsafe lock - to achieve this we search
1659 * the backwards-subgraph starting at <prev>, and the
1660 * forwards-subgraph starting at <next>:
1662 if (!check_usage(curr, prev, next, bit,
1663 exclusive_bit(bit), state_name(bit)))
1669 * Prove that the new dependency does not connect a hardirq-safe-read
1670 * lock with a hardirq-unsafe lock - to achieve this we search
1671 * the backwards-subgraph starting at <prev>, and the
1672 * forwards-subgraph starting at <next>:
1674 if (!check_usage(curr, prev, next, bit,
1675 exclusive_bit(bit), state_name(bit)))
1682 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1683 struct held_lock *next)
1685 #define LOCKDEP_STATE(__STATE) \
1686 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1688 #include "lockdep_states.h"
1689 #undef LOCKDEP_STATE
1694 static void inc_chains(void)
1696 if (current->hardirq_context)
1697 nr_hardirq_chains++;
1699 if (current->softirq_context)
1700 nr_softirq_chains++;
1702 nr_process_chains++;
1709 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1710 struct held_lock *next)
1715 static inline void inc_chains(void)
1717 nr_process_chains++;
1723 print_deadlock_scenario(struct held_lock *nxt,
1724 struct held_lock *prv)
1726 struct lock_class *next = hlock_class(nxt);
1727 struct lock_class *prev = hlock_class(prv);
1729 printk(" Possible unsafe locking scenario:\n\n");
1733 __print_lock_name(prev);
1734 printk(KERN_CONT ");\n");
1736 __print_lock_name(next);
1737 printk(KERN_CONT ");\n");
1738 printk("\n *** DEADLOCK ***\n\n");
1739 printk(" May be due to missing lock nesting notation\n\n");
1743 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1744 struct held_lock *next)
1746 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1750 pr_warn("============================================\n");
1751 pr_warn("WARNING: possible recursive locking detected\n");
1752 print_kernel_ident();
1753 pr_warn("--------------------------------------------\n");
1754 pr_warn("%s/%d is trying to acquire lock:\n",
1755 curr->comm, task_pid_nr(curr));
1757 pr_warn("\nbut task is already holding lock:\n");
1760 pr_warn("\nother info that might help us debug this:\n");
1761 print_deadlock_scenario(next, prev);
1762 lockdep_print_held_locks(curr);
1764 pr_warn("\nstack backtrace:\n");
1771 * Check whether we are holding such a class already.
1773 * (Note that this has to be done separately, because the graph cannot
1774 * detect such classes of deadlocks.)
1776 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1779 check_deadlock(struct task_struct *curr, struct held_lock *next,
1780 struct lockdep_map *next_instance, int read)
1782 struct held_lock *prev;
1783 struct held_lock *nest = NULL;
1786 for (i = 0; i < curr->lockdep_depth; i++) {
1787 prev = curr->held_locks + i;
1789 if (prev->instance == next->nest_lock)
1792 if (hlock_class(prev) != hlock_class(next))
1796 * Allow read-after-read recursion of the same
1797 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1799 if ((read == 2) && prev->read)
1803 * We're holding the nest_lock, which serializes this lock's
1804 * nesting behaviour.
1809 return print_deadlock_bug(curr, prev, next);
1815 * There was a chain-cache miss, and we are about to add a new dependency
1816 * to a previous lock. We recursively validate the following rules:
1818 * - would the adding of the <prev> -> <next> dependency create a
1819 * circular dependency in the graph? [== circular deadlock]
1821 * - does the new prev->next dependency connect any hardirq-safe lock
1822 * (in the full backwards-subgraph starting at <prev>) with any
1823 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1824 * <next>)? [== illegal lock inversion with hardirq contexts]
1826 * - does the new prev->next dependency connect any softirq-safe lock
1827 * (in the full backwards-subgraph starting at <prev>) with any
1828 * softirq-unsafe lock (in the full forwards-subgraph starting at
1829 * <next>)? [== illegal lock inversion with softirq contexts]
1831 * any of these scenarios could lead to a deadlock.
1833 * Then if all the validations pass, we add the forwards and backwards
1837 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1838 struct held_lock *next, int distance, struct stack_trace *trace,
1839 int (*save)(struct stack_trace *trace))
1841 struct lock_list *uninitialized_var(target_entry);
1842 struct lock_list *entry;
1843 struct lock_list this;
1847 * Prove that the new <prev> -> <next> dependency would not
1848 * create a circular dependency in the graph. (We do this by
1849 * forward-recursing into the graph starting at <next>, and
1850 * checking whether we can reach <prev>.)
1852 * We are using global variables to control the recursion, to
1853 * keep the stackframe size of the recursive functions low:
1855 this.class = hlock_class(next);
1857 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1858 if (unlikely(!ret)) {
1859 if (!trace->entries) {
1861 * If @save fails here, the printing might trigger
1862 * a WARN but because of the !nr_entries it should
1863 * not do bad things.
1867 return print_circular_bug(&this, target_entry, next, prev, trace);
1869 else if (unlikely(ret < 0))
1870 return print_bfs_bug(ret);
1872 if (!check_prev_add_irq(curr, prev, next))
1876 * For recursive read-locks we do all the dependency checks,
1877 * but we dont store read-triggered dependencies (only
1878 * write-triggered dependencies). This ensures that only the
1879 * write-side dependencies matter, and that if for example a
1880 * write-lock never takes any other locks, then the reads are
1881 * equivalent to a NOP.
1883 if (next->read == 2 || prev->read == 2)
1886 * Is the <prev> -> <next> dependency already present?
1888 * (this may occur even though this is a new chain: consider
1889 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1890 * chains - the second one will be new, but L1 already has
1891 * L2 added to its dependency list, due to the first chain.)
1893 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1894 if (entry->class == hlock_class(next)) {
1896 entry->distance = 1;
1902 * Is the <prev> -> <next> link redundant?
1904 this.class = hlock_class(prev);
1906 ret = check_redundant(&this, hlock_class(next), &target_entry);
1908 debug_atomic_inc(nr_redundant);
1912 return print_bfs_bug(ret);
1915 if (!trace->entries && !save(trace))
1919 * Ok, all validations passed, add the new lock
1920 * to the previous lock's dependency list:
1922 ret = add_lock_to_list(hlock_class(next),
1923 &hlock_class(prev)->locks_after,
1924 next->acquire_ip, distance, trace);
1929 ret = add_lock_to_list(hlock_class(prev),
1930 &hlock_class(next)->locks_before,
1931 next->acquire_ip, distance, trace);
1939 * Add the dependency to all directly-previous locks that are 'relevant'.
1940 * The ones that are relevant are (in increasing distance from curr):
1941 * all consecutive trylock entries and the final non-trylock entry - or
1942 * the end of this context's lock-chain - whichever comes first.
1945 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1947 int depth = curr->lockdep_depth;
1948 struct held_lock *hlock;
1949 struct stack_trace trace = {
1959 * Depth must not be zero for a non-head lock:
1964 * At least two relevant locks must exist for this
1967 if (curr->held_locks[depth].irq_context !=
1968 curr->held_locks[depth-1].irq_context)
1972 int distance = curr->lockdep_depth - depth + 1;
1973 hlock = curr->held_locks + depth - 1;
1976 * Only non-recursive-read entries get new dependencies
1979 if (hlock->read != 2 && hlock->check) {
1980 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace);
1985 * Stop after the first non-trylock entry,
1986 * as non-trylock entries have added their
1987 * own direct dependencies already, so this
1988 * lock is connected to them indirectly:
1990 if (!hlock->trylock)
1996 * End of lock-stack?
2001 * Stop the search if we cross into another context:
2003 if (curr->held_locks[depth].irq_context !=
2004 curr->held_locks[depth-1].irq_context)
2009 if (!debug_locks_off_graph_unlock())
2013 * Clearly we all shouldn't be here, but since we made it we
2014 * can reliable say we messed up our state. See the above two
2015 * gotos for reasons why we could possibly end up here.
2022 unsigned long nr_lock_chains;
2023 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2024 int nr_chain_hlocks;
2025 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2027 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2029 return lock_classes + chain_hlocks[chain->base + i];
2033 * Returns the index of the first held_lock of the current chain
2035 static inline int get_first_held_lock(struct task_struct *curr,
2036 struct held_lock *hlock)
2039 struct held_lock *hlock_curr;
2041 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2042 hlock_curr = curr->held_locks + i;
2043 if (hlock_curr->irq_context != hlock->irq_context)
2051 #ifdef CONFIG_DEBUG_LOCKDEP
2053 * Returns the next chain_key iteration
2055 static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2057 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2059 printk(" class_idx:%d -> chain_key:%016Lx",
2061 (unsigned long long)new_chain_key);
2062 return new_chain_key;
2066 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2068 struct held_lock *hlock;
2070 int depth = curr->lockdep_depth;
2073 printk("depth: %u\n", depth + 1);
2074 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2075 hlock = curr->held_locks + i;
2076 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2081 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2082 print_lock(hlock_next);
2085 static void print_chain_keys_chain(struct lock_chain *chain)
2091 printk("depth: %u\n", chain->depth);
2092 for (i = 0; i < chain->depth; i++) {
2093 class_id = chain_hlocks[chain->base + i];
2094 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2096 print_lock_name(lock_classes + class_id);
2101 static void print_collision(struct task_struct *curr,
2102 struct held_lock *hlock_next,
2103 struct lock_chain *chain)
2106 pr_warn("============================\n");
2107 pr_warn("WARNING: chain_key collision\n");
2108 print_kernel_ident();
2109 pr_warn("----------------------------\n");
2110 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2111 pr_warn("Hash chain already cached but the contents don't match!\n");
2113 pr_warn("Held locks:");
2114 print_chain_keys_held_locks(curr, hlock_next);
2116 pr_warn("Locks in cached chain:");
2117 print_chain_keys_chain(chain);
2119 pr_warn("\nstack backtrace:\n");
2125 * Checks whether the chain and the current held locks are consistent
2126 * in depth and also in content. If they are not it most likely means
2127 * that there was a collision during the calculation of the chain_key.
2128 * Returns: 0 not passed, 1 passed
2130 static int check_no_collision(struct task_struct *curr,
2131 struct held_lock *hlock,
2132 struct lock_chain *chain)
2134 #ifdef CONFIG_DEBUG_LOCKDEP
2137 i = get_first_held_lock(curr, hlock);
2139 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2140 print_collision(curr, hlock, chain);
2144 for (j = 0; j < chain->depth - 1; j++, i++) {
2145 id = curr->held_locks[i].class_idx - 1;
2147 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2148 print_collision(curr, hlock, chain);
2157 * Adds a dependency chain into chain hashtable. And must be called with
2160 * Return 0 if fail, and graph_lock is released.
2161 * Return 1 if succeed, with graph_lock held.
2163 static inline int add_chain_cache(struct task_struct *curr,
2164 struct held_lock *hlock,
2167 struct lock_class *class = hlock_class(hlock);
2168 struct hlist_head *hash_head = chainhashentry(chain_key);
2169 struct lock_chain *chain;
2173 * Allocate a new chain entry from the static array, and add
2178 * We might need to take the graph lock, ensure we've got IRQs
2179 * disabled to make this an IRQ-safe lock.. for recursion reasons
2180 * lockdep won't complain about its own locking errors.
2182 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2185 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2186 if (!debug_locks_off_graph_unlock())
2189 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2193 chain = lock_chains + nr_lock_chains++;
2194 chain->chain_key = chain_key;
2195 chain->irq_context = hlock->irq_context;
2196 i = get_first_held_lock(curr, hlock);
2197 chain->depth = curr->lockdep_depth + 1 - i;
2199 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2200 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2201 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2203 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2204 chain->base = nr_chain_hlocks;
2205 for (j = 0; j < chain->depth - 1; j++, i++) {
2206 int lock_id = curr->held_locks[i].class_idx - 1;
2207 chain_hlocks[chain->base + j] = lock_id;
2209 chain_hlocks[chain->base + j] = class - lock_classes;
2212 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2213 nr_chain_hlocks += chain->depth;
2215 #ifdef CONFIG_DEBUG_LOCKDEP
2217 * Important for check_no_collision().
2219 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
2220 if (!debug_locks_off_graph_unlock())
2223 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2229 hlist_add_head_rcu(&chain->entry, hash_head);
2230 debug_atomic_inc(chain_lookup_misses);
2237 * Look up a dependency chain.
2239 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2241 struct hlist_head *hash_head = chainhashentry(chain_key);
2242 struct lock_chain *chain;
2245 * We can walk it lock-free, because entries only get added
2248 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2249 if (chain->chain_key == chain_key) {
2250 debug_atomic_inc(chain_lookup_hits);
2258 * If the key is not present yet in dependency chain cache then
2259 * add it and return 1 - in this case the new dependency chain is
2260 * validated. If the key is already hashed, return 0.
2261 * (On return with 1 graph_lock is held.)
2263 static inline int lookup_chain_cache_add(struct task_struct *curr,
2264 struct held_lock *hlock,
2267 struct lock_class *class = hlock_class(hlock);
2268 struct lock_chain *chain = lookup_chain_cache(chain_key);
2272 if (!check_no_collision(curr, hlock, chain))
2275 if (very_verbose(class)) {
2276 printk("\nhash chain already cached, key: "
2277 "%016Lx tail class: [%px] %s\n",
2278 (unsigned long long)chain_key,
2279 class->key, class->name);
2285 if (very_verbose(class)) {
2286 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
2287 (unsigned long long)chain_key, class->key, class->name);
2294 * We have to walk the chain again locked - to avoid duplicates:
2296 chain = lookup_chain_cache(chain_key);
2302 if (!add_chain_cache(curr, hlock, chain_key))
2308 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2309 struct held_lock *hlock, int chain_head, u64 chain_key)
2312 * Trylock needs to maintain the stack of held locks, but it
2313 * does not add new dependencies, because trylock can be done
2316 * We look up the chain_key and do the O(N^2) check and update of
2317 * the dependencies only if this is a new dependency chain.
2318 * (If lookup_chain_cache_add() return with 1 it acquires
2319 * graph_lock for us)
2321 if (!hlock->trylock && hlock->check &&
2322 lookup_chain_cache_add(curr, hlock, chain_key)) {
2324 * Check whether last held lock:
2326 * - is irq-safe, if this lock is irq-unsafe
2327 * - is softirq-safe, if this lock is hardirq-unsafe
2329 * And check whether the new lock's dependency graph
2330 * could lead back to the previous lock.
2332 * any of these scenarios could lead to a deadlock. If
2335 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2340 * Mark recursive read, as we jump over it when
2341 * building dependencies (just like we jump over
2347 * Add dependency only if this lock is not the head
2348 * of the chain, and if it's not a secondary read-lock:
2350 if (!chain_head && ret != 2) {
2351 if (!check_prevs_add(curr, hlock))
2357 /* after lookup_chain_cache_add(): */
2358 if (unlikely(!debug_locks))
2365 static inline int validate_chain(struct task_struct *curr,
2366 struct lockdep_map *lock, struct held_lock *hlock,
2367 int chain_head, u64 chain_key)
2374 * We are building curr_chain_key incrementally, so double-check
2375 * it from scratch, to make sure that it's done correctly:
2377 static void check_chain_key(struct task_struct *curr)
2379 #ifdef CONFIG_DEBUG_LOCKDEP
2380 struct held_lock *hlock, *prev_hlock = NULL;
2384 for (i = 0; i < curr->lockdep_depth; i++) {
2385 hlock = curr->held_locks + i;
2386 if (chain_key != hlock->prev_chain_key) {
2389 * We got mighty confused, our chain keys don't match
2390 * with what we expect, someone trample on our task state?
2392 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2393 curr->lockdep_depth, i,
2394 (unsigned long long)chain_key,
2395 (unsigned long long)hlock->prev_chain_key);
2399 * Whoops ran out of static storage again?
2401 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
2404 if (prev_hlock && (prev_hlock->irq_context !=
2405 hlock->irq_context))
2407 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
2410 if (chain_key != curr->curr_chain_key) {
2413 * More smoking hash instead of calculating it, damn see these
2414 * numbers float.. I bet that a pink elephant stepped on my memory.
2416 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2417 curr->lockdep_depth, i,
2418 (unsigned long long)chain_key,
2419 (unsigned long long)curr->curr_chain_key);
2425 print_usage_bug_scenario(struct held_lock *lock)
2427 struct lock_class *class = hlock_class(lock);
2429 printk(" Possible unsafe locking scenario:\n\n");
2433 __print_lock_name(class);
2434 printk(KERN_CONT ");\n");
2435 printk(" <Interrupt>\n");
2437 __print_lock_name(class);
2438 printk(KERN_CONT ");\n");
2439 printk("\n *** DEADLOCK ***\n\n");
2443 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2444 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2446 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2450 pr_warn("================================\n");
2451 pr_warn("WARNING: inconsistent lock state\n");
2452 print_kernel_ident();
2453 pr_warn("--------------------------------\n");
2455 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2456 usage_str[prev_bit], usage_str[new_bit]);
2458 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2459 curr->comm, task_pid_nr(curr),
2460 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2461 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2462 trace_hardirqs_enabled(curr),
2463 trace_softirqs_enabled(curr));
2466 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
2467 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2469 print_irqtrace_events(curr);
2470 pr_warn("\nother info that might help us debug this:\n");
2471 print_usage_bug_scenario(this);
2473 lockdep_print_held_locks(curr);
2475 pr_warn("\nstack backtrace:\n");
2482 * Print out an error if an invalid bit is set:
2485 valid_state(struct task_struct *curr, struct held_lock *this,
2486 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2488 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2489 return print_usage_bug(curr, this, bad_bit, new_bit);
2493 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2494 enum lock_usage_bit new_bit);
2496 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2499 * print irq inversion bug:
2502 print_irq_inversion_bug(struct task_struct *curr,
2503 struct lock_list *root, struct lock_list *other,
2504 struct held_lock *this, int forwards,
2505 const char *irqclass)
2507 struct lock_list *entry = other;
2508 struct lock_list *middle = NULL;
2511 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2515 pr_warn("========================================================\n");
2516 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2517 print_kernel_ident();
2518 pr_warn("--------------------------------------------------------\n");
2519 pr_warn("%s/%d just changed the state of lock:\n",
2520 curr->comm, task_pid_nr(curr));
2523 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2525 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2526 print_lock_name(other->class);
2527 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2529 pr_warn("\nother info that might help us debug this:\n");
2531 /* Find a middle lock (if one exists) */
2532 depth = get_lock_depth(other);
2534 if (depth == 0 && (entry != root)) {
2535 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
2539 entry = get_lock_parent(entry);
2541 } while (entry && entry != root && (depth >= 0));
2543 print_irq_lock_scenario(root, other,
2544 middle ? middle->class : root->class, other->class);
2546 print_irq_lock_scenario(other, root,
2547 middle ? middle->class : other->class, root->class);
2549 lockdep_print_held_locks(curr);
2551 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2552 if (!save_trace(&root->trace))
2554 print_shortest_lock_dependencies(other, root);
2556 pr_warn("\nstack backtrace:\n");
2563 * Prove that in the forwards-direction subgraph starting at <this>
2564 * there is no lock matching <mask>:
2567 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2568 enum lock_usage_bit bit, const char *irqclass)
2571 struct lock_list root;
2572 struct lock_list *uninitialized_var(target_entry);
2575 root.class = hlock_class(this);
2576 ret = find_usage_forwards(&root, bit, &target_entry);
2578 return print_bfs_bug(ret);
2582 return print_irq_inversion_bug(curr, &root, target_entry,
2587 * Prove that in the backwards-direction subgraph starting at <this>
2588 * there is no lock matching <mask>:
2591 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2592 enum lock_usage_bit bit, const char *irqclass)
2595 struct lock_list root;
2596 struct lock_list *uninitialized_var(target_entry);
2599 root.class = hlock_class(this);
2600 ret = find_usage_backwards(&root, bit, &target_entry);
2602 return print_bfs_bug(ret);
2606 return print_irq_inversion_bug(curr, &root, target_entry,
2610 void print_irqtrace_events(struct task_struct *curr)
2612 printk("irq event stamp: %u\n", curr->irq_events);
2613 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
2614 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2615 (void *)curr->hardirq_enable_ip);
2616 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
2617 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2618 (void *)curr->hardirq_disable_ip);
2619 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
2620 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2621 (void *)curr->softirq_enable_ip);
2622 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
2623 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2624 (void *)curr->softirq_disable_ip);
2627 static int HARDIRQ_verbose(struct lock_class *class)
2630 return class_filter(class);
2635 static int SOFTIRQ_verbose(struct lock_class *class)
2638 return class_filter(class);
2643 #define STRICT_READ_CHECKS 1
2645 static int (*state_verbose_f[])(struct lock_class *class) = {
2646 #define LOCKDEP_STATE(__STATE) \
2648 #include "lockdep_states.h"
2649 #undef LOCKDEP_STATE
2652 static inline int state_verbose(enum lock_usage_bit bit,
2653 struct lock_class *class)
2655 return state_verbose_f[bit >> 2](class);
2658 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2659 enum lock_usage_bit bit, const char *name);
2662 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2663 enum lock_usage_bit new_bit)
2665 int excl_bit = exclusive_bit(new_bit);
2666 int read = new_bit & 1;
2667 int dir = new_bit & 2;
2670 * mark USED_IN has to look forwards -- to ensure no dependency
2671 * has ENABLED state, which would allow recursion deadlocks.
2673 * mark ENABLED has to look backwards -- to ensure no dependee
2674 * has USED_IN state, which, again, would allow recursion deadlocks.
2676 check_usage_f usage = dir ?
2677 check_usage_backwards : check_usage_forwards;
2680 * Validate that this particular lock does not have conflicting
2683 if (!valid_state(curr, this, new_bit, excl_bit))
2687 * Validate that the lock dependencies don't have conflicting usage
2690 if ((!read || !dir || STRICT_READ_CHECKS) &&
2691 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2695 * Check for read in write conflicts
2698 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2701 if (STRICT_READ_CHECKS &&
2702 !usage(curr, this, excl_bit + 1,
2703 state_name(new_bit + 1)))
2707 if (state_verbose(new_bit, hlock_class(this)))
2714 #define LOCKDEP_STATE(__STATE) __STATE,
2715 #include "lockdep_states.h"
2716 #undef LOCKDEP_STATE
2720 * Mark all held locks with a usage bit:
2723 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2725 enum lock_usage_bit usage_bit;
2726 struct held_lock *hlock;
2729 for (i = 0; i < curr->lockdep_depth; i++) {
2730 hlock = curr->held_locks + i;
2732 usage_bit = 2 + (mark << 2); /* ENABLED */
2734 usage_bit += 1; /* READ */
2736 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2741 if (!mark_lock(curr, hlock, usage_bit))
2749 * Hardirqs will be enabled:
2751 static void __trace_hardirqs_on_caller(unsigned long ip)
2753 struct task_struct *curr = current;
2755 /* we'll do an OFF -> ON transition: */
2756 curr->hardirqs_enabled = 1;
2759 * We are going to turn hardirqs on, so set the
2760 * usage bit for all held locks:
2762 if (!mark_held_locks(curr, HARDIRQ))
2765 * If we have softirqs enabled, then set the usage
2766 * bit for all held locks. (disabled hardirqs prevented
2767 * this bit from being set before)
2769 if (curr->softirqs_enabled)
2770 if (!mark_held_locks(curr, SOFTIRQ))
2773 curr->hardirq_enable_ip = ip;
2774 curr->hardirq_enable_event = ++curr->irq_events;
2775 debug_atomic_inc(hardirqs_on_events);
2778 void lockdep_hardirqs_on(unsigned long ip)
2780 if (unlikely(!debug_locks || current->lockdep_recursion))
2783 if (unlikely(current->hardirqs_enabled)) {
2785 * Neither irq nor preemption are disabled here
2786 * so this is racy by nature but losing one hit
2787 * in a stat is not a big deal.
2789 __debug_atomic_inc(redundant_hardirqs_on);
2794 * We're enabling irqs and according to our state above irqs weren't
2795 * already enabled, yet we find the hardware thinks they are in fact
2796 * enabled.. someone messed up their IRQ state tracing.
2798 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2802 * See the fine text that goes along with this variable definition.
2804 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2808 * Can't allow enabling interrupts while in an interrupt handler,
2809 * that's general bad form and such. Recursion, limited stack etc..
2811 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2814 current->lockdep_recursion = 1;
2815 __trace_hardirqs_on_caller(ip);
2816 current->lockdep_recursion = 0;
2818 NOKPROBE_SYMBOL(lockdep_hardirqs_on);
2821 * Hardirqs were disabled:
2823 void lockdep_hardirqs_off(unsigned long ip)
2825 struct task_struct *curr = current;
2827 if (unlikely(!debug_locks || current->lockdep_recursion))
2831 * So we're supposed to get called after you mask local IRQs, but for
2832 * some reason the hardware doesn't quite think you did a proper job.
2834 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2837 if (curr->hardirqs_enabled) {
2839 * We have done an ON -> OFF transition:
2841 curr->hardirqs_enabled = 0;
2842 curr->hardirq_disable_ip = ip;
2843 curr->hardirq_disable_event = ++curr->irq_events;
2844 debug_atomic_inc(hardirqs_off_events);
2846 debug_atomic_inc(redundant_hardirqs_off);
2848 NOKPROBE_SYMBOL(lockdep_hardirqs_off);
2851 * Softirqs will be enabled:
2853 void trace_softirqs_on(unsigned long ip)
2855 struct task_struct *curr = current;
2857 if (unlikely(!debug_locks || current->lockdep_recursion))
2861 * We fancy IRQs being disabled here, see softirq.c, avoids
2862 * funny state and nesting things.
2864 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2867 if (curr->softirqs_enabled) {
2868 debug_atomic_inc(redundant_softirqs_on);
2872 current->lockdep_recursion = 1;
2874 * We'll do an OFF -> ON transition:
2876 curr->softirqs_enabled = 1;
2877 curr->softirq_enable_ip = ip;
2878 curr->softirq_enable_event = ++curr->irq_events;
2879 debug_atomic_inc(softirqs_on_events);
2881 * We are going to turn softirqs on, so set the
2882 * usage bit for all held locks, if hardirqs are
2885 if (curr->hardirqs_enabled)
2886 mark_held_locks(curr, SOFTIRQ);
2887 current->lockdep_recursion = 0;
2891 * Softirqs were disabled:
2893 void trace_softirqs_off(unsigned long ip)
2895 struct task_struct *curr = current;
2897 if (unlikely(!debug_locks || current->lockdep_recursion))
2901 * We fancy IRQs being disabled here, see softirq.c
2903 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2906 if (curr->softirqs_enabled) {
2908 * We have done an ON -> OFF transition:
2910 curr->softirqs_enabled = 0;
2911 curr->softirq_disable_ip = ip;
2912 curr->softirq_disable_event = ++curr->irq_events;
2913 debug_atomic_inc(softirqs_off_events);
2915 * Whoops, we wanted softirqs off, so why aren't they?
2917 DEBUG_LOCKS_WARN_ON(!softirq_count());
2919 debug_atomic_inc(redundant_softirqs_off);
2922 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2925 * If non-trylock use in a hardirq or softirq context, then
2926 * mark the lock as used in these contexts:
2928 if (!hlock->trylock) {
2930 if (curr->hardirq_context)
2931 if (!mark_lock(curr, hlock,
2932 LOCK_USED_IN_HARDIRQ_READ))
2934 if (curr->softirq_context)
2935 if (!mark_lock(curr, hlock,
2936 LOCK_USED_IN_SOFTIRQ_READ))
2939 if (curr->hardirq_context)
2940 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2942 if (curr->softirq_context)
2943 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2947 if (!hlock->hardirqs_off) {
2949 if (!mark_lock(curr, hlock,
2950 LOCK_ENABLED_HARDIRQ_READ))
2952 if (curr->softirqs_enabled)
2953 if (!mark_lock(curr, hlock,
2954 LOCK_ENABLED_SOFTIRQ_READ))
2957 if (!mark_lock(curr, hlock,
2958 LOCK_ENABLED_HARDIRQ))
2960 if (curr->softirqs_enabled)
2961 if (!mark_lock(curr, hlock,
2962 LOCK_ENABLED_SOFTIRQ))
2970 static inline unsigned int task_irq_context(struct task_struct *task)
2972 return 2 * !!task->hardirq_context + !!task->softirq_context;
2975 static int separate_irq_context(struct task_struct *curr,
2976 struct held_lock *hlock)
2978 unsigned int depth = curr->lockdep_depth;
2981 * Keep track of points where we cross into an interrupt context:
2984 struct held_lock *prev_hlock;
2986 prev_hlock = curr->held_locks + depth-1;
2988 * If we cross into another context, reset the
2989 * hash key (this also prevents the checking and the
2990 * adding of the dependency to 'prev'):
2992 if (prev_hlock->irq_context != hlock->irq_context)
2998 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3001 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3002 enum lock_usage_bit new_bit)
3004 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3008 static inline int mark_irqflags(struct task_struct *curr,
3009 struct held_lock *hlock)
3014 static inline unsigned int task_irq_context(struct task_struct *task)
3019 static inline int separate_irq_context(struct task_struct *curr,
3020 struct held_lock *hlock)
3025 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3028 * Mark a lock with a usage bit, and validate the state transition:
3030 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3031 enum lock_usage_bit new_bit)
3033 unsigned int new_mask = 1 << new_bit, ret = 1;
3036 * If already set then do not dirty the cacheline,
3037 * nor do any checks:
3039 if (likely(hlock_class(this)->usage_mask & new_mask))
3045 * Make sure we didn't race:
3047 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3052 hlock_class(this)->usage_mask |= new_mask;
3054 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
3058 #define LOCKDEP_STATE(__STATE) \
3059 case LOCK_USED_IN_##__STATE: \
3060 case LOCK_USED_IN_##__STATE##_READ: \
3061 case LOCK_ENABLED_##__STATE: \
3062 case LOCK_ENABLED_##__STATE##_READ:
3063 #include "lockdep_states.h"
3064 #undef LOCKDEP_STATE
3065 ret = mark_lock_irq(curr, this, new_bit);
3070 debug_atomic_dec(nr_unused_locks);
3073 if (!debug_locks_off_graph_unlock())
3082 * We must printk outside of the graph_lock:
3085 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3087 print_irqtrace_events(curr);
3095 * Initialize a lock instance's lock-class mapping info:
3097 void lockdep_init_map(struct lockdep_map *lock, const char *name,
3098 struct lock_class_key *key, int subclass)
3102 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3103 lock->class_cache[i] = NULL;
3105 #ifdef CONFIG_LOCK_STAT
3106 lock->cpu = raw_smp_processor_id();
3110 * Can't be having no nameless bastards around this place!
3112 if (DEBUG_LOCKS_WARN_ON(!name)) {
3113 lock->name = "NULL";
3120 * No key, no joy, we need to hash something.
3122 if (DEBUG_LOCKS_WARN_ON(!key))
3125 * Sanity check, the lock-class key must be persistent:
3127 if (!static_obj(key)) {
3128 printk("BUG: key %px not in .data!\n", key);
3130 * What it says above ^^^^^, I suggest you read it.
3132 DEBUG_LOCKS_WARN_ON(1);
3137 if (unlikely(!debug_locks))
3141 unsigned long flags;
3143 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3146 raw_local_irq_save(flags);
3147 current->lockdep_recursion = 1;
3148 register_lock_class(lock, subclass, 1);
3149 current->lockdep_recursion = 0;
3150 raw_local_irq_restore(flags);
3153 EXPORT_SYMBOL_GPL(lockdep_init_map);
3155 struct lock_class_key __lockdep_no_validate__;
3156 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3159 print_lock_nested_lock_not_held(struct task_struct *curr,
3160 struct held_lock *hlock,
3163 if (!debug_locks_off())
3165 if (debug_locks_silent)
3169 pr_warn("==================================\n");
3170 pr_warn("WARNING: Nested lock was not taken\n");
3171 print_kernel_ident();
3172 pr_warn("----------------------------------\n");
3174 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3177 pr_warn("\nbut this task is not holding:\n");
3178 pr_warn("%s\n", hlock->nest_lock->name);
3180 pr_warn("\nstack backtrace:\n");
3183 pr_warn("\nother info that might help us debug this:\n");
3184 lockdep_print_held_locks(curr);
3186 pr_warn("\nstack backtrace:\n");
3192 static int __lock_is_held(const struct lockdep_map *lock, int read);
3195 * This gets called for every mutex_lock*()/spin_lock*() operation.
3196 * We maintain the dependency maps and validate the locking attempt:
3198 * The callers must make sure that IRQs are disabled before calling it,
3199 * otherwise we could get an interrupt which would want to take locks,
3200 * which would end up in lockdep again.
3202 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3203 int trylock, int read, int check, int hardirqs_off,
3204 struct lockdep_map *nest_lock, unsigned long ip,
3205 int references, int pin_count)
3207 struct task_struct *curr = current;
3208 struct lock_class *class = NULL;
3209 struct held_lock *hlock;
3215 if (unlikely(!debug_locks))
3218 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3221 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3222 class = lock->class_cache[subclass];
3226 if (unlikely(!class)) {
3227 class = register_lock_class(lock, subclass, 0);
3232 debug_class_ops_inc(class);
3234 if (very_verbose(class)) {
3235 printk("\nacquire class [%px] %s", class->key, class->name);
3236 if (class->name_version > 1)
3237 printk(KERN_CONT "#%d", class->name_version);
3238 printk(KERN_CONT "\n");
3243 * Add the lock to the list of currently held locks.
3244 * (we dont increase the depth just yet, up until the
3245 * dependency checks are done)
3247 depth = curr->lockdep_depth;
3249 * Ran out of static storage for our per-task lock stack again have we?
3251 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3254 class_idx = class - lock_classes + 1;
3257 hlock = curr->held_locks + depth - 1;
3258 if (hlock->class_idx == class_idx && nest_lock) {
3259 if (hlock->references) {
3261 * Check: unsigned int references:12, overflow.
3263 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3266 hlock->references++;
3268 hlock->references = 2;
3275 hlock = curr->held_locks + depth;
3277 * Plain impossible, we just registered it and checked it weren't no
3278 * NULL like.. I bet this mushroom I ate was good!
3280 if (DEBUG_LOCKS_WARN_ON(!class))
3282 hlock->class_idx = class_idx;
3283 hlock->acquire_ip = ip;
3284 hlock->instance = lock;
3285 hlock->nest_lock = nest_lock;
3286 hlock->irq_context = task_irq_context(curr);
3287 hlock->trylock = trylock;
3289 hlock->check = check;
3290 hlock->hardirqs_off = !!hardirqs_off;
3291 hlock->references = references;
3292 #ifdef CONFIG_LOCK_STAT
3293 hlock->waittime_stamp = 0;
3294 hlock->holdtime_stamp = lockstat_clock();
3296 hlock->pin_count = pin_count;
3298 if (check && !mark_irqflags(curr, hlock))
3301 /* mark it as used: */
3302 if (!mark_lock(curr, hlock, LOCK_USED))
3306 * Calculate the chain hash: it's the combined hash of all the
3307 * lock keys along the dependency chain. We save the hash value
3308 * at every step so that we can get the current hash easily
3309 * after unlock. The chain hash is then used to cache dependency
3312 * The 'key ID' is what is the most compact key value to drive
3313 * the hash, not class->key.
3316 * Whoops, we did it again.. ran straight out of our static allocation.
3318 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
3321 chain_key = curr->curr_chain_key;
3324 * How can we have a chain hash when we ain't got no keys?!
3326 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3331 hlock->prev_chain_key = chain_key;
3332 if (separate_irq_context(curr, hlock)) {
3336 chain_key = iterate_chain_key(chain_key, class_idx);
3338 if (nest_lock && !__lock_is_held(nest_lock, -1))
3339 return print_lock_nested_lock_not_held(curr, hlock, ip);
3341 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3344 curr->curr_chain_key = chain_key;
3345 curr->lockdep_depth++;
3346 check_chain_key(curr);
3347 #ifdef CONFIG_DEBUG_LOCKDEP
3348 if (unlikely(!debug_locks))
3351 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3353 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3354 printk(KERN_DEBUG "depth: %i max: %lu!\n",
3355 curr->lockdep_depth, MAX_LOCK_DEPTH);
3357 lockdep_print_held_locks(current);
3358 debug_show_all_locks();
3364 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3365 max_lockdep_depth = curr->lockdep_depth;
3371 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3374 if (!debug_locks_off())
3376 if (debug_locks_silent)
3380 pr_warn("=====================================\n");
3381 pr_warn("WARNING: bad unlock balance detected!\n");
3382 print_kernel_ident();
3383 pr_warn("-------------------------------------\n");
3384 pr_warn("%s/%d is trying to release lock (",
3385 curr->comm, task_pid_nr(curr));
3386 print_lockdep_cache(lock);
3389 pr_warn("but there are no more locks to release!\n");
3390 pr_warn("\nother info that might help us debug this:\n");
3391 lockdep_print_held_locks(curr);
3393 pr_warn("\nstack backtrace:\n");
3399 static int match_held_lock(const struct held_lock *hlock,
3400 const struct lockdep_map *lock)
3402 if (hlock->instance == lock)
3405 if (hlock->references) {
3406 const struct lock_class *class = lock->class_cache[0];
3409 class = look_up_lock_class(lock, 0);
3412 * If look_up_lock_class() failed to find a class, we're trying
3413 * to test if we hold a lock that has never yet been acquired.
3414 * Clearly if the lock hasn't been acquired _ever_, we're not
3415 * holding it either, so report failure.
3421 * References, but not a lock we're actually ref-counting?
3422 * State got messed up, follow the sites that change ->references
3423 * and try to make sense of it.
3425 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3428 if (hlock->class_idx == class - lock_classes + 1)
3435 /* @depth must not be zero */
3436 static struct held_lock *find_held_lock(struct task_struct *curr,
3437 struct lockdep_map *lock,
3438 unsigned int depth, int *idx)
3440 struct held_lock *ret, *hlock, *prev_hlock;
3444 hlock = curr->held_locks + i;
3446 if (match_held_lock(hlock, lock))
3450 for (i--, prev_hlock = hlock--;
3452 i--, prev_hlock = hlock--) {
3454 * We must not cross into another context:
3456 if (prev_hlock->irq_context != hlock->irq_context) {
3460 if (match_held_lock(hlock, lock)) {
3471 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3474 struct held_lock *hlock;
3476 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3479 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3480 if (!__lock_acquire(hlock->instance,
3481 hlock_class(hlock)->subclass,
3483 hlock->read, hlock->check,
3484 hlock->hardirqs_off,
3485 hlock->nest_lock, hlock->acquire_ip,
3486 hlock->references, hlock->pin_count))
3493 __lock_set_class(struct lockdep_map *lock, const char *name,
3494 struct lock_class_key *key, unsigned int subclass,
3497 struct task_struct *curr = current;
3498 struct held_lock *hlock;
3499 struct lock_class *class;
3503 depth = curr->lockdep_depth;
3505 * This function is about (re)setting the class of a held lock,
3506 * yet we're not actually holding any locks. Naughty user!
3508 if (DEBUG_LOCKS_WARN_ON(!depth))
3511 hlock = find_held_lock(curr, lock, depth, &i);
3513 return print_unlock_imbalance_bug(curr, lock, ip);
3515 lockdep_init_map(lock, name, key, 0);
3516 class = register_lock_class(lock, subclass, 0);
3517 hlock->class_idx = class - lock_classes + 1;
3519 curr->lockdep_depth = i;
3520 curr->curr_chain_key = hlock-&