[PATCH 09/29] bcache: dynamically allocate the md-bcache shrinker

From: Qi Zheng
Date: Thu Jun 22 2023 - 04:42:18 EST


From: Qi Zheng <zhengqi.arch@xxxxxxxxxxxxx>

In preparation for implementing lockless slab shrink,
we need to dynamically allocate the md-bcache shrinker,
so that it can be freed asynchronously using kfree_rcu().
Then it doesn't need to wait for RCU read-side critical
section when releasing the struct cache_set.

Signed-off-by: Qi Zheng <zhengqi.arch@xxxxxxxxxxxxx>
---
drivers/md/bcache/bcache.h | 2 +-
drivers/md/bcache/btree.c | 23 ++++++++++++++---------
drivers/md/bcache/sysfs.c | 2 +-
3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 700dc5588d5f..53c73b372e7a 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -541,7 +541,7 @@ struct cache_set {
struct bio_set bio_split;

/* For the btree cache */
- struct shrinker shrink;
+ struct shrinker *shrink;

/* For the btree cache and anything allocation related */
struct mutex bucket_lock;
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 569f48958bde..1131ae91f62a 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -667,7 +667,7 @@ static int mca_reap(struct btree *b, unsigned int min_order, bool flush)
static unsigned long bch_mca_scan(struct shrinker *shrink,
struct shrink_control *sc)
{
- struct cache_set *c = container_of(shrink, struct cache_set, shrink);
+ struct cache_set *c = shrink->private_data;
struct btree *b, *t;
unsigned long i, nr = sc->nr_to_scan;
unsigned long freed = 0;
@@ -734,7 +734,7 @@ static unsigned long bch_mca_scan(struct shrinker *shrink,
static unsigned long bch_mca_count(struct shrinker *shrink,
struct shrink_control *sc)
{
- struct cache_set *c = container_of(shrink, struct cache_set, shrink);
+ struct cache_set *c = shrink->private_data;

if (c->shrinker_disabled)
return 0;
@@ -752,8 +752,8 @@ void bch_btree_cache_free(struct cache_set *c)

closure_init_stack(&cl);

- if (c->shrink.list.next)
- unregister_shrinker(&c->shrink);
+ if (c->shrink->list.next)
+ unregister_and_free_shrinker(c->shrink);

mutex_lock(&c->bucket_lock);

@@ -828,14 +828,19 @@ int bch_btree_cache_alloc(struct cache_set *c)
c->verify_data = NULL;
#endif

- c->shrink.count_objects = bch_mca_count;
- c->shrink.scan_objects = bch_mca_scan;
- c->shrink.seeks = 4;
- c->shrink.batch = c->btree_pages * 2;
+ c->shrink = shrinker_alloc_and_init(bch_mca_count, bch_mca_scan,
+ c->btree_pages * 2, 4, 0, c);
+ if (!c->shrink) {
+ pr_warn("bcache: %s: could not allocate shrinker\n",
+ __func__);
+ return -ENOMEM;
+ }

- if (register_shrinker(&c->shrink, "md-bcache:%pU", c->set_uuid))
+ if (register_shrinker(c->shrink, "md-bcache:%pU", c->set_uuid)) {
pr_warn("bcache: %s: could not register shrinker\n",
__func__);
+ shrinker_free(c->shrink);
+ }

return 0;
}
diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index c6f677059214..771577581f52 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -866,7 +866,7 @@ STORE(__bch_cache_set)

sc.gfp_mask = GFP_KERNEL;
sc.nr_to_scan = strtoul_or_return(buf);
- c->shrink.scan_objects(&c->shrink, &sc);
+ c->shrink->scan_objects(c->shrink, &sc);
}

sysfs_strtoul_clamp(congested_read_threshold_us,
--
2.30.2