mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 RESEND] PCI: fix reference leak in pci_alloc_child_bus()
@ 2025-01-19  7:05 Ma Ke
  2025-01-24  9:19 ` Ilpo Järvinen
  0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2025-01-19  7:05 UTC (permalink / raw)
  To: bhelgaas, yinghai, rafael.j.wysocki, akpm
  Cc: linux-pci, linux-kernel, Ma Ke, stable

When device_register(&child->dev) failed, calling put_device() to
explicitly release child->dev. Otherwise, it could cause double free
problem.

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: 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
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..ae12f92c6a9d 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] 2+ messages in thread

* Re: [PATCH v2 RESEND] PCI: fix reference leak in pci_alloc_child_bus()
  2025-01-19  7:05 [PATCH v2 RESEND] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
@ 2025-01-24  9:19 ` Ilpo Järvinen
  0 siblings, 0 replies; 2+ messages in thread
From: Ilpo Järvinen @ 2025-01-24  9:19 UTC (permalink / raw)
  To: Ma Ke; +Cc: bhelgaas, yinghai, rafael.j.wysocki, akpm, linux-pci, LKML, stable

On Sun, 19 Jan 2025, Ma Ke wrote:

> When device_register(&child->dev) failed, calling put_device() to
> explicitly release child->dev. Otherwise, it could cause double free
> problem.

While the code fix seem okay, this double free part in the problem 
description isn't. The reference is held w/o this fix so it's not getting 
freed at all, let alone freed twice.

> 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: 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> 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..ae12f92c6a9d 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);
>  
> 

-- 
 i.


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

end of thread, other threads:[~2025-01-24  9:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-19  7:05 [PATCH v2 RESEND] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
2025-01-24  9:19 ` Ilpo Järvinen

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®