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>
53 #include <asm/sections.h>
55 #include "lockdep_internals.h"
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/lock.h>
60 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
61 #include <linux/slab.h>
64 #ifdef CONFIG_PROVE_LOCKING
65 int prove_locking = 1;
66 module_param(prove_locking, int, 0644);
68 #define prove_locking 0
71 #ifdef CONFIG_LOCK_STAT
73 module_param(lock_stat, int, 0644);
79 * lockdep_lock: protects the lockdep graph, the hashes and the
80 * class/list/hash allocators.
82 * This is one of the rare exceptions where it's justified
83 * to use a raw spinlock - we really dont want the spinlock
84 * code to recurse back into the lockdep code...
86 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
88 static int graph_lock(void)
90 arch_spin_lock(&lockdep_lock);
92 * Make sure that if another CPU detected a bug while
93 * walking the graph we dont change it (while the other
94 * CPU is busy printing out stuff with the graph lock
98 arch_spin_unlock(&lockdep_lock);
101 /* prevent any recursions within lockdep from causing deadlocks */
102 current->lockdep_recursion++;
106 static inline int graph_unlock(void)
108 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
110 * The lockdep graph lock isn't locked while we expect it to
111 * be, we're confused now, bye!
113 return DEBUG_LOCKS_WARN_ON(1);
116 current->lockdep_recursion--;
117 arch_spin_unlock(&lockdep_lock);
122 * Turn lock debugging off and return with 0 if it was off already,
123 * and also release the graph lock:
125 static inline int debug_locks_off_graph_unlock(void)
127 int ret = debug_locks_off();
129 arch_spin_unlock(&lockdep_lock);
134 unsigned long nr_list_entries;
135 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
138 * All data structures here are protected by the global debug_lock.
140 * Mutex key structs only get allocated, once during bootup, and never
141 * get freed - this significantly simplifies the debugging code.
143 unsigned long nr_lock_classes;
144 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
146 static inline struct lock_class *hlock_class(struct held_lock *hlock)
148 if (!hlock->class_idx) {
150 * Someone passed in garbage, we give up.
152 DEBUG_LOCKS_WARN_ON(1);
155 return lock_classes + hlock->class_idx - 1;
158 #ifdef CONFIG_LOCK_STAT
159 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
161 static inline u64 lockstat_clock(void)
163 return local_clock();
166 static int lock_point(unsigned long points[], unsigned long ip)
170 for (i = 0; i < LOCKSTAT_POINTS; i++) {
171 if (points[i] == 0) {
182 static void lock_time_inc(struct lock_time *lt, u64 time)
187 if (time < lt->min || !lt->nr)
194 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
199 if (src->max > dst->max)
202 if (src->min < dst->min || !dst->nr)
205 dst->total += src->total;
209 struct lock_class_stats lock_stats(struct lock_class *class)
211 struct lock_class_stats stats;
214 memset(&stats, 0, sizeof(struct lock_class_stats));
215 for_each_possible_cpu(cpu) {
216 struct lock_class_stats *pcs =
217 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
219 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
220 stats.contention_point[i] += pcs->contention_point[i];
222 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
223 stats.contending_point[i] += pcs->contending_point[i];
225 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
226 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
228 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
229 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
231 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
232 stats.bounces[i] += pcs->bounces[i];
238 void clear_lock_stats(struct lock_class *class)
242 for_each_possible_cpu(cpu) {
243 struct lock_class_stats *cpu_stats =
244 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
246 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
248 memset(class->contention_point, 0, sizeof(class->contention_point));
249 memset(class->contending_point, 0, sizeof(class->contending_point));
252 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
254 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
257 static void put_lock_stats(struct lock_class_stats *stats)
259 put_cpu_var(cpu_lock_stats);
262 static void lock_release_holdtime(struct held_lock *hlock)
264 struct lock_class_stats *stats;
270 holdtime = lockstat_clock() - hlock->holdtime_stamp;
272 stats = get_lock_stats(hlock_class(hlock));
274 lock_time_inc(&stats->read_holdtime, holdtime);
276 lock_time_inc(&stats->write_holdtime, holdtime);
277 put_lock_stats(stats);
280 static inline void lock_release_holdtime(struct held_lock *hlock)
286 * We keep a global list of all lock classes. The list only grows,
287 * never shrinks. The list is only accessed with the lockdep
288 * spinlock lock held.
290 LIST_HEAD(all_lock_classes);
293 * The lockdep classes are in a hash-table as well, for fast lookup:
295 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
296 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
297 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
298 #define classhashentry(key) (classhash_table + __classhashfn((key)))
300 static struct hlist_head classhash_table[CLASSHASH_SIZE];
303 * We put the lock dependency chains into a hash-table as well, to cache
306 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
307 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
308 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
309 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
311 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
314 * The hash key of the lock dependency chains is a hash itself too:
315 * it's a hash of all locks taken up to that lock, including that lock.
316 * It's a 64-bit hash, because it's important for the keys to be
319 static inline u64 iterate_chain_key(u64 key, u32 idx)
321 u32 k0 = key, k1 = key >> 32;
323 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
325 return k0 | (u64)k1 << 32;
328 void lockdep_off(void)
330 current->lockdep_recursion++;
332 EXPORT_SYMBOL(lockdep_off);
334 void lockdep_on(void)
336 current->lockdep_recursion--;
338 EXPORT_SYMBOL(lockdep_on);
341 * Debugging switches:
345 #define VERY_VERBOSE 0
348 # define HARDIRQ_VERBOSE 1
349 # define SOFTIRQ_VERBOSE 1
351 # define HARDIRQ_VERBOSE 0
352 # define SOFTIRQ_VERBOSE 0
355 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
357 * Quick filtering for interesting events:
359 static int class_filter(struct lock_class *class)
363 if (class->name_version == 1 &&
364 !strcmp(class->name, "lockname"))
366 if (class->name_version == 1 &&
367 !strcmp(class->name, "&struct->lockfield"))
370 /* Filter everything else. 1 would be to allow everything else */
375 static int verbose(struct lock_class *class)
378 return class_filter(class);
384 * Stack-trace: tightly packed array of stack backtrace
385 * addresses. Protected by the graph_lock.
387 unsigned long nr_stack_trace_entries;
388 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
390 static void print_lockdep_off(const char *bug_msg)
392 printk(KERN_DEBUG "%s\n", bug_msg);
393 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
394 #ifdef CONFIG_LOCK_STAT
395 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
399 static int save_trace(struct stack_trace *trace)
401 trace->nr_entries = 0;
402 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
403 trace->entries = stack_trace + nr_stack_trace_entries;
407 save_stack_trace(trace);
410 * Some daft arches put -1 at the end to indicate its a full trace.
412 * <rant> this is buggy anyway, since it takes a whole extra entry so a
413 * complete trace that maxes out the entries provided will be reported
414 * as incomplete, friggin useless </rant>
416 if (trace->nr_entries != 0 &&
417 trace->entries[trace->nr_entries-1] == ULONG_MAX)
420 trace->max_entries = trace->nr_entries;
422 nr_stack_trace_entries += trace->nr_entries;
424 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
425 if (!debug_locks_off_graph_unlock())
428 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
437 unsigned int nr_hardirq_chains;
438 unsigned int nr_softirq_chains;
439 unsigned int nr_process_chains;
440 unsigned int max_lockdep_depth;
442 #ifdef CONFIG_DEBUG_LOCKDEP
444 * Various lockdep statistics:
446 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
453 #define __USAGE(__STATE) \
454 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
455 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
456 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
457 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
459 static const char *usage_str[] =
461 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
462 #include "lockdep_states.h"
464 [LOCK_USED] = "INITIAL USE",
467 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
469 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
472 static inline unsigned long lock_flag(enum lock_usage_bit bit)
477 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
481 if (class->usage_mask & lock_flag(bit + 2))
483 if (class->usage_mask & lock_flag(bit)) {
485 if (class->usage_mask & lock_flag(bit + 2))
492 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
496 #define LOCKDEP_STATE(__STATE) \
497 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
498 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
499 #include "lockdep_states.h"
505 static void __print_lock_name(struct lock_class *class)
507 char str[KSYM_NAME_LEN];
512 name = __get_key_name(class->key, str);
513 printk(KERN_CONT "%s", name);
515 printk(KERN_CONT "%s", name);
516 if (class->name_version > 1)
517 printk(KERN_CONT "#%d", class->name_version);
519 printk(KERN_CONT "/%d", class->subclass);
523 static void print_lock_name(struct lock_class *class)
525 char usage[LOCK_USAGE_CHARS];
527 get_usage_chars(class, usage);
529 printk(KERN_CONT " (");
530 __print_lock_name(class);
531 printk(KERN_CONT "){%s}", usage);
534 static void print_lockdep_cache(struct lockdep_map *lock)
537 char str[KSYM_NAME_LEN];
541 name = __get_key_name(lock->key->subkeys, str);
543 printk(KERN_CONT "%s", name);
546 static void print_lock(struct held_lock *hlock)
549 * We can be called locklessly through debug_show_all_locks() so be
550 * extra careful, the hlock might have been released and cleared.
552 unsigned int class_idx = hlock->class_idx;
554 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
557 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
558 printk(KERN_CONT "<RELEASED>\n");
562 print_lock_name(lock_classes + class_idx - 1);
563 printk(KERN_CONT ", at: [<%p>] %pS\n",
564 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip);
567 static void lockdep_print_held_locks(struct task_struct *curr)
569 int i, depth = curr->lockdep_depth;
572 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
575 printk("%d lock%s held by %s/%d:\n",
576 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
578 for (i = 0; i < depth; i++) {
580 print_lock(curr->held_locks + i);
584 static void print_kernel_ident(void)
586 printk("%s %.*s %s\n", init_utsname()->release,
587 (int)strcspn(init_utsname()->version, " "),
588 init_utsname()->version,
592 static int very_verbose(struct lock_class *class)
595 return class_filter(class);
601 * Is this the address of a static object:
604 static int static_obj(void *obj)
606 unsigned long start = (unsigned long) &_stext,
607 end = (unsigned long) &_end,
608 addr = (unsigned long) obj;
613 if ((addr >= start) && (addr < end))
616 if (arch_is_kernel_data(addr))
620 * in-kernel percpu var?
622 if (is_kernel_percpu_address(addr))
626 * module static or percpu var?
628 return is_module_address(addr) || is_module_percpu_address(addr);
633 * To make lock name printouts unique, we calculate a unique
634 * class->name_version generation counter:
636 static int count_matching_names(struct lock_class *new_class)
638 struct lock_class *class;
641 if (!new_class->name)
644 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
645 if (new_class->key - new_class->subclass == class->key)
646 return class->name_version;
647 if (class->name && !strcmp(class->name, new_class->name))
648 count = max(count, class->name_version);
655 * Register a lock's class in the hash-table, if the class is not present
656 * yet. Otherwise we look it up. We cache the result in the lock object
657 * itself, so actual lookup of the hash should be once per lock object.
659 static inline struct lock_class *
660 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
662 struct lockdep_subclass_key *key;
663 struct hlist_head *hash_head;
664 struct lock_class *class;
665 bool is_static = false;
667 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
670 "BUG: looking up invalid subclass: %u\n", subclass);
672 "turning off the locking correctness validator.\n");
678 * Static locks do not have their class-keys yet - for them the key
679 * is the lock object itself. If the lock is in the per cpu area,
680 * the canonical address of the lock (per cpu offset removed) is
683 if (unlikely(!lock->key)) {
684 unsigned long can_addr, addr = (unsigned long)lock;
686 if (__is_kernel_percpu_address(addr, &can_addr))
687 lock->key = (void *)can_addr;
688 else if (__is_module_percpu_address(addr, &can_addr))
689 lock->key = (void *)can_addr;
690 else if (static_obj(lock))
691 lock->key = (void *)lock;
693 return ERR_PTR(-EINVAL);
698 * NOTE: the class-key must be unique. For dynamic locks, a static
699 * lock_class_key variable is passed in through the mutex_init()
700 * (or spin_lock_init()) call - which acts as the key. For static
701 * locks we use the lock object itself as the key.
703 BUILD_BUG_ON(sizeof(struct lock_class_key) >
704 sizeof(struct lockdep_map));
706 key = lock->key->subkeys + subclass;
708 hash_head = classhashentry(key);
711 * We do an RCU walk of the hash, see lockdep_free_key_range().
713 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
716 hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
717 if (class->key == key) {
719 * Huh! same key, different name? Did someone trample
720 * on some memory? We're most confused.
722 WARN_ON_ONCE(class->name != lock->name);
727 return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
730 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
731 static void cross_init(struct lockdep_map *lock, int cross);
732 static int cross_lock(struct lockdep_map *lock);
733 static int lock_acquire_crosslock(struct held_lock *hlock);
734 static int lock_release_crosslock(struct lockdep_map *lock);
736 static inline void cross_init(struct lockdep_map *lock, int cross) {}
737 static inline int cross_lock(struct lockdep_map *lock) { return 0; }
738 static inline int lock_acquire_crosslock(struct held_lock *hlock) { return 2; }
739 static inline int lock_release_crosslock(struct lockdep_map *lock) { return 2; }
743 * Register a lock's class in the hash-table, if the class is not present
744 * yet. Otherwise we look it up. We cache the result in the lock object
745 * itself, so actual lookup of the hash should be once per lock object.
747 static struct lock_class *
748 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
750 struct lockdep_subclass_key *key;
751 struct hlist_head *hash_head;
752 struct lock_class *class;
754 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
756 class = look_up_lock_class(lock, subclass);
757 if (likely(!IS_ERR_OR_NULL(class)))
758 goto out_set_class_cache;
761 * Debug-check: all keys must be persistent!
765 printk("INFO: trying to register non-static key.\n");
766 printk("the code is fine but needs lockdep annotation.\n");
767 printk("turning off the locking correctness validator.\n");
772 key = lock->key->subkeys + subclass;
773 hash_head = classhashentry(key);
779 * We have to do the hash-walk again, to avoid races
782 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
783 if (class->key == key)
788 * Allocate a new key from the static array, and add it to
791 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
792 if (!debug_locks_off_graph_unlock()) {
796 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
800 class = lock_classes + nr_lock_classes++;
801 debug_atomic_inc(nr_unused_locks);
803 class->name = lock->name;
804 class->subclass = subclass;
805 INIT_LIST_HEAD(&class->lock_entry);
806 INIT_LIST_HEAD(&class->locks_before);
807 INIT_LIST_HEAD(&class->locks_after);
808 class->name_version = count_matching_names(class);
810 * We use RCU's safe list-add method to make
811 * parallel walking of the hash-list safe:
813 hlist_add_head_rcu(&class->hash_entry, hash_head);
815 * Add it to the global list of classes:
817 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
819 if (verbose(class)) {
822 printk("\nnew class %p: %s", class->key, class->name);
823 if (class->name_version > 1)
824 printk(KERN_CONT "#%d", class->name_version);
825 printk(KERN_CONT "\n");
836 if (!subclass || force)
837 lock->class_cache[0] = class;
838 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
839 lock->class_cache[subclass] = class;
842 * Hash collision, did we smoke some? We found a class with a matching
843 * hash but the subclass -- which is hashed in -- didn't match.
845 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
851 #ifdef CONFIG_PROVE_LOCKING
853 * Allocate a lockdep entry. (assumes the graph_lock held, returns
854 * with NULL on failure)
856 static struct lock_list *alloc_list_entry(void)
858 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
859 if (!debug_locks_off_graph_unlock())
862 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
866 return list_entries + nr_list_entries++;
870 * Add a new dependency to the head of the list:
872 static int add_lock_to_list(struct lock_class *this, struct list_head *head,
873 unsigned long ip, int distance,
874 struct stack_trace *trace)
876 struct lock_list *entry;
878 * Lock not present yet - get a new dependency struct and
879 * add it to the list:
881 entry = alloc_list_entry();
886 entry->distance = distance;
887 entry->trace = *trace;
889 * Both allocation and removal are done under the graph lock; but
890 * iteration is under RCU-sched; see look_up_lock_class() and
891 * lockdep_free_key_range().
893 list_add_tail_rcu(&entry->entry, head);
899 * For good efficiency of modular, we use power of 2
901 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
902 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
905 * The circular_queue and helpers is used to implement the
906 * breadth-first search(BFS)algorithem, by which we can build
907 * the shortest path from the next lock to be acquired to the
908 * previous held lock if there is a circular between them.
910 struct circular_queue {
911 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
912 unsigned int front, rear;
915 static struct circular_queue lock_cq;
917 unsigned int max_bfs_queue_depth;
919 static unsigned int lockdep_dependency_gen_id;
921 static inline void __cq_init(struct circular_queue *cq)
923 cq->front = cq->rear = 0;
924 lockdep_dependency_gen_id++;
927 static inline int __cq_empty(struct circular_queue *cq)
929 return (cq->front == cq->rear);
932 static inline int __cq_full(struct circular_queue *cq)
934 return ((cq->rear + 1) & CQ_MASK) == cq->front;
937 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
942 cq->element[cq->rear] = elem;
943 cq->rear = (cq->rear + 1) & CQ_MASK;
947 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
952 *elem = cq->element[cq->front];
953 cq->front = (cq->front + 1) & CQ_MASK;
957 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
959 return (cq->rear - cq->front) & CQ_MASK;
962 static inline void mark_lock_accessed(struct lock_list *lock,
963 struct lock_list *parent)
967 nr = lock - list_entries;
968 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
969 lock->parent = parent;
970 lock->class->dep_gen_id = lockdep_dependency_gen_id;
973 static inline unsigned long lock_accessed(struct lock_list *lock)
977 nr = lock - list_entries;
978 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
979 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
982 static inline struct lock_list *get_lock_parent(struct lock_list *child)
984 return child->parent;
987 static inline int get_lock_depth(struct lock_list *child)
990 struct lock_list *parent;
992 while ((parent = get_lock_parent(child))) {
999 static int __bfs(struct lock_list *source_entry,
1001 int (*match)(struct lock_list *entry, void *data),
1002 struct lock_list **target_entry,
1005 struct lock_list *entry;
1006 struct list_head *head;
1007 struct circular_queue *cq = &lock_cq;
1010 if (match(source_entry, data)) {
1011 *target_entry = source_entry;
1017 head = &source_entry->class->locks_after;
1019 head = &source_entry->class->locks_before;
1021 if (list_empty(head))
1025 __cq_enqueue(cq, (unsigned long)source_entry);
1027 while (!__cq_empty(cq)) {
1028 struct lock_list *lock;
1030 __cq_dequeue(cq, (unsigned long *)&lock);
1038 head = &lock->class->locks_after;
1040 head = &lock->class->locks_before;
1042 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1044 list_for_each_entry_rcu(entry, head, entry) {
1045 if (!lock_accessed(entry)) {
1046 unsigned int cq_depth;
1047 mark_lock_accessed(entry, lock);
1048 if (match(entry, data)) {
1049 *target_entry = entry;
1054 if (__cq_enqueue(cq, (unsigned long)entry)) {
1058 cq_depth = __cq_get_elem_count(cq);
1059 if (max_bfs_queue_depth < cq_depth)
1060 max_bfs_queue_depth = cq_depth;
1068 static inline int __bfs_forwards(struct lock_list *src_entry,
1070 int (*match)(struct lock_list *entry, void *data),
1071 struct lock_list **target_entry)
1073 return __bfs(src_entry, data, match, target_entry, 1);
1077 static inline int __bfs_backwards(struct lock_list *src_entry,
1079 int (*match)(struct lock_list *entry, void *data),
1080 struct lock_list **target_entry)
1082 return __bfs(src_entry, data, match, target_entry, 0);
1087 * Recursive, forwards-direction lock-dependency checking, used for
1088 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1093 * Print a dependency chain entry (this is only done when a deadlock
1094 * has been detected):
1097 print_circular_bug_entry(struct lock_list *target, int depth)
1099 if (debug_locks_silent)
1101 printk("\n-> #%u", depth);
1102 print_lock_name(target->class);
1103 printk(KERN_CONT ":\n");
1104 print_stack_trace(&target->trace, 6);
1110 print_circular_lock_scenario(struct held_lock *src,
1111 struct held_lock *tgt,
1112 struct lock_list *prt)
1114 struct lock_class *source = hlock_class(src);
1115 struct lock_class *target = hlock_class(tgt);
1116 struct lock_class *parent = prt->class;
1119 * A direct locking problem where unsafe_class lock is taken
1120 * directly by safe_class lock, then all we need to show
1121 * is the deadlock scenario, as it is obvious that the
1122 * unsafe lock is taken under the safe lock.
1124 * But if there is a chain instead, where the safe lock takes
1125 * an intermediate lock (middle_class) where this lock is
1126 * not the same as the safe lock, then the lock chain is
1127 * used to describe the problem. Otherwise we would need
1128 * to show a different CPU case for each link in the chain
1129 * from the safe_class lock to the unsafe_class lock.
1131 if (parent != source) {
1132 printk("Chain exists of:\n ");
1133 __print_lock_name(source);
1134 printk(KERN_CONT " --> ");
1135 __print_lock_name(parent);
1136 printk(KERN_CONT " --> ");
1137 __print_lock_name(target);
1138 printk(KERN_CONT "\n\n");
1141 if (cross_lock(tgt->instance)) {
1142 printk(" Possible unsafe locking scenario by crosslock:\n\n");
1143 printk(" CPU0 CPU1\n");
1144 printk(" ---- ----\n");
1146 __print_lock_name(parent);
1147 printk(KERN_CONT ");\n");
1149 __print_lock_name(target);
1150 printk(KERN_CONT ");\n");
1152 __print_lock_name(source);
1153 printk(KERN_CONT ");\n");
1155 __print_lock_name(target);
1156 printk(KERN_CONT ");\n");
1157 printk("\n *** DEADLOCK ***\n\n");
1159 printk(" Possible unsafe locking scenario:\n\n");
1160 printk(" CPU0 CPU1\n");
1161 printk(" ---- ----\n");
1163 __print_lock_name(target);
1164 printk(KERN_CONT ");\n");
1166 __print_lock_name(parent);
1167 printk(KERN_CONT ");\n");
1169 __print_lock_name(target);
1170 printk(KERN_CONT ");\n");
1172 __print_lock_name(source);
1173 printk(KERN_CONT ");\n");
1174 printk("\n *** DEADLOCK ***\n\n");
1179 * When a circular dependency is detected, print the
1183 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1184 struct held_lock *check_src,
1185 struct held_lock *check_tgt)
1187 struct task_struct *curr = current;
1189 if (debug_locks_silent)
1193 pr_warn("======================================================\n");
1194 pr_warn("WARNING: possible circular locking dependency detected\n");
1195 print_kernel_ident();
1196 pr_warn("------------------------------------------------------\n");
1197 pr_warn("%s/%d is trying to acquire lock:\n",
1198 curr->comm, task_pid_nr(curr));
1199 print_lock(check_src);
1201 if (cross_lock(check_tgt->instance))
1202 pr_warn("\nbut now in release context of a crosslock acquired at the following:\n");
1204 pr_warn("\nbut task is already holding lock:\n");
1206 print_lock(check_tgt);
1207 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1208 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1210 print_circular_bug_entry(entry, depth);
1215 static inline int class_equal(struct lock_list *entry, void *data)
1217 return entry->class == data;
1220 static noinline int print_circular_bug(struct lock_list *this,
1221 struct lock_list *target,
1222 struct held_lock *check_src,
1223 struct held_lock *check_tgt,
1224 struct stack_trace *trace)
1226 struct task_struct *curr = current;
1227 struct lock_list *parent;
1228 struct lock_list *first_parent;
1231 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1234 if (cross_lock(check_tgt->instance))
1235 this->trace = *trace;
1236 else if (!save_trace(&this->trace))
1239 depth = get_lock_depth(target);
1241 print_circular_bug_header(target, depth, check_src, check_tgt);
1243 parent = get_lock_parent(target);
1244 first_parent = parent;
1247 print_circular_bug_entry(parent, --depth);
1248 parent = get_lock_parent(parent);
1251 printk("\nother info that might help us debug this:\n\n");
1252 print_circular_lock_scenario(check_src, check_tgt,
1255 lockdep_print_held_locks(curr);
1257 printk("\nstack backtrace:\n");
1263 static noinline int print_bfs_bug(int ret)
1265 if (!debug_locks_off_graph_unlock())
1269 * Breadth-first-search failed, graph got corrupted?
1271 WARN(1, "lockdep bfs error:%d\n", ret);
1276 static int noop_count(struct lock_list *entry, void *data)
1278 (*(unsigned long *)data)++;
1282 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1284 unsigned long count = 0;
1285 struct lock_list *uninitialized_var(target_entry);
1287 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1291 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1293 unsigned long ret, flags;
1294 struct lock_list this;
1299 raw_local_irq_save(flags);
1300 current->lockdep_recursion = 1;
1301 arch_spin_lock(&lockdep_lock);
1302 ret = __lockdep_count_forward_deps(&this);
1303 arch_spin_unlock(&lockdep_lock);
1304 current->lockdep_recursion = 0;
1305 raw_local_irq_restore(flags);
1310 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1312 unsigned long count = 0;
1313 struct lock_list *uninitialized_var(target_entry);
1315 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1320 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1322 unsigned long ret, flags;
1323 struct lock_list this;
1328 raw_local_irq_save(flags);
1329 current->lockdep_recursion = 1;
1330 arch_spin_lock(&lockdep_lock);
1331 ret = __lockdep_count_backward_deps(&this);
1332 arch_spin_unlock(&lockdep_lock);
1333 current->lockdep_recursion = 0;
1334 raw_local_irq_restore(flags);
1340 * Prove that the dependency graph starting at <entry> can not
1341 * lead to <target>. Print an error and return 0 if it does.
1344 check_noncircular(struct lock_list *root, struct lock_class *target,
1345 struct lock_list **target_entry)
1349 debug_atomic_inc(nr_cyclic_checks);
1351 result = __bfs_forwards(root, target, class_equal, target_entry);
1357 check_redundant(struct lock_list *root, struct lock_class *target,
1358 struct lock_list **target_entry)
1362 debug_atomic_inc(nr_redundant_checks);
1364 result = __bfs_forwards(root, target, class_equal, target_entry);
1369 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1371 * Forwards and backwards subgraph searching, for the purposes of
1372 * proving that two subgraphs can be connected by a new dependency
1373 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1376 static inline int usage_match(struct lock_list *entry, void *bit)
1378 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1384 * Find a node in the forwards-direction dependency sub-graph starting
1385 * at @root->class that matches @bit.
1387 * Return 0 if such a node exists in the subgraph, and put that node
1388 * into *@target_entry.
1390 * Return 1 otherwise and keep *@target_entry unchanged.
1391 * Return <0 on error.
1394 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1395 struct lock_list **target_entry)
1399 debug_atomic_inc(nr_find_usage_forwards_checks);
1401 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1407 * Find a node in the backwards-direction dependency sub-graph starting
1408 * at @root->class that matches @bit.
1410 * Return 0 if such a node exists in the subgraph, and put that node
1411 * into *@target_entry.
1413 * Return 1 otherwise and keep *@target_entry unchanged.
1414 * Return <0 on error.
1417 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1418 struct lock_list **target_entry)
1422 debug_atomic_inc(nr_find_usage_backwards_checks);
1424 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1429 static void print_lock_class_header(struct lock_class *class, int depth)
1433 printk("%*s->", depth, "");
1434 print_lock_name(class);
1435 printk(KERN_CONT " ops: %lu", class->ops);
1436 printk(KERN_CONT " {\n");
1438 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1439 if (class->usage_mask & (1 << bit)) {
1442 len += printk("%*s %s", depth, "", usage_str[bit]);
1443 len += printk(KERN_CONT " at:\n");
1444 print_stack_trace(class->usage_traces + bit, len);
1447 printk("%*s }\n", depth, "");
1449 printk("%*s ... key at: [<%p>] %pS\n",
1450 depth, "", class->key, class->key);
1454 * printk the shortest lock dependencies from @start to @end in reverse order:
1457 print_shortest_lock_dependencies(struct lock_list *leaf,
1458 struct lock_list *root)
1460 struct lock_list *entry = leaf;
1463 /*compute depth from generated tree by BFS*/
1464 depth = get_lock_depth(leaf);
1467 print_lock_class_header(entry->class, depth);
1468 printk("%*s ... acquired at:\n", depth, "");
1469 print_stack_trace(&entry->trace, 2);
1472 if (depth == 0 && (entry != root)) {
1473 printk("lockdep:%s bad path found in chain graph\n", __func__);
1477 entry = get_lock_parent(entry);
1479 } while (entry && (depth >= 0));
1485 print_irq_lock_scenario(struct lock_list *safe_entry,
1486 struct lock_list *unsafe_entry,
1487 struct lock_class *prev_class,
1488 struct lock_class *next_class)
1490 struct lock_class *safe_class = safe_entry->class;
1491 struct lock_class *unsafe_class = unsafe_entry->class;
1492 struct lock_class *middle_class = prev_class;
1494 if (middle_class == safe_class)
1495 middle_class = next_class;
1498 * A direct locking problem where unsafe_class lock is taken
1499 * directly by safe_class lock, then all we need to show
1500 * is the deadlock scenario, as it is obvious that the
1501 * unsafe lock is taken under the safe lock.
1503 * But if there is a chain instead, where the safe lock takes
1504 * an intermediate lock (middle_class) where this lock is
1505 * not the same as the safe lock, then the lock chain is
1506 * used to describe the problem. Otherwise we would need
1507 * to show a different CPU case for each link in the chain
1508 * from the safe_class lock to the unsafe_class lock.
1510 if (middle_class != unsafe_class) {
1511 printk("Chain exists of:\n ");
1512 __print_lock_name(safe_class);
1513 printk(KERN_CONT " --> ");
1514 __print_lock_name(middle_class);
1515 printk(KERN_CONT " --> ");
1516 __print_lock_name(unsafe_class);
1517 printk(KERN_CONT "\n\n");
1520 printk(" Possible interrupt unsafe locking scenario:\n\n");
1521 printk(" CPU0 CPU1\n");
1522 printk(" ---- ----\n");
1524 __print_lock_name(unsafe_class);
1525 printk(KERN_CONT ");\n");
1526 printk(" local_irq_disable();\n");
1528 __print_lock_name(safe_class);
1529 printk(KERN_CONT ");\n");
1531 __print_lock_name(middle_class);
1532 printk(KERN_CONT ");\n");
1533 printk(" <Interrupt>\n");
1535 __print_lock_name(safe_class);
1536 printk(KERN_CONT ");\n");
1537 printk("\n *** DEADLOCK ***\n\n");
1541 print_bad_irq_dependency(struct task_struct *curr,
1542 struct lock_list *prev_root,
1543 struct lock_list *next_root,
1544 struct lock_list *backwards_entry,
1545 struct lock_list *forwards_entry,
1546 struct held_lock *prev,
1547 struct held_lock *next,
1548 enum lock_usage_bit bit1,
1549 enum lock_usage_bit bit2,
1550 const char *irqclass)
1552 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1556 pr_warn("=====================================================\n");
1557 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1558 irqclass, irqclass);
1559 print_kernel_ident();
1560 pr_warn("-----------------------------------------------------\n");
1561 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1562 curr->comm, task_pid_nr(curr),
1563 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1564 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1565 curr->hardirqs_enabled,
1566 curr->softirqs_enabled);
1569 pr_warn("\nand this task is already holding:\n");
1571 pr_warn("which would create a new lock dependency:\n");
1572 print_lock_name(hlock_class(prev));
1574 print_lock_name(hlock_class(next));
1577 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1579 print_lock_name(backwards_entry->class);
1580 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
1582 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1584 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
1585 print_lock_name(forwards_entry->class);
1586 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1589 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1591 pr_warn("\nother info that might help us debug this:\n\n");
1592 print_irq_lock_scenario(backwards_entry, forwards_entry,
1593 hlock_class(prev), hlock_class(next));
1595 lockdep_print_held_locks(curr);
1597 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
1598 if (!save_trace(&prev_root->trace))
1600 print_shortest_lock_dependencies(backwards_entry, prev_root);
1602 pr_warn("\nthe dependencies between the lock to be acquired");
1603 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
1604 if (!save_trace(&next_root->trace))
1606 print_shortest_lock_dependencies(forwards_entry, next_root);
1608 pr_warn("\nstack backtrace:\n");
1615 check_usage(struct task_struct *curr, struct held_lock *prev,
1616 struct held_lock *next, enum lock_usage_bit bit_backwards,
1617 enum lock_usage_bit bit_forwards, const char *irqclass)
1620 struct lock_list this, that;
1621 struct lock_list *uninitialized_var(target_entry);
1622 struct lock_list *uninitialized_var(target_entry1);
1626 this.class = hlock_class(prev);
1627 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1629 return print_bfs_bug(ret);
1634 that.class = hlock_class(next);
1635 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1637 return print_bfs_bug(ret);
1641 return print_bad_irq_dependency(curr, &this, &that,
1642 target_entry, target_entry1,
1644 bit_backwards, bit_forwards, irqclass);
1647 static const char *state_names[] = {
1648 #define LOCKDEP_STATE(__STATE) \
1649 __stringify(__STATE),
1650 #include "lockdep_states.h"
1651 #undef LOCKDEP_STATE
1654 static const char *state_rnames[] = {
1655 #define LOCKDEP_STATE(__STATE) \
1656 __stringify(__STATE)"-READ",
1657 #include "lockdep_states.h"
1658 #undef LOCKDEP_STATE
1661 static inline const char *state_name(enum lock_usage_bit bit)
1663 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1666 static int exclusive_bit(int new_bit)
1674 * bit 0 - write/read
1675 * bit 1 - used_in/enabled
1679 int state = new_bit & ~3;
1680 int dir = new_bit & 2;
1683 * keep state, bit flip the direction and strip read.
1685 return state | (dir ^ 2);
1688 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1689 struct held_lock *next, enum lock_usage_bit bit)
1692 * Prove that the new dependency does not connect a hardirq-safe
1693 * lock with a hardirq-unsafe lock - to achieve this we search
1694 * the backwards-subgraph starting at <prev>, and the
1695 * forwards-subgraph starting at <next>:
1697 if (!check_usage(curr, prev, next, bit,
1698 exclusive_bit(bit), state_name(bit)))
1704 * Prove that the new dependency does not connect a hardirq-safe-read
1705 * lock with a hardirq-unsafe lock - to achieve this we search
1706 * the backwards-subgraph starting at <prev>, and the
1707 * forwards-subgraph starting at <next>:
1709 if (!check_usage(curr, prev, next, bit,
1710 exclusive_bit(bit), state_name(bit)))
1717 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1718 struct held_lock *next)
1720 #define LOCKDEP_STATE(__STATE) \
1721 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1723 #include "lockdep_states.h"
1724 #undef LOCKDEP_STATE
1729 static void inc_chains(void)
1731 if (current->hardirq_context)
1732 nr_hardirq_chains++;
1734 if (current->softirq_context)
1735 nr_softirq_chains++;
1737 nr_process_chains++;
1744 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1745 struct held_lock *next)
1750 static inline void inc_chains(void)
1752 nr_process_chains++;
1758 print_deadlock_scenario(struct held_lock *nxt,
1759 struct held_lock *prv)
1761 struct lock_class *next = hlock_class(nxt);
1762 struct lock_class *prev = hlock_class(prv);
1764 printk(" Possible unsafe locking scenario:\n\n");
1768 __print_lock_name(prev);
1769 printk(KERN_CONT ");\n");
1771 __print_lock_name(next);
1772 printk(KERN_CONT ");\n");
1773 printk("\n *** DEADLOCK ***\n\n");
1774 printk(" May be due to missing lock nesting notation\n\n");
1778 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1779 struct held_lock *next)
1781 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1785 pr_warn("============================================\n");
1786 pr_warn("WARNING: possible recursive locking detected\n");
1787 print_kernel_ident();
1788 pr_warn("--------------------------------------------\n");
1789 pr_warn("%s/%d is trying to acquire lock:\n",
1790 curr->comm, task_pid_nr(curr));
1792 pr_warn("\nbut task is already holding lock:\n");
1795 pr_warn("\nother info that might help us debug this:\n");
1796 print_deadlock_scenario(next, prev);
1797 lockdep_print_held_locks(curr);
1799 pr_warn("\nstack backtrace:\n");
1806 * Check whether we are holding such a class already.
1808 * (Note that this has to be done separately, because the graph cannot
1809 * detect such classes of deadlocks.)
1811 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1814 check_deadlock(struct task_struct *curr, struct held_lock *next,
1815 struct lockdep_map *next_instance, int read)
1817 struct held_lock *prev;
1818 struct held_lock *nest = NULL;
1821 for (i = 0; i < curr->lockdep_depth; i++) {
1822 prev = curr->held_locks + i;
1824 if (prev->instance == next->nest_lock)
1827 if (hlock_class(prev) != hlock_class(next))
1831 * Allow read-after-read recursion of the same
1832 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1834 if ((read == 2) && prev->read)
1838 * We're holding the nest_lock, which serializes this lock's
1839 * nesting behaviour.
1844 if (cross_lock(prev->instance))
1847 return print_deadlock_bug(curr, prev, next);
1853 * There was a chain-cache miss, and we are about to add a new dependency
1854 * to a previous lock. We recursively validate the following rules:
1856 * - would the adding of the <prev> -> <next> dependency create a
1857 * circular dependency in the graph? [== circular deadlock]
1859 * - does the new prev->next dependency connect any hardirq-safe lock
1860 * (in the full backwards-subgraph starting at <prev>) with any
1861 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1862 * <next>)? [== illegal lock inversion with hardirq contexts]
1864 * - does the new prev->next dependency connect any softirq-safe lock
1865 * (in the full backwards-subgraph starting at <prev>) with any
1866 * softirq-unsafe lock (in the full forwards-subgraph starting at
1867 * <next>)? [== illegal lock inversion with softirq contexts]
1869 * any of these scenarios could lead to a deadlock.
1871 * Then if all the validations pass, we add the forwards and backwards
1875 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1876 struct held_lock *next, int distance, struct stack_trace *trace,
1877 int (*save)(struct stack_trace *trace))
1879 struct lock_list *uninitialized_var(target_entry);
1880 struct lock_list *entry;
1881 struct lock_list this;
1885 * Prove that the new <prev> -> <next> dependency would not
1886 * create a circular dependency in the graph. (We do this by
1887 * forward-recursing into the graph starting at <next>, and
1888 * checking whether we can reach <prev>.)
1890 * We are using global variables to control the recursion, to
1891 * keep the stackframe size of the recursive functions low:
1893 this.class = hlock_class(next);
1895 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1896 if (unlikely(!ret)) {
1897 if (!trace->entries) {
1899 * If @save fails here, the printing might trigger
1900 * a WARN but because of the !nr_entries it should
1901 * not do bad things.
1905 return print_circular_bug(&this, target_entry, next, prev, trace);
1907 else if (unlikely(ret < 0))
1908 return print_bfs_bug(ret);
1910 if (!check_prev_add_irq(curr, prev, next))
1914 * For recursive read-locks we do all the dependency checks,
1915 * but we dont store read-triggered dependencies (only
1916 * write-triggered dependencies). This ensures that only the
1917 * write-side dependencies matter, and that if for example a
1918 * write-lock never takes any other locks, then the reads are
1919 * equivalent to a NOP.
1921 if (next->read == 2 || prev->read == 2)
1924 * Is the <prev> -> <next> dependency already present?
1926 * (this may occur even though this is a new chain: consider
1927 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1928 * chains - the second one will be new, but L1 already has
1929 * L2 added to its dependency list, due to the first chain.)
1931 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1932 if (entry->class == hlock_class(next)) {
1934 entry->distance = 1;
1940 * Is the <prev> -> <next> link redundant?
1942 this.class = hlock_class(prev);
1944 ret = check_redundant(&this, hlock_class(next), &target_entry);
1946 debug_atomic_inc(nr_redundant);
1950 return print_bfs_bug(ret);
1953 if (!trace->entries && !save(trace))
1957 * Ok, all validations passed, add the new lock
1958 * to the previous lock's dependency list:
1960 ret = add_lock_to_list(hlock_class(next),
1961 &hlock_class(prev)->locks_after,
1962 next->acquire_ip, distance, trace);
1967 ret = add_lock_to_list(hlock_class(prev),
1968 &hlock_class(next)->locks_before,
1969 next->acquire_ip, distance, trace);
1977 * Add the dependency to all directly-previous locks that are 'relevant'.
1978 * The ones that are relevant are (in increasing distance from curr):
1979 * all consecutive trylock entries and the final non-trylock entry - or
1980 * the end of this context's lock-chain - whichever comes first.
1983 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1985 int depth = curr->lockdep_depth;
1986 struct held_lock *hlock;
1987 struct stack_trace trace = {
1997 * Depth must not be zero for a non-head lock:
2002 * At least two relevant locks must exist for this
2005 if (curr->held_locks[depth].irq_context !=
2006 curr->held_locks[depth-1].irq_context)
2010 int distance = curr->lockdep_depth - depth + 1;
2011 hlock = curr->held_locks + depth - 1;
2013 * Only non-crosslock entries get new dependencies added.
2014 * Crosslock entries will be added by commit later:
2016 if (!cross_lock(hlock->instance)) {
2018 * Only non-recursive-read entries get new dependencies
2021 if (hlock->read != 2 && hlock->check) {
2022 int ret = check_prev_add(curr, hlock, next,
2023 distance, &trace, save_trace);
2028 * Stop after the first non-trylock entry,
2029 * as non-trylock entries have added their
2030 * own direct dependencies already, so this
2031 * lock is connected to them indirectly:
2033 if (!hlock->trylock)
2039 * End of lock-stack?
2044 * Stop the search if we cross into another context:
2046 if (curr->held_locks[depth].irq_context !=
2047 curr->held_locks[depth-1].irq_context)
2052 if (!debug_locks_off_graph_unlock())
2056 * Clearly we all shouldn't be here, but since we made it we
2057 * can reliable say we messed up our state. See the above two
2058 * gotos for reasons why we could possibly end up here.
2065 unsigned long nr_lock_chains;
2066 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2067 int nr_chain_hlocks;
2068 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2070 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2072 return lock_classes + chain_hlocks[chain->base + i];
2076 * Returns the index of the first held_lock of the current chain
2078 static inline int get_first_held_lock(struct task_struct *curr,
2079 struct held_lock *hlock)
2082 struct held_lock *hlock_curr;
2084 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2085 hlock_curr = curr->held_locks + i;
2086 if (hlock_curr->irq_context != hlock->irq_context)
2094 #ifdef CONFIG_DEBUG_LOCKDEP
2096 * Returns the next chain_key iteration
2098 static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2100 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2102 printk(" class_idx:%d -> chain_key:%016Lx",
2104 (unsigned long long)new_chain_key);
2105 return new_chain_key;
2109 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2111 struct held_lock *hlock;
2113 int depth = curr->lockdep_depth;
2116 printk("depth: %u\n", depth + 1);
2117 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2118 hlock = curr->held_locks + i;
2119 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2124 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2125 print_lock(hlock_next);
2128 static void print_chain_keys_chain(struct lock_chain *chain)
2134 printk("depth: %u\n", chain->depth);
2135 for (i = 0; i < chain->depth; i++) {
2136 class_id = chain_hlocks[chain->base + i];
2137 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2139 print_lock_name(lock_classes + class_id);
2144 static void print_collision(struct task_struct *curr,
2145 struct held_lock *hlock_next,
2146 struct lock_chain *chain)
2149 pr_warn("============================\n");
2150 pr_warn("WARNING: chain_key collision\n");
2151 print_kernel_ident();
2152 pr_warn("----------------------------\n");
2153 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2154 pr_warn("Hash chain already cached but the contents don't match!\n");
2156 pr_warn("Held locks:");
2157 print_chain_keys_held_locks(curr, hlock_next);
2159 pr_warn("Locks in cached chain:");
2160 print_chain_keys_chain(chain);
2162 pr_warn("\nstack backtrace:\n");
2168 * Checks whether the chain and the current held locks are consistent
2169 * in depth and also in content. If they are not it most likely means
2170 * that there was a collision during the calculation of the chain_key.
2171 * Returns: 0 not passed, 1 passed
2173 static int check_no_collision(struct task_struct *curr,
2174 struct held_lock *hlock,
2175 struct lock_chain *chain)
2177 #ifdef CONFIG_DEBUG_LOCKDEP
2180 i = get_first_held_lock(curr, hlock);
2182 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2183 print_collision(curr, hlock, chain);
2187 for (j = 0; j < chain->depth - 1; j++, i++) {
2188 id = curr->held_locks[i].class_idx - 1;
2190 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2191 print_collision(curr, hlock, chain);
2200 * This is for building a chain between just two different classes,
2201 * instead of adding a new hlock upon current, which is done by
2202 * add_chain_cache().
2204 * This can be called in any context with two classes, while
2205 * add_chain_cache() must be done within the lock owener's context
2206 * since it uses hlock which might be racy in another context.
2208 static inline int add_chain_cache_classes(unsigned int prev,
2210 unsigned int irq_context,
2213 struct hlist_head *hash_head = chainhashentry(chain_key);
2214 struct lock_chain *chain;
2217 * Allocate a new chain entry from the static array, and add
2222 * We might need to take the graph lock, ensure we've got IRQs
2223 * disabled to make this an IRQ-safe lock.. for recursion reasons
2224 * lockdep won't complain about its own locking errors.
2226 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2229 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2230 if (!debug_locks_off_graph_unlock())
2233 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2238 chain = lock_chains + nr_lock_chains++;
2239 chain->chain_key = chain_key;
2240 chain->irq_context = irq_context;
2242 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2243 chain->base = nr_chain_hlocks;
2244 nr_chain_hlocks += chain->depth;
2245 chain_hlocks[chain->base] = prev - 1;
2246 chain_hlocks[chain->base + 1] = next -1;
2248 #ifdef CONFIG_DEBUG_LOCKDEP
2250 * Important for check_no_collision().
2253 if (!debug_locks_off_graph_unlock())
2256 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2262 hlist_add_head_rcu(&chain->entry, hash_head);
2263 debug_atomic_inc(chain_lookup_misses);
2270 * Adds a dependency chain into chain hashtable. And must be called with
2273 * Return 0 if fail, and graph_lock is released.
2274 * Return 1 if succeed, with graph_lock held.
2276 static inline int add_chain_cache(struct task_struct *curr,
2277 struct held_lock *hlock,
2280 struct lock_class *class = hlock_class(hlock);
2281 struct hlist_head *hash_head = chainhashentry(chain_key);
2282 struct lock_chain *chain;
2286 * Allocate a new chain entry from the static array, and add
2291 * We might need to take the graph lock, ensure we've got IRQs
2292 * disabled to make this an IRQ-safe lock.. for recursion reasons
2293 * lockdep won't complain about its own locking errors.
2295 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2298 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2299 if (!debug_locks_off_graph_unlock())
2302 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2306 chain = lock_chains + nr_lock_chains++;
2307 chain->chain_key = chain_key;
2308 chain->irq_context = hlock->irq_context;
2309 i = get_first_held_lock(curr, hlock);
2310 chain->depth = curr->lockdep_depth + 1 - i;
2312 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2313 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2314 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2316 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2317 chain->base = nr_chain_hlocks;
2318 for (j = 0; j < chain->depth - 1; j++, i++) {
2319 int lock_id = curr->held_locks[i].class_idx - 1;
2320 chain_hlocks[chain->base + j] = lock_id;
2322 chain_hlocks[chain->base + j] = class - lock_classes;
2325 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2326 nr_chain_hlocks += chain->depth;
2328 #ifdef CONFIG_DEBUG_LOCKDEP
2330 * Important for check_no_collision().
2332 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
2333 if (!debug_locks_off_graph_unlock())
2336 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2342 hlist_add_head_rcu(&chain->entry, hash_head);
2343 debug_atomic_inc(chain_lookup_misses);
2350 * Look up a dependency chain.
2352 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2354 struct hlist_head *hash_head = chainhashentry(chain_key);
2355 struct lock_chain *chain;
2358 * We can walk it lock-free, because entries only get added
2361 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2362 if (chain->chain_key == chain_key) {
2363 debug_atomic_inc(chain_lookup_hits);
2371 * If the key is not present yet in dependency chain cache then
2372 * add it and return 1 - in this case the new dependency chain is
2373 * validated. If the key is already hashed, return 0.
2374 * (On return with 1 graph_lock is held.)
2376 static inline int lookup_chain_cache_add(struct task_struct *curr,
2377 struct held_lock *hlock,
2380 struct lock_class *class = hlock_class(hlock);
2381 struct lock_chain *chain = lookup_chain_cache(chain_key);
2385 if (!check_no_collision(curr, hlock, chain))
2388 if (very_verbose(class)) {
2389 printk("\nhash chain already cached, key: "
2390 "%016Lx tail class: [%p] %s\n",
2391 (unsigned long long)chain_key,
2392 class->key, class->name);
2398 if (very_verbose(class)) {
2399 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2400 (unsigned long long)chain_key, class->key, class->name);
2407 * We have to walk the chain again locked - to avoid duplicates:
2409 chain = lookup_chain_cache(chain_key);
2415 if (!add_chain_cache(curr, hlock, chain_key))
2421 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2422 struct held_lock *hlock, int chain_head, u64 chain_key)
2425 * Trylock needs to maintain the stack of held locks, but it
2426 * does not add new dependencies, because trylock can be done
2429 * We look up the chain_key and do the O(N^2) check and update of
2430 * the dependencies only if this is a new dependency chain.
2431 * (If lookup_chain_cache_add() return with 1 it acquires
2432 * graph_lock for us)
2434 if (!hlock->trylock && hlock->check &&
2435 lookup_chain_cache_add(curr, hlock, chain_key)) {
2437 * Check whether last held lock:
2439 * - is irq-safe, if this lock is irq-unsafe
2440 * - is softirq-safe, if this lock is hardirq-unsafe
2442 * And check whether the new lock's dependency graph
2443 * could lead back to the previous lock.
2445 * any of these scenarios could lead to a deadlock. If
2448 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2453 * Mark recursive read, as we jump over it when
2454 * building dependencies (just like we jump over
2460 * Add dependency only if this lock is not the head
2461 * of the chain, and if it's not a secondary read-lock:
2463 if (!chain_head && ret != 2) {
2464 if (!check_prevs_add(curr, hlock))
2470 /* after lookup_chain_cache_add(): */
2471 if (unlikely(!debug_locks))
2478 static inline int validate_chain(struct task_struct *curr,
2479 struct lockdep_map *lock, struct held_lock *hlock,
2480 int chain_head, u64 chain_key)
2487 * We are building curr_chain_key incrementally, so double-check
2488 * it from scratch, to make sure that it's done correctly:
2490 static void check_chain_key(struct task_struct *curr)
2492 #ifdef CONFIG_DEBUG_LOCKDEP
2493 struct held_lock *hlock, *prev_hlock = NULL;
2497 for (i = 0; i < curr->lockdep_depth; i++) {
2498 hlock = curr->held_locks + i;
2499 if (chain_key != hlock->prev_chain_key) {
2502 * We got mighty confused, our chain keys don't match
2503 * with what we expect, someone trample on our task state?
2505 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2506 curr->lockdep_depth, i,
2507 (unsigned long long)chain_key,
2508 (unsigned long long)hlock->prev_chain_key);
2512 * Whoops ran out of static storage again?
2514 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
2517 if (prev_hlock && (prev_hlock->irq_context !=
2518 hlock->irq_context))
2520 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
2523 if (chain_key != curr->curr_chain_key) {
2526 * More smoking hash instead of calculating it, damn see these
2527 * numbers float.. I bet that a pink elephant stepped on my memory.
2529 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2530 curr->lockdep_depth, i,
2531 (unsigned long long)chain_key,
2532 (unsigned long long)curr->curr_chain_key);
2538 print_usage_bug_scenario(struct held_lock *lock)
2540 struct lock_class *class = hlock_class(lock);
2542 printk(" Possible unsafe locking scenario:\n\n");
2546 __print_lock_name(class);
2547 printk(KERN_CONT ");\n");
2548 printk(" <Interrupt>\n");
2550 __print_lock_name(class);
2551 printk(KERN_CONT ");\n");
2552 printk("\n *** DEADLOCK ***\n\n");
2556 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2557 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2559 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2563 pr_warn("================================\n");
2564 pr_warn("WARNING: inconsistent lock state\n");
2565 print_kernel_ident();
2566 pr_warn("--------------------------------\n");
2568 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2569 usage_str[prev_bit], usage_str[new_bit]);
2571 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2572 curr->comm, task_pid_nr(curr),
2573 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2574 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2575 trace_hardirqs_enabled(curr),
2576 trace_softirqs_enabled(curr));
2579 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
2580 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2582 print_irqtrace_events(curr);
2583 pr_warn("\nother info that might help us debug this:\n");
2584 print_usage_bug_scenario(this);
2586 lockdep_print_held_locks(curr);
2588 pr_warn("\nstack backtrace:\n");
2595 * Print out an error if an invalid bit is set:
2598 valid_state(struct task_struct *curr, struct held_lock *this,
2599 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2601 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2602 return print_usage_bug(curr, this, bad_bit, new_bit);
2606 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2607 enum lock_usage_bit new_bit);
2609 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2612 * print irq inversion bug:
2615 print_irq_inversion_bug(struct task_struct *curr,
2616 struct lock_list *root, struct lock_list *other,
2617 struct held_lock *this, int forwards,
2618 const char *irqclass)
2620 struct lock_list *entry = other;
2621 struct lock_list *middle = NULL;
2624 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2628 pr_warn("========================================================\n");
2629 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2630 print_kernel_ident();
2631 pr_warn("--------------------------------------------------------\n");
2632 pr_warn("%s/%d just changed the state of lock:\n",
2633 curr->comm, task_pid_nr(curr));
2636 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2638 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2639 print_lock_name(other->class);
2640 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2642 pr_warn("\nother info that might help us debug this:\n");
2644 /* Find a middle lock (if one exists) */
2645 depth = get_lock_depth(other);
2647 if (depth == 0 && (entry != root)) {
2648 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
2652 entry = get_lock_parent(entry);
2654 } while (entry && entry != root && (depth >= 0));
2656 print_irq_lock_scenario(root, other,
2657 middle ? middle->class : root->class, other->class);
2659 print_irq_lock_scenario(other, root,
2660 middle ? middle->class : other->class, root->class);
2662 lockdep_print_held_locks(curr);
2664 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2665 if (!save_trace(&root->trace))
2667 print_shortest_lock_dependencies(other, root);
2669 pr_warn("\nstack backtrace:\n");
2676 * Prove that in the forwards-direction subgraph starting at <this>
2677 * there is no lock matching <mask>:
2680 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2681 enum lock_usage_bit bit, const char *irqclass)
2684 struct lock_list root;
2685 struct lock_list *uninitialized_var(target_entry);
2688 root.class = hlock_class(this);
2689 ret = find_usage_forwards(&root, bit, &target_entry);
2691 return print_bfs_bug(ret);
2695 return print_irq_inversion_bug(curr, &root, target_entry,
2700 * Prove that in the backwards-direction subgraph starting at <this>
2701 * there is no lock matching <mask>:
2704 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2705 enum lock_usage_bit bit, const char *irqclass)
2708 struct lock_list root;
2709 struct lock_list *uninitialized_var(target_entry);
2712 root.class = hlock_class(this);
2713 ret = find_usage_backwards(&root, bit, &target_entry);
2715 return print_bfs_bug(ret);
2719 return print_irq_inversion_bug(curr, &root, target_entry,
2723 void print_irqtrace_events(struct task_struct *curr)
2725 printk("irq event stamp: %u\n", curr->irq_events);
2726 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2727 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2728 (void *)curr->hardirq_enable_ip);
2729 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2730 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2731 (void *)curr->hardirq_disable_ip);
2732 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2733 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2734 (void *)curr->softirq_enable_ip);
2735 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2736 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2737 (void *)curr->softirq_disable_ip);
2740 static int HARDIRQ_verbose(struct lock_class *class)
2743 return class_filter(class);
2748 static int SOFTIRQ_verbose(struct lock_class *class)
2751 return class_filter(class);
2756 #define STRICT_READ_CHECKS 1
2758 static int (*state_verbose_f[])(struct lock_class *class) = {
2759 #define LOCKDEP_STATE(__STATE) \
2761 #include "lockdep_states.h"
2762 #undef LOCKDEP_STATE
2765 static inline int state_verbose(enum lock_usage_bit bit,
2766 struct lock_class *class)
2768 return state_verbose_f[bit >> 2](class);
2771 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2772 enum lock_usage_bit bit, const char *name);
2775 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2776 enum lock_usage_bit new_bit)
2778 int excl_bit = exclusive_bit(new_bit);
2779 int read = new_bit & 1;
2780 int dir = new_bit & 2;
2783 * mark USED_IN has to look forwards -- to ensure no dependency
2784 * has ENABLED state, which would allow recursion deadlocks.
2786 * mark ENABLED has to look backwards -- to ensure no dependee
2787 * has USED_IN state, which, again, would allow recursion deadlocks.
2789 check_usage_f usage = dir ?
2790 check_usage_backwards : check_usage_forwards;
2793 * Validate that this particular lock does not have conflicting
2796 if (!valid_state(curr, this, new_bit, excl_bit))
2800 * Validate that the lock dependencies don't have conflicting usage
2803 if ((!read || !dir || STRICT_READ_CHECKS) &&
2804 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2808 * Check for read in write conflicts
2811 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2814 if (STRICT_READ_CHECKS &&
2815 !usage(curr, this, excl_bit + 1,
2816 state_name(new_bit + 1)))
2820 if (state_verbose(new_bit, hlock_class(this)))
2827 #define LOCKDEP_STATE(__STATE) __STATE,
2828 #include "lockdep_states.h"
2829 #undef LOCKDEP_STATE
2833 * Mark all held locks with a usage bit:
2836 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2838 enum lock_usage_bit usage_bit;
2839 struct held_lock *hlock;
2842 for (i = 0; i < curr->lockdep_depth; i++) {
2843 hlock = curr->held_locks + i;
2845 usage_bit = 2 + (mark << 2); /* ENABLED */
2847 usage_bit += 1; /* READ */
2849 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2854 if (!mark_lock(curr, hlock, usage_bit))
2862 * Hardirqs will be enabled:
2864 static void __trace_hardirqs_on_caller(unsigned long ip)
2866 struct task_struct *curr = current;
2868 /* we'll do an OFF -> ON transition: */
2869 curr->hardirqs_enabled = 1;
2872 * We are going to turn hardirqs on, so set the
2873 * usage bit for all held locks:
2875 if (!mark_held_locks(curr, HARDIRQ))
2878 * If we have softirqs enabled, then set the usage
2879 * bit for all held locks. (disabled hardirqs prevented
2880 * this bit from being set before)
2882 if (curr->softirqs_enabled)
2883 if (!mark_held_locks(curr, SOFTIRQ))
2886 curr->hardirq_enable_ip = ip;
2887 curr->hardirq_enable_event = ++curr->irq_events;
2888 debug_atomic_inc(hardirqs_on_events);
2891 __visible void trace_hardirqs_on_caller(unsigned long ip)
2893 time_hardirqs_on(CALLER_ADDR0, ip);
2895 if (unlikely(!debug_locks || current->lockdep_recursion))
2898 if (unlikely(current->hardirqs_enabled)) {
2900 * Neither irq nor preemption are disabled here
2901 * so this is racy by nature but losing one hit
2902 * in a stat is not a big deal.
2904 __debug_atomic_inc(redundant_hardirqs_on);
2909 * We're enabling irqs and according to our state above irqs weren't
2910 * already enabled, yet we find the hardware thinks they are in fact
2911 * enabled.. someone messed up their IRQ state tracing.
2913 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2917 * See the fine text that goes along with this variable definition.
2919 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2923 * Can't allow enabling interrupts while in an interrupt handler,
2924 * that's general bad form and such. Recursion, limited stack etc..
2926 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2929 current->lockdep_recursion = 1;
2930 __trace_hardirqs_on_caller(ip);
2931 current->lockdep_recursion = 0;
2933 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2935 void trace_hardirqs_on(void)
2937 trace_hardirqs_on_caller(CALLER_ADDR0);
2939 EXPORT_SYMBOL(trace_hardirqs_on);
2942 * Hardirqs were disabled:
2944 __visible void trace_hardirqs_off_caller(unsigned long ip)
2946 struct task_struct *curr = current;
2948 time_hardirqs_off(CALLER_ADDR0, ip);
2950 if (unlikely(!debug_locks || current->lockdep_recursion))
2954 * So we're supposed to get called after you mask local IRQs, but for
2955 * some reason the hardware doesn't quite think you did a proper job.
2957 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2960 if (curr->hardirqs_enabled) {
2962 * We have done an ON -> OFF transition:
2964 curr->hardirqs_enabled = 0;
2965 curr->hardirq_disable_ip = ip;
2966 curr->hardirq_disable_event = ++curr->irq_events;
2967 debug_atomic_inc(hardirqs_off_events);
2969 debug_atomic_inc(redundant_hardirqs_off);
2971 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2973 void trace_hardirqs_off(void)
2975 trace_hardirqs_off_caller(CALLER_ADDR0);
2977 EXPORT_SYMBOL(trace_hardirqs_off);
2980 * Softirqs will be enabled:
2982 void trace_softirqs_on(unsigned long ip)
2984 struct task_struct *curr = current;
2986 if (unlikely(!debug_locks || current->lockdep_recursion))
2990 * We fancy IRQs being disabled here, see softirq.c, avoids
2991 * funny state and nesting things.
2993 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2996 if (curr->softirqs_enabled) {
2997 debug_atomic_inc(redundant_softirqs_on);
3001 current->lockdep_recursion = 1;
3003 * We'll do an OFF -> ON transition:
3005 curr->softirqs_enabled = 1;
3006 curr->softirq_enable_ip = ip;
3007 curr->softirq_enable_event = ++curr->irq_events;
3008 debug_atomic_inc(softirqs_on_events);
3010 * We are going to turn softirqs on, so set the
3011 * usage bit for all held locks, if hardirqs are
3014 if (curr->hardirqs_enabled)
3015 mark_held_locks(curr, SOFTIRQ);
3016 current->lockdep_recursion = 0;
3020 * Softirqs were disabled:
3022 void trace_softirqs_off(unsigned long ip)
3024 struct task_struct *curr = current;
3026 if (unlikely(!debug_locks || current->lockdep_recursion))
3030 * We fancy IRQs being disabled here, see softirq.c
3032 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3035 if (curr->softirqs_enabled) {
3037 * We have done an ON -> OFF transition:
3039 curr->softirqs_enabled = 0;
3040 curr->softirq_disable_ip = ip;
3041 curr->softirq_disable_event = ++curr->irq_events;
3042 debug_atomic_inc(softirqs_off_events);
3044 * Whoops, we wanted softirqs off, so why aren't they?
3046 DEBUG_LOCKS_WARN_ON(!softirq_count());
3048 debug_atomic_inc(redundant_softirqs_off);
3051 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3054 * If non-trylock use in a hardirq or softirq context, then
3055 * mark the lock as used in these contexts:
3057 if (!hlock->trylock) {
3059 if (curr->hardirq_context)
3060 if (!mark_lock(curr, hlock,
3061 LOCK_USED_IN_HARDIRQ_READ))
3063 if (curr->softirq_context)
3064 if (!mark_lock(curr, hlock,
3065 LOCK_USED_IN_SOFTIRQ_READ))
3068 if (curr->hardirq_context)
3069 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3071 if (curr->softirq_context)
3072 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3076 if (!hlock->hardirqs_off) {
3078 if (!mark_lock(curr, hlock,
3079 LOCK_ENABLED_HARDIRQ_READ))
3081 if (curr->softirqs_enabled)
3082 if (!mark_lock(curr, hlock,
3083 LOCK_ENABLED_SOFTIRQ_READ))
3086 if (!mark_lock(curr, hlock,
3087 LOCK_ENABLED_HARDIRQ))
3089 if (curr->softirqs_enabled)
3090 if (!mark_lock(curr, hlock,
3091 LOCK_ENABLED_SOFTIRQ))
3099 static inline unsigned int task_irq_context(struct task_struct *task)
3101 return 2 * !!task->hardirq_context + !!task->softirq_context;
3104 static int separate_irq_context(struct task_struct *curr,
3105 struct held_lock *hlock)
3107 unsigned int depth = curr->lockdep_depth;
3110 * Keep track of points where we cross into an interrupt context:
3113 struct held_lock *prev_hlock;
3115 prev_hlock = curr->held_locks + depth-1;
3117 * If we cross into another context, reset the
3118 * hash key (this also prevents the checking and the
3119 * adding of the dependency to 'prev'):
3121 if (prev_hlock->irq_context != hlock->irq_context)
3127 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3130 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3131 enum lock_usage_bit new_bit)
3133 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3137 static inline int mark_irqflags(struct task_struct *curr,
3138 struct held_lock *hlock)
3143 static inline unsigned int task_irq_context(struct task_struct *task)
3148 static inline int separate_irq_context(struct task_struct *curr,
3149 struct held_lock *hlock)
3154 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3157 * Mark a lock with a usage bit, and validate the state transition:
3159 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3160 enum lock_usage_bit new_bit)
3162 unsigned int new_mask = 1 << new_bit, ret = 1;
3165 * If already set then do not dirty the cacheline,
3166 * nor do any checks:
3168 if (likely(hlock_class(this)->usage_mask & new_mask))
3174 * Make sure we didn't race:
3176 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3181 hlock_class(this)->usage_mask |= new_mask;
3183 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
3187 #define LOCKDEP_STATE(__STATE) \
3188 case LOCK_USED_IN_##__STATE: \
3189 case LOCK_USED_IN_##__STATE##_READ: \
3190 case LOCK_ENABLED_##__STATE: \
3191 case LOCK_ENABLED_##__STATE##_READ:
3192 #include "lockdep_states.h"
3193 #undef LOCKDEP_STATE
3194 ret = mark_lock_irq(curr, this, new_bit);
3199 debug_atomic_dec(nr_unused_locks);
3202 if (!debug_locks_off_graph_unlock())
3211 * We must printk outside of the graph_lock:
3214 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3216 print_irqtrace_events(curr);
3224 * Initialize a lock instance's lock-class mapping info:
3226 static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
3227 struct lock_class_key *key, int subclass)
3231 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3232 lock->class_cache[i] = NULL;
3234 #ifdef CONFIG_LOCK_STAT
3235 lock->cpu = raw_smp_processor_id();
3239 * Can't be having no nameless bastards around this place!
3241 if (DEBUG_LOCKS_WARN_ON(!name)) {
3242 lock->name = "NULL";
3249 * No key, no joy, we need to hash something.
3251 if (DEBUG_LOCKS_WARN_ON(!key))
3254 * Sanity check, the lock-class key must be persistent:
3256 if (!static_obj(key)) {
3257 printk("BUG: key %p not in .data!\n", key);
3259 * What it says above ^^^^^, I suggest you read it.
3261 DEBUG_LOCKS_WARN_ON(1);
3266 if (unlikely(!debug_locks))
3270 unsigned long flags;
3272 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3275 raw_local_irq_save(flags);
3276 current->lockdep_recursion = 1;
3277 register_lock_class(lock, subclass, 1);
3278 current->lockdep_recursion = 0;
3279 raw_local_irq_restore(flags);
3283 void lockdep_init_map(struct lockdep_map *lock, const char *name,
3284 struct lock_class_key *key, int subclass)
3286 cross_init(lock, 0);
3287 __lockdep_init_map(lock, name, key, subclass);
3289 EXPORT_SYMBOL_GPL(lockdep_init_map);
3291 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
3292 void lockdep_init_map_crosslock(struct lockdep_map *lock, const char *name,
3293 struct lock_class_key *key, int subclass)
3295 cross_init(lock, 1);
3296 __lockdep_init_map(lock, name, key, subclass);
3298 EXPORT_SYMBOL_GPL(lockdep_init_map_crosslock);
3301 struct lock_class_key __lockdep_no_validate__;
3302 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3305 print_lock_nested_lock_not_held(struct task_struct *curr,
3306 struct held_lock *hlock,
3309 if (!debug_locks_off())
3311 if (debug_locks_silent)
3315 pr_warn("==================================\n");
3316 pr_warn("WARNING: Nested lock was not taken\n");
3317 print_kernel_ident();
3318 pr_warn("----------------------------------\n");
3320 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3323 pr_warn("\nbut this task is not holding:\n");
3324 pr_warn("%s\n", hlock->nest_lock->name);
3326 pr_warn("\nstack backtrace:\n");
3329 pr_warn("\nother info that might help us debug this:\n");
3330 lockdep_print_held_locks(curr);
3332 pr_warn("\nstack backtrace:\n");
3338 static int __lock_is_held(struct lockdep_map *lock, int read);
3341 * This gets called for every mutex_lock*()/spin_lock*() operation.
3342 * We maintain the dependency maps and validate the locking attempt:
3344 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3345 int trylock, int read, int check, int hardirqs_off,
3346 struct lockdep_map *nest_lock, unsigned long ip,
3347 int references, int pin_count)
3349 struct task_struct *curr = current;
3350 struct lock_class *class = NULL;
3351 struct held_lock *hlock;
3358 if (unlikely(!debug_locks))
3362 * Lockdep should run with IRQs disabled, otherwise we could
3363 * get an interrupt which would want to take locks, which would
3364 * end up in lockdep and have you got a head-ache already?
3366 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3369 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3372 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3373 class = lock->class_cache[subclass];
3377 if (unlikely(!class)) {
3378 class = register_lock_class(lock, subclass, 0);
3382 atomic_inc((atomic_t *)&class->ops);
3383 if (very_verbose(class)) {
3384 printk("\nacquire class [%p] %s", class->key, class->name);
3385 if (class->name_version > 1)
3386 printk(KERN_CONT "#%d", class->name_version);
3387 printk(KERN_CONT "\n");
3392 * Add the lock to the list of currently held locks.
3393 * (we dont increase the depth just yet, up until the
3394 * dependency checks are done)
3396 depth = curr->lockdep_depth;
3398 * Ran out of static storage for our per-task lock stack again have we?
3400 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3403 class_idx = class - lock_classes + 1;
3405 /* TODO: nest_lock is not implemented for crosslock yet. */
3406 if (depth && !cross_lock(lock)) {
3407 hlock = curr->held_locks + depth - 1;
3408 if (hlock->class_idx == class_idx && nest_lock) {
3412 if (!hlock->references)
3413 hlock->references++;
3415 hlock->references += references;
3418 if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
3425 hlock = curr->held_locks + depth;
3427 * Plain impossible, we just registered it and checked it weren't no
3428 * NULL like.. I bet this mushroom I ate was good!
3430 if (DEBUG_LOCKS_WARN_ON(!class))
3432 hlock->class_idx = class_idx;
3433 hlock->acquire_ip = ip;
3434 hlock->instance = lock;
3435 hlock->nest_lock = nest_lock;
3436 hlock->irq_context = task_irq_context(curr);
3437 hlock->trylock = trylock;
3439 hlock->check = check;
3440 hlock->hardirqs_off = !!hardirqs_off;
3441 hlock->references = references;
3442 #ifdef CONFIG_LOCK_STAT
3443 hlock->waittime_stamp = 0;
3444 hlock->holdtime_stamp = lockstat_clock();
3446 hlock->pin_count = pin_count;
3448 if (check && !mark_irqflags(curr, hlock))
3451 /* mark it as used: */
3452 if (!mark_lock(curr, hlock, LOCK_USED))
3456 * Calculate the chain hash: it's the combined hash of all the
3457 * lock keys along the dependency chain. We save the hash value
3458 * at every step so that we can get the current hash easily
3459 * after unlock. The chain hash is then used to cache dependency
3462 * The 'key ID' is what is the most compact key value to drive
3463 * the hash, not class->key.
3466 * Whoops, we did it again.. ran straight out of our static allocation.
3468 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
3471 chain_key = curr->curr_chain_key;
3474 * How can we have a chain hash when we ain't got no keys?!
3476 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3481 hlock->prev_chain_key = chain_key;
3482 if (separate_irq_context(curr, hlock)) {
3486 chain_key = iterate_chain_key(chain_key, class_idx);
3488 if (nest_lock && !__lock_is_held(nest_lock, -1))
3489 return print_lock_nested_lock_not_held(curr, hlock, ip);
3491 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3494 ret = lock_acquire_crosslock(hlock);
3496 * 2 means normal acquire operations are needed. Otherwise, it's
3497 * ok just to return with '0:fail, 1:success'.
3502 curr->curr_chain_key = chain_key;
3503 curr->lockdep_depth++;
3504 check_chain_key(curr);
3505 #ifdef CONFIG_DEBUG_LOCKDEP
3506 if (unlikely(!debug_locks))
3509 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3511 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3512 printk(KERN_DEBUG "depth: %i max: %lu!\n",
3513 curr->lockdep_depth, MAX_LOCK_DEPTH);
3515 lockdep_print_held_locks(current);
3516 debug_show_all_locks();
3522 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3523 max_lockdep_depth = curr->lockdep_depth;
3529 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3532 if (!debug_locks_off())
3534 if (debug_locks_silent)
3538 pr_warn("=====================================\n");
3539 pr_warn("WARNING: bad unlock balance detected!\n");
3540 print_kernel_ident();
3541 pr_warn("-------------------------------------\n");
3542 pr_warn("%s/%d is trying to release lock (",
3543 curr->comm, task_pid_nr(curr));
3544 print_lockdep_cache(lock);
3547 pr_warn("but there are no more locks to release!\n");
3548 pr_warn("\nother info that might help us debug this:\n");
3549 lockdep_print_held_locks(curr);
3551 pr_warn("\nstack backtrace:\n");
3557 static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3559 if (hlock->instance == lock)
3562 if (hlock->references) {
3563 struct lock_class *class = lock->class_cache[0];
3566 class = look_up_lock_class(lock, 0);
3569 * If look_up_lock_class() failed to find a class, we're trying
3570 * to test if we hold a lock that has never yet been acquired.
3571 * Clearly if the lock hasn't been acquired _ever_, we're not
3572 * holding it either, so report failure.
3574 if (IS_ERR_OR_NULL(class))
3578 * References, but not a lock we're actually ref-counting?
3579 * State got messed up, follow the sites that change ->references
3580 * and try to make sense of it.
3582 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3585 if (hlock->class_idx == class - lock_classes + 1)
3592 /* @depth must not be zero */
3593 static struct held_lock *find_held_lock(struct task_struct *curr,
3594 struct lockdep_map *lock,
3595 unsigned int depth, int *idx)
3597 struct held_lock *ret, *hlock, *prev_hlock;
3601 hlock = curr->held_locks + i;
3603 if (match_held_lock(hlock, lock))
3607 for (i--, prev_hlock = hlock--;
3609 i--, prev_hlock = hlock--) {
3611 * We must not cross into another context:
3613 if (prev_hlock->irq_context != hlock->irq_context) {
3617 if (match_held_lock(hlock, lock)) {
3628 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3631 struct held_lock *hlock;
3633 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3634 if (!__lock_acquire(hlock->instance,
3635 hlock_class(hlock)->subclass,
3637 hlock->read, hlock->check,
3638 hlock->hardirqs_off,
3639 hlock->nest_lock, hlock->acquire_ip,
3640 hlock->references, hlock->pin_count))
3647 __lock_set_class(struct lockdep_map *lock, const char *name,
3648 struct lock_class_key *key, unsigned int subclass,
3651 struct task_struct *curr = current;
3652 struct held_lock *hlock;
3653 struct lock_class *class;
3657 depth = curr->lockdep_depth;
3659 * This function is about (re)setting the class of a held lock,
3660 * yet we're not actually holding any locks. Naughty user!
3662 if (DEBUG_LOCKS_WARN_ON(!depth))
3665 hlock = find_held_lock(curr, lock, depth, &i);
3667 return print_unlock_imbalance_bug(curr, lock, ip);
3669 lockdep_init_map(lock, name, key, 0);
3670 class = register_lock_class(lock, subclass, 0);
3671 hlock->class_idx = class - lock_classes + 1;
3673 curr->lockdep_depth = i;
3674 curr->curr_chain_key = hlock->prev_chain_key;
3676 if (reacquire_held_locks(curr, depth, i))
3680 * I took it apart and put it back together again, except now I have
3681 * these 'spare' parts.. where shall I put them.
3683 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3688 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3690 struct task_struct *curr = current;
3691 struct held_lock *hlock;
3695 if (unlikely(!debug_locks))
3698 depth = curr->lockdep_depth;
3700 * This function is about (re)setting the class of a held lock,
3701 * yet we're not actually holding any locks. Naughty user!
3703 if (DEBUG_LOCKS_WARN_ON(!depth))
3706 hlock = find_held_lock(curr, lock, depth, &i);
3708 return print_unlock_imbalance_bug(curr, lock, ip);
3710 curr->lockdep_depth = i;
3711 curr->curr_chain_key = hlock->prev_chain_key;
3713 WARN(hlock->read, "downgrading a read lock");
3715 hlock->acquire_ip = ip;
3717 if (reacquire_held_locks(curr, depth, i))
3721 * I took it apart and put it back together again, except now I have
3722 * these 'spare' parts.. where shall I put them.
3724 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3730 * Remove the lock to the list of currently held locks - this gets
3731 * called on mutex_unlock()/spin_unlock*() (or on a failed
3732 * mutex_lock_interruptible()).
3734 * @nested is an hysterical artifact, needs a tree wide cleanup.
3737 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3739 struct task_struct *curr = current;
3740 struct held_lock *hlock;
3744 if (unlikely(!debug_locks))
3747 ret = lock_release_crosslock(lock);
3749 * 2 means normal release operations are needed. Otherwise, it's
3750 * ok just to return with '0:fail, 1:success'.
3755 depth = curr->lockdep_depth;
3757 * So we're all set to release this lock.. wait what lock? We don't
3758 * own any locks, you've been drinking again?
3760 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3761 return print_unlock_imbalance_bug(curr, lock, ip);
3764 * Check whether the lock exists in the current stack
3767 hlock = find_held_lock(curr, lock, depth, &i);
3769 return print_unlock_imbalance_bug(curr, lock, ip);
3771 if (hlock->instance == lock)
3772 lock_release_holdtime(hlock);
3774 WARN(hlock->pin_count, "releasing a pinned lock\n");
3776 if (hlock->references) {
3777 hlock->references--;
3778 if (hlock->references) {
3780 * We had, and after removing one, still have
3781 * references, the current lock stack is still
3782 * valid. We're done!
3789 * We have the right lock to unlock, 'hlock' points to it.
3790 * Now we remove it from the stack, and add back the other
3791 * entries (if any), recalculating the hash along the way:
3794 curr->lockdep_depth = i;
3795 curr->curr_chain_key = hlock->prev_chain_key;
3797 if (reacquire_held_locks(curr, depth, i + 1))
3801 * We had N bottles of beer on the wall, we drank one, but now
3802 * there's not N-1 bottles of beer left on the wall...
3804 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3810 static int __lock_is_held(struct lockdep_map *lock, int read)
3812 struct task_struct *curr = current;
3815 for (i = 0; i < curr->lockdep_depth; i++) {
3816 struct held_lock *hlock = curr->held_locks + i;
3818 if (match_held_lock(hlock, lock)) {
3819 if (read == -1 || hlock->read == read)
3829 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3831 struct pin_cookie cookie = NIL_COOKIE;
3832 struct task_struct *curr = current;
3835 if (unlikely(!debug_locks))
3838 for (i = 0; i < curr->lockdep_depth; i++) {
3839 struct held_lock *hlock = curr->held_locks + i;
3841 if (match_held_lock(hlock, lock)) {
3843 * Grab 16bits of randomness; this is sufficient to not
3844 * be guessable and still allows some pin nesting in
3845 * our u32 pin_count.
3847 cookie.val = 1 + (prandom_u32() >> 16);
3848 hlock->pin_count += cookie.val;
3853 WARN(1, "pinning an unheld lock\n");
3857 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3859 struct task_struct *curr = current;
3862 if (unlikely(!debug_locks))
3865 for (i = 0; i < curr->lockdep_depth; i++) {
3866 struct held_lock *hlock = curr->held_locks + i;
3868 if (match_held_lock(hlock, lock)) {
3869 hlock->pin_count += cookie.val;
3874 WARN(1, "pinning an unheld lock\n");
3877 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3879 struct task_struct *curr = current;
3882 if (unlikely(!debug_locks))
3885 for (i = 0; i < curr->lockdep_depth; i++) {
3886 struct held_lock *hlock = curr->held_locks + i;
3888 if (match_held_lock(hlock, lock)) {
3889 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3892 hlock->pin_count -= cookie.val;
3894 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3895 hlock->pin_count = 0;
3901 WARN(1, "unpinning an unheld lock\n");
3905 * Check whether we follow the irq-flags state precisely:
3907 static void check_flags(unsigned long flags)
3909 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3910 defined(CONFIG_TRACE_IRQFLAGS)
3914 if (irqs_disabled_flags(flags)) {
3915 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3916 printk("possible reason: unannotated irqs-off.\n");
3919 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3920 printk("possible reason: unannotated irqs-on.\n");
3925 * We dont accurately track softirq state in e.g.
3926 * hardirq contexts (such as on 4KSTACKS), so only
3927 * check if not in hardirq contexts:
3929 if (!hardirq_count()) {
3930 if (softirq_count()) {
3931 /* like the above, but with softirqs */
3932 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3934 /* lick the above, does it taste good? */
3935 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3940 print_irqtrace_events(current);
3944 void lock_set_class(struct lockdep_map *lock, const char *name,
3945 struct lock_class_key *key, unsigned int subclass,
3948 unsigned long flags;
3950 if (unlikely(current->lockdep_recursion))
3953 raw_local_irq_save(flags);
3954 current->lockdep_recursion = 1;
3956 if (__lock_set_class(lock, name, key, subclass, ip))
3957 check_chain_key(current);
3958 current->lockdep_recursion = 0;
3959 raw_local_irq_restore(flags);
3961 EXPORT_SYMBOL_GPL(lock_set_class);
3963 void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3965 unsigned long flags;
3967 if (unlikely(current->lockdep_recursion))
3970 raw_local_irq_save(flags);
3971 current->lockdep_recursion = 1;
3973 if (__lock_downgrade(lock, ip))
3974 check_chain_key(current);
3975 current->lockdep_recursion = 0;
3976 raw_local_irq_restore(flags);
3978 EXPORT_SYMBOL_GPL(lock_downgrade);
3981 * We are not always called with irqs disabled - do that here,
3982 * and also avoid lockdep recursion:
3984 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3985 int trylock, int read, int check,
3986 struct lockdep_map *nest_lock, unsigned long ip)
3988 unsigned long flags;
3990 if (unlikely(current->lockdep_recursion))
3993 raw_local_irq_save(flags);
3996 current->lockdep_recursion = 1;
3997 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3998 __lock_acquire(lock, subclass, trylock, read, check,
3999 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
4000 current->lockdep_recursion = 0;
4001 raw_local_irq_restore(flags);
4003 EXPORT_SYMBOL_GPL(lock_acquire);
4005 void lock_release(struct lockdep_map *lock, int nested,
4008 unsigned long flags;
4010 if (unlikely(current->lockdep_recursion))
4013 raw_local_irq_save(flags);
4015 current->lockdep_recursion = 1;
4016 trace_lock_release(lock, ip);
4017 if (__lock_release(lock, nested, ip))
4018 check_chain_key(current);
4019 current->lockdep_recursion = 0;
4020 raw_local_irq_restore(flags);
4022 EXPORT_SYMBOL_GPL(lock_release);
4024 int lock_is_held_type(struct lockdep_map *lock, int read)
4026 unsigned long flags;
4029 if (unlikely(current->lockdep_recursion))
4030 return 1; /* avoid false negative lockdep_assert_held() */
4032 raw_local_irq_save(flags);
4035 current->lockdep_recursion = 1;
4036 ret = __lock_is_held(lock, read);
4037 current->lockdep_recursion = 0;
4038 raw_local_irq_restore(flags);
4042 EXPORT_SYMBOL_GPL(lock_is_held_type);
4044 struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
4046 struct pin_cookie cookie = NIL_COOKIE;
4047 unsigned long flags;
4049 if (unlikely(current->lockdep_recursion))
4052 raw_local_irq_save(flags);
4055 current->lockdep_recursion = 1;
4056 cookie = __lock_pin_lock(lock);
4057 current->lockdep_recursion = 0;
4058 raw_local_irq_restore(flags);
4062 EXPORT_SYMBOL_GPL(lock_pin_lock);
4064 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4066 unsigned long flags;
4068 if (unlikely(current->lockdep_recursion))
4071 raw_local_irq_save(flags);
4074 current->lockdep_recursion = 1;
4075 __lock_repin_lock(lock, cookie);
4076 current->lockdep_recursion = 0;
4077 raw_local_irq_restore(flags);
4079 EXPORT_SYMBOL_GPL(lock_repin_lock);
4081 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4083 unsigned long flags;
4085 if (unlikely(current->lockdep_recursion))
4088 raw_local_irq_save(flags);
4091 current->lockdep_recursion = 1;
4092 __lock_unpin_lock(lock, cookie);
4093 current->lockdep_recursion = 0;
4094 raw_local_irq_restore(flags);
4096 EXPORT_SYMBOL_GPL(lock_unpin_lock);
4098 #ifdef CONFIG_LOCK_STAT
4100 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
4103 if (!debug_locks_off())
4105 if (debug_locks_silent)
4109 pr_warn("=================================\n");
4110 pr_warn("WARNING: bad contention detected!\n");
4111 print_kernel_ident();
4112 pr_warn("---------------------------------\n");
4113 pr_warn("%s/%d is trying to contend lock (",
4114 curr->comm, task_pid_nr(curr));
4115 print_lockdep_cache(lock);
4118 pr_warn("but there are no locks held!\n");
4119 pr_warn("\nother info that might help us debug this:\n");
4120 lockdep_print_held_locks(curr);
4122 pr_warn("\nstack backtrace:\n");
4129 __lock_contended(struct lockdep_map *lock, unsigned long ip)
4131 struct task_struct *curr = current;
4132 struct held_lock *hlock;
4133 struct lock_class_stats *stats;
4135 int i, contention_point, contending_point;
4137 depth = curr->lockdep_depth;
4139 * Whee, we contended on this lock, except it seems we're not
4140 * actually trying to acquire anything much at all..
4142 if (DEBUG_LOCKS_WARN_ON(!depth))
4145 hlock = find_held_lock(curr, lock, depth, &i);
4147 print_lock_contention_bug(curr, lock, ip);
4151 if (hlock->instance != lock)
4154 hlock->waittime_stamp = lockstat_clock();
4156 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4157 contending_point = lock_point(hlock_class(hlock)->contending_point,
4160 stats = get_lock_stats(hlock_class(hlock));
4161 if (contention_point < LOCKSTAT_POINTS)
4162 stats->contention_point[contention_point]++;
4163 if (contending_point < LOCKSTAT_POINTS)
4164 stats->contending_point[contending_point]++;
4165 if (lock->cpu != smp_processor_id())
4166 stats->bounces[bounce_contended + !!hlock->read]++;
4167 put_lock_stats(stats);
4171 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
4173 struct task_struct *curr = current;
4174 struct held_lock *hlock;
4175 struct lock_class_stats *stats;
4177 u64 now, waittime = 0;
4180 depth = curr->lockdep_depth;
4182 * Yay, we acquired ownership of this lock we didn't try to
4183 * acquire, how the heck did that happen?
4185 if (DEBUG_LOCKS_WARN_ON(!depth))
4188 hlock = find_held_lock(curr, lock, depth, &i);
4190 print_lock_contention_bug(curr, lock, _RET_IP_);
4194 if (hlock->instance != lock)
4197 cpu = smp_processor_id();
4198 if (hlock->waittime_stamp) {
4199 now = lockstat_clock();
4200 waittime = now - hlock->waittime_stamp;
4201 hlock->holdtime_stamp = now;
4204 trace_lock_acquired(lock, ip);
4206 stats = get_lock_stats(hlock_class(hlock));
4209 lock_time_inc(&stats->read_waittime, waittime);
4211 lock_time_inc(&stats->write_waittime, waittime);
4213 if (lock->cpu != cpu)
4214 stats->bounces[bounce_acquired + !!hlock->read]++;
4215 put_lock_stats(stats);
4221 void lock_contended(struct lockdep_map *lock, unsigned long ip)
4223 unsigned long flags;
4225 if (unlikely(!lock_stat || !debug_locks))
4228 if (unlikely(current->lockdep_recursion))
4231 raw_local_irq_save(flags);
4233 current->lockdep_recursion = 1;
4234 trace_lock_contended(lock, ip);
4235 __lock_contended(lock, ip);
4236 current->lockdep_recursion = 0;
4237 raw_local_irq_restore(flags);
4239 EXPORT_SYMBOL_GPL(lock_contended);
4241 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
4243 unsigned long flags;
4245 if (unlikely(!lock_stat || !debug_locks))
4248 if (unlikely(current->lockdep_recursion))
4251 raw_local_irq_save(flags);
4253 current->lockdep_recursion = 1;
4254 __lock_acquired(lock, ip);
4255 current->lockdep_recursion = 0;
4256 raw_local_irq_restore(flags);
4258 EXPORT_SYMBOL_GPL(lock_acquired);
4262 * Used by the testsuite, sanitize the validator state
4263 * after a simulated failure:
4266 void lockdep_reset(void)
4268 unsigned long flags;
4271 raw_local_irq_save(flags);
4272 current->curr_chain_key = 0;
4273 current->lockdep_depth = 0;
4274 current->lockdep_recursion = 0;
4275 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4276 nr_hardirq_chains = 0;
4277 nr_softirq_chains = 0;
4278 nr_process_chains = 0;
4280 for (i = 0; i < CHAINHASH_SIZE; i++)
4281 INIT_HLIST_HEAD(chainhash_table + i);
4282 raw_local_irq_restore(flags);
4285 static void zap_class(struct lock_class *class)
4290 * Remove all dependencies this lock is
4293 for (i = 0; i < nr_list_entries; i++) {
4294 if (list_entries[i].class == class)
4295 list_del_rcu(&list_entries[i].entry);
4298 * Unhash the class and remove it from the all_lock_classes list:
4300 hlist_del_rcu(&class->hash_entry);
4301 list_del_rcu(&class->lock_entry);
4303 RCU_INIT_POINTER(class->key, NULL);
4304 RCU_INIT_POINTER(class->name, NULL);
4307 static inline int within(const void *addr, void *start, unsigned long size)
4309 return addr >= start && addr < start + size;
4313 * Used in module.c to remove lock classes from memory that is going to be
4314 * freed; and possibly re-used by other modules.
4316 * We will have had one sync_sched() before getting here, so we're guaranteed
4317 * nobody will look up these exact classes -- they're properly dead but still
4320 void lockdep_free_key_range(void *start, unsigned long size)
4322 struct lock_class *class;
4323 struct hlist_head *head;
4324 unsigned long flags;
4328 raw_local_irq_save(flags);
4329 locked = graph_lock();
4332 * Unhash all classes that were created by this module:
4334 for (i = 0; i < CLASSHASH_SIZE; i++) {
4335 head = classhash_table + i;
4336 hlist_for_each_entry_rcu(class, head, hash_entry) {
4337 if (within(class->key, start, size))
4339 else if (within(class->name, start, size))
4346 raw_local_irq_restore(flags);
4349 * Wait for any possible iterators from look_up_lock_class() to pass
4350 * before continuing to free the memory they refer to.
4352 * sync_sched() is sufficient because the read-side is IRQ disable.
4354 synchronize_sched();
4357 * XXX at this point we could return the resources to the pool;
4358 * instead we leak them. We would need to change to bitmap allocators
4359 * instead of the linear allocators we have now.
4363 void lockdep_reset_lock(struct lockdep_map *lock)
4365 struct lock_class *class;
4366 struct hlist_head *head;
4367 unsigned long flags;
4371 raw_local_irq_save(flags);
4374 * Remove all classes this lock might have:
4376 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4378 * If the class exists we look it up and zap it:
4380 class = look_up_lock_class(lock, j);
4381 if (!IS_ERR_OR_NULL(class))
4385 * Debug check: in the end all mapped classes should
4388 locked = graph_lock();
4389 for (i = 0; i < CLASSHASH_SIZE; i++) {
4390 head = classhash_table + i;
4391 hlist_for_each_entry_rcu(class, head, hash_entry) {
4394 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4395 match |= class == lock->class_cache[j];
4397 if (unlikely(match)) {
4398 if (debug_locks_off_graph_unlock()) {
4400 * We all just reset everything, how did it match?
4412 raw_local_irq_restore(flags);
4415 void __init lockdep_info(void)
4417 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4419 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
4420 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4421 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
4422 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
4423 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4424 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4425 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4427 printk(" memory used by lock dependency info: %lu kB\n",
4428 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4429 sizeof(struct list_head) * CLASSHASH_SIZE +
4430 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4431 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
4432 sizeof(struct list_head) * CHAINHASH_SIZE
4433 #ifdef CONFIG_PROVE_LOCKING
4434 + sizeof(struct circular_queue)
4439 printk(" per task-struct memory footprint: %lu bytes\n",
4440 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
4444 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
4445 const void *mem_to, struct held_lock *hlock)
4447 if (!debug_locks_off())
4449 if (debug_locks_silent)
4453 pr_warn("=========================\n");
4454 pr_warn("WARNING: held lock freed!\n");
4455 print_kernel_ident();
4456 pr_warn("-------------------------\n");
4457 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4458 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
4460 lockdep_print_held_locks(curr);
4462 pr_warn("\nstack backtrace:\n");
4466 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4467 const void* lock_from, unsigned long lock_len)
4469 return lock_from + lock_len <= mem_from ||
4470 mem_from + mem_len <= lock_from;
4474 * Called when kernel memory is freed (or unmapped), or if a lock
4475 * is destroyed or reinitialized - this code checks whether there is
4476 * any held lock in the memory range of <from> to <to>:
4478 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4480 struct task_struct *curr = current;
4481 struct held_lock *hlock;
4482 unsigned long flags;
4485 if (unlikely(!debug_locks))
4488 raw_local_irq_save(flags);
4489 for (i = 0; i < curr->lockdep_depth; i++) {
4490 hlock = curr->held_locks + i;
4492 if (not_in_range(mem_from, mem_len, hlock->instance,
4493 sizeof(*hlock->instance)))
4496 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
4499 raw_local_irq_restore(flags);
4501 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
4503 static void print_held_locks_bug(void)
4505 if (!debug_locks_off())
4507 if (debug_locks_silent)
4511 pr_warn("====================================\n");
4512 pr_warn("WARNING: %s/%d still has locks held!\n",
4513 current->comm, task_pid_nr(current));
4514 print_kernel_ident();
4515 pr_warn("------------------------------------\n");
4516 lockdep_print_held_locks(current);
4517 pr_warn("\nstack backtrace:\n");
4521 void debug_check_no_locks_held(void)
4523 if (unlikely(current->lockdep_depth > 0))
4524 print_held_locks_bug();
4526 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
4529 void debug_show_all_locks(void)
4531 struct task_struct *g, *p;
4535 if (unlikely(!debug_locks)) {
4536 pr_warn("INFO: lockdep is turned off.\n");
4539 pr_warn("\nShowing all locks held in the system:\n");
4542 * Here we try to get the tasklist_lock as hard as possible,
4543 * if not successful after 2 seconds we ignore it (but keep
4544 * trying). This is to enable a debug printout even if a
4545 * tasklist_lock-holding task deadlocks or crashes.
4548 if (!read_trylock(&tasklist_lock)) {
4550 pr_warn("hm, tasklist_lock locked, retrying... ");
4553 pr_cont(" #%d", 10-count);
4557 pr_cont(" ignoring it.\n");
4561 pr_cont(" locked it.\n");
4564 do_each_thread(g, p) {
4566 * It's not reliable to print a task's held locks
4567 * if it's not sleeping (or if it's not the current
4570 if (p->state == TASK_RUNNING && p != current)
4572 if (p->lockdep_depth)
4573 lockdep_print_held_locks(p);
4575 if (read_trylock(&tasklist_lock))
4577 } while_each_thread(g, p);
4580 pr_warn("=============================================\n\n");
4583 read_unlock(&tasklist_lock);
4585 EXPORT_SYMBOL_GPL(debug_show_all_locks);
4589 * Careful: only use this function if you are sure that
4590 * the task cannot run in parallel!
4592 void debug_show_held_locks(struct task_struct *task)
4594 if (unlikely(!debug_locks)) {
4595 printk("INFO: lockdep is turned off.\n");
4598 lockdep_print_held_locks(task);
4600 EXPORT_SYMBOL_GPL(debug_show_held_locks);
4602 asmlinkage __visible void lockdep_sys_exit(void)
4604 struct task_struct *curr = current;
4606 if (unlikely(curr->lockdep_depth)) {
4607 if (!debug_locks_off())
4610 pr_warn("================================================\n");
4611 pr_warn("WARNING: lock held when returning to user space!\n");
4612 print_kernel_ident();
4613 pr_warn("------------------------------------------------\n");
4614 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4615 curr->comm, curr->pid);
4616 lockdep_print_held_locks(curr);
4620 * The lock history for each syscall should be independent. So wipe the
4621 * slate clean on return to userspace.
4623 lockdep_invariant_state(false);
4626 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
4628 struct task_struct *curr = current;
4630 /* Note: the following can be executed concurrently, so be careful. */
4632 pr_warn("=============================\n");
4633 pr_warn("WARNING: suspicious RCU usage\n");
4634 print_kernel_ident();
4635 pr_warn("-----------------------------\n");
4636 pr_warn("%s:%d %s!\n", file, line, s);
4637 pr_warn("\nother info that might help us debug this:\n\n");
4638 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4639 !rcu_lockdep_current_cpu_online()
4640 ? "RCU used illegally from offline CPU!\n"
4641 : !rcu_is_watching()
4642 ? "RCU used illegally from idle CPU!\n"
4644 rcu_scheduler_active, debug_locks);
4647 * If a CPU is in the RCU-free window in idle (ie: in the section
4648 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4649 * considers that CPU to be in an "extended quiescent state",
4650 * which means that RCU will be completely ignoring that CPU.
4651 * Therefore, rcu_read_lock() and friends have absolutely no
4652 * effect on a CPU running in that state. In other words, even if
4653 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4654 * delete data structures out from under it. RCU really has no
4655 * choice here: we need to keep an RCU-free window in idle where
4656 * the CPU may possibly enter into low power mode. This way we can
4657 * notice an extended quiescent state to other CPUs that started a grace
4658 * period. Otherwise we would delay any grace period as long as we run
4661 * So complain bitterly if someone does call rcu_read_lock(),
4662 * rcu_read_lock_bh() and so on from extended quiescent states.
4664 if (!rcu_is_watching())
4665 pr_warn("RCU used illegally from extended quiescent state!\n");
4667 lockdep_print_held_locks(curr);
4668 pr_warn("\nstack backtrace:\n");
4671 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
4673 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
4676 * Crossrelease works by recording a lock history for each thread and
4677 * connecting those historic locks that were taken after the
4678 * wait_for_completion() in the complete() context.
4685 * wait_for_completion(&C);
4686 * lock_acquire_crosslock();
4687 * atomic_inc_return(&cross_gen_id);
4690 * | mutex_unlock(&B);
4693 * `-- lock_commit_crosslock();
4695 * Which will then add a dependency between B and C.
4698 #define xhlock(i) (current->xhlocks[(i) % MAX_XHLOCKS_NR])
4701 * Whenever a crosslock is held, cross_gen_id will be increased.
4703 static atomic_t cross_gen_id; /* Can be wrapped */
4706 * Make an entry of the ring buffer invalid.
4708 static inline void invalidate_xhlock(struct hist_lock *xhlock)
4711 * Normally, xhlock->hlock.instance must be !NULL.
4713 xhlock->hlock.instance = NULL;
4717 * Lock history stacks; we have 2 nested lock history stacks:
4722 * The thing is that once we complete a HARD/SOFT IRQ the future task locks
4723 * should not depend on any of the locks observed while running the IRQ. So
4724 * what we do is rewind the history buffer and erase all our knowledge of that
4728 void crossrelease_hist_start(enum xhlock_context_t c)
4730 struct task_struct *cur = current;
4735 cur->xhlock_idx_hist[c] = cur->xhlock_idx;
4736 cur->hist_id_save[c] = cur->hist_id;
4739 void crossrelease_hist_end(enum xhlock_context_t c)
4741 struct task_struct *cur = current;
4744 unsigned int idx = cur->xhlock_idx_hist[c];
4745 struct hist_lock *h = &xhlock(idx);
4747 cur->xhlock_idx = idx;
4749 /* Check if the ring was overwritten. */
4750 if (h->hist_id != cur->hist_id_save[c])
4751 invalidate_xhlock(h);
4756 * lockdep_invariant_state() is used to annotate independence inside a task, to
4757 * make one task look like multiple independent 'tasks'.
4759 * Take for instance workqueues; each work is independent of the last. The
4760 * completion of a future work does not depend on the completion of a past work
4761 * (in general). Therefore we must not carry that (lock) dependency across
4764 * This is true for many things; pretty much all kthreads fall into this
4765 * pattern, where they have an invariant state and future completions do not
4766 * depend on past completions. Its just that since they all have the 'same'
4767 * form -- the kthread does the same over and over -- it doesn't typically
4770 * The same is true for system-calls, once a system call is completed (we've
4771 * returned to userspace) the next system call does not depend on the lock
4772 * history of the previous system call.
4774 * They key property for independence, this invariant state, is that it must be
4775 * a point where we hold no locks and have no history. Because if we were to
4776 * hold locks, the restore at _end() would not necessarily recover it's history
4777 * entry. Similarly, independence per-definition means it does not depend on
4780 void lockdep_invariant_state(bool force)
4783 * We call this at an invariant point, no current state, no history.
4784 * Verify the former, enforce the latter.
4786 WARN_ON_ONCE(!force && current->lockdep_depth);
4787 if (current->xhlocks)
4788 invalidate_xhlock(&xhlock(current->xhlock_idx));
4791 static int cross_lock(struct lockdep_map *lock)
4793 return lock ? lock->cross : 0;
4797 * This is needed to decide the relationship between wrapable variables.
4799 static inline int before(unsigned int a, unsigned int b)
4801 return (int)(a - b) < 0;
4804 static inline struct lock_class *xhlock_class(struct hist_lock *xhlock)
4806 return hlock_class(&xhlock->hlock);
4809 static inline struct lock_class *xlock_class(struct cross_lock *xlock)
4811 return hlock_class(&xlock->hlock);
4815 * Should we check a dependency with previous one?
4817 static inline int depend_before(struct held_lock *hlock)
4819 return hlock->read != 2 && hlock->check && !hlock->trylock;
4823 * Should we check a dependency with next one?
4825 static inline int depend_after(struct held_lock *hlock)
4827 return hlock->read != 2 && hlock->check;
4831 * Check if the xhlock is valid, which would be false if,
4833 * 1. Has not used after initializaion yet.
4834 * 2. Got invalidated.
4836 * Remind hist_lock is implemented as a ring buffer.
4838 static inline int xhlock_valid(struct hist_lock *xhlock)
4841 * xhlock->hlock.instance must be !NULL.
4843 return !!xhlock->hlock.instance;
4847 * Record a hist_lock entry.
4849 * Irq disable is only required.
4851 static void add_xhlock(struct held_lock *hlock)
4853 unsigned int idx = ++current->xhlock_idx;
4854 struct hist_lock *xhlock = &xhlock(idx);
4856 #ifdef CONFIG_DEBUG_LOCKDEP
4858 * This can be done locklessly because they are all task-local
4859 * state, we must however ensure IRQs are disabled.
4861 WARN_ON_ONCE(!irqs_disabled());
4864 /* Initialize hist_lock's members */
4865 xhlock->hlock = *hlock;
4866 xhlock->hist_id = ++current->hist_id;
4868 xhlock->trace.nr_entries = 0;
4869 xhlock->trace.max_entries = MAX_XHLOCK_TRACE_ENTRIES;
4870 xhlock->trace.entries = xhlock->trace_entries;
4871 xhlock->trace.skip = 3;
4872 save_stack_trace(&xhlock->trace);
4875 static inline int same_context_xhlock(struct hist_lock *xhlock)
4877 return xhlock->hlock.irq_context == task_irq_context(current);
4881 * This should be lockless as far as possible because this would be
4882 * called very frequently.
4884 static void check_add_xhlock(struct held_lock *hlock)
4887 * Record a hist_lock, only in case that acquisitions ahead
4888 * could depend on the held_lock. For example, if the held_lock
4889 * is trylock then acquisitions ahead never depends on that.
4890 * In that case, we don't need to record it. Just return.
4892 if (!current->xhlocks || !depend_before(hlock))
4901 static int add_xlock(struct held_lock *hlock)
4903 struct cross_lock *xlock;
4904 unsigned int gen_id;
4909 xlock = &((struct lockdep_map_cross *)hlock->instance)->xlock;
4912 * When acquisitions for a crosslock are overlapped, we use
4913 * nr_acquire to perform commit for them, based on cross_gen_id
4914 * of the first acquisition, which allows to add additional
4917 * Moreover, when no acquisition of a crosslock is in progress,
4918 * we should not perform commit because the lock might not exist
4919 * any more, which might cause incorrect memory access. So we
4920 * have to track the number of acquisitions of a crosslock.
4922 * depend_after() is necessary to initialize only the first
4923 * valid xlock so that the xlock can be used on its commit.
4925 if (xlock->nr_acquire++ && depend_after(&xlock->hlock))
4928 gen_id = (unsigned int)atomic_inc_return(&cross_gen_id);
4929 xlock->hlock = *hlock;
4930 xlock->hlock.gen_id = gen_id;
4937 * Called for both normal and crosslock acquires. Normal locks will be
4938 * pushed on the hist_lock queue. Cross locks will record state and
4939 * stop regular lock_acquire() to avoid being placed on the held_lock
4942 * Return: 0 - failure;
4943 * 1 - crosslock, done;
4944 * 2 - normal lock, continue to held_lock[] ops.
4946 static int lock_acquire_crosslock(struct held_lock *hlock)
4949 * CONTEXT 1 CONTEXT 2
4950 * --------- ---------
4952 * X = atomic_inc_return(&cross_gen_id)
4953 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4954 * Y = atomic_read_acquire(&cross_gen_id)
4957 * atomic_read_acquire() is for ordering between A and B,
4958 * IOW, A happens before B, when CONTEXT 2 see Y >= X.
4960 * Pairs with atomic_inc_return() in add_xlock().
4962 hlock->gen_id = (unsigned int)atomic_read_acquire(&cross_gen_id);
4964 if (cross_lock(hlock->instance))
4965 return add_xlock(hlock);
4967 check_add_xhlock(hlock);
4971 static int copy_trace(struct stack_trace *trace)
4973 unsigned long *buf = stack_trace + nr_stack_trace_entries;
4974 unsigned int max_nr = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
4975 unsigned int nr = min(max_nr, trace->nr_entries);
4977 trace->nr_entries = nr;
4978 memcpy(buf, trace->entries, nr * sizeof(trace->entries[0]));
4979 trace->entries = buf;
4980 nr_stack_trace_entries += nr;
4982 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
4983 if (!debug_locks_off_graph_unlock())
4986 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
4995 static int commit_xhlock(struct cross_lock *xlock, struct hist_lock *xhlock)
4997 unsigned int xid, pid;
5000 xid = xlock_class(xlock) - lock_classes;
5001 chain_key = iterate_chain_key((u64)0, xid);
5002 pid = xhlock_class(xhlock) - lock_classes;
5003 chain_key = iterate_chain_key(chain_key, pid);
5005 if (lookup_chain_cache(chain_key))
5008 if (!add_chain_cache_classes(xid, pid, xhlock->hlock.irq_context,
5012 if (!check_prev_add(current, &xlock->hlock, &xhlock->hlock, 1,
5013 &xhlock->trace, copy_trace))
5019 static void commit_xhlocks(struct cross_lock *xlock)
5021 unsigned int cur = current->xhlock_idx;
5022 unsigned int prev_hist_id = xhlock(cur).hist_id;
5028 if (xlock->nr_acquire) {
5029 for (i = 0; i < MAX_XHLOCKS_NR; i++) {
5030 struct hist_lock *xhlock = &xhlock(cur - i);
5032 if (!xhlock_valid(xhlock))
5035 if (before(xhlock->hlock.gen_id, xlock->hlock.gen_id))
5038 if (!same_context_xhlock(xhlock))
5042 * Filter out the cases where the ring buffer was
5043 * overwritten and the current entry has a bigger
5044 * hist_id than the previous one, which is impossible
5047 if (unlikely(before(prev_hist_id, xhlock->hist_id)))
5050 prev_hist_id = xhlock->hist_id;
5053 * commit_xhlock() returns 0 with graph_lock already
5056 if (!commit_xhlock(xlock, xhlock))
5064 void lock_commit_crosslock(struct lockdep_map *lock)
5066 struct cross_lock *xlock;
5067 unsigned long flags;
5069 if (unlikely(!debug_locks || current->lockdep_recursion))
5072 if (!current->xhlocks)
5076 * Do commit hist_locks with the cross_lock, only in case that
5077 * the cross_lock could depend on acquisitions after that.
5079 * For example, if the cross_lock does not have the 'check' flag
5080 * then we don't need to check dependencies and commit for that.
5081 * Just skip it. In that case, of course, the cross_lock does
5082 * not depend on acquisitions ahead, either.
5084 * WARNING: Don't do that in add_xlock() in advance. When an
5085 * acquisition context is different from the commit context,
5086 * invalid(skipped) cross_lock might be accessed.
5088 if (!depend_after(&((struct lockdep_map_cross *)lock)->xlock.hlock))
5091 raw_local_irq_save(flags);
5093 current->lockdep_recursion = 1;
5094 xlock = &((struct lockdep_map_cross *)lock)->xlock;
5095 commit_xhlocks(xlock);
5096 current->lockdep_recursion = 0;
5097 raw_local_irq_restore(flags);
5099 EXPORT_SYMBOL_GPL(lock_commit_crosslock);
5102 * Return: 0 - failure;
5103 * 1 - crosslock, done;
5104 * 2 - normal lock, continue to held_lock[] ops.
5106 static int lock_release_crosslock(struct lockdep_map *lock)
5108 if (cross_lock(lock)) {
5111 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire--;
5118 static void cross_init(struct lockdep_map *lock, int cross)
5121 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire = 0;
5123 lock->cross = cross;
5126 * Crossrelease assumes that the ring buffer size of xhlocks
5127 * is aligned with power of 2. So force it on build.
5129 BUILD_BUG_ON(MAX_XHLOCKS_NR & (MAX_XHLOCKS_NR - 1));
5132 void lockdep_init_task(struct task_struct *task)
5136 task->xhlock_idx = UINT_MAX;
5139 for (i = 0; i < XHLOCK_CTX_NR; i++) {
5140 task->xhlock_idx_hist[i] = UINT_MAX;
5141 task->hist_id_save[i] = 0;
5144 task->xhlocks = kzalloc(sizeof(struct hist_lock) * MAX_XHLOCKS_NR,
5148 void lockdep_free_task(struct task_struct *task)
5150 if (task->xhlocks) {
5151 void *tmp = task->xhlocks;
5152 /* Diable crossrelease for current */
5153 task->xhlocks = NULL;