Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] dm-verity: unsynchronized prefetch in verity_prefetch_hash()
  across suspend
- [Low] dm-verity: unchecked start when total is 0 and duplicated range
  parsing
--

commit 836508fe811e1c71a731130ee4c4606452302695
Author: Yongpeng Yang <[email protected]>

dm-verity: add messages to inspect, prefetch and drop hash blocks

This commit adds target messages to dm-verity to inspect or set the bufio
retain target (retain_bytes), asynchronously prefetch hash blocks
(prefetch_hash), and evict cached hash blocks (drop_hash), along with
documentation in verity.rst.

> diff --git a/Documentation/admin-guide/device-mapper/verity.rst 
> b/Documentation/admin-guide/device-mapper/verity.rst
> index 9e9d4b8157c03..ec29cc6b6dc73 100644
> --- a/Documentation/admin-guide/device-mapper/verity.rst
> +++ b/Documentation/admin-guide/device-mapper/verity.rst
> @@ -328,6 +328,49 @@ Status

[ ... ]

> +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 15dee27d58a7a..78ae2e6bff186 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -1924,6 +1924,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);

[Severity: Medium]
Can verity_prefetch_hash() race with verity_postsuspend() or submit reads
while the target is suspended?

target_message() runs under the live table SRCU lock without holding
md->suspend_lock or checking dm_suspended(ti).  Unlike verity_prefetch_io(),
which queues work on v->verify_wq that verity_postsuspend() drains before
calling dm_bufio_client_reset(v->bufio), verity_prefetch_hash() calls
dm_bufio_prefetch() directly in the ioctl context:

CPU0                              CPU1
target_message()
  verity_prefetch_hash()
    __dm_bufio_prefetch()
      dm_bufio_lock(c)
      b = __bufio_new(c, block, ...)
        /* b->hold_count = 1 */
      dm_bufio_unlock(c)
                                  dm_suspend()
                                    verity_postsuspend()
                                      flush_workqueue(v->verify_wq)
                                      dm_bufio_client_reset(v->bufio)
                                        drop_buffers(c)
                                          cache_iterate(..., warn_leak, ...)
                                            WARN_ON(!(*warned))
      submit_io(b, ...)
      dm_bufio_release(b)

drop_buffers() sees b->hold_count == 1 and triggers WARN_ON in warn_leak()
and WARN_ON(cache_count(&c->cache, i)); with
CONFIG_DM_DEBUG_BLOCK_STACK_TRACING enabled, warn_leak() also resets
b->hold_count to 0 and drop_buffers() frees b while CPU0 still passes b to
submit_io() and dm_bufio_release(), causing a use-after-free and
BUG_ON(!atomic_read(&b->hold_count)).

In addition, because v->bufio is backed by v->hash_dev->bdev rather than
the dm-verity mapped_device, calling prefetch_hash while suspended
immediately submits read BIOs to v->hash_dev->bdev and populates v->bufio
(which a subsequent drop_hash while suspended can then drop), rather than
queueing reads until resume as described in verity.rst and expected by
2de510fccbca ("dm-verity: fix prefetch-vs-suspend race").

> +
> +     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;
> +     }

[Severity: Low]
When total is 0 (levels == 0), should a non-zero start argument such as
"prefetch_hash 100 10" or "drop_hash 100 10" still be rejected with
-EINVAL?

Both verity_prefetch_hash() and verity_drop_hash() take the !total branch
even when start > 0, jumping to out: and computing v->hash_start + start
with an unvalidated start offset.  Factoring the tasklet check, argument
parsing, and range clamping into a shared helper that only treats
(!start && !total) as an empty-range no-op would also remove the
duplicated parsing code between the two functions.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to