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 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. Publish the clients under /sys/block/dm-N/dm-bufio/<name>. Registration runs from ->resume, not ->ctr, because the dm device's block device does not exist until the table is loaded; the directories are detached again in ->presuspend, which frees the names but keeps the kobjects until the table is destroyed. A reloaded table can then publish its own directories synchronously from ->resume, with no collision and no kernfs drain while the queue is frozen. The parameter only affects devices created afterwards. Signed-off-by: Yongpeng Yang <[email protected]> Assisted-by: LLM --- v2: - Destroy the FEC bufio clients before putting the data and hash devices in verity_dtr(). - 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. - Detach the sysfs directories in ->presuspend (names freed, kobjects kept until the table is destroyed) so a table reload publishes its directories synchronously from ->resume; add dm_bufio_client_sysfs_detach() and re-attachment in dm_bufio_client_sysfs_add(). - Document dm_verity.hash_retain_percent= in kernel-parameters.txt. --- .../admin-guide/kernel-parameters.txt | 6 + drivers/md/dm-verity-target.c | 123 +++++++++++++++++- drivers/md/dm-verity.h | 2 + 3 files changed, 130 insertions(+), 1 deletion(-) 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..8059c842b9f9 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,13 +1093,19 @@ 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); + if (v->bufio_kobj) { + if (v->bufio_kobj->state_in_sysfs) + kobject_del(v->bufio_kobj); + kobject_put(v->bufio_kobj); + } kfree(v->signature_key_desc); @@ -1421,6 +1436,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 +1654,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. @@ -1806,6 +1834,49 @@ static inline void verity_security_clear_integrity(struct dm_target *ti) #endif /* CONFIG_SECURITY */ +/* Detach the dm-bufio directories from sysfs, keeping the kobjects. */ +static void verity_detach_bufio_sysfs(struct dm_verity *v) +{ + dm_bufio_client_sysfs_detach(v->bufio); + if (verity_fec_is_enabled(v)) { + dm_bufio_client_sysfs_detach(v->fec->bufio); + dm_bufio_client_sysfs_detach(v->fec->data_bufio); + } + + if (v->bufio_kobj && v->bufio_kobj->state_in_sysfs) + kobject_del(v->bufio_kobj); +} + +/* + * Free the directory names before a table reload resumes a new + * instance. Presuspend runs before the queue is frozen, so the kernfs + * drain cannot block on I/O. + */ +static void verity_presuspend(struct dm_target *ti) +{ + verity_detach_bufio_sysfs(ti->private); +} + +static void verity_presuspend_undo(struct dm_target *ti) +{ + struct dm_verity *v = ti->private; + struct mapped_device *md = dm_table_get_md(v->ti->table); + struct kobject *parent = &disk_to_dev(dm_disk(md))->kobj; + + if (!v->bufio_kobj) + return; + + if (!v->bufio_kobj->state_in_sysfs && + kobject_add(v->bufio_kobj, parent, "%s", "dm-bufio")) + DMERR("cannot re-attach dm-bufio sysfs directory"); + + (void)dm_bufio_client_sysfs_add(v->bufio, v->bufio_kobj, "hash"); + if (verity_fec_is_enabled(v)) { + (void)dm_bufio_client_sysfs_add(v->fec->bufio, v->bufio_kobj, "fec"); + (void)dm_bufio_client_sysfs_add(v->fec->data_bufio, v->bufio_kobj, "fec-data"); + } +} + static void verity_postsuspend(struct dm_target *ti) { struct dm_verity *v = ti->private; @@ -1815,6 +1886,53 @@ static void verity_postsuspend(struct dm_target *ti) verity_security_clear_integrity(ti); } +/* + * Publish the bufio clients under /sys/block/dm-N/dm-bufio/. This runs + * from ->resume, not ->ctr, because the dm device's block device does not + * exist until the table is loaded. + */ +static void verity_register_bufio_sysfs(struct dm_verity *v) +{ + struct mapped_device *md = dm_table_get_md(v->ti->table); + struct kobject *parent = &disk_to_dev(dm_disk(md))->kobj; + + if (v->bufio_kobj) + return; + + v->bufio_kobj = kobject_create_and_add("dm-bufio", parent); + if (!v->bufio_kobj) { + DMERR("cannot create dm-bufio sysfs directory"); + 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) +{ + struct dm_verity *v = ti->private; + + if (v->bufio_kobj) { + /* detached by presuspend, or re-attached by presuspend_undo */ + if (v->bufio_kobj->state_in_sysfs) + kobject_del(v->bufio_kobj); + kobject_put(v->bufio_kobj); + v->bufio_kobj = NULL; + } + + verity_register_bufio_sysfs(v); +} + +static void verity_register_bufio_sysfs(struct dm_verity *v); + static struct target_type verity_target = { .name = "verity", /* Note: the LSMs depend on the singleton and immutable features */ @@ -1824,7 +1942,10 @@ static struct target_type verity_target = { .ctr = verity_ctr, .dtr = verity_dtr, .map = verity_map, + .presuspend = verity_presuspend, + .presuspend_undo = verity_presuspend_undo, .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
