Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1739348rwl; Wed, 5 Apr 2023 23:56:37 -0700 (PDT) X-Google-Smtp-Source: AKy350ZLA+vKzML4OVltzTvlxV5R/QqnYnVFDeKMxpVfYhVjHYkelk++U8/+zJ+ruePCd/0AAN0e X-Received: by 2002:a17:90b:1811:b0:244:9227:5eca with SMTP id lw17-20020a17090b181100b0024492275ecamr1664990pjb.49.1680764197301; Wed, 05 Apr 2023 23:56:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680764197; cv=none; d=google.com; s=arc-20160816; b=sGrIg2NSp8Fj1mmorG1KhMN95X8VCu3OIhGzRRtmQEkciTQkn2z0rJq6vFIjQngXID 6IuI+D4LOYUeiCmZeTHsmW5hJ4W/pjJ+Wi6V+QoWd79rRneXwao8+dNqM6c0pQPV3Uq9 juhm+wvv2W7LRwVw/cYYKwMtlhAz/8hESt0r1+10l/gFP4KMlQvcHyxbeB+3s2CL/Ca6 1y/17DuXe17b+KugTDGPiZLm9YUOCf0m+SolTXIhtTjuCyJuEZ3crGgw3SAenGO1ANt6 u6tvtqYIanhWfU3lR/8JDwnHQ5AmdiBSQEwsUHi1AmjT4jgfWpwtJ1NWzr8hvAeR9vFr bHsQ== 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:dkim-signature; bh=1oUzGhhJdDKXkivHUFdL9p7gn/fPHmG5FZr6zeya3I4=; b=Xb5GA9pmtkKzmpTIIq8VQtkiN74HNOvaNNoczaGr7AaPoSlYZD2rlwuFXMHClKeDoY z/jv+8KNQBOuxDDXj0cYCKxxjpoFMB9xuv58W7P44vsdQL+2Frh9zRYzqU1IcrfKXawS vV4QnISeSeYzlJFuBvrbjT0gYv8syPp2ETDCLp6KWTXRZGV9NQygQZ1BFy5HVkbBMLtU VyQiS2Ud3Mo0LVutkjZF86nDnzJDzs3AMKUW6ObeNZChRQz4+3Cp+k2bcwxdrY2/jOgM VM3gtg83THUQIBJc+KRzK0oS6+tyJBpoXcYqCAY/I8wBsckzcl3alHMrSXIiFVAffTQB 72rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ejWK0ZtE; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g14-20020a17090a4b0e00b0023d4a3bec5csi611451pjh.161.2023.04.05.23.56.23; Wed, 05 Apr 2023 23:56:37 -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=@linuxfoundation.org header.s=korg header.b=ejWK0ZtE; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235519AbjDFGhO (ORCPT + 99 others); Thu, 6 Apr 2023 02:37:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235208AbjDFGhM (ORCPT ); Thu, 6 Apr 2023 02:37:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5725B8A6F; Wed, 5 Apr 2023 23:37:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CF68262BE3; Thu, 6 Apr 2023 06:37:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE5BAC433D2; Thu, 6 Apr 2023 06:37:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1680763030; bh=vq6kY+dugOOAccH81P3M0ANBZyroiFKbkpLxZ/jFuJE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ejWK0ZtEJPBoEPNXKh7IG/KEcweD5pQjgKfNDiyvF8hsLLomw6JghcL2BLFiiHbso JihpWmbjVhgsg0BYEJ4QABnboCa93r5zojzsSneDBUaRv3mDSAybkmIRH3fYcv01VY 1oMmfnedzjigXwQIKMXN53ASf9Hz8l/sakUWD2e0= Date: Thu, 6 Apr 2023 08:37:07 +0200 From: Greg KH To: Badhri Jagan Sridharan Cc: stern@rowland.harvard.edu, 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 Subject: Re: [PATCH v2 1/2] usb: gadget: udc: core: Invoke usb_gadget_connect only when started Message-ID: <2023040639-lair-risotto-4693@gregkh> References: <20230406062549.2461917-1-badhri@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230406062549.2461917-1-badhri@google.com> X-Spam-Status: No, score=-5.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 06, 2023 at 06:25:48AM +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->vbus, udc->started, gadget->connect, gadget->deactivate > related functions with connect_lock. usb_gadget_connect_locked, > usb_gadget_disconnect_locked, usb_udc_connect_control_locked, > usb_gadget_udc_start_locked, usb_gadget_udc_stop_locked are called with > this lock held as they can be simulataneously invoked from different code > paths. > > Adding an additional check to make sure udc is started(udc->started) > before pullup callback is invoked. > > Cc: stable@vger.kernel.org > Fixes: 628ef0d273a6 ("usb: udc: add usb_udc_vbus_handler") > Signed-off-by: Badhri Jagan Sridharan > --- > * Fixed commit message comments. > * Renamed udc_connect_control_lock to connect_lock and made it per > device. > * udc->vbus, udc->started, gadget->connect, gadget->deactivate are all > now guarded by connect_lock. > * Code now checks for udc->started to be set before invoking pullup > callback. > --- > drivers/usb/gadget/udc/core.c | 140 +++++++++++++++++++++++----------- > 1 file changed, 96 insertions(+), 44 deletions(-) > > diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c > index 3dcbba739db6..41d3a1998cff 100644 > --- a/drivers/usb/gadget/udc/core.c > +++ b/drivers/usb/gadget/udc/core.c > @@ -37,6 +37,10 @@ static struct bus_type gadget_bus_type; > * @vbus: for udcs who care about vbus status, this value is real vbus status; > * for udcs who do not care about vbus status, this value is always true > * @started: the UDC's started state. True if the UDC had started. > + * @connect_lock: protects udc->vbus, udc->started, gadget->connect, gadget->deactivate related > + * functions. usb_gadget_connect_locked, usb_gadget_disconnect_locked, > + * usb_udc_connect_control_locked, usb_gadget_udc_start_locked, usb_gadget_udc_stop_locked are > + * called with this lock held. > * > * This represents the internal data structure which is used by the UDC-class > * to hold information about udc driver and gadget together. > @@ -48,6 +52,7 @@ struct usb_udc { > struct list_head list; > bool vbus; > bool started; > + struct mutex connect_lock; > }; > > static struct class *udc_class; > @@ -687,17 +692,8 @@ int usb_gadget_vbus_disconnect(struct usb_gadget *gadget) > } > EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect); > > -/** > - * usb_gadget_connect - software-controlled connect to USB host > - * @gadget:the peripheral being connected > - * > - * Enables the D+ (or potentially D-) pullup. The host will start > - * enumerating this gadget when the pullup is active and a VBUS session > - * is active (the link is powered). > - * > - * Returns zero on success, else negative errno. > - */ > -int usb_gadget_connect(struct usb_gadget *gadget) > +/* Internal version of usb_gadget_connect needs to be called with udc_connect_control_lock held. */ Shouldn't you just use the __must_hold() marking here to document this so that the tools can properly check and validate it as well? thanks, greg k-h