dm-verity: add messages to inspect, prefetch and drop hash blocks
A message that is not in the archive (sent off-list or not yet received): [email protected]
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@@ -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 thehash_retain_percentcalculation inverity_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__udivdi3and__umoddi3calls 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 toULONG_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 exposingv->fec->data_bufiounder fec-data in commit "dm-verity: size the hash retain target from the hash area" cause a use-after-free onc->bdevduringverity_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_bufiostores a raw pointer tov->data_dev->bdev, whichdm_bufio_attr_show()dereferences when reading fec-data/bdev or fec-data/bdev_name. Becauseverity_dtr()callsdm_put_device(ti, v->data_dev)beforeverity_fec_dtr(v)destroysv->fec->data_bufioand removes its sysfs kobject, a concurrent sysfs read during target teardown can dereferencev->data_dev->bdevafter 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 callingkobject_create_and_add("dm-bufio", parent)inverity_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?
Duringdo_resume(),dm_resume()invokesverity_resume()on the new table beforedm_table_destroy()runsverity_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-EEXISTinsysfs_create_dir_ns()and callssysfs_warn_dup(), which prints a kernel warning and callsdump_stack(), and then destroying the old table removes/sys/block/dm-N/dm-bufioaltogether.
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@@ -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 inverity_prefetch_hash()andverity_drop_hash()reject zero-argument calls on a valid 0-level verity device?
Whenv->data_blocks<= 1,verity_ctr()setsv->levels= 0 andv->hash_end=v->hash_start, so total is 0. A zero-argumentprefetch_hashordrop_hashsets start = 0 and n = 0, hits start >= total, logs an error, and fails with-EINVALinstead 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]
Canverity_prefetch_hash()race withverity_postsuspend()or submit reads tov->hash_devwhile the target is suspended?
CPU0 CPU1target_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 onv->verify_wqand is drained byflush_workqueue(v->verify_wq)inverity_postsuspend()(see2de510fccbca("dm-verity: fix prefetch-vs-suspend race")),target_message()callsverity_message()without holdingmd->suspend_lockor checkingdm_suspended(ti). Ifdm_bufio_client_reset()runs while__dm_bufio_prefetch()has droppeddm_bufio_lock(c)with a buffer'shold_count== 1,drop_buffers()cannot evict that buffer and triggers the leakWARN_ONsinwarn_leak()anddrop_buffers()(and underCONFIG_DM_DEBUG_BLOCK_STACK_TRACING,warn_leak()zeroesb->hold_countanddrop_buffers()frees b beforesubmit_io()anddm_bufio_release(b)run, causing a use-after-free).
In addition, becausev->bufiosubmits reads directly tov->hash_dev->bdevrather than the dm-verity mapped device, callingprefetch_hashwhile suspended immediately issues reads tov->hash_dev->bdevand populatesv->bufioinstead of queueing them until resume as described inverity.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 callingverity_prefetch_hash()on av->use_bh_wqdevice leave in-flightB_READINGbuffers thatdrop_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_wqis enabled,v->bufiois created withDM_BUFIO_CLIENT_NO_SLEEP(c->no_sleep= true).verity_prefetch_hash()does not rejectv->use_bh_wqand submits asynchronous read BIOs tov->hash_dev->bdevwithB_READINGset andb->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 theB_READINGbuffers.drop_buffers()then triggers WARN_ON(!(*warned)) inwarn_leak(), callscond_resched()inside__cache_iterate()while holdingspin_lock_bh(&c->spinlock), and on target destruction destroysc->slab_bufferand frees c whileread_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