locking: lockdep: Decrease nr_unused_locks if lock unused in zap_class()
Subsystems (2)
PATCH 1/1 locking: lockdep: Decrease nr_unused_locks if lock unused in zap_class()
Currently, when a lock class is allocated, nr_unused_locks will be increased by 1, until it gets used: nr_unused_locks will be decreased by 1 in mark_lock(). However, one scenario is missed: a lock class may be zapped without even being used once. This could result into a situation that nr_unused_locks != 0 but no unused lock class is active in the system, and when cat /proc/lockdep_stats, a WARN_ON() will be triggered in a CONFIG_DEBUG_LOCKDEP=y kernel:
[...] DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused)
[...] WARNING: CPU: 41 PID: 1121 at kernel/locking/lockdep_proc.c:283 lockdep_stats_show+0xba9/0xbd0
And as a result, lockdep will be disabled after this.
Therefore, nr_unused_locks needs to be accounted correctly at zap_class() time.
--- kernel/locking/lockdep.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/locking/lockdep.c +3 -0
@@ -6264,6 +6264,9 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)hlist_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); +/* class allocated but not used, -1 in nr_unused_locks */ +if (class->usage_mask ==0 ) +debug_atomic_dec (nr_unused_locks); nr_lock_classes--;__clear_bit (class - lock_classes, lock_classes_in_use);if (class - lock_classes == max_lock_class_idx)
Signature · 1 line
2.47.1
Quoted 11 lines · Boqun Feng
Currently, when a lock class is allocated,
nr_unused_lockswill be increased by 1, until it gets used:nr_unused_lockswill be decreased by 1 inmark_lock(). However, one scenario is missed: a lock class may be zapped without even being used once. This could result into a situation thatnr_unused_locks!= 0 but no unused lock class is active in the system, and whencat /proc/lockdep_stats, aWARN_ON()will be triggered in aCONFIG_DEBUG_LOCKDEP=ykernel:[...]
DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused)
[...] WARNING: CPU: 41 PID: 1121 atkernel/locking/lockdep_proc.c:283lockdep_stats_show+0xba9/0xbd0
Quoted 7 lines
And as a result, lockdep will be disabled after this.
Therefore,
nr_unused_locksneeds to be accounted correctly atzap_class()time.Signee-off-by Boqun Feng <[email protected]>
Typo: "Signee-off-by"?
Other than that, LGTM
Quoted 18 lines
--- kernel/locking/lockdep.c | 3 +++ 1 file changed, 3 insertions(+)kernel/locking/lockdep.c +3
-0@@ -6264,6 +6264,9 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)hlist_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); +/* class allocated but not used, -1 in nr_unused_locks */ +if (class->usage_mask ==0 ) +debug_atomic_dec (nr_unused_locks); nr_lock_classes--;__clear_bit (class - lock_classes, lock_classes_in_use);if (class - lock_classes == max_lock_class_idx)
Quoted 15 lines · Waiman Long
Quoted 13 lines · Boqun Feng
Currently, when a lock class is allocated,
nr_unused_lockswill be increased by 1, until it gets used:nr_unused_lockswill be decreased by 1 inmark_lock(). However, one scenario is missed: a lock class may be zapped without even being used once. This could result into a situation thatnr_unused_locks!= 0 but no unused lock class is active in the system, and whencat /proc/lockdep_stats, aWARN_ON()will be triggered in aCONFIG_DEBUG_LOCKDEP=ykernel:[...]
DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused)
[...] WARNING: CPU: 41 PID: 1121 atkernel/locking/lockdep_proc.c:283lockdep_stats_show+0xba9/0xbd0And as a result, lockdep will be disabled after this.
Quoted 8 lines
Quoted 5 lines
Therefore,
nr_unused_locksneeds to be accounted correctly atzap_class()time.Signee-off-by Boqun Feng <[email protected]>Typo: "Signee-off-by"?
Oops, yeah.
Quoted 4 lines
Other than that, LGTM
Reviewed-by Waiman Long <[email protected]>
Thanks!
Regards,
Boqun
Quoted 19 lines
Quoted 18 lines
--- kernel/locking/lockdep.c | 3 +++ 1 file changed, 3 insertions(+)kernel/locking/lockdep.c +3
-0@@ -6264,6 +6264,9 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)hlist_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); +/* class allocated but not used, -1 in nr_unused_locks */ +if (class->usage_mask ==0 ) +debug_atomic_dec (nr_unused_locks); nr_lock_classes--;__clear_bit (class - lock_classes, lock_classes_in_use);if (class - lock_classes == max_lock_class_idx)
kernel/locking/lockdep.c static void zap_class(struct pending_free *pf, struct lock_class *class) · line 6269
Quoted 11 lines · Boqun Feng
kernel/locking/lockdep.c +3
-0@@ -6264,6 +6264,9 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)hlist_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); +/* class allocated but not used, -1 in nr_unused_locks */ +if (class->usage_mask ==0 ) +debug_atomic_dec (nr_unused_locks);
Nit: capitalization in comments should follow the style of the surrounding code - ie. I did the change below.
Thanks,
Ingo
======================>
kernel/locking/lockdep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/lockdep.c +1 -1
@@ -6264,7 +6264,7 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)hlist_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); -/* class allocated but not used, -1 in nr_unused_locks */ +/* Class allocated but not used, -1 in nr_unused_locks */ if (class->usage_mask ==0 )debug_atomic_dec (nr_unused_locks); nr_lock_classes--;
[tip: locking/urgent] locking/lockdep: Decrease nr_unused_locks if lock unused in zap_class()
In reply to Boqun FengThe following commit has been merged into the locking/urgent branch of tip:
The following commit has been merged into the locking/urgent branch of tip:
Commit-ID: 495f53d5cca0f939eaed9dca90b67e7e6fb0e30c Gitweb: https://git.kernel.org/tip/495f53d5cca0f939eaed9dca90b67e7e6fb0e30c Author: Boqun Feng <[email protected]> AuthorDate: Wed, 26 Mar 2025 11:08:30 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Mar 2025 08:23:17 +01:00
locking/lockdep: Decrease nr_unused_locks if lock unused in zap_class()
Currently, when a lock class is allocated, nr_unused_locks will be increased by 1, until it gets used: nr_unused_locks will be decreased by 1 in mark_lock(). However, one scenario is missed: a lock class may be zapped without even being used once. This could result into a situation that nr_unused_locks != 0 but no unused lock class is active in the system, and when cat /proc/lockdep_stats, a WARN_ON() will be triggered in a CONFIG_DEBUG_LOCKDEP=y kernel:
[...] DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused)
[...] WARNING: CPU: 41 PID: 1121 at kernel/locking/lockdep_proc.c:283 lockdep_stats_show+0xba9/0xbd0
And as a result, lockdep will be disabled after this.
Therefore, nr_unused_locks needs to be accounted correctly at zap_class() time.
--- kernel/locking/lockdep.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/locking/lockdep.c +3 -0
@@ -6264,6 +6264,9 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)hlist_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); +/* Class allocated but not used, -1 in nr_unused_locks */ +if (class->usage_mask ==0 ) +debug_atomic_dec (nr_unused_locks); nr_lock_classes--;__clear_bit (class - lock_classes, lock_classes_in_use);if (class - lock_classes == max_lock_class_idx)