diff mbox series

[v3,04/26] vhost-user: simplify vhost_user_init/vhost_user_cleanup

Message ID 20180618161729.334-5-marcandre.lureau@redhat.com
State New
Headers show
Series vhost-user for input & GPU | expand

Commit Message

Marc-André Lureau June 18, 2018, 4:17 p.m. UTC
Take a VhostUserState* that can be pre-allocated, and initialize it
with the associated chardev.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/hw/virtio/vhost-user-blk.h  |  2 +-
 include/hw/virtio/vhost-user-scsi.h |  2 +-
 include/hw/virtio/vhost-user.h      |  2 +-
 backends/cryptodev-vhost-user.c     | 18 ++++--------------
 hw/block/vhost-user-blk.c           | 22 ++++------------------
 hw/scsi/vhost-user-scsi.c           | 20 ++++----------------
 hw/virtio/vhost-stub.c              |  2 +-
 hw/virtio/vhost-user.c              | 16 ++++++++++++----
 net/vhost-user.c                    |  8 ++++----
 9 files changed, 32 insertions(+), 60 deletions(-)

Comments

Tiwei Bie June 21, 2018, 12:33 p.m. UTC | #1
On Mon, Jun 18, 2018 at 06:17:07PM +0200, Marc-André Lureau wrote:
[...]
> diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c
> index 049089b5e2..323dfcc46a 100644
> --- a/hw/virtio/vhost-stub.c
> +++ b/hw/virtio/vhost-stub.c
> @@ -7,7 +7,7 @@ bool vhost_has_free_slot(void)
>      return true;
>  }
>  
> -VhostUserState *vhost_user_init(void)
> +bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
>  {
>      return NULL;

It would be better to return false.

Best regards,
Tiwei Bie

>  }
[...]
Marc-André Lureau June 21, 2018, 12:48 p.m. UTC | #2
On Thu, Jun 21, 2018 at 2:33 PM, Tiwei Bie <tiwei.bie@intel.com> wrote:
> On Mon, Jun 18, 2018 at 06:17:07PM +0200, Marc-André Lureau wrote:
> [...]
>> diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c
>> index 049089b5e2..323dfcc46a 100644
>> --- a/hw/virtio/vhost-stub.c
>> +++ b/hw/virtio/vhost-stub.c
>> @@ -7,7 +7,7 @@ bool vhost_has_free_slot(void)
>>      return true;
>>  }
>>
>> -VhostUserState *vhost_user_init(void)
>> +bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
>>  {
>>      return NULL;
>
> It would be better to return false.
>

Good catch, fixed.

Except that, would you give a reviewed-by?

> Best regards,
> Tiwei Bie
>
>>  }
> [...]
>
Tiwei Bie June 21, 2018, 1:27 p.m. UTC | #3
On Thu, Jun 21, 2018 at 02:48:08PM +0200, Marc-André Lureau wrote:
> On Thu, Jun 21, 2018 at 2:33 PM, Tiwei Bie <tiwei.bie@intel.com> wrote:
> > On Mon, Jun 18, 2018 at 06:17:07PM +0200, Marc-André Lureau wrote:
> > [...]
> >> diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c
> >> index 049089b5e2..323dfcc46a 100644
> >> --- a/hw/virtio/vhost-stub.c
> >> +++ b/hw/virtio/vhost-stub.c
> >> @@ -7,7 +7,7 @@ bool vhost_has_free_slot(void)
> >>      return true;
> >>  }
> >>
> >> -VhostUserState *vhost_user_init(void)
> >> +bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
> >>  {
> >>      return NULL;
> >
> > It would be better to return false.
> >
> 
> Good catch, fixed.
> 
> Except that, would you give a reviewed-by?

Sorry, I missed something previously. In below diff:

diff --git a/net/vhost-user.c b/net/vhost-user.c
index a39f9c9974..5e5b8f3fc9 100644
--- a/net/vhost-user.c
+++ b/net/vhost-user.c
@@ -291,14 +291,14 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
 {
     Error *err = NULL;
     NetClientState *nc, *nc0 = NULL;
-    VhostUserState *user = NULL;
     NetVhostUserState *s = NULL;
+    VhostUserState *user;
     int i;
 
     assert(name);
     assert(queues > 0);
 
-    user = vhost_user_init();
+    user = g_new0(struct VhostUserState, 1);
     if (!user) {
         error_report("failed to init vhost_user");
         goto err;

It might be better to change the error message
to something like:

"failed to allocate vhost-user state".

Except that, it looks good to me and you can add
my reviewed-by.

Best regards,
Tiwei Bie

> 
> > Best regards,
> > Tiwei Bie
> >
> >>  }
> > [...]
> >
> 
> 
> 
> -- 
> Marc-André Lureau
Marc-André Lureau June 26, 2018, 12:24 p.m. UTC | #4
Hi

On Thu, Jun 21, 2018 at 3:27 PM, Tiwei Bie <tiwei.bie@intel.com> wrote:
> On Thu, Jun 21, 2018 at 02:48:08PM +0200, Marc-André Lureau wrote:
>> On Thu, Jun 21, 2018 at 2:33 PM, Tiwei Bie <tiwei.bie@intel.com> wrote:
>> > On Mon, Jun 18, 2018 at 06:17:07PM +0200, Marc-André Lureau wrote:
>> > [...]
>> >> diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c
>> >> index 049089b5e2..323dfcc46a 100644
>> >> --- a/hw/virtio/vhost-stub.c
>> >> +++ b/hw/virtio/vhost-stub.c
>> >> @@ -7,7 +7,7 @@ bool vhost_has_free_slot(void)
>> >>      return true;
>> >>  }
>> >>
>> >> -VhostUserState *vhost_user_init(void)
>> >> +bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
>> >>  {
>> >>      return NULL;
>> >
>> > It would be better to return false.
>> >
>>
>> Good catch, fixed.
>>
>> Except that, would you give a reviewed-by?
>
> Sorry, I missed something previously. In below diff:
>
> diff --git a/net/vhost-user.c b/net/vhost-user.c
> index a39f9c9974..5e5b8f3fc9 100644
> --- a/net/vhost-user.c
> +++ b/net/vhost-user.c
> @@ -291,14 +291,14 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
>  {
>      Error *err = NULL;
>      NetClientState *nc, *nc0 = NULL;
> -    VhostUserState *user = NULL;
>      NetVhostUserState *s = NULL;
> +    VhostUserState *user;
>      int i;
>
>      assert(name);
>      assert(queues > 0);
>
> -    user = vhost_user_init();
> +    user = g_new0(struct VhostUserState, 1);
>      if (!user) {
>          error_report("failed to init vhost_user");
>          goto err;
>
> It might be better to change the error message
> to something like:
>
> "failed to allocate vhost-user state".
>
> Except that, it looks good to me and you can add
> my reviewed-by.

Good point, g_new() aborts on failure, so we can simply remove the error report.

Thanks

>
> Best regards,
> Tiwei Bie
>
>>
>> > Best regards,
>> > Tiwei Bie
>> >
>> >>  }
>> > [...]
>> >
>>
>>
>>
>> --
>> Marc-André Lureau
diff mbox series

Patch

diff --git a/include/hw/virtio/vhost-user-blk.h b/include/hw/virtio/vhost-user-blk.h
index d52944aeeb..a8a106eecb 100644
--- a/include/hw/virtio/vhost-user-blk.h
+++ b/include/hw/virtio/vhost-user-blk.h
@@ -36,7 +36,7 @@  typedef struct VHostUserBlk {
     uint32_t queue_size;
     uint32_t config_wce;
     struct vhost_dev dev;
-    VhostUserState *vhost_user;
+    VhostUserState vhost_user;
 } VHostUserBlk;
 
 #endif
diff --git a/include/hw/virtio/vhost-user-scsi.h b/include/hw/virtio/vhost-user-scsi.h
index 3ec34ae867..7c22bdc957 100644
--- a/include/hw/virtio/vhost-user-scsi.h
+++ b/include/hw/virtio/vhost-user-scsi.h
@@ -31,7 +31,7 @@ 
 typedef struct VHostUserSCSI {
     VHostSCSICommon parent_obj;
     uint64_t host_features;
-    VhostUserState *vhost_user;
+    VhostUserState vhost_user;
 } VHostUserSCSI;
 
 #endif /* VHOST_USER_SCSI_H */
diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h
index fd660393a0..811e325f42 100644
--- a/include/hw/virtio/vhost-user.h
+++ b/include/hw/virtio/vhost-user.h
@@ -22,7 +22,7 @@  typedef struct VhostUserState {
     VhostUserHostNotifier notifier[VIRTIO_QUEUE_MAX];
 } VhostUserState;
 
-VhostUserState *vhost_user_init(void);
+bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
 void vhost_user_cleanup(VhostUserState *user);
 
 #endif
diff --git a/backends/cryptodev-vhost-user.c b/backends/cryptodev-vhost-user.c
index d52daccfcd..63573e211e 100644
--- a/backends/cryptodev-vhost-user.c
+++ b/backends/cryptodev-vhost-user.c
@@ -47,7 +47,7 @@ 
 typedef struct CryptoDevBackendVhostUser {
     CryptoDevBackend parent_obj;
 
-    VhostUserState *vhost_user;
+    VhostUserState vhost_user;
     CharBackend chr;
     char *chr_name;
     bool opened;
@@ -104,7 +104,7 @@  cryptodev_vhost_user_start(int queues,
             continue;
         }
 
-        options.opaque = s->vhost_user;
+        options.opaque = &s->vhost_user;
         options.backend_type = VHOST_BACKEND_TYPE_USER;
         options.cc = b->conf.peers.ccs[i];
         s->vhost_crypto[i] = cryptodev_vhost_init(&options);
@@ -187,7 +187,6 @@  static void cryptodev_vhost_user_init(
     size_t i;
     Error *local_err = NULL;
     Chardev *chr;
-    VhostUserState *user;
     CryptoDevBackendClient *cc;
     CryptoDevBackendVhostUser *s =
                       CRYPTODEV_BACKEND_VHOST_USER(backend);
@@ -218,15 +217,10 @@  static void cryptodev_vhost_user_init(
         }
     }
 
-    user = vhost_user_init();
-    if (!user) {
-        error_setg(errp, "Failed to init vhost_user");
+    if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
         return;
     }
 
-    user->chr = &s->chr;
-    s->vhost_user = user;
-
     qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
                      cryptodev_vhost_user_event, NULL, s, NULL, true);
 
@@ -312,11 +306,7 @@  static void cryptodev_vhost_user_cleanup(
         }
     }
 
-    if (s->vhost_user) {
-        vhost_user_cleanup(s->vhost_user);
-        g_free(s->vhost_user);
-        s->vhost_user = NULL;
-    }
+    vhost_user_cleanup(&s->vhost_user);
 }
 
 static void cryptodev_vhost_user_set_chardev(Object *obj,
diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c
index d755223643..5a2c6fe5f3 100644
--- a/hw/block/vhost-user-blk.c
+++ b/hw/block/vhost-user-blk.c
@@ -224,7 +224,6 @@  static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VHostUserBlk *s = VHOST_USER_BLK(vdev);
-    VhostUserState *user;
     int i, ret;
 
     if (!s->chardev.chr) {
@@ -242,15 +241,10 @@  static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    user = vhost_user_init();
-    if (!user) {
-        error_setg(errp, "vhost-user-blk: failed to init vhost_user");
+    if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
         return;
     }
 
-    user->chr = &s->chardev;
-    s->vhost_user = user;
-
     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
                 sizeof(struct virtio_blk_config));
 
@@ -266,7 +260,7 @@  static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
 
     vhost_dev_set_config_notifier(&s->dev, &blk_ops);
 
-    ret = vhost_dev_init(&s->dev, s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
+    ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
     if (ret < 0) {
         error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
                    strerror(-ret));
@@ -291,10 +285,7 @@  vhost_err:
 virtio_err:
     g_free(s->dev.vqs);
     virtio_cleanup(vdev);
-
-    vhost_user_cleanup(user);
-    g_free(user);
-    s->vhost_user = NULL;
+    vhost_user_cleanup(&s->vhost_user);
 }
 
 static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
@@ -306,12 +297,7 @@  static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
     vhost_dev_cleanup(&s->dev);
     g_free(s->dev.vqs);
     virtio_cleanup(vdev);
-
-    if (s->vhost_user) {
-        vhost_user_cleanup(s->vhost_user);
-        g_free(s->vhost_user);
-        s->vhost_user = NULL;
-    }
+    vhost_user_cleanup(&s->vhost_user);
 }
 
 static void vhost_user_blk_instance_init(Object *obj)
diff --git a/hw/scsi/vhost-user-scsi.c b/hw/scsi/vhost-user-scsi.c
index 9355cfdf07..b620725bc2 100644
--- a/hw/scsi/vhost-user-scsi.c
+++ b/hw/scsi/vhost-user-scsi.c
@@ -69,7 +69,6 @@  static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
     VHostUserSCSI *s = VHOST_USER_SCSI(dev);
     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
-    VhostUserState *user;
     Error *err = NULL;
     int ret;
 
@@ -86,30 +85,24 @@  static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    user = vhost_user_init();
-    if (!user) {
-        error_setg(errp, "vhost-user-scsi: failed to init vhost_user");
+    if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
         return;
     }
-    user->chr = &vs->conf.chardev;
 
     vsc->dev.nvqs = 2 + vs->conf.num_queues;
     vsc->dev.vqs = g_new(struct vhost_virtqueue, vsc->dev.nvqs);
     vsc->dev.vq_index = 0;
     vsc->dev.backend_features = 0;
 
-    ret = vhost_dev_init(&vsc->dev, user,
+    ret = vhost_dev_init(&vsc->dev, &s->vhost_user,
                          VHOST_BACKEND_TYPE_USER, 0);
     if (ret < 0) {
         error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
                    strerror(-ret));
-        vhost_user_cleanup(user);
-        g_free(user);
+        vhost_user_cleanup(&s->vhost_user);
         return;
     }
 
-    s->vhost_user = user;
-
     /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
     vsc->channel = 0;
     vsc->lun = 0;
@@ -129,12 +122,7 @@  static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
     g_free(vsc->dev.vqs);
 
     virtio_scsi_common_unrealize(dev, errp);
-
-    if (s->vhost_user) {
-        vhost_user_cleanup(s->vhost_user);
-        g_free(s->vhost_user);
-        s->vhost_user = NULL;
-    }
+    vhost_user_cleanup(&s->vhost_user);
 }
 
 static uint64_t vhost_user_scsi_get_features(VirtIODevice *vdev,
diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c
index 049089b5e2..323dfcc46a 100644
--- a/hw/virtio/vhost-stub.c
+++ b/hw/virtio/vhost-stub.c
@@ -7,7 +7,7 @@  bool vhost_has_free_slot(void)
     return true;
 }
 
-VhostUserState *vhost_user_init(void)
+bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
 {
     return NULL;
 }
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
index b041343632..44795880d6 100644
--- a/hw/virtio/vhost-user.c
+++ b/hw/virtio/vhost-user.c
@@ -1743,17 +1743,24 @@  static bool vhost_user_mem_section_filter(struct vhost_dev *dev,
     return result;
 }
 
-VhostUserState *vhost_user_init(void)
+bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
 {
-    VhostUserState *user = g_new0(struct VhostUserState, 1);
-
-    return user;
+    if (user->chr) {
+        error_setg(errp, "Cannot initialize vhost-user state");
+        return false;
+    }
+    user->chr = chr;
+    return true;
 }
 
 void vhost_user_cleanup(VhostUserState *user)
 {
     int i;
 
+    if (!user->chr) {
+        return;
+    }
+
     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
         if (user->notifier[i].addr) {
             object_unparent(OBJECT(&user->notifier[i].mr));
@@ -1761,6 +1768,7 @@  void vhost_user_cleanup(VhostUserState *user)
             user->notifier[i].addr = NULL;
         }
     }
+    user->chr = NULL;
 }
 
 const VhostOps user_ops = {
diff --git a/net/vhost-user.c b/net/vhost-user.c
index a39f9c9974..5e5b8f3fc9 100644
--- a/net/vhost-user.c
+++ b/net/vhost-user.c
@@ -291,14 +291,14 @@  static int net_vhost_user_init(NetClientState *peer, const char *device,
 {
     Error *err = NULL;
     NetClientState *nc, *nc0 = NULL;
-    VhostUserState *user = NULL;
     NetVhostUserState *s = NULL;
+    VhostUserState *user;
     int i;
 
     assert(name);
     assert(queues > 0);
 
-    user = vhost_user_init();
+    user = g_new0(struct VhostUserState, 1);
     if (!user) {
         error_report("failed to init vhost_user");
         goto err;
@@ -312,11 +312,11 @@  static int net_vhost_user_init(NetClientState *peer, const char *device,
         if (!nc0) {
             nc0 = nc;
             s = DO_UPCAST(NetVhostUserState, nc, nc);
-            if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
+            if (!qemu_chr_fe_init(&s->chr, chr, &err) ||
+                !vhost_user_init(user, &s->chr, &err)) {
                 error_report_err(err);
                 goto err;
             }
-            user->chr = &s->chr;
         }
         s = DO_UPCAST(NetVhostUserState, nc, nc);
         s->vhost_user = user;