From: Yongpeng Yang <[email protected]>

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


Reply via email to