Skip to contentSkip to conversation

lockdep lock comparison function

PATCH In review Kent Overstreet 2 participants 8 messages Feb 18, 2023 – Feb 23, 2023

Subsystems (2)
lore
Share & tools
lore

Kent Overstreet

author maintainer
#0 lore raw

cover lockdep lock comparison function

This patch implements the lock comparison function I've been talking about, and converts one of bcache's locks to use it.

b->write_lock has different locking rules; I'm not sure there's an easy way to get rid of lockdep_set_novalidate_class for it - but the code has changed and my memory is foggy.

I'd like it if we could convert existing uses of *_lock_nested() to this approach, since it's more rigorous and IMO, much clearer. That'll require looking at specific use cases, though - the inode lock in fs/inode.c is the only one I looked at and it's got a lot of nutty stuff going on.

Kent Overstreet (2):
lockdep: lock_set_lock_cmp_fn()
bcache: Convert to lock_cmp_fn

 drivers/md/bcache/btree.c     | 15 ++++++++++-
 drivers/md/bcache/btree.h     |  4 +--
 include/linux/lockdep.h       |  8 ++++++
 include/linux/lockdep_types.h |  6 +++++
 kernel/locking/lockdep.c      | 51 ++++++++++++++++++++++++++++++++++-
 5 files changed, 80 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH 1/2 lockdep: lock_set_lock_cmp_fn()

This implements a new interface to lockedp, lock_set_cmp_fn(), for defining an ordering when taking multiple locks of the same type.

This provides a more rigorous mechanism than the subclass mechanism - it's hoped that this might be able to replace subclasses.

Cc Peter Zijlstra <[email protected]> (maintainer:LOCKING PRIMITIVES)
Cc Ingo Molnar <[email protected]> (maintainer:LOCKING PRIMITIVES)
---
 include/linux/lockdep.h       |  8 ++++++
 include/linux/lockdep_types.h |  6 +++++
 kernel/locking/lockdep.c      | 51 ++++++++++++++++++++++++++++++++++-
 3 files changed, 64 insertions(+), 1 deletion(-)
include/linux/lockdep.h +8 -0
index 1f1099dac3..a4673a6f7a 100644
@@ -662,4 +662,12 @@ lockdep_rcu_suspicious(const char *file, const int line, const char *s)
 }
 #endif
 
+#ifdef CONFIG_PROVE_LOCKING
+void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn fn);
+
+#define lock_set_lock_cmp_fn(lock, fn)		lockdep_set_lock_cmp_fn(&(lock)->dep_map, fn)
+#else
+#define lock_set_lock_cmp_fn(lock, fn)		do {} while (0)
+#endif
+
 #endif /* __LINUX_LOCKDEP_H */
include/linux/lockdep_types.h +6 -0
index d22430840b..813467dccb 100644
@@ -84,6 +84,10 @@ struct lock_trace;
 
 #define LOCKSTAT_POINTS		4
 
+struct lockdep_map;
+typedef int (*lock_cmp_fn)(const struct lockdep_map *a,
+			   const struct lockdep_map *b);
+
 /*
  * The lock-class itself. The order of the structure members matters.
  * reinit_class() zeroes the key member and all subsequent members.
@@ -109,6 +113,8 @@ struct lock_class {
 	struct list_head		locks_after, locks_before;
 
 	const struct lockdep_subclass_key *key;
+	lock_cmp_fn			cmp_fn;
+
 	unsigned int			subclass;
 	unsigned int			dep_gen_id;
 
kernel/locking/lockdep.c +50 -1

96 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#2 lore raw

Re: [PATCH 1/2] lockdep: lock_set_lock_cmp_fn()

Review print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, · line 2995

Quoted 8 lines · Kent Overstreet
(quoted diff) +4 -0
@@ -2982,6 +2989,10 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
 	pr_warn("\nbut task is already holding lock:\n");
 	print_lock(prev);
 
+	if (class->cmp_fn)
+		pr_warn("and the lock comparison function returns %i:\n",
+			class->cmp_fn(prev->instance, next->instance));
+

Please, use {} for any actual multi-line.

But is this sufficient data to debug a splat? Given an inversion on this class, we'll get something like:

A
A -a
A -b

vs

A
A c

which is I suppose sufficient to say that A messed up, but not much more. With subclasses we would've gotten

A/0
A/1
A/2

vs

A/2
A/0

which is much simpler to work with. Can we improve on this? Give the cmp_fn an additinoal optional argument of a string pointer or so to fill out with actual details to be printed?

Review void lockdep_rcu_suspicious(const char *file, const int line, const char *s) · line 6627

Quoted 10 lines
(quoted diff) +2 -0
 	pr_warn("\nother info that might help us debug this:\n");
 	print_deadlock_scenario(next, prev);
 	lockdep_print_held_locks(curr);
@@ -6597,3 +6623,26 @@ void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
 	dump_stack();
 }
 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
+
+#ifdef CONFIG_PROVE_LOCKING

Surely we can find an existing #ifdef block to squirrel this in?

Quoted 10 lines
(quoted diff) +10 -0
+void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn fn)
+{
+	struct lock_class *class = lock->class_cache[0];
+	unsigned long flags;
+
+	raw_local_irq_save(flags);
+	lockdep_recursion_inc();
+
+	if (!class)
+		class = register_lock_class(lock, 0, 0);

Review void lockdep_rcu_suspicious(const char *file, const int line, const char *s) · line 6647

Quoted 10 lines
(quoted diff) +10 -0
+
+	WARN_ON(class && class->cmp_fn && class->cmp_fn != fn);
+
+	if (class)
+		class->cmp_fn = fn;
+
+	lockdep_recursion_finish();
+	raw_local_irq_restore(flags);
+}
+EXPORT_SYMBOL_GPL(lockdep_set_lock_cmp_fn);

However, the bigger question is if this should be part of the lockdep_init_map_*() family. Perhaps something like below.

With the thinking that the comparator is very much part of the class.

include/linux/lockdep.h +12 -2
index 1f1099dac3f0..ab50a889991f 100644
@@ -181,8 +181,18 @@ extern void lockdep_unregister_key(struct lock_class_key *key);
  * to lockdep:
  */
 
-extern void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
-	struct lock_class_key *key, int subclass, u8 inner, u8 outer, u8 lock_type);
+extern void
+lockdep_init_map_cmp(struct lockdep_map *lock, const char *name,
+		     struct lock_class_key *key, int subclass, u8 inner, u8 outer,
+		     u8 lock_type, lock_cmp_fn cmp_fn);
+
+static inline void
+lockdep_init_map_type(struct lockdep_map *lock, const char *name,
+		      struct lock_class_key *key, int subclass, u8 inner, u8 outer,
+		      u8 lock_type)
+{
+	lockdep_init_map_cmp(lock, name, key, subclass, inner, outer, lock_type, NULL);
+}
 
 static inline void
 lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
kernel/locking/lockdep.c +9 -5
index e3375bc40dad..5a6c9512d5b2 100644
@@ -4800,9 +4800,10 @@ static inline int check_wait_context(struct task_struct *curr,
 /*
  * Initialize a lock instance's lock-class mapping info:
  */
-void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
-			    struct lock_class_key *key, int subclass,
-			    u8 inner, u8 outer, u8 lock_type)
+void lockdep_init_map_cmp(struct lockdep_map *lock, const char *name,
+			  struct lock_class_key *key, int subclass,
+			  u8 inner, u8 outer, u8 lock_type,
+			  lock_cmp_fn cmp_fn)
 {
 	int i;
 
@@ -4847,7 +4848,8 @@ void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
 	if (unlikely(!debug_locks))
 		return;
 
-	if (subclass) {
+	if (subclass || cmp_fn) {
+		struct lock_class *class;
 		unsigned long flags;
 
 		if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
@@ -4855,7 +4857,9 @@ void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
 
 		raw_local_irq_save(flags);
 		lockdep_recursion_inc();
-		register_lock_class(lock, subclass, 1);
+		class = register_lock_class(lock, subclass, 1);
+		if (!WARN_ON(!class || (class->cmp_fn && class->cmp_fn != cmp_fn)))
+			class->cmp_fn = cmp_fn;
 		lockdep_recursion_finish();
 		raw_local_irq_restore(flags);
 	}
author maintainer
#3 lore raw

Re: [PATCH 1/2] lockdep: lock_set_lock_cmp_fn()

Quoted 32 lines · Peter Zijlstra

Review print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, · line 2995

Quoted 8 lines · Kent Overstreet
(quoted diff) +4 -0
@@ -2982,6 +2989,10 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
 	pr_warn("\nbut task is already holding lock:\n");
 	print_lock(prev);
 
+	if (class->cmp_fn)
+		pr_warn("and the lock comparison function returns %i:\n",
+			class->cmp_fn(prev->instance, next->instance));
+

Please, use {} for any actual multi-line.

But is this sufficient data to debug a splat? Given an inversion on this class, we'll get something like:

A
A -a
A -b

vs

A
A c

which is I suppose sufficient to say that A messed up, but not much more. With subclasses we would've gotten

A/0
A/1
A/2

Quoted 8 lines

vs

A/2
A/0

which is much simpler to work with. Can we improve on this? Give the cmp_fn an additinoal optional argument of a string pointer or so to fill out with actual details to be printed?

Yes. This is where printbufs and %pf() would've been really nice, it'll be doable but ugly with what we have now for string output. We just need to add another callback, either .lock_print() or .lock_to_text(), and that can print the information about the lock that's relevant for lock ordering.

For bcache, that'd be something like

static void btree_lock_to_text()

{
	struct btree *b = container_of()

	seq_buf_printf("l=%u %llu:%llu", b->level, KEY_INODE(), KEY_OFFSET()...)
}
author maintainer
#4 lore raw

Re: [PATCH 1/2] lockdep: lock_set_lock_cmp_fn()

In reply to Peter Zijlstra
Quoted 3 lines · Peter Zijlstra

which is much simpler to work with. Can we improve on this? Give the cmp_fn an additinoal optional argument of a string pointer or so to fill out with actual details to be printed?

Here you go, example bcache output:

Patch is not as pretty as I'd like because not every path that prints a lock has held_lock available - but the ones we care about in this scenario do.

============================================
WARNING: possible recursive locking detected
6.2.0-rc8-00003-g7d81e591ca6a-dirty #15 Not tainted
--------------------------------------------
kworker/14:3/938 is trying to acquire lock:
ffff8880143218c8 (&b->lock l=0 0:2803368){++++}-{3:3}, at: bch_btree_node_get.part.0+0x81/0x2b0

but task is already holding lock:
ffff8880143de8c8 (&b->lock l=1 1048575:9223372036854775807){++++}-{3:3}, at: __bch_btree_map_nodes+0xea/0x1e0 and the lock comparison function returns 1:

other info that might help us debug this:

 Possible unsafe locking scenario:
       CPU0
       ----

lock(&b->lock l=1 1048575:9223372036854775807);
lock(&b->lock l=0 0:2803368);

*** DEADLOCK ***

May be due to missing lock nesting notation

3 locks held by kworker/14:3/938:
#0: ffff888005ea9d38 ((wq_completion)bcache){+.+.}-{0:0}, at: process_one_work+0x1ec/0x530
#1: ffff8880098c3e70 ((work_completion)(&cl->work)#3){+.+.}-{0:0}, at: process_one_work+0x1ec/0x530 #2: ffff8880143de8c8 (&b->lock l=1 1048575:9223372036854775807){++++}-{3:3}, at: __bch_btree_map_nodes+0xea/0x1e0

-- >8 --
Subject: [PATCH] lockdep: lock_set_print_fn()

This implements a new interface to lockedp, lock_set_print_fn(), for printing additional information about a lock.

This is intended to be useful with the previous lock_set_cmp_fn(); when defininig an ordering for locks of a given type, we'll want to print out information about that lock that's relevant for the ordering we defined.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/lockdep.h       |  3 ++
 include/linux/lockdep_types.h |  2 +
 kernel/locking/lockdep.c      | 81 ++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 29 deletions(-)
include/linux/lockdep.h +3 -0
index 98e0338a74..66d8a5a24e 100644
@@ -664,10 +664,13 @@ lockdep_rcu_suspicious(const char *file, const int line, const char *s)
 
 #ifdef CONFIG_PROVE_LOCKING
 void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn fn);
+void lockdep_set_lock_print_fn(struct lockdep_map *lock, lock_print_fn fn);
 
 #define lock_set_cmp_fn(lock, fn)	lockdep_set_lock_cmp_fn(&(lock)->dep_map, fn)
+#define lock_set_print_fn(lock, fn)	lockdep_set_lock_print_fn(&(lock)->dep_map, fn)
 #else
 #define lock_set_cmp_fn(lock, fn)	do {} while (0)
+#define lock_set_print_fn(lock, fn)	do {} while (0)
 #endif
 
 #endif /* __LINUX_LOCKDEP_H */
include/linux/lockdep_types.h +2 -0
index 813467dccb..8bf79c4e48 100644
@@ -87,6 +87,7 @@ struct lock_trace;
 struct lockdep_map;
 typedef int (*lock_cmp_fn)(const struct lockdep_map *a,
 			   const struct lockdep_map *b);
+typedef void (*lock_print_fn)(const struct lockdep_map *map);
 
 /*
  * The lock-class itself. The order of the structure members matters.
@@ -114,6 +115,7 @@ struct lock_class {
 
 	const struct lockdep_subclass_key *key;
 	lock_cmp_fn			cmp_fn;
+	lock_print_fn			print_fn;
 
 	unsigned int			subclass;
 	unsigned int			dep_gen_id;
kernel/locking/lockdep.c +52 -29

205 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#5 lore raw

Re: [PATCH 1/2] lockdep: lock_set_lock_cmp_fn()

Quoted 10 lines · Kent Overstreet
Quoted 3 lines · Peter Zijlstra

which is much simpler to work with. Can we improve on this? Give the cmp_fn an additinoal optional argument of a string pointer or so to fill out with actual details to be printed?

Here you go, example bcache output:

Patch is not as pretty as I'd like because not every path that prints a lock has held_lock available - but the ones we care about in this scenario do.

Right, unavoidable that.

Quoted 19 lines

============================================
WARNING: possible recursive locking detected
6.2.0-rc8-00003-g7d81e591ca6a-dirty #15 Not tainted
--------------------------------------------
kworker/14:3/938 is trying to acquire lock:
ffff8880143218c8 (&b->lock l=0 0:2803368){++++}-{3:3}, at: bch_btree_node_get.part.0+0x81/0x2b0

but task is already holding lock:
ffff8880143de8c8 (&b->lock l=1 1048575:9223372036854775807){++++}-{3:3}, at: __bch_btree_map_nodes+0xea/0x1e0 and the lock comparison function returns 1:

other info that might help us debug this:

 Possible unsafe locking scenario:
       CPU0
       ----

lock(&b->lock l=1 1048575:9223372036854775807);
lock(&b->lock l=0 0:2803368);

Quoted 8 lines

*** DEADLOCK ***

May be due to missing lock nesting notation

3 locks held by kworker/14:3/938:
#0: ffff888005ea9d38 ((wq_completion)bcache){+.+.}-{0:0}, at: process_one_work+0x1ec/0x530
#1: ffff8880098c3e70 ((work_completion)(&cl->work)#3){+.+.}-{0:0}, at: process_one_work+0x1ec/0x530 #2: ffff8880143de8c8 (&b->lock l=1 1048575:9223372036854775807){++++}-{3:3}, at: __bch_btree_map_nodes+0xea/0x1e0

Much better indeed. Thanks!

Quoted 24 lines

-- >8 --
Subject: [PATCH] lockdep: lock_set_print_fn()

This implements a new interface to lockedp, lock_set_print_fn(), for printing additional information about a lock.

This is intended to be useful with the previous lock_set_cmp_fn(); when defininig an ordering for locks of a given type, we'll want to print out information about that lock that's relevant for the ordering we defined.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/lockdep.h       |  3 ++
 include/linux/lockdep_types.h |  2 +
 kernel/locking/lockdep.c      | 81 ++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 29 deletions(-)
include/linux/lockdep.h +0 -0
index 98e0338a74..66d8a5a24e 100644

include/linux/lockdep.h lockdep_rcu_suspicious(const char *file, const int line, const char *s) · line 667

Quoted 4 lines
include/linux/lockdep.h +1 -0
@@ -664,10 +664,13 @@ lockdep_rcu_suspicious(const char *file, const int line, const char *s)
 
 #ifdef CONFIG_PROVE_LOCKING
 void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn fn);
+void lockdep_set_lock_print_fn(struct lockdep_map *lock, lock_print_fn fn);

I would suggest the same as last time; integrate this in the class setting zoo of functions. If you insiste, please keep it one function and force print_fn when cmp_fn. We don't want people to skimp out on this.

Other than that, I don't think this can fully replace subclasses, since subclasses would allow lock hierarchies with other classes inter-twined, while this really relies on pure class nesting.

That is:

	A/0
	B
	A/1

is a valid subclass nesting set, but you can't achieve the same with this.

That said; it does seem like a very useful additional annotation for the more complex nesting sets.

Thanks!

author maintainer
#6 lore raw

Re: [PATCH 1/2] lockdep: lock_set_lock_cmp_fn()

Quoted 2 lines · Peter Zijlstra

I would suggest the same as last time; integrate this in the class setting zoo of functions.

I don't really see a particular zoo, things like pretty scattered to me. With lockdep_init_map_type?

Quoted 3 lines

If you insiste, please keep it one function
and force print_fn when cmp_fn. We don't want people to skimp out on this.

That's easily done.

Quoted 5 lines

Other than that, I don't think this can fully replace subclasses, since subclasses would allow lock hierarchies with other classes inter-twined, while this really relies on pure class nesting.

Quoted 8 lines

That is:

	A/0
	B
	A/1

is a valid subclass nesting set, but you can't achieve the same with this.

Why not just assign those As to two different classes?

Quoted 4 lines

That said; it does seem like a very useful additional annotation for the more complex nesting sets.

Thanks!

Cheers

author maintainer
#7 lore raw

PATCH 2/2 bcache: Convert to lock_cmp_fn

In reply to Kent Overstreet
Cc Coly Li <[email protected]> (maintainer:BCACHE (BLOCK LAYER CACHE))
---
 drivers/md/bcache/btree.c | 15 ++++++++++++++-
 drivers/md/bcache/btree.h |  4 ++--
 2 files changed, 16 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +14 -1
index 147c493a98..db2bf2402c 100644
@@ -559,6 +559,19 @@ static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp)
 	}
 }
 
+#define cmp_int(l, r)		((l > r) - (l < r))
+
+#ifdef CONFIG_PROVE_LOCKING
+static int btree_lock_cmp_fn(const struct lockdep_map *_a,
+			     const struct lockdep_map *_b)
+{
+	const struct btree *a = container_of(_a, struct btree, lock.dep_map);
+	const struct btree *b = container_of(_b, struct btree, lock.dep_map);
+
+	return -cmp_int(a->level, b->level) ?: bkey_cmp(&a->key, &b->key);
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
 				      struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +585,7 @@ static struct btree *mca_bucket_alloc(struct cache_set *c,
 		return NULL;
 
 	init_rwsem(&b->lock);
-	lockdep_set_novalidate_class(&b->lock);
+	lock_set_lock_cmp_fn(&b->lock, btree_lock_cmp_fn);
 	mutex_init(&b->write_lock);
 	lockdep_set_novalidate_class(&b->write_lock);
 	INIT_LIST_HEAD(&b->list);
drivers/md/bcache/btree.h +2 -2
index 1b5fdbc0d8..17b1d201ce 100644
@@ -247,8 +247,8 @@ static inline void bch_btree_op_init(struct btree_op *op, int write_lock_level)
 
 static inline void rw_lock(bool w, struct btree *b, int level)
 {
-	w ? down_write_nested(&b->lock, level + 1)
-	  : down_read_nested(&b->lock, level + 1);
+	w ? down_write(&b->lock)
+	  : down_read(&b->lock);
 	if (w)
 		b->seq++;
 }
Signature · 1 line
2.39.2