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 two messages. 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. Together they let userspace warm the cache when a device is about to be used and release the memory when it goes cold, instead of occupying it continuously. 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 --- v3: - Split out of the previous message patch; contains only the cache lifecycle commands. - 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 | 16 +++ drivers/md/dm-verity-target.c | 115 +++++++++++++++++- 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst index 1e532c4251ae..ab772fb2d7b6 100644 --- a/Documentation/admin-guide/device-mapper/verity.rst +++ b/Documentation/admin-guide/device-mapper/verity.rst @@ -353,6 +353,22 @@ Messages ``retain_bytes=<bytes> current_bytes=<bytes> buffers=<n>``, so the client's actual memory use can be watched through the message. +``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. + +While the device is suspended, ``prefetch_hash`` reads are queued and +complete on resume, and ``drop_hash`` finds an empty cache because +verity resets the bufio client on suspend; both are harmless. + Example ======= Set up a device:: diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index bd2368312b05..a4ac8a13e405 100644 --- a/drivers/md/dm-verity-target.c +++ b/drivers/md/dm-verity-target.c @@ -1837,12 +1837,117 @@ static void verity_postsuspend(struct dm_target *ti) verity_security_clear_integrity(ti); } +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). The reply - * also reports the bytes and buffers currently cached by the client. + * 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) @@ -1894,6 +1999,12 @@ static int verity_message(struct dm_target *ti, unsigned int argc, char **argv, 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; -- 2.43.0
