[PATCH] backends/vhost-user: fix memory leak when vhost_dev_init() fails

Stefano Garzarella posted 1 patch 2 weeks, 3 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261105162443.232929-1-sgarzare@redhat.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>
backends/vhost-user.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH] backends/vhost-user: fix memory leak when vhost_dev_init() fails
Posted by Stefano Garzarella 2 weeks, 3 days ago
From: Stefano Garzarella <sgarzare@redhat.com>

vhost_dev_init() clears the struct vhost_dev in the error path calling
vhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a
local reference to the vhost_virtqueue array and deallocate it in case
of failure, just as most other callers of vhost_dev_init() do.

Discovered while reviewing a patch by Alexandr Moshkov fixing the
same issue in vhost-user-fs.

Fixes: f24bb7287ddb ("Add vhost-user-backend")
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 backends/vhost-user.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/backends/vhost-user.c b/backends/vhost-user.c
index de6b1ba267..a3cd15a57e 100644
--- a/backends/vhost-user.c
+++ b/backends/vhost-user.c
@@ -24,6 +24,7 @@ int
 vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
                             unsigned nvqs, Error **errp)
 {
+    struct vhost_virtqueue *vqs;
     int ret;
 
     assert(!b->vdev && vdev);
@@ -32,13 +33,16 @@ vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
         return -1;
     }
 
+    vqs = g_new0(struct vhost_virtqueue, nvqs);
+
     b->vdev = vdev;
     b->dev.nvqs = nvqs;
-    b->dev.vqs = g_new0(struct vhost_virtqueue, nvqs);
+    b->dev.vqs = vqs;
 
     ret = vhost_dev_init(&b->dev, &b->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
                          errp);
     if (ret < 0) {
+        g_free(vqs);
         return -1;
     }
 
-- 
2.56.0
Re: [PATCH] backends/vhost-user: fix memory leak when vhost_dev_init() fails
Posted by Alexandr Moshkov 1 week, 6 days ago
On 11/5/26 21:24, Stefano Garzarella wrote:
> From: Stefano Garzarella <sgarzare@redhat.com>
>
> vhost_dev_init() clears the struct vhost_dev in the error path calling
> vhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a
> local reference to the vhost_virtqueue array and deallocate it in case
> of failure, just as most other callers of vhost_dev_init() do.
>
> Discovered while reviewing a patch by Alexandr Moshkov fixing the
> same issue in vhost-user-fs.
>
> Fixes: f24bb7287ddb ("Add vhost-user-backend")
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---

Reviewed-by: Alexandr Moshkov <dtalexundeer@yandex-team.ru>


Best regards,

Alexandr
Re: [PATCH] backends/vhost-user: fix memory leak when vhost_dev_init() fails
Posted by Luigi Leonardi 2 weeks, 2 days ago
On Thu, Nov 05, 2026 at 05:24:43PM +0100, Stefano Garzarella wrote:
>From: Stefano Garzarella <sgarzare@redhat.com>
>
>vhost_dev_init() clears the struct vhost_dev in the error path calling
>vhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a
>local reference to the vhost_virtqueue array and deallocate it in case
>of failure, just as most other callers of vhost_dev_init() do.
>
>Discovered while reviewing a patch by Alexandr Moshkov fixing the
>same issue in vhost-user-fs.
>
>Fixes: f24bb7287ddb ("Add vhost-user-backend")
>Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
>---
>

Reviewed-by: Luigi Leonardi <leonardi@redhat.com>

Thanks,
Luigi