Subject: Re: [PATCH 2/4] dm-bufio: allow exposing a client in sysfs under a caller-chosen kobject In-Reply-To: <[email protected]>
On Tue, 17 Nov 2026 at 01:40:55PM +0000, [email protected] wrote: > > diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c > > index 75b6e5c027ae2..627a64f981c5c 100644 > > --- a/drivers/md/dm-bufio.c > > +++ b/drivers/md/dm-bufio.c > > [ ... ] > > > @@ -2504,6 +2510,144 @@ static unsigned long dm_bufio_shrink_count(struct > > shrinker *shrink, struct shrin > > [ ... ] > > > +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); > > +} > [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 CPU1 > dm_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. 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. > > + > > +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); > [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(). 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; } > > @@ -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. 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
