Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1739031rwl; Wed, 5 Apr 2023 23:56:12 -0700 (PDT) X-Google-Smtp-Source: AKy350YVt/Q+zPuJa42xO9xxsmx7cEm5+U2hs+unNv3FQP4B8QpiGPi93TOdIL6qY/BJH+f40HBp X-Received: by 2002:a05:6a20:3b21:b0:da:6602:790 with SMTP id c33-20020a056a203b2100b000da66020790mr1983453pzh.52.1680764172296; Wed, 05 Apr 2023 23:56:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680764172; cv=none; d=google.com; s=arc-20160816; b=WATc2KttyRWNG0JEUgerww3XEv9vw42Y/kwpMUDZvGpBeLVhlqRsiYuvwMWqmkWeCl T4jZJnwxwX1bhMNG6FD99LvuRi0EmEvxyEly9pDiEkL1QrAbtl7CqRR8VBAJepcFarCs aC5xhX28xns7ndv9oNpfwuoPA7j4Pbp8GhAXtx4+zzugD/UnH6TAdCGBnrz2oXIMsSet UvQiebW+/NMcqnvEhrWwwBeKSfL0R0J7oaLIeIIQln5AovqYt6cRXtNdQV/VFC8uQMSx DBYvncerIYkn77AaOTvoswlqWfloBAlFJk8EnpA6HHCZmh1dmo1a4Ql2w2YkPwlAsqBj xdGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ywRy2NXg/YzPGPsQQtseGk+ykosJNLGdFknAZq9oon8=; b=zHbFzpHxGOrMVATNuptrzHxmUWS2ea7Tt7kc4E+VrPNbDBD4SOXhvF+JoFDoEJg46P tcj+2COShaP9xnxTGZ/HtSNgDfrIYA2pu0kYHG2HT7kv2Lt+K5vM5hpkIvL5yaZEaCZm srEy0XX+ZZk5kXZR5UigyyXF3SfQJQiyL9mMlC8aikmd4alOj25LYyzQKf3zSxkPPV74 nNEHCKrMrj9yEf8FmI6ipDE2rWv2B/iraI34ttJTgJPwruWK0HcFCoArbzDlg7NXNMub bd/KzpBbj0ctDvLeoC4hxD2g52oYcSWFbELgA+VJvBDh2dVQhPWXD7TagjwqQguDAQle kdVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="SAJGqK+/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w4-20020a656944000000b005021cf73e8fsi589906pgq.119.2023.04.05.23.56.00; Wed, 05 Apr 2023 23:56:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="SAJGqK+/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235508AbjDFGeZ (ORCPT + 99 others); Thu, 6 Apr 2023 02:34:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235481AbjDFGeT (ORCPT ); Thu, 6 Apr 2023 02:34:19 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AA1F93C7 for ; Wed, 5 Apr 2023 23:34:16 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id bt19so25150105pfb.3 for ; Wed, 05 Apr 2023 23:34:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680762856; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ywRy2NXg/YzPGPsQQtseGk+ykosJNLGdFknAZq9oon8=; b=SAJGqK+/VO4Kto7Td+yYhsLmaOk7HrEBQbs3efgqgCbat5ZLAhp8+7A64Ttj+QL4Ol kMoPp46DeleCcru+sKESaaS4XM8ojsMov4kZwNlNa50x3yChuEl72k/EwiJTJSoZqyvu lQ+tanhKOzHQ57N5CyFVO/lPL4ioc+kZIXfDcRZP2Sn7RoWZZXcu8cVe8uYVotuX+vLH 0lTtWjAVgfNB3YW+nlyP+cQ09U3HbvNmVPfL8Lu64z/oi02i4r26PbgHCxm496DzVioK mmTbSN81gjo2RmFNF0vgxkrm3vXkwoTucl/0hout5Ksx3y6KrzCJfT47HD/rPnQjyXcm VBSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680762856; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ywRy2NXg/YzPGPsQQtseGk+ykosJNLGdFknAZq9oon8=; b=oUXhtGgUYOHxh+unvmXaZWisOkUAOWCcoeDwvk4N8CMR4tNgQWXwLjEEnu+lM2fYBJ XkGlc/fSbPAbMF/pOyfeIXfm6xMIUbetGIe3IEfG/LlplLzKDLMQ6Vl/qRoJwlZZJi/z HnLVAe7NJnGQSh/spzBd/XpRZIkWZF6wRFai7wY2KjSGpTmu5ypNFh+AyXwuHlasHvg+ n4UH4XhC7FC0zsWLzbvGRRI3wqYrNz2rs6UESIuUxqn8B2dTUNSmm9YYg6VD9QO8dAYr RFewg9RDQhAuKXi/Ri9PI/N6rKBI/xWvEGmyqLGx7DuDKF2aMedIlmCRQdt1DKpyWbt/ pTAw== X-Gm-Message-State: AAQBX9dd6W7p8HSVtyh9MORWEGpvKeJ6jpYd+8c+DrGejhIK9AB5iMxO KzMjokjPQwcqVK2Y3vYi9tdSQYmU5eYjhhwN1UpGnA== X-Received: by 2002:a63:e148:0:b0:503:7bbb:9a77 with SMTP id h8-20020a63e148000000b005037bbb9a77mr2897768pgk.8.1680762855762; Wed, 05 Apr 2023 23:34:15 -0700 (PDT) MIME-Version: 1.0 References: <20230405093133.1858140-1-badhri@google.com> <56abca17-7240-4bd5-98db-ef48059ff315@rowland.harvard.edu> In-Reply-To: <56abca17-7240-4bd5-98db-ef48059ff315@rowland.harvard.edu> From: Badhri Jagan Sridharan Date: Wed, 5 Apr 2023 23:33:39 -0700 Message-ID: Subject: Re: [PATCH v1 1/2] usb: gadget: udc: core: Invoke usb_gadget_connect only when started To: Alan Stern Cc: gregkh@linuxfoundation.org, colin.i.king@gmail.com, xuetao09@huawei.com, quic_eserrao@quicinc.com, water.zhangjiantao@huawei.com, peter.chen@freescale.com, balbi@ti.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > No blank line after cc: stable, and put the fixes above your signed-off-by line please. Fixed all commit message related concerns. > Why a global lock? Shouldn't this be a per-device lock? Ack ! Addressed this in V2. On Wed, Apr 5, 2023 at 6:29=E2=80=AFPM Alan Stern wrote: > > On Wed, Apr 05, 2023 at 09:31:32AM +0000, Badhri Jagan Sridharan wrote: > > usb_udc_connect_control does not check to see if the udc > > has already been started. This causes gadget->ops->pullup > > to be called through usb_gadget_connect when invoked > > from usb_udc_vbus_handler even before usb_gadget_udc_start > > is called. Guard this by checking for udc->started in > > usb_udc_connect_control before invoking usb_gadget_connect. > > > > Guarding udc_connect_control, udc->started and udc->vbus > > with its own mutex as usb_udc_connect_control_locked > > can be simulataneously invoked from different code paths. > > > > Cc: stable@vger.kernel.org > > > > Signed-off-by: Badhri Jagan Sridharan > > Fixes: 628ef0d273a6 ("usb: udc: add usb_udc_vbus_handler") > > There's a problem with this patch. > > > --- > > drivers/usb/gadget/udc/core.c | 20 ++++++++++++++++---- > > 1 file changed, 16 insertions(+), 4 deletions(-) > > > > diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/cor= e.c > > index 3dcbba739db6..890f92cb6344 100644 > > --- a/drivers/usb/gadget/udc/core.c > > +++ b/drivers/usb/gadget/udc/core.c > > > @@ -1140,14 +1145,18 @@ static inline int usb_gadget_udc_start(struct u= sb_udc *udc) > > { > > int ret; > > > > + mutex_lock(&udc_connect_control_lock); > > if (udc->started) { > > dev_err(&udc->dev, "UDC had already started\n"); > > + mutex_unlock(&udc_connect_control_lock); > > return -EBUSY; > > } > > > > ret =3D udc->gadget->ops->udc_start(udc->gadget, udc->driver); > > if (!ret) > > udc->started =3D true; > > + usb_udc_connect_control_locked(udc); > > + mutex_unlock(&udc_connect_control_lock); > > You moved the connect_control call up here, into usb_gadget_udc_start(). Have moved it back into gadget_bind_driver. > > > return ret; > > } > > @@ -1165,13 +1174,17 @@ static inline int usb_gadget_udc_start(struct u= sb_udc *udc) > > */ > > static inline void usb_gadget_udc_stop(struct usb_udc *udc) > > { > > + mutex_lock(&udc_connect_control_lock); > > if (!udc->started) { > > dev_err(&udc->dev, "UDC had already stopped\n"); > > + mutex_unlock(&udc_connect_control_lock); > > return; > > } > > > > udc->gadget->ops->udc_stop(udc->gadget); > > udc->started =3D false; > > + usb_udc_connect_control_locked(udc); > > + mutex_unlock(&udc_connect_control_lock); > > } > > > > /** > > @@ -1527,7 +1540,6 @@ static int gadget_bind_driver(struct device *dev) > > if (ret) > > goto err_start; > > usb_gadget_enable_async_callbacks(udc); > > - usb_udc_connect_control(udc); > > This is where it used to be. > > The problem is that in the gadget_bind_driver pathway, > usb_gadget_enable_async_callbacks() has to run _before_ the gadget > connects. Maybe you can fix this by leaving the function call in its > original location and protecting it with the new mutex? > > There may be a similar problem with disconnecting and the > gadget_unbind_driver pathway (usb_gadget_disable_async_callbacks() has to > run _after_ the disconnect occurs). I haven't tried to follow the patch > in enough detail to see whether that's an issue. Thanks for explaining what's the expectation here. I have incorporated the feedback in v2. The new lock now additionally guards gadget->connect and gadget->deactivat= e as well. Guarding all with the new lock as they are related to one another. I have made sure that the gadget_bind_driver and gadget_unbind_driver sequence remains unaltered. > > Alan Stern > > > > > kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); > > return 0; > > > > base-commit: d629c0e221cd99198b843d8351a0a9bfec6c0423 > > -- > > 2.40.0.348.gf938b09366-goog > >