lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250128055241.30760-1-oushixiong1025@163.com>
Date: Tue, 28 Jan 2025 13:52:41 +0800
From: oushixiong1025@....com
To: Amit Shah <amit@...nel.org>
Cc: Arnd Bergmann <arnd@...db.de>,
	Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
	virtualization@...ts.linux.dev,
	linux-kernel@...r.kernel.org,
	Shixiong Ou <oushixiong@...inos.cn>
Subject: [PATCH] virtio_console: Convert to use devm funcs

From: Shixiong Ou <oushixiong@...inos.cn>

Convert to devm_* funcs so that no need to manual free in error path.

Signed-off-by: Shixiong Ou <oushixiong@...inos.cn>
---
 drivers/char/virtio_console.c | 43 ++++++++++++++---------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index c62b208b42f1..657cf15dad55 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -1811,15 +1811,17 @@ static int init_vqs(struct ports_device *portdev)
 	nr_ports = portdev->max_nr_ports;
 	nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
 
-	vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
-	vqs_info = kcalloc(nr_queues, sizeof(*vqs_info), GFP_KERNEL);
-	portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
-					GFP_KERNEL);
-	portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
-					 GFP_KERNEL);
+	vqs = devm_kmalloc_array(&portdev->vdev->dev, nr_queues,
+				 sizeof(struct virtqueue *), GFP_KERNEL);
+	vqs_info = devm_kcalloc(&portdev->vdev->dev, nr_queues,
+				sizeof(*vqs_info), GFP_KERNEL);
+	portdev->in_vqs = devm_kmalloc_array(&portdev->vdev->dev,
+					     nr_ports, sizeof(struct virtqueue *), GFP_KERNEL);
+	portdev->out_vqs = devm_kmalloc_array(&portdev->vdev->dev,
+					      nr_ports, sizeof(struct virtqueue *), GFP_KERNEL);
 	if (!vqs || !vqs_info || !portdev->in_vqs || !portdev->out_vqs) {
 		err = -ENOMEM;
-		goto free;
+		return err;
 	}
 
 	/*
@@ -1850,7 +1852,7 @@ static int init_vqs(struct ports_device *portdev)
 	/* Find the queues. */
 	err = virtio_find_vqs(portdev->vdev, nr_queues, vqs, vqs_info, NULL);
 	if (err)
-		goto free;
+		return err;
 
 	j = 0;
 	portdev->in_vqs[0] = vqs[0];
@@ -1866,18 +1868,10 @@ static int init_vqs(struct ports_device *portdev)
 			portdev->out_vqs[i] = vqs[j + 1];
 		}
 	}
-	kfree(vqs_info);
-	kfree(vqs);
+	devm_kfree(&portdev->vdev->dev, vqs_info);
+	devm_kfree(&portdev->vdev->dev, vqs);
 
 	return 0;
-
-free:
-	kfree(portdev->out_vqs);
-	kfree(portdev->in_vqs);
-	kfree(vqs_info);
-	kfree(vqs);
-
-	return err;
 }
 
 static const struct file_operations portdev_fops = {
@@ -1897,8 +1891,8 @@ static void remove_vqs(struct ports_device *portdev)
 		cond_resched();
 	}
 	portdev->vdev->config->del_vqs(portdev->vdev);
-	kfree(portdev->in_vqs);
-	kfree(portdev->out_vqs);
+	devm_kfree(&portdev->vdev->dev, portdev->in_vqs);
+	devm_kfree(&portdev->vdev->dev, portdev->out_vqs);
 }
 
 static void virtcons_remove(struct virtio_device *vdev)
@@ -1941,7 +1935,6 @@ static void virtcons_remove(struct virtio_device *vdev)
 	 * away.
 	 */
 	remove_vqs(portdev);
-	kfree(portdev);
 }
 
 /*
@@ -1967,7 +1960,7 @@ static int virtcons_probe(struct virtio_device *vdev)
 		return -EINVAL;
 	}
 
-	portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
+	portdev = devm_kmalloc(&vdev->dev, sizeof(*portdev), GFP_KERNEL);
 	if (!portdev) {
 		err = -ENOMEM;
 		goto fail;
@@ -1984,7 +1977,7 @@ static int virtcons_probe(struct virtio_device *vdev)
 			"Error %d registering chrdev for device %u\n",
 			portdev->chr_major, vdev->index);
 		err = portdev->chr_major;
-		goto free;
+		goto fail;
 	}
 
 	multiport = false;
@@ -2001,7 +1994,7 @@ static int virtcons_probe(struct virtio_device *vdev)
 				"Invalidate max_nr_ports %d",
 				portdev->max_nr_ports);
 			err = -EINVAL;
-			goto free;
+			goto fail;
 		}
 		multiport = true;
 	}
@@ -2060,8 +2053,6 @@ static int virtcons_probe(struct virtio_device *vdev)
 
 free_chrdev:
 	unregister_chrdev(portdev->chr_major, "virtio-portsdev");
-free:
-	kfree(portdev);
 fail:
 	return err;
 }
-- 
2.43.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ