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>
54 #include <asm/sections.h>
56 #include "lockdep_internals.h"
58 #include <trace/events/preemptirq.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 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
144 static inline struct lock_class *hlock_class(struct held_lock *hlock)
146 if (!hlock->class_idx) {
148 * Someone passed in garbage, we give up.
150 DEBUG_LOCKS_WARN_ON(1);
153 return lock_classes + hlock->class_idx - 1;
156 #ifdef CONFIG_LOCK_STAT
157 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
159 static inline u64 lockstat_clock(void)
161 return local_clock();
164 static int lock_point(unsigned long points[], unsigned long ip)
168 for (i = 0; i < LOCKSTAT_POINTS; i++) {
169 if (points[i] == 0) {
180 static void lock_time_inc(struct lock_time *lt, u64 time)
185 if (time < lt->min || !lt->nr)
192 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
197 if (src->max > dst->max)
200 if (src->min < dst->min || !dst->nr)
203 dst->total += src->total;
207 struct lock_class_stats lock_stats(struct lock_class *class)
209 struct lock_class_stats stats;
212 memset(&stats, 0, sizeof(struct lock_class_stats));
213 for_each_possible_cpu(cpu) {
214 struct lock_class_stats *pcs =
215 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
217 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
218 stats.contention_point[i] += pcs->contention_point[i];
220 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
221 stats.contending_point[i] += pcs->contending_point[i];
223 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
224 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
226 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
227 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
229 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
230 stats.bounces[i] += pcs->bounces[i];
236 void clear_lock_stats(struct lock_class *class)
240 for_each_possible_cpu(cpu) {
241 struct lock_class_stats *cpu_stats =
242 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
244 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
246 memset(class->contention_point, 0, sizeof(class->contention_point));
247 memset(class->contending_point, 0, sizeof(class->contending_point));
250 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
252 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
255 static void lock_release_holdtime(struct held_lock *hlock)
257 struct lock_class_stats *stats;
263 holdtime = lockstat_clock() - hlock->holdtime_stamp;
265 stats = get_lock_stats(hlock_class(hlock));
267 lock_time_inc(&stats->read_holdtime, holdtime);
269 lock_time_inc(&stats->write_holdtime, holdtime);
272 static inline void lock_release_holdtime(struct held_lock *hlock)
278 * We keep a global list of all lock classes. The list only grows,
279 * never shrinks. The list is only accessed with the lockdep
280 * spinlock lock held.
282 LIST_HEAD(all_lock_classes);
285 * The lockdep classes are in a hash-table as well, for fast lookup:
287 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
288 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
289 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
290 #define classhashentry(key) (classhash_table + __classhashfn((key)))
292 static struct hlist_head classhash_table[CLASSHASH_SIZE];
295 * We put the lock dependency chains into a hash-table as well, to cache
298 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
299 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
300 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
301 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
303 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
306 * The hash key of the lock dependency chains is a hash itself too:
307 * it's a hash of all locks taken up to that lock, including that lock.
308 * It's a 64-bit hash, because it's important for the keys to be
311 static inline u64 iterate_chain_key(u64 key, u32 idx)
313 u32 k0 = key, k1 = key >> 32;
315 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
317 return k0 | (u64)k1 << 32;
320 void lockdep_off(void)
322 current->lockdep_recursion++;
324 EXPORT_SYMBOL(lockdep_off);
326 void lockdep_on(void)
328 current->lockdep_recursion--;
330 EXPORT_SYMBOL(lockdep_on);
333 * Debugging switches:
337 #define VERY_VERBOSE 0
340 # define HARDIRQ_VERBOSE 1
341 # define SOFTIRQ_VERBOSE 1
343 # define HARDIRQ_VERBOSE 0
344 # define SOFTIRQ_VERBOSE 0
347 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
349 * Quick filtering for interesting events:
351 static int class_filter(struct lock_class *class)
355 if (class->name_version == 1 &&
356 !strcmp(class->name, "lockname"))
358 if (class->name_version == 1 &&
359 !strcmp(class->name, "&struct->lockfield"))
362 /* Filter everything else. 1 would be to allow everything else */
367 static int verbose(struct lock_class *class)
370 return class_filter(class);
376 * Stack-trace: tightly packed array of stack backtrace
377 * addresses. Protected by the graph_lock.
379 unsigned long nr_stack_trace_entries;
380 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
382 static void print_lockdep_off(const char *bug_msg)
384 printk(KERN_DEBUG "%s\n", bug_msg);
385 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
386 #ifdef CONFIG_LOCK_STAT
387 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
391 static int save_trace(struct stack_trace *trace)
393 trace->nr_entries = 0;
394 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
395 trace->entries = stack_trace + nr_stack_trace_entries;
399 save_stack_trace(trace);
402 * Some daft arches put -1 at the end to indicate its a full trace.
404 * <rant> this is buggy anyway, since it takes a whole extra entry so a
405 * complete trace that maxes out the entries provided will be reported
406 * as incomplete, friggin useless </rant>
408 if (trace->nr_entries != 0 &&
409 trace->entries[trace->nr_entries-1] == ULONG_MAX)
412 trace->max_entries = trace->nr_entries;
414 nr_stack_trace_entries += trace->nr_entries;
416 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
417 if (!debug_locks_off_graph_unlock())
420 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
429 unsigned int nr_hardirq_chains;
430 unsigned int nr_softirq_chains;
431 unsigned int nr_process_chains;
432 unsigned int max_lockdep_depth;
434 #ifdef CONFIG_DEBUG_LOCKDEP
436 * Various lockdep statistics:
438 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
439 DEFINE_PER_CPU(unsigned long [MAX_LOCKDEP_KEYS], lock_class_ops);
446 #define __USAGE(__STATE) \
447 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
448 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
449 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
450 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
452 static const char *usage_str[] =
454 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
455 #include "lockdep_states.h"
457 [LOCK_USED] = "INITIAL USE",
460 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
462 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
465 static inline unsigned long lock_flag(enum lock_usage_bit bit)
470 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
474 if (class->usage_mask & lock_flag(bit + 2))
476 if (class->usage_mask & lock_flag(bit)) {
478 if (class->usage_mask & lock_flag(bit + 2))
485 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
489 #define LOCKDEP_STATE(__STATE) \
490 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
491 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
492 #include "lockdep_states.h"
498 static void __print_lock_name(struct lock_class *class)
500 char str[KSYM_NAME_LEN];
505 name = __get_key_name(class->key, str);
506 printk(KERN_CONT "%s", name);
508 printk(KERN_CONT "%s", name);
509 if (class->name_version > 1)
510 printk(KERN_CONT "#%d", class->name_version);
512 printk(KERN_CONT "/%d", class->subclass);
516 static void print_lock_name(struct lock_class *class)
518 char usage[LOCK_USAGE_CHARS];
520 get_usage_chars(class, usage);
522 printk(KERN_CONT " (");
523 __print_lock_name(class);
524 printk(KERN_CONT "){%s}", usage);
527 static void print_lockdep_cache(struct lockdep_map *lock)
530 char str[KSYM_NAME_LEN];
534 name = __get_key_name(lock->key->subkeys, str);
536 printk(KERN_CONT "%s", name);
539 static void print_lock(struct held_lock *hlock)
542 * We can be called locklessly through debug_show_all_locks() so be
543 * extra careful, the hlock might have been released and cleared.
545 unsigned int class_idx = hlock->class_idx;
547 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
550 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
551 printk(KERN_CONT "<RELEASED>\n");
555 printk(KERN_CONT "%p", hlock->instance);
556 print_lock_name(lock_classes + class_idx - 1);
557 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
560 static void lockdep_print_held_locks(struct task_struct *p)
562 int i, depth = READ_ONCE(p->lockdep_depth);
565 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
567 printk("%d lock%s held by %s/%d:\n", depth,
568 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
570 * It's not reliable to print a task's held locks if it's not sleeping
571 * and it's not the current task.
573 if (p->state == TASK_RUNNING && p != current)
575 for (i = 0; i < depth; i++) {
577 print_lock(p->held_locks + i);
581 static void print_kernel_ident(void)
583 printk("%s %.*s %s\n", init_utsname()->release,
584 (int)strcspn(init_utsname()->version, " "),
585 init_utsname()->version,
589 static int very_verbose(struct lock_class *class)
592 return class_filter(class);
598 * Is this the address of a static object:
601 static int static_obj(void *obj)
603 unsigned long start = (unsigned long) &_stext,
604 end = (unsigned long) &_end,
605 addr = (unsigned long) obj;
610 if ((addr >= start) && (addr < end))
613 if (arch_is_kernel_data(addr))
617 * in-kernel percpu var?
619 if (is_kernel_percpu_address(addr))
623 * module static or percpu var?
625 return is_module_address(addr) || is_module_percpu_address(addr);
630 * To make lock name printouts unique, we calculate a unique
631 * class->name_version generation counter:
633 static int count_matching_names(struct lock_class *new_class)
635 struct lock_class *class;
638 if (!new_class->name)
641 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
642 if (new_class->key - new_class->subclass == class->key)
643 return class->name_version;
644 if (class->name && !strcmp(class->name, new_class->name))
645 count = max(count, class->name_version);
651 static inline struct lock_class *
652 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
654 struct lockdep_subclass_key *key;
655 struct hlist_head *hash_head;
656 struct lock_class *class;
658 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
661 "BUG: looking up invalid subclass: %u\n", subclass);
663 "turning off the locking correctness validator.\n");
669 * If it is not initialised then it has never been locked,
670 * so it won't be present in the hash table.
672 if (unlikely(!lock->key))
676 * NOTE: the class-key must be unique. For dynamic locks, a static
677 * lock_class_key variable is passed in through the mutex_init()
678 * (or spin_lock_init()) call - which acts as the key. For static
679 * locks we use the lock object itself as the key.
681 BUILD_BUG_ON(sizeof(struct lock_class_key) >
682 sizeof(struct lockdep_map));
684 key = lock->key->subkeys + subclass;
686 hash_head = classhashentry(key);
689 * We do an RCU walk of the hash, see lockdep_free_key_range().
691 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
694 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
695 if (class->key == key) {
697 * Huh! same key, different name? Did someone trample
698 * on some memory? We're most confused.
700 WARN_ON_ONCE(class->name != lock->name);
709 * Static locks do not have their class-keys yet - for them the key is
710 * the lock object itself. If the lock is in the per cpu area, the
711 * canonical address of the lock (per cpu offset removed) is used.
713 static bool assign_lock_key(struct lockdep_map *lock)
715 unsigned long can_addr, addr = (unsigned long)lock;
717 if (__is_kernel_percpu_address(addr, &can_addr))
718 lock->key = (void *)can_addr;
719 else if (__is_module_percpu_address(addr, &can_addr))
720 lock->key = (void *)can_addr;
721 else if (static_obj(lock))
722 lock->key = (void *)lock;
724 /* Debug-check: all keys must be persistent! */
726 pr_err("INFO: trying to register non-static key.\n");
727 pr_err("the code is fine but needs lockdep annotation.\n");
728 pr_err("turning off the locking correctness validator.\n");
737 * Register a lock's class in the hash-table, if the class is not present
738 * yet. Otherwise we look it up. We cache the result in the lock object
739 * itself, so actual lookup of the hash should be once per lock object.
741 static struct lock_class *
742 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
744 struct lockdep_subclass_key *key;
745 struct hlist_head *hash_head;
746 struct lock_class *class;
748 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
750 class = look_up_lock_class(lock, subclass);
752 goto out_set_class_cache;
755 if (!assign_lock_key(lock))
757 } else if (!static_obj(lock->key)) {
761 key = lock->key->subkeys + subclass;
762 hash_head = classhashentry(key);
768 * We have to do the hash-walk again, to avoid races
771 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
772 if (class->key == key)
777 * Allocate a new key from the static array, and add it to
780 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
781 if (!debug_locks_off_graph_unlock()) {
785 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
789 class = lock_classes + nr_lock_classes++;
790 debug_atomic_inc(nr_unused_locks);
792 class->name = lock->name;
793 class->subclass = subclass;
794 INIT_LIST_HEAD(&class->lock_entry);
795 INIT_LIST_HEAD(&class->locks_before);
796 INIT_LIST_HEAD(&class->locks_after);
797 class->name_version = count_matching_names(class);
799 * We use RCU's safe list-add method to make
800 * parallel walking of the hash-list safe:
802 hlist_add_head_rcu(&class->hash_entry, hash_head);
804 * Add it to the global list of classes:
806 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
808 if (verbose(class)) {
811 printk("\nnew class %px: %s", class->key, class->name);
812 if (class->name_version > 1)
813 printk(KERN_CONT "#%d", class->name_version);
814 printk(KERN_CONT "\n");
825 if (!subclass || force)
826 lock->class_cache[0] = class;
827 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
828 lock->class_cache[subclass] = class;
831 * Hash collision, did we smoke some? We found a class with a matching
832 * hash but the subclass -- which is hashed in -- didn't match.
834 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
840 #ifdef CONFIG_PROVE_LOCKING
842 * Allocate a lockdep entry. (assumes the graph_lock held, returns
843 * with NULL on failure)
845 static struct lock_list *alloc_list_entry(void)
847 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
848 if (!debug_locks_off_graph_unlock())
851 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
855 return list_entries + nr_list_entries++;
859 * Add a new dependency to the head of the list:
861 static int add_lock_to_list(struct lock_class *this, struct list_head *head,
862 unsigned long ip, int distance,
863 struct stack_trace *trace)
865 struct lock_list *entry;
867 * Lock not present yet - get a new dependency struct and
868 * add it to the list:
870 entry = alloc_list_entry();
875 entry->distance = distance;
876 entry->trace = *trace;
878 * Both allocation and removal are done under the graph lock; but
879 * iteration is under RCU-sched; see look_up_lock_class() and
880 * lockdep_free_key_range().
882 list_add_tail_rcu(&entry->entry, head);
888 * For good efficiency of modular, we use power of 2
890 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
891 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
894 * The circular_queue and helpers is used to implement the
895 * breadth-first search(BFS)algorithem, by which we can build
896 * the shortest path from the next lock to be acquired to the
897 * previous held lock if there is a circular between them.
899 struct circular_queue {
900 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
901 unsigned int front, rear;
904 static struct circular_queue lock_cq;
906 unsigned int max_bfs_queue_depth;
908 static unsigned int lockdep_dependency_gen_id;
910 static inline void __cq_init(struct circular_queue *cq)
912 cq->front = cq->rear = 0;
913 lockdep_dependency_gen_id++;
916 static inline int __cq_empty(struct circular_queue *cq)
918 return (cq->front == cq->rear);
921 static inline int __cq_full(struct circular_queue *cq)
923 return ((cq->rear + 1) & CQ_MASK) == cq->front;
926 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
931 cq->element[cq->rear] = elem;
932 cq->rear = (cq->rear + 1) & CQ_MASK;
936 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
941 *elem = cq->element[cq->front];
942 cq->front = (cq->front + 1) & CQ_MASK;
946 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
948 return (cq->rear - cq->front) & CQ_MASK;
951 static inline void mark_lock_accessed(struct lock_list *lock,
952 struct lock_list *parent)
956 nr = lock - list_entries;
957 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
958 lock->parent = parent;
959 lock->class->dep_gen_id = lockdep_dependency_gen_id;
962 static inline unsigned long lock_accessed(struct lock_list *lock)
966 nr = lock - list_entries;
967 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
968 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
971 static inline struct lock_list *get_lock_parent(struct lock_list *child)
973 return child->parent;
976 static inline int get_lock_depth(struct lock_list *child)
979 struct lock_list *parent;
981 while ((parent = get_lock_parent(child))) {
988 static int __bfs(struct lock_list *source_entry,
990 int (*match)(struct lock_list *entry, void *data),
991 struct lock_list **target_entry,
994 struct lock_list *entry;
995 struct list_head *head;
996 struct circular_queue *cq = &lock_cq;
999 if (match(source_entry, data)) {
1000 *target_entry = source_entry;
1006 head = &source_entry->class->locks_after;
1008 head = &source_entry->class->locks_before;
1010 if (list_empty(head))
1014 __cq_enqueue(cq, (unsigned long)source_entry);
1016 while (!__cq_empty(cq)) {
1017 struct lock_list *lock;
1019 __cq_dequeue(cq, (unsigned long *)&lock);
1027 head = &lock->class->locks_after;
1029 head = &lock->class->locks_before;
1031 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1033 list_for_each_entry_rcu(entry, head, entry) {
1034 if (!lock_accessed(entry)) {
1035 unsigned int cq_depth;
1036 mark_lock_accessed(entry, lock);
1037 if (match(entry, data)) {
1038 *target_entry = entry;
1043 if (__cq_enqueue(cq, (unsigned long)entry)) {
1047 cq_depth = __cq_get_elem_count(cq);
1048 if (max_bfs_queue_depth < cq_depth)
1049 max_bfs_queue_depth = cq_depth;
1057 static inline int __bfs_forwards(struct lock_list *src_entry,
1059 int (*match)(struct lock_list *entry, void *data),
1060 struct lock_list **target_entry)
1062 return __bfs(src_entry, data, match, target_entry, 1);
1066 static inline int __bfs_backwards(struct lock_list *src_entry,
1068 int (*match)(struct lock_list *entry, void *data),
1069 struct lock_list **target_entry)
1071 return __bfs(src_entry, data, match, target_entry, 0);
1076 * Recursive, forwards-direction lock-dependency checking, used for
1077 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1082 * Print a dependency chain entry (this is only done when a deadlock
1083 * has been detected):
1086 print_circular_bug_entry(struct lock_list *target, int depth)
1088 if (debug_locks_silent)
1090 printk("\n-> #%u", depth);
1091 print_lock_name(target->class);
1092 printk(KERN_CONT ":\n");
1093 print_stack_trace(&target->trace, 6);
1099 print_circular_lock_scenario(struct held_lock *src,
1100 struct held_lock *tgt,
1101 struct lock_list *prt)
1103 struct lock_class *source = hlock_class(src);
1104 struct lock_class *target = hlock_class(tgt);
1105 struct lock_class *parent = prt->class;
1108 * A direct locking problem where unsafe_class lock is taken
1109 * directly by safe_class lock, then all we need to show
1110 * is the deadlock scenario, as it is obvious that the
1111 * unsafe lock is taken under the safe lock.
1113 * But if there is a chain instead, where the safe lock takes
1114 * an intermediate lock (middle_class) where this lock is
1115 * not the same as the safe lock, then the lock chain is
1116 * used to describe the problem. Otherwise we would need
1117 * to show a different CPU case for each link in the chain
1118 * from the safe_class lock to the unsafe_class lock.
1120 if (parent != source) {
1121 printk("Chain exists of:\n ");
1122 __print_lock_name(source);
1123 printk(KERN_CONT " --> ");
1124 __print_lock_name(parent);
1125 printk(KERN_CONT " --> ");
1126 __print_lock_name(target);
1127 printk(KERN_CONT "\n\n");
1130 printk(" Possible unsafe locking scenario:\n\n");
1131 printk(" CPU0 CPU1\n");
1132 printk(" ---- ----\n");
1134 __print_lock_name(target);
1135 printk(KERN_CONT ");\n");
1137 __print_lock_name(parent);
1138 printk(KERN_CONT ");\n");
1140 __print_lock_name(target);
1141 printk(KERN_CONT ");\n");
1143 __print_lock_name(source);
1144 printk(KERN_CONT ");\n");
1145 printk("\n *** DEADLOCK ***\n\n");
1149 * When a circular dependency is detected, print the
1153 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1154 struct held_lock *check_src,
1155 struct held_lock *check_tgt)
1157 struct task_struct *curr = current;
1159 if (debug_locks_silent)
1163 pr_warn("======================================================\n");
1164 pr_warn("WARNING: possible circular locking dependency detected\n");
1165 print_kernel_ident();
1166 pr_warn("------------------------------------------------------\n");
1167 pr_warn("%s/%d is trying to acquire lock:\n",
1168 curr->comm, task_pid_nr(curr));
1169 print_lock(check_src);
1171 pr_warn("\nbut task is already holding lock:\n");
1173 print_lock(check_tgt);
1174 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1175 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1177 print_circular_bug_entry(entry, depth);
1182 static inline int class_equal(struct lock_list *entry, void *data)
1184 return entry->class == data;
1187 static noinline int print_circular_bug(struct lock_list *this,
1188 struct lock_list *target,
1189 struct held_lock *check_src,
1190 struct held_lock *check_tgt,
1191 struct stack_trace *trace)
1193 struct task_struct *curr = current;
1194 struct lock_list *parent;
1195 struct lock_list *first_parent;
1198 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1201 if (!save_trace(&this->trace))
1204 depth = get_lock_depth(target);
1206 print_circular_bug_header(target, depth, check_src, check_tgt);
1208 parent = get_lock_parent(target);
1209 first_parent = parent;
1212 print_circular_bug_entry(parent, --depth);
1213 parent = get_lock_parent(parent);
1216 printk("\nother info that might help us debug this:\n\n");
1217 print_circular_lock_scenario(check_src, check_tgt,
1220 lockdep_print_held_locks(curr);
1222 printk("\nstack backtrace:\n");
1228 static noinline int print_bfs_bug(int ret)
1230 if (!debug_locks_off_graph_unlock())
1234 * Breadth-first-search failed, graph got corrupted?
1236 WARN(1, "lockdep bfs error:%d\n", ret);
1241 static int noop_count(struct lock_list *entry, void *data)
1243 (*(unsigned long *)data)++;
1247 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1249 unsigned long count = 0;
1250 struct lock_list *uninitialized_var(target_entry);
1252 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1256 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1258 unsigned long ret, flags;
1259 struct lock_list this;
1264 raw_local_irq_save(flags);
1265 arch_spin_lock(&lockdep_lock);
1266 ret = __lockdep_count_forward_deps(&this);
1267 arch_spin_unlock(&lockdep_lock);
1268 raw_local_irq_restore(flags);
1273 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1275 unsigned long count = 0;
1276 struct lock_list *uninitialized_var(target_entry);
1278 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1283 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1285 unsigned long ret, flags;
1286 struct lock_list this;
1291 raw_local_irq_save(flags);
1292 arch_spin_lock(&lockdep_lock);
1293 ret = __lockdep_count_backward_deps(&this);
1294 arch_spin_unlock(&lockdep_lock);
1295 raw_local_irq_restore(flags);
1301 * Prove that the dependency graph starting at <entry> can not
1302 * lead to <target>. Print an error and return 0 if it does.
1305 check_noncircular(struct lock_list *root, struct lock_class *target,
1306 struct lock_list **target_entry)
1310 debug_atomic_inc(nr_cyclic_checks);
1312 result = __bfs_forwards(root, target, class_equal, target_entry);
1318 check_redundant(struct lock_list *root, struct lock_class *target,
1319 struct lock_list **target_entry)
1323 debug_atomic_inc(nr_redundant_checks);
1325 result = __bfs_forwards(root, target, class_equal, target_entry);
1330 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1332 * Forwards and backwards subgraph searching, for the purposes of
1333 * proving that two subgraphs can be connected by a new dependency
1334 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1337 static inline int usage_match(struct lock_list *entry, void *bit)
1339 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1345 * Find a node in the forwards-direction dependency sub-graph starting
1346 * at @root->class that matches @bit.
1348 * Return 0 if such a node exists in the subgraph, and put that node
1349 * into *@target_entry.
1351 * Return 1 otherwise and keep *@target_entry unchanged.
1352 * Return <0 on error.
1355 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1356 struct lock_list **target_entry)
1360 debug_atomic_inc(nr_find_usage_forwards_checks);
1362 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1368 * Find a node in the backwards-direction dependency sub-graph starting
1369 * at @root->class that matches @bit.
1371 * Return 0 if such a node exists in the subgraph, and put that node
1372 * into *@target_entry.
1374 * Return 1 otherwise and keep *@target_entry unchanged.
1375 * Return <0 on error.
1378 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1379 struct lock_list **target_entry)
1383 debug_atomic_inc(nr_find_usage_backwards_checks);
1385 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1390 static void print_lock_class_header(struct lock_class *class, int depth)
1394 printk("%*s->", depth, "");
1395 print_lock_name(class);
1396 #ifdef CONFIG_DEBUG_LOCKDEP
1397 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
1399 printk(KERN_CONT " {\n");
1401 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1402 if (class->usage_mask & (1 << bit)) {
1405 len += printk("%*s %s", depth, "", usage_str[bit]);
1406 len += printk(KERN_CONT " at:\n");
1407 print_stack_trace(class->usage_traces + bit, len);
1410 printk("%*s }\n", depth, "");
1412 printk("%*s ... key at: [<%px>] %pS\n",
1413 depth, "", class->key, class->key);
1417 * printk the shortest lock dependencies from @start to @end in reverse order:
1420 print_shortest_lock_dependencies(struct lock_list *leaf,
1421 struct lock_list *root)
1423 struct lock_list *entry = leaf;
1426 /*compute depth from generated tree by BFS*/
1427 depth = get_lock_depth(leaf);
1430 print_lock_class_header(entry->class, depth);
1431 printk("%*s ... acquired at:\n", depth, "");
1432 print_stack_trace(&entry->trace, 2);
1435 if (depth == 0 && (entry != root)) {
1436 printk("lockdep:%s bad path found in chain graph\n", __func__);
1440 entry = get_lock_parent(entry);
1442 } while (entry && (depth >= 0));
1448 print_irq_lock_scenario(struct lock_list *safe_entry,
1449 struct lock_list *unsafe_entry,
1450 struct lock_class *prev_class,
1451 struct lock_class *next_class)
1453 struct lock_class *safe_class = safe_entry->class;
1454 struct lock_class *unsafe_class = unsafe_entry->class;
1455 struct lock_class *middle_class = prev_class;
1457 if (middle_class == safe_class)
1458 middle_class = next_class;
1461 * A direct locking problem where unsafe_class lock is taken
1462 * directly by safe_class lock, then all we need to show
1463 * is the deadlock scenario, as it is obvious that the
1464 * unsafe lock is taken under the safe lock.
1466 * But if there is a chain instead, where the safe lock takes
1467 * an intermediate lock (middle_class) where this lock is
1468 * not the same as the safe lock, then the lock chain is
1469 * used to describe the problem. Otherwise we would need
1470 * to show a different CPU case for each link in the chain
1471 * from the safe_class lock to the unsafe_class lock.
1473 if (middle_class != unsafe_class) {
1474 printk("Chain exists of:\n ");
1475 __print_lock_name(safe_class);
1476 printk(KERN_CONT " --> ");
1477 __print_lock_name(middle_class);
1478 printk(KERN_CONT " --> ");
1479 __print_lock_name(unsafe_class);
1480 printk(KERN_CONT "\n\n");
1483 printk(" Possible interrupt unsafe locking scenario:\n\n");
1484 printk(" CPU0 CPU1\n");
1485 printk(" ---- ----\n");
1487 __print_lock_name(unsafe_class);
1488 printk(KERN_CONT ");\n");
1489 printk(" local_irq_disable();\n");
1491 __print_lock_name(safe_class);
1492 printk(KERN_CONT ");\n");
1494 __print_lock_name(middle_class);
1495 printk(KERN_CONT ");\n");
1496 printk(" <Interrupt>\n");
1498 __print_lock_name(safe_class);
1499 printk(KERN_CONT ");\n");
1500 printk("\n *** DEADLOCK ***\n\n");
1504 print_bad_irq_dependency(struct task_struct *curr,
1505 struct lock_list *prev_root,
1506 struct lock_list *next_root,
1507 struct lock_list *backwards_entry,
1508 struct lock_list *forwards_entry,
1509 struct held_lock *prev,
1510 struct held_lock *next,
1511 enum lock_usage_bit bit1,
1512 enum lock_usage_bit bit2,
1513 const char *irqclass)
1515 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1519 pr_warn("=====================================================\n");
1520 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1521 irqclass, irqclass);
1522 print_kernel_ident();
1523 pr_warn("-----------------------------------------------------\n");
1524 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1525 curr->comm, task_pid_nr(curr),
1526 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1527 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1528 curr->hardirqs_enabled,
1529 curr->softirqs_enabled);
1532 pr_warn("\nand this task is already holding:\n");
1534 pr_warn("which would create a new lock dependency:\n");
1535 print_lock_name(hlock_class(prev));
1537 print_lock_name(hlock_class(next));
1540 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1542 print_lock_name(backwards_entry->class);
1543 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
1545 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1547 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
1548 print_lock_name(forwards_entry->class);
1549 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1552 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1554 pr_warn("\nother info that might help us debug this:\n\n");
1555 print_irq_lock_scenario(backwards_entry, forwards_entry,
1556 hlock_class(prev), hlock_class(next));
1558 lockdep_print_held_locks(curr);
1560 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
1561 if (!save_trace(&prev_root->trace))
1563 print_shortest_lock_dependencies(backwards_entry, prev_root);
1565 pr_warn("\nthe dependencies between the lock to be acquired");
1566 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
1567 if (!save_trace(&next_root->trace))
1569 print_shortest_lock_dependencies(forwards_entry, next_root);
1571 pr_warn("\nstack backtrace:\n");
1578 check_usage(struct task_struct *curr, struct held_lock *prev,
1579 struct held_lock *next, enum lock_usage_bit bit_backwards,
1580 enum lock_usage_bit bit_forwards, const char *irqclass)
1583 struct lock_list this, that;
1584 struct lock_list *uninitialized_var(target_entry);
1585 struct lock_list *uninitialized_var(target_entry1);
1589 this.class = hlock_class(prev);
1590 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1592 return print_bfs_bug(ret);
1597 that.class = hlock_class(next);
1598 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1600 return print_bfs_bug(ret);
1604 return print_bad_irq_dependency(curr, &this, &that,
1605 target_entry, target_entry1,
1607 bit_backwards, bit_forwards, irqclass);
1610 static const char *state_names[] = {
1611 #define LOCKDEP_STATE(__STATE) \
1612 __stringify(__STATE),
1613 #include "lockdep_states.h"
1614 #undef LOCKDEP_STATE
1617 static const char *state_rnames[] = {
1618 #define LOCKDEP_STATE(__STATE) \
1619 __stringify(__STATE)"-READ",
1620 #include "lockdep_states.h"
1621 #undef LOCKDEP_STATE
1624 static inline const char *state_name(enum lock_usage_bit bit)
1626 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1629 static int exclusive_bit(int new_bit)
1637 * bit 0 - write/read
1638 * bit 1 - used_in/enabled
1642 int state = new_bit & ~3;
1643 int dir = new_bit & 2;
1646 * keep state, bit flip the direction and strip read.
1648 return state | (dir ^ 2);
1651 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1652 struct held_lock *next, enum lock_usage_bit bit)
1655 * Prove that the new dependency does not connect a hardirq-safe
1656 * lock with a hardirq-unsafe lock - to achieve this we search
1657 * the backwards-subgraph starting at <prev>, and the
1658 * forwards-subgraph starting at <next>:
1660 if (!check_usage(curr, prev, next, bit,
1661 exclusive_bit(bit), state_name(bit)))
1667 * Prove that the new dependency does not connect a hardirq-safe-read
1668 * lock with a hardirq-unsafe lock - to achieve this we search
1669 * the backwards-subgraph starting at <prev>, and the
1670 * forwards-subgraph starting at <next>:
1672 if (!check_usage(curr, prev, next, bit,
1673 exclusive_bit(bit), state_name(bit)))
1680 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1681 struct held_lock *next)
1683 #define LOCKDEP_STATE(__STATE) \
1684 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1686 #include "lockdep_states.h"
1687 #undef LOCKDEP_STATE
1692 static void inc_chains(void)
1694 if (current->hardirq_context)
1695 nr_hardirq_chains++;
1697 if (current->softirq_context)
1698 nr_softirq_chains++;
1700 nr_process_chains++;
1707 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1708 struct held_lock *next)
1713 static inline void inc_chains(void)
1715 nr_process_chains++;
1721 print_deadlock_scenario(struct held_lock *nxt,
1722 struct held_lock *prv)
1724 struct lock_class *next = hlock_class(nxt);
1725 struct lock_class *prev = hlock_class(prv);
1727 printk(" Possible unsafe locking scenario:\n\n");
1731 __print_lock_name(prev);
1732 printk(KERN_CONT ");\n");
1734 __print_lock_name(next);
1735 printk(KERN_CONT ");\n");
1736 printk("\n *** DEADLOCK ***\n\n");
1737 printk(" May be due to missing lock nesting notation\n\n");
1741 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1742 struct held_lock *next)
1744 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1748 pr_warn("============================================\n");
1749 pr_warn("WARNING: possible recursive locking detected\n");
1750 print_kernel_ident();
1751 pr_warn("--------------------------------------------\n");
1752 pr_warn("%s/%d is trying to acquire lock:\n",
1753 curr->comm, task_pid_nr(curr));
1755 pr_warn("\nbut task is already holding lock:\n");
1758 pr_warn("\nother info that might help us debug this:\n");
1759 print_deadlock_scenario(next, prev);
1760 lockdep_print_held_locks(curr);
1762 pr_warn("\nstack backtrace:\n");
1769 * Check whether we are holding such a class already.
1771 * (Note that this has to be done separately, because the graph cannot
1772 * detect such classes of deadlocks.)
1774 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1777 check_deadlock(struct task_struct *curr, struct held_lock *next,
1778 struct lockdep_map *next_instance, int read)
1780 struct held_lock *prev;
1781 struct held_lock *nest = NULL;
1784 for (i = 0; i < curr->lockdep_depth; i++) {
1785 prev = curr->held_locks + i;
1787 if (prev->instance == next->nest_lock)
1790 if (hlock_class(prev) != hlock_class(next))
1794 * Allow read-after-read recursion of the same
1795 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1797 if ((read == 2) && prev->read)
1801 * We're holding the nest_lock, which serializes this lock's
1802 * nesting behaviour.
1807 return print_deadlock_bug(curr, prev, next);
1813 * There was a chain-cache miss, and we are about to add a new dependency
1814 * to a previous lock. We recursively validate the following rules:
1816 * - would the adding of the <prev> -> <next> dependency create a
1817 * circular dependency in the graph? [== circular deadlock]
1819 * - does the new prev->next dependency connect any hardirq-safe lock
1820 * (in the full backwards-subgraph starting at <prev>) with any
1821 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1822 * <next>)? [== illegal lock inversion with hardirq contexts]
1824 * - does the new prev->next dependency connect any softirq-safe lock
1825 * (in the full backwards-subgraph starting at <prev>) with any
1826 * softirq-unsafe lock (in the full forwards-subgraph starting at
1827 * <next>)? [== illegal lock inversion with softirq contexts]
1829 * any of these scenarios could lead to a deadlock.
1831 * Then if all the validations pass, we add the forwards and backwards
1835 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1836 struct held_lock *next, int distance, struct stack_trace *trace,
1837 int (*save)(struct stack_trace *trace))
1839 struct lock_list *uninitialized_var(target_entry);
1840 struct lock_list *entry;
1841 struct lock_list this;
1845 * Prove that the new <prev> -> <next> dependency would not
1846 * create a circular dependency in the graph. (We do this by
1847 * forward-recursing into the graph starting at <next>, and
1848 * checking whether we can reach <prev>.)
1850 * We are using global variables to control the recursion, to
1851 * keep the stackframe size of the recursive functions low:
1853 this.class = hlock_class(next);
1855 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1856 if (unlikely(!ret)) {
1857 if (!trace->entries) {
1859 * If @save fails here, the printing might trigger
1860 * a WARN but because of the !nr_entries it should
1861 * not do bad things.
1865 return print_circular_bug(&this, target_entry, next, prev, trace);
1867 else if (unlikely(ret < 0))
1868 return print_bfs_bug(ret);
1870 if (!check_prev_add_irq(curr, prev, next))
1874 * For recursive read-locks we do all the dependency checks,
1875 * but we dont store read-triggered dependencies (only
1876 * write-triggered dependencies). This ensures that only the
1877 * write-side dependencies matter, and that if for example a
1878 * write-lock never takes any other locks, then the reads are
1879 * equivalent to a NOP.
1881 if (next->read == 2 || prev->read == 2)
1884 * Is the <prev> -> <next> dependency already present?
1886 * (this may occur even though this is a new chain: consider
1887 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1888 * chains - the second one will be new, but L1 already has
1889 * L2 added to its dependency list, due to the first chain.)
1891 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1892 if (entry->class == hlock_class(next)) {
1894 entry->distance = 1;
1900 * Is the <prev> -> <next> link redundant?
1902 this.class = hlock_class(prev);
1904 ret = check_redundant(&this, hlock_class(next), &target_entry);
1906 debug_atomic_inc(nr_redundant);
1910 return print_bfs_bug(ret);
1913 if (!trace->entries && !save(trace))
1917 * Ok, all validations passed, add the new lock
1918 * to the previous lock's dependency list:
1920 ret = add_lock_to_list(hlock_class(next),
1921 &hlock_class(prev)->locks_after,
1922 next->acquire_ip, distance, trace);
1927 ret = add_lock_to_list(hlock_class(prev),
1928 &hlock_class(next)->locks_before,
1929 next->acquire_ip, distance, trace);
1937 * Add the dependency to all directly-previous locks that are 'relevant'.
1938 * The ones that are relevant are (in increasing distance from curr):
1939 * all consecutive trylock entries and the final non-trylock entry - or
1940 * the end of this context's lock-chain - whichever comes first.
1943 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1945 int depth = curr->lockdep_depth;
1946 struct held_lock *hlock;
1947 struct stack_trace trace = {
1957 * Depth must not be zero for a non-head lock:
1962 * At least two relevant locks must exist for this
1965 if (curr->held_locks[depth].irq_context !=
1966 curr->held_locks[depth-1].irq_context)
1970 int distance = curr->lockdep_depth - depth + 1;
1971 hlock = curr->held_locks + depth - 1;
1974 * Only non-recursive-read entries get new dependencies
1977 if (hlock->read != 2 && hlock->check) {
1978 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace);
1983 * Stop after the first non-trylock entry,
1984 * as non-trylock entries have added their
1985 * own direct dependencies already, so this
1986 * lock is connected to them indirectly:
1988 if (!hlock->trylock)
1994 * End of lock-stack?
1999 * Stop the search if we cross into another context:
2001 if (curr->held_locks[depth].irq_context !=
2002 curr->held_locks[depth-1].irq_context)
2007 if (!debug_locks_off_graph_unlock())
2011 * Clearly we all shouldn't be here, but since we made it we
2012 * can reliable say we messed up our state. See the above two
2013 * gotos for reasons why we could possibly end up here.
2020 unsigned long nr_lock_chains;
2021 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2022 int nr_chain_hlocks;
2023 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2025 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2027 return lock_classes + chain_hlocks[chain->base + i];
2031 * Returns the index of the first held_lock of the current chain
2033 static inline int get_first_held_lock(struct task_struct *curr,
2034 struct held_lock *hlock)
2037 struct held_lock *hlock_curr;
2039 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2040 hlock_curr = curr->held_locks + i;
2041 if (hlock_curr->irq_context != hlock->irq_context)
2049 #ifdef CONFIG_DEBUG_LOCKDEP
2051 * Returns the next chain_key iteration
2053 static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2055 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2057 printk(" class_idx:%d -> chain_key:%016Lx",
2059 (unsigned long long)new_chain_key);
2060 return new_chain_key;
2064 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2066 struct held_lock *hlock;
2068 int depth = curr->lockdep_depth;
2071 printk("depth: %u\n", depth + 1);
2072 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2073 hlock = curr->held_locks + i;
2074 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2079 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2080 print_lock(hlock_next);
2083 static void print_chain_keys_chain(struct lock_chain *chain)
2089 printk("depth: %u\n", chain->depth);
2090 for (i = 0; i < chain->depth; i++) {
2091 class_id = chain_hlocks[chain->base + i];
2092 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2094 print_lock_name(lock_classes + class_id);
2099 static void print_collision(struct task_struct *curr,
2100 struct held_lock *hlock_next,
2101 struct lock_chain *chain)
2104 pr_warn("============================\n");
2105 pr_warn("WARNING: chain_key collision\n");
2106 print_kernel_ident();
2107 pr_warn("----------------------------\n");
2108 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2109 pr_warn("Hash chain already cached but the contents don't match!\n");
2111 pr_warn("Held locks:");
2112 print_chain_keys_held_locks(curr, hlock_next);
2114 pr_warn("Locks in cached chain:");
2115 print_chain_keys_chain(chain);
2117 pr_warn("\nstack backtrace:\n");
2123 * Checks whether the chain and the current held locks are consistent
2124 * in depth and also in content. If they are not it most likely means
2125 * that there was a collision during the calculation of the chain_key.
2126 * Returns: 0 not passed, 1 passed
2128 static int check_no_collision(struct task_struct *curr,
2129 struct held_lock *hlock,
2130 struct lock_chain *chain)
2132 #ifdef CONFIG_DEBUG_LOCKDEP
2135 i = get_first_held_lock(curr, hlock);
2137 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2138 print_collision(curr, hlock, chain);
2142 for (j = 0; j < chain->depth - 1; j++, i++) {
2143 id = curr->held_locks[i].class_idx - 1;
2145 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2146 print_collision(curr, hlock, chain);
2155 * Adds a dependency chain into chain hashtable. And must be called with
2158 * Return 0 if fail, and graph_lock is released.
2159 * Return 1 if succeed, with graph_lock held.
2161 static inline int add_chain_cache(struct task_struct *curr,
2162 struct held_lock *hlock,
2165 struct lock_class *class = hlock_class(hlock);
2166 struct hlist_head *hash_head = chainhashentry(chain_key);
2167 struct lock_chain *chain;
2171 * Allocate a new chain entry from the static array, and add
2176 * We might need to take the graph lock, ensure we've got IRQs
2177 * disabled to make this an IRQ-safe lock.. for recursion reasons
2178 * lockdep won't complain about its own locking errors.
2180 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2183 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2184 if (!debug_locks_off_graph_unlock())
2187 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2191 chain = lock_chains + nr_lock_chains++;
2192 chain->chain_key = chain_key;
2193 chain->irq_context = hlock->irq_context;
2194 i = get_first_held_lock(curr, hlock);
2195 chain->depth = curr->lockdep_depth + 1 - i;
2197 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2198 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2199 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2201 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2202 chain->base = nr_chain_hlocks;
2203 for (j = 0; j < chain->depth - 1; j++, i++) {
2204 int lock_id = curr->held_locks[i].class_idx - 1;
2205 chain_hlocks[chain->base + j] = lock_id;
2207 chain_hlocks[chain->base + j] = class - lock_classes;
2210 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2211 nr_chain_hlocks += chain->depth;
2213 #ifdef CONFIG_DEBUG_LOCKDEP
2215 * Important for check_no_collision().
2217 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
2218 if (!debug_locks_off_graph_unlock())
2221 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2227 hlist_add_head_rcu(&chain->entry, hash_head);
2228 debug_atomic_inc(chain_lookup_misses);
2235 * Look up a dependency chain.
2237 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2239 struct hlist_head *hash_head = chainhashentry(chain_key);
2240 struct lock_chain *chain;
2243 * We can walk it lock-free, because entries only get added
2246 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2247 if (chain->chain_key == chain_key) {
2248 debug_atomic_inc(chain_lookup_hits);
2256 * If the key is not present yet in dependency chain cache then
2257 * add it and return 1 - in this case the new dependency chain is
2258 * validated. If the key is already hashed, return 0.
2259 * (On return with 1 graph_lock is held.)
2261 static inline int lookup_chain_cache_add(struct task_struct *curr,
2262 struct held_lock *hlock,
2265 struct lock_class *class = hlock_class(hlock);
2266 struct lock_chain *chain = lookup_chain_cache(chain_key);
2270 if (!check_no_collision(curr, hlock, chain))
2273 if (very_verbose(class)) {
2274 printk("\nhash chain already cached, key: "
2275 "%016Lx tail class: [%px] %s\n",
2276 (unsigned long long)chain_key,
2277 class->key, class->name);
2283 if (very_verbose(class)) {
2284 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
2285 (unsigned long long)chain_key, class->key, class->name);
2292 * We have to walk the chain again locked - to avoid duplicates:
2294 chain = lookup_chain_cache(chain_key);
2300 if (!add_chain_cache(curr, hlock, chain_key))
2306 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2307 struct held_lock *hlock, int chain_head, u64 chain_key)
2310 * Trylock needs to maintain the stack of held locks, but it
2311 * does not add new dependencies, because trylock can be done
2314 * We look up the chain_key and do the O(N^2) check and update of
2315 * the dependencies only if this is a new dependency chain.
2316 * (If lookup_chain_cache_add() return with 1 it acquires
2317 * graph_lock for us)
2319 if (!hlock->trylock && hlock->check &&
2320 lookup_chain_cache_add(curr, hlock, chain_key)) {
2322 * Check whether last held lock:
2324 * - is irq-safe, if this lock is irq-unsafe
2325 * - is softirq-safe, if this lock is hardirq-unsafe
2327 * And check whether the new lock's dependency graph
2328 * could lead back to the previous lock.
2330 * any of these scenarios could lead to a deadlock. If
2333 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2338 * Mark recursive read, as we jump over it when
2339 * building dependencies (just like we jump over
2345 * Add dependency only if this lock is not the head
2346 * of the chain, and if it's not a secondary read-lock:
2348 if (!chain_head && ret != 2) {
2349 if (!check_prevs_add(curr, hlock))
2355 /* after lookup_chain_cache_add(): */
2356 if (unlikely(!debug_locks))
2363 static inline int validate_chain(struct task_struct *curr,
2364 struct lockdep_map *lock, struct held_lock *hlock,
2365 int chain_head, u64 chain_key)
2372 * We are building curr_chain_key incrementally, so double-check
2373 * it from scratch, to make sure that it's done correctly:
2375 static void check_chain_key(struct task_struct *curr)
2377 #ifdef CONFIG_DEBUG_LOCKDEP
2378 struct held_lock *hlock, *prev_hlock = NULL;
2382 for (i = 0; i < curr->lockdep_depth; i++) {
2383 hlock = curr->held_locks + i;
2384 if (chain_key != hlock->prev_chain_key) {
2387 * We got mighty confused, our chain keys don't match
2388 * with what we expect, someone trample on our task state?
2390 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2391 curr->lockdep_depth, i,
2392 (unsigned long long)chain_key,
2393 (unsigned long long)hlock->prev_chain_key);
2397 * Whoops ran out of static storage again?
2399 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
2402 if (prev_hlock && (prev_hlock->irq_context !=
2403 hlock->irq_context))
2405 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
2408 if (chain_key != curr->curr_chain_key) {
2411 * More smoking hash instead of calculating it, damn see these
2412 * numbers float.. I bet that a pink elephant stepped on my memory.
2414 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2415 curr->lockdep_depth, i,
2416 (unsigned long long)chain_key,
2417 (unsigned long long)curr->curr_chain_key);
2423 print_usage_bug_scenario(struct held_lock *lock)
2425 struct lock_class *class = hlock_class(lock);
2427 printk(" Possible unsafe locking scenario:\n\n");
2431 __print_lock_name(class);
2432 printk(KERN_CONT ");\n");
2433 printk(" <Interrupt>\n");
2435 __print_lock_name(class);
2436 printk(KERN_CONT ");\n");
2437 printk("\n *** DEADLOCK ***\n\n");
2441 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2442 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2444 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2448 pr_warn("================================\n");
2449 pr_warn("WARNING: inconsistent lock state\n");
2450 print_kernel_ident();
2451 pr_warn("--------------------------------\n");
2453 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2454 usage_str[prev_bit], usage_str[new_bit]);
2456 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2457 curr->comm, task_pid_nr(curr),
2458 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2459 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2460 trace_hardirqs_enabled(curr),
2461 trace_softirqs_enabled(curr));
2464 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
2465 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2467 print_irqtrace_events(curr);
2468 pr_warn("\nother info that might help us debug this:\n");
2469 print_usage_bug_scenario(this);
2471 lockdep_print_held_locks(curr);
2473 pr_warn("\nstack backtrace:\n");
2480 * Print out an error if an invalid bit is set:
2483 valid_state(struct task_struct *curr, struct held_lock *this,
2484 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2486 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2487 return print_usage_bug(curr, this, bad_bit, new_bit);
2491 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2492 enum lock_usage_bit new_bit);
2494 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2497 * print irq inversion bug:
2500 print_irq_inversion_bug(struct task_struct *curr,
2501 struct lock_list *root, struct lock_list *other,
2502 struct held_lock *this, int forwards,
2503 const char *irqclass)
2505 struct lock_list *entry = other;
2506 struct lock_list *middle = NULL;
2509 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2513 pr_warn("========================================================\n");
2514 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2515 print_kernel_ident();
2516 pr_warn("--------------------------------------------------------\n");
2517 pr_warn("%s/%d just changed the state of lock:\n",
2518 curr->comm, task_pid_nr(curr));
2521 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2523 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2524 print_lock_name(other->class);
2525 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2527 pr_warn("\nother info that might help us debug this:\n");
2529 /* Find a middle lock (if one exists) */
2530 depth = get_lock_depth(other);
2532 if (depth == 0 && (entry != root)) {
2533 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
2537 entry = get_lock_parent(entry);
2539 } while (entry && entry != root && (depth >= 0));
2541 print_irq_lock_scenario(root, other,
2542 middle ? middle->class : root->class, other->class);
2544 print_irq_lock_scenario(other, root,
2545 middle ? middle->class : other->class, root->class);
2547 lockdep_print_held_locks(curr);
2549 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2550 if (!save_trace(&root->trace))
2552 print_shortest_lock_dependencies(other, root);
2554 pr_warn("\nstack backtrace:\n");
2561 * Prove that in the forwards-direction subgraph starting at <this>
2562 * there is no lock matching <mask>:
2565 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2566 enum lock_usage_bit bit, const char *irqclass)
2569 struct lock_list root;
2570 struct lock_list *uninitialized_var(target_entry);
2573 root.class = hlock_class(this);
2574 ret = find_usage_forwards(&root, bit, &target_entry);
2576 return print_bfs_bug(ret);
2580 return print_irq_inversion_bug(curr, &root, target_entry,
2585 * Prove that in the backwards-direction subgraph starting at <this>
2586 * there is no lock matching <mask>:
2589 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2590 enum lock_usage_bit bit, const char *irqclass)
2593 struct lock_list root;
2594 struct lock_list *uninitialized_var(target_entry);
2597 root.class = hlock_class(this);
2598 ret = find_usage_backwards(&root, bit, &target_entry);
2600 return print_bfs_bug(ret);
2604 return print_irq_inversion_bug(curr, &root, target_entry,
2608 void print_irqtrace_events(struct task_struct *curr)
2610 printk("irq event stamp: %u\n", curr->irq_events);
2611 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
2612 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2613 (void *)curr->hardirq_enable_ip);
2614 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
2615 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2616 (void *)curr->hardirq_disable_ip);
2617 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
2618 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2619 (void *)curr->softirq_enable_ip);
2620 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
2621 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2622 (void *)curr->softirq_disable_ip);
2625 static int HARDIRQ_verbose(struct lock_class *class)
2628 return class_filter(class);
2633 static int SOFTIRQ_verbose(struct lock_class *class)
2636 return class_filter(class);
2641 #define STRICT_READ_CHECKS 1
2643 static int (*state_verbose_f[])(struct lock_class *class) = {
2644 #define LOCKDEP_STATE(__STATE) \
2646 #include "lockdep_states.h"
2647 #undef LOCKDEP_STATE
2650 static inline int state_verbose(enum lock_usage_bit bit,
2651 struct lock_class *class)
2653 return state_verbose_f[bit >> 2](class);
2656 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2657 enum lock_usage_bit bit, const char *name);
2660 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2661 enum lock_usage_bit new_bit)
2663 int excl_bit = exclusive_bit(new_bit);
2664 int read = new_bit & 1;
2665 int dir = new_bit & 2;
2668 * mark USED_IN has to look forwards -- to ensure no dependency
2669 * has ENABLED state, which would allow recursion deadlocks.
2671 * mark ENABLED has to look backwards -- to ensure no dependee
2672 * has USED_IN state, which, again, would allow recursion deadlocks.
2674 check_usage_f usage = dir ?
2675 check_usage_backwards : check_usage_forwards;
2678 * Validate that this particular lock does not have conflicting
2681 if (!valid_state(curr, this, new_bit, excl_bit))
2685 * Validate that the lock dependencies don't have conflicting usage
2688 if ((!read || !dir || STRICT_READ_CHECKS) &&
2689 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2693 * Check for read in write conflicts
2696 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2699 if (STRICT_READ_CHECKS &&
2700 !usage(curr, this, excl_bit + 1,
2701 state_name(new_bit + 1)))
2705 if (state_verbose(new_bit, hlock_class(this)))
2712 #define LOCKDEP_STATE(__STATE) __STATE,
2713 #include "lockdep_states.h"
2714 #undef LOCKDEP_STATE
2718 * Mark all held locks with a usage bit:
2721 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2723 enum lock_usage_bit usage_bit;
2724 struct held_lock *hlock;
2727 for (i = 0; i < curr->lockdep_depth; i++) {
2728 hlock = curr->held_locks + i;
2730 usage_bit = 2 + (mark << 2); /* ENABLED */
2732 usage_bit += 1; /* READ */
2734 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2739 if (!mark_lock(curr, hlock, usage_bit))
2747 * Hardirqs will be enabled:
2749 static void __trace_hardirqs_on_caller(unsigned long ip)
2751 struct task_struct *curr = current;
2753 /* we'll do an OFF -> ON transition: */
2754 curr->hardirqs_enabled = 1;
2757 * We are going to turn hardirqs on, so set the
2758 * usage bit for all held locks:
2760 if (!mark_held_locks(curr, HARDIRQ))
2763 * If we have softirqs enabled, then set the usage
2764 * bit for all held locks. (disabled hardirqs prevented
2765 * this bit from being set before)
2767 if (curr->softirqs_enabled)
2768 if (!mark_held_locks(curr, SOFTIRQ))
2771 curr->hardirq_enable_ip = ip;
2772 curr->hardirq_enable_event = ++curr->irq_events;
2773 debug_atomic_inc(hardirqs_on_events);
2776 void lockdep_hardirqs_on(unsigned long ip)
2778 if (unlikely(!debug_locks || current->lockdep_recursion))
2781 if (unlikely(current->hardirqs_enabled)) {
2783 * Neither irq nor preemption are disabled here
2784 * so this is racy by nature but losing one hit
2785 * in a stat is not a big deal.
2787 __debug_atomic_inc(redundant_hardirqs_on);
2792 * We're enabling irqs and according to our state above irqs weren't
2793 * already enabled, yet we find the hardware thinks they are in fact
2794 * enabled.. someone messed up their IRQ state tracing.
2796 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2800 * See the fine text that goes along with this variable definition.
2802 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2806 * Can't allow enabling interrupts while in an interrupt handler,
2807 * that's general bad form and such. Recursion, limited stack etc..
2809 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2812 current->lockdep_recursion = 1;
2813 __trace_hardirqs_on_caller(ip);
2814 current->lockdep_recursion = 0;
2818 * Hardirqs were disabled:
2820 void lockdep_hardirqs_off(unsigned long ip)
2822 struct task_struct *curr = current;
2824 if (unlikely(!debug_locks || current->lockdep_recursion))
2828 * So we're supposed to get called after you mask local IRQs, but for
2829 * some reason the hardware doesn't quite think you did a proper job.
2831 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2834 if (curr->hardirqs_enabled) {
2836 * We have done an ON -> OFF transition:
2838 curr->hardirqs_enabled = 0;
2839 curr->hardirq_disable_ip = ip;
2840 curr->hardirq_disable_event = ++curr->irq_events;
2841 debug_atomic_inc(hardirqs_off_events);
2843 debug_atomic_inc(redundant_hardirqs_off);
2847 * Softirqs will be enabled:
2849 void trace_softirqs_on(unsigned long ip)
2851 struct task_struct *curr = current;
2853 if (unlikely(!debug_locks || current->lockdep_recursion))
2857 * We fancy IRQs being disabled here, see softirq.c, avoids
2858 * funny state and nesting things.
2860 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2863 if (curr->softirqs_enabled) {
2864 debug_atomic_inc(redundant_softirqs_on);
2868 current->lockdep_recursion = 1;
2870 * We'll do an OFF -> ON transition:
2872 curr->softirqs_enabled = 1;
2873 curr->softirq_enable_ip = ip;
2874 curr->softirq_enable_event = ++curr->irq_events;
2875 debug_atomic_inc(softirqs_on_events);
2877 * We are going to turn softirqs on, so set the
2878 * usage bit for all held locks, if hardirqs are
2881 if (curr->hardirqs_enabled)
2882 mark_held_locks(curr, SOFTIRQ);
2883 current->lockdep_recursion = 0;
2887 * Softirqs were disabled:
2889 void trace_softirqs_off(unsigned long ip)
2891 struct task_struct *curr = current;
2893 if (unlikely(!debug_locks || current->lockdep_recursion))
2897 * We fancy IRQs being disabled here, see softirq.c
2899 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2902 if (curr->softirqs_enabled) {
2904 * We have done an ON -> OFF transition:
2906 curr->softirqs_enabled = 0;
2907 curr->softirq_disable_ip = ip;
2908 curr->softirq_disable_event = ++curr->irq_events;
2909 debug_atomic_inc(softirqs_off_events);
2911 * Whoops, we wanted softirqs off, so why aren't they?
2913 DEBUG_LOCKS_WARN_ON(!softirq_count());
2915 debug_atomic_inc(redundant_softirqs_off);
2918 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2921 * If non-trylock use in a hardirq or softirq context, then
2922 * mark the lock as used in these contexts:
2924 if (!hlock->trylock) {
2926 if (curr->hardirq_context)
2927 if (!mark_lock(curr, hlock,
2928 LOCK_USED_IN_HARDIRQ_READ))
2930 if (curr->softirq_context)
2931 if (!mark_lock(curr, hlock,
2932 LOCK_USED_IN_SOFTIRQ_READ))
2935 if (curr->hardirq_context)
2936 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2938 if (curr->softirq_context)
2939 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2943 if (!hlock->hardirqs_off) {
2945 if (!mark_lock(curr, hlock,
2946 LOCK_ENABLED_HARDIRQ_READ))
2948 if (curr->softirqs_enabled)
2949 if (!mark_lock(curr, hlock,
2950 LOCK_ENABLED_SOFTIRQ_READ))
2953 if (!mark_lock(curr, hlock,
2954 LOCK_ENABLED_HARDIRQ))
2956 if (curr->softirqs_enabled)
2957 if (!mark_lock(curr, hlock,
2958 LOCK_ENABLED_SOFTIRQ))
2966 static inline unsigned int task_irq_context(struct task_struct *task)
2968 return 2 * !!task->hardirq_context + !!task->softirq_context;
2971 static int separate_irq_context(struct task_struct *curr,
2972 struct held_lock *hlock)
2974 unsigned int depth = curr->lockdep_depth;
2977 * Keep track of points where we cross into an interrupt context:
2980 struct held_lock *prev_hlock;
2982 prev_hlock = curr->held_locks + depth-1;
2984 * If we cross into another context, reset the
2985 * hash key (this also prevents the checking and the
2986 * adding of the dependency to 'prev'):
2988 if (prev_hlock->irq_context != hlock->irq_context)
2994 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2997 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2998 enum lock_usage_bit new_bit)
3000 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3004 static inline int mark_irqflags(struct task_struct *curr,
3005 struct held_lock *hlock)
3010 static inline unsigned int task_irq_context(struct task_struct *task)
3015 static inline int separate_irq_context(struct task_struct *curr,
3016 struct held_lock *hlock)
3021 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3024 * Mark a lock with a usage bit, and validate the state transition:
3026 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3027 enum lock_usage_bit new_bit)
3029 unsigned int new_mask = 1 << new_bit, ret = 1;
3032 * If already set then do not dirty the cacheline,
3033 * nor do any checks:
3035 if (likely(hlock_class(this)->usage_mask & new_mask))
3041 * Make sure we didn't race:
3043 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3048 hlock_class(this)->usage_mask |= new_mask;
3050 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
3054 #define LOCKDEP_STATE(__STATE) \
3055 case LOCK_USED_IN_##__STATE: \
3056 case LOCK_USED_IN_##__STATE##_READ: \
3057 case LOCK_ENABLED_##__STATE: \
3058 case LOCK_ENABLED_##__STATE##_READ:
3059 #include "lockdep_states.h"
3060 #undef LOCKDEP_STATE
3061 ret = mark_lock_irq(curr, this, new_bit);
3066 debug_atomic_dec(nr_unused_locks);
3069 if (!debug_locks_off_graph_unlock())
3078 * We must printk outside of the graph_lock:
3081 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3083 print_irqtrace_events(curr);
3091 * Initialize a lock instance's lock-class mapping info:
3093 static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
3094 struct lock_class_key *key, int subclass)
3098 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3099 lock->class_cache[i] = NULL;
3101 #ifdef CONFIG_LOCK_STAT
3102 lock->cpu = raw_smp_processor_id();
3106 * Can't be having no nameless bastards around this place!
3108 if (DEBUG_LOCKS_WARN_ON(!name)) {
3109 lock->name = "NULL";
3116 * No key, no joy, we need to hash something.
3118 if (DEBUG_LOCKS_WARN_ON(!key))
3121 * Sanity check, the lock-class key must be persistent:
3123 if (!static_obj(key)) {
3124 printk("BUG: key %px not in .data!\n", key);
3126 * What it says above ^^^^^, I suggest you read it.
3128 DEBUG_LOCKS_WARN_ON(1);
3133 if (unlikely(!debug_locks))
3137 unsigned long flags;
3139 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3142 raw_local_irq_save(flags);
3143 current->lockdep_recursion = 1;
3144 register_lock_class(lock, subclass, 1);
3145 current->lockdep_recursion = 0;
3146 raw_local_irq_restore(flags);
3150 void lockdep_init_map(struct lockdep_map *lock, const char *name,
3151 struct lock_class_key *key, int subclass)
3153 __lockdep_init_map(lock, name, key, subclass);
3155 EXPORT_SYMBOL_GPL(lockdep_init_map);
3157 struct lock_class_key __lockdep_no_validate__;
3158 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3161 print_lock_nested_lock_not_held(struct task_struct *curr,
3162 struct held_lock *hlock,
3165 if (!debug_locks_off())
3167 if (debug_locks_silent)
3171 pr_warn("==================================\n");
3172 pr_warn("WARNING: Nested lock was not taken\n");
3173 print_kernel_ident();
3174 pr_warn("----------------------------------\n");
3176 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3179 pr_warn("\nbut this task is not holding:\n");
3180 pr_warn("%s\n", hlock->nest_lock->name);
3182 pr_warn("\nstack backtrace:\n");
3185 pr_warn("\nother info that might help us debug this:\n");
3186 lockdep_print_held_locks(curr);
3188 pr_warn("\nstack backtrace:\n");
3194 static int __lock_is_held(const struct lockdep_map *lock, int read);
3197 * This gets called for every mutex_lock*()/spin_lock*() operation.
3198 * We maintain the dependency maps and validate the locking attempt:
3200 * The callers must make sure that IRQs are disabled before calling it,
3201 * otherwise we could get an interrupt which would want to take locks,
3202 * which would end up in lockdep again.
3204 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3205 int trylock, int read, int check, int hardirqs_off,
3206 struct lockdep_map *nest_lock, unsigned long ip,
3207 int references, int pin_count)
3209 struct task_struct *curr = current;
3210 struct lock_class *class = NULL;
3211 struct held_lock *hlock;
3217 if (unlikely(!debug_locks))
3220 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3223 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3224 class = lock->class_cache[subclass];
3228 if (unlikely(!class)) {
3229 class = register_lock_class(lock, subclass, 0);
3234 debug_class_ops_inc(class);
3236 if (very_verbose(class)) {
3237 printk("\nacquire class [%px] %s", class->key, class->name);
3238 if (class->name_version > 1)
3239 printk(KERN_CONT "#%d", class->name_version);
3240 printk(KERN_CONT "\n");
3245 * Add the lock to the list of currently held locks.
3246 * (we dont increase the depth just yet, up until the
3247 * dependency checks are done)
3249 depth = curr->lockdep_depth;
3251 * Ran out of static storage for our per-task lock stack again have we?
3253 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3256 class_idx = class - lock_classes + 1;
3259 hlock = curr->held_locks + depth - 1;
3260 if (hlock->class_idx == class_idx && nest_lock) {
3261 if (hlock->references) {
3263 * Check: unsigned int references:12, overflow.
3265 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3268 hlock->references++;
3270 hlock->references = 2;
3277 hlock = curr->held_locks + depth;
3279 * Plain impossible, we just registered it and checked it weren't no
3280 * NULL like.. I bet this mushroom I ate was good!
3282 if (DEBUG_LOCKS_WARN_ON(!class))
3284 hlock->class_idx = class_idx;
3285 hlock->acquire_ip = ip;
3286 hlock->instance = lock;
3287 hlock->nest_lock = nest_lock;
3288 hlock->irq_context = task_irq_context(curr);
3289 hlock->trylock = trylock;
3291 hlock->check = check;
3292 hlock->hardirqs_off = !!hardirqs_off;
3293 hlock->references = references;
3294 #ifdef CONFIG_LOCK_STAT
3295 hlock->waittime_stamp = 0;
3296 hlock->holdtime_stamp = lockstat_clock();
3298 hlock->pin_count = pin_count;
3300 if (check && !mark_irqflags(curr, hlock))
3303 /* mark it as used: */
3304 if (!mark_lock(curr, hlock, LOCK_USED))
3308 * Calculate the chain hash: it's the combined hash of all the
3309 * lock keys along the dependency chain. We save the hash value
3310 * at every step so that we can get the current hash easily
3311 * after unlock. The chain hash is then used to cache dependency
3314 * The 'key ID' is what is the most compact key value to drive
3315 * the hash, not class->key.
3318 * Whoops, we did it again.. ran straight out of our static allocation.
3320 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
3323 chain_key = curr->curr_chain_key;
3326 * How can we have a chain hash when we ain't got no keys?!
3328 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3333 hlock->prev_chain_key = chain_key;
3334 if (separate_irq_context(curr, hlock)) {
3338 chain_key = iterate_chain_key(chain_key, class_idx);
3340 if (nest_lock && !__lock_is_held(nest_lock, -1))
3341 return print_lock_nested_lock_not_held(curr, hlock, ip);
3343 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3346 curr->curr_chain_key = chain_key;
3347 curr->lockdep_depth++;
3348 check_chain_key(curr);
3349 #ifdef CONFIG_DEBUG_LOCKDEP
3350 if (unlikely(!debug_locks))
3353 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3355 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3356 printk(KERN_DEBUG "depth: %i max: %lu!\n",
3357 curr->lockdep_depth, MAX_LOCK_DEPTH);
3359 lockdep_print_held_locks(current);
3360 debug_show_all_locks();
3366 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3367 max_lockdep_depth = curr->lockdep_depth;
3373 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3376 if (!debug_locks_off())
3378 if (debug_locks_silent)
3382 pr_warn("=====================================\n");
3383 pr_warn("WARNING: bad unlock balance detected!\n");
3384 print_kernel_ident();
3385 pr_warn("-------------------------------------\n");
3386 pr_warn("%s/%d is trying to release lock (",
3387 curr->comm, task_pid_nr(curr));
3388 print_lockdep_cache(lock);
3391 pr_warn("but there are no more locks to release!\n");
3392 pr_warn("\nother info that might help us debug this:\n");
3393 lockdep_print_held_locks(curr);
3395 pr_warn("\nstack backtrace:\n");
3401 static int match_held_lock(const struct held_lock *hlock,
3402 const struct lockdep_map *lock)
3404 if (hlock->instance == lock)
3407 if (hlock->references) {
3408 const struct lock_class *class = lock->class_cache[0];
3411 class = look_up_lock_class(lock, 0);
3414 * If look_up_lock_class() failed to find a class, we're trying
3415 * to test if we hold a lock that has never yet been acquired.
3416 * Clearly if the lock hasn't been acquired _ever_, we're not
3417 * holding it either, so report failure.
3423 * References, but not a lock we're actually ref-counting?
3424 * State got messed up, follow the sites that change ->references
3425 * and try to make sense of it.
3427 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3430 if (hlock->class_idx == class - lock_classes + 1)
3437 /* @depth must not be zero */
3438 static struct held_lock *find_held_lock(struct task_struct *curr,
3439 struct lockdep_map *lock,
3440 unsigned int depth, int *idx)
3442 struct held_lock *ret, *hlock, *prev_hlock;
3446 hlock = curr->held_locks + i;
3448 if (match_held_lock(hlock, lock))
3452 for (i--, prev_hlock = hlock--;
3454 i--, prev_hlock = hlock--) {
3456 * We must not cross into another context:
3458 if (prev_hlock->irq_context != hlock->irq_context) {
3462 if (match_held_lock(hlock, lock)) {
3473 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3476 struct held_lock *hlock;
3478 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3481 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3482 if (!__lock_acquire(hlock->instance,
3483 hlock_class(hlock)->subclass,
3485 hlock->read, hlock->check,
3486 hlock->hardirqs_off,
3487 hlock->nest_lock, hlock->acquire_ip,
3488 hlock->references, hlock->pin_count))
3495 __lock_set_class(struct lockdep_map *lock, const char *name,
3496 struct lock_class_key *key, unsigned int subclass,
3499 struct task_struct *curr = current;
3500 struct held_lock *hlock;
3501 struct lock_class *class;
3505 depth = curr->lockdep_depth;
3507 * This function is about (re)setting the class of a held lock,
3508 * yet we're not actually holding any locks. Naughty user!
3510 if (DEBUG_LOCKS_WARN_ON(!depth))
3513 hlock = find_held_lock(curr, lock, depth, &i);
3515 return print_unlock_imbalance_bug(curr, lock, ip);
3517 lockdep_init_map(lock, name, key, 0);
3518 class = register_lock_class(lock, subclass, 0);
3519 hlock->class_idx = class - lock_classes + 1;
3521 curr->lockdep_depth = i;
3522 curr->curr_chain_key = hlock->prev_chain_key;
3524 if (reacquire_held_locks(curr, depth, i))
3528 * I took it apart and put it back together again, except now I have
3529 * these 'spare' parts.. where shall I put them.
3531 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3536 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3538 struct task_struct *curr = current;
3539 struct held_lock *hlock;
3543 depth = curr->lockdep_depth;
3545 * This function is about (re)setting the class of a held lock,
3546 * yet we're not actually holding any locks. Naughty user!
3548 if (DEBUG_LOCKS_WARN_ON(!depth))
3551 hlock = find_held_lock(curr, lock, depth, &i);
3553 return print_unlock_imbalance_bug(curr, lock, ip);
3555 curr->lockdep_depth = i;
3556 curr->curr_chain_key = hlock->prev_chain_key;
3558 WARN(hlock->read, "downgrading a read lock");
3560 hlock->acquire_ip = ip;
3562 if (reacquire_held_locks(curr, depth, i))
3566 * I took it apart and put it back together again, except now I have
3567 * these 'spare' parts.. where shall I put them.
3569 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3575 * Remove the lock to the list of currently held locks - this gets
3576 * called on mutex_unlock()/spin_unlock*() (or on a failed
3577 * mutex_lock_interruptible()).
3579 * @nested is an hysterical artifact, needs a tree wide cleanup.
3582 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3584 struct task_struct *curr = current;
3585 struct held_lock *hlock;
3589 if (unlikely(!debug_locks))
3592 depth = curr->lockdep_depth;
3594 * So we're all set to release this lock.. wait what lock? We don't
3595 * own any locks, you've been drinking again?
3597 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3598 return print_unlock_imbalance_bug(curr, lock, ip);
3601 * Check whether the lock exists in the current stack
3604 hlock = find_held_lock(curr, lock, depth, &i);
3606 return print_unlock_imbalance_bug(curr, lock, ip);
3608 if (hlock->instance == lock)
3609 lock_release_holdtime(hlock);
3611 WARN(hlock->pin_count, "releasing a pinned lock\n");
3613 if (hlock->references) {
3614 hlock->references--;
3615 if (hlock->references) {
3617 * We had, and after removing one, still have
3618 * references, the current lock stack is still
3619 * valid. We're done!
3626 * We have the right lock to unlock, 'hlock' points to it.
3627 * Now we remove it from the stack, and add back the other
3628 * entries (if any), recalculating the hash along the way:
3631 curr->lockdep_depth = i;
3632 curr->curr_chain_key = hlock->prev_chain_key;
3635 * The most likely case is when the unlock is on the innermost
3636 * lock. In this case, we are done!
3641 if (reacquire_held_locks(curr, depth, i + 1))
3645 * We had N bottles of beer on the wall, we drank one, but now
3646 * there's not N-1 bottles of beer left on the wall...
3648 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1);
3651 * Since reacquire_held_locks() would have called check_chain_key()
3652 * indirectly via __lock_acquire(), we don't need to do it again
3658 static int __lock_is_held(const struct lockdep_map *lock, int read)
3660 struct task_struct *curr = current;
3663 for (i = 0; i < curr->lockdep_depth; i++) {
3664 struct held_lock *hlock = curr->held_locks + i;
3666 if (match_held_lock(hlock, lock)) {
3667 if (read == -1 || hlock->read == read)
3677 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3679 struct pin_cookie cookie = NIL_COOKIE;
3680 struct task_struct *curr = current;
3683 if (unlikely(!debug_locks))
3686 for (i = 0; i < curr->lockdep_depth; i++) {
3687 struct held_lock *hlock = curr->held_locks + i;
3689 if (match_held_lock(hlock, lock)) {
3691 * Grab 16bits of randomness; this is sufficient to not
3692 * be guessable and still allows some pin nesting in
3693 * our u32 pin_count.
3695 cookie.val = 1 + (prandom_u32() >> 16);
3696 hlock->pin_count += cookie.val;
3701 WARN(1, "pinning an unheld lock\n");
3705 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3707 struct task_struct *curr = current;
3710 if (unlikely(!debug_locks))
3713 for (i = 0; i < curr->lockdep_depth; i++) {
3714 struct held_lock *hlock = curr->held_locks + i;
3716 if (match_held_lock(hlock, lock)) {
3717 hlock->pin_count += cookie.val;
3722 WARN(1, "pinning an unheld lock\n");
3725 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3727 struct task_struct *curr = current;
3730 if (unlikely(!debug_locks))
3733 for (i = 0; i < curr->lockdep_depth; i++) {
3734 struct held_lock *hlock = curr->held_locks + i;
3736 if (match_held_lock(hlock, lock)) {
3737 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3740 hlock->pin_count -= cookie.val;
3742 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3743 hlock->pin_count = 0;
3749 WARN(1, "unpinning an unheld lock\n");
3753 * Check whether we follow the irq-flags state precisely:
3755 static void check_flags(unsigned long flags)
3757 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3758 defined(CONFIG_TRACE_IRQFLAGS)
3762 if (irqs_disabled_flags(flags)) {
3763 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3764 printk("possible reason: unannotated irqs-off.\n");
3767 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3768 printk("possible reason: unannotated irqs-on.\n");
3773 * We dont accurately track softirq state in e.g.
3774 * hardirq contexts (such as on 4KSTACKS), so only
3775 * check if not in hardirq contexts:
3777 if (!hardirq_count()) {
3778 if (softirq_count()) {
3779 /* like the above, but with softirqs */
3780 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3782 /* lick the above, does it taste good? */
3783 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3788 print_irqtrace_events(current);
3792 void lock_set_class(struct lockdep_map *lock, const char *name,
3793 struct lock_class_key *key, unsigned int subclass,
3796 unsigned long flags;
3798 if (unlikely(current->lockdep_recursion))
3801 raw_local_irq_save(flags);
3802 current->lockdep_recursion = 1;
3804 if (__lock_set_class(lock, name, key, subclass, ip))
3805 check_chain_key(current);
3806 current->lockdep_recursion = 0;
3807 raw_local_irq_restore(flags);
3809 EXPORT_SYMBOL_GPL(lock_set_class);
3811 void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3813 unsigned long flags;
3815 if (unlikely(current->lockdep_recursion))
3818 raw_local_irq_save(flags);
3819 current->lockdep_recursion = 1;
3821 if (__lock_downgrade(lock, ip))
3822 check_chain_key(current);
3823 current->lockdep_recursion = 0;
3824 raw_local_irq_restore(flags);
3826 EXPORT_SYMBOL_GPL(lock_downgrade);
3829 * We are not always called with irqs disabled - do that here,
3830 * and also avoid lockdep recursion:
3832 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3833 int trylock, int read, int check,
3834 struct lockdep_map *nest_lock, unsigned long ip)
3836 unsigned long flags;
3838 if (unlikely(current->lockdep_recursion))
3841 raw_local_irq_save(flags);
3844 current->lockdep_recursion = 1;
3845 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3846 __lock_acquire(lock, subclass, trylock, read, check,
3847 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
3848 current->lockdep_recursion = 0;
3849 raw_local_irq_restore(flags);
3851 EXPORT_SYMBOL_GPL(lock_acquire);
3853 void lock_release(struct lockdep_map *lock, int nested,
3856 unsigned long flags;
3858 if (unlikely(current->lockdep_recursion))
3861 raw_local_irq_save(flags);
3863 current->lockdep_recursion = 1;
3864 trace_lock_release(lock, ip);
3865 if (__lock_release(lock, nested, ip))
3866 check_chain_key(current);
3867 current->lockdep_recursion = 0;
3868 raw_local_irq_restore(flags);
3870 EXPORT_SYMBOL_GPL(lock_release);
3872 int lock_is_held_type(const struct lockdep_map *lock, int read)
3874 unsigned long flags;
3877 if (unlikely(current->lockdep_recursion))
3878 return 1; /* avoid false negative lockdep_assert_held() */
3880 raw_local_irq_save(flags);
3883 current->lockdep_recursion = 1;
3884 ret = __lock_is_held(lock, read);
3885 current->lockdep_recursion = 0;
3886 raw_local_irq_restore(flags);
3890 EXPORT_SYMBOL_GPL(lock_is_held_type);
3892 struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
3894 struct pin_cookie cookie = NIL_COOKIE;
3895 unsigned long flags;
3897 if (unlikely(current->lockdep_recursion))
3900 raw_local_irq_save(flags);
3903 current->lockdep_recursion = 1;
3904 cookie = __lock_pin_lock(lock);
3905 current->lockdep_recursion = 0;
3906 raw_local_irq_restore(flags);
3910 EXPORT_SYMBOL_GPL(lock_pin_lock);
3912 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3914 unsigned long flags;
3916 if (unlikely(current->lockdep_recursion))
3919 raw_local_irq_save(flags);
3922 current->lockdep_recursion = 1;
3923 __lock_repin_lock(lock, cookie);
3924 current->lockdep_recursion = 0;
3925 raw_local_irq_restore(flags);
3927 EXPORT_SYMBOL_GPL(lock_repin_lock);
3929 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3931 unsigned long flags;
3933 if (unlikely(current->lockdep_recursion))
3936 raw_local_irq_save(flags);
3939 current->lockdep_recursion = 1;
3940 __lock_unpin_lock(lock, cookie);
3941 current->lockdep_recursion = 0;
3942 raw_local_irq_restore(flags);
3944 EXPORT_SYMBOL_GPL(lock_unpin_lock);
3946 #ifdef CONFIG_LOCK_STAT
3948 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3951 if (!debug_locks_off())
3953 if (debug_locks_silent)
3957 pr_warn("=================================\n");
3958 pr_warn("WARNING: bad contention detected!\n");
3959 print_kernel_ident();
3960 pr_warn("---------------------------------\n");
3961 pr_warn("%s/%d is trying to contend lock (",
3962 curr->comm, task_pid_nr(curr));
3963 print_lockdep_cache(lock);
3966 pr_warn("but there are no locks held!\n");
3967 pr_warn("\nother info that might help us debug this:\n");
3968 lockdep_print_held_locks(curr);
3970 pr_warn("\nstack backtrace:\n");
3977 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3979 struct task_struct *curr = current;
3980 struct held_lock *hlock;
3981 struct lock_class_stats *stats;
3983 int i, contention_point, contending_point;
3985 depth = curr->lockdep_depth;
3987 * Whee, we contended on this lock, except it seems we're not
3988 * actually trying to acquire anything much at all..
3990 if (DEBUG_LOCKS_WARN_ON(!depth))
3993 hlock = find_held_lock(curr, lock, depth, &i);
3995 print_lock_contention_bug(curr, lock, ip);
3999 if (hlock->instance != lock)
4002 hlock->waittime_stamp = lockstat_clock();
4004 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4005 contending_point = lock_point(hlock_class(hlock)->contending_point,
4008 stats = get_lock_stats(hlock_class(hlock));
4009 if (contention_point < LOCKSTAT_POINTS)
4010 stats->contention_point[contention_point]++;
4011 if (contending_point < LOCKSTAT_POINTS)
4012 stats->contending_point[contending_point]++;
4013 if (lock->cpu != smp_processor_id())
4014 stats->bounces[bounce_contended + !!hlock->read]++;
4018 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
4020 struct task_struct *curr = current;
4021 struct held_lock *hlock;
4022 struct lock_class_stats *stats;
4024 u64 now, waittime = 0;
4027 depth = curr->lockdep_depth;
4029 * Yay, we acquired ownership of this lock we didn't try to
4030 * acquire, how the heck did that happen?
4032 if (DEBUG_LOCKS_WARN_ON(!depth))
4035 hlock = find_held_lock(curr, lock, depth, &i);
4037 print_lock_contention_bug(curr, lock, _RET_IP_);
4041 if (hlock->instance != lock)
4044 cpu = smp_processor_id();
4045 if (hlock->waittime_stamp) {
4046 now = lockstat_clock();
4047 waittime = now - hlock->waittime_stamp;
4048 hlock->holdtime_stamp = now;
4051 trace_lock_acquired(lock, ip);
4053 stats = get_lock_stats(hlock_class(hlock));
4056 lock_time_inc(&stats->read_waittime, waittime);
4058 lock_time_inc(&stats->write_waittime, waittime);
4060 if (lock->cpu != cpu)
4061 stats->bounces[bounce_acquired + !!hlock->read]++;
4067 void lock_contended(struct lockdep_map *lock, unsigned long ip)
4069 unsigned long flags;
4071 if (unlikely(!lock_stat))
4074 if (unlikely(current->lockdep_recursion))
4077 raw_local_irq_save(flags);
4079 current->lockdep_recursion = 1;
4080 trace_lock_contended(lock, ip);
4081 __lock_contended(lock, ip);
4082 current->lockdep_recursion = 0;
4083 raw_local_irq_restore(flags);
4085 EXPORT_SYMBOL_GPL(lock_contended);
4087 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
4089 unsigned long flags;
4091 if (unlikely(!lock_stat))
4094 if (unlikely(current->lockdep_recursion))
4097 raw_local_irq_save(flags);
4099 current->lockdep_recursion = 1;
4100 __lock_acquired(lock, ip);
4101 current->lockdep_recursion = 0;
4102 raw_local_irq_restore(flags);
4104 EXPORT_SYMBOL_GPL(lock_acquired);
4108 * Used by the testsuite, sanitize the validator state
4109 * after a simulated failure:
4112 void lockdep_reset(void)
4114 unsigned long flags;
4117 raw_local_irq_save(flags);
4118 current->curr_chain_key = 0;
4119 current->lockdep_depth = 0;
4120 current->lockdep_recursion = 0;
4121 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4122 nr_hardirq_chains = 0;
4123 nr_softirq_chains = 0;
4124 nr_process_chains = 0;
4126 for (i = 0; i < CHAINHASH_SIZE; i++)
4127 INIT_HLIST_HEAD(chainhash_table + i);
4128 raw_local_irq_restore(flags);
4131 static void zap_class(struct lock_class *class)
4136 * Remove all dependencies this lock is
4139 for (i = 0; i < nr_list_entries; i++) {
4140 if (list_entries[i].class == class)
4141 list_del_rcu(&list_entries[i].entry);
4144 * Unhash the class and remove it from the all_lock_classes list:
4146 hlist_del_rcu(&class->hash_entry);
4147 list_del_rcu(&class->lock_entry);
4149 RCU_INIT_POINTER(class->key, NULL);
4150 RCU_INIT_POINTER(class->name, NULL);
4153 static inline int within(const void *addr, void *start, unsigned long size)
4155 return addr >= start && addr < start + size;
4159 * Used in module.c to remove lock classes from memory that is going to be
4160 * freed; and possibly re-used by other modules.
4162 * We will have had one sync_sched() before getting here, so we're guaranteed
4163 * nobody will look up these exact classes -- they're properly dead but still
4166 void lockdep_free_key_range(void *start, unsigned long size)
4168 struct lock_class *class;
4169 struct hlist_head *head;
4170 unsigned long flags;
4174 raw_local_irq_save(flags);
4175 locked = graph_lock();
4178 * Unhash all classes that were created by this module:
4180 for (i = 0; i < CLASSHASH_SIZE; i++) {
4181 head = classhash_table + i;
4182 hlist_for_each_entry_rcu(class, head, hash_entry) {
4183 if (within(class->key, start, size))
4185 else if (within(class->name, start, size))
4192 raw_local_irq_restore(flags);
4195 * Wait for any possible iterators from look_up_lock_class() to pass
4196 * before continuing to free the memory they refer to.
4198 * sync_sched() is sufficient because the read-side is IRQ disable.
4200 synchronize_sched();
4203 * XXX at this point we could return the resources to the pool;
4204 * instead we leak them. We would need to change to bitmap allocators
4205 * instead of the linear allocators we have now.
4209 void lockdep_reset_lock(struct lockdep_map *lock)
4211 struct lock_class *class;
4212 struct hlist_head *head;
4213 unsigned long flags;
4217 raw_local_irq_save(flags);
4220 * Remove all classes this lock might have:
4222 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4224 * If the class exists we look it up and zap it:
4226 class = look_up_lock_class(lock, j);
4231 * Debug check: in the end all mapped classes should
4234 locked = graph_lock();
4235 for (i = 0; i < CLASSHASH_SIZE; i++) {
4236 head = classhash_table + i;
4237 hlist_for_each_entry_rcu(class, head, hash_entry) {
4240 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4241 match |= class == lock->class_cache[j];
4243 if (unlikely(match)) {
4244 if (debug_locks_off_graph_unlock()) {
4246 * We all just reset everything, how did it match?
4258 raw_local_irq_restore(flags);
4261 void __init lockdep_init(void)
4263 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4265 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
4266 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4267 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
4268 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
4269 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4270 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4271 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4273 printk(" memory used by lock dependency info: %lu kB\n",
4274 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4275 sizeof(struct list_head) * CLASSHASH_SIZE +
4276 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4277 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
4278 sizeof(struct list_head) * CHAINHASH_SIZE
4279 #ifdef CONFIG_PROVE_LOCKING
4280 + sizeof(struct circular_queue)
4285 printk(" per task-struct memory footprint: %lu bytes\n",
4286 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
4290 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
4291 const void *mem_to, struct held_lock *hlock)
4293 if (!debug_locks_off())
4295 if (debug_locks_silent)
4299 pr_warn("=========================\n");
4300 pr_warn("WARNING: held lock freed!\n");
4301 print_kernel_ident();
4302 pr_warn("-------------------------\n");
4303 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
4304 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
4306 lockdep_print_held_locks(curr);
4308 pr_warn("\nstack backtrace:\n");
4312 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4313 const void* lock_from, unsigned long lock_len)
4315 return lock_from + lock_len <= mem_from ||
4316 mem_from + mem_len <= lock_from;
4320 * Called when kernel memory is freed (or unmapped), or if a lock
4321 * is destroyed or reinitialized - this code checks whether there is
4322 * any held lock in the memory range of <from> to <to>:
4324 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4326 struct task_struct *curr = current;
4327 struct held_lock *hlock;
4328 unsigned long flags;
4331 if (unlikely(!debug_locks))
4334 raw_local_irq_save(flags);
4335 for (i = 0; i < curr->lockdep_depth; i++) {
4336 hlock = curr->held_locks + i;
4338 if (not_in_range(mem_from, mem_len, hlock->instance,
4339 sizeof(*hlock->instance)))
4342 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
4345 raw_local_irq_restore(flags);
4347 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
4349 static void print_held_locks_bug(void)
4351 if (!debug_locks_off())
4353 if (debug_locks_silent)
4357 pr_warn("====================================\n");
4358 pr_warn("WARNING: %s/%d still has locks held!\n",
4359 current->comm, task_pid_nr(current));
4360 print_kernel_ident();
4361 pr_warn("------------------------------------\n");
4362 lockdep_print_held_locks(current);
4363 pr_warn("\nstack backtrace:\n");
4367 void debug_check_no_locks_held(void)
4369 if (unlikely(current->lockdep_depth > 0))
4370 print_held_locks_bug();
4372 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
4375 void debug_show_all_locks(void)
4377 struct task_struct *g, *p;
4379 if (unlikely(!debug_locks)) {
4380 pr_warn("INFO: lockdep is turned off.\n");
4383 pr_warn("\nShowing all locks held in the system:\n");
4386 for_each_process_thread(g, p) {
4387 if (!p->lockdep_depth)
4389 lockdep_print_held_locks(p);
4390 touch_nmi_watchdog();
4391 touch_all_softlockup_watchdogs();
4396 pr_warn("=============================================\n\n");
4398 EXPORT_SYMBOL_GPL(debug_show_all_locks);
4402 * Careful: only use this function if you are sure that
4403 * the task cannot run in parallel!
4405 void debug_show_held_locks(struct task_struct *task)
4407 if (unlikely(!debug_locks)) {
4408 printk("INFO: lockdep is turned off.\n");
4411 lockdep_print_held_locks(task);
4413 EXPORT_SYMBOL_GPL(debug_show_held_locks);
4415 asmlinkage __visible void lockdep_sys_exit(void)
4417 struct task_struct *curr = current;
4419 if (unlikely(curr->lockdep_depth)) {
4420 if (!debug_locks_off())
4423 pr_warn("================================================\n");
4424 pr_warn("WARNING: lock held when returning to user space!\n");
4425 print_kernel_ident();
4426 pr_warn("------------------------------------------------\n");
4427 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4428 curr->comm, curr->pid);
4429 lockdep_print_held_locks(curr);
4433 * The lock history for each syscall should be independent. So wipe the
4434 * slate clean on return to userspace.
4436 lockdep_invariant_state(false);
4439 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
4441 struct task_struct *curr = current;
4443 /* Note: the following can be executed concurrently, so be careful. */
4445 pr_warn("=============================\n");
4446 pr_warn("WARNING: suspicious RCU usage\n");
4447 print_kernel_ident();
4448 pr_warn("-----------------------------\n");
4449 pr_warn("%s:%d %s!\n", file, line, s);
4450 pr_warn("\nother info that might help us debug this:\n\n");
4451 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4452 !rcu_lockdep_current_cpu_online()
4453 ? "RCU used illegally from offline CPU!\n"
4454 : !rcu_is_watching()
4455 ? "RCU used illegally from idle CPU!\n"
4457 rcu_scheduler_active, debug_locks);
4460 * If a CPU is in the RCU-free window in idle (ie: in the section
4461 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4462 * considers that CPU to be in an "extended quiescent state",
4463 * which means that RCU will be completely ignoring that CPU.
4464 * Therefore, rcu_read_lock() and friends have absolutely no
4465 * effect on a CPU running in that state. In other words, even if
4466 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4467 * delete data structures out from under it. RCU really has no
4468 * choice here: we need to keep an RCU-free window in idle where
4469 * the CPU may possibly enter into low power mode. This way we can
4470 * notice an extended quiescent state to other CPUs that started a grace
4471 * period. Otherwise we would delay any grace period as long as we run
4474 * So complain bitterly if someone does call rcu_read_lock(),
4475 * rcu_read_lock_bh() and so on from extended quiescent states.
4477 if (!rcu_is_watching())
4478 pr_warn("RCU used illegally from extended quiescent state!\n");
4480 lockdep_print_held_locks(curr);
4481 pr_warn("\nstack backtrace:\n");
4484 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);