From: Huisong Li <lihuisong@huawei.com>
To: <gregkh@linuxfoundation.org>, <khtsai@google.com>,
<hhhuuu@google.com>, <kees@kernel.org>,
<kexinsun@smail.nju.edu.cn>
Cc: <linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>,
<linuxarm@huawei.com>, <zhanjie9@hisilicon.com>,
<lihuisong@huawei.com>
Subject: [PATCH v1 2/2] usb: gadget: udc: simplify connect_lock holding using guards
Date: Mon, 20 Apr 2026 11:57:22 +0800 [thread overview]
Message-ID: <20260420035722.1133418-3-lihuisong@huawei.com> (raw)
In-Reply-To: <20260420035722.1133418-1-lihuisong@huawei.com>
Use guard() and scoped_guard() to manage connect_lock to simplify the
code in gadget_bind_driver() and gadget_unbind_driver().
Signed-off-by: Huisong Li <lihuisong@huawei.com>
---
drivers/usb/gadget/udc/core.c | 53 ++++++++++++++++-------------------
1 file changed, 24 insertions(+), 29 deletions(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index e3f27fb39e9e..1d615a870907 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1630,31 +1630,26 @@ static int gadget_bind_driver(struct device *dev)
if (ret)
goto err_bind;
- mutex_lock(&udc->connect_lock);
- ret = usb_gadget_udc_start_locked(udc);
- if (ret) {
- mutex_unlock(&udc->connect_lock);
- goto err_start;
+ scoped_guard(mutex, &udc->connect_lock) {
+ ret = usb_gadget_udc_start_locked(udc);
+ if (ret)
+ goto err_start;
+ usb_gadget_enable_async_callbacks(udc);
+ udc->allow_connect = true;
+ ret = usb_udc_connect_control_locked(udc);
+ if (ret) {
+ udc->allow_connect = false;
+ usb_gadget_disable_async_callbacks(udc);
+ if (gadget->irq)
+ synchronize_irq(gadget->irq);
+ usb_gadget_udc_stop_locked(udc);
+ goto err_start;
+ }
}
- usb_gadget_enable_async_callbacks(udc);
- udc->allow_connect = true;
- ret = usb_udc_connect_control_locked(udc);
- if (ret)
- goto err_connect_control;
-
- mutex_unlock(&udc->connect_lock);
kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
return 0;
- err_connect_control:
- udc->allow_connect = false;
- usb_gadget_disable_async_callbacks(udc);
- if (gadget->irq)
- synchronize_irq(gadget->irq);
- usb_gadget_udc_stop_locked(udc);
- mutex_unlock(&udc->connect_lock);
-
err_start:
driver->unbind(udc->gadget);
@@ -1680,18 +1675,18 @@ static void gadget_unbind_driver(struct device *dev)
udc->allow_connect = false;
cancel_work_sync(&udc->vbus_work);
- mutex_lock(&udc->connect_lock);
- usb_gadget_disconnect_locked(gadget);
- usb_gadget_disable_async_callbacks(udc);
- if (gadget->irq)
- synchronize_irq(gadget->irq);
- mutex_unlock(&udc->connect_lock);
+ scoped_guard(mutex, &udc->connect_lock) {
+ usb_gadget_disconnect_locked(gadget);
+ usb_gadget_disable_async_callbacks(udc);
+ if (gadget->irq)
+ synchronize_irq(gadget->irq);
+ }
udc->driver->unbind(gadget);
- mutex_lock(&udc->connect_lock);
- usb_gadget_udc_stop_locked(udc);
- mutex_unlock(&udc->connect_lock);
+ scoped_guard(mutex, &udc->connect_lock) {
+ usb_gadget_udc_stop_locked(udc);
+ }
scoped_guard(mutex, &udc_lock) {
driver->is_bound = false;
--
2.33.0
next prev parent reply other threads:[~2026-04-20 3:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-20 3:57 [PATCH v1 0/2] usb: gadget: udc: simplify lock holding using guards Huisong Li
2026-04-20 3:57 ` [PATCH v1 1/2] usb: gadget: udc: simplify udc_lock " Huisong Li
2026-04-20 3:57 ` Huisong Li [this message]
2026-04-20 5:40 ` [PATCH v1 0/2] usb: gadget: udc: simplify lock " Greg KH
2026-04-20 6:21 ` lihuisong (C)
2026-04-20 6:30 ` Greg KH
2026-04-20 6:56 ` lihuisong (C)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260420035722.1133418-3-lihuisong@huawei.com \
--to=lihuisong@huawei.com \
--cc=gregkh@linuxfoundation.org \
--cc=hhhuuu@google.com \
--cc=kees@kernel.org \
--cc=kexinsun@smail.nju.edu.cn \
--cc=khtsai@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=linuxarm@huawei.com \
--cc=zhanjie9@hisilicon.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox