[PATCH v2 03/25] vhost: rework vhost_virtqueue_init()

Vladimir Sementsov-Ogievskiy posted 25 patches 4 weeks, 1 day ago
[PATCH v2 03/25] vhost: rework vhost_virtqueue_init()
Posted by Vladimir Sementsov-Ogievskiy 4 weeks, 1 day ago
We are going to split vhost_dev_init() so that the first part will do
early initialization of QEMU structures, but don't communicate with
backend, and the second part will do backend communication. We need
this for future support for backend-transfer migration support for
vhost-user-blk (backend will not be available in the early
initialization point).

With this commit, let's split vhost_virtqueue_init(). The whole function
is mostly about configuring the backend, so this logic will be postponed
until backend become available. The only thing to keep in early
initialization is attaching vhost_dev structure. Let's simply move it to
vhost_dev_init().

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 hw/virtio/vhost.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index 551d1687fc..1998663461 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -1500,9 +1500,9 @@ static void vhost_virtqueue_error_notifier(EventNotifier *n)
     }
 }
 
-static int vhost_virtqueue_init(struct vhost_dev *dev,
-                                struct vhost_virtqueue *vq, int n)
+static int vhost_virtqueue_connect(struct vhost_virtqueue *vq, int n)
 {
+    struct vhost_dev *dev = vq->dev;
     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
     struct vhost_vring_file file = {
         .index = vhost_vq_index,
@@ -1519,8 +1519,6 @@ static int vhost_virtqueue_init(struct vhost_dev *dev,
         goto fail_call;
     }
 
-    vq->dev = dev;
-
     if (dev->vhost_ops->vhost_set_vring_err) {
         r = event_notifier_init(&vq->error_notifier, 0);
         if (r < 0) {
@@ -1629,6 +1627,10 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     hdev->migration_blocker = NULL;
     hdev->busyloop_timeout = busyloop_timeout;
 
+    for (i = 0; i < hdev->nvqs; ++i) {
+        hdev->vqs[i].dev = hdev;
+    }
+
     r = vhost_set_backend_type(hdev, backend_type);
     assert(r >= 0);
 
@@ -1680,7 +1682,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     }
 
     for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
-        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
+        r = vhost_virtqueue_connect(hdev->vqs + i, hdev->vq_index + i);
         if (r < 0) {
             error_setg_errno(errp, -r, "Failed to initialize virtqueue %d", i);
             goto fail;
-- 
2.48.1
Re: [PATCH v2 03/25] vhost: rework vhost_virtqueue_init()
Posted by Raphael Norwitz 3 weeks, 3 days ago
Reviewed-by: Raphael Norwitz <raphael.s.norwitz@gmail.com>


On Thu, Oct 16, 2025 at 7:48 AM Vladimir Sementsov-Ogievskiy
<vsementsov@yandex-team.ru> wrote:
>
> We are going to split vhost_dev_init() so that the first part will do
> early initialization of QEMU structures, but don't communicate with
> backend, and the second part will do backend communication. We need
> this for future support for backend-transfer migration support for
> vhost-user-blk (backend will not be available in the early
> initialization point).
>
> With this commit, let's split vhost_virtqueue_init(). The whole function
> is mostly about configuring the backend, so this logic will be postponed

nit: "until the backend becomes"

> until backend become available. The only thing to keep in early
> initialization is attaching vhost_dev structure. Let's simply move it to
> vhost_dev_init().
>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  hw/virtio/vhost.c | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
> index 551d1687fc..1998663461 100644
> --- a/hw/virtio/vhost.c
> +++ b/hw/virtio/vhost.c
> @@ -1500,9 +1500,9 @@ static void vhost_virtqueue_error_notifier(EventNotifier *n)
>      }
>  }
>
> -static int vhost_virtqueue_init(struct vhost_dev *dev,
> -                                struct vhost_virtqueue *vq, int n)
> +static int vhost_virtqueue_connect(struct vhost_virtqueue *vq, int n)
>  {
> +    struct vhost_dev *dev = vq->dev;
>      int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
>      struct vhost_vring_file file = {
>          .index = vhost_vq_index,
> @@ -1519,8 +1519,6 @@ static int vhost_virtqueue_init(struct vhost_dev *dev,
>          goto fail_call;
>      }
>
> -    vq->dev = dev;
> -
>      if (dev->vhost_ops->vhost_set_vring_err) {
>          r = event_notifier_init(&vq->error_notifier, 0);
>          if (r < 0) {
> @@ -1629,6 +1627,10 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
>      hdev->migration_blocker = NULL;
>      hdev->busyloop_timeout = busyloop_timeout;
>
> +    for (i = 0; i < hdev->nvqs; ++i) {
> +        hdev->vqs[i].dev = hdev;
> +    }
> +
>      r = vhost_set_backend_type(hdev, backend_type);
>      assert(r >= 0);
>
> @@ -1680,7 +1682,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
>      }
>
>      for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
> -        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
> +        r = vhost_virtqueue_connect(hdev->vqs + i, hdev->vq_index + i);
>          if (r < 0) {
>              error_setg_errno(errp, -r, "Failed to initialize virtqueue %d", i);
>              goto fail;
> --
> 2.48.1
>
>