[PATCH v4 06/11] nvmem: imx-ocotp-ele: Use scope-based cleanup helpers in imx_ocotp_reg_read()

Frieder Schrempf <[email protected]>
Newsgroups dev.linux.lists.imx,org.infradead.lists.linux-arm-kernel,org.kernel.vger.linux-devicetree,org.kernel.vger.linux-kernel
Message-ID <20260727-upstreaming-next-20260609-imx-ocotp-ele-v4-6-1a515de33093@kontron.de>
From: Frieder Schrempf <[email protected]>

This simplifies the code and makes sure that buffers are cleared and
locks are released at the end of the function scope.

Signed-off-by: Frieder Schrempf <[email protected]>
---
 drivers/nvmem/imx-ocotp-ele.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/nvmem/imx-ocotp-ele.c b/drivers/nvmem/imx-ocotp-ele.c
index 9d3f94e35508..010b53001323 100644
--- a/drivers/nvmem/imx-ocotp-ele.c
+++ b/drivers/nvmem/imx-ocotp-ele.c
@@ -5,6 +5,7 @@
  * Copyright 2023 NXP
  */
 
+#include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/io.h>
 #include <linux/module.h>
@@ -71,7 +72,6 @@ static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, siz
 	u32 count, index, num_bytes;
 	enum fuse_type type;
 	u32 *buf;
-	void *p;
 	int i;
 	u8 skipbytes;
 
@@ -83,11 +83,11 @@ static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, siz
 	num_bytes = round_up(bytes + skipbytes, 4);
 	count = num_bytes >> 2;
 
-	p = kzalloc(num_bytes, GFP_KERNEL);
+	void *p __free(kfree) = kzalloc(num_bytes, GFP_KERNEL);
 	if (!p)
 		return -ENOMEM;
 
-	mutex_lock(&priv->lock);
+	guard(mutex)(&priv->lock);
 
 	buf = p;
 
@@ -106,10 +106,6 @@ static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, siz
 
 	memcpy(val, ((u8 *)p) + skipbytes, bytes);
 
-	mutex_unlock(&priv->lock);
-
-	kfree(p);
-
 	return 0;
 };
 

-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.