Skip to contentSkip to conversation

usb: host: xhci-plat: fix possible kernel oops while resuming

PATCH Superseded Sergey Shtylyov 1 participant 2 messages Jun 30, 2023 – Jul 1, 2023

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: host: xhci-plat: fix possible kernel oops while resuming

If this driver enables the xHC clocks while resuming from sleep, it calls clk_prepare_enable() without checking for errors and blithely goes on to read/write the xHC's registers -- which, with the xHC not being clocked, at least on ARM32 usually causes an imprecise external abort exceptions which cause kernel oops. Currently, the chips for which the driver does the clock dance on suspend/resume seem to be the Broadcom STB SoCs, based on ARM32 CPUs, as it seems...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Fixes 8bd954c56197 ("usb: host: xhci-plat: suspend and resume clocks")

---
This patch is against the 'usb-linus' branch of Greg KH's 'usb.git' repo...

Changes in version 2:
- fixed up the error path for clk_prepare_enable() calls in xhci_plat_resume().

 drivers/usb/host/xhci-plat.c |   21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-plat.c +17 -4
Index: usb/drivers/usb/host/xhci-plat.c
===================================================================
@@ -470,23 +470,36 @@ static int __maybe_unused xhci_plat_resu
 	int ret;
 
 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
-		clk_prepare_enable(xhci->clk);
-		clk_prepare_enable(xhci->reg_clk);
+		ret = clk_prepare_enable(xhci->clk);
+		if (ret)
+			return ret;
+
+		ret = clk_prepare_enable(xhci->reg_clk);
+		if (ret)
+			goto disable_clk;
 	}
 
 	ret = xhci_priv_resume_quirk(hcd);
 	if (ret)
-		return ret;
+		goto disable_reg_clk;
 
 	ret = xhci_resume(xhci, 0);
 	if (ret)
-		return ret;
+		goto disable_reg_clk;
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_active(dev);
 	pm_runtime_enable(dev);
 
 	return 0;
+
+disable_reg_clk:
+	clk_disable_unprepare(xhci->reg_clk);
+
+disable_clk:
+	clk_disable_unprepare(xhci->clk);
+
+	return ret;
 }
 
 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
#1 lore raw
Quoted 11 lines · Sergey Shtylyov

If this driver enables the xHC clocks while resuming from sleep, it calls clk_prepare_enable() without checking for errors and blithely goes on to read/write the xHC's registers -- which, with the xHC not being clocked, at least on ARM32 usually causes an imprecise external abort exceptions which cause kernel oops. Currently, the chips for which the driver does the clock dance on suspend/resume seem to be the Broadcom STB SoCs, based on ARM32 CPUs, as it seems...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

Quoted 8 lines
Fixes 8bd954c56197 ("usb: host: xhci-plat: suspend and resume clocks")

---
This patch is against the 'usb-linus' branch of Greg KH's 'usb.git' repo...

Changes in version 2:
- fixed up the error path for clk_prepare_enable() calls in xhci_plat_resume().

Scratch that, still not correct enough...

Quoted 36 lines
 drivers/usb/host/xhci-plat.c |   21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

Index: usb/drivers/usb/host/xhci-plat.c
===================================================================

drivers/usb/host/xhci-plat.c +9 -4
@@ -470,23 +470,36 @@ static int __maybe_unused xhci_plat_resu
 	int ret;
 
 	if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
-		clk_prepare_enable(xhci->clk);
-		clk_prepare_enable(xhci->reg_clk);
+		ret = clk_prepare_enable(xhci->clk);
+		if (ret)
+			return ret;
+
+		ret = clk_prepare_enable(xhci->reg_clk);
+		if (ret)
+			goto disable_clk;
 	}
 
 	ret = xhci_priv_resume_quirk(hcd);
 	if (ret)
-		return ret;
+		goto disable_reg_clk;
 
 	ret = xhci_resume(xhci, 0);
 	if (ret)
-		return ret;
+		goto disable_reg_clk;
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_active(dev);
 	pm_runtime_enable(dev);
 

drivers/usb/host/xhci-plat.c static int __maybe_unused xhci_plat_resu · line 503

Quoted 10 lines
drivers/usb/host/xhci-plat.c +8 -0
 	return 0;
+
+disable_reg_clk:
+	clk_disable_unprepare(xhci->reg_clk);
+
+disable_clk:
+	clk_disable_unprepare(xhci->clk);
+
+	return ret;
 }

[...]

Grr, the error path is still incorrect here -- it should've checked
xhci->quirks/etc. before calling the clock disable/unprepare... :-(

MBR, Sergey