From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Mark Rutland <mark.rutland@arm.com>
Cc: Guangshuo Li <lgs201920130244@gmail.com>,
Will Deacon <will@kernel.org>,
Anshuman Khandual <anshuman.khandual@arm.com>,
linux-arm-kernel@lists.infradead.org,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] arm_pmu: acpi: fix reference leak on failed device registration
Date: Thu, 16 Apr 2026 06:40:55 +0200 [thread overview]
Message-ID: <2026041603-guts-crested-ef76@gregkh> (raw)
In-Reply-To: <ad_WmuauLJ3xDKqh@J2N7QTR9R3>
On Wed, Apr 15, 2026 at 07:19:06PM +0100, Mark Rutland wrote:
> Hi,
>
> Thanks for the patch, but from a quick skim, I don't think this is the right
> fix.
>
> Greg, I think we might want to rework the core API here; question for
> you at the end.
>
> On Thu, Apr 16, 2026 at 01:41:59AM +0800, Guangshuo Li wrote:
> > When platform_device_register() fails in arm_acpi_register_pmu_device(),
> > the embedded struct device in pdev has already been initialized by
> > device_initialize(), but the failure path only unregisters the GSI and
> > does not drop the device reference for the current platform device:
> >
> > arm_acpi_register_pmu_device()
> > -> platform_device_register(pdev)
> > -> device_initialize(&pdev->dev)
> > -> setup_pdev_dma_masks(pdev)
> > -> platform_device_add(pdev)
> >
> > This leads to a reference leak when platform_device_register() fails.
>
> AFAICT you're saying that the reference was taken *within*
> platform_device_register(), and then platform_device_register() itself
> has failed. I think it's surprising that platform_device_register()
> doesn't clean that up itself in the case of an error.
>
> There are *tonnes* of calls to platform_device_register() throughout the
> kernel that don't even bother to check the return value, and many that
> just pass the return onto a caller that can't possibly know to call
> platform_device_put().
>
> Code in the same file as platform_device_register() expects it to clean up
> after itself, e.g.
>
> | int platform_add_devices(struct platform_device **devs, int num)
> | {
> | int i, ret = 0;
> |
> | for (i = 0; i < num; i++) {
> | ret = platform_device_register(devs[i]);
> | if (ret) {
> | while (--i >= 0)
> | platform_device_unregister(devs[i]);
> | break;
> | }
> | }
> |
> | return ret;
> | }
>
> That's been there since the initial git commit, and back then,
> platform_device_register() didn't mention that callers needed to perform
> any cleanup.
>
> I see a comment was added to platform_device_register() in commit:
>
> 67e532a42cf4 ("driver core: platform: document registration-failure requirement")
>
> ... and that copied the commend added for device_register() in commit:
>
> 5739411acbaa ("Driver core: Clarify device cleanup.")
>
> ... but the potential brokenness is so widespread, and the behaviour is
> so surprising, that I'd argue the real but is that device_register()
> doesn't clean up in case of error. I don't think it's worth changing
> this single instance given the prevalance and churn fixing all of that
> would involve.
>
> I think it would be far better to fix the core driver API such that when
> those functions return an error, they've already cleaned up for
> themselves.
>
> Greg, am I missing some functional reason why we can't rework
> device_register() and friends to handle cleanup themselves? I appreciate
> that'll involve churn for some callers, but AFAICT the majority of
> callers don't have the required cleanup.
Yes, we should fix the platform core code here, this should not be
required to do everywhere as obviously we all got it wrong.
Guangshuo, can you submit a patch to do that instead and ask for all of
your other patches to not be applied as well?
thanks,
greg k-h
next prev parent reply other threads:[~2026-04-16 4:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-15 17:41 [PATCH] arm_pmu: acpi: fix reference leak on failed device registration Guangshuo Li
2026-04-15 18:19 ` Mark Rutland
2026-04-16 4:40 ` Greg Kroah-Hartman [this message]
2026-04-16 6:34 ` Guangshuo Li
2026-04-16 7:23 ` Johan Hovold
2026-04-16 8:59 ` Guangshuo Li
2026-04-16 9:50 ` Mark Rutland
2026-04-16 9:30 ` Mark Rutland
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=2026041603-guts-crested-ef76@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=anshuman.khandual@arm.com \
--cc=lgs201920130244@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=stable@vger.kernel.org \
--cc=will@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox