vhost-user: fix reconnection support for host notifier
PATCH 1/1 vhost-user: fix reconnection support for host notifier
We need to destroy the host notifiers when cleaning up the backend. Otherwise, some resources are not released after the connection is closed, and it may prevent the external backend from reopening them (e.g. VFIO files) during restart.
--- hw/virtio/vhost-user.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
hw/virtio/vhost-user.c +16 -0
@@ -1454,10 +1454,24 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)static int vhost_user_backend_cleanup (struct vhost_dev *dev) {struct vhost_user *u; +VhostUserState *user; +int i;assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); u = dev->opaque; + +if (dev->vq_index ==0 ) { + user = u->user; +for (i =0 ; i <VIRTIO_QUEUE_MAX ; i++) { +if (user->notifier[i].addr) { +object_unparent (OBJECT (&user->notifier[i].mr)); +munmap (user->notifier[i].addr, qemu_real_host_page_size); + user->notifier[i].addr =NULL ; + } + } + } +if (u->postcopy_notifier.notify) {postcopy_remove_notifier (&u->postcopy_notifier); u->postcopy_notifier.notify =NULL ;
@@ -1881,6 +1895,8 @@ bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)error_setg (errp,"Cannot initialize vhost-user state" );return false; } + +memset (user,0 ,sizeof (*user)); user->chr = chr;return true; }
Signature · 1 line
2.17.1
Hi
Quoted 30 lines · Tiwei Bie
We need to destroy the host notifiers when cleaning up the backend. Otherwise, some resources are not released after the connection is closed, and it may prevent the external backend from reopening them (e.g. VFIO files) during restart.
Fixes 44866521bd6e ("vhost-user: support registering external host notifiers")Signed-off-by Tiwei Bie <[email protected]>--- hw/virtio/vhost-user.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)hw/virtio/vhost-user.c +3
-0@@ -1454,10 +1454,24 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)static int vhost_user_backend_cleanup (struct vhost_dev *dev) {struct vhost_user *u; +VhostUserState *user; +int i;assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER ); u = dev->opaque; +
hw/virtio/vhost-user.c static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque) · line 1473
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+if (dev->vq_index ==0 ) { + user = u->user; +for (i =0 ; i <VIRTIO_QUEUE_MAX ; i++) { +if (user->notifier[i].addr) { +object_unparent (OBJECT (&user->notifier[i].mr)); +munmap (user->notifier[i].addr, qemu_real_host_page_size); + user->notifier[i].addr =NULL ; + } + } + }
Why not call vhost_user_cleanup() ? Alternatively, factor the notifier code in a seperate vhost_user_notifiers_cleanup() ?
hw/virtio/vhost-user.c bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp) · line 1899
Quoted 10 lines
hw/virtio/vhost-user.c +3
-0+if (u->postcopy_notifier.notify) {postcopy_remove_notifier (&u->postcopy_notifier); u->postcopy_notifier.notify =NULL ;@@ -1881,6 +1895,8 @@ bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)error_setg (errp,"Cannot initialize vhost-user state" );return false; } + +memset (user,0 ,sizeof (*user));
This looks superflous. Is it really needed?
I wish there would be some basic tests for external host notifiers. Is it too much to ask to add it in vhost-user-test.c ?
Quoted 7 lines
hw/virtio/vhost-user.c +0
-1user->chr = chr;return true; } --2.17.1
Signature · 1 line
Marc-André Lureau
Hi,
Quoted 31 lines · Marc-André Lureau
Hi
Quoted 28 lines · Tiwei Bie
We need to destroy the host notifiers when cleaning up the backend. Otherwise, some resources are not released after the connection is closed, and it may prevent the external backend from reopening them (e.g. VFIO files) during restart.
Fixes 44866521bd6e ("vhost-user: support registering external host notifiers")Signed-off-by Tiwei Bie <[email protected]>--- hw/virtio/vhost-user.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)hw/virtio/vhost-user.c +2
-0@@ -1454,10 +1454,24 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)static int vhost_user_backend_cleanup (struct vhost_dev *dev) {struct vhost_user *u; +VhostUserState *user; +int i;assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER );
Quoted 15 lines
hw/virtio/vhost-user.c static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque) · line 1473
Quoted 12 lines
hw/virtio/vhost-user.c +11
-0u = dev->opaque; + +if (dev->vq_index ==0 ) { + user = u->user; +for (i =0 ; i <VIRTIO_QUEUE_MAX ; i++) { +if (user->notifier[i].addr) { +object_unparent (OBJECT (&user->notifier[i].mr)); +munmap (user->notifier[i].addr, qemu_real_host_page_size); + user->notifier[i].addr =NULL ; + } + } + }Why not call
vhost_user_cleanup()? Alternatively, factor the notifier code in a seperatevhost_user_notifiers_cleanup()?
I like the idea to factor the notifier code in a seperate vhost_user_notifiers_cleanup(). I can do it. Thanks!
Quoted 13 lines
hw/virtio/vhost-user.c bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp) · line 1899
Quoted 11 lines
hw/virtio/vhost-user.c +3
-0+if (u->postcopy_notifier.notify) {postcopy_remove_notifier (&u->postcopy_notifier); u->postcopy_notifier.notify =NULL ;@@ -1881,6 +1895,8 @@ bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)error_setg (errp,"Cannot initialize vhost-user state" );return false; } + +memset (user,0 ,sizeof (*user));This looks superflous. Is it really needed?
I think you are right. We already checked whether user->chr is zero. The caller should make sure that the VhostUserState will be zero initialized.
Quoted 3 lines
I wish there would be some basic tests for external host notifiers. Is it too much to ask to add it in
vhost-user-test.c?
Sounds good to me. Besides, there are already some basic external host notifier supports in tests/vhost-user-bridge.c that can be enabled with -H. I'm not sure whether that already met what you want. If adding some basic tests in vhost-user-test.c would help, I'd like to do it.
Thanks for the review!
Tiwei
Quoted 13 lines
Quoted 9 lines
hw/virtio/vhost-user.c +0
-1user->chr = chr;return true; } --2.17.1
Signature · 1 line
Marc-André Lureau