Virtuozzo QEMU development (svt-core@virtuozzo.com)
 help / color / mirror / Atom feed
From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
To: svt-core@virtuozzo.com
Cc: den@openvz.org, andrey.drobyshev@virtuozzo.com
Subject: [QEMU HCI-8.0 PATCH v4 1/5] vhost-blk: improve vhost_blk_device_realize cleanup
Date: Tue,  8 Sep 2026 15:52:04 +0300	[thread overview]
Message-ID: <20260908125208.278402-2-andrey.zhadchenko@virtuozzo.com> (raw)
In-Reply-To: <20260908125208.278402-1-andrey.zhadchenko@virtuozzo.com>

vhost_dev_init() does a lot of unexpected things:
 - closes the backend fd on failure
 - memsets dev as 0 (thus clearing dev->vqs)

Cleanup properly: don't double-close vhostfd, factor out vqs
so we do not leak it during cleanup.

Fixes: 0ff3cb5ff2 ("block: add vhost-blk backend")
https://virtuozzo.atlassian.net/browse/VSTOR-143437
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
v2:
 - old patch dropped

v3:
 - extend the double-close fix to preserve and free vqs after
vhost_dev_init() clears dev

 hw/block/vhost-blk.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index c52851fcf8..20c2f41691 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -237,6 +237,7 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VHostBlk *s = VHOST_BLK(vdev);
     VhostBlkConf *conf = &s->conf;
+    struct vhost_virtqueue *vqs;
     int i, ret;
 
     if (!conf->conf.blk) {
@@ -289,7 +290,8 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
 
     s->dev.nvqs = conf->num_queues;
     s->dev.max_queues = conf->num_queues;
-    s->dev.vqs = g_new0(struct vhost_virtqueue, s->dev.nvqs);
+    vqs = g_new0(struct vhost_virtqueue, s->dev.nvqs);
+    s->dev.vqs = vqs;
     s->dev.vq_index = 0;
 
     virtio_init(vdev, VIRTIO_ID_BLOCK, sizeof(struct virtio_blk_config));
@@ -318,6 +320,8 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
     if (ret < 0) {
         error_setg(errp, "vhost-blk: vhost initialization failed: %s",
                 strerror(-ret));
+        /* vhost_dev_init() closes vhostfd on failure */
+        s->vhostfd = -1;
         goto cleanup;
     }
 
@@ -327,8 +331,11 @@ cleanup:
     if (s->mighand) {
             qemu_del_vm_change_state_handler(s->mighand);
     }
-    g_free(s->dev.vqs);
-    close(s->vhostfd);
+    g_free(vqs);
+    if (s->vhostfd >= 0) {
+        close(s->vhostfd);
+        s->vhostfd = -1;
+    }
     for (i = 0; i < conf->num_queues; i++) {
         virtio_del_queue(vdev, i);
     }
@@ -470,6 +477,7 @@ static void vhost_blk_instance_init(Object *obj)
 {
     VHostBlk *s = VHOST_BLK(obj);
 
+    s->vhostfd = -1;
     device_add_bootindex_property(obj, &s->conf.conf.bootindex,
                                   "bootindex", "/disk@0,0",
                                   DEVICE(obj));
-- 
2.43.5


  reply	other threads:[~2026-09-08 12:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-08 12:52 [QEMU HCI-8.0 PATCH v4 0/5] vhost-blk change backend setup Andrey Zhadchenko
2026-09-08 12:52 ` Andrey Zhadchenko [this message]
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 2/5] vhost-blk: " Andrey Zhadchenko
2026-09-08 13:34   ` Andrey Drobyshev
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 3/5] vhost-blk: add read-only flag Andrey Zhadchenko
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 4/5] vhost-blk: watch the device for resize events Andrey Zhadchenko
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 5/5] vhost-blk: preserve the uevent socket across cpr-exec Andrey Zhadchenko
2026-09-08 13:34   ` Andrey Drobyshev
2026-09-08 13:34 ` [QEMU HCI-8.0 PATCH v4 0/5] vhost-blk change backend setup Andrey Drobyshev

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=20260908125208.278402-2-andrey.zhadchenko@virtuozzo.com \
    --to=andrey.zhadchenko@virtuozzo.com \
    --cc=andrey.drobyshev@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=svt-core@virtuozzo.com \
    /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