1 // SPDX-License-Identifier: GPL-2.0-only
5 * Runtime locking correctness validator
7 * Started by Ingo Molnar:
9 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
12 * this code maps all the lock dependencies as they occur in a live kernel
13 * and will warn about the following classes of locking bugs:
15 * - lock inversion scenarios
16 * - circular lock dependencies
17 * - hardirq/softirq safe/unsafe locking bugs
19 * Bugs are reported even if the current locking scenario does not cause
20 * any deadlock at this point.
22 * I.e. if anytime in the past two locks were taken in a different order,
23 * even if it happened for another task, even if those were different
24 * locks (but of the same class as this lock), this code will detect it.
26 * Thanks to Arjan van de Ven for coming up with the initial idea of
27 * mapping lock dependencies runtime.
29 #define DISABLE_BRANCH_PROFILING
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/sched/clock.h>
33 #include <linux/sched/task.h>
34 #include <linux/sched/mm.h>
35 #include <linux/delay.h>
36 #include <linux/module.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/stacktrace.h>
43 #include <linux/debug_locks.h>
44 #include <linux/irqflags.h>
45 #include <linux/utsname.h>
46 #include <linux/hash.h>
47 #include <linux/ftrace.h>
48 #include <linux/stringify.h>
49 #include <linux/bitmap.h>
50 #include <linux/bitops.h>
51 #include <linux/gfp.h>
52 #include <linux/random.h>
53 #include <linux/jhash.h>
54 #include <linux/nmi.h>
55 #include <linux/rcupdate.h>
56 #include <linux/kprobes.h>
58 #include <asm/sections.h>
60 #include "lockdep_internals.h"
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/lock.h>
65 #ifdef CONFIG_PROVE_LOCKING
66 int prove_locking
= 1;
67 module_param(prove_locking
, int, 0644);
69 #define prove_locking 0
72 #ifdef CONFIG_LOCK_STAT
74 module_param(lock_stat
, int, 0644);
80 * lockdep_lock: protects the lockdep graph, the hashes and the
81 * class/list/hash allocators.
83 * This is one of the rare exceptions where it's justified
84 * to use a raw spinlock - we really dont want the spinlock
85 * code to recurse back into the lockdep code...
87 static arch_spinlock_t __lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
88 static struct task_struct
*__owner
;
90 static inline void lockdep_lock(void)
92 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
94 arch_spin_lock(&__lock
);
96 current
->lockdep_recursion
++;
99 static inline void lockdep_unlock(void)
101 if (debug_locks
&& DEBUG_LOCKS_WARN_ON(__owner
!= current
))
104 current
->lockdep_recursion
--;
106 arch_spin_unlock(&__lock
);
109 static inline bool lockdep_assert_locked(void)
111 return DEBUG_LOCKS_WARN_ON(__owner
!= current
);
114 static struct task_struct
*lockdep_selftest_task_struct
;
117 static int graph_lock(void)
121 * Make sure that if another CPU detected a bug while
122 * walking the graph we dont change it (while the other
123 * CPU is busy printing out stuff with the graph lock
133 static inline void graph_unlock(void)
139 * Turn lock debugging off and return with 0 if it was off already,
140 * and also release the graph lock:
142 static inline int debug_locks_off_graph_unlock(void)
144 int ret
= debug_locks_off();
151 unsigned long nr_list_entries
;
152 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
153 static DECLARE_BITMAP(list_entries_in_use
, MAX_LOCKDEP_ENTRIES
);
156 * All data structures here are protected by the global debug_lock.
158 * nr_lock_classes is the number of elements of lock_classes[] that is
161 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
162 #define KEYHASH_SIZE (1UL << KEYHASH_BITS)
163 static struct hlist_head lock_keys_hash
[KEYHASH_SIZE
];
164 unsigned long nr_lock_classes
;
165 unsigned long nr_zapped_classes
;
166 #ifndef CONFIG_DEBUG_LOCKDEP
169 struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
170 static DECLARE_BITMAP(lock_classes_in_use
, MAX_LOCKDEP_KEYS
);
172 static inline struct lock_class
*hlock_class(struct held_lock
*hlock
)
174 unsigned int class_idx
= hlock
->class_idx
;
176 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */
179 if (!test_bit(class_idx
, lock_classes_in_use
)) {
181 * Someone passed in garbage, we give up.
183 DEBUG_LOCKS_WARN_ON(1);
188 * At this point, if the passed hlock->class_idx is still garbage,
189 * we just have to live with it
191 return lock_classes
+ class_idx
;
194 #ifdef CONFIG_LOCK_STAT
195 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], cpu_lock_stats
);
197 static inline u64
lockstat_clock(void)
199 return local_clock();
202 static int lock_point(unsigned long points
[], unsigned long ip
)
206 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
207 if (points
[i
] == 0) {
218 static void lock_time_inc(struct lock_time
*lt
, u64 time
)
223 if (time
< lt
->min
|| !lt
->nr
)
230 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
235 if (src
->max
> dst
->max
)
238 if (src
->min
< dst
->min
|| !dst
->nr
)
241 dst
->total
+= src
->total
;
245 struct lock_class_stats
lock_stats(struct lock_class
*class)
247 struct lock_class_stats stats
;
250 memset(&stats
, 0, sizeof(struct lock_class_stats
));
251 for_each_possible_cpu(cpu
) {
252 struct lock_class_stats
*pcs
=
253 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
255 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
256 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
258 for (i
= 0; i
< ARRAY_SIZE(stats
.contending_point
); i
++)
259 stats
.contending_point
[i
] += pcs
->contending_point
[i
];
261 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
262 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
264 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
265 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
267 for (i
= 0; i
< ARRAY_SIZE(stats
.bounces
); i
++)
268 stats
.bounces
[i
] += pcs
->bounces
[i
];
274 void clear_lock_stats(struct lock_class
*class)
278 for_each_possible_cpu(cpu
) {
279 struct lock_class_stats
*cpu_stats
=
280 &per_cpu(cpu_lock_stats
, cpu
)[class - lock_classes
];
282 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
284 memset(class->contention_point
, 0, sizeof(class->contention_point
));
285 memset(class->contending_point
, 0, sizeof(class->contending_point
));
288 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
290 return &this_cpu_ptr(cpu_lock_stats
)[class - lock_classes
];
293 static void lock_release_holdtime(struct held_lock
*hlock
)
295 struct lock_class_stats
*stats
;
301 holdtime
= lockstat_clock() - hlock
->holdtime_stamp
;
303 stats
= get_lock_stats(hlock_class(hlock
));
305 lock_time_inc(&stats
->read_holdtime
, holdtime
);
307 lock_time_inc(&stats
->write_holdtime
, holdtime
);
310 static inline void lock_release_holdtime(struct held_lock
*hlock
)
316 * We keep a global list of all lock classes. The list is only accessed with
317 * the lockdep spinlock lock held. free_lock_classes is a list with free
318 * elements. These elements are linked together by the lock_entry member in
321 LIST_HEAD(all_lock_classes
);
322 static LIST_HEAD(free_lock_classes
);
325 * struct pending_free - information about data structures about to be freed
326 * @zapped: Head of a list with struct lock_class elements.
327 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
328 * are about to be freed.
330 struct pending_free
{
331 struct list_head zapped
;
332 DECLARE_BITMAP(lock_chains_being_freed
, MAX_LOCKDEP_CHAINS
);
336 * struct delayed_free - data structures used for delayed freeing
338 * A data structure for delayed freeing of data structures that may be
339 * accessed by RCU readers at the time these were freed.
341 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
342 * @index: Index of @pf to which freed data structures are added.
343 * @scheduled: Whether or not an RCU callback has been scheduled.
344 * @pf: Array with information about data structures about to be freed.
346 static struct delayed_free
{
347 struct rcu_head rcu_head
;
350 struct pending_free pf
[2];
354 * The lockdep classes are in a hash-table as well, for fast lookup:
356 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
357 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
358 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
359 #define classhashentry(key) (classhash_table + __classhashfn((key)))
361 static struct hlist_head classhash_table
[CLASSHASH_SIZE
];
364 * We put the lock dependency chains into a hash-table as well, to cache
367 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
368 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
369 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
370 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
372 static struct hlist_head chainhash_table
[CHAINHASH_SIZE
];
375 * The hash key of the lock dependency chains is a hash itself too:
376 * it's a hash of all locks taken up to that lock, including that lock.
377 * It's a 64-bit hash, because it's important for the keys to be
380 static inline u64
iterate_chain_key(u64 key
, u32 idx
)
382 u32 k0
= key
, k1
= key
>> 32;
384 __jhash_mix(idx
, k0
, k1
); /* Macro that modifies arguments! */
386 return k0
| (u64
)k1
<< 32;
389 void lockdep_init_task(struct task_struct
*task
)
391 task
->lockdep_depth
= 0; /* no locks held yet */
392 task
->curr_chain_key
= INITIAL_CHAIN_KEY
;
393 task
->lockdep_recursion
= 0;
397 * Split the recrursion counter in two to readily detect 'off' vs recursion.
399 #define LOCKDEP_RECURSION_BITS 16
400 #define LOCKDEP_OFF (1U << LOCKDEP_RECURSION_BITS)
401 #define LOCKDEP_RECURSION_MASK (LOCKDEP_OFF - 1)
403 void lockdep_off(void)
405 current
->lockdep_recursion
+= LOCKDEP_OFF
;
407 EXPORT_SYMBOL(lockdep_off
);
409 void lockdep_on(void)
411 current
->lockdep_recursion
-= LOCKDEP_OFF
;
413 EXPORT_SYMBOL(lockdep_on
);
415 static inline void lockdep_recursion_finish(void)
417 if (WARN_ON_ONCE(--current
->lockdep_recursion
))
418 current
->lockdep_recursion
= 0;
421 void lockdep_set_selftest_task(struct task_struct
*task
)
423 lockdep_selftest_task_struct
= task
;
427 * Debugging switches:
431 #define VERY_VERBOSE 0
434 # define HARDIRQ_VERBOSE 1
435 # define SOFTIRQ_VERBOSE 1
437 # define HARDIRQ_VERBOSE 0
438 # define SOFTIRQ_VERBOSE 0
441 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
443 * Quick filtering for interesting events:
445 static int class_filter(struct lock_class
*class)
449 if (class->name_version
== 1 &&
450 !strcmp(class->name
, "lockname"))
452 if (class->name_version
== 1 &&
453 !strcmp(class->name
, "&struct->lockfield"))
456 /* Filter everything else. 1 would be to allow everything else */
461 static int verbose(struct lock_class
*class)
464 return class_filter(class);
469 static void print_lockdep_off(const char *bug_msg
)
471 printk(KERN_DEBUG
"%s\n", bug_msg
);
472 printk(KERN_DEBUG
"turning off the locking correctness validator.\n");
473 #ifdef CONFIG_LOCK_STAT
474 printk(KERN_DEBUG
"Please attach the output of /proc/lock_stat to the bug report\n");
478 unsigned long nr_stack_trace_entries
;
480 #ifdef CONFIG_PROVE_LOCKING
482 * struct lock_trace - single stack backtrace
483 * @hash_entry: Entry in a stack_trace_hash[] list.
484 * @hash: jhash() of @entries.
485 * @nr_entries: Number of entries in @entries.
486 * @entries: Actual stack backtrace.
489 struct hlist_node hash_entry
;
492 unsigned long entries
[0] __aligned(sizeof(unsigned long));
494 #define LOCK_TRACE_SIZE_IN_LONGS \
495 (sizeof(struct lock_trace) / sizeof(unsigned long))
497 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
499 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
500 static struct hlist_head stack_trace_hash
[STACK_TRACE_HASH_SIZE
];
502 static bool traces_identical(struct lock_trace
*t1
, struct lock_trace
*t2
)
504 return t1
->hash
== t2
->hash
&& t1
->nr_entries
== t2
->nr_entries
&&
505 memcmp(t1
->entries
, t2
->entries
,
506 t1
->nr_entries
* sizeof(t1
->entries
[0])) == 0;
509 static struct lock_trace
*save_trace(void)
511 struct lock_trace
*trace
, *t2
;
512 struct hlist_head
*hash_head
;
516 BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE
);
517 BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS
>= MAX_STACK_TRACE_ENTRIES
);
519 trace
= (struct lock_trace
*)(stack_trace
+ nr_stack_trace_entries
);
520 max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
-
521 LOCK_TRACE_SIZE_IN_LONGS
;
523 if (max_entries
<= 0) {
524 if (!debug_locks_off_graph_unlock())
527 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
532 trace
->nr_entries
= stack_trace_save(trace
->entries
, max_entries
, 3);
534 hash
= jhash(trace
->entries
, trace
->nr_entries
*
535 sizeof(trace
->entries
[0]), 0);
537 hash_head
= stack_trace_hash
+ (hash
& (STACK_TRACE_HASH_SIZE
- 1));
538 hlist_for_each_entry(t2
, hash_head
, hash_entry
) {
539 if (traces_identical(trace
, t2
))
542 nr_stack_trace_entries
+= LOCK_TRACE_SIZE_IN_LONGS
+ trace
->nr_entries
;
543 hlist_add_head(&trace
->hash_entry
, hash_head
);
548 /* Return the number of stack traces in the stack_trace[] array. */
549 u64
lockdep_stack_trace_count(void)
551 struct lock_trace
*trace
;
555 for (i
= 0; i
< ARRAY_SIZE(stack_trace_hash
); i
++) {
556 hlist_for_each_entry(trace
, &stack_trace_hash
[i
], hash_entry
) {
564 /* Return the number of stack hash chains that have at least one stack trace. */
565 u64
lockdep_stack_hash_count(void)
570 for (i
= 0; i
< ARRAY_SIZE(stack_trace_hash
); i
++)
571 if (!hlist_empty(&stack_trace_hash
[i
]))
578 unsigned int nr_hardirq_chains
;
579 unsigned int nr_softirq_chains
;
580 unsigned int nr_process_chains
;
581 unsigned int max_lockdep_depth
;
583 #ifdef CONFIG_DEBUG_LOCKDEP
585 * Various lockdep statistics:
587 DEFINE_PER_CPU(struct lockdep_stats
, lockdep_stats
);
590 #ifdef CONFIG_PROVE_LOCKING
595 #define __USAGE(__STATE) \
596 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
597 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
598 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
599 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
601 static const char *usage_str
[] =
603 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
604 #include "lockdep_states.h"
606 [LOCK_USED
] = "INITIAL USE",
607 [LOCK_USAGE_STATES
] = "IN-NMI",
611 const char *__get_key_name(const struct lockdep_subclass_key
*key
, char *str
)
613 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
616 static inline unsigned long lock_flag(enum lock_usage_bit bit
)
621 static char get_usage_char(struct lock_class
*class, enum lock_usage_bit bit
)
624 * The usage character defaults to '.' (i.e., irqs disabled and not in
625 * irq context), which is the safest usage category.
630 * The order of the following usage checks matters, which will
631 * result in the outcome character as follows:
633 * - '+': irq is enabled and not in irq context
634 * - '-': in irq context and irq is disabled
635 * - '?': in irq context and irq is enabled
637 if (class->usage_mask
& lock_flag(bit
+ LOCK_USAGE_DIR_MASK
)) {
639 if (class->usage_mask
& lock_flag(bit
))
641 } else if (class->usage_mask
& lock_flag(bit
))
647 void get_usage_chars(struct lock_class
*class, char usage
[LOCK_USAGE_CHARS
])
651 #define LOCKDEP_STATE(__STATE) \
652 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
653 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
654 #include "lockdep_states.h"
660 static void __print_lock_name(struct lock_class
*class)
662 char str
[KSYM_NAME_LEN
];
667 name
= __get_key_name(class->key
, str
);
668 printk(KERN_CONT
"%s", name
);
670 printk(KERN_CONT
"%s", name
);
671 if (class->name_version
> 1)
672 printk(KERN_CONT
"#%d", class->name_version
);
674 printk(KERN_CONT
"/%d", class->subclass
);
678 static void print_lock_name(struct lock_class
*class)
680 char usage
[LOCK_USAGE_CHARS
];
682 get_usage_chars(class, usage
);
684 printk(KERN_CONT
" (");
685 __print_lock_name(class);
686 printk(KERN_CONT
"){%s}-{%hd:%hd}", usage
,
687 class->wait_type_outer
?: class->wait_type_inner
,
688 class->wait_type_inner
);
691 static void print_lockdep_cache(struct lockdep_map
*lock
)
694 char str
[KSYM_NAME_LEN
];
698 name
= __get_key_name(lock
->key
->subkeys
, str
);
700 printk(KERN_CONT
"%s", name
);
703 static void print_lock(struct held_lock
*hlock
)
706 * We can be called locklessly through debug_show_all_locks() so be
707 * extra careful, the hlock might have been released and cleared.
709 * If this indeed happens, lets pretend it does not hurt to continue
710 * to print the lock unless the hlock class_idx does not point to a
711 * registered class. The rationale here is: since we don't attempt
712 * to distinguish whether we are in this situation, if it just
713 * happened we can't count on class_idx to tell either.
715 struct lock_class
*lock
= hlock_class(hlock
);
718 printk(KERN_CONT
"<RELEASED>\n");
722 printk(KERN_CONT
"%px", hlock
->instance
);
723 print_lock_name(lock
);
724 printk(KERN_CONT
", at: %pS\n", (void *)hlock
->acquire_ip
);
727 static void lockdep_print_held_locks(struct task_struct
*p
)
729 int i
, depth
= READ_ONCE(p
->lockdep_depth
);
732 printk("no locks held by %s/%d.\n", p
->comm
, task_pid_nr(p
));
734 printk("%d lock%s held by %s/%d:\n", depth
,
735 depth
> 1 ? "s" : "", p
->comm
, task_pid_nr(p
));
737 * It's not reliable to print a task's held locks if it's not sleeping
738 * and it's not the current task.
740 if (p
->state
== TASK_RUNNING
&& p
!= current
)
742 for (i
= 0; i
< depth
; i
++) {
744 print_lock(p
->held_locks
+ i
);
748 static void print_kernel_ident(void)
750 printk("%s %.*s %s\n", init_utsname()->release
,
751 (int)strcspn(init_utsname()->version
, " "),
752 init_utsname()->version
,
756 static int very_verbose(struct lock_class
*class)
759 return class_filter(class);
765 * Is this the address of a static object:
768 static int static_obj(const void *obj
)
770 unsigned long start
= (unsigned long) &_stext
,
771 end
= (unsigned long) &_end
,
772 addr
= (unsigned long) obj
;
774 if (arch_is_kernel_initmem_freed(addr
))
780 if ((addr
>= start
) && (addr
< end
))
783 if (arch_is_kernel_data(addr
))
787 * in-kernel percpu var?
789 if (is_kernel_percpu_address(addr
))
793 * module static or percpu var?
795 return is_module_address(addr
) || is_module_percpu_address(addr
);
800 * To make lock name printouts unique, we calculate a unique
801 * class->name_version generation counter. The caller must hold the graph
804 static int count_matching_names(struct lock_class
*new_class
)
806 struct lock_class
*class;
809 if (!new_class
->name
)
812 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
813 if (new_class
->key
- new_class
->subclass
== class->key
)
814 return class->name_version
;
815 if (class->name
&& !strcmp(class->name
, new_class
->name
))
816 count
= max(count
, class->name_version
);
822 /* used from NMI context -- must be lockless */
823 static inline struct lock_class
*
824 look_up_lock_class(const struct lockdep_map
*lock
, unsigned int subclass
)
826 struct lockdep_subclass_key
*key
;
827 struct hlist_head
*hash_head
;
828 struct lock_class
*class;
830 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
833 "BUG: looking up invalid subclass: %u\n", subclass
);
835 "turning off the locking correctness validator.\n");
841 * If it is not initialised then it has never been locked,
842 * so it won't be present in the hash table.
844 if (unlikely(!lock
->key
))
848 * NOTE: the class-key must be unique. For dynamic locks, a static
849 * lock_class_key variable is passed in through the mutex_init()
850 * (or spin_lock_init()) call - which acts as the key. For static
851 * locks we use the lock object itself as the key.
853 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
854 sizeof(struct lockdep_map
));
856 key
= lock
->key
->subkeys
+ subclass
;
858 hash_head
= classhashentry(key
);
861 * We do an RCU walk of the hash, see lockdep_free_key_range().
863 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
866 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
867 if (class->key
== key
) {
869 * Huh! same key, different name? Did someone trample
870 * on some memory? We're most confused.
872 WARN_ON_ONCE(class->name
!= lock
->name
&&
873 lock
->key
!= &__lockdep_no_validate__
);
882 * Static locks do not have their class-keys yet - for them the key is
883 * the lock object itself. If the lock is in the per cpu area, the
884 * canonical address of the lock (per cpu offset removed) is used.
886 static bool assign_lock_key(struct lockdep_map
*lock
)
888 unsigned long can_addr
, addr
= (unsigned long)lock
;
892 * lockdep_free_key_range() assumes that struct lock_class_key
893 * objects do not overlap. Since we use the address of lock
894 * objects as class key for static objects, check whether the
895 * size of lock_class_key objects does not exceed the size of
896 * the smallest lock object.
898 BUILD_BUG_ON(sizeof(struct lock_class_key
) > sizeof(raw_spinlock_t
));
901 if (__is_kernel_percpu_address(addr
, &can_addr
))
902 lock
->key
= (void *)can_addr
;
903 else if (__is_module_percpu_address(addr
, &can_addr
))
904 lock
->key
= (void *)can_addr
;
905 else if (static_obj(lock
))
906 lock
->key
= (void *)lock
;
908 /* Debug-check: all keys must be persistent! */
910 pr_err("INFO: trying to register non-static key.\n");
911 pr_err("the code is fine but needs lockdep annotation.\n");
912 pr_err("turning off the locking correctness validator.\n");
920 #ifdef CONFIG_DEBUG_LOCKDEP
922 /* Check whether element @e occurs in list @h */
923 static bool in_list(struct list_head
*e
, struct list_head
*h
)
927 list_for_each(f
, h
) {
936 * Check whether entry @e occurs in any of the locks_after or locks_before
939 static bool in_any_class_list(struct list_head
*e
)
941 struct lock_class
*class;
944 for (i
= 0; i
< ARRAY_SIZE(lock_classes
); i
++) {
945 class = &lock_classes
[i
];
946 if (in_list(e
, &class->locks_after
) ||
947 in_list(e
, &class->locks_before
))
953 static bool class_lock_list_valid(struct lock_class
*c
, struct list_head
*h
)
957 list_for_each_entry(e
, h
, entry
) {
958 if (e
->links_to
!= c
) {
959 printk(KERN_INFO
"class %s: mismatch for lock entry %ld; class %s <> %s",
961 (unsigned long)(e
- list_entries
),
962 e
->links_to
&& e
->links_to
->name
?
963 e
->links_to
->name
: "(?)",
964 e
->class && e
->class->name
? e
->class->name
:
972 #ifdef CONFIG_PROVE_LOCKING
973 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
976 static bool check_lock_chain_key(struct lock_chain
*chain
)
978 #ifdef CONFIG_PROVE_LOCKING
979 u64 chain_key
= INITIAL_CHAIN_KEY
;
982 for (i
= chain
->base
; i
< chain
->base
+ chain
->depth
; i
++)
983 chain_key
= iterate_chain_key(chain_key
, chain_hlocks
[i
]);
985 * The 'unsigned long long' casts avoid that a compiler warning
986 * is reported when building tools/lib/lockdep.
988 if (chain
->chain_key
!= chain_key
) {
989 printk(KERN_INFO
"chain %lld: key %#llx <> %#llx\n",
990 (unsigned long long)(chain
- lock_chains
),
991 (unsigned long long)chain
->chain_key
,
992 (unsigned long long)chain_key
);
999 static bool in_any_zapped_class_list(struct lock_class
*class)
1001 struct pending_free
*pf
;
1004 for (i
= 0, pf
= delayed_free
.pf
; i
< ARRAY_SIZE(delayed_free
.pf
); i
++, pf
++) {
1005 if (in_list(&class->lock_entry
, &pf
->zapped
))
1012 static bool __check_data_structures(void)
1014 struct lock_class
*class;
1015 struct lock_chain
*chain
;
1016 struct hlist_head
*head
;
1017 struct lock_list
*e
;
1020 /* Check whether all classes occur in a lock list. */
1021 for (i
= 0; i
< ARRAY_SIZE(lock_classes
); i
++) {
1022 class = &lock_classes
[i
];
1023 if (!in_list(&class->lock_entry
, &all_lock_classes
) &&
1024 !in_list(&class->lock_entry
, &free_lock_classes
) &&
1025 !in_any_zapped_class_list(class)) {
1026 printk(KERN_INFO
"class %px/%s is not in any class list\n",
1027 class, class->name
? : "(?)");
1032 /* Check whether all classes have valid lock lists. */
1033 for (i
= 0; i
< ARRAY_SIZE(lock_classes
); i
++) {
1034 class = &lock_classes
[i
];
1035 if (!class_lock_list_valid(class, &class->locks_before
))
1037 if (!class_lock_list_valid(class, &class->locks_after
))
1041 /* Check the chain_key of all lock chains. */
1042 for (i
= 0; i
< ARRAY_SIZE(chainhash_table
); i
++) {
1043 head
= chainhash_table
+ i
;
1044 hlist_for_each_entry_rcu(chain
, head
, entry
) {
1045 if (!check_lock_chain_key(chain
))
1051 * Check whether all list entries that are in use occur in a class
1054 for_each_set_bit(i
, list_entries_in_use
, ARRAY_SIZE(list_entries
)) {
1055 e
= list_entries
+ i
;
1056 if (!in_any_class_list(&e
->entry
)) {
1057 printk(KERN_INFO
"list entry %d is not in any class list; class %s <> %s\n",
1058 (unsigned int)(e
- list_entries
),
1059 e
->class->name
? : "(?)",
1060 e
->links_to
->name
? : "(?)");
1066 * Check whether all list entries that are not in use do not occur in
1067 * a class lock list.
1069 for_each_clear_bit(i
, list_entries_in_use
, ARRAY_SIZE(list_entries
)) {
1070 e
= list_entries
+ i
;
1071 if (in_any_class_list(&e
->entry
)) {
1072 printk(KERN_INFO
"list entry %d occurs in a class list; class %s <> %s\n",
1073 (unsigned int)(e
- list_entries
),
1074 e
->class && e
->class->name
? e
->class->name
:
1076 e
->links_to
&& e
->links_to
->name
?
1077 e
->links_to
->name
: "(?)");
1085 int check_consistency
= 0;
1086 module_param(check_consistency
, int, 0644);
1088 static void check_data_structures(void)
1090 static bool once
= false;
1092 if (check_consistency
&& !once
) {
1093 if (!__check_data_structures()) {
1100 #else /* CONFIG_DEBUG_LOCKDEP */
1102 static inline void check_data_structures(void) { }
1104 #endif /* CONFIG_DEBUG_LOCKDEP */
1106 static void init_chain_block_buckets(void);
1109 * Initialize the lock_classes[] array elements, the free_lock_classes list
1110 * and also the delayed_free structure.
1112 static void init_data_structures_once(void)
1114 static bool __read_mostly ds_initialized
, rcu_head_initialized
;
1117 if (likely(rcu_head_initialized
))
1120 if (system_state
>= SYSTEM_SCHEDULING
) {
1121 init_rcu_head(&delayed_free
.rcu_head
);
1122 rcu_head_initialized
= true;
1128 ds_initialized
= true;
1130 INIT_LIST_HEAD(&delayed_free
.pf
[0].zapped
);
1131 INIT_LIST_HEAD(&delayed_free
.pf
[1].zapped
);
1133 for (i
= 0; i
< ARRAY_SIZE(lock_classes
); i
++) {
1134 list_add_tail(&lock_classes
[i
].lock_entry
, &free_lock_classes
);
1135 INIT_LIST_HEAD(&lock_classes
[i
].locks_after
);
1136 INIT_LIST_HEAD(&lock_classes
[i
].locks_before
);
1138 init_chain_block_buckets();
1141 static inline struct hlist_head
*keyhashentry(const struct lock_class_key
*key
)
1143 unsigned long hash
= hash_long((uintptr_t)key
, KEYHASH_BITS
);
1145 return lock_keys_hash
+ hash
;
1148 /* Register a dynamically allocated key. */
1149 void lockdep_register_key(struct lock_class_key
*key
)
1151 struct hlist_head
*hash_head
;
1152 struct lock_class_key
*k
;
1153 unsigned long flags
;
1155 if (WARN_ON_ONCE(static_obj(key
)))
1157 hash_head
= keyhashentry(key
);
1159 raw_local_irq_save(flags
);
1162 hlist_for_each_entry_rcu(k
, hash_head
, hash_entry
) {
1163 if (WARN_ON_ONCE(k
== key
))
1166 hlist_add_head_rcu(&key
->hash_entry
, hash_head
);
1170 raw_local_irq_restore(flags
);
1172 EXPORT_SYMBOL_GPL(lockdep_register_key
);
1174 /* Check whether a key has been registered as a dynamic key. */
1175 static bool is_dynamic_key(const struct lock_class_key
*key
)
1177 struct hlist_head
*hash_head
;
1178 struct lock_class_key
*k
;
1181 if (WARN_ON_ONCE(static_obj(key
)))
1185 * If lock debugging is disabled lock_keys_hash[] may contain
1186 * pointers to memory that has already been freed. Avoid triggering
1187 * a use-after-free in that case by returning early.
1192 hash_head
= keyhashentry(key
);
1195 hlist_for_each_entry_rcu(k
, hash_head
, hash_entry
) {
1207 * Register a lock's class in the hash-table, if the class is not present
1208 * yet. Otherwise we look it up. We cache the result in the lock object
1209 * itself, so actual lookup of the hash should be once per lock object.
1211 static struct lock_class
*
1212 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
1214 struct lockdep_subclass_key
*key
;
1215 struct hlist_head
*hash_head
;
1216 struct lock_class
*class;
1218 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1220 class = look_up_lock_class(lock
, subclass
);
1222 goto out_set_class_cache
;
1225 if (!assign_lock_key(lock
))
1227 } else if (!static_obj(lock
->key
) && !is_dynamic_key(lock
->key
)) {
1231 key
= lock
->key
->subkeys
+ subclass
;
1232 hash_head
= classhashentry(key
);
1234 if (!graph_lock()) {
1238 * We have to do the hash-walk again, to avoid races
1241 hlist_for_each_entry_rcu(class, hash_head
, hash_entry
) {
1242 if (class->key
== key
)
1243 goto out_unlock_set
;
1246 init_data_structures_once();
1248 /* Allocate a new lock class and add it to the hash. */
1249 class = list_first_entry_or_null(&free_lock_classes
, typeof(*class),
1252 if (!debug_locks_off_graph_unlock()) {
1256 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
1261 __set_bit(class - lock_classes
, lock_classes_in_use
);
1262 debug_atomic_inc(nr_unused_locks
);
1264 class->name
= lock
->name
;
1265 class->subclass
= subclass
;
1266 WARN_ON_ONCE(!list_empty(&class->locks_before
));
1267 WARN_ON_ONCE(!list_empty(&class->locks_after
));
1268 class->name_version
= count_matching_names(class);
1269 class->wait_type_inner
= lock
->wait_type_inner
;
1270 class->wait_type_outer
= lock
->wait_type_outer
;
1272 * We use RCU's safe list-add method to make
1273 * parallel walking of the hash-list safe:
1275 hlist_add_head_rcu(&class->hash_entry
, hash_head
);
1277 * Remove the class from the free list and add it to the global list
1280 list_move_tail(&class->lock_entry
, &all_lock_classes
);
1282 if (verbose(class)) {
1285 printk("\nnew class %px: %s", class->key
, class->name
);
1286 if (class->name_version
> 1)
1287 printk(KERN_CONT
"#%d", class->name_version
);
1288 printk(KERN_CONT
"\n");
1291 if (!graph_lock()) {
1298 out_set_class_cache
:
1299 if (!subclass
|| force
)
1300 lock
->class_cache
[0] = class;
1301 else if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
1302 lock
->class_cache
[subclass
] = class;
1305 * Hash collision, did we smoke some? We found a class with a matching
1306 * hash but the subclass -- which is hashed in -- didn't match.
1308 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
1314 #ifdef CONFIG_PROVE_LOCKING
1316 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1317 * with NULL on failure)
1319 static struct lock_list
*alloc_list_entry(void)
1321 int idx
= find_first_zero_bit(list_entries_in_use
,
1322 ARRAY_SIZE(list_entries
));
1324 if (idx
>= ARRAY_SIZE(list_entries
)) {
1325 if (!debug_locks_off_graph_unlock())
1328 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
1333 __set_bit(idx
, list_entries_in_use
);
1334 return list_entries
+ idx
;
1338 * Add a new dependency to the head of the list:
1340 static int add_lock_to_list(struct lock_class
*this,
1341 struct lock_class
*links_to
, struct list_head
*head
,
1342 unsigned long ip
, int distance
,
1343 const struct lock_trace
*trace
)
1345 struct lock_list
*entry
;
1347 * Lock not present yet - get a new dependency struct and
1348 * add it to the list:
1350 entry
= alloc_list_entry();
1354 entry
->class = this;
1355 entry
->links_to
= links_to
;
1356 entry
->distance
= distance
;
1357 entry
->trace
= trace
;
1359 * Both allocation and removal are done under the graph lock; but
1360 * iteration is under RCU-sched; see look_up_lock_class() and
1361 * lockdep_free_key_range().
1363 list_add_tail_rcu(&entry
->entry
, head
);
1369 * For good efficiency of modular, we use power of 2
1371 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
1372 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1375 * The circular_queue and helpers are used to implement graph
1376 * breadth-first search (BFS) algorithm, by which we can determine
1377 * whether there is a path from a lock to another. In deadlock checks,
1378 * a path from the next lock to be acquired to a previous held lock
1379 * indicates that adding the <prev> -> <next> lock dependency will
1380 * produce a circle in the graph. Breadth-first search instead of
1381 * depth-first search is used in order to find the shortest (circular)
1384 struct circular_queue
{
1385 struct lock_list
*element
[MAX_CIRCULAR_QUEUE_SIZE
];
1386 unsigned int front
, rear
;
1389 static struct circular_queue lock_cq
;
1391 unsigned int max_bfs_queue_depth
;
1393 static unsigned int lockdep_dependency_gen_id
;
1395 static inline void __cq_init(struct circular_queue
*cq
)
1397 cq
->front
= cq
->rear
= 0;
1398 lockdep_dependency_gen_id
++;
1401 static inline int __cq_empty(struct circular_queue
*cq
)
1403 return (cq
->front
== cq
->rear
);
1406 static inline int __cq_full(struct circular_queue
*cq
)
1408 return ((cq
->rear
+ 1) & CQ_MASK
) == cq
->front
;
1411 static inline int __cq_enqueue(struct circular_queue
*cq
, struct lock_list
*elem
)
1416 cq
->element
[cq
->rear
] = elem
;
1417 cq
->rear
= (cq
->rear
+ 1) & CQ_MASK
;
1422 * Dequeue an element from the circular_queue, return a lock_list if
1423 * the queue is not empty, or NULL if otherwise.
1425 static inline struct lock_list
* __cq_dequeue(struct circular_queue
*cq
)
1427 struct lock_list
* lock
;
1432 lock
= cq
->element
[cq
->front
];
1433 cq
->front
= (cq
->front
+ 1) & CQ_MASK
;
1438 static inline unsigned int __cq_get_elem_count(struct circular_queue
*cq
)
1440 return (cq
->rear
- cq
->front
) & CQ_MASK
;
1443 static inline void mark_lock_accessed(struct lock_list
*lock
,
1444 struct lock_list
*parent
)
1448 nr
= lock
- list_entries
;
1449 WARN_ON(nr
>= ARRAY_SIZE(list_entries
)); /* Out-of-bounds, input fail */
1450 lock
->parent
= parent
;
1451 lock
->class->dep_gen_id
= lockdep_dependency_gen_id
;
1454 static inline unsigned long lock_accessed(struct lock_list
*lock
)
1458 nr
= lock
- list_entries
;
1459 WARN_ON(nr
>= ARRAY_SIZE(list_entries
)); /* Out-of-bounds, input fail */
1460 return lock
->class->dep_gen_id
== lockdep_dependency_gen_id
;
1463 static inline struct lock_list
*get_lock_parent(struct lock_list
*child
)
1465 return child
->parent
;
1468 static inline int get_lock_depth(struct lock_list
*child
)
1471 struct lock_list
*parent
;
1473 while ((parent
= get_lock_parent(child
))) {
1481 * Return the forward or backward dependency list.
1483 * @lock: the lock_list to get its class's dependency list
1484 * @offset: the offset to struct lock_class to determine whether it is
1485 * locks_after or locks_before
1487 static inline struct list_head
*get_dep_list(struct lock_list
*lock
, int offset
)
1489 void *lock_class
= lock
->class;
1491 return lock_class
+ offset
;
1495 * Forward- or backward-dependency search, used for both circular dependency
1496 * checking and hardirq-unsafe/softirq-unsafe checking.
1498 static int __bfs(struct lock_list
*source_entry
,
1500 int (*match
)(struct lock_list
*entry
, void *data
),
1501 struct lock_list
**target_entry
,
1504 struct lock_list
*entry
;
1505 struct lock_list
*lock
;
1506 struct list_head
*head
;
1507 struct circular_queue
*cq
= &lock_cq
;
1510 lockdep_assert_locked();
1512 if (match(source_entry
, data
)) {
1513 *target_entry
= source_entry
;
1518 head
= get_dep_list(source_entry
, offset
);
1519 if (list_empty(head
))
1523 __cq_enqueue(cq
, source_entry
);
1525 while ((lock
= __cq_dequeue(cq
))) {
1532 head
= get_dep_list(lock
, offset
);
1534 list_for_each_entry_rcu(entry
, head
, entry
) {
1535 if (!lock_accessed(entry
)) {
1536 unsigned int cq_depth
;
1537 mark_lock_accessed(entry
, lock
);
1538 if (match(entry
, data
)) {
1539 *target_entry
= entry
;
1544 if (__cq_enqueue(cq
, entry
)) {
1548 cq_depth
= __cq_get_elem_count(cq
);
1549 if (max_bfs_queue_depth
< cq_depth
)
1550 max_bfs_queue_depth
= cq_depth
;
1558 static inline int __bfs_forwards(struct lock_list
*src_entry
,
1560 int (*match
)(struct lock_list
*entry
, void *data
),
1561 struct lock_list
**target_entry
)
1563 return __bfs(src_entry
, data
, match
, target_entry
,
1564 offsetof(struct lock_class
, locks_after
));
1568 static inline int __bfs_backwards(struct lock_list
*src_entry
,
1570 int (*match
)(struct lock_list
*entry
, void *data
),
1571 struct lock_list
**target_entry
)
1573 return __bfs(src_entry
, data
, match
, target_entry
,
1574 offsetof(struct lock_class
, locks_before
));
1578 static void print_lock_trace(const struct lock_trace
*trace
,
1579 unsigned int spaces
)
1581 stack_trace_print(trace
->entries
, trace
->nr_entries
, spaces
);
1585 * Print a dependency chain entry (this is only done when a deadlock
1586 * has been detected):
1588 static noinline
void
1589 print_circular_bug_entry(struct lock_list
*target
, int depth
)
1591 if (debug_locks_silent
)
1593 printk("\n-> #%u", depth
);
1594 print_lock_name(target
->class);
1595 printk(KERN_CONT
":\n");
1596 print_lock_trace(target
->trace
, 6);
1600 print_circular_lock_scenario(struct held_lock
*src
,
1601 struct held_lock
*tgt
,
1602 struct lock_list
*prt
)
1604 struct lock_class
*source
= hlock_class(src
);
1605 struct lock_class
*target
= hlock_class(tgt
);
1606 struct lock_class
*parent
= prt
->class;
1609 * A direct locking problem where unsafe_class lock is taken
1610 * directly by safe_class lock, then all we need to show
1611 * is the deadlock scenario, as it is obvious that the
1612 * unsafe lock is taken under the safe lock.
1614 * But if there is a chain instead, where the safe lock takes
1615 * an intermediate lock (middle_class) where this lock is
1616 * not the same as the safe lock, then the lock chain is
1617 * used to describe the problem. Otherwise we would need
1618 * to show a different CPU case for each link in the chain
1619 * from the safe_class lock to the unsafe_class lock.
1621 if (parent
!= source
) {
1622 printk("Chain exists of:\n ");
1623 __print_lock_name(source
);
1624 printk(KERN_CONT
" --> ");
1625 __print_lock_name(parent
);
1626 printk(KERN_CONT
" --> ");
1627 __print_lock_name(target
);
1628 printk(KERN_CONT
"\n\n");
1631 printk(" Possible unsafe locking scenario:\n\n");
1632 printk(" CPU0 CPU1\n");
1633 printk(" ---- ----\n");
1635 __print_lock_name(target
);
1636 printk(KERN_CONT
");\n");
1638 __print_lock_name(parent
);
1639 printk(KERN_CONT
");\n");
1641 __print_lock_name(target
);
1642 printk(KERN_CONT
");\n");
1644 __print_lock_name(source
);
1645 printk(KERN_CONT
");\n");
1646 printk("\n *** DEADLOCK ***\n\n");
1650 * When a circular dependency is detected, print the
1653 static noinline
void
1654 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
,
1655 struct held_lock
*check_src
,
1656 struct held_lock
*check_tgt
)
1658 struct task_struct
*curr
= current
;
1660 if (debug_locks_silent
)
1664 pr_warn("======================================================\n");
1665 pr_warn("WARNING: possible circular locking dependency detected\n");
1666 print_kernel_ident();
1667 pr_warn("------------------------------------------------------\n");
1668 pr_warn("%s/%d is trying to acquire lock:\n",
1669 curr
->comm
, task_pid_nr(curr
));
1670 print_lock(check_src
);
1672 pr_warn("\nbut task is already holding lock:\n");
1674 print_lock(check_tgt
);
1675 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1676 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1678 print_circular_bug_entry(entry
, depth
);
1681 static inline int class_equal(struct lock_list
*entry
, void *data
)
1683 return entry
->class == data
;
1686 static noinline
void print_circular_bug(struct lock_list
*this,
1687 struct lock_list
*target
,
1688 struct held_lock
*check_src
,
1689 struct held_lock
*check_tgt
)
1691 struct task_struct
*curr
= current
;
1692 struct lock_list
*parent
;
1693 struct lock_list
*first_parent
;
1696 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1699 this->trace
= save_trace();
1703 depth
= get_lock_depth(target
);
1705 print_circular_bug_header(target
, depth
, check_src
, check_tgt
);
1707 parent
= get_lock_parent(target
);
1708 first_parent
= parent
;
1711 print_circular_bug_entry(parent
, --depth
);
1712 parent
= get_lock_parent(parent
);
1715 printk("\nother info that might help us debug this:\n\n");
1716 print_circular_lock_scenario(check_src
, check_tgt
,
1719 lockdep_print_held_locks(curr
);
1721 printk("\nstack backtrace:\n");
1725 static noinline
void print_bfs_bug(int ret
)
1727 if (!debug_locks_off_graph_unlock())
1731 * Breadth-first-search failed, graph got corrupted?
1733 WARN(1, "lockdep bfs error:%d\n", ret
);
1736 static int noop_count(struct lock_list
*entry
, void *data
)
1738 (*(unsigned long *)data
)++;
1742 static unsigned long __lockdep_count_forward_deps(struct lock_list
*this)
1744 unsigned long count
= 0;
1745 struct lock_list
*uninitialized_var(target_entry
);
1747 __bfs_forwards(this, (void *)&count
, noop_count
, &target_entry
);
1751 unsigned long lockdep_count_forward_deps(struct lock_class
*class)
1753 unsigned long ret
, flags
;
1754 struct lock_list
this;
1759 raw_local_irq_save(flags
);
1761 ret
= __lockdep_count_forward_deps(&this);
1763 raw_local_irq_restore(flags
);
1768 static unsigned long __lockdep_count_backward_deps(struct lock_list
*this)
1770 unsigned long count
= 0;
1771 struct lock_list
*uninitialized_var(target_entry
);
1773 __bfs_backwards(this, (void *)&count
, noop_count
, &target_entry
);
1778 unsigned long lockdep_count_backward_deps(struct lock_class
*class)
1780 unsigned long ret
, flags
;
1781 struct lock_list
this;
1786 raw_local_irq_save(flags
);
1788 ret
= __lockdep_count_backward_deps(&this);
1790 raw_local_irq_restore(flags
);
1796 * Check that the dependency graph starting at <src> can lead to
1797 * <target> or not. Print an error and return 0 if it does.
1800 check_path(struct lock_class
*target
, struct lock_list
*src_entry
,
1801 struct lock_list
**target_entry
)
1805 ret
= __bfs_forwards(src_entry
, (void *)target
, class_equal
,
1808 if (unlikely(ret
< 0))
1815 * Prove that the dependency graph starting at <src> can not
1816 * lead to <target>. If it can, there is a circle when adding
1817 * <target> -> <src> dependency.
1819 * Print an error and return 0 if it does.
1822 check_noncircular(struct held_lock
*src
, struct held_lock
*target
,
1823 struct lock_trace
**const trace
)
1826 struct lock_list
*uninitialized_var(target_entry
);
1827 struct lock_list src_entry
= {
1828 .class = hlock_class(src
),
1832 debug_atomic_inc(nr_cyclic_checks
);
1834 ret
= check_path(hlock_class(target
), &src_entry
, &target_entry
);
1836 if (unlikely(!ret
)) {
1839 * If save_trace fails here, the printing might
1840 * trigger a WARN but because of the !nr_entries it
1841 * should not do bad things.
1843 *trace
= save_trace();
1846 print_circular_bug(&src_entry
, target_entry
, src
, target
);
1852 #ifdef CONFIG_LOCKDEP_SMALL
1854 * Check that the dependency graph starting at <src> can lead to
1855 * <target> or not. If it can, <src> -> <target> dependency is already
1858 * Print an error and return 2 if it does or 1 if it does not.
1861 check_redundant(struct held_lock
*src
, struct held_lock
*target
)
1864 struct lock_list
*uninitialized_var(target_entry
);
1865 struct lock_list src_entry
= {
1866 .class = hlock_class(src
),
1870 debug_atomic_inc(nr_redundant_checks
);
1872 ret
= check_path(hlock_class(target
), &src_entry
, &target_entry
);
1875 debug_atomic_inc(nr_redundant
);
1884 #ifdef CONFIG_TRACE_IRQFLAGS
1886 static inline int usage_accumulate(struct lock_list
*entry
, void *mask
)
1888 *(unsigned long *)mask
|= entry
->class->usage_mask
;
1894 * Forwards and backwards subgraph searching, for the purposes of
1895 * proving that two subgraphs can be connected by a new dependency
1896 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1899 static inline int usage_match(struct lock_list
*entry
, void *mask
)
1901 return entry
->class->usage_mask
& *(unsigned long *)mask
;
1905 * Find a node in the forwards-direction dependency sub-graph starting
1906 * at @root->class that matches @bit.
1908 * Return 0 if such a node exists in the subgraph, and put that node
1909 * into *@target_entry.
1911 * Return 1 otherwise and keep *@target_entry unchanged.
1912 * Return <0 on error.
1915 find_usage_forwards(struct lock_list
*root
, unsigned long usage_mask
,
1916 struct lock_list
**target_entry
)
1920 debug_atomic_inc(nr_find_usage_forwards_checks
);
1922 result
= __bfs_forwards(root
, &usage_mask
, usage_match
, target_entry
);
1928 * Find a node in the backwards-direction dependency sub-graph starting
1929 * at @root->class that matches @bit.
1931 * Return 0 if such a node exists in the subgraph, and put that node
1932 * into *@target_entry.
1934 * Return 1 otherwise and keep *@target_entry unchanged.
1935 * Return <0 on error.
1938 find_usage_backwards(struct lock_list
*root
, unsigned long usage_mask
,
1939 struct lock_list
**target_entry
)
1943 debug_atomic_inc(nr_find_usage_backwards_checks
);
1945 result
= __bfs_backwards(root
, &usage_mask
, usage_match
, target_entry
);
1950 static void print_lock_class_header(struct lock_class
*class, int depth
)
1954 printk("%*s->", depth
, "");
1955 print_lock_name(class);
1956 #ifdef CONFIG_DEBUG_LOCKDEP
1957 printk(KERN_CONT
" ops: %lu", debug_class_ops_read(class));
1959 printk(KERN_CONT
" {\n");
1961 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
1962 if (class->usage_mask
& (1 << bit
)) {
1965 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
1966 len
+= printk(KERN_CONT
" at:\n");
1967 print_lock_trace(class->usage_traces
[bit
], len
);
1970 printk("%*s }\n", depth
, "");
1972 printk("%*s ... key at: [<%px>] %pS\n",
1973 depth
, "", class->key
, class->key
);
1977 * printk the shortest lock dependencies from @start to @end in reverse order:
1980 print_shortest_lock_dependencies(struct lock_list
*leaf
,
1981 struct lock_list
*root
)
1983 struct lock_list
*entry
= leaf
;
1986 /*compute depth from generated tree by BFS*/
1987 depth
= get_lock_depth(leaf
);
1990 print_lock_class_header(entry
->class, depth
);
1991 printk("%*s ... acquired at:\n", depth
, "");
1992 print_lock_trace(entry
->trace
, 2);
1995 if (depth
== 0 && (entry
!= root
)) {
1996 printk("lockdep:%s bad path found in chain graph\n", __func__
);
2000 entry
= get_lock_parent(entry
);
2002 } while (entry
&& (depth
>= 0));
2006 print_irq_lock_scenario(struct lock_list
*safe_entry
,
2007 struct lock_list
*unsafe_entry
,
2008 struct lock_class
*prev_class
,
2009 struct lock_class
*next_class
)
2011 struct lock_class
*safe_class
= safe_entry
->class;
2012 struct lock_class
*unsafe_class
= unsafe_entry
->class;
2013 struct lock_class
*middle_class
= prev_class
;
2015 if (middle_class
== safe_class
)
2016 middle_class
= next_class
;
2019 * A direct locking problem where unsafe_class lock is taken
2020 * directly by safe_class lock, then all we need to show
2021 * is the deadlock scenario, as it is obvious that the
2022 * unsafe lock is taken under the safe lock.
2024 * But if there is a chain instead, where the safe lock takes
2025 * an intermediate lock (middle_class) where this lock is
2026 * not the same as the safe lock, then the lock chain is
2027 * used to describe the problem. Otherwise we would need
2028 * to show a different CPU case for each link in the chain
2029 * from the safe_class lock to the unsafe_class lock.
2031 if (middle_class
!= unsafe_class
) {
2032 printk("Chain exists of:\n ");
2033 __print_lock_name(safe_class
);
2034 printk(KERN_CONT
" --> ");
2035 __print_lock_name(middle_class
);
2036 printk(KERN_CONT
" --> ");
2037 __print_lock_name(unsafe_class
);
2038 printk(KERN_CONT
"\n\n");
2041 printk(" Possible interrupt unsafe locking scenario:\n\n");
2042 printk(" CPU0 CPU1\n");
2043 printk(" ---- ----\n");
2045 __print_lock_name(unsafe_class
);
2046 printk(KERN_CONT
");\n");
2047 printk(" local_irq_disable();\n");
2049 __print_lock_name(safe_class
);
2050 printk(KERN_CONT
");\n");
2052 __print_lock_name(middle_class
);
2053 printk(KERN_CONT
");\n");
2054 printk(" <Interrupt>\n");
2056 __print_lock_name(safe_class
);
2057 printk(KERN_CONT
");\n");
2058 printk("\n *** DEADLOCK ***\n\n");
2062 print_bad_irq_dependency(struct task_struct
*curr
,
2063 struct lock_list
*prev_root
,
2064 struct lock_list
*next_root
,
2065 struct lock_list
*backwards_entry
,
2066 struct lock_list
*forwards_entry
,
2067 struct held_lock
*prev
,
2068 struct held_lock
*next
,
2069 enum lock_usage_bit bit1
,
2070 enum lock_usage_bit bit2
,
2071 const char *irqclass
)
2073 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2077 pr_warn("=====================================================\n");
2078 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
2079 irqclass
, irqclass
);
2080 print_kernel_ident();
2081 pr_warn("-----------------------------------------------------\n");
2082 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
2083 curr
->comm
, task_pid_nr(curr
),
2084 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
2085 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
2086 curr
->hardirqs_enabled
,
2087 curr
->softirqs_enabled
);
2090 pr_warn("\nand this task is already holding:\n");
2092 pr_warn("which would create a new lock dependency:\n");
2093 print_lock_name(hlock_class(prev
));
2095 print_lock_name(hlock_class(next
));
2098 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
2100 print_lock_name(backwards_entry
->class);
2101 pr_warn("\n... which became %s-irq-safe at:\n", irqclass
);
2103 print_lock_trace(backwards_entry
->class->usage_traces
[bit1
], 1);
2105 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass
);
2106 print_lock_name(forwards_entry
->class);
2107 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass
);
2110 print_lock_trace(forwards_entry
->class->usage_traces
[bit2
], 1);
2112 pr_warn("\nother info that might help us debug this:\n\n");
2113 print_irq_lock_scenario(backwards_entry
, forwards_entry
,
2114 hlock_class(prev
), hlock_class(next
));
2116 lockdep_print_held_locks(curr
);
2118 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass
);
2119 prev_root
->trace
= save_trace();
2120 if (!prev_root
->trace
)
2122 print_shortest_lock_dependencies(backwards_entry
, prev_root
);
2124 pr_warn("\nthe dependencies between the lock to be acquired");
2125 pr_warn(" and %s-irq-unsafe lock:\n", irqclass
);
2126 next_root
->trace
= save_trace();
2127 if (!next_root
->trace
)
2129 print_shortest_lock_dependencies(forwards_entry
, next_root
);
2131 pr_warn("\nstack backtrace:\n");
2135 static const char *state_names
[] = {
2136 #define LOCKDEP_STATE(__STATE) \
2137 __stringify(__STATE),
2138 #include "lockdep_states.h"
2139 #undef LOCKDEP_STATE
2142 static const char *state_rnames
[] = {
2143 #define LOCKDEP_STATE(__STATE) \
2144 __stringify(__STATE)"-READ",
2145 #include "lockdep_states.h"
2146 #undef LOCKDEP_STATE
2149 static inline const char *state_name(enum lock_usage_bit bit
)
2151 if (bit
& LOCK_USAGE_READ_MASK
)
2152 return state_rnames
[bit
>> LOCK_USAGE_DIR_MASK
];
2154 return state_names
[bit
>> LOCK_USAGE_DIR_MASK
];
2158 * The bit number is encoded like:
2160 * bit0: 0 exclusive, 1 read lock
2161 * bit1: 0 used in irq, 1 irq enabled
2164 static int exclusive_bit(int new_bit
)
2166 int state
= new_bit
& LOCK_USAGE_STATE_MASK
;
2167 int dir
= new_bit
& LOCK_USAGE_DIR_MASK
;
2170 * keep state, bit flip the direction and strip read.
2172 return state
| (dir
^ LOCK_USAGE_DIR_MASK
);
2176 * Observe that when given a bitmask where each bitnr is encoded as above, a
2177 * right shift of the mask transforms the individual bitnrs as -1 and
2178 * conversely, a left shift transforms into +1 for the individual bitnrs.
2180 * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
2181 * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
2182 * instead by subtracting the bit number by 2, or shifting the mask right by 2.
2184 * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
2186 * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
2187 * all bits set) and recompose with bitnr1 flipped.
2189 static unsigned long invert_dir_mask(unsigned long mask
)
2191 unsigned long excl
= 0;
2194 excl
|= (mask
& LOCKF_ENABLED_IRQ_ALL
) >> LOCK_USAGE_DIR_MASK
;
2195 excl
|= (mask
& LOCKF_USED_IN_IRQ_ALL
) << LOCK_USAGE_DIR_MASK
;
2201 * As above, we clear bitnr0 (LOCK_*_READ off) with bitmask ops. First, for all
2202 * bits with bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*).
2203 * And then mask out all bitnr0.
2205 static unsigned long exclusive_mask(unsigned long mask
)
2207 unsigned long excl
= invert_dir_mask(mask
);
2210 excl
|= (excl
& LOCKF_IRQ_READ
) >> LOCK_USAGE_READ_MASK
;
2211 excl
&= ~LOCKF_IRQ_READ
;
2217 * Retrieve the _possible_ original mask to which @mask is
2218 * exclusive. Ie: this is the opposite of exclusive_mask().
2219 * Note that 2 possible original bits can match an exclusive
2220 * bit: one has LOCK_USAGE_READ_MASK set, the other has it
2221 * cleared. So both are returned for each exclusive bit.
2223 static unsigned long original_mask(unsigned long mask
)
2225 unsigned long excl
= invert_dir_mask(mask
);
2227 /* Include read in existing usages */
2228 excl
|= (excl
& LOCKF_IRQ
) << LOCK_USAGE_READ_MASK
;
2234 * Find the first pair of bit match between an original
2235 * usage mask and an exclusive usage mask.
2237 static int find_exclusive_match(unsigned long mask
,
2238 unsigned long excl_mask
,
2239 enum lock_usage_bit
*bitp
,
2240 enum lock_usage_bit
*excl_bitp
)
2244 for_each_set_bit(bit
, &mask
, LOCK_USED
) {
2245 excl
= exclusive_bit(bit
);
2246 if (excl_mask
& lock_flag(excl
)) {
2256 * Prove that the new dependency does not connect a hardirq-safe(-read)
2257 * lock with a hardirq-unsafe lock - to achieve this we search
2258 * the backwards-subgraph starting at <prev>, and the
2259 * forwards-subgraph starting at <next>:
2261 static int check_irq_usage(struct task_struct
*curr
, struct held_lock
*prev
,
2262 struct held_lock
*next
)
2264 unsigned long usage_mask
= 0, forward_mask
, backward_mask
;
2265 enum lock_usage_bit forward_bit
= 0, backward_bit
= 0;
2266 struct lock_list
*uninitialized_var(target_entry1
);
2267 struct lock_list
*uninitialized_var(target_entry
);
2268 struct lock_list
this, that
;
2272 * Step 1: gather all hard/soft IRQs usages backward in an
2273 * accumulated usage mask.
2276 this.class = hlock_class(prev
);
2278 ret
= __bfs_backwards(&this, &usage_mask
, usage_accumulate
, NULL
);
2284 usage_mask
&= LOCKF_USED_IN_IRQ_ALL
;
2289 * Step 2: find exclusive uses forward that match the previous
2290 * backward accumulated mask.
2292 forward_mask
= exclusive_mask(usage_mask
);
2295 that
.class = hlock_class(next
);
2297 ret
= find_usage_forwards(&that
, forward_mask
, &target_entry1
);
2306 * Step 3: we found a bad match! Now retrieve a lock from the backward
2307 * list whose usage mask matches the exclusive usage mask from the
2308 * lock found on the forward list.
2310 backward_mask
= original_mask(target_entry1
->class->usage_mask
);
2312 ret
= find_usage_backwards(&this, backward_mask
, &target_entry
);
2317 if (DEBUG_LOCKS_WARN_ON(ret
== 1))
2321 * Step 4: narrow down to a pair of incompatible usage bits
2324 ret
= find_exclusive_match(target_entry
->class->usage_mask
,
2325 target_entry1
->class->usage_mask
,
2326 &backward_bit
, &forward_bit
);
2327 if (DEBUG_LOCKS_WARN_ON(ret
== -1))
2330 print_bad_irq_dependency(curr
, &this, &that
,
2331 target_entry
, target_entry1
,
2333 backward_bit
, forward_bit
,
2334 state_name(backward_bit
));
2341 static inline int check_irq_usage(struct task_struct
*curr
,
2342 struct held_lock
*prev
, struct held_lock
*next
)
2346 #endif /* CONFIG_TRACE_IRQFLAGS */
2348 static void inc_chains(int irq_context
)
2350 if (irq_context
& LOCK_CHAIN_HARDIRQ_CONTEXT
)
2351 nr_hardirq_chains
++;
2352 else if (irq_context
& LOCK_CHAIN_SOFTIRQ_CONTEXT
)
2353 nr_softirq_chains
++;
2355 nr_process_chains
++;
2358 static void dec_chains(int irq_context
)
2360 if (irq_context
& LOCK_CHAIN_HARDIRQ_CONTEXT
)
2361 nr_hardirq_chains
--;
2362 else if (irq_context
& LOCK_CHAIN_SOFTIRQ_CONTEXT
)
2363 nr_softirq_chains
--;
2365 nr_process_chains
--;
2369 print_deadlock_scenario(struct held_lock
*nxt
, struct held_lock
*prv
)
2371 struct lock_class
*next
= hlock_class(nxt
);
2372 struct lock_class
*prev
= hlock_class(prv
);
2374 printk(" Possible unsafe locking scenario:\n\n");
2378 __print_lock_name(prev
);
2379 printk(KERN_CONT
");\n");
2381 __print_lock_name(next
);
2382 printk(KERN_CONT
");\n");
2383 printk("\n *** DEADLOCK ***\n\n");
2384 printk(" May be due to missing lock nesting notation\n\n");
2388 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
2389 struct held_lock
*next
)
2391 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
2395 pr_warn("============================================\n");
2396 pr_warn("WARNING: possible recursive locking detected\n");
2397 print_kernel_ident();
2398 pr_warn("--------------------------------------------\n");
2399 pr_warn("%s/%d is trying to acquire lock:\n",
2400 curr
->comm
, task_pid_nr(curr
));
2402 pr_warn("\nbut task is already holding lock:\n");
2405 pr_warn("\nother info that might help us debug this:\n");
2406 print_deadlock_scenario(next
, prev
);
2407 lockdep_print_held_locks(curr
);
2409 pr_warn("\nstack backtrace:\n");
2414 * Check whether we are holding such a class already.
2416 * (Note that this has to be done separately, because the graph cannot
2417 * detect such classes of deadlocks.)
2419 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
2422 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
)
2424 struct held_lock
*prev
;
2425 struct held_lock
*nest
= NULL
;
2428 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2429 prev
= curr
->held_locks
+ i
;
2431 if (prev
->instance
== next
->nest_lock
)
2434 if (hlock_class(prev
) != hlock_class(next
))
2438 * Allow read-after-read recursion of the same
2439 * lock class (i.e. read_lock(lock)+read_lock(lock)):
2441 if ((next
->read
== 2) && prev
->read
)
2445 * We're holding the nest_lock, which serializes this lock's
2446 * nesting behaviour.
2451 print_deadlock_bug(curr
, prev
, next
);
2458 * There was a chain-cache miss, and we are about to add a new dependency
2459 * to a previous lock. We validate the following rules:
2461 * - would the adding of the <prev> -> <next> dependency create a
2462 * circular dependency in the graph? [== circular deadlock]
2464 * - does the new prev->next dependency connect any hardirq-safe lock
2465 * (in the full backwards-subgraph starting at <prev>) with any
2466 * hardirq-unsafe lock (in the full forwards-subgraph starting at
2467 * <next>)? [== illegal lock inversion with hardirq contexts]
2469 * - does the new prev->next dependency connect any softirq-safe lock
2470 * (in the full backwards-subgraph starting at <prev>) with any
2471 * softirq-unsafe lock (in the full forwards-subgraph starting at
2472 * <next>)? [== illegal lock inversion with softirq contexts]
2474 * any of these scenarios could lead to a deadlock.
2476 * Then if all the validations pass, we add the forwards and backwards
2480 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
2481 struct held_lock
*next
, int distance
,
2482 struct lock_trace
**const trace
)
2484 struct lock_list
*entry
;
2487 if (!hlock_class(prev
)->key
|| !hlock_class(next
)->key
) {
2489 * The warning statements below may trigger a use-after-free
2490 * of the class name. It is better to trigger a use-after free
2491 * and to have the class name most of the time instead of not
2492 * having the class name available.
2494 WARN_ONCE(!debug_locks_silent
&& !hlock_class(prev
)->key
,
2495 "Detected use-after-free of lock class %px/%s\n",
2497 hlock_class(prev
)->name
);
2498 WARN_ONCE(!debug_locks_silent
&& !hlock_class(next
)->key
,
2499 "Detected use-after-free of lock class %px/%s\n",
2501 hlock_class(next
)->name
);
2506 * Prove that the new <prev> -> <next> dependency would not
2507 * create a circular dependency in the graph. (We do this by
2508 * a breadth-first search into the graph starting at <next>,
2509 * and check whether we can reach <prev>.)
2511 * The search is limited by the size of the circular queue (i.e.,
2512 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
2513 * in the graph whose neighbours are to be checked.
2515 ret
= check_noncircular(next
, prev
, trace
);
2516 if (unlikely(ret
<= 0))
2519 if (!check_irq_usage(curr
, prev
, next
))
2523 * For recursive read-locks we do all the dependency checks,
2524 * but we dont store read-triggered dependencies (only
2525 * write-triggered dependencies). This ensures that only the
2526 * write-side dependencies matter, and that if for example a
2527 * write-lock never takes any other locks, then the reads are
2528 * equivalent to a NOP.
2530 if (next
->read
== 2 || prev
->read
== 2)
2533 * Is the <prev> -> <next> dependency already present?
2535 * (this may occur even though this is a new chain: consider
2536 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
2537 * chains - the second one will be new, but L1 already has
2538 * L2 added to its dependency list, due to the first chain.)
2540 list_for_each_entry(entry
, &hlock_class(prev
)->locks_after
, entry
) {
2541 if (entry
->class == hlock_class(next
)) {
2543 entry
->distance
= 1;
2548 #ifdef CONFIG_LOCKDEP_SMALL
2550 * Is the <prev> -> <next> link redundant?
2552 ret
= check_redundant(prev
, next
);
2558 *trace
= save_trace();
2564 * Ok, all validations passed, add the new lock
2565 * to the previous lock's dependency list:
2567 ret
= add_lock_to_list(hlock_class(next
), hlock_class(prev
),
2568 &hlock_class(prev
)->locks_after
,
2569 next
->acquire_ip
, distance
, *trace
);
2574 ret
= add_lock_to_list(hlock_class(prev
), hlock_class(next
),
2575 &hlock_class(next
)->locks_before
,
2576 next
->acquire_ip
, distance
, *trace
);
2584 * Add the dependency to all directly-previous locks that are 'relevant'.
2585 * The ones that are relevant are (in increasing distance from curr):
2586 * all consecutive trylock entries and the final non-trylock entry - or
2587 * the end of this context's lock-chain - whichever comes first.
2590 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
2592 struct lock_trace
*trace
= NULL
;
2593 int depth
= curr
->lockdep_depth
;
2594 struct held_lock
*hlock
;
2599 * Depth must not be zero for a non-head lock:
2604 * At least two relevant locks must exist for this
2607 if (curr
->held_locks
[depth
].irq_context
!=
2608 curr
->held_locks
[depth
-1].irq_context
)
2612 int distance
= curr
->lockdep_depth
- depth
+ 1;
2613 hlock
= curr
->held_locks
+ depth
- 1;
2616 * Only non-recursive-read entries get new dependencies
2619 if (hlock
->read
!= 2 && hlock
->check
) {
2620 int ret
= check_prev_add(curr
, hlock
, next
, distance
,
2626 * Stop after the first non-trylock entry,
2627 * as non-trylock entries have added their
2628 * own direct dependencies already, so this
2629 * lock is connected to them indirectly:
2631 if (!hlock
->trylock
)
2637 * End of lock-stack?
2642 * Stop the search if we cross into another context:
2644 if (curr
->held_locks
[depth
].irq_context
!=
2645 curr
->held_locks
[depth
-1].irq_context
)
2650 if (!debug_locks_off_graph_unlock())
2654 * Clearly we all shouldn't be here, but since we made it we
2655 * can reliable say we messed up our state. See the above two
2656 * gotos for reasons why we could possibly end up here.
2663 struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
2664 static DECLARE_BITMAP(lock_chains_in_use
, MAX_LOCKDEP_CHAINS
);
2665 static u16 chain_hlocks
[MAX_LOCKDEP_CHAIN_HLOCKS
];
2666 unsigned long nr_zapped_lock_chains
;
2667 unsigned int nr_free_chain_hlocks
; /* Free chain_hlocks in buckets */
2668 unsigned int nr_lost_chain_hlocks
; /* Lost chain_hlocks */
2669 unsigned int nr_large_chain_blocks
; /* size > MAX_CHAIN_BUCKETS */
2672 * The first 2 chain_hlocks entries in the chain block in the bucket
2673 * list contains the following meta data:
2676 * Bit 15 - always set to 1 (it is not a class index)
2677 * Bits 0-14 - upper 15 bits of the next block index
2678 * entry[1] - lower 16 bits of next block index
2680 * A next block index of all 1 bits means it is the end of the list.
2682 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
2683 * the chain block size:
2685 * entry[2] - upper 16 bits of the chain block size
2686 * entry[3] - lower 16 bits of the chain block size
2688 #define MAX_CHAIN_BUCKETS 16
2689 #define CHAIN_BLK_FLAG (1U << 15)
2690 #define CHAIN_BLK_LIST_END 0xFFFFU
2692 static int chain_block_buckets
[MAX_CHAIN_BUCKETS
];
2694 static inline int size_to_bucket(int size
)
2696 if (size
> MAX_CHAIN_BUCKETS
)
2703 * Iterate all the chain blocks in a bucket.
2705 #define for_each_chain_block(bucket, prev, curr) \
2706 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
2708 (prev) = (curr), (curr) = chain_block_next(curr))
2713 static inline int chain_block_next(int offset
)
2715 int next
= chain_hlocks
[offset
];
2717 WARN_ON_ONCE(!(next
& CHAIN_BLK_FLAG
));
2719 if (next
== CHAIN_BLK_LIST_END
)
2722 next
&= ~CHAIN_BLK_FLAG
;
2724 next
|= chain_hlocks
[offset
+ 1];
2732 static inline int chain_block_size(int offset
)
2734 return (chain_hlocks
[offset
+ 2] << 16) | chain_hlocks
[offset
+ 3];
2737 static inline void init_chain_block(int offset
, int next
, int bucket
, int size
)
2739 chain_hlocks
[offset
] = (next
>> 16) | CHAIN_BLK_FLAG
;
2740 chain_hlocks
[offset
+ 1] = (u16
)next
;
2742 if (size
&& !bucket
) {
2743 chain_hlocks
[offset
+ 2] = size
>> 16;
2744 chain_hlocks
[offset
+ 3] = (u16
)size
;
2748 static inline void add_chain_block(int offset
, int size
)
2750 int bucket
= size_to_bucket(size
);
2751 int next
= chain_block_buckets
[bucket
];
2754 if (unlikely(size
< 2)) {
2756 * We can't store single entries on the freelist. Leak them.
2758 * One possible way out would be to uniquely mark them, other
2759 * than with CHAIN_BLK_FLAG, such that we can recover them when
2760 * the block before it is re-added.
2763 nr_lost_chain_hlocks
++;
2767 nr_free_chain_hlocks
+= size
;
2769 nr_large_chain_blocks
++;
2772 * Variable sized, sort large to small.
2774 for_each_chain_block(0, prev
, curr
) {
2775 if (size
>= chain_block_size(curr
))
2778 init_chain_block(offset
, curr
, 0, size
);
2780 chain_block_buckets
[0] = offset
;
2782 init_chain_block(prev
, offset
, 0, 0);
2786 * Fixed size, add to head.
2788 init_chain_block(offset
, next
, bucket
, size
);
2789 chain_block_buckets
[bucket
] = offset
;
2793 * Only the first block in the list can be deleted.
2795 * For the variable size bucket[0], the first block (the largest one) is
2796 * returned, broken up and put back into the pool. So if a chain block of
2797 * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
2798 * queued up after the primordial chain block and never be used until the
2799 * hlock entries in the primordial chain block is almost used up. That
2800 * causes fragmentation and reduce allocation efficiency. That can be
2801 * monitored by looking at the "large chain blocks" number in lockdep_stats.
2803 static inline void del_chain_block(int bucket
, int size
, int next
)
2805 nr_free_chain_hlocks
-= size
;
2806 chain_block_buckets
[bucket
] = next
;
2809 nr_large_chain_blocks
--;
2812 static void init_chain_block_buckets(void)
2816 for (i
= 0; i
< MAX_CHAIN_BUCKETS
; i
++)
2817 chain_block_buckets
[i
] = -1;
2819 add_chain_block(0, ARRAY_SIZE(chain_hlocks
));
2823 * Return offset of a chain block of the right size or -1 if not found.
2825 * Fairly simple worst-fit allocator with the addition of a number of size
2826 * specific free lists.
2828 static int alloc_chain_hlocks(int req
)
2830 int bucket
, curr
, size
;
2833 * We rely on the MSB to act as an escape bit to denote freelist
2834 * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
2836 BUILD_BUG_ON((MAX_LOCKDEP_KEYS
-1) & CHAIN_BLK_FLAG
);
2838 init_data_structures_once();
2840 if (nr_free_chain_hlocks
< req
)
2844 * We require a minimum of 2 (u16) entries to encode a freelist
2848 bucket
= size_to_bucket(req
);
2849 curr
= chain_block_buckets
[bucket
];
2853 del_chain_block(bucket
, req
, chain_block_next(curr
));
2857 curr
= chain_block_buckets
[0];
2861 * The variable sized freelist is sorted by size; the first entry is
2862 * the largest. Use it if it fits.
2865 size
= chain_block_size(curr
);
2866 if (likely(size
>= req
)) {
2867 del_chain_block(0, size
, chain_block_next(curr
));
2868 add_chain_block(curr
+ req
, size
- req
);
2874 * Last resort, split a block in a larger sized bucket.
2876 for (size
= MAX_CHAIN_BUCKETS
; size
> req
; size
--) {
2877 bucket
= size_to_bucket(size
);
2878 curr
= chain_block_buckets
[bucket
];
2882 del_chain_block(bucket
, size
, chain_block_next(curr
));
2883 add_chain_block(curr
+ req
, size
- req
);
2890 static inline void free_chain_hlocks(int base
, int size
)
2892 add_chain_block(base
, max(size
, 2));
2895 struct lock_class
*lock_chain_get_class(struct lock_chain
*chain
, int i
)
2897 return lock_classes
+ chain_hlocks
[chain
->base
+ i
];
2901 * Returns the index of the first held_lock of the current chain
2903 static inline int get_first_held_lock(struct task_struct
*curr
,
2904 struct held_lock
*hlock
)
2907 struct held_lock
*hlock_curr
;
2909 for (i
= curr
->lockdep_depth
- 1; i
>= 0; i
--) {
2910 hlock_curr
= curr
->held_locks
+ i
;
2911 if (hlock_curr
->irq_context
!= hlock
->irq_context
)
2919 #ifdef CONFIG_DEBUG_LOCKDEP
2921 * Returns the next chain_key iteration
2923 static u64
print_chain_key_iteration(int class_idx
, u64 chain_key
)
2925 u64 new_chain_key
= iterate_chain_key(chain_key
, class_idx
);
2927 printk(" class_idx:%d -> chain_key:%016Lx",
2929 (unsigned long long)new_chain_key
);
2930 return new_chain_key
;
2934 print_chain_keys_held_locks(struct task_struct
*curr
, struct held_lock
*hlock_next
)
2936 struct held_lock
*hlock
;
2937 u64 chain_key
= INITIAL_CHAIN_KEY
;
2938 int depth
= curr
->lockdep_depth
;
2939 int i
= get_first_held_lock(curr
, hlock_next
);
2941 printk("depth: %u (irq_context %u)\n", depth
- i
+ 1,
2942 hlock_next
->irq_context
);
2943 for (; i
< depth
; i
++) {
2944 hlock
= curr
->held_locks
+ i
;
2945 chain_key
= print_chain_key_iteration(hlock
->class_idx
, chain_key
);
2950 print_chain_key_iteration(hlock_next
->class_idx
, chain_key
);
2951 print_lock(hlock_next
);
2954 static void print_chain_keys_chain(struct lock_chain
*chain
)
2957 u64 chain_key
= INITIAL_CHAIN_KEY
;
2960 printk("depth: %u\n", chain
->depth
);
2961 for (i
= 0; i
< chain
->depth
; i
++) {
2962 class_id
= chain_hlocks
[chain
->base
+ i
];
2963 chain_key
= print_chain_key_iteration(class_id
, chain_key
);
2965 print_lock_name(lock_classes
+ class_id
);
2970 static void print_collision(struct task_struct
*curr
,
2971 struct held_lock
*hlock_next
,
2972 struct lock_chain
*chain
)
2975 pr_warn("============================\n");
2976 pr_warn("WARNING: chain_key collision\n");
2977 print_kernel_ident();
2978 pr_warn("----------------------------\n");
2979 pr_warn("%s/%d: ", current
->comm
, task_pid_nr(current
));
2980 pr_warn("Hash chain already cached but the contents don't match!\n");
2982 pr_warn("Held locks:");
2983 print_chain_keys_held_locks(curr
, hlock_next
);
2985 pr_warn("Locks in cached chain:");
2986 print_chain_keys_chain(chain
);
2988 pr_warn("\nstack backtrace:\n");
2994 * Checks whether the chain and the current held locks are consistent
2995 * in depth and also in content. If they are not it most likely means
2996 * that there was a collision during the calculation of the chain_key.
2997 * Returns: 0 not passed, 1 passed
2999 static int check_no_collision(struct task_struct
*curr
,
3000 struct held_lock
*hlock
,
3001 struct lock_chain
*chain
)
3003 #ifdef CONFIG_DEBUG_LOCKDEP
3006 i
= get_first_held_lock(curr
, hlock
);
3008 if (DEBUG_LOCKS_WARN_ON(chain
->depth
!= curr
->lockdep_depth
- (i
- 1))) {
3009 print_collision(curr
, hlock
, chain
);
3013 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
3014 id
= curr
->held_locks
[i
].class_idx
;
3016 if (DEBUG_LOCKS_WARN_ON(chain_hlocks
[chain
->base
+ j
] != id
)) {
3017 print_collision(curr
, hlock
, chain
);
3026 * Given an index that is >= -1, return the index of the next lock chain.
3027 * Return -2 if there is no next lock chain.
3029 long lockdep_next_lockchain(long i
)
3031 i
= find_next_bit(lock_chains_in_use
, ARRAY_SIZE(lock_chains
), i
+ 1);
3032 return i
< ARRAY_SIZE(lock_chains
) ? i
: -2;
3035 unsigned long lock_chain_count(void)
3037 return bitmap_weight(lock_chains_in_use
, ARRAY_SIZE(lock_chains
));
3040 /* Must be called with the graph lock held. */
3041 static struct lock_chain
*alloc_lock_chain(void)
3043 int idx
= find_first_zero_bit(lock_chains_in_use
,
3044 ARRAY_SIZE(lock_chains
));
3046 if (unlikely(idx
>= ARRAY_SIZE(lock_chains
)))
3048 __set_bit(idx
, lock_chains_in_use
);
3049 return lock_chains
+ idx
;
3053 * Adds a dependency chain into chain hashtable. And must be called with
3056 * Return 0 if fail, and graph_lock is released.
3057 * Return 1 if succeed, with graph_lock held.
3059 static inline int add_chain_cache(struct task_struct
*curr
,
3060 struct held_lock
*hlock
,
3063 struct lock_class
*class = hlock_class(hlock
);
3064 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
3065 struct lock_chain
*chain
;
3069 * The caller must hold the graph lock, ensure we've got IRQs
3070 * disabled to make this an IRQ-safe lock.. for recursion reasons
3071 * lockdep won't complain about its own locking errors.
3073 if (lockdep_assert_locked())
3076 chain
= alloc_lock_chain();
3078 if (!debug_locks_off_graph_unlock())
3081 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
3085 chain
->chain_key
= chain_key
;
3086 chain
->irq_context
= hlock
->irq_context
;
3087 i
= get_first_held_lock(curr
, hlock
);
3088 chain
->depth
= curr
->lockdep_depth
+ 1 - i
;
3090 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks
));
3091 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr
->held_locks
));
3092 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks
[0])) <= ARRAY_SIZE(lock_classes
));
3094 j
= alloc_chain_hlocks(chain
->depth
);
3096 if (!debug_locks_off_graph_unlock())
3099 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
3105 for (j
= 0; j
< chain
->depth
- 1; j
++, i
++) {
3106 int lock_id
= curr
->held_locks
[i
].class_idx
;
3108 chain_hlocks
[chain
->base
+ j
] = lock_id
;
3110 chain_hlocks
[chain
->base
+ j
] = class - lock_classes
;
3111 hlist_add_head_rcu(&chain
->entry
, hash_head
);
3112 debug_atomic_inc(chain_lookup_misses
);
3113 inc_chains(chain
->irq_context
);
3119 * Look up a dependency chain. Must be called with either the graph lock or
3120 * the RCU read lock held.
3122 static inline struct lock_chain
*lookup_chain_cache(u64 chain_key
)
3124 struct hlist_head
*hash_head
= chainhashentry(chain_key
);
3125 struct lock_chain
*chain
;
3127 hlist_for_each_entry_rcu(chain
, hash_head
, entry
) {
3128 if (READ_ONCE(chain
->chain_key
) == chain_key
) {
3129 debug_atomic_inc(chain_lookup_hits
);
3137 * If the key is not present yet in dependency chain cache then
3138 * add it and return 1 - in this case the new dependency chain is
3139 * validated. If the key is already hashed, return 0.
3140 * (On return with 1 graph_lock is held.)
3142 static inline int lookup_chain_cache_add(struct task_struct
*curr
,
3143 struct held_lock
*hlock
,
3146 struct lock_class
*class = hlock_class(hlock
);
3147 struct lock_chain
*chain
= lookup_chain_cache(chain_key
);
3151 if (!check_no_collision(curr
, hlock
, chain
))
3154 if (very_verbose(class)) {
3155 printk("\nhash chain already cached, key: "
3156 "%016Lx tail class: [%px] %s\n",
3157 (unsigned long long)chain_key
,
3158 class->key
, class->name
);
3164 if (very_verbose(class)) {
3165 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
3166 (unsigned long long)chain_key
, class->key
, class->name
);
3173 * We have to walk the chain again locked - to avoid duplicates:
3175 chain
= lookup_chain_cache(chain_key
);
3181 if (!add_chain_cache(curr
, hlock
, chain_key
))
3187 static int validate_chain(struct task_struct
*curr
,
3188 struct held_lock
*hlock
,
3189 int chain_head
, u64 chain_key
)
3192 * Trylock needs to maintain the stack of held locks, but it
3193 * does not add new dependencies, because trylock can be done
3196 * We look up the chain_key and do the O(N^2) check and update of
3197 * the dependencies only if this is a new dependency chain.
3198 * (If lookup_chain_cache_add() return with 1 it acquires
3199 * graph_lock for us)
3201 if (!hlock
->trylock
&& hlock
->check
&&
3202 lookup_chain_cache_add(curr
, hlock
, chain_key
)) {
3204 * Check whether last held lock:
3206 * - is irq-safe, if this lock is irq-unsafe
3207 * - is softirq-safe, if this lock is hardirq-unsafe
3209 * And check whether the new lock's dependency graph
3210 * could lead back to the previous lock:
3212 * - within the current held-lock stack
3213 * - across our accumulated lock dependency records
3215 * any of these scenarios could lead to a deadlock.
3218 * The simple case: does the current hold the same lock
3221 int ret
= check_deadlock(curr
, hlock
);
3226 * Mark recursive read, as we jump over it when
3227 * building dependencies (just like we jump over
3233 * Add dependency only if this lock is not the head
3234 * of the chain, and if it's not a secondary read-lock:
3236 if (!chain_head
&& ret
!= 2) {
3237 if (!check_prevs_add(curr
, hlock
))
3243 /* after lookup_chain_cache_add(): */
3244 if (unlikely(!debug_locks
))
3251 static inline int validate_chain(struct task_struct
*curr
,
3252 struct held_lock
*hlock
,
3253 int chain_head
, u64 chain_key
)
3258 static void init_chain_block_buckets(void) { }
3259 #endif /* CONFIG_PROVE_LOCKING */
3262 * We are building curr_chain_key incrementally, so double-check
3263 * it from scratch, to make sure that it's done correctly:
3265 static void check_chain_key(struct task_struct
*curr
)
3267 #ifdef CONFIG_DEBUG_LOCKDEP
3268 struct held_lock
*hlock
, *prev_hlock
= NULL
;
3270 u64 chain_key
= INITIAL_CHAIN_KEY
;
3272 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3273 hlock
= curr
->held_locks
+ i
;
3274 if (chain_key
!= hlock
->prev_chain_key
) {
3277 * We got mighty confused, our chain keys don't match
3278 * with what we expect, someone trample on our task state?
3280 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
3281 curr
->lockdep_depth
, i
,
3282 (unsigned long long)chain_key
,
3283 (unsigned long long)hlock
->prev_chain_key
);
3288 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
3289 * it registered lock class index?
3291 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock
->class_idx
, lock_classes_in_use
)))
3294 if (prev_hlock
&& (prev_hlock
->irq_context
!=
3295 hlock
->irq_context
))
3296 chain_key
= INITIAL_CHAIN_KEY
;
3297 chain_key
= iterate_chain_key(chain_key
, hlock
->class_idx
);
3300 if (chain_key
!= curr
->curr_chain_key
) {
3303 * More smoking hash instead of calculating it, damn see these
3304 * numbers float.. I bet that a pink elephant stepped on my memory.
3306 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
3307 curr
->lockdep_depth
, i
,
3308 (unsigned long long)chain_key
,
3309 (unsigned long long)curr
->curr_chain_key
);
3314 #ifdef CONFIG_PROVE_LOCKING
3315 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
3316 enum lock_usage_bit new_bit
);
3318 static void print_usage_bug_scenario(struct held_lock
*lock
)
3320 struct lock_class
*class = hlock_class(lock
);
3322 printk(" Possible unsafe locking scenario:\n\n");
3326 __print_lock_name(class);
3327 printk(KERN_CONT
");\n");
3328 printk(" <Interrupt>\n");
3330 __print_lock_name(class);
3331 printk(KERN_CONT
");\n");
3332 printk("\n *** DEADLOCK ***\n\n");
3336 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
3337 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
3339 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
3343 pr_warn("================================\n");
3344 pr_warn("WARNING: inconsistent lock state\n");
3345 print_kernel_ident();
3346 pr_warn("--------------------------------\n");
3348 pr_warn("inconsistent {%s} -> {%s} usage.\n",
3349 usage_str
[prev_bit
], usage_str
[new_bit
]);
3351 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
3352 curr
->comm
, task_pid_nr(curr
),
3353 lockdep_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
3354 lockdep_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
3355 lockdep_hardirqs_enabled(curr
),
3356 lockdep_softirqs_enabled(curr
));
3359 pr_warn("{%s} state was registered at:\n", usage_str
[prev_bit
]);
3360 print_lock_trace(hlock_class(this)->usage_traces
[prev_bit
], 1);
3362 print_irqtrace_events(curr
);
3363 pr_warn("\nother info that might help us debug this:\n");
3364 print_usage_bug_scenario(this);
3366 lockdep_print_held_locks(curr
);
3368 pr_warn("\nstack backtrace:\n");
3373 * Print out an error if an invalid bit is set:
3376 valid_state(struct task_struct
*curr
, struct held_lock
*this,
3377 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
3379 if (unlikely(hlock_class(this)->usage_mask
& (1 << bad_bit
))) {
3380 print_usage_bug(curr
, this, bad_bit
, new_bit
);
3388 * print irq inversion bug:
3391 print_irq_inversion_bug(struct task_struct
*curr
,
3392 struct lock_list
*root
, struct lock_list
*other
,
3393 struct held_lock
*this, int forwards
,
3394 const char *irqclass
)
3396 struct lock_list
*entry
= other
;
3397 struct lock_list
*middle
= NULL
;
3400 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
3404 pr_warn("========================================================\n");
3405 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
3406 print_kernel_ident();
3407 pr_warn("--------------------------------------------------------\n");
3408 pr_warn("%s/%d just changed the state of lock:\n",
3409 curr
->comm
, task_pid_nr(curr
));
3412 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass
);
3414 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass
);
3415 print_lock_name(other
->class);
3416 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
3418 pr_warn("\nother info that might help us debug this:\n");
3420 /* Find a middle lock (if one exists) */
3421 depth
= get_lock_depth(other
);
3423 if (depth
== 0 && (entry
!= root
)) {
3424 pr_warn("lockdep:%s bad path found in chain graph\n", __func__
);
3428 entry
= get_lock_parent(entry
);
3430 } while (entry
&& entry
!= root
&& (depth
>= 0));
3432 print_irq_lock_scenario(root
, other
,
3433 middle
? middle
->class : root
->class, other
->class);
3435 print_irq_lock_scenario(other
, root
,
3436 middle
? middle
->class : other
->class, root
->class);
3438 lockdep_print_held_locks(curr
);
3440 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
3441 root
->trace
= save_trace();
3444 print_shortest_lock_dependencies(other
, root
);
3446 pr_warn("\nstack backtrace:\n");
3451 * Prove that in the forwards-direction subgraph starting at <this>
3452 * there is no lock matching <mask>:
3455 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
3456 enum lock_usage_bit bit
, const char *irqclass
)
3459 struct lock_list root
;
3460 struct lock_list
*uninitialized_var(target_entry
);
3463 root
.class = hlock_class(this);
3464 ret
= find_usage_forwards(&root
, lock_flag(bit
), &target_entry
);
3472 print_irq_inversion_bug(curr
, &root
, target_entry
,
3478 * Prove that in the backwards-direction subgraph starting at <this>
3479 * there is no lock matching <mask>:
3482 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
3483 enum lock_usage_bit bit
, const char *irqclass
)
3486 struct lock_list root
;
3487 struct lock_list
*uninitialized_var(target_entry
);
3490 root
.class = hlock_class(this);
3491 ret
= find_usage_backwards(&root
, lock_flag(bit
), &target_entry
);
3499 print_irq_inversion_bug(curr
, &root
, target_entry
,
3504 void print_irqtrace_events(struct task_struct
*curr
)
3506 printk("irq event stamp: %u\n", curr
->irq_events
);
3507 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
3508 curr
->hardirq_enable_event
, (void *)curr
->hardirq_enable_ip
,
3509 (void *)curr
->hardirq_enable_ip
);
3510 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
3511 curr
->hardirq_disable_event
, (void *)curr
->hardirq_disable_ip
,
3512 (void *)curr
->hardirq_disable_ip
);
3513 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
3514 curr
->softirq_enable_event
, (void *)curr
->softirq_enable_ip
,
3515 (void *)curr
->softirq_enable_ip
);
3516 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
3517 curr
->softirq_disable_event
, (void *)curr
->softirq_disable_ip
,
3518 (void *)curr
->softirq_disable_ip
);
3521 static int HARDIRQ_verbose(struct lock_class
*class)
3524 return class_filter(class);
3529 static int SOFTIRQ_verbose(struct lock_class
*class)
3532 return class_filter(class);
3537 #define STRICT_READ_CHECKS 1
3539 static int (*state_verbose_f
[])(struct lock_class
*class) = {
3540 #define LOCKDEP_STATE(__STATE) \
3542 #include "lockdep_states.h"
3543 #undef LOCKDEP_STATE
3546 static inline int state_verbose(enum lock_usage_bit bit
,
3547 struct lock_class
*class)
3549 return state_verbose_f
[bit
>> LOCK_USAGE_DIR_MASK
](class);
3552 typedef int (*check_usage_f
)(struct task_struct
*, struct held_lock
*,
3553 enum lock_usage_bit bit
, const char *name
);
3556 mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
3557 enum lock_usage_bit new_bit
)
3559 int excl_bit
= exclusive_bit(new_bit
);
3560 int read
= new_bit
& LOCK_USAGE_READ_MASK
;
3561 int dir
= new_bit
& LOCK_USAGE_DIR_MASK
;
3564 * mark USED_IN has to look forwards -- to ensure no dependency
3565 * has ENABLED state, which would allow recursion deadlocks.
3567 * mark ENABLED has to look backwards -- to ensure no dependee
3568 * has USED_IN state, which, again, would allow recursion deadlocks.
3570 check_usage_f usage
= dir
?
3571 check_usage_backwards
: check_usage_forwards
;
3574 * Validate that this particular lock does not have conflicting
3577 if (!valid_state(curr
, this, new_bit
, excl_bit
))
3581 * Validate that the lock dependencies don't have conflicting usage
3584 if ((!read
|| STRICT_READ_CHECKS
) &&
3585 !usage(curr
, this, excl_bit
, state_name(new_bit
& ~LOCK_USAGE_READ_MASK
)))
3589 * Check for read in write conflicts
3592 if (!valid_state(curr
, this, new_bit
, excl_bit
+ LOCK_USAGE_READ_MASK
))
3595 if (STRICT_READ_CHECKS
&&
3596 !usage(curr
, this, excl_bit
+ LOCK_USAGE_READ_MASK
,
3597 state_name(new_bit
+ LOCK_USAGE_READ_MASK
)))
3601 if (state_verbose(new_bit
, hlock_class(this)))
3608 * Mark all held locks with a usage bit:
3611 mark_held_locks(struct task_struct
*curr
, enum lock_usage_bit base_bit
)
3613 struct held_lock
*hlock
;
3616 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3617 enum lock_usage_bit hlock_bit
= base_bit
;
3618 hlock
= curr
->held_locks
+ i
;
3621 hlock_bit
+= LOCK_USAGE_READ_MASK
;
3623 BUG_ON(hlock_bit
>= LOCK_USAGE_STATES
);
3628 if (!mark_lock(curr
, hlock
, hlock_bit
))
3636 * Hardirqs will be enabled:
3638 static void __trace_hardirqs_on_caller(unsigned long ip
)
3640 struct task_struct
*curr
= current
;
3642 /* we'll do an OFF -> ON transition: */
3643 curr
->hardirqs_enabled
= 1;
3646 * We are going to turn hardirqs on, so set the
3647 * usage bit for all held locks:
3649 if (!mark_held_locks(curr
, LOCK_ENABLED_HARDIRQ
))
3652 * If we have softirqs enabled, then set the usage
3653 * bit for all held locks. (disabled hardirqs prevented
3654 * this bit from being set before)
3656 if (curr
->softirqs_enabled
)
3657 if (!mark_held_locks(curr
, LOCK_ENABLED_SOFTIRQ
))
3660 curr
->hardirq_enable_ip
= ip
;
3661 curr
->hardirq_enable_event
= ++curr
->irq_events
;
3662 debug_atomic_inc(hardirqs_on_events
);
3665 void lockdep_hardirqs_on(unsigned long ip
)
3667 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
3670 if (unlikely(current
->hardirqs_enabled
)) {
3672 * Neither irq nor preemption are disabled here
3673 * so this is racy by nature but losing one hit
3674 * in a stat is not a big deal.
3676 __debug_atomic_inc(redundant_hardirqs_on
);
3681 * We're enabling irqs and according to our state above irqs weren't
3682 * already enabled, yet we find the hardware thinks they are in fact
3683 * enabled.. someone messed up their IRQ state tracing.
3685 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3689 * See the fine text that goes along with this variable definition.
3691 if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled
))
3695 * Can't allow enabling interrupts while in an interrupt handler,
3696 * that's general bad form and such. Recursion, limited stack etc..
3698 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
3701 current
->lockdep_recursion
++;
3702 __trace_hardirqs_on_caller(ip
);
3703 lockdep_recursion_finish();
3705 NOKPROBE_SYMBOL(lockdep_hardirqs_on
);
3708 * Hardirqs were disabled:
3710 void lockdep_hardirqs_off(unsigned long ip
)
3712 struct task_struct
*curr
= current
;
3714 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
3718 * So we're supposed to get called after you mask local IRQs, but for
3719 * some reason the hardware doesn't quite think you did a proper job.
3721 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3724 if (curr
->hardirqs_enabled
) {
3726 * We have done an ON -> OFF transition:
3728 curr
->hardirqs_enabled
= 0;
3729 curr
->hardirq_disable_ip
= ip
;
3730 curr
->hardirq_disable_event
= ++curr
->irq_events
;
3731 debug_atomic_inc(hardirqs_off_events
);
3733 debug_atomic_inc(redundant_hardirqs_off
);
3735 NOKPROBE_SYMBOL(lockdep_hardirqs_off
);
3738 * Softirqs will be enabled:
3740 void lockdep_softirqs_on(unsigned long ip
)
3742 struct task_struct
*curr
= current
;
3744 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
3748 * We fancy IRQs being disabled here, see softirq.c, avoids
3749 * funny state and nesting things.
3751 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3754 if (curr
->softirqs_enabled
) {
3755 debug_atomic_inc(redundant_softirqs_on
);
3759 current
->lockdep_recursion
++;
3761 * We'll do an OFF -> ON transition:
3763 curr
->softirqs_enabled
= 1;
3764 curr
->softirq_enable_ip
= ip
;
3765 curr
->softirq_enable_event
= ++curr
->irq_events
;
3766 debug_atomic_inc(softirqs_on_events
);
3768 * We are going to turn softirqs on, so set the
3769 * usage bit for all held locks, if hardirqs are
3772 if (curr
->hardirqs_enabled
)
3773 mark_held_locks(curr
, LOCK_ENABLED_SOFTIRQ
);
3774 lockdep_recursion_finish();
3778 * Softirqs were disabled:
3780 void lockdep_softirqs_off(unsigned long ip
)
3782 struct task_struct
*curr
= current
;
3784 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
3788 * We fancy IRQs being disabled here, see softirq.c
3790 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3793 if (curr
->softirqs_enabled
) {
3795 * We have done an ON -> OFF transition:
3797 curr
->softirqs_enabled
= 0;
3798 curr
->softirq_disable_ip
= ip
;
3799 curr
->softirq_disable_event
= ++curr
->irq_events
;
3800 debug_atomic_inc(softirqs_off_events
);
3802 * Whoops, we wanted softirqs off, so why aren't they?
3804 DEBUG_LOCKS_WARN_ON(!softirq_count());
3806 debug_atomic_inc(redundant_softirqs_off
);
3810 mark_usage(struct task_struct
*curr
, struct held_lock
*hlock
, int check
)
3816 * If non-trylock use in a hardirq or softirq context, then
3817 * mark the lock as used in these contexts:
3819 if (!hlock
->trylock
) {
3821 if (curr
->hardirq_context
)
3822 if (!mark_lock(curr
, hlock
,
3823 LOCK_USED_IN_HARDIRQ_READ
))
3825 if (curr
->softirq_context
)
3826 if (!mark_lock(curr
, hlock
,
3827 LOCK_USED_IN_SOFTIRQ_READ
))
3830 if (curr
->hardirq_context
)
3831 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
3833 if (curr
->softirq_context
)
3834 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
3838 if (!hlock
->hardirqs_off
) {
3840 if (!mark_lock(curr
, hlock
,
3841 LOCK_ENABLED_HARDIRQ_READ
))
3843 if (curr
->softirqs_enabled
)
3844 if (!mark_lock(curr
, hlock
,
3845 LOCK_ENABLED_SOFTIRQ_READ
))
3848 if (!mark_lock(curr
, hlock
,
3849 LOCK_ENABLED_HARDIRQ
))
3851 if (curr
->softirqs_enabled
)
3852 if (!mark_lock(curr
, hlock
,
3853 LOCK_ENABLED_SOFTIRQ
))
3859 /* mark it as used: */
3860 if (!mark_lock(curr
, hlock
, LOCK_USED
))
3866 static inline unsigned int task_irq_context(struct task_struct
*task
)
3868 return LOCK_CHAIN_HARDIRQ_CONTEXT
* !!task
->hardirq_context
+
3869 LOCK_CHAIN_SOFTIRQ_CONTEXT
* !!task
->softirq_context
;
3872 static int separate_irq_context(struct task_struct
*curr
,
3873 struct held_lock
*hlock
)
3875 unsigned int depth
= curr
->lockdep_depth
;
3878 * Keep track of points where we cross into an interrupt context:
3881 struct held_lock
*prev_hlock
;
3883 prev_hlock
= curr
->held_locks
+ depth
-1;
3885 * If we cross into another context, reset the
3886 * hash key (this also prevents the checking and the
3887 * adding of the dependency to 'prev'):
3889 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
3896 * Mark a lock with a usage bit, and validate the state transition:
3898 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
3899 enum lock_usage_bit new_bit
)
3901 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
3903 if (new_bit
>= LOCK_USAGE_STATES
) {
3904 DEBUG_LOCKS_WARN_ON(1);
3909 * If already set then do not dirty the cacheline,
3910 * nor do any checks:
3912 if (likely(hlock_class(this)->usage_mask
& new_mask
))
3918 * Make sure we didn't race:
3920 if (unlikely(hlock_class(this)->usage_mask
& new_mask
)) {
3925 hlock_class(this)->usage_mask
|= new_mask
;
3927 if (!(hlock_class(this)->usage_traces
[new_bit
] = save_trace()))
3932 debug_atomic_dec(nr_unused_locks
);
3935 ret
= mark_lock_irq(curr
, this, new_bit
);
3943 * We must printk outside of the graph_lock:
3946 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
3948 print_irqtrace_events(curr
);
3955 static inline short task_wait_context(struct task_struct
*curr
)
3958 * Set appropriate wait type for the context; for IRQs we have to take
3959 * into account force_irqthread as that is implied by PREEMPT_RT.
3961 if (curr
->hardirq_context
) {
3963 * Check if force_irqthreads will run us threaded.
3965 if (curr
->hardirq_threaded
|| curr
->irq_config
)
3966 return LD_WAIT_CONFIG
;
3968 return LD_WAIT_SPIN
;
3969 } else if (curr
->softirq_context
) {
3971 * Softirqs are always threaded.
3973 return LD_WAIT_CONFIG
;
3980 print_lock_invalid_wait_context(struct task_struct
*curr
,
3981 struct held_lock
*hlock
)
3985 if (!debug_locks_off())
3987 if (debug_locks_silent
)
3991 pr_warn("=============================\n");
3992 pr_warn("[ BUG: Invalid wait context ]\n");
3993 print_kernel_ident();
3994 pr_warn("-----------------------------\n");
3996 pr_warn("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
3999 pr_warn("other info that might help us debug this:\n");
4001 curr_inner
= task_wait_context(curr
);
4002 pr_warn("context-{%d:%d}\n", curr_inner
, curr_inner
);
4004 lockdep_print_held_locks(curr
);
4006 pr_warn("stack backtrace:\n");
4013 * Verify the wait_type context.
4015 * This check validates we takes locks in the right wait-type order; that is it
4016 * ensures that we do not take mutexes inside spinlocks and do not attempt to
4017 * acquire spinlocks inside raw_spinlocks and the sort.
4019 * The entire thing is slightly more complex because of RCU, RCU is a lock that
4020 * can be taken from (pretty much) any context but also has constraints.
4021 * However when taken in a stricter environment the RCU lock does not loosen
4024 * Therefore we must look for the strictest environment in the lock stack and
4025 * compare that to the lock we're trying to acquire.
4027 static int check_wait_context(struct task_struct
*curr
, struct held_lock
*next
)
4029 short next_inner
= hlock_class(next
)->wait_type_inner
;
4030 short next_outer
= hlock_class(next
)->wait_type_outer
;
4034 if (!curr
->lockdep_depth
|| !next_inner
|| next
->trylock
)
4038 next_outer
= next_inner
;
4041 * Find start of current irq_context..
4043 for (depth
= curr
->lockdep_depth
- 1; depth
>= 0; depth
--) {
4044 struct held_lock
*prev
= curr
->held_locks
+ depth
;
4045 if (prev
->irq_context
!= next
->irq_context
)
4050 curr_inner
= task_wait_context(curr
);
4052 for (; depth
< curr
->lockdep_depth
; depth
++) {
4053 struct held_lock
*prev
= curr
->held_locks
+ depth
;
4054 short prev_inner
= hlock_class(prev
)->wait_type_inner
;
4058 * We can have a bigger inner than a previous one
4059 * when outer is smaller than inner, as with RCU.
4061 * Also due to trylocks.
4063 curr_inner
= min(curr_inner
, prev_inner
);
4067 if (next_outer
> curr_inner
)
4068 return print_lock_invalid_wait_context(curr
, next
);
4073 #else /* CONFIG_PROVE_LOCKING */
4076 mark_usage(struct task_struct
*curr
, struct held_lock
*hlock
, int check
)
4081 static inline unsigned int task_irq_context(struct task_struct
*task
)
4086 static inline int separate_irq_context(struct task_struct
*curr
,
4087 struct held_lock
*hlock
)
4092 static inline int check_wait_context(struct task_struct
*curr
,
4093 struct held_lock
*next
)
4098 #endif /* CONFIG_PROVE_LOCKING */
4101 * Initialize a lock instance's lock-class mapping info:
4103 void lockdep_init_map_waits(struct lockdep_map
*lock
, const char *name
,
4104 struct lock_class_key
*key
, int subclass
,
4105 short inner
, short outer
)
4109 for (i
= 0; i
< NR_LOCKDEP_CACHING_CLASSES
; i
++)
4110 lock
->class_cache
[i
] = NULL
;
4112 #ifdef CONFIG_LOCK_STAT
4113 lock
->cpu
= raw_smp_processor_id();
4117 * Can't be having no nameless bastards around this place!
4119 if (DEBUG_LOCKS_WARN_ON(!name
)) {
4120 lock
->name
= "NULL";
4126 lock
->wait_type_outer
= outer
;
4127 lock
->wait_type_inner
= inner
;
4130 * No key, no joy, we need to hash something.
4132 if (DEBUG_LOCKS_WARN_ON(!key
))
4135 * Sanity check, the lock-class key must either have been allocated
4136 * statically or must have been registered as a dynamic key.
4138 if (!static_obj(key
) && !is_dynamic_key(key
)) {
4140 printk(KERN_ERR
"BUG: key %px has not been registered!\n", key
);
4141 DEBUG_LOCKS_WARN_ON(1);
4146 if (unlikely(!debug_locks
))
4150 unsigned long flags
;
4152 if (DEBUG_LOCKS_WARN_ON(current
->lockdep_recursion
))
4155 raw_local_irq_save(flags
);
4156 current
->lockdep_recursion
++;
4157 register_lock_class(lock
, subclass
, 1);
4158 lockdep_recursion_finish();
4159 raw_local_irq_restore(flags
);
4162 EXPORT_SYMBOL_GPL(lockdep_init_map_waits
);
4164 struct lock_class_key __lockdep_no_validate__
;
4165 EXPORT_SYMBOL_GPL(__lockdep_no_validate__
);
4168 print_lock_nested_lock_not_held(struct task_struct
*curr
,
4169 struct held_lock
*hlock
,
4172 if (!debug_locks_off())
4174 if (debug_locks_silent
)
4178 pr_warn("==================================\n");
4179 pr_warn("WARNING: Nested lock was not taken\n");
4180 print_kernel_ident();
4181 pr_warn("----------------------------------\n");
4183 pr_warn("%s/%d is trying to lock:\n", curr
->comm
, task_pid_nr(curr
));
4186 pr_warn("\nbut this task is not holding:\n");
4187 pr_warn("%s\n", hlock
->nest_lock
->name
);
4189 pr_warn("\nstack backtrace:\n");
4192 pr_warn("\nother info that might help us debug this:\n");
4193 lockdep_print_held_locks(curr
);
4195 pr_warn("\nstack backtrace:\n");
4199 static int __lock_is_held(const struct lockdep_map
*lock
, int read
);
4202 * This gets called for every mutex_lock*()/spin_lock*() operation.
4203 * We maintain the dependency maps and validate the locking attempt:
4205 * The callers must make sure that IRQs are disabled before calling it,
4206 * otherwise we could get an interrupt which would want to take locks,
4207 * which would end up in lockdep again.
4209 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
4210 int trylock
, int read
, int check
, int hardirqs_off
,
4211 struct lockdep_map
*nest_lock
, unsigned long ip
,
4212 int references
, int pin_count
)
4214 struct task_struct
*curr
= current
;
4215 struct lock_class
*class = NULL
;
4216 struct held_lock
*hlock
;
4222 if (unlikely(!debug_locks
))
4225 if (!prove_locking
|| lock
->key
== &__lockdep_no_validate__
)
4228 if (subclass
< NR_LOCKDEP_CACHING_CLASSES
)
4229 class = lock
->class_cache
[subclass
];
4233 if (unlikely(!class)) {
4234 class = register_lock_class(lock
, subclass
, 0);
4239 debug_class_ops_inc(class);
4241 if (very_verbose(class)) {
4242 printk("\nacquire class [%px] %s", class->key
, class->name
);
4243 if (class->name_version
> 1)
4244 printk(KERN_CONT
"#%d", class->name_version
);
4245 printk(KERN_CONT
"\n");
4250 * Add the lock to the list of currently held locks.
4251 * (we dont increase the depth just yet, up until the
4252 * dependency checks are done)
4254 depth
= curr
->lockdep_depth
;
4256 * Ran out of static storage for our per-task lock stack again have we?
4258 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
4261 class_idx
= class - lock_classes
;
4263 if (depth
) { /* we're holding locks */
4264 hlock
= curr
->held_locks
+ depth
- 1;
4265 if (hlock
->class_idx
== class_idx
&& nest_lock
) {
4269 if (!hlock
->references
)
4270 hlock
->references
++;
4272 hlock
->references
+= references
;
4275 if (DEBUG_LOCKS_WARN_ON(hlock
->references
< references
))
4282 hlock
= curr
->held_locks
+ depth
;
4284 * Plain impossible, we just registered it and checked it weren't no
4285 * NULL like.. I bet this mushroom I ate was good!
4287 if (DEBUG_LOCKS_WARN_ON(!class))
4289 hlock
->class_idx
= class_idx
;
4290 hlock
->acquire_ip
= ip
;
4291 hlock
->instance
= lock
;
4292 hlock
->nest_lock
= nest_lock
;
4293 hlock
->irq_context
= task_irq_context(curr
);
4294 hlock
->trylock
= trylock
;
4296 hlock
->check
= check
;
4297 hlock
->hardirqs_off
= !!hardirqs_off
;
4298 hlock
->references
= references
;
4299 #ifdef CONFIG_LOCK_STAT
4300 hlock
->waittime_stamp
= 0;
4301 hlock
->holdtime_stamp
= lockstat_clock();
4303 hlock
->pin_count
= pin_count
;
4305 if (check_wait_context(curr
, hlock
))
4308 /* Initialize the lock usage bit */
4309 if (!mark_usage(curr
, hlock
, check
))
4313 * Calculate the chain hash: it's the combined hash of all the
4314 * lock keys along the dependency chain. We save the hash value
4315 * at every step so that we can get the current hash easily
4316 * after unlock. The chain hash is then used to cache dependency
4319 * The 'key ID' is what is the most compact key value to drive
4320 * the hash, not class->key.
4323 * Whoops, we did it again.. class_idx is invalid.
4325 if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx
, lock_classes_in_use
)))
4328 chain_key
= curr
->curr_chain_key
;
4331 * How can we have a chain hash when we ain't got no keys?!
4333 if (DEBUG_LOCKS_WARN_ON(chain_key
!= INITIAL_CHAIN_KEY
))
4338 hlock
->prev_chain_key
= chain_key
;
4339 if (separate_irq_context(curr
, hlock
)) {
4340 chain_key
= INITIAL_CHAIN_KEY
;
4343 chain_key
= iterate_chain_key(chain_key
, class_idx
);
4345 if (nest_lock
&& !__lock_is_held(nest_lock
, -1)) {
4346 print_lock_nested_lock_not_held(curr
, hlock
, ip
);
4350 if (!debug_locks_silent
) {
4351 WARN_ON_ONCE(depth
&& !hlock_class(hlock
- 1)->key
);
4352 WARN_ON_ONCE(!hlock_class(hlock
)->key
);
4355 if (!validate_chain(curr
, hlock
, chain_head
, chain_key
))
4358 curr
->curr_chain_key
= chain_key
;
4359 curr
->lockdep_depth
++;
4360 check_chain_key(curr
);
4361 #ifdef CONFIG_DEBUG_LOCKDEP
4362 if (unlikely(!debug_locks
))
4365 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
4367 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
4368 printk(KERN_DEBUG
"depth: %i max: %lu!\n",
4369 curr
->lockdep_depth
, MAX_LOCK_DEPTH
);
4371 lockdep_print_held_locks(current
);
4372 debug_show_all_locks();
4378 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
4379 max_lockdep_depth
= curr
->lockdep_depth
;
4384 static void print_unlock_imbalance_bug(struct task_struct
*curr
,
4385 struct lockdep_map
*lock
,
4388 if (!debug_locks_off())
4390 if (debug_locks_silent
)
4394 pr_warn("=====================================\n");
4395 pr_warn("WARNING: bad unlock balance detected!\n");
4396 print_kernel_ident();
4397 pr_warn("-------------------------------------\n");
4398 pr_warn("%s/%d is trying to release lock (",
4399 curr
->comm
, task_pid_nr(curr
));
4400 print_lockdep_cache(lock
);
4403 pr_warn("but there are no more locks to release!\n");
4404 pr_warn("\nother info that might help us debug this:\n");
4405 lockdep_print_held_locks(curr
);
4407 pr_warn("\nstack backtrace:\n");
4411 static int match_held_lock(const struct held_lock
*hlock
,
4412 const struct lockdep_map
*lock
)
4414 if (hlock
->instance
== lock
)
4417 if (hlock
->references
) {
4418 const struct lock_class
*class = lock
->class_cache
[0];
4421 class = look_up_lock_class(lock
, 0);
4424 * If look_up_lock_class() failed to find a class, we're trying
4425 * to test if we hold a lock that has never yet been acquired.
4426 * Clearly if the lock hasn't been acquired _ever_, we're not
4427 * holding it either, so report failure.
4433 * References, but not a lock we're actually ref-counting?
4434 * State got messed up, follow the sites that change ->references
4435 * and try to make sense of it.
4437 if (DEBUG_LOCKS_WARN_ON(!hlock
->nest_lock
))
4440 if (hlock
->class_idx
== class - lock_classes
)
4447 /* @depth must not be zero */
4448 static struct held_lock
*find_held_lock(struct task_struct
*curr
,
4449 struct lockdep_map
*lock
,
4450 unsigned int depth
, int *idx
)
4452 struct held_lock
*ret
, *hlock
, *prev_hlock
;
4456 hlock
= curr
->held_locks
+ i
;
4458 if (match_held_lock(hlock
, lock
))
4462 for (i
--, prev_hlock
= hlock
--;
4464 i
--, prev_hlock
= hlock
--) {
4466 * We must not cross into another context:
4468 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
4472 if (match_held_lock(hlock
, lock
)) {
4483 static int reacquire_held_locks(struct task_struct
*curr
, unsigned int depth
,
4484 int idx
, unsigned int *merged
)
4486 struct held_lock
*hlock
;
4487 int first_idx
= idx
;
4489 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4492 for (hlock
= curr
->held_locks
+ idx
; idx
< depth
; idx
++, hlock
++) {
4493 switch (__lock_acquire(hlock
->instance
,
4494 hlock_class(hlock
)->subclass
,
4496 hlock
->read
, hlock
->check
,
4497 hlock
->hardirqs_off
,
4498 hlock
->nest_lock
, hlock
->acquire_ip
,
4499 hlock
->references
, hlock
->pin_count
)) {
4505 *merged
+= (idx
== first_idx
);
4516 __lock_set_class(struct lockdep_map
*lock
, const char *name
,
4517 struct lock_class_key
*key
, unsigned int subclass
,
4520 struct task_struct
*curr
= current
;
4521 unsigned int depth
, merged
= 0;
4522 struct held_lock
*hlock
;
4523 struct lock_class
*class;
4526 if (unlikely(!debug_locks
))
4529 depth
= curr
->lockdep_depth
;
4531 * This function is about (re)setting the class of a held lock,
4532 * yet we're not actually holding any locks. Naughty user!
4534 if (DEBUG_LOCKS_WARN_ON(!depth
))
4537 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4539 print_unlock_imbalance_bug(curr
, lock
, ip
);
4543 lockdep_init_map_waits(lock
, name
, key
, 0,
4544 lock
->wait_type_inner
,
4545 lock
->wait_type_outer
);
4546 class = register_lock_class(lock
, subclass
, 0);
4547 hlock
->class_idx
= class - lock_classes
;
4549 curr
->lockdep_depth
= i
;
4550 curr
->curr_chain_key
= hlock
->prev_chain_key
;
4552 if (reacquire_held_locks(curr
, depth
, i
, &merged
))
4556 * I took it apart and put it back together again, except now I have
4557 * these 'spare' parts.. where shall I put them.
4559 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- merged
))
4564 static int __lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
4566 struct task_struct
*curr
= current
;
4567 unsigned int depth
, merged
= 0;
4568 struct held_lock
*hlock
;
4571 if (unlikely(!debug_locks
))
4574 depth
= curr
->lockdep_depth
;
4576 * This function is about (re)setting the class of a held lock,
4577 * yet we're not actually holding any locks. Naughty user!
4579 if (DEBUG_LOCKS_WARN_ON(!depth
))
4582 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4584 print_unlock_imbalance_bug(curr
, lock
, ip
);
4588 curr
->lockdep_depth
= i
;
4589 curr
->curr_chain_key
= hlock
->prev_chain_key
;
4591 WARN(hlock
->read
, "downgrading a read lock");
4593 hlock
->acquire_ip
= ip
;
4595 if (reacquire_held_locks(curr
, depth
, i
, &merged
))
4598 /* Merging can't happen with unchanged classes.. */
4599 if (DEBUG_LOCKS_WARN_ON(merged
))
4603 * I took it apart and put it back together again, except now I have
4604 * these 'spare' parts.. where shall I put them.
4606 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
))
4613 * Remove the lock from the list of currently held locks - this gets
4614 * called on mutex_unlock()/spin_unlock*() (or on a failed
4615 * mutex_lock_interruptible()).
4618 __lock_release(struct lockdep_map
*lock
, unsigned long ip
)
4620 struct task_struct
*curr
= current
;
4621 unsigned int depth
, merged
= 1;
4622 struct held_lock
*hlock
;
4625 if (unlikely(!debug_locks
))
4628 depth
= curr
->lockdep_depth
;
4630 * So we're all set to release this lock.. wait what lock? We don't
4631 * own any locks, you've been drinking again?
4634 print_unlock_imbalance_bug(curr
, lock
, ip
);
4639 * Check whether the lock exists in the current stack
4642 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
4644 print_unlock_imbalance_bug(curr
, lock
, ip
);
4648 if (hlock
->instance
== lock
)
4649 lock_release_holdtime(hlock
);
4651 WARN(hlock
->pin_count
, "releasing a pinned lock\n");
4653 if (hlock
->references
) {
4654 hlock
->references
--;
4655 if (hlock
->references
) {
4657 * We had, and after removing one, still have
4658 * references, the current lock stack is still
4659 * valid. We're done!
4666 * We have the right lock to unlock, 'hlock' points to it.
4667 * Now we remove it from the stack, and add back the other
4668 * entries (if any), recalculating the hash along the way:
4671 curr
->lockdep_depth
= i
;
4672 curr
->curr_chain_key
= hlock
->prev_chain_key
;
4675 * The most likely case is when the unlock is on the innermost
4676 * lock. In this case, we are done!
4681 if (reacquire_held_locks(curr
, depth
, i
+ 1, &merged
))
4685 * We had N bottles of beer on the wall, we drank one, but now
4686 * there's not N-1 bottles of beer left on the wall...
4687 * Pouring two of the bottles together is acceptable.
4689 DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- merged
);
4692 * Since reacquire_held_locks() would have called check_chain_key()
4693 * indirectly via __lock_acquire(), we don't need to do it again
4699 static nokprobe_inline
4700 int __lock_is_held(const struct lockdep_map
*lock
, int read
)
4702 struct task_struct
*curr
= current
;
4705 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4706 struct held_lock
*hlock
= curr
->held_locks
+ i
;
4708 if (match_held_lock(hlock
, lock
)) {
4709 if (read
== -1 || hlock
->read
== read
)
4719 static struct pin_cookie
__lock_pin_lock(struct lockdep_map
*lock
)
4721 struct pin_cookie cookie
= NIL_COOKIE
;
4722 struct task_struct
*curr
= current
;
4725 if (unlikely(!debug_locks
))
4728 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4729 struct held_lock
*hlock
= curr
->held_locks
+ i
;
4731 if (match_held_lock(hlock
, lock
)) {
4733 * Grab 16bits of randomness; this is sufficient to not
4734 * be guessable and still allows some pin nesting in
4735 * our u32 pin_count.
4737 cookie
.val
= 1 + (prandom_u32() >> 16);
4738 hlock
->pin_count
+= cookie
.val
;
4743 WARN(1, "pinning an unheld lock\n");
4747 static void __lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
4749 struct task_struct
*curr
= current
;
4752 if (unlikely(!debug_locks
))
4755 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4756 struct held_lock
*hlock
= curr
->held_locks
+ i
;
4758 if (match_held_lock(hlock
, lock
)) {
4759 hlock
->pin_count
+= cookie
.val
;
4764 WARN(1, "pinning an unheld lock\n");
4767 static void __lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
4769 struct task_struct
*curr
= current
;
4772 if (unlikely(!debug_locks
))
4775 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
4776 struct held_lock
*hlock
= curr
->held_locks
+ i
;
4778 if (match_held_lock(hlock
, lock
)) {
4779 if (WARN(!hlock
->pin_count
, "unpinning an unpinned lock\n"))
4782 hlock
->pin_count
-= cookie
.val
;
4784 if (WARN((int)hlock
->pin_count
< 0, "pin count corrupted\n"))
4785 hlock
->pin_count
= 0;
4791 WARN(1, "unpinning an unheld lock\n");
4795 * Check whether we follow the irq-flags state precisely:
4797 static void check_flags(unsigned long flags
)
4799 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
4803 if (irqs_disabled_flags(flags
)) {
4804 if (DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
)) {
4805 printk("possible reason: unannotated irqs-off.\n");
4808 if (DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
)) {
4809 printk("possible reason: unannotated irqs-on.\n");
4814 * We dont accurately track softirq state in e.g.
4815 * hardirq contexts (such as on 4KSTACKS), so only
4816 * check if not in hardirq contexts:
4818 if (!hardirq_count()) {
4819 if (softirq_count()) {
4820 /* like the above, but with softirqs */
4821 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
4823 /* lick the above, does it taste good? */
4824 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
4829 print_irqtrace_events(current
);
4833 void lock_set_class(struct lockdep_map
*lock
, const char *name
,
4834 struct lock_class_key
*key
, unsigned int subclass
,
4837 unsigned long flags
;
4839 if (unlikely(current
->lockdep_recursion
))
4842 raw_local_irq_save(flags
);
4843 current
->lockdep_recursion
++;
4845 if (__lock_set_class(lock
, name
, key
, subclass
, ip
))
4846 check_chain_key(current
);
4847 lockdep_recursion_finish();
4848 raw_local_irq_restore(flags
);
4850 EXPORT_SYMBOL_GPL(lock_set_class
);
4852 void lock_downgrade(struct lockdep_map
*lock
, unsigned long ip
)
4854 unsigned long flags
;
4856 if (unlikely(current
->lockdep_recursion
))
4859 raw_local_irq_save(flags
);
4860 current
->lockdep_recursion
++;
4862 if (__lock_downgrade(lock
, ip
))
4863 check_chain_key(current
);
4864 lockdep_recursion_finish();
4865 raw_local_irq_restore(flags
);
4867 EXPORT_SYMBOL_GPL(lock_downgrade
);
4869 /* NMI context !!! */
4870 static void verify_lock_unused(struct lockdep_map
*lock
, struct held_lock
*hlock
, int subclass
)
4872 #ifdef CONFIG_PROVE_LOCKING
4873 struct lock_class
*class = look_up_lock_class(lock
, subclass
);
4875 /* if it doesn't have a class (yet), it certainly hasn't been used yet */
4879 if (!(class->usage_mask
& LOCK_USED
))
4882 hlock
->class_idx
= class - lock_classes
;
4884 print_usage_bug(current
, hlock
, LOCK_USED
, LOCK_USAGE_STATES
);
4888 static bool lockdep_nmi(void)
4890 if (current
->lockdep_recursion
& LOCKDEP_RECURSION_MASK
)
4900 * We are not always called with irqs disabled - do that here,
4901 * and also avoid lockdep recursion:
4903 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
4904 int trylock
, int read
, int check
,
4905 struct lockdep_map
*nest_lock
, unsigned long ip
)
4907 unsigned long flags
;
4909 if (unlikely(current
->lockdep_recursion
)) {
4910 /* XXX allow trylock from NMI ?!? */
4911 if (lockdep_nmi() && !trylock
) {
4912 struct held_lock hlock
;
4914 hlock
.acquire_ip
= ip
;
4915 hlock
.instance
= lock
;
4916 hlock
.nest_lock
= nest_lock
;
4917 hlock
.irq_context
= 2; // XXX
4918 hlock
.trylock
= trylock
;
4920 hlock
.check
= check
;
4921 hlock
.hardirqs_off
= true;
4922 hlock
.references
= 0;
4924 verify_lock_unused(lock
, &hlock
, subclass
);
4929 raw_local_irq_save(flags
);
4932 current
->lockdep_recursion
++;
4933 trace_lock_acquire(lock
, subclass
, trylock
, read
, check
, nest_lock
, ip
);
4934 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
4935 irqs_disabled_flags(flags
), nest_lock
, ip
, 0, 0);
4936 lockdep_recursion_finish();
4937 raw_local_irq_restore(flags
);
4939 EXPORT_SYMBOL_GPL(lock_acquire
);
4941 void lock_release(struct lockdep_map
*lock
, unsigned long ip
)
4943 unsigned long flags
;
4945 if (unlikely(current
->lockdep_recursion
))
4948 raw_local_irq_save(flags
);
4950 current
->lockdep_recursion
++;
4951 trace_lock_release(lock
, ip
);
4952 if (__lock_release(lock
, ip
))
4953 check_chain_key(current
);
4954 lockdep_recursion_finish();
4955 raw_local_irq_restore(flags
);
4957 EXPORT_SYMBOL_GPL(lock_release
);
4959 int lock_is_held_type(const struct lockdep_map
*lock
, int read
)
4961 unsigned long flags
;
4964 if (unlikely(current
->lockdep_recursion
))
4965 return 1; /* avoid false negative lockdep_assert_held() */
4967 raw_local_irq_save(flags
);
4970 current
->lockdep_recursion
++;
4971 ret
= __lock_is_held(lock
, read
);
4972 lockdep_recursion_finish();
4973 raw_local_irq_restore(flags
);
4977 EXPORT_SYMBOL_GPL(lock_is_held_type
);
4978 NOKPROBE_SYMBOL(lock_is_held_type
);
4980 struct pin_cookie
lock_pin_lock(struct lockdep_map
*lock
)
4982 struct pin_cookie cookie
= NIL_COOKIE
;
4983 unsigned long flags
;
4985 if (unlikely(current
->lockdep_recursion
))
4988 raw_local_irq_save(flags
);
4991 current
->lockdep_recursion
++;
4992 cookie
= __lock_pin_lock(lock
);
4993 lockdep_recursion_finish();
4994 raw_local_irq_restore(flags
);
4998 EXPORT_SYMBOL_GPL(lock_pin_lock
);
5000 void lock_repin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
5002 unsigned long flags
;
5004 if (unlikely(current
->lockdep_recursion
))
5007 raw_local_irq_save(flags
);
5010 current
->lockdep_recursion
++;
5011 __lock_repin_lock(lock
, cookie
);
5012 lockdep_recursion_finish();
5013 raw_local_irq_restore(flags
);
5015 EXPORT_SYMBOL_GPL(lock_repin_lock
);
5017 void lock_unpin_lock(struct lockdep_map
*lock
, struct pin_cookie cookie
)
5019 unsigned long flags
;
5021 if (unlikely(current
->lockdep_recursion
))
5024 raw_local_irq_save(flags
);
5027 current
->lockdep_recursion
++;
5028 __lock_unpin_lock(lock
, cookie
);
5029 lockdep_recursion_finish();
5030 raw_local_irq_restore(flags
);
5032 EXPORT_SYMBOL_GPL(lock_unpin_lock
);
5034 #ifdef CONFIG_LOCK_STAT
5035 static void print_lock_contention_bug(struct task_struct
*curr
,
5036 struct lockdep_map
*lock
,
5039 if (!debug_locks_off())
5041 if (debug_locks_silent
)
5045 pr_warn("=================================\n");
5046 pr_warn("WARNING: bad contention detected!\n");
5047 print_kernel_ident();
5048 pr_warn("---------------------------------\n");
5049 pr_warn("%s/%d is trying to contend lock (",
5050 curr
->comm
, task_pid_nr(curr
));
5051 print_lockdep_cache(lock
);
5054 pr_warn("but there are no locks held!\n");
5055 pr_warn("\nother info that might help us debug this:\n");
5056 lockdep_print_held_locks(curr
);
5058 pr_warn("\nstack backtrace:\n");
5063 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
5065 struct task_struct
*curr
= current
;
5066 struct held_lock
*hlock
;
5067 struct lock_class_stats
*stats
;
5069 int i
, contention_point
, contending_point
;
5071 depth
= curr
->lockdep_depth
;
5073 * Whee, we contended on this lock, except it seems we're not
5074 * actually trying to acquire anything much at all..
5076 if (DEBUG_LOCKS_WARN_ON(!depth
))
5079 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
5081 print_lock_contention_bug(curr
, lock
, ip
);
5085 if (hlock
->instance
!= lock
)
5088 hlock
->waittime_stamp
= lockstat_clock();
5090 contention_point
= lock_point(hlock_class(hlock
)->contention_point
, ip
);
5091 contending_point
= lock_point(hlock_class(hlock
)->contending_point
,
5094 stats
= get_lock_stats(hlock_class(hlock
));
5095 if (contention_point
< LOCKSTAT_POINTS
)
5096 stats
->contention_point
[contention_point
]++;
5097 if (contending_point
< LOCKSTAT_POINTS
)
5098 stats
->contending_point
[contending_point
]++;
5099 if (lock
->cpu
!= smp_processor_id())
5100 stats
->bounces
[bounce_contended
+ !!hlock
->read
]++;
5104 __lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
5106 struct task_struct
*curr
= current
;
5107 struct held_lock
*hlock
;
5108 struct lock_class_stats
*stats
;
5110 u64 now
, waittime
= 0;
5113 depth
= curr
->lockdep_depth
;
5115 * Yay, we acquired ownership of this lock we didn't try to
5116 * acquire, how the heck did that happen?
5118 if (DEBUG_LOCKS_WARN_ON(!depth
))
5121 hlock
= find_held_lock(curr
, lock
, depth
, &i
);
5123 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
5127 if (hlock
->instance
!= lock
)
5130 cpu
= smp_processor_id();
5131 if (hlock
->waittime_stamp
) {
5132 now
= lockstat_clock();
5133 waittime
= now
- hlock
->waittime_stamp
;
5134 hlock
->holdtime_stamp
= now
;
5137 trace_lock_acquired(lock
, ip
);
5139 stats
= get_lock_stats(hlock_class(hlock
));
5142 lock_time_inc(&stats
->read_waittime
, waittime
);
5144 lock_time_inc(&stats
->write_waittime
, waittime
);
5146 if (lock
->cpu
!= cpu
)
5147 stats
->bounces
[bounce_acquired
+ !!hlock
->read
]++;
5153 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
5155 unsigned long flags
;
5157 if (unlikely(!lock_stat
|| !debug_locks
))
5160 if (unlikely(current
->lockdep_recursion
))
5163 raw_local_irq_save(flags
);
5165 current
->lockdep_recursion
++;
5166 trace_lock_contended(lock
, ip
);
5167 __lock_contended(lock
, ip
);
5168 lockdep_recursion_finish();
5169 raw_local_irq_restore(flags
);
5171 EXPORT_SYMBOL_GPL(lock_contended
);
5173 void lock_acquired(struct lockdep_map
*lock
, unsigned long ip
)
5175 unsigned long flags
;
5177 if (unlikely(!lock_stat
|| !debug_locks
))
5180 if (unlikely(current
->lockdep_recursion
))
5183 raw_local_irq_save(flags
);
5185 current
->lockdep_recursion
++;
5186 __lock_acquired(lock
, ip
);
5187 lockdep_recursion_finish();
5188 raw_local_irq_restore(flags
);
5190 EXPORT_SYMBOL_GPL(lock_acquired
);
5194 * Used by the testsuite, sanitize the validator state
5195 * after a simulated failure:
5198 void lockdep_reset(void)
5200 unsigned long flags
;
5203 raw_local_irq_save(flags
);
5204 lockdep_init_task(current
);
5205 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
5206 nr_hardirq_chains
= 0;
5207 nr_softirq_chains
= 0;
5208 nr_process_chains
= 0;
5210 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
5211 INIT_HLIST_HEAD(chainhash_table
+ i
);
5212 raw_local_irq_restore(flags
);
5215 /* Remove a class from a lock chain. Must be called with the graph lock held. */
5216 static void remove_class_from_lock_chain(struct pending_free
*pf
,
5217 struct lock_chain
*chain
,
5218 struct lock_class
*class)
5220 #ifdef CONFIG_PROVE_LOCKING
5223 for (i
= chain
->base
; i
< chain
->base
+ chain
->depth
; i
++) {
5224 if (chain_hlocks
[i
] != class - lock_classes
)
5227 * Each lock class occurs at most once in a lock chain so once
5228 * we found a match we can break out of this loop.
5230 goto free_lock_chain
;
5232 /* Since the chain has not been modified, return. */
5236 free_chain_hlocks(chain
->base
, chain
->depth
);
5237 /* Overwrite the chain key for concurrent RCU readers. */
5238 WRITE_ONCE(chain
->chain_key
, INITIAL_CHAIN_KEY
);
5239 dec_chains(chain
->irq_context
);
5242 * Note: calling hlist_del_rcu() from inside a
5243 * hlist_for_each_entry_rcu() loop is safe.
5245 hlist_del_rcu(&chain
->entry
);
5246 __set_bit(chain
- lock_chains
, pf
->lock_chains_being_freed
);
5247 nr_zapped_lock_chains
++;
5251 /* Must be called with the graph lock held. */
5252 static void remove_class_from_lock_chains(struct pending_free
*pf
,
5253 struct lock_class
*class)
5255 struct lock_chain
*chain
;
5256 struct hlist_head
*head
;
5259 for (i
= 0; i
< ARRAY_SIZE(chainhash_table
); i
++) {
5260 head
= chainhash_table
+ i
;
5261 hlist_for_each_entry_rcu(chain
, head
, entry
) {
5262 remove_class_from_lock_chain(pf
, chain
, class);
5268 * Remove all references to a lock class. The caller must hold the graph lock.
5270 static void zap_class(struct pending_free
*pf
, struct lock_class
*class)
5272 struct lock_list
*entry
;
5275 WARN_ON_ONCE(!class->key
);
5278 * Remove all dependencies this lock is
5281 for_each_set_bit(i
, list_entries_in_use
, ARRAY_SIZE(list_entries
)) {
5282 entry
= list_entries
+ i
;
5283 if (entry
->class != class && entry
->links_to
!= class)
5285 __clear_bit(i
, list_entries_in_use
);
5287 list_del_rcu(&entry
->entry
);
5289 if (list_empty(&class->locks_after
) &&
5290 list_empty(&class->locks_before
)) {
5291 list_move_tail(&class->lock_entry
, &pf
->zapped
);
5292 hlist_del_rcu(&class->hash_entry
);
5293 WRITE_ONCE(class->key
, NULL
);
5294 WRITE_ONCE(class->name
, NULL
);
5296 __clear_bit(class - lock_classes
, lock_classes_in_use
);
5298 WARN_ONCE(true, "%s() failed for class %s\n", __func__
,
5302 remove_class_from_lock_chains(pf
, class);
5303 nr_zapped_classes
++;
5306 static void reinit_class(struct lock_class
*class)
5308 void *const p
= class;
5309 const unsigned int offset
= offsetof(struct lock_class
, key
);
5311 WARN_ON_ONCE(!class->lock_entry
.next
);
5312 WARN_ON_ONCE(!list_empty(&class->locks_after
));
5313 WARN_ON_ONCE(!list_empty(&class->locks_before
));
5314 memset(p
+ offset
, 0, sizeof(*class) - offset
);
5315 WARN_ON_ONCE(!class->lock_entry
.next
);
5316 WARN_ON_ONCE(!list_empty(&class->locks_after
));
5317 WARN_ON_ONCE(!list_empty(&class->locks_before
));
5320 static inline int within(const void *addr
, void *start
, unsigned long size
)
5322 return addr
>= start
&& addr
< start
+ size
;
5325 static bool inside_selftest(void)
5327 return current
== lockdep_selftest_task_struct
;
5330 /* The caller must hold the graph lock. */
5331 static struct pending_free
*get_pending_free(void)
5333 return delayed_free
.pf
+ delayed_free
.index
;
5336 static void free_zapped_rcu(struct rcu_head
*cb
);
5339 * Schedule an RCU callback if no RCU callback is pending. Must be called with
5340 * the graph lock held.
5342 static void call_rcu_zapped(struct pending_free
*pf
)
5344 WARN_ON_ONCE(inside_selftest());
5346 if (list_empty(&pf
->zapped
))
5349 if (delayed_free
.scheduled
)
5352 delayed_free
.scheduled
= true;
5354 WARN_ON_ONCE(delayed_free
.pf
+ delayed_free
.index
!= pf
);
5355 delayed_free
.index
^= 1;
5357 call_rcu(&delayed_free
.rcu_head
, free_zapped_rcu
);
5360 /* The caller must hold the graph lock. May be called from RCU context. */
5361 static void __free_zapped_classes(struct pending_free
*pf
)
5363 struct lock_class
*class;
5365 check_data_structures();
5367 list_for_each_entry(class, &pf
->zapped
, lock_entry
)
5368 reinit_class(class);
5370 list_splice_init(&pf
->zapped
, &free_lock_classes
);
5372 #ifdef CONFIG_PROVE_LOCKING
5373 bitmap_andnot(lock_chains_in_use
, lock_chains_in_use
,
5374 pf
->lock_chains_being_freed
, ARRAY_SIZE(lock_chains
));
5375 bitmap_clear(pf
->lock_chains_being_freed
, 0, ARRAY_SIZE(lock_chains
));
5379 static void free_zapped_rcu(struct rcu_head
*ch
)
5381 struct pending_free
*pf
;
5382 unsigned long flags
;
5384 if (WARN_ON_ONCE(ch
!= &delayed_free
.rcu_head
))
5387 raw_local_irq_save(flags
);
5391 pf
= delayed_free
.pf
+ (delayed_free
.index
^ 1);
5392 __free_zapped_classes(pf
);
5393 delayed_free
.scheduled
= false;
5396 * If there's anything on the open list, close and start a new callback.
5398 call_rcu_zapped(delayed_free
.pf
+ delayed_free
.index
);
5401 raw_local_irq_restore(flags
);
5405 * Remove all lock classes from the class hash table and from the
5406 * all_lock_classes list whose key or name is in the address range [start,
5407 * start + size). Move these lock classes to the zapped_classes list. Must
5408 * be called with the graph lock held.
5410 static void __lockdep_free_key_range(struct pending_free
*pf
, void *start
,
5413 struct lock_class
*class;
5414 struct hlist_head
*head
;
5417 /* Unhash all classes that were created by a module. */
5418 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
5419 head
= classhash_table
+ i
;
5420 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
5421 if (!within(class->key
, start
, size
) &&
5422 !within(class->name
, start
, size
))
5424 zap_class(pf
, class);
5430 * Used in module.c to remove lock classes from memory that is going to be
5431 * freed; and possibly re-used by other modules.
5433 * We will have had one synchronize_rcu() before getting here, so we're
5434 * guaranteed nobody will look up these exact classes -- they're properly dead
5435 * but still allocated.
5437 static void lockdep_free_key_range_reg(void *start
, unsigned long size
)
5439 struct pending_free
*pf
;
5440 unsigned long flags
;
5442 init_data_structures_once();
5444 raw_local_irq_save(flags
);
5446 pf
= get_pending_free();
5447 __lockdep_free_key_range(pf
, start
, size
);
5448 call_rcu_zapped(pf
);
5450 raw_local_irq_restore(flags
);
5453 * Wait for any possible iterators from look_up_lock_class() to pass
5454 * before continuing to free the memory they refer to.
5460 * Free all lockdep keys in the range [start, start+size). Does not sleep.
5461 * Ignores debug_locks. Must only be used by the lockdep selftests.
5463 static void lockdep_free_key_range_imm(void *start
, unsigned long size
)
5465 struct pending_free
*pf
= delayed_free
.pf
;
5466 unsigned long flags
;
5468 init_data_structures_once();
5470 raw_local_irq_save(flags
);
5472 __lockdep_free_key_range(pf
, start
, size
);
5473 __free_zapped_classes(pf
);
5475 raw_local_irq_restore(flags
);
5478 void lockdep_free_key_range(void *start
, unsigned long size
)
5480 init_data_structures_once();
5482 if (inside_selftest())
5483 lockdep_free_key_range_imm(start
, size
);
5485 lockdep_free_key_range_reg(start
, size
);
5489 * Check whether any element of the @lock->class_cache[] array refers to a
5490 * registered lock class. The caller must hold either the graph lock or the
5493 static bool lock_class_cache_is_registered(struct lockdep_map
*lock
)
5495 struct lock_class
*class;
5496 struct hlist_head
*head
;
5499 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
5500 head
= classhash_table
+ i
;
5501 hlist_for_each_entry_rcu(class, head
, hash_entry
) {
5502 for (j
= 0; j
< NR_LOCKDEP_CACHING_CLASSES
; j
++)
5503 if (lock
->class_cache
[j
] == class)
5510 /* The caller must hold the graph lock. Does not sleep. */
5511 static void __lockdep_reset_lock(struct pending_free
*pf
,
5512 struct lockdep_map
*lock
)
5514 struct lock_class
*class;
5518 * Remove all classes this lock might have:
5520 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
5522 * If the class exists we look it up and zap it:
5524 class = look_up_lock_class(lock
, j
);
5526 zap_class(pf
, class);
5529 * Debug check: in the end all mapped classes should
5532 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock
)))
5537 * Remove all information lockdep has about a lock if debug_locks == 1. Free
5538 * released data structures from RCU context.
5540 static void lockdep_reset_lock_reg(struct lockdep_map
*lock
)
5542 struct pending_free
*pf
;
5543 unsigned long flags
;
5546 raw_local_irq_save(flags
);
5547 locked
= graph_lock();
5551 pf
= get_pending_free();
5552 __lockdep_reset_lock(pf
, lock
);
5553 call_rcu_zapped(pf
);
5557 raw_local_irq_restore(flags
);
5561 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
5562 * lockdep selftests.
5564 static void lockdep_reset_lock_imm(struct lockdep_map
*lock
)
5566 struct pending_free
*pf
= delayed_free
.pf
;
5567 unsigned long flags
;
5569 raw_local_irq_save(flags
);
5571 __lockdep_reset_lock(pf
, lock
);
5572 __free_zapped_classes(pf
);
5574 raw_local_irq_restore(flags
);
5577 void lockdep_reset_lock(struct lockdep_map
*lock
)
5579 init_data_structures_once();
5581 if (inside_selftest())
5582 lockdep_reset_lock_imm(lock
);
5584 lockdep_reset_lock_reg(lock
);
5587 /* Unregister a dynamically allocated key. */
5588 void lockdep_unregister_key(struct lock_class_key
*key
)
5590 struct hlist_head
*hash_head
= keyhashentry(key
);
5591 struct lock_class_key
*k
;
5592 struct pending_free
*pf
;
5593 unsigned long flags
;
5598 if (WARN_ON_ONCE(static_obj(key
)))
5601 raw_local_irq_save(flags
);
5605 pf
= get_pending_free();
5606 hlist_for_each_entry_rcu(k
, hash_head
, hash_entry
) {
5608 hlist_del_rcu(&k
->hash_entry
);
5613 WARN_ON_ONCE(!found
);
5614 __lockdep_free_key_range(pf
, key
, 1);
5615 call_rcu_zapped(pf
);
5618 raw_local_irq_restore(flags
);
5620 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
5623 EXPORT_SYMBOL_GPL(lockdep_unregister_key
);
5625 void __init
lockdep_init(void)
5627 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
5629 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
5630 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
5631 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
5632 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
5633 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
5634 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
5635 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
5637 printk(" memory used by lock dependency info: %zu kB\n",
5638 (sizeof(lock_classes
) +
5639 sizeof(lock_classes_in_use
) +
5640 sizeof(classhash_table
) +
5641 sizeof(list_entries
) +
5642 sizeof(list_entries_in_use
) +
5643 sizeof(chainhash_table
) +
5644 sizeof(delayed_free
)
5645 #ifdef CONFIG_PROVE_LOCKING
5647 + sizeof(lock_chains
)
5648 + sizeof(lock_chains_in_use
)
5649 + sizeof(chain_hlocks
)
5654 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
5655 printk(" memory used for stack traces: %zu kB\n",
5656 (sizeof(stack_trace
) + sizeof(stack_trace_hash
)) / 1024
5660 printk(" per task-struct memory footprint: %zu bytes\n",
5661 sizeof(((struct task_struct
*)NULL
)->held_locks
));
5665 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
5666 const void *mem_to
, struct held_lock
*hlock
)
5668 if (!debug_locks_off())
5670 if (debug_locks_silent
)
5674 pr_warn("=========================\n");
5675 pr_warn("WARNING: held lock freed!\n");
5676 print_kernel_ident();
5677 pr_warn("-------------------------\n");
5678 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
5679 curr
->comm
, task_pid_nr(curr
), mem_from
, mem_to
-1);
5681 lockdep_print_held_locks(curr
);
5683 pr_warn("\nstack backtrace:\n");
5687 static inline int not_in_range(const void* mem_from
, unsigned long mem_len
,
5688 const void* lock_from
, unsigned long lock_len
)
5690 return lock_from
+ lock_len
<= mem_from
||
5691 mem_from
+ mem_len
<= lock_from
;
5695 * Called when kernel memory is freed (or unmapped), or if a lock
5696 * is destroyed or reinitialized - this code checks whether there is
5697 * any held lock in the memory range of <from> to <to>:
5699 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
5701 struct task_struct
*curr
= current
;
5702 struct held_lock
*hlock
;
5703 unsigned long flags
;
5706 if (unlikely(!debug_locks
))
5709 raw_local_irq_save(flags
);
5710 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
5711 hlock
= curr
->held_locks
+ i
;
5713 if (not_in_range(mem_from
, mem_len
, hlock
->instance
,
5714 sizeof(*hlock
->instance
)))
5717 print_freed_lock_bug(curr
, mem_from
, mem_from
+ mem_len
, hlock
);
5720 raw_local_irq_restore(flags
);
5722 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
5724 static void print_held_locks_bug(void)
5726 if (!debug_locks_off())
5728 if (debug_locks_silent
)
5732 pr_warn("====================================\n");
5733 pr_warn("WARNING: %s/%d still has locks held!\n",
5734 current
->comm
, task_pid_nr(current
));
5735 print_kernel_ident();
5736 pr_warn("------------------------------------\n");
5737 lockdep_print_held_locks(current
);
5738 pr_warn("\nstack backtrace:\n");
5742 void debug_check_no_locks_held(void)
5744 if (unlikely(current
->lockdep_depth
> 0))
5745 print_held_locks_bug();
5747 EXPORT_SYMBOL_GPL(debug_check_no_locks_held
);
5750 void debug_show_all_locks(void)
5752 struct task_struct
*g
, *p
;
5754 if (unlikely(!debug_locks
)) {
5755 pr_warn("INFO: lockdep is turned off.\n");
5758 pr_warn("\nShowing all locks held in the system:\n");
5761 for_each_process_thread(g
, p
) {
5762 if (!p
->lockdep_depth
)
5764 lockdep_print_held_locks(p
);
5765 touch_nmi_watchdog();
5766 touch_all_softlockup_watchdogs();
5771 pr_warn("=============================================\n\n");
5773 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
5777 * Careful: only use this function if you are sure that
5778 * the task cannot run in parallel!
5780 void debug_show_held_locks(struct task_struct
*task
)
5782 if (unlikely(!debug_locks
)) {
5783 printk("INFO: lockdep is turned off.\n");
5786 lockdep_print_held_locks(task
);
5788 EXPORT_SYMBOL_GPL(debug_show_held_locks
);
5790 asmlinkage __visible
void lockdep_sys_exit(void)
5792 struct task_struct
*curr
= current
;
5794 if (unlikely(curr
->lockdep_depth
)) {
5795 if (!debug_locks_off())
5798 pr_warn("================================================\n");
5799 pr_warn("WARNING: lock held when returning to user space!\n");
5800 print_kernel_ident();
5801 pr_warn("------------------------------------------------\n");
5802 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
5803 curr
->comm
, curr
->pid
);
5804 lockdep_print_held_locks(curr
);
5808 * The lock history for each syscall should be independent. So wipe the
5809 * slate clean on return to userspace.
5811 lockdep_invariant_state(false);
5814 void lockdep_rcu_suspicious(const char *file
, const int line
, const char *s
)
5816 struct task_struct
*curr
= current
;
5818 /* Note: the following can be executed concurrently, so be careful. */
5820 pr_warn("=============================\n");
5821 pr_warn("WARNING: suspicious RCU usage\n");
5822 print_kernel_ident();
5823 pr_warn("-----------------------------\n");
5824 pr_warn("%s:%d %s!\n", file
, line
, s
);
5825 pr_warn("\nother info that might help us debug this:\n\n");
5826 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
5827 !rcu_lockdep_current_cpu_online()
5828 ? "RCU used illegally from offline CPU!\n"
5829 : !rcu_is_watching()
5830 ? "RCU used illegally from idle CPU!\n"
5832 rcu_scheduler_active
, debug_locks
);
5835 * If a CPU is in the RCU-free window in idle (ie: in the section
5836 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
5837 * considers that CPU to be in an "extended quiescent state",
5838 * which means that RCU will be completely ignoring that CPU.
5839 * Therefore, rcu_read_lock() and friends have absolutely no
5840 * effect on a CPU running in that state. In other words, even if
5841 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
5842 * delete data structures out from under it. RCU really has no
5843 * choice here: we need to keep an RCU-free window in idle where
5844 * the CPU may possibly enter into low power mode. This way we can
5845 * notice an extended quiescent state to other CPUs that started a grace
5846 * period. Otherwise we would delay any grace period as long as we run
5849 * So complain bitterly if someone does call rcu_read_lock(),
5850 * rcu_read_lock_bh() and so on from extended quiescent states.
5852 if (!rcu_is_watching())
5853 pr_warn("RCU used illegally from extended quiescent state!\n");
5855 lockdep_print_held_locks(curr
);
5856 pr_warn("\nstack backtrace:\n");
5859 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious
);