public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] floppy: fix reference leak on platform_device_register() failure
@ 2026-04-15 14:57 Guangshuo Li
  2026-04-17 20:39 ` Jens Axboe
  0 siblings, 1 reply; 7+ messages in thread
From: Guangshuo Li @ 2026-04-15 14:57 UTC (permalink / raw)
  To: Denis Efremov, Jens Axboe, Greg Kroah-Hartman, linux-block,
	linux-kernel
  Cc: Guangshuo Li, stable

When platform_device_register() fails in do_floppy_init(), the embedded
struct device in floppy_device[drive] has already been initialized by
device_initialize(), but the failure path jumps to out_remove_drives
without dropping the device reference for the current drive.

Previously registered floppy devices are cleaned up in out_remove_drives,
but the device for the drive that fails registration is not, leading to
a reference leak.

The issue was identified by a static analysis tool I developed and
confirmed by manual review. Fix this by calling put_device() for the
current floppy device before jumping to the common cleanup path.

Fixes: 94fd0db7bfb4a ("[PATCH] Floppy: Add cmos attribute to floppy driver")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
v2:
  - Replace put_device() with platform_device_put() in the
    platform_device_register() failure path
  - Fix the device_add_disk() failure path by unregistering the current
    platform device before jumping to out_remove_drives


 drivers/block/floppy.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index 92e446a64371..461e14d19422 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -4724,15 +4724,19 @@ static int __init do_floppy_init(void)
 		floppy_device[drive].dev.groups = floppy_dev_groups;
 
 		err = platform_device_register(&floppy_device[drive]);
-		if (err)
+		if (err) {
+			platform_device_put(&floppy_device[drive]);
 			goto out_remove_drives;
-
+		}
 		registered[drive] = true;
 
 		err = device_add_disk(&floppy_device[drive].dev,
 				      disks[drive][0], NULL);
-		if (err)
+		if (err) {
+			platform_device_unregister(&floppy_device[drive]);
+			registered[drive] = false;
 			goto out_remove_drives;
+		}
 	}
 
 	return 0;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] floppy: fix reference leak on platform_device_register() failure
  2026-04-15 14:57 [PATCH v2] floppy: fix reference leak on platform_device_register() failure Guangshuo Li
@ 2026-04-17 20:39 ` Jens Axboe
  2026-04-23  5:11   ` Jiri Slaby
  0 siblings, 1 reply; 7+ messages in thread
From: Jens Axboe @ 2026-04-17 20:39 UTC (permalink / raw)
  To: Denis Efremov, Greg Kroah-Hartman, linux-block, linux-kernel,
	Guangshuo Li
  Cc: stable


On Wed, 15 Apr 2026 22:57:08 +0800, Guangshuo Li wrote:
> When platform_device_register() fails in do_floppy_init(), the embedded
> struct device in floppy_device[drive] has already been initialized by
> device_initialize(), but the failure path jumps to out_remove_drives
> without dropping the device reference for the current drive.
> 
> Previously registered floppy devices are cleaned up in out_remove_drives,
> but the device for the drive that fails registration is not, leading to
> a reference leak.
> 
> [...]

Applied, thanks!

[1/1] floppy: fix reference leak on platform_device_register() failure
      commit: e784f2ea0b4fd0e7b70028ff8218f22456c5dcf8

Best regards,
-- 
Jens Axboe




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] floppy: fix reference leak on platform_device_register() failure
  2026-04-17 20:39 ` Jens Axboe
@ 2026-04-23  5:11   ` Jiri Slaby
  2026-04-23 11:06     ` Jens Axboe
  0 siblings, 1 reply; 7+ messages in thread
From: Jiri Slaby @ 2026-04-23  5:11 UTC (permalink / raw)
  To: Jens Axboe, Denis Efremov, Greg Kroah-Hartman, linux-block,
	linux-kernel, Guangshuo Li
  Cc: stable

On 17. 04. 26, 22:39, Jens Axboe wrote:
> 
> On Wed, 15 Apr 2026 22:57:08 +0800, Guangshuo Li wrote:
>> When platform_device_register() fails in do_floppy_init(), the embedded
>> struct device in floppy_device[drive] has already been initialized by
>> device_initialize(), but the failure path jumps to out_remove_drives
>> without dropping the device reference for the current drive.
>>
>> Previously registered floppy devices are cleaned up in out_remove_drives,
>> but the device for the drive that fails registration is not, leading to
>> a reference leak.
>>
>> [...]
> 
> Applied, thanks!
> 
> [1/1] floppy: fix reference leak on platform_device_register() failure
>        commit: e784f2ea0b4fd0e7b70028ff8218f22456c5dcf8

The patch is likely wrong. Given the pdev is static, the struct device 
has no ->release, so releasing it will trigger a warning. AFAIR, the 
consensus was to fix platform_device_register() proper.

thanks,
-- 
js
suse labs


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] floppy: fix reference leak on platform_device_register() failure
  2026-04-23  5:11   ` Jiri Slaby
@ 2026-04-23 11:06     ` Jens Axboe
  2026-04-24  3:14       ` Guangshuo Li
  0 siblings, 1 reply; 7+ messages in thread
From: Jens Axboe @ 2026-04-23 11:06 UTC (permalink / raw)
  To: Jiri Slaby, Denis Efremov, Greg Kroah-Hartman, linux-block,
	linux-kernel, Guangshuo Li
  Cc: stable

On 4/22/26 11:11 PM, Jiri Slaby wrote:
> On 17. 04. 26, 22:39, Jens Axboe wrote:
>>
>> On Wed, 15 Apr 2026 22:57:08 +0800, Guangshuo Li wrote:
>>> When platform_device_register() fails in do_floppy_init(), the embedded
>>> struct device in floppy_device[drive] has already been initialized by
>>> device_initialize(), but the failure path jumps to out_remove_drives
>>> without dropping the device reference for the current drive.
>>>
>>> Previously registered floppy devices are cleaned up in out_remove_drives,
>>> but the device for the drive that fails registration is not, leading to
>>> a reference leak.
>>>
>>> [...]
>>
>> Applied, thanks!
>>
>> [1/1] floppy: fix reference leak on platform_device_register() failure
>>        commit: e784f2ea0b4fd0e7b70028ff8218f22456c5dcf8
> 
> The patch is likely wrong. Given the pdev is static, the struct device
> has no ->release, so releasing it will trigger a warning. AFAIR, the
> consensus was to fix platform_device_register() proper.

Thanks for letting me know, I'll revert this change for now.

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] floppy: fix reference leak on platform_device_register() failure
  2026-04-23 11:06     ` Jens Axboe
@ 2026-04-24  3:14       ` Guangshuo Li
  2026-04-24  4:15         ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Guangshuo Li @ 2026-04-24  3:14 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Jiri Slaby, Denis Efremov, Greg Kroah-Hartman, linux-block,
	linux-kernel, stable

Hi Jiri, Jens,

Thanks for the review and for catching this.

On Thu, 23 Apr 2026 at 19:06, Jens Axboe <axboe@kernel.dk> wrote:
>
> > The patch is likely wrong. Given the pdev is static, the struct device
> > has no ->release, so releasing it will trigger a warning. AFAIR, the
> > consensus was to fix platform_device_register() proper.
>
> Thanks for letting me know, I'll revert this change for now.
>

You are right that this fix is not appropriate for this case. Since the
platform device is static and the embedded struct device has no release
callback, calling platform_device_put() / releasing it here is wrong and
can trigger warnings.

Please disregard this patch. I will drop it and not pursue it further in
its current form.

Sorry for the noise, and thanks again for the clarification.

Best regards,
Guangshuo Li

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] floppy: fix reference leak on platform_device_register() failure
  2026-04-24  3:14       ` Guangshuo Li
@ 2026-04-24  4:15         ` Greg KH
  2026-04-24  7:40           ` Guangshuo Li
  0 siblings, 1 reply; 7+ messages in thread
From: Greg KH @ 2026-04-24  4:15 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Jens Axboe, Jiri Slaby, Denis Efremov, Greg Kroah-Hartman,
	linux-block, linux-kernel, stable

On Fri, Apr 24, 2026 at 11:14:17AM +0800, Guangshuo Li wrote:
> Hi Jiri, Jens,
> 
> Thanks for the review and for catching this.
> 
> On Thu, 23 Apr 2026 at 19:06, Jens Axboe <axboe@kernel.dk> wrote:
> >
> > > The patch is likely wrong. Given the pdev is static, the struct device
> > > has no ->release, so releasing it will trigger a warning. AFAIR, the
> > > consensus was to fix platform_device_register() proper.
> >
> > Thanks for letting me know, I'll revert this change for now.
> >
> 
> You are right that this fix is not appropriate for this case. Since the
> platform device is static and the embedded struct device has no release
> callback, calling platform_device_put() / releasing it here is wrong and
> can trigger warnings.
> 
> Please disregard this patch. I will drop it and not pursue it further in
> its current form.

Can you go back and verify that all of the other patches you sent out
for this same pattern are also either ignored, or reverted?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] floppy: fix reference leak on platform_device_register() failure
  2026-04-24  4:15         ` Greg KH
@ 2026-04-24  7:40           ` Guangshuo Li
  0 siblings, 0 replies; 7+ messages in thread
From: Guangshuo Li @ 2026-04-24  7:40 UTC (permalink / raw)
  To: Greg KH
  Cc: Jens Axboe, Jiri Slaby, Denis Efremov, Greg Kroah-Hartman,
	linux-block, linux-kernel, stable

Hi Greg,

Thanks for reviewing and pointing this out.

On Fri, 24 Apr 2026 at 12:15, Greg KH <gregkh@linuxfoundation.org> wrote:
>
>
> Can you go back and verify that all of the other patches you sent out
> for this same pattern are also either ignored, or reverted?
>
> thanks,
>
> greg k-h

Yes, I will go back and check all the other patches I sent for this same
pattern. For any cases involving static platform devices or similar static
objects where this fix is not appropriate, I will follow up by email to
make sure they are either ignored or reverted as needed.

Thanks again for the guidance.

Best regards,
Guangshuo Li

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-04-24  7:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-15 14:57 [PATCH v2] floppy: fix reference leak on platform_device_register() failure Guangshuo Li
2026-04-17 20:39 ` Jens Axboe
2026-04-23  5:11   ` Jiri Slaby
2026-04-23 11:06     ` Jens Axboe
2026-04-24  3:14       ` Guangshuo Li
2026-04-24  4:15         ` Greg KH
2026-04-24  7:40           ` Guangshuo Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox