usb: xhci: return an error if the host is not halted
Subsystems (2)
PATCH 1/1 usb: xhci: return an error if the host is not halted
xhci_reset() returns 0 when the host is not halted, without ever writing CMD_RESET. Every other path that fails to reset the host returns an error, so callers that check the return value are told the reset succeeded on the one path where it did not happen.
Return -EBUSY when the reset is aborted because the host is not halted.
--- drivers/usb/host/xhci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci.c +1 -1
@@ -195,7 +195,7 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)if ((state &STS_HALT ) ==0 ) {xhci_warn (xhci,"Host controller not halted, aborting reset.\n" ); -return 0 ; +return -EBUSY ; }xhci_dbg_trace (xhci, trace_xhci_dbg_init,"// Reset the HC" );
Signature · 1 line
2.43.0
Quoted 17 lines · Henry Tseng
xhci_reset()returns 0 when the host is not halted, without ever writingCMD_RESET. Every other path that fails to reset the host returns an error, so callers that check the return value are told the reset succeeded on the one path where it did not happen.Return
-EBUSYwhen the reset is aborted because the host is not halted.Signed-off-by Henry Tseng <[email protected]>--- drivers/usb/host/xhci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/host/xhci.c +0
-0
drivers/usb/host/xhci.c int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us) · line 198
Quoted 5 lines
drivers/usb/host/xhci.c +1
-1@@ -195,7 +195,7 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)if ((state &STS_HALT ) ==0 ) {xhci_warn (xhci,"Host controller not halted, aborting reset.\n" ); -return 0 ; +return -EBUSY ;
That's probably how it should be, but I wonder how did you find this bug and are you aware of any cases where it makes a difference?
There is apparently one caller (xhci_resume()) which cares about xhci_reset() return value but not about the preceding xhci_halt().
Quoted 6 lines
drivers/usb/host/xhci.c +0
-0}xhci_dbg_trace (xhci, trace_xhci_dbg_init,"// Reset the HC" );Signature · 1 line
2.43.0
Quoted 2 lines · Michal Pecio
That's probably how it should be, but I wonder how did you find this bug and are you aware of any cases where it makes a difference?
I noticed it while debugging a downstream kernel (based on v6.6) for an embedded NAS platform, which carries an xHC error recovery routine doing the same sequence as the reset_registers path in xhci_resume().
Since the NAS platform doesn't run a mainline kernel easily, I did the testing on a separate machine. On this machine (Intel Meteor Lake-P xHC, 8086:7e7d) STS_HALT is set by the time xhci_resume() runs, so I added a fault injection flag to xhci_reset() on mainline to force the abort path, making it return 0 without doing the reset. Resume then restarts the HCD on a host that was never reset. Every command afterwards times out and gets aborted, and devices loop on failed re-enumeration for minutes:
[ 100.109411] xhci_hcd 0000:00:14.0: Host controller not halted, aborting reset. [ 100.110025] xhci_hcd 0000:00:14.0: Start the primary HCD [ 100.110132] xhci_hcd 0000:00:14.0: Start the secondary HCD [ 100.194666] xhci_hcd 0000:00:14.0: The device to be reset with slot ID 0 does not exist. Re-allocate the device [ 107.573055] xhci_hcd 0000:00:14.0: Error while assigning device slot ID: Command Aborted [ 107.573059] xhci_hcd 0000:00:14.0: Max number of devices this xHCI host supports is 64. ... [ 259.125506] xhci_hcd 0000:00:14.0: Error while assigning device slot ID: Command Aborted [ 259.125509] xhci_hcd 0000:00:14.0: Max number of devices this xHCI host supports is 64. [ 259.548200] xhci_hcd 0000:00:14.0: xHCI xhci_drop_endpoint called with unaddressed device [ 259.549751] xhci_hcd 0000:00:14.0: xHCI xhci_free_dev called with unaddressed device
On the test machine I couldn't reproduce the not-halted case through a normal xhci_suspend()/xhci_resume() cycle. But the reset_registers path is taken exactly when the host state is not trusted (power lost, broken suspend, HCE, RESET_ON_RESUME), so making sure xhci_reset() actually completed seems right regardless.
Quoted 2 lines
There is apparently one caller (
xhci_resume()) which cares aboutxhci_reset()return value but not about the precedingxhci_halt().
Yes, xhci_resume() is exactly the caller I had in mind. Since it doesn't check xhci_halt(), the reset return value is the only thing standing between a host that failed to halt and the reinit.
Thanks,
Henry
Quoted 29 lines · Henry Tseng
xhci_reset()returns 0 when the host is not halted, without ever writingCMD_RESET. Every other path that fails to reset the host returns an error, so callers that check the return value are told the reset succeeded on the one path where it did not happen.Return
-EBUSYwhen the reset is aborted because the host is not halted.Signed-off-by Henry Tseng <[email protected]>--- drivers/usb/host/xhci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/host/xhci.c +1
-1@@ -195,7 +195,7 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)if ((state &STS_HALT ) ==0 ) {xhci_warn (xhci,"Host controller not halted, aborting reset.\n" ); -return 0 ; +return -EBUSY ; }xhci_dbg_trace (xhci, trace_xhci_dbg_init,"// Reset the HC" );Signature · 1 line
2.43.0
Hi Mathias,
A gentle ping on this one. Please let me know if the approach needs any changes.
Thanks,
Henry
Quoted 32 lines · Henry Tseng
Quoted 29 lines · Henry Tseng
xhci_reset()returns 0 when the host is not halted, without ever writingCMD_RESET. Every other path that fails to reset the host returns an error, so callers that check the return value are told the reset succeeded on the one path where it did not happen.Return
-EBUSYwhen the reset is aborted because the host is not halted.Signed-off-by Henry Tseng <[email protected]>--- drivers/usb/host/xhci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/host/xhci.c +1
-1@@ -195,7 +195,7 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)if ((state &STS_HALT ) ==0 ) {xhci_warn (xhci,"Host controller not halted, aborting reset.\n" ); -return 0 ; +return -EBUSY ; }xhci_dbg_trace (xhci, trace_xhci_dbg_init,"// Reset the HC" );Signature · 1 line
2.43.0Hi Mathias,
Quoted 2 lines
A gentle ping on this one. Please let me know if the approach needs any changes.
Patch added to queue, thanks
Mathias