Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2278129ybl; Thu, 19 Dec 2019 10:54:25 -0800 (PST) X-Google-Smtp-Source: APXvYqxchAaN2lbfz0Zr2aMjVFEYjLbDRWHIM1ERejBRwEC/PiazORgPtFxK4dafEjez9ky9o6Xy X-Received: by 2002:a9d:65cf:: with SMTP id z15mr10651018oth.238.1576781664918; Thu, 19 Dec 2019 10:54:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576781664; cv=none; d=google.com; s=arc-20160816; b=Zw0hue4MgGVFgE8txa6RYnh5Odf0Q+7p0Q4qmo+TyjkRtM+BG6kUVwPxQyT+sjW0O4 MuZK+nmDTQC8/Fx+Orrfy3ePEvBTTcM9bVmuTVsgWS9G1X5PhM63SFAQFHu9n3/kJgvh jW7N8WKlbq1b++CtukvGiBZcHZ3IzQ8fuzQVEnabEluCCLEtzrhFNTG/bmSxCSptUoGH vv/kob6Qwm6B7Zr7Q5/ZRk2o35dN2Qv8TBc/vrhXwu1gLklwisdm8+gSVOAtC0t7wTLK LvcaWImRE3q03MXsrKtrLvW77m439xmmDljYGqFuOSzA+0gJ9//yh1KTzAWPbbsG2/dF VIsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3g2NVgihw7CMFVFj7xycT8N5Anr15lHeOvmQPJVJkqU=; b=Wd57X+H0yFvU9vJPsQbtDMudNUJmEOLUMpzMeyOk4gqCC26xYdHlq1QlCXdgc0VZpC S75f9xZVi/1ucyM++wXKy5gr3ChxId/9iJmw0oXQuidoHo3xStEfk3RrqoANAEdUwfSt YXn841UKmwlU+arqtwAT9WB1ZNPr7Y65B9Hy9m7Ff1mGNQJlJYUkOg8CJEun+pry/37q rN6L7uRtCzpcKcSsyE0Xa6PfZo0Juw896B6ya2QbrYp/GW2nA0w1l4gYzvkAB00ytD4i P44u9R1K1ta9bYc3d5lqjW7GC3ZazIM12QPiB55PStiueVq2x/kKWTmkJiAtiSWjAKWm Y3UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@kernel.org header.s=default header.b=FUDSqdIh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z11si3589003oic.119.2019.12.19.10.54.12; Thu, 19 Dec 2019 10:54:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@kernel.org header.s=default header.b=FUDSqdIh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730336AbfLSSxK (ORCPT + 99 others); Thu, 19 Dec 2019 13:53:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:48050 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730330AbfLSSxJ (ORCPT ); Thu, 19 Dec 2019 13:53:09 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E789D222C2; Thu, 19 Dec 2019 18:53:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576781588; bh=Zg5KqcZ26rTjP1tK1brjKZIEG7HDAAKzdkuGMhpZMaU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FUDSqdIhn+nNBeHL50Q70gJ1GwTTiUV2R891VCfdlWd0kbpejfdZCZi1qoPEjVTZz Bi4IEMN2zO8KEzCWot0+gfDIsH2Oj7JV2nNHy+/oeXgEWyp8fR+ZSo0r51zCAmdSZx aTOLwPl4uiue6Hm5wb8fcaO7+vseibkQ5yQpo70I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Lee, Hou-hsun" , "Lee, Chiasheng" , Mathias Nyman , Lee@vger.kernel.org Subject: [PATCH 4.19 47/47] xhci: fix USB3 device initiated resume race with roothub autosuspend Date: Thu, 19 Dec 2019 19:35:01 +0100 Message-Id: <20191219182957.735319195@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191219182857.659088743@linuxfoundation.org> References: <20191219182857.659088743@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mathias Nyman commit 057d476fff778f1d3b9f861fdb5437ea1a3cfc99 upstream. A 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 Cc: Cc: Lee, Hou-hsun Reported-by: Lee, Chiasheng Signed-off-by: Mathias Nyman Link: https://lore.kernel.org/r/20191211142007.8847-3-mathias.nyman@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/host/xhci-hub.c | 8 ++++++++ drivers/usb/host/xhci-ring.c | 3 +-- 2 files changed, 9 insertions(+), 2 deletions(-) --- a/drivers/usb/host/xhci-hub.c +++ b/drivers/usb/host/xhci-hub.c @@ -868,6 +868,14 @@ static u32 xhci_get_port_status(struct u status |= USB_PORT_STAT_C_BH_RESET << 16; if ((raw_port_status & PORT_CEC)) status |= USB_PORT_STAT_C_CONFIG_ERROR << 16; + + /* USB3 remote wake resume signaling completed */ + if (bus_state->port_remote_wakeup & (1 << wIndex) && + (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME && + (raw_port_status & PORT_PLS_MASK) != XDEV_RECOVERY) { + bus_state->port_remote_wakeup &= ~(1 << wIndex); + usb_hcd_end_port_resume(&hcd->self, wIndex); + } } if (hcd->speed < HCD_USB3) { --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -1609,7 +1609,6 @@ static void handle_port_status(struct xh 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) { @@ -1630,6 +1629,7 @@ static void handle_port_status(struct xh bus_state->port_remote_wakeup |= 1 << hcd_portnum; xhci_test_and_clear_bit(xhci, port, PORT_PLC); xhci_set_link_state(xhci, port, XDEV_U0); + usb_hcd_start_port_resume(&hcd->self, hcd_portnum); /* Need to wait until the next link state change * indicates the device is actually in U0. */ @@ -1669,7 +1669,6 @@ static void handle_port_status(struct xh 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);