Skip to contentSkip to conversation

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

PATCH Superseded Bin Guo 3 participants 4 messages Jun 4 – Jul 4

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 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_vector 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 source-compatible.

Resolves a long-standing TODO at the unmask call.

---
 hw/virtio/vhost.c         | 12 +++++++++---
 include/hw/virtio/vhost.h |  4 ++--
 2 files changed, 11 insertions(+), 5 deletions(-)
hw/virtio/vhost.c +9 -3
index b9dc4ed13b..42e2fc9952 100644
@@ -1351,8 +1351,11 @@ 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) {
+            /* No call eventfd means no interrupts; bail out. */
+            goto fail_vector;
+        }
     }
 
     if (k->query_guest_notifiers &&
@@ -1815,7 +1818,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);
@@ -1836,7 +1839,10 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
     r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
     if (r < 0) {
         error_report("vhost_set_vring_call failed %d", -r);
+        return r;
     }
+
+    return 0;
 }
 
 bool vhost_config_pending(struct vhost_dev *hdev)
include/hw/virtio/vhost.h +2 -2
index 89817bd848..96cc261822 100644
@@ -317,8 +317,8 @@ 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,
-                          bool mask);
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+                         bool mask);
 
 /**
  * vhost_get_features_ex() - sanitize the extended features set
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

Hi,

Quoted 30 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_vector 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 source-compatible.

Resolves a long-standing TODO at the unmask call.

---
  hw/virtio/vhost.c         | 12 +++++++++---
  include/hw/virtio/vhost.h |  4 ++--
  2 files changed, 11 insertions(+), 5 deletions(-)

/* Mask/unmask events from this vq. */

hw/virtio/vhost.c +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)
  {
      struct VirtQueue *vvq = virtio_get_queue(vdev, n);

hw/virtio/vhost.c void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, · line 1841

Quoted 3 lines
hw/virtio/vhost.c +0 -0
@@ -1836,7 +1839,10 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
      r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
      if (r < 0) {
          error_report("vhost_set_vring_call failed %d", -r);

IIUC the intention behind this TODO is to convert to "a function returning a boolean and taking Error** as last argument", replacing this call with error_setg_errno().

See in "Error reporting system loosely patterned after Glib's GError" rules in "qapi/error.h".

Quoted 5 lines
hw/virtio/vhost.c +3 -0
+        return r;
      }
+
+    return 0;
  }
author
#2 lore raw
Quoted 5 lines · Philippe Mathieu-Daudé

IIUC the intention behind this TODO is to convert to a function returning a boolean and taking Error** as last argument, replacing this call with error_setg_errno().
See in 'Error reporting system loosely patterned after Glib's GError' rules in 'qapi/error.h'.

Thanks for the suggestion. I considered changing the signature to bool + Error **errp, but decided to keep the current int + error_report() approach. vhost_set_vring_call is unlikely to fail in practice, so this is defensive programming rather than a regular error path. Changing the signature would propagate to ~15 call sites across multiple files for what is a rare edge case, which seems disproportionate. The patch already resolves the TODO and confines the change to the start path.

#3 lore raw
In reply to Bin Guo
Quoted 30 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_vector 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 source-compatible.

Resolves a long-standing TODO at the unmask call.

---
 hw/virtio/vhost.c         | 12 +++++++++---
 include/hw/virtio/vhost.h |  4 ++--
 2 files changed, 11 insertions(+), 5 deletions(-)
hw/virtio/vhost.c +0 -0
index b9dc4ed13b..42e2fc9952 100644
@@ -1351,8 +1351,11 @@ int vhost_virtqueue_start(struct vhost_dev *dev,
      * will do it later.
      */
     if (!vdev->use_guest_notifier_mask) {

hw/virtio/vhost.c int vhost_virtqueue_start(struct vhost_dev *dev, · line 1361

Quoted 10 lines
hw/virtio/vhost.c +5 -2
-        /* TODO: check and handle errors. */
-        vhost_virtqueue_mask(dev, vdev, idx, false);
+        r = vhost_virtqueue_mask(dev, vdev, idx, false);
+        if (r < 0) {
+            /* No call eventfd means no interrupts; bail out. */
+            goto fail_vector;
+        }
     }
 
     if (k->query_guest_notifiers &&

Can you rebase please? Thanks!

Quoted 37 lines
hw/virtio/vhost.c +4 -1
@@ -1815,7 +1818,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);
@@ -1836,7 +1839,10 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
     r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
     if (r < 0) {
         error_report("vhost_set_vring_call failed %d", -r);
+        return r;
     }
+
+    return 0;
 }
 
 bool vhost_config_pending(struct vhost_dev *hdev)
include/hw/virtio/vhost.h +2 -2
index 89817bd848..96cc261822 100644
@@ -317,8 +317,8 @@ 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,
-                          bool mask);
+int vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
+                         bool mask);
 
 /**
  * vhost_get_features_ex() - sanitize the extended features set
Signature · 1 line
2.50.1 (Apple Git-155)