Re: [PATCH -next] usb: dwc2: Fix the error handling of dwc2_pci_probe()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2/7/2017 7:54 PM, Wei Yongjun wrote:
> From: Wei Yongjun <weiyongjun1@xxxxxxxxxx>
>
> Fix the error handling of dwc2_pci_probe() to avoid resources leak.
>
> Signed-off-by: Wei Yongjun <weiyongjun1@xxxxxxxxxx>
> ---
>  drivers/usb/dwc2/pci.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/usb/dwc2/pci.c b/drivers/usb/dwc2/pci.c
> index fdeb8c7..58fc1bf 100644
> --- a/drivers/usb/dwc2/pci.c
> +++ b/drivers/usb/dwc2/pci.c
> @@ -124,7 +124,7 @@ static int dwc2_pci_probe(struct pci_dev *pci,
>  	ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
>  	if (ret) {
>  		dev_err(dev, "couldn't add resources to dwc2 device\n");
> -		return ret;
> +		goto err_device_put;
>  	}
>
>  	dwc2->dev.parent = dev;
> @@ -133,30 +133,37 @@ static int dwc2_pci_probe(struct pci_dev *pci,
>  	if (IS_ERR(phy)) {
>  		dev_err(dev, "error registering generic PHY (%ld)\n",
>  			PTR_ERR(phy));
> -		return PTR_ERR(phy);
> +		ret = PTR_ERR(phy);
> +		goto err_device_put;
>  	}
>
>  	ret = dwc2_pci_quirks(pci, dwc2);
>  	if (ret)
> -		goto err;
> +		goto err_unregister;
>
>  	ret = platform_device_add(dwc2);
>  	if (ret) {
>  		dev_err(dev, "failed to register dwc2 device\n");
> -		goto err;
> +		goto err_unregister;
>  	}
>
>  	glue = kzalloc(sizeof(*glue), GFP_KERNEL);
> -	if (!glue)
> -		return -ENOMEM;
> +	if (!glue) {
> +		ret = -ENOMEM;
> +		goto err_device_del;
> +	}
>
>  	glue->phy = phy;
>  	glue->dwc2 = dwc2;
>  	pci_set_drvdata(pci, glue);
>
>  	return 0;
> -err:
> +
> +err_device_del:
> +	platform_device_del(dwc2);
> +err_unregister:
>  	usb_phy_generic_unregister(phy);
> +err_device_put:
>  	platform_device_put(dwc2);
>  	return ret;
>  }
>
> --

Hi Wei Yongjun,

We have patch doing the same in our internal branch, please review it 
below. We will send it out after internal review/testing process.

usb: dwc2: pci: Fix error handling in dwc2_pci_probe

Move usb_phy_generic_register() function call to the top, to simplify
error handling.

Replace kzalloc() with devm_kzalloc().

After platform_device_add(), if we error out, we must do
platform_device_unregister(), which also does the put. So lets move
devm_kzalloc() to simplify error handling and avoid
calling of platform_device_unregister().

Change-Id: Ic183ee233f5dc622e9e6126c1bfd4890baee1f76
Signed-off-by: Vardan Mikayelyan <mvardan@xxxxxxxxxxxx>

diff --git a/drivers/usb/dwc2/pci.c b/drivers/usb/dwc2/pci.c
index fdeb8c7..7696131 100644
--- a/drivers/usb/dwc2/pci.c
+++ b/drivers/usb/dwc2/pci.c
@@ -104,10 +104,17 @@ static int dwc2_pci_probe(struct pci_dev *pci,

         pci_set_master(pci);

+       phy = usb_phy_generic_register();
+       if (IS_ERR(phy)) {
+               dev_err(dev, "error registering generic PHY (%ld)\n",
+                       PTR_ERR(phy));
+               return PTR_ERR(phy);
+       }
+
         dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO);
         if (!dwc2) {
                 dev_err(dev, "couldn't allocate dwc2 device\n");
-               return -ENOMEM;
+               goto err;
         }

         memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
@@ -124,32 +131,25 @@ static int dwc2_pci_probe(struct pci_dev *pci,
         ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
         if (ret) {
                 dev_err(dev, "couldn't add resources to dwc2 device\n");
-               return ret;
+               goto err;
         }

         dwc2->dev.parent = dev;

-       phy = usb_phy_generic_register();
-       if (IS_ERR(phy)) {
-               dev_err(dev, "error registering generic PHY (%ld)\n",
-                       PTR_ERR(phy));
-               return PTR_ERR(phy);
-       }
-
         ret = dwc2_pci_quirks(pci, dwc2);
         if (ret)
                 goto err;

+       glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
+       if (!glue)
+               goto err;
+
         ret = platform_device_add(dwc2);
         if (ret) {
                 dev_err(dev, "failed to register dwc2 device\n");
                 goto err;
         }

-       glue = kzalloc(sizeof(*glue), GFP_KERNEL);
-       if (!glue)
-               return -ENOMEM;
-
         glue->phy = phy;


Thanks,
Vardan.



--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux