dm: release the sysfs kobject when its initialization fails
Subsystems (2)
PATCH 1/1 dm: release the sysfs kobject when its initialization fails
dm_sysfs_init() uses kobject_init_and_add(), which requires a matching put even when adding the object fails. dm_setup_md_queue() currently unwinds the disk and holder links without releasing this object. Since md->type is still DM_TYPE_NONE, cleanup_mapped_device() also skips the sysfs cleanup, leaving the initialized kobject behind.
Call dm_sysfs_exit() before unwinding a failed sysfs initialization. This both puts the kobject and waits for its release callback, including when CONFIG_DEBUG_KOBJECT_RELEASE delays that callback, before the mapped device can be freed or its queue setup retried.
--- drivers/md/dm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm.c +3 -1
@@ -2647,8 +2647,10 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)mutex_unlock (&md->table_devices_lock); r =dm_sysfs_init (md); -if (r) +if (r) { +dm_sysfs_exit (md); goto lock_out_undo_holders; + } md->type = type;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] dm: double free and kobject re-init on dm_setup_md_queue() retry --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dm: double free and kobject re-init on dm_setup_md_queue() retry --
commit ade936ffb3a2324034883b5668b49bcb6d404c6d
Author: Wentao Liang <[email protected]>
dm: release the sysfs kobject when its initialization fails
This commit calls dm_sysfs_exit() on the error path of dm_setup_md_queue() when dm_sysfs_init() fails, so that the initialized kobject is released and waited on before unwinding the disk and holder links.
drivers/md/dm.c int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) · line 2653
Quoted 13 lines
drivers/md/dm.c +3
-1@@ -2647,8 +2647,10 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)mutex_unlock (&md->table_devices_lock); r =dm_sysfs_init (md); -if (r) +if (r) { +dm_sysfs_exit (md); goto lock_out_undo_holders; + }
[Severity: High]
What happens if dm_setup_md_queue() is retried on the same mapped_device after dm_sysfs_init() fails and dm_sysfs_exit() runs?
In dm_sysfs_init(), kobject_init_and_add() calls kobject_init(), which sets kobj->state_initialized = 1, and kobject_set_name_vargs(), which sets kobj->name (heap-allocated via kstrdup() when dm-mod is built as a module, since the string literal "dm" is in module rodata rather than core kernel rodata).
If adding the kobject to sysfs fails, dm_sysfs_exit() calls kobject_put() and waits for completion:
lib/kobject.c:kobject_cleanup() {
...
/* free name if we allocated it */
if (name) {
pr_debug("'%s': free name\n", name);
kfree_const(name);
}
}kobject_cleanup() frees kobj->name via kfree_const(), but neither kobject_cleanup() nor dm_sysfs_exit() clears kobj->name or kobj->state_initialized.
Because dm_setup_md_queue() returns before setting md->type, md remains alive with md->type == DM_TYPE_NONE:
drivers/md/dm-ioctl.c:table_load() {
...
if (dm_get_md_type(md) == DM_TYPE_NONE) {
/* setup md->queue to reflect md's type (may block) */
r = dm_setup_md_queue(md, t);
if (r) {
DMERR("unable to set up device queue for new table.");
goto err_unlock_md_type;
}
}
}If userspace retries DM_TABLE_LOAD on the same mapped_device, table_load() calls dm_setup_md_queue() -> dm_sysfs_init() -> kobject_init_and_add() on the same embedded md->kobj_holder.kobj.
Won't kobject_init() complain that kobj->state_initialized is already 1 and dump a stack trace, followed by kobject_set_name_vargs() calling kfree_const(kobj->name) on the dangling pointer that was already freed during the first dm_sysfs_exit()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 11 lines · Wentao Liang
dm_sysfs_init()useskobject_init_and_add(), which requires a matching put even when adding the object fails.dm_setup_md_queue()currently unwinds the disk and holder links without releasing this object. Sincemd->typeis stillDM_TYPE_NONE,cleanup_mapped_device()also skips the sysfs cleanup, leaving the initialized kobject behind.Call
dm_sysfs_exit()before unwinding a failed sysfs initialization. This both puts the kobject and waits for its release callback, including whenCONFIG_DEBUG_KOBJECT_RELEASEdelays that callback, before the mapped device can be freed or its queue setup retried.
Quoted 8 lines
Fixes 784aae735d9b ("dm: add name and uuid to sysfs")Assisted-by DeepSeek-V4.1-Flash and OpenAI CodexSigned-off-by Wentao Liang <[email protected]>--- drivers/md/dm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Multiple things here:
1. Your team ignored completely previous feedback. Multiple times.
2. You use multiple identities with this email address, thus I doubt that we speak with an actual person.
3. Your bot does not even bother running checkpatch, right?
4. Nothing of this was reproduced nor tested.
5. Finally, the same feedback:
You sent multiple independent patches, to multiple independent subsystems. The amount of these patches clearly suggest this was AI generated and most likely not tested.
More importantly, you sent all this work without properly organizing relevant patches into patchsets. This makes reviewing difficult and might cause multiple reviewers to address the same issue. Replying to the entire set is impossible and requires handling each patch independently, instead of applying or discarding the set. Maintainers also won't see the bigger picture of your work. Quite worrying.
This is on the verge of hostile patch: bomb us with so many contributions, we won't be able to handle them in efficient manner, like responding ONCE to ask you to slow down. Considering all this is untested and LLM generated, I have even more doubts whether this should be considered for review.
Please read kernel documentation BEFORE posting more work. It will explain you how to identify subsystems, how to organize your work per subsystem (so a patchset grouping multiple patches with a short cover letter), how to document the issue discovery method and what was or was not tested.
Best regards,
Krzysztof
Mikulas Patocka
maintainerPATCH 1/1 dm-sysfs: fix misbehavior if kobject_init_and_add fails
In reply to Wentao LiangHi
The bug is real, but the patch is wrong. As Sashiko noted, kobject_init_and_add would see an uninitialized kboject if it were called the second time.
I created this patch for this bug.
Mikulas
dm-sysfs: fix misbehavior if kobject_init_and_add fails
If kobject_init_and_add, we must call kobject_put (see the comment at kobject_init_and_add).
Before calling kobject_init_and_add, we must zero the kobject (see
https://lwn.net/Articles/52085/). If the first call to dm_sysfs_init failed, it may be called again, and that second call would see an uninitialized kobject.
--- drivers/md/dm-sysfs.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/md/dm-sysfs.c +14 -3
@@ -128,9 +128,20 @@ static const struct kobj_type dm_ktype =*/ int dm_sysfs_init (struct mapped_device *md) { -return kobject_init_and_add (dm_kobject (md), &dm_ktype, - &disk_to_dev (dm_disk (md))->kobj, -"%s" ,"dm" ); +struct kobject *kobj =dm_kobject (md); +int r; + +/* The kobject must be zeroed before calling kobject_init */ +memset (kobj,0 ,sizeof (struct kobject )); + r =kobject_init_and_add (kobj, &dm_ktype, + &disk_to_dev (dm_disk (md))->kobj, +"%s" ,"dm" ); +if (unlikely (r)) { +/* Weird semantics - we must call kobject_put on failure */ +dm_sysfs_exit (md); +return r; + } +return 0 ; }/*
Quoted 38 lines · Wentao Liang
dm_sysfs_init()useskobject_init_and_add(), which requires a matching put even when adding the object fails.dm_setup_md_queue()currently unwinds the disk and holder links without releasing this object. Sincemd->typeis stillDM_TYPE_NONE,cleanup_mapped_device()also skips the sysfs cleanup, leaving the initialized kobject behind.Call
dm_sysfs_exit()before unwinding a failed sysfs initialization. This both puts the kobject and waits for its release callback, including whenCONFIG_DEBUG_KOBJECT_RELEASEdelays that callback, before the mapped device can be freed or its queue setup retried.Fixes 784aae735d9b ("dm: add name and uuid to sysfs")Assisted-by DeepSeek-V4.1-Flash and OpenAI CodexSigned-off-by Wentao Liang <[email protected]>--- drivers/md/dm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/md/dm.c +3
-1@@ -2647,8 +2647,10 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)mutex_unlock (&md->table_devices_lock); r =dm_sysfs_init (md); -if (r) +if (r) { +dm_sysfs_exit (md); goto lock_out_undo_holders; + } md->type = type;Signature · 1 line
2.34.1