vhost-user-blk: Error handling fixes during initialistion
PATCH v2 6/6 vhost-user-blk: Check that num-queues is supported by backend
In reply to Kevin WolfCreating a device with a number of queues that isn't supported by the backend is pointless, the device won't work properly and the error messages are rather confusing.
Just fail to create the device if num-queues is higher than what the backend supports.
Since the relationship between num-queues and the number of virtqueues depends on the specific device, this is an additional value that needs to be initialised by the device. For convenience, allow leaving it 0 if the check should be skipped. This makes sense for vhost-user-net where separate vhost devices are used for the queues and custom initialisation code is needed to perform the check.
--- include/hw/virtio/vhost.h | 2 ++ hw/block/vhost-user-blk.c | 1 + hw/virtio/vhost-user.c | 5 +++++ 3 files changed, 8 insertions(+)
include/hw/virtio/vhost.h +2 -0
@@ -74,6 +74,8 @@ struct vhost_dev {int nvqs;/* the first virtqueue which would be used by this vhost dev */ int vq_index; +/* if non-zero, minimum required value for max_queues */ +int num_queues;uint64_t features;uint64_t acked_features;uint64_t backend_features;
hw/block/vhost-user-blk.c +1 -0
@@ -324,6 +324,7 @@ static int vhost_user_blk_connect(DeviceState *dev, Error **errp) } s->connected = true; + s->dev.num_queues = s->num_queues; s->dev.nvqs = s->num_queues; s->dev.vqs = s->vhost_vqs; s->dev.vq_index =0 ;
hw/virtio/vhost-user.c +5 -0
@@ -1909,6 +1909,11 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)return err; } } +if (dev->num_queues && dev->max_queues < dev->num_queues) { +error_report ("The maximum number of queues supported by the " +"backend is %" PRIu64, dev->max_queues); +return -EINVAL ; + } if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) && !(virtio_has_feature(dev->protocol_features,
Signature · 1 line
2.30.2
v2 cover vhost-user-blk: Error handling fixes during initialistion
vhost-user-blk neglects for several properties to check whether the configured value is even compatible with the backend. This results sometimes in crashes because of buggy error handling code, and sometimes in devices that are presented differently to the guest than the backend would expect and that don't work properly therefore.
This series fixes some of these bugs.
v2:
- Fix error paths in realize() that didn't set errp
- Added vhost_dev_cleanup() back in the error path (more faithful revert of 77542d43149)
Kevin Wolf (6):
vhost-user-blk: Make sure to set Error on realize failure
vhost-user-blk: Don't reconnect during initialisation
vhost-user-blk: Improve error reporting in realize
vhost-user-blk: Get more feature flags from vhost device
virtio: Fail if iommu_platform is requested, but unsupported
vhost-user-blk: Check that num-queues is supported by backend
include/hw/virtio/vhost.h | 2 + hw/block/vhost-user-blk.c | 85 ++++++++++++++------------------------- hw/virtio/vhost-user.c | 5 +++ hw/virtio/virtio-bus.c | 5 +++ 4 files changed, 42 insertions(+), 55 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 3/6 vhost-user-blk: Improve error reporting in realize
Now that vhost_user_blk_connect() is not called from an event handler any more, but directly from vhost_user_blk_device_realize(), we can actually make use of Error again instead of calling error_report() in the inner function and setting a more generic and therefore less useful error message in realize() itself.
With Error, the callers are responsible for adding context if necessary (such as the "-device" option the error refers to). Additional prefixes are redundant and better omitted.
--- hw/block/vhost-user-blk.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
hw/block/vhost-user-blk.c +11 -12
@@ -311,7 +311,7 @@ static void vhost_user_blk_reset(VirtIODevice *vdev)vhost_dev_free_inflight (s->inflight); } -static int vhost_user_blk_connect (DeviceState *dev) +static int vhost_user_blk_connect (DeviceState *dev,Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBlk *s =VHOST_USER_BLK (vdev);
@@ -331,8 +331,7 @@ static int vhost_user_blk_connect(DeviceState *dev) ret =vhost_dev_init (&s->dev, &s->vhost_user,VHOST_BACKEND_TYPE_USER ,0 );if (ret <0 ) { -error_report ("vhost-user-blk: vhost initialization failed: %s" , -strerror (-ret)); +error_setg_errno (errp, -ret,"vhost initialization failed" );return ret; }
@@ -340,8 +339,7 @@ static int vhost_user_blk_connect(DeviceState *dev)if (virtio_device_started (vdev, vdev->status)) { ret =vhost_user_blk_start (vdev);if (ret <0 ) { -error_report ("vhost-user-blk: vhost start failed: %s" , -strerror (-ret)); +error_setg_errno (errp, -ret,"vhost start failed" );return ret; } }
@@ -380,10 +378,12 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)DeviceState *dev = opaque;VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBlk *s =VHOST_USER_BLK (vdev); +Error *local_err =NULL ;switch (event) {case CHR_EVENT_OPENED : -if (vhost_user_blk_connect (dev) <0 ) { +if (vhost_user_blk_connect (dev, &local_err) <0 ) { +error_report_err (local_err);qemu_chr_fe_disconnect (&s->chardev);return ; }
@@ -426,7 +426,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)int i, ret;if (!s->chardev.chr) { -error_setg (errp,"vhost-user-blk: chardev is mandatory" ); +error_setg (errp,"chardev is mandatory" );return ; }
@@ -434,16 +434,16 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) s->num_queues =1 ; }if (!s->num_queues || s->num_queues >VIRTIO_QUEUE_MAX ) { -error_setg (errp,"vhost-user-blk: invalid number of IO queues" ); +error_setg (errp,"invalid number of IO queues" );return ; }if (!s->queue_size) { -error_setg (errp,"vhost-user-blk: queue size must be non-zero" ); +error_setg (errp,"queue size must be non-zero" );return ; }if (s->queue_size >VIRTQUEUE_MAX_SIZE ) { -error_setg (errp,"vhost-user-blk: queue size must not exceed %d" , +error_setg (errp,"queue size must not exceed %d" ,VIRTQUEUE_MAX_SIZE );return ; }
@@ -469,8 +469,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) goto virtio_err; } -if (vhost_user_blk_connect (dev) <0 ) { -error_setg (errp,"vhost-user-blk: could not connect" ); +if (vhost_user_blk_connect (dev, errp) <0 ) {qemu_chr_fe_disconnect (&s->chardev); goto virtio_err; }
Signature · 1 line
2.30.2
PATCH v2 2/6 vhost-user-blk: Don't reconnect during initialisation
In reply to Kevin WolfThis is a partial revert of commits 77542d43149 and bc79c87bcde.
Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Additionally, calling vhost_user_blk_disconnect() from the chardev event handler could result in use-after-free because none of the initialisation code expects that the device could just go away in the middle. So removing the call fixes crashes in several places.
For example, using a num-queues setting that is incompatible with the backend would result in a crash like this (dereferencing dev->opaque, which is already NULL):
#0 0x0000555555d0a4bd in vhost_user_read_cb (source=0x5555568f4690, condition=(G_IO_IN | G_IO_HUP), opaque=0x7fffffffcbf0) at ../hw/virtio/vhost-user.c:313 #1 0x0000555555d950d3 in qio_channel_fd_source_dispatch (source=0x555557c3f750, callback=0x555555d0a478 <vhost_user_read_cb>, user_data=0x7fffffffcbf0) at ../io/channel-watch.c:84 #2 0x00007ffff7b32a9f in g_main_context_dispatch () at /lib64/libglib-2.0.so.0 #3 0x00007ffff7b84a98 in g_main_context_iterate.constprop () at /lib64/libglib-2.0.so.0 #4 0x00007ffff7b32163 in g_main_loop_run () at /lib64/libglib-2.0.so.0 #5 0x0000555555d0a724 in vhost_user_read (dev=0x555557bc62f8, msg=0x7fffffffcc50) at ../hw/virtio/vhost-user.c:402 #6 0x0000555555d0ee6b in vhost_user_get_config (dev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost-user.c:2133 #7 0x0000555555d56d46 in vhost_dev_get_config (hdev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost.c:1566 #8 0x0000555555cdd150 in vhost_user_blk_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcf90) at ../hw/block/vhost-user-blk.c:510 #9 0x0000555555d08f6d in virtio_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcff0) at ../hw/virtio/virtio.c:3660
--- hw/block/vhost-user-blk.c | 59 +++++++++++---------------------------- 1 file changed, 17 insertions(+), 42 deletions(-)
hw/block/vhost-user-blk.c +17 -42
@@ -50,6 +50,8 @@ static const int user_feature_bits[] = { VHOST_INVALID_FEATURE_BIT }; +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event); +static void vhost_user_blk_update_config (VirtIODevice *vdev,uint8_t *config) {VHostUserBlk *s =VHOST_USER_BLK (vdev);
@@ -362,19 +364,6 @@ static void vhost_user_blk_disconnect(DeviceState *dev)vhost_dev_cleanup (&s->dev); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized); - -static void vhost_user_blk_event_realize (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, false); -} - -static void vhost_user_blk_event_oper (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, true); -} -static void vhost_user_blk_chr_closed_bh (void *opaque) {DeviceState *dev = opaque;
@@ -382,12 +371,11 @@ static void vhost_user_blk_chr_closed_bh(void *opaque)VHostUserBlk *s =VHOST_USER_BLK (vdev);vhost_user_blk_disconnect (dev); -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_oper,NULL , opaque,NULL , true); +qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , vhost_user_blk_event, +NULL , opaque,NULL , true); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized) +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event) {DeviceState *dev = opaque;VirtIODevice *vdev =VIRTIO_DEVICE (dev);
@@ -401,17 +389,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event, }break ;case CHR_EVENT_CLOSED : -/* -* Closing the connection should happen differently on device -* initialization and operation stages. -* On initalization, we want to re-start vhost_dev initialization -* from the very beginning right away when the connection is closed, -* so we clean up vhost_dev on each connection closing. -* On operation, we want to postpone vhost_dev cleanup to let the -* other code perform its own cleanup sequence using vhost_dev data -* (e.g. vhost_dev_set_log). -*/ -if (realized && !runstate_check (RUN_STATE_SHUTDOWN )) { +if (!runstate_check (RUN_STATE_SHUTDOWN )) {/* * A close event may happen during a read/write, but vhost * code assumes the vhost_dev remains setup, so delay the
@@ -431,8 +409,6 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event,* knowing its type (in this case vhost-user). */ s->dev.started = false; - }else { -vhost_user_blk_disconnect (dev); }break ;case CHR_EVENT_BREAK :
@@ -489,33 +465,32 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); s->connected = false; -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_realize,NULL , (void *)dev, -NULL , true); - -reconnect:if (qemu_chr_fe_wait_connected (&s->chardev, errp) <0 ) { goto virtio_err; } -/* check whether vhost_user_blk_connect() failed or not */ -if (!s->connected) { - goto reconnect; +if (vhost_user_blk_connect (dev) <0 ) { +error_setg (errp,"vhost-user-blk: could not connect" ); +qemu_chr_fe_disconnect (&s->chardev); + goto virtio_err; } +assert (s->connected); ret =vhost_dev_get_config (&s->dev, (uint8_t *)&s->blkcfg,sizeof (struct virtio_blk_config ));if (ret <0 ) { -error_report ("vhost-user-blk: get block config failed" ); - goto reconnect; +error_setg (errp,"vhost-user-blk: get block config failed" ); + goto vhost_err; } -/* we're fully initialized, now we can operate, so change the handler */ +/* 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_oper,NULL , (void *)dev, + vhost_user_blk_event,NULL , (void *)dev,NULL , true);return ; +vhost_err: +vhost_dev_cleanup (&s->dev); virtio_err:g_free (s->vhost_vqs); s->vhost_vqs =NULL ;
Signature · 1 line
2.30.2
PATCH v2 1/6 vhost-user-blk: Make sure to set Error on realize failure
In reply to Kevin WolfWe have to set errp before jumping to virtio_err, otherwise the caller (virtio_device_realize()) will take this as success and crash when it later tries to access things that we've already freed in the error path.
--- hw/block/vhost-user-blk.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/block/vhost-user-blk.c +1 -3
@@ -447,7 +447,6 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBlk *s =VHOST_USER_BLK (vdev); -Error *err =NULL ;int i, ret;if (!s->chardev.chr) {
@@ -495,8 +494,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) NULL, true); reconnect: -if (qemu_chr_fe_wait_connected (&s->chardev, &err) <0 ) { -error_report_err (err); +if (qemu_chr_fe_wait_connected (&s->chardev, errp) <0 ) { goto virtio_err; }
Signature · 1 line
2.30.2
PATCH v2 5/6 virtio: Fail if iommu_platform is requested, but unsupported
In reply to Kevin WolfCommit 2943b53f6 (' virtio: force VIRTIO_F_IOMMU_PLATFORM') made sure that vhost can't just reject VIRTIO_F_IOMMU_PLATFORM when it was requested. However, just adding it back to the negotiated flags isn't right either because it promises support to the guest that the device actually doesn't support. One example of a vhost-user device that doesn't have support for the flag is the vhost-user-blk export of QEMU.
Instead of successfully creating a device that doesn't work, just fail to plug the device when it doesn't support the feature, but it was requested. This results in much clearer error messages.
--- hw/virtio/virtio-bus.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/virtio/virtio-bus.c +5 -0
@@ -69,6 +69,11 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)return ; } +if (has_iommu && !virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM )) { +error_setg (errp,"iommu_platform=true is not supported by the device" ); +return ; + } +if (klass->device_plugged !=NULL ) { klass->device_plugged (qbus->parent, &local_err); }
Signature · 1 line
2.30.2
PATCH v2 4/6 vhost-user-blk: Get more feature flags from vhost device
In reply to Kevin WolfVIRTIO_F_RING_PACKED and VIRTIO_F_IOMMU_PLATFORM need to be supported by the vhost device, otherwise advertising it to the guest doesn't result in a working configuration. They are currently not supported by the vhost-user-blk export in QEMU.
--- hw/block/vhost-user-blk.c | 2 ++ 1 file changed, 2 insertions(+)
hw/block/vhost-user-blk.c +2 -0
@@ -47,6 +47,8 @@ static const int user_feature_bits[] = {VIRTIO_RING_F_INDIRECT_DESC ,VIRTIO_RING_F_EVENT_IDX ,VIRTIO_F_NOTIFY_ON_EMPTY , +VIRTIO_F_RING_PACKED , +VIRTIO_F_IOMMU_PLATFORM , VHOST_INVALID_FEATURE_BIT };
Signature · 1 line
2.30.2
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
In reply to Kevin WolfSo we're not going with the suggestion to retry once or a fixed number of times? Any reason why not?
Quoted 157 lines · Kevin Wolf
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Additionally, calling
vhost_user_blk_disconnect()from the chardev event handler could result in use-after-free because none of the initialisation code expects that the device could just go away in the middle. So removing the call fixes crashes in several places.For example, using a num-queues setting that is incompatible with the backend would result in a crash like this (dereferencing
dev->opaque, which is already NULL):#0 0x0000555555d0a4bd in vhost_user_read_cb (source=0x5555568f4690, condition=(G_IO_IN | G_IO_HUP), opaque=0x7fffffffcbf0) at ../hw/virtio/vhost-user.c:313 #1 0x0000555555d950d3 in qio_channel_fd_source_dispatch (source=0x555557c3f750, callback=0x555555d0a478 <vhost_user_read_cb>, user_data=0x7fffffffcbf0) at ../io/channel-watch.c:84 #2 0x00007ffff7b32a9f in g_main_context_dispatch () at /lib64/libglib-2.0.so.0 #3 0x00007ffff7b84a98 in g_main_context_iterate.constprop () at /lib64/libglib-2.0.so.0 #4 0x00007ffff7b32163 in g_main_loop_run () at /lib64/libglib-2.0.so.0 #5 0x0000555555d0a724 in vhost_user_read (dev=0x555557bc62f8, msg=0x7fffffffcc50) at ../hw/virtio/vhost-user.c:402 #6 0x0000555555d0ee6b in vhost_user_get_config (dev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost-user.c:2133 #7 0x0000555555d56d46 in vhost_dev_get_config (hdev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost.c:1566 #8 0x0000555555cdd150 in vhost_user_blk_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcf90) at ../hw/block/vhost-user-blk.c:510 #9 0x0000555555d08f6d in virtio_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcff0) at ../hw/virtio/virtio.c:3660Signed-off-by Kevin Wolf <[email protected]>--- hw/block/vhost-user-blk.c | 59 +++++++++++---------------------------- 1 file changed, 17 insertions(+), 42 deletions(-)hw/block/vhost-user-blk.c +17
-42@@ -50,6 +50,8 @@ static const int user_feature_bits[] = { VHOST_INVALID_FEATURE_BIT }; +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event); +static void vhost_user_blk_update_config (VirtIODevice *vdev,uint8_t *config) {VHostUserBlk *s =VHOST_USER_BLK (vdev);@@ -362,19 +364,6 @@ static void vhost_user_blk_disconnect(DeviceState *dev)vhost_dev_cleanup (&s->dev); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized); - -static void vhost_user_blk_event_realize (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, false); -} - -static void vhost_user_blk_event_oper (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, true); -} -static void vhost_user_blk_chr_closed_bh (void *opaque) {DeviceState *dev = opaque;@@ -382,12 +371,11 @@ static void vhost_user_blk_chr_closed_bh(void *opaque)VHostUserBlk *s =VHOST_USER_BLK (vdev);vhost_user_blk_disconnect (dev); -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_oper,NULL , opaque,NULL , true); +qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , vhost_user_blk_event, +NULL , opaque,NULL , true); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized) +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event) {DeviceState *dev = opaque;VirtIODevice *vdev =VIRTIO_DEVICE (dev);@@ -401,17 +389,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event, }break ;case CHR_EVENT_CLOSED : -/* -* Closing the connection should happen differently on device -* initialization and operation stages. -* On initalization, we want to re-start vhost_dev initialization -* from the very beginning right away when the connection is closed, -* so we clean up vhost_dev on each connection closing. -* On operation, we want to postpone vhost_dev cleanup to let the -* other code perform its own cleanup sequence using vhost_dev data -* (e.g. vhost_dev_set_log). -*/ -if (realized && !runstate_check (RUN_STATE_SHUTDOWN )) { +if (!runstate_check (RUN_STATE_SHUTDOWN )) {/* * A close event may happen during a read/write, but vhost * code assumes the vhost_dev remains setup, so delay the @@ -431,8 +409,6 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event,* knowing its type (in this case vhost-user). */ s->dev.started = false; - }else { -vhost_user_blk_disconnect (dev); }break ;case CHR_EVENT_BREAK :@@ -489,33 +465,32 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); s->connected = false; -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_realize,NULL , (void *)dev, -NULL , true); - -reconnect:if (qemu_chr_fe_wait_connected (&s->chardev, errp) <0 ) { goto virtio_err; } -/* check whether vhost_user_blk_connect() failed or not */ -if (!s->connected) { - goto reconnect; +if (vhost_user_blk_connect (dev) <0 ) { +error_setg (errp,"vhost-user-blk: could not connect" ); +qemu_chr_fe_disconnect (&s->chardev); + goto virtio_err; } +assert (s->connected); ret =vhost_dev_get_config (&s->dev, (uint8_t *)&s->blkcfg,sizeof (struct virtio_blk_config ));if (ret <0 ) { -error_report ("vhost-user-blk: get block config failed" ); - goto reconnect; +error_setg (errp,"vhost-user-blk: get block config failed" ); + goto vhost_err; } -/* we're fully initialized, now we can operate, so change the handler */ +/* 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_oper,NULL , (void *)dev, + vhost_user_blk_event,NULL , (void *)dev,NULL , true);return ; +vhost_err: +vhost_dev_cleanup (&s->dev); virtio_err:g_free (s->vhost_vqs); s->vhost_vqs =NULL ;Signature · 1 line
2.30.2
Re: [PATCH v2 1/6] vhost-user-blk: Make sure to set Error on realize failure
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
We have to set errp before jumping to
virtio_err, otherwise the caller (virtio_device_realize()) will take this as success and crash when it later tries to access things that we've already freed in the error path.Fixes 77542d431491788d1e8e79d93ce10172ef207775Signed-off-by Kevin Wolf <[email protected]>--- hw/block/vhost-user-blk.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH v2 1/6] vhost-user-blk: Make sure to set Error on realize failure
In reply to Kevin WolfQuoted 35 lines · Kevin Wolf
We have to set errp before jumping to
virtio_err, otherwise the caller (virtio_device_realize()) will take this as success and crash when it later tries to access things that we've already freed in the error path.Fixes 77542d431491788d1e8e79d93ce10172ef207775Signed-off-by Kevin Wolf <[email protected]>--- hw/block/vhost-user-blk.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/block/vhost-user-blk.c +1
-3@@ -447,7 +447,6 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBlk *s =VHOST_USER_BLK (vdev); -Error *err =NULL ;int i, ret;if (!s->chardev.chr) {@@ -495,8 +494,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) NULL, true); reconnect: -if (qemu_chr_fe_wait_connected (&s->chardev, &err) <0 ) { -error_report_err (err); +if (qemu_chr_fe_wait_connected (&s->chardev, errp) <0 ) { goto virtio_err; }Signature · 1 line
2.30.2
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
In reply to Kevin WolfQuoted 6 lines · Kevin Wolf
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
So there are several possible reasons for an error:
1. remote restarted - we would like to reconnect,
this was the original use-case for reconnect.
I am not very happy that we are killing this usecase.
2. qemu detected an error and closed the connection looks like we try to handle that by reconnect, this is something we should address.
3. remote failed due to a bad command from qemu. this usecase isn't well supported at the moment.
How about supporting it on the remote side? I think
that if the data is well-formed just has a configuration
remote can not support then instead of closing the connection, remote can wait for commands with need_reply set, and respond with
an error. Or at least do it
if VHOST_USER_PROTOCOL_F_REPLY_ACK has been negotiated.
If VHOST_USER_SET_VRING_ERR is used then signalling that
fd might also be reasonable.
OTOH if qemu is buggy and sends malformed data and remote detects that then hacing qemu retry forever is ok, might actually be benefitial for debugging.
Quoted 8 lines
Additionally, calling
vhost_user_blk_disconnect()from the chardev event handler could result in use-after-free because none of the initialisation code expects that the device could just go away in the middle. So removing the call fixes crashes in several places. For example, using a num-queues setting that is incompatible with the backend would result in a crash like this (dereferencingdev->opaque, which is already NULL):
Quoted 10 lines
#0 0x0000555555d0a4bd in vhost_user_read_cb (source=0x5555568f4690, condition=(G_IO_IN | G_IO_HUP), opaque=0x7fffffffcbf0) at ../hw/virtio/vhost-user.c:313 #1 0x0000555555d950d3 in qio_channel_fd_source_dispatch (source=0x555557c3f750, callback=0x555555d0a478 <vhost_user_read_cb>, user_data=0x7fffffffcbf0) at ../io/channel-watch.c:84 #2 0x00007ffff7b32a9f in g_main_context_dispatch () at /lib64/libglib-2.0.so.0 #3 0x00007ffff7b84a98 in g_main_context_iterate.constprop () at /lib64/libglib-2.0.so.0 #4 0x00007ffff7b32163 in g_main_loop_run () at /lib64/libglib-2.0.so.0 #5 0x0000555555d0a724 in vhost_user_read (dev=0x555557bc62f8, msg=0x7fffffffcc50) at ../hw/virtio/vhost-user.c:402 #6 0x0000555555d0ee6b in vhost_user_get_config (dev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost-user.c:2133 #7 0x0000555555d56d46 in vhost_dev_get_config (hdev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost.c:1566 #8 0x0000555555cdd150 in vhost_user_blk_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcf90) at ../hw/block/vhost-user-blk.c:510 #9 0x0000555555d08f6d in virtio_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcff0) at ../hw/virtio/virtio.c:3660
Right. So that's definitely something to fix.
Quoted 130 lines
Signed-off-by Kevin Wolf <[email protected]>--- hw/block/vhost-user-blk.c | 59 +++++++++++---------------------------- 1 file changed, 17 insertions(+), 42 deletions(-)hw/block/vhost-user-blk.c +17
-42@@ -50,6 +50,8 @@ static const int user_feature_bits[] = { VHOST_INVALID_FEATURE_BIT }; +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event); +static void vhost_user_blk_update_config (VirtIODevice *vdev,uint8_t *config) {VHostUserBlk *s =VHOST_USER_BLK (vdev);@@ -362,19 +364,6 @@ static void vhost_user_blk_disconnect(DeviceState *dev)vhost_dev_cleanup (&s->dev); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized); - -static void vhost_user_blk_event_realize (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, false); -} - -static void vhost_user_blk_event_oper (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, true); -} -static void vhost_user_blk_chr_closed_bh (void *opaque) {DeviceState *dev = opaque;@@ -382,12 +371,11 @@ static void vhost_user_blk_chr_closed_bh(void *opaque)VHostUserBlk *s =VHOST_USER_BLK (vdev);vhost_user_blk_disconnect (dev); -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_oper,NULL , opaque,NULL , true); +qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , vhost_user_blk_event, +NULL , opaque,NULL , true); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized) +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event) {DeviceState *dev = opaque;VirtIODevice *vdev =VIRTIO_DEVICE (dev);@@ -401,17 +389,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event, }break ;case CHR_EVENT_CLOSED : -/* -* Closing the connection should happen differently on device -* initialization and operation stages. -* On initalization, we want to re-start vhost_dev initialization -* from the very beginning right away when the connection is closed, -* so we clean up vhost_dev on each connection closing. -* On operation, we want to postpone vhost_dev cleanup to let the -* other code perform its own cleanup sequence using vhost_dev data -* (e.g. vhost_dev_set_log). -*/ -if (realized && !runstate_check (RUN_STATE_SHUTDOWN )) { +if (!runstate_check (RUN_STATE_SHUTDOWN )) {/* * A close event may happen during a read/write, but vhost * code assumes the vhost_dev remains setup, so delay the @@ -431,8 +409,6 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event,* knowing its type (in this case vhost-user). */ s->dev.started = false; - }else { -vhost_user_blk_disconnect (dev); }break ;case CHR_EVENT_BREAK :@@ -489,33 +465,32 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); s->connected = false; -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_realize,NULL , (void *)dev, -NULL , true); - -reconnect:if (qemu_chr_fe_wait_connected (&s->chardev, errp) <0 ) { goto virtio_err; } -/* check whether vhost_user_blk_connect() failed or not */ -if (!s->connected) { - goto reconnect; +if (vhost_user_blk_connect (dev) <0 ) { +error_setg (errp,"vhost-user-blk: could not connect" ); +qemu_chr_fe_disconnect (&s->chardev); + goto virtio_err; } +assert (s->connected); ret =vhost_dev_get_config (&s->dev, (uint8_t *)&s->blkcfg,sizeof (struct virtio_blk_config ));if (ret <0 ) { -error_report ("vhost-user-blk: get block config failed" ); - goto reconnect; +error_setg (errp,"vhost-user-blk: get block config failed" ); + goto vhost_err; } -/* we're fully initialized, now we can operate, so change the handler */ +/* 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_oper,NULL , (void *)dev, + vhost_user_blk_event,NULL , (void *)dev,NULL , true);return ; +vhost_err: +vhost_dev_cleanup (&s->dev); virtio_err:g_free (s->vhost_vqs); s->vhost_vqs =NULL ;Signature · 1 line
2.30.2
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
In reply to Raphael NorwitzAm 03.05.2021 um 19:01 hat Raphael Norwitz geschrieben:
Quoted 2 lines
So we're not going with the suggestion to retry once or a fixed number of times? Any reason why not?
I thought we agreed that we'd add reconnection back in a follow-up series that also addresses the different kinds of errors and retries only when it makes sense?
Kevin
Quoted 159 lines · Kevin Wolf
Quoted 157 lines
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Additionally, calling
vhost_user_blk_disconnect()from the chardev event handler could result in use-after-free because none of the initialisation code expects that the device could just go away in the middle. So removing the call fixes crashes in several places.For example, using a num-queues setting that is incompatible with the backend would result in a crash like this (dereferencing
dev->opaque, which is already NULL):#0 0x0000555555d0a4bd in vhost_user_read_cb (source=0x5555568f4690, condition=(G_IO_IN | G_IO_HUP), opaque=0x7fffffffcbf0) at ../hw/virtio/vhost-user.c:313 #1 0x0000555555d950d3 in qio_channel_fd_source_dispatch (source=0x555557c3f750, callback=0x555555d0a478 <vhost_user_read_cb>, user_data=0x7fffffffcbf0) at ../io/channel-watch.c:84 #2 0x00007ffff7b32a9f in g_main_context_dispatch () at /lib64/libglib-2.0.so.0 #3 0x00007ffff7b84a98 in g_main_context_iterate.constprop () at /lib64/libglib-2.0.so.0 #4 0x00007ffff7b32163 in g_main_loop_run () at /lib64/libglib-2.0.so.0 #5 0x0000555555d0a724 in vhost_user_read (dev=0x555557bc62f8, msg=0x7fffffffcc50) at ../hw/virtio/vhost-user.c:402 #6 0x0000555555d0ee6b in vhost_user_get_config (dev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost-user.c:2133 #7 0x0000555555d56d46 in vhost_dev_get_config (hdev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost.c:1566 #8 0x0000555555cdd150 in vhost_user_blk_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcf90) at ../hw/block/vhost-user-blk.c:510 #9 0x0000555555d08f6d in virtio_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcff0) at ../hw/virtio/virtio.c:3660Signed-off-by Kevin Wolf <[email protected]>--- hw/block/vhost-user-blk.c | 59 +++++++++++---------------------------- 1 file changed, 17 insertions(+), 42 deletions(-)hw/block/vhost-user-blk.c +17
-42@@ -50,6 +50,8 @@ static const int user_feature_bits[] = { VHOST_INVALID_FEATURE_BIT }; +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event); +static void vhost_user_blk_update_config (VirtIODevice *vdev,uint8_t *config) {VHostUserBlk *s =VHOST_USER_BLK (vdev);@@ -362,19 +364,6 @@ static void vhost_user_blk_disconnect(DeviceState *dev)vhost_dev_cleanup (&s->dev); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized); - -static void vhost_user_blk_event_realize (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, false); -} - -static void vhost_user_blk_event_oper (void *opaque,QEMUChrEvent event) -{ -vhost_user_blk_event (opaque, event, true); -} -static void vhost_user_blk_chr_closed_bh (void *opaque) {DeviceState *dev = opaque;@@ -382,12 +371,11 @@ static void vhost_user_blk_chr_closed_bh(void *opaque)VHostUserBlk *s =VHOST_USER_BLK (vdev);vhost_user_blk_disconnect (dev); -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_oper,NULL , opaque,NULL , true); +qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , vhost_user_blk_event, +NULL , opaque,NULL , true); } -static void vhost_user_blk_event (void *opaque,QEMUChrEvent event, -bool realized) +static void vhost_user_blk_event (void *opaque,QEMUChrEvent event) {DeviceState *dev = opaque;VirtIODevice *vdev =VIRTIO_DEVICE (dev);@@ -401,17 +389,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event, }break ;case CHR_EVENT_CLOSED : -/* -* Closing the connection should happen differently on device -* initialization and operation stages. -* On initalization, we want to re-start vhost_dev initialization -* from the very beginning right away when the connection is closed, -* so we clean up vhost_dev on each connection closing. -* On operation, we want to postpone vhost_dev cleanup to let the -* other code perform its own cleanup sequence using vhost_dev data -* (e.g. vhost_dev_set_log). -*/ -if (realized && !runstate_check (RUN_STATE_SHUTDOWN )) { +if (!runstate_check (RUN_STATE_SHUTDOWN )) {/* * A close event may happen during a read/write, but vhost * code assumes the vhost_dev remains setup, so delay the @@ -431,8 +409,6 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event,* knowing its type (in this case vhost-user). */ s->dev.started = false; - }else { -vhost_user_blk_disconnect (dev); }break ;case CHR_EVENT_BREAK :@@ -489,33 +465,32 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); s->connected = false; -qemu_chr_fe_set_handlers (&s->chardev,NULL ,NULL , - vhost_user_blk_event_realize,NULL , (void *)dev, -NULL , true); - -reconnect:if (qemu_chr_fe_wait_connected (&s->chardev, errp) <0 ) { goto virtio_err; } -/* check whether vhost_user_blk_connect() failed or not */ -if (!s->connected) { - goto reconnect; +if (vhost_user_blk_connect (dev) <0 ) { +error_setg (errp,"vhost-user-blk: could not connect" ); +qemu_chr_fe_disconnect (&s->chardev); + goto virtio_err; } +assert (s->connected); ret =vhost_dev_get_config (&s->dev, (uint8_t *)&s->blkcfg,sizeof (struct virtio_blk_config ));if (ret <0 ) { -error_report ("vhost-user-blk: get block config failed" ); - goto reconnect; +error_setg (errp,"vhost-user-blk: get block config failed" ); + goto vhost_err; } -/* we're fully initialized, now we can operate, so change the handler */ +/* 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_oper,NULL , (void *)dev, + vhost_user_blk_event,NULL , (void *)dev,NULL , true);return ; +vhost_err: +vhost_dev_cleanup (&s->dev); virtio_err:g_free (s->vhost_vqs); s->vhost_vqs =NULL ;Signature · 1 line
2.30.2
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
In reply to Michael S. TsirkinAm 04.05.2021 um 10:59 hat Michael S. Tsirkin geschrieben:
Quoted 8 lines · Kevin Wolf
Quoted 6 lines
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Quoted 6 lines
So there are several possible reasons for an error:
1. remote restarted - we would like to reconnect,
this was the original use-case for reconnect.I am not very happy that we are killing this usecase.
This patch is killing it only during initialisation, where it's quite unlikely compared to other cases and where the current implementation is rather broken. So reverting the broken feature and going back to a simpler correct state feels like a good idea to me.
The idea is to add the "retry during initialisation" feature back on top of this, but it requires some more changes in the error paths so that we can actually distinguish different kinds of errors and don't retry when we already know that it can't succeed.
Quoted 3 lines
2. qemu detected an error and closed the connection looks like we try to handle that by reconnect, this is something we should address.
Yes, if qemu produces the error locally, retrying is useless.
Quoted 11 lines
3. remote failed due to a bad command from qemu. this usecase isn't well supported at the moment.
How about supporting it on the remote side? I think that if the data is well-formed just has a configuration remote can not support then instead of closing the connection, remote can wait for commands with
need_replyset, and respond with an error. Or at least do it ifVHOST_USER_PROTOCOL_F_REPLY_ACKhas been negotiated. IfVHOST_USER_SET_VRING_ERRis used then signalling that fd might also be reasonable.
Quoted 3 lines
OTOH if qemu is buggy and sends malformed data and remote detects that then hacing qemu retry forever is ok, might actually be benefitial for debugging.
I haven't really checked this case yet, it seems to be less common. Explicitly communicating an error is certainly better than just cutting the connection. But as you say, it means QEMU is buggy, so blindly retrying in this case is kind of acceptable.
Raphael suggested that we could limit the number of retries during initialisation so that it wouldn't result in a hang at least.
Quoted 19 lines
Quoted 8 lines
Additionally, calling
vhost_user_blk_disconnect()from the chardev event handler could result in use-after-free because none of the initialisation code expects that the device could just go away in the middle. So removing the call fixes crashes in several places. For example, using a num-queues setting that is incompatible with the backend would result in a crash like this (dereferencingdev->opaque, which is already NULL):Quoted 10 lines
#0 0x0000555555d0a4bd in vhost_user_read_cb (source=0x5555568f4690, condition=(G_IO_IN | G_IO_HUP), opaque=0x7fffffffcbf0) at ../hw/virtio/vhost-user.c:313 #1 0x0000555555d950d3 in qio_channel_fd_source_dispatch (source=0x555557c3f750, callback=0x555555d0a478 <vhost_user_read_cb>, user_data=0x7fffffffcbf0) at ../io/channel-watch.c:84 #2 0x00007ffff7b32a9f in g_main_context_dispatch () at /lib64/libglib-2.0.so.0 #3 0x00007ffff7b84a98 in g_main_context_iterate.constprop () at /lib64/libglib-2.0.so.0 #4 0x00007ffff7b32163 in g_main_loop_run () at /lib64/libglib-2.0.so.0 #5 0x0000555555d0a724 in vhost_user_read (dev=0x555557bc62f8, msg=0x7fffffffcc50) at ../hw/virtio/vhost-user.c:402 #6 0x0000555555d0ee6b in vhost_user_get_config (dev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost-user.c:2133 #7 0x0000555555d56d46 in vhost_dev_get_config (hdev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost.c:1566 #8 0x0000555555cdd150 in vhost_user_blk_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcf90) at ../hw/block/vhost-user-blk.c:510 #9 0x0000555555d08f6d in virtio_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcff0) at ../hw/virtio/virtio.c:3660
Quoted 4 lines
Right. So that's definitely something to fix.
Quoted 2 lines
Signed-off-by Kevin Wolf <[email protected]>
Kevin
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
Quoted 21 lines · Kevin Wolf
Am 04.05.2021 um 10:59 hat Michael S. Tsirkin geschrieben:
Quoted 8 lines · Kevin Wolf
Quoted 6 lines
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Quoted 6 lines
So there are several possible reasons for an error:
1. remote restarted - we would like to reconnect,
this was the original use-case for reconnect.I am not very happy that we are killing this usecase.
This patch is killing it only during initialisation, where it's quite unlikely compared to other cases and where the current implementation is rather broken. So reverting the broken feature and going back to a simpler correct state feels like a good idea to me.
Quoted 4 lines
The idea is to add the "retry during initialisation" feature back on top of this, but it requires some more changes in the error paths so that we can actually distinguish different kinds of errors and don't retry when we already know that it can't succeed.
Okay ... let's make all this explicit in the commit log though, ok?
Quoted 21 lines
Quoted 3 lines
2. qemu detected an error and closed the connection looks like we try to handle that by reconnect, this is something we should address.
Yes, if qemu produces the error locally, retrying is useless.
Quoted 11 lines
3. remote failed due to a bad command from qemu. this usecase isn't well supported at the moment.
How about supporting it on the remote side? I think that if the data is well-formed just has a configuration remote can not support then instead of closing the connection, remote can wait for commands with
need_replyset, and respond with an error. Or at least do it ifVHOST_USER_PROTOCOL_F_REPLY_ACKhas been negotiated. IfVHOST_USER_SET_VRING_ERRis used then signalling that fd might also be reasonable.Quoted 3 lines
OTOH if qemu is buggy and sends malformed data and remote detects that then hacing qemu retry forever is ok, might actually be benefitial for debugging.
Quoted 7 lines
I haven't really checked this case yet, it seems to be less common. Explicitly communicating an error is certainly better than just cutting the connection. But as you say, it means QEMU is buggy, so blindly retrying in this case is kind of acceptable.
Raphael suggested that we could limit the number of retries during initialisation so that it wouldn't result in a hang at least.
not sure how do I feel about random limits ... how would we set the limit?
Quoted 25 lines
Quoted 19 lines
Quoted 8 lines
Additionally, calling
vhost_user_blk_disconnect()from the chardev event handler could result in use-after-free because none of the initialisation code expects that the device could just go away in the middle. So removing the call fixes crashes in several places. For example, using a num-queues setting that is incompatible with the backend would result in a crash like this (dereferencingdev->opaque, which is already NULL):Quoted 10 lines
#0 0x0000555555d0a4bd in vhost_user_read_cb (source=0x5555568f4690, condition=(G_IO_IN | G_IO_HUP), opaque=0x7fffffffcbf0) at ../hw/virtio/vhost-user.c:313 #1 0x0000555555d950d3 in qio_channel_fd_source_dispatch (source=0x555557c3f750, callback=0x555555d0a478 <vhost_user_read_cb>, user_data=0x7fffffffcbf0) at ../io/channel-watch.c:84 #2 0x00007ffff7b32a9f in g_main_context_dispatch () at /lib64/libglib-2.0.so.0 #3 0x00007ffff7b84a98 in g_main_context_iterate.constprop () at /lib64/libglib-2.0.so.0 #4 0x00007ffff7b32163 in g_main_loop_run () at /lib64/libglib-2.0.so.0 #5 0x0000555555d0a724 in vhost_user_read (dev=0x555557bc62f8, msg=0x7fffffffcc50) at ../hw/virtio/vhost-user.c:402 #6 0x0000555555d0ee6b in vhost_user_get_config (dev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost-user.c:2133 #7 0x0000555555d56d46 in vhost_dev_get_config (hdev=0x555557bc62f8, config=0x555557bc62ac "", config_len=60) at ../hw/virtio/vhost.c:1566 #8 0x0000555555cdd150 in vhost_user_blk_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcf90) at ../hw/block/vhost-user-blk.c:510 #9 0x0000555555d08f6d in virtio_device_realize (dev=0x555557bc60b0, errp=0x7fffffffcff0) at ../hw/virtio/virtio.c:3660Quoted 4 lines
Right. So that's definitely something to fix.
Quoted 2 lines
Signed-off-by Kevin Wolf <[email protected]>Kevin
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
Am 04.05.2021 um 11:44 hat Michael S. Tsirkin geschrieben:
Quoted 22 lines · Kevin Wolf
Quoted 21 lines
Am 04.05.2021 um 10:59 hat Michael S. Tsirkin geschrieben:
Quoted 8 lines · Kevin Wolf
Quoted 6 lines
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Quoted 6 lines
So there are several possible reasons for an error:
1. remote restarted - we would like to reconnect,
this was the original use-case for reconnect.I am not very happy that we are killing this usecase.
This patch is killing it only during initialisation, where it's quite unlikely compared to other cases and where the current implementation is rather broken. So reverting the broken feature and going back to a simpler correct state feels like a good idea to me.
Quoted 6 lines
Quoted 4 lines
The idea is to add the "retry during initialisation" feature back on top of this, but it requires some more changes in the error paths so that we can actually distinguish different kinds of errors and don't retry when we already know that it can't succeed.
Okay ... let's make all this explicit in the commit log though, ok?
That's fair, I'll add a paragraph addressing this case when merging the series, like this:
Note that this removes the ability to reconnect during initialisation (but not during operation) when there is no permanent error, but the backend restarts, as the implementation was buggy. This feature can be added back in a follow-up series after changing error paths to distinguish cases where retrying could help from cases with permanent errors.
Quoted 26 lines
Quoted 26 lines
Quoted 3 lines
2. qemu detected an error and closed the connection looks like we try to handle that by reconnect, this is something we should address.
Yes, if qemu produces the error locally, retrying is useless.
Quoted 11 lines
3. remote failed due to a bad command from qemu. this usecase isn't well supported at the moment.
How about supporting it on the remote side? I think that if the data is well-formed just has a configuration remote can not support then instead of closing the connection, remote can wait for commands with
need_replyset, and respond with an error. Or at least do it ifVHOST_USER_PROTOCOL_F_REPLY_ACKhas been negotiated. IfVHOST_USER_SET_VRING_ERRis used then signalling that fd might also be reasonable.Quoted 3 lines
OTOH if qemu is buggy and sends malformed data and remote detects that then hacing qemu retry forever is ok, might actually be benefitial for debugging.
I haven't really checked this case yet, it seems to be less common. Explicitly communicating an error is certainly better than just cutting the connection. But as you say, it means QEMU is buggy, so blindly retrying in this case is kind of acceptable.
Quoted 5 lines
Quoted 2 lines
Raphael suggested that we could limit the number of retries during initialisation so that it wouldn't result in a hang at least.
not sure how do I feel about random limits ... how would we set the limit?
To be honest, probably even 1 would already be good enough in practice. Make it 5 or something and you definitely cover any realistic case when there is no bug involved.
Even hitting this case once requires bad luck with the timing, so that the restart of the backend coincides with already having connected to the socket, but not completed the configuration yet, which is a really short window. Having the backend drop the connection again in the same short window on the second attempt is an almost sure sign of a bug with one of the operations done during initialisation.
Even if this corner case turned out to be a bit less unlikely to happen than I'm thinking (which is, it won't happen at all), randomly failing a device-add once in a while still feels a lot better than hanging the VM once in a while.
Kevin
Re: [PATCH v2 2/6] vhost-user-blk: Don't reconnect during initialisation
Quoted 83 lines · Kevin Wolf
Am 04.05.2021 um 11:44 hat Michael S. Tsirkin geschrieben:
Quoted 22 lines · Kevin Wolf
Quoted 21 lines
Am 04.05.2021 um 10:59 hat Michael S. Tsirkin geschrieben:
Quoted 8 lines · Kevin Wolf
Quoted 6 lines
This is a partial revert of commits
77542d43149andbc79c87bcde.Usually, an error during initialisation means that the configuration was wrong. Reconnecting won't make the error go away, but just turn the error condition into an endless loop. Avoid this and return errors again.
Quoted 6 lines
So there are several possible reasons for an error:
1. remote restarted - we would like to reconnect,
this was the original use-case for reconnect.I am not very happy that we are killing this usecase.
This patch is killing it only during initialisation, where it's quite unlikely compared to other cases and where the current implementation is rather broken. So reverting the broken feature and going back to a simpler correct state feels like a good idea to me.
Quoted 6 lines
Quoted 4 lines
The idea is to add the "retry during initialisation" feature back on top of this, but it requires some more changes in the error paths so that we can actually distinguish different kinds of errors and don't retry when we already know that it can't succeed.
Okay ... let's make all this explicit in the commit log though, ok?
That's fair, I'll add a paragraph addressing this case when merging the series, like this:
Note that this removes the ability to reconnect during initialisation (but not during operation) when there is no permanent error, but the backend restarts, as the implementation was buggy. This feature can be added back in a follow-up series after changing error paths to distinguish cases where retrying could help from cases with permanent errors.
Quoted 26 lines
Quoted 26 lines
Quoted 3 lines
2. qemu detected an error and closed the connection looks like we try to handle that by reconnect, this is something we should address.
Yes, if qemu produces the error locally, retrying is useless.
Quoted 11 lines
3. remote failed due to a bad command from qemu. this usecase isn't well supported at the moment.
How about supporting it on the remote side? I think that if the data is well-formed just has a configuration remote can not support then instead of closing the connection, remote can wait for commands with
need_replyset, and respond with an error. Or at least do it ifVHOST_USER_PROTOCOL_F_REPLY_ACKhas been negotiated. IfVHOST_USER_SET_VRING_ERRis used then signalling that fd might also be reasonable.Quoted 3 lines
OTOH if qemu is buggy and sends malformed data and remote detects that then hacing qemu retry forever is ok, might actually be benefitial for debugging.
I haven't really checked this case yet, it seems to be less common. Explicitly communicating an error is certainly better than just cutting the connection. But as you say, it means QEMU is buggy, so blindly retrying in this case is kind of acceptable.
Quoted 5 lines
Quoted 2 lines
Raphael suggested that we could limit the number of retries during initialisation so that it wouldn't result in a hang at least.
not sure how do I feel about random limits ... how would we set the limit?
To be honest, probably even 1 would already be good enough in practice. Make it 5 or something and you definitely cover any realistic case when there is no bug involved.
Even hitting this case once requires bad luck with the timing, so that the restart of the backend coincides with already having connected to the socket, but not completed the configuration yet, which is a really short window. Having the backend drop the connection again in the same short window on the second attempt is an almost sure sign of a bug with one of the operations done during initialisation.
Quoted 6 lines
Even if this corner case turned out to be a bit less unlikely to happen than I'm thinking (which is, it won't happen at all), randomly failing a device-add once in a while still feels a lot better than hanging the VM once in a while.
Kevin
Well if backend is e.g. just stuck and connection does not close, then VM hangs anyway. So IMHO it's not such a big deal. If we really want to address this we should handle all this asynchronously. As in make device-add succeed and then progress in stages but do not block the monitor. That would be nice but it's a big change in the code.
Signature · 1 line
MST
Quoted 7 lines · Kevin Wolf
vhost-user-blk neglects for several properties to check whether the configured value is even compatible with the backend. This results sometimes in crashes because of buggy error handling code, and sometimes in devices that are presented differently to the guest than the backend would expect and that don't work properly therefore.
This series fixes some of these bugs.
OK so where is this going? Kevin you said you will merge? If so
Quoted 21 lines
v2:
- Fix error paths inrealize()that didn't set errp
- Addedvhost_dev_cleanup()back in the error path (more faithful revert of77542d43149)Kevin Wolf (6):
vhost-user-blk: Make sure to set Error on realize failure
vhost-user-blk: Don't reconnect during initialisation
vhost-user-blk: Improve error reporting in realize
vhost-user-blk: Get more feature flags from vhost device
virtio: Fail ifiommu_platformis requested, but unsupported
vhost-user-blk: Check that num-queues is supported by backendinclude/hw/virtio/vhost.h | 2 + hw/block/vhost-user-blk.c | 85 ++++++++++++++------------------------- hw/virtio/vhost-user.c | 5 +++ hw/virtio/virtio-bus.c | 5 +++ 4 files changed, 42 insertions(+), 55 deletions(-)Signature · 1 line
2.30.2
Am 14.05.2021 um 14:20 hat Michael S. Tsirkin geschrieben:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
vhost-user-blk neglects for several properties to check whether the configured value is even compatible with the backend. This results sometimes in crashes because of buggy error handling code, and sometimes in devices that are presented differently to the guest than the backend would expect and that don't work properly therefore.
Quoted 6 lines
Quoted 1 line
This series fixes some of these bugs.
OK so where is this going? Kevin you said you will merge? If so
Reviewed-by Michael S. Tsirkin <[email protected]>
Thanks, Michael. I've applied it to my block tree and am preparing a pull request now.
Kevin