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: 80062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp809949vqo; Wed, 5 Apr 2023 23:21:41 -0700 (PDT) X-Google-Smtp-Source: AKy350auQUy1ipt1qvaZS8d1j+fZaXFCPTIuZVkBy5cBS8KFKv0w6qOsrZ3BtUnDjCeyv7/YGLj5 X-Received: by 2002:a17:907:7050:b0:930:e495:b1cb with SMTP id ws16-20020a170907705000b00930e495b1cbmr4580774ejb.75.1680762101706; Wed, 05 Apr 2023 23:21:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680762101; cv=none; d=google.com; s=arc-20160816; b=mpQDZP8Sqafy9q3xe0tF08VDNx8ZwROsncj6lKDt0enVcVAqGq21bhJGECYPI6XJ3v PsIcGJ5944Pqtf32B83ROgx1nLPYOyXzHeL2n8CdRjceqyZyl2+zbjZDK+HllToNxCRs Myj02BKvZgq966ba5iWS13VmixepTp0qHJnKF3lsvXGUg4siuaTa03KgWIDKNgnnVSGo 0S7a+9DqkF/JFsnrEw06W3Y2lz6a5HaUrt7oZyGwJPh8S/YMSr/53TknntEXCiTPjEA6 5CCacgkPDFpTAK4Ggq7TLBUUXWsRKurdSmTHH5nzXCVhC5zikN6I4anD7u4rskY8K3tV 871Q== 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=JFM4H/QIcMaQK1wausxr0nbgWdj+VZJHgSjVRpLnLliDgvN2Qo+Zb4mkc1K1rgb2xu ZWbtFcTFzx0bg2tkIbzQJzMdBzbwaZPiSMASHmJHz4bHvm0uTipVhMe//DAf2Ji018Zg vonRGA2aSqn/jOk5SkElkJOp0TECVGlfUB+QCEpIhh5su/HwWyjBc6k/Ag+7bYsM7wpt LCFM+zQyk1fSdw6AuPKeSCBlrj8mRIz+Jebbf/5vnetKTu+ONUXjPCYccwsEY/F9q2mG F/ljj4naE0iSKM2F0T/Iv1NAYv80Mx9Rhn4aMDPbWYLXJn+uU0K0m6UF1VGcFV+f01j5 9Zzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=f95UCFKn; 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 jz24-20020a17090775f800b0092b5384d67dsi570918ejc.613.2023.04.05.23.21.18; Wed, 05 Apr 2023 23:21:41 -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=f95UCFKn; 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 S235171AbjDFGTN (ORCPT + 99 others); Thu, 6 Apr 2023 02:19:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235146AbjDFGTL (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 E59088A6F for ; Wed, 5 Apr 2023 23:19:09 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id g187-20020a2520c4000000b00b74680a7904so37847814ybg.15 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=4vW8pzX2NyjfFqAF0mBt74CctaPG75ivxnQX5bamMA6IekVWwcLLzw9P4qM9Zr7TVC YL/Bbz1vCY/zaw4AiT2PYROMshDhRt2MPs7mVjXPjbvWZLLCIc4npPGPCcGzbMsWQOSe KKsHyKCE9Xnp39uUL5YabOhIwt6ApoXw49yBLS9SkBbRi7/iYKOe7jWbIZ+YqWQ4qTqV dQiiyJY7jZV0wyu2Z7/1rq9I1Sf6xuhQB9YxjLqpgzJ9N7x/02xaoPs/7g4LlScgmw7a h33k2e0boFTDfd13/nEJFRk38uW2p0sbyAkKkoyj6TuaslkPTxn7fFcKcq80PqRzoKX+ 2YsQ== X-Gm-Message-State: AAQBX9fvOxCfXXB3IGeweX2yx2xczXvXqN9wzYYlzoKyhOKW2W1uQNBp B1HiAHbq/CKB+raYcBgzYxuQHNLfA+w= 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 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?1762406801548971580?= X-GMAIL-MSGID: =?utf-8?q?1762406801548971580?= 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; From patchwork Thu Apr 6 06:19:05 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: 80063 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp810044vqo; Wed, 5 Apr 2023 23:21:59 -0700 (PDT) X-Google-Smtp-Source: AKy350bCs0P0zGsXaozt9uXWGIUB23JUra85vU7R01eW8ggJMUNV+JKX9CE/awXsmt3h7XpnbIip X-Received: by 2002:a17:906:1dc7:b0:930:8fd7:789f with SMTP id v7-20020a1709061dc700b009308fd7789fmr5544550ejh.38.1680762119416; Wed, 05 Apr 2023 23:21:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680762119; cv=none; d=google.com; s=arc-20160816; b=UWSxylIIOHFTNWR8gsphapIaUzyHpreLjRm/93h2vt7SWIsizBhz4aZ1Y1b6IrVR4Q CpRr1ZLApDjpOzfRDDGSh4GyoEfPIiXtfF7Lsd3IYPbd18JaJIf70eL4fJ79/Sh9Zs4F 9zGPVpBESyux7gkzENHorgNIC1IsU30GXkue05WbDCT7g8zeZ56inr5gCzqHa5J3/lKO RUFySeTRLKUtrc6yGaaceKmxtGQ+WiLt9WVLB7SIRL7vzK+6NqCH23qngqWgontp7T1X FTMoVRKCFLQe6clDc+WwpT6jQWtdVhIpGqpo5f/bQnpHdfs6YRJqd3xBiFIl2aCs+7di 9lOQ== 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:references :mime-version:in-reply-to:date:dkim-signature; bh=Z/f7k2pCQfyKc9i/c1APoOdFbnqMktgzwaYI9MvzI7I=; b=LdyNk7GyXEN74En4todfdNtz5wzSMrLZON2S3s9hd9EM7Kl2hZQ+dUn0v74tytC7YP smzYEjezE2tEv+20gKGU90a0PPefMvKIyUQrZ2HtpsKATL+ww+Y4DFZHL+fATV+y2qa5 P3JbbEut8oMuEi7j2kmevjfXyK5piNsFGVaXnGbQXoJkswZlcFl8Hbo2IuNicQ7S6iC6 0mg9dgZGjKAOk3FUN8GhATD1AjMWOa/isWPU3VEiGu5qAB/F1BECxCCAVI1TLEBCDpUq hh10gCM84Lz0VlP24+QDFfcLW1ykF7KajWk2xhAUJMvtYK839prQZtY+6+pai3geVHPS c0JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HC1tvqen; 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 nb31-20020a1709071c9f00b0093e601a52e9si878601ejc.41.2023.04.05.23.21.35; Wed, 05 Apr 2023 23:21:59 -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=HC1tvqen; 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 S235089AbjDFGTP (ORCPT + 99 others); Thu, 6 Apr 2023 02:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235162AbjDFGTM (ORCPT ); Thu, 6 Apr 2023 02:19:12 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A46B59019 for ; Wed, 5 Apr 2023 23:19:11 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5463f2d3223so207478547b3.14 for ; Wed, 05 Apr 2023 23:19:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680761951; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Z/f7k2pCQfyKc9i/c1APoOdFbnqMktgzwaYI9MvzI7I=; b=HC1tvqen+SYVHhiRrMODrf/6S8bdwKpI+yt4teqebTIbY0JBmRRXhr20JjZXktd3I4 F66rfE2Is4Qu9p1TfNS5iCUeIomqVNB5B9pfoUwHBcCe+jCtE7iHW5NSO/+DU9mThwEk 9snHuN8YGSee2wvz2Idm7U2+wqV2Qy6ssaFNI2sxaEpMba45OPyqNDWImo6Sc/5k2KUf 9YeX2q7D783NI+HlWIJdrk66Ki4oVoLI+Yut4ZYAW8I5mOz/EnuJviU0CWGco3MFDl4j LamLj+bt8zhJrkna8q8FVBQVM9Y7sh9ay6CfAJm7DUKZTK1obcEHq/hZzxoh66/Oqtdg FuNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680761951; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Z/f7k2pCQfyKc9i/c1APoOdFbnqMktgzwaYI9MvzI7I=; b=zKQAIsDzG29sYX0VBXon2Pf2mbb1bkLbW4077S/pidUqrYWQNETfVaA8WbHcHgBUnc 4r2VpdVyMEDIEVnYDl4+bAB54LraCKcvir8Ijm6U/YFKPq3VKx3SP3+u9+lSrqwX0Srk AilZ67UCQTPBUqxYI8TGU0DljAXu7kmKznstKgnCjyNW9xNrjL3EKmuxblWWgDwurbLS LpGxd6Abe623IejApa+RdUjoJXfGlorm5XqtNP1/c3PsszSX/vOUrKqM37/lrLvDKwvB 75NMLIA0fppNPYhQMt5wVwBRwle9yrmdbuInfmRx5pi4Fv5b/7u/RwrdAKprDL0ccSLh OXhg== X-Gm-Message-State: AAQBX9c5c45QiG/KZVS6+FiNZMFqXo2U8Q8CTGmdvrED96WjK1mJqN/v 8J74wOT6X/V4iHolKpcNL6AltuvNwp8= X-Received: from badhri.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:6442]) (user=badhri job=sendgmr) by 2002:a25:cf88:0:b0:b8b:f1ac:9c6c with SMTP id f130-20020a25cf88000000b00b8bf1ac9c6cmr1399315ybg.3.1680761950871; Wed, 05 Apr 2023 23:19:10 -0700 (PDT) Date: Thu, 6 Apr 2023 06:19:05 +0000 In-Reply-To: <20230406061905.2460827-1-badhri@google.com> Mime-Version: 1.0 References: <20230406061905.2460827-1-badhri@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230406061905.2460827-2-badhri@google.com> Subject: [PATCH v1 2/2] usb: gadget: udc: core: Prevent redundant calls to pullup 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?1762406820305942908?= X-GMAIL-MSGID: =?utf-8?q?1762406820305942908?= usb_gadget_connect calls gadget->ops->pullup without checking whether gadget->connected was previously set. Make this symmetric to usb_gadget_disconnect by returning early if gadget->connected is already set. Cc: stable@vger.kernel.org Signed-off-by: Badhri Jagan Sridharan Fixes: 5a1da544e572 ("usb: gadget: core: do not try to disconnect gadget if it is not connected") --- drivers/usb/gadget/udc/core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c index 890f92cb6344..7eeaf7dbb350 100644 --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -708,6 +708,9 @@ int usb_gadget_connect(struct usb_gadget *gadget) goto out; } + if (gadget->connected) + goto out; + if (gadget->deactivated) { /* * If gadget is deactivated we only save new state.