Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Yang Yingliang <yangyingliang@huawei.com>
To: Bjorn Helgaas <helgaas@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <linux-pci@vger.kernel.org>,
	<bhelgaas@google.com>, Arnd Bergmann <arnd@arndb.de>,
	Rob Herring <robh@kernel.org>
Subject: Re: [PATCH -next 2/3] PCI: fix possible memory leak in error case in pci_register_host_bridge()
Date: Sat, 27 Aug 2022 17:29:35 +0800	[thread overview]
Message-ID: <83f42399-6dc3-d227-bb48-891172e061f9@huawei.com> (raw)
In-Reply-To: <20220826223810.GA2961041@bhelgaas>


On 2022/8/27 6:38, Bjorn Helgaas wrote:
> [+cc Arnd, Rob]
>
> On Thu, Aug 25, 2022 at 08:27:52PM +0800, Yang Yingliang wrote:
>> 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;
>> +	}
> Calling put_device(X) after device_register(X) returns failure doesn't
> need explanation because that's the standard pattern.  I think that
> was just missing before.
>
> In this error case, we previously did called put_device() for the
> *bridge* instead of the bus.  That was likely a typo and seems like
> the important thing here.
put_device() for the bridge will be called in the callback of put for 
the bus.
So it doesn't call put bridge device here.

Thanks,
Yang
>>   	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-27  9:29 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 ` [PATCH -next 2/3] PCI: fix possible memory leak in error case in pci_register_host_bridge() Yang Yingliang
2022-08-26 22:38   ` Bjorn Helgaas
2022-08-27  9:29     ` Yang Yingliang [this message]
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=83f42399-6dc3-d227-bb48-891172e061f9@huawei.com \
    --to=yangyingliang@huawei.com \
    --cc=arnd@arndb.de \
    --cc=bhelgaas@google.com \
    --cc=helgaas@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=robh@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