Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp780275rwb; Mon, 26 Sep 2022 05:54:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM45QM0yzsNOfza5WjRZV3liQuCVJ/+hGF390C5JYwIN4DunhZuF+zuqCiKj21Nz9+VHjpZl X-Received: by 2002:a05:6a00:8cb:b0:52c:6962:2782 with SMTP id s11-20020a056a0008cb00b0052c69622782mr23547800pfu.81.1664196884134; Mon, 26 Sep 2022 05:54:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664196884; cv=none; d=google.com; s=arc-20160816; b=hzoT/cIXFCaCPoDCn2Jdv3EQL36y/78lQfX6SJpEBbJ3MCw35vjJ39/kDwr6T4AWNa s/0gD1f0nLm4OvNcFo0DKQ5KD7B8Ma+IyPnSlMUM7+ox1rttRkVNGyx6OKpSepRZJ8rt M5RBkMaJ4CYvu2uuNvtSsSm5TttDfkwT2nk8THGlD3TKEc78NJh0yJoAzbsom4gmxvxf 3xoa/maJTi3uX+EvutB5EDDzchbo80DAL3WCphmJIDwhW5wDaYR8Z1H9FExBYC4YEVFY Wnp+1zIcVG9CF/ZH7vzkxuq79S5m6daL83dK4+d2ns30Y6YynfugtnQCzz82TacfaXdK JnOQ== 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=6pSJmtIWDxiqMiy744dtYqHElXd80VE8wy7LAE5tBDQ=; b=Cv8IvRijXVhXLycUODTEq5H57zZdMcCnqj8n1Kgz3dDANBvfFPKQUxegWlWiRZD/99 ZalASHDN3KBwEFqpKbrS3w8NJGg8KdsUX302LJbM55G15iCb7GJCH7SVuKXqEBQjl9ih 5mg2ABv75O6cqk5+NAP456DxwKm4ybwvqTd66GWRqm4eRmBVhLrI2lEVslaph7n956++ B0UVNwY/ykY7QNF+oQ6jQZQknIHNu24yRK0Q7Uld2gw+4zKqtR239SLrtt9yBhcmKQlN A5OhUbzWwrXdsD+0yqSsfCM+IpPmGHQhgyCxOOsjBURpPrbn959BqOfMwvKzDMZgkw7c 7YMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="PBIk/4Ak"; 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 e14-20020a63544e000000b0043447486c84si17283289pgm.875.2022.09.26.05.54.31; Mon, 26 Sep 2022 05:54:44 -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="PBIk/4Ak"; 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 S237378AbiIZLKP (ORCPT + 99 others); Mon, 26 Sep 2022 07:10:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237411AbiIZLII (ORCPT ); Mon, 26 Sep 2022 07:08:08 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF7AA5FF56; Mon, 26 Sep 2022 03:34:29 -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 ams.source.kernel.org (Postfix) with ESMTPS id 40D60B80942; Mon, 26 Sep 2022 10:34:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91E8CC433D6; Mon, 26 Sep 2022 10:34:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664188467; bh=N3t1Fk/3q7GjP5+7SHbkjr93tAC2v25ZVbxLpcJQ0sM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PBIk/4AkRF3XVB3Nf/5q7zpebIPiX3VQed/iepNI0g9IPF2yEi5o4DHER3dOQ9l/D jl9ZoX3JGk6ZLP0zg+Opoe0quQ/XLc+j1J7azRjkHMDFiU3ig7tjDy5JCnrinKwRvm dkzMkwbW4Y83BCC1kGVs7WIvTqRqi4O5jU1X68Ag= 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.15 005/148] usb: dwc3: gadget: Avoid starting DWC3 gadget during UDC unbind Date: Mon, 26 Sep 2022 12:10:39 +0200 Message-Id: <20220926100756.259989964@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100756.074519146@linuxfoundation.org> References: <20220926100756.074519146@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 fd5d42ec5350..dbae57725f52 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -1028,6 +1028,7 @@ struct dwc3_scratchpad_array { * @tx_fifo_resize_max_num: max number of fifos allocated during txfifo resize * @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 @@ -1247,6 +1248,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 322754a7f91c..5f9a0ab09f4b 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2442,7 +2442,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. @@ -4421,7 +4421,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