From patchwork Thu Apr 6 06:19:04 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: 13202881 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B4B5C76196 for ; Thu, 6 Apr 2023 06:19:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233235AbjDFGTM (ORCPT ); Thu, 6 Apr 2023 02:19:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234713AbjDFGTL (ORCPT ); Thu, 6 Apr 2023 02:19:11 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E49C48A68 for ; Wed, 5 Apr 2023 23:19:09 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e145-20020a255097000000b00b8bf8ac12e9so745478ybb.20 for ; Wed, 05 Apr 2023 23:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680761949; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yBhMce5MJnmp23KM1dsJQjjtpNOxg2YPVBV4k7AdjEQ=; b=f95UCFKn0uTCbjQCWEXcq7AdTnSUiCnb/MbtUrtqIyUocE6lFMHuZSKtyzDtH3J2MJ zPpUQhZ8QUk/rHLV4sISAlNJzO5VtsRsROsNTA5E96PHrGGjT7qceMIE8FyjnQShkxOg hwLIC93bWqAE7MQxWglfHo71HBWsdThRaBGoV5va6sY+IbKam7YDkJGmXRASxnNZsBrI bE/MDSpbGjcUvhy6zAfQA6v9DITyCxipViW5mXoOgPcm2P+DvpBISv1btzoK+3J6sCKM FVieDNpMD4J2YDkyJQHgYPVxpurh3mCKd01/jKrc14JBOeQmzDZQREQfxbHhUny8tOLs XDIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680761949; 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=W6Br8koFxUNvN1rrNuc36eG96TlEI1b2GWca1aSPeH4PrvGN4fl3AuAVXyBo53k2AO O9Y0FRNAtpxOGsI5FypYlihHW+StMxVMeHNrgC1txZNy2tuIpxgerMwKSSSI4s7spgNy mnugB9+YlhQOdyAh0RNCKdDJbFZ3imgCio8TyPzDykhImbu157o2cZh0CStvJpUwGgqw pACfcRmkWMyRt8sTBacnEYlcmEuldYYBO2zxw0EI5G4E4jBMtnvVkHvUXli8QRx8vKdk AImbAHp4jAcwYrBc1x/Ip17HH4VX8QPh0vg5yyeZX0y0WAZIgsQ7G715lqCgmDwUQfd4 3EEA== X-Gm-Message-State: AAQBX9cKEbFKWO6ys2EZka/O6P5ygSFKkQX9iNyI9I7fypB/VAd2Al0R JWe9chxt+beQ7hCuI3PsOVcgAUHGZFk= X-Google-Smtp-Source: AKy350Zo04k7Ro9p6QXCkkCo0nTUklSt9cX0wzh9BfIVX4oI2g65uM6b3SUtF6OXma7tzCQPwu/2G9y1AfE= X-Received: from badhri.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:6442]) (user=badhri job=sendgmr) by 2002:a81:b389:0:b0:545:5b2c:4bf6 with SMTP id r131-20020a81b389000000b005455b2c4bf6mr5083031ywh.7.1680761949204; Wed, 05 Apr 2023 23:19:09 -0700 (PDT) Date: Thu, 6 Apr 2023 06:19:04 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230406061905.2460827-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 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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;