Skip to contentSkip to conversation

hw/misc/ivshmem-pci: Improve error handling

PATCH Superseded David Hamilton 2 participants 4 messages Feb 24 – Feb 25

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover hw/misc/ivshmem-pci: Improve error handling

This series addresses two error handling issues in the ivshmem PCI device:

1. Handle the return value of kvm_irqchip_add_irqfd_notifier_gsi() which was being silently ignored.

2. Check for errors from setup_interrupt() in process_msg_connect() before continuing to ioeventfd setup. Add ERRP_GUARD() to make the errp dereference safe.

David Hamilton (2):
hw/misc/ivshmem-pci: Handle error from
kvm_irqchip_add_irqfd_notifier_gsi()
hw/misc/ivshmem-pci: Check error after setup_interrupt() in
process_msg_connect()

 hw/misc/ivshmem-pci.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/2 hw/misc/ivshmem-pci: Handle error from kvm_irqchip_add_irqfd_notifier_gsi()

The return value of kvm_irqchip_add_irqfd_notifier_gsi() was being ignored. Propagate the error to the caller via errp.

Resolves the TODO comment at the call site.

---
 hw/misc/ivshmem-pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/misc/ivshmem-pci.c +7 -2
index a3a43f53bd..45b0e20363 100644
@@ -449,6 +449,7 @@ static void setup_interrupt(IVShmemState *s, int vector, Error **errp)
         ivshmem_has_feature(s, IVSHMEM_MSI);
     PCIDevice *pdev = PCI_DEVICE(s);
     Error *err = NULL;
+    int ret;
 
     IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector);
 
@@ -464,9 +465,13 @@ static void setup_interrupt(IVShmemState *s, int vector, Error **errp)
         }
 
         if (!msix_is_masked(pdev, vector)) {
-            kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL,
+            ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL,
                                                s->msi_vectors[vector].virq);
-            /* TODO handle error */
+            if (ret < 0) {
+                error_setg(errp, "Failed to configure irqfd notifier"
+                            " for vector %d, vector");
+                return;
+            }
         }
     } else {
         /* it will be delayed until msix is enabled, in write_config */
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

Re: [PATCH 1/2] hw/misc/ivshmem-pci: Handle error from kvm_irqchip_add_irqfd_notifier_gsi()

Quoted 23 lines · David Hamilton

The return value of kvm_irqchip_add_irqfd_notifier_gsi() was being ignored. Propagate the error to the caller via errp.

Resolves the TODO comment at the call site.

---
 hw/misc/ivshmem-pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/misc/ivshmem-pci.c +1 -0
index a3a43f53bd..45b0e20363 100644
@@ -449,6 +449,7 @@ static void setup_interrupt(IVShmemState *s, int vector, Error **errp)
         ivshmem_has_feature(s, IVSHMEM_MSI);
     PCIDevice *pdev = PCI_DEVICE(s);
     Error *err = NULL;
+    int ret;
 
     IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector);
 

hw/misc/ivshmem-pci.c static void setup_interrupt(IVShmemState *s, int vector, Error **errp) · line 472

Quoted 10 lines
hw/misc/ivshmem-pci.c +4 -2
@@ -464,9 +465,13 @@ static void setup_interrupt(IVShmemState *s, int vector, Error **errp)
         }
 
         if (!msix_is_masked(pdev, vector)) {
-            kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL,
+            ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL,
                                                s->msi_vectors[vector].virq);
-            /* TODO handle error */
+            if (ret < 0) {
+                error_setg(errp, "Failed to configure irqfd notifier"
+                            " for vector %d, vector");

Scratch ", vector".

hw/misc/ivshmem-pci.c static void setup_interrupt(IVShmemState *s, int vector, Error **errp) · line 477

Quoted 5 lines
hw/misc/ivshmem-pci.c +2 -0
+                return;
+            }
         }
     } else {
         /* it will be delayed until msix is enabled, in write_config */

qapi/error.h:

* - Whenever practical, also return a value that indicates success /

 *   failure.  This can make the error checking more concise, and can
 *   avoid useless error object creation and destruction.  Note that
 *   we still have many functions returning void.  We recommend
 *   • bool-valued functions return true on success / false on failure,
 *   • pointer-valued functions return non-null / null pointer, and
 *   • integer-valued functions return non-negative / negative.

Please make the function return false on error, true on success.

This will simplify the next patch.

#3 lore raw

PATCH 2/2 hw/misc/ivshmem-pci: Check error after setup_interrupt() in process_msg_connect()

In reply to David Hamilton

setup_interrupt() can fail and report via errp, but process_msg_connect() was ignoring the error and continuing to ivshmem_add_eventfd(). Add ERRP_GUARD() and check *errp to avoid operating on a vector whose interrupt setup failed.

Resolves the TODO comment at the call site.

---
 hw/misc/ivshmem-pci.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/ivshmem-pci.c +4 -1
index 45b0e20363..85614feca0 100644
@@ -528,6 +528,7 @@ static void process_msg_disconnect(IVShmemState *s, uint16_t posn,
 static void process_msg_connect(IVShmemState *s, uint16_t posn, int fd,
                                 Error **errp)
 {
+    ERRP_GUARD();
     Peer *peer = &s->peers[posn];
     int vector;
 
@@ -554,7 +555,9 @@ static void process_msg_connect(IVShmemState *s, uint16_t posn, int fd,
 
     if (posn == s->vm_id) {
         setup_interrupt(s, vector, errp);
-        /* TODO do we need to handle the error? */
+        if (*errp) {
+            return;
+        }
     }
 
     if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
Signature · 1 line
2.50.1 (Apple Git-155)