Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp775621pxb; Tue, 3 Nov 2020 12:09:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZ1IFk8OA3H9F0FBtFyAGeduE3gjcBOlOGrPQJoCXM514bSOXZSXNxxOHId/WeUE8HTqKS X-Received: by 2002:a17:906:444:: with SMTP id e4mr23024164eja.218.1604434174644; Tue, 03 Nov 2020 12:09:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604434174; cv=none; d=google.com; s=arc-20160816; b=u3P6GMislNDquKi9yozkhHw9nroZ6Xh4okIcz/uMMltQTK/fA2gXXHU/xtXsp9rWME NsIfOV6EMFMADASLPQn3d9XVvyoPN2KNZ1Dk6nuSMw4/iZd5w4biZ8AU2OZ8bNejfGa4 tnFYkcCHguPYoU6KsqsDp2R+aJHhLG1JG4Tp/HLX3vpEPUdvcKdjU2oRXGET1vX9YL44 i2j27DWS4F6jXecVAhLhwtOx9ufxfjfjQxLuAmHCZ7m2ildxFnaG0X5tcdgAwO5Bb0Lw U9gtDjR+rxkX32ckoSQsxx4NVJKByPdP7yQ52fKdTbJ16JYNyXHHjfN71ZbxMZwsKXkk f5rQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=10fC3tV3BxE0ITkW6I7Q5rWxLeVMLM672UX2BpBOuI8=; b=igDm/KELo9gPAJtwbQQ0e3eCuzHjw/Hqg7gKUjUHekc5JjYXf8autoyQV1kyBIeJwu 9nbHDLG68EOGppYPxG09vhDqqRwtBomKJcUyaP834TI0Anojwa4GFtLbPJCqIwlzWaPD 1NhMaS7JHdf0rVITPXnU+d4IeiWGUt5Qz4xpwIqoczoN7uTbIOhBftLu6Ws4EusreozB vz7FNTkxhMNfDCRTYVpSnj5wQuROCNOHgJkJjjRTg/pIBx0foLdG91OubbiQMAaF0Kq6 98WeyZMytUiEA6Dp416MDRAkDRhlCWE6RXFfYtfkc6iyrCo08nEoma5oB/tyRvOsxsS/ rSQg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b15si952404edv.389.2020.11.03.12.09.11; Tue, 03 Nov 2020 12:09:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729263AbgKCUHX (ORCPT + 99 others); Tue, 3 Nov 2020 15:07:23 -0500 Received: from netrider.rowland.org ([192.131.102.5]:40629 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1729160AbgKCUHW (ORCPT ); Tue, 3 Nov 2020 15:07:22 -0500 Received: (qmail 1538831 invoked by uid 1000); 3 Nov 2020 15:07:16 -0500 Date: Tue, 3 Nov 2020 15:07:16 -0500 From: Alan Stern To: Wesley Cheng Cc: balbi@kernel.org, gregkh@linuxfoundation.org, Thinh.Nguyen@synopsys.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, jackp@codeaurora.org Subject: Re: [PATCH 1/2] usb: dwc3: gadget: Allow runtime suspend if UDC unbinded Message-ID: <20201103200716.GA1538425@rowland.harvard.edu> References: <20201028234311.6464-1-wcheng@codeaurora.org> <20201028234311.6464-2-wcheng@codeaurora.org> <20201029010748.GA1303156@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 03, 2020 at 11:02:25AM -0800, Wesley Cheng wrote: > > > On 10/28/2020 6:07 PM, Alan Stern wrote: > >> --- a/drivers/usb/dwc3/gadget.c > >> +++ b/drivers/usb/dwc3/gadget.c > >> @@ -1995,6 +1995,11 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) > >> unsigned long flags; > >> int ret; > >> > >> + if (pm_runtime_suspended(dwc->dev)) { > >> + pm_request_resume(dwc->dev); > >> + return 0; > >> + } > > > > Isn't this racy? What happens if the controller was active but a > > runtime suspend occurs right here? > > > > Alan Stern > > > > Hi Alan, > > Ah, yes you're right. I was hoping that the PM runtime layer would be > utilizing the spinlock when reading out the runtime status, but even > then, we wouldn't be able to catch intermediate states with this API > (i.e. RPM_RESUMING or RPM_SUSPENDING) > > Tried a few different approaches, and came up with something like the > following: > > static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) > { > ... > ret = pm_runtime_get_sync(dwc->dev); > if (!ret) { > pm_runtime_put(dwc->dev); > return 0; > } > ... > pm_runtime_put(dwc->dev); > > return 0; > } > > I think this should be good to address your concern. The only way we > would be able to ensure that the runtime PM state doesn't enter > idle/suspend is if we increment the usage count for the duration we're > accessing the DWC3 registers. With the synchronous PM runtime resume > call, we can also ensure that no pending runtime suspends are executing > in parallel while running this code. That's correct. > The check for the zero return value would be for avoiding running the > DWC3 run stop sequence for the case where we executed the runtime PM > resume, as the DWC3 runtime PM resume routine will set the run stop bit > in there. If you need to add an explanation of this subtle point in your email message, then you should add a similar explanation as a comment in the code. And don't forget to check for ret < 0 (i.e., a resume error). Alan Stern