Skip to contentSkip to conversation

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

PATCH Yongpeng Yang 1 participant 1 message Nov 18 1 message not in the archive Device mapper

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Subject: Re: [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks
In-Reply-To: <[email protected]>

Quoted 32 lines · On Tue, 17 Nov 2026 at 01:49:07PM +0000, [email protected] wrote:
Quoted 8 lines
Documentation/admin-guide/device-mapper/verity.rst +0 -0
index 9e9d4b8157c03..8fea6130765bb 100644
@@ -328,6 +328,48 @@ Status
 2. Number of corrected blocks by Forward Error Correction.
    '-' if Forward Error Correction is not enabled.
 

Documentation/admin-guide/device-mapper/verity.rst Status · line 340

Quoted 10 lines
Documentation/admin-guide/device-mapper/verity.rst +10 -0
+Metadata caching
+================
+
+The hash blocks, and the FEC parity blocks when FEC is enabled, are cached in
+memory by dm-bufio.  How much of the hash tree is kept cached is controlled by
+the ``hash_retain_percent`` module parameter (default 10), settable at boot
+time with ``dm_verity.hash_retain_percent=<0-100>`` or at run time through
+``/sys/module/dm_verity/parameters/hash_retain_percent``.  A value of 0 or
+above 100 leaves the dm-bufio default (256 KiB), which is also what the FEC
+clients keep.  Only devices created afterwards are affected.

[Severity: High]
Does the hash_retain_percent calculation in verity_ctr() from commit "dm-verity: size the hash retain target from the hash area" fail to link on 32-bit architectures and truncate retain sizes at or above 4 GiB?

drivers/md/dm-verity-target.c:verity_ctr() {
    ...
	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));
    ...
}
Quoted 5 lines

mult_frac(x, n, d) uses typeof(x) and plain C '/' and '%' operators, so passing a u64 first argument emits __udivdi3 and __umoddi3 calls on 32-bit builds. In addition, dm_bufio_set_retain_bytes() takes an unsigned long, so a 64-bit byte count at or above 4 GiB wraps modulo 2^32 on 32-bit systems unless clamped to ULONG_MAX.

Same answer as in patch 3: the link failure is a false positive (__udivdi3 exists everywhere), but the truncation is real, and v2 will compute the percentage with mul_u64_u32_div() and clamp the result at ULONG_MAX, as dm-bufio does for its own cache sizing.

Quoted 17 lines

Documentation/admin-guide/device-mapper/verity.rst Status · line 346

Quoted 6 lines
Documentation/admin-guide/device-mapper/verity.rst +6 -0
+
+Each client's retain target and memory use are visible under
+``/sys/block/dm-N/dm-bufio/<hash|fec|fec-data>/`` on the verity device's own
+block device, and the target can be changed by writing that directory's
+``retain_bytes`` attribute; see
+Documentation/admin-guide/device-mapper/dm-bufio.rst.  The directories appear

[Severity: Medium]
Can exposing v->fec->data_bufio under fec-data in commit "dm-verity: size the hash retain target from the hash area" cause a use-after-free on c->bdev during verity_dtr()?

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

v->fec->data_bufio stores a raw pointer to v->data_dev->bdev, which dm_bufio_attr_show() dereferences when reading fec-data/bdev or fec-data/bdev_name. Because verity_dtr() calls dm_put_device(ti, v->data_dev) before verity_fec_dtr(v) destroys v->fec->data_bufio and removes its sysfs kobject, a concurrent sysfs read during target teardown can dereference v->data_dev->bdev after the device reference has been dropped.

Same fix as patch 3: verity_fec_dtr() will run before the device puts, so the FEC bufio clients and their sysfs entries are gone first.

Quoted 7 lines

Documentation/admin-guide/device-mapper/verity.rst Status · line 349

Quoted 3 lines
Documentation/admin-guide/device-mapper/verity.rst +3 -0
+when the device is first activated and disappear with it.  Reloading the
+table removes the old table's directories; the new table's reappear on the
+next resume.

[Severity: Medium]
Does calling kobject_create_and_add("dm-bufio", parent) in verity_register_bufio_sysfs() from commit "dm-verity: size the hash retain target from the hash area" trigger a kernel warning and stack trace on every

Quoted 8 lines

table reload?
During do_resume(), dm_resume() invokes verity_resume() on the new table before dm_table_destroy() runs verity_dtr() on the old table. Because the old table's "dm-bufio" kobject is still registered under the disk kobject, kobject_create_and_add("dm-bufio", parent) fails with -EEXIST in sysfs_create_dir_ns() and calls sysfs_warn_dup(), which prints a kernel warning and calls dump_stack(), and then destroying the old table removes /sys/block/dm-N/dm-bufio altogether.

Same fix as in patches 2 and 3: ->presuspend detaches the directories (names freed, kobjects kept) and ->resume publishes the new table's directories synchronously; no -EEXIST warning, no work, no deadlock.

Quoted 38 lines

[ ... ]

Quoted 26 lines
drivers/md/dm-verity-target.c +18 -0
index d4fb06cb4e7ea..7f92bc14d39fe 100644
@@ -1865,6 +1865,154 @@ static void verity_resume(struct dm_target *ti)
 	verity_register_bufio_sysfs(ti->private);
 }
 
+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;
+
+	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;

drivers/md/dm-verity-target.c static void verity_resume(struct dm_target *ti) · line 1895

Quoted 10 lines
drivers/md/dm-verity-target.c +10 -0
+		n = value;
+	} else {
+		return -EINVAL;
+	}
+
+	if (start >= total) {
+		DMERR("prefetch_hash start %llu beyond hash area (%llu blocks)",
+		      (unsigned long long)start, (unsigned long long)total);
+		return -EINVAL;
+	}
Quoted 8 lines

[Severity: Low]
Does the start >= total check in verity_prefetch_hash() and verity_drop_hash() reject zero-argument calls on a valid 0-level verity device?
When v->data_blocks <= 1, verity_ctr() sets v->levels = 0 and v->hash_end = v->hash_start, so total is 0. A zero-argument prefetch_hash or drop_hash sets start = 0 and n = 0, hits start >= total, logs an error, and fails with -EINVAL instead of succeeding as a 0-block no-op.

Will be fixed in v2: an empty hash area will succeed as a no-op and report 0 blocks.

Quoted 27 lines

drivers/md/dm-verity-target.c static void verity_resume(struct dm_target *ti) · line 1900

Quoted 5 lines
drivers/md/dm-verity-target.c +5 -0
+
+	n = min(n, total - start);
+	n = min_t(sector_t, n, UINT_MAX);
+
+	dm_bufio_prefetch(v->bufio, v->hash_start + start, n);

[Severity: Medium]
Can verity_prefetch_hash() race with verity_postsuspend() or submit reads to v->hash_dev while the target is suspended?
CPU0 CPU1
target_message()
verity_message()

    verity_prefetch_hash()
      dm_bufio_prefetch(v->bufio, ...)
        dm_bufio_lock(c)
        b = __bufio_new(...)
          /* 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))
                                              WARN_ON(cache_count(...))
        submit_io(b, ...)
        dm_bufio_release(b)
Quoted 15 lines

Unlike verity_prefetch_io(), which runs on v->verify_wq and is drained by flush_workqueue(v->verify_wq) in verity_postsuspend() (see 2de510fccbca ("dm-verity: fix prefetch-vs-suspend race")), target_message() calls verity_message() without holding md->suspend_lock or checking dm_suspended(ti). If dm_bufio_client_reset() runs while __dm_bufio_prefetch() has dropped dm_bufio_lock(c) with a buffer's hold_count == 1, drop_buffers() cannot evict that buffer and triggers the leak WARN_ONs in warn_leak() and drop_buffers() (and under CONFIG_DM_DEBUG_BLOCK_STACK_TRACING, warn_leak() zeroes b->hold_count and drop_buffers() frees b before submit_io() and dm_bufio_release(b) run, causing a use-after-free).
In addition, because v->bufio submits reads directly to v->hash_dev->bdev rather than the dm-verity mapped device, calling prefetch_hash while suspended immediately issues reads to v->hash_dev->bdev and populates v->bufio instead of queueing them until resume as described in verity.rst.

The hold_count == 1 window in this scenario doesn't exist: the prefetch loop releases each buffer while still holding dm_bufio_lock, so drop_buffers(), which takes the same lock, cannot observe the held state. What remains is the in-flight B_READING state, which on a normal client is waited for via wait_on_bit_io(), not warned on. So on a sleeping client this is a safe no-op or a wasted read, and on a no_sleep client it is the case below, to be rejected at the command level. I therefore left the messages available while suspended, and verity.rst documents that a suspended prefetch queues reads that complete on resume.

Quoted 33 lines
Quoted 12 lines
drivers/md/dm-verity-target.c +12 -0
+
+	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;

drivers/md/dm-verity-target.c static void verity_resume(struct dm_target *ti) · line 1922

Quoted 10 lines
drivers/md/dm-verity-target.c +10 -0
+	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;
+	}

[Severity: High]
Can calling verity_prefetch_hash() on a v->use_bh_wq device leave in-flight B_READING buffers that drop_buffers() cannot evict, leading to warnings and a use-after-free on teardown?

drivers/md/dm-bufio.c:is_clean() {
    ...
	if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep &&
	    unlikely(test_bit(B_READING, &b->state)))
		return ER_DONT_EVICT;
    ...
}
Quoted 12 lines

When v->use_bh_wq is enabled, v->bufio is created with DM_BUFIO_CLIENT_NO_SLEEP (c->no_sleep = true). verity_prefetch_hash() does not reject v->use_bh_wq and submits asynchronous read BIOs to v->hash_dev->bdev with B_READING set and b->hold_count == 0, returning to userspace while those reads are still in flight. If the device is then suspended (dm_bufio_client_reset()) or destroyed (dm_bufio_client_destroy()) before all prefetched reads complete, __get_unclaimed_buffer() -> is_clean() refuses to evict the B_READING buffers. drop_buffers() then triggers WARN_ON(!(*warned)) in warn_leak(), calls cond_resched() inside __cache_iterate() while holding spin_lock_bh(&c->spinlock), and on target destruction destroys c->slab_buffer and frees c while read_endio() is still in flight.

Confirmed. On a normal client, reset and destroy wait for in-flight reads via __make_buffer_clean()'s wait_on_bit_io(B_READING), but a no_sleep client cannot wait by design, so its eviction simply refuses the buffer and teardown proceeds with the read still in flight. Will be fixed in v2 the same way drop_hash handles it: prefetch_hash will fail with -EOPNOTSUPP on try_verify_in_tasklet devices.

Thanks for the review,
Yongpeng