linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yang Yingliang <yangyingliang@huawei.com>
To: <linux-pci@vger.kernel.org>
Cc: <bhelgaas@google.com>, <yangyingliang@huawei.com>
Subject: [PATCH] PCI: fix possible memory leak in error case in pci_register_host_bridge()
Date: Tue, 18 Oct 2022 14:17:46 +0800	[thread overview]
Message-ID: <20221018061746.2392717-1-yangyingliang@huawei.com> (raw)

If device_register() fails in pci_register_host_bridge(), the refcount
of bus device is leaked, so device name that set by dev_set_name() can
not be freed. Fix this by calling put_device() when device_register()
fails, so the device name will be freed in kobject_cleanup().

The 'bus' is freed in release_pcibus_dev(), so set it to NULL to avoid
double free.

The refcount of bridge device is decreased in release_pcibus_dev(), so
remove the put_device() in error path and change error label name.

Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/pci/probe.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index f61754d76aa7..03b926d7c7ec 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -946,8 +946,11 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 	name = dev_name(&bus->dev);
 
 	err = device_register(&bus->dev);
-	if (err)
-		goto unregister;
+	if (err) {
+		put_device(&bus->dev);
+		bus = NULL;
+		goto del_device;
+	}
 
 	pcibios_add_bus(bus);
 
@@ -1023,8 +1026,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 
 	return 0;
 
-unregister:
-	put_device(&bridge->dev);
+del_device:
 	device_del(&bridge->dev);
 
 free:
-- 
2.25.1


                 reply	other threads:[~2022-10-18  6:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221018061746.2392717-1-yangyingliang@huawei.com \
    --to=yangyingliang@huawei.com \
    --cc=bhelgaas@google.com \
    --cc=linux-pci@vger.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;
as well as URLs for NNTP newsgroup(s).