Skip to contentSkip to conversation

Error recovery for zPCI passthrough devices

PATCH Superseded Farhan Ali 3 participants 18 messages Sep 25, 2025 – Oct 6, 2025

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
author
#16 lore raw

PATCH v3 4/5 s390x/pci: Add PCI error handling for vfio pci devices

In reply to Farhan Ali

Add an s390x specific callback for vfio error handling. For s390x pci devices, we have platform specific error information. We need to retrieve this error information for passthrough devices. This is done via a memory region which exposes that information.

Once this error information is retrieved we can then inject an error into the guest, and let the guest drive the recovery.

---
 hw/s390x/s390-pci-bus.c          |  9 ++++
 hw/s390x/s390-pci-vfio.c         | 81 ++++++++++++++++++++++++++++++++
 include/hw/s390x/s390-pci-bus.h  |  1 +
 include/hw/s390x/s390-pci-vfio.h |  6 +++
 4 files changed, 97 insertions(+)
hw/s390x/s390-pci-bus.c +9 -0
index f87d2748b6..9f7b17e807 100644
@@ -158,6 +158,8 @@ static void s390_pci_perform_unplug(S390PCIBusDevice *pbdev)
 {
     HotplugHandler *hotplug_ctrl;
 
+    qemu_mutex_destroy(&pbdev->err_handler_lock);
+
     if (pbdev->pft == ZPCI_PFT_ISM) {
         notifier_remove(&pbdev->shutdown_notifier);
     }
@@ -1074,6 +1076,7 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
     S390pciState *s = S390_PCI_HOST_BRIDGE(hotplug_dev);
     PCIDevice *pdev = NULL;
     S390PCIBusDevice *pbdev = NULL;
+    Error *local_err = NULL;
     int rc;
 
     if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
@@ -1140,6 +1143,7 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         pbdev->iommu->pbdev = pbdev;
         pbdev->state = ZPCI_FS_DISABLED;
         set_pbdev_info(pbdev);
+        qemu_mutex_init(&pbdev->err_handler_lock);
 
         if (object_dynamic_cast(OBJECT(dev), "vfio-pci")) {
             /*
@@ -1164,6 +1168,11 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
             pbdev->iommu->dma_limit = s390_pci_start_dma_count(s, pbdev);
             /* Fill in CLP information passed via the vfio region */
             s390_pci_get_clp_info(pbdev);
+            /* Setup error handler for error recovery */
+            if (!s390_pci_setup_err_handler(pbdev, &local_err)) {
+                warn_report_err(local_err);
+            }
+
             if (!pbdev->interp) {
                 /* Do vfio passthrough but intercept for I/O */
                 pbdev->fh |= FH_SHM_VFIO;
hw/s390x/s390-pci-vfio.c +81 -0

90 lines not on this page · raw message

include/hw/s390x/s390-pci-bus.h +1 -0
index 04944d4fed..3795e0bbfc 100644
@@ -364,6 +364,7 @@ struct S390PCIBusDevice {
     bool forwarding_assist;
     bool aif;
     bool rtr_avail;
+    QemuMutex err_handler_lock;
     QTAILQ_ENTRY(S390PCIBusDevice) link;
 };
 
include/hw/s390x/s390-pci-vfio.h +6 -0
index ae1b126ff7..b45ffa5044 100644
@@ -22,6 +22,7 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
 void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt);
 bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh);
 void s390_pci_get_clp_info(S390PCIBusDevice *pbdev);
+bool s390_pci_setup_err_handler(S390PCIBusDevice *pbdev, Error **errp);
 #else
 static inline bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
 {
@@ -39,6 +40,11 @@ static inline bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)
     return false;
 }
 static inline void s390_pci_get_clp_info(S390PCIBusDevice *pbdev) { }
+static inline bool s390_pci_setup_err_handler(S390PCIBusDevice *pbdev, Error **errp)
+{
+    error_setg(errp, "VFIO not available, cannot setup error handler\n");
+    return false;
+}
 #endif
 
 #endif
Signature · 1 line
2.43.0

Farhan Ali

author
#0 lore raw

v3 cover Error recovery for zPCI passthrough devices

Hi,

This patch series introduces support for error recovery for passthrough PCI devices on System Z (s390x). This is the user space component for the Linux kernel patches [1]. For QEMU on eventfd notification for PCI error from vfio-pci driver we call the vfio error handler. We can use a per device error handler callback to override the default vfio error handler.

For s390x specific error handler, we retrieve the architecture specific PCI error information and inject the information into the guest. Once the guest receives the error information, the guest drivers will drive the error recovery. Typically recovery involves a device reset which translate to CLP disable/enable cycle for the device.

I would appreciate some feedback on this patch series.

Thanks
Farhan

[1] https://lore.kernel.org/all/[email protected]/

ChangeLog
---------
v2 https://lore.kernel.org/qemu-devel/[email protected]/
v2 -> v3
- Update arch_err_handler to err_handler and include Error ** in function definition. (patch 2)

- Introduce helper function to hide the internal indirection of device_feature() (patch 3)

- Update function definitions to include Error ** (patch 4)

v1 https://lore.kernel.org/qemu-devel/[email protected]/
v1 -> v2
- Use VFIO_DEVICE_FEATURE ioctl to get device error information. (Based on Alex's feedback on kernel series)

Farhan Ali (5):
[NOTFORMERGE] linux-headers: Update for zpci vfio device vfio/pci: Add an error handler callback
vfio: Introduce vfio_device_feature helper function s390x/pci: Add PCI error handling for vfio pci devices s390x/pci: Reset a device in error state

 hw/s390x/s390-pci-bus.c          | 16 ++++++
 hw/s390x/s390-pci-vfio.c         | 87 ++++++++++++++++++++++++++++++++
 hw/vfio/device.c                 |  6 +++
 hw/vfio/pci.c                    |  8 +++
 hw/vfio/pci.h                    |  1 +
 include/hw/s390x/s390-pci-bus.h  |  1 +
 include/hw/s390x/s390-pci-vfio.h |  8 +++
 include/hw/vfio/vfio-device.h    |  2 +
 linux-headers/linux/vfio.h       | 15 ++++++
 9 files changed, 144 insertions(+)
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/5 vfio/pci: Add an error handler callback

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
 hw/vfio/pci.c | 8 ++++++++
 hw/vfio/pci.h | 1 +
 2 files changed, 9 insertions(+)
hw/vfio/pci.c +8 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
 static void vfio_err_notifier_handler(void *opaque)
 {
     VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 
     if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
         return;
     }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }
+
     /*
      * TBD. Retrieve the error details and decide what action
      * needs to be taken. One of the actions could be to pass
hw/vfio/pci.h +1 -0
index e0aef82a89..faadce487c 100644
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {
     EventNotifier err_notifier;
     EventNotifier req_notifier;
     int (*resetfn)(struct VFIOPCIDevice *);
+    bool (*err_handler)(struct VFIOPCIDevice *, Error **);
     uint32_t vendor_id;
     uint32_t device_id;
     uint32_t sub_vendor_id;
Signature · 1 line
2.43.0
author
#17 lore raw

PATCH v3 5/5 s390x/pci: Reset a device in error state

In reply to Farhan Ali

For passthrough devices in error state, for a guest driven reset of the device we can attempt a reset to recover the device. A reset of the device will trigger a CLP disable/enable cycle on the host to bring the device into a recovered state.

---
 hw/s390x/s390-pci-bus.c          | 7 +++++++
 hw/s390x/s390-pci-vfio.c         | 6 ++++++
 include/hw/s390x/s390-pci-vfio.h | 2 ++
 3 files changed, 15 insertions(+)
hw/s390x/s390-pci-bus.c +7 -0
index 9f7b17e807..c0216d4a82 100644
@@ -1497,6 +1497,8 @@ static void s390_pci_device_reset(DeviceState *dev)
         return;
     case ZPCI_FS_STANDBY:
         break;
+    case ZPCI_FS_ERROR:
+        break;
     default:
         pbdev->fh &= ~FH_MASK_ENABLE;
         pbdev->state = ZPCI_FS_DISABLED;
@@ -1509,6 +1511,11 @@ static void s390_pci_device_reset(DeviceState *dev)
     } else if (pbdev->summary_ind) {
         pci_dereg_irqs(pbdev);
     }
+
+    if (pbdev->state == ZPCI_FS_ERROR) {
+        s390_pci_reset(pbdev);
+    }
+
     if (pbdev->iommu->enabled) {
         pci_dereg_ioat(pbdev->iommu);
     }
hw/s390x/s390-pci-vfio.c +6 -0
index 1697a84de7..27e300f95d 100644
@@ -155,6 +155,12 @@ static bool s390_pci_err_handler(VFIOPCIDevice *vfio_pci, Error **errp)
     return true;
 }
 
+void s390_pci_reset(S390PCIBusDevice *pbdev)
+{
+    VFIOPCIDevice *vfio_pci = VFIO_PCI_BASE(pbdev->pdev);
+    ioctl(vfio_pci->vbasedev.fd, VFIO_DEVICE_RESET);
+}
+
 static void s390_pci_read_base(S390PCIBusDevice *pbdev,
                                struct vfio_device_info *info)
 {
include/hw/s390x/s390-pci-vfio.h +2 -0
index b45ffa5044..5d7f21023f 100644
@@ -23,6 +23,7 @@ void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt);
 bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh);
 void s390_pci_get_clp_info(S390PCIBusDevice *pbdev);
 bool s390_pci_setup_err_handler(S390PCIBusDevice *pbdev, Error **errp);
+void s390_pci_reset(S390PCIBusDevice *pbdev);
 #else
 static inline bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
 {
@@ -45,6 +46,7 @@ static inline bool s390_pci_setup_err_handler(S390PCIBusDevice *pbdev, Error **e
     error_setg(errp, "VFIO not available, cannot setup error handler\n");
     return false;
 }
+static inline void s390_pci_reset(S390PCIBusDevice *pbdev) { }
 #endif
 
 #endif
Signature · 1 line
2.43.0
author
#15 lore raw

PATCH v3 3/5 vfio: Introduce vfio_device_feature helper function

In reply to Farhan Ali

Introduce a helper function to call internal VFIODeviceIOOps device_feature().

---
 hw/vfio/device.c              | 6 ++++++
 include/hw/vfio/vfio-device.h | 2 ++
 2 files changed, 8 insertions(+)
hw/vfio/device.c +6 -0
index 08f12ac31f..2ea2af3f79 100644
@@ -504,6 +504,12 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
     vbasedev->bcontainer = NULL;
 }
 
+int vfio_device_feature(VFIODevice *vbasedev,
+                        struct vfio_device_feature *feature)
+{
+    return vbasedev->io_ops->device_feature(vbasedev, feature);
+}
+
 /*
  * Traditional ioctl() based io
  */
include/hw/vfio/vfio-device.h +2 -0
index e7e6243e2d..a7f00d2a80 100644
@@ -157,6 +157,8 @@ bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
                                       Error **errp);
 void vfio_device_detach(VFIODevice *vbasedev);
 VFIODevice *vfio_get_vfio_device(Object *obj);
+int vfio_device_feature(VFIODevice *vbasedev,
+                        struct vfio_device_feature *feat);
 
 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
 extern VFIODeviceList vfio_device_list;
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/5 linux-headers: Update for zpci vfio device

In reply to Farhan Ali
---
 linux-headers/linux/vfio.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
linux-headers/linux/vfio.h +15 -0
index 79bf8c0cc5..2918080ad9 100644
@@ -1468,6 +1468,21 @@ struct vfio_device_feature_bus_master {
 };
 #define VFIO_DEVICE_FEATURE_BUS_MASTER 10
 
+/**
+ * VFIO_DEVICE_FEATURE_ZPCI_ERROR feature provides PCI error information to
+ * userspace for vfio-pci devices on s390x. On s390x PCI error recovery involves
+ * platform firmware and notification to operating system is done by
+ * architecture specific mechanism.  Exposing this information to userspace
+ * allows userspace to take appropriate actions to handle an error on the
+ * device.
+ */
+struct vfio_device_feature_zpci_err {
+        __u16 pec;
+        __u8 pending_errors;
+        __u8 pad;
+};
+#define VFIO_DEVICE_FEATURE_ZPCI_ERROR 11
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

In reply to Farhan Ali
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
 hw/vfio/pci.c | 8 ++++++++
 hw/vfio/pci.h | 1 +
 2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
 static void vfio_err_notifier_handler(void *opaque)
 {
     VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
     if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
         return;
     }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply. In particular:

* - On success, the function should not touch *errp. On failure, it
* should set a new error, e.g. with error_setg(errp, ...), or
* propagate an existing one, e.g. with error_propagate(errp, ...).
*
* - 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,

If ->err_handler() behaved that way, it @err would be null after it returns false. We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Quoted 16 lines
hw/vfio/pci.c +1 -0
+
     /*
      * TBD. Retrieve the error details and decide what action
      * needs to be taken. One of the actions could be to pass
hw/vfio/pci.h +1 -0
index e0aef82a89..faadce487c 100644
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {
     EventNotifier err_notifier;
     EventNotifier req_notifier;
     int (*resetfn)(struct VFIOPCIDevice *);
+    bool (*err_handler)(struct VFIOPCIDevice *, Error **);
     uint32_t vendor_id;
     uint32_t device_id;
     uint32_t sub_vendor_id;
#4 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 25 lines · Markus Armbruster
Quoted 23 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
  hw/vfio/pci.c | 8 ++++++++
  hw/vfio/pci.h | 1 +
  2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
  static void vfio_err_notifier_handler(void *opaque)
  {
      VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 
      if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
          return;
      }
 
Quoted 8 lines

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 6 lines
hw/vfio/pci.c +6 -0
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

and the compiler complains :

../hw/vfio/pci.c: In function ‘vfio_err_notifier_handler’:
../hw/vfio/pci.c:3076:9: error: dangling pointer to ‘err’ may be used [-Werror=dangling-pointer=]

 3076 |         error_report_err(err);
      |         ^~~~~~~~~~~~~~~~~~~~~

../hw/vfio/pci.c:3066:12: note: ‘err’ declared here
3066 | Error *err = NULL;
| ^~~
cc1: all warnings being treated as errors

C.

Quoted 39 lines

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply. In particular:

* - On success, the function should not touch *errp. On failure, it
* should set a new error, e.g. with error_setg(errp, ...), or
* propagate an existing one, e.g. with error_propagate(errp, ...).
*
* - 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,

If ->err_handler() behaved that way, it @err would be null after it returns false. We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Quoted 16 lines
hw/vfio/pci.c +1 -0
+
      /*
       * TBD. Retrieve the error details and decide what action
       * needs to be taken. One of the actions could be to pass
hw/vfio/pci.h +1 -0
index e0aef82a89..faadce487c 100644
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {
      EventNotifier err_notifier;
      EventNotifier req_notifier;
      int (*resetfn)(struct VFIOPCIDevice *);
+    bool (*err_handler)(struct VFIOPCIDevice *, Error **);
      uint32_t vendor_id;
      uint32_t device_id;
      uint32_t sub_vendor_id;
author
#6 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

In reply to Markus Armbruster
Quoted 46 lines · Markus Armbruster
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
  hw/vfio/pci.c | 8 ++++++++
  hw/vfio/pci.h | 1 +
  2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
  static void vfio_err_notifier_handler(void *opaque)
  {
      VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
      if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
          return;
      }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply. In particular:

* - On success, the function should not touch *errp. On failure, it
* should set a new error, e.g. with error_setg(errp, ...), or
* propagate an existing one, e.g. with error_propagate(errp, ...).
*
* - 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,
Quoted 7 lines

If ->err_handler() behaved that way, it @err would be null after it returns false. We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Hi Markus,

Thanks for reviewing! The intended behavior for err_handler() is to set errp and report the error on false/failure. With the above code, I also intended fall through to vm_stop() when err_handler() fails.

I think I misunderstood the errp error handling, it seems like the correct way to do what I intended would be

hw/vfio/pci.c +0 -0
index b02a974954..630de46c90 100644
@@ -3070,10 +3070,11 @@ static void vfio_err_notifier_handler(void *opaque)

     }

     if (vdev->err_handler) {
-        if (vdev->err_handler(vdev, &err)) {
+        if (!vdev->err_handler(vdev, &err)) {
+            error_report_err(err);
+        } else {
             return;
         }
-        error_report_err(err);
     }

Please correct me if I missed anything.

Thanks
Farhan

Quoted 18 lines
Quoted 17 lines
hw/vfio/pci.c +1 -0
+
      /*
       * TBD. Retrieve the error details and decide what action
       * needs to be taken. One of the actions could be to pass
hw/vfio/pci.h +1 -0
index e0aef82a89..faadce487c 100644
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {
      EventNotifier err_notifier;
      EventNotifier req_notifier;
      int (*resetfn)(struct VFIOPCIDevice *);
+    bool (*err_handler)(struct VFIOPCIDevice *, Error **);
      uint32_t vendor_id;
      uint32_t device_id;
      uint32_t sub_vendor_id;
author
#5 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

In reply to Cédric Le Goater
Quoted 45 lines · Cédric Le Goater
Quoted 23 lines · Markus Armbruster
Quoted 21 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
  hw/vfio/pci.c | 8 ++++++++
  hw/vfio/pci.h | 1 +

  2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
   static void vfio_err_notifier_handler(void *opaque)
   {
       VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
         if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
           return;
       }
Quoted 8 lines

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 6 lines
hw/vfio/pci.c +5 -0
   +    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

and the compiler complains :

../hw/vfio/pci.c: In function ‘vfio_err_notifier_handler’:
../hw/vfio/pci.c:3076:9: error: dangling pointer to ‘err’ may be used [-Werror=dangling-pointer=] 3076 |         error_report_err(err);
      |         ^~~~~~~~~~~~~~~~~~~~~
../hw/vfio/pci.c:3066:12: note: ‘err’ declared here
 3066 |     Error *err = NULL;
      |            ^~~
cc1: all warnings being treated as errors

Quoted 1 line

C.

Compiling on s390x didn't cause this compiler error, but indeed compiling on x86 it did.

Thanks
Farhan

Quoted 43 lines
Quoted 41 lines

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply.  In particular:

  * - On success, the function should not touch *errp.  On failure, it *   should set a new error, e.g. with error_setg(errp, ...), or *   propagate an existing one, e.g. with error_propagate(errp, ...). *
  * - 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,

If ->err_handler() behaved that way, it @err would be null after it returns false.  We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Quoted 16 lines
hw/vfio/pci.c +1 -0
+
       /*
        * TBD. Retrieve the error details and decide what action
        * needs to be taken. One of the actions could be to pass
hw/vfio/pci.h +1 -0
index e0aef82a89..faadce487c 100644
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {
       EventNotifier err_notifier;
       EventNotifier req_notifier;
       int (*resetfn)(struct VFIOPCIDevice *);
+    bool (*err_handler)(struct VFIOPCIDevice *, Error **);
       uint32_t vendor_id;
       uint32_t device_id;
       uint32_t sub_vendor_id;
#7 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

In reply to Farhan Ali
Quoted 79 lines · Farhan Ali
Quoted 47 lines · Markus Armbruster
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
  hw/vfio/pci.c | 8 ++++++++
  hw/vfio/pci.h | 1 +
  2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
 static void vfio_err_notifier_handler(void *opaque)
 {
     VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
     if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
         return;
     }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply. In particular:

* - On success, the function should not touch *errp. On failure, it
* should set a new error, e.g. with error_setg(errp, ...), or
* propagate an existing one, e.g. with error_propagate(errp, ...).
*
* - 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,
Quoted 7 lines

If ->err_handler() behaved that way, it @err would be null after it returns false. We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Hi Markus,

Thanks for reviewing! The intended behavior for err_handler() is to set errp and report the error on false/failure. With the above code, I also intended fall through to vm_stop() when err_handler() fails.

I think I misunderstood the errp error handling, it seems like the correct way to do what I intended would be

hw/vfio/pci.c +3 -2
index b02a974954..630de46c90 100644
@@ -3070,10 +3070,11 @@ static void vfio_err_notifier_handler(void *opaque)
      }
 
      if (vdev->err_handler) {
-        if (vdev->err_handler(vdev, &err)) {
+        if (!vdev->err_handler(vdev, &err)) {
+            error_report_err(err);
+        } else {
              return;
          }
-        error_report_err(err);
      }
 
Quoted 1 line

Please correct me if I missed anything.

Resulting function:

   static void vfio_err_notifier_handler(void *opaque)
   {
       VFIOPCIDevice *vdev = opaque;
       Error *err = NULL;
       if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
           return;
       }

       if (vdev->err_handler) {
           if (!vdev->err_handler(vdev, &err)) {
               error_report_err(err);
           } else {
               return;
           }
       }

       /*
        * TBD. Retrieve the error details and decide what action
        * needs to be taken. One of the actions could be to pass
        * the error to the guest and have the guest driver recover
        * from the error. This requires that PCIe capabilities be
        * exposed to the guest. For now, we just terminate the
        * guest to contain the error.
        */

       error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

       vm_stop(RUN_STATE_INTERNAL_ERROR);

}

Slighly rearranged for clearer control flow:

   static void vfio_err_notifier_handler(void *opaque)
   {
       VFIOPCIDevice *vdev = opaque;
       Error *err = NULL;
       if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
           return;
       }

       if (vdev->err_handler) {
           if (vdev->err_handler(vdev, &err)) {
               /* Error successfully handled */
               return;
           }
           error_report_err(err);
       }

       /*
        * TBD. Retrieve the error details and decide what action
        * needs to be taken. One of the actions could be to pass
        * the error to the guest and have the guest driver recover
        * from the error. This requires that PCIe capabilities be
        * exposed to the guest. For now, we just terminate the
        * guest to contain the error.
        */

       error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

       vm_stop(RUN_STATE_INTERNAL_ERROR);

}

Questions / issues:

* Is the comment still accurate?

* When ->err_handler() fails, we report the error twice. Would it make sense to combine the two error reports into one?

* Preexisting: the second error message is ugly.

Error messages should be short and to the point: single phrase, with no newline or trailing punctuation. The "please collect ..." part does not belong to the error message proper, it's advice on what to do. Better: report the error, then print advice:

       error_report("%s(%s) Unrecoverable error detected",
                    __func__, vdev->vbasedev.name);
       error_printf("Please collect any data possible and then kill the guest.");

Including __func__ in an error message is an anti-pattern. Look at

vfio_err_notifier_handler(fred) Unrecoverable error detected

with a user's eyes: "vfio_err_notifier_handler" is programmer gobbledygook, the device name "fred" is useful once you realize what it is, "Unrecoverable error detected" lacks detail.

[...]

#8 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 149 lines · Markus Armbruster
Quoted 79 lines · Farhan Ali
Quoted 47 lines · Markus Armbruster
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
   hw/vfio/pci.c | 8 ++++++++
   hw/vfio/pci.h | 1 +
   2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
  static void vfio_err_notifier_handler(void *opaque)
  {
      VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
      if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
          return;
      }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply. In particular:

* - On success, the function should not touch *errp. On failure, it
* should set a new error, e.g. with error_setg(errp, ...), or
* propagate an existing one, e.g. with error_propagate(errp, ...).
*
* - 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,
Quoted 7 lines

If ->err_handler() behaved that way, it @err would be null after it returns false. We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Hi Markus,

Thanks for reviewing! The intended behavior for err_handler() is to set errp and report the error on false/failure. With the above code, I also intended fall through to vm_stop() when err_handler() fails.

I think I misunderstood the errp error handling, it seems like the correct way to do what I intended would be

hw/vfio/pci.c +3 -2
index b02a974954..630de46c90 100644
@@ -3070,10 +3070,11 @@ static void vfio_err_notifier_handler(void *opaque)
      }
 
      if (vdev->err_handler) {
-        if (vdev->err_handler(vdev, &err)) {
+        if (!vdev->err_handler(vdev, &err)) {
+            error_report_err(err);
+        } else {
              return;
          }
-        error_report_err(err);
      }
 
Quoted 1 line

Please correct me if I missed anything.

Resulting function:

    static void vfio_err_notifier_handler(void *opaque)
    {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
            return;
        }

        if (vdev->err_handler) {
            if (!vdev->err_handler(vdev, &err)) {
                error_report_err(err);
            } else {
                return;
            }
        }

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Slighly rearranged for clearer control flow:

    static void vfio_err_notifier_handler(void *opaque)
    {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
            return;
        }

        if (vdev->err_handler) {
            if (vdev->err_handler(vdev, &err)) {
                /* Error successfully handled */
                return;
            }
            error_report_err(err);
        }

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }
Quoted 6 lines

Questions / issues:

* Is the comment still accurate?

* When ->err_handler() fails, we report the error twice. Would it make sense to combine the two error reports into one?

Yes. It was my request too.

Thanks,

C.

Quoted 21 lines

* Preexisting: the second error message is ugly.

Error messages should be short and to the point: single phrase, with no newline or trailing punctuation. The "please collect ..." part does not belong to the error message proper, it's advice on what to do. Better: report the error, then print advice:

        error_report("%s(%s) Unrecoverable error detected",
                     __func__, vdev->vbasedev.name);
        error_printf("Please collect any data possible and then kill the guest.");

Including __func__ in an error message is an anti-pattern. Look at

vfio_err_notifier_handler(fred) Unrecoverable error detected

with a user's eyes: "vfio_err_notifier_handler" is programmer gobbledygook, the device name "fred" is useful once you realize what it is, "Unrecoverable error detected" lacks detail.

[...]

author
#9 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 129 lines · Cédric Le Goater
Quoted 128 lines · Markus Armbruster
Quoted 80 lines · Farhan Ali
Quoted 47 lines · Markus Armbruster
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
   hw/vfio/pci.c | 8 ++++++++
   hw/vfio/pci.h | 1 +

   2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
   static void vfio_err_notifier_handler(void *opaque)
   {
       VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
       if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
           return;
       }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply.  In particular:

   * - On success, the function should not touch *errp.  On failure, it *   should set a new error, e.g. with error_setg(errp, ...), or *   propagate an existing one, e.g. with error_propagate(errp, ...). *
   * - 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,

Quoted 7 lines

If ->err_handler() behaved that way, it @err would be null after it returns false.  We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Hi Markus,

Thanks for reviewing! The intended behavior for err_handler() is to set errp and report the error on false/failure. With the above code, I also intended fall through to vm_stop() when err_handler() fails.

I think I misunderstood the errp error handling, it seems like the correct way to do what I intended would be

hw/vfio/pci.c +3 -2
index b02a974954..630de46c90 100644
@@ -3070,10 +3070,11 @@ static void vfio_err_notifier_handler(void 
 *opaque)
       }
 
       if (vdev->err_handler) {
-        if (vdev->err_handler(vdev, &err)) {
+        if (!vdev->err_handler(vdev, &err)) {
+            error_report_err(err);
+        } else {
               return;
           }
-        error_report_err(err);
       }
 
Quoted 1 line

Please correct me if I missed anything.

Resulting function:

    static void vfio_err_notifier_handler(void *opaque) {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) { return;
        }

        if (vdev->err_handler) {
            if (!vdev->err_handler(vdev, &err)) {
                error_report_err(err);
            } else {
                return;
            }
        }

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Slighly rearranged for clearer control flow:

    static void vfio_err_notifier_handler(void *opaque) {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) { return;
        }

Quoted 7 lines
Quoted 7 lines

        if (vdev->err_handler) {
            if (vdev->err_handler(vdev, &err)) {
                /* Error successfully handled */
                return;
            }
            error_report_err(err);
        }

Yes, this is what i intended to do with my patch and provide a clearer flow. Though the compiler error reported by Cedric, is a little confusing, need to understand why that happens.

Quoted 10 lines
Quoted 10 lines

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

Quoted 8 lines
Quoted 8 lines

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Questions / issues:

* Is the comment still accurate?

This comment would still apply for vfio-pci devices on other architectures except for s390x. We are trying to change this behavior for s390x.

Quoted 9 lines
Quoted 3 lines

* When ->err_handler() fails, we report the error twice. Would it make sense to combine the two error reports into one?

Yes. It was my request too.

Thanks,

C.

I was a little hesitant about changing the existing error message as its been there for almost 12 years (since commit 7b4b0e9eda ("vfio: QEMU-AER: Qemu changes to support AER for VFIO-PCI devices")). Nothing should ever dependent on specific error messages, but still.. .If the preference is to combine/change the message I can do that.

Quoted 16 lines
Quoted 16 lines

* Preexisting: the second error message is ugly.

   Error messages should be short and to the point: single phrase, with no newline or trailing punctuation.  The "please collect ..." part does not belong to the error message proper, it's advice on what to do.  Better: report the error, then print advice:

        error_report("%s(%s) Unrecoverable error detected",
                     __func__, vdev->vbasedev.name); error_printf("Please collect any data possible and then kill the guest.");

   Including __func__ in an error message is an anti-pattern. Look at

Quoted 9 lines
Quoted 8 lines

     vfio_err_notifier_handler(fred) Unrecoverable error detected

   with a user's eyes: "vfio_err_notifier_handler" is programmer gobbledygook, the device name "fred" is useful once you realize what it is, "Unrecoverable error detected" lacks detail.

[...]

How about "(device) Unrecoverable PCIe error detected for device"

Thanks
Farhan

#10 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 152 lines · Farhan Ali
Quoted 128 lines · Cédric Le Goater
Quoted 127 lines · Markus Armbruster
Quoted 79 lines · Farhan Ali
Quoted 47 lines · Markus Armbruster
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
   hw/vfio/pci.c | 8 ++++++++
   hw/vfio/pci.h | 1 +

   2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
   static void vfio_err_notifier_handler(void *opaque)
   {
       VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
       if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
           return;
       }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply.  In particular:

   * - On success, the function should not touch *errp.  On failure, it *   should set a new error, e.g. with error_setg(errp, ...), or *   propagate an existing one, e.g. with error_propagate(errp, ...). *
   * - 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,

Quoted 7 lines

If ->err_handler() behaved that way, it @err would be null after it returns false.  We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Hi Markus,

Thanks for reviewing! The intended behavior for err_handler() is to set errp and report the error on false/failure. With the above code, I also intended fall through to vm_stop() when err_handler() fails.

I think I misunderstood the errp error handling, it seems like the correct way to do what I intended would be

hw/vfio/pci.c +3 -2
index b02a974954..630de46c90 100644
@@ -3070,10 +3070,11 @@ static void vfio_err_notifier_handler(void *opaque)
       }
 
       if (vdev->err_handler) {
-        if (vdev->err_handler(vdev, &err)) {
+        if (!vdev->err_handler(vdev, &err)) {
+            error_report_err(err);
+        } else {
               return;
           }
-        error_report_err(err);
       }
 
Quoted 1 line

Please correct me if I missed anything.

Resulting function:

    static void vfio_err_notifier_handler(void *opaque) {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) { return;
        }

        if (vdev->err_handler) {
            if (!vdev->err_handler(vdev, &err)) {
                error_report_err(err);
            } else {
                return;
            }
        }

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Slighly rearranged for clearer control flow:

    static void vfio_err_notifier_handler(void *opaque) {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) { return;
        }

Quoted 7 lines
Quoted 7 lines

        if (vdev->err_handler) {
            if (vdev->err_handler(vdev, &err)) {
                /* Error successfully handled */
                return;
            }
            error_report_err(err);
        }

Yes, this is what i intended to do with my patch and provide a clearer flow. Though the compiler error reported by Cedric, is a little confusing, need to understand why that happens.

Quoted 12 lines
Quoted 12 lines

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Questions / issues:

* Is the comment still accurate?

This comment would still apply for vfio-pci devices on other architectures except for s390x. We are trying to change this behavior for s390x.

The comment is about things that should be done to handle the error. Would these things be done here, or in a suitable ->err_handler()?

Quoted 11 lines
Quoted 9 lines
Quoted 3 lines

* When ->err_handler() fails, we report the error twice. Would it make sense to combine the two error reports into one?

Yes. It was my request too.

Thanks,

C.

I was a little hesitant about changing the existing error message as its been there for almost 12 years (since commit 7b4b0e9eda ("vfio: QEMU-AER: Qemu changes to support AER for VFIO-PCI devices")). Nothing should ever dependent on specific error messages, but still.. .If the preference is to combine/change the message I can do that.

Don't hesitate to improve error messages.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

* Preexisting: the second error message is ugly.

   Error messages should be short and to the point: single phrase, with no newline or trailing punctuation.  The "please collect ..." part does not belong to the error message proper, it's advice on what to do.  Better: report the error, then print advice:

        error_report("%s(%s) Unrecoverable error detected",
                     __func__, vdev->vbasedev.name); error_printf("Please collect any data possible and then kill the guest.");

   Including __func__ in an error message is an anti-pattern. Look at

     vfio_err_notifier_handler(fred) Unrecoverable error detected

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

   with a user's eyes: "vfio_err_notifier_handler" is programmer gobbledygook, the device name "fred" is useful once you realize what it is, "Unrecoverable error detected" lacks detail.

[...]

How about "(device) Unrecoverable PCIe error detected for device"

Suggest "for device %s", where %s identifies the device to the user.

author
#11 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 152 lines · Markus Armbruster
Quoted 150 lines · Farhan Ali
Quoted 125 lines · Cédric Le Goater
Quoted 124 lines · Markus Armbruster
Quoted 77 lines · Farhan Ali
Quoted 46 lines · Markus Armbruster
Quoted 19 lines · Farhan Ali

Provide a vfio error handling callback, that can be used by devices to handle PCI errors for passthrough devices.

---
    hw/vfio/pci.c | 8 ++++++++
    hw/vfio/pci.h | 1 +
    2 files changed, 9 insertions(+)
hw/vfio/pci.c +1 -0
index bc0b4c4d56..b02a974954 100644
@@ -3063,11 +3063,19 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
   static void vfio_err_notifier_handler(void *opaque)
   {
       VFIOPCIDevice *vdev = opaque;
+    Error *err = NULL;
 

hw/vfio/pci.c void vfio_pci_put_device(VFIOPCIDevice *vdev) · line 3077

Quoted 10 lines
hw/vfio/pci.c +6 -0
       if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
           return;
       }
 
+    if (vdev->err_handler) {
+        if (vdev->err_handler(vdev, &err)) {
+            return;
+        }
+        error_report_err(err);
+    }

This is unusual.

Functions taking an Error ** argument usually do so to report errors. The rules spelled out in qapi/error.h apply.  In particular:

   * - On success, the function should not touch *errp.  On failure, it *   should set a new error, e.g. with error_setg(errp, ...), or *   propagate an existing one, e.g. with error_propagate(errp, ...). *
   * - 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,

Quoted 7 lines

If ->err_handler() behaved that way, it @err would be null after it returns false.  We'd call error_report_err(NULL), and crash.

Functions with unusual behavior need a contract: a comment spelling out their behavior.

What is the intended behavior of the err_handler() callback?

Hi Markus,

Thanks for reviewing! The intended behavior for err_handler() is to set errp and report the error on false/failure. With the above code, I also intended fall through to vm_stop() when err_handler() fails.

I think I misunderstood the errp error handling, it seems like the correct way to do what I intended would be

hw/vfio/pci.c +3 -2
index b02a974954..630de46c90 100644
@@ -3070,10 +3070,11 @@ static void vfio_err_notifier_handler(void *opaque)
       }
 
       if (vdev->err_handler) {
-        if (vdev->err_handler(vdev, &err)) {
+        if (!vdev->err_handler(vdev, &err)) {
+            error_report_err(err);
+        } else {
               return;
           }
-        error_report_err(err);
       }
 
Quoted 1 line

Please correct me if I missed anything.

Resulting function:

    static void vfio_err_notifier_handler(void *opaque) {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) { return;
        }

        if (vdev->err_handler) {
            if (!vdev->err_handler(vdev, &err)) { error_report_err(err);
            } else {
                return;
            }
        }

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Slighly rearranged for clearer control flow:

    static void vfio_err_notifier_handler(void *opaque) {
        VFIOPCIDevice *vdev = opaque;
        Error *err = NULL;

        if (!event_notifier_test_and_clear(&vdev->err_notifier)) { return;
        }

Quoted 7 lines
Quoted 7 lines

        if (vdev->err_handler) {
            if (vdev->err_handler(vdev, &err)) {
                /* Error successfully handled */
                return;
            }
            error_report_err(err);
        }

Yes, this is what i intended to do with my patch and provide a clearer flow. Though the compiler error reported by Cedric, is a little confusing, need to understand why that happens.

Quoted 14 lines
Quoted 14 lines

        /*
         * TBD. Retrieve the error details and decide what action
         * needs to be taken. One of the actions could be to pass
         * the error to the guest and have the guest driver recover
         * from the error. This requires that PCIe capabilities be
         * exposed to the guest. For now, we just terminate the
         * guest to contain the error.
         */

        error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);

        vm_stop(RUN_STATE_INTERNAL_ERROR);
    }

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

Questions / issues:

* Is the comment still accurate?

This comment would still apply for vfio-pci devices on other architectures except for s390x. We are trying to change this behavior for s390x.

The comment is about things that should be done to handle the error. Would these things be done here, or in a suitable ->err_handler()?

Ideally in the err_handler(). And for s390x we try do what the comment mentions, which is inject the error into the guest through s390x architecture specific mechanism. I can remove the comment block.

Quoted 26 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

* When ->err_handler() fails, we report the error twice. Would it make sense to combine the two error reports into one?

Yes. It was my request too.

Thanks,

C.

I was a little hesitant about changing the existing error message as its been there for almost 12 years (since commit 7b4b0e9eda ("vfio: QEMU-AER: Qemu changes to support AER for VFIO-PCI devices")). Nothing should ever dependent on specific error messages, but still.. .If the preference is to combine/change the message I can do that.

Don't hesitate to improve error messages.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

* Preexisting: the second error message is ugly.

   Error messages should be short and to the point: single phrase, with no newline or trailing punctuation.  The "please collect ..." part does not belong to the error message proper, it's advice on what to do.  Better: report the error, then print advice:

        error_report("%s(%s) Unrecoverable error detected",
                     __func__, vdev->vbasedev.name); error_printf("Please collect any data possible and then kill the guest.");

   Including __func__ in an error message is an anti-pattern. Look at

     vfio_err_notifier_handler(fred) Unrecoverable error detected

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

   with a user's eyes: "vfio_err_notifier_handler" is programmer gobbledygook, the device name "fred" is useful once you realize what it is, "Unrecoverable error detected" lacks detail.

[...]

How about "(device) Unrecoverable PCIe error detected for device"

Suggest "for device %s", where %s identifies the device to the user.

Sure, I can make the change.

Thanks

Farhan

Quoted 1 line
#12 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 5 lines · Farhan Ali
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Farhan Ali
Quoted 1 line · Cédric Le Goater

On 9/27/25 07:59, Markus Armbruster wrote:

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

* Is the comment still accurate?

This comment would still apply for vfio-pci devices on other architectures except for s390x. We are trying to change this behavior for s390x.

The comment is about things that should be done to handle the error. Would these things be done here, or in a suitable ->err_handler()?

Ideally in the err_handler(). And for s390x we try do what the comment mentions, which is inject the error into the guest through s390x architecture specific mechanism. I can remove the comment block.

Well, if there's stuff left to do, a comment outlining it is desirable. If I understand you correctly, then the one we have is no longer accurate. Could you update it, so it is?

[...]

author
#13 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 11 lines · Markus Armbruster
Quoted 5 lines · Farhan Ali
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Farhan Ali
Quoted 1 line · Cédric Le Goater

On 9/27/25 07:59, Markus Armbruster wrote:

[...]

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

* Is the comment still accurate?

This comment would still apply for vfio-pci devices on other architectures except for s390x. We are trying to change this behavior for s390x.

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

The comment is about things that should be done to handle the error. Would these things be done here, or in a suitable ->err_handler()?

Ideally in the err_handler(). And for s390x we try do what the comment mentions, which is inject the error into the guest through s390x architecture specific mechanism. I can remove the comment block.

Well, if there's stuff left to do, a comment outlining it is desirable. If I understand you correctly, then the one we have is no longer accurate. Could you update it, so it is?

[...]

How about something like this?

We can retrieve the error details and decide what action needs to be taken in err_handler(). One of the actions could be to pass the error to the guest and have the guest driver recover from the error. This requires that PCIe capabilities be exposed to the guest. If err_handler() is not implemented/fails, we just terminate the guest to contain the error.

Thanks

Farhan

#14 lore raw

Re: [PATCH v3 2/5] vfio/pci: Add an error handler callback

Quoted 24 lines · Farhan Ali
Quoted 23 lines · Markus Armbruster
Quoted 5 lines · Farhan Ali
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Farhan Ali
Quoted 1 line · Cédric Le Goater

On 9/27/25 07:59, Markus Armbruster wrote:

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

* Is the comment still accurate?

This comment would still apply for vfio-pci devices on other architectures except for s390x. We are trying to change this behavior for s390x.

The comment is about things that should be done to handle the error. Would these things be done here, or in a suitable ->err_handler()?

Ideally in the err_handler(). And for s390x we try do what the comment mentions, which is inject the error into the guest through s390x architecture specific mechanism. I can remove the comment block.

Well, if there's stuff left to do, a comment outlining it is desirable. If I understand you correctly, then the one we have is no longer accurate. Could you update it, so it is?

Quoted 5 lines
Quoted 2 lines

[...]

How about something like this?

We can retrieve the error details and decide what action needs to be taken in err_handler(). One of the actions could be to pass the error to the guest and have the guest driver recover from the error. This requires that PCIe capabilities be exposed to the guest. If err_handler() is not implemented/fails, we just terminate the guest to contain the error.

Looks good to me. I lack the expertise to really judge.