Commit 43fe3a99 authored by Alan Stern's avatar Alan Stern Committed by Greg Kroah-Hartman

USB: EHCI: resolve some unlikely races

This patch (as1589) resolves some unlikely races involving system
shutdown or controller death in ehci-hcd:

	Shutdown races with both root-hub resume and controller
	resume.

	Controller death races with root-hub suspend.

A new bitflag is added to indicate that the controller has been shut
down (whether for system shutdown or because it died).  Tests are
added in the suspend and resume pathways to avoid reactivating the
controller after any sort of shutdown.
Signed-off-by: default avatarAlan Stern <stern@rowland.harvard.edu>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent c4f34764
...@@ -343,6 +343,7 @@ static void ehci_shutdown(struct usb_hcd *hcd) ...@@ -343,6 +343,7 @@ static void ehci_shutdown(struct usb_hcd *hcd)
struct ehci_hcd *ehci = hcd_to_ehci(hcd); struct ehci_hcd *ehci = hcd_to_ehci(hcd);
spin_lock_irq(&ehci->lock); spin_lock_irq(&ehci->lock);
ehci->shutdown = true;
ehci->rh_state = EHCI_RH_STOPPING; ehci->rh_state = EHCI_RH_STOPPING;
ehci->enabled_hrtimer_events = 0; ehci->enabled_hrtimer_events = 0;
spin_unlock_irq(&ehci->lock); spin_unlock_irq(&ehci->lock);
...@@ -823,6 +824,7 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd) ...@@ -823,6 +824,7 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd)
usb_hc_died(hcd); usb_hc_died(hcd);
/* Don't let the controller do anything more */ /* Don't let the controller do anything more */
ehci->shutdown = true;
ehci->rh_state = EHCI_RH_STOPPING; ehci->rh_state = EHCI_RH_STOPPING;
ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE); ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
ehci_writel(ehci, ehci->command, &ehci->regs->command); ehci_writel(ehci, ehci->command, &ehci->regs->command);
...@@ -1129,6 +1131,9 @@ static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated) ...@@ -1129,6 +1131,9 @@ static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated)
/* Mark hardware accessible again as we are back to full power by now */ /* Mark hardware accessible again as we are back to full power by now */
set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
if (ehci->shutdown)
return 0; /* Controller is dead */
/* /*
* If CF is still set and we aren't resuming from hibernation * If CF is still set and we aren't resuming from hibernation
* then we maintained suspend power. * then we maintained suspend power.
...@@ -1139,10 +1144,17 @@ static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated) ...@@ -1139,10 +1144,17 @@ static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated)
int mask = INTR_MASK; int mask = INTR_MASK;
ehci_prepare_ports_for_controller_resume(ehci); ehci_prepare_ports_for_controller_resume(ehci);
spin_lock_irq(&ehci->lock);
if (ehci->shutdown)
goto skip;
if (!hcd->self.root_hub->do_remote_wakeup) if (!hcd->self.root_hub->do_remote_wakeup)
mask &= ~STS_PCD; mask &= ~STS_PCD;
ehci_writel(ehci, mask, &ehci->regs->intr_enable); ehci_writel(ehci, mask, &ehci->regs->intr_enable);
ehci_readl(ehci, &ehci->regs->intr_enable); ehci_readl(ehci, &ehci->regs->intr_enable);
skip:
spin_unlock_irq(&ehci->lock);
return 0; return 0;
} }
...@@ -1154,14 +1166,20 @@ static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated) ...@@ -1154,14 +1166,20 @@ static int __maybe_unused ehci_resume(struct usb_hcd *hcd, bool hibernated)
(void) ehci_halt(ehci); (void) ehci_halt(ehci);
(void) ehci_reset(ehci); (void) ehci_reset(ehci);
spin_lock_irq(&ehci->lock);
if (ehci->shutdown)
goto skip;
ehci_writel(ehci, ehci->command, &ehci->regs->command); ehci_writel(ehci, ehci->command, &ehci->regs->command);
ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag); ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */ ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
ehci->rh_state = EHCI_RH_SUSPENDED;
spin_unlock_irq(&ehci->lock);
/* here we "know" root ports should always stay powered */ /* here we "know" root ports should always stay powered */
ehci_port_power(ehci, 1); ehci_port_power(ehci, 1);
ehci->rh_state = EHCI_RH_SUSPENDED;
return 1; return 1;
} }
......
...@@ -221,6 +221,8 @@ static int ehci_bus_suspend (struct usb_hcd *hcd) ...@@ -221,6 +221,8 @@ static int ehci_bus_suspend (struct usb_hcd *hcd)
ehci_quiesce(ehci); ehci_quiesce(ehci);
spin_lock_irq (&ehci->lock); spin_lock_irq (&ehci->lock);
if (ehci->rh_state < EHCI_RH_RUNNING)
goto done;
/* Once the controller is stopped, port resumes that are already /* Once the controller is stopped, port resumes that are already
* in progress won't complete. Hence if remote wakeup is enabled * in progress won't complete. Hence if remote wakeup is enabled
...@@ -306,6 +308,10 @@ static int ehci_bus_suspend (struct usb_hcd *hcd) ...@@ -306,6 +308,10 @@ static int ehci_bus_suspend (struct usb_hcd *hcd)
ehci_halt (ehci); ehci_halt (ehci);
spin_lock_irq(&ehci->lock); spin_lock_irq(&ehci->lock);
if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
ehci_handle_controller_death(ehci);
if (ehci->rh_state != EHCI_RH_RUNNING)
goto done;
ehci->rh_state = EHCI_RH_SUSPENDED; ehci->rh_state = EHCI_RH_SUSPENDED;
end_unlink_async(ehci); end_unlink_async(ehci);
...@@ -320,6 +326,7 @@ static int ehci_bus_suspend (struct usb_hcd *hcd) ...@@ -320,6 +326,7 @@ static int ehci_bus_suspend (struct usb_hcd *hcd)
ehci_writel(ehci, mask, &ehci->regs->intr_enable); ehci_writel(ehci, mask, &ehci->regs->intr_enable);
ehci_readl(ehci, &ehci->regs->intr_enable); ehci_readl(ehci, &ehci->regs->intr_enable);
done:
ehci->next_statechange = jiffies + msecs_to_jiffies(10); ehci->next_statechange = jiffies + msecs_to_jiffies(10);
ehci->enabled_hrtimer_events = 0; ehci->enabled_hrtimer_events = 0;
ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT; ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
...@@ -342,10 +349,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd) ...@@ -342,10 +349,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
if (time_before (jiffies, ehci->next_statechange)) if (time_before (jiffies, ehci->next_statechange))
msleep(5); msleep(5);
spin_lock_irq (&ehci->lock); spin_lock_irq (&ehci->lock);
if (!HCD_HW_ACCESSIBLE(hcd)) { if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
spin_unlock_irq(&ehci->lock); goto shutdown;
return -ESHUTDOWN;
}
if (unlikely(ehci->debug)) { if (unlikely(ehci->debug)) {
if (!dbgp_reset_prep()) if (!dbgp_reset_prep())
...@@ -384,6 +389,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd) ...@@ -384,6 +389,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
spin_unlock_irq(&ehci->lock); spin_unlock_irq(&ehci->lock);
msleep(8); msleep(8);
spin_lock_irq(&ehci->lock); spin_lock_irq(&ehci->lock);
if (ehci->shutdown)
goto shutdown;
/* clear phy low-power mode before resume */ /* clear phy low-power mode before resume */
if (ehci->bus_suspended && ehci->has_hostpc) { if (ehci->bus_suspended && ehci->has_hostpc) {
...@@ -401,6 +408,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd) ...@@ -401,6 +408,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
spin_unlock_irq(&ehci->lock); spin_unlock_irq(&ehci->lock);
msleep(5); msleep(5);
spin_lock_irq(&ehci->lock); spin_lock_irq(&ehci->lock);
if (ehci->shutdown)
goto shutdown;
} }
/* manually resume the ports we suspended during bus_suspend() */ /* manually resume the ports we suspended during bus_suspend() */
...@@ -421,6 +430,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd) ...@@ -421,6 +430,8 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
spin_unlock_irq(&ehci->lock); spin_unlock_irq(&ehci->lock);
msleep(20); msleep(20);
spin_lock_irq(&ehci->lock); spin_lock_irq(&ehci->lock);
if (ehci->shutdown)
goto shutdown;
} }
i = HCS_N_PORTS (ehci->hcs_params); i = HCS_N_PORTS (ehci->hcs_params);
...@@ -439,10 +450,18 @@ static int ehci_bus_resume (struct usb_hcd *hcd) ...@@ -439,10 +450,18 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
ehci_handover_companion_ports(ehci); ehci_handover_companion_ports(ehci);
/* Now we can safely re-enable irqs */ /* Now we can safely re-enable irqs */
spin_lock_irq(&ehci->lock);
if (ehci->shutdown)
goto shutdown;
ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable); ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
(void) ehci_readl(ehci, &ehci->regs->intr_enable); (void) ehci_readl(ehci, &ehci->regs->intr_enable);
spin_unlock_irq(&ehci->lock);
return 0; return 0;
shutdown:
spin_unlock_irq(&ehci->lock);
return -ESHUTDOWN;
} }
#else #else
......
...@@ -118,6 +118,7 @@ struct ehci_hcd { /* one per controller */ ...@@ -118,6 +118,7 @@ struct ehci_hcd { /* one per controller */
bool need_rescan:1; bool need_rescan:1;
bool intr_unlinking:1; bool intr_unlinking:1;
bool async_unlinking:1; bool async_unlinking:1;
bool shutdown:1;
struct ehci_qh *qh_scan_next; struct ehci_qh *qh_scan_next;
/* async schedule support */ /* async schedule support */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment