From: Abinash Singh <abinashsinghlalotra@gmail.com>
To: amit@kernel.org
Cc: arnd@arndb.de, gregkh@linuxfoundation.org,
virtualization@lists.linux.dev, linux-kernel@vger.kernel.org,
Abinash Singh <abinashsinghlalotra@gmail.com>
Subject: [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers
Date: Mon, 4 Aug 2025 17:00:20 +0530 [thread overview]
Message-ID: <20250804113020.620352-1-abinashsinghlalotra@gmail.com> (raw)
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@gmail.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
next reply other threads:[~2025-08-04 11:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-04 11:30 Abinash Singh [this message]
2025-08-04 16:50 ` [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers Greg KH
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=20250804113020.620352-1-abinashsinghlalotra@gmail.com \
--to=abinashsinghlalotra@gmail.com \
--cc=amit@kernel.org \
--cc=arnd@arndb.de \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=virtualization@lists.linux.dev \
/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