From: Su Yue <l@damenly.org>
To: Guangshuo Li <lgs201920130244@gmail.com>
Cc: Song Liu <song@kernel.org>, Yu Kuai <yukuai@fnnas.com>,
Greg Kroah-Hartman <gregkh@suse.de>,
linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH v2] md: fix kobject reference leak in md_import_device()
Date: Tue, 14 Apr 2026 22:05:12 +0800 [thread overview]
Message-ID: <a4v58wo7.fsf@damenly.org> (raw)
In-Reply-To: <CANUHTR-G1X5OBXQNiw8-mXGiugnuP8ryHcsrMXLKcD4VefuKmw@mail.gmail.com> (Guangshuo Li's message of "Tue, 14 Apr 2026 19:32:07 +0800")
On Tue 14 Apr 2026 at 19:32, Guangshuo Li
<lgs201920130244@gmail.com> wrote:
> Hi Su,
>
> Thanks for reviewing.
>
> On Tue, 14 Apr 2026 at 09:29, Su Yue <l@damenly.org> wrote:
>> Why not just:
>>
>> out_blkdev_put:
>> kobject_put(&rdev->kobj);
>> fput(rdev->bdev_file);
>> out_clear_rdev:
>> md_rdev_clear(rdev);
>> out_free_rdev:
>> kfree(rdev);
>> return ERR_PTR(err);
>>
>> --
>> Su
>
> I wonder if that ordering might cause a problem.
>
> After kobject_init(&rdev->kobj, &rdev_ktype),
> kobject_put(&rdev->kobj)
> may immediately drop the last reference and run the release
> callback
> from rdev_ktype:
>
> static const struct kobj_type rdev_ktype = {
> .release = rdev_free,
> .sysfs_ops = &rdev_sysfs_ops,
> .default_groups = rdev_default_groups,
> };
>
> static void rdev_free(struct kobject *ko)
> {
> struct md_rdev *rdev = container_of(ko, struct md_rdev,
> kobj);
> kfree(rdev);
> }
>
> So in:
>
> out_blkdev_put:
> kobject_put(&rdev->kobj);
> fput(rdev->bdev_file);
>
> it seems possible that kobject_put() would already free rdev via
> rdev_free(), and then fput(rdev->bdev_file) would dereference
> rdev
> after free.
>
> That was why I changed it to:
>
> out_blkdev_put:
> fput(rdev->bdev_file);
> md_rdev_clear(rdev);
> kobject_put(&rdev->kobj);
> return ERR_PTR(err);
>
> so that the cleanup which still needs rdev is done before
> kobject_put(), and this path returns directly instead of falling
> through to the old kfree(rdev) path.
>
> Please let me know if I overlooked something.
>
Thanks for your detailed explanation. It's totally correct.
--
Su
> Thanks,
> Guangshuo
next prev parent reply other threads:[~2026-04-14 14:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-13 14:17 [PATCH v2] md: fix kobject reference leak in md_import_device() Guangshuo Li
2026-04-14 1:28 ` Su Yue
2026-04-14 11:32 ` Guangshuo Li
2026-04-14 14:05 ` Su Yue [this message]
2026-04-19 4:42 ` Yu Kuai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a4v58wo7.fsf@damenly.org \
--to=l@damenly.org \
--cc=gregkh@suse.de \
--cc=lgs201920130244@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-raid@vger.kernel.org \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=yukuai@fnnas.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.