From: Yongpeng Yang <[email protected]>

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 <[email protected]>
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


Reply via email to