From: Yongpeng Yang <[email protected]>

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


Reply via email to