public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
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 1/2] usb: gadget: udc: simplify udc_lock holding using guards
Date: Mon, 20 Apr 2026 11:57:21 +0800	[thread overview]
Message-ID: <20260420035722.1133418-2-lihuisong@huawei.com> (raw)
In-Reply-To: <20260420035722.1133418-1-lihuisong@huawei.com>

Use guard() and scoped_guard() to manage udc_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 | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index e8861eaad907..e3f27fb39e9e 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1614,14 +1614,13 @@ static int gadget_bind_driver(struct device *dev)
 			struct usb_gadget_driver, driver);
 	int ret = 0;
 
-	mutex_lock(&udc_lock);
-	if (driver->is_bound) {
-		mutex_unlock(&udc_lock);
-		return -ENXIO;		/* Driver binds to only one gadget */
+	scoped_guard(mutex, &udc_lock) {
+		/* Driver binds to only one gadget */
+		if (driver->is_bound)
+			return -ENXIO;
+		driver->is_bound = true;
+		udc->driver = driver;
 	}
-	driver->is_bound = true;
-	udc->driver = driver;
-	mutex_unlock(&udc_lock);
 
 	dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function);
 
@@ -1664,10 +1663,9 @@ static int gadget_bind_driver(struct device *dev)
 		dev_err(&udc->dev, "failed to start %s: %d\n",
 			driver->function, ret);
 
-	mutex_lock(&udc_lock);
+	guard(mutex)(&udc_lock);
 	udc->driver = NULL;
 	driver->is_bound = false;
-	mutex_unlock(&udc_lock);
 
 	return ret;
 }
@@ -1695,10 +1693,10 @@ static void gadget_unbind_driver(struct device *dev)
 	usb_gadget_udc_stop_locked(udc);
 	mutex_unlock(&udc->connect_lock);
 
-	mutex_lock(&udc_lock);
-	driver->is_bound = false;
-	udc->driver = NULL;
-	mutex_unlock(&udc_lock);
+	scoped_guard(mutex, &udc_lock) {
+		driver->is_bound = false;
+		udc->driver = NULL;
+	}
 
 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
 }
-- 
2.33.0


  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 ` Huisong Li [this message]
2026-04-20  3:57 ` [PATCH v1 2/2] usb: gadget: udc: simplify connect_lock " Huisong Li
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-2-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