mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] PCI: fix reference leak in pci_register_host_bridge()
@ 2025-01-05 10:59 Ma Ke
  2025-01-05 20:26 ` Markus Elfring
  0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2025-01-05 10:59 UTC (permalink / raw)
  To: bhelgaas, arnd, treding; +Cc: linux-pci, linux-kernel, Ma Ke, stable

Once device_register() failed, we should call put_device() to
decrement reference count for cleanup. Or it could cause memory leak.

device_register() includes device_add(). As comment of device_add()
says, 'if device_add() succeeds, you should call device_del() when you
want to get rid of it. If device_add() has not succeeded, use only
put_device() to drop the reference count'.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 37d6a0a6f470 ("PCI: Add pci_register_host_bridge() interface")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 drivers/pci/probe.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 2e81ab0f5a25..73464ffbb0fc 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -974,8 +974,10 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 	name = dev_name(&bus->dev);
 
 	err = device_register(&bus->dev);
-	if (err)
+	if (err) {
+		put_device(&bus->dev);
 		goto unregister;
+	}
 
 	pcibios_add_bus(bus);
 
-- 
2.25.1


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

* Re: [PATCH] PCI: fix reference leak in pci_register_host_bridge()
  2025-01-05 10:59 [PATCH] PCI: fix reference leak in pci_register_host_bridge() Ma Ke
@ 2025-01-05 20:26 ` Markus Elfring
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Elfring @ 2025-01-05 20:26 UTC (permalink / raw)
  To: make24, linux-pci, Arnd Bergmann, Bjorn Helgaas, Thierry Reding
  Cc: stable, LKML, kernel-janitors

> Once device_register() failed, we should call put_device() to
> decrement reference count for cleanup. Or it could cause memory leak.
>
> device_register() includes device_add(). As comment of device_add()
…

I doubt that the information from the second description paragraph is helpful
for this issue.


> Found by code review.

Would you become interested to check how many similar control flows
can still be detected by the means of automated advanced source code analyses?

Regards,
Markus

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

end of thread, other threads:[~2025-01-05 20:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-05 10:59 [PATCH] PCI: fix reference leak in pci_register_host_bridge() Ma Ke
2025-01-05 20:26 ` Markus Elfring

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

all inboxes | Powered by JetHome®