drivers/char/virtio_console.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
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
On Mon, Aug 04, 2025 at 05:00:20PM +0530, Abinash Singh wrote: > This refactors the 'alloc_buf' function to replace traditional goto-based > error handling with scope-based cleanup helpers. > > No functional changes intended. Please only do this in new code being submitted, no need to change any existing code unless it is needed. > Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> > --- > drivers/char/virtio_console.c | 14 +++++--------- > 1 file changed, 5 insertions(+), 9 deletions(-) Did you test this? thanks, greg k-h
© 2016 - 2025 Red Hat, Inc.