From patchwork Wed Apr 5 09:31:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Badhri Jagan Sridharan X-Patchwork-Id: 79533 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp177025vqo; Wed, 5 Apr 2023 02:50:00 -0700 (PDT) X-Google-Smtp-Source: AKy350YYTksWSkMRvGEfKS+qE0OKuxHc+xTB6+ksKa5NmJjURUrzsMTSTW+s7Ll3KjkBXvLPu98S X-Received: by 2002:aa7:9639:0:b0:628:bf87:54c1 with SMTP id r25-20020aa79639000000b00628bf8754c1mr5937038pfg.6.1680688200583; Wed, 05 Apr 2023 02:50:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680688200; cv=none; d=google.com; s=arc-20160816; b=X31aCYfC9YNoykBpcEgPkRJTKAyvhnepCxeccfKTpjUV32JKxK3im7ogdN226nVpTE E7x0Dp7/h33C8YyJlypHai6Sg7MtPQc6VlEczZc0z0RUm6NxnrmfrY5sA/XmzJBbAFHq CJDvfn+QgIxppj4zW0ylmWMQwY7+hktqkNUtBCKTrSLL991NgSOtNtKyIFtBYOPGnD5G LEauxHuyPNco0d3Bpo+7XyWNWTCbFfdjn1jDYX/S13emh1doIHCLrtsxoMk9gkwugtug 5okEQ3BoQAn2xMTSQT64cP4u4qvTxW6XmiVYj8Znv+eRsMmkozOi3LzJ2qj5s0E9PDW1 FNlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=yBhMce5MJnmp23KM1dsJQjjtpNOxg2YPVBV4k7AdjEQ=; b=IMkzV7f5xbFiwFGZBQbjfyzrPXqZhbfJqPjFEr4UE0LQuNzrvbJoZBVGqd4/fMsMH7 EXOadInfK2QOkQ7hnGraiq8hvce+FXqkh6mWAHjuu42KNct2E66UCh/VNUwJejodvwTq TdCeXOQcPUdz2KOH/wAN/RnoKees+Ql/qgePehJd9QNtHbSqZJ1i+QtwWEnxbrHUtINC c1pziTKo0dG5z71XMjrxbJkYkgAdG0jTSpnuVsCK2SKDaJ9iwkzXQ7rztuYpStbOAeS3 J1kleVRyMlsvIbItVCKVD/dvMWnad2bLsc3NW5sXqBXAUHBemO9WeY3xSzI5cezdsaWR yEag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aKsY5zNF; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k29-20020aa79d1d000000b00623d0b5b8c4si12080092pfp.115.2023.04.05.02.49.48; Wed, 05 Apr 2023 02:50:00 -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=aKsY5zNF; 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 S237700AbjDEJco (ORCPT + 99 others); Wed, 5 Apr 2023 05:32:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237497AbjDEJc1 (ORCPT ); Wed, 5 Apr 2023 05:32:27 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 632F85273 for ; Wed, 5 Apr 2023 02:31:51 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e186-20020a2537c3000000b00b72501acf50so35078262yba.20 for ; Wed, 05 Apr 2023 02:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680687096; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yBhMce5MJnmp23KM1dsJQjjtpNOxg2YPVBV4k7AdjEQ=; b=aKsY5zNFDmGcT2oLevWC/U6nVR8VVMOStPWNb4b9XzcOaVjjp+bwM1RXUgrG4WynDQ EAxvcUnxeK8linTrOHXJ23Neiwz4JAipa8sCiG5FwsjKyZnSg4nLhAAeeHaeGvIkyxBN 0vxBwJODeOm8+XNgsAemb9wyJOCmS1+BVmwQwIj7dxnUwF+JhVy0qD0yKI1q8aiR2Bfp BqOqXQFOrEOccYY5wWn/4RG6V5hIFL7gced4oTznR9QP98pyNMjTO50nnFeJpqBjnJWN tHmMzJjz+MdOldH4zur6+Mfj77JQM81yJGOVtkzG2xueaSaWjstQgEmhlCbHFgtozBqT 4ihA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680687096; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yBhMce5MJnmp23KM1dsJQjjtpNOxg2YPVBV4k7AdjEQ=; b=mk+dH3ceoNKIUOUjYOIrjf2lBYHdUTRXIQTK7QHn+Tkq0CsCwOsFRqb/tGrW5nz2jp p2RPbU9PXnp71CPbUXKRqmsehnOKwzJx67/HRUKf2l6iLV25/63AkGTw3ZxLtgtavj+W Tk+LjWYR5tzUQLIkTBb02QPO0h8wy2alUVtHm7SCeG7Hc0Ex0ojSqsyah2ORcnHpMo6n aO3IDqKCvKBHs206cn0OYfbasTOU5l7vjXFEI2keIQaT/Tyh304L5dcPFV3Hs+Dh571m hS2++/8wVEQiJhitLxjYyBN1ZMQPa+mw0oADAhDwLLl1V8c1j37T2vqc+exbyY8e86ma dz7w== X-Gm-Message-State: AAQBX9dEO/NdNkzUdVu/+z81jt80fZS2Yc5uDPgiOz2ZYezVTZkdiYjg V29i20LmHwOBPKl0ii8k9tFAmmmzLJ4= X-Received: from badhri.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:6442]) (user=badhri job=sendgmr) by 2002:a25:da4e:0:b0:a02:a3a6:78fa with SMTP id n75-20020a25da4e000000b00a02a3a678famr3137102ybf.12.1680687096140; Wed, 05 Apr 2023 02:31:36 -0700 (PDT) Date: Wed, 5 Apr 2023 09:31:32 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230405093133.1858140-1-badhri@google.com> Subject: [PATCH v1 1/2] usb: gadget: udc: core: Invoke usb_gadget_connect only when started From: Badhri Jagan Sridharan To: gregkh@linuxfoundation.org, 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 Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Badhri Jagan Sridharan X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762329310547065585?= X-GMAIL-MSGID: =?utf-8?q?1762329310547065585?= 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") --- drivers/usb/gadget/udc/core.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) base-commit: d629c0e221cd99198b843d8351a0a9bfec6c0423 diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c index 3dcbba739db6..890f92cb6344 100644 --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -56,6 +56,8 @@ static LIST_HEAD(udc_list); /* Protects udc_list, udc->driver, driver->is_bound, and related calls */ static DEFINE_MUTEX(udc_lock); +/* Protects udc->vbus, udc-started and udc_connect_control_locked */ +static DEFINE_MUTEX(udc_connect_control_lock); /* ------------------------------------------------------------------------- */ /** @@ -1078,9 +1080,10 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state); /* ------------------------------------------------------------------------- */ -static void usb_udc_connect_control(struct usb_udc *udc) +/* Acquire udc_connect_control_lock before calling this function. */ +static void usb_udc_connect_control_locked(struct usb_udc *udc) { - if (udc->vbus) + if (udc->vbus && udc->started) usb_gadget_connect(udc->gadget); else usb_gadget_disconnect(udc->gadget); @@ -1099,10 +1102,12 @@ void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status) { struct usb_udc *udc = gadget->udc; + mutex_lock(&udc_connect_control_lock); if (udc) { udc->vbus = status; - usb_udc_connect_control(udc); + usb_udc_connect_control_locked(udc); } + mutex_unlock(&udc_connect_control_lock); } EXPORT_SYMBOL_GPL(usb_udc_vbus_handler); @@ -1140,14 +1145,18 @@ static inline int usb_gadget_udc_start(struct usb_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 = udc->gadget->ops->udc_start(udc->gadget, udc->driver); if (!ret) udc->started = true; + usb_udc_connect_control_locked(udc); + mutex_unlock(&udc_connect_control_lock); return ret; } @@ -1165,13 +1174,17 @@ static inline int usb_gadget_udc_start(struct usb_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 = 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); kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); return 0;