Re: [PATCH] ALSA: pcmtest: fix reference leak on failed device registration

Takashi Iwai
2026-04-17T10:52:53Z

On Fri, 17 Apr 2026 09:57:20 +0200,
Takashi Iwai wrote:
>
> On Wed, 15 Apr 2026 21:31:38 +0200,
> Guangshuo Li wrote:
> >
> > When platform_device_register() fails in mod_init(), the embedded struct
> > device in pcmtst_pdev has already been initialized by
> > device_initialize(), but the failure path returns the error without
> > dropping the device reference for the current platform device:
> >
> > mod_init()
> > -> platform_device_register(&pcmtst_pdev)
> > -> device_initialize(&pcmtst_pdev.dev)
> > -> setup_pdev_dma_masks(&pcmtst_pdev)
> > -> platform_device_add(&pcmtst_pdev)
> >
> > This leads to a reference leak when platform_device_register() fails.
> > Fix this by calling platform_device_put() before returning the error.
> >
> > The issue was identified by a static analysis tool I developed and
> > confirmed by manual review.
> >
> > Fixes: 315a3d57c64c5 ("ALSA: Implement the new Virtual PCM Test Driver")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
>
> Thanks, applied now.

... and now I looked through the whole tree, and noticed that the
majority of callers of platform_device_register() don't care the error
cases without calling platform_device_put(). There are over a hundred
callers of platform_device_register() while only 5 or so are doing the
proper cleanup at the error.

Judging from the numbers above, it might be better to change the
behavior of platform_device_register() itself to call *_put() at the
error case internally. Or, if we keep the current behavior, at least
we should properly document it.

Greg, Rafael, Danilo, what do you think?

thanks,

Takashi