Received: by 10.223.185.116 with SMTP id b49csp2429699wrg; Mon, 5 Mar 2018 02:44:38 -0800 (PST) X-Google-Smtp-Source: AG47ELuGyS3y1j7YWVk4/Zcpu9vZVv/DcELk5X1xqY5/CSxcIYtE3l1uT/xqD7XD9FHL2gDFVCDb X-Received: by 10.98.86.151 with SMTP id h23mr14817718pfj.79.1520246678147; Mon, 05 Mar 2018 02:44:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520246678; cv=none; d=google.com; s=arc-20160816; b=SzUvQi96ue9We75TjcciX58djZkxOtQsqXccUaYCTdsNiDh/WX9SrEY+KKVcskD7DT QXbQvxhIDCoz8ikvCUKHxLHavCAu1nI/iUzNtOlicaCYpoXPxsTCxhfWyQKGM5rjUmCV CJfqT1x8VdtuHMjRekpmA1Rfghuq8zT/BST4g8OuN4VhdIPscX6ikQxzZWA3jfvonULr 6Ct0eghzsBRvBvGGqbMTdyZLyB3nM2kK+WsNc2rDfN9hitglFhY8idkqvMgPx9SE/3kd tjvy5CkRXuKGhYwMKTM273APTwzX/bAWPs8KuZrB7kVdF2RHOxBSGIfx2kdsk3phXIv6 r1Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=/rSIkw5PhBNC6rv/XNrFamaJGg6I/9CwOHqSc84jFOQ=; b=x7ha4H8UQ+4HkULN8vvScV2o3A2rciEGg60Mb5jOr3UiiUIzdpj8iVBhpe3QdSxHuV 5LwCbGq2xxejnpcaW6oI08fUewKZDBdLIwhl69iaJDumPfOUqaImS9hNFStSqpqTy3Hn q+D1kiu5FtAjIf4/cEVDFOggwSxsAVMyrLouExjD/prjye5WAu+hRAQRsZ7PfTBuTzlO 5d4XJ77kmkuPg1cEqU64rI1/7nrw7CjdYe5Gm6v9prPV3qJPHEuncGjH9FTizvuhj6c0 7o8dBHqKbnMKG03JRajS22LheTTWVviFZ/gykVhng1v08wxLNyoIBERfoGFK3/O+D/eD AB3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=C2ey1wT/; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g9si8334311pfi.310.2018.03.05.02.44.23; Mon, 05 Mar 2018 02:44:38 -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=@linaro.org header.s=google header.b=C2ey1wT/; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934409AbeCEKlf (ORCPT + 99 others); Mon, 5 Mar 2018 05:41:35 -0500 Received: from mail-ot0-f194.google.com ([74.125.82.194]:43774 "EHLO mail-ot0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933579AbeCEKlb (ORCPT ); Mon, 5 Mar 2018 05:41:31 -0500 Received: by mail-ot0-f194.google.com with SMTP id m22so14452693otf.10 for ; Mon, 05 Mar 2018 02:41:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=/rSIkw5PhBNC6rv/XNrFamaJGg6I/9CwOHqSc84jFOQ=; b=C2ey1wT/vW6/PvBxSJqz+U+5LcWS6AVwCm5N0JqZrGE+obvIRjtqFwxarY29tWOa/i PDtjXUVepAtLVa/KcjCCzETCjtOETVCbP+a7D2GnGlLUVLBXgWf+6+P2dQziob2OM+rV GqMHdG+VcSOCTj1PkKYgMKIW5+Q/VSSokb+3c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=/rSIkw5PhBNC6rv/XNrFamaJGg6I/9CwOHqSc84jFOQ=; b=NHTCWfEN8mbQ+eajb9TUcfzNYxRBoHTjY1rcRVyZrYz7vz3ttoHPIiTHRLdwf5P13f apK5R9wGsVAGRfSWSXUKb5GwkIeOIl0ok7TYQyEl0IARvBdp722X76cRrV4yrF4J95zD 7H2sVQR+2S7pAD08QGV8XV5FHrYw9StHG5aAcJr05yrpiZaSnFWo2r9wM/acTN6eo4mX zMVHI0mDtw07C+KJvogBnowJVBBSRZuGmpC4FBiFFqYs2dyTAbKrzw7naPK+eoRTaDCg tvnCfta7fMt6COBFszFYIQVe58/7EKnHnmj3DKejsGflPjuqjPuawZ03G6UWb58hf0r+ yQ5g== X-Gm-Message-State: APf1xPA2fgDpL8qiY9jfAjaVnqvTNizNyyFwG+EIx8sSaVQ72HVvoscZ W6YKaGj8Bn9q603HejkR1yKI/BqdN3Gvvy2CXdjBqA== X-Received: by 10.157.85.9 with SMTP id l9mr9757435oth.177.1520246491266; Mon, 05 Mar 2018 02:41:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.157.96.5 with HTTP; Mon, 5 Mar 2018 02:41:30 -0800 (PST) In-Reply-To: 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: Baolin Wang Date: Mon, 5 Mar 2018 18:41:30 +0800 Message-ID: Subject: Re: [PATCH] usb: dwc3: Prevent indefinite sleep in _dwc3_set_mode during suspend/resume To: Roger Quadros Cc: Felipe Balbi , USB , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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. -- Baolin.wang Best Regards