2023-02-18 03:21:30

by Kent Overstreet

[permalink] [raw]
Subject: [PATCH 0/2] 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(-)

--
2.39.2



2023-02-18 03:21:35

by Kent Overstreet

[permalink] [raw]
Subject: [PATCH 2/2] bcache: Convert to lock_cmp_fn

Signed-off-by: Kent Overstreet <[email protected]>
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(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 147c493a98..db2bf2402c 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -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);
diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index 1b5fdbc0d8..17b1d201ce 100644
--- a/drivers/md/bcache/btree.h
+++ b/drivers/md/bcache/btree.h
@@ -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++;
}
--
2.39.2