All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/4] dm-bufio per-client retain and dm-verity cache control
@ 2026-11-21  8:41 Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 1/4] dm-bufio: make the retain target per client Yongpeng Yang
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Yongpeng Yang @ 2026-11-21  8:41 UTC (permalink / raw)
  To: agk, bmarzins, mpatocka, snitzer, okozina, zkabelac, dm-devel
  Cc: yangyongpeng

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.  The hash areas we see range from a few MB to
tens of MB: with the 256 KiB default, a large partition's hash blocks
are evicted under memory pressure and re-read later, while a uniform
large value makes every cold, idle device hold memory it does not
need -- and most verity devices are cold most of the time.  What is
wanted is a lifecycle: prefetch when the device is about to be used,
drop when it goes cold again.

This series adds per-client granularity to dm-bufio and gives
dm-verity message-based control over its caches:

 1. Move the retain target into struct dm_bufio_client, initialized
    from the module parameter at creation, and export set/get plus a
    cached-buffer count.  The parameter keeps its meaning as the
    default for new clients.

 2. dm-verity: size the hash client's retain target as
    hash_retain_percent (default 10) of the hash area, so a small
    device keeps little and a large one keeps enough.

 3. dm-verity: add the retain_bytes message to report or set a
    client's retain target; the reply also reports current_bytes and
    buffers, so memory use can be watched through the message.

 4. dm-verity: add prefetch_hash and drop_hash messages to warm the
    cache ahead of use and release it afterwards.  Both are rejected
    with -EBUSY while the device is suspended.

Nothing changes for existing users: every client starts with the
value the module parameter holds.

The code, commit messages and documentation were drafted with
assistance from an AI coding assistant, from prompts describing the
desired behaviour and review feedback; the result was reviewed,
reworked and tested by hand.

Changes since v2:
 - The sysfs interface (per-client directories, ABI documentation and
   the suspend/reload handling around them) is dropped entirely, per
   Mikulas Patocka: state is better shown through the message
   interface.  The retain_bytes reply now carries current_bytes and
   buffers instead.

Changes since v3:
 - prefetch_hash and drop_hash now fail with -EBUSY while the device
   is suspended, so no new I/O is issued against the quiesced device
   and no message can race the bufio reset in postsuspend.  The check
   needs no further serialization: __dm_suspend() sets the suspended
   flag before the final synchronize_srcu() that precedes
   ->postsuspend, so a message that passed the check is drained by
   that barrier before the reset (sashiko).

Tested on a VM running v7.4-rc1: message write paths and the memory
reporting in the reply, the shrinker retain floor under memory
pressure, prefetch/drop lifecycles, suspend/resume cycles, a
prefetch-spam-during-suspend stress loop, and error-path injection.
No kernel warnings.

[1] v1: https://lore.kernel.org/all/20261117131918.2745193-1-yangyongpeng.storage@gmail.com/
[2] v2: https://lore.kernel.org/all/20261118172133.689163-1-yangyongpeng.storage@gmail.com/
[3] v3: https://lore.kernel.org/all/20261119163110.1630269-1-yangyongpeng.storage@gmail.com/

Comments welcome.

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

Yongpeng Yang (4):
  dm-bufio: make the retain target per client
  dm-verity: size the hash retain target from the hash area
  dm-verity: add retain_bytes message to inspect and tune the cache
    floor
  dm-verity: add messages to prefetch and drop the hash cache

 .../admin-guide/device-mapper/verity.rst      |  40 ++++
 .../admin-guide/kernel-parameters.txt         |   6 +
 drivers/md/dm-bufio.c                         |  41 +++-
 drivers/md/dm-verity-target.c                 | 209 +++++++++++++++++-
 include/linux/dm-bufio.h                      |   8 +
 5 files changed, 301 insertions(+), 3 deletions(-)

-- 
2.43.0


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

* [PATCH v4 1/4] dm-bufio: make the retain target per client
  2026-11-21  8:41 [PATCH v4 0/4] dm-bufio per-client retain and dm-verity cache control Yongpeng Yang
@ 2026-11-21  8:41 ` Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 2/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Yongpeng Yang @ 2026-11-21  8:41 UTC (permalink / raw)
  To: agk, bmarzins, mpatocka, snitzer, okozina, zkabelac, dm-devel
  Cc: yangyongpeng

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.  Also export a
way to query the retain target and the cached buffer count, so a
target can report its memory use.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
v3:
- Round a non-zero target up to a block boundary instead of down, so
  it keeps at least as many bytes as requested.
---
 drivers/md/dm-bufio.c    | 41 +++++++++++++++++++++++++++++++++++++++-
 include/linux/dm-bufio.h |  8 ++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c
index 3b66cf1cb931..f3abf90793c9 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,42 @@ 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 up to a block
+ * boundary, so the target keeps at least as many bytes as requested.
+ */
+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;
+	}
+
+	/* clamp so the round-up below cannot overflow */
+	if (bytes > ULONG_MAX - c->block_size + 1)
+		bytes = ULONG_MAX - c->block_size + 1;
+
+	WRITE_ONCE(c->retain_bytes,
+		   DIV_ROUND_UP(bytes, c->block_size) * c->block_size);
+}
+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);
+
+/*
+ * The number of buffers currently cached by this client.
+ */
+unsigned long dm_bufio_get_cached_buffers(struct dm_bufio_client *c)
+{
+	return cache_total(&c->cache);
+}
+EXPORT_SYMBOL_GPL(dm_bufio_get_cached_buffers);
+
 unsigned int dm_bufio_get_block_size(struct dm_bufio_client *c)
 {
 	return c->block_size;
@@ -2389,7 +2427,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 +2560,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..f4d86b4b7e0f 100644
--- a/include/linux/dm-bufio.h
+++ b/include/linux/dm-bufio.h
@@ -158,6 +158,14 @@ 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 long dm_bufio_get_cached_buffers(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] 6+ messages in thread

* [PATCH v4 2/4] dm-verity: size the hash retain target from the hash area
  2026-11-21  8:41 [PATCH v4 0/4] dm-bufio per-client retain and dm-verity cache control Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 1/4] dm-bufio: make the retain target per client Yongpeng Yang
@ 2026-11-21  8:41 ` Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 3/4] dm-verity: add retain_bytes message to inspect and tune the cache floor Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 4/4] dm-verity: add messages to prefetch and drop the hash cache Yongpeng Yang
  3 siblings, 0 replies; 6+ messages in thread
From: Yongpeng Yang @ 2026-11-21  8:41 UTC (permalink / raw)
  To: agk, bmarzins, mpatocka, snitzer, okozina, zkabelac, dm-devel
  Cc: yangyongpeng

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 mul_u64_u32_div() and clamped at
ULONG_MAX so a large area cannot truncate to near-zero on 32-bit.  A
percentage above 100 is ignored, leaving the dm-bufio default.

The parameter only affects devices created afterwards.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
v3:
- Compute the percentage with mul_u64_u32_div() and clamp it at
  ULONG_MAX, so a large area cannot truncate to near-zero on 32-bit.
- The leftover sysfs-era bufio_kobj field is removed.
---
 .../admin-guide/device-mapper/verity.rst      | 15 +++++++++++
 .../admin-guide/kernel-parameters.txt         |  6 +++++
 drivers/md/dm-verity-target.c                 | 26 +++++++++++++++++--
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst
index 9e9d4b8157c0..7d6e567cef33 100644
--- a/Documentation/admin-guide/device-mapper/verity.rst
+++ b/Documentation/admin-guide/device-mapper/verity.rst
@@ -328,6 +328,21 @@ 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.
+
+The retain target is a floor applied by the memory shrinker, not a cap: when
+there is no memory pressure a fully warmed cache may grow past it, and when
+memory is short dm-bufio evicts buffers until the target is met again.
+
 Example
 =======
 Set up a device::
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..914da403c5de 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1351,6 +1351,12 @@ Kernel parameters
 			For details see:
 			Documentation/admin-guide/hw-vuln/reg-file-data-sampling.rst
 
+	dm_verity.hash_retain_percent=
+			[DM] Percentage of the hash area that dm-verity
+			asks dm-bufio to keep cached for a new device.
+			Defaults to 10; 0 or a value above 100 leaves the
+			dm-bufio default.
+
 	dm_verity.keyring_unsealed=
 			[KNL] When set to 1, leave the dm-verity keyring
 			unsealed after initialization so userspace can
diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index 451c75e30a8f..0e8af1a57d75 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
@@ -1084,14 +1093,14 @@ static void verity_dtr(struct dm_target *ti)
 
 	kfree(v->alg_name);
 
+	verity_fec_dtr(v);
+
 	if (v->hash_dev)
 		dm_put_device(ti, v->hash_dev);
 
 	if (v->data_dev)
 		dm_put_device(ti, v->data_dev);
 
-	verity_fec_dtr(v);
-
 	kfree(v->signature_key_desc);
 
 	if (v->use_bh_wq)
@@ -1421,6 +1430,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 +1648,18 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		goto bad;
 	}
 
+	/* Keep a configurable fraction of the hash tree cached by default. */
+	hash_retain_percent = READ_ONCE(dm_verity_hash_retain_percent);
+	if (hash_retain_percent <= 100) {
+		u64 retain = mul_u64_u32_div((u64)(v->hash_end - v->hash_start) <<
+					     v->hash_dev_block_bits,
+					     hash_retain_percent, 100);
+
+		if (retain > ULONG_MAX)
+			retain = ULONG_MAX;
+		dm_bufio_set_retain_bytes(v->bufio, retain);
+	}
+
 	/*
 	 * Using WQ_HIGHPRI improves throughput and completion latency by
 	 * reducing wait times when reading from a dm-verity device.
-- 
2.43.0


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

* [PATCH v4 3/4] dm-verity: add retain_bytes message to inspect and tune the cache floor
  2026-11-21  8:41 [PATCH v4 0/4] dm-bufio per-client retain and dm-verity cache control Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 1/4] dm-bufio: make the retain target per client Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 2/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
@ 2026-11-21  8:41 ` Yongpeng Yang
  2026-11-21  8:41 ` [PATCH v4 4/4] dm-verity: add messages to prefetch and drop the hash cache Yongpeng Yang
  3 siblings, 0 replies; 6+ messages in thread
From: Yongpeng Yang @ 2026-11-21  8:41 UTC (permalink / raw)
  To: agk, bmarzins, mpatocka, snitzer, okozina, zkabelac, dm-devel
  Cc: yangyongpeng

From: Yongpeng Yang <yangyongpeng@xiaomi.com>

Nothing lets a user see how much memory a verity device keeps cached,
or tune it for one device without touching every future client.

Add the retain_bytes <hash|fec> [<bytes>] message.  It reports the
selected client's retain target, or sets it when <bytes> is given (0
resets to the dm-bufio default); the reply also reports the bytes and
buffers currently cached, so memory use can be watched through the
message itself.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
v3:
- The reply now also reports current_bytes and buffers, replacing the
  v1/v2 sysfs interface, which is dropped entirely.
---
 .../admin-guide/device-mapper/verity.rst      | 10 +++
 drivers/md/dm-verity-target.c                 | 63 +++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst
index 7d6e567cef33..1e532c4251ae 100644
--- a/Documentation/admin-guide/device-mapper/verity.rst
+++ b/Documentation/admin-guide/device-mapper/verity.rst
@@ -343,6 +343,16 @@ The retain target is a floor applied by the memory shrinker, not a cap: when
 there is no memory pressure a fully warmed cache may grow past it, and when
 memory is short dm-bufio evicts buffers until the target is met again.
 
+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> current_bytes=<bytes> buffers=<n>``, so the
+    client's actual memory use can be watched through the message.
+
 Example
 =======
 Set up a device::
diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index 0e8af1a57d75..bd2368312b05 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -1837,6 +1837,68 @@ static void verity_postsuspend(struct dm_target *ti)
 	verity_security_clear_integrity(ti);
 }
 
+/*
+ * 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 up to a block boundary).  The reply
+ * also reports the bytes and buffers currently cached by the client.
+ */
+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 long buffers;
+	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);
+	}
+
+	buffers = dm_bufio_get_cached_buffers(bufio);
+
+	DMEMIT("retain_bytes=%lu current_bytes=%llu buffers=%lu",
+	       dm_bufio_get_retain_bytes(bufio),
+	       (u64)buffers * dm_bufio_get_block_size(bufio), buffers);
+
+	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);
+
+	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 */
@@ -1848,6 +1910,7 @@ static struct target_type verity_target = {
 	.map		= verity_map,
 	.postsuspend	= verity_postsuspend,
 	.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] 6+ messages in thread

* [PATCH v4 4/4] dm-verity: add messages to prefetch and drop the hash cache
  2026-11-21  8:41 [PATCH v4 0/4] dm-bufio per-client retain and dm-verity cache control Yongpeng Yang
                   ` (2 preceding siblings ...)
  2026-11-21  8:41 ` [PATCH v4 3/4] dm-verity: add retain_bytes message to inspect and tune the cache floor Yongpeng Yang
@ 2026-11-21  8:41 ` Yongpeng Yang
  2026-11-21  8:55   ` sashiko-bot
  3 siblings, 1 reply; 6+ messages in thread
From: Yongpeng Yang @ 2026-11-21  8:41 UTC (permalink / raw)
  To: agk, bmarzins, mpatocka, snitzer, okozina, zkabelac, dm-devel
  Cc: yangyongpeng

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 two messages.  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.  Together they let
userspace warm the cache when a device is about to be used and release
the memory when it goes cold, instead of occupying it continuously.

Both are rejected with -EBUSY while the device is suspended, since
their I/O bypasses the suspended dm queue, and with -EOPNOTSUPP on
try_verify_in_tasklet devices, whose bufio client cannot evict or wait
for a buffer that is still being read.

The suspended check needs no further serialization: __dm_suspend()
sets the suspended flag before the final synchronize_srcu() that
precedes ->postsuspend, so a message that passed the check is drained
by that barrier before the bufio reset (the whole prefetch execution,
including the window where a buffer sits with hold_count == 1 outside
dm_bufio_lock, happens inside the SRCU read section), and a message
that arrives after the barrier sees the flag.  The reads a completed
prefetch left in flight are waited out by the reset itself:
drop_buffers() evicts the buffer and __make_buffer_clean() waits on
B_READING.

Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com>
Assisted-by: LLM
---
v4:
- prefetch_hash and drop_hash now return -EBUSY while the device is
  suspended: their I/O bypasses the suspended dm queue, and the check
  is safe without further serialization because __dm_suspend() sets
  the suspended flag before the final synchronize_srcu() that precedes
  ->postsuspend, so a message that passed the check is drained before
  the bufio reset (sashiko).
v3:
- Split out of the previous message patch; contains only the cache
  lifecycle commands.
- Reject prefetch_hash on try_verify_in_tasklet devices
  (-EOPNOTSUPP), same as drop_hash.
- Accept prefetch_hash/drop_hash with an empty hash area
  (levels == 0) as a no-op instead of failing.
---
 .../admin-guide/device-mapper/verity.rst      |  15 +++
 drivers/md/dm-verity-target.c                 | 124 +++++++++++++++++-
 2 files changed, 137 insertions(+), 2 deletions(-)

diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst
index 1e532c4251ae..17e98e85e416 100644
--- a/Documentation/admin-guide/device-mapper/verity.rst
+++ b/Documentation/admin-guide/device-mapper/verity.rst
@@ -353,6 +353,21 @@ Messages
     ``retain_bytes=<bytes> current_bytes=<bytes> buffers=<n>``, so the
     client's actual memory use can be watched through the message.
 
+``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.  Not supported on devices
+    created with ``try_verify_in_tasklet`` (``-EOPNOTSUPP``), whose bufio
+    client cannot evict or wait for a buffer that is still being read.
+
+``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.
+
+``prefetch_hash`` and ``drop_hash`` return ``-EBUSY`` while the device is
+suspended, since their I/O bypasses the suspended dm queue.
+
 Example
 =======
 Set up a device::
diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index bd2368312b05..b7c29a8083d2 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -1837,12 +1837,117 @@ static void verity_postsuspend(struct dm_target *ti)
 	verity_security_clear_integrity(ti);
 }
 
+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;
+
+	/*
+	 * On a no_sleep client a buffer being read cannot be evicted or
+	 * waited for, so an unfinished prefetch would outlive a suspend or
+	 * teardown; drop_hash rejects this case for the same reason.
+	 */
+	if (v->use_bh_wq) {
+		DMERR("prefetch_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) {
+		if (!total) {
+			n = 0;
+			goto out;
+		}
+		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);
+
+out:
+	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) {
+		if (!total) {
+			n = 0;
+			goto out;
+		}
+		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);
+
+out:
+	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 up to a block boundary).  The reply
- * also reports the bytes and buffers currently cached by the client.
+ * dm-bufio default, non-zero is rounded up to a block boundary).
  */
 static int verity_retain_bytes(struct dm_verity *v, unsigned int argc,
 			       char **argv, char *result, unsigned int maxlen)
@@ -1894,6 +1999,21 @@ static int verity_message(struct dm_target *ti, unsigned int argc, char **argv,
 	if (!strcasecmp(argv[0], "retain_bytes"))
 		return verity_retain_bytes(v, argc, argv, result, maxlen);
 
+	/*
+	 * prefetch/drop touch the bufio cache and the backing device, whose
+	 * reads bypass the suspended dm queue: reject them while suspended.
+	 */
+	if (dm_suspended(ti)) {
+		DMERR("message during suspend");
+		return -EBUSY;
+	}
+
+	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;
-- 
2.43.0


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

* Re: [PATCH v4 4/4] dm-verity: add messages to prefetch and drop the hash cache
  2026-11-21  8:41 ` [PATCH v4 4/4] dm-verity: add messages to prefetch and drop the hash cache Yongpeng Yang
@ 2026-11-21  8:55   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-21  8:55 UTC (permalink / raw)
  To: Yongpeng Yang, agk, bmarzins, mpatocka, snitzer, okozina,
	zkabelac, dm-devel
  Cc: yangyongpeng

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm-verity: race between verity_message() and __dm_destroy()
  postsuspend
--

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

dm-verity: add messages to prefetch and drop the hash cache

This commit adds the prefetch_hash and drop_hash target messages to
dm-verity to allow userspace to warm or evict blocks from the hash cache.

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index 44b95dbdaebbc..04a986c343666 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c

[ ... ]

> @@ -1885,6 +1990,21 @@ static int verity_message(struct dm_target *ti, unsigned int argc, char **argv,
>  	if (!strcasecmp(argv[0], "retain_bytes"))
>  		return verity_retain_bytes(v, argc, argv, result, maxlen);
>  
> +	/*
> +	 * prefetch/drop touch the bufio cache and the backing device, whose
> +	 * reads bypass the suspended dm queue: reject them while suspended.
> +	 */
> +	if (dm_suspended(ti)) {
> +		DMERR("message during suspend");
> +		return -EBUSY;
> +	}
> +
> +	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);

[Severity: Medium]
Can verity_prefetch_hash() still race with verity_postsuspend() when an
active, unsuspended device is removed?

Unlike __dm_suspend(), __dm_destroy() sets DMF_SUSPENDED and immediately
calls dm_table_postsuspend_targets() without an intervening
synchronize_srcu(&md->io_barrier) and before waiting for md->holders to
drain:

drivers/md/dm.c:__dm_destroy() {
    ...
	if (!dm_suspended_md(md)) {
		dm_table_presuspend_targets(map);
		set_bit(DMF_SUSPENDED, &md->flags);
		set_bit(DMF_POST_SUSPENDING, &md->flags);
		dm_table_postsuspend_targets(map);
	}
    ...
}

Because target_message() only takes a holder reference (via find_device())
and does not increment md->open_count, removing an unmounted device via
dev_remove() while prefetch_hash is running allows verity_postsuspend() to
run concurrently with dm_bufio_prefetch():

CPU0                                  CPU1
target_message()
  dm_get_live_table()
  verity_message()
    dm_suspended(ti) -> false
    verity_prefetch_hash()
      dm_bufio_prefetch()
        __bufio_new()
          /* b->hold_count == 1 */
        dm_bufio_unlock(c)
                                      dev_remove()
                                        __dm_destroy()
                                          set_bit(DMF_SUSPENDED, ...)
                                          dm_table_postsuspend_targets()
                                            verity_postsuspend()
                                              dm_bufio_client_reset()
                                                drop_buffers()
                                                  <-- fails eviction / WARN_ON
        submit_io(...)
        dm_bufio_release(b)

When drop_buffers() encounters the buffer held with hold_count == 1 outside
dm_bufio_lock, eviction fails and triggers WARN_ON in warn_leak() and
drop_buffers() (or a use-after-free if CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
is enabled, where warn_leak() zeroes b->hold_count and drop_buffers() frees
b while __dm_bufio_prefetch() is still submitting I/O on it).

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

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

end of thread, other threads:[~2026-11-21  8:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  8:41 [PATCH v4 0/4] dm-bufio per-client retain and dm-verity cache control Yongpeng Yang
2026-11-21  8:41 ` [PATCH v4 1/4] dm-bufio: make the retain target per client Yongpeng Yang
2026-11-21  8:41 ` [PATCH v4 2/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
2026-11-21  8:41 ` [PATCH v4 3/4] dm-verity: add retain_bytes message to inspect and tune the cache floor Yongpeng Yang
2026-11-21  8:41 ` [PATCH v4 4/4] dm-verity: add messages to prefetch and drop the hash cache Yongpeng Yang
2026-11-21  8:55   ` 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.