[PATCH 1/3] PCI/pwrctrl: Fix device leak at registration

Johan Hovold posted 3 patches 2 months, 2 weeks ago
[PATCH 1/3] PCI/pwrctrl: Fix device leak at registration
Posted by Johan Hovold 2 months, 2 weeks ago
Make sure to drop the reference to the pwrctrl device taken by
of_find_device_by_node() when registering a PCI device.

Fixes: b458ff7e8176 ("PCI/pwrctl: Ensure that pwrctl drivers are probed before PCI client drivers")
Cc: stable@vger.kernel.org	# 6.13
Cc: Manivannan Sadhasivam <mani@kernel.org>
Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
---
 drivers/pci/bus.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 69048869ef1c..0394a9c77b38 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -362,11 +362,15 @@ void pci_bus_add_device(struct pci_dev *dev)
 	 * before PCI client drivers.
 	 */
 	pdev = of_find_device_by_node(dn);
-	if (pdev && of_pci_supply_present(dn)) {
-		if (!device_link_add(&dev->dev, &pdev->dev,
-				     DL_FLAG_AUTOREMOVE_CONSUMER))
-			pci_err(dev, "failed to add device link to power control device %s\n",
-				pdev->name);
+	if (pdev) {
+		if (of_pci_supply_present(dn)) {
+			if (!device_link_add(&dev->dev, &pdev->dev,
+					     DL_FLAG_AUTOREMOVE_CONSUMER)) {
+				pci_err(dev, "failed to add device link to power control device %s\n",
+					pdev->name);
+			}
+		}
+		put_device(&pdev->dev);
 	}
 
 	if (!dn || of_device_is_available(dn))
-- 
2.49.1
Re: [PATCH 1/3] PCI/pwrctrl: Fix device leak at registration
Posted by Markus Elfring 2 months, 2 weeks ago
…
> +++ b/drivers/pci/bus.c
> @@ -362,11 +362,15 @@ void pci_bus_add_device(struct pci_dev *dev)
…
> +		if (of_pci_supply_present(dn)) {
> +			if (!device_link_add(&dev->dev, &pdev->dev,
> +					     DL_FLAG_AUTOREMOVE_CONSUMER)) {
> +				pci_err(dev, "failed to add device link to power control device %s\n",
> +					pdev->name);
> +			}
> +		}
…

How do you think about to reconsider the usage of any curly brackets
once more?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.16-rc7#n197

Regards,
Markus
Re: [PATCH 1/3] PCI/pwrctrl: Fix device leak at registration
Posted by Jonathan Cameron 2 months, 2 weeks ago
On Mon, 21 Jul 2025 17:36:07 +0200
Johan Hovold <johan+linaro@kernel.org> wrote:

> Make sure to drop the reference to the pwrctrl device taken by
> of_find_device_by_node() when registering a PCI device.
> 
> Fixes: b458ff7e8176 ("PCI/pwrctl: Ensure that pwrctl drivers are probed before PCI client drivers")
> Cc: stable@vger.kernel.org	# 6.13
> Cc: Manivannan Sadhasivam <mani@kernel.org>
> Signed-off-by: Johan Hovold <johan+linaro@kernel.org>

Hi Johan,

Perhaps time for 
DEFINE_FREE(put_pdev, struct platform_device *, if (_T) put_device(&_T->dev));

then...

> ---
>  drivers/pci/bus.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
> index 69048869ef1c..0394a9c77b38 100644
> --- a/drivers/pci/bus.c
> +++ b/drivers/pci/bus.c
> @@ -362,11 +362,15 @@ void pci_bus_add_device(struct pci_dev *dev)
>  	 * before PCI client drivers.
>  	 */
>  	pdev = of_find_device_by_node(dn);
> -	if (pdev && of_pci_supply_present(dn)) {
> -		if (!device_link_add(&dev->dev, &pdev->dev,
> -				     DL_FLAG_AUTOREMOVE_CONSUMER))
> -			pci_err(dev, "failed to add device link to power control device %s\n",
> -				pdev->name);

	struct platform_device *pdev __free(put_pdev) =
		of_find_device_by_node(dn);
> +	if (pdev) {
> +		if (of_pci_supply_present(dn)) {
> +			if (!device_link_add(&dev->dev, &pdev->dev,
> +					     DL_FLAG_AUTOREMOVE_CONSUMER)) {
> +				pci_err(dev, "failed to add device link to power control device %s\n",
> +					pdev->name);
> +			}
> +		}
> +		put_device(&pdev->dev);

and no need for any explicit put.

We already do this extensively in some subsystems (e.g. CXL) and it
greatly simplifies code.

>  	}
>  
>  	if (!dn || of_device_is_available(dn))
Re: [PATCH 1/3] PCI/pwrctrl: Fix device leak at registration
Posted by Johan Hovold 2 months, 2 weeks ago
On Tue, Jul 22, 2025 at 11:05:26AM +0100, Jonathan Cameron wrote:
> On Mon, 21 Jul 2025 17:36:07 +0200
> Johan Hovold <johan+linaro@kernel.org> wrote:

> Perhaps time for 
> DEFINE_FREE(put_pdev, struct platform_device *, if (_T) put_device(&_T->dev));
> 
> then...
> 
> > ---
> >  drivers/pci/bus.c | 14 +++++++++-----
> >  1 file changed, 9 insertions(+), 5 deletions(-)
> > 
> > diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
> > index 69048869ef1c..0394a9c77b38 100644
> > --- a/drivers/pci/bus.c
> > +++ b/drivers/pci/bus.c
> > @@ -362,11 +362,15 @@ void pci_bus_add_device(struct pci_dev *dev)
> >  	 * before PCI client drivers.
> >  	 */
> >  	pdev = of_find_device_by_node(dn);
> > -	if (pdev && of_pci_supply_present(dn)) {
> > -		if (!device_link_add(&dev->dev, &pdev->dev,
> > -				     DL_FLAG_AUTOREMOVE_CONSUMER))
> > -			pci_err(dev, "failed to add device link to power control device %s\n",
> > -				pdev->name);
> 
> 	struct platform_device *pdev __free(put_pdev) =
> 		of_find_device_by_node(dn);
> > +	if (pdev) {
> > +		if (of_pci_supply_present(dn)) {
> > +			if (!device_link_add(&dev->dev, &pdev->dev,
> > +					     DL_FLAG_AUTOREMOVE_CONSUMER)) {
> > +				pci_err(dev, "failed to add device link to power control device %s\n",
> > +					pdev->name);
> > +			}
> > +		}
> > +		put_device(&pdev->dev);
> 
> and no need for any explicit put.
> 
> We already do this extensively in some subsystems (e.g. CXL) and it
> greatly simplifies code.

No, I'm no fan of those kind of changes which I find leads to less
readable code (e.g. with those in-code declarations).

Johan