vhost: enable vrings in vhost_dev_start() for vhost-user devices
PATCH 1/1 vhost: enable vrings in vhost_dev_start() for vhost-user devices
Commit 02b61f38d3 ("hw/virtio: incorporate backend features in features") properly negotiates VHOST_USER_F_PROTOCOL_FEATURES with the vhost-user backend, but we forgot to enable vrings as specified in docs/interop/vhost-user.rst:
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling vhost_ops.vhost_set_vring_enable() directly:
- backends/cryptodev-vhost.c
- hw/net/virtio-net.c
- hw/virtio/vhost-user-gpio.c
But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in vhost_dev_start() for vhost-user front-ends that don't already do this directly. Same thing also in vhost_dev_stop() where we disable vrings.
[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217
--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)
include/hw/virtio/vhost.h +4 -2
@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling
backends/cryptodev-vhost.c +2 -2
@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }
@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }
backends/vhost-user.c +2 -2
@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;
@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,
hw/block/vhost-user-blk.c +2 -2
@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;
@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {
hw/net/vhost_net.c +4 -4
@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }
@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:
@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }
@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }
hw/scsi/vhost-scsi-common.c +2 -2
@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;
@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);
hw/virtio/vhost-user-fs.c +2 -2
@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;
@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {
hw/virtio/vhost-user-gpio.c +2 -2
@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;
@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {
hw/virtio/vhost-user-i2c.c +2 -2
@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;
@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {
hw/virtio/vhost-user-rng.c +2 -2
@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;
@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {
hw/virtio/vhost-vsock-common.c +2 -2
@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;
@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {
hw/virtio/vhost.c +39 -5
88 lines not on this page · raw message
hw/virtio/trace-events +2 -2
@@ -9,8 +9,8 @@ vhost_section(const char *name) "%s" vhost_reject_section(const char *name, int d) "%s:%d" vhost_iotlb_miss(void *dev, int step) "%p step %d" vhost_dev_cleanup(void *dev) "%p" -vhost_dev_start(void *dev, const char *name) "%p:%s" -vhost_dev_stop(void *dev, const char *name) "%p:%s" +vhost_dev_start(void *dev, const char *name, bool vrings) "%p:%s vrings:%d" +vhost_dev_stop(void *dev, const char *name, bool vrings) "%p:%s vrings:%d" # vhost-user.c
Signature · 1 line
2.38.1
Quoted 15 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Quoted 5 lines
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.c
To simplify why not rather change these devices to use the new semantics?
Quoted 11 lines
But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.
Quoted 6 lines
[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>
Looks good for vhost-user-blk/vhost-user-scsi.
Quoted 401 lines
---
include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +2
-2@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);
fail_start:vhost_dev_disable_notifiers(&net->dev, dev);
fail_notifiers:hw/net/vhost_net.c +2
-2@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +34
-3@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; } +static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable) +{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* +* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + } + +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +} +/* Host notifiers must be enabled at this point. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev) +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings) {int i, r;/* should only be called after backend is connected */ assert (hdev->vhost_ops); -trace_vhost_dev_start (hdev, vdev->name); +trace_vhost_dev_start (hdev, vdev->name, vrings); vdev->vhost_started = true; hdev->started = true;@@ -1830,10 +1851,16 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) goto fail_log; } } +if (vrings) { + r =vhost_dev_set_vring_enable (hdev, true); +if (r) { + goto fail_log; + } + }if (hdev->vhost_ops->vhost_dev_start) { r = hdev->vhost_ops->vhost_dev_start (hdev, true);if (r) { - goto fail_log; + goto fail_start; } } if (vhost_dev_has_iommu(hdev) &&@@ -1848,6 +1875,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) } }return 0 ; +fail_start: +if (vrings) { +vhost_dev_set_vring_enable (hdev, false); + }
fail_log:vhost_log_put(hdev, false);
fail_vq:hw/virtio/vhost.c +5
-2@@ -1866,18 +1897,21 @@ fail_features: }/* Host notifiers must be enabled at this point. */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev) +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings) {int i;/* should only be called after backend is connected */ assert (hdev->vhost_ops); -trace_vhost_dev_stop (hdev, vdev->name); +trace_vhost_dev_stop (hdev, vdev->name, vrings);if (hdev->vhost_ops->vhost_dev_start) { hdev->vhost_ops->vhost_dev_start (hdev, false); } +if (vrings) { +vhost_dev_set_vring_enable (hdev, false); + }for (i =0 ; i < hdev->nvqs; ++i) { vhost_virtqueue_stop(hdev, vdev,hw/virtio/trace-events +0
-0@@ -9,8 +9,8 @@ vhost_section(const char *name) "%s"
vhost_reject_section(const char *name, int d)"%s:%d"vhost_iotlb_miss(void *dev, int step)"%p step %d"vhost_dev_cleanup(void *dev)"%p"hw/virtio/trace-events +2
-2-vhost_dev_start(void *dev, const char *name) "%p:%s" -vhost_dev_stop(void *dev, const char *name) "%p:%s" +vhost_dev_start(void *dev, const char *name, bool vrings) "%p:%s vrings:%d" +vhost_dev_stop(void *dev, const char *name, bool vrings) "%p:%s vrings:%d"# vhost-user.cSignature · 1 line
2.38.1
Quoted 15 lines · Raphael Norwitz
Quoted 15 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Quoted 7 lines
Quoted 5 lines
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cTo simplify why not rather change these devices to use the new semantics?
Granted this is already scary enough for this release.
Quoted 423 lines
Quoted 11 lines
But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.Quoted 6 lines
[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>Looks good for vhost-user-blk/vhost-user-scsi.
Acked-by Raphael Norwitz <[email protected]>Quoted 401 lines
---
include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +2
-2@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);
fail_start:vhost_dev_disable_notifiers(&net->dev, dev);
fail_notifiers:hw/net/vhost_net.c +2
-2@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +34
-3@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; } +static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable) +{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* +* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + } + +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +} +/* Host notifiers must be enabled at this point. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev) +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings) {int i, r;/* should only be called after backend is connected */ assert (hdev->vhost_ops); -trace_vhost_dev_start (hdev, vdev->name); +trace_vhost_dev_start (hdev, vdev->name, vrings); vdev->vhost_started = true; hdev->started = true;@@ -1830,10 +1851,16 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) goto fail_log; } } +if (vrings) { + r =vhost_dev_set_vring_enable (hdev, true); +if (r) { + goto fail_log; + } + }if (hdev->vhost_ops->vhost_dev_start) { r = hdev->vhost_ops->vhost_dev_start (hdev, true);if (r) { - goto fail_log; + goto fail_start; } } if (vhost_dev_has_iommu(hdev) &&@@ -1848,6 +1875,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) } }return 0 ; +fail_start: +if (vrings) { +vhost_dev_set_vring_enable (hdev, false); + }
fail_log:vhost_log_put(hdev, false);
fail_vq:hw/virtio/vhost.c +5
-2@@ -1866,18 +1897,21 @@ fail_features: }/* Host notifiers must be enabled at this point. */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev) +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings) {int i;/* should only be called after backend is connected */ assert (hdev->vhost_ops); -trace_vhost_dev_stop (hdev, vdev->name); +trace_vhost_dev_stop (hdev, vdev->name, vrings);if (hdev->vhost_ops->vhost_dev_start) { hdev->vhost_ops->vhost_dev_start (hdev, false); } +if (vrings) { +vhost_dev_set_vring_enable (hdev, false); + }for (i =0 ; i < hdev->nvqs; ++i) { vhost_virtqueue_stop(hdev, vdev,hw/virtio/trace-events +0
-0@@ -9,8 +9,8 @@ vhost_section(const char *name) "%s"
vhost_reject_section(const char *name, int d)"%s:%d"vhost_iotlb_miss(void *dev, int step)"%p step %d"vhost_dev_cleanup(void *dev)"%p"hw/virtio/trace-events +2
-2-vhost_dev_start(void *dev, const char *name) "%p:%s" -vhost_dev_stop(void *dev, const char *name) "%p:%s" +vhost_dev_start(void *dev, const char *name, bool vrings) "%p:%s vrings:%d" +vhost_dev_stop(void *dev, const char *name, bool vrings) "%p:%s vrings:%d"# vhost-user.cSignature · 1 line
2.38.1
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Raphael Norwitz
Quoted 15 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cTo simplify why not rather change these devices to use the new semantics?
Maybe the only one I wouldn't be scared of is vhost-user-gpio, but for example vhost-net would require a lot of changes, maybe better after the release.
For example, we could do like vhost-vdpa and call SET_VRING_ENABLE in the VhostOps.vhost_dev_start callback of vhost-user.c, but I think it's too risky to do that now.
Quoted 2 lines
Granted this is already scary enough for this release.
Yeah, I tried to touch as little as possible but I'm scared too, I just haven't found a better solution for now :-(
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>
Quoted 3 lines
Quoted 3 lines
Looks good for vhost-user-blk/vhost-user-scsi.
Acked-by Raphael Norwitz <[email protected]>
Thanks for the review!
Stefano
Quoted 29 lines · Stefano Garzarella
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Raphael Norwitz
Quoted 15 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cTo simplify why not rather change these devices to use the new semantics?
Quoted 9 lines
Maybe the only one I wouldn't be scared of is vhost-user-gpio, but for example vhost-net would require a lot of changes, maybe better after the release.
For example, we could do like vhost-vdpa and call
SET_VRING_ENABLEin the VhostOps.vhost_dev_start callback ofvhost-user.c, but I think it's too risky to do that now.Quoted 2 lines
Granted this is already scary enough for this release.
Yeah, I tried to touch as little as possible but I'm scared too, I just haven't found a better solution for now :-(
Sure - no need to force a more disruptive change in right before the release. If anything can be simplified later.
Quoted 23 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
[1] https://urldefense.proofpoint.com/v2/url?u=https-3A__gitlab.com_virtio-2Dfs_virtiofsd&d=DwIBAg&c=s883GpUCOChKOHiocYtGcg&r=In4gmR1pGzKB8G5p6LUrWqkSMec2L5EtXZow_FZNJZk&m=PcC4TEq5C80Knek-ScCNI26rQ13h0n3QEMNNhc-ENd7Txd8wHYqwC1TYfXW_hYor&s=5pdxt8m4-ks8VB2tRSXQV05kdfdP50iy-aAxuGe-Ffc&e= > [2] https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_rust-2Dvmm_vhost_blob_240fc2966_crates_vhost-2Duser-2Dbackend_src_event-5Floop.rs-23L217&d=DwIBAg&c=s883GpUCOChKOHiocYtGcg&r=In4gmR1pGzKB8G5p6LUrWqkSMec2L5EtXZow_FZNJZk&m=PcC4TEq5C80Knek-ScCNI26rQ13h0n3QEMNNhc-ENd7Txd8wHYqwC1TYfXW_hYor&s=-3NUG1pPKN-FwUeDkuu52roXoPoeLR1y4gjrddHUz2U&e= > Fixes:
02b61f38d3("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>Looks good for vhost-user-blk/vhost-user-scsi.
Acked-by Raphael Norwitz <[email protected]>Thanks for the review!
Stefano
Quoted 327 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cBut most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +4
-4@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +0
-0
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1780
Quoted 4 lines
hw/virtio/vhost.c +1
-0@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; } +static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable)
There is a similarly-named vhost_set_vring_enable(NetClientState *nc, int enable) function which is actually part of vhost_net. Please rename it to vhost_net_set_vring_enable(). It should probably call vhost_dev_set_vring_enable().
Quoted 6 lines
hw/virtio/vhost.c +6
-0+{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/*
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1796
Quoted 10 lines
hw/virtio/vhost.c +10
-0+* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + }
These semantics are the opposite of vhost_user_set_vring_enable():
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
return -EINVAL;
}Please make vhost_user_set_vring_enable() and vhost_dev_set_vring_enable() consistent. Code gets really confusing when layers have different semantics for the same operation.
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1799
Quoted 3 lines
hw/virtio/vhost.c +3
-0+ +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +}
The return value is hard to understand. An error return is only returned by vhost-user devices with VHOST_USER_F_PROTOCOL_FEATURES. There are other cases that seem like they should return an error but return success instead. For example, when called with enable=false on a non-VHOST_USER_F_PROTOCOL_FEATURES device (e.g. vhost-kernel or legacy vhost-user) we return success even though the vring wasn't disabled.
I know a quick fix is needed for 7.2, but I don't think this patch should be merged in its current state. Given that this is a new function, it should be possible to come up with straightforward return value semantics.
Quoted 86 lines
hw/virtio/vhost.c +19
-5+/* Host notifiers must be enabled at this point. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev) +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings) {int i, r;/* should only be called after backend is connected */ assert (hdev->vhost_ops); -trace_vhost_dev_start (hdev, vdev->name); +trace_vhost_dev_start (hdev, vdev->name, vrings); vdev->vhost_started = true; hdev->started = true;@@ -1830,10 +1851,16 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) goto fail_log; } } +if (vrings) { + r =vhost_dev_set_vring_enable (hdev, true); +if (r) { + goto fail_log; + } + }if (hdev->vhost_ops->vhost_dev_start) { r = hdev->vhost_ops->vhost_dev_start (hdev, true);if (r) { - goto fail_log; + goto fail_start; } } if (vhost_dev_has_iommu(hdev) &&@@ -1848,6 +1875,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) } }return 0 ; +fail_start: +if (vrings) { +vhost_dev_set_vring_enable (hdev, false); + } fail_log:vhost_log_put (hdev, false); fail_vq:@@ -1866,18 +1897,21 @@ fail_features: }/* Host notifiers must be enabled at this point. */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev) +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings) {int i;/* should only be called after backend is connected */ assert (hdev->vhost_ops); -trace_vhost_dev_stop (hdev, vdev->name); +trace_vhost_dev_stop (hdev, vdev->name, vrings);if (hdev->vhost_ops->vhost_dev_start) { hdev->vhost_ops->vhost_dev_start (hdev, false); } +if (vrings) { +vhost_dev_set_vring_enable (hdev, false); + }for (i =0 ; i < hdev->nvqs; ++i) { vhost_virtqueue_stop(hdev, vdev,hw/virtio/trace-events +2
-2@@ -9,8 +9,8 @@ vhost_section(const char *name) "%s" vhost_reject_section(const char *name, int d) "%s:%d" vhost_iotlb_miss(void *dev, int step) "%p step %d" vhost_dev_cleanup(void *dev) "%p" -vhost_dev_start(void *dev, const char *name) "%p:%s" -vhost_dev_stop(void *dev, const char *name) "%p:%s" +vhost_dev_start(void *dev, const char *name, bool vrings) "%p:%s vrings:%d" +vhost_dev_stop(void *dev, const char *name, bool vrings) "%p:%s vrings:%d" # vhost-user.cSignature · 1 line
2.38.1
Quoted 331 lines · Stefan Hajnoczi
Quoted 330 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cBut most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +4
-4@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +0
-0@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; }
Quoted 5 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1780
Quoted 1 line
hw/virtio/vhost.c +1
-0+static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable)There is a similarly-named
vhost_set_vring_enable(NetClientState *nc, int enable)function which is actually part ofvhost_net. Please rename it tovhost_net_set_vring_enable().
Should I rename it in this patch?
Quoted 2 lines
It should probably call
vhost_dev_set_vring_enable().
Ehm, the idea of this patch was to touch as little as possible to avoid new regressions.
Also, the semantics of vhost_dev_set_vring_enable() was meant to keep vhost_dev_start()/vhost_dev_stop() simple, not to be exposed to frontends. (maybe I should have written it, sorry about that).
However I agree that we should clean up vhost-net and also the other frontends as Raphael also suggested, but honestly I'm scared to do that now in this patch...
What I would have wanted to do, would be similar to what we do for vhost-vdpa: call SET_VRING_ENABLE in the vhost_ops->vhost_dev_start() callback of vhost-user.c. Removing all the call to vhost_ops->vhost_set_vring_enable() in the frontends, but I think it's too risky to do that now.
Quoted 20 lines
Quoted 7 lines
hw/virtio/vhost.c +6
-0+{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1796
Quoted 10 lines
hw/virtio/vhost.c +10
-0+* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + }These semantics are the opposite of
vhost_user_set_vring_enable():
Quoted 7 lines
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { return -EINVAL;}
Please make
vhost_user_set_vring_enable()andvhost_dev_set_vring_enable()consistent. Code gets really confusing when layers have different semantics for the same operation.
It's the opposite precisely because we shouldn't let vhost_dev_start()/vhost_dev_stop() fail if vhost_ops->vhost_set_vring_enable() can't be called because it would fail.
If I do it this way, then I have to put the check inside vhost_dev_start()/vhost_dev_stop(), and at this point I remove the function that would be useless (just a wrapper of hdev->vhost_ops->vhost_set_vring_enable). Actually this was the first implementation I did, then I added the function just to have vhost_dev_start()/vhost_dev_stop() cleaner and to avoid duplicating the check.
Quoted 11 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1799
Quoted 4 lines
hw/virtio/vhost.c +3
-0+ +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +}The return value is hard to understand. An error return is only returned by vhost-user devices with
VHOST_USER_F_PROTOCOL_FEATURES. There are other cases that seem like they should return an error but return success instead. For example, when called with enable=false on a non-VHOST_USER_F_PROTOCOL_FEATURES device (e.g. vhost-kernel or legacy vhost-user) we return success even though the vring wasn't disabled.
As I explained above, the idea was not to expose this function outside, but to use it only in vhost_dev_start()/vhost_dev_stop(). So the return value is 0 both when it has successes and when there is no need/way to enable/disable the vrings.
Perhaps since it is confusing, I will remove the function and put the code directly into vhost_dev_start()/vhost_dev_stop().
What do you think?
Thanks,
Stefano
Quoted 417 lines · Stefano Garzarella
Quoted 331 lines · Stefan Hajnoczi
Quoted 330 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cBut most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +4
-4@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +0
-0@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; }Quoted 5 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1780
Quoted 1 line
hw/virtio/vhost.c +1
-0+static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable)There is a similarly-named
vhost_set_vring_enable(NetClientState *nc, int enable)function which is actually part ofvhost_net. Please rename it tovhost_net_set_vring_enable().Should I rename it in this patch?
Quoted 2 lines
It should probably call
vhost_dev_set_vring_enable().Ehm, the idea of this patch was to touch as little as possible to avoid new regressions.
Also, the semantics of
vhost_dev_set_vring_enable()was meant to keep vhost_dev_start()/vhost_dev_stop() simple, not to be exposed to frontends. (maybe I should have written it, sorry about that).However I agree that we should clean up vhost-net and also the other frontends as Raphael also suggested, but honestly I'm scared to do that now in this patch...
What I would have wanted to do, would be similar to what we do for
vhost-vdpa: callSET_VRING_ENABLEin thevhost_ops->vhost_dev_start()callback ofvhost-user.c.
Removing all the call tovhost_ops->vhost_set_vring_enable()in the frontends, but I think it's too risky to do that now.Quoted 20 lines
Quoted 7 lines
hw/virtio/vhost.c +6
-0+{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1796
Quoted 10 lines
hw/virtio/vhost.c +10
-0+* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + }These semantics are the opposite of
vhost_user_set_vring_enable():Quoted 7 lines
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { return -EINVAL;}
Please make
vhost_user_set_vring_enable()andvhost_dev_set_vring_enable()consistent. Code gets really confusing when layers have different semantics for the same operation.It's the opposite precisely because we shouldn't let vhost_dev_start()/vhost_dev_stop() fail if
vhost_ops->vhost_set_vring_enable()can't be called because it would fail.If I do it this way, then I have to put the check inside vhost_dev_start()/vhost_dev_stop(), and at this point I remove the function that would be useless (just a wrapper of
hdev->vhost_ops->vhost_set_vring_enable).
Actually this was the first implementation I did, then I added the function just to have vhost_dev_start()/vhost_dev_stop() cleaner and to avoid duplicating the check.Quoted 11 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1799
Quoted 4 lines
hw/virtio/vhost.c +3
-0+ +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +}The return value is hard to understand. An error return is only returned by vhost-user devices with
VHOST_USER_F_PROTOCOL_FEATURES. There are other cases that seem like they should return an error but return success instead. For example, when called with enable=false on a non-VHOST_USER_F_PROTOCOL_FEATURES device (e.g. vhost-kernel or legacy vhost-user) we return success even though the vring wasn't disabled.As I explained above, the idea was not to expose this function outside, but to use it only in vhost_dev_start()/vhost_dev_stop(). So the return value is 0 both when it has successes and when there is no need/way to enable/disable the vrings.
Quoted 4 lines
Perhaps since it is confusing, I will remove the function and put the code directly into vhost_dev_start()/vhost_dev_stop().
What do you think?
It's late now. We can merge it as-is.
I think this patch makes the vhost code even harder to understand and it's important to do the clean ups that have been discussed for 8.0. Will you work on the changes we discussed for 8.0?
Thanks,
Stefan
Quoted 421 lines · Stefan Hajnoczi
Quoted 420 lines · Stefano Garzarella
Quoted 331 lines · Stefan Hajnoczi
Quoted 330 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cBut most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +4
-4@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +0
-0@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; }Quoted 5 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1780
Quoted 1 line
hw/virtio/vhost.c +1
-0+static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable)There is a similarly-named
vhost_set_vring_enable(NetClientState *nc, int enable)function which is actually part ofvhost_net. Please rename it tovhost_net_set_vring_enable().Should I rename it in this patch?
Quoted 2 lines
It should probably call
vhost_dev_set_vring_enable().Ehm, the idea of this patch was to touch as little as possible to avoid new regressions.
Also, the semantics of
vhost_dev_set_vring_enable()was meant to keep vhost_dev_start()/vhost_dev_stop() simple, not to be exposed to frontends. (maybe I should have written it, sorry about that).However I agree that we should clean up vhost-net and also the other frontends as Raphael also suggested, but honestly I'm scared to do that now in this patch...
What I would have wanted to do, would be similar to what we do for
vhost-vdpa: callSET_VRING_ENABLEin thevhost_ops->vhost_dev_start()callback ofvhost-user.c.
Removing all the call tovhost_ops->vhost_set_vring_enable()in the frontends, but I think it's too risky to do that now.Quoted 20 lines
Quoted 7 lines
hw/virtio/vhost.c +6
-0+{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1796
Quoted 10 lines
hw/virtio/vhost.c +10
-0+* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + }These semantics are the opposite of
vhost_user_set_vring_enable():Quoted 7 lines
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { return -EINVAL;}
Please make
vhost_user_set_vring_enable()andvhost_dev_set_vring_enable()consistent. Code gets really confusing when layers have different semantics for the same operation.It's the opposite precisely because we shouldn't let vhost_dev_start()/vhost_dev_stop() fail if
vhost_ops->vhost_set_vring_enable()can't be called because it would fail.If I do it this way, then I have to put the check inside vhost_dev_start()/vhost_dev_stop(), and at this point I remove the function that would be useless (just a wrapper of
hdev->vhost_ops->vhost_set_vring_enable).
Actually this was the first implementation I did, then I added the function just to have vhost_dev_start()/vhost_dev_stop() cleaner and to avoid duplicating the check.Quoted 11 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1799
Quoted 4 lines
hw/virtio/vhost.c +3
-0+ +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +}The return value is hard to understand. An error return is only returned by vhost-user devices with
VHOST_USER_F_PROTOCOL_FEATURES. There are other cases that seem like they should return an error but return success instead. For example, when called with enable=false on a non-VHOST_USER_F_PROTOCOL_FEATURES device (e.g. vhost-kernel or legacy vhost-user) we return success even though the vring wasn't disabled.As I explained above, the idea was not to expose this function outside, but to use it only in vhost_dev_start()/vhost_dev_stop(). So the return value is 0 both when it has successes and when there is no need/way to enable/disable the vrings.
Perhaps since it is confusing, I will remove the function and put the code directly into vhost_dev_start()/vhost_dev_stop().
Quoted 6 lines
Quoted 1 line
What do you think?
It's late now. We can merge it as-is.
I think this patch makes the vhost code even harder to understand and it's important to do the clean ups that have been discussed for 8.0.
Yes I agree. Only acked because we don't have better ideas on how to fix vmstate issues without breaking CI.
Quoted 4 lines
Will you work on the changes we discussed for 8.0?
Thanks,
Stefan
Signature · 1 line
MST
Quoted 421 lines · Stefan Hajnoczi
Quoted 420 lines · Stefano Garzarella
Quoted 331 lines · Stefan Hajnoczi
Quoted 330 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cBut most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.[1] https://gitlab.com/virtio-fs/virtiofsd
[2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +4
-4@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +0
-0@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; }Quoted 5 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1780
Quoted 1 line
hw/virtio/vhost.c +1
-0+static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable)There is a similarly-named
vhost_set_vring_enable(NetClientState *nc, int enable)function which is actually part ofvhost_net. Please rename it tovhost_net_set_vring_enable().Should I rename it in this patch?
Quoted 2 lines
It should probably call
vhost_dev_set_vring_enable().Ehm, the idea of this patch was to touch as little as possible to avoid new regressions.
Also, the semantics of
vhost_dev_set_vring_enable()was meant to keep vhost_dev_start()/vhost_dev_stop() simple, not to be exposed to frontends. (maybe I should have written it, sorry about that).However I agree that we should clean up vhost-net and also the other frontends as Raphael also suggested, but honestly I'm scared to do that now in this patch...
What I would have wanted to do, would be similar to what we do for
vhost-vdpa: callSET_VRING_ENABLEin thevhost_ops->vhost_dev_start()callback ofvhost-user.c.
Removing all the call tovhost_ops->vhost_set_vring_enable()in the frontends, but I think it's too risky to do that now.Quoted 20 lines
Quoted 7 lines
hw/virtio/vhost.c +6
-0+{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1796
Quoted 10 lines
hw/virtio/vhost.c +10
-0+* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + }These semantics are the opposite of
vhost_user_set_vring_enable():Quoted 7 lines
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { return -EINVAL;}
Please make
vhost_user_set_vring_enable()andvhost_dev_set_vring_enable()consistent. Code gets really confusing when layers have different semantics for the same operation.It's the opposite precisely because we shouldn't let vhost_dev_start()/vhost_dev_stop() fail if
vhost_ops->vhost_set_vring_enable()can't be called because it would fail.If I do it this way, then I have to put the check inside vhost_dev_start()/vhost_dev_stop(), and at this point I remove the function that would be useless (just a wrapper of
hdev->vhost_ops->vhost_set_vring_enable).
Actually this was the first implementation I did, then I added the function just to have vhost_dev_start()/vhost_dev_stop() cleaner and to avoid duplicating the check.Quoted 11 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1799
Quoted 4 lines
hw/virtio/vhost.c +3
-0+ +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +}The return value is hard to understand. An error return is only returned by vhost-user devices with
VHOST_USER_F_PROTOCOL_FEATURES. There are other cases that seem like they should return an error but return success instead. For example, when called with enable=false on a non-VHOST_USER_F_PROTOCOL_FEATURES device (e.g. vhost-kernel or legacy vhost-user) we return success even though the vring wasn't disabled.As I explained above, the idea was not to expose this function outside, but to use it only in vhost_dev_start()/vhost_dev_stop(). So the return value is 0 both when it has successes and when there is no need/way to enable/disable the vrings.
Perhaps since it is confusing, I will remove the function and put the code directly into vhost_dev_start()/vhost_dev_stop().
Quoted 6 lines
Quoted 1 line
What do you think?
It's late now. We can merge it as-is.
I think this patch makes the vhost code even harder to understand and it's important to do the clean ups that have been discussed for 8.0.
Agree.
Quoted 1 line
Will you work on the changes we discussed for 8.0?
Yep, sure.
I will try to unify all vhost/vhost-user devices.
Now I think it's also a mess because the devices do different things, we should have everything in the core.
Thanks,
Stefano
Quoted 430 lines · Stefano Garzarella
Quoted 27 lines · Stefan Hajnoczi
Quoted 26 lines · Stefano Garzarella
Quoted 25 lines · Stefan Hajnoczi
Quoted 24 lines · Stefano Garzarella
Commit
02b61f38d3("hw/virtio: incorporate backend features in features") properly negotiatesVHOST_USER_F_PROTOCOL_FEATURESwith the vhost-user backend, but we forgot to enable vrings as specified indocs/interop/vhost-user.rst:If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state.
If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1.
Some vhost-user front-ends already did this by calling
vhost_ops.vhost_set_vring_enable()directly:
-backends/cryptodev-vhost.c
-hw/net/virtio-net.c
-hw/virtio/vhost-user-gpio.cBut most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Let's fix this issue by enabling the vrings in
vhost_dev_start()for vhost-user front-ends that don't already do this directly. Same thing also invhost_dev_stop()where we disable vrings.https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217
Quoted 388 lines
Quoted 388 lines
Quoted 300 lines
Quoted 300 lines
Fixes 02b61f38d3 ("hw/virtio: incorporate backend features in features")Reported-by German Maglione <[email protected]>Tested-by German Maglione <[email protected]>Signed-off-by Stefano Garzarella <[email protected]>--- include/hw/virtio/vhost.h | 6 +++-- backends/cryptodev-vhost.c | 4 ++-- backends/vhost-user.c | 4 ++-- hw/block/vhost-user-blk.c | 4 ++-- hw/net/vhost_net.c | 8 +++---- hw/scsi/vhost-scsi-common.c | 4 ++-- hw/virtio/vhost-user-fs.c | 4 ++-- hw/virtio/vhost-user-gpio.c | 4 ++-- hw/virtio/vhost-user-i2c.c | 4 ++-- hw/virtio/vhost-user-rng.c | 4 ++-- hw/virtio/vhost-vsock-common.c | 4 ++-- hw/virtio/vhost.c | 44 ++++++++++++++++++++++++++++++---- hw/virtio/trace-events | 4 ++-- 13 files changed, 67 insertions(+), 31 deletions(-)include/hw/virtio/vhost.h +4
-2@@ -184,24 +184,26 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)* vhost_dev_start() - start the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings enabled in this call * * Starts the vhost device. From this point VirtIO feature negotiation * can start and the device can start processing VirtIO transactions. * * Return: 0 on success, < 0 on error. */ -int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev); +int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * vhost_dev_stop() - stop the vhost device * @hdev: common vhost_dev structure * @vdev: the VirtIODevice structure +* @vrings: true to have vrings disabled in this call * * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). */ -void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev,bool vrings);/** * DOC: vhost device configuration handling backends/cryptodev-vhost.c +2
-2@@ -94,7 +94,7 @@ cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto, goto fail_notifiers; } - r =vhost_dev_start (&crypto->dev, dev); + r =vhost_dev_start (&crypto->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -111,7 +111,7 @@ static voidcryptodev_vhost_stop_one (CryptoDevBackendVhost *crypto, VirtIODevice *dev) { -vhost_dev_stop (&crypto->dev, dev); +vhost_dev_stop (&crypto->dev, dev, false);vhost_dev_disable_notifiers (&crypto->dev, dev); }backends/vhost-user.c +2
-2@@ -85,7 +85,7 @@ vhost_user_backend_start(VhostUserBackend *b) } b->dev.acked_features = b->vdev->guest_features; - ret =vhost_dev_start (&b->dev, b->vdev); + ret =vhost_dev_start (&b->dev, b->vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -120,7 +120,7 @@ vhost_user_backend_stop(VhostUserBackend *b)return ; } -vhost_dev_stop (&b->dev, b->vdev); +vhost_dev_stop (&b->dev, b->vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers(qbus->parent,hw/block/vhost-user-blk.c +2
-2@@ -178,7 +178,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) } s->dev.vq_index_end = s->dev.nvqs; - ret =vhost_dev_start (&s->dev, vdev); + ret =vhost_dev_start (&s->dev, vdev, true);if (ret <0 ) {error_setg_errno (errp, -ret,"Error starting vhost" ); goto err_guest_notifiers;@@ -213,7 +213,7 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&s->dev, vdev); +vhost_dev_stop (&s->dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, s->dev.nvqs, false);if (ret <0 ) {hw/net/vhost_net.c +4
-4@@ -259,7 +259,7 @@ static int vhost_net_start_one(struct vhost_net *net, goto fail_notifiers; } - r =vhost_dev_start (&net->dev, dev); + r =vhost_dev_start (&net->dev, dev, false);if (r <0 ) { goto fail_start; }@@ -308,7 +308,7 @@ fail:if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false); fail_start:vhost_dev_disable_notifiers (&net->dev, dev); fail_notifiers:@@ -329,7 +329,7 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } -vhost_dev_stop (&net->dev, dev); +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc); }@@ -606,7 +606,7 @@ err_start:assert (r >=0 ); } -vhost_dev_stop (&net->dev, vdev); +vhost_dev_stop (&net->dev, vdev, false);return r; }hw/scsi/vhost-scsi-common.c +2
-2@@ -68,7 +68,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) goto err_guest_notifiers; } - ret =vhost_dev_start (&vsc->dev, vdev); + ret =vhost_dev_start (&vsc->dev, vdev, true);if (ret <0 ) {error_report ("Error start vhost dev" ); goto err_guest_notifiers;@@ -101,7 +101,7 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc)VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int ret =0 ; -vhost_dev_stop (&vsc->dev, vdev); +vhost_dev_stop (&vsc->dev, vdev, true);if (k->set_guest_notifiers) { ret = k->set_guest_notifiers (qbus->parent, vsc->dev.nvqs, false);hw/virtio/vhost-user-fs.c +2
-2@@ -76,7 +76,7 @@ static void vuf_start(VirtIODevice *vdev) } fs->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&fs->vhost_dev, vdev); + ret =vhost_dev_start (&fs->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -110,7 +110,7 @@ static void vuf_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&fs->vhost_dev, vdev); +vhost_dev_stop (&fs->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, fs->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-gpio.c +2
-2@@ -81,7 +81,7 @@ static int vu_gpio_start(VirtIODevice *vdev)*/ vhost_ack_features (&gpio->vhost_dev, feature_bits, vdev->guest_features); - ret =vhost_dev_start (&gpio->vhost_dev, vdev); + ret =vhost_dev_start (&gpio->vhost_dev, vdev, false);if (ret <0 ) {error_report ("Error starting vhost-user-gpio: %d" , ret); goto err_guest_notifiers;@@ -139,7 +139,7 @@ static void vu_gpio_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (vhost_dev, vdev); +vhost_dev_stop (vhost_dev, vdev, false); ret = k->set_guest_notifiers (qbus->parent, vhost_dev->nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-i2c.c +2
-2@@ -46,7 +46,7 @@ static void vu_i2c_start(VirtIODevice *vdev) i2c->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&i2c->vhost_dev, vdev); + ret =vhost_dev_start (&i2c->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-i2c: %d" , -ret); goto err_guest_notifiers;@@ -80,7 +80,7 @@ static void vu_i2c_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&i2c->vhost_dev, vdev); +vhost_dev_stop (&i2c->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-user-rng.c +2
-2@@ -47,7 +47,7 @@ static void vu_rng_start(VirtIODevice *vdev) } rng->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&rng->vhost_dev, vdev); + ret =vhost_dev_start (&rng->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost-user-rng: %d" , -ret); goto err_guest_notifiers;@@ -81,7 +81,7 @@ static void vu_rng_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&rng->vhost_dev, vdev); +vhost_dev_stop (&rng->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, rng->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost-vsock-common.c +2
-2@@ -70,7 +70,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev) } vvc->vhost_dev.acked_features = vdev->guest_features; - ret =vhost_dev_start (&vvc->vhost_dev, vdev); + ret =vhost_dev_start (&vvc->vhost_dev, vdev, true);if (ret <0 ) {error_report ("Error starting vhost: %d" , -ret); goto err_guest_notifiers;@@ -105,7 +105,7 @@ void vhost_vsock_common_stop(VirtIODevice *vdev)return ; } -vhost_dev_stop (&vvc->vhost_dev, vdev); +vhost_dev_stop (&vvc->vhost_dev, vdev, true); ret = k->set_guest_notifiers (qbus->parent, vvc->vhost_dev.nvqs, false);if (ret <0 ) {hw/virtio/vhost.c +0
-0@@ -1777,15 +1777,36 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,return 0 ; }Quoted 5 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1780
Quoted 1 line
hw/virtio/vhost.c +1
-0+static int vhost_dev_set_vring_enable (struct vhost_dev *hdev,int enable)There is a similarly-named
vhost_set_vring_enable(NetClientState *nc, int enable)function which is actually part ofvhost_net. Please rename it tovhost_net_set_vring_enable().Should I rename it in this patch?
Quoted 2 lines
It should probably call
vhost_dev_set_vring_enable().Ehm, the idea of this patch was to touch as little as possible to avoid new regressions.
Also, the semantics of
vhost_dev_set_vring_enable()was meant to keep vhost_dev_start()/vhost_dev_stop() simple, not to be exposed to frontends. (maybe I should have written it, sorry about that).However I agree that we should clean up vhost-net and also the other frontends as Raphael also suggested, but honestly I'm scared to do that now in this patch...
What I would have wanted to do, would be similar to what we do for
vhost-vdpa: callSET_VRING_ENABLEin thevhost_ops->vhost_dev_start()callback ofvhost-user.c.
Removing all the call tovhost_ops->vhost_set_vring_enable()in the frontends, but I think it's too risky to do that now.Quoted 20 lines
Quoted 7 lines
hw/virtio/vhost.c +6
-0+{ +if (!hdev->vhost_ops->vhost_set_vring_enable) { +return 0 ; + } + +/* hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1796
Quoted 10 lines
hw/virtio/vhost.c +10
-0+* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not +* been negotiated, the rings start directly in the enabled state, and +* .vhost_set_vring_enable callback will fail since +* VHOST_USER_SET_VRING_ENABLE is not supported. +*/ +if (hdev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_USER && + !virtio_has_feature (hdev->backend_features, +VHOST_USER_F_PROTOCOL_FEATURES )) { +return 0 ; + }These semantics are the opposite of
vhost_user_set_vring_enable():Quoted 7 lines
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { return -EINVAL;}
Please make
vhost_user_set_vring_enable()andvhost_dev_set_vring_enable()consistent. Code gets really confusing when layers have different semantics for the same operation.It's the opposite precisely because we shouldn't let vhost_dev_start()/vhost_dev_stop() fail if
vhost_ops->vhost_set_vring_enable()can't be called because it would fail.If I do it this way, then I have to put the check inside vhost_dev_start()/vhost_dev_stop(), and at this point I remove the function that would be useless (just a wrapper of
hdev->vhost_ops->vhost_set_vring_enable).
Actually this was the first implementation I did, then I added the function just to have vhost_dev_start()/vhost_dev_stop() cleaner and to avoid duplicating the check.Quoted 11 lines
hw/virtio/vhost.c int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, · line 1799
Quoted 4 lines
hw/virtio/vhost.c +3
-0+ +return hdev->vhost_ops->vhost_set_vring_enable (hdev, enable); +}The return value is hard to understand. An error return is only returned by vhost-user devices with
VHOST_USER_F_PROTOCOL_FEATURES. There are other cases that seem like they should return an error but return success instead. For example, when called with enable=false on a non-VHOST_USER_F_PROTOCOL_FEATURES device (e.g. vhost-kernel or legacy vhost-user) we return success even though the vring wasn't disabled.As I explained above, the idea was not to expose this function outside, but to use it only in vhost_dev_start()/vhost_dev_stop(). So the return value is 0 both when it has successes and when there is no need/way to enable/disable the vrings.
Perhaps since it is confusing, I will remove the function and put the code directly into vhost_dev_start()/vhost_dev_stop().
Quoted 6 lines
Quoted 1 line
What do you think?
It's late now. We can merge it as-is.
I think this patch makes the vhost code even harder to understand and it's important to do the clean ups that have been discussed for 8.0.
Quoted 8 lines
Agree.
Quoted 1 line
Will you work on the changes we discussed for 8.0?
Yep, sure.
I will try to unify all vhost/vhost-user devices.
Now I think it's also a mess because the devices do different things, we should have everything in the core.
Do you think rust-vmm's vhost crates have enough of the state management to manage vhost and vhost-user backends? Maybe it would be a good experiment in replacing a (small well defined) piece of functionality with rust?
That said there is a lot of deep magic in the vhost-net stuff which I think is down to the interaction with things like vdpk and other network optimisations that might be missing. For the rest of the devices most of the code is basically boiler plate which has grown variations due to code motion and change. This is the sort of thing that generics solves well.
Quoted 3 lines
Thanks,
Stefano
Signature · 1 line
Alex Bennée
Quoted 11 lines · Alex Bennée
Do you think rust-vmm's vhost crates have enough of the state management to manage vhost and vhost-user backends? Maybe it would be a good experiment in replacing a (small well defined) piece of functionality with rust?
That said there is a lot of deep magic in the vhost-net stuff which I think is down to the interaction with things like vdpk and other network optimisations that might be missing. For the rest of the devices most of the code is basically boiler plate which has grown variations due to code motion and change. This is the sort of thing that generics solves well.
Not sure what you want to replace with what though, libvhost-user or vhost-user bits in qemu?
Signature · 1 line
MST
Quoted 13 lines · Michael S. Tsirkin
Quoted 11 lines · Alex Bennée
Do you think rust-vmm's vhost crates have enough of the state management to manage vhost and vhost-user backends? Maybe it would be a good experiment in replacing a (small well defined) piece of functionality with rust?
That said there is a lot of deep magic in the vhost-net stuff which I think is down to the interaction with things like vdpk and other network optimisations that might be missing. For the rest of the devices most of the code is basically boiler plate which has grown variations due to code motion and change. This is the sort of thing that generics solves well.
Quoted 2 lines
Not sure what you want to replace with what though, libvhost-user or vhost-user bits in qemu?
The vhost-user bits in the main QEMU binary. We already don't use libvhost-user for most of our backends anyway ;-)
Signature · 1 line
Alex Bennée
Quoted 16 lines · Alex Bennée
Quoted 13 lines · Michael S. Tsirkin
Quoted 11 lines · Alex Bennée
Do you think rust-vmm's vhost crates have enough of the state management to manage vhost and vhost-user backends? Maybe it would be a good experiment in replacing a (small well defined) piece of functionality with rust?
That said there is a lot of deep magic in the vhost-net stuff which I think is down to the interaction with things like vdpk and other network optimisations that might be missing. For the rest of the devices most of the code is basically boiler plate which has grown variations due to code motion and change. This is the sort of thing that generics solves well.
Quoted 5 lines
Quoted 2 lines
Not sure what you want to replace with what though, libvhost-user or vhost-user bits in qemu?
The vhost-user bits in the main QEMU binary. We already don't use libvhost-user for most of our backends anyway ;-)
Mixing C and Rust like this is far from trivial. I'd start with something much less ambitious that virtio.
Signature · 1 line
MST
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Alex Bennée
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Alex Bennée
Do you think rust-vmm's vhost crates have enough of the state management to manage vhost and vhost-user backends? Maybe it would be a good experiment in replacing a (small well defined) piece of functionality with rust?
I honestly don't know, but I think they are not 100% complete.
I do agree that an experiment would be nice though, maybe a GSoC? Or maybe not, since it's not very clear whether we get to the end or not....
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
That said there is a lot of deep magic in the vhost-net stuff which I think is down to the interaction with things like vdpk and other network optimisations that might be missing. For the rest of the devices most of the code is basically boiler plate which has grown variations due to code motion and change. This is the sort of thing that generics solves well.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Not sure what you want to replace with what though, libvhost-user or vhost-user bits in qemu?
The vhost-user bits in the main QEMU binary. We already don't use libvhost-user for most of our backends anyway ;-)
Mixing C and Rust like this is far from trivial. I'd start with something much less ambitious that virtio.
We recently merged libblkio [1] support in QEMU [2], where we did something similar.
libblkio provides a C API, but the library is totally written in Rust. Writing the C interface was not too complicated.
The advantage here is that the Rust code is already part of the rust-vmm crates.
I think it needs adjustments, but it's not a bad idea in my opinion.
Anyway, I don't think it's an easy job, though, so maybe GSoC could just be about writing the C interface and adapting Rust crates. Touching QEMU might require too much knowledge of the current code.
Thanks,
Stefano
[1] https://libblkio.gitlab.io/libblkio/
[2] https://lists.nongnu.org/archive/html/qemu-devel/2022-10/msg02215.html