Linux virtualization list
 help / color / mirror / Atom feed
* [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers
@ 2025-08-04 11:30 Abinash Singh
  2025-08-04 16:50 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Abinash Singh @ 2025-08-04 11:30 UTC (permalink / raw)
  To: amit; +Cc: arnd, gregkh, virtualization, linux-kernel, Abinash Singh

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers
  2025-08-04 11:30 [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers Abinash Singh
@ 2025-08-04 16:50 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2025-08-04 16:50 UTC (permalink / raw)
  To: Abinash Singh; +Cc: amit, arnd, virtualization, linux-kernel

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-04 16:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-04 11:30 [PATCH] drivers/char/virtio_console : refactor resource cleanup to use scope-based helpers Abinash Singh
2025-08-04 16:50 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox