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
3737 # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
3838 # define trace_hardirq_enter() \
3939 do { \
40− current->hardirq_context++; \
40+ if (!current->hardirq_context++) \
41+ current->hardirq_threaded = 0; \
42+} while (0)
43+# define trace_hardirq_threaded() \
44+do { \
45+ current->hardirq_threaded = 1; \
4146 } while (0)
4247 # define trace_hardirq_exit() \
4348 do { \
@@ -59,6 +64,7 @@ do { \ · 15 unmodified lines
5964 # define trace_hardirqs_enabled(p) 0
6065 # define trace_softirqs_enabled(p) 0
6166 # define trace_hardirq_enter() do { } while (0)
67+# define trace_hardirq_threaded() do { } while (0)
6268 # define trace_hardirq_exit() do { } while (0)
6369 # define lockdep_softirq_enter() do { } while (0)
6470 # define lockdep_softirq_exit() do { } while (0)
include/linux/lockdep.h+60−11
@@ -21,6 +21,22 @@ extern int lock_stat; · 20 unmodified lines
2121
2222 #include <linux/types.h>
2323
24+enum 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+
2440 #ifdef CONFIG_LOCKDEP
2541
2642 #include <linux/linkage.h>
@@ -111,6 +127,9 @@ struct lock_class { · 84 unmodified lines
111127 int name_version;
112128 const char *name;
113129
130+ short wait_type_inner;
131+ short wait_type_outer;
132+
114133 #ifdef CONFIG_LOCK_STAT
115134 unsigned long contention_point[LOCKSTAT_POINTS];
116135 unsigned long contending_point[LOCKSTAT_POINTS];
@@ -158,6 +177,8 @@ struct lockdep_map { · 41 unmodified lines
158177 struct lock_class_key *key;
159178 struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES];
160179 const char *name;
180+ short wait_type_outer; /* can be taken in this context */
181+ short wait_type_inner; /* presents this context */
161182 #ifdef CONFIG_LOCK_STAT
162183 int cpu;
163184 unsigned long ip;
@@ -299,8 +320,21 @@ extern void lockdep_unregister_key(struct lock_class_key *key); · 135 unmodified lines
299320 * to lockdep:
300321 */
301322
302−extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
303− struct lock_class_key *key, int subclass);
323+extern void lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
324+ struct lock_class_key *key, int subclass, short inner, short outer);
325+
326+static inline void
327+lockdep_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+
333+static 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+}
304338
305339 /*
306340 * 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
308342 * of dependencies wrong: they are either too broad (they need a class-split)
309343 * or they are too narrow (they suffer from a false class-split):
310344 */
311−#define lockdep_set_class(lock, key) \
312− lockdep_init_map(&(lock)->dep_map, #key, key, 0)
313−#define lockdep_set_class_and_name(lock, key, name) \
314− lockdep_init_map(&(lock)->dep_map, name, key, 0)
315−#define lockdep_set_class_and_subclass(lock, key, sub) \
316− lockdep_init_map(&(lock)->dep_map, #key, key, sub)
317−#define lockdep_set_subclass(lock, sub) \
318− lockdep_init_map(&(lock)->dep_map, #lock, \
319− (lock)->dep_map.key, sub)
345+#define lockdep_set_class(lock, key) \
346+ lockdep_init_map_waits(&(lock)->dep_map, #key, key, 0, \
347+ (lock)->dep_map.wait_type_inner, \
348+ (lock)->dep_map.wait_type_outer)
349+
350+#define lockdep_set_class_and_name(lock, key, name) \
351+ lockdep_init_map_waits(&(lock)->dep_map, name, key, 0, \
352+ (lock)->dep_map.wait_type_inner, \
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)
320364
321365 #define lockdep_set_novalidate_class(lock) \
322366 lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock)
367+
323368 /*
324369 * Compare locking classes
325370 */
@@ -432,6 +477,10 @@ static inline void lockdep_set_selftest_task(struct task_struct *task) · 106 unmodified lines
432477 # define lock_set_class(l, n, k, s, i) do { } while (0)
433478 # define lock_set_subclass(l, s, i) do { } while (0)
434479 # 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)
435484 # define lockdep_init_map(lock, name, key, sub) \
436485 do { (void)(name); (void)(key); } while (0)
437486 # 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
109109 } while (0)
110110
111111 #ifdef CONFIG_DEBUG_LOCK_ALLOC
112−# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
113− , .dep_map = { .name = #lockname }
112+# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
113+ , .dep_map = { \
114+ .name = #lockname, \
115+ .wait_type_inner = LD_WAIT_SLEEP, \
116+ }
114117 #else
115118 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
116119 #endif
include/linux/rwlock_types.h+5−1
@@ -22,7 +22,11 @@ typedef struct { · 21 unmodified lines
2222 #define RWLOCK_MAGIC 0xdeaf1eed
2323
2424 #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+ }
2630 #else
2731 # define RW_DEP_MAP_INIT(lockname)
2832 #endif
include/linux/rwsem.h+5−1
@@ -65,7 +65,11 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem) · 64 unmodified lines
6565 /* Common initializer macros and functions */
6666
6767 #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+ }
6973 #else
7074 # define __RWSEM_DEP_MAP_INIT(lockname)
7175 #endif
include/linux/sched.h+1−0
@@ -970,6 +970,7 @@ struct task_struct { · 969 unmodified lines
970970
971971 #ifdef CONFIG_TRACE_IRQFLAGS
972972 unsigned int irq_events;
973+ unsigned int hardirq_threaded;
973974 unsigned long hardirq_enable_ip;
974975 unsigned long hardirq_disable_ip;
975976 unsigned int hardirq_enable_event;
include/linux/spinlock.h+25−10
@@ -93,12 +93,13 @@ · 92 unmodified lines
9393
9494 #ifdef CONFIG_DEBUG_SPINLOCK
9595 extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
96− struct lock_class_key *key);
97−# define raw_spin_lock_init(lock) \
98−do { \
99− static struct lock_class_key __key; \
100− \
101− __raw_spin_lock_init((lock), #lock, &__key); \
96+ struct lock_class_key *key, short inner);
97+
98+# define raw_spin_lock_init(lock) \
99+do { \
100+ static struct lock_class_key __key; \
101+ \
102+ __raw_spin_lock_init((lock), #lock, &__key, LD_WAIT_SPIN); \
102103 } while (0)
103104
104105 #else
@@ -327,12 +328,26 @@ static __always_inline raw_spinlock_t *spinlock_check(spinlock_t *lock) · 222 unmodified lines
327328 return &lock->rlock;
328329 }
329330
330−#define spin_lock_init(_lock) \
331−do { \
332− spinlock_check(_lock); \
333− raw_spin_lock_init(&(_lock)->rlock); \
331+#ifdef CONFIG_DEBUG_SPINLOCK
332+
333+# define spin_lock_init(lock) \
334+do { \
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) \
344+do { \
345+ spinlock_check(_lock); \
346+ *(_lock) = __SPIN_LOCK_UNLOCKED(_lock); \
334347 } while (0)
335348
349+#endif
350+
336351 static __always_inline void spin_lock(spinlock_t *lock)
337352 {
338353 raw_spin_lock(&lock->rlock);
include/linux/spinlock_types.h+20−4
@@ -33,8 +33,18 @@ typedef struct raw_spinlock { · 32 unmodified lines
3333 #define SPINLOCK_OWNER_INIT ((void *)-1L)
3434
3535 #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+ }
3746 #else
47+# define RAW_SPIN_DEP_MAP_INIT(lockname)
3848 # define SPIN_DEP_MAP_INIT(lockname)
3949 #endif
4050
@@ -51,7 +61,7 @@ typedef struct raw_spinlock { · 10 unmodified lines
5161 { \
5262 .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
5363 SPIN_DEBUG_INIT(lockname) \
54− SPIN_DEP_MAP_INIT(lockname) }
64+ RAW_SPIN_DEP_MAP_INIT(lockname) }
5565
5666 #define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
5767 (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
@@ -72,11 +82,17 @@ typedef struct spinlock { · 14 unmodified lines
7282 };
7383 } spinlock_t;
7484
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+
7591 #define __SPIN_LOCK_INITIALIZER(lockname) \
76− { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
92+ { { .rlock = ___SPIN_LOCK_INITIALIZER(lockname) } }
7793
7894 #define __SPIN_LOCK_UNLOCKED(lockname) \
79− (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
95+ (spinlock_t) __SPIN_LOCK_INITIALIZER(lockname)
8096
8197 #define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
8298
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
145145 for_each_action_of_desc(desc, action) {
146146 irqreturn_t res;
147147
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+
148155 trace_irq_handler_entry(irq, action);
149156 res = action->handler(irq, action->dev_id);
150157 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
683683
684684 printk(KERN_CONT " (");
685685 __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);
687689 }
688690
689691 static 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
12641266 WARN_ON_ONCE(!list_empty(&class->locks_before));
12651267 WARN_ON_ONCE(!list_empty(&class->locks_after));
12661268 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;
12671271 /*
12681272 * We use RCU's safe list-add method to make
12691273 * 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
39483952 return ret;
39493953 }
39503954
3955+static int
3956+print_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+ */
3997+static 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+
39514062 #else /* CONFIG_PROVE_LOCKING */
39524063
39534064 static inline int
@@ -3967,13 +4078,20 @@ static inline int separate_irq_context(struct task_struct *curr, · 13 unmodified lines
39674078 return 0;
39684079 }
39694080
4081+static inline int check_wait_context(struct task_struct *curr,
4082+ struct held_lock *next)
4083+{
4084+ return 0;
4085+}
4086+
39704087 #endif /* CONFIG_PROVE_LOCKING */
39714088
39724089 /*
39734090 * Initialize a lock instance's lock-class mapping info:
39744091 */
3975−void lockdep_init_map(struct lockdep_map *lock, const char *name,
3976− struct lock_class_key *key, int subclass)
4092+void lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
4093+ struct lock_class_key *key, int subclass,
4094+ short inner, short outer)
39774095 {
39784096 int i;
39794097
@@ -3994,6 +4112,9 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name, · 14 unmodified lines
39944112
39954113 lock->name = name;
39964114
4115+ lock->wait_type_outer = outer;
4116+ lock->wait_type_inner = inner;
4117+
39974118 /*
39984119 * No key, no joy, we need to hash something.
39994120 */
@@ -4027,7 +4148,7 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name, · 27 unmodified lines
40274148 raw_local_irq_restore(flags);
40284149 }
40294150 }
4030−EXPORT_SYMBOL_GPL(lockdep_init_map);
4151+EXPORT_SYMBOL_GPL(lockdep_init_map_waits);
40314152
40324153 struct lock_class_key __lockdep_no_validate__;
40334154 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
@@ -4128,7 +4249,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, · 94 unmodified lines
41284249
41294250 class_idx = class - lock_classes;
41304251
4131− if (depth) {
4252+ if (depth) { /* we're holding locks */
41324253 hlock = curr->held_locks + depth - 1;
41334254 if (hlock->class_idx == class_idx && nest_lock) {
41344255 if (!references)
@@ -4170,6 +4291,9 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, · 35 unmodified lines
41704291 #endif
41714292 hlock->pin_count = pin_count;
41724293
4294+ if (check_wait_context(curr, hlock))
4295+ return 0;
4296+
41734297 /* Initialize the lock usage bit */
41744298 if (!mark_usage(curr, hlock, check))
41754299 return 0;
@@ -4405,7 +4529,9 @@ __lock_set_class(struct lockdep_map *lock, const char *name, · 229 unmodified lines
44054529 return 0;
44064530 }
44074531
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);
44094535 class = register_lock_class(lock, subclass, 0);
44104536 hlock->class_idx = class - lock_classes;
44114537
kernel/locking/mutex-debug.c+1−1
@@ -85,7 +85,7 @@ void debug_mutex_init(struct mutex *lock, const char *name, · 84 unmodified lines
8585 * Make sure we are not reinitializing a held lock:
8686 */
8787 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);
8989 #endif
9090 lock->magic = lock;
9191 }
kernel/locking/rwsem.c+1−1
@@ -328,7 +328,7 @@ void __init_rwsem(struct rw_semaphore *sem, const char *name, · 327 unmodified lines
328328 * Make sure we are not reinitializing a held semaphore:
329329 */
330330 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);
332332 #endif
333333 #ifdef CONFIG_DEBUG_RWSEMS
334334 sem->magic = sem;
kernel/locking/spinlock_debug.c+3−3
@@ -14,14 +14,14 @@ · 13 unmodified lines
1414 #include <linux/export.h>
1515
1616 void __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)
1818 {
1919 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2020 /*
2121 * Make sure we are not reinitializing a held lock:
2222 */
2323 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);
2525 #endif
2626 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
2727 lock->magic = SPINLOCK_MAGIC;
@@ -39,7 +39,7 @@ void __rwlock_init(rwlock_t *lock, const char *name, · 11 unmodified lines
3939 * Make sure we are not reinitializing a held lock:
4040 */
4141 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);
4343 #endif
4444 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
4545 lock->magic = RWLOCK_MAGIC;
kernel/rcu/update.c+18−6
@@ -227,18 +227,30 @@ core_initcall(rcu_set_runtime_mode); · 226 unmodified lines
227227
228228 #ifdef CONFIG_DEBUG_LOCK_ALLOC
229229 static struct lock_class_key rcu_lock_key;
230−struct lockdep_map rcu_lock_map =
231− STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
230+struct lockdep_map rcu_lock_map = {
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+};
232236 EXPORT_SYMBOL_GPL(rcu_lock_map);
233237
234238 static struct lock_class_key rcu_bh_lock_key;
235−struct lockdep_map rcu_bh_lock_map =
236− STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
239+struct lockdep_map rcu_bh_lock_map = {
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+};
237245 EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
238246
239247 static struct lock_class_key rcu_sched_lock_key;
240−struct lockdep_map rcu_sched_lock_map =
241− STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
248+struct lockdep_map rcu_sched_lock_map = {
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+};
242254 EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
243255
244256 static struct lock_class_key rcu_callback_key;
lib/Kconfig.debug+17−0
@@ -1086,6 +1086,23 @@ config PROVE_LOCKING · 1085 unmodified lines
10861086
10871087 For more details, see Documentation/locking/lockdep-design.rst.
10881088
1089+config 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+
10891106 config LOCK_STAT
10901107 bool "Lock usage statistics"
10911108 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