Skip to contentSkip to conversation

hw/virtio: propagate vhost_virtqueue_mask() errors from start path

PATCH In review Bin Guo 3 participants 3 messages Jul 16 – Aug 3

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH v2 1/1 hw/virtio: propagate vhost_virtqueue_mask() errors from start path

vhost_virtqueue_start() unmasks the call notifier by calling vhost_virtqueue_mask(), whose vhost_set_vring_call ioctl can fail (closed vhost-user socket, kernel ENOMEM, revoked guest_notifier fd) but whose void signature throws the error away. vhost_dev_start() then reports success while the backend has no valid call eventfd for that vq, leaving the guest with a working kick path but no virtqueue interrupts -- a half-up state harder to diagnose than a clean failure.

Make vhost_virtqueue_mask() return int and handle the error in the start path via the existing fail unwind. Other callers reach the function through VirtioDeviceClass.guest_notifier_mask, whose void signature offers no upward error channel; they invoke it as a statement and remain unchanged.

---
 hw/virtio/vhost.c         | 9 ++++++---
 include/hw/virtio/vhost.h | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)
hw/virtio/vhost.c +6 -3
index af41841b52..ad22c37c33 100644
@@ -1470,8 +1470,10 @@ int vhost_virtqueue_start(struct vhost_dev *dev,
      * will do it later.
      */
     if (!vdev->use_guest_notifier_mask) {
-        /* TODO: check and handle errors. */
-        vhost_virtqueue_mask(dev, vdev, idx, false);
+        r = vhost_virtqueue_mask(dev, vdev, idx, false);
+        if (r < 0) {
+            goto fail;
+        }
     }
 
     if (k->query_guest_notifiers &&
@@ -1918,7 +1920,7 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
 }
 
 /* Mask/unmask events from this vq. */
-void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                          bool mask)
 {
     struct VirtQueue *vvq = virtio_get_queue(vdev, n);
@@ -1940,6 +1942,7 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
     if (r < 0) {
         error_report("vhost_set_vring_call failed %d", -r);
     }
+    return r;
 }
 
 bool vhost_config_pending(struct vhost_dev *hdev)
include/hw/virtio/vhost.h +1 -1
index 684bafcaad..1f62332d60 100644
@@ -312,7 +312,7 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
 
 /* Mask/unmask events from this vq.
  */
-void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                           bool mask);
 
 /**
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Quoted 7 lines · Bin Guo

vhost_virtqueue_start() unmasks the call notifier by calling vhost_virtqueue_mask(), whose vhost_set_vring_call ioctl can fail (closed vhost-user socket, kernel ENOMEM, revoked guest_notifier fd) but whose void signature throws the error away. vhost_dev_start() then reports success while the backend has no valid call eventfd for that vq, leaving the guest with a working kick path but no virtqueue interrupts -- a half-up state harder to diagnose than a clean failure.

Is there a real problem though?
So what if socket closed one second after we sent the fd, does it matter?
And what does it mean for a guest_notifier fd to be revoked? Is qemu likely to survive long after we started getting ENOMEM for allocations of a hundred of bytes from the kernel?

I do not object to the patch on principle but let's get it clear how it was tested, what is being fixed and why?

Quoted 61 lines

Make vhost_virtqueue_mask() return int and handle the error in the start path via the existing fail unwind. Other callers reach the function through VirtioDeviceClass.guest_notifier_mask, whose void signature offers no upward error channel; they invoke it as a statement and remain unchanged.

---
 hw/virtio/vhost.c         | 9 ++++++---
 include/hw/virtio/vhost.h | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)
hw/virtio/vhost.c +6 -3
index af41841b52..ad22c37c33 100644
@@ -1470,8 +1470,10 @@ int vhost_virtqueue_start(struct vhost_dev *dev,
      * will do it later.
      */
     if (!vdev->use_guest_notifier_mask) {
-        /* TODO: check and handle errors. */
-        vhost_virtqueue_mask(dev, vdev, idx, false);
+        r = vhost_virtqueue_mask(dev, vdev, idx, false);
+        if (r < 0) {
+            goto fail;
+        }
     }
 
     if (k->query_guest_notifiers &&
@@ -1918,7 +1920,7 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
 }
 
 /* Mask/unmask events from this vq. */
-void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                          bool mask)
 {
     struct VirtQueue *vvq = virtio_get_queue(vdev, n);
@@ -1940,6 +1942,7 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
     if (r < 0) {
         error_report("vhost_set_vring_call failed %d", -r);
     }
+    return r;
 }
 
 bool vhost_config_pending(struct vhost_dev *hdev)
include/hw/virtio/vhost.h +1 -1
index 684bafcaad..1f62332d60 100644
@@ -312,7 +312,7 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
 
 /* Mask/unmask events from this vq.
  */
-void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                           bool mask);
 
 /**
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw
In reply to Bin Guo

Hi,

Quoted 26 lines · Bin Guo

vhost_virtqueue_start() unmasks the call notifier by calling vhost_virtqueue_mask(), whose vhost_set_vring_call ioctl can fail (closed vhost-user socket, kernel ENOMEM, revoked guest_notifier fd) but whose void signature throws the error away. vhost_dev_start() then reports success while the backend has no valid call eventfd for that vq, leaving the guest with a working kick path but no virtqueue interrupts -- a half-up state harder to diagnose than a clean failure.

Make vhost_virtqueue_mask() return int and handle the error in the start path via the existing fail unwind. Other callers reach the function through VirtioDeviceClass.guest_notifier_mask, whose void signature offers no upward error channel; they invoke it as a statement and remain unchanged.

---
  hw/virtio/vhost.c         | 9 ++++++---
  include/hw/virtio/vhost.h | 2 +-
  2 files changed, 7 insertions(+), 4 deletions(-)
include/hw/virtio/vhost.h +0 -0
index 684bafcaad..1f62332d60 100644

include/hw/virtio/vhost.h bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n); · line 313

Quoted 2 lines
include/hw/virtio/vhost.h +0 -0
@@ -312,7 +312,7 @@ bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
 
  /* Mask/unmask events from this vq.

Please document the new returned value, i.e.:

" * Return 0 on success, negative errno on failure."

(Good opportunity to convert to a proper docstring describing arguments)

Quoted 6 lines
include/hw/virtio/vhost.h +1 -1
   */
-void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                            bool mask);
 
  /**