From: Yongpeng Yang <[email protected]>

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


Reply via email to