xhci fixes for usb-linus
Subsystems (3)
cover xhci fixes for usb-linus
Hi Greg
A few xhci fixes that resolve a race condition, a memory leak, incorrect irqrestore and other issues.
-Mathias
Henry Lin (1):
usb: xhci: only set D3hot for pci device
Kai-Heng Feng (1):
xhci: Increase STS_HALT timeout in xhci_suspend()
Mathias Nyman (3):
xhci: fix USB3 device initiated resume race with roothub autosuspend
xhci: handle some XHCI_TRUST_TX_LENGTH quirks cases as default
behaviour.
xhci: make sure interrupts are restored to correct state
Mika Westerberg (1):
xhci: Fix memory leak in xhci_add_in_port()
drivers/usb/host/xhci-hub.c | 22 ++++++++++++++++------ drivers/usb/host/xhci-mem.c | 4 ++++ drivers/usb/host/xhci-pci.c | 13 +++++++++++++ drivers/usb/host/xhci-ring.c | 6 +++--- drivers/usb/host/xhci.c | 9 +++------ drivers/usb/host/xhci.h | 1 + 6 files changed, 40 insertions(+), 15 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/6 xhci: Fix memory leak in xhci_add_in_port()
From: Mika Westerberg <[email protected]>
When xHCI is part of Alpine or Titan Ridge Thunderbolt controller and the xHCI device is hot-removed as a result of unplugging a dock for example, the driver leaks memory it allocates for xhci->usb3_rhub.psi and xhci->usb2_rhub.psi in xhci_add_in_port() as reported by kmemleak:
unreferenced object 0xffff922c24ef42f0 (size 16):
comm "kworker/u16:2", pid 178, jiffies 4294711640 (age 956.620s) hex dump (first 16 bytes):
21 00 0c 00 12 00 dc 05 23 00 e0 01 00 00 00 00 !.......#.......
backtrace:
[<000000007ac80914>] xhci_mem_init+0xcf8/0xeb7
[<0000000001b6d775>] xhci_init+0x7c/0x160
[<00000000db443fe3>] xhci_gen_setup+0x214/0x340
[<00000000fdffd320>] xhci_pci_setup+0x48/0x110
[<00000000541e1e03>] usb_add_hcd.cold+0x265/0x747
[<00000000ca47a56b>] usb_hcd_pci_probe+0x219/0x3b4
[<0000000021043861>] xhci_pci_probe+0x24/0x1c0
[<00000000b9231f25>] local_pci_probe+0x3d/0x70
[<000000006385c9d7>] pci_device_probe+0xd0/0x150
[<0000000070241068>] really_probe+0xf5/0x3c0
[<0000000061f35c0a>] driver_probe_device+0x58/0x100
[<000000009da11198>] bus_for_each_drv+0x79/0xc0
[<000000009ce45f69>] __device_attach+0xda/0x160
[<00000000df201aaf>] pci_bus_add_device+0x46/0x70
[<0000000088a1bc48>] pci_bus_add_devices+0x27/0x60
[<00000000ad9ee708>] pci_bus_add_devices+0x52/0x60
unreferenced object 0xffff922c24ef3318 (size 8):
comm "kworker/u16:2", pid 178, jiffies 4294711640 (age 956.620s)
hex dump (first 8 bytes):
34 01 05 00 35 41 0a 00 4...5A..
backtrace:
[<000000007ac80914>] xhci_mem_init+0xcf8/0xeb7
[<0000000001b6d775>] xhci_init+0x7c/0x160
[<00000000db443fe3>] xhci_gen_setup+0x214/0x340
[<00000000fdffd320>] xhci_pci_setup+0x48/0x110
[<00000000541e1e03>] usb_add_hcd.cold+0x265/0x747
[<00000000ca47a56b>] usb_hcd_pci_probe+0x219/0x3b4
[<0000000021043861>] xhci_pci_probe+0x24/0x1c0
[<00000000b9231f25>] local_pci_probe+0x3d/0x70
[<000000006385c9d7>] pci_device_probe+0xd0/0x150
[<0000000070241068>] really_probe+0xf5/0x3c0
[<0000000061f35c0a>] driver_probe_device+0x58/0x100
[<000000009da11198>] bus_for_each_drv+0x79/0xc0
[<000000009ce45f69>] __device_attach+0xda/0x160
[<00000000df201aaf>] pci_bus_add_device+0x46/0x70
[<0000000088a1bc48>] pci_bus_add_devices+0x27/0x60
[<00000000ad9ee708>] pci_bus_add_devices+0x52/0x60Fix this by calling kfree() for the both psi objects in xhci_mem_cleanup().
--- drivers/usb/host/xhci-mem.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/usb/host/xhci-mem.c +4 -0
@@ -1909,13 +1909,17 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci) xhci->usb3_rhub.num_ports =0 ; xhci->num_active_eps =0 ;kfree (xhci->usb2_rhub.ports); +kfree (xhci->usb2_rhub.psi);kfree (xhci->usb3_rhub.ports); +kfree (xhci->usb3_rhub.psi);kfree (xhci->hw_ports);kfree (xhci->rh_bw);kfree (xhci->ext_caps); xhci->usb2_rhub.ports =NULL ; + xhci->usb2_rhub.psi =NULL ; xhci->usb3_rhub.ports =NULL ; + xhci->usb3_rhub.psi =NULL ; xhci->hw_ports =NULL ; xhci->rh_bw =NULL ; xhci->ext_caps =NULL ;
Signature · 1 line
2.17.1
PATCH 2/6 xhci: fix USB3 device initiated resume race with roothub autosuspend
In reply to Mathias NymanA race in xhci USB3 remote wake handling may force device back to suspend after it initiated resume siganaling, causing a missed resume event or warm reset of device.
When a USB3 link completes resume signaling and goes to enabled (UO) state a interrupt is issued and the interrupt handler will clear the bus_state->port_remote_wakeup resume flag, allowing bus suspend.
If the USB3 roothub thread just finished reading port status before the interrupt, finding ports still in suspended (U3) state, but hasn't yet started suspending the hub, then the xhci interrupt handler will clear the flag that prevented roothub suspend and allow bus to suspend, forcing all port links back to suspended (U3) state.
Example case:usb_runtime_suspend() # because all ports still show suspended U3
usb_suspend_both()
hub_suspend(); # successful as hub->wakeup_bits not set yet
==> INTERRUPT
xhci_irq()
handle_port_status()
clear bus_state->port_remote_wakeup
usb_wakeup_notification()
sets hub->wakeup_bits;
kick_hub_wq()<== END INTERRUPT
hcd_bus_suspend()
xhci_bus_suspend() # success as port_remote_wakeup bits cleared
Fix this by increasing roothub usage count during port resume to prevent roothub autosuspend, and by making sure bus_state->port_remote_wakeup flag is only cleared after resume completion is visible, i.e. after xhci roothub returned U0 or other non-U3 link state link on a get port status request.
Issue rootcaused by Chiasheng Lee
--- drivers/usb/host/xhci-hub.c | 10 ++++++++++ drivers/usb/host/xhci-ring.c | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-hub.c +10 -0
@@ -920,11 +920,13 @@ static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status, {struct xhci_bus_state *bus_state;struct xhci_hcd *xhci; +struct usb_hcd *hcd;u32 link_state;u32 portnum; bus_state = &port->rhub->bus_state; xhci =hcd_to_xhci (port->rhub->hcd); + hcd = port->rhub->hcd; link_state = portsc &PORT_PLS_MASK ; portnum = port->hcd_portnum;
@@ -952,6 +954,14 @@ static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status, bus_state->suspended_ports &= ~(1 << portnum); } +/* remote wake resume signaling complete */ +if (bus_state->port_remote_wakeup & (1 << portnum) && + link_state !=XDEV_RESUME && + link_state !=XDEV_RECOVERY ) { + bus_state->port_remote_wakeup &= ~(1 << portnum); +usb_hcd_end_port_resume (&hcd->self, portnum); + } +xhci_hub_report_usb3_link_state (xhci, status, portsc);xhci_del_comp_mod_timer (xhci, portsc, portnum); }
drivers/usb/host/xhci-ring.c +1 -2
@@ -1628,7 +1628,6 @@ static void handle_port_status(struct xhci_hcd *xhci, slot_id =xhci_find_slot_id_by_port (hcd, xhci, hcd_portnum +1 );if (slot_id && xhci->devs[slot_id]) xhci->devs[slot_id]->flags |=VDEV_PORT_ERROR ; - bus_state->port_remote_wakeup &= ~(1 << hcd_portnum); }if ((portsc &PORT_PLC ) && (portsc &PORT_PLS_MASK ) ==XDEV_RESUME ) {
@@ -1648,6 +1647,7 @@ static void handle_port_status(struct xhci_hcd *xhci,*/ bus_state->port_remote_wakeup |=1 << hcd_portnum;xhci_test_and_clear_bit (xhci, port,PORT_PLC ); +usb_hcd_start_port_resume (&hcd->self, hcd_portnum);xhci_set_link_state (xhci, port,XDEV_U0 );/* Need to wait until the next link state change * indicates the device is actually in U0.
@@ -1688,7 +1688,6 @@ static void handle_port_status(struct xhci_hcd *xhci,if (slot_id && xhci->devs[slot_id])xhci_ring_device (xhci, slot_id);if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) { - bus_state->port_remote_wakeup &= ~(1 << hcd_portnum);xhci_test_and_clear_bit (xhci, port,PORT_PLC );usb_wakeup_notification (hcd->self.root_hub, hcd_portnum +1 );
Signature · 1 line
2.17.1
PATCH 3/6 usb: xhci: only set D3hot for pci device
In reply to Mathias NymanFrom: Henry Lin <[email protected]>
Xhci driver cannot call pci_set_power_state() on non-pci xhci host controllers. For example, NVIDIA Tegra XHCI host controller which acts as platform device with XHCI_SPURIOUS_WAKEUP quirk set in some platform hits this issue during shutdown.
--- drivers/usb/host/xhci-pci.c | 13 +++++++++++++ drivers/usb/host/xhci.c | 7 ++----- drivers/usb/host/xhci.h | 1 + 3 files changed, 16 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-pci.c +13 -0
@@ -521,6 +521,18 @@ static int xhci_pci_resume(struct usb_hcd *hcd, bool hibernated) }#endif /* CONFIG_PM */ +static void xhci_pci_shutdown (struct usb_hcd *hcd) +{ +struct xhci_hcd *xhci =hcd_to_xhci (hcd); +struct pci_dev *pdev =to_pci_dev (hcd->self.controller); + +xhci_shutdown (hcd); + +/* Yet another workaround for spurious wakeups at shutdown with HSW */ +if (xhci->quirks &XHCI_SPURIOUS_WAKEUP ) +pci_set_power_state (pdev, PCI_D3hot); +} +/*-------------------------------------------------------------------------*/ /* PCI driver selection metadata; PCI hotplugging uses this */
@@ -556,6 +568,7 @@ static int __init xhci_pci_init(void)#ifdef CONFIG_PM xhci_pci_hc_driver.pci_suspend = xhci_pci_suspend; xhci_pci_hc_driver.pci_resume = xhci_pci_resume; + xhci_pci_hc_driver.shutdown = xhci_pci_shutdown;#endif return pci_register_driver (&xhci_pci_driver); }
drivers/usb/host/xhci.c +2 -5
@@ -770,7 +770,7 @@ static void xhci_stop(struct usb_hcd *hcd)* * This will only ever be called with the main usb_hcd (the USB3 roothub). */ -static void xhci_shutdown (struct usb_hcd *hcd) +void xhci_shutdown (struct usb_hcd *hcd) {struct xhci_hcd *xhci =hcd_to_xhci (hcd);
@@ -789,11 +789,8 @@ static void xhci_shutdown(struct usb_hcd *hcd)xhci_dbg_trace (xhci, trace_xhci_dbg_init,"xhci_shutdown completed - status = %x" ,readl (&xhci->op_regs->status)); - -/* Yet another workaround for spurious wakeups at shutdown with HSW */ -if (xhci->quirks &XHCI_SPURIOUS_WAKEUP ) -pci_set_power_state (to_pci_dev (hcd->self.sysdev), PCI_D3hot); } +EXPORT_SYMBOL_GPL (xhci_shutdown);#ifdef CONFIG_PM static void xhci_save_registers (struct xhci_hcd *xhci)
drivers/usb/host/xhci.h +1 -0
@@ -2050,6 +2050,7 @@ int xhci_start(struct xhci_hcd *xhci);int xhci_reset (struct xhci_hcd *xhci);int xhci_run (struct usb_hcd *hcd);int xhci_gen_setup (struct usb_hcd *hcd,xhci_get_quirks_t get_quirks); +void xhci_shutdown (struct usb_hcd *hcd);void xhci_init_driver (struct hc_driver *drv,const struct xhci_driver_overrides *over);int xhci_disable_slot (struct xhci_hcd *xhci,u32 slot_id);
Signature · 1 line
2.17.1
PATCH 4/6 xhci: Increase STS_HALT timeout in xhci_suspend()
In reply to Mathias NymanFrom: Kai-Heng Feng <[email protected]>
I've recently observed failed xHCI suspend attempt on AMD Raven Ridge system:
kernel: xhci_hcd 0000:04:00.4: WARN: xHC CMD_RUN timeout
kernel: PM: suspend_common(): xhci_pci_suspend+0x0/0xd0 returns -110
kernel: PM: pci_pm_suspend(): hcd_pci_suspend+0x0/0x30 returns -110
kernel: PM: dpm_run_callback(): pci_pm_suspend+0x0/0x150 returns -110
kernel: PM: Device 0000:04:00.4 failed to suspend async: error -110
Similar to commit ac343366846a ("xhci: Increase STS_SAVE timeout in xhci_suspend()") we also need to increase the HALT timeout to make it be able to suspend again.
--- drivers/usb/host/xhci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci.c +1 -1
@@ -970,7 +970,7 @@ static bool xhci_pending_portevent(struct xhci_hcd *xhci)int xhci_suspend (struct xhci_hcd *xhci,bool do_wakeup) {int rc =0 ; -unsigned int delay =XHCI_MAX_HALT_USEC ; +unsigned int delay =XHCI_MAX_HALT_USEC *2 ;struct usb_hcd *hcd =xhci_to_hcd (xhci);u32 command;u32 res;
Signature · 1 line
2.17.1
PATCH 5/6 xhci: handle some XHCI_TRUST_TX_LENGTH quirks cases as default behaviour.
In reply to Mathias Nymanxhci driver claims it needs XHCI_TRUST_TX_LENGTH quirk for both Broadcom/Cavium and a Renesas xHC controllers.
The quirk was inteded for handling false "success" complete event for transfers that had data left untransferred.
These transfers should complete with "short packet" events instead.
In these two new cases the false "success" completion is reported after a "short packet" if the TD consists of several TRBs. xHCI specs 4.10.1.1.2 say remaining TRBs should report "short packet" as well after the first short packet in a TD, but this issue seems so common it doesn't make sense to add the quirk for all vendors.
Turn these events into short packets automatically instead.
This gets rid of the "The WARN Successful completion on short TX for slot 1 ep 1: needs XHCI_TRUST_TX_LENGTH quirk" warning in many cases.
--- drivers/usb/host/xhci-ring.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +2 -1
@@ -2381,7 +2381,8 @@ static int handle_tx_event(struct xhci_hcd *xhci,case COMP_SUCCESS :if (EVENT_TRB_LEN (le32_to_cpu (event->transfer_len)) ==0 )break ; -if (xhci->quirks &XHCI_TRUST_TX_LENGTH ) +if (xhci->quirks &XHCI_TRUST_TX_LENGTH || + ep_ring->last_td_was_short) trb_comp_code =COMP_SHORT_PACKET ;else xhci_warn_ratelimited(xhci,
Signature · 1 line
2.17.1
PATCH 6/6 xhci: make sure interrupts are restored to correct state
In reply to Mathias Nymanspin_unlock_irqrestore() might be called with stale flags after reading port status, possibly restoring interrupts to a incorrect state.
If a usb2 port just finished resuming while the port status is read the spin lock will be temporary released and re-acquired in a separate function. The flags parameter is passed as value instead of a pointer, not updating flags properly before the final spin_unlock_irqrestore() is called.
--- drivers/usb/host/xhci-hub.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/usb/host/xhci-hub.c +6 -6
@@ -806,7 +806,7 @@ static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,static int xhci_handle_usb2_port_link_resume (struct xhci_port *port,u32 *status,u32 portsc, -unsigned long flags) +unsigned long *flags) {struct xhci_bus_state *bus_state;struct xhci_hcd *xhci;
@@ -860,11 +860,11 @@ static int xhci_handle_usb2_port_link_resume(struct xhci_port *port,xhci_test_and_clear_bit (xhci, port,PORT_PLC );xhci_set_link_state (xhci, port,XDEV_U0 ); -spin_unlock_irqrestore (&xhci->lock, flags); +spin_unlock_irqrestore (&xhci->lock, *flags); time_left =wait_for_completion_timeout ( &bus_state->rexit_done[wIndex],msecs_to_jiffies (XHCI_MAX_REXIT_TIMEOUT_MS )); -spin_lock_irqsave (&xhci->lock, flags); +spin_lock_irqsave (&xhci->lock, *flags);if (time_left) { slot_id = xhci_find_slot_id_by_port(hcd, xhci,
@@ -967,7 +967,7 @@ static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status, }static void xhci_get_usb2_port_status (struct xhci_port *port,u32 *status, -u32 portsc,unsigned long flags) +u32 portsc,unsigned long *flags) {struct xhci_bus_state *bus_state;u32 link_state;
@@ -1017,7 +1017,7 @@ static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status,static u32 xhci_get_port_status (struct usb_hcd *hcd,struct xhci_bus_state *bus_state,u16 wIndex,u32 raw_port_status, -unsigned long flags) +unsigned long *flags) __releases(&xhci->lock) __acquires(&xhci->lock) {
@@ -1140,7 +1140,7 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, }trace_xhci_get_port_status (wIndex, temp); status =xhci_get_port_status (hcd, bus_state, wIndex, temp, - flags); + &flags);if (status ==0xffffffff ) goto error;
Signature · 1 line
2.17.1