2023-05-09 20:02:14

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]>
---
drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
drivers/md/bcache/btree.h | 4 ++--
2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 147c493a98..569f48958b 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -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);
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.40.1


2023-05-10 05:26:50

by Coly Li

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



> 2023年5月10日 03:58,Kent Overstreet <[email protected]> 写道:
>
> Signed-off-by: Kent Overstreet <[email protected]>
> Cc: Coly Li <[email protected]>

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

Thanks for this change :-)

Coly Li


> ---
> drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
> drivers/md/bcache/btree.h | 4 ++--
> 2 files changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
> index 147c493a98..569f48958b 100644
> --- a/drivers/md/bcache/btree.c
> +++ b/drivers/md/bcache/btree.c
> @@ -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);
> 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.40.1
>

2023-05-10 13:16:04

by Peter Zijlstra

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

On Tue, May 09, 2023 at 03:58:47PM -0400, Kent Overstreet wrote:
> Signed-off-by: Kent Overstreet <[email protected]>
> Cc: Coly Li <[email protected]>
> ---
> drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
> drivers/md/bcache/btree.h | 4 ++--
> 2 files changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
> index 147c493a98..569f48958b 100644
> --- a/drivers/md/bcache/btree.c
> +++ b/drivers/md/bcache/btree.c
> @@ -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);

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

2023-05-10 17:27:16

by Kent Overstreet

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

On Wed, May 10, 2023 at 03:01:51PM +0200, Peter Zijlstra wrote:
> On Tue, May 09, 2023 at 03:58:47PM -0400, Kent Overstreet wrote:
> > Signed-off-by: Kent Overstreet <[email protected]>
> > Cc: Coly Li <[email protected]>
> > ---
> > drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
> > drivers/md/bcache/btree.h | 4 ++--
> > 2 files changed, 24 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
> > index 147c493a98..569f48958b 100644
> > --- a/drivers/md/bcache/btree.c
> > +++ b/drivers/md/bcache/btree.c
> > @@ -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);
>
> 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.

2023-05-24 11:23:46

by tip-bot2 for Jacob Pan

[permalink] [raw]
Subject: [tip: locking/core] bcache: Convert to lock_cmp_fn

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.

[peterz: changelog]
Signed-off-by: Kent Overstreet <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Coly Li <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
drivers/md/bcache/btree.c | 23 ++++++++++++++++++++++-
drivers/md/bcache/btree.h | 4 ++--
2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 147c493..569f489 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -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);
diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index 1b5fdbc..17b1d20 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++;
}

2023-05-25 05:52:00

by Kent Overstreet

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

On Wed, May 10, 2023 at 03:01:51PM +0200, Peter Zijlstra wrote:
> > 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);
>
> 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...