History

lockdep: Introduce wait-type checks

Extend lockdep to validate lock wait-type context. The current wait-types are: LD_WAIT_FREE, /* wait free, rcu etc.. */ LD_WAIT_SPIN, /* spin loops, raw_spinlock_t etc.. */ LD_WAIT_CONFIG, /* CONFIG_PREEMPT_LOCK, spinlock_t etc.. */ LD_WAIT_SLEEP, /* sleeping locks, mutex_t etc.. */ Where lockdep validates that the current lock (the one being acquired) fits in the current wait-context (as generated by the held stack). This ensures that there is no attempt to acquire mutexes while holding spinlocks, to acquire spinlocks while holding raw_spinlocks and so on. In other words, its a more fancy might_sleep(). Obviously RCU made the entire ordeal more complex than a simple single value test because RCU can be acquired in (pretty much) any context and while it presents a context to nested locks it is not the same as it got acquired in. Therefore its necessary to split the wait_type into two values, one representing the acquire (outer) and one representing the nested context (inner). For most 'normal' locks these two are the same. [ To make static initialization easier we have the rule that: .outer == INV means .outer == .inner; because INV == 0. ] It further means that its required to find the minimal .inner of the held stack to compare against the outer of the new lock; because while 'normal' RCU presents a CONFIG type to nested locks, if it is taken while already holding a SPIN type it obviously doesn't relax the rules. Below is an example output generated by the trivial test code: raw_spin_lock(&foo); spin_lock(&bar); spin_unlock(&bar); raw_spin_unlock(&foo); [ BUG: Invalid wait context ] ----------------------------- swapper/0/1 is trying to lock: ffffc90000013f20 (&bar){....}-{3:3}, at: kernel_init+0xdb/0x187 other info that might help us debug this: 1 lock held by swapper/0/1: #0: ffffc90000013ee0 (&foo){+.+.}-{2:2}, at: kernel_init+0xd1/0x187 The way to read it is to look at the new -{n,m} part in the lock description; -{3:3} for the attempted lock, and try and match that up to the held locks, which in this case is the one: -{2,2}. This tells that the acquiring lock requires a more relaxed environment than presented by the lock stack. Currently only the normal locks and RCU are converted, the rest of the lockdep users defaults to .inner = INV which is ignored. More conversions can be done when desired. The check for spinlock_t nesting is not enabled by default. It's a separate config option for now as there are known problems which are currently addressed. The config option allows to identify these problems and to verify that the solutions found are indeed solving them. The config switch will be removed and the checks will permanently enabled once the vast majority of issues has been addressed. [ bigeasy: Move LD_WAIT_FREE,… out of CONFIG_LOCKDEP to avoid compile failure with CONFIG_DEBUG_SPINLOCK + !CONFIG_LOCKDEP] [ tglx: Add the config option ]

Peter Zijlstra committed 6 years ago

Commit de8f5e41 parent a5c623415 files · +307 −47Browse files

Unified · Split

15 files
  1. include/linux/irqflags.h +7 −1
  2. include/linux/lockdep.h +60 −11
  3. include/linux/mutex.h +5 −2
  4. include/linux/rwlock_types.h +5 −1
  5. include/linux/rwsem.h +5 −1
  6. include/linux/sched.h +1 −0
  7. include/linux/spinlock.h +25 −10
  8. include/linux/spinlock_types.h +20 −4
  9. kernel/irq/handle.c +7 −0
  10. kernel/locking/lockdep.c +132 −6
  11. kernel/locking/mutex-debug.c +1 −1
  12. kernel/locking/rwsem.c +1 −1
  13. kernel/locking/spinlock_debug.c +3 −3
  14. kernel/rcu/update.c +18 −6
  15. lib/Kconfig.debug +17 −0
include/linux/irqflags.h+7−1
@@ -37,7 +37,12 @@ · 36 unmodified lines
37# define trace_softirqs_enabled(p) ((p)->softirqs_enabled)37# define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
38# define trace_hardirq_enter() \38# define trace_hardirq_enter() \
39do { \39do { \
40 current->hardirq_context++; \40 if (!current->hardirq_context++) \
41 current->hardirq_threaded = 0; \
42} while (0)
43# define trace_hardirq_threaded() \
44do { \
45 current->hardirq_threaded = 1; \
41} while (0)46} while (0)
42# define trace_hardirq_exit() \47# define trace_hardirq_exit() \
43do { \48do { \
@@ -59,6 +64,7 @@ do { \ · 15 unmodified lines
59# define trace_hardirqs_enabled(p) 064# define trace_hardirqs_enabled(p) 0
60# define trace_softirqs_enabled(p) 065# define trace_softirqs_enabled(p) 0
61# define trace_hardirq_enter() do { } while (0)66# define trace_hardirq_enter() do { } while (0)
67# define trace_hardirq_threaded() do { } while (0)
62# define trace_hardirq_exit() do { } while (0)68# define trace_hardirq_exit() do { } while (0)
63# define lockdep_softirq_enter() do { } while (0)69# define lockdep_softirq_enter() do { } while (0)
64# define lockdep_softirq_exit() do { } while (0)70# define lockdep_softirq_exit() do { } while (0)
include/linux/lockdep.h+60−11
@@ -21,6 +21,22 @@ extern int lock_stat; · 20 unmodified lines
21 21
22#include <linux/types.h>22#include <linux/types.h>
23 23
24enum lockdep_wait_type {
25 LD_WAIT_INV = 0, /* not checked, catch all */
26
27 LD_WAIT_FREE, /* wait free, rcu etc.. */
28 LD_WAIT_SPIN, /* spin loops, raw_spinlock_t etc.. */
29
30#ifdef CONFIG_PROVE_RAW_LOCK_NESTING
31 LD_WAIT_CONFIG, /* CONFIG_PREEMPT_LOCK, spinlock_t etc.. */
32#else
33 LD_WAIT_CONFIG = LD_WAIT_SPIN,
34#endif
35 LD_WAIT_SLEEP, /* sleeping locks, mutex_t etc.. */
36
37 LD_WAIT_MAX, /* must be last */
38};
39
24#ifdef CONFIG_LOCKDEP40#ifdef CONFIG_LOCKDEP
25 41
26#include <linux/linkage.h>42#include <linux/linkage.h>
@@ -111,6 +127,9 @@ struct lock_class { · 84 unmodified lines
111 int name_version;127 int name_version;
112 const char *name;128 const char *name;
113 129
130 short wait_type_inner;
131 short wait_type_outer;
132
114#ifdef CONFIG_LOCK_STAT133#ifdef CONFIG_LOCK_STAT
115 unsigned long contention_point[LOCKSTAT_POINTS];134 unsigned long contention_point[LOCKSTAT_POINTS];
116 unsigned long contending_point[LOCKSTAT_POINTS];135 unsigned long contending_point[LOCKSTAT_POINTS];
@@ -158,6 +177,8 @@ struct lockdep_map { · 41 unmodified lines
158 struct lock_class_key *key;177 struct lock_class_key *key;
159 struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES];178 struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES];
160 const char *name;179 const char *name;
180 short wait_type_outer; /* can be taken in this context */
181 short wait_type_inner; /* presents this context */
161#ifdef CONFIG_LOCK_STAT182#ifdef CONFIG_LOCK_STAT
162 int cpu;183 int cpu;
163 unsigned long ip;184 unsigned long ip;
@@ -299,8 +320,21 @@ extern void lockdep_unregister_key(struct lock_class_key *key); · 135 unmodified lines
299 * to lockdep:320 * to lockdep:
300 */321 */
301 322
302extern void lockdep_init_map(struct lockdep_map *lock, const char *name,323extern void lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
303 struct lock_class_key *key, int subclass);324 struct lock_class_key *key, int subclass, short inner, short outer);
325
326static inline void
327lockdep_init_map_wait(struct lockdep_map *lock, const char *name,
328 struct lock_class_key *key, int subclass, short inner)
329{
330 lockdep_init_map_waits(lock, name, key, subclass, inner, LD_WAIT_INV);
331}
332
333static inline void lockdep_init_map(struct lockdep_map *lock, const char *name,
334 struct lock_class_key *key, int subclass)
335{
336 lockdep_init_map_wait(lock, name, key, subclass, LD_WAIT_INV);
337}
304 338
305/*339/*
306 * Reinitialize a lock key - for cases where there is special locking or340 * Reinitialize a lock key - for cases where there is special locking or
@@ -308,18 +342,29 @@ extern void lockdep_init_map(struct lockdep_map *lock, const char *name, · 1 unmodified line
308 * of dependencies wrong: they are either too broad (they need a class-split)342 * of dependencies wrong: they are either too broad (they need a class-split)
309 * or they are too narrow (they suffer from a false class-split):343 * or they are too narrow (they suffer from a false class-split):
310 */344 */
311#define lockdep_set_class(lock, key) \345#define lockdep_set_class(lock, key) \
312 lockdep_init_map(&(lock)->dep_map, #key, key, 0)346 lockdep_init_map_waits(&(lock)->dep_map, #key, key, 0, \
313#define lockdep_set_class_and_name(lock, key, name) \347 (lock)->dep_map.wait_type_inner, \
314 lockdep_init_map(&(lock)->dep_map, name, key, 0)348 (lock)->dep_map.wait_type_outer)
315#define lockdep_set_class_and_subclass(lock, key, sub) \349
316 lockdep_init_map(&(lock)->dep_map, #key, key, sub)350#define lockdep_set_class_and_name(lock, key, name) \
317#define lockdep_set_subclass(lock, sub) \351 lockdep_init_map_waits(&(lock)->dep_map, name, key, 0, \
318 lockdep_init_map(&(lock)->dep_map, #lock, \352 (lock)->dep_map.wait_type_inner, \
319 (lock)->dep_map.key, sub)353 (lock)->dep_map.wait_type_outer)
354
355#define lockdep_set_class_and_subclass(lock, key, sub) \
356 lockdep_init_map_waits(&(lock)->dep_map, #key, key, sub,\
357 (lock)->dep_map.wait_type_inner, \
358 (lock)->dep_map.wait_type_outer)
359
360#define lockdep_set_subclass(lock, sub) \
361 lockdep_init_map_waits(&(lock)->dep_map, #lock, (lock)->dep_map.key, sub,\
362 (lock)->dep_map.wait_type_inner, \
363 (lock)->dep_map.wait_type_outer)
320 364
321#define lockdep_set_novalidate_class(lock) \365#define lockdep_set_novalidate_class(lock) \
322 lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock)366 lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock)
367
323/*368/*
324 * Compare locking classes369 * Compare locking classes
325 */370 */
@@ -432,6 +477,10 @@ static inline void lockdep_set_selftest_task(struct task_struct *task) · 106 unmodified lines
432# define lock_set_class(l, n, k, s, i) do { } while (0)477# define lock_set_class(l, n, k, s, i) do { } while (0)
433# define lock_set_subclass(l, s, i) do { } while (0)478# define lock_set_subclass(l, s, i) do { } while (0)
434# define lockdep_init() do { } while (0)479# define lockdep_init() do { } while (0)
480# define lockdep_init_map_waits(lock, name, key, sub, inner, outer) \
481 do { (void)(name); (void)(key); } while (0)
482# define lockdep_init_map_wait(lock, name, key, sub, inner) \
483 do { (void)(name); (void)(key); } while (0)
435# define lockdep_init_map(lock, name, key, sub) \484# define lockdep_init_map(lock, name, key, sub) \
436 do { (void)(name); (void)(key); } while (0)485 do { (void)(name); (void)(key); } while (0)
437# define lockdep_set_class(lock, key) do { (void)(key); } while (0)486# define lockdep_set_class(lock, key) do { (void)(key); } while (0)
include/linux/mutex.h+5−2
@@ -109,8 +109,11 @@ do { \ · 108 unmodified lines
109} while (0)109} while (0)
110 110
111#ifdef CONFIG_DEBUG_LOCK_ALLOC111#ifdef CONFIG_DEBUG_LOCK_ALLOC
112# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \112# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
113 , .dep_map = { .name = #lockname }113 , .dep_map = { \
114 .name = #lockname, \
115 .wait_type_inner = LD_WAIT_SLEEP, \
116 }
114#else117#else
115# define __DEP_MAP_MUTEX_INITIALIZER(lockname)118# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
116#endif119#endif
include/linux/rwlock_types.h+5−1
@@ -22,7 +22,11 @@ typedef struct { · 21 unmodified lines
22#define RWLOCK_MAGIC 0xdeaf1eed22#define RWLOCK_MAGIC 0xdeaf1eed
23 23
24#ifdef CONFIG_DEBUG_LOCK_ALLOC24#ifdef CONFIG_DEBUG_LOCK_ALLOC
25# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }25# define RW_DEP_MAP_INIT(lockname) \
26 .dep_map = { \
27 .name = #lockname, \
28 .wait_type_inner = LD_WAIT_CONFIG, \
29 }
26#else30#else
27# define RW_DEP_MAP_INIT(lockname)31# define RW_DEP_MAP_INIT(lockname)
28#endif32#endif
include/linux/rwsem.h+5−1
@@ -65,7 +65,11 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem) · 64 unmodified lines
65/* Common initializer macros and functions */65/* Common initializer macros and functions */
66 66
67#ifdef CONFIG_DEBUG_LOCK_ALLOC67#ifdef CONFIG_DEBUG_LOCK_ALLOC
68# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }68# define __RWSEM_DEP_MAP_INIT(lockname) \
69 , .dep_map = { \
70 .name = #lockname, \
71 .wait_type_inner = LD_WAIT_SLEEP, \
72 }
69#else73#else
70# define __RWSEM_DEP_MAP_INIT(lockname)74# define __RWSEM_DEP_MAP_INIT(lockname)
71#endif75#endif
include/linux/sched.h+1−0
@@ -970,6 +970,7 @@ struct task_struct { · 969 unmodified lines
970 970
971#ifdef CONFIG_TRACE_IRQFLAGS971#ifdef CONFIG_TRACE_IRQFLAGS
972 unsigned int irq_events;972 unsigned int irq_events;
973 unsigned int hardirq_threaded;
973 unsigned long hardirq_enable_ip;974 unsigned long hardirq_enable_ip;
974 unsigned long hardirq_disable_ip;975 unsigned long hardirq_disable_ip;
975 unsigned int hardirq_enable_event;976 unsigned int hardirq_enable_event;
include/linux/spinlock.h+25−10
@@ -93,12 +93,13 @@ · 92 unmodified lines
93 93
94#ifdef CONFIG_DEBUG_SPINLOCK94#ifdef CONFIG_DEBUG_SPINLOCK
95 extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,95 extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
96 struct lock_class_key *key);96 struct lock_class_key *key, short inner);
97# define raw_spin_lock_init(lock) \97
98do { \98# define raw_spin_lock_init(lock) \
99 static struct lock_class_key __key; \99do { \
100 \100 static struct lock_class_key __key; \
101 __raw_spin_lock_init((lock), #lock, &__key); \101 \
102 __raw_spin_lock_init((lock), #lock, &__key, LD_WAIT_SPIN); \
102} while (0)103} while (0)
103 104
104#else105#else
@@ -327,12 +328,26 @@ static __always_inline raw_spinlock_t *spinlock_check(spinlock_t *lock) · 222 unmodified lines
327 return &lock->rlock;328 return &lock->rlock;
328}329}
329 330
330#define spin_lock_init(_lock) \331#ifdef CONFIG_DEBUG_SPINLOCK
331do { \332
332 spinlock_check(_lock); \333# define spin_lock_init(lock) \
333 raw_spin_lock_init(&(_lock)->rlock); \334do { \
335 static struct lock_class_key __key; \
336 \
337 __raw_spin_lock_init(spinlock_check(lock), \
338 #lock, &__key, LD_WAIT_CONFIG); \
339} while (0)
340
341#else
342
343# define spin_lock_init(_lock) \
344do { \
345 spinlock_check(_lock); \
346 *(_lock) = __SPIN_LOCK_UNLOCKED(_lock); \
334} while (0)347} while (0)
335 348
349#endif
350
336static __always_inline void spin_lock(spinlock_t *lock)351static __always_inline void spin_lock(spinlock_t *lock)
337{352{
338 raw_spin_lock(&lock->rlock);353 raw_spin_lock(&lock->rlock);
include/linux/spinlock_types.h+20−4
@@ -33,8 +33,18 @@ typedef struct raw_spinlock { · 32 unmodified lines
33#define SPINLOCK_OWNER_INIT ((void *)-1L)33#define SPINLOCK_OWNER_INIT ((void *)-1L)
34 34
35#ifdef CONFIG_DEBUG_LOCK_ALLOC35#ifdef CONFIG_DEBUG_LOCK_ALLOC
36# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }36# define RAW_SPIN_DEP_MAP_INIT(lockname) \
37 .dep_map = { \
38 .name = #lockname, \
39 .wait_type_inner = LD_WAIT_SPIN, \
40 }
41# define SPIN_DEP_MAP_INIT(lockname) \
42 .dep_map = { \
43 .name = #lockname, \
44 .wait_type_inner = LD_WAIT_CONFIG, \
45 }
37#else46#else
47# define RAW_SPIN_DEP_MAP_INIT(lockname)
38# define SPIN_DEP_MAP_INIT(lockname)48# define SPIN_DEP_MAP_INIT(lockname)
39#endif49#endif
40 50
@@ -51,7 +61,7 @@ typedef struct raw_spinlock { · 10 unmodified lines
51 { \61 { \
52 .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \62 .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
53 SPIN_DEBUG_INIT(lockname) \63 SPIN_DEBUG_INIT(lockname) \
54 SPIN_DEP_MAP_INIT(lockname) }64 RAW_SPIN_DEP_MAP_INIT(lockname) }
55 65
56#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \66#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
57 (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)67 (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
@@ -72,11 +82,17 @@ typedef struct spinlock { · 14 unmodified lines
72 };82 };
73} spinlock_t;83} spinlock_t;
74 84
85#define ___SPIN_LOCK_INITIALIZER(lockname) \
86 { \
87 .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
88 SPIN_DEBUG_INIT(lockname) \
89 SPIN_DEP_MAP_INIT(lockname) }
90
75#define __SPIN_LOCK_INITIALIZER(lockname) \91#define __SPIN_LOCK_INITIALIZER(lockname) \
76 { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }92 { { .rlock = ___SPIN_LOCK_INITIALIZER(lockname) } }
77 93
78#define __SPIN_LOCK_UNLOCKED(lockname) \94#define __SPIN_LOCK_UNLOCKED(lockname) \
79 (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)95 (spinlock_t) __SPIN_LOCK_INITIALIZER(lockname)
80 96
81#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)97#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
82 98
kernel/irq/handle.c+7−0
@@ -145,6 +145,13 @@ irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags · 144 unmodified lines
145 for_each_action_of_desc(desc, action) {145 for_each_action_of_desc(desc, action) {
146 irqreturn_t res;146 irqreturn_t res;
147 147
148 /*
149 * If this IRQ would be threaded under force_irqthreads, mark it so.
150 */
151 if (irq_settings_can_thread(desc) &&
152 !(action->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT)))
153 trace_hardirq_threaded();
154
148 trace_irq_handler_entry(irq, action);155 trace_irq_handler_entry(irq, action);
149 res = action->handler(irq, action->dev_id);156 res = action->handler(irq, action->dev_id);
150 trace_irq_handler_exit(irq, action, res);157 trace_irq_handler_exit(irq, action, res);
kernel/locking/lockdep.c+132−6
@@ -683,7 +683,9 @@ static void print_lock_name(struct lock_class *class) · 682 unmodified lines
683 683
684 printk(KERN_CONT " (");684 printk(KERN_CONT " (");
685 __print_lock_name(class);685 __print_lock_name(class);
686 printk(KERN_CONT "){%s}", usage);686 printk(KERN_CONT "){%s}-{%hd:%hd}", usage,
687 class->wait_type_outer ?: class->wait_type_inner,
688 class->wait_type_inner);
687}689}
688 690
689static void print_lockdep_cache(struct lockdep_map *lock)691static void print_lockdep_cache(struct lockdep_map *lock)
@@ -1264,6 +1266,8 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) · 574 unmodified lines
1264 WARN_ON_ONCE(!list_empty(&class->locks_before));1266 WARN_ON_ONCE(!list_empty(&class->locks_before));
1265 WARN_ON_ONCE(!list_empty(&class->locks_after));1267 WARN_ON_ONCE(!list_empty(&class->locks_after));
1266 class->name_version = count_matching_names(class);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;
1267 /*1271 /*
1268 * We use RCU's safe list-add method to make1272 * We use RCU's safe list-add method to make
1269 * parallel walking of the hash-list safe:1273 * parallel walking of the hash-list safe:
@@ -3948,6 +3952,113 @@ static int mark_lock(struct task_struct *curr, struct held_lock *this, · 2678 unmodified lines
3948 return ret;3952 return ret;
3949}3953}
3950 3954
3955static int
3956print_lock_invalid_wait_context(struct task_struct *curr,
3957 struct held_lock *hlock)
3958{
3959 if (!debug_locks_off())
3960 return 0;
3961 if (debug_locks_silent)
3962 return 0;
3963
3964 pr_warn("\n");
3965 pr_warn("=============================\n");
3966 pr_warn("[ BUG: Invalid wait context ]\n");
3967 print_kernel_ident();
3968 pr_warn("-----------------------------\n");
3969
3970 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3971 print_lock(hlock);
3972
3973 pr_warn("other info that might help us debug this:\n");
3974 lockdep_print_held_locks(curr);
3975
3976 pr_warn("stack backtrace:\n");
3977 dump_stack();
3978
3979 return 0;
3980}
3981
3982/*
3983 * Verify the wait_type context.
3984 *
3985 * This check validates we takes locks in the right wait-type order; that is it
3986 * ensures that we do not take mutexes inside spinlocks and do not attempt to
3987 * acquire spinlocks inside raw_spinlocks and the sort.
3988 *
3989 * The entire thing is slightly more complex because of RCU, RCU is a lock that
3990 * can be taken from (pretty much) any context but also has constraints.
3991 * However when taken in a stricter environment the RCU lock does not loosen
3992 * the constraints.
3993 *
3994 * Therefore we must look for the strictest environment in the lock stack and
3995 * compare that to the lock we're trying to acquire.
3996 */
3997static int check_wait_context(struct task_struct *curr, struct held_lock *next)
3998{
3999 short next_inner = hlock_class(next)->wait_type_inner;
4000 short next_outer = hlock_class(next)->wait_type_outer;
4001 short curr_inner;
4002 int depth;
4003
4004 if (!curr->lockdep_depth || !next_inner || next->trylock)
4005 return 0;
4006
4007 if (!next_outer)
4008 next_outer = next_inner;
4009
4010 /*
4011 * Find start of current irq_context..
4012 */
4013 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
4014 struct held_lock *prev = curr->held_locks + depth;
4015 if (prev->irq_context != next->irq_context)
4016 break;
4017 }
4018 depth++;
4019
4020 /*
4021 * Set appropriate wait type for the context; for IRQs we have to take
4022 * into account force_irqthread as that is implied by PREEMPT_RT.
4023 */
4024 if (curr->hardirq_context) {
4025 /*
4026 * Check if force_irqthreads will run us threaded.
4027 */
4028 if (curr->hardirq_threaded)
4029 curr_inner = LD_WAIT_CONFIG;
4030 else
4031 curr_inner = LD_WAIT_SPIN;
4032 } else if (curr->softirq_context) {
4033 /*
4034 * Softirqs are always threaded.
4035 */
4036 curr_inner = LD_WAIT_CONFIG;
4037 } else {
4038 curr_inner = LD_WAIT_MAX;
4039 }
4040
4041 for (; depth < curr->lockdep_depth; depth++) {
4042 struct held_lock *prev = curr->held_locks + depth;
4043 short prev_inner = hlock_class(prev)->wait_type_inner;
4044
4045 if (prev_inner) {
4046 /*
4047 * We can have a bigger inner than a previous one
4048 * when outer is smaller than inner, as with RCU.
4049 *
4050 * Also due to trylocks.
4051 */
4052 curr_inner = min(curr_inner, prev_inner);
4053 }
4054 }
4055
4056 if (next_outer > curr_inner)
4057 return print_lock_invalid_wait_context(curr, next);
4058
4059 return 0;
4060}
4061
3951#else /* CONFIG_PROVE_LOCKING */4062#else /* CONFIG_PROVE_LOCKING */
3952 4063
3953static inline int4064static inline int
@@ -3967,13 +4078,20 @@ static inline int separate_irq_context(struct task_struct *curr, · 13 unmodified lines
3967 return 0;4078 return 0;
3968}4079}
3969 4080
4081static inline int check_wait_context(struct task_struct *curr,
4082 struct held_lock *next)
4083{
4084 return 0;
4085}
4086
3970#endif /* CONFIG_PROVE_LOCKING */4087#endif /* CONFIG_PROVE_LOCKING */
3971 4088
3972/*4089/*
3973 * Initialize a lock instance's lock-class mapping info:4090 * Initialize a lock instance's lock-class mapping info:
3974 */4091 */
3975void lockdep_init_map(struct lockdep_map *lock, const char *name,4092void lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
3976 struct lock_class_key *key, int subclass)4093 struct lock_class_key *key, int subclass,
4094 short inner, short outer)
3977{4095{
3978 int i;4096 int i;
3979 4097
@@ -3994,6 +4112,9 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name, · 14 unmodified lines
3994 4112
3995 lock->name = name;4113 lock->name = name;
3996 4114
4115 lock->wait_type_outer = outer;
4116 lock->wait_type_inner = inner;
4117
3997 /*4118 /*
3998 * No key, no joy, we need to hash something.4119 * No key, no joy, we need to hash something.
3999 */4120 */
@@ -4027,7 +4148,7 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name, · 27 unmodified lines
4027 raw_local_irq_restore(flags);4148 raw_local_irq_restore(flags);
4028 }4149 }
4029}4150}
4030EXPORT_SYMBOL_GPL(lockdep_init_map);4151EXPORT_SYMBOL_GPL(lockdep_init_map_waits);
4031 4152
4032struct lock_class_key __lockdep_no_validate__;4153struct lock_class_key __lockdep_no_validate__;
4033EXPORT_SYMBOL_GPL(__lockdep_no_validate__);4154EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
@@ -4128,7 +4249,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, · 94 unmodified lines
4128 4249
4129 class_idx = class - lock_classes;4250 class_idx = class - lock_classes;
4130 4251
4131 if (depth) {4252 if (depth) { /* we're holding locks */
4132 hlock = curr->held_locks + depth - 1;4253 hlock = curr->held_locks + depth - 1;
4133 if (hlock->class_idx == class_idx && nest_lock) {4254 if (hlock->class_idx == class_idx && nest_lock) {
4134 if (!references)4255 if (!references)
@@ -4170,6 +4291,9 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, · 35 unmodified lines
4170#endif4291#endif
4171 hlock->pin_count = pin_count;4292 hlock->pin_count = pin_count;
4172 4293
4294 if (check_wait_context(curr, hlock))
4295 return 0;
4296
4173 /* Initialize the lock usage bit */4297 /* Initialize the lock usage bit */
4174 if (!mark_usage(curr, hlock, check))4298 if (!mark_usage(curr, hlock, check))
4175 return 0;4299 return 0;
@@ -4405,7 +4529,9 @@ __lock_set_class(struct lockdep_map *lock, const char *name, · 229 unmodified lines
4405 return 0;4529 return 0;
4406 }4530 }
4407 4531
4408 lockdep_init_map(lock, name, key, 0);4532 lockdep_init_map_waits(lock, name, key, 0,
4533 lock->wait_type_inner,
4534 lock->wait_type_outer);
4409 class = register_lock_class(lock, subclass, 0);4535 class = register_lock_class(lock, subclass, 0);
4410 hlock->class_idx = class - lock_classes;4536 hlock->class_idx = class - lock_classes;
4411 4537
kernel/locking/mutex-debug.c+1−1
@@ -85,7 +85,7 @@ void debug_mutex_init(struct mutex *lock, const char *name, · 84 unmodified lines
85 * Make sure we are not reinitializing a held lock:85 * Make sure we are not reinitializing a held lock:
86 */86 */
87 debug_check_no_locks_freed((void *)lock, sizeof(*lock));87 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
88 lockdep_init_map(&lock->dep_map, name, key, 0);88 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
89#endif89#endif
90 lock->magic = lock;90 lock->magic = lock;
91}91}
kernel/locking/rwsem.c+1−1
@@ -328,7 +328,7 @@ void __init_rwsem(struct rw_semaphore *sem, const char *name, · 327 unmodified lines
328 * Make sure we are not reinitializing a held semaphore:328 * Make sure we are not reinitializing a held semaphore:
329 */329 */
330 debug_check_no_locks_freed((void *)sem, sizeof(*sem));330 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
331 lockdep_init_map(&sem->dep_map, name, key, 0);331 lockdep_init_map_wait(&sem->dep_map, name, key, 0, LD_WAIT_SLEEP);
332#endif332#endif
333#ifdef CONFIG_DEBUG_RWSEMS333#ifdef CONFIG_DEBUG_RWSEMS
334 sem->magic = sem;334 sem->magic = sem;
kernel/locking/spinlock_debug.c+3−3
@@ -14,14 +14,14 @@ · 13 unmodified lines
14#include <linux/export.h>14#include <linux/export.h>
15 15
16void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,16void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
17 struct lock_class_key *key)17 struct lock_class_key *key, short inner)
18{18{
19#ifdef CONFIG_DEBUG_LOCK_ALLOC19#ifdef CONFIG_DEBUG_LOCK_ALLOC
20 /*20 /*
21 * Make sure we are not reinitializing a held lock:21 * Make sure we are not reinitializing a held lock:
22 */22 */
23 debug_check_no_locks_freed((void *)lock, sizeof(*lock));23 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
24 lockdep_init_map(&lock->dep_map, name, key, 0);24 lockdep_init_map_wait(&lock->dep_map, name, key, 0, inner);
25#endif25#endif
26 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;26 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
27 lock->magic = SPINLOCK_MAGIC;27 lock->magic = SPINLOCK_MAGIC;
@@ -39,7 +39,7 @@ void __rwlock_init(rwlock_t *lock, const char *name, · 11 unmodified lines
39 * Make sure we are not reinitializing a held lock:39 * Make sure we are not reinitializing a held lock:
40 */40 */
41 debug_check_no_locks_freed((void *)lock, sizeof(*lock));41 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
42 lockdep_init_map(&lock->dep_map, name, key, 0);42 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_CONFIG);
43#endif43#endif
44 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;44 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
45 lock->magic = RWLOCK_MAGIC;45 lock->magic = RWLOCK_MAGIC;
kernel/rcu/update.c+18−6
@@ -227,18 +227,30 @@ core_initcall(rcu_set_runtime_mode); · 226 unmodified lines
227 227
228#ifdef CONFIG_DEBUG_LOCK_ALLOC228#ifdef CONFIG_DEBUG_LOCK_ALLOC
229static struct lock_class_key rcu_lock_key;229static struct lock_class_key rcu_lock_key;
230struct lockdep_map rcu_lock_map =230struct lockdep_map rcu_lock_map = {
231 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);231 .name = "rcu_read_lock",
232 .key = &rcu_lock_key,
233 .wait_type_outer = LD_WAIT_FREE,
234 .wait_type_inner = LD_WAIT_CONFIG, /* XXX PREEMPT_RCU ? */
235};
232EXPORT_SYMBOL_GPL(rcu_lock_map);236EXPORT_SYMBOL_GPL(rcu_lock_map);
233 237
234static struct lock_class_key rcu_bh_lock_key;238static struct lock_class_key rcu_bh_lock_key;
235struct lockdep_map rcu_bh_lock_map =239struct lockdep_map rcu_bh_lock_map = {
236 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);240 .name = "rcu_read_lock_bh",
241 .key = &rcu_bh_lock_key,
242 .wait_type_outer = LD_WAIT_FREE,
243 .wait_type_inner = LD_WAIT_CONFIG, /* PREEMPT_LOCK also makes BH preemptible */
244};
237EXPORT_SYMBOL_GPL(rcu_bh_lock_map);245EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
238 246
239static struct lock_class_key rcu_sched_lock_key;247static struct lock_class_key rcu_sched_lock_key;
240struct lockdep_map rcu_sched_lock_map =248struct lockdep_map rcu_sched_lock_map = {
241 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);249 .name = "rcu_read_lock_sched",
250 .key = &rcu_sched_lock_key,
251 .wait_type_outer = LD_WAIT_FREE,
252 .wait_type_inner = LD_WAIT_SPIN,
253};
242EXPORT_SYMBOL_GPL(rcu_sched_lock_map);254EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
243 255
244static struct lock_class_key rcu_callback_key;256static struct lock_class_key rcu_callback_key;
lib/Kconfig.debug+17−0
@@ -1086,6 +1086,23 @@ config PROVE_LOCKING · 1085 unmodified lines
1086 1086
1087 For more details, see Documentation/locking/lockdep-design.rst.1087 For more details, see Documentation/locking/lockdep-design.rst.
1088 1088
1089config PROVE_RAW_LOCK_NESTING
1090 bool "Enable raw_spinlock - spinlock nesting checks"
1091 depends on PROVE_LOCKING
1092 default n
1093 help
1094 Enable the raw_spinlock vs. spinlock nesting checks which ensure
1095 that the lock nesting rules for PREEMPT_RT enabled kernels are
1096 not violated.
1097
1098 NOTE: There are known nesting problems. So if you enable this
1099 option expect lockdep splats until these problems have been fully
1100 addressed which is work in progress. This config switch allows to
1101 identify and analyze these problems. It will be removed and the
1102 check permanentely enabled once the main issues have been fixed.
1103
1104 If unsure, select N.
1105
1089config LOCK_STAT1106config LOCK_STAT
1090 bool "Lock usage statistics"1107 bool "Lock usage statistics"
1091 depends on DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT1108 depends on DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t