locking/lockdep: Add a new class of terminal locks
Subsystems (13)
Waiman Long
reviewerv2 cover locking/lockdep: Add a new class of terminal locks
v1->v2:
- Mark more locks as terminal.
- Add a patch to remove the unused version field from lock_class.
- Steal some bits from pin_count of held_lock for flags.
- Add a patch to warn if a task holding a raw spinlock is acquiring an non-raw lock.
The purpose of this patchset is to add a new class of locks called terminal locks and converts some of the low level raw or regular spinlocks to terminal locks. A terminal lock does not have forward dependency and it won't allow a lock or unlock operation on another lock. Two level nesting of terminal locks is allowed, though.
Only spinlocks that are acquired with the _irq/_irqsave variants or acquired in an IRQ disabled context should be classified as terminal locks.
Because of the restrictions on terminal locks, we can do simple checks on them without using the lockdep lock validation machinery. The advantages of making these changes are as follows:
1) It saves table entries used by the validation code and hence make it harder to overflow those tables.
2) The lockdep check in __lock_acquire() will be a little bit faster for terminal locks without using the lock validation code.
In fact, it is possible to overflow some of the tables by running a variety of different workloads on a debug kernel. I have seen bug reports about exhausting MAX_LOCKDEP_KEYS, MAX_LOCKDEP_ENTRIES and MAX_STACK_TRACE_ENTRIES. This patch will help to reduce the chance of overflowing some of the tables.
Below were selected output lines from the lockdep_stats files of the patched and unpatched kernels after bootup and running parallel kernel builds and some perf benchmarks.
Item Unpatched kernel Patched kernel % Change ---- ---------------- -------------- -------- direct dependencies 9924 9032 -9.0% dependency chains 18258 16326 -10.6% dependency chain hlocks 73198 64927 -11.3%
stack-trace entries 110502 103225 -6.6%
There were some reductions in the size of the lockdep tables. They were not significant, but it is still a good start to rein in the number of entries in those tables to make it harder to overflow them.
In term of performance, there isn't that much noticeable differences in both the kernel build and perf benchmark. Low level locking microbenchmark with 4 locking threads shows the following locking rates on a Haswell system:
Kernel Lock Rate
------ ---- ----
Unpatched kernel Regular lock 2,288 kop/s
Patched kernel Regular lock 2,352 kop/s
Terminal lock 2,512 kop/sI was not sure why there was a slight performance improvement with the patched kernel. However, comparing the regular and terminal lock results, there was a slight 7% improvement in locking throughput for terminal locks.
Looking at the perf ouput for regular lock:
5.43% run-locktest [kernel.vmlinux] [k] __lock_acquire 4.65% run-locktest [kernel.vmlinux] [k] lock_contended 2.80% run-locktest [kernel.vmlinux] [k] lock_acquired 2.53% run-locktest [kernel.vmlinux] [k] lock_release 1.42% run-locktest [kernel.vmlinux] [k] mark_lock
For terminal lock:
5.00% run-locktest [kernel.vmlinux] [k] __lock_acquire 4.66% run-locktest [kernel.vmlinux] [k] lock_contended 2.88% run-locktest [kernel.vmlinux] [k] lock_acquired 2.55% run-locktest [kernel.vmlinux] [k] lock_release 1.34% run-locktest [kernel.vmlinux] [k] mark_lock
The __lock_acquire() function ran a bit faster with terminal lock, but the other lockdep functions remain more or less the same in term of performance.
In term internal lockdep structure sizes, there should be no size increase for 64-bit architectures with CONFIG_LOCK_STAT defined. The lockdep_map structure will increase in size for 32-bit architectures or when CONFIG_LOCK_STAT isn't defined.
Waiman Long (17):
locking/lockdep: Remove version from lock_class structure locking/lockdep: Rework lockdep_set_novalidate_class() locking/lockdep: Add a new terminal lock type locking/lockdep: Add DEFINE_TERMINAL_SPINLOCK() and related macros
printk: Mark logbuf_lock & console_owner_lock as terminal locks
debugobjects: Mark pool_lock as a terminal lock
debugobjects: Move printk out of db lock critical sections locking/lockdep: Add support for nestable terminal locks
debugobjects: Make object hash locks nestable terminal locks lib/stackdepot: Make depot_lock a terminal spinlock locking/rwsem: Mark rwsem.wait_lock as a terminal lock
cgroup: Mark the rstat percpu lock as terminal mm/kasan: Make quarantine_lock a terminal lock
dma-debug: Mark free_entries_lock as terminal
kernfs: Mark kernfs_open_node_lock as terminal lock
delay_acct: Mark task's delays->lock as terminal spinlock locking/lockdep: Check raw/non-raw locking conflicts
fs/kernfs/file.c | 2 +- include/linux/lockdep.h | 59 +++++++++++++++++++++++---- include/linux/rwsem.h | 11 +++++- include/linux/spinlock_types.h | 34 ++++++++++------ kernel/cgroup/rstat.c | 9 ++++- kernel/delayacct.c | 4 +- kernel/dma/debug.c | 2 +- kernel/locking/lockdep.c | 81 ++++++++++++++++++++++++++++++++------ kernel/locking/lockdep_internals.h | 5 +++ kernel/locking/lockdep_proc.c | 11 +++++- kernel/locking/rwsem-xadd.c | 1 + kernel/locking/spinlock_debug.c | 1 + kernel/printk/printk.c | 4 +- kernel/printk/printk_safe.c | 2 +- lib/debugobjects.c | 70 ++++++++++++++++++++++---------- lib/stackdepot.c | 2 +- mm/kasan/quarantine.c | 2 +- 17 files changed, 235 insertions(+), 65 deletions(-)
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 1/17 locking/lockdep: Remove version from lock_class structure
It turns out the version field in the lock_class structure isn't used anywhere. Just remove it.
--- include/linux/lockdep.h | 2 -- 1 file changed, 2 deletions(-)
include/linux/lockdep.h +0 -2
@@ -97,8 +97,6 @@ struct lock_class {* Generation counter, when doing certain classes of graph walking, * to ensure that we check one node only once: */ -unsigned int version; -int name_version;const char *name;
Signature · 1 line
1.8.3.1
tip-bot for Waiman Long
merge bot[tip:locking/core] locking/lockdep: Remove ::version from lock_class structure
locking/lockdep: Remove ::version from lock_class structure
Commit-ID: 2421b7f3573babfe1673a5ffee1677a5013e6df1 Gitweb: https://git.kernel.org/tip/2421b7f3573babfe1673a5ffee1677a5013e6df1 Author: Waiman Long <[email protected]> AuthorDate: Mon, 19 Nov 2018 13:55:10 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 11 Dec 2018 14:54:46 +0100
locking/lockdep: Remove ::version from lock_class structure
It turns out the version field in the lock_class structure isn't used anywhere. Just remove it.
--- include/linux/lockdep.h | 2 -- 1 file changed, 2 deletions(-)
include/linux/lockdep.h +0 -2
@@ -97,8 +97,6 @@ struct lock_class {* Generation counter, when doing certain classes of graph walking, * to ensure that we check one node only once: */ -unsigned int version; -int name_version;const char *name;
Waiman Long
reviewerPATCH v2 2/17 locking/lockdep: Rework lockdep_set_novalidate_class()
In reply to Waiman LongThe current lockdep_set_novalidate_class() implementation is like a hack. It assigns a special class key for that lock and calls lockdep_init_map() twice.
This patch changes the implementation to make it as a flag bit instead. This will allow other special locking class types to be defined and used in the lockdep code. A new "type" field is added to both the lockdep_map and lock_class structures.
The new field can now be used to designate a lock and a class object as novalidate. The lockdep_set_novalidate_class() call, however, should be called only after lock initialization which calls lockdep_init_map().
For 64-bit architectures, the new type field won't increase the size of the lock_class structure. The lockdep_map structure won't change as well for 64-bit architectures with CONFIG_LOCK_STAT configured.
Please note that lockdep_set_novalidate_class() should not be used at all unless there is overwhelming reason to do so. Hopefully we can retired it in the near future.
--- include/linux/lockdep.h | 17 ++++++++++++++--- kernel/locking/lockdep.c | 14 +++++++------- 2 files changed, 21 insertions(+), 10 deletions(-)
include/linux/lockdep.h +14 -3
@@ -58,8 +58,6 @@ struct lock_class_key {struct lockdep_subclass_key subkeys[MAX_LOCKDEP_SUBCLASSES ]; }; -extern struct lock_class_key __lockdep_no_validate__; -#define LOCKSTAT_POINTS 4/*
@@ -94,6 +92,11 @@ struct lock_class {struct list_head locks_after, locks_before;/* +* Lock class type flags +*/ +unsigned int flags; + +/* * Generation counter, when doing certain classes of graph walking, * to ensure that we check one node only once: */
@@ -140,6 +143,12 @@ struct lock_class_stats {#endif /* +* Lockdep class type flags +* 1) LOCKDEP_FLAG_NOVALIDATE: No full validation, just simple checks. +*/ +#define LOCKDEP_FLAG_NOVALIDATE (1 << 0) + +/* * Map the lock object (the lock instance) to the lock-class object. * This is embedded into specific lock instances: */
@@ -147,6 +156,7 @@ struct lockdep_map {struct lock_class_key *key;struct lock_class *class_cache[NR_LOCKDEP_CACHING_CLASSES ];const char *name; +unsigned int flags;#ifdef CONFIG_LOCK_STAT int cpu;unsigned long ip;
@@ -294,7 +304,8 @@ extern void lockdep_init_map(struct lockdep_map *lock, const char *name, (lock)->dep_map.key, sub)#define lockdep_set_novalidate_class (lock) \ - lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock) + do { (lock)->dep_map.flags |= LOCKDEP_FLAG_NOVALIDATE; } while (0) +/* * Compare locking classes */
kernel/locking/lockdep.c +7 -7
@@ -692,10 +692,11 @@ static int count_matching_names(struct lock_class *new_class)hlist_for_each_entry_rcu (class, hash_head, hash_entry) {if (class->key == key) {/* -* Huh! same key, different name? Did someone trample -* on some memory? We're most confused. +* Huh! same key, different name or flags? Did someone +* trample on some memory? We're most confused. */ -WARN_ON_ONCE (class->name != lock->name); +WARN_ON_ONCE ((class->name != lock->name) || + (class->flags != lock->flags));return class; } }
@@ -788,6 +789,7 @@ static bool assign_lock_key(struct lockdep_map *lock)debug_atomic_inc (nr_unused_locks); class->key = key; class->name = lock->name; + class->flags = lock->flags; class->subclass = subclass;INIT_LIST_HEAD (&class->lock_entry);INIT_LIST_HEAD (&class->locks_before);
@@ -3108,6 +3110,7 @@ static void __lockdep_init_map(struct lockdep_map *lock, const char *name,return ; } + lock->flags =0 ; lock->name = name;/*
@@ -3152,9 +3155,6 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name, }EXPORT_SYMBOL_GPL (lockdep_init_map); -struct lock_class_key __lockdep_no_validate__; -EXPORT_SYMBOL_GPL (__lockdep_no_validate__); - static int print_lock_nested_lock_not_held(struct task_struct *curr, struct held_lock *hlock,
@@ -3215,7 +3215,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,if (unlikely (!debug_locks))return 0 ; -if (!prove_locking || lock->key == &__lockdep_no_validate__) +if (!prove_locking || (lock->flags &LOCKDEP_FLAG_NOVALIDATE )) check =0 ;if (subclass <NR_LOCKDEP_CACHING_CLASSES )
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 3/17 locking/lockdep: Add a new terminal lock type
In reply to Waiman LongA terminal lock is a lock where further locking or unlocking on another lock is not allowed. IOW, no forward dependency is permitted.
With such a restriction in place, we don't really need to do a full validation of the lock chain involving a terminal lock. Instead, we just check if there is any further locking or unlocking on another lock when a terminal lock is being held.
Only spinlocks which are acquired by the _irq or _irqsave variants or in IRQ disabled context should be classified as terminal locks.
By adding this new lock type, we can save entries in lock_chains[], chain_hlocks[], list_entries[] and stack_trace[]. By marking suitable locks as terminal, we reduce the chance of overflowing those tables allowing them to focus on locks that can have both forward and backward dependencies.
Four bits are stolen from the pin_count of the held_lock structure to hold a new 4-bit flags field. The pin_count field is essentially a summation of 16-bit random cookie values. Removing 4 bits still allow the pin_count to accumulate up to almost 4096 of those cookie values.
--- include/linux/lockdep.h | 29 ++++++++++++++++++++--- kernel/locking/lockdep.c | 47 ++++++++++++++++++++++++++++++++------ kernel/locking/lockdep_internals.h | 5 ++++ kernel/locking/lockdep_proc.c | 11 +++++++-- 4 files changed, 80 insertions(+), 12 deletions(-)
include/linux/lockdep.h +26 -3
@@ -144,9 +144,20 @@ struct lock_class_stats {/* * Lockdep class type flags +* * 1) LOCKDEP_FLAG_NOVALIDATE: No full validation, just simple checks. +* 2) LOCKDEP_FLAG_TERMINAL: This is a terminal lock where lock/unlock on +* another lock within its critical section is not allowed. +* +* Only the least significant 4 bits of the flags will be copied to the +* held_lock structure. */ -#define LOCKDEP_FLAG_NOVALIDATE (1 << 0) +#define LOCKDEP_FLAG_TERMINAL (1 << 0) +#define LOCKDEP_FLAG_NOVALIDATE (1 << 4) + +#define LOCKDEP_HLOCK_FLAGS_MASK 0x0f +#define LOCKDEP_NOCHECK_FLAGS (LOCKDEP_FLAG_NOVALIDATE |\ + LOCKDEP_FLAG_TERMINAL)/* * Map the lock object (the lock instance) to the lock-class object.
@@ -263,7 +274,16 @@ struct held_lock { unsigned int check:1; /* see lock_acquire() comment */ unsigned int hardirqs_off:1; unsigned int references:12; /* 32 bits */ -unsigned int pin_count; +/* +* Four bits are stolen from pin_count for flags so as not to +* increase the size of the structure. The stolen bits may not +* be enough in the future as more flag bits are added. However, +* not all of them may need to be checked in the held_lock +* structure. We just have to make sure that the the relevant +* ones will be in the 4 least significant bits. +*/ + unsigned int pin_count:28; + unsigned int flags:4; };/*
@@ -305,6 +325,8 @@ extern void lockdep_init_map(struct lockdep_map *lock, const char *name,#define lockdep_set_novalidate_class (lock) \ do { (lock)->dep_map.flags |= LOCKDEP_FLAG_NOVALIDATE; } while (0) +#define lockdep_set_terminal_class (lock) \ + do { (lock)->dep_map.flags |= LOCKDEP_FLAG_TERMINAL; } while (0)/* * Compare locking classes
@@ -420,7 +442,8 @@ static inline void lockdep_on(void) do { (void)(key); } while (0)#define lockdep_set_subclass (lock, sub) do { } while (0) -#define lockdep_set_novalidate_class (lock) do { } while (0) +#define lockdep_set_novalidate_class (lock) do { } while (0) +#define lockdep_set_terminal_class (lock) do { } while (0)/* * We don't define lockdep_match_class() and lockdep_match_key() for !LOCKDEP
kernel/locking/lockdep.c +40 -7
115 lines not on this page · raw message
kernel/locking/lockdep_internals.h +5 -0
@@ -212,3 +212,8 @@ static inline unsigned long debug_class_ops_read(struct lock_class *class)# define debug_atomic_read (ptr) 0# define debug_class_ops_inc (ptr) do { } while (0)#endif + +static inline unsigned int flags_is_terminal (unsigned int flags) +{ +return flags &LOCKDEP_FLAG_TERMINAL ; +}
kernel/locking/lockdep_proc.c +9 -2
@@ -78,7 +78,10 @@ static int l_show(struct seq_file *m, void *v)get_usage_chars (class, usage);seq_printf (m," %s" , usage); -seq_printf (m,": " ); +/* +* Print terminal lock status +*/ +seq_printf (m,"%c: " ,flags_is_terminal (class->flags) ?'T' :' ' );print_name (m, class);seq_puts (m,"\n" );
@@ -208,7 +211,7 @@ static int lockdep_stats_show(struct seq_file *m, void *v) nr_irq_read_safe =0 , nr_irq_read_unsafe =0 , nr_softirq_read_safe =0 , nr_softirq_read_unsafe =0 , nr_hardirq_read_safe =0 , nr_hardirq_read_unsafe =0 , - sum_forward_deps =0 ; + nr_nocheck =0 , sum_forward_deps =0 ;list_for_each_entry (class, &all_lock_classes, lock_entry) {
@@ -240,6 +243,8 @@ static int lockdep_stats_show(struct seq_file *m, void *v) nr_hardirq_read_safe++;if (class->usage_mask &LOCKF_ENABLED_HARDIRQ_READ ) nr_hardirq_read_unsafe++; +if (class->flags &LOCKDEP_NOCHECK_FLAGS ) + nr_nocheck++;#ifdef CONFIG_PROVE_LOCKING sum_forward_deps +=lockdep_count_forward_deps (class);
@@ -318,6 +323,8 @@ static int lockdep_stats_show(struct seq_file *m, void *v) nr_uncategorized);seq_printf (m," unused locks: %11lu\n" , nr_unused); +seq_printf (m," unchecked locks: %11lu\n" , + nr_nocheck);seq_printf (m," max locking depth: %11u\n" , max_lockdep_depth);#ifdef CONFIG_PROVE_LOCKING
Signature · 1 line
1.8.3.1
Peter Zijlstra
maintainerRe: [PATCH v2 03/17] locking/lockdep: Add a new terminal lock type
Quoted 39 lines · Waiman Long
A terminal lock is a lock where further locking or unlocking on another lock is not allowed. IOW, no forward dependency is permitted.
With such a restriction in place, we don't really need to do a full validation of the lock chain involving a terminal lock. Instead, we just check if there is any further locking or unlocking on another lock when a terminal lock is being held.
Only spinlocks which are acquired by the
_irqor_irqsavevariants or in IRQ disabled context should be classified as terminal locks.By adding this new lock type, we can save entries in lock_chains[], chain_hlocks[], list_entries[] and stack_trace[]. By marking suitable locks as terminal, we reduce the chance of overflowing those tables allowing them to focus on locks that can have both forward and backward dependencies.
Four bits are stolen from the
pin_countof theheld_lockstructure to hold a new 4-bit flags field. Thepin_countfield is essentially a summation of 16-bit random cookie values. Removing 4 bits still allow thepin_countto accumulate up to almost 4096 of those cookie values.Signed-off-by Waiman Long <[email protected]>--- include/linux/lockdep.h | 29 ++++++++++++++++++++--- kernel/locking/lockdep.c | 47 ++++++++++++++++++++++++++++++++------ kernel/locking/lockdep_internals.h | 5 ++++ kernel/locking/lockdep_proc.c | 11 +++++++-- 4 files changed, 80 insertions(+), 12 deletions(-)include/linux/lockdep.h +1
-0@@ -144,9 +144,20 @@ struct lock_class_stats {/* * Lockdep class type flags +*
include/linux/lockdep.h struct lock_class_stats { · line 156
Quoted 10 lines
include/linux/lockdep.h +7
-1* 1) LOCKDEP_FLAG_NOVALIDATE: No full validation, just simple checks. +* 2) LOCKDEP_FLAG_TERMINAL: This is a terminal lock where lock/unlock on +* another lock within its critical section is not allowed. +* +* Only the least significant 4 bits of the flags will be copied to the +* held_lock structure. */ -#define LOCKDEP_FLAG_NOVALIDATE (1 << 0) +#define LOCKDEP_FLAG_TERMINAL (1 << 0) +#define LOCKDEP_FLAG_NOVALIDATE (1 << 4)
Just leave the novalidate thing along, then you don't get to do silly things like this.
Also; I have a pending patch (that I never quite finished) that tests lock nesting type (ie. raw_spinlock_t < spinlock_t < struct mutex) that wanted to use many of these same holes you took.
I think we can easily fit the lot together in bitfields though, since you really don't need that many flags.
I refreshed the below patch a number of months ago (no idea if it still applies, I think it was before Paul munged all of RCU). You need to kill printk and lift a few RT patches for the below to 'work' IIRC.
---
Subject: lockdep: Introduce wait-type checks
From: Peter Zijlstra <[email protected]>
Date: Tue, 19 Nov 2013 21:45:48 +0100
This patch extends 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 we do not try and acquire mutices while holding spinlocks, do not attempt 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 we can acquire RCU 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 we needed 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 we need 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 example:
raw_spin_lock(&foo); spin_lock(&bar); spin_unlock(&bar); raw_spin_unlock(&foo);
The way to read it is to look at the new -{n,m} part in the lock description; -{3:3} for our attempted lock, and try and match that up to the held locks, which in this case is the one: -{2,2}.
This tells us that the acquiring lock requires a more relaxed environment that 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 convertions can be done when desired.
--- include/linux/irqflags.h | 8 ++ include/linux/lockdep.h | 66 +++++++++++++++---- include/linux/mutex.h | 7 +- include/linux/rwlock_types.h | 6 + include/linux/rwsem.h | 6 + include/linux/sched.h | 1 include/linux/spinlock.h | 35 +++++++--- include/linux/spinlock_types.h | 24 +++++- kernel/irq/handle.c | 7 ++ kernel/locking/lockdep.c | 138 ++++++++++++++++++++++++++++++++++++---- kernel/locking/mutex-debug.c | 2 kernel/locking/rwsem-spinlock.c | 2 kernel/locking/rwsem-xadd.c | 2 kernel/locking/spinlock_debug.c | 6 - kernel/rcu/update.c | 24 +++++- 15 files changed, 280 insertions(+), 54 deletions(-)
include/linux/irqflags.h +7 -1
@@ -37,7 +37,12 @@# define trace_softirqs_enabled (p) ((p)->softirqs_enabled)# define trace_hardirq_enter () \ do { \ - current->hardirq_context++; \ + if (!current->hardirq_context++) \ + current->hardirq_threaded = 0; \ +} while (0) +# define trace_hardirq_threaded () \ +do { \ + current->hardirq_threaded = 1; \ } while (0)# define trace_hardirq_exit () \ do { \
@@ -59,6 +64,7 @@ do { \# define trace_hardirqs_enabled (p) 0# define trace_softirqs_enabled (p) 0# define trace_hardirq_enter () do { } while (0) +# define trace_hardirq_threaded () do { } while (0)# define trace_hardirq_exit () do { } while (0)# define lockdep_softirq_enter () do { } while (0)# define lockdep_softirq_exit () do { } while (0)
include/linux/lockdep.h +55 -11
105 lines not on this page · raw message
include/linux/mutex.h +5 -2
@@ -119,8 +119,11 @@ do { \ } while (0)#ifdef CONFIG_DEBUG_LOCK_ALLOC -# define __DEP_MAP_MUTEX_INITIALIZER (lockname) \ - , .dep_map = { .name = #lockname } +# define __DEP_MAP_MUTEX_INITIALIZER (lockname) \ + , .dep_map = { \ + .name = #lockname, \ + .wait_type_inner = LD_WAIT_SLEEP, \ + }#else # define __DEP_MAP_MUTEX_INITIALIZER (lockname)#endif
include/linux/rwlock_types.h +5 -1
@@ -22,7 +22,11 @@ typedef struct {#define RWLOCK_MAGIC 0xdeaf1eed#ifdef CONFIG_DEBUG_LOCK_ALLOC -# define RW_DEP_MAP_INIT (lockname) .dep_map = { .name = #lockname } +# define RW_DEP_MAP_INIT (lockname) \ + .dep_map = { \ + .name = #lockname, \ + .wait_type_inner = LD_WAIT_CONFIG, \ + }#else # define RW_DEP_MAP_INIT (lockname)#endif
include/linux/rwsem.h +5 -1
@@ -72,7 +72,11 @@ static inline int rwsem_is_locked(struct/* Common initializer macros and functions */ #ifdef CONFIG_DEBUG_LOCK_ALLOC -# define __RWSEM_DEP_MAP_INIT (lockname) , .dep_map = { .name = #lockname } +# define __RWSEM_DEP_MAP_INIT (lockname) \ + , .dep_map = { \ + .name = #lockname, \ + .wait_type_inner = LD_WAIT_SLEEP, \ + }#else # define __RWSEM_DEP_MAP_INIT (lockname)#endif
include/linux/sched.h +1 -0
@@ -920,6 +920,7 @@ struct task_struct {#ifdef CONFIG_TRACE_IRQFLAGS unsigned int irq_events; +unsigned int hardirq_threaded;unsigned long hardirq_enable_ip;unsigned long hardirq_disable_ip;unsigned int hardirq_enable_event;
include/linux/spinlock.h +25 -10
@@ -92,12 +92,13 @@#ifdef CONFIG_DEBUG_SPINLOCK extern void __raw_spin_lock_init (raw_spinlock_t *lock,const char *name, -struct lock_class_key *key); -# define raw_spin_lock_init (lock) \ -do { \ - static struct lock_class_key __key; \ - \ - __raw_spin_lock_init((lock), #lock, &__key); \ +struct lock_class_key *key,short inner); + +# define raw_spin_lock_init (lock) \ +do { \ + static struct lock_class_key __key; \ + \ + __raw_spin_lock_init((lock), #lock, &__key, LD_WAIT_SPIN); \ } while (0)#else
@@ -318,12 +319,26 @@ static __always_inline raw_spinlock_t *sreturn &lock->rlock; } -#define spin_lock_init (_lock) \ -do { \ - spinlock_check(_lock); \ - raw_spin_lock_init(&(_lock)->rlock); \ +#ifdef CONFIG_DEBUG_SPINLOCK + +# define spin_lock_init (lock) \ +do { \ + static struct lock_class_key __key; \ + \ + __raw_spin_lock_init(spinlock_check(lock), \ + #lock, &__key, LD_WAIT_CONFIG); \ +} while (0) + +#else + +# define spin_lock_init (_lock) \ +do { \ + spinlock_check(_lock); \ + *(lock) = __SPIN_LOCK_UNLOCKED(lock); \ } while (0) +#endif + static __always_inline void spin_lock(spinlock_t *lock) {raw_spin_lock (&lock->rlock);
include/linux/spinlock_types.h +20 -4
@@ -33,8 +33,18 @@ typedef struct raw_spinlock {#define SPINLOCK_OWNER_INIT ((void *)-1L)#ifdef CONFIG_DEBUG_LOCK_ALLOC -# define SPIN_DEP_MAP_INIT (lockname) .dep_map = { .name = #lockname } +# define RAW_SPIN_DEP_MAP_INIT (lockname) \ + .dep_map = { \ + .name = #lockname, \ + .wait_type_inner = LD_WAIT_SPIN, \ + } +# define SPIN_DEP_MAP_INIT (lockname) \ + .dep_map = { \ + .name = #lockname, \ + .wait_type_inner = LD_WAIT_CONFIG, \ + }#else +# define RAW_SPIN_DEP_MAP_INIT (lockname)# define SPIN_DEP_MAP_INIT (lockname)#endif
@@ -51,7 +61,7 @@ typedef struct raw_spinlock { { \ .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \ SPIN_DEBUG_INIT(lockname) \ - SPIN_DEP_MAP_INIT(lockname) } + RAW_SPIN_DEP_MAP_INIT(lockname) }#define __RAW_SPIN_LOCK_UNLOCKED (lockname) \ (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
@@ -72,11 +82,17 @@ typedef struct spinlock { }; } spinlock_t; +#define ___SPIN_LOCK_INITIALIZER (lockname) \ + { \ + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \ + SPIN_DEBUG_INIT(lockname) \ + SPIN_DEP_MAP_INIT(lockname) } +#define __SPIN_LOCK_INITIALIZER (lockname) \ - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } } + { { .rlock = ___SPIN_LOCK_INITIALIZER(lockname) } }#define __SPIN_LOCK_UNLOCKED (lockname) \ - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname) + (spinlock_t) __SPIN_LOCK_INITIALIZER(lockname)#define DEFINE_SPINLOCK (x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
kernel/irq/handle.c +7 -0
@@ -145,6 +145,13 @@ irqreturn_t __handle_irq_event_percpu(stfor_each_action_of_desc (desc, action) {irqreturn_t res; +/* +* If this IRQ would be threaded under force_irqthreads, mark it so. +*/ +if (irq_settings_can_thread (desc) && + !(action->flags & (IRQF_NO_THREAD |IRQF_PERCPU |IRQF_ONESHOT ))) +trace_hardirq_threaded (); +trace_irq_handler_entry (irq, action); res = action->handler (irq, action->dev_id);trace_irq_handler_exit (irq, action, res);
kernel/locking/lockdep.c +126 -12
192 lines not on this page · raw message
kernel/locking/mutex-debug.c +1 -1
@@ -85,7 +85,7 @@ void debug_mutex_init(struct mutex *lock* Make sure we are not reinitializing a held lock: */ debug_check_no_locks_freed ((void *)lock,sizeof (*lock)); -lockdep_init_map (&lock->dep_map, name, key,0 ); +lockdep_init_map_wait (&lock->dep_map, name, key,0 ,LD_WAIT_SLEEP );#endif lock->magic = lock; }
kernel/locking/rwsem-spinlock.c +1 -1
@@ -46,7 +46,7 @@ void __init_rwsem(struct rw_semaphore *s* Make sure we are not reinitializing a held semaphore: */ debug_check_no_locks_freed ((void *)sem,sizeof (*sem)); -lockdep_init_map (&sem->dep_map, name, key,0 ); +lockdep_init_map_wait (&sem->dep_map, name, key,0 ,LD_WAIT_SLEEP );#endif sem->count =0 ;raw_spin_lock_init (&sem->wait_lock);
kernel/locking/rwsem-xadd.c +1 -1
@@ -81,7 +81,7 @@ void __init_rwsem(struct rw_semaphore *s* Make sure we are not reinitializing a held semaphore: */ debug_check_no_locks_freed ((void *)sem,sizeof (*sem)); -lockdep_init_map (&sem->dep_map, name, key,0 ); +lockdep_init_map_wait (&sem->dep_map, name, key,0 ,LD_WAIT_SLEEP );#endif atomic_long_set (&sem->count,RWSEM_UNLOCKED_VALUE );raw_spin_lock_init (&sem->wait_lock);
kernel/locking/spinlock_debug.c +3 -3
@@ -14,14 +14,14 @@#include <linux/export.h> void __raw_spin_lock_init (raw_spinlock_t *lock,const char *name, -struct lock_class_key *key) +struct lock_class_key *key,short inner) {#ifdef CONFIG_DEBUG_LOCK_ALLOC /* * Make sure we are not reinitializing a held lock: */ debug_check_no_locks_freed ((void *)lock,sizeof (*lock)); -lockdep_init_map (&lock->dep_map, name, key,0 ); +lockdep_init_map_wait (&lock->dep_map, name, key,0 , inner);#endif lock->raw_lock = (arch_spinlock_t )__ARCH_SPIN_LOCK_UNLOCKED; lock->magic =SPINLOCK_MAGIC ;
@@ -39,7 +39,7 @@ void __rwlock_init(rwlock_t *lock, const* Make sure we are not reinitializing a held lock: */ debug_check_no_locks_freed ((void *)lock,sizeof (*lock)); -lockdep_init_map (&lock->dep_map, name, key,0 ); +lockdep_init_map_wait (&lock->dep_map, name, key,0 ,LD_WAIT_CONFIG );#endif lock->raw_lock = (arch_rwlock_t ) __ARCH_RW_LOCK_UNLOCKED; lock->magic =RWLOCK_MAGIC ;
kernel/rcu/update.c +18 -6
@@ -228,18 +228,30 @@ core_initcall(rcu_set_runtime_mode);#ifdef CONFIG_DEBUG_LOCK_ALLOC static struct lock_class_key rcu_lock_key; -struct lockdep_map rcu_lock_map = -STATIC_LOCKDEP_MAP_INIT ("rcu_read_lock" , &rcu_lock_key); +struct lockdep_map rcu_lock_map = { + .name ="rcu_read_lock" , + .key = &rcu_lock_key, + .wait_type_outer =LD_WAIT_FREE , + .wait_type_inner =LD_WAIT_CONFIG ,/* XXX PREEMPT_RCU ? */ +};EXPORT_SYMBOL_GPL (rcu_lock_map);static struct lock_class_key rcu_bh_lock_key; -struct lockdep_map rcu_bh_lock_map = -STATIC_LOCKDEP_MAP_INIT ("rcu_read_lock_bh" , &rcu_bh_lock_key); +struct lockdep_map rcu_bh_lock_map = { + .name ="rcu_read_lock_bh" , + .key = &rcu_bh_lock_key, + .wait_type_outer =LD_WAIT_FREE , + .wait_type_inner =LD_WAIT_CONFIG ,/* PREEMPT_LOCK also makes BH preemptible */ +};EXPORT_SYMBOL_GPL (rcu_bh_lock_map);static struct lock_class_key rcu_sched_lock_key; -struct lockdep_map rcu_sched_lock_map = -STATIC_LOCKDEP_MAP_INIT ("rcu_read_lock_sched" , &rcu_sched_lock_key); +struct lockdep_map rcu_sched_lock_map = { + .name ="rcu_read_lock_sched" , + .key = &rcu_sched_lock_key, + .wait_type_outer =LD_WAIT_FREE , + .wait_type_inner =LD_WAIT_SPIN , +};EXPORT_SYMBOL_GPL (rcu_sched_lock_map);static struct lock_class_key rcu_callback_key;
Waiman Long
reviewerPATCH v2 4/17 locking/lockdep: Add DEFINE_TERMINAL_SPINLOCK() and related macros
In reply to Waiman LongAdd new DEFINE_RAW_TERMINAL_SPINLOCK() and DEFINE_TERMINAL_SPINLOCK() macro to define a raw terminal spinlock and a terminal spinlock.
--- include/linux/spinlock_types.h | 34 +++++++++++++++++++++++----------- kernel/printk/printk_safe.c | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-)
include/linux/spinlock_types.h +23 -11
@@ -33,9 +33,10 @@#define SPINLOCK_OWNER_INIT ((void *)-1L)#ifdef CONFIG_DEBUG_LOCK_ALLOC -# define SPIN_DEP_MAP_INIT (lockname) .dep_map = { .name = #lockname } +# define SPIN_DEP_MAP_INIT (lockname, f) .dep_map = { .name = #lockname, \ + .flags = f }#else -# define SPIN_DEP_MAP_INIT (lockname) +# define SPIN_DEP_MAP_INIT (lockname, f)#endif #ifdef CONFIG_DEBUG_SPINLOCK
@@ -47,16 +48,22 @@# define SPIN_DEBUG_INIT (lockname)#endif -#define __RAW_SPIN_LOCK_INITIALIZER (lockname) \ - { \ - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \ - SPIN_DEBUG_INIT(lockname) \ - SPIN_DEP_MAP_INIT(lockname) } +#define __RAW_SPIN_LOCK_INITIALIZER (lockname, f) \ + { \ + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \ + SPIN_DEBUG_INIT(lockname) \ + SPIN_DEP_MAP_INIT(lockname, f) }#define __RAW_SPIN_LOCK_UNLOCKED (lockname) \ - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname) + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname, 0) + +#define __RAW_TERMINAL_SPIN_LOCK_UNLOCKED (lockname) \ + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname, \ + LOCKDEP_FLAG_TERMINAL)#define DEFINE_RAW_SPINLOCK (x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x) +#define DEFINE_RAW_TERMINAL_SPINLOCK (x) \ + raw_spinlock_t x = __RAW_TERMINAL_SPIN_LOCK_UNLOCKED(x) typedef struct spinlock { union {
@@ -72,13 +79,18 @@ }; } spinlock_t; -#define __SPIN_LOCK_INITIALIZER (lockname) \ - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } } +#define __SPIN_LOCK_INITIALIZER (lockname, f) \ + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname, f) } }#define __SPIN_LOCK_UNLOCKED (lockname) \ - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname) + (spinlock_t) __SPIN_LOCK_INITIALIZER(lockname, 0) + +#define __TERMINAL_SPIN_LOCK_UNLOCKED (lockname) \ + (spinlock_t) __SPIN_LOCK_INITIALIZER(lockname, LOCKDEP_FLAG_TERMINAL)#define DEFINE_SPINLOCK (x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x) +#define DEFINE_TERMINAL_SPINLOCK (x) \ + spinlock_t x = __TERMINAL_SPIN_LOCK_UNLOCKED(x)#include <linux/rwlock_types.h>
kernel/printk/printk_safe.c +1 -1
@@ -192,7 +192,7 @@ static void report_message_lost(struct printk_safe_seq_buf *s)static void __printk_safe_flush (struct irq_work *work) {static raw_spinlock_t read_lock = -__RAW_SPIN_LOCK_INITIALIZER (read_lock); +__RAW_SPIN_LOCK_UNLOCKED (read_lock);struct printk_safe_seq_buf *s = container_of(work, struct printk_safe_seq_buf, work);unsigned long flags;
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 5/17 printk: Mark logbuf_lock & console_owner_lock as terminal locks
In reply to Waiman LongBy marking logbuf_lock and console_owner_lock as terminal locks, it reduces the performance overhead when those locks are used with lockdep enabled.
--- kernel/printk/printk.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/printk/printk.c +2 -2
@@ -367,7 +367,7 @@ __packed __aligned(4)* within the scheduler's rq lock. It must be released before calling * console_unlock() or anything else that might wake up a process. */ -DEFINE_RAW_SPINLOCK (logbuf_lock); +DEFINE_RAW_TERMINAL_SPINLOCK (logbuf_lock);/* * Helper macros to lock/unlock logbuf_lock and switch between
@@ -1568,7 +1568,7 @@ int do_syslog(int type, char __user *buf, int len, int source) };#endif -static DEFINE_RAW_SPINLOCK (console_owner_lock); +static DEFINE_RAW_TERMINAL_SPINLOCK (console_owner_lock);static struct task_struct *console_owner;static bool console_waiter;
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 6/17 debugobjects: Mark pool_lock as a terminal lock
In reply to Waiman LongBy marking the internal pool_lock as a terminal lock, lockdep will be able to skip full validation to improve locking performance.
--- lib/debugobjects.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/debugobjects.c +1 -1
@@ -39,7 +39,7 @@ struct debug_bucket {static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE ]__initdata ; -static DEFINE_RAW_SPINLOCK (pool_lock); +static DEFINE_RAW_TERMINAL_SPINLOCK (pool_lock);static HLIST_HEAD (obj_pool);static HLIST_HEAD (obj_to_free);
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 7/17 debugobjects: Move printk out of db lock critical sections
In reply to Waiman LongThe db->lock is a raw spinlock and so the lock hold time is supposed to be short. This will not be the case when printk() is being involved in some of the critical sections. In order to avoid the long hold time, in case some messages need to be printed, the debug_object_is_on_stack() and debug_print_object() calls are now moved out of those critical sections.
--- lib/debugobjects.c | 61 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 19 deletions(-)
lib/debugobjects.c +42 -19
@@ -376,6 +376,8 @@ static void debug_object_is_on_stack(void *addr, int onstack)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false; +bool debug_chkstack = false;fill_pool ();
@@ -392,7 +394,7 @@ static void debug_object_is_on_stack(void *addr, int onstack)debug_objects_oom ();return ; } -debug_object_is_on_stack (addr, onstack); + debug_chkstack = true; }switch (obj->state) {
@@ -403,20 +405,25 @@ static void debug_object_is_on_stack(void *addr, int onstack)break ;case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" ); + }/**
@@ -474,6 +481,8 @@ int debug_object_activate(void *addr, struct debug_obj_descr *descr) obj =lookup_object (addr, db);if (obj) { +bool debug_printobj = false; +switch (obj->state) {case ODEBUG_STATE_INIT :case ODEBUG_STATE_INACTIVE :
@@ -482,14 +491,14 @@ int debug_object_activate(void *addr, struct debug_obj_descr *descr)break ;case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"activate" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"activate" ); ret =debug_object_fixup (descr->fixup_activate, addr, state);return ret ?0 : -EINVAL ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"activate" ); + debug_printobj = true; ret = -EINVAL ;break ;default :
@@ -497,10 +506,13 @@ int debug_object_activate(void *addr, struct debug_obj_descr *descr)break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_printobj) +debug_print_object (obj,"activate" );return ret; }raw_spin_unlock_irqrestore (&db->lock, flags); +/* * We are here when a static object is activated. We * let the type specific code confirm whether this is
@@ -532,6 +544,7 @@ void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false;if (!debug_objects_enabled)return ;
@@ -549,24 +562,27 @@ void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)if (!obj->astate) obj->state =ODEBUG_STATE_INACTIVE ;else -debug_print_object (obj,"deactivate" ); + debug_printobj = true;break ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"deactivate" ); + debug_printobj = true;break ;default :break ; } - }else { + } + +raw_spin_unlock_irqrestore (&db->lock, flags); +if (!obj) {struct debug_obj o = { .object = addr, .state =ODEBUG_STATE_NOTAVAILABLE , .descr = descr };debug_print_object (&o,"deactivate" ); + }else if (debug_printobj) { +debug_print_object (obj,"deactivate" ); } - -raw_spin_unlock_irqrestore (&db->lock, flags); }EXPORT_SYMBOL_GPL (debug_object_deactivate);
@@ -581,6 +597,7 @@ void debug_object_destroy(void *addr, struct debug_obj_descr *descr)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false;if (!debug_objects_enabled)return ;
@@ -600,20 +617,22 @@ void debug_object_destroy(void *addr, struct debug_obj_descr *descr) obj->state =ODEBUG_STATE_DESTROYED ;break ;case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"destroy" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"destroy" );debug_object_fixup (descr->fixup_destroy, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"destroy" ); + debug_printobj = true;break ;default :break ; } out_unlock:raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_printobj) +debug_print_object (obj,"destroy" ); }EXPORT_SYMBOL_GPL (debug_object_destroy);
@@ -642,9 +661,9 @@ void debug_object_free(void *addr, struct debug_obj_descr *descr)switch (obj->state) {case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"free" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"free" );debug_object_fixup (descr->fixup_free, addr, state);return ;default :
@@ -717,6 +736,7 @@ void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false;if (!debug_objects_enabled)return ;
@@ -732,22 +752,25 @@ void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)if (obj->astate == expect) obj->astate = next;else -debug_print_object (obj,"active_state" ); + debug_printobj = true;break ;default : -debug_print_object (obj,"active_state" ); + debug_printobj = true;break ; } - }else { + } + +raw_spin_unlock_irqrestore (&db->lock, flags); +if (!obj) {struct debug_obj o = { .object = addr, .state =ODEBUG_STATE_NOTAVAILABLE , .descr = descr };debug_print_object (&o,"active_state" ); + }else if (debug_printobj) { +debug_print_object (obj,"active_state" ); } - -raw_spin_unlock_irqrestore (&db->lock, flags); }EXPORT_SYMBOL_GPL (debug_object_active_state);
@@ -783,10 +806,10 @@ static void __debug_check_no_obj_freed(const void *address, unsigned long size)switch (obj->state) {case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"free" ); descr = obj->descr; state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"free" );debug_object_fixup (descr->fixup_free, (void *) oaddr, state); goto repeat;
Signature · 1 line
1.8.3.1
Waiman Long
reviewerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 228 lines · Waiman Long
The
db->lockis a raw spinlock and so the lock hold time is supposed to be short. This will not be the case whenprintk()is being involved in some of the critical sections. In order to avoid the long hold time, in case some messages need to be printed, thedebug_object_is_on_stack()anddebug_print_object()calls are now moved out of those critical sections.Signed-off-by Waiman Long <[email protected]>--- lib/debugobjects.c | 61 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 19 deletions(-)lib/debugobjects.c +41
-18@@ -376,6 +376,8 @@ static void debug_object_is_on_stack(void *addr, int onstack)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false; +bool debug_chkstack = false;fill_pool ();@@ -392,7 +394,7 @@ static void debug_object_is_on_stack(void *addr, int onstack)debug_objects_oom ();return ; } -debug_object_is_on_stack (addr, onstack); + debug_chkstack = true; }switch (obj->state) {@@ -403,20 +405,25 @@ static void debug_object_is_on_stack(void *addr, int onstack)break ;case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" ); + }/** @@ -474,6 +481,8 @@ int debug_object_activate(void *addr, struct debug_obj_descr *descr) obj =lookup_object (addr, db);if (obj) { +bool debug_printobj = false; +switch (obj->state) {case ODEBUG_STATE_INIT :case ODEBUG_STATE_INACTIVE :@@ -482,14 +491,14 @@ int debug_object_activate(void *addr, struct debug_obj_descr *descr)break ;case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"activate" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"activate" ); ret =debug_object_fixup (descr->fixup_activate, addr, state);return ret ?0 : -EINVAL ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"activate" ); + debug_printobj = true; ret = -EINVAL ;break ;default :@@ -497,10 +506,13 @@ int debug_object_activate(void *addr, struct debug_obj_descr *descr)break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_printobj) +debug_print_object (obj,"activate" );return ret; }raw_spin_unlock_irqrestore (&db->lock, flags); +/* * We are here when a static object is activated. We * let the type specific code confirm whether this is @@ -532,6 +544,7 @@ void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false;if (!debug_objects_enabled)return ;@@ -549,24 +562,27 @@ void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)if (!obj->astate) obj->state =ODEBUG_STATE_INACTIVE ;else -debug_print_object (obj,"deactivate" ); + debug_printobj = true;break ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"deactivate" ); + debug_printobj = true;break ;default :break ; } - }else { + } + +raw_spin_unlock_irqrestore (&db->lock, flags); +if (!obj) {struct debug_obj o = { .object = addr, .state =ODEBUG_STATE_NOTAVAILABLE , .descr = descr };debug_print_object (&o,"deactivate" ); + }else if (debug_printobj) { +debug_print_object (obj,"deactivate" ); } - -raw_spin_unlock_irqrestore (&db->lock, flags); }EXPORT_SYMBOL_GPL (debug_object_deactivate);@@ -581,6 +597,7 @@ void debug_object_destroy(void *addr, struct debug_obj_descr *descr)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false;if (!debug_objects_enabled)return ;@@ -600,20 +617,22 @@ void debug_object_destroy(void *addr, struct debug_obj_descr *descr) obj->state =ODEBUG_STATE_DESTROYED ;break ;case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"destroy" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"destroy" );debug_object_fixup (descr->fixup_destroy, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"destroy" ); + debug_printobj = true;break ;default :break ; } out_unlock:raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_printobj) +debug_print_object (obj,"destroy" ); }EXPORT_SYMBOL_GPL (debug_object_destroy);@@ -642,9 +661,9 @@ void debug_object_free(void *addr, struct debug_obj_descr *descr)switch (obj->state) {case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"free" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"free" );debug_object_fixup (descr->fixup_free, addr, state);return ;default :@@ -717,6 +736,7 @@ void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)struct debug_bucket *db;struct debug_obj *obj;unsigned long flags; +bool debug_printobj = false;if (!debug_objects_enabled)return ;@@ -732,22 +752,25 @@ void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)if (obj->astate == expect) obj->astate = next;else -debug_print_object (obj,"active_state" ); + debug_printobj = true;break ;default : -debug_print_object (obj,"active_state" ); + debug_printobj = true;break ; } - }else { + } + +raw_spin_unlock_irqrestore (&db->lock, flags); +if (!obj) {struct debug_obj o = { .object = addr, .state =ODEBUG_STATE_NOTAVAILABLE , .descr = descr };debug_print_object (&o,"active_state" ); + }else if (debug_printobj) { +debug_print_object (obj,"active_state" ); } - -raw_spin_unlock_irqrestore (&db->lock, flags); }EXPORT_SYMBOL_GPL (debug_object_active_state);@@ -783,10 +806,10 @@ static void __debug_check_no_obj_freed(const void *address, unsigned long size)
lib/debugobjects.c static void __debug_check_no_obj_freed(const void *address, unsigned long size) · line 815
Quoted 10 lines
lib/debugobjects.c +1
-1switch (obj->state) {case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"free" ); descr = obj->descr; state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"free" );debug_object_fixup (descr->fixup_free, (void *) oaddr, state); goto repeat;
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call printk() while holding a raw spinlock.
Cheers,
Longman
Re: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
On (11/21/18 11:49), Waiman Long wrote:
[..]
Quoted 22 lines
Quoted 22 lines
lib/debugobjects.c +6
-2case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" );
[..]
Quoted 6 lines
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call
printk()while holding a raw spinlock.
Right, I like this patch.
And I think that we, maybe, can go even further.
Some serial consoles call mod_timer(). So what we could have with the debug objects enabled was
mod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlock
That's one possible scenario. The other one can involve console's IRQ handler, uart port spinlock, mod_timer, debug objects, printk, and an eventual deadlock on the uart port spinlock. This one can be mitigated with printk_safe. But mod_timer() deadlock will require a different fix.
So maybe we need to switch debug objects print-outs to _always_ printk_deferred(). Debug objects can be used in code which cannot do direct printk() - timekeeping is just one example.
-ss
Peter Zijlstra
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 18 lines · Sergey Senozhatsky
Some serial consoles call
mod_timer(). So what we could have with the debug objects enabled wasmod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlockThat's one possible scenario. The other one can involve console's IRQ handler, uart port spinlock,
mod_timer, debug objects, printk, and an eventual deadlock on the uart port spinlock. This one can be mitigated withprintk_safe. Butmod_timer()deadlock will require a different fix.
Quoted 3 lines
So maybe we need to switch debug objects print-outs to _always_
printk_deferred(). Debug objects can be used in code which cannot do directprintk()- timekeeping is just one example.
No, printk_deferred() is a disease, it needs to be eradicated, not spread around.
Re: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines
So maybe we need to switch debug objects print-outs to _always_
printk_deferred(). Debug objects can be used in code which cannot do directprintk()- timekeeping is just one example.No,
printk_deferred()is a disease, it needs to be eradicated, not spread around.
deadlock-free printk() is deferred, but OK.
Another idea then:
---
lib/debugobjects.c +3 -0
@@ -323,10 +323,13 @@ static void debug_print_object(struct debug_obj *obj, char *msg)void *hint = descr->debug_hint ? descr->debug_hint (obj->object) :NULL ; limit++; + +bust_spinlocks (1 );WARN (1 ,KERN_ERR "ODEBUG: %s %s (active state %u) " "object type: %s hint: %pS\n" , msg, obj_states[obj->state], obj->astate, descr->name, hint); +bust_spinlocks (0 ); } debug_objects_warnings++; }
---
This should make serial consoles re-entrant.
So printk->console_driver_write() hopefully will not deadlock.
IOW, this turns serial consoles into early_consoles, for a while ;)
-ss
Petr Mladek
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 9 lines · Sergey Senozhatsky
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines
So maybe we need to switch debug objects print-outs to _always_
printk_deferred(). Debug objects can be used in code which cannot do directprintk()- timekeeping is just one example.No,
printk_deferred()is a disease, it needs to be eradicated, not spread around.deadlock-free
printk()is deferred, but OK.
The best solution would be lockless console drivers. Sigh.
Quoted 25 lines
Another idea then:
---
lib/debugobjects.c +3
-1@@ -323,10 +323,13 @@ static void debug_print_object(struct debug_obj *obj, char *msg)void *hint = descr->debug_hint ? descr->debug_hint (obj->object) :NULL ; limit++; + +bust_spinlocks (1 );WARN (1 ,KERN_ERR "ODEBUG: %s %s (active state %u) " "object type: %s hint: %pS\n" , msg, obj_states[obj->state], obj->astate, descr->name, hint); +bust_spinlocks (0 ); } debug_objects_warnings++; } ---
Quoted 2 lines
This should make serial consoles re-entrant.
Soprintk->console_driver_write()hopefully will not deadlock.
Is the re-entrance safe? Some risk might be acceptable in Oops/panic situations. It is much less acceptable for random warnings.
Best Regards,
Petr
Re: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
On (11/23/18 12:48), Petr Mladek wrote:
[..]
Quoted 5 lines
Quoted 2 lines
This should make serial consoles re-entrant.
Soprintk->console_driver_write()hopefully will not deadlock.Is the re-entrance safe? Some risk might be acceptable in Oops/panic situations. It is much less acceptable for random warnings.
Good question.
But what's the alternative? A deadlock in a serial console driver; such that even panic() is not guaranteed to make through it (at least of now). debug objects are used from the code which cannot re-entrant console drivers.
bust_spinlock is called from various paths, not only panic. git grep bust_spinlocks | wc -l
62
So we already switch to re-entrant consoles (and accept the risks) in mm/fault.c, kernel/traps.c and so on. Which, I guess, makes us a little more confident, faults/traps happen often enough.
It seems, that, more or less, serial consoles are ready to handle it. UART consoles in ->write() callbacks just do a bunch of writel() [for every char + \r\n].
-ss
Petr Mladek
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 10 lines · Sergey Senozhatsky
On (11/23/18 12:48), Petr Mladek wrote:
[..]Quoted 5 lines
Quoted 2 lines
This should make serial consoles re-entrant.
Soprintk->console_driver_write()hopefully will not deadlock.Is the re-entrance safe? Some risk might be acceptable in Oops/panic situations. It is much less acceptable for random warnings.
Good question.
Quoted 8 lines
But what's the alternative? A deadlock in a serial console driver; such that even
panic()is not guaranteed to make through it (at least of now). debug objects are used from the code which cannot re-entrant console drivers.
bust_spinlockis called from various paths, not only panic. git grepbust_spinlocks| wc -l
62
bust_spinlocks() is followed by die() in several situations. The rests seems to be Oops situations where we an invalid address is being accessed. There is a nontrivial chance that the system would die anyway.
Now, if I look into Documentation/core-api/debug-objects.rst, the API is used to detect:
- Activation of uninitialized objects - Initialization of active objects - Usage of freed/destroyed objects
Of course, all the above situations might lead to the system crash. But even in the worst case, use-after-free, there is a non-trivial chance that the data still would be valid and the system would survive.
There might be many other warnings of the same severity.
Quoted 3 lines
So we already switch to re-entrant consoles (and accept the risks) in
mm/fault.c,kernel/traps.cand so on. Which, I guess, makes us a little more confident, faults/traps happen often enough.
Where is the border line, please?
Do we want to have the kernel sources full of bust_spinlocks() callers?
Quoted 3 lines
It seems, that, more or less, serial consoles are ready to handle it. UART consoles in ->write() callbacks just do a bunch of
writel()[for every char + \r\n].
But oops_in_progress does not affect only serial (UART) consoles.
We want safe lockless consoles. We do not want to run a most-of-the-time-safe code too often.
BTW: I have heard that someone from the RT people is working on a big printk() rewrite. One part is a lockless buffer. Another part should be a different handling of safe (lockless) and more complicated consoles. It was presented on some recent conference (I forgot which one). I do not know any details. But the first version should be sent in a near future.
Best Regards,
Petr
Petr Mladek
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
In reply to Sergey SenozhatskyQuoted 18 lines · Sergey Senozhatsky
On (11/21/18 11:49), Waiman Long wrote:
[..]Quoted 16 lines
Quoted 16 lines
lib/debugobjects.c +2
-2case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }
Quoted 13 lines
Quoted 6 lines
Quoted 6 lines
lib/debugobjects.c +4
-0raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" );[..]
Quoted 6 lines
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call
printk()while holding a raw spinlock.
Please, was the system hang reproducible? I wonder if it was a deadlock described by Sergey below.
The commit message is right. printk() might take too long and cause softlockup or livelock. But it does not explain why the system could competely hang.
Also note that prinkt() should not longer block a single process indefinitely thanks to the commit dbdda842fe96f8932 ("printk:
Add console owner and waiter logic to load balance console writes").
Quoted 11 lines
Some serial consoles call
mod_timer(). So what we could have with the debug objects enabled wasmod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlock
Anyway, I wonder what was the primary motivation for this patch. Was it the system hang? Or was it lockdep report about nesting two terminal locks: db->lock, pool_lock with logbuf_lock?
Printk is problematic. It might delay any atomic context where it is used. I would like to better understand the problem here before we start spread printk-related hacks.
Best Regards,
Petr
Waiman Long
reviewerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 26 lines · Petr Mladek
Quoted 25 lines · Sergey Senozhatsky
On (11/21/18 11:49), Waiman Long wrote:
[..]Quoted 22 lines
Quoted 22 lines
lib/debugobjects.c +6
-2case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" );[..]
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call
printk()while holding a raw spinlock.Please, was the system hang reproducible? I wonder if it was a deadlock described by Sergey below.
Yes, it is 100% reproducible on the testing system that I used.
Quoted 7 lines
The commit message is right.
printk()might take too long and cause softlockup or livelock. But it does not explain why the system could competely hang.Also note that
prinkt()should not longer block a single process indefinitely thanks to the commitdbdda842fe96f8932("printk:
Add console owner and waiter logic to load balance console writes").
The problem might have been caused by the fact that IRQ was also disabled in the lock critical section.
Quoted 11 lines
Quoted 11 lines
Some serial consoles call
mod_timer(). So what we could have with the debug objects enabled wasmod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlock
Quoted 3 lines
Anyway, I wonder what was the primary motivation for this patch. Was it the system hang? Or was it lockdep report about nesting two terminal locks:
db->lock,pool_lockwithlogbuf_lock?
The primary motivation was to make sure that printk() won't be called while holding either db->lock or pool_lock in the debugobjects code. In the determination of which locks can be made terminal, I focused on local spinlocks that won't cause boundary to an unrelated subsystem as it will greatly complicate the analysis.
I didn't realize that it fixed a hang problem that I was seeing until I did bisection to find out that it was caused by the patch that cause the debugobjects splat in the first place a few days ago. I was comparing the performance status of the pre and post patched kernel. The pre-patch kernel failed to boot in the one of my test systems, but the post-patched kernel could. I narrowed it down to this patch as the fix for the hang problem.
Cheers,
Longman
Petr Mladek
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 10 lines · Waiman Long
Quoted 3 lines · Petr Mladek
Anyway, I wonder what was the primary motivation for this patch. Was it the system hang? Or was it lockdep report about nesting two terminal locks:
db->lock,pool_lockwithlogbuf_lock?The primary motivation was to make sure that
printk()won't be called while holding eitherdb->lockorpool_lockin the debugobjects code. In the determination of which locks can be made terminal, I focused on local spinlocks that won't cause boundary to an unrelated subsystem as it will greatly complicate the analysis.
Then please mention this as the real reason in the commit message.
The reason that printk might take too long in IRQ context does not explain why we need the patch. printk() is called in IRQ context in many other locations. I do not see why this place should be special. The delay is the price for the chance to see the problem.
Quoted 7 lines
I didn't realize that it fixed a hang problem that I was seeing until I did bisection to find out that it was caused by the patch that cause the debugobjects splat in the first place a few days ago. I was comparing the performance status of the pre and post patched kernel. The pre-patch kernel failed to boot in the one of my test systems, but the post-patched kernel could. I narrowed it down to this patch as the fix for the hang problem.
The hang is a mystery. My guess is that there is a race somewhere. The patch might have reduced the race window but it probably did not fix the race itself.
Best Regards,
Petr
Waiman Long
reviewerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
In reply to Sergey SenozhatskyQuoted 51 lines · Sergey Senozhatsky
On (11/21/18 11:49), Waiman Long wrote:
[..]Quoted 22 lines
Quoted 22 lines
lib/debugobjects.c +6
-2case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" );[..]
Quoted 5 lines
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call
printk()while holding a raw spinlock.Right, I like this patch.
And I think that we, maybe, can go even further.Some serial consoles call
mod_timer(). So what we could have with the debug objects enabled wasmod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlockThat's one possible scenario. The other one can involve console's IRQ handler, uart port spinlock,
mod_timer, debug objects, printk, and an eventual deadlock on the uart port spinlock. This one can be mitigated withprintk_safe. Butmod_timer()deadlock will require a different fix.
Quoted 5 lines
So maybe we need to switch debug objects print-outs to _always_
printk_deferred(). Debug objects can be used in code which cannot do directprintk()- timekeeping is just one example.-ss
Actually, I don't think that was the cause of the hang. The debugobjects splat was caused by debug_object_is_on_stack(), below was the output:
[ 6.890048] ODEBUG: object (____ptrval____) is NOT on stack (____ptrval____), but annotated.
[ 6.891000] WARNING: CPU: 28 PID: 1 at lib/debugobjects.c:369 __debug_object_init.cold.11+0x51/0x2d6
[ 6.891000] Modules linked in:
[ 6.891000] CPU: 28 PID: 1 Comm: swapper/0 Not tainted 4.18.0-41.el8.bz1651764_cgroup_debug.x86_64+debug #1
[ 6.891000] Hardware name: HPE ProLiant DL120 Gen10/ProLiant DL120 Gen10, BIOS U36 11/14/2017
[ 6.891000] RIP: 0010:__debug_object_init.cold.11+0x51/0x2d6 [ 6.891000] Code: ea 03 80 3c 02 00 0f 85 85 02 00 00 49 8b 54 24 18 48 89 de 4c 89 44 24 10 48 c7 c7 00 ce 22 94 e8 73 18 62 ff 4c 8b 44 24 10 <0f> 0b e9 60 db ff ff 41 83 c4 01 b8 ff ff 37 00 44 89 25 ce 46 f9 [ 6.891000] RSP: 0000:ffff880104187960 EFLAGS: 00010086
[ 6.891000] RAX: 0000000000000050 RBX: ffffffff9764c570 RCX:
0000000000000000
[ 6.891000] RDX: 0000000000000000 RSI: 0000000000000000 RDI:ffff880104178ca8
[ 6.891000] RBP: 1ffff10020830f34 R08: ffff8807ce68a1d0 R09:fffffbfff2923554
[ 6.891000] R10: fffffbfff2923554 R11: ffffffff9491aaa3 R12:ffff880104178000
[ 6.891000] R13: ffffffff96c809b8 R14: 000000000000a370 R15:ffff8807ce68a1c0
[ 6.891000] FS: 0000000000000000(0000) GS:ffff8807d4200000(0000) knlGS:0000000000000000
[ 6.891000] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 6.891000] CR2: 0000000000000000 CR3: 000000028de16001 CR4:00000000007606e0
[ 6.891000] DR0: 0000000000000000 DR1: 0000000000000000 DR2:
0000000000000000
[ 6.891000] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7:
0000000000000400
[ 6.891000] PKRU: 00000000
[ 6.891000] Call Trace:
[ 6.891000] ? debug_object_fixup+0x30/0x30
[ 6.891000] ? _raw_spin_unlock_irqrestore+0x4b/0x60
[ 6.891000] ? __lockdep_init_map+0x12f/0x510
[ 6.891000] ? __lockdep_init_map+0x12f/0x510
[ 6.891000] virt_efi_get_next_variable+0xa2/0x160
[ 6.891000] efivar_init+0x1c4/0x6d7
[ 6.891000] ? efivar_ssdt_setup+0x3b/0x3b
[ 6.891000] ? efivar_entry_iter+0x120/0x120
[ 6.891000] ? find_held_lock+0x3a/0x1c0
[ 6.891000] ? lock_downgrade+0x5e0/0x5e0
[ 6.891000] ? kmsg_dump_rewind_nolock+0xd9/0xd9
[ 6.891000] ? _raw_spin_unlock_irqrestore+0x4b/0x60
[ 6.891000] ? trace_hardirqs_on_caller+0x381/0x570
[ 6.891000] ? efivar_ssdt_iter+0x1f4/0x1f4
[ 6.891000] efisubsys_init+0x1be/0x4ae
[ 6.891000] ? kernfs_get.part.8+0x4c/0x60
[ 6.891000] ? efivar_ssdt_iter+0x1f4/0x1f4
[ 6.891000] ? __kernfs_create_file+0x235/0x2e0
[ 6.891000] ? efivar_ssdt_iter+0x1f4/0x1f4
[ 6.891000] do_one_initcall+0xe9/0x5fd
[ 6.891000] ? perf_trace_initcall_level+0x450/0x450
[ 6.891000] ? __wake_up_common+0x5a0/0x5a0
[ 6.891000] ? lock_downgrade+0x5e0/0x5e0
[ 6.891000] kernel_init_freeable+0x51a/0x5f2
[ 6.891000] ? start_kernel+0x7b8/0x7b8
[ 6.891000] ? finish_task_switch+0x19a/0x690
[ 6.891000] ? __switch_to_asm+0x40/0x70
[ 6.891000] ? __switch_to_asm+0x34/0x70
[ 6.891000] ? rest_init+0xe9/0xe9
[ 6.891000] kernel_init+0xc/0x110
[ 6.891000] ? rest_init+0xe9/0xe9
[ 6.891000] ret_from_fork+0x24/0x50
[ 6.891000] irq event stamp: 1081352
[ 6.891000] hardirqs last enabled at (1081351): [<ffffffff93af7dab>] _raw_spin_unlock_irqrestore+0x4b/0x60
[ 6.891000] hardirqs last disabled at (1081352): [<ffffffff93af85c2>] _raw_spin_lock_irqsave+0x22/0x81
[ 6.891000] softirqs last enabled at (1081334): [<ffffffff93e006f9>] __do_softirq+0x6f9/0xaa0
[ 6.891000] softirqs last disabled at (1081325): [<ffffffff921b993f>] irq_exit+0x27f/0x2d0
[ 6.891000] ---[ end trace 15e1083fc009a526 ]---
All the messages above were printed while holding a raw spinlock with IRQ disabled. Further down the bootup sequence, the system appeared to hang:
11.270654] systemd[1]: systemd 239 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy)
[ 11.311307] systemd[1]: Detected architecture x86-64.
[ 11.316420] systemd[1]: Running in initial RAM disk.
Welcome to
The system is not responsive at this point.
I am not totally sure what caused this. Maybe it was caused by disabling IRQ for too long leading to some kind of corruption. Anyway, moving debug_object_is_on_stack() outside of the IRQ disabled lock critical section seemed to fix the hang problem.
Cheers,
Longman
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
Quoted 27 lines · Waiman Long
Quoted 27 lines
[..]
Quoted 5 lines
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call
printk()while holding a raw spinlock.Right, I like this patch.
And I think that we, maybe, can go even further.Some serial consoles call
mod_timer(). So what we could have with the debug objects enabled wasmod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlockThat's one possible scenario. The other one can involve console's IRQ handler, uart port spinlock,
mod_timer, debug objects, printk, and an eventual deadlock on the uart port spinlock. This one can be mitigated withprintk_safe. Butmod_timer()deadlock will require a different fix.
Quoted 5 lines
Quoted 3 lines
So maybe we need to switch debug objects print-outs to _always_
printk_deferred(). Debug objects can be used in code which cannot do directprintk()- timekeeping is just one example.Actually, I don't think that was the cause of the hang.
Oh, I didn't suggest that this was the case. Just talked about more problems with printk in debug objects. Serial consoles call mod_time, mod_timer calls debug objects, debug objects call printk and end up in serial console again. Serial consoles are not re-entrant at this point.
Quoted 7 lines
The debugobjects splat was caused by
debug_object_is_on_stack(), below was the output:[ 6.890048] ODEBUG: object (
____ptrval____) is NOT on stack (____ptrval____), but annotated.
[ 6.891000] WARNING: CPU: 28 PID: 1 atlib/debugobjects.c:369__debug_object_init.cold.11+0x51/0x2d6
[..]
Quoted 7 lines
11.270654] systemd[1]: systemd 239 running in system mode. (+PAM
(quoted diff) +3
-0+AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy)[ 11.311307] systemd[1]: Detected architecture x86-64. [ 11.316420] systemd[1]: Running in initial RAM disk.
Quoted 5 lines
Welcome to
The system is not responsive at this point.
I am not totally sure what caused this.
Hmm, me neither.
-ss
Petr Mladek
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
In reply to Waiman LongQuoted 149 lines · Waiman Long
Quoted 51 lines · Sergey Senozhatsky
On (11/21/18 11:49), Waiman Long wrote:
[..]Quoted 22 lines
Quoted 22 lines
lib/debugobjects.c +6
-2case ODEBUG_STATE_ACTIVE : -debug_print_object (obj,"init" ); state = obj->state;raw_spin_unlock_irqrestore (&db->lock, flags); +debug_print_object (obj,"init" );debug_object_fixup (descr->fixup_init, addr, state);return ;case ODEBUG_STATE_DESTROYED : -debug_print_object (obj,"init" ); + debug_printobj = true;break ;default :break ; }raw_spin_unlock_irqrestore (&db->lock, flags); +if (debug_chkstack) +debug_object_is_on_stack (addr, onstack); +if (debug_printobj) +debug_print_object (obj,"init" );[..]
Quoted 5 lines
As a side note, one of the test systems that I used generated a debugobjects splat in the bootup process and the system hanged afterward. Applying this patch alone fix the hanging problem and the system booted up successfully. So it is not really a good idea to call
printk()while holding a raw spinlock.Right, I like this patch.
And I think that we, maybe, can go even further.Some serial consoles call
mod_timer(). So what we could have with the debug objects enabled wasmod_timer() lock_timer_base() debug_activate() printk() call_console_drivers() foo_console() mod_timer() lock_timer_base() << deadlockThat's one possible scenario. The other one can involve console's IRQ handler, uart port spinlock,
mod_timer, debug objects, printk, and an eventual deadlock on the uart port spinlock. This one can be mitigated withprintk_safe. Butmod_timer()deadlock will require a different fix.Quoted 5 lines
So maybe we need to switch debug objects print-outs to _always_
printk_deferred(). Debug objects can be used in code which cannot do directprintk()- timekeeping is just one example.-ssActually, I don't think that was the cause of the hang. The debugobjects splat was caused by
debug_object_is_on_stack(), below was the output:[ 6.890048] ODEBUG: object (
____ptrval____) is NOT on stack (____ptrval____), but annotated.
[ 6.891000] WARNING: CPU: 28 PID: 1 atlib/debugobjects.c:369__debug_object_init.cold.11+0x51/0x2d6
[ 6.891000] Modules linked in:
[ 6.891000] CPU: 28 PID: 1 Comm: swapper/0 Not tainted 4.18.0-41.el8.bz1651764_cgroup_debug.x86_64+debug #1
[ 6.891000] Hardware name: HPE ProLiant DL120 Gen10/ProLiant DL120 Gen10, BIOS U36 11/14/2017
[ 6.891000] RIP: 0010:__debug_object_init.cold.11+0x51/0x2d6 [ 6.891000] Code: ea 03 80 3c 02 00 0f 85 85 02 00 00 49 8b 54 24 18 48 89 de 4c 89 44 24 10 48 c7 c7 00 ce 22 94 e8 73 18 62 ff 4c 8b 44 24 10 <0f> 0b e9 60 db ff ff 41 83 c4 01 b8 ff ff 37 00 44 89 25 ce 46 f9 [ 6.891000] RSP: 0000:ffff880104187960 EFLAGS: 00010086
[ 6.891000] RAX: 0000000000000050 RBX:ffffffff9764c570RCX:
0000000000000000
[ 6.891000] RDX: 0000000000000000 RSI: 0000000000000000 RDI:ffff880104178ca8
[ 6.891000] RBP:1ffff10020830f34R08:ffff8807ce68a1d0R09:fffffbfff2923554
[ 6.891000] R10:fffffbfff2923554R11:ffffffff9491aaa3R12:ffff880104178000
[ 6.891000] R13:ffffffff96c809b8R14:000000000000a370R15:ffff8807ce68a1c0
[ 6.891000] FS: 0000000000000000(0000) GS:ffff8807d4200000(0000) knlGS:0000000000000000
[ 6.891000] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 6.891000] CR2: 0000000000000000 CR3:000000028de16001CR4:00000000007606e0
[ 6.891000] DR0: 0000000000000000 DR1: 0000000000000000 DR2:
0000000000000000
[ 6.891000] DR3: 0000000000000000 DR6:00000000fffe0ff0DR7:
0000000000000400
[ 6.891000] PKRU: 00000000
[ 6.891000] Call Trace:
[ 6.891000] ?debug_object_fixup+0x30/0x30
[ 6.891000] ?_raw_spin_unlock_irqrestore+0x4b/0x60
[ 6.891000] ?__lockdep_init_map+0x12f/0x510
[ 6.891000] ?__lockdep_init_map+0x12f/0x510
[ 6.891000]virt_efi_get_next_variable+0xa2/0x160
[ 6.891000]efivar_init+0x1c4/0x6d7
[ 6.891000] ?efivar_ssdt_setup+0x3b/0x3b
[ 6.891000] ?efivar_entry_iter+0x120/0x120
[ 6.891000] ?find_held_lock+0x3a/0x1c0
[ 6.891000] ?lock_downgrade+0x5e0/0x5e0
[ 6.891000] ?kmsg_dump_rewind_nolock+0xd9/0xd9
[ 6.891000] ?_raw_spin_unlock_irqrestore+0x4b/0x60
[ 6.891000] ?trace_hardirqs_on_caller+0x381/0x570
[ 6.891000] ?efivar_ssdt_iter+0x1f4/0x1f4
[ 6.891000]efisubsys_init+0x1be/0x4ae
[ 6.891000] ? kernfs_get.part.8+0x4c/0x60
[ 6.891000] ?efivar_ssdt_iter+0x1f4/0x1f4
[ 6.891000] ?__kernfs_create_file+0x235/0x2e0
[ 6.891000] ?efivar_ssdt_iter+0x1f4/0x1f4
[ 6.891000]do_one_initcall+0xe9/0x5fd
[ 6.891000] ?perf_trace_initcall_level+0x450/0x450
[ 6.891000] ?__wake_up_common+0x5a0/0x5a0
[ 6.891000] ?lock_downgrade+0x5e0/0x5e0
[ 6.891000]kernel_init_freeable+0x51a/0x5f2
[ 6.891000] ?start_kernel+0x7b8/0x7b8
[ 6.891000] ?finish_task_switch+0x19a/0x690
[ 6.891000] ?__switch_to_asm+0x40/0x70
[ 6.891000] ?__switch_to_asm+0x34/0x70
[ 6.891000] ?rest_init+0xe9/0xe9
[ 6.891000]kernel_init+0xc/0x110
[ 6.891000] ?rest_init+0xe9/0xe9
[ 6.891000]ret_from_fork+0x24/0x50
[ 6.891000] irq event stamp: 1081352
[ 6.891000] hardirqs last enabled at (1081351): [<ffffffff93af7dab>]_raw_spin_unlock_irqrestore+0x4b/0x60
[ 6.891000] hardirqs last disabled at (1081352): [<ffffffff93af85c2>]_raw_spin_lock_irqsave+0x22/0x81
[ 6.891000] softirqs last enabled at (1081334): [<ffffffff93e006f9>]__do_softirq+0x6f9/0xaa0
[ 6.891000] softirqs last disabled at (1081325): [<ffffffff921b993f>]irq_exit+0x27f/0x2d0
[ 6.891000] ---[ end trace15e1083fc009a526]---All the messages above were printed while holding a raw spinlock with IRQ disabled. Further down the bootup sequence, the system appeared to hang:
11.270654] systemd[1]: systemd 239 running in system mode. (+PAM
(quoted diff) +3
-0+AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy)[ 11.311307] systemd[1]: Detected architecture x86-64. [ 11.316420] systemd[1]: Running in initial RAM disk.
Quoted 8 lines
Welcome to
The system is not responsive at this point.
I am not totally sure what caused this. Maybe it was caused by disabling IRQ for too long leading to some kind of corruption. Anyway, moving
debug_object_is_on_stack()outside of the IRQ disabled lock critical section seemed to fix the hang problem.
It is hard to say why printing the above with disabled interrupts would break anything. efivar_init() itself should get delayed the same way with and without the patch.
Some clue might be in the rest of the log. It would be interesting to compare full logs of non-patched and patched system.
Best Regards,
Petr
Peter Zijlstra
maintainerRe: [PATCH v2 07/17] debugobjects: Move printk out of db lock critical sections
In reply to Waiman LongQuoted 6 lines · Waiman Long
The
db->lockis a raw spinlock and so the lock hold time is supposed to be short. This will not be the case whenprintk()is being involved in some of the critical sections. In order to avoid the long hold time, in case some messages need to be printed, thedebug_object_is_on_stack()anddebug_print_object()calls are now moved out of those critical sections.
That's not why you did this patch though; you want to make these locks terminal locks and that means no printk() inside, as that uses locks again.
Please write relevant changelogs.
Waiman Long
reviewerPATCH v2 8/17 locking/lockdep: Add support for nestable terminal locks
In reply to Waiman LongThere are use cases where we want to allow nesting of one terminal lock underneath another terminal-like lock. That new lock type is called nestable terminal lock which can optionally allow the acquisition of no more than one regular (non-nestable) terminal lock underneath it.
--- include/linux/lockdep.h | 9 ++++++++- kernel/locking/lockdep.c | 15 +++++++++++++-- kernel/locking/lockdep_internals.h | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-)
include/linux/lockdep.h +8 -1
@@ -148,16 +148,20 @@ struct lock_class_stats {* 1) LOCKDEP_FLAG_NOVALIDATE: No full validation, just simple checks. * 2) LOCKDEP_FLAG_TERMINAL: This is a terminal lock where lock/unlock on * another lock within its critical section is not allowed. +* 3) LOCKDEP_FLAG_TERMINAL_NESTABLE: This is a terminal lock that can +* allow one more regular terminal lock to be nested underneath it. * * Only the least significant 4 bits of the flags will be copied to the * held_lock structure. */ #define LOCKDEP_FLAG_TERMINAL (1 << 0) +#define LOCKDEP_FLAG_TERMINAL_NESTABLE (1 << 1)#define LOCKDEP_FLAG_NOVALIDATE (1 << 4)#define LOCKDEP_HLOCK_FLAGS_MASK 0x0f#define LOCKDEP_NOCHECK_FLAGS (LOCKDEP_FLAG_NOVALIDATE |\ - LOCKDEP_FLAG_TERMINAL) + LOCKDEP_FLAG_TERMINAL |\ + LOCKDEP_FLAG_TERMINAL_NESTABLE)/* * Map the lock object (the lock instance) to the lock-class object.
@@ -327,6 +331,8 @@ extern void lockdep_init_map(struct lockdep_map *lock, const char *name, do { (lock)->dep_map.flags |= LOCKDEP_FLAG_NOVALIDATE; } while (0)#define lockdep_set_terminal_class (lock) \ do { (lock)->dep_map.flags |= LOCKDEP_FLAG_TERMINAL; } while (0) +#define lockdep_set_terminal_nestable_class (lock) \ + do { (lock)->dep_map.flags |= LOCKDEP_FLAG_TERMINAL_NESTABLE; } while (0)/* * Compare locking classes
@@ -444,6 +450,7 @@ static inline void lockdep_on(void)#define lockdep_set_novalidate_class (lock) do { } while (0)#define lockdep_set_terminal_class (lock) do { } while (0) +#define lockdep_set_terminal_nestable_class (lock) do { } while (0)/* * We don't define lockdep_match_class() and lockdep_match_key() for !LOCKDEP
kernel/locking/lockdep.c +13 -2
@@ -3263,13 +3263,24 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, class_idx = class - lock_classes +1 ;if (depth) { +int prev_type; + hlock = curr->held_locks + depth -1 ;/* -* Warn if the previous lock is a terminal lock. +* Warn if the previous lock is a terminal lock or the +* previous lock is a nestable terminal lock and the current +* one isn't a regular terminal lock. */ -if (DEBUG_LOCKS_WARN_ON (hlock_is_terminal (hlock))) + prev_type =hlock_is_terminal (hlock); +if (DEBUG_LOCKS_WARN_ON ((prev_type ==LOCKDEP_FLAG_TERMINAL ) || + ((prev_type ==LOCKDEP_FLAG_TERMINAL_NESTABLE ) && + (flags_is_terminal (class->flags) != +LOCKDEP_FLAG_TERMINAL )))) { +pr_warn ("Terminal lock error: prev lock = %s, curr lock = %s\n" , + hlock->instance->name, class->name);return 0 ; + }if (hlock->class_idx == class_idx && nest_lock) {if (hlock->references) {
kernel/locking/lockdep_internals.h +1 -1
@@ -215,5 +215,5 @@ static inline unsigned long debug_class_ops_read(struct lock_class *class)static inline unsigned int flags_is_terminal (unsigned int flags) { -return flags &LOCKDEP_FLAG_TERMINAL ; +return flags & (LOCKDEP_FLAG_TERMINAL |LOCKDEP_FLAG_TERMINAL_NESTABLE ); }
Signature · 1 line
1.8.3.1
Peter Zijlstra
maintainerRe: [PATCH v2 08/17] locking/lockdep: Add support for nestable terminal locks
Quoted 4 lines · Waiman Long
There are use cases where we want to allow nesting of one terminal lock underneath another terminal-like lock. That new lock type is called nestable terminal lock which can optionally allow the acquisition of no more than one regular (non-nestable) terminal lock underneath it.
I think I asked for a more coherent changelog on this. The above is still self contradictory and doesn't explain why you'd ever want such a 'misfeature' :-)
Peter Zijlstra
maintainerRe: [PATCH v2 08/17] locking/lockdep: Add support for nestable terminal locks
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Waiman Long
There are use cases where we want to allow nesting of one terminal lock underneath another terminal-like lock. That new lock type is called nestable terminal lock which can optionally allow the acquisition of no more than one regular (non-nestable) terminal lock underneath it.
I think I asked for a more coherent changelog on this. The above is still self contradictory and doesn't explain why you'd ever want such a 'misfeature' :-)
So maybe call the thing penterminal (contraction of penultimate and terminal) locks and explain why this annotation is safe -- in great detail.
Waiman Long
reviewerPATCH v2 9/17 debugobjects: Make object hash locks nestable terminal locks
In reply to Waiman LongBy making the object hash locks nestable terminal locks, we can avoid a bunch of unnecessary lockdep validations as well as saving space in the lockdep tables.
--- lib/debugobjects.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/debugobjects.c +6 -1
@@ -1129,8 +1129,13 @@ void __init debug_objects_early_init(void) {int i; -for (i =0 ; i <ODEBUG_HASH_SIZE ; i++) +/* +* Make the obj_hash locks nestable terminal locks. +*/ +for (i =0 ; i <ODEBUG_HASH_SIZE ; i++) {raw_spin_lock_init (&obj_hash[i].lock); +lockdep_set_terminal_nestable_class (&obj_hash[i].lock); + }for (i =0 ; i <ODEBUG_POOL_SIZE ; i++)hlist_add_head (&obj_static_pool[i].node, &obj_pool);
Signature · 1 line
1.8.3.1
Petr Mladek
maintainerRe: [PATCH v2 09/17] debugobjects: Make object hash locks nestable terminal locks
Quoted 3 lines · Waiman Long
By making the object hash locks nestable terminal locks, we can avoid a bunch of unnecessary lockdep validations as well as saving space in the lockdep tables.
Please, explain which terminal lock might be nested.
Hmm, it would hide eventual nesting of other terminal locks. It might reduce lockdep reliability. I wonder if the space optimization is worth it.
Finally, it might be good to add a short explanation what (nested) terminal locks mean into each commit. It would help people to understand the effect without digging into the lockdep code, ...
Best Regards,
Petr
Waiman Long
reviewerRe: [PATCH v2 09/17] debugobjects: Make object hash locks nestable terminal locks
Quoted 10 lines · Petr Mladek
Quoted 3 lines · Waiman Long
By making the object hash locks nestable terminal locks, we can avoid a bunch of unnecessary lockdep validations as well as saving space in the lockdep tables.
Please, explain which terminal lock might be nested.
Hmm, it would hide eventual nesting of other terminal locks. It might reduce lockdep reliability. I wonder if the space optimization is worth it.
Quoted 6 lines
Finally, it might be good to add a short explanation what (nested) terminal locks mean into each commit. It would help people to understand the effect without digging into the lockdep code, ...
Best Regards,
Petr
Nested terminal lock is currently only used in the debugobjects code. It should only be used on a case-by-case basis. In the case of the debugojects code, the locking patterns are:
(1)
raw_spin_lock(&db_lock);
...raw_spin_unlock(&db_lock);
(2)
raw_spin_lock(&db_lock);
...raw_spin_lock(&pool_lock);
...raw_spin_unlock(&pool_lock);
...raw_spin_unlock(&db_lock);
(3)
raw_spin_lock(&pool_lock);
...raw_spin_unlock(&pool_lock);
So the db_lock is made to be nestable that it can allow acquisition of pool_lock (a regular terminal lock) underneath it.
Cheers,
Longman
Petr Mladek
maintainerRe: [PATCH v2 09/17] debugobjects: Make object hash locks nestable terminal locks
Quoted 9 lines · Waiman Long
Quoted 5 lines · Petr Mladek
Quoted 3 lines · Waiman Long
By making the object hash locks nestable terminal locks, we can avoid a bunch of unnecessary lockdep validations as well as saving space in the lockdep tables.
Please, explain which terminal lock might be nested.
So the
db_lockis made to be nestable that it can allow acquisition ofpool_lock(a regular terminal lock) underneath it.
Please, mention this in the commit message.
Best Regards,
Petr
Peter Zijlstra
maintainerRe: [PATCH v2 09/17] debugobjects: Make object hash locks nestable terminal locks
In reply to Waiman LongQuoted 3 lines · Waiman Long
By making the object hash locks nestable terminal locks, we can avoid a bunch of unnecessary lockdep validations as well as saving space in the lockdep tables.
So the 'problem'; which you've again not explained; is that debugobjects has the following lock order:
&db->lock &pool_lock
And you seem to want to tag '&db->lock' as terminal, which is obviuosly a big fat lie.
You've also not explained why it is safe to do this (I think it actually is, but you really should've spelled it out).
Furthermore; you've not justified any of this 'insanity' with numbers. What do we gain with this nestable madness that justifies the crazy?
Waiman Long
reviewerPATCH v2 10/17 lib/stackdepot: Make depot_lock a terminal spinlock
In reply to Waiman LongBy defining depot_lock as a terminal spinlock, it reduces the lockdep overhead when this lock is being used.
--- lib/stackdepot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/stackdepot.c +1 -1
@@ -78,7 +78,7 @@ struct stack_record {static int depot_index;static int next_slab_inited;static size_t depot_offset; -static DEFINE_SPINLOCK (depot_lock); +static DEFINE_TERMINAL_SPINLOCK (depot_lock);static bool init_stack_slab (void **prealloc) {
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 11/17 locking/rwsem: Mark rwsem.wait_lock as a terminal lock
In reply to Waiman LongThe wait_lock in a rwsem is always acquired with IRQ disabled. For the rwsem-xadd.c implementation, no other lock will be called while holding the wait_lock. So it satisfies the condition of being a terminal lock. By marking it as terminal, the lockdep overhead will be reduced.
--- include/linux/rwsem.h | 11 ++++++++++- kernel/locking/rwsem-xadd.c | 1 + 2 files changed, 11 insertions(+), 1 deletion(-)
include/linux/rwsem.h +10 -1
@@ -77,16 +77,25 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem)# define __RWSEM_DEP_MAP_INIT (lockname)#endif +/* +* The wait_lock is marked as a terminal lock to reduce lockdep overhead +* when the rwsem-xadd.c is used. This is implied when +* CONFIG_RWSEM_SPIN_ON_OWNER is true. The rwsem-spinlock.c implementation +* allows calling wake_up_process() while holding the wait_lock. So it +* can't be marked as terminal in this case. +*/ #ifdef CONFIG_RWSEM_SPIN_ON_OWNER #define __RWSEM_OPT_INIT (lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL +#define __RWSEM_WAIT_LOCK_INIT (x) __RAW_TERMINAL_SPIN_LOCK_UNLOCKED(x)#else #define __RWSEM_OPT_INIT (lockname) +#define __RWSEM_WAIT_LOCK_INIT (x) __RAW_SPIN_LOCK_UNLOCKED(x)#endif #define __RWSEM_INITIALIZER (name) \ { __RWSEM_INIT_COUNT(name), \ .wait_list = LIST_HEAD_INIT((name).wait_list), \ - .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ + .wait_lock = __RWSEM_WAIT_LOCK_INIT(name.wait_lock) \ __RWSEM_OPT_INIT(name) \ __RWSEM_DEP_MAP_INIT(name) }
kernel/locking/rwsem-xadd.c +1 -0
@@ -85,6 +85,7 @@ void __init_rwsem(struct rw_semaphore *sem, const char *name,#endif atomic_long_set (&sem->count,RWSEM_UNLOCKED_VALUE );raw_spin_lock_init (&sem->wait_lock); +lockdep_set_terminal_class (&sem->wait_lock);INIT_LIST_HEAD (&sem->wait_list);#ifdef CONFIG_RWSEM_SPIN_ON_OWNER sem->owner =NULL ;
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 12/17 cgroup: Mark the rstat percpu lock as terminal
In reply to Waiman LongBy classifying the cgroup rstat percpu locks as terminal locks, it reduces the lockdep overhead when these locks are being used.
--- kernel/cgroup/rstat.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/cgroup/rstat.c +7 -2
@@ -291,8 +291,13 @@ void __init cgroup_rstat_boot(void) {int cpu; -for_each_possible_cpu (cpu) -raw_spin_lock_init (per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu)); +for_each_possible_cpu (cpu) { +raw_spinlock_t *cgroup_rstat_percpu_lock = +per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu); + +raw_spin_lock_init (cgroup_rstat_percpu_lock); +lockdep_set_terminal_class (cgroup_rstat_percpu_lock); + }BUG_ON (cgroup_rstat_init (&cgrp_dfl_root.cgrp)); }
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 13/17 mm/kasan: Make quarantine_lock a terminal lock
In reply to Waiman LongBy making quarantine_lock a terminal spinlock, it reduces the lockdep overhead when this lock is being used.
--- mm/kasan/quarantine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/kasan/quarantine.c +1 -1
@@ -103,7 +103,7 @@ static void qlist_move_all(struct qlist_head *from, struct qlist_head *to)static int quarantine_tail;/* Total size of all objects in global_quarantine across all batches. */ static unsigned long quarantine_size; -static DEFINE_RAW_SPINLOCK (quarantine_lock); +static DEFINE_RAW_TERMINAL_SPINLOCK (quarantine_lock);DEFINE_STATIC_SRCU (remove_cache_srcu);/* Maximum size of the global queue. */
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 14/17 dma-debug: Mark free_entries_lock as terminal
In reply to Waiman LongBy making free_entries_lock a terminal spinlock, it reduces the lockdep overhead when this lock is used.
--- kernel/dma/debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/dma/debug.c +1 -1
@@ -106,7 +106,7 @@ struct hash_bucket {/* List of pre-allocated dma_debug_entry's */ static LIST_HEAD (free_entries);/* Lock for the list above */ -static DEFINE_SPINLOCK (free_entries_lock); +static DEFINE_TERMINAL_SPINLOCK (free_entries_lock);/* Global disable flag - will be set in case of an error */ static bool global_disable__read_mostly ;
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 15/17 kernfs: Mark kernfs_open_node_lock as terminal lock
In reply to Waiman LongBy making kernfs_open_node_lock a terminal spinlock, it reduces the lockdep overhead when this lock is used.
--- fs/kernfs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/kernfs/file.c +1 -1
@@ -29,7 +29,7 @@* kernfs_open_file. kernfs_open_files are chained at * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. */ -static DEFINE_SPINLOCK (kernfs_open_node_lock); +static DEFINE_TERMINAL_SPINLOCK (kernfs_open_node_lock);static DEFINE_MUTEX (kernfs_open_file_mutex);struct kernfs_open_node {
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 16/17 delay_acct: Mark task's delays->lock as terminal spinlock
In reply to Waiman LongBy making task's delays->lock a terminal spinlock, it reduces the lockdep overhead when this lock is used.
--- kernel/delayacct.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/delayacct.c +3 -1
@@ -43,8 +43,10 @@ void delayacct_init(void)void __delayacct_tsk_init (struct task_struct *tsk) { tsk->delays =kmem_cache_zalloc (delayacct_cache,GFP_KERNEL ); -if (tsk->delays) +if (tsk->delays) {raw_spin_lock_init (&tsk->delays->lock); +lockdep_set_terminal_class (&tsk->delays->lock); + } }/*
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v2 17/17 locking/lockdep: Check raw/non-raw locking conflicts
In reply to Waiman LongA task holding a raw spinlock should not acquire a non-raw lock as that will break PREEMPT_RT kernel. Checking is now added and a lockdep warning will be printed if that happens.
--- include/linux/lockdep.h | 6 ++++++ include/linux/spinlock_types.h | 4 ++-- kernel/locking/lockdep.c | 15 +++++++++++++-- kernel/locking/spinlock_debug.c | 1 + 4 files changed, 22 insertions(+), 4 deletions(-)
include/linux/lockdep.h +6 -0
@@ -150,12 +150,15 @@ struct lock_class_stats {* another lock within its critical section is not allowed. * 3) LOCKDEP_FLAG_TERMINAL_NESTABLE: This is a terminal lock that can * allow one more regular terminal lock to be nested underneath it. +* 4) LOCKDEP_FLAG_RAW: This is a raw spinlock. A task holding a raw +* spinlock should not acquire a non-raw lock. * * Only the least significant 4 bits of the flags will be copied to the * held_lock structure. */ #define LOCKDEP_FLAG_TERMINAL (1 << 0)#define LOCKDEP_FLAG_TERMINAL_NESTABLE (1 << 1) +#define LOCKDEP_FLAG_RAW (1 << 2)#define LOCKDEP_FLAG_NOVALIDATE (1 << 4)#define LOCKDEP_HLOCK_FLAGS_MASK 0x0f
@@ -333,6 +336,8 @@ extern void lockdep_init_map(struct lockdep_map *lock, const char *name, do { (lock)->dep_map.flags |= LOCKDEP_FLAG_TERMINAL; } while (0)#define lockdep_set_terminal_nestable_class (lock) \ do { (lock)->dep_map.flags |= LOCKDEP_FLAG_TERMINAL_NESTABLE; } while (0) +#define lockdep_set_raw_class (lock) \ + do { (lock)->dep_map.flags |= LOCKDEP_FLAG_RAW; } while (0)/* * Compare locking classes
@@ -448,6 +453,7 @@ static inline void lockdep_on(void) do { (void)(key); } while (0)#define lockdep_set_subclass (lock, sub) do { } while (0) +#define lockdep_set_raw_class (lock) do { } while (0)#define lockdep_set_novalidate_class (lock) do { } while (0)#define lockdep_set_terminal_class (lock) do { } while (0)#define lockdep_set_terminal_nestable_class (lock) do { } while (0)
include/linux/spinlock_types.h +2 -2
@@ -55,11 +55,11 @@ SPIN_DEP_MAP_INIT(lockname, f) }#define __RAW_SPIN_LOCK_UNLOCKED (lockname) \ - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname, 0) + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname, LOCKDEP_FLAG_RAW)#define __RAW_TERMINAL_SPIN_LOCK_UNLOCKED (lockname) \ (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname, \ - LOCKDEP_FLAG_TERMINAL) + LOCKDEP_FLAG_TERMINAL|LOCKDEP_FLAG_RAW)#define DEFINE_RAW_SPINLOCK (x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)#define DEFINE_RAW_TERMINAL_SPINLOCK (x) \
kernel/locking/lockdep.c +13 -2
@@ -3273,8 +3273,8 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,* one isn't a regular terminal lock. */ prev_type =hlock_is_terminal (hlock); -if (DEBUG_LOCKS_WARN_ON ((prev_type ==LOCKDEP_FLAG_TERMINAL ) || - ((prev_type ==LOCKDEP_FLAG_TERMINAL_NESTABLE ) && +if (DEBUG_LOCKS_WARN_ON ((prev_type &LOCKDEP_FLAG_TERMINAL ) || + ((prev_type &LOCKDEP_FLAG_TERMINAL_NESTABLE ) && (flags_is_terminal (class->flags) !=LOCKDEP_FLAG_TERMINAL )))) { pr_warn("Terminal lock error: prev lock = %s, curr lock = %s\n",
@@ -3282,6 +3282,17 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,return 0 ; } +/* +* A task holding a raw spinlock should not acquire another +* non-raw lock. +*/ +if (DEBUG_LOCKS_WARN_ON ((prev_type &LOCKDEP_FLAG_RAW ) && + !(class->flags &LOCKDEP_FLAG_RAW ))) { +pr_warn ("Raw lock error: prev lock = %s, curr lock = %s\n" , + hlock->instance->name, class->name); +return 0 ; + } +if (hlock->class_idx == class_idx && nest_lock) {if (hlock->references) {/*
kernel/locking/spinlock_debug.c +1 -0
@@ -22,6 +22,7 @@ void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,*/ debug_check_no_locks_freed ((void *)lock,sizeof (*lock));lockdep_init_map (&lock->dep_map, name, key,0 ); +lockdep_set_raw_class (lock);#endif lock->raw_lock = (arch_spinlock_t )__ARCH_SPIN_LOCK_UNLOCKED; lock->magic =SPINLOCK_MAGIC ;
Signature · 1 line
1.8.3.1