Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp802105rwb; Mon, 26 Sep 2022 06:08:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM49UGPMiQ4IpXR84EYxaCCxhVfM/VUF+wctjVx51EnK+2VEwJu7tgkeROJAfYYsiEK9PeR6 X-Received: by 2002:a17:906:591:b0:73d:c3ef:84ae with SMTP id 17-20020a170906059100b0073dc3ef84aemr18468390ejn.155.1664197702070; Mon, 26 Sep 2022 06:08:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664197702; cv=none; d=google.com; s=arc-20160816; b=CSpRAZoWKkA3BBXmu0kX31pvXwRinTGIwfA4bSSlxOEmGVw2s3DLgdRy9U1mBs/5nb zr9Lim6COzWIlWXr5vjjOuigPIjEXlCM0nnxjvvQmUgplyjtxQ3GxpNPCoYeAHPAh2uC 2+/nKl2IyT1YO1M/A+ssDIq1U2a4RO0tFeBbdsO7ZRJSifoKHmMrtQMhcKbg34091xqP gPwOLrtVC/a+Q6rtQ+zGIM+1dOIwo0yo76noN9cjdsB12/4AdpH0MXReLQaRhlxr2Lwn diNUTFoU1OHmnErdu/GQc2mSppUiuN2YgnvAYrbQYEVimRpjCbyrh4sVrQjTOgNw1BX2 voRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wOe0PRGiqNkTZrnn6V8xenYgR4h1D8tEizwaLuNluxo=; b=zSA8IxCCIO2R8cxhMwIZ2JBhhuO+UfzfgaOST11x6VtCzd+zfJay1f+8Glk/ovFqBY Spm2hrXGAbaXH8aT4ott7/kW/57qvBDpjqkPdH7udP0flDmqTH8cGgg3GyRe4DXIajfk qgyJPJohIiqB3nMpr9nLppRT6YTTi3a9tZT2q8LVM7wC1a27JKBloS7i9osmBl8gu24p JD/QmbRGnp1S4PIOYeaMT6IiyIsPlkRQbqN80LhvrD8KA3XJzvb34jqkIIypVS4uD/+q YxIzGTUlhPLVVfyGAya0qLWjeCKRZpgpqbqaWou1FGciVJ+tkwNSzJ9vtM0FlhV4l6iT RXrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=i0iTH3Hp; 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 s7-20020a50ab07000000b00446dc8f5e44si15756048edc.243.2022.09.26.06.07.37; Mon, 26 Sep 2022 06:08:22 -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=i0iTH3Hp; 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 S235646AbiIZKhA (ORCPT + 99 others); Mon, 26 Sep 2022 06:37:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236269AbiIZKec (ORCPT ); Mon, 26 Sep 2022 06:34:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 818644AD5F; Mon, 26 Sep 2022 03:21:34 -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 91EEF60AD6; Mon, 26 Sep 2022 10:21:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 84272C4314C; Mon, 26 Sep 2022 10:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664187693; bh=hySRG2UVRTaeI4Q4gk6xmE/6JOw27WgqZsbbmA+cNNM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i0iTH3HpZBLkFVLzKE9UFVrQHWc5WyvfajLWkMFqtDgUVAiG/yLwOMpd/btQ0xaEc 9h+ujXZBIfhhULU+uuU+05rm28Yhsqz8+Mkj67ikx+xMDq4qFnoQ9xmBAv/7Dpb3YV dxN21RfQEo9GqVPNfmAwT2BvynUDtQezwbmkHaao= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Felipe Balbi , Wesley Cheng , Sasha Levin Subject: [PATCH 5.4 029/120] usb: dwc3: gadget: Avoid starting DWC3 gadget during UDC unbind Date: Mon, 26 Sep 2022 12:11:02 +0200 Message-Id: <20220926100751.719085566@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100750.519221159@linuxfoundation.org> References: <20220926100750.519221159@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 From: Wesley Cheng [ Upstream commit 8217f07a50236779880f13e87f99224cd9117f83 ] There is a race present where the DWC3 runtime resume runs in parallel to the UDC unbind sequence. This will eventually lead to a possible scenario where we are enabling the run/stop bit, without a valid composition defined. Thread#1 (handling UDC unbind): usb_gadget_remove_driver() -->usb_gadget_disconnect() -->dwc3_gadget_pullup(0) --> continue UDC unbind sequence -->Thread#2 is running in parallel here Thread#2 (handing next cable connect) __dwc3_set_mode() -->pm_runtime_get_sync() -->dwc3_gadget_resume() -->dwc->gadget_driver is NOT NULL yet -->dwc3_gadget_run_stop(1) --> _dwc3gadget_start() ... Fix this by tracking the pullup disable routine, and avoiding resuming of the DWC3 gadget. Once the UDC is re-binded, that will trigger the pullup enable routine, which would handle enabling the DWC3 gadget. Acked-by: Felipe Balbi Signed-off-by: Wesley Cheng Link: https://lore.kernel.org/r/20210917021852.2037-1-wcheng@codeaurora.org Signed-off-by: Greg Kroah-Hartman Stable-dep-of: 040f2dbd2010 ("usb: dwc3: gadget: Avoid duplicate requests to enable Run/Stop") Signed-off-by: Sasha Levin --- drivers/usb/dwc3/core.h | 2 ++ drivers/usb/dwc3/gadget.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index da296f888f45..a93c61bc5a7d 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -994,6 +994,7 @@ struct dwc3_scratchpad_array { * @tx_max_burst_prd: max periodic ESS transmit burst size * @hsphy_interface: "utmi" or "ulpi" * @connected: true when we're connected to a host, false otherwise + * @softconnect: true when gadget connect is called, false when disconnect runs * @delayed_status: true when gadget driver asks for delayed status * @ep0_bounced: true when we used bounce buffer * @ep0_expect_in: true when we expect a DATA IN transfer @@ -1196,6 +1197,7 @@ struct dwc3 { const char *hsphy_interface; unsigned connected:1; + unsigned softconnect:1; unsigned delayed_status:1; unsigned ep0_bounced:1; unsigned ep0_expect_in:1; diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 80fee7ea83ca..079919b85d87 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2015,7 +2015,7 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) int ret; is_on = !!is_on; - + dwc->softconnect = is_on; /* * Per databook, when we want to stop the gadget, if a control transfer * is still in process, complete it and get the core into setup phase. @@ -3791,7 +3791,7 @@ int dwc3_gadget_resume(struct dwc3 *dwc) { int ret; - if (!dwc->gadget_driver) + if (!dwc->gadget_driver || !dwc->softconnect) return 0; ret = __dwc3_gadget_start(dwc); -- 2.35.1