public inbox for linux-pci@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] PCI: fix reference leak in pci_alloc_child_bus()
@ 2025-02-02  6:23 Ma Ke
  2025-02-04  8:45 ` Ilpo Järvinen
  2025-02-27 22:04 ` Bjorn Helgaas
  0 siblings, 2 replies; 3+ messages in thread
From: Ma Ke @ 2025-02-02  6:23 UTC (permalink / raw)
  To: bhelgaas, rafael.j.wysocki, yinghai
  Cc: linux-pci, linux-kernel, Ma Ke, stable

When device_register(&child->dev) failed, we should call put_device()
to explicitly release child->dev.

As comment of device_register() says, 'NOTE: _Never_ directly free
@dev after calling this function, even if it returned an error! Always
use put_device() to give up the reference initialized in this function
instead.'

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
Changes in v3:
- modified the description as suggestions.
Changes in v2:
- added the bug description about the comment of device_add();
- fixed the patch as suggestions;
- added Cc and Fixes table.
---
 drivers/pci/probe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 2e81ab0f5a25..51b78fcda4eb 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
 add_dev:
 	pci_set_bus_msi_domain(child);
 	ret = device_register(&child->dev);
-	WARN_ON(ret < 0);
+	if (WARN_ON(ret < 0)) {
+		put_device(&child->dev);
+		return NULL;
+	}
 
 	pcibios_add_bus(child);
 
-- 
2.25.1


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

* Re: [PATCH v3] PCI: fix reference leak in pci_alloc_child_bus()
  2025-02-02  6:23 [PATCH v3] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
@ 2025-02-04  8:45 ` Ilpo Järvinen
  2025-02-27 22:04 ` Bjorn Helgaas
  1 sibling, 0 replies; 3+ messages in thread
From: Ilpo Järvinen @ 2025-02-04  8:45 UTC (permalink / raw)
  To: Ma Ke; +Cc: bhelgaas, rafael.j.wysocki, yinghai, linux-pci, LKML, stable

[-- Attachment #1: Type: text/plain, Size: 1690 bytes --]

On Sun, 2 Feb 2025, Ma Ke wrote:

> When device_register(&child->dev) failed, we should call put_device()
> to explicitly release child->dev.
> 
> As comment of device_register() says, 'NOTE: _Never_ directly free
> @dev after calling this function, even if it returned an error! Always
> use put_device() to give up the reference initialized in this function
> instead.'
> 
> Found by code review.
> 
> Cc: stable@vger.kernel.org
> Fixes: 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> Changes in v3:
> - modified the description as suggestions.
> Changes in v2:
> - added the bug description about the comment of device_add();
> - fixed the patch as suggestions;
> - added Cc and Fixes table.
> ---
>  drivers/pci/probe.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 2e81ab0f5a25..51b78fcda4eb 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
>  add_dev:
>  	pci_set_bus_msi_domain(child);
>  	ret = device_register(&child->dev);
> -	WARN_ON(ret < 0);
> +	if (WARN_ON(ret < 0)) {
> +		put_device(&child->dev);
> +		return NULL;
> +	}
>  
>  	pcibios_add_bus(child);

Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

Unrelated to this fix, IMO that WARN_ON() is overkill and I'm skeptical 
that printing a stack trace on a failure in device_register() is helpful.
IMO, a simple error print would suffice to tell something (unexpectedly)
went wrong here.

-- 
 i.

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

* Re: [PATCH v3] PCI: fix reference leak in pci_alloc_child_bus()
  2025-02-02  6:23 [PATCH v3] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
  2025-02-04  8:45 ` Ilpo Järvinen
@ 2025-02-27 22:04 ` Bjorn Helgaas
  1 sibling, 0 replies; 3+ messages in thread
From: Bjorn Helgaas @ 2025-02-27 22:04 UTC (permalink / raw)
  To: Ma Ke; +Cc: bhelgaas, rafael.j.wysocki, yinghai, linux-pci, linux-kernel,
	stable

On Sun, Feb 02, 2025 at 02:23:57PM +0800, Ma Ke wrote:
> When device_register(&child->dev) failed, we should call put_device()
> to explicitly release child->dev.
> 
> As comment of device_register() says, 'NOTE: _Never_ directly free
> @dev after calling this function, even if it returned an error! Always
> use put_device() to give up the reference initialized in this function
> instead.'
> 
> Found by code review.
> 
> Cc: stable@vger.kernel.org
> Fixes: 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>

Applied with Ilpo's reviewed-by to pci/enumeration for v6.15, thanks!

> ---
> Changes in v3:
> - modified the description as suggestions.
> Changes in v2:
> - added the bug description about the comment of device_add();
> - fixed the patch as suggestions;
> - added Cc and Fixes table.
> ---
>  drivers/pci/probe.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 2e81ab0f5a25..51b78fcda4eb 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
>  add_dev:
>  	pci_set_bus_msi_domain(child);
>  	ret = device_register(&child->dev);
> -	WARN_ON(ret < 0);
> +	if (WARN_ON(ret < 0)) {
> +		put_device(&child->dev);
> +		return NULL;
> +	}
>  
>  	pcibios_add_bus(child);
>  
> -- 
> 2.25.1
> 

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

end of thread, other threads:[~2025-02-27 22:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-02  6:23 [PATCH v3] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
2025-02-04  8:45 ` Ilpo Järvinen
2025-02-27 22:04 ` Bjorn Helgaas

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