From: Yongpeng Yang <[email protected]> Nothing lets a user warm a verity device's cache ahead of the reads that will need it, or release the memory a device no longer needs.
Add three messages. retain_bytes <hash|fec> [<bytes>] reports or sets a client's retain target. prefetch_hash [<start> <count>] starts reading hash blocks asynchronously; it is not limited by the retain target, which only applies to eviction. drop_hash [<start> <count>] drops hash blocks, keeping those that are in use or dirty. prefetch_hash and drop_hash are unsupported on try_verify_in_tasklet devices (-EOPNOTSUPP), whose bufio client cannot evict or wait for a buffer that is still being read. Signed-off-by: Yongpeng Yang <[email protected]> Assisted-by: LLM --- v2: - Reject prefetch_hash on try_verify_in_tasklet devices (-EOPNOTSUPP), same as drop_hash. - Accept prefetch_hash/drop_hash with an empty hash area (levels == 0) as a no-op instead of failing. --- .../admin-guide/device-mapper/verity.rst | 43 +++++ drivers/md/dm-verity-target.c | 169 ++++++++++++++++++ 2 files changed, 212 insertions(+) diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst index 9e9d4b8157c0..ec29cc6b6dc7 100644 --- a/Documentation/admin-guide/device-mapper/verity.rst +++ b/Documentation/admin-guide/device-mapper/verity.rst @@ -328,6 +328,49 @@ 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. + +Each client's retain target and memory use are visible under +``/sys/block/dm-N/dm-bufio/<hash|fec|fec-data>/`` on the verity device's own +block device, and the target can be changed by writing that directory's +``retain_bytes`` attribute; see +Documentation/admin-guide/device-mapper/dm-bufio.rst. The directories are +created when the device is activated and removed when it is suspended, so a +table reload always finds the new table's directories in place. + +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>``. + +``prefetch_hash [<start> <count>]`` + Start reading hash blocks into the cache; without arguments, the whole + hash area. The read is asynchronous and is not limited by the retain + target, which only applies to eviction. Not supported on devices + created with ``try_verify_in_tasklet`` (``-EOPNOTSUPP``), whose bufio + client cannot evict or wait for a buffer that is still being read. + +``drop_hash [<start> <count>]`` + Drop hash blocks from the cache, keeping blocks that are in use or + dirty. Not supported on devices created with ``try_verify_in_tasklet`` + (``-EOPNOTSUPP``), whose bufio client cannot drop buffers. + +Both cache commands still work while the device is suspended: prefetch +reads are queued and complete on resume, and ``drop_hash`` finds an empty +cache because verity resets the bufio client on suspend. + Example ======= Set up a device:: diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index 8059c842b9f9..94ace13c6091 100644 --- a/drivers/md/dm-verity-target.c +++ b/drivers/md/dm-verity-target.c @@ -1933,6 +1933,174 @@ static void verity_resume(struct dm_target *ti) static void verity_register_bufio_sysfs(struct dm_verity *v); +static int verity_prefetch_hash(struct dm_verity *v, unsigned int argc, + char **argv, char *result, unsigned int maxlen) +{ + sector_t total = v->hash_end - v->hash_start; + sector_t start, n; + unsigned long long value; + unsigned int sz = 0; + + /* + * On a no_sleep client a buffer being read cannot be evicted or + * waited for, so an unfinished prefetch would outlive a suspend or + * teardown; drop_hash rejects this case for the same reason. + */ + if (v->use_bh_wq) { + DMERR("prefetch_hash unsupported on tasklet-verifying device"); + return -EOPNOTSUPP; + } + + if (argc == 1) { + start = 0; + n = total; + } else if (argc == 3) { + if (kstrtoull(argv[1], 0, &value) || (sector_t)value != value) + return -EINVAL; + start = value; + + if (kstrtoull(argv[2], 0, &value) || (sector_t)value != value) + return -EINVAL; + n = value; + } else { + return -EINVAL; + } + + if (start >= total) { + if (!total) { + n = 0; + goto out; + } + DMERR("prefetch_hash start %llu beyond hash area (%llu blocks)", + (unsigned long long)start, (unsigned long long)total); + return -EINVAL; + } + + n = min(n, total - start); + n = min_t(sector_t, n, UINT_MAX); + +out: + dm_bufio_prefetch(v->bufio, v->hash_start + start, n); + + DMEMIT("prefetch_hash: started %llu blocks", (unsigned long long)n); + + return 1; +} + +static int verity_drop_hash(struct dm_verity *v, unsigned int argc, + char **argv, char *result, unsigned int maxlen) +{ + sector_t total = v->hash_end - v->hash_start; + sector_t start, n; + unsigned long long value; + unsigned int sz = 0; + + /* + * dm_bufio_forget_buffers() ends up in cache_remove_range(), which + * cannot run on a client created with DM_BUFIO_CLIENT_NO_SLEEP. + */ + if (v->use_bh_wq) { + DMERR("drop_hash unsupported on tasklet-verifying device"); + return -EOPNOTSUPP; + } + + if (argc == 1) { + start = 0; + n = total; + } else if (argc == 3) { + if (kstrtoull(argv[1], 0, &value) || (sector_t)value != value) + return -EINVAL; + start = value; + + if (kstrtoull(argv[2], 0, &value) || (sector_t)value != value) + return -EINVAL; + n = value; + } else { + return -EINVAL; + } + + if (start >= total) { + if (!total) { + n = 0; + goto out; + } + DMERR("drop_hash start %llu beyond hash area (%llu blocks)", + (unsigned long long)start, (unsigned long long)total); + return -EINVAL; + } + + n = min(n, total - start); + +out: + dm_bufio_forget_buffers(v->bufio, v->hash_start + start, n); + + DMEMIT("drop_hash: dropped %llu", (unsigned long long)n); + + return 1; +} + +/* + * 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). + */ +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 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); + } + + DMEMIT("retain_bytes=%lu", dm_bufio_get_retain_bytes(bufio)); + + 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); + + if (!strcasecmp(argv[0], "prefetch_hash")) + return verity_prefetch_hash(v, argc, argv, result, maxlen); + + if (!strcasecmp(argv[0], "drop_hash")) + return verity_drop_hash(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 */ @@ -1947,6 +2115,7 @@ static struct target_type verity_target = { .postsuspend = verity_postsuspend, .resume = verity_resume, .status = verity_status, + .message = verity_message, .prepare_ioctl = verity_prepare_ioctl, .iterate_devices = verity_iterate_devices, .io_hints = verity_io_hints, -- 2.43.0
