dm-bufio: per-client retain target, sysfs and dm-verity cache control
Subsystems (2)
v2 cover dm-bufio: per-client retain target, sysfs and dm-verity cache control
From: Yongpeng Yang <[email protected]>
Device-mapper's verity targets read their hash trees, and FEC parity when FEC is enabled, through dm-bufio. dm-bufio has a single module-wide retain_bytes value shared by every client, no way to see or change one client's memory use, and no way to warm the cache ahead of the reads that need it.
That fits poorly systems with many verified devices of very different sizes, such as Android, which mounts a dozen verity partitions plus per-app verified images: a large partition's hash tree (tens of MB) is evicted and re-read under memory pressure with the 256 KiB default, while raising the global value would make every small, idle verity device hold memory it does not need, and would still be the wrong size for most of them. The same system also wants to preload a device's hash blocks at boot and to release the memory when the device is no longer in use, which today requires dropping caches globally.
This series adds per-client granularity to dm-bufio and lets dm-verity size and manage its caches:
1. Move the retain target into struct dm_bufio_client, initialized from the module parameter at creation. The parameter keeps its meaning as the default for new clients.
2. Add dm_bufio_client_sysfs_add(), publishing a client in sysfs under a caller-chosen kobject and name, with a writable retain_bytes and the rest of the state read-only. It is opt-in, so targets that do not want it pay nothing.
3. dm-verity: size the hash client's retain target as hash_retain_percent (default 10) of the hash area, and register the hash, FEC parity and FEC data clients under /sys/block/dm-N/dm-bufio/<hash|fec|fec-data>/.
4. dm-verity: add messages to report or set a client's retain target and to prefetch or drop hash blocks, for warming the cache ahead of use and releasing it afterwards.
Nothing changes for existing users: every client starts with the value the module parameter holds, and only dm-verity opts into sysfs.
Changes since v1 [1]:
- Publish the sysfs directories from ->resume and detach them in ->presuspend, keeping the kobjects until the table is destroyed. A table reload then publishes its directories synchronously with no collision, and no kernfs drain ever runs while the queue is frozen, which would deadlock against userspace (udev) probing the suspended device.
- Round a non-zero retain target up to a block boundary, so the target keeps at least as many bytes as requested (Benjamin Marzinski).
- Compute the percentage with mul_u64_u32_div() and clamp the result at ULONG_MAX, the same way dm-bufio sizes its own cache, so a large area cannot truncate to near-zero on 32-bit.
- Zero the kobject after a failed dm_bufio_client_sysfs_add() so a retried call cannot free the already-freed name again.
- Destroy the FEC bufio clients before releasing the data and hash devices in verity_dtr(), so a concurrent sysfs read cannot dereference the block device after it is put.
- Reject prefetch_hash on try_verify_in_tasklet devices (-EOPNOTSUPP): their no-sleep bufio client cannot evict or wait for a buffer still being read.
- Accept prefetch_hash/drop_hash with no hash blocks (levels == 0) as a no-op instead of failing.
- Document dm_verity.hash_retain_percent= in kernel-parameters.txt.
Tested on a VM running v7.4-rc1: message and sysfs write paths, the shrinker retain floor under memory pressure, suspend/resume cycles, table reloads, 100 create/destroy cycles watching slab counters for leaks, and error-path injection. No kernel warnings.
[1] https://lore.kernel.org/all/[email protected]/
Comments welcome.
Yongpeng Yang (4):
dm-bufio: make the retain target per client
dm-bufio: allow exposing a client in sysfs under a caller-chosen
kobject
dm-verity: size the hash retain target from the hash area
dm-verity: add messages to inspect, prefetch and drop hash blocks
.../ABI/testing/sysfs-kernel-dm-bufio | 76 +++++ .../admin-guide/device-mapper/dm-bufio.rst | 54 ++++ .../admin-guide/device-mapper/index.rst | 1 + .../admin-guide/device-mapper/verity.rst | 43 +++ .../admin-guide/kernel-parameters.txt | 6 + drivers/md/dm-bufio.c | 200 +++++++++++- drivers/md/dm-verity-target.c | 292 +++++++++++++++++- drivers/md/dm-verity.h | 2 + include/linux/dm-bufio.h | 16 + 9 files changed, 688 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-kernel-dm-bufio create mode 100644 Documentation/admin-guide/device-mapper/dm-bufio.rst
Signature · 1 line
2.43.0
PATCH v2 1/4 dm-bufio: make the retain target per client
From: Yongpeng Yang <[email protected]>
dm_bufio_retain_bytes is a module-wide value shared by every dm-bufio user, so raising it to keep one device's metadata cached also makes every small, idle device hold on to memory it does not need.
Give each client its own target, initialized from the module parameter at creation. The parameter keeps its meaning as the default for new clients; clients that already exist are not affected.
---
v2:
- Round a non-zero target up to a block boundary instead of down, so it keeps at least as many bytes as requested.
--- drivers/md/dm-bufio.c | 32 +++++++++++++++++++++++++++++++- include/linux/dm-bufio.h | 7 +++++++ 2 files changed, 38 insertions(+), 1 deletion(-)
drivers/md/dm-bufio.c +31 -1
@@ -994,6 +994,8 @@ struct dm_bufio_client {unsigned int minimum_buffers; +unsigned long retain_bytes;/* READ_ONCE/WRITE_ONCE, no lock needed */ +sector_t start;struct shrinker *shrinker;
@@ -2284,6 +2286,33 @@ void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n) }EXPORT_SYMBOL_GPL (dm_bufio_set_minimum_buffers); +/* +* Set the number of bytes this client tries to keep cached; 0 resets it +* to the module default. Non-zero values are rounded up to a block +* boundary, so the target keeps at least as many bytes as requested. +*/ +void dm_bufio_set_retain_bytes (struct dm_bufio_client *c,unsigned long bytes) +{ +if (!bytes) { +WRITE_ONCE (c->retain_bytes,READ_ONCE (dm_bufio_retain_bytes)); +return ; + } + +/* clamp so the round-up below cannot overflow */ +if (bytes >ULONG_MAX - c->block_size +1 ) + bytes =ULONG_MAX - c->block_size +1 ; + +WRITE_ONCE (c->retain_bytes, +DIV_ROUND_UP (bytes, c->block_size) * c->block_size); +} +EXPORT_SYMBOL_GPL (dm_bufio_set_retain_bytes); + +unsigned long dm_bufio_get_retain_bytes (struct dm_bufio_client *c) +{ +return READ_ONCE (c->retain_bytes); +} +EXPORT_SYMBOL_GPL (dm_bufio_get_retain_bytes); +unsigned int dm_bufio_get_block_size (struct dm_bufio_client *c) {return c->block_size;
@@ -2389,7 +2418,7 @@ static void drop_buffers(struct dm_bufio_client *c)static unsigned long get_retain_buffers (struct dm_bufio_client *c) { -unsigned long retain_bytes =READ_ONCE (dm_bufio_retain_bytes); +unsigned long retain_bytes =READ_ONCE (c->retain_bytes);if (likely (c->sectors_per_block_bits >=0 )) retain_bytes >>= c->sectors_per_block_bits +SECTOR_SHIFT ;
@@ -2522,6 +2551,7 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsignmutex_init (&c->lock);spin_lock_init (&c->spinlock); + c->retain_bytes =READ_ONCE (dm_bufio_retain_bytes);INIT_LIST_HEAD (&c->reserved_buffers); c->need_reserved_buffers = reserved_buffers;
include/linux/dm-bufio.h +7 -0
@@ -158,6 +158,13 @@ void dm_bufio_forget_buffers(struct dm_bufio_client *c, sector_t block, sector_t*/ void dm_bufio_set_minimum_buffers (struct dm_bufio_client *c,unsigned int n); +/* +* Configure and query the per-client retain target; 0 resets the +* module default. +*/ +void dm_bufio_set_retain_bytes (struct dm_bufio_client *c,unsigned long bytes); +unsigned long dm_bufio_get_retain_bytes (struct dm_bufio_client *c); +unsigned int dm_bufio_get_block_size (struct dm_bufio_client *c);sector_t dm_bufio_get_device_size (struct dm_bufio_client *c);struct dm_io_client *dm_bufio_get_dm_io_client (struct dm_bufio_client *c);
Signature · 1 line
2.43.0
PATCH v2 2/4 dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
In reply to Yongpeng YangFrom: Yongpeng Yang <[email protected]>
There is no way to see how much memory a particular dm-bufio client is using, or to change one client's retain target without also affecting every future client.
Add dm_bufio_client_sysfs_add(), which publishes a client as <name> under a kobject supplied by its user, with the retain target writable and the rest of its state read-only. Callers that have no use for it simply never call it.
The kobject is embedded in the client, which is therefore freed only after the release method runs, as in dm-sysfs.
---
v2:
- Zero the kobject after a failed dm_bufio_client_sysfs_add() so a retried call cannot free the already-freed name again.
--- .../ABI/testing/sysfs-kernel-dm-bufio | 76 ++++++++ .../admin-guide/device-mapper/dm-bufio.rst | 54 ++++++ .../admin-guide/device-mapper/index.rst | 1 + drivers/md/dm-bufio.c | 168 ++++++++++++++++++ include/linux/dm-bufio.h | 9 + 5 files changed, 308 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-kernel-dm-bufio create mode 100644 Documentation/admin-guide/device-mapper/dm-bufio.rst
Documentation/ABI/testing/sysfs-kernel-dm-bufio new file +76 -0
@@ -0,0 +1,76 @@ +What: /sys/block/<dm-device>/dm-bufio/<name>/ +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Directory exposing one dm-bufio client whose user opted in + through dm_bufio_client_sysfs_add(). <name> is chosen by that + user; dm-verity, which places the directory under the block + device of its own dm device, uses <name> "hash" for the hash + tree client, "fec" for the FEC parity client and "fec-data" + for the FEC data client. The directory lives from + registration until the client is destroyed, independently of + any device-mapper table loaded on the device, so a suspend or + table reload never leaves dangling entries behind. The + read-only counters are instantaneous snapshots and may be + approximate. + +What: /sys/block/<dm-device>/dm-bufio/<name>/bdev +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Major and minor of the client's block device, in the form + <major>:<minor>. Read-only. + +What: /sys/block/<dm-device>/dm-bufio/<name>/bdev_name +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Name of the client's block device. Read-only. + +What: /sys/block/<dm-device>/dm-bufio/<name>/block_size +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Block size of the client in bytes. Read-only. + +What: /sys/block/<dm-device>/dm-bufio/<name>/retain_bytes +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Number of bytes dm-bufio tries to keep cached for this + client. It is a floor applied by the memory shrinker, not a + cap, so the cache may grow past it when memory is plentiful. + + Writable. A non-zero value is rounded up to a block + boundary, so the target keeps at least as many bytes as + requested; zero restores the dm-bufio default. Writing it + affects only this client, not the retain_bytes module + parameter and not other clients. + +What: /sys/block/<dm-device>/dm-bufio/<name>/current_bytes +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Number of bytes currently cached by this client. Read-only. + +What: /sys/block/<dm-device>/dm-bufio/<name>/buffers +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Number of buffers currently cached by this client. + Read-only. + +What: /sys/block/<dm-device>/dm-bufio/<name>/minimum_buffers +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Minimum number of buffers the client keeps before cleanup + happens. Read-only.
Documentation/admin-guide/device-mapper/dm-bufio.rst new file +54 -0
54 lines not on this page · raw message
Documentation/admin-guide/device-mapper/index.rst +1 -0
@@ -8,6 +8,7 @@ Device Mapper cache-policies cache delay + dm-bufio dm-clone dm-crypt dm-dust
drivers/md/dm-bufio.c +168 -0
192 lines not on this page · raw message
include/linux/dm-bufio.h +9 -0
@@ -17,6 +17,7 @@struct dm_bufio_client ;struct dm_buffer ; +struct kobject ;/* * Flags for dm_bufio_client_create
@@ -165,6 +166,14 @@ void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n);void dm_bufio_set_retain_bytes (struct dm_bufio_client *c,unsigned long bytes);unsigned long dm_bufio_get_retain_bytes (struct dm_bufio_client *c); +/* +* Publish the client as <name> under <parent> in sysfs; removed in +* dm_bufio_client_destroy(). +*/ +int dm_bufio_client_sysfs_add (struct dm_bufio_client *c,struct kobject *parent, +const char *name); +void dm_bufio_client_sysfs_detach (struct dm_bufio_client *c); +unsigned int dm_bufio_get_block_size (struct dm_bufio_client *c);sector_t dm_bufio_get_device_size (struct dm_bufio_client *c);struct dm_io_client *dm_bufio_get_dm_io_client (struct dm_bufio_client *c);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] dm-bufio: sysfs client exposure retained despite maintainer direction - [Medium] dm-bufio: module…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] dm-bufio: sysfs client exposure retained despite maintainer direction
- [Medium] dm-bufio: module unload race in dm_bufio_kobj_release()
- [Low] dm-bufio: undocumented dm_bufio_client_sysfs_detach() and lifetime claims
--
commit a1c1b2bd57f6306f0bce7f21a188c32909bec5c3
Author: Yongpeng Yang <[email protected]>
dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
This commit adds dm_bufio_client_sysfs_add() and dm_bufio_client_sysfs_detach() to expose a dm-bufio client's attributes and writable retain_bytes target in sysfs under a caller-supplied kobject.
Quoted 12 lines
Documentation/ABI/testing/sysfs-kernel-dm-bufio new file +6
-0@@ -0,0 +1,76 @@ +What: /sys/block/<dm-device>/dm-bufio/<name>/ +Date: November 2026 +KernelVersion: 7.4 +Contact: [email protected] +Description: + Directory exposing one dm-bufio client whose user opted in
Documentation/ABI/testing/sysfs-kernel-dm-bufio line 16
Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-dm-bufio new file +10
-0+ through dm_bufio_client_sysfs_add(). <name> is chosen by that + user; dm-verity, which places the directory under the block + device of its own dm device, uses <name> "hash" for the hash + tree client, "fec" for the FEC parity client and "fec-data" + for the FEC data client. The directory lives from + registration until the client is destroyed, independently of + any device-mapper table loaded on the device, so a suspend or + table reload never leaves dangling entries behind. The + read-only counters are instantaneous snapshots and may be + approximate.
[Severity: High]
As reported by Mikulas Patocka on v1 [1], should dm-bufio clients be exposed in sysfs at all?
Multiple targets in a device-mapper table would clash under the same /sys/block/<dm-device>/dm-bufio/ directory, and reloading a table creates a new dm_bufio_client and discards any retain_bytes value configured in sysfs. The maintainer requested configuring dm-bufio parameters through device-mapper target messages or constructor arguments instead.
[ ... ]
Quoted 4 lines
drivers/md/dm-bufio.c +0
-0
[ ... ]
Quoted 1 line
drivers/md/dm-bufio.c +0
-0@@ -2505,6 +2511,165 @@ static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrin
[ ... ]
Quoted 21 lines
drivers/md/dm-bufio.c +21
-0+static void dm_bufio_kobj_release (struct kobject *kobj) +{ + struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj); + +/* +* The client is freed only after this completes, as in dm-sysfs. +*/ +complete (&c->kobj_unregister); +} + +static const struct kobj_type dm_bufio_ktype = { + .sysfs_ops = &dm_bufio_sysfs_ops, + .default_groups = dm_bufio_groups, + .release = dm_bufio_kobj_release, +}; + +static int dm_bufio_kobj_add (struct dm_bufio_client *c,struct kobject *parent, +const char *name) +{ +init_completion (&c->kobj_unregister); +
drivers/md/dm-bufio.c static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrin · line 2631
Quoted 10 lines
drivers/md/dm-bufio.c +10
-0+return kobject_init_and_add (&c->kobj, &dm_bufio_ktype, parent,"%s" , + name); +} + +/* Drop the sysfs reference and wait for the release method to run. */ +static void dm_bufio_kobj_del (struct dm_bufio_client *c) +{ +kobject_put (&c->kobj); +wait_for_completion (&c->kobj_unregister); +}
[Severity: Medium]
Can dm-bufio.ko be unloaded while dm_bufio_kobj_release() is still executing?
CONFIG_DM_BUFIO is tristate, and when the release callback runs asynchronously (for example via kobject_delayed_cleanup() under CONFIG_DEBUG_KOBJECT_RELEASE=y, or when another task drops the last kobject reference), the caller waiting in dm_bufio_kobj_del() unblocks as soon as complete() is called:
CPU0 CPU1dm_bufio_client_destroy()
dm_bufio_kobj_del()
kobject_put(&c->kobj)
wait_for_completion()
kobject_release()
dm_bufio_kobj_release()
complete(&c->kobj_unregister)
<preempted> kfree(c)sys_delete_module("dm_bufio")
return; <-- unmapped module text
2995fa78e423 ("dm sysfs: fix a module unload race") added dm_kobject_release() in drivers/md/dm-builtin.c (always built into vmlinux when CONFIG_BLK_DEV_DM is enabled) so kobject release callbacks that signal a completion do not reside in unloadable module text.
Quoted 4 lines
include/linux/dm-bufio.h +0
-0
[ ... ]
include/linux/dm-bufio.h void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n); · line 175
Quoted 11 lines
include/linux/dm-bufio.h +7
-0@@ -165,6 +166,14 @@ void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n);void dm_bufio_set_retain_bytes (struct dm_bufio_client *c,unsigned long bytes);unsigned long dm_bufio_get_retain_bytes (struct dm_bufio_client *c); +/* +* Publish the client as <name> under <parent> in sysfs; removed in +* dm_bufio_client_destroy(). +*/ +int dm_bufio_client_sysfs_add (struct dm_bufio_client *c,struct kobject *parent, +const char *name); +void dm_bufio_client_sysfs_detach (struct dm_bufio_client *c);
[Severity: Low]
This comment, the commit message, Documentation/ABI/testing/sysfs-kernel-dm-bufio, and Documentation/admin-guide/device-mapper/dm-bufio.rst all state that the sysfs directory lives from registration until the client is destroyed in dm_bufio_client_destroy(), without mentioning dm_bufio_client_sysfs_detach() or that dm_bufio_client_sysfs_add() can re-attach a detached directory across suspend and table reload.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/4 dm-verity: size the hash retain target from the hash area
In reply to Yongpeng YangFrom: Yongpeng Yang <[email protected]>
With the dm-bufio default of 256 KiB a large device re-reads hash blocks evicted under memory pressure, while a small device holds far more than it needs.
Set the hash client's target to hash_retain_percent (default 10) of the hash area, computed with mul_u64_u32_div() and clamped at ULONG_MAX so a large area cannot truncate to near-zero on 32-bit. A percentage above 100 is ignored, leaving the dm-bufio default.
Publish the clients under /sys/block/dm-N/dm-bufio/<name>. Registration runs from ->resume, not ->ctr, because the dm device's block device does not exist until the table is loaded; the directories are detached again in ->presuspend, which frees the names but keeps the kobjects until the table is destroyed. A reloaded table can then publish its own directories synchronously from ->resume, with no collision and no kernfs drain while the queue is frozen. The parameter only affects devices created afterwards.
---
v2:
- Destroy the FEC bufio clients before putting the data and hash devices in verity_dtr().
- Compute the percentage with mul_u64_u32_div() and clamp it at ULONG_MAX, so a large area cannot truncate to near-zero on 32-bit.
- Detach the sysfs directories in ->presuspend (names freed, kobjects kept until the table is destroyed) so a table reload publishes its directories synchronously from ->resume; add dm_bufio_client_sysfs_detach() and re-attachment in dm_bufio_client_sysfs_add().
- Document dm_verity.hash_retain_percent= in kernel-parameters.txt.
--- .../admin-guide/kernel-parameters.txt | 6 + drivers/md/dm-verity-target.c | 123 +++++++++++++++++- drivers/md/dm-verity.h | 2 + 3 files changed, 130 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -1351,6 +1351,12 @@ Kernel parameters For details see: Documentation/admin-guide/hw-vuln/reg-file-data-sampling.rst + dm_verity.hash_retain_percent= + [DM] Percentage of the hash area that dm-verity + asks dm-bufio to keep cached for a new device. + Defaults to 10; 0 or a value above 100 leaves the + dm-bufio default. + dm_verity.keyring_unsealed= [KNL] When set to 1, leave the dm-verity keyring unsealed after initialization so userspace can
drivers/md/dm-verity-target.c +122 -1
178 lines not on this page · raw message
drivers/md/dm-verity.h +2 -0
@@ -82,6 +82,8 @@ struct dm_verity {struct dm_io_client *io;mempool_t recheck_pool; + +struct kobject *bufio_kobj;/* /sys/block/dm-N/dm-bufio, or NULL */ };struct pending_block {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 3/4] dm-verity: size the hash retain target from the hash area
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Low] dm-verity: missing return on kobject_add() error in verity_presuspend_undo() - [Low] dm-verity…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Low] dm-verity: missing return on kobject_add() error in verity_presuspend_undo()
- [Low] dm-verity: redundant post-definition verity_register_bufio_sysfs() prototype
- [Low] dm-verity: dead code and redundant kobject reallocation in verity_resume()
--
commit 2adf10b9bb56217937bcf599cad99cc4d02fedf6
Author: Yongpeng Yang <[email protected]>
dm-verity: size the hash retain target from the hash area
This commit sets the default dm-bufio retain target for a dm-verity hash client to a configurable percentage of the hash area and publishes the dm-bufio clients in sysfs under /sys/block/dm-N/dm-bufio/.
Quoted 4 lines
drivers/md/dm-verity-target.c +0
-0
[ ... ]
Quoted 36 lines
drivers/md/dm-verity-target.c +32
-0@@ -1797,6 +1825,49 @@ static inline void verity_security_clear_integrity(struct dm_target *ti)#endif /* CONFIG_SECURITY */ +/* Detach the dm-bufio directories from sysfs, keeping the kobjects. */ +static void verity_detach_bufio_sysfs (struct dm_verity *v) +{ +dm_bufio_client_sysfs_detach (v->bufio); +if (verity_fec_is_enabled (v)) { +dm_bufio_client_sysfs_detach (v->fec->bufio); +dm_bufio_client_sysfs_detach (v->fec->data_bufio); + } + +if (v->bufio_kobj && v->bufio_kobj->state_in_sysfs) +kobject_del (v->bufio_kobj); +} + +/* +* Free the directory names before a table reload resumes a new +* instance. Presuspend runs before the queue is frozen, so the kernfs +* drain cannot block on I/O. +*/ +static void verity_presuspend (struct dm_target *ti) +{ +verity_detach_bufio_sysfs (ti->private); +} + +static void verity_presuspend_undo (struct dm_target *ti) +{ +struct dm_verity *v = ti->private; +struct mapped_device *md =dm_table_get_md (v->ti->table); +struct kobject *parent = &disk_to_dev (dm_disk (md))->kobj; + +if (!v->bufio_kobj) +return ; +
drivers/md/dm-verity-target.c static inline void verity_security_clear_integrity(struct dm_target *ti) · line 1869
Quoted 10 lines
drivers/md/dm-verity-target.c +10
-0+if (!v->bufio_kobj->state_in_sysfs && +kobject_add (v->bufio_kobj, parent,"%s" ,"dm-bufio" )) +DMERR ("cannot re-attach dm-bufio sysfs directory" ); + + (void )dm_bufio_client_sysfs_add (v->bufio, v->bufio_kobj,"hash" ); +if (verity_fec_is_enabled (v)) { + (void )dm_bufio_client_sysfs_add (v->fec->bufio, v->bufio_kobj,"fec" ); + (void )dm_bufio_client_sysfs_add (v->fec->data_bufio, v->bufio_kobj,"fec-data" ); + } +}
[Severity: Low]
Should verity_presuspend_undo() return early when kobject_add() fails to re-attach v->bufio_kobj? Falling through passes the unattached v->bufio_kobj (with a NULL kernfs node) as the parent to dm_bufio_client_sysfs_add(), causing each child registration to fail with -ENOENT and emit additional kobject error messages. Also, the client registration sequence here duplicates verity_register_bufio_sysfs().
Quoted 41 lines
drivers/md/dm-verity-target.c +34
-0static void verity_postsuspend (struct dm_target *ti) {struct dm_verity *v = ti->private;@@ -1806,6 +1877,53 @@ static void verity_postsuspend(struct dm_target *ti)verity_security_clear_integrity (ti); } +/* +* Publish the bufio clients under /sys/block/dm-N/dm-bufio/. This runs +* from ->resume, not ->ctr, because the dm device's block device does not +* exist until the table is loaded. +*/ +static void verity_register_bufio_sysfs (struct dm_verity *v) +{ +struct mapped_device *md =dm_table_get_md (v->ti->table); +struct kobject *parent = &disk_to_dev (dm_disk (md))->kobj; + +if (v->bufio_kobj) +return ; + + v->bufio_kobj =kobject_create_and_add ("dm-bufio" , parent); +if (!v->bufio_kobj) { +DMERR ("cannot create dm-bufio sysfs directory" ); +return ; + } + +if (dm_bufio_client_sysfs_add (v->bufio, v->bufio_kobj,"hash" )) +DMERR ("cannot register hash bufio client in sysfs" ); + +if (verity_fec_is_enabled (v)) { +if (dm_bufio_client_sysfs_add (v->fec->bufio, v->bufio_kobj,"fec" )) +DMERR ("cannot register FEC bufio client in sysfs" ); +if (dm_bufio_client_sysfs_add (v->fec->data_bufio, v->bufio_kobj,"fec-data" )) +DMERR ("cannot register FEC data bufio client in sysfs" ); + } +} + +static void verity_resume (struct dm_target *ti) +{ +struct dm_verity *v = ti->private; +
drivers/md/dm-verity-target.c static void verity_postsuspend(struct dm_target *ti) · line 1923
Quoted 10 lines
drivers/md/dm-verity-target.c +10
-0+if (v->bufio_kobj) { +/* detached by presuspend, or re-attached by presuspend_undo */ +if (v->bufio_kobj->state_in_sysfs) +kobject_del (v->bufio_kobj); +kobject_put (v->bufio_kobj); + v->bufio_kobj =NULL ; + } + +verity_register_bufio_sysfs (v); +}
[Severity: Low]
Why does verity_resume() drop and reallocate v->bufio_kobj instead of re-attaching it?
The commit message notes that presuspend keeps the kobjects until the table is destroyed, and verity_register_bufio_sysfs() checks if v->bufio_kobj is already allocated, which is dead code because verity_resume() is its only caller and always clears v->bufio_kobj first. In addition, if v->bufio_kobj->state_in_sysfs were ever set on entry to verity_resume(), calling kobject_del(v->bufio_kobj) without detaching the child client kobjects first would leave their state_in_sysfs flags set and cause the subsequent dm_bufio_client_sysfs_add() calls to fail with -EBUSY.
drivers/md/dm-verity-target.c static void verity_postsuspend(struct dm_target *ti) · line 1925
Quoted 2 lines
drivers/md/dm-verity-target.c +2
-0+ +static void verity_register_bufio_sysfs (struct dm_verity *v);
[Severity: Low]
This isn't a bug, but this forward declaration of verity_register_bufio_sysfs() is placed after both its definition and its caller.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/4 dm-verity: add messages to inspect, prefetch and drop hash blocks
In reply to Yongpeng YangFrom: Yongpeng Yang <[email protected]>
Nothing lets a user warm a verity device's cache ahead of the reads that will need it, or release the memory a device no longer needs.
Add three messages. retain_bytes <hash|fec> [<bytes>] reports or sets a client's retain target. prefetch_hash [<start> <count>] starts reading hash blocks asynchronously; it is not limited by the retain target, which only applies to eviction. drop_hash [<start> <count>] drops hash blocks, keeping those that are in use or dirty.
prefetch_hash and drop_hash are unsupported on try_verify_in_tasklet devices (-EOPNOTSUPP), whose bufio client cannot evict or wait for a buffer that is still being read.
---
v2:
- Reject prefetch_hash on try_verify_in_tasklet devices (-EOPNOTSUPP), same as drop_hash.
- Accept prefetch_hash/drop_hash with an empty hash area (levels == 0) as a no-op instead of failing.
--- .../admin-guide/device-mapper/verity.rst | 43 +++++ drivers/md/dm-verity-target.c | 169 ++++++++++++++++++ 2 files changed, 212 insertions(+)
Documentation/admin-guide/device-mapper/verity.rst +43 -0
@@ -328,6 +328,49 @@ Status 2. Number of corrected blocks by Forward Error Correction. '-' if Forward Error Correction is not enabled. +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. + +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 are +created when the device is activated and removed when it is suspended, so a +table reload always finds the new table's directories in place. + +Messages +======== + +``retain_bytes <hash|fec> [<bytes>]`` + Report the retain target of the hash or FEC parity client, or set it + (0 resets to the dm-bufio default). ``fec`` requires FEC to be + enabled. The reply is ``retain_bytes=<bytes>``. + +``prefetch_hash [<start> <count>]`` + Start reading hash blocks into the cache; without arguments, the whole + hash area. The read is asynchronous and is not limited by the retain + target, which only applies to eviction. Not supported on devices + created with ``try_verify_in_tasklet`` (``-EOPNOTSUPP``), whose bufio + client cannot evict or wait for a buffer that is still being read. + +``drop_hash [<start> <count>]`` + Drop hash blocks from the cache, keeping blocks that are in use or + dirty. Not supported on devices created with ``try_verify_in_tasklet`` + (``-EOPNOTSUPP``), whose bufio client cannot drop buffers. + +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::
drivers/md/dm-verity-target.c +169 -0
181 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks
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…
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.
Quoted 5 lines
Documentation/admin-guide/device-mapper/verity.rst +0
-0@@ -328,6 +328,49 @@ Status
[ ... ]
Quoted 53 lines
Documentation/admin-guide/device-mapper/verity.rst +4
-0+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::drivers/md/dm-verity-target.c +38
-0@@ -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; + }
drivers/md/dm-verity-target.c static void verity_resume(struct dm_target *ti) · line 1974
Quoted 10 lines
drivers/md/dm-verity-target.c +10
-0+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 CPU1target_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").
Quoted 37 lines
drivers/md/dm-verity-target.c +37
-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; +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 ; + }
drivers/md/dm-verity-target.c static void verity_resume(struct dm_target *ti) · line 2030
Quoted 10 lines
drivers/md/dm-verity-target.c +10
-0+ +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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4