USB: lpc32xx: fix error handling
Subsystems (4)
cover USB: lpc32xx: fix error handling
A recent change fixing a device reference leak introduced a clock imbalance by reusing an error path so that the clock may be disabled before having been enabled.
The very same change could also lead to a use-after-free in case the driver is used with non-OF probing.
This series fixes the resulting mess as well as the device leak in the NXP OHCI driver.
Included are also two related cleanups.
Johan
Johan Hovold (5):
usb: gadget: lpc32xx_udc: fix clock imbalance in error path
usb: phy: isp1301: fix non-OF device reference imbalance
usb: ohci-nxp: fix device leak on probe failure
usb: gadget: lpc32xx_udc: clean up probe error labels
usb: ohci-nxp: clean up probe error labels
drivers/usb/gadget/udc/lpc32xx_udc.c | 41 ++++++++++++++-------------- drivers/usb/host/ohci-nxp.c | 18 ++++++------ drivers/usb/phy/phy-isp1301.c | 7 ++++- 3 files changed, 36 insertions(+), 30 deletions(-)
Signature · 1 line
2.51.2
PATCH 1/5 usb: gadget: lpc32xx_udc: fix clock imbalance in error path
A recent change fixing a device reference leak introduced a clock imbalance by reusing an error path so that the clock may be disabled before having been enabled.
Note that the clock framework allows for passing in NULL clocks so there is no risk for a NULL pointer dereference.
Also drop the bogus I2C client NULL check added by the offending commit as the pointer has already been verified to be non-NULL.
--- drivers/usb/gadget/udc/lpc32xx_udc.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/usb/gadget/udc/lpc32xx_udc.c +10 -10
@@ -3020,7 +3020,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev) pdev->dev.dma_mask = &lpc32xx_usbd_dmamask; retval =dma_set_coherent_mask (&pdev->dev,DMA_BIT_MASK (32 ));if (retval) - goto i2c_fail; + goto err_put_client; udc->board = &lpc32xx_usbddata;
@@ -3040,7 +3040,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev) udc->udp_irq[i] =platform_get_irq (pdev, i);if (udc->udp_irq[i] <0 ) { retval = udc->udp_irq[i]; - goto i2c_fail; + goto err_put_client; } }
@@ -3048,7 +3048,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (IS_ERR (udc->udp_baseaddr)) {dev_err (udc->dev,"IO map failure\n" ); retval =PTR_ERR (udc->udp_baseaddr); - goto i2c_fail; + goto err_put_client; }/* Get USB device clock */
@@ -3056,14 +3056,14 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (IS_ERR (udc->usb_slv_clk)) {dev_err (udc->dev,"failed to acquire USB device clock\n" ); retval =PTR_ERR (udc->usb_slv_clk); - goto i2c_fail; + goto err_put_client; }/* Enable USB device clock */ retval =clk_prepare_enable (udc->usb_slv_clk);if (retval <0 ) {dev_err (udc->dev,"failed to start USB device clock\n" ); - goto i2c_fail; + goto err_put_client; }/* Setup deferred workqueue data */
@@ -3165,9 +3165,10 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)dma_free_coherent (&pdev->dev,UDCA_BUFF_SIZE , udc->udca_v_base, udc->udca_p_base); i2c_fail: -if (udc->isp1301_i2c_client) -put_device (&udc->isp1301_i2c_client->dev);clk_disable_unprepare (udc->usb_slv_clk); +err_put_client: +put_device (&udc->isp1301_i2c_client->dev); +dev_err (udc->dev,"%s probe failed, %d\n" , driver_name, retval);return retval;
@@ -3195,10 +3196,9 @@ static void lpc32xx_udc_remove(struct platform_device *pdev)dma_free_coherent (&pdev->dev,UDCA_BUFF_SIZE , udc->udca_v_base, udc->udca_p_base); -if (udc->isp1301_i2c_client) -put_device (&udc->isp1301_i2c_client->dev); -clk_disable_unprepare (udc->usb_slv_clk); + +put_device (&udc->isp1301_i2c_client->dev); }#ifdef CONFIG_PM
Signature · 1 line
2.51.2
PATCH 2/5 usb: phy: isp1301: fix non-OF device reference imbalance
In reply to Johan HovoldA recent change fixing a device reference leak in a UDC driver introduced a potential use-after-free in the non-OF case as the isp1301_get_client() helper only increases the reference count for the returned I2C device in the OF case.
Increment the reference count also for non-OF so that the caller can decrement it unconditionally.
Note that this is inherently racy just as using the returned I2C device is since nothing is preventing the PHY driver from being unbound while in use.
--- drivers/usb/phy/phy-isp1301.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/usb/phy/phy-isp1301.c +6 -1
@@ -149,7 +149,12 @@ struct i2c_client *isp1301_get_client(struct device_node *node)return client;/* non-DT: only one ISP1301 chip supported */ -return isp1301_i2c_client; +if (isp1301_i2c_client) { +get_device (&isp1301_i2c_client->dev); +return isp1301_i2c_client; + } + +return NULL ; }EXPORT_SYMBOL_GPL (isp1301_get_client);
Signature · 1 line
2.51.2
PATCH 3/5 usb: ohci-nxp: fix device leak on probe failure
In reply to Johan HovoldMake sure to drop the reference taken when looking up the PHY I2C device during probe on probe failure (e.g. probe deferral) and on driver unbind.
--- drivers/usb/host/ohci-nxp.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/usb/host/ohci-nxp.c +7 -5
@@ -169,13 +169,13 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) ret =dma_coerce_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (32 ));if (ret) - goto fail_disable; + goto err_put_client;dev_dbg (&pdev->dev,"%s: " DRIVER_DESC " (nxp)\n" , hcd_name);if (usb_disabled ()) {dev_err (&pdev->dev,"USB is disabled\n" ); ret = -ENODEV ; - goto fail_disable; + goto err_put_client; }/* Enable USB host clock */
@@ -183,7 +183,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev)if (IS_ERR (usb_host_clk)) {dev_err (&pdev->dev,"failed to acquire and start USB OHCI clock\n" ); ret =PTR_ERR (usb_host_clk); - goto fail_disable; + goto err_put_client; }isp1301_configure ();
@@ -192,7 +192,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev)if (!hcd) {dev_err (&pdev->dev,"Failed to allocate HC buffer\n" ); ret = -ENOMEM ; - goto fail_disable; + goto err_put_client; } hcd->regs =devm_platform_get_and_ioremap_resource (pdev,0 , &res);
@@ -222,7 +222,8 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev)ohci_nxp_stop_hc (); fail_resource:usb_put_hcd (hcd); -fail_disable: +err_put_client: +put_device (&isp1301_i2c_client->dev); isp1301_i2c_client =NULL ;return ret; }
@@ -234,6 +235,7 @@ static void ohci_hcd_nxp_remove(struct platform_device *pdev)usb_remove_hcd (hcd);ohci_nxp_stop_hc ();usb_put_hcd (hcd); +put_device (&isp1301_i2c_client->dev); isp1301_i2c_client =NULL ; }
Signature · 1 line
2.51.2
PATCH 4/5 usb: gadget: lpc32xx_udc: clean up probe error labels
In reply to Johan HovoldError labels should be named after what they do rather than after from where they are jumped to.
Rename the probe error labels for consistency and to improve readability.
--- drivers/usb/gadget/udc/lpc32xx_udc.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/usb/gadget/udc/lpc32xx_udc.c +10 -11
@@ -3084,7 +3084,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (!udc->udca_v_base) {dev_err (udc->dev,"error getting UDCA region\n" ); retval = -ENOMEM ; - goto i2c_fail; + goto err_disable_clk; } udc->udca_p_base = dma_handle; dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
@@ -3097,7 +3097,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (!udc->dd_cache) {dev_err (udc->dev,"error getting DD DMA region\n" ); retval = -ENOMEM ; - goto dma_alloc_fail; + goto err_free_dma; }/* Clear USB peripheral and initialize gadget endpoints */
@@ -3111,14 +3111,14 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (retval <0 ) {dev_err (udc->dev,"LP request irq %d failed\n" , udc->udp_irq[IRQ_USB_LP ]); - goto irq_req_fail; + goto err_destroy_pool; } retval =devm_request_irq (dev, udc->udp_irq[IRQ_USB_HP ], lpc32xx_usb_hp_irq,0 ,"udc_hp" , udc);if (retval <0 ) {dev_err (udc->dev,"HP request irq %d failed\n" , udc->udp_irq[IRQ_USB_HP ]); - goto irq_req_fail; + goto err_destroy_pool; } retval = devm_request_irq(dev, udc->udp_irq[IRQ_USB_DEVDMA],
@@ -3126,7 +3126,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (retval <0 ) {dev_err (udc->dev,"DEV request irq %d failed\n" , udc->udp_irq[IRQ_USB_DEVDMA ]); - goto irq_req_fail; + goto err_destroy_pool; }/* The transceiver interrupt is used for VBUS detection and will
@@ -3137,7 +3137,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)if (retval <0 ) {dev_err (udc->dev,"VBUS request irq %d failed\n" , udc->udp_irq[IRQ_USB_ATX ]); - goto irq_req_fail; + goto err_destroy_pool; }/* Initialize wait queue */
@@ -3146,7 +3146,7 @@ static int lpc32xx_udc_probe(struct platform_device *pdev) retval =usb_add_gadget_udc (dev, &udc->gadget);if (retval <0 ) - goto add_gadget_fail; + goto err_destroy_pool;dev_set_drvdata (dev, udc);device_init_wakeup (dev,1 );
@@ -3158,13 +3158,12 @@ static int lpc32xx_udc_probe(struct platform_device *pdev)dev_info (udc->dev,"%s version %s\n" , driver_name,DRIVER_VERSION );return 0 ; -add_gadget_fail: -irq_req_fail: +err_destroy_pool:dma_pool_destroy (udc->dd_cache); -dma_alloc_fail: +err_free_dma:dma_free_coherent (&pdev->dev,UDCA_BUFF_SIZE , udc->udca_v_base, udc->udca_p_base); -i2c_fail: +err_disable_clk:clk_disable_unprepare (udc->usb_slv_clk); err_put_client:put_device (&udc->isp1301_i2c_client->dev);
Signature · 1 line
2.51.2
PATCH 5/5 usb: ohci-nxp: clean up probe error labels
In reply to Johan HovoldError labels should be named after what they do rather than after from where they are jumped to.
Rename the probe error labels for consistency and to improve readability.
--- drivers/usb/host/ohci-nxp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/host/ohci-nxp.c +3 -3
@@ -198,7 +198,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) hcd->regs =devm_platform_get_and_ioremap_resource (pdev,0 , &res);if (IS_ERR (hcd->regs)) { ret =PTR_ERR (hcd->regs); - goto fail_resource; + goto err_put_hcd; } hcd->rsrc_start = res->start; hcd->rsrc_len =resource_size (res);
@@ -206,7 +206,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) irq =platform_get_irq (pdev,0 );if (irq <0 ) { ret = -ENXIO ; - goto fail_resource; + goto err_put_hcd; }ohci_nxp_start_hc ();
@@ -220,7 +220,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) }ohci_nxp_stop_hc (); -fail_resource: +err_put_hcd:usb_put_hcd (hcd); err_put_client:put_device (&isp1301_i2c_client->dev);
Signature · 1 line
2.51.2
Alan Stern
maintainerRe: [PATCH 5/5] usb: ohci-nxp: clean up probe error labels
Quoted 8 lines · Johan Hovold
Error labels should be named after what they do rather than after from where they are jumped to.
Rename the probe error labels for consistency and to improve readability.
Signed-off-by Johan Hovold <[email protected]>---
Johan, in the 3/5 patch you also changed a statement label, which was not directly related to that patch's actual purpose, but it is directly related to this one's. Can you move that rename from that patch to this one?
Alan Stern
Quoted 37 lines
drivers/usb/host/ohci-nxp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/usb/host/ohci-nxp.c +3
-3@@ -198,7 +198,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) hcd->regs =devm_platform_get_and_ioremap_resource (pdev,0 , &res);if (IS_ERR (hcd->regs)) { ret =PTR_ERR (hcd->regs); - goto fail_resource; + goto err_put_hcd; } hcd->rsrc_start = res->start; hcd->rsrc_len =resource_size (res);@@ -206,7 +206,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) irq =platform_get_irq (pdev,0 );if (irq <0 ) { ret = -ENXIO ; - goto fail_resource; + goto err_put_hcd; }ohci_nxp_start_hc ();@@ -220,7 +220,7 @@ static int ohci_hcd_nxp_probe(struct platform_device *pdev) }ohci_nxp_stop_hc (); -fail_resource: +err_put_hcd:usb_put_hcd (hcd); err_put_client:put_device (&isp1301_i2c_client->dev);Signature · 1 line
2.51.2
Re: [PATCH 5/5] usb: ohci-nxp: clean up probe error labels
Quoted 7 lines · Alan Stern
Quoted 6 lines · Johan Hovold
Error labels should be named after what they do rather than after from where they are jumped to.
Rename the probe error labels for consistency and to improve readability.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Johan Hovold <[email protected]>---
Johan, in the 3/5 patch you also changed a statement label, which was not directly related to that patch's actual purpose, but it is directly related to this one's. Can you move that rename from that patch to this one?
Sure. The label "fail_disable" didn't make any sense to me but looking at the driver again now I see that it's named after the usb_disabled() check and the rename could indeed be done as part of this patch.
I'll address this in a v2.
Johan