public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI: fix reference leak in pci_alloc_child_bus()
@ 2024-12-17  3:54 Ma Ke
  2024-12-18 15:12 ` Ilpo Järvinen
  0 siblings, 1 reply; 3+ messages in thread
From: Ma Ke @ 2024-12-17  3:54 UTC (permalink / raw)
  To: bhelgaas, rafael.j.wysocki, yinghai
  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.

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 <make_ruc2021@163.com>
---
 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..d3146c588d7f 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 (ret) {
+		WARN_ON(ret < 0);
+		put_device(&child->dev);
+	}
 
 	pcibios_add_bus(child);
 
-- 
2.25.1


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

* Re: [PATCH] PCI: fix reference leak in pci_alloc_child_bus()
  2024-12-17  3:54 [PATCH] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
@ 2024-12-18 15:12 ` Ilpo Järvinen
  2024-12-19  1:39   ` Ma Ke
  0 siblings, 1 reply; 3+ messages in thread
From: Ilpo Järvinen @ 2024-12-18 15:12 UTC (permalink / raw)
  To: Ma Ke, Lukas Wunner, bhelgaas
  Cc: rafael.j.wysocki, yinghai, linux-pci, LKML, stable

On Tue, 17 Dec 2024, Ma Ke wrote:

> When device_register(&child->dev) failed, calling put_device() to
> explicitly release child->dev. Otherwise, it could cause double free
> problem.
> 
> 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 <make_ruc2021@163.com>
> ---
>  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..d3146c588d7f 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 (ret) {
> +		WARN_ON(ret < 0);

The usual way is:

	if (WARN_ON(ret < 0))

> +		put_device(&child->dev);
> +	}
>  
>  	pcibios_add_bus(child);

But more serious problem here is that should this code even proceed as if 
nothing happened when an error occurs? pci_register_host_bridge() does 
proper rollback when device_register() fails but this function doesn't.

Into the same vein, is using WARN_ON() even correct here? Why should this 
print a stacktrace if device_register() fails instead of simply printing 
and error?

-- 
 i.


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

* Re: [PATCH] PCI: fix reference leak in pci_alloc_child_bus()
  2024-12-18 15:12 ` Ilpo Järvinen
@ 2024-12-19  1:39   ` Ma Ke
  0 siblings, 0 replies; 3+ messages in thread
From: Ma Ke @ 2024-12-19  1:39 UTC (permalink / raw)
  To: ilpo.jarvinen
  Cc: bhelgaas, linux-kernel, linux-pci, lukas, make_ruc2021,
	rafael.j.wysocki, stable, yinghai

Ilpo Järvinen<ilpo.jarvinen@linux.intel.com> wrote:
> Ma Ke <make_ruc2021@163.com> writes:
> > When device_register(&child->dev) failed, calling put_device() to
> > explicitly release child->dev. Otherwise, it could cause double free
> > problem.
> > 
> > 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 <make_ruc2021@163.com>
> > ---
> >  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..d3146c588d7f 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 (ret) {
> > +		WARN_ON(ret < 0);
> 
> The usual way is:
> 
> 	if (WARN_ON(ret < 0))
> 
> > +		put_device(&child->dev);
> > +	}
> >  
> >  	pcibios_add_bus(child);
> 
> But more serious problem here is that should this code even proceed as if 
> nothing happened when an error occurs? pci_register_host_bridge() does 
> proper rollback when device_register() fails but this function doesn't.
> 
> Into the same vein, is using WARN_ON() even correct here? Why should this 
> print a stacktrace if device_register() fails instead of simply printing 
> and error?
Thank you for your guidance and suggestions. I have the same confusion
about the simple handling(WARN_ON()) of errors if device_add() fails. 
I am looking forward to receiving guidance and insights from other 
experts.

--
Regards,

Ma Ke


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

end of thread, other threads:[~2024-12-19  1:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-17  3:54 [PATCH] PCI: fix reference leak in pci_alloc_child_bus() Ma Ke
2024-12-18 15:12 ` Ilpo Järvinen
2024-12-19  1:39   ` Ma Ke

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