[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250804113020.620352-1-abinashsinghlalotra@gmail.com>
Date: Mon, 4 Aug 2025 17:00:20 +0530
From: Abinash Singh <abinashsinghlalotra@...il.com>
To: amit@...nel.org
Cc: arnd@...db.de,
gregkh@...uxfoundation.org,
virtualization@...ts.linux.dev,
linux-kernel@...r.kernel.org,
Abinash Singh <abinashsinghlalotra@...il.com>
Subject: [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers
This refactors the 'alloc_buf' function to replace traditional goto-based
error handling with scope-based cleanup helpers.
No functional changes intended.
Signed-off-by: Abinash Singh <abinashsinghlalotra@...il.com>
---
drivers/char/virtio_console.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index 088182e54deb..9875f5f07b32 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -27,6 +27,7 @@
#include <linux/module.h>
#include <linux/dma-mapping.h>
#include <linux/string_choices.h>
+#include <linux/cleanup.h>
#include "../tty/hvc/hvc_console.h"
#define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
@@ -404,7 +405,7 @@ static void reclaim_dma_bufs(void)
static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
int pages)
{
- struct port_buffer *buf;
+ struct port_buffer *buf __free(kfree) = NULL;
reclaim_dma_bufs();
@@ -414,7 +415,7 @@ static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size
*/
buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
if (!buf)
- goto fail;
+ return NULL;
buf->sgpages = pages;
if (pages > 0) {
@@ -432,7 +433,7 @@ static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size
*/
buf->dev = vdev->dev.parent;
if (!buf->dev)
- goto free_buf;
+ return NULL;
/* Increase device refcnt to avoid freeing it */
get_device(buf->dev);
@@ -444,16 +445,11 @@ static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size
}
if (!buf->buf)
- goto free_buf;
+ return NULL;
buf->len = 0;
buf->offset = 0;
buf->size = buf_size;
return buf;
-
-free_buf:
- kfree(buf);
-fail:
- return NULL;
}
/* Callers should take appropriate locks */
--
2.50.1
Powered by blists - more mailing lists