dm-bufio: per-client retain target, sysfs and dm-verity cache control
Subsystems (3)
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.
Tested on a VM running v7.4-rc1: message and sysfs write paths, the shrinker retain floor under memory pressure, 500 suspend/resume cycles, 100 create/destroy cycles watching slab counters for leaks, and error-path injection. No kernel warnings.
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 | 42 ++++ drivers/md/dm-bufio.c | 178 ++++++++++++++- drivers/md/dm-verity-target.c | 209 ++++++++++++++++++ drivers/md/dm-verity.h | 2 + include/linux/dm-bufio.h | 15 ++ 8 files changed, 576 insertions(+), 1 deletion(-) 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 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.
--- drivers/md/dm-bufio.c | 31 ++++++++++++++++++++++++++++++- include/linux/dm-bufio.h | 7 +++++++ 2 files changed, 37 insertions(+), 1 deletion(-)
drivers/md/dm-bufio.c +30 -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,32 @@ 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 to a block +* boundary, at least one block. +*/ +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 ; + } + + bytes -= bytes % c->block_size; +if (!bytes) + bytes = c->block_size; + +WRITE_ONCE (c->retain_bytes, bytes); +} +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 +2417,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 +2550,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
Benjamin Marzinski
maintainerRe: [PATCH 1/4] dm-bufio: make the retain target per client
Quoted 6 lines · Yongpeng Yang
From: Yongpeng Yang <[email protected]>
dm_bufio_retain_bytesis 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.
Quoted 6 lines
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.
Signed-off-by Yongpeng Yang <[email protected]>Assisted-by LLM
Looks good except one minor nitpick.
Quoted 9 lines
--- drivers/md/dm-bufio.c | 31 ++++++++++++++++++++++++++++++- include/linux/dm-bufio.h | 7 +++++++ 2 files changed, 37 insertions(+), 1 deletion(-)drivers/md/dm-bufio.c +0
-0
[ ... ]
Quoted 9 lines
drivers/md/dm-bufio.c +5
-0@@ -2284,6 +2286,32 @@ 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 to a block +* boundary, at least one block. +*/
drivers/md/dm-bufio.c void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n) · line 2303
Quoted 10 lines
drivers/md/dm-bufio.c +10
-0+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 ; + } + + bytes -= bytes % c->block_size; +if (!bytes) + bytes = c->block_size;
Instead of rounding down, except when that would round to 0, why not always round up, so dm_bufio retains at least as many bytes as requested?
-Ben
Re: [PATCH 1/4] dm-bufio: make the retain target per client
Quoted 15 lines · Benjamin Marzinski
drivers/md/dm-bufio.c void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n) · line 2303
Quoted 10 lines · Yongpeng Yang
drivers/md/dm-bufio.c +10
-0+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 ; + } + + bytes -= bytes % c->block_size; +if (!bytes) + bytes = c->block_size;Instead of rounding down, except when that would round to 0, why not always round up, so
dm_bufioretains at least as many bytes as requested?
Makes sense, I'll fix it in v2.
Thanks for the review,
Yongpeng
PATCH 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.
--- .../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 | 147 ++++++++++++++++++ include/linux/dm-bufio.h | 8 + 5 files changed, 286 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 to a block + boundary (but at least one block); 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 +147 -0
171 lines not on this page · raw message
include/linux/dm-bufio.h +8 -0
@@ -17,6 +17,7 @@struct dm_bufio_client ;struct dm_buffer ; +struct kobject ;/* * Flags for dm_bufio_client_create
@@ -165,6 +166,13 @@ 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); +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 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: - [Medium] dm-bufio: module unload race in dm_bufio_kobj_release() - [Medium] dm-bufio: double free of…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] dm-bufio: module unload race in dm_bufio_kobj_release()
- [Medium] dm-bufio: double free of kobj.name if dm_bufio_client_sysfs_add() is retried
- [Medium] dm-bufio: missing sysfs unregistration API breaks DM table reloads
--
commit 865aec6258e84e1a7aa1322dc0e3b22f9e5e966b
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() to expose per-client dm-bufio statistics and the writable retain_bytes target in sysfs under a caller-supplied kobject.
Quoted 4 lines
drivers/md/dm-bufio.c +0
-0
[ ... ]
Quoted 1 line
drivers/md/dm-bufio.c +0
-0@@ -2504,6 +2510,144 @@ 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 2630
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 another task is still returning from dm_bufio_kobj_release()?
CONFIG_DM_BUFIO is tristate, so dm_bufio_kobj_release() lives in loadable module text. If the last reference to c->kobj is dropped by another context after dm_bufio_kobj_del() calls kobject_put() (for example, a concurrent sysfs reader or kobject_delayed_cleanup()), that context can be preempted right after complete(&c->kobj_unregister):
CPU0 CPU1dm_bufio_client_destroy()
dm_bufio_kobj_del()
kobject_put(&c->kobj)
kobject_put(&c->kobj)
dm_bufio_kobj_release()
complete(&c->kobj_unregister)
<preempted>
wait_for_completion() kfree(c)delete_module("dm_bufio")
return; <-- unmapped module text
dm-sysfs avoids this race by placing dm_kobject_release() in built-in code in drivers/md/dm-builtin.c.
Quoted 9 lines
drivers/md/dm-bufio.c +9
-0+ +int dm_bufio_client_sysfs_add (struct dm_bufio_client *c,struct kobject *parent, +const char *name) +{ +int r; + +if (c->sysfs_added) +return -EBUSY ; +
drivers/md/dm-bufio.c static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrin · line 2649
Quoted 10 lines
drivers/md/dm-bufio.c +10
-0+ r =dm_bufio_kobj_add (c, parent, name); +if (r) { +dm_bufio_kobj_del (c); +return r; + } + + c->sysfs_added = true; +return 0 ; +} +EXPORT_SYMBOL_GPL (dm_bufio_client_sysfs_add);
[Severity: Medium]
Does retrying dm_bufio_client_sysfs_add() after a failed call double free c->kobj.name?
When kobject_init_and_add() fails after kobject_init() and kobject_set_name_vargs(), dm_bufio_kobj_del() runs kobject_cleanup(), which calls kfree_const(c->kobj.name) without clearing c->kobj.name or c->kobj.state_initialized. Because c->sysfs_added remains false and c->kobj is not zeroed on the error path, a second call to dm_bufio_client_sysfs_add() on the same client triggers the state_initialized warning in kobject_init() and then calls kfree_const(kobj->name) on the dangling pointer in kobject_set_name_vargs().
drivers/md/dm-bufio.c void dm_bufio_client_destroy(struct dm_bufio_client *c) · line 2812
Quoted 10 lines
drivers/md/dm-bufio.c +3
-0@@ -2660,6 +2804,9 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c) {unsigned int i; +if (c->sysfs_added) +dm_bufio_kobj_del (c); +drop_buffers (c);shrinker_free (c->shrinker);
[Severity: Medium]
How can a device-mapper target reload its table without colliding on the sysfs directory when c->kobj is only removed in dm_bufio_client_destroy()?
During a table reload in do_resume(), dm_suspend() suspends the old table and dm_resume() resumes the new table before dm_table_destroy() destroys the old table and its dm_bufio_client. When a target registers its clients under the device-level path /sys/block/<dm-device>/dm-bufio/<name> (as documented in Documentation/ABI/testing/sysfs-kernel-dm-bufio and used by dm-verity in commit "dm-verity: size the hash retain target from the hash area"), the old table's sysfs entries are still present when the new table resumes, so kobject_init_and_add() fails with -EEXIST and triggers a dump_stack() warning in sysfs_warn_dup(). Without an exported unregistration helper that targets can call during suspend, reloading a table fails to publish the new client's sysfs directory and leaves the device without /sys/block/<dm-device>/dm-bufio/ once the old table is destroyed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Subject: Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
In-Reply-To: <[email protected]>
Quoted 59 lines · On Tue, 17 Nov 2026 at 01:40:55PM +0000, [email protected] wrote:
Quoted 4 lines
drivers/md/dm-bufio.c +0
-0[ ... ]
Quoted 1 line
drivers/md/dm-bufio.c +0
-0@@ -2504,6 +2510,144 @@ 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 2630
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 another task is still returning fromdm_bufio_kobj_release()?CONFIG_DM_BUFIOis tristate, sodm_bufio_kobj_release()lives in loadable module text. If the last reference toc->kobjis dropped by another context afterdm_bufio_kobj_del()callskobject_put()(for example, a concurrent sysfs reader orkobject_delayed_cleanup()), that context can be preempted right aftercomplete(&c->kobj_unregister):
CPU0 CPU1dm_bufio_client_destroy()
dm_bufio_kobj_del()kobject_put(&c->kobj) kobject_put(&c->kobj) dm_bufio_kobj_release() complete(&c->kobj_unregister) <preempted> wait_for_completion()
Quoted 5 lines
kfree(c)
delete_module("dm_bufio")
return; <-- unmapped module text
dm-sysfs avoids this race by placingdm_kobject_release()in built-in code indrivers/md/dm-builtin.c.
I don't think this race is reachable here. dm-builtin.c exists because the dm kobject can be referenced from outside dm-mod itself (dm_get_from_kobject() and friends), so a foreign reference can outlive the exit path. Our client kobject is only ever referenced by dm-bufio:
nothing exports it, and sysfs readers hold kernfs active references, which are drained before release runs, not kobject references. The final put therefore always comes from dm_bufio_kobj_del() inside dm_bufio_client_destroy() or from the failed-add path, and wait_for_completion() holds the exit path until release has returned, module text included.
Quoted 23 lines
Quoted 9 lines
drivers/md/dm-bufio.c +9
-0+ +int dm_bufio_client_sysfs_add (struct dm_bufio_client *c,struct kobject *parent, +const char *name) +{ +int r; + +if (c->sysfs_added) +return -EBUSY ; +drivers/md/dm-bufio.c static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrin · line 2649
Quoted 10 lines
drivers/md/dm-bufio.c +10
-0+ r =dm_bufio_kobj_add (c, parent, name); +if (r) { +dm_bufio_kobj_del (c); +return r; + } + + c->sysfs_added = true; +return 0 ; +} +EXPORT_SYMBOL_GPL (dm_bufio_client_sysfs_add);[Severity: Medium]
Does retryingdm_bufio_client_sysfs_add()after a failed call double freec->kobj.name?
Whenkobject_init_and_add()fails afterkobject_init()and
Quoted 8 lines
kobject_set_name_vargs(),dm_bufio_kobj_del()runskobject_cleanup(), which callskfree_const(c->kobj.name)without clearingc->kobj.nameorc->kobj.state_initialized. Becausec->sysfs_addedremains false andc->kobjis not zeroed on the error path, a second call todm_bufio_client_sysfs_add()on the same client triggers thestate_initializedwarning inkobject_init()and then callskfree_const(kobj->name)on the dangling pointer inkobject_set_name_vargs().
Right -- kobject_cleanup() frees the name through a local variable and leaves kobj->name dangling. This will be fixed in v2 by zeroing the kobject after the failure, so a retried add starts from scratch:
if (r) {
dm_bufio_kobj_del(c);
/*
* kobject_cleanup() freed kobj.name but left the dangling
* pointer, so a retried add would free it again; clear the
* whole kobject to make a retry start from scratch.
*/
memset(&c->kobj, 0, sizeof(c->kobj));
return r;
}Quoted 19 lines
drivers/md/dm-bufio.c void dm_bufio_client_destroy(struct dm_bufio_client *c) · line 2812
Quoted 10 lines
drivers/md/dm-bufio.c +3
-0@@ -2660,6 +2804,9 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c) {unsigned int i; +if (c->sysfs_added) +dm_bufio_kobj_del (c); +drop_buffers (c);shrinker_free (c->shrinker);[Severity: Medium]
How can a device-mapper target reload its table without colliding on the sysfs directory whenc->kobjis only removed indm_bufio_client_destroy()?
During a table reload indo_resume(),dm_suspend()suspends the old table anddm_resume()resumes the new table beforedm_table_destroy()destroys the old table and itsdm_bufio_client. When a target registers its clients under the device-level path /sys/block/<dm-device>/dm-bufio/<name> (as documented inDocumentation/ABI/testing/sysfs-kernel-dm-bufioand
Quoted 8 lines
used by dm-verity in commit "dm-verity: size the hash retain target from the hash area"), the old table's sysfs entries are still present when the new table resumes, so
kobject_init_and_add()fails with-EEXISTand triggers adump_stack()warning insysfs_warn_dup(). Without an exported unregistration helper that targets can call during suspend, reloading a table fails to publish the new client's sysfs directory and leaves the device without /sys/block/<dm-device>/dm-bufio/ once the old table is destroyed.
Will be fixed in v2 by detaching, not deferring: ->presuspend removes the directories from sysfs (a new dm_bufio_client_sysfs_detach) but keeps the kobjects, which are released when the table is destroyed. The names are free before a reloaded table is resumed, so its directories are created synchronously from ->resume -- no warning and no delayed retry.
Presuspend runs before the queue is frozen, so the kernfs drain cannot block on I/O. The ->postsuspend removal has exactly the deadlock you describe, because it drains sysfs while userspace (udev) is blocked opening the frozen device; that is why the detach moved to the pre-freeze hook.
Thanks for the review,
Yongpeng
Mikulas Patocka
maintainerRe: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
In reply to Yongpeng YangHi
I discussed it with Alasdair and Zdenek and we concluded that it is not clear how to assign sysfs names to dm-bufio instances.
There may be multiple targets in a table - if you use the name of the md device as the base directory, they will clash.
There may be at most two tables for a given "md" device (the active table and the inactive table). Each will have its own dm-bufio instance and they will clash.
Another problem is that if you reload a dm table, you lose all dm-bufio settings that were set by the user in sysfs.
So, we concluded that it's better to do it the device-mapper-way - use the messages or constructor line to specify the dm-bufio parameters.
There is a requirement that the returned table line must be equivalent to the passed table line. If we report something different on the returned table line, it would confuse lvm.
So, we can use either constructor line or messages, but not both.
Using the mwssages has the advantage that we would not need userspace support (the settings could be modified or queried with the 'dmsetup message' command), the disadvantage is that the settings would be lost when someone reloads the table.
Using the constructor line would need userspace support (from veritysetup/integritysetup/lvm2), but the settings could be stored in metadata and re-applied when the table is reloaded.
What do you think? Which of these two possibilities is better?
Mikulas
Quoted 388 lines · Yongpeng Yang
From: 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.
Signed-off-by Yongpeng Yang <[email protected]>Assisted-by LLM--- .../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 | 147 ++++++++++++++++++ include/linux/dm-bufio.h | 8 + 5 files changed, 286 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-kernel-dm-bufio create mode 100644 Documentation/admin-guide/device-mapper/dm-bufio.rstDocumentation/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 to a block + boundary (but at least one block); 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@@ -0,0 +1,54 @@ +======== +dm-bufio +======== + +dm-bufio caches small metadata blocks on block devices for several +device-mapper targets (dm-verity, dm-thin, dm-cache, dm-integrity and +dm-ebs among others). Each user creates clients on a block device; the +caches are independent, even for clients on the same device. + +Retain target +============= + +dm-bufio bounds nothing by itself: buffers live until the memory shrinker +evicts them, and the shrinker stops once a client is back down to its +retain target. The target is a floor under memory pressure, not a cap, +so a fully warmed cache may hold far more. + +The default is the ``retain_bytes`` module parameter of dm_bufio +(256 KiB), which only affects clients created after it is changed. One +client changes its own target through the ``retain_bytes`` attribute of +its sysfs directory or a target-specific message. + +sysfs interface +=============== + +A client may be published in sysfs with dm_bufio_client_sysfs_add(), +passing the parent kobject and the directory name; dm-bufio does not +choose where it lives. The directory lives until the client is +destroyed. dm-verity registers its clients under the block device of +its own dm device:: + + /sys/block/dm-N/dm-bufio/hash/ the hash tree client + /sys/block/dm-N/dm-bufio/fec/ the FEC parity client + /sys/block/dm-N/dm-bufio/fec-data/ the FEC data client + +The attributes, including the read-write ``retain_bytes``, are documented +in Documentation/ABI/testing/sysfs-kernel-dm-bufio. + +Example +======= + +Inspect the hash client of a dm-verity device:: + + # ls /sys/block/dm-1/dm-bufio/ + fec fec-data hash + + # cat /sys/block/dm-1/dm-bufio/hash/retain_bytes + 2535424 + + # echo 8388608 > /sys/block/dm-1/dm-bufio/hash/retain_bytes + # cat /sys/block/dm-1/dm-bufio/hash/retain_bytes + 8388608 + +The other clients keep their own targets.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-dustdrivers/md/dm-bufio.c +147
-0@@ -20,6 +20,9 @@#include <linux/rbtree.h> #include <linux/stacktrace.h> #include <linux/jump_label.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> +#include <linux/completion.h> #include "dm.h" @@ -995,6 +998,9 @@ struct dm_bufio_client {unsigned int minimum_buffers;unsigned long retain_bytes;/* READ_ONCE/WRITE_ONCE, no lock needed */ +bool sysfs_added; +struct kobject kobj; +struct completion kobj_unregister;sector_t start;@@ -2504,6 +2510,144 @@ static unsigned long dm_bufio_shrink_count(struct shrinker *shrink, struct shrinreturn count; } +/* +* A client may be published in sysfs with dm_bufio_client_sysfs_add(); +* the directory lives until the client is destroyed. +*/ + +static ssize_t dm_bufio_attr_show (struct kobject *kobj,struct attribute *attr, +char *buf); +static ssize_t dm_bufio_attr_store (struct kobject *kobj,struct attribute *attr, +const char *buf,size_t size); + +#define DM_BUFIO_ATTR_RO (_name) \ + static struct attribute dm_bufio_attr_##_name = \ + { .name = #_name, .mode = 0444 } + +#define DM_BUFIO_ATTR_RW (_name) \ + static struct attribute dm_bufio_attr_##_name = \ + { .name = #_name, .mode = 0644 } + +DM_BUFIO_ATTR_RO (bdev); +DM_BUFIO_ATTR_RO (bdev_name); +DM_BUFIO_ATTR_RO (block_size); +DM_BUFIO_ATTR_RW (retain_bytes); +DM_BUFIO_ATTR_RO (current_bytes); +DM_BUFIO_ATTR_RO (buffers); +DM_BUFIO_ATTR_RO (minimum_buffers); + +static ssize_t dm_bufio_attr_show (struct kobject *kobj,struct attribute *attr, +char *buf) +{ + struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj); + +if (attr == &dm_bufio_attr_bdev) +return sysfs_emit (buf,"%d:%d\n" ,MAJOR (c->bdev->bd_dev), +MINOR (c->bdev->bd_dev)); +if (attr == &dm_bufio_attr_bdev_name) +return sysfs_emit (buf,"%pg\n" , c->bdev); +if (attr == &dm_bufio_attr_block_size) +return sysfs_emit (buf,"%u\n" , c->block_size); +if (attr == &dm_bufio_attr_retain_bytes) +return sysfs_emit (buf,"%lu\n" ,READ_ONCE (c->retain_bytes)); +if (attr == &dm_bufio_attr_current_bytes) +return sysfs_emit (buf,"%llu\n" , + (u64 )cache_total (&c->cache) * c->block_size); +if (attr == &dm_bufio_attr_buffers) +return sysfs_emit (buf,"%lu\n" ,cache_total (&c->cache)); +if (attr == &dm_bufio_attr_minimum_buffers) +return sysfs_emit (buf,"%u\n" , c->minimum_buffers); + +return -EINVAL ; +} + +static ssize_t dm_bufio_attr_store (struct kobject *kobj,struct attribute *attr, +const char *buf,size_t size) +{ + struct dm_bufio_client *c = container_of(kobj, struct dm_bufio_client, kobj); + +if (attr == &dm_bufio_attr_retain_bytes) { +unsigned long new; + +if (kstrtoul (buf,0 , &new)) +return -EINVAL ; + +dm_bufio_set_retain_bytes (c, new); + +return size; + } + +return -EINVAL ; +} + +static struct attribute *dm_bufio_attrs[] = { + &dm_bufio_attr_bdev, + &dm_bufio_attr_bdev_name, + &dm_bufio_attr_block_size, + &dm_bufio_attr_retain_bytes, + &dm_bufio_attr_current_bytes, + &dm_bufio_attr_buffers, + &dm_bufio_attr_minimum_buffers, +NULL , +}; +ATTRIBUTE_GROUPS (dm_bufio); + +static const struct sysfs_ops dm_bufio_sysfs_ops = { + .show = dm_bufio_attr_show, + .store = dm_bufio_attr_store, +}; + +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); + +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); +} + +int dm_bufio_client_sysfs_add (struct dm_bufio_client *c,struct kobject *parent, +const char *name) +{ +int r; + +if (c->sysfs_added) +return -EBUSY ; + + r =dm_bufio_kobj_add (c, parent, name); +if (r) { +dm_bufio_kobj_del (c); +return r; + } + + c->sysfs_added = true; +return 0 ; +} +EXPORT_SYMBOL_GPL (dm_bufio_client_sysfs_add); +/* * Create the buffering interface */ @@ -2660,6 +2804,9 @@ void dm_bufio_client_destroy(struct dm_bufio_client *c) {unsigned int i; +if (c->sysfs_added) +dm_bufio_kobj_del (c); +drop_buffers (c);shrinker_free (c->shrinker);include/linux/dm-bufio.h +8
-0@@ -17,6 +17,7 @@struct dm_bufio_client ;struct dm_buffer ; +struct kobject ;/* * Flags for dm_bufio_client_create @@ -165,6 +166,13 @@ 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); +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
Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Quoted 2 lines · Mikulas Patocka
There may be multiple targets in a table - if you use the name of the md device as the base directory, they will clash.
That would be a real problem, but none of the existing dm-bufio users can reach it for now: dm-verity, dm-thin and dm-integrity are all DM_TARGET_SINGLETON, so a table can hold at most one of them and the directory under a device holds at most one "hash"/"fec"/"fec-data" client per role. If a custom targe_type which allows multi target and each of them have a dm-bufio client, that will be hard to handle. I just want to show the client info through sysfs, how about show such info via message?
Quoted 3 lines
There may be at most two tables for a given "md" device (the active table and the inactive table). Each will have its own dm-bufio instance and they will clash.
That did collide in v1; in v2 the directories are detached in ->presuspend (names freed, kobjects kept until the table is destroyed), so the new table's directories are created synchronously at resume, with no warning.
Quoted 2 lines
Another problem is that if you reload a dm table, you lose all dm-bufio settings that were set by the user in sysfs.
That applies equally to messages. The default is recomputed from hash_retain_percent at every ctr, only an explicit runtime tweak is lost. Most of the sysfs content is state anyway (current_bytes/buffers), not settings.
Quoted 1 line
What do you think? Which of these two possibilities is better?
I prefer messages, please review [PATCH 4/4]:
1. The constructor line would have to be taught to veritysetup, dmctl in android and lvm2; a message is one command, no userspace change.
2. This knob wants runtime adjustment (raise at boot, drop later); persisting it in the table line makes every tweak a suspend/resume reload, while a message adjusts the live device directly.
Thanks for the review,
Yongpeng
Mikulas Patocka
maintainerRe: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Quoted 4 lines · Yongpeng Yang
Quoted 2 lines · Mikulas Patocka
There may be multiple targets in a table - if you use the name of the md device as the base directory, they will clash.
Quoted 8 lines
That would be a real problem, but none of the existing dm-bufio users can reach it for now: dm-verity, dm-thin and dm-integrity are all
DM_TARGET_SINGLETON, so a table can hold at most one of them and the directory under a device holds at most one "hash"/"fec"/"fec-data" client per role. If a customtarge_typewhich allows multi target and each of them have a dm-bufio client, that will be hard to handle. I just want to show the client info through sysfs, how about show such info via message?
Yes, showing the state in a message is better.
Quoted 10 lines
Quoted 1 line
What do you think? Which of these two possibilities is better?
I prefer messages, please review [PATCH 4/4]:
1. The constructor line would have to be taught to veritysetup, dmctl in android and lvm2; a message is one command, no userspace change.
2. This knob wants runtime adjustment (raise at boot, drop later); persisting it in the table line makes every tweak a suspend/resume reload, while a message adjusts the live device directly.
Quoted 2 lines
Thanks for the review,
Yongpeng
I see.
BTW. I'd like to ask another question - please describe in detail the performance problem that you are experiencing and that this patchset is fixing.
Is the kernel dropping dm-bufio buffers too aggressively? Or is it not dropping them so that they consume too much memory?
Could it be fixed through other means that are more general? (for example, having a global dm-bufio shrinker and dropping buffers from the client that has the longest unused buffer)
Mikulas
Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Quoted 2 lines · Mikulas Patocka
Is the kernel dropping dm-bufio buffers too aggressively? Or is it not dropping them so that they consume too much memory?
Both, and the tension is why one global value cannot work. Our hash areas run from 0.x MB to 40+ MB: the 256 KiB default makes large devices re-read evicted hash blocks, while a uniformly large value makes every cold, idle device hold memory forever -- and most verity devices are cold most of the time, which is what small-memory systems complain about.
Quoted 3 lines
Could it be fixed through other means that are more general? (for example, having a global dm-bufio shrinker and dropping buffers from the client that has the longest unused buffer)
A global LRU shrinker would tell hot from cold given time, but:
- Our userspace already knows which device will be used and when, so it does not need the heuristic, it can drop exactly that device's hash blocks, precisely and immediately.
- On low-memory devices we need the memory back quickly, not when pressure finally ages the buffers out.
- Verity, thin and cache clients share one pool with very different access patterns; a single LRU lets them fight for it, while per-client floors keep the shares apart.
Thanks for the review,
Yongpeng
Mikulas Patocka
maintainerRe: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Quoted 9 lines · Yongpeng Yang
Quoted 2 lines · Mikulas Patocka
Is the kernel dropping dm-bufio buffers too aggressively? Or is it not dropping them so that they consume too much memory?
Both, and the tension is why one global value cannot work. Our hash areas run from 0.x MB to 40+ MB: the 256 KiB default makes large devices re-read evicted hash blocks, while a uniformly large value makes every cold, idle device hold memory forever -- and most verity devices are cold most of the time, which is what small-memory systems complain about.
What about improving the shrinker memory management?
The problem with shrinkers is that the generic memory management code doesn't know how old are objects in the registered shrinkers. So, it may ask the shrinkers that have only recently used objects to free memory.
Maybe we could add an upcall that returns the last used time of the oldest object in a given shrinker, select the shrinker with the oldest object and shrink from it? Would this be feasible?
Quoted 9 lines
Quoted 3 lines
Could it be fixed through other means that are more general? (for example, having a global dm-bufio shrinker and dropping buffers from the client that has the longest unused buffer)
A global LRU shrinker would tell hot from cold given time, but:
- Our userspace already knows which device will be used and when, so it does not need the heuristic, it can drop exactly that device's hash blocks, precisely and immediately.
Please, show some numbers - how much does this patchset improve Android boot time (if you are prefetching blocks that will be needed at boot) or application load time?
Would prefetching with the readahead() syscall be better? (it would prefetch data down to the page cache, so I presume it would be more efficient)
Back in the era of mechanical disks, there used to be a daemon that prefetched blocks to improve boot time. With SSDs, the idea was abandoned because it didn't provide any advantage. Are you implementing something like this?
Quoted 2 lines
- On low-memory devices we need the memory back quickly, not when pressure finally ages the buffers out.
I don't see how shrinking bufio should be different from shrinking page cache, inodes, dentries or other kernel objects.
Maybe you need to tune /proc/sys/vm/min_free_kbytes if the kernel is not shrinking memory fast enough.
Quoted 6 lines
- Verity, thin and cache clients share one pool with very different access patterns; a single LRU lets them fight for it, while per-client floors keep the shares apart.
Thanks for the review,
Yongpeng
Mikulas
Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject
Quoted 3 lines · Mikulas Patocka
Please, show some numbers - how much does this patchset improve Android boot time (if you are prefetching blocks that will be needed at boot) or application load time?
Here are numbers from three stock phones (UFS flagship, GKI 6.18; UFS 6.12; eMMC 6.6), 3 cold boots each. Method: the sysfs block stats are cumulative since kernel start, and verity hash reads bypass the dm-verity queue and land on the underlying device, so "hash reads = underlying.sectors - verity.sectors" read at boot_completed gives the whole boot. A 4K hash block covers 128 4K data blocks, so the minimum hash traffic for the data actually read is data/128 (less with ignore_zero_blocks, which skips zero blocks -- the real minimum is lower, so the amplification ratios below are conservative).
The hash blocks are re-read under boot memory pressure, because the 256 KiB module-wide floor cannot hold any large partition's tree:
device kernel hash read minimum amplification
UFS flagship 12.1s 48.4 MB 10.8 MB 4.5x UFS 6.12 14.8s 31.1 MB 7.6 MB 4.1x eMMC mid-range 42.4s 17.7 MB 6.6 MB 2.7x
On the eMMC phone every hash read is a synchronous dependency of a data read; system_ext alone re-reads its tree 5.5x.
With the patchset (v5[1]) and a small fs_mgr change -- size each verity client's retain target to its hash tree, and send "prefetch_hash" once the device is up, off the critical path -- the same phone, 3 cold boots each:
baseline patched
kernel boot time 42.36s 41.97s
hash read at boot 17.7 MB 14.8 MB
The composition changes: the baseline's excess over the needed minimum is pure re-reads; with the per-client retain targets every device's total stays within its tree size, and the re-read amplification is gone. The prefetch itself does not move boot time -- the win it provides is elsewhere.
The boot case is one user; the other is application launch. Android reads verified data mostly through erofs on dm-verity, and preloading the files an app will need means readahead on those volumes. That readahead must stay modest -- aggressive preloading competes with the application's own I/O -- which is what makes prefetching the hash blocks the right granularity: the hash stream is a small fraction of the data volume, yet without it every block read pays an extra hash read, the hash reads occupy the verify kworkers, and on low-end devices the verity kworker CPU becomes visible (a patch has been proposed to expose the verify workqueue for tuning[2] for exactly this reason). Prefetching the hash blocks in advance, in batches, removes the per-block read and the kworker storm. The trees on our devices run 80+ MB, so caching them entirely is real pressure on low-memory phones -- which is the per-device tuning problem the retain target and the retain_bytes message expose.
Quoted 3 lines
Would prefetching with the
readahead()syscall be better? (it would prefetch data down to the page cache, so I presume it would be more efficient)
The hash reads are submitted as bios and do not consult the block device's page cache, so readahead() on the hash device warms a cache that verity never reads through. The test build ships AOSP's libprefetch (posix_fadvise-based, page-cache level) and it cannot reach dm-bufio whether enabled or not.
The other direction -- reading the data files so that their hash blocks come up as a side effect -- is not precise either: the verified volumes are erofs filesystems, one 4K hash block covers 512 KB of data across file boundaries, and userspace cannot know which files map to the hash blocks it will need. Making sure the needed hash blocks are resident that way means reading a large amount of data, and for application launch that volume is not acceptable. The message reads exactly the hash blocks and nothing else.
Quoted 4 lines
Back in the era of mechanical disks, there used to be a daemon that prefetched blocks to improve boot time. With SSDs, the idea was abandoned because it didn't provide any advantage. Are you implementing something like this?
No: this is not sequential-throughput streaming. Each 4K data read must first fetch and verify its hash block, and pressure eviction makes it fetch it again; the numbers above are that dependency and those re-reads, which a faster disk reduces but does not remove.
Quoted 3 lines
Maybe we could add an upcall that returns the last used time of the oldest object in a given shrinker, select the shrinker with the oldest object and shrink from it? Would this be feasible?
For our scenario, picking the client with the oldest buffer would be an improvement over the current count-only order. But the oldest-buffer timestamp alone can misjudge: a client that is mostly hot with a few long-idle buffers would be selected first, and I have not found a single metric that weighs a client's overall temperature correctly.
The precise version would be one LRU holding every dm-buffer regardless of client. That has two problems: the lock contention of a single list shared by all clients, and the loss of any per-client priority -- there would be no way to protect the clients whose buffers matter most while reclaiming the rest. That protection is exactly what the per-client retain floor provides, which is why I see the two as complementary rather than
alternatives: the LRU would order eviction, the floor would bound it.
Quoted 2 lines
Maybe you need to tune
/proc/sys/vm/min_free_kbytesif the kernel is not shrinking memory fast enough.
min_free_kbytes is a global reserve; the need here is per-device targeting, not reclaim latency.
To summarize where this leaves the series: prefetch_hash does not move boot time on its own, but the retain_bytes changes measurably remove the re-read amplification (2.7x on this device, 4.5x on the flagship). If you have reservations about prefetch_hash and drop_hash, would you consider taking the retain_bytes-related patches first (per-client target, sizing from the hash area, the retain_bytes message -- patches 2-4 of v5) and leaving the prefetch/drop lifecycle messages for a follow-up discussion?
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Thanks for the review,
Yongpeng
PATCH 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 mult_frac(). 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 because the dm device's block device does not exist until the table is loaded. The parameter only affects devices created afterwards.
--- drivers/md/dm-verity-target.c | 60 +++++++++++++++++++++++++++++++++++ drivers/md/dm-verity.h | 2 ++ 2 files changed, 62 insertions(+)
drivers/md/dm-verity-target.c +60 -0
@@ -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_SIZEmodule_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" ); +static unsigned int dm_verity_use_bh_bytes[4 ] = {DM_VERITY_USE_BH_DEFAULT_BYTES ,// IOPRIO_CLASS_NONE DM_VERITY_USE_BH_DEFAULT_BYTES ,// IOPRIO_CLASS_RT
@@ -1092,6 +1101,9 @@ static void verity_dtr(struct dm_target *ti)verity_fec_dtr (v); +if (v->bufio_kobj) +kobject_put (v->bufio_kobj); +kfree (v->signature_key_desc);if (v->use_bh_wq)
@@ -1421,6 +1433,7 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)struct dm_verity_sig_opts verify_args = {0 };struct dm_arg_set as;unsigned int num; +unsigned int hash_retain_percent;unsigned long long num_ll;int r;int i;
@@ -1638,6 +1651,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 )); +/* * Using WQ_HIGHPRI improves throughput and completion latency by * reducing wait times when reading from a dm-verity device.
@@ -1815,6 +1835,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 ; + } + +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) +{ +verity_register_bufio_sysfs (ti->private); +} +static struct target_type verity_target = { .name ="verity" ,/* Note: the LSMs depend on the singleton and immutable features */
@@ -1825,6 +1884,7 @@ static struct target_type verity_target = { .dtr = verity_dtr, .map = verity_map, .postsuspend = verity_postsuspend, + .resume = verity_resume, .status = verity_status, .prepare_ioctl = verity_prepare_ioctl, .iterate_devices = verity_iterate_devices,
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 3/4] dm-verity: size the hash retain target from the hash area
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…
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 <[email protected]>
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/.
Quoted 14 lines
drivers/md/dm-verity-target.c +2
-0@@ -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"
drivers/md/dm-verity-target.c static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE · line 60
Quoted 9 lines
drivers/md/dm-verity-target.c +6
-0@@ -50,6 +52,13 @@ static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZEmodule_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?
drivers/md/dm-verity-target.c static void verity_dtr(struct dm_target *ti) · line 1097
Quoted 7 lines
drivers/md/dm-verity-target.c +3
-0@@ -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 CPU1verity_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.
[ ... ]
drivers/md/dm-verity-target.c static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv) · line 1651
Quoted 11 lines
drivers/md/dm-verity-target.c +7
-0@@ -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.
Quoted 16 lines
drivers/md/dm-verity-target.c +12
-0@@ -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 ; +
drivers/md/dm-verity-target.c static void verity_postsuspend(struct dm_target *ti) · line 1850
Quoted 10 lines
drivers/md/dm-verity-target.c +10
-0+ 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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/4] dm-verity: size the hash retain target from the hash area
Subject: Re: [PATCH 3/4] dm-verity: size the hash retain target from the hash area
In-Reply-To: <[email protected]>
Quoted 17 lines · On Tue, 17 Nov 2026 at 01:44:55PM +0000, [email protected] wrote:
Quoted 17 lines
drivers/md/dm-verity-target.c +2
-0@@ -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_SIZEmodule_param_named (prefetch_cluster, dm_verity_prefetch_cluster, uint,0644 );
Quoted 10 lines
drivers/md/dm-verity-target.c static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE · line 60
Quoted 6 lines
drivers/md/dm-verity-target.c +6
-0+/* 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]
SinceDocumentation/admin-guide/kernel-parameters.txtalready documents dm_verity.keyring_unsealed=, should dm_verity.hash_retain_percent= be added there as well?
Will be done in v2.
Quoted 29 lines
drivers/md/dm-verity-target.c static void verity_dtr(struct dm_target *ti) · line 1097
Quoted 7 lines
drivers/md/dm-verity-target.c +3
-0@@ -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 exposingv->fec->data_bufioin sysfs cause a use-after-free ofv->data_dev->bdevduring teardown?verity_dtr()callsdm_put_device(ti, v->data_dev)beforeverity_fec_dtr(v)destroys and unregistersf->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_bufiois created onv->data_dev->bdev, anddm_bufio_client_create()storesc->bdevwithout taking its own reference. When FEC uses a separate device fromdata_dev,dm_put_device(ti, v->data_dev)closes the underlying block device while/sys/block/dm-N/dm-bufio/fec-datais still registered in sysfs, so a concurrent read of the bdev orbdev_nameattribute indm_bufio_attr_show()can dereferencec->bdevafter it has been released:
CPU0 CPU1verity_dtr()
dm_put_device(ti, v->data_dev)
Quoted 8 lines
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 releasingv->data_devandv->hash_devwould ensure the FEC bufio clients are unregistered before their backing devices are put.
Will be fixed in v2 exactly this way: verity_dtr() will call verity_fec_dtr() before dm_put_device() of the hash and data devices, so the FEC bufio clients (and their sysfs entries) are gone before the bdev is released.
Quoted 15 lines
[ ... ]
drivers/md/dm-verity-target.c static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv) · line 1651
Quoted 11 lines
drivers/md/dm-verity-target.c +7
-0@@ -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 tomult_frac()break 32-bit builds and silently
Quoted 8 lines
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__udivdi3and__umoddi3libcalls 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, resetsretain_bytesto the global default). Usingmul_u64_u32_div()andmin_t(u64, ..., ULONG_MAX)would avoid both problems.
The build claim is a false positive: 64-bit division links fine on 32-bit, the kernel provides those libcalls. The truncation is real though, so v2 will 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.
Quoted 42 lines
Quoted 16 lines
drivers/md/dm-verity-target.c +12
-0@@ -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 ; +drivers/md/dm-verity-target.c static void verity_postsuspend(struct dm_target *ti) · line 1850
Quoted 10 lines
drivers/md/dm-verity-target.c +10
-0+ 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 asysfs_warn_dup()stack trace and leave the device without a/sys/block/dm-N/dm-bufiodirectory? On table reload,do_resume()suspends the old table, swaps in the new table, and callsdm_resume(md)before callingdm_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); ... }
Quoted 11 lines
Because the old table's
v->bufio_kobjis only freed inverity_dtr(),/sys/block/dm-N/dm-bufiostill exists whenverity_resume()runs on the new table.kobject_create_and_add("dm-bufio", parent)hits-EEXISTinsysfs_create_dir_ns()and callssysfs_warn_dup(), which logs a duplicate filename warning and a fulldump_stack()backtrace. Thendm_table_destroy(old_map)runsverity_dtr()on the old table and removes the directory, and since no second resume occurs, the reloaded table is left withv->bufio_kobj== NULL and no dm-bufio sysfs directory. Unregistering and freeingv->bufio_kobj(and removing the bufio clients from sysfs) inverity_postsuspend()would release the old table's directory before the new table's ->resume hook runs.
Same fix as in patch 2: ->presuspend detaches the directories and frees the names, keeping the kobjects until the table is destroyed, so the reloaded table publishes its directories synchronously from ->resume. The ->postsuspend variant deadlocks against udev probing the frozen device, which is what prompted the pre-freeze detach.
Thanks for the review,
Yongpeng
PATCH 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.
drop_hash is unsupported on try_verify_in_tasklet devices, whose bufio client cannot drop buffers (-EOPNOTSUPP).
--- .../admin-guide/device-mapper/verity.rst | 42 +++++ drivers/md/dm-verity-target.c | 149 ++++++++++++++++++ 2 files changed, 191 insertions(+)
Documentation/admin-guide/device-mapper/verity.rst +42 -0
@@ -328,6 +328,48 @@ 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 appear +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. + +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. + +``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 +149 -0
161 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 4/4] dm-verity: add messages to inspect, prefetch and drop hash blocks
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] dm-verity: verity_prefetch_hash() on no_sleep client causes UAF on teardown - [High] dm-verity…
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] dm-verity: verity_prefetch_hash() on no_sleep client causes UAF on teardown
- [High] dm-verity: 64-bit mult_frac() division and truncation in verity_ctr()
- [Medium] dm-verity: unsynchronized prefetch in verity_prefetch_hash() across suspend
- [Medium] dm-verity: duplicate sysfs kobject warning on reload in verity_register_bufio_sysfs()
- [Medium] dm-verity: premature dm_put_device() before verity_fec_dtr() in verity_dtr()
- [Low] dm-verity: zero-arg prefetch_hash and drop_hash fail when levels is zero
--
commit 1669fe0d8b13a61ae6909ea388b60b10f26a0954
Author: Yongpeng Yang <[email protected]>
dm-verity: add messages to inspect, prefetch and drop hash blocks
This commit adds target messages (retain_bytes, prefetch_hash, and drop_hash) to dm-verity to inspect or configure bufio retain targets and to prefetch or drop cached hash blocks, and documents dm-verity metadata caching and the new messages.
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 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));
...
}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.
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);
...
}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.
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 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.
[ ... ]
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 ; + }
[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.
drivers/md/dm-verity-target.c static void verity_resume(struct dm_target *ti) · line 1909
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 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)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.
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 1931
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;
...
}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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4