Skip to contentSkip to conversation

Reduce memory usage when there are dozens dm-verity devices.

PATCH Superseded LongPing Wei 1 participant 3 messages Apr 23, 2025 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Reduce memory usage when there are dozens dm-verity devices.

# Before

# cat /proc/slabinfo | grep biovec
biovec-max          2159   2173   4096    8    8 : tunables    0    0    0 : slabdata    276    276      0

# After

# cat /proc/slabinfo | grep biovec
biovec-max           232    248   4096    8    8 : tunables    0    0    0 : slabdata     31     31      0

LongPing Wei (2):
dm-verity: all verity devices share the same dm_io_client and
recheck_pool.
dm-bufio: all dm_bufio_client share the same dm_io_client.

 drivers/md/dm-bufio.c         | 29 +++++++-------
 drivers/md/dm-verity-target.c | 71 ++++++++++++++++++++++++-----------
 drivers/md/dm-verity.h        |  3 --
 3 files changed, 64 insertions(+), 39 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 dm-verity: all verity devices share the same dm_io_client and recheck_pool.

The size of biovec's memory of one dm_io_client will be reserved_bio_based_ios(default value is 16) * 4KiB memory. When the number of dm-verity devices is dozens, the total memory size will be several MiBs.

The number of dm-verity devices is 59 on my test device, and the size of biovec's memory here will be 3.87MiB.

This commit will reduce the memory usage of dm-verity
by making all verity devices share the same dm_io_client and recheck_pool.

Fixes 9177f3c0dea61 ("dm-verity: recheck the hash after a failure")
---
 drivers/md/dm-verity-target.c | 71 ++++++++++++++++++++++++-----------
 drivers/md/dm-verity.h        |  3 --
 2 files changed, 49 insertions(+), 25 deletions(-)
drivers/md/dm-verity-target.c +49 -22
index 3c427f18a04b..986c3386aa59 100644
@@ -72,6 +72,9 @@ struct dm_verity_prefetch_work {
 	unsigned int n_blocks;
 };
 
+static struct dm_io_client *verity_io_client;
+static mempool_t verity_recheck_pool;
+
 /*
  * Auxiliary structure appended to each dm-bufio buffer. If the value
  * hash_verified is nonzero, hash of the block has been verified.
@@ -449,14 +452,14 @@ static noinline int verity_recheck(struct dm_verity *v, struct dm_verity_io *io,
 	struct dm_io_request io_req;
 	struct dm_io_region io_loc;
 
-	page = mempool_alloc(&v->recheck_pool, GFP_NOIO);
+	page = mempool_alloc(&verity_recheck_pool, GFP_NOIO);
 	buffer = page_to_virt(page);
 
 	io_req.bi_opf = REQ_OP_READ;
 	io_req.mem.type = DM_IO_KMEM;
 	io_req.mem.ptr.addr = buffer;
 	io_req.notify.fn = NULL;
-	io_req.client = v->io;
+	io_req.client = verity_io_client;
 	io_loc.bdev = v->data_dev->bdev;
 	io_loc.sector = cur_block << (v->data_dev_block_bits - SECTOR_SHIFT);
 	io_loc.count = 1 << (v->data_dev_block_bits - SECTOR_SHIFT);
@@ -478,7 +481,7 @@ static noinline int verity_recheck(struct dm_verity *v, struct dm_verity_io *io,
 	memcpy(dest, buffer, 1 << v->data_dev_block_bits);
 	r = 0;
 free_ret:
-	mempool_free(page, &v->recheck_pool);
+	mempool_free(page, &verity_recheck_pool);
 
 	return r;
 }
@@ -1075,10 +1078,6 @@ static void verity_dtr(struct dm_target *ti)
 	if (v->verify_wq)
 		destroy_workqueue(v->verify_wq);
 
-	mempool_exit(&v->recheck_pool);
-	if (v->io)
-		dm_io_client_destroy(v->io);
-
 	if (v->bufio)
 		dm_bufio_client_destroy(v->bufio);
 
@@ -1625,20 +1624,6 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	}
 	v->hash_blocks = hash_position;
 
-	r = mempool_init_page_pool(&v->recheck_pool, 1, 0);
-	if (unlikely(r)) {
-		ti->error = "Cannot allocate mempool";
-		goto bad;
-	}
-
-	v->io = dm_io_client_create();
-	if (IS_ERR(v->io)) {
-		r = PTR_ERR(v->io);
-		v->io = NULL;
-		ti->error = "Cannot allocate dm io";
-		goto bad;
-	}
-
 	v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
 		1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
 		dm_bufio_alloc_callback, NULL,
@@ -1822,7 +1807,49 @@ static struct target_type verity_target = {
 	.preresume	= verity_preresume,
 #endif /* CONFIG_SECURITY */
 };
-module_dm(verity);
+
+static int __init dm_verity_init(void)
+{
+	int r;
+
+	r = mempool_init_page_pool(&verity_recheck_pool, 1, 0);
+	if (unlikely(r)) {
+		DMERR("Cannot allocate mempool");
+		goto mempool_init_page_pool_failed;
+	}
+
+	verity_io_client = dm_io_client_create();
+	if (IS_ERR(verity_io_client)) {
+		r = PTR_ERR(verity_io_client);
+		verity_io_client = NULL;
+		DMERR("Cannot allocate dm io");
+		goto dm_io_client_create_failed;
+	}
+
+	r = dm_register_target(&(verity_target));
+	if (unlikely(r))
+		goto dm_register_target_failed;
+	return r;
+
+dm_register_target_failed:
+	dm_io_client_destroy(verity_io_client);
+	verity_io_client = NULL;
+dm_io_client_create_failed:
+	mempool_exit(&verity_recheck_pool);
+mempool_init_page_pool_failed:
+	return r;
+}
+module_init(dm_verity_init);
+
+static void __exit dm_verity_exit(void)
+{
+	dm_unregister_target(&(verity_target));
+	mempool_exit(&verity_recheck_pool);
+	if (verity_io_client)
+		dm_io_client_destroy(verity_io_client);
+	verity_io_client = NULL;
+}
+module_exit(dm_verity_exit)
 
 /*
  * Check whether a DM target is a verity target.
drivers/md/dm-verity.h +0 -3
index 8cbb57862ae1..f28454f81249 100644
@@ -75,9 +75,6 @@ struct dm_verity {
 	unsigned long *validated_blocks; /* bitset blocks validated */
 
 	char *signature_key_desc; /* signature keyring reference */
-
-	struct dm_io_client *io;
-	mempool_t recheck_pool;
 };
 
 struct dm_verity_io {
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 dm-bufio: all dm_bufio_client share the same dm_io_client.

In reply to LongPing Wei

The size of biovec's memory of one dm_io_client will be reserved_bio_based_ios(default value is 16) * 4KiB memory. When the number of dm-bufio(dm-verity) devices is dozens, the total memory size will be several MiBs.

The number of dm-bufio(dm-verity) devices is 59 on my test device, and the size of biovec's memory here will be 3.87MiB.

This commit will reduce the memory usage of dm-bufio by making all dm_bufio_client share the same dm_io_client.

Fixes 95d402f057f2e ("dm: add bufio")
---
 drivers/md/dm-bufio.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/md/dm-bufio.c +15 -14
index f0b5a6931161..5aa53b06112d 100644
@@ -70,6 +70,8 @@
 
 #define SCAN_RESCHED_CYCLE	16
 
+static struct dm_io_client *bufio_io_client;
+
 /*--------------------------------------------------------------*/
 
 /*
@@ -992,7 +994,6 @@ struct dm_bufio_client {
 	void (*write_callback)(struct dm_buffer *buf);
 	struct kmem_cache *slab_buffer;
 	struct kmem_cache *slab_cache;
-	struct dm_io_client *dm_io;
 
 	struct list_head reserved_buffers;
 	unsigned int need_reserved_buffers;
@@ -1311,7 +1312,7 @@ static void use_dmio(struct dm_buffer *b, enum req_op op, sector_t sector,
 		.bi_opf = op,
 		.notify.fn = dmio_complete,
 		.notify.context = b,
-		.client = b->c->dm_io,
+		.client = bufio_io_client,
 	};
 	struct dm_io_region region = {
 		.bdev = b->c->bdev,
@@ -2197,7 +2198,7 @@ int dm_bufio_issue_flush(struct dm_bufio_client *c)
 		.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC,
 		.mem.type = DM_IO_KMEM,
 		.mem.ptr.addr = NULL,
-		.client = c->dm_io,
+		.client = bufio_io_client,
 	};
 	struct dm_io_region io_reg = {
 		.bdev = c->bdev,
@@ -2221,7 +2222,7 @@ int dm_bufio_issue_discard(struct dm_bufio_client *c, sector_t block, sector_t c
 		.bi_opf = REQ_OP_DISCARD | REQ_SYNC,
 		.mem.type = DM_IO_KMEM,
 		.mem.ptr.addr = NULL,
-		.client = c->dm_io,
+		.client = bufio_io_client,
 	};
 	struct dm_io_region io_reg = {
 		.bdev = c->bdev,
@@ -2310,7 +2311,7 @@ EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
 
 struct dm_io_client *dm_bufio_get_dm_io_client(struct dm_bufio_client *c)
 {
-	return c->dm_io;
+	return bufio_io_client;
 }
 EXPORT_SYMBOL_GPL(dm_bufio_get_dm_io_client);
 
@@ -2530,12 +2531,6 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
 	init_waitqueue_head(&c->free_buffer_wait);
 	c->async_write_error = 0;
 
-	c->dm_io = dm_io_client_create();
-	if (IS_ERR(c->dm_io)) {
-		r = PTR_ERR(c->dm_io);
-		goto bad_dm_io;
-	}
-
 	if (block_size <= KMALLOC_MAX_SIZE && !is_power_of_2(block_size)) {
 		unsigned int align = min(1U << __ffs(block_size), (unsigned int)PAGE_SIZE);
 
@@ -2606,8 +2601,6 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
 	}
 	kmem_cache_destroy(c->slab_cache);
 	kmem_cache_destroy(c->slab_buffer);
-	dm_io_client_destroy(c->dm_io);
-bad_dm_io:
 	mutex_destroy(&c->lock);
 	if (c->no_sleep)
 		static_branch_dec(&no_sleep_enabled);
@@ -2657,7 +2650,6 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c)
 	cache_destroy(&c->cache);
 	kmem_cache_destroy(c->slab_cache);
 	kmem_cache_destroy(c->slab_buffer);
-	dm_io_client_destroy(c->dm_io);
 	mutex_destroy(&c->lock);
 	if (c->no_sleep)
 		static_branch_dec(&no_sleep_enabled);
@@ -2944,6 +2936,15 @@ static int __init dm_bufio_init(void)
 	if (!dm_bufio_wq)
 		return -ENOMEM;
 
+	bufio_io_client = dm_io_client_create();
+	if (IS_ERR(bufio_io_client)) {
+		int r = PTR_ERR(bufio_io_client);
+		bufio_io_client = NULL;
+		DMERR("Cannot allocate dm io");
+		destroy_workqueue(dm_bufio_wq);
+		return r;
+	}
+
 	INIT_DELAYED_WORK(&dm_bufio_cleanup_old_work, work_fn);
 	INIT_WORK(&dm_bufio_replacement_work, do_global_cleanup);
 	queue_delayed_work(dm_bufio_wq, &dm_bufio_cleanup_old_work,
Signature · 1 line
2.34.1