[PATCH] staging: greybus: Use guard(mutex) in receive_data()

Matvey Oborotov posted 1 patch 1 month, 1 week ago
drivers/staging/greybus/raw.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
[PATCH] staging: greybus: Use guard(mutex) in receive_data()
Posted by Matvey Oborotov 1 month, 1 week ago
Replace manual mutex_lock/unlock with guard(mutex) in raw.c
receive_data(). This automates lock release on function exit, ensures
that lock is released on early returns, and makes the code cleaner.

Signed-off-by: Matvey Oborotov <oborotovmatvey@gmail.com>
---
 drivers/staging/greybus/raw.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/drivers/staging/greybus/raw.c b/drivers/staging/greybus/raw.c
index 3027a2c25bcd..6c31bae0d8ea 100644
--- a/drivers/staging/greybus/raw.c
+++ b/drivers/staging/greybus/raw.c
@@ -59,34 +59,28 @@ static int receive_data(struct gb_raw *raw, u32 len, u8 *data)
 {
 	struct raw_data *raw_data;
 	struct device *dev = &raw->connection->bundle->dev;
-	int retval = 0;
 
 	if (len > MAX_PACKET_SIZE) {
 		dev_err(dev, "Too big of a data packet, rejected\n");
 		return -EINVAL;
 	}
 
-	mutex_lock(&raw->list_lock);
+	guard(mutex)(&raw->list_lock);
 	if ((raw->list_data + len) > MAX_DATA_SIZE) {
 		dev_err(dev, "Too much data in receive buffer, now dropping packets\n");
-		retval = -EINVAL;
-		goto exit;
+		return -EINVAL;
 	}
 
 	raw_data = kmalloc_flex(*raw_data, data, len);
-	if (!raw_data) {
-		retval = -ENOMEM;
-		goto exit;
-	}
+	if (!raw_data)
+		return -ENOMEM;
 
 	raw->list_data += len;
 	raw_data->len = len;
 	memcpy(&raw_data->data[0], data, len);
 
 	list_add_tail(&raw_data->entry, &raw->list);
-exit:
-	mutex_unlock(&raw->list_lock);
-	return retval;
+	return 0;
 }
 
 static int gb_raw_request_handler(struct gb_operation *op)
-- 
2.43.0
Re: [PATCH] staging: greybus: Use guard(mutex) in receive_data()
Posted by Greg Kroah-Hartman 1 month, 1 week ago
On Wed, Feb 25, 2026 at 03:30:06PM +0500, Matvey Oborotov wrote:
> Replace manual mutex_lock/unlock with guard(mutex) in raw.c
> receive_data(). This automates lock release on function exit, ensures
> that lock is released on early returns, and makes the code cleaner.
> 
> Signed-off-by: Matvey Oborotov <oborotovmatvey@gmail.com>
> ---
>  drivers/staging/greybus/raw.c | 16 +++++-----------
>  1 file changed, 5 insertions(+), 11 deletions(-)

Please only use guard() logic with new code, or if you are fixing a bug.
Making changes like this to existing code is not needed at all, it just
causes unwanted churn for no good reason.

sorry,

greg k-h