Skip to contentSkip to conversation

lockdep: lock_set_cmp_fn()

PATCH Applied Kent Overstreet 5 participants 12 messages May 9, 2023 – May 25, 2023 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Kent Overstreet

author maintainer
#0 lore raw

PATCH 1/2 lockdep: lock_set_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 |   8 +++
 kernel/locking/lockdep.c      | 115 +++++++++++++++++++++++++---------
 3 files changed, 101 insertions(+), 30 deletions(-)
include/linux/lockdep.h +8 -0
index 1023f349af..2b4497ee3d 100644
@@ -379,6 +379,14 @@ static inline void lockdep_set_selftest_task(struct task_struct *task)
 		do { (void)(key); } while (0)
 #define lockdep_set_subclass(lock, sub)		do { } while (0)
 
+#ifdef CONFIG_PROVE_LOCKING
+void lockdep_set_lock_cmp_fn(struct lockdep_map *, lock_cmp_fn, lock_print_fn);
+
+#define lock_set_cmp_fn(lock, ...)	lockdep_set_lock_cmp_fn(&(lock)->dep_map, __VA_ARGS__)
+#else
+#define lock_set_cmp_fn(lock, ...)	do {} while (0)
+#endif
+
 #define lockdep_set_novalidate_class(lock) do { } while (0)
 
 /*
include/linux/lockdep_types.h +8 -0
index d22430840b..8bf79c4e48 100644
@@ -84,6 +84,11 @@ 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);
+typedef void (*lock_print_fn)(const struct lockdep_map *map);
+
 /*
  * The lock-class itself. The order of the structure members matters.
  * reinit_class() zeroes the key member and all subsequent members.
@@ -109,6 +114,9 @@ struct lock_class {
 	struct list_head		locks_after, locks_before;
 
 	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 +85 -30

283 lines not on this page · raw message

Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 2/2 bcache: Convert to lock_cmp_fn

---
 drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
 drivers/md/bcache/btree.h |  4 ++--
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +22 -1
index 147c493a98..569f48958b 100644
@@ -559,6 +559,27 @@ 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);
+}
+
+static void btree_lock_print_fn(const struct lockdep_map *map)
+{
+	const struct btree *b = container_of(map, struct btree, lock.dep_map);
+
+	printk(KERN_CONT " l=%u %llu:%llu", b->level,
+	       KEY_INODE(&b->key), KEY_OFFSET(&b->key));
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
 				      struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +593,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_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_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.40.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] bcache: Convert to lock_cmp_fn

Quoted 6 lines

2023年5月10日 03:58,Kent Overstreet <[email protected]> 写道:

Acked-by Coly Li <[email protected] <mailto:[email protected]>>

Thanks for this change :-)

Coly Li

Quoted 64 lines

---
drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++- drivers/md/bcache/btree.h | 4 ++--

2 files changed, 24 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +22 -1
index 147c493a98..569f48958b 100644
@@ -559,6 +559,27 @@ 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);
+}
+
+static void btree_lock_print_fn(const struct lockdep_map *map)
+{
+ const struct btree *b = container_of(map, struct btree, lock.dep_map);
+
+ printk(KERN_CONT " l=%u %llu:%llu", b->level,
+       KEY_INODE(&b->key), KEY_OFFSET(&b->key));
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
     struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +593,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_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_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.40.1
maintainer
#3 lore raw

Re: [PATCH 2/2] bcache: Convert to lock_cmp_fn

In reply to Kent Overstreet
Quoted 40 lines · Kent Overstreet
---
 drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
 drivers/md/bcache/btree.h |  4 ++--
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +21 -0
index 147c493a98..569f48958b 100644
@@ -559,6 +559,27 @@ 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);
+}
+
+static void btree_lock_print_fn(const struct lockdep_map *map)
+{
+	const struct btree *b = container_of(map, struct btree, lock.dep_map);
+
+	printk(KERN_CONT " l=%u %llu:%llu", b->level,
+	       KEY_INODE(&b->key), KEY_OFFSET(&b->key));
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
 				      struct bkey *k, gfp_t gfp)
 {

drivers/md/bcache/btree.c static struct btree *mca_bucket_alloc(struct cache_set *c, · line 598

Quoted 7 lines
drivers/md/bcache/btree.c +1 -1
@@ -572,7 +593,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_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_fn);
 	mutex_init(&b->write_lock);
 	lockdep_set_novalidate_class(&b->write_lock);

I can't help but notice you've got yet another novalidate_class usage here. What does it take to get rid of that?

author maintainer
#4 lore raw

Re: [PATCH 2/2] bcache: Convert to lock_cmp_fn

Quoted 43 lines · Peter Zijlstra
Quoted 42 lines · Kent Overstreet
---
 drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
 drivers/md/bcache/btree.h |  4 ++--
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +21 -0
index 147c493a98..569f48958b 100644
@@ -559,6 +559,27 @@ 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);
+}
+
+static void btree_lock_print_fn(const struct lockdep_map *map)
+{
+	const struct btree *b = container_of(map, struct btree, lock.dep_map);
+
+	printk(KERN_CONT " l=%u %llu:%llu", b->level,
+	       KEY_INODE(&b->key), KEY_OFFSET(&b->key));
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
 				      struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +593,7 @@ static struct btree *mca_bucket_alloc(struct cache_set *c,
 		return NULL;
 
Quoted 8 lines

drivers/md/bcache/btree.c static struct btree *mca_bucket_alloc(struct cache_set *c, · line 598

Quoted 5 lines
drivers/md/bcache/btree.c +1 -1
 	init_rwsem(&b->lock);
-	lockdep_set_novalidate_class(&b->lock);
+	lock_set_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_fn);
 	mutex_init(&b->write_lock);
 	lockdep_set_novalidate_class(&b->write_lock);

I can't help but notice you've got yet another novalidate_class usage here. What does it take to get rid of that?

this is a tricky one, because the correct lock ordering refers to particular locks of different types; we take b->lock before b->write_lock, for a given btree node.

And like b->lock, b->write_lock can be held simultaneously for multiple nodes, with the same ordering that btree_lock_cmp_fn() defines.

Conceptually we'd need a lock_cmp_fn that can compare locks of different types...

This patchset might be almost enough to do that, I'll give it a bit more thought.

author maintainer
#5 lore raw

Re: [PATCH 2/2] bcache: Convert to lock_cmp_fn

In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static struct btree *mca_bucket_alloc(struct cache_set *c,

drivers/md/bcache/btree.c +0 -0
 				      struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +593,7 @@ static struct btree *mca_bucket_alloc(struct cache_set *c,
 		return NULL;
 
Quoted 8 lines

drivers/md/bcache/btree.c static struct btree *mca_bucket_alloc(struct cache_set *c, · line 598

Quoted 5 lines
drivers/md/bcache/btree.c +1 -1
 	init_rwsem(&b->lock);
-	lockdep_set_novalidate_class(&b->lock);
+	lock_set_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_fn);
 	mutex_init(&b->write_lock);
 	lockdep_set_novalidate_class(&b->write_lock);

I can't help but notice you've got yet another novalidate_class usage here. What does it take to get rid of that?

A locking rework, probably switching bcache to six locks.

I'd rather prioritize getting bcachefs merged and then working on an upgrade path from bcache -> bcahcefs so we can depracate bcache, though...

A message that is not in the archive (sent off-list or not yet received): 168457974565.404.16611061652498882569.tip-bot2@tip-bot2

#7 lore raw

Re: [tip: locking/core] bcache: Convert to lock_cmp_fn

Quoted 15 lines

2023年5月20日 18:49,tip-bot2 for Kent Overstreet <[email protected]> 写道:

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

Commit-ID:     0ad397b556936a14052aa65d8fa958a9f3175add
Gitweb:        https://git.kernel.org/tip/0ad397b556936a14052aa65d8fa958a9f3175add
Author:        Kent Overstreet <[email protected]>
AuthorDate:    Tue, 09 May 2023 15:58:47 -04:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 19 May 2023 12:35:10 +02:00

bcache: Convert to lock_cmp_fn

Quoted 7 lines

Replace one of bcache's lockdep_set_novalidate_class() usage with the newly introduced custom lock nesting annotation.

Can the above “<mailto:[email protected]>” be removed from my acked-by. This was automatically and invisibly added by MacOS email client, which just introduced chaos in such use case.

Thanks.

Coly Li

Quoted 62 lines

---
drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++- drivers/md/bcache/btree.h | 4 ++--

2 files changed, 24 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +22 -1
index 147c493..569f489 100644
@@ -559,6 +559,27 @@ 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);
+}
+
+static void btree_lock_print_fn(const struct lockdep_map *map)
+{
+ const struct btree *b = container_of(map, struct btree, lock.dep_map);
+
+ printk(KERN_CONT " l=%u %llu:%llu", b->level,
+       KEY_INODE(&b->key), KEY_OFFSET(&b->key));
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
     struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +593,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_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_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 1b5fdbc..17b1d20 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++;
 }
maintainer
#8 lore raw

Re: [tip: locking/core] bcache: Convert to lock_cmp_fn

Quoted 24 lines · Coly Li
Quoted 15 lines

2023年5月20日 18:49,tip-bot2 for Kent Overstreet <[email protected]> 写道:

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

Commit-ID:     0ad397b556936a14052aa65d8fa958a9f3175add
Gitweb:        https://git.kernel.org/tip/0ad397b556936a14052aa65d8fa958a9f3175add
Author:        Kent Overstreet <[email protected]>
AuthorDate:    Tue, 09 May 2023 15:58:47 -04:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 19 May 2023 12:35:10 +02:00

bcache: Convert to lock_cmp_fn

Quoted 7 lines

Replace one of bcache's lockdep_set_novalidate_class() usage with the newly introduced custom lock nesting annotation.

Quoted 3 lines

Can the above “<mailto:[email protected]>” be removed from my acked-by. This was automatically and invisibly added by MacOS email client, which just introduced chaos in such use case.

Thanks.

Urgh, something I should add to my script on a rainy day I suppose.

I'll have to rebase the tree, but I suppose I can do that. Let me go see if I can remember the git incantations required.

#9 lore raw

[tip: locking/core] bcache: Convert to lock_cmp_fn

In reply to Kent Overstreet
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:     4c8a49244c6abc5fb829d81abaaf2435ad2a44bf
Gitweb:        https://git.kernel.org/tip/4c8a49244c6abc5fb829d81abaaf2435ad2a44bf
Author:        Kent Overstreet <[email protected]>
AuthorDate:    Tue, 09 May 2023 15:58:47 -04:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 24 May 2023 12:21:22 +02:00

bcache: Convert to lock_cmp_fn

Replace one of bcache's lockdep_set_novalidate_class() usage with the newly introduced custom lock nesting annotation.

---
 drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
 drivers/md/bcache/btree.h |  4 ++--
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/md/bcache/btree.c +22 -1
index 147c493..569f489 100644
@@ -559,6 +559,27 @@ 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);
+}
+
+static void btree_lock_print_fn(const struct lockdep_map *map)
+{
+	const struct btree *b = container_of(map, struct btree, lock.dep_map);
+
+	printk(KERN_CONT " l=%u %llu:%llu", b->level,
+	       KEY_INODE(&b->key), KEY_OFFSET(&b->key));
+}
+#endif
+
 static struct btree *mca_bucket_alloc(struct cache_set *c,
 				      struct bkey *k, gfp_t gfp)
 {
@@ -572,7 +593,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_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_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 1b5fdbc..17b1d20 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++;
 }
#10 lore raw
In reply to Kent Overstreet
kernel test robot noticed the following build warnings:

Hi Kent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/locking/core]
[cannot apply to linus/master v6.4-rc1 next-20230509]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kent-Overstreet/bcache-Convert-to-lock_cmp_fn/20230510-040030
base:   tip/locking/core
patch link:    https://lore.kernel.org/r/20230509195847.1745548-1-kent.overstreet%40linux.dev
patch subject: [PATCH 1/2] lockdep: lock_set_cmp_fn()
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230510/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/f265373c48cadb6f81e032df9893abffc370ab89
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Kent-Overstreet/bcache-Convert-to-lock_cmp_fn/20230510-040030
git checkout f265373c48cadb6f81e032df9893abffc370ab89
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash drivers/md/bcache/ kernel/locking/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

kernel/locking/lockdep.c: In function 'print_chain_keys_chain':
kernel/locking/lockdep.c:3592:46: error: passing argument 1 of 'print_lock_name' from incompatible pointer type [-Werror=incompatible-pointer-types]

    3592 |                 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
         |                                 ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                              |
         |                                              struct lock_class *
   kernel/locking/lockdep.c:732:47: note: expected 'struct held_lock *' but argument is of type 'struct lock_class *'
     732 | static void print_lock_name(struct held_lock *hlock, struct lock_class *class)
         |                             ~~~~~~~~~~~~~~~~~~^~~~~
   kernel/locking/lockdep.c:3592:17: error: too few arguments to function 'print_lock_name'
    3592 |                 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
         |                 ^~~~~~~~~~~~~~~
   kernel/locking/lockdep.c:732:13: note: declared here
     732 | static void print_lock_name(struct held_lock *hlock, struct lock_class *class)
         |             ^~~~~~~~~~~~~~~
   kernel/locking/lockdep.c: At top level:
>> kernel/locking/lockdep.c:4898:6: warning: no previous prototype for 'lockdep_set_lock_cmp_fn' [-Wmissing-prototypes]

4898 | void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn,
| ^~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +/lockdep_set_lock_cmp_fn +4898 kernel/locking/lockdep.c

4896
4897 #ifdef CONFIG_PROVE_LOCKING

> 4898	void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn,

4899 lock_print_fn print_fn)
4900 {
4901 struct lock_class *class = lock->class_cache[0];
4902 unsigned long flags;
4903
4904 raw_local_irq_save(flags);
4905 lockdep_recursion_inc();
4906
4907 if (!class)
4908 class = register_lock_class(lock, 0, 0);
4909
4910 if (class) {
4911 WARN_ON(class->cmp_fn && class->cmp_fn != cmp_fn);
4912 WARN_ON(class->print_fn && class->print_fn != print_fn);
4913
4914 class->cmp_fn = cmp_fn;
4915 class->print_fn = print_fn;
4916 }
4917
4918 lockdep_recursion_finish();
4919 raw_local_irq_restore(flags);
4920 }
4921 EXPORT_SYMBOL_GPL(lockdep_set_lock_cmp_fn);
4922 #endif
4923

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#11 lore raw
In reply to Kent Overstreet
kernel test robot noticed the following build errors:

Hi Kent,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/locking/core]
[cannot apply to linus/master v6.4-rc1 next-20230509]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kent-Overstreet/bcache-Convert-to-lock_cmp_fn/20230510-040030
base:   tip/locking/core
patch link:    https://lore.kernel.org/r/20230509195847.1745548-1-kent.overstreet%40linux.dev
patch subject: [PATCH 1/2] lockdep: lock_set_cmp_fn()
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230510/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/f265373c48cadb6f81e032df9893abffc370ab89
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Kent-Overstreet/bcache-Convert-to-lock_cmp_fn/20230510-040030
git checkout f265373c48cadb6f81e032df9893abffc370ab89
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash drivers/md/ kernel/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/locking/lockdep.c: In function 'print_chain_keys_chain':

>> kernel/locking/lockdep.c:3592:46: error: passing argument 1 of 'print_lock_name' from incompatible pointer type [-Werror=incompatible-pointer-types]
    3592 |                 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
         |                                 ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                              |
         |                                              struct lock_class *
   kernel/locking/lockdep.c:732:47: note: expected 'struct held_lock *' but argument is of type 'struct lock_class *'
     732 | static void print_lock_name(struct held_lock *hlock, struct lock_class *class)
         |                             ~~~~~~~~~~~~~~~~~~^~~~~
>> kernel/locking/lockdep.c:3592:17: error: too few arguments to function 'print_lock_name'
    3592 |                 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
         |                 ^~~~~~~~~~~~~~~

kernel/locking/lockdep.c:732:13: note: declared here
732 | static void print_lock_name(struct held_lock *hlock, struct lock_class *class)
| ^~~~~~~~~~~~~~~
kernel/locking/lockdep.c: At top level:
kernel/locking/lockdep.c:4898:6: warning: no previous prototype for 'lockdep_set_lock_cmp_fn' [-Wmissing-prototypes]
4898 | void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn,
| ^~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +/print_lock_name +3592 kernel/locking/lockdep.c

39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3580  
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3581  static void print_chain_keys_chain(struct lock_chain *chain)
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3582  {
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3583  	int i;
f6ec8829ac9d59 Yuyang Du                 2019-05-06  3584  	u64 chain_key = INITIAL_CHAIN_KEY;
f611e8cf98ec90 Boqun Feng                2020-08-07  3585  	u16 hlock_id;
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3586  
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3587  	printk("depth: %u\n", chain->depth);
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3588  	for (i = 0; i < chain->depth; i++) {
f611e8cf98ec90 Boqun Feng                2020-08-07  3589  		hlock_id = chain_hlocks[chain->base + i];
f611e8cf98ec90 Boqun Feng                2020-08-07  3590  		chain_key = print_chain_key_iteration(hlock_id, chain_key);
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3591  
28df029d53a2fd Cheng Jui Wang            2022-02-10 @3592  		print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3593  		printk("\n");
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3594  	}
39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30  3595  }

39e2e173fb1f90 Alfredo Alvarez Fernandez 2016-03-30 3596

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#12 lore raw

[tip: locking/core] lockdep: Add lock_set_cmp_fn() annotation

In reply to Kent Overstreet
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:     eb1cfd09f788e39948a82be8063e54e40dd018d9
Gitweb:        https://git.kernel.org/tip/eb1cfd09f788e39948a82be8063e54e40dd018d9
Author:        Kent Overstreet <[email protected]>
AuthorDate:    Tue, 09 May 2023 15:58:46 -04:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 19 May 2023 12:35:10 +02:00

lockdep: Add lock_set_cmp_fn() annotation

This implements a new interface to lockdep, lock_set_cmp_fn(), for defining a custom ordering when taking multiple locks of the same class.

This is an alternative to subclasses, but can not fully replace them since subclasses allow lock hierarchies with other clasees inter-twined, while this relies on pure class nesting.

Specifically, if A is our nesting class then:

A/0 <- B <- A/1

Would be a valid lock order with subclasses (each subclass really is a full class from the validation PoV) but not with this annotation, which requires all nesting to be consecutive.

Example output:

| ============================================
| 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
---
 include/linux/lockdep.h       |   8 ++-
 include/linux/lockdep_types.h |   8 ++-
 kernel/locking/lockdep.c      | 118 ++++++++++++++++++++++++---------
 3 files changed, 103 insertions(+), 31 deletions(-)
include/linux/lockdep.h +8 -0
index b32256e..3bac150 100644
@@ -434,6 +434,14 @@ extern int lockdep_is_held(const void *);
 
 #endif /* !LOCKDEP */
 
+#ifdef CONFIG_PROVE_LOCKING
+void lockdep_set_lock_cmp_fn(struct lockdep_map *, lock_cmp_fn, lock_print_fn);
+
+#define lock_set_cmp_fn(lock, ...)	lockdep_set_lock_cmp_fn(&(lock)->dep_map, __VA_ARGS__)
+#else
+#define lock_set_cmp_fn(lock, ...)	do { } while (0)
+#endif
+
 enum xhlock_context_t {
 	XHLOCK_HARD,
 	XHLOCK_SOFT,
include/linux/lockdep_types.h +8 -0
index d224308..8bf79c4 100644
@@ -84,6 +84,11 @@ 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);
+typedef void (*lock_print_fn)(const struct lockdep_map *map);
+
 /*
  * The lock-class itself. The order of the structure members matters.
  * reinit_class() zeroes the key member and all subsequent members.
@@ -109,6 +114,9 @@ struct lock_class {
 	struct list_head		locks_after, locks_before;
 
 	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 +87 -31

296 lines not on this page · raw message