[PATCH v2 11/25] vhost-user-blk: move initial reconnect loop to separate function

Vladimir Sementsov-Ogievskiy posted 25 patches 4 weeks, 1 day ago
[PATCH v2 11/25] vhost-user-blk: move initial reconnect loop to separate function
Posted by Vladimir Sementsov-Ogievskiy 4 weeks, 1 day ago
Simplify _realize function, and prepare to further changes.

While being here, also rename virtio_err: label to more generic
fail:, virtio_err doesn't improve readability here.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 hw/block/vhost-user-blk.c | 54 +++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 22 deletions(-)

diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c
index 9c727c3977..36e32229ad 100644
--- a/hw/block/vhost-user-blk.c
+++ b/hw/block/vhost-user-blk.c
@@ -489,14 +489,40 @@ static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
     return 0;
 }
 
-static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
+static int vhost_user_blk_realize_connect_loop(VHostUserBlk *s, Error **errp)
 {
     ERRP_GUARD();
+    DeviceState *dev = DEVICE(s);
+    int ret, retries = VU_REALIZE_CONN_RETRIES;
+
+    assert(!*errp);
+    do {
+        if (*errp) {
+            error_prepend(errp, "Reconnecting after error: ");
+            error_report_err(*errp);
+            *errp = NULL;
+        }
+        ret = vhost_user_blk_realize_connect(s, errp);
+    } while (ret < 0 && retries--);
+
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* we're fully initialized, now we can operate, so add the handler */
+    qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL,
+                             vhost_user_blk_event, NULL, (void *)dev,
+                             NULL, true);
+
+    return 0;
+}
+
+static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
+{
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VHostUserBlk *s = VHOST_USER_BLK(vdev);
     size_t config_size;
-    int retries;
-    int i, ret;
+    int i;
 
     trace_vhost_user_blk_device_realize_in(vdev);
 
@@ -540,31 +566,15 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
     s->inflight = g_new0(struct vhost_inflight, 1);
     s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
 
-    retries = VU_REALIZE_CONN_RETRIES;
-    assert(!*errp);
-    do {
-        if (*errp) {
-            error_prepend(errp, "Reconnecting after error: ");
-            error_report_err(*errp);
-            *errp = NULL;
-        }
-        ret = vhost_user_blk_realize_connect(s, errp);
-    } while (ret < 0 && retries--);
-
-    if (ret < 0) {
-        goto virtio_err;
+    if (vhost_user_blk_realize_connect_loop(s, errp) < 0) {
+        goto fail;
     }
 
-    /* we're fully initialized, now we can operate, so add the handler */
-    qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL,
-                             vhost_user_blk_event, NULL, (void *)dev,
-                             NULL, true);
-
     trace_vhost_user_blk_device_realize_out(vdev);
 
     return;
 
-virtio_err:
+fail:
     g_free(s->vhost_vqs);
     s->vhost_vqs = NULL;
     g_free(s->inflight);
-- 
2.48.1
Re: [PATCH v2 11/25] vhost-user-blk: move initial reconnect loop to separate function
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:46 AM Vladimir Sementsov-Ogievskiy
<vsementsov@yandex-team.ru> wrote:
>
> Simplify _realize function, and prepare to further changes.
>
> While being here, also rename virtio_err: label to more generic
> fail:, virtio_err doesn't improve readability here.
>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  hw/block/vhost-user-blk.c | 54 +++++++++++++++++++++++----------------
>  1 file changed, 32 insertions(+), 22 deletions(-)
>
> diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c
> index 9c727c3977..36e32229ad 100644
> --- a/hw/block/vhost-user-blk.c
> +++ b/hw/block/vhost-user-blk.c
> @@ -489,14 +489,40 @@ static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
>      return 0;
>  }
>
> -static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
> +static int vhost_user_blk_realize_connect_loop(VHostUserBlk *s, Error **errp)
>  {
>      ERRP_GUARD();
> +    DeviceState *dev = DEVICE(s);
> +    int ret, retries = VU_REALIZE_CONN_RETRIES;
> +
> +    assert(!*errp);
> +    do {
> +        if (*errp) {
> +            error_prepend(errp, "Reconnecting after error: ");
> +            error_report_err(*errp);
> +            *errp = NULL;
> +        }
> +        ret = vhost_user_blk_realize_connect(s, errp);
> +    } while (ret < 0 && retries--);
> +
> +    if (ret < 0) {
> +        return ret;
> +    }
> +
> +    /* we're fully initialized, now we can operate, so add the handler */
> +    qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL,
> +                             vhost_user_blk_event, NULL, (void *)dev,
> +                             NULL, true);
> +
> +    return 0;
> +}
> +
> +static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
> +{
>      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
>      VHostUserBlk *s = VHOST_USER_BLK(vdev);
>      size_t config_size;
> -    int retries;
> -    int i, ret;
> +    int i;
>
>      trace_vhost_user_blk_device_realize_in(vdev);
>
> @@ -540,31 +566,15 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
>      s->inflight = g_new0(struct vhost_inflight, 1);
>      s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
>
> -    retries = VU_REALIZE_CONN_RETRIES;
> -    assert(!*errp);
> -    do {
> -        if (*errp) {
> -            error_prepend(errp, "Reconnecting after error: ");
> -            error_report_err(*errp);
> -            *errp = NULL;
> -        }
> -        ret = vhost_user_blk_realize_connect(s, errp);
> -    } while (ret < 0 && retries--);
> -
> -    if (ret < 0) {
> -        goto virtio_err;
> +    if (vhost_user_blk_realize_connect_loop(s, errp) < 0) {
> +        goto fail;
>      }
>
> -    /* we're fully initialized, now we can operate, so add the handler */
> -    qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL,
> -                             vhost_user_blk_event, NULL, (void *)dev,
> -                             NULL, true);
> -
>      trace_vhost_user_blk_device_realize_out(vdev);
>
>      return;
>
> -virtio_err:
> +fail:
>      g_free(s->vhost_vqs);
>      s->vhost_vqs = NULL;
>      g_free(s->inflight);
> --
> 2.48.1
>
>