Skip to contentSkip to conversation

locking/lockdep: Cleanup reinit_class()

PATCH Applied Waiman Long 3 participants 5 messages Feb 11, 2022 – Feb 17, 2022 Locking primitives

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

PATCH 1/2 locking/lockdep: Cleanup reinit_class()

Replace the duplicated WARN_ON_ONCE() tests in reinit_class() with BUILD_BUG_ON().

---
 include/linux/lockdep_types.h | 4 ++++
 kernel/locking/lockdep.c      | 6 +++---
 2 files changed, 7 insertions(+), 3 deletions(-)
include/linux/lockdep_types.h +4 -0
index d22430840b53..446d981ae3a2 100644
@@ -108,6 +108,10 @@ struct lock_class {
 	 */
 	struct list_head		locks_after, locks_before;
 
+	/*
+	 * All the fields that require re-initialization after being zapped
+	 * must be put after "key".
+	 */
 	const struct lockdep_subclass_key *key;
 	unsigned int			subclass;
 	unsigned int			dep_gen_id;
kernel/locking/lockdep.c +3 -3
index d4252b5c9863..77bbe557d48b 100644
@@ -6015,10 +6015,10 @@ static void reinit_class(struct lock_class *class)
 	WARN_ON_ONCE(!class->lock_entry.next);
 	WARN_ON_ONCE(!list_empty(&class->locks_after));
 	WARN_ON_ONCE(!list_empty(&class->locks_before));
+	BUILD_BUG_ON(offsetof(struct lock_class, lock_entry) > offset);
+	BUILD_BUG_ON(offsetof(struct lock_class, locks_after) > offset);
+	BUILD_BUG_ON(offsetof(struct lock_class, locks_before) > offset);
 	memset(p + offset, 0, sizeof(*class) - offset);
-	WARN_ON_ONCE(!class->lock_entry.next);
-	WARN_ON_ONCE(!list_empty(&class->locks_after));
-	WARN_ON_ONCE(!list_empty(&class->locks_before));
 }
 
 static inline int within(const void *addr, void *start, unsigned long size)
Signature · 1 line
2.27.0
author reviewer
#1 lore raw

PATCH 2/2 locking/lockdep: Iterate lock_classes directly when reading lockdep files

When dumping lock_classes information via /proc/lockdep, we can't take the lockdep lock as the lock hold time is indeterminate. Iterating over all_lock_classes without holding lock can be dangerous as there is a slight chance that it may branch off to other lists leading to infinite loop or even access invalid memory if changes are made to all_lock_classes list in parallel.

To avoid this problem, iteration of lock classes is now done directly on the lock_classes array itself. The lock_classes_in_use bitmap is checked to see if the lock class is being used. To avoid iterating the full array all the times, a new max_lock_class_idx value is added to track the maximum lock_class index that is currently being used.

We can theoretically take the lockdep lock for iterating all_lock_classes when other lockdep files (lockdep_stats and lock_stat) are accessed as the lock hold time will be shorter for them. For consistency, they are also modified to iterate the lock_classes array directly.

---
 kernel/locking/lockdep.c           | 14 +++++---
 kernel/locking/lockdep_internals.h |  6 ++--
 kernel/locking/lockdep_proc.c      | 51 +++++++++++++++++++++++++-----
 3 files changed, 56 insertions(+), 15 deletions(-)
kernel/locking/lockdep.c +9 -5
index 77bbe557d48b..7a9ce6d26285 100644
@@ -183,11 +183,9 @@ static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
 static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
 unsigned long nr_lock_classes;
 unsigned long nr_zapped_classes;
-#ifndef CONFIG_DEBUG_LOCKDEP
-static
-#endif
+unsigned long max_lock_class_idx;
 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
-static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
+DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
 
 static inline struct lock_class *hlock_class(struct held_lock *hlock)
 {
@@ -338,7 +336,7 @@ static inline void lock_release_holdtime(struct held_lock *hlock)
  * elements. These elements are linked together by the lock_entry member in
  * struct lock_class.
  */
-LIST_HEAD(all_lock_classes);
+static LIST_HEAD(all_lock_classes);
 static LIST_HEAD(free_lock_classes);
 
 /**
@@ -1252,6 +1250,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
 	struct lockdep_subclass_key *key;
 	struct hlist_head *hash_head;
 	struct lock_class *class;
+	int idx;
 
 	DEBUG_LOCKS_WARN_ON(!irqs_disabled());
 
@@ -1317,6 +1316,9 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
 	 * of classes.
 	 */
 	list_move_tail(&class->lock_entry, &all_lock_classes);
+	idx = class - lock_classes;
+	if (idx > max_lock_class_idx)
+		max_lock_class_idx = idx;
 
 	if (verbose(class)) {
 		graph_unlock();
@@ -5998,6 +6000,8 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)
 		WRITE_ONCE(class->name, NULL);
 		nr_lock_classes--;
 		__clear_bit(class - lock_classes, lock_classes_in_use);
+		if (class - lock_classes == max_lock_class_idx)
+			max_lock_class_idx--;
 	} else {
 		WARN_ONCE(true, "%s() failed for class %s\n", __func__,
 			  class->name);
kernel/locking/lockdep_internals.h +4 -2
index ecb8662e7a4e..bbe9000260d0 100644
@@ -121,7 +121,6 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ =
 
 #define MAX_LOCKDEP_CHAIN_HLOCKS (MAX_LOCKDEP_CHAINS*5)
 
-extern struct list_head all_lock_classes;
 extern struct lock_chain lock_chains[];
 
 #define LOCK_USAGE_CHARS (2*XXX_LOCK_USAGE_STATES + 1)
@@ -151,6 +150,10 @@ extern unsigned int nr_large_chain_blocks;
 
 extern unsigned int max_lockdep_depth;
 extern unsigned int max_bfs_queue_depth;
+extern unsigned long max_lock_class_idx;
+
+extern struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
+extern unsigned long lock_classes_in_use[];
 
 #ifdef CONFIG_PROVE_LOCKING
 extern unsigned long lockdep_count_forward_deps(struct lock_class *);
@@ -205,7 +208,6 @@ struct lockdep_stats {
 };
 
 DECLARE_PER_CPU(struct lockdep_stats, lockdep_stats);
-extern struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
 
 #define __debug_atomic_inc(ptr)					\
 	this_cpu_inc(lockdep_stats.ptr);
kernel/locking/lockdep_proc.c +43 -8

117 lines not on this page · raw message

Signature · 1 line
2.27.0
merge bot Applied
#2 lore raw

[tip: locking/core] locking/lockdep: Iterate lock_classes directly when reading lockdep files

The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     fb7275acd6fb988313dddd8d3d19efa70d9015ad
Gitweb:        https://git.kernel.org/tip/fb7275acd6fb988313dddd8d3d19efa70d9015ad
Author:        Waiman Long <[email protected]>
AuthorDate:    Thu, 10 Feb 2022 22:55:26 -05:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 16 Feb 2022 15:57:58 +01:00

locking/lockdep: Iterate lock_classes directly when reading lockdep files

When dumping lock_classes information via /proc/lockdep, we can't take the lockdep lock as the lock hold time is indeterminate. Iterating over all_lock_classes without holding lock can be dangerous as there is a slight chance that it may branch off to other lists leading to infinite loop or even access invalid memory if changes are made to all_lock_classes list in parallel.

To avoid this problem, iteration of lock classes is now done directly on the lock_classes array itself. The lock_classes_in_use bitmap is checked to see if the lock class is being used. To avoid iterating the full array all the times, a new max_lock_class_idx value is added to track the maximum lock_class index that is currently being used.

We can theoretically take the lockdep lock for iterating all_lock_classes when other lockdep files (lockdep_stats and lock_stat) are accessed as the lock hold time will be shorter for them. For consistency, they are also modified to iterate the lock_classes array directly.

---
 kernel/locking/lockdep.c           | 14 +++++---
 kernel/locking/lockdep_internals.h |  6 ++-
 kernel/locking/lockdep_proc.c      | 51 ++++++++++++++++++++++++-----
 3 files changed, 56 insertions(+), 15 deletions(-)
kernel/locking/lockdep.c +9 -5
index 2e6892e..50036c1 100644
@@ -183,11 +183,9 @@ static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
 static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
 unsigned long nr_lock_classes;
 unsigned long nr_zapped_classes;
-#ifndef CONFIG_DEBUG_LOCKDEP
-static
-#endif
+unsigned long max_lock_class_idx;
 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
-static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
+DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
 
 static inline struct lock_class *hlock_class(struct held_lock *hlock)
 {
@@ -338,7 +336,7 @@ static inline void lock_release_holdtime(struct held_lock *hlock)
  * elements. These elements are linked together by the lock_entry member in
  * struct lock_class.
  */
-LIST_HEAD(all_lock_classes);
+static LIST_HEAD(all_lock_classes);
 static LIST_HEAD(free_lock_classes);
 
 /**
@@ -1252,6 +1250,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
 	struct lockdep_subclass_key *key;
 	struct hlist_head *hash_head;
 	struct lock_class *class;
+	int idx;
 
 	DEBUG_LOCKS_WARN_ON(!irqs_disabled());
 
@@ -1317,6 +1316,9 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
 	 * of classes.
 	 */
 	list_move_tail(&class->lock_entry, &all_lock_classes);
+	idx = class - lock_classes;
+	if (idx > max_lock_class_idx)
+		max_lock_class_idx = idx;
 
 	if (verbose(class)) {
 		graph_unlock();
@@ -6000,6 +6002,8 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)
 		WRITE_ONCE(class->name, NULL);
 		nr_lock_classes--;
 		__clear_bit(class - lock_classes, lock_classes_in_use);
+		if (class - lock_classes == max_lock_class_idx)
+			max_lock_class_idx--;
 	} else {
 		WARN_ONCE(true, "%s() failed for class %s\n", __func__,
 			  class->name);
kernel/locking/lockdep_internals.h +4 -2
index ecb8662..bbe9000 100644
@@ -121,7 +121,6 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ =
 
 #define MAX_LOCKDEP_CHAIN_HLOCKS (MAX_LOCKDEP_CHAINS*5)
 
-extern struct list_head all_lock_classes;
 extern struct lock_chain lock_chains[];
 
 #define LOCK_USAGE_CHARS (2*XXX_LOCK_USAGE_STATES + 1)
@@ -151,6 +150,10 @@ extern unsigned int nr_large_chain_blocks;
 
 extern unsigned int max_lockdep_depth;
 extern unsigned int max_bfs_queue_depth;
+extern unsigned long max_lock_class_idx;
+
+extern struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
+extern unsigned long lock_classes_in_use[];
 
 #ifdef CONFIG_PROVE_LOCKING
 extern unsigned long lockdep_count_forward_deps(struct lock_class *);
@@ -205,7 +208,6 @@ struct lockdep_stats {
 };
 
 DECLARE_PER_CPU(struct lockdep_stats, lockdep_stats);
-extern struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
 
 #define __debug_atomic_inc(ptr)					\
 	this_cpu_inc(lockdep_stats.ptr);
kernel/locking/lockdep_proc.c +43 -8

117 lines not on this page · raw message

maintainer
#3 lore raw
In reply to Waiman Long
Quoted 45 lines · Waiman Long

Replace the duplicated WARN_ON_ONCE() tests in reinit_class() with BUILD_BUG_ON().

---
 include/linux/lockdep_types.h | 4 ++++
 kernel/locking/lockdep.c      | 6 +++---
 2 files changed, 7 insertions(+), 3 deletions(-)
include/linux/lockdep_types.h +4 -0
index d22430840b53..446d981ae3a2 100644
@@ -108,6 +108,10 @@ struct lock_class {
 	 */
 	struct list_head		locks_after, locks_before;
 
+	/*
+	 * All the fields that require re-initialization after being zapped
+	 * must be put after "key".
+	 */
 	const struct lockdep_subclass_key *key;
 	unsigned int			subclass;
 	unsigned int			dep_gen_id;
kernel/locking/lockdep.c +3 -3
index d4252b5c9863..77bbe557d48b 100644
@@ -6015,10 +6015,10 @@ static void reinit_class(struct lock_class *class)
 	WARN_ON_ONCE(!class->lock_entry.next);
 	WARN_ON_ONCE(!list_empty(&class->locks_after));
 	WARN_ON_ONCE(!list_empty(&class->locks_before));
+	BUILD_BUG_ON(offsetof(struct lock_class, lock_entry) > offset);
+	BUILD_BUG_ON(offsetof(struct lock_class, locks_after) > offset);
+	BUILD_BUG_ON(offsetof(struct lock_class, locks_before) > offset);
 	memset(p + offset, 0, sizeof(*class) - offset);
-	WARN_ON_ONCE(!class->lock_entry.next);
-	WARN_ON_ONCE(!list_empty(&class->locks_after));
-	WARN_ON_ONCE(!list_empty(&class->locks_before));
 }
 
 static inline int within(const void *addr, void *start, unsigned long size)
Signature · 1 line
2.27.0

Yeah, except that this doesn't build because it conflicts with a previous patch you actually Acked:

https://lore.kernel.org/all/[email protected]/T/#m0e9474a9204ec29fb2135135ce3134640a3f27b5

author reviewer
#4 lore raw
Quoted 42 lines · Peter Zijlstra
Quoted 41 lines · Waiman Long

Replace the duplicated WARN_ON_ONCE() tests in reinit_class() with BUILD_BUG_ON().

---
  include/linux/lockdep_types.h | 4 ++++
  kernel/locking/lockdep.c      | 6 +++---
  2 files changed, 7 insertions(+), 3 deletions(-)
include/linux/lockdep_types.h +4 -0
index d22430840b53..446d981ae3a2 100644
@@ -108,6 +108,10 @@ struct lock_class {
  	 */
  	struct list_head		locks_after, locks_before;
 
+	/*
+	 * All the fields that require re-initialization after being zapped
+	 * must be put after "key".
+	 */
  	const struct lockdep_subclass_key *key;
  	unsigned int			subclass;
  	unsigned int			dep_gen_id;
kernel/locking/lockdep.c +3 -3
index d4252b5c9863..77bbe557d48b 100644
@@ -6015,10 +6015,10 @@ static void reinit_class(struct lock_class *class)
  	WARN_ON_ONCE(!class->lock_entry.next);
  	WARN_ON_ONCE(!list_empty(&class->locks_after));
  	WARN_ON_ONCE(!list_empty(&class->locks_before));
+	BUILD_BUG_ON(offsetof(struct lock_class, lock_entry) > offset);
+	BUILD_BUG_ON(offsetof(struct lock_class, locks_after) > offset);
+	BUILD_BUG_ON(offsetof(struct lock_class, locks_before) > offset);
  	memset(p + offset, 0, sizeof(*class) - offset);
-	WARN_ON_ONCE(!class->lock_entry.next);
-	WARN_ON_ONCE(!list_empty(&class->locks_after));
-	WARN_ON_ONCE(!list_empty(&class->locks_before));
  }
 
Quoted 9 lines
Quoted 4 lines
kernel/locking/lockdep.c +0 -0
  static inline int within(const void *addr, void *start, unsigned long size)
Signature · 1 line
2.27.0

Yeah, except that this doesn't build because it conflicts with a previous patch you actually Acked:

https://lore.kernel.org/all/[email protected]/T/#m0e9474a9204ec29fb2135135ce3134640a3f27b5

Sorry, my bad. I forgot to check tip.

I have just sent out a v2 that will work on tip.

Cheers,
Longman