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. Both are rejected with -EBUSY while the device is suspended, since their I/O bypasses the suspended dm queue, and with -EOPNOTSUPP on try_verify_in_tasklet devices, whose bufio client cannot evict or wait for a buffer that is still being read. The suspended check needs no further serialization: __dm_suspend() sets the suspended flag before the final synchronize_srcu() that precedes ->postsuspend, so a message that passed the check is drained by that barrier before the bufio reset (the whole prefetch execution, including the window where a buffer sits with hold_count == 1 outside dm_bufio_lock, happens inside the SRCU read section), and a message that arrives after the barrier sees the flag. The reads a completed prefetch left in flight are waited out by the reset itself: drop_buffers() evicts the buffer and __make_buffer_clean() waits on B_READING. Signed-off-by: Yongpeng Yang <[email protected]> Assisted-by: LLM --- v4: - prefetch_hash and drop_hash now return -EBUSY while the device is suspended: their I/O bypasses the suspended dm queue, and the check is safe without further serialization because __dm_suspend() sets the suspended flag before the final synchronize_srcu() that precedes ->postsuspend, so a message that passed the check is drained before the bufio reset (sashiko). 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 | 15 +++ drivers/md/dm-verity-target.c | 124 +++++++++++++++++- 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/device-mapper/verity.rst b/Documentation/admin-guide/device-mapper/verity.rst index 1e532c4251ae..17e98e85e416 100644 --- a/Documentation/admin-guide/device-mapper/verity.rst +++ b/Documentation/admin-guide/device-mapper/verity.rst @@ -353,6 +353,21 @@ 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. + +``prefetch_hash`` and ``drop_hash`` return ``-EBUSY`` while the device is +suspended, since their I/O bypasses the suspended dm queue. + Example ======= Set up a device:: diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index bd2368312b05..b7c29a8083d2 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,21 @@ 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); + /* + * prefetch/drop touch the bufio cache and the backing device, whose + * reads bypass the suspended dm queue: reject them while suspended. + */ + if (dm_suspended(ti)) { + DMERR("message during suspend"); + return -EBUSY; + } + + 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
