All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control
@ 2026-11-17 13:19 Yongpeng Yang
  2026-11-17 13:19 ` [PATCH 1/4] dm-bufio: make the retain target per client Yongpeng Yang
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Yongpeng Yang @ 2026-11-17 13:19 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

From: Yongpeng Yang <yangyongpeng@xiaomi.com>

Device-mapper's verity targets read their hash trees, and FEC parity
when FEC is enabled, through dm-bufio.  dm-bufio has a single
module-wide retain_bytes value shared by every client, no way to see
or change one client's memory use, and no way to warm the cache ahead
of the reads that need it.

That fits poorly systems with many verified devices of very different
sizes, such as Android, which mounts a dozen verity partitions plus
per-app verified images: a large partition's hash tree (tens of MB) is
evicted and re-read under memory pressure with the 256 KiB default,
while raising the global value would make every small, idle verity
device hold memory it does not need, and would still be the wrong size
for most of them.  The same system also wants to preload a device's
hash blocks at boot and to release the memory when the device is no
longer in use, which today requires dropping caches globally.

This series adds per-client granularity to dm-bufio and lets dm-verity
size and manage its caches:

 1. Move the retain target into struct dm_bufio_client, initialized
    from the module parameter at creation.  The parameter keeps its
    meaning as the default for new clients.

 2. Add dm_bufio_client_sysfs_add(), publishing a client in sysfs
    under a caller-chosen kobject and name, with a writable
    retain_bytes and the rest of the state read-only.  It is opt-in,
    so targets that do not want it pay nothing.

 3. dm-verity: size the hash client's retain target as
    hash_retain_percent (default 10) of the hash area, and register
    the hash, FEC parity and FEC data clients under
    /sys/block/dm-N/dm-bufio/<hash|fec|fec-data>/.

 4. dm-verity: add messages to report or set a client's retain target
    and to prefetch or drop hash blocks, for warming the cache ahead
    of use and releasing it afterwards.

Nothing changes for existing users: every client starts with the value
the module parameter holds, and only dm-verity opts into sysfs.

Tested on a VM running v7.4-rc1: message and sysfs write paths, the
shrinker retain floor under memory pressure, 500 suspend/resume
cycles, 100 create/destroy cycles watching slab counters for leaks,
and error-path injection.  No kernel warnings.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM

Yongpeng Yang (4):
  dm-bufio: make the retain target per client
  dm-bufio: allow exposing a client in sysfs under a caller-chosen
    kobject
  dm-verity: size the hash retain target from the hash area
  dm-verity: add messages to inspect, prefetch and drop hash blocks

 .../ABI/testing/sysfs-kernel-dm-bufio         |  76 +++++++
 .../admin-guide/device-mapper/dm-bufio.rst    |  54 +++++
 .../admin-guide/device-mapper/index.rst       |   1 +
 .../admin-guide/device-mapper/verity.rst      |  42 ++++
 drivers/md/dm-bufio.c                         | 178 ++++++++++++++-
 drivers/md/dm-verity-target.c                 | 209 ++++++++++++++++++
 drivers/md/dm-verity.h                        |   2 +
 include/linux/dm-bufio.h                      |  15 ++
 8 files changed, 576 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-dm-bufio
 create mode 100644 Documentation/admin-guide/device-mapper/dm-bufio.rst

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/4] dm-bufio: make the retain target per client
  2026-11-17 13:19 [PATCH 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control Yongpeng Yang
@ 2026-11-17 13:19 ` Yongpeng Yang
  2026-11-18  0:41   ` Benjamin Marzinski
  2026-11-17 13:19 ` [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject Yongpeng Yang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Yongpeng Yang @ 2026-11-17 13:19 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

From: Yongpeng Yang <yangyongpeng@xiaomi.com>

dm_bufio_retain_bytes is a module-wide value shared by every dm-bufio
user, so raising it to keep one device's metadata cached also makes
every small, idle device hold on to memory it does not need.

Give each client its own target, initialized from the module parameter
at creation.  The parameter keeps its meaning as the default for new
clients; clients that already exist are not affected.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
 drivers/md/dm-bufio.c    | 31 ++++++++++++++++++++++++++++++-
 include/linux/dm-bufio.h |  7 +++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c
index 3b66cf1cb931..75b6e5c027ae 100644
--- a/drivers/md/dm-bufio.c
+++ b/drivers/md/dm-bufio.c
@@ -994,6 +994,8 @@ struct dm_bufio_client {
 
 	unsigned int minimum_buffers;
 
+	unsigned long retain_bytes;	/* READ_ONCE/WRITE_ONCE, no lock needed */
+
 	sector_t start;
 
 	struct shrinker *shrinker;
@@ -2284,6 +2286,32 @@ void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n)
 }
 EXPORT_SYMBOL_GPL(dm_bufio_set_minimum_buffers);
 
+/*
+ * Set the number of bytes this client tries to keep cached; 0 resets it
+ * to the module default.  Non-zero values are rounded to a block
+ * boundary, at least one block.
+ */
+void dm_bufio_set_retain_bytes(struct dm_bufio_client *c, unsigned long bytes)
+{
+	if (!bytes) {
+		WRITE_ONCE(c->retain_bytes, READ_ONCE(dm_bufio_retain_bytes));
+		return;
+	}
+
+	bytes -= bytes % c->block_size;
+	if (!bytes)
+		bytes = c->block_size;
+
+	WRITE_ONCE(c->retain_bytes, bytes);
+}
+EXPORT_SYMBOL_GPL(dm_bufio_set_retain_bytes);
+
+unsigned long dm_bufio_get_retain_bytes(struct dm_bufio_client *c)
+{
+	return READ_ONCE(c->retain_bytes);
+}
+EXPORT_SYMBOL_GPL(dm_bufio_get_retain_bytes);
+
 unsigned int dm_bufio_get_block_size(struct dm_bufio_client *c)
 {
 	return c->block_size;
@@ -2389,7 +2417,7 @@ static void drop_buffers(struct dm_bufio_client *c)
 
 static unsigned long get_retain_buffers(struct dm_bufio_client *c)
 {
-	unsigned long retain_bytes = READ_ONCE(dm_bufio_retain_bytes);
+	unsigned long retain_bytes = READ_ONCE(c->retain_bytes);
 
 	if (likely(c->sectors_per_block_bits >= 0))
 		retain_bytes >>= c->sectors_per_block_bits + SECTOR_SHIFT;
@@ -2522,6 +2550,7 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
 
 	mutex_init(&c->lock);
 	spin_lock_init(&c->spinlock);
+	c->retain_bytes = READ_ONCE(dm_bufio_retain_bytes);
 	INIT_LIST_HEAD(&c->reserved_buffers);
 	c->need_reserved_buffers = reserved_buffers;
 
diff --git a/include/linux/dm-bufio.h b/include/linux/dm-bufio.h
index d1503b815a78..9bf3f805c43c 100644
--- a/include/linux/dm-bufio.h
+++ b/include/linux/dm-bufio.h
@@ -158,6 +158,13 @@ void dm_bufio_forget_buffers(struct dm_bufio_client *c, sector_t block, sector_t
  */
 void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n);
 
+/*
+ * Configure and query the per-client retain target; 0 resets the
+ * module default.
+ */
+void dm_bufio_set_retain_bytes(struct dm_bufio_client *c, unsigned long bytes);
+unsigned long dm_bufio_get_retain_bytes(struct dm_bufio_client *c);
+
 unsigned int dm_bufio_get_block_size(struct dm_bufio_client *c);
 sector_t dm_bufio_get_device_size(struct dm_bufio_client *c);
 struct dm_io_client *dm_bufio_get_dm_io_client(struct dm_bufio_client *c);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
  2026-11-17 13:19 [PATCH 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control Yongpeng Yang
  2026-11-17 13:19 ` [PATCH 1/4] dm-bufio: make the retain target per client Yongpeng Yang
@ 2026-11-17 13:19 ` Yongpeng Yang
  2026-11-17 13:40   ` sashiko-bot
  2026-11-17 13:19 ` [PATCH 3/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
  2026-11-17 13:19 ` [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks Yongpeng Yang
  3 siblings, 1 reply; 9+ messages in thread
From: Yongpeng Yang @ 2026-11-17 13:19 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

From: Yongpeng Yang <yangyongpeng@xiaomi.com>

There is no way to see how much memory a particular dm-bufio client is
using, or to change one client's retain target without also affecting
every future client.

Add dm_bufio_client_sysfs_add(), which publishes a client as <name>
under a kobject supplied by its user, with the retain target writable
and the rest of its state read-only.  Callers that have no use for it
simply never call it.

The kobject is embedded in the client, which is therefore freed only
after the release method runs, as in dm-sysfs.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
 .../ABI/testing/sysfs-kernel-dm-bufio         |  76 +++++++++
 .../admin-guide/device-mapper/dm-bufio.rst    |  54 +++++++
 .../admin-guide/device-mapper/index.rst       |   1 +
 drivers/md/dm-bufio.c                         | 147 ++++++++++++++++++
 include/linux/dm-bufio.h                      |   8 +
 5 files changed, 286 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-dm-bufio
 create mode 100644 Documentation/admin-guide/device-mapper/dm-bufio.rst

diff --git a/Documentation/ABI/testing/sysfs-kernel-dm-bufio b/Documentation/ABI/testing/sysfs-kernel-dm-bufio
new file mode 100644
index 000000000000..f514e7a6a019
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-kernel-dm-bufio
@@ -0,0 +1,76 @@
+What:		/sys/block/<dm-device>/dm-bufio/<name>/
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Directory exposing one dm-bufio client whose user opted in
+		through dm_bufio_client_sysfs_add().  <name> is chosen by that
+		user; dm-verity, which places the directory under the block
+		device of its own dm device, uses <name> "hash" for the hash
+		tree client, "fec" for the FEC parity client and "fec-data"
+		for the FEC data client.  The directory lives from
+		registration until the client is destroyed, independently of
+		any device-mapper table loaded on the device, so a suspend or
+		table reload never leaves dangling entries behind.  The
+		read-only counters are instantaneous snapshots and may be
+		approximate.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/bdev
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Major and minor of the client's block device, in the form
+		<major>:<minor>.  Read-only.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/bdev_name
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Name of the client's block device.  Read-only.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/block_size
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Block size of the client in bytes.  Read-only.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/retain_bytes
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Number of bytes dm-bufio tries to keep cached for this
+		client.  It is a floor applied by the memory shrinker, not a
+		cap, so the cache may grow past it when memory is plentiful.
+
+		Writable.  A non-zero value is rounded to a block
+		boundary (but at least one block); zero restores the
+		dm-bufio default.  Writing it affects only this client,
+		not the retain_bytes module parameter and not other
+		clients.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/current_bytes
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Number of bytes currently cached by this client.  Read-only.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/buffers
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Number of buffers currently cached by this client.
+		Read-only.
+
+What:		/sys/block/<dm-device>/dm-bufio/<name>/minimum_buffers
+Date:		November 2026
+KernelVersion:	7.4
+Contact:	dm-devel@lists.linux.dev
+Description:
+		Minimum number of buffers the client keeps before cleanup
+		happens.  Read-only.
diff --git a/Documentation/admin-guide/device-mapper/dm-bufio.rst b/Documentation/admin-guide/device-mapper/dm-bufio.rst
new file mode 100644
index 000000000000..c35e415acacd
--- /dev/null
+++ b/Documentation/admin-guide/device-mapper/dm-bufio.rst
@@ -0,0 +1,54 @@
+========
+dm-bufio
+========
+
+dm-bufio caches small metadata blocks on block devices for several
+device-mapper targets (dm-verity, dm-thin, dm-cache, dm-integrity and
+dm-ebs among others).  Each user creates clients on a block device; the
+caches are independent, even for clients on the same device.
+
+Retain target
+=============
+
+dm-bufio bounds nothing by itself: buffers live until the memory shrinker
+evicts them, and the shrinker stops once a client is back down to its
+retain target.  The target is a floor under memory pressure, not a cap,
+so a fully warmed cache may hold far more.
+
+The default is the ``retain_bytes`` module parameter of dm_bufio
+(256 KiB), which only affects clients created after it is changed.  One
+client changes its own target through the ``retain_bytes`` attribute of
+its sysfs directory or a target-specific message.
+
+sysfs interface
+===============
+
+A client may be published in sysfs with dm_bufio_client_sysfs_add(),
+passing the parent kobject and the directory name; dm-bufio does not
+choose where it lives.  The directory lives until the client is
+destroyed.  dm-verity registers its clients under the block device of
+its own dm device::
+
+  /sys/block/dm-N/dm-bufio/hash/        the hash tree client
+  /sys/block/dm-N/dm-bufio/fec/         the FEC parity client
+  /sys/block/dm-N/dm-bufio/fec-data/    the FEC data client
+
+The attributes, including the read-write ``retain_bytes``, are documented
+in Documentation/ABI/testing/sysfs-kernel-dm-bufio.
+
+Example
+=======
+
+Inspect the hash client of a dm-verity device::
+
+  # ls /sys/block/dm-1/dm-bufio/
+  fec  fec-data  hash
+
+  # cat /sys/block/dm-1/dm-bufio/hash/retain_bytes
+  2535424
+
+  # echo 8388608 > /sys/block/dm-1/dm-bufio/hash/retain_bytes
+  # cat /sys/block/dm-1/dm-bufio/hash/retain_bytes
+  8388608
+
+The other clients keep their own targets.
diff --git a/Documentation/admin-guide/device-mapper/index.rst b/Documentation/admin-guide/device-mapper/index.rst
index 73c1f2002134..3b6c348f71f5 100644
--- a/Documentation/admin-guide/device-mapper/index.rst
+++ b/Documentation/admin-guide/device-mapper/index.rst
@@ -8,6 +8,7 @@ Device Mapper
     cache-policies
     cache
     delay
+    dm-bufio
     dm-clone
     dm-crypt
     dm-dust
diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c
index 75b6e5c027ae..627a64f981c5 100644
--- a/drivers/md/dm-bufio.c
+++ b/drivers/md/dm-bufio.c
@@ -20,6 +20,9 @@
 #include <linux/rbtree.h>
 #include <linux/stacktrace.h>
 #include <linux/jump_label.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
+#include <linux/completion.h>
 
 #include "dm.h"
 
@@ -995,6 +998,9 @@ struct dm_bufio_client {
 	unsigned int minimum_buffers;
 
 	unsigned long retain_bytes;	/* READ_ONCE/WRITE_ONCE, no lock needed */
+	bool sysfs_added;
+	struct kobject kobj;
+	struct completion kobj_unregister;
 
 	sector_t start;
 
@@ -2504,6 +2510,144 @@ static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrin
 	return count;
 }
 
+/*
+ * A client may be published in sysfs with dm_bufio_client_sysfs_add();
+ * the directory lives until the client is destroyed.
+ */
+
+static ssize_t dm_bufio_attr_show(struct kobject *kobj, struct attribute *attr,
+				  char *buf);
+static ssize_t dm_bufio_attr_store(struct kobject *kobj, struct attribute *attr,
+				   const char *buf, size_t size);
+
+#define DM_BUFIO_ATTR_RO(_name)						\
+	static struct attribute dm_bufio_attr_##_name =			\
+		{ .name = #_name, .mode = 0444 }
+
+#define DM_BUFIO_ATTR_RW(_name)						\
+	static struct attribute dm_bufio_attr_##_name =			\
+		{ .name = #_name, .mode = 0644 }
+
+DM_BUFIO_ATTR_RO(bdev);
+DM_BUFIO_ATTR_RO(bdev_name);
+DM_BUFIO_ATTR_RO(block_size);
+DM_BUFIO_ATTR_RW(retain_bytes);
+DM_BUFIO_ATTR_RO(current_bytes);
+DM_BUFIO_ATTR_RO(buffers);
+DM_BUFIO_ATTR_RO(minimum_buffers);
+
+static ssize_t dm_bufio_attr_show(struct kobject *kobj, struct attribute *attr,
+				  char *buf)
+{
+	struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj);
+
+	if (attr == &dm_bufio_attr_bdev)
+		return sysfs_emit(buf, "%d:%d\n", MAJOR(c->bdev->bd_dev),
+				  MINOR(c->bdev->bd_dev));
+	if (attr == &dm_bufio_attr_bdev_name)
+		return sysfs_emit(buf, "%pg\n", c->bdev);
+	if (attr == &dm_bufio_attr_block_size)
+		return sysfs_emit(buf, "%u\n", c->block_size);
+	if (attr == &dm_bufio_attr_retain_bytes)
+		return sysfs_emit(buf, "%lu\n", READ_ONCE(c->retain_bytes));
+	if (attr == &dm_bufio_attr_current_bytes)
+		return sysfs_emit(buf, "%llu\n",
+				  (u64)cache_total(&c->cache) * c->block_size);
+	if (attr == &dm_bufio_attr_buffers)
+		return sysfs_emit(buf, "%lu\n", cache_total(&c->cache));
+	if (attr == &dm_bufio_attr_minimum_buffers)
+		return sysfs_emit(buf, "%u\n", c->minimum_buffers);
+
+	return -EINVAL;
+}
+
+static ssize_t dm_bufio_attr_store(struct kobject *kobj, struct attribute *attr,
+				   const char *buf, size_t size)
+{
+	struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj);
+
+	if (attr == &dm_bufio_attr_retain_bytes) {
+		unsigned long new;
+
+		if (kstrtoul(buf, 0, &new))
+			return -EINVAL;
+
+		dm_bufio_set_retain_bytes(c, new);
+
+		return size;
+	}
+
+	return -EINVAL;
+}
+
+static struct attribute *dm_bufio_attrs[] = {
+	&dm_bufio_attr_bdev,
+	&dm_bufio_attr_bdev_name,
+	&dm_bufio_attr_block_size,
+	&dm_bufio_attr_retain_bytes,
+	&dm_bufio_attr_current_bytes,
+	&dm_bufio_attr_buffers,
+	&dm_bufio_attr_minimum_buffers,
+	NULL,
+};
+ATTRIBUTE_GROUPS(dm_bufio);
+
+static const struct sysfs_ops dm_bufio_sysfs_ops = {
+	.show	= dm_bufio_attr_show,
+	.store	= dm_bufio_attr_store,
+};
+
+static void dm_bufio_kobj_release(struct kobject *kobj)
+{
+	struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj);
+
+	/*
+	 * The client is freed only after this completes, as in dm-sysfs.
+	 */
+	complete(&c->kobj_unregister);
+}
+
+static const struct kobj_type dm_bufio_ktype = {
+	.sysfs_ops	= &dm_bufio_sysfs_ops,
+	.default_groups	= dm_bufio_groups,
+	.release	= dm_bufio_kobj_release,
+};
+
+static int dm_bufio_kobj_add(struct dm_bufio_client *c, struct kobject *parent,
+			     const char *name)
+{
+	init_completion(&c->kobj_unregister);
+
+	return kobject_init_and_add(&c->kobj, &dm_bufio_ktype, parent, "%s",
+				    name);
+}
+
+/* Drop the sysfs reference and wait for the release method to run. */
+static void dm_bufio_kobj_del(struct dm_bufio_client *c)
+{
+	kobject_put(&c->kobj);
+	wait_for_completion(&c->kobj_unregister);
+}
+
+int dm_bufio_client_sysfs_add(struct dm_bufio_client *c, struct kobject *parent,
+			      const char *name)
+{
+	int r;
+
+	if (c->sysfs_added)
+		return -EBUSY;
+
+	r = dm_bufio_kobj_add(c, parent, name);
+	if (r) {
+		dm_bufio_kobj_del(c);
+		return r;
+	}
+
+	c->sysfs_added = true;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(dm_bufio_client_sysfs_add);
+
 /*
  * Create the buffering interface
  */
@@ -2660,6 +2804,9 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c)
 {
 	unsigned int i;
 
+	if (c->sysfs_added)
+		dm_bufio_kobj_del(c);
+
 	drop_buffers(c);
 
 	shrinker_free(c->shrinker);
diff --git a/include/linux/dm-bufio.h b/include/linux/dm-bufio.h
index 9bf3f805c43c..6d82f1200aeb 100644
--- a/include/linux/dm-bufio.h
+++ b/include/linux/dm-bufio.h
@@ -17,6 +17,7 @@
 
 struct dm_bufio_client;
 struct dm_buffer;
+struct kobject;
 
 /*
  * Flags for dm_bufio_client_create
@@ -165,6 +166,13 @@ void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n);
 void dm_bufio_set_retain_bytes(struct dm_bufio_client *c, unsigned long bytes);
 unsigned long dm_bufio_get_retain_bytes(struct dm_bufio_client *c);
 
+/*
+ * Publish the client as <name> under <parent> in sysfs; removed in
+ * dm_bufio_client_destroy().
+ */
+int dm_bufio_client_sysfs_add(struct dm_bufio_client *c, struct kobject *parent,
+			      const char *name);
+
 unsigned int dm_bufio_get_block_size(struct dm_bufio_client *c);
 sector_t dm_bufio_get_device_size(struct dm_bufio_client *c);
 struct dm_io_client *dm_bufio_get_dm_io_client(struct dm_bufio_client *c);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/4] dm-verity: size the hash retain target from the hash area
  2026-11-17 13:19 [PATCH 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control Yongpeng Yang
  2026-11-17 13:19 ` [PATCH 1/4] dm-bufio: make the retain target per client Yongpeng Yang
  2026-11-17 13:19 ` [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject Yongpeng Yang
@ 2026-11-17 13:19 ` Yongpeng Yang
  2026-11-17 13:44   ` sashiko-bot
  2026-11-17 13:19 ` [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks Yongpeng Yang
  3 siblings, 1 reply; 9+ messages in thread
From: Yongpeng Yang @ 2026-11-17 13:19 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

From: Yongpeng Yang <yangyongpeng@xiaomi.com>

With the dm-bufio default of 256 KiB a large device re-reads hash
blocks evicted under memory pressure, while a small device holds far
more than it needs.

Set the hash client's target to hash_retain_percent (default 10) of
the hash area, computed with mult_frac().  A percentage above 100 is
ignored, leaving the dm-bufio default.

Publish the clients under /sys/block/dm-N/dm-bufio/<name>.
Registration runs from ->resume because the dm device's block device
does not exist until the table is loaded.  The parameter only affects
devices created afterwards.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
 drivers/md/dm-verity-target.c | 60 +++++++++++++++++++++++++++++++++++
 drivers/md/dm-verity.h        |  2 ++
 2 files changed, 62 insertions(+)

diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index 451c75e30a8f..3ae1b25fdbf6 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -32,6 +32,8 @@
 #define DM_VERITY_DEFAULT_PREFETCH_SIZE	262144
 #define DM_VERITY_USE_BH_DEFAULT_BYTES	8192
 
+#define DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT	10
+
 #define DM_VERITY_MAX_CORRUPTED_ERRS	100
 
 #define DM_VERITY_OPT_LOGGING		"ignore_corruption"
@@ -50,6 +52,13 @@ static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE
 
 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, 0644);
 
+/* Default fraction of the hash tree that dm-bufio is asked to keep cached. */
+static unsigned int dm_verity_hash_retain_percent = DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT;
+
+module_param_named(hash_retain_percent, dm_verity_hash_retain_percent, uint, 0644);
+MODULE_PARM_DESC(hash_retain_percent,
+	"Percentage of the hash area to keep cached by default");
+
 static unsigned int dm_verity_use_bh_bytes[4] = {
 	DM_VERITY_USE_BH_DEFAULT_BYTES,	// IOPRIO_CLASS_NONE
 	DM_VERITY_USE_BH_DEFAULT_BYTES,	// IOPRIO_CLASS_RT
@@ -1092,6 +1101,9 @@ static void verity_dtr(struct dm_target *ti)
 
 	verity_fec_dtr(v);
 
+	if (v->bufio_kobj)
+		kobject_put(v->bufio_kobj);
+
 	kfree(v->signature_key_desc);
 
 	if (v->use_bh_wq)
@@ -1421,6 +1433,7 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	struct dm_verity_sig_opts verify_args = {0};
 	struct dm_arg_set as;
 	unsigned int num;
+	unsigned int hash_retain_percent;
 	unsigned long long num_ll;
 	int r;
 	int i;
@@ -1638,6 +1651,13 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto bad;
 	}
 
+	hash_retain_percent = READ_ONCE(dm_verity_hash_retain_percent);
+	if (hash_retain_percent <= 100)
+		dm_bufio_set_retain_bytes(v->bufio,
+			mult_frac((u64)(v->hash_end - v->hash_start) <<
+				  v->hash_dev_block_bits,
+				  hash_retain_percent, 100));
+
 	/*
 	 * Using WQ_HIGHPRI improves throughput and completion latency by
 	 * reducing wait times when reading from a dm-verity device.
@@ -1815,6 +1835,45 @@ static void verity_postsuspend(struct dm_target *ti)
 	verity_security_clear_integrity(ti);
 }
 
+/*
+ * Publish the bufio clients under /sys/block/dm-N/dm-bufio/.  Runs from
+ * ->resume because the dm device's block device does not exist in ->ctr.
+ */
+static void verity_register_bufio_sysfs(struct dm_verity *v)
+{
+	struct mapped_device *md = dm_table_get_md(v->ti->table);
+	struct kobject *parent;
+
+	if (v->bufio_kobj)
+		return;
+
+	parent = &disk_to_dev(dm_disk(md))->kobj;
+	/*
+	 * On a table reload the old table's directory is still there; it is
+	 * removed only after this resume returns.  We retry on the next one.
+	 */
+	v->bufio_kobj = kobject_create_and_add("dm-bufio", parent);
+	if (!v->bufio_kobj) {
+		DMERR("cannot create dm-bufio sysfs directory, will retry on next resume");
+		return;
+	}
+
+	if (dm_bufio_client_sysfs_add(v->bufio, v->bufio_kobj, "hash"))
+		DMERR("cannot register hash bufio client in sysfs");
+
+	if (verity_fec_is_enabled(v)) {
+		if (dm_bufio_client_sysfs_add(v->fec->bufio, v->bufio_kobj, "fec"))
+			DMERR("cannot register FEC bufio client in sysfs");
+		if (dm_bufio_client_sysfs_add(v->fec->data_bufio, v->bufio_kobj, "fec-data"))
+			DMERR("cannot register FEC data bufio client in sysfs");
+	}
+}
+
+static void verity_resume(struct dm_target *ti)
+{
+	verity_register_bufio_sysfs(ti->private);
+}
+
 static struct target_type verity_target = {
 	.name		= "verity",
 /* Note: the LSMs depend on the singleton and immutable features */
@@ -1825,6 +1884,7 @@ static struct target_type verity_target = {
 	.dtr		= verity_dtr,
 	.map		= verity_map,
 	.postsuspend	= verity_postsuspend,
+	.resume		= verity_resume,
 	.status		= verity_status,
 	.prepare_ioctl	= verity_prepare_ioctl,
 	.iterate_devices = verity_iterate_devices,
diff --git a/drivers/md/dm-verity.h b/drivers/md/dm-verity.h
index e104a651c657..1bd625f5b45a 100644
--- a/drivers/md/dm-verity.h
+++ b/drivers/md/dm-verity.h
@@ -82,6 +82,8 @@ struct dm_verity {
 
 	struct dm_io_client *io;
 	mempool_t recheck_pool;
+
+	struct kobject *bufio_kobj;	/* /sys/block/dm-N/dm-bufio, or NULL */
 };
 
 struct pending_block {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks
  2026-11-17 13:19 [PATCH 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control Yongpeng Yang
                   ` (2 preceding siblings ...)
  2026-11-17 13:19 ` [PATCH 3/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
@ 2026-11-17 13:19 ` Yongpeng Yang
  2026-11-17 13:49   ` sashiko-bot
  3 siblings, 1 reply; 9+ messages in thread
From: Yongpeng Yang @ 2026-11-17 13:19 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

From: Yongpeng Yang <yangyongpeng@xiaomi.com>

Nothing lets a user warm a verity device's cache ahead of the reads
that will need it, or release the memory a device no longer needs.

Add three messages.  retain_bytes <hash|fec> [<bytes>] reports or sets
a client's retain target.  prefetch_hash [<start> <count>] starts
reading hash blocks asynchronously; it is not limited by the retain
target, which only applies to eviction.  drop_hash [<start> <count>]
drops hash blocks, keeping those that are in use or dirty.

drop_hash is unsupported on try_verify_in_tasklet devices, whose bufio
client cannot drop buffers (-EOPNOTSUPP).

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
 .../admin-guide/device-mapper/verity.rst      |  42 +++++
 drivers/md/dm-verity-target.c                 | 149 ++++++++++++++++++
 2 files changed, 191 insertions(+)

diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst
index 9e9d4b8157c0..8fea6130765b 100644
--- a/Documentation/admin-guide/device-mapper/verity.rst
+++ b/Documentation/admin-guide/device-mapper/verity.rst
@@ -328,6 +328,48 @@ Status
 2. Number of corrected blocks by Forward Error Correction.
    '-' if Forward Error Correction is not enabled.
 
+Metadata caching
+================
+
+The hash blocks, and the FEC parity blocks when FEC is enabled, are cached in
+memory by dm-bufio.  How much of the hash tree is kept cached is controlled by
+the ``hash_retain_percent`` module parameter (default 10), settable at boot
+time with ``dm_verity.hash_retain_percent=<0-100>`` or at run time through
+``/sys/module/dm_verity/parameters/hash_retain_percent``.  A value of 0 or
+above 100 leaves the dm-bufio default (256 KiB), which is also what the FEC
+clients keep.  Only devices created afterwards are affected.
+
+Each client's retain target and memory use are visible under
+``/sys/block/dm-N/dm-bufio/<hash|fec|fec-data>/`` on the verity device's own
+block device, and the target can be changed by writing that directory's
+``retain_bytes`` attribute; see
+Documentation/admin-guide/device-mapper/dm-bufio.rst.  The directories appear
+when the device is first activated and disappear with it.  Reloading the
+table removes the old table's directories; the new table's reappear on the
+next resume.
+
+Messages
+========
+
+``retain_bytes <hash|fec> [<bytes>]``
+    Report the retain target of the hash or FEC parity client, or set it
+    (0 resets to the dm-bufio default).  ``fec`` requires FEC to be
+    enabled.  The reply is ``retain_bytes=<bytes>``.
+
+``prefetch_hash [<start> <count>]``
+    Start reading hash blocks into the cache; without arguments, the whole
+    hash area.  The read is asynchronous and is not limited by the retain
+    target, which only applies to eviction.
+
+``drop_hash [<start> <count>]``
+    Drop hash blocks from the cache, keeping blocks that are in use or
+    dirty.  Not supported on devices created with ``try_verify_in_tasklet``
+    (``-EOPNOTSUPP``), whose bufio client cannot drop buffers.
+
+Both cache commands still work while the device is suspended: prefetch
+reads are queued and complete on resume, and ``drop_hash`` finds an empty
+cache because verity resets the bufio client on suspend.
+
 Example
 =======
 Set up a device::
diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index 3ae1b25fdbf6..e197befe0793 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -1874,6 +1874,154 @@ static void verity_resume(struct dm_target *ti)
 	verity_register_bufio_sysfs(ti->private);
 }
 
+static int verity_prefetch_hash(struct dm_verity *v, unsigned int argc,
+				char **argv, char *result, unsigned int maxlen)
+{
+	sector_t total = v->hash_end - v->hash_start;
+	sector_t start, n;
+	unsigned long long value;
+	unsigned int sz = 0;
+
+	if (argc == 1) {
+		start = 0;
+		n = total;
+	} else if (argc == 3) {
+		if (kstrtoull(argv[1], 0, &value) || (sector_t)value != value)
+			return -EINVAL;
+		start = value;
+
+		if (kstrtoull(argv[2], 0, &value) || (sector_t)value != value)
+			return -EINVAL;
+		n = value;
+	} else {
+		return -EINVAL;
+	}
+
+	if (start >= total) {
+		DMERR("prefetch_hash start %llu beyond hash area (%llu blocks)",
+		      (unsigned long long)start, (unsigned long long)total);
+		return -EINVAL;
+	}
+
+	n = min(n, total - start);
+	n = min_t(sector_t, n, UINT_MAX);
+
+	dm_bufio_prefetch(v->bufio, v->hash_start + start, n);
+
+	DMEMIT("prefetch_hash: started %llu blocks", (unsigned long long)n);
+
+	return 1;
+}
+
+static int verity_drop_hash(struct dm_verity *v, unsigned int argc,
+			    char **argv, char *result, unsigned int maxlen)
+{
+	sector_t total = v->hash_end - v->hash_start;
+	sector_t start, n;
+	unsigned long long value;
+	unsigned int sz = 0;
+
+	/*
+	 * dm_bufio_forget_buffers() ends up in cache_remove_range(), which
+	 * cannot run on a client created with DM_BUFIO_CLIENT_NO_SLEEP.
+	 */
+	if (v->use_bh_wq) {
+		DMERR("drop_hash unsupported on tasklet-verifying device");
+		return -EOPNOTSUPP;
+	}
+
+	if (argc == 1) {
+		start = 0;
+		n = total;
+	} else if (argc == 3) {
+		if (kstrtoull(argv[1], 0, &value) || (sector_t)value != value)
+			return -EINVAL;
+		start = value;
+
+		if (kstrtoull(argv[2], 0, &value) || (sector_t)value != value)
+			return -EINVAL;
+		n = value;
+	} else {
+		return -EINVAL;
+	}
+
+	if (start >= total) {
+		DMERR("drop_hash start %llu beyond hash area (%llu blocks)",
+		      (unsigned long long)start, (unsigned long long)total);
+		return -EINVAL;
+	}
+
+	n = min(n, total - start);
+
+	dm_bufio_forget_buffers(v->bufio, v->hash_start + start, n);
+
+	DMEMIT("drop_hash: dropped %llu", (unsigned long long)n);
+
+	return 1;
+}
+
+/*
+ * retain_bytes <hash|fec> [<bytes>]
+ *
+ * Report the selected client's retain target, or set it (0 resets to the
+ * dm-bufio default, non-zero is rounded down to a block boundary).
+ */
+static int verity_retain_bytes(struct dm_verity *v, unsigned int argc,
+			       char **argv, char *result, unsigned int maxlen)
+{
+	struct dm_bufio_client *bufio;
+	unsigned long long value;
+	unsigned int sz = 0;
+
+	if (argc < 2 || argc > 3)
+		return -EINVAL;
+
+	if (!strcasecmp(argv[1], "hash")) {
+		bufio = v->bufio;
+	} else if (!strcasecmp(argv[1], "fec")) {
+		if (!verity_fec_is_enabled(v)) {
+			DMERR("FEC is not enabled");
+			return -EINVAL;
+		}
+		bufio = v->fec->bufio;
+	} else {
+		return -EINVAL;
+	}
+
+	if (argc == 3) {
+		if (kstrtoull(argv[2], 0, &value) ||
+		    (unsigned long)value != value)
+			return -EINVAL;
+		dm_bufio_set_retain_bytes(bufio, value);
+	}
+
+	DMEMIT("retain_bytes=%lu", dm_bufio_get_retain_bytes(bufio));
+
+	return 1;
+}
+
+static int verity_message(struct dm_target *ti, unsigned int argc, char **argv,
+			  char *result, unsigned int maxlen)
+{
+	struct dm_verity *v = ti->private;
+
+	if (!argc)
+		return -EINVAL;
+
+	if (!strcasecmp(argv[0], "retain_bytes"))
+		return verity_retain_bytes(v, argc, argv, result, maxlen);
+
+	if (!strcasecmp(argv[0], "prefetch_hash"))
+		return verity_prefetch_hash(v, argc, argv, result, maxlen);
+
+	if (!strcasecmp(argv[0], "drop_hash"))
+		return verity_drop_hash(v, argc, argv, result, maxlen);
+
+	DMERR("unknown message '%s'", argv[0]);
+
+	return -EINVAL;
+}
+
 static struct target_type verity_target = {
 	.name		= "verity",
 /* Note: the LSMs depend on the singleton and immutable features */
@@ -1886,6 +2034,7 @@ static struct target_type verity_target = {
 	.postsuspend	= verity_postsuspend,
 	.resume		= verity_resume,
 	.status		= verity_status,
+	.message	= verity_message,
 	.prepare_ioctl	= verity_prepare_ioctl,
 	.iterate_devices = verity_iterate_devices,
 	.io_hints	= verity_io_hints,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
  2026-11-17 13:19 ` [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject Yongpeng Yang
@ 2026-11-17 13:40   ` sashiko-bot
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 13:40 UTC (permalink / raw)
  To: Yongpeng Yang, Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] dm-bufio: module unload race in dm_bufio_kobj_release()
- [Medium] dm-bufio: double free of kobj.name if
  dm_bufio_client_sysfs_add() is retried
- [Medium] dm-bufio: missing sysfs unregistration API breaks DM table
  reloads
--

commit 865aec6258e84e1a7aa1322dc0e3b22f9e5e966b
Author: Yongpeng Yang <yangyongpeng@xiaomi.com>

dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject

This commit adds dm_bufio_client_sysfs_add() to expose per-client dm-bufio
statistics and the writable retain_bytes target in sysfs under a
caller-supplied kobject.

> diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c
> index 75b6e5c027ae2..627a64f981c5c 100644
> --- a/drivers/md/dm-bufio.c
> +++ b/drivers/md/dm-bufio.c

[ ... ]

> @@ -2504,6 +2510,144 @@ static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrin

[ ... ]

> +static void dm_bufio_kobj_release(struct kobject *kobj)
> +{
> +	struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj);
> +
> +	/*
> +	 * The client is freed only after this completes, as in dm-sysfs.
> +	 */
> +	complete(&c->kobj_unregister);
> +}
> +
> +static const struct kobj_type dm_bufio_ktype = {
> +	.sysfs_ops	= &dm_bufio_sysfs_ops,
> +	.default_groups	= dm_bufio_groups,
> +	.release	= dm_bufio_kobj_release,
> +};
> +
> +static int dm_bufio_kobj_add(struct dm_bufio_client *c, struct kobject *parent,
> +			     const char *name)
> +{
> +	init_completion(&c->kobj_unregister);
> +
> +	return kobject_init_and_add(&c->kobj, &dm_bufio_ktype, parent, "%s",
> +				    name);
> +}
> +
> +/* Drop the sysfs reference and wait for the release method to run. */
> +static void dm_bufio_kobj_del(struct dm_bufio_client *c)
> +{
> +	kobject_put(&c->kobj);
> +	wait_for_completion(&c->kobj_unregister);
> +}

[Severity: Medium]
Can dm-bufio.ko be unloaded while another task is still returning from
dm_bufio_kobj_release()?

CONFIG_DM_BUFIO is tristate, so dm_bufio_kobj_release() lives in
loadable module text.  If the last reference to c->kobj is dropped by
another context after dm_bufio_kobj_del() calls kobject_put() (for
example, a concurrent sysfs reader or kobject_delayed_cleanup()), that
context can be preempted right after complete(&c->kobj_unregister):

CPU0                              CPU1
dm_bufio_client_destroy()
  dm_bufio_kobj_del()
    kobject_put(&c->kobj)
                                  kobject_put(&c->kobj)
                                    dm_bufio_kobj_release()
                                      complete(&c->kobj_unregister)
                                      <preempted>
    wait_for_completion()
  kfree(c)
delete_module("dm_bufio")
                                      return;  <-- unmapped module text

dm-sysfs avoids this race by placing dm_kobject_release() in built-in
code in drivers/md/dm-builtin.c.

> +
> +int dm_bufio_client_sysfs_add(struct dm_bufio_client *c, struct kobject *parent,
> +			      const char *name)
> +{
> +	int r;
> +
> +	if (c->sysfs_added)
> +		return -EBUSY;
> +
> +	r = dm_bufio_kobj_add(c, parent, name);
> +	if (r) {
> +		dm_bufio_kobj_del(c);
> +		return r;
> +	}
> +
> +	c->sysfs_added = true;
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(dm_bufio_client_sysfs_add);

[Severity: Medium]
Does retrying dm_bufio_client_sysfs_add() after a failed call double free
c->kobj.name?

When kobject_init_and_add() fails after kobject_init() and
kobject_set_name_vargs(), dm_bufio_kobj_del() runs kobject_cleanup(),
which calls kfree_const(c->kobj.name) without clearing c->kobj.name or
c->kobj.state_initialized.  Because c->sysfs_added remains false and
c->kobj is not zeroed on the error path, a second call to
dm_bufio_client_sysfs_add() on the same client triggers the
state_initialized warning in kobject_init() and then calls
kfree_const(kobj->name) on the dangling pointer in
kobject_set_name_vargs().

> @@ -2660,6 +2804,9 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c)
>  {
>  	unsigned int i;
>  
> +	if (c->sysfs_added)
> +		dm_bufio_kobj_del(c);
> +
>  	drop_buffers(c);
>  
>  	shrinker_free(c->shrinker);

[Severity: Medium]
How can a device-mapper target reload its table without colliding on the
sysfs directory when c->kobj is only removed in
dm_bufio_client_destroy()?

During a table reload in do_resume(), dm_suspend() suspends the old table
and dm_resume() resumes the new table before dm_table_destroy() destroys
the old table and its dm_bufio_client.  When a target registers its
clients under the device-level path /sys/block/<dm-device>/dm-bufio/<name>
(as documented in Documentation/ABI/testing/sysfs-kernel-dm-bufio and
used by dm-verity in commit "dm-verity: size the hash retain target from
the hash area"), the old table's sysfs entries are still present when the
new table resumes, so kobject_init_and_add() fails with -EEXIST and
triggers a dump_stack() warning in sysfs_warn_dup().  Without an exported
unregistration helper that targets can call during suspend, reloading a
table fails to publish the new client's sysfs directory and leaves the
device without /sys/block/<dm-device>/dm-bufio/ once the old table is
destroyed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117131918.2745193-2-yangyongpeng.storage@gmail.com?part=2

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/4] dm-verity: size the hash retain target from the hash area
  2026-11-17 13:19 ` [PATCH 3/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
@ 2026-11-17 13:44   ` sashiko-bot
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 13:44 UTC (permalink / raw)
  To: Yongpeng Yang, Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] dm-verity: 64-bit division and 32-bit truncation in
  verity_ctr()
- [Medium] dm-verity: duplicate sysfs dir warning and missing dir on
  table reload
- [Medium] dm-verity: release of data_dev before verity_fec_dtr() in
  verity_dtr()
- [Low] dm-verity: document dm_verity.hash_retain_percent in
  kernel-parameters.txt
--

commit 6fe0fff5c789faa19fdefcb12c794d8f32e868cd
Author: Yongpeng Yang <yangyongpeng@xiaomi.com>

dm-verity: size the hash retain target from the hash area

This commit configures the dm-verity hash bufio client's retain target as a
configurable percentage of the hash area and exposes dm-verity's bufio
clients under /sys/block/dm-N/dm-bufio/.

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index 6d589867623c0..d4fb06cb4e7ea 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -32,6 +32,8 @@
>  #define DM_VERITY_DEFAULT_PREFETCH_SIZE	262144
>  #define DM_VERITY_USE_BH_DEFAULT_BYTES	8192
>  
> +#define DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT	10
> +
>  #define DM_VERITY_MAX_CORRUPTED_ERRS	100
>  
>  #define DM_VERITY_OPT_LOGGING		"ignore_corruption"
> @@ -50,6 +52,13 @@ static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE
>  
>  module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, 0644);
>  
> +/* Default fraction of the hash tree that dm-bufio is asked to keep cached. */
> +static unsigned int dm_verity_hash_retain_percent = DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT;
> +
> +module_param_named(hash_retain_percent, dm_verity_hash_retain_percent, uint, 0644);
> +MODULE_PARM_DESC(hash_retain_percent,
> +	"Percentage of the hash area to keep cached by default");

[Severity: Low]
Since Documentation/admin-guide/kernel-parameters.txt already documents
dm_verity.keyring_unsealed=, should dm_verity.hash_retain_percent= be added
there as well?

> @@ -1083,6 +1092,9 @@ static void verity_dtr(struct dm_target *ti)
>  
>  	verity_fec_dtr(v);
>  
> +	if (v->bufio_kobj)
> +		kobject_put(v->bufio_kobj);
> +

[Severity: Medium]
Can exposing v->fec->data_bufio in sysfs cause a use-after-free of
v->data_dev->bdev during teardown?

verity_dtr() calls dm_put_device(ti, v->data_dev) before verity_fec_dtr(v)
destroys and unregisters f->data_bufio:

drivers/md/dm-verity-target.c:verity_dtr() {
    ...
	if (v->data_dev)
		dm_put_device(ti, v->data_dev);

	verity_fec_dtr(v);
    ...
}

v->fec->data_bufio is created on v->data_dev->bdev, and
dm_bufio_client_create() stores c->bdev without taking its own reference.
When FEC uses a separate device from data_dev, dm_put_device(ti, v->data_dev)
closes the underlying block device while /sys/block/dm-N/dm-bufio/fec-data is
still registered in sysfs, so a concurrent read of the bdev or bdev_name
attribute in dm_bufio_attr_show() can dereference c->bdev after it has been
released:

CPU0                                  CPU1
verity_dtr()
  dm_put_device(ti, v->data_dev)
                                      dm_bufio_attr_show()
                                        sysfs_emit(buf, "%pg\n", c->bdev)
                                          <-- use-after-free
  verity_fec_dtr(v)
    dm_bufio_client_destroy(f->data_bufio)

Calling verity_fec_dtr(v) before releasing v->data_dev and v->hash_dev would
ensure the FEC bufio clients are unregistered before their backing devices
are put.

[ ... ]

> @@ -1629,6 +1642,13 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
>  		goto bad;
>  	}
>  
> +	hash_retain_percent = READ_ONCE(dm_verity_hash_retain_percent);
> +	if (hash_retain_percent <= 100)
> +		dm_bufio_set_retain_bytes(v->bufio,
> +			mult_frac((u64)(v->hash_end - v->hash_start) <<
> +				  v->hash_dev_block_bits,
> +				  hash_retain_percent, 100));
> +

[Severity: High]
Does passing a u64 expression to mult_frac() break 32-bit builds and silently
truncate the retain target above ULONG_MAX?

mult_frac(x, n, d) declares its internal variables with typeof(x) and expands
to raw '/' and '%' operations, which emit __udivdi3 and __umoddi3 libcalls on
32-bit architectures when x is u64 and n is a runtime variable.  In addition,
dm_bufio_set_retain_bytes() takes an unsigned long, so on 32-bit kernels a
u64 result at or above 4 GiB is truncated modulo 2^32 (and if truncated to 0,
resets retain_bytes to the global default).  Using mul_u64_u32_div() and
min_t(u64, ..., ULONG_MAX) would avoid both problems.

> @@ -1806,6 +1826,45 @@ static void verity_postsuspend(struct dm_target *ti)
>  	verity_security_clear_integrity(ti);
>  }
>  
> +/*
> + * Publish the bufio clients under /sys/block/dm-N/dm-bufio/.  Runs from
> + * ->resume because the dm device's block device does not exist in ->ctr.
> + */
> +static void verity_register_bufio_sysfs(struct dm_verity *v)
> +{
> +	struct mapped_device *md = dm_table_get_md(v->ti->table);
> +	struct kobject *parent;
> +
> +	if (v->bufio_kobj)
> +		return;
> +
> +	parent = &disk_to_dev(dm_disk(md))->kobj;
> +	/*
> +	 * On a table reload the old table's directory is still there; it is
> +	 * removed only after this resume returns.  We retry on the next one.
> +	 */
> +	v->bufio_kobj = kobject_create_and_add("dm-bufio", parent);
> +	if (!v->bufio_kobj) {
> +		DMERR("cannot create dm-bufio sysfs directory, will retry on next resume");
> +		return;
> +	}

[Severity: Medium]
Does reloading a dm-verity table trigger a sysfs_warn_dup() stack trace and
leave the device without a /sys/block/dm-N/dm-bufio directory?

On table reload, do_resume() suspends the old table, swaps in the new table,
and calls dm_resume(md) before calling dm_table_destroy(old_map):

drivers/md/dm-ioctl.c:do_resume() {
    ...
	if (dm_suspended_md(md)) {
		r = dm_resume(md);
    ...
	}
    ...
	if (old_map)
		dm_table_destroy(old_map);
    ...
}

Because the old table's v->bufio_kobj is only freed in verity_dtr(),
/sys/block/dm-N/dm-bufio still exists when verity_resume() runs on the new
table.  kobject_create_and_add("dm-bufio", parent) hits -EEXIST in
sysfs_create_dir_ns() and calls sysfs_warn_dup(), which logs a duplicate
filename warning and a full dump_stack() backtrace.  Then
dm_table_destroy(old_map) runs verity_dtr() on the old table and removes the
directory, and since no second resume occurs, the reloaded table is left with
v->bufio_kobj == NULL and no dm-bufio sysfs directory.

Unregistering and freeing v->bufio_kobj (and removing the bufio clients from
sysfs) in verity_postsuspend() would release the old table's directory before
the new table's ->resume hook runs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117131918.2745193-2-yangyongpeng.storage@gmail.com?part=3

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks
  2026-11-17 13:19 ` [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks Yongpeng Yang
@ 2026-11-17 13:49   ` sashiko-bot
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 13:49 UTC (permalink / raw)
  To: Yongpeng Yang, Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, Yongpeng Yang

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] dm-verity: verity_prefetch_hash() on no_sleep client causes UAF
  on teardown
- [High] dm-verity: 64-bit mult_frac() division and truncation in
  verity_ctr()
- [Medium] dm-verity: unsynchronized prefetch in verity_prefetch_hash()
  across suspend
- [Medium] dm-verity: duplicate sysfs kobject warning on reload in
  verity_register_bufio_sysfs()
- [Medium] dm-verity: premature dm_put_device() before verity_fec_dtr()
  in verity_dtr()
- [Low] dm-verity: zero-arg prefetch_hash and drop_hash fail when levels
  is zero
--

commit 1669fe0d8b13a61ae6909ea388b60b10f26a0954
Author: Yongpeng Yang <yangyongpeng@xiaomi.com>

dm-verity: add messages to inspect, prefetch and drop hash blocks

This commit adds target messages (retain_bytes, prefetch_hash, and
drop_hash) to dm-verity to inspect or configure bufio retain targets and to
prefetch or drop cached hash blocks, and documents dm-verity metadata
caching and the new messages.

> diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst
> index 9e9d4b8157c03..8fea6130765bb 100644
> --- a/Documentation/admin-guide/device-mapper/verity.rst
> +++ b/Documentation/admin-guide/device-mapper/verity.rst
> @@ -328,6 +328,48 @@ Status
>  2. Number of corrected blocks by Forward Error Correction.
>     '-' if Forward Error Correction is not enabled.
>  
> +Metadata caching
> +================
> +
> +The hash blocks, and the FEC parity blocks when FEC is enabled, are cached in
> +memory by dm-bufio.  How much of the hash tree is kept cached is controlled by
> +the ``hash_retain_percent`` module parameter (default 10), settable at boot
> +time with ``dm_verity.hash_retain_percent=<0-100>`` or at run time through
> +``/sys/module/dm_verity/parameters/hash_retain_percent``.  A value of 0 or
> +above 100 leaves the dm-bufio default (256 KiB), which is also what the FEC
> +clients keep.  Only devices created afterwards are affected.

[Severity: High]
Does the hash_retain_percent calculation in verity_ctr() from commit
"dm-verity: size the hash retain target from the hash area" fail to link on
32-bit architectures and truncate retain sizes at or above 4 GiB?

drivers/md/dm-verity-target.c:verity_ctr() {
    ...
	hash_retain_percent = READ_ONCE(dm_verity_hash_retain_percent);
	if (hash_retain_percent <= 100)
		dm_bufio_set_retain_bytes(v->bufio,
			mult_frac((u64)(v->hash_end - v->hash_start) <<
				  v->hash_dev_block_bits,
				  hash_retain_percent, 100));
    ...
}

mult_frac(x, n, d) uses typeof(x) and plain C '/' and '%' operators, so
passing a u64 first argument emits __udivdi3 and __umoddi3 calls on 32-bit
builds.  In addition, dm_bufio_set_retain_bytes() takes an unsigned long, so
a 64-bit byte count at or above 4 GiB wraps modulo 2^32 on 32-bit systems
unless clamped to ULONG_MAX.

> +
> +Each client's retain target and memory use are visible under
> +``/sys/block/dm-N/dm-bufio/<hash|fec|fec-data>/`` on the verity device's own
> +block device, and the target can be changed by writing that directory's
> +``retain_bytes`` attribute; see
> +Documentation/admin-guide/device-mapper/dm-bufio.rst.  The directories appear

[Severity: Medium]
Can exposing v->fec->data_bufio under fec-data in commit "dm-verity: size
the hash retain target from the hash area" cause a use-after-free on
c->bdev during verity_dtr()?

drivers/md/dm-verity-target.c:verity_dtr() {
    ...
	if (v->data_dev)
		dm_put_device(ti, v->data_dev);

	verity_fec_dtr(v);
    ...
}

v->fec->data_bufio stores a raw pointer to v->data_dev->bdev, which
dm_bufio_attr_show() dereferences when reading fec-data/bdev or
fec-data/bdev_name.  Because verity_dtr() calls dm_put_device(ti,
v->data_dev) before verity_fec_dtr(v) destroys v->fec->data_bufio and
removes its sysfs kobject, a concurrent sysfs read during target teardown
can dereference v->data_dev->bdev after the device reference has been
dropped.

> +when the device is first activated and disappear with it.  Reloading the
> +table removes the old table's directories; the new table's reappear on the
> +next resume.

[Severity: Medium]
Does calling kobject_create_and_add("dm-bufio", parent) in
verity_register_bufio_sysfs() from commit "dm-verity: size the hash retain
target from the hash area" trigger a kernel warning and stack trace on every
table reload?

During do_resume(), dm_resume() invokes verity_resume() on the new table
before dm_table_destroy() runs verity_dtr() on the old table.  Because the
old table's "dm-bufio" kobject is still registered under the disk kobject,
kobject_create_and_add("dm-bufio", parent) fails with -EEXIST in
sysfs_create_dir_ns() and calls sysfs_warn_dup(), which prints a kernel
warning and calls dump_stack(), and then destroying the old table removes
/sys/block/dm-N/dm-bufio altogether.

[ ... ]

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index d4fb06cb4e7ea..7f92bc14d39fe 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -1865,6 +1865,154 @@ static void verity_resume(struct dm_target *ti)
>  	verity_register_bufio_sysfs(ti->private);
>  }
>  
> +static int verity_prefetch_hash(struct dm_verity *v, unsigned int argc,
> +				char **argv, char *result, unsigned int maxlen)
> +{
> +	sector_t total = v->hash_end - v->hash_start;
> +	sector_t start, n;
> +	unsigned long long value;
> +	unsigned int sz = 0;
> +
> +	if (argc == 1) {
> +		start = 0;
> +		n = total;
> +	} else if (argc == 3) {
> +		if (kstrtoull(argv[1], 0, &value) || (sector_t)value != value)
> +			return -EINVAL;
> +		start = value;
> +
> +		if (kstrtoull(argv[2], 0, &value) || (sector_t)value != value)
> +			return -EINVAL;
> +		n = value;
> +	} else {
> +		return -EINVAL;
> +	}
> +
> +	if (start >= total) {
> +		DMERR("prefetch_hash start %llu beyond hash area (%llu blocks)",
> +		      (unsigned long long)start, (unsigned long long)total);
> +		return -EINVAL;
> +	}

[Severity: Low]
Does the start >= total check in verity_prefetch_hash() and
verity_drop_hash() reject zero-argument calls on a valid 0-level verity
device?

When v->data_blocks <= 1, verity_ctr() sets v->levels = 0 and
v->hash_end = v->hash_start, so total is 0.  A zero-argument prefetch_hash
or drop_hash sets start = 0 and n = 0, hits start >= total, logs an error,
and fails with -EINVAL instead of succeeding as a 0-block no-op.

> +
> +	n = min(n, total - start);
> +	n = min_t(sector_t, n, UINT_MAX);
> +
> +	dm_bufio_prefetch(v->bufio, v->hash_start + start, n);

[Severity: Medium]
Can verity_prefetch_hash() race with verity_postsuspend() or submit reads to
v->hash_dev while the target is suspended?

CPU0                                  CPU1
target_message()
  verity_message()
    verity_prefetch_hash()
      dm_bufio_prefetch(v->bufio, ...)
        dm_bufio_lock(c)
        b = __bufio_new(...)
          /* b->hold_count == 1 */
        dm_bufio_unlock(c)
                                      dm_suspend()
                                        verity_postsuspend()
                                          flush_workqueue(v->verify_wq)
                                          dm_bufio_client_reset(v->bufio)
                                            drop_buffers(c)
                                              cache_iterate(..., warn_leak)
                                                WARN_ON(!(*warned))
                                              WARN_ON(cache_count(...))
        submit_io(b, ...)
        dm_bufio_release(b)

Unlike verity_prefetch_io(), which runs on v->verify_wq and is drained by
flush_workqueue(v->verify_wq) in verity_postsuspend() (see 2de510fccbca
("dm-verity: fix prefetch-vs-suspend race")), target_message() calls
verity_message() without holding md->suspend_lock or checking
dm_suspended(ti).  If dm_bufio_client_reset() runs while
__dm_bufio_prefetch() has dropped dm_bufio_lock(c) with a buffer's
hold_count == 1, drop_buffers() cannot evict that buffer and triggers the
leak WARN_ONs in warn_leak() and drop_buffers() (and under
CONFIG_DM_DEBUG_BLOCK_STACK_TRACING, warn_leak() zeroes b->hold_count and
drop_buffers() frees b before submit_io() and dm_bufio_release(b) run,
causing a use-after-free).

In addition, because v->bufio submits reads directly to v->hash_dev->bdev
rather than the dm-verity mapped device, calling prefetch_hash while
suspended immediately issues reads to v->hash_dev->bdev and populates
v->bufio instead of queueing them until resume as described in verity.rst.

> +
> +	DMEMIT("prefetch_hash: started %llu blocks", (unsigned long long)n);
> +
> +	return 1;
> +}
> +
> +static int verity_drop_hash(struct dm_verity *v, unsigned int argc,
> +			    char **argv, char *result, unsigned int maxlen)
> +{
> +	sector_t total = v->hash_end - v->hash_start;
> +	sector_t start, n;
> +	unsigned long long value;
> +	unsigned int sz = 0;
> +
> +	/*
> +	 * dm_bufio_forget_buffers() ends up in cache_remove_range(), which
> +	 * cannot run on a client created with DM_BUFIO_CLIENT_NO_SLEEP.
> +	 */
> +	if (v->use_bh_wq) {
> +		DMERR("drop_hash unsupported on tasklet-verifying device");
> +		return -EOPNOTSUPP;
> +	}

[Severity: High]
Can calling verity_prefetch_hash() on a v->use_bh_wq device leave in-flight
B_READING buffers that drop_buffers() cannot evict, leading to warnings and
a use-after-free on teardown?

drivers/md/dm-bufio.c:is_clean() {
    ...
	if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep &&
	    unlikely(test_bit(B_READING, &b->state)))
		return ER_DONT_EVICT;
    ...
}

When v->use_bh_wq is enabled, v->bufio is created with
DM_BUFIO_CLIENT_NO_SLEEP (c->no_sleep = true).  verity_prefetch_hash() does
not reject v->use_bh_wq and submits asynchronous read BIOs to
v->hash_dev->bdev with B_READING set and b->hold_count == 0, returning to
userspace while those reads are still in flight.  If the device is then
suspended (dm_bufio_client_reset()) or destroyed (dm_bufio_client_destroy())
before all prefetched reads complete, __get_unclaimed_buffer() -> is_clean()
refuses to evict the B_READING buffers.  drop_buffers() then triggers
WARN_ON(!(*warned)) in warn_leak(), calls cond_resched() inside
__cache_iterate() while holding spin_lock_bh(&c->spinlock), and on target
destruction destroys c->slab_buffer and frees c while read_endio() is still
in flight.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117131918.2745193-2-yangyongpeng.storage@gmail.com?part=4

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/4] dm-bufio: make the retain target per client
  2026-11-17 13:19 ` [PATCH 1/4] dm-bufio: make the retain target per client Yongpeng Yang
@ 2026-11-18  0:41   ` Benjamin Marzinski
  0 siblings, 0 replies; 9+ messages in thread
From: Benjamin Marzinski @ 2026-11-18  0:41 UTC (permalink / raw)
  To: Yongpeng Yang
  Cc: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, dm-devel,
	Yongpeng Yang

On Tue, Nov 17, 2026 at 09:19:17PM +0800, Yongpeng Yang wrote:
> From: Yongpeng Yang <yangyongpeng@xiaomi.com>
> 
> dm_bufio_retain_bytes is a module-wide value shared by every dm-bufio
> user, so raising it to keep one device's metadata cached also makes
> every small, idle device hold on to memory it does not need.
> 
> Give each client its own target, initialized from the module parameter
> at creation.  The parameter keeps its meaning as the default for new
> clients; clients that already exist are not affected.
> 
> Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
> Assisted-by: LLM

Looks good except one minor nitpick.

> ---
>  drivers/md/dm-bufio.c    | 31 ++++++++++++++++++++++++++++++-
>  include/linux/dm-bufio.h |  7 +++++++
>  2 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c
> index 3b66cf1cb931..75b6e5c027ae 100644
> --- a/drivers/md/dm-bufio.c
> +++ b/drivers/md/dm-bufio.c

[ ... ]

> @@ -2284,6 +2286,32 @@ void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n)
>  }
>  EXPORT_SYMBOL_GPL(dm_bufio_set_minimum_buffers);
>  
> +/*
> + * Set the number of bytes this client tries to keep cached; 0 resets it
> + * to the module default.  Non-zero values are rounded to a block
> + * boundary, at least one block.
> + */
> +void dm_bufio_set_retain_bytes(struct dm_bufio_client *c, unsigned long bytes)
> +{
> +	if (!bytes) {
> +		WRITE_ONCE(c->retain_bytes, READ_ONCE(dm_bufio_retain_bytes));
> +		return;
> +	}
> +
> +	bytes -= bytes % c->block_size;
> +	if (!bytes)
> +		bytes = c->block_size;

Instead of rounding down, except when that would round to 0, why not
always round up, so dm_bufio retains at least as many bytes as
requested?

-Ben


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-18  0:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 13:19 [PATCH 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control Yongpeng Yang
2026-11-17 13:19 ` [PATCH 1/4] dm-bufio: make the retain target per client Yongpeng Yang
2026-11-18  0:41   ` Benjamin Marzinski
2026-11-17 13:19 ` [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject Yongpeng Yang
2026-11-17 13:40   ` sashiko-bot
2026-11-17 13:19 ` [PATCH 3/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
2026-11-17 13:44   ` sashiko-bot
2026-11-17 13:19 ` [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks Yongpeng Yang
2026-11-17 13:49   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.