xhci: Fix leaking USB3 shared_hcd at xhci removal
Subsystems (2)
PATCH 1/2 xhci: Fix leaking USB3 shared_hcd at xhci removal
Quoted 136 lines · Mathias Nyman
Ensure that the
shared_hcdpointer is valid when callingusb_put_hcd()The
shared_hcdis removed and freed in xhci by first callingusb_remove_hcd(xhci->shared_hcd), and laterusb_put_hcd(xhci->shared_hcd)Afer commit
fe190ed0d602("xhci: Do not halt the host until both HCD have disconnected their devices.") theshared_hcdwas never properly put asxhci->shared_hcdwas set to NULL beforeusb_put_hcd(xhci->shared_hcd)was called.
shared_hcd(USB3) is removed before primary hcd (USB2). While removing the primary hcd we might need to handle xhci interrupts to cleanly remove last USB2 devices, therefore we need to setxhci->shared_hcdto NULL before removing the primary hcd to let xhci interrupt handler knowshared_hcdis no longer available.
xhci-plat.c,xhci-histb.cand xhci-mtk first create both their hcd's before adding them. so to keep the correct reverse removal order use a temporaryshared_hcdvariable for them.
For more details see commit4ac53087d6d4("usb: xhci: plat: Create both HCDs before adding them")Fixes fe190ed0d602 ("xhci: Do not halt the host until both HCD have disconnected their devices.")Cc Joel Stanley <[email protected]>Cc Chunfeng Yun <[email protected]>Cc Thierry Reding <[email protected]>Cc Jianguo Sun <[email protected]>Cc <[email protected]>Reported-by Jack Pham <[email protected]>Signed-off-by Mathias Nyman <[email protected]>--- drivers/usb/host/xhci-histb.c | 6 ++++-- drivers/usb/host/xhci-mtk.c | 6 ++++-- drivers/usb/host/xhci-pci.c | 1 + drivers/usb/host/xhci-plat.c | 6 ++++-- drivers/usb/host/xhci-tegra.c | 1 + drivers/usb/host/xhci.c | 2 -- 6 files changed, 14 insertions(+), 8 deletions(-)drivers/usb/host/xhci-histb.c +4
-2@@ -325,14 +325,16 @@ static int xhci_histb_remove(struct platform_device *dev)struct xhci_hcd_histb *histb =platform_get_drvdata (dev);struct usb_hcd *hcd = histb->hcd;struct xhci_hcd *xhci =hcd_to_xhci (hcd); +struct usb_hcd *shared_hcd = xhci->shared_hcd; xhci->xhc_state |=XHCI_STATE_REMOVING ; -usb_remove_hcd (xhci->shared_hcd); +usb_remove_hcd (shared_hcd); + xhci->shared_hcd =NULL ;device_wakeup_disable (&dev->dev);usb_remove_hcd (hcd); -usb_put_hcd (xhci->shared_hcd); +usb_put_hcd (shared_hcd);xhci_histb_host_disable (histb);usb_put_hcd (hcd);drivers/usb/host/xhci-mtk.c +4
-2@@ -590,12 +590,14 @@ static int xhci_mtk_remove(struct platform_device *dev)struct xhci_hcd_mtk *mtk =platform_get_drvdata (dev);struct usb_hcd *hcd = mtk->hcd;struct xhci_hcd *xhci =hcd_to_xhci (hcd); +struct usb_hcd *shared_hcd = xhci->shared_hcd; -usb_remove_hcd (xhci->shared_hcd); +usb_remove_hcd (shared_hcd); + xhci->shared_hcd =NULL ;device_init_wakeup (&dev->dev, false);usb_remove_hcd (hcd); -usb_put_hcd (xhci->shared_hcd); +usb_put_hcd (shared_hcd);usb_put_hcd (hcd);xhci_mtk_sch_exit (mtk);xhci_mtk_clks_disable (mtk);drivers/usb/host/xhci-pci.c +1
-0@@ -356,6 +356,7 @@ static void xhci_pci_remove(struct pci_dev *dev)if (xhci->shared_hcd) {usb_remove_hcd (xhci->shared_hcd);usb_put_hcd (xhci->shared_hcd); + xhci->shared_hcd =NULL ; }/* Workaround for spurious wakeups at shutdown with HSW */ drivers/usb/host/xhci-plat.c +4
-2@@ -359,14 +359,16 @@ static int xhci_plat_remove(struct platform_device *dev)struct xhci_hcd *xhci =hcd_to_xhci (hcd);struct clk *clk = xhci->clk;struct clk *reg_clk = xhci->reg_clk; +struct usb_hcd *shared_hcd = xhci->shared_hcd; xhci->xhc_state |=XHCI_STATE_REMOVING ; -usb_remove_hcd (xhci->shared_hcd); +usb_remove_hcd (shared_hcd); + xhci->shared_hcd =NULL ;usb_phy_shutdown (hcd->usb_phy);usb_remove_hcd (hcd); -usb_put_hcd (xhci->shared_hcd); +usb_put_hcd (shared_hcd);clk_disable_unprepare (clk);clk_disable_unprepare (reg_clk);drivers/usb/host/xhci-tegra.c +1
-0@@ -1240,6 +1240,7 @@ static int tegra_xusb_remove(struct platform_device *pdev)usb_remove_hcd (xhci->shared_hcd);usb_put_hcd (xhci->shared_hcd); + xhci->shared_hcd =NULL ;usb_remove_hcd (tegra->hcd);usb_put_hcd (tegra->hcd);drivers/usb/host/xhci.c +0
-0
drivers/usb/host/xhci.c static void xhci_stop(struct usb_hcd *hcd) · line 724
Quoted 8 lines
drivers/usb/host/xhci.c +0
-2@@ -719,8 +719,6 @@ static void xhci_stop(struct usb_hcd *hcd)/* Only halt host and free memory after both hcds are removed */ if (!usb_hcd_is_primary_hcd (hcd)) { -/* usb core will free this hcd shortly, unset pointer */ - xhci->shared_hcd =NULL ;mutex_unlock (&xhci->mutex);return ; }