Skip to contentSkip to conversation

VFIO: Clear stale MSIx table during EEH reset

PATCH Superseded Gavin Shan 4 participants 15 messages Mar 11, 2015 – Mar 16, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH 1/3 VFIO: Clear stale MSIx table during EEH reset

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch clears stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/vfio/common.c       |  6 +++++-
 hw/vfio/pci.c          | 39 +++++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h |  3 ++-
 3 files changed, 46 insertions(+), 2 deletions(-)
hw/vfio/common.c +5 -1
index 148eb53..e3833f4 100644
@@ -949,8 +949,12 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
     switch (req) {
     case VFIO_CHECK_EXTENSION:
     case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
-    case VFIO_EEH_PE_OP:
         break;
+    case VFIO_EEH_PE_OP:
+        if (!vfio_container_eeh_event(as, groupid, param)) {
+            break;
+        }
+        /* fallthru */
     default:
         /* Return an error on unknown requests */
         error_report("vfio: unsupported ioctl %X", req);
hw/vfio/pci.c +39 -0
index 6b80539..8c4a8cb 100644
@@ -3319,6 +3319,45 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                             struct vfio_eeh_pe_op *op)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        vfio_put_group(group);
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    switch (op->op) {
+    case VFIO_EEH_PE_RESET_HOT:
+    case VFIO_EEH_PE_RESET_FUNDAMENTAL:
+        /*
+         * The MSIx table will be cleaned out by reset. We need
+         * disable it so that it can be reenabled properly. Also,
+         * the cached MSIx table should be cleared as it's not
+         * reflecting the contents in hardware.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            if (msix_enabled(&vdev->pdev)) {
+                vfio_disable_msix(vdev);
+            }
+
+            msix_reset(&vdev->pdev);
+        }
+
+        break;
+    }
+
+    vfio_put_group(group);
+    return 0;
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -1
index 0b26cd8..99528a3 100644
@@ -5,5 +5,6 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
-
+extern int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                                    struct vfio_eeh_pe_op *op);
 #endif
Signature · 1 line
1.8.3.2
author
#1 lore raw

PATCH 2/3 VFIO: Clear INTx pending state on EEH reset

When Linux guest recovers from EEH error on the following Emulex adapter, the MSIx interrupts are disabled and the INTx emulation is enabled. One INTx interrupt is injected to the guest by host because of detected pending INTx interrupts on the adapter. QEMU disables mmap'ed BAR regions and starts a timer to enable those regions at later point the INTx interrupt handler. Unfortunately, "VFIOPCIDevice->intx.pending" isn't cleared, meaning those disabled mapp'ed BAR regions won't be reenabled properly. It leads to EEH recovery failure at guest side because of hanged MMIO access.

 # lspci | grep Emulex
 0000:01:00.0 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)
 0000:01:00.1 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)

The patch clears "VFIOPCIDevice->intx.pending" after EEH reset is completed on the PE, which contains the adapter. In turn, the mmap'ed BAR regions can be reenabled to avoid EEH recovery failure.

---
 hw/vfio/pci.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/pci.c +14 -0
index 8c4a8cb..55e0904 100644
@@ -3352,6 +3352,20 @@ int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
         }
 
         break;
+    case VFIO_EEH_PE_RESET_DEACTIVATE:
+        /*
+         * We might have INTx interrupt whose handler disabled the
+         * memory mapped BARs. Without clearing the INTx pending
+         * state, the timer kicked by the INTx interrupt handler
+         * won't enable those disabled memory mapped BARs, which
+         * leads EEH recovery failure.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            vdev->intx.pending = false;
+        }
+
+        break;
     }
 
     vfio_put_group(group);
Signature · 1 line
1.8.3.2
#2 lore raw

Re: [Qemu-devel] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 40 lines · Gavin Shan

When Linux guest recovers from EEH error on the following Emulex adapter, the MSIx interrupts are disabled and the INTx emulation is enabled. One INTx interrupt is injected to the guest by host because of detected pending INTx interrupts on the adapter. QEMU disables mmap'ed BAR regions and starts a timer to enable those regions at later point the INTx interrupt handler. Unfortunately, "VFIOPCIDevice->intx.pending" isn't cleared, meaning those disabled mapp'ed BAR regions won't be reenabled properly. It leads to EEH recovery failure at guest side because of hanged MMIO access.

 # lspci | grep Emulex
 0000:01:00.0 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)
 0000:01:00.1 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)

The patch clears "VFIOPCIDevice->intx.pending" after EEH reset is completed on the PE, which contains the adapter. In turn, the mmap'ed BAR regions can be reenabled to avoid EEH recovery failure.

---
 hw/vfio/pci.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/pci.c +7 -0
index 8c4a8cb..55e0904 100644
@@ -3352,6 +3352,20 @@ int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
         }
 
         break;
+    case VFIO_EEH_PE_RESET_DEACTIVATE:
+        /*
+         * We might have INTx interrupt whose handler disabled the
+         * memory mapped BARs. Without clearing the INTx pending
+         * state, the timer kicked by the INTx interrupt handler
+         * won't enable those disabled memory mapped BARs, which
+         * leads EEH recovery failure.

hw/vfio/pci.c int vfio_container_eeh_event(AddressSpace *as, int32_t groupid, · line 3371

Quoted 10 lines
hw/vfio/pci.c +7 -0
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            vdev->intx.pending = false;
+        }
+
+        break;
     }
 
     vfio_put_group(group);

I'm not sure that invoking these side effects from the low-level ioctl() wrapper makes a lot of sense. Wouldn't it be clearer to explicitly do the necessary state cleanup in the reset callers.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#3 lore raw

Re: [Qemu-devel] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 50 lines · David Gibson
Quoted 49 lines · Gavin Shan

When Linux guest recovers from EEH error on the following Emulex adapter, the MSIx interrupts are disabled and the INTx emulation is enabled. One INTx interrupt is injected to the guest by host because of detected pending INTx interrupts on the adapter. QEMU disables mmap'ed BAR regions and starts a timer to enable those regions at later point the INTx interrupt handler. Unfortunately, "VFIOPCIDevice->intx.pending" isn't cleared, meaning those disabled mapp'ed BAR regions won't be reenabled properly. It leads to EEH recovery failure at guest side because of hanged MMIO access.

 # lspci | grep Emulex
 0000:01:00.0 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)
 0000:01:00.1 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)

The patch clears "VFIOPCIDevice->intx.pending" after EEH reset is completed on the PE, which contains the adapter. In turn, the mmap'ed BAR regions can be reenabled to avoid EEH recovery failure.

---
 hw/vfio/pci.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/pci.c +14 -0
index 8c4a8cb..55e0904 100644
@@ -3352,6 +3352,20 @@ int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
         }
 
         break;
+    case VFIO_EEH_PE_RESET_DEACTIVATE:
+        /*
+         * We might have INTx interrupt whose handler disabled the
+         * memory mapped BARs. Without clearing the INTx pending
+         * state, the timer kicked by the INTx interrupt handler
+         * won't enable those disabled memory mapped BARs, which
+         * leads EEH recovery failure.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            vdev->intx.pending = false;
+        }
+
+        break;
     }
 
Quoted 6 lines

hw/vfio/pci.c int vfio_container_eeh_event(AddressSpace *as, int32_t groupid, · line 3371

Quoted 1 line
hw/vfio/pci.c +0 -0
     vfio_put_group(group);

I'm not sure that invoking these side effects from the low-level ioctl() wrapper makes a lot of sense. Wouldn't it be clearer to explicitly do the necessary state cleanup in the reset callers.

Yes, I agree that putting this into reset caller can help isolating PowerPC unique EEH code from general code. The only problem I have is "struct VFIOPCIDevice" is only visible in hw/vfio/pci.c

Thanks,
Gavin

Quoted 5 lines
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

In reply to Gavin Shan
Quoted 35 lines · Gavin Shan

When Linux guest recovers from EEH error on the following Emulex adapter, the MSIx interrupts are disabled and the INTx emulation is enabled. One INTx interrupt is injected to the guest by host because of detected pending INTx interrupts on the adapter. QEMU disables mmap'ed BAR regions and starts a timer to enable those regions at later point the INTx interrupt handler. Unfortunately, "VFIOPCIDevice->intx.pending" isn't cleared, meaning those disabled mapp'ed BAR regions won't be reenabled properly. It leads to EEH recovery failure at guest side because of hanged MMIO access.

 # lspci | grep Emulex
 0000:01:00.0 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)
 0000:01:00.1 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)

The patch clears "VFIOPCIDevice->intx.pending" after EEH reset is completed on the PE, which contains the adapter. In turn, the mmap'ed BAR regions can be reenabled to avoid EEH recovery failure.

---
 hw/vfio/pci.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/pci.c +2 -0
index 8c4a8cb..55e0904 100644
@@ -3352,6 +3352,20 @@ int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
         }
 
         break;
+    case VFIO_EEH_PE_RESET_DEACTIVATE:
+        /*

hw/vfio/pci.c int vfio_container_eeh_event(AddressSpace *as, int32_t groupid, · line 3366

Quoted 10 lines
hw/vfio/pci.c +10 -0
+         * We might have INTx interrupt whose handler disabled the
+         * memory mapped BARs. Without clearing the INTx pending
+         * state, the timer kicked by the INTx interrupt handler
+         * won't enable those disabled memory mapped BARs, which
+         * leads EEH recovery failure.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            vdev->intx.pending = false;
+        }

I'm nervous that "pending" is trying to track that a) the host interrupt is masked and b) the emulated INTx line for the device is asserted, but we're not clearing the state of any of that here. We can handle a spurious EOI, the device should simply re-assert the interrupt, but changing one piece of tracking w/o getting everything in sync seems like a looming bug. Thanks,

Alex

Quoted 5 lines
hw/vfio/pci.c +2 -0
+
+        break;
     }
 
     vfio_put_group(group);
author
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 45 lines · Alex Williamson
Quoted 44 lines · Gavin Shan

When Linux guest recovers from EEH error on the following Emulex adapter, the MSIx interrupts are disabled and the INTx emulation is enabled. One INTx interrupt is injected to the guest by host because of detected pending INTx interrupts on the adapter. QEMU disables mmap'ed BAR regions and starts a timer to enable those regions at later point the INTx interrupt handler. Unfortunately, "VFIOPCIDevice->intx.pending" isn't cleared, meaning those disabled mapp'ed BAR regions won't be reenabled properly. It leads to EEH recovery failure at guest side because of hanged MMIO access.

 # lspci | grep Emulex
 0000:01:00.0 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)
 0000:01:00.1 Ethernet controller: Emulex Corporation \
              OneConnect 10Gb NIC (be3) (rev 02)

The patch clears "VFIOPCIDevice->intx.pending" after EEH reset is completed on the PE, which contains the adapter. In turn, the mmap'ed BAR regions can be reenabled to avoid EEH recovery failure.

---
 hw/vfio/pci.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/vfio/pci.c +11 -0
index 8c4a8cb..55e0904 100644
@@ -3352,6 +3352,20 @@ int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
         }
 
         break;
+    case VFIO_EEH_PE_RESET_DEACTIVATE:
+        /*
+         * We might have INTx interrupt whose handler disabled the
+         * memory mapped BARs. Without clearing the INTx pending
+         * state, the timer kicked by the INTx interrupt handler
+         * won't enable those disabled memory mapped BARs, which
+         * leads EEH recovery failure.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            vdev->intx.pending = false;
Quoted 9 lines

hw/vfio/pci.c int vfio_container_eeh_event(AddressSpace *as, int32_t groupid, · line 3366

Quoted 1 line
hw/vfio/pci.c +1 -0
+        }

I'm nervous that "pending" is trying to track that a) the host interrupt is masked and b) the emulated INTx line for the device is asserted, but we're not clearing the state of any of that here. We can handle a spurious EOI, the device should simply re-assert the interrupt, but changing one piece of tracking w/o getting everything in sync seems like a looming bug. Thanks,

After the PCI device takes EEH reset, we shouldn't see pending INTx from hardware side. So I think it's safe to clear "intx.pending" when EEH reset is completed. However, I think I have to explain more about how this issue was found and my understanding:

(1) Guest detects EEH errors. The device driver disables MSIx interrupt by calling pci_disable_msix(), which disable MSIx vectors and then enable INTx.

(2) QEMU sends IOCTL commands to host to disable MSIx and enable INTx. At this stage the INTx is still masked. At later point, the guest is requesting unmasking INTx, which is captured by host. Host checks and founds pending INTx, which is sent to QEMU. In QEMU INTx handler (vfio_intx_interrupt()), the mmap'ed regions are disabled, "intx.pending" is set and a timer is started to reenable mmap'ed regions if "intx.pending" is cleared there. However, "intx.pending" is only cleared upon BAR access in slow path, which is never happing.

(3) After guest disables MSIx and issue EEH reset, the device driver starts to check its firmware state by reading MMIO register, which isn't completed by QEMU VFIO BAR slow path (Note: fast path supported by mmaped regions have been disabled). Eventually, the guest hangs on reading MMIO register. With this patch applied to QEMU, I didn't see the problem again.

Thanks,
Gavin

Quoted 10 lines

Alex

Quoted 5 lines
hw/vfio/pci.c +2 -0
+
+        break;
     }
 
     vfio_put_group(group);
#6 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 11 lines · Gavin Shan

(2) QEMU sends IOCTL commands to host to disable MSIx and enable INTx. At this stage the INTx is still masked. At later point, the guest is requesting unmasking INTx, which is captured by host. Host checks and founds pending INTx, which is sent to QEMU. In QEMU INTx handler (vfio_intx_interrupt()), the mmap'ed regions are disabled, "intx.pending" is set and a timer is started to reenable mmap'ed regions if "intx.pending" is cleared there. However, "intx.pending" is only cleared upon BAR access in slow path, which is never happing.

Quoted 5 lines

(3) After guest disables MSIx and issue EEH reset, the device driver starts to check its firmware state by reading MMIO register, which isn't completed by QEMU VFIO BAR slow path (Note: fast path supported by mmaped regions have been disabled). Eventually, the guest hangs on reading MMIO register. With this patch applied to QEMU, I didn't see the problem again.

Note that it might be a good idea to disable INTx (and synchronize with a cfg read of some sort) around resetting a device.

Otherwise, you may hit a known issue if the device is behind a switch and has sent the INTx "assert" message, and not the "deassert" one before it gets reset.

That can cause the INTx to effectively be "stuck" in the switch preventing a subsequent one from being delivered.

Cheers,
Ben.

author
#7 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 18 lines · Benjamin Herrenschmidt
Quoted 11 lines · Gavin Shan

(2) QEMU sends IOCTL commands to host to disable MSIx and enable INTx. At this stage the INTx is still masked. At later point, the guest is requesting unmasking INTx, which is captured by host. Host checks and founds pending INTx, which is sent to QEMU. In QEMU INTx handler (vfio_intx_interrupt()), the mmap'ed regions are disabled, "intx.pending" is set and a timer is started to reenable mmap'ed regions if "intx.pending" is cleared there. However, "intx.pending" is only cleared upon BAR access in slow path, which is never happing.

Quoted 5 lines

(3) After guest disables MSIx and issue EEH reset, the device driver starts to check its firmware state by reading MMIO register, which isn't completed by QEMU VFIO BAR slow path (Note: fast path supported by mmaped regions have been disabled). Eventually, the guest hangs on reading MMIO register. With this patch applied to QEMU, I didn't see the problem again.

Quoted 9 lines

Note that it might be a good idea to disable INTx (and synchronize with a cfg read of some sort) around resetting a device.

Otherwise, you may hit a known issue if the device is behind a switch and has sent the INTx "assert" message, and not the "deassert" one before it gets reset.

That can cause the INTx to effectively be "stuck" in the switch preventing a subsequent one from being delivered.

Yeah, It makes more sense to disable INTx before issuing EEH reset. I verified that disabling INTx interrupt upon EEH reset can avoid the issue as well. I'll post updated patch accordingly if Alex Williamson doesn't object.

Thanks,
Gavin

Quoted 4 lines

Cheers,
Ben.

#8 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 25 lines · Gavin Shan
Quoted 24 lines · Benjamin Herrenschmidt
Quoted 11 lines · Gavin Shan

(2) QEMU sends IOCTL commands to host to disable MSIx and enable INTx. At this stage the INTx is still masked. At later point, the guest is requesting unmasking INTx, which is captured by host. Host checks and founds pending INTx, which is sent to QEMU. In QEMU INTx handler (vfio_intx_interrupt()), the mmap'ed regions are disabled, "intx.pending" is set and a timer is started to reenable mmap'ed regions if "intx.pending" is cleared there. However, "intx.pending" is only cleared upon BAR access in slow path, which is never happing.

Quoted 5 lines

(3) After guest disables MSIx and issue EEH reset, the device driver starts to check its firmware state by reading MMIO register, which isn't completed by QEMU VFIO BAR slow path (Note: fast path supported by mmaped regions have been disabled). Eventually, the guest hangs on reading MMIO register. With this patch applied to QEMU, I didn't see the problem again.

Note that it might be a good idea to disable INTx (and synchronize with a cfg read of some sort) around resetting a device.

Otherwise, you may hit a known issue if the device is behind a switch and has sent the INTx "assert" message, and not the "deassert" one before it gets reset.

Quoted 7 lines
Quoted 3 lines

That can cause the INTx to effectively be "stuck" in the switch preventing a subsequent one from being delivered.

Yeah, It makes more sense to disable INTx before issuing EEH reset. I verified that disabling INTx interrupt upon EEH reset can avoid the issue as well. I'll post updated patch accordingly if Alex Williamson doesn't object.

That sounds like a cleaner approach, but you seem to be skipping something around why the slow-path clearing of intx.pending isn't working for you. Step (2) says "... is only cleared upon BAR access in slow path, which is never happening." Step (3) "the device driver starts to check its firmware state by reading MMIO register, which isn't
completed by QEMU VFIO BAR slow path". So it sounds like (3) is doing
exactly what should allow the QEMU path INTx state machine to advance, so why doesn't it work? Thanks,

Alex

author
#9 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 2/3] VFIO: Clear INTx pending state on EEH reset

Quoted 33 lines · Alex Williamson
Quoted 24 lines · Gavin Shan
Quoted 23 lines · Benjamin Herrenschmidt
Quoted 10 lines · Gavin Shan

(2) QEMU sends IOCTL commands to host to disable MSIx and enable INTx. At this stage the INTx is still masked. At later point, the guest is requesting unmasking INTx, which is captured by host. Host checks and founds pending INTx, which is sent to QEMU. In QEMU INTx handler (vfio_intx_interrupt()), the mmap'ed regions are disabled, "intx.pending" is set and a timer is started to reenable mmap'ed regions if "intx.pending" is cleared there. However, "intx.pending" is only cleared upon BAR access in slow path, which is never happing.

Quoted 5 lines

(3) After guest disables MSIx and issue EEH reset, the device driver starts to check its firmware state by reading MMIO register, which isn't completed by QEMU VFIO BAR slow path (Note: fast path supported by mmaped regions have been disabled). Eventually, the guest hangs on reading MMIO register. With this patch applied to QEMU, I didn't see the problem again.

Note that it might be a good idea to disable INTx (and synchronize with a cfg read of some sort) around resetting a device.

Otherwise, you may hit a known issue if the device is behind a switch and has sent the INTx "assert" message, and not the "deassert" one before it gets reset.

Quoted 7 lines
Quoted 3 lines

That can cause the INTx to effectively be "stuck" in the switch preventing a subsequent one from being delivered.

Yeah, It makes more sense to disable INTx before issuing EEH reset. I verified that disabling INTx interrupt upon EEH reset can avoid the issue as well. I'll post updated patch accordingly if Alex Williamson doesn't object.

Quoted 9 lines

That sounds like a cleaner approach, but you seem to be skipping something around why the slow-path clearing of intx.pending isn't working for you. Step (2) says "... is only cleared upon BAR access in slow path, which is never happening." Step (3) "the device driver starts to check its firmware state by reading MMIO register, which isn't
completed by QEMU VFIO BAR slow path". So it sounds like (3) is doing
exactly what should allow the QEMU path INTx state machine to advance, so why doesn't it work? Thanks,

Thanks for confirm. I'll send out v2 tomorrow.

Nope, I'm not skipping why the slow path doesn't work. I didn't understand well on how the QEMU memory model works together with KVM. I need more time to trace and update with the findings.

Thanks,
Gavin

Quoted 3 lines

Alex

author
#10 lore raw

PATCH 3/3 sPAPR: Reenable EEH functionality on reboot

In reply to Gavin Shan

When rebooting the guest, some PEs might be in frozen state. The contained PCI devices won't work properly if their frozen states aren't cleared in time. One case running into this situation would be maximal EEH error times encountered in the guest.

The patch reenables the EEH functinality on PEs on PHB's reset callback, which will clear their frozen states if needed.

---
 hw/ppc/spapr_pci_vfio.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci_vfio.c +13 -1
index 99a1be5..0cd96d3 100644
@@ -71,9 +71,21 @@ static void spapr_phb_vfio_finish_realize(sPAPRPHBState *sphb, Error **errp)
                                 spapr_tce_get_iommu(tcet));
 }
 
+/*
+ * The PE might be in frozen state. To reenable the EEH
+ * functionality on it will clean the frozen state, which
+ * ensures that the comtained PCI devices will work properly.
+ */
 static void spapr_phb_vfio_reset(DeviceState *qdev)
 {
-    /* Do nothing */
+    sPAPRPHBVFIOState *svphb = SPAPR_PCI_VFIO_HOST_BRIDGE(qdev);
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = VFIO_EEH_PE_ENABLE,
+    };
+
+    vfio_container_ioctl(&svphb->phb.iommu_as,
+                         svphb->iommugroupid, VFIO_EEH_PE_OP, &op);
 }
 
 static int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
Signature · 1 line
1.8.3.2
#11 lore raw
In reply to Gavin Shan
Quoted 21 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch clears stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/vfio/common.c       |  6 +++++-
 hw/vfio/pci.c          | 39 +++++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h |  3 ++-
 3 files changed, 46 insertions(+), 2 deletions(-)
hw/vfio/common.c +0 -0
index 148eb53..e3833f4 100644

hw/vfio/common.c int vfio_container_ioctl(AddressSpace *as, int32_t groupid, · line 954

Quoted 7 lines
hw/vfio/common.c +2 -1
@@ -949,8 +949,12 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
     switch (req) {
     case VFIO_CHECK_EXTENSION:
     case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
-    case VFIO_EEH_PE_OP:
         break;
+    case VFIO_EEH_PE_OP:
+        if (!vfio_container_eeh_event(as, groupid, param)) {

Please use == 0 not !, remembering that !some_function() is the success case hurts my brain.

hw/vfio/common.c int vfio_container_ioctl(AddressSpace *as, int32_t groupid, · line 957

Quoted 3 lines
hw/vfio/common.c +3 -0
+            break;
+        }
+        /* fallthru */

It doesn't look like the fallthrough will generate the correct error
message: it will say "unsupported ioctl" but vfio_container_eeh_event() could fail for some other reason.

Quoted 11 lines
hw/vfio/common.c +0 -0
     default:
         /* Return an error on unknown requests */
         error_report("vfio: unsupported ioctl %X", req);
hw/vfio/pci.c +0 -0
index 6b80539..8c4a8cb 100644
@@ -3319,6 +3319,45 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3331

Quoted 10 lines
hw/vfio/pci.c +10 -0
+int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                             struct vfio_eeh_pe_op *op)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        vfio_put_group(group);

Is vfio_put_group(NULL) really what you want?

Quoted 44 lines
hw/vfio/pci.c +29 -0
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    switch (op->op) {
+    case VFIO_EEH_PE_RESET_HOT:
+    case VFIO_EEH_PE_RESET_FUNDAMENTAL:
+        /*
+         * The MSIx table will be cleaned out by reset. We need
+         * disable it so that it can be reenabled properly. Also,
+         * the cached MSIx table should be cleared as it's not
+         * reflecting the contents in hardware.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            if (msix_enabled(&vdev->pdev)) {
+                vfio_disable_msix(vdev);
+            }
+
+            msix_reset(&vdev->pdev);
+        }
+
+        break;
+    }
+
+    vfio_put_group(group);
+    return 0;
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -1
index 0b26cd8..99528a3 100644
@@ -5,5 +5,6 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
-
+extern int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                                    struct vfio_eeh_pe_op *op);
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#12 lore raw
Quoted 17 lines · David Gibson
Quoted 16 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch clears stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/vfio/common.c       |  6 +++++-
 hw/vfio/pci.c          | 39 +++++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h |  3 ++-
 3 files changed, 46 insertions(+), 2 deletions(-)
Quoted 16 lines

hw/vfio/common.c int vfio_container_ioctl(AddressSpace *as, int32_t groupid, · line 954

Quoted 12 lines
hw/vfio/common.c +2 -1
index 148eb53..e3833f4 100644
@@ -949,8 +949,12 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
     switch (req) {
     case VFIO_CHECK_EXTENSION:
     case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
-    case VFIO_EEH_PE_OP:
         break;
+    case VFIO_EEH_PE_OP:
+        if (!vfio_container_eeh_event(as, groupid, param)) {

Please use == 0 not !, remembering that !some_function() is the success case hurts my brain.

Yes, I'll fix it as below.

Quoted 8 lines

hw/vfio/common.c int vfio_container_ioctl(AddressSpace *as, int32_t groupid, · line 957

Quoted 3 lines
hw/vfio/common.c +3 -0
+            break;
+        }
+        /* fallthru */

It doesn't look like the fallthrough will generate the correct error
message: it will say "unsupported ioctl" but vfio_container_eeh_event() could fail for some other reason.

For now, vfio_container_eeh_event() fails when VFIO group can't be found, which is checked by vfio_container_do_ioctl() as well. However, it's worthy to have precise message as follows:

	case VFIO_EEH_PE_OP:
	    if (vfio_container_eeh_event(as, groupid, param) != 0) {
                error_report("vfio: cannot handle EEH event on group %d\n",
                             groupid);
            }

            break;
Quoted 11 lines
Quoted 11 lines
hw/vfio/common.c +0 -0
     default:
         /* Return an error on unknown requests */
         error_report("vfio: unsupported ioctl %X", req);
hw/vfio/pci.c +0 -0
index 6b80539..8c4a8cb 100644
@@ -3319,6 +3319,45 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
Quoted 13 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3331

Quoted 10 lines
hw/vfio/pci.c +10 -0
+int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                             struct vfio_eeh_pe_op *op)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        vfio_put_group(group);

Is vfio_put_group(NULL) really what you want?

No, it should be dropped.

Thanks,
Gavin

Quoted 50 lines
Quoted 44 lines
hw/vfio/pci.c +29 -0
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    switch (op->op) {
+    case VFIO_EEH_PE_RESET_HOT:
+    case VFIO_EEH_PE_RESET_FUNDAMENTAL:
+        /*
+         * The MSIx table will be cleaned out by reset. We need
+         * disable it so that it can be reenabled properly. Also,
+         * the cached MSIx table should be cleared as it's not
+         * reflecting the contents in hardware.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            if (msix_enabled(&vdev->pdev)) {
+                vfio_disable_msix(vdev);
+            }
+
+            msix_reset(&vdev->pdev);
+        }
+
+        break;
+    }
+
+    vfio_put_group(group);
+    return 0;
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -1
index 0b26cd8..99528a3 100644
@@ -5,5 +5,6 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
-
+extern int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                                    struct vfio_eeh_pe_op *op);
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#13 lore raw
In reply to Gavin Shan
Quoted 39 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch clears stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/vfio/common.c       |  6 +++++-
 hw/vfio/pci.c          | 39 +++++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h |  3 ++-
 3 files changed, 46 insertions(+), 2 deletions(-)
hw/vfio/common.c +5 -1
index 148eb53..e3833f4 100644
@@ -949,8 +949,12 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
     switch (req) {
     case VFIO_CHECK_EXTENSION:
     case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
-    case VFIO_EEH_PE_OP:
         break;
+    case VFIO_EEH_PE_OP:
+        if (!vfio_container_eeh_event(as, groupid, param)) {
+            break;
+        }
+        /* fallthru */
     default:
         /* Return an error on unknown requests */
         error_report("vfio: unsupported ioctl %X", req);
hw/vfio/pci.c +0 -0
index 6b80539..8c4a8cb 100644

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3323

Quoted 5 lines
hw/vfio/pci.c +2 -0
@@ -3319,6 +3319,45 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                             struct vfio_eeh_pe_op *op)

Called by common, but lives in pci. This would need a stub if QEMU is built without CONFIG_PCI. Thanks,

Alex

Quoted 52 lines
hw/vfio/pci.c +37 -0
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        vfio_put_group(group);
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    switch (op->op) {
+    case VFIO_EEH_PE_RESET_HOT:
+    case VFIO_EEH_PE_RESET_FUNDAMENTAL:
+        /*
+         * The MSIx table will be cleaned out by reset. We need
+         * disable it so that it can be reenabled properly. Also,
+         * the cached MSIx table should be cleared as it's not
+         * reflecting the contents in hardware.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            if (msix_enabled(&vdev->pdev)) {
+                vfio_disable_msix(vdev);
+            }
+
+            msix_reset(&vdev->pdev);
+        }
+
+        break;
+    }
+
+    vfio_put_group(group);
+    return 0;
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -1
index 0b26cd8..99528a3 100644
@@ -5,5 +5,6 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
-
+extern int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                                    struct vfio_eeh_pe_op *op);
 #endif
author
#14 lore raw
Quoted 43 lines · Alex Williamson
Quoted 42 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch clears stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/vfio/common.c       |  6 +++++-
 hw/vfio/pci.c          | 39 +++++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h |  3 ++-
 3 files changed, 46 insertions(+), 2 deletions(-)
hw/vfio/common.c +5 -1
index 148eb53..e3833f4 100644
@@ -949,8 +949,12 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
     switch (req) {
     case VFIO_CHECK_EXTENSION:
     case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
-    case VFIO_EEH_PE_OP:
         break;
+    case VFIO_EEH_PE_OP:
+        if (!vfio_container_eeh_event(as, groupid, param)) {
+            break;
+        }
+        /* fallthru */
     default:
         /* Return an error on unknown requests */
         error_report("vfio: unsupported ioctl %X", req);
hw/vfio/pci.c +0 -0
index 6b80539..8c4a8cb 100644
@@ -3319,6 +3319,45 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
Quoted 6 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3323

Quoted 2 lines
hw/vfio/pci.c +2 -0
+int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                             struct vfio_eeh_pe_op *op)

Called by common, but lives in pci. This would need a stub if QEMU is built without CONFIG_PCI. Thanks,

Right. I'll add a stub for !CONFIG_PCI.

Thanks,
Gavin

Quoted 57 lines

Alex

Quoted 52 lines
hw/vfio/pci.c +37 -0
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        vfio_put_group(group);
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    switch (op->op) {
+    case VFIO_EEH_PE_RESET_HOT:
+    case VFIO_EEH_PE_RESET_FUNDAMENTAL:
+        /*
+         * The MSIx table will be cleaned out by reset. We need
+         * disable it so that it can be reenabled properly. Also,
+         * the cached MSIx table should be cleared as it's not
+         * reflecting the contents in hardware.
+         */
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+            if (msix_enabled(&vdev->pdev)) {
+                vfio_disable_msix(vdev);
+            }
+
+            msix_reset(&vdev->pdev);
+        }
+
+        break;
+    }
+
+    vfio_put_group(group);
+    return 0;
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -1
index 0b26cd8..99528a3 100644
@@ -5,5 +5,6 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
-
+extern int vfio_container_eeh_event(AddressSpace *as, int32_t groupid,
+                                    struct vfio_eeh_pe_op *op);
 #endif