Received: by 10.223.185.116 with SMTP id b49csp2603979wrg; Mon, 5 Mar 2018 05:54:43 -0800 (PST) X-Google-Smtp-Source: AG47ELsWIzqy/XpLICdlR0BytCa7pE5logVv4m13M+tQcIxPiJE17Mw2XadcW0S3SUHFfzkrcUBN X-Received: by 2002:a17:902:8a89:: with SMTP id p9-v6mr13139517plo.379.1520258083224; Mon, 05 Mar 2018 05:54:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520258083; cv=none; d=google.com; s=arc-20160816; b=vRzKBAMiSqo+9BL9R5Gq49NIAfSTXMX7op9rxmNGm/Q8OXUz9nE8u4VoUPL0oqXOrY +CepaqXYyeJMyiimPS3g5MS7oKywHBdZXdnOL/eTDDbW8VvpG+DOpiKpjo51lTuCfW38 c81OmT9ELc3gHfIpB/Qb7jbYGO49rf5bta6C9ut5kX4BRKwp88NLEAIY7aT/pZQqAsFX evW9BVKcaYBpDse30eu9kRNkmOk8u4ARP55ewjC8bfMZ44VS0C49dQK0EnMIn8uSXKdz Zw9pCWGMz8ymfaWBXnjqWdKYwXuC/0Wi8eq5fTeQvu5O5csP2hr+n/PCjND2RU5UQS11 AqMQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=w6VwGr1kqSkK4IWJIHel+Q/njft1IPlBJOUvcWvJWos=; b=XyknX3NDibyv79ZNMhetHn/oSvPYETJgLq1mi1gSLw8p4+twg2lYW1IgTVLNTVXOV6 AVbA62XizBHz+bmimWGJqixdIm5vRSY/pzjTl0TbCF1GgN/YUNUOOse9HFxz6JoQsJVU trLVJs0Q9SzQv4Z/t2LqumrKrAtoTH48wh4BfVziyKGAmwHhA+IkN4viIYUOFhJWKAio olSBkpdsW97iTJbJu7Y2QWicM/99UD6CJXjiFqSnJI8WxK0ZN15NxkKA7wY1eH925rZ6 S0Kwzv/YM+SvOyz/6SRog/WoZhY7J0o8VUrAO4do++RFajmHsIa3qliOP418BTAy9MnI Hu8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=ZfGzKtEq; 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; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h12-v6si9375463plt.524.2018.03.05.05.54.28; Mon, 05 Mar 2018 05:54:43 -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=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=ZfGzKtEq; 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; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933255AbeCELDi (ORCPT + 99 others); Mon, 5 Mar 2018 06:03:38 -0500 Received: from lelnx193.ext.ti.com ([198.47.27.77]:55971 "EHLO lelnx193.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751824AbeCELDe (ORCPT ); Mon, 5 Mar 2018 06:03:34 -0500 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by lelnx193.ext.ti.com (8.15.1/8.15.1) with ESMTP id w25B3WFn032462; Mon, 5 Mar 2018 05:03:32 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ti.com; s=ti-com-17Q1; t=1520247812; bh=5a23l/yB2/wS3Rimnfy9/WG3ziQ25Y8vcCuOFNaUmdc=; h=Subject:To:CC:References:From:Date:In-Reply-To; b=ZfGzKtEqnyMxKzWf5ncbIbJrhKu6W/eUFQOSKKBvlIZCr82gYbS99szxyRZeLlQ4L ljm7b+afveOTv4fz1YLphg8My92WdtjdoTrd0R5W5fFD+5PIWfMZiI0M/SMufbTdq4 dQKb14mA7JR9y66eLQUi3PW6PzJNZpejaapEbiGU= Received: from DFLE111.ent.ti.com (dfle111.ent.ti.com [10.64.6.32]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id w25B3WTU028240; Mon, 5 Mar 2018 05:03:32 -0600 Received: from DFLE113.ent.ti.com (10.64.6.34) by DFLE111.ent.ti.com (10.64.6.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1261.35; Mon, 5 Mar 2018 05:03:31 -0600 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE113.ent.ti.com (10.64.6.34) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1261.35 via Frontend Transport; Mon, 5 Mar 2018 05:03:31 -0600 Received: from [192.168.2.6] (ileax41-snat.itg.ti.com [10.172.224.153]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id w25B3UDJ002765; Mon, 5 Mar 2018 05:03:30 -0600 Subject: Re: [PATCH] usb: dwc3: Prevent indefinite sleep in _dwc3_set_mode during suspend/resume To: Baolin Wang CC: Felipe Balbi , USB , LKML References: <1519730526-22274-1-git-send-email-rogerq@ti.com> <87sh9l5z4l.fsf@linux.intel.com> <94cd6377-1327-2309-8d69-6ab0de2bdfd4@ti.com> <87po4i3o1v.fsf@linux.intel.com> From: Roger Quadros Message-ID: <6e3a58fb-f1d8-610f-d39c-7d1259833911@ti.com> Date: Mon, 5 Mar 2018 13:03:29 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-GB Content-Transfer-Encoding: 8bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/03/18 12:41, Baolin Wang wrote: > Hi Roger, > > On 5 March 2018 at 17:45, Roger Quadros wrote: >> Felipe, >> >> On 05/03/18 10:49, Felipe Balbi wrote: >>> >>> Hi, >>> >>> Roger Quadros writes: >>>>> Roger Quadros writes: >>>>>> In the following test we get stuck by sleeping forever in _dwc3_set_mode() >>>>>> after which dual-role switching doesn't work. >>>>>> >>>>>> On dra7-evm's dual-role port, >>>>>> - Load g_zero gadget driver and enumerate to host >>>>>> - suspend to mem >>>>>> - disconnect USB cable to host and connect otg cable with Pen drive in it. >>>>>> - resume system >>>>>> - we sleep indefinitely in _dwc3_set_mode due to. >>>>>> dwc3_gadget_exit()->usb_del_gadget_udc()->udc_stop()-> >>>>>> dwc3_gadget_stop()->wait_event_lock_irq() >>>>>> >>>>>> Let's clear the DWC3_EP_END_TRANSFER_PENDING flag on all endpoints >>>>>> so we don't wait in dwc3_gadget_stop(). >>>>>> >>>>>> Signed-off-by: Roger Quadros >>>>>> --- >>>>>> drivers/usb/dwc3/gadget.c | 14 ++++++++++++++ >>>>>> 1 file changed, 14 insertions(+) >>>>>> >>>>>> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c >>>>>> index 2bda4eb..0a360da 100644 >>>>>> --- a/drivers/usb/dwc3/gadget.c >>>>>> +++ b/drivers/usb/dwc3/gadget.c >>>>>> @@ -3273,6 +3273,20 @@ int dwc3_gadget_init(struct dwc3 *dwc) >>>>>> >>>>>> void dwc3_gadget_exit(struct dwc3 *dwc) >>>>>> { >>>>>> + int epnum; >>>>>> + unsigned long flags; >>>>>> + >>>>>> + spin_lock_irqsave(&dwc->lock, flags); >>>>>> + for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { >>>>>> + struct dwc3_ep *dep = dwc->eps[epnum]; >>>>>> + >>>>>> + if (!dep) >>>>>> + continue; >>>>>> + >>>>>> + dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; >>>>>> + } >>>>>> + spin_unlock_irqrestore(&dwc->lock, flags); >>>>>> + >>>>>> usb_del_gadget_udc(&dwc->gadget); >>>>>> dwc3_gadget_free_endpoints(dwc); >>>>> >>>>> free endpoints is a better place for this. It's already going to free >>>>> the memory anyway. Might as well clear all flags to 0 there. >>>>> >>>> >>>> But it won't solve the deadlock issue. Since dwc3_gadget_free_endpoints() >>>> is called after usb_del_gadget_udc() and the deadlock happens when >>>> >>>> usb_del_gadget_udc()->udc_stop()->dwc3_gadget_stop()->wait_event_lock_irq() >>>> >>>> and DWC3_EP_END_TRANSFER_PENDING flag is set. >>> >>> indeed. Iterating twice over the entire endpoint list seems >>> wasteful. Perhaps we just shouldn't wait when removing the UDC since >>> that's essentially what this patch will do, right? If you clear the flag >>> before calling ->udc_stop(), this means the loop in dwc3_gadget_stop() >>> will do nothing. Might as well remove it. >>> >> >> This means that we will never wait for DWC3_EP_END_TRANSFER_PENDING to clear >> in dwc3_gadget_stop() like we used to. This is perfectly fine, right? >> >> It makes sense to me as dwc3_gadget_stop() calls __dwc3_gadget_stop() which >> masks all interrupts and nobody will ever clear that flag if it was set. > > I don't think so. It can not mask the endpoint events, please check > the events which will be masked in DEVTEN register. The reason why we Correct, endpoint events are not managed by DEVTEN. > should wait for DWC3_EP_END_TRANSFER_PENDING to clear is that, > sometimes the DWC3_DEPEVT_EPCMDCMPLT event will be triggered later > than 100us, but now we may have freed the gadget irq which will cause > crash. > OK. So what is the right approach here? In the test case I mentioned in the commit log the endpoint interrupt never happens and it waits forever in dwc3_gadget_stop(). Since we know we're winding up, can we explicitly disable the endpoint events here? -- cheers, -roger Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki