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. The parameter only affects devices created afterwards. Signed-off-by: Yongpeng Yang <[email protected]> Assisted-by: LLM --- v3: - 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. - The leftover sysfs-era bufio_kobj field is removed. --- .../admin-guide/device-mapper/verity.rst | 15 +++++++++++ .../admin-guide/kernel-parameters.txt | 6 +++++ drivers/md/dm-verity-target.c | 26 +++++++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst index 9e9d4b8157c0..7d6e567cef33 100644 --- a/Documentation/admin-guide/device-mapper/verity.rst +++ b/Documentation/admin-guide/device-mapper/verity.rst @@ -328,6 +328,21 @@ 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. + +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. + Example ======= Set up a device:: 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..0e8af1a57d75 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,14 +1093,14 @@ 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); - kfree(v->signature_key_desc); if (v->use_bh_wq) @@ -1421,6 +1430,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 +1648,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. -- 2.43.0
