All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yongpeng Yang" <yangyongpeng.storage@gmail.com>,
	agk@redhat.com, bmarzins@redhat.com, dm-devel@lists.linux.dev,
	mpatocka@redhat.com, snitzer@kernel.org
Cc: yangyongpeng@xiaomi.com
Subject: Re: [PATCH v2 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks
Date: Wed, 18 Nov 2026 17:43:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-221679@kernel.org> (raw)
In-Reply-To: <20261118172133.689163-6-yangyongpeng.storage@gmail.com>

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 <yangyongpeng@xiaomi.com>

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/20261118172133.689163-2-yangyongpeng.storage@gmail.com?part=4

      reply	other threads:[~2026-11-18 17:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 17:21 [PATCH v2 0/4] dm-bufio: per-client retain target, sysfs and dm-verity cache control Yongpeng Yang
2026-11-18 17:21 ` [PATCH v2 1/4] dm-bufio: make the retain target per client Yongpeng Yang
2026-11-18 17:21 ` [PATCH v2 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject Yongpeng Yang
2026-11-18 17:57   ` sashiko-bot
2026-11-18 17:21 ` [PATCH v2 3/4] dm-verity: size the hash retain target from the hash area Yongpeng Yang
2026-11-18 17:50   ` sashiko-bot
2026-11-18 17:21 ` [PATCH v2 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks Yongpeng Yang
2026-11-18 17:43   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-221679@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=mpatocka@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=snitzer@kernel.org \
    --cc=yangyongpeng.storage@gmail.com \
    --cc=yangyongpeng@xiaomi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.