All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yongpeng Yang" <yangyongpeng.storage@gmail.com>,
	"Alasdair Kergon" <agk@redhat.com>,
	"Mike Snitzer" <snitzer@kernel.org>,
	"Mikulas Patocka" <mpatocka@redhat.com>,
	"Benjamin Marzinski" <bmarzins@redhat.com>
Cc: dm-devel@lists.linux.dev, "Yongpeng Yang" <yangyongpeng@xiaomi.com>
Subject: Re: [PATCH 3/4] dm-verity: size the hash retain target from the hash area
Date: Tue, 17 Nov 2026 13:44:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-218898@kernel.org> (raw)
In-Reply-To: <20261117131918.2745193-6-yangyongpeng.storage@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] dm-verity: 64-bit division and 32-bit truncation in
  verity_ctr()
- [Medium] dm-verity: duplicate sysfs dir warning and missing dir on
  table reload
- [Medium] dm-verity: release of data_dev before verity_fec_dtr() in
  verity_dtr()
- [Low] dm-verity: document dm_verity.hash_retain_percent in
  kernel-parameters.txt
--

commit 6fe0fff5c789faa19fdefcb12c794d8f32e868cd
Author: Yongpeng Yang <yangyongpeng@xiaomi.com>

dm-verity: size the hash retain target from the hash area

This commit configures the dm-verity hash bufio client's retain target as a
configurable percentage of the hash area and exposes dm-verity's bufio
clients under /sys/block/dm-N/dm-bufio/.

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index 6d589867623c0..d4fb06cb4e7ea 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -32,6 +32,8 @@
>  #define DM_VERITY_DEFAULT_PREFETCH_SIZE	262144
>  #define DM_VERITY_USE_BH_DEFAULT_BYTES	8192
>  
> +#define DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT	10
> +
>  #define DM_VERITY_MAX_CORRUPTED_ERRS	100
>  
>  #define DM_VERITY_OPT_LOGGING		"ignore_corruption"
> @@ -50,6 +52,13 @@ static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE
>  
>  module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, 0644);
>  
> +/* Default fraction of the hash tree that dm-bufio is asked to keep cached. */
> +static unsigned int dm_verity_hash_retain_percent = DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT;
> +
> +module_param_named(hash_retain_percent, dm_verity_hash_retain_percent, uint, 0644);
> +MODULE_PARM_DESC(hash_retain_percent,
> +	"Percentage of the hash area to keep cached by default");

[Severity: Low]
Since Documentation/admin-guide/kernel-parameters.txt already documents
dm_verity.keyring_unsealed=, should dm_verity.hash_retain_percent= be added
there as well?

> @@ -1083,6 +1092,9 @@ static void verity_dtr(struct dm_target *ti)
>  
>  	verity_fec_dtr(v);
>  
> +	if (v->bufio_kobj)
> +		kobject_put(v->bufio_kobj);
> +

[Severity: Medium]
Can exposing v->fec->data_bufio in sysfs cause a use-after-free of
v->data_dev->bdev during teardown?

verity_dtr() calls dm_put_device(ti, v->data_dev) before verity_fec_dtr(v)
destroys and unregisters f->data_bufio:

drivers/md/dm-verity-target.c:verity_dtr() {
    ...
	if (v->data_dev)
		dm_put_device(ti, v->data_dev);

	verity_fec_dtr(v);
    ...
}

v->fec->data_bufio is created on v->data_dev->bdev, and
dm_bufio_client_create() stores c->bdev without taking its own reference.
When FEC uses a separate device from data_dev, dm_put_device(ti, v->data_dev)
closes the underlying block device while /sys/block/dm-N/dm-bufio/fec-data is
still registered in sysfs, so a concurrent read of the bdev or bdev_name
attribute in dm_bufio_attr_show() can dereference c->bdev after it has been
released:

CPU0                                  CPU1
verity_dtr()
  dm_put_device(ti, v->data_dev)
                                      dm_bufio_attr_show()
                                        sysfs_emit(buf, "%pg\n", c->bdev)
                                          <-- use-after-free
  verity_fec_dtr(v)
    dm_bufio_client_destroy(f->data_bufio)

Calling verity_fec_dtr(v) before releasing v->data_dev and v->hash_dev would
ensure the FEC bufio clients are unregistered before their backing devices
are put.

[ ... ]

> @@ -1629,6 +1642,13 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
>  		goto bad;
>  	}
>  
> +	hash_retain_percent = READ_ONCE(dm_verity_hash_retain_percent);
> +	if (hash_retain_percent <= 100)
> +		dm_bufio_set_retain_bytes(v->bufio,
> +			mult_frac((u64)(v->hash_end - v->hash_start) <<
> +				  v->hash_dev_block_bits,
> +				  hash_retain_percent, 100));
> +

[Severity: High]
Does passing a u64 expression to mult_frac() break 32-bit builds and silently
truncate the retain target above ULONG_MAX?

mult_frac(x, n, d) declares its internal variables with typeof(x) and expands
to raw '/' and '%' operations, which emit __udivdi3 and __umoddi3 libcalls on
32-bit architectures when x is u64 and n is a runtime variable.  In addition,
dm_bufio_set_retain_bytes() takes an unsigned long, so on 32-bit kernels a
u64 result at or above 4 GiB is truncated modulo 2^32 (and if truncated to 0,
resets retain_bytes to the global default).  Using mul_u64_u32_div() and
min_t(u64, ..., ULONG_MAX) would avoid both problems.

> @@ -1806,6 +1826,45 @@ static void verity_postsuspend(struct dm_target *ti)
>  	verity_security_clear_integrity(ti);
>  }
>  
> +/*
> + * Publish the bufio clients under /sys/block/dm-N/dm-bufio/.  Runs from
> + * ->resume because the dm device's block device does not exist in ->ctr.
> + */
> +static void verity_register_bufio_sysfs(struct dm_verity *v)
> +{
> +	struct mapped_device *md = dm_table_get_md(v->ti->table);
> +	struct kobject *parent;
> +
> +	if (v->bufio_kobj)
> +		return;
> +
> +	parent = &disk_to_dev(dm_disk(md))->kobj;
> +	/*
> +	 * On a table reload the old table's directory is still there; it is
> +	 * removed only after this resume returns.  We retry on the next one.
> +	 */
> +	v->bufio_kobj = kobject_create_and_add("dm-bufio", parent);
> +	if (!v->bufio_kobj) {
> +		DMERR("cannot create dm-bufio sysfs directory, will retry on next resume");
> +		return;
> +	}

[Severity: Medium]
Does reloading a dm-verity table trigger a sysfs_warn_dup() stack trace and
leave the device without a /sys/block/dm-N/dm-bufio directory?

On table reload, do_resume() suspends the old table, swaps in the new table,
and calls dm_resume(md) before calling dm_table_destroy(old_map):

drivers/md/dm-ioctl.c:do_resume() {
    ...
	if (dm_suspended_md(md)) {
		r = dm_resume(md);
    ...
	}
    ...
	if (old_map)
		dm_table_destroy(old_map);
    ...
}

Because the old table's v->bufio_kobj is only freed in verity_dtr(),
/sys/block/dm-N/dm-bufio still exists when verity_resume() runs on the new
table.  kobject_create_and_add("dm-bufio", parent) hits -EEXIST in
sysfs_create_dir_ns() and calls sysfs_warn_dup(), which logs a duplicate
filename warning and a full dump_stack() backtrace.  Then
dm_table_destroy(old_map) runs verity_dtr() on the old table and removes the
directory, and since no second resume occurs, the reloaded table is left with
v->bufio_kobj == NULL and no dm-bufio sysfs directory.

Unregistering and freeing v->bufio_kobj (and removing the bufio clients from
sysfs) in verity_postsuspend() would release the old table's directory before
the new table's ->resume hook runs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117131918.2745193-2-yangyongpeng.storage@gmail.com?part=3

  reply	other threads:[~2026-11-17 13:44 UTC|newest]

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

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-218898@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.