Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Yang Yingliang <yangyingliang@huawei.com>
To: <linux-kernel@vger.kernel.org>, <linux-pci@vger.kernel.org>
Cc: <bhelgaas@google.com>
Subject: [PATCH -next 2/3] PCI: fix possible memory leak in error case in pci_register_host_bridge()
Date: Thu, 25 Aug 2022 20:27:52 +0800	[thread overview]
Message-ID: <20220825122753.1838930-2-yangyingliang@huawei.com> (raw)
In-Reply-To: <20220825122753.1838930-1-yangyingliang@huawei.com>

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().

Fixes: 37d6a0a6f470 ("PCI: Add pci_register_host_bridge() interface")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/pci/probe.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index e500eb9d6468..292d9da146ce 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -948,8 +948,17 @@ 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) {
+		/*
+		 * release_pcibus_dev() will decrease the refcount of bridge
+		 * device and free the memory of bus.
+		 * The memory of bus device name will be freed when the refcount
+		 * get to zero.
+		 */
+		put_device(&bus->dev);
+		device_unregister(&bridge->dev);
+		return err;
+	}
 
 	pcibios_add_bus(bus);
 
@@ -1025,10 +1034,6 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 
 	return 0;
 
-unregister:
-	put_device(&bridge->dev);
-	device_unregister(&bridge->dev);
-
 free:
 	kfree(bus);
 	return err;
-- 
2.25.1


  reply	other threads:[~2022-08-25 12:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-25 12:27 [PATCH -next 1/3] PCI: fix double put_device() in error case in pci_create_root_bus() Yang Yingliang
2022-08-25 12:27 ` Yang Yingliang [this message]
2022-08-26 22:38   ` [PATCH -next 2/3] PCI: fix possible memory leak in error case in pci_register_host_bridge() Bjorn Helgaas
2022-08-27  9:29     ` Yang Yingliang
2022-08-25 12:27 ` [PATCH -next 3/3] PCI: fix handle error case in pci_alloc_child_bus() Yang Yingliang
2022-08-26 21:14 ` [PATCH -next 1/3] PCI: fix double put_device() in error case in pci_create_root_bus() Bjorn Helgaas
2022-08-27  9:15   ` Yang Yingliang

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=20220825122753.1838930-2-yangyingliang@huawei.com \
    --to=yangyingliang@huawei.com \
    --cc=bhelgaas@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --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