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.

> 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.

> +
> +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().

> @@ -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.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to