Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2121843rdh; Tue, 26 Sep 2023 13:09:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGyGyzn+QrHJSva9rV8ozqqkufsCpviji0txcjykGSUwX4AbhA6oPUWFVtKU9IQAzdIdhTu X-Received: by 2002:a05:6358:9987:b0:143:8084:e625 with SMTP id j7-20020a056358998700b001438084e625mr134142rwb.11.1695758972573; Tue, 26 Sep 2023 13:09:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695758972; cv=none; d=google.com; s=arc-20160816; b=pPCzlXLpRoLGdA/s05WDdBEGunz0vCEKMFaOjMqQC9RcgnMYXPQAiJ2Xk4sIat68uB ywQqavfsrHAL9lICCd5jmT2iwdOxkovlIrvEPcJyVag90EAKgLb42QAU1WqLw7NtLoM3 3Iba+UJpjZ5DQDYWg+vxrtfFWwaEVlPz8Wty8rNGKvUnXIpKn5Ls2aRU876jpAwg2PBB Gi4n2Lw2GyOOP8D+cRCeB2mkNAt/bBbjiw0pOWUt9N+qb0wTPYF+S+439p4Rq9G5TG/f Pp7iqTXFnTqiW66x9TzI8SpsbzmSn4vqioW90yFrlR/wW6K5rI8zfjbFbtfgkMTFW2Qh 9o0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=6PAzvY0MYUjGu5ZTimJruukax+GunvBGPYGdIoichUY=; fh=HFbn/fsDQwdAoEzomvdAarmTcJm9ZYPuAae+045vsPQ=; b=Py3dTy7H7HifbZ8LHpT9Km45cQfp7J66IM+le55deS8ze+xa0W8Tjzs1eke4reN3Vn o1MBIS4bRqcOrV9EN+4FvkM/ILcSGACgk5FQoibPTtY5UUea+cwlIom+LYrE5EVIW3ev I2ffN613C0tKuk5WuSNOWus8fl/o0/90pI2ZAZy9O4jbXH/mASSWsF0QcuFwkzlTnTBa 1EoJtyrGyuc161iWIq2fd7VuLRZr9U4ZPe934Z8XLvvdLIrtYuuWC2SSjsmMRnNlqaft 0+N3A7CADoW8nhxcDooNvKtpdUDEKHNJxl7JUqJ4VGv9j9u2oPeCXmjaole35zZ5jZGT i8nQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id y4-20020a636404000000b00578c8ce14edsi14141784pgb.252.2023.09.26.13.09.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 13:09:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 84C238026623; Tue, 26 Sep 2023 12:54:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229862AbjIZTyQ (ORCPT + 99 others); Tue, 26 Sep 2023 15:54:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230344AbjIZTyP (ORCPT ); Tue, 26 Sep 2023 15:54:15 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 437A311D for ; Tue, 26 Sep 2023 12:54:07 -0700 (PDT) Received: (qmail 1473364 invoked by uid 1000); 26 Sep 2023 15:54:06 -0400 Date: Tue, 26 Sep 2023 15:54:06 -0400 From: Alan Stern To: Krishna Kurapati Cc: Greg Kroah-Hartman , Francesco Dolcini , Badhri Jagan Sridharan , Michael Grzeschik , Ivan Orlov , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, quic_ppratap@quicinc.com, quic_wcheng@quicinc.com, quic_jackp@quicinc.com Subject: Re: [PATCH v4] usb: gadget: udc: Handle gadget_connect failure during bind operation Message-ID: <2178cf29-5e5c-4ed6-8d1c-916bc7036589@rowland.harvard.edu> References: <20230926193708.22405-1-quic_kriskura@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230926193708.22405-1-quic_kriskura@quicinc.com> X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 26 Sep 2023 12:54:17 -0700 (PDT) On Wed, Sep 27, 2023 at 01:07:08AM +0530, Krishna Kurapati wrote: > In the event, gadget_connect call (which invokes pullup) fails, s/event,/event/ > propagate the error to udc bind operation which inturn sends the s/inturn/in turn/ > error to configfs. The userspace can then retry enumeartion if s/enumeartion/enumeration/ > it chooses to. > > Signed-off-by: Krishna Kurapati > --- > Changes in v4: Fixed mutex locking imbalance during connect_control > failure > Link to v3: https://lore.kernel.org/all/20230510075252.31023-3-quic_kriskura@quicinc.com/ > > drivers/usb/gadget/udc/core.c | 23 +++++++++++++++++++---- > 1 file changed, 19 insertions(+), 4 deletions(-) > > diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c > index 7d49d8a0b00c..53af25a333a1 100644 > --- a/drivers/usb/gadget/udc/core.c > +++ b/drivers/usb/gadget/udc/core.c > @@ -1125,12 +1125,16 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state); > /* ------------------------------------------------------------------------- */ > > /* Acquire connect_lock before calling this function. */ > -static void usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock) > +static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock) > { > + int ret; > + > if (udc->vbus) > - usb_gadget_connect_locked(udc->gadget); > + ret = usb_gadget_connect_locked(udc->gadget); > else > - usb_gadget_disconnect_locked(udc->gadget); > + ret = usb_gadget_disconnect_locked(udc->gadget); > + > + return ret; You don't actually need the new variable ret. You can just do: if (udc->vbus) return usb_gadget_connect_locked(udc->gadget); else return usb_gadget_disconnect_locked(udc->gadget); > } > > static void vbus_event_work(struct work_struct *work) > @@ -1604,12 +1608,23 @@ static int gadget_bind_driver(struct device *dev) > } > usb_gadget_enable_async_callbacks(udc); > udc->allow_connect = true; > - usb_udc_connect_control_locked(udc); > + ret = usb_udc_connect_control_locked(udc); > + if (ret) { > + mutex_unlock(&udc->connect_lock); > + goto err_connect_control; > + } > + > mutex_unlock(&udc->connect_lock); > > kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); > return 0; > > + err_connect_control: > + usb_gadget_disable_async_callbacks(udc); > + if (gadget->irq) > + synchronize_irq(gadget->irq); > + usb_gadget_udc_stop_locked(udc); Not good -- usb_gadget_udc_stop_locked() expects you to be holding udc->connect_lock, but you just dropped the lock! Also, you never set udc->allow_connect back to false. You should move the mutex_unlock() call from inside the "if" statement to down here, and add a line for udc->allow_connect. Alan Stern > + > err_start: > driver->unbind(udc->gadget); > > -- > 2.42.0 >