ppc/spapr: Fix MSI-X shadow and BAR mmap handling during EEH PE reset
v3 cover ppc/spapr: Fix MSI-X shadow and BAR mmap handling during EEH PE reset
This series fixes two independent but related problems in the sPAPR EEH PE reset path for VFIO pass-through devices.
Patch 1 fixes MSI-X interrupt delivery after EEH recovery. The existing pre-reset path calls msix_reset(), which clears QEMU's MSI-X table shadow. On pSeries, this shadow is populated through:
ibm,change-msi
-> spapr_msi_setmsg()
-> msix_set_message()and is not necessarily rebuilt before the guest restores MSI-X after an EEH reset.
Instead of resetting the MSI-X state, disable MSI-X by clearing only the Enable bit using QEMU's cached PCI configuration value and dispatch the write through pci_host_config_write_common(). This lets VFIO tear down the active vectors and KVM irqfd routes through vfio_msix_disable() while preserving the MSI-X shadow needed when the guest later re-enables MSI-X.
Patch 2 handles BAR mmap access while the PE is undergoing an EEH reset. BAR mmap access is disabled before issuing the hot or fundamental PE reset and restored only after VFIO_EEH_PE_CONFIGURE completes successfully. During this interval BAR accesses fall back to the VFIO region read/write path.
Following review feedback, the BAR mmap and INTx mmap timer handling are now encapsulated in VFIO PCI helpers instead of accessing VFIOPCIDevice internals directly from the pSeries code. The pre-reset helper cancels a pending INTx mmap re-enable timer and disables the BAR mmap path after interrupt teardown. The post-configure helper restores the normal mmap/INTx timer policy after successful PE configuration.
Without the fixes, EEH recovery itself reports success for the passed-through BCM5719/tg3 device, but the interface does not recover its link:
<snip>
EEH: Beginning: 'resume'
PCI 0001:00:01.0#0001: EEH: Invoking tg3->resume()
PCI 0001:00:01.0#0001: EEH: tg3 driver reports: 'none'
EEH: Finished:'resume'
EEH: Recovery successful.
</snip>
<snip>
# ethtool enP1p0s1 | grep Link
Link detected: no
</snip>With the series applied, EEH recovery completes and the link subsequently comes back:
<snip>
EEH: Beginning: 'resume'
PCI 0001:00:01.0#0001: EEH: Invoking tg3->resume()
PCI 0001:00:01.0#0001: EEH: tg3 driver reports: 'none'
EEH: Finished:'resume'
EEH: Recovery successful.
tg3 0001:00:01.0 enP1p0s1: Link is up at 1000 Mbps, full duplex
tg3 0001:00:01.0 enP1p0s1: Flow control is on for TX and on for RX
</snip>
<snip>
# ethtool enP1p0s1 | grep Link
Link detected: yes
</snip>---
Change log:
v2:
- Reworked the BAR mmap handling based on review feedback so that generic VFIO provides only the mmap mechanism and the pSeries code retains the EEH policy.
- Replaced the EEH-specific generic VFIO helpers vfio_pci_eeh_pre_reset() and vfio_pci_eeh_post_configure() with a neutral vfio_pci_mmap_block() helper.
- Added per-device mmap_blocked state in vfio-pci. Once BAR mmap is blocked, later attempts to re-enable the mmap subregions, including those from INTx timer or interrupt-mode transitions, are ignored.
- Changed the BAR mmap handling from temporary disable/restore to a persistent block after an EEH PE reset. In particular, BAR mmap is no longer restored after VFIO_EEH_PE_CONFIGURE.
- Removed the pSeries post-configure device/bus walkers that were added in v2 solely to restore BAR mmap.
- Moved the generic mmap-block implementation into patch 1/2 and kept the pSeries EEH use of that mechanism in patch 2/2.
- Block BAR mmap before changing MSI-X state during EEH reset preparation, so subsequent VFIO interrupt-mode activity cannot restore the direct mmap path.
- Retained the v2 MSI-X shadow preservation behavior: clear only PCI_MSIX_FLAGS_ENABLE through the normal PCI config-write path and do not call msix_reset().
- Updated the BAR mmap recovery policy based on BCM5719 passthrough
testing: restoring direct BAR mmap after PE configure could result in the device becoming inaccessible again during guest resume, while keeping BAR accesses on the VFIO region read/write path allowed the Guest-2 EEH recovery to complete and the device to remain functional.
v1:
- Move BAR mmap and INTx mmap timer handling into VFIO PCI helpers,
as suggested by Mahesh.
- Avoid direct access to VFIOPCIDevice BAR and INTx internals from spapr_pci_vfio.c.
- Restore normal INTx mmap handling after successful PE configuration.
- Keep BAR mmap disabled when VFIO_EEH_PE_CONFIGURE fails.
- Keep the MSI-X shadow preservation behavior from patch 1 unchanged.
v1: https://lore.kernel.org/qemu-devel/[email protected]
Narayana Murty N (2):
vfio/pci: Add persistent BAR mmap blocking helper ppc/spapr: Preserve VFIO MSI-X shadow and block BAR mmap on EEH reset
hw/ppc/spapr_pci_vfio.c | 75 ++++++++++++++++++++++++++++------------- hw/vfio/pci.c | 59 ++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 7 ++++ 3 files changed, 117 insertions(+), 24 deletions(-)
Signature · 1 line
2.54.0
PATCH v3 1/2 vfio/pci: Add persistent BAR mmap blocking helper
Some platform recovery flows need to stop using direct mmap-backed BAR subregions after a device reset. Disabling the subregions once is not sufficient because the VFIO INTx mmap timer and interrupt-mode transitions can later call vfio_mmap_set_enabled() with enabled=true.
Add a generic per-device mmap_blocked state and vfio_pci_mmap_block(). Once a device is blocked, attempts to re-enable its direct BAR mmap subregions are ignored. The helper also cancels a pending INTx mmap re-enable timer and disables all currently active BAR mmap subregions.
The parent VFIO MemoryRegion remains enabled, so BAR accesses continue through vfio_region_read()/vfio_region_write() using the VFIO device fd. The helper contains no platform or EEH policy; callers decide when a recovery flow requires direct mmap to remain blocked.
The block is intentionally persistent for the lifetime of the VFIO device object. No unblock operation is added until there is a proven safe point at which an existing direct BAR mmap can be reused after such a reset. The state is not migrated because it controls a local access path optimization rather than guest-visible device state.
--- hw/vfio/pci.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 7 ++++++ 2 files changed, 66 insertions(+)
hw/vfio/pci.c +59 -0
@@ -1949,11 +1949,70 @@ static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled) {int i; +/* +* Some recovery flows require direct BAR mmap to remain disabled +* after a device reset. Honour mmap_blocked so that INTx timer +* callbacks and interrupt-mode transitions cannot re-enable +* the direct mmap subregions once the device has been blocked. +*/ +if (enabled && vdev->mmap_blocked) { +return ; + } +for (i =0 ; i <PCI_ROM_SLOT ; i++) {vfio_region_mmaps_set_enabled (&vdev->bars[i].region, enabled); } } +/** +* vfio_pci_mmap_block - Persistently block direct BAR mmap access +* @vdev: the VFIO PCI device +* +* Some recovery flows cannot safely resume direct BAR mmap access using +* mappings that were established before the device was reset. Once the +* device is blocked, keep accesses on the VFIO region read/write path +* rather than allowing later interrupt-mode or timer activity to restore +* the direct mmap subregions. +* +* This helper: +* 1. Sets mmap_blocked so that no subsequent interrupt-mode +* transition or INTx mmap timer callback can re-enable direct +* BAR mmap access. +* 2. Cancels any pending INTx mmap re-enable timer. +* 3. Disables all currently active BAR mmap subregions. +* +* The parent slow-path VFIO MemoryRegion remains enabled, so BAR +* accesses continue via vfio_region_read()/vfio_region_write() which +* use pread()/pwrite() on the VFIO device fd. This is slower than +* the direct mmap path but remains available while mmap is blocked. +* +* The flag is intentionally not cleared by this helper. Blocking is +* persistent for the lifetime of the VFIO device object unless a future +* change introduces an explicit, proven-safe unblock operation. +* +* The function is idempotent. +*/ +void vfio_pci_mmap_block (VFIOPCIDevice *vdev) +{ +if (vdev->mmap_blocked) { +return ; + } + + vdev->mmap_blocked = true; + +/* +* Cancel any pending INTx mmap re-enable timer. Without this the +* timer could still fire and call vfio_mmap_set_enabled() with +* enabled=true. That would be blocked by the flag above, but +* cancelling it avoids the unnecessary wakeup. +*/ +if (vdev->intx.mmap_timer) { +timer_del (vdev->intx.mmap_timer); + } + +vfio_mmap_set_enabled (vdev, false); +} +static void vfio_bar_prepare (VFIOPCIDevice *vdev,int nr) {VFIOBAR *bar = &vdev->bars[nr];
hw/vfio/pci.h +7 -0
@@ -187,6 +187,12 @@ struct VFIOPCIDevice {bool defer_kvm_irq_routing;bool clear_parent_atomics_on_exit;bool skip_vsc_check; +/* +* Block direct BAR mmap subregions for the lifetime of this +* VFIO device. Calls that would re-enable mmap are ignored +* while this flag is set. +*/ +bool mmap_blocked;uint16_t vpasid_cap_offset;OnOffAuto ats;VFIODisplay *dpy;
@@ -243,6 +249,7 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp);/* BARs */ void vfio_pci_bars_exit (VFIOPCIDevice *vdev);void vfio_sub_page_bar_update_mappings (VFIOPCIDevice *vdev); +void vfio_pci_mmap_block (VFIOPCIDevice *vdev);/* Capabilities */ bool vfio_pci_add_capabilities (VFIOPCIDevice *vdev,Error **errp);
Signature · 1 line
2.54.0
PATCH v3 2/2 ppc/spapr: Preserve VFIO MSI-X shadow and block BAR mmap on EEH reset
In reply to Narayana Murty NPrepare VFIO PCI devices before a pSeries EEH hot or fundamental PE reset without destroying the MSI-X state that the guest needs to recover interrupts.
The existing path clears MSI-X Enable and then calls msix_reset(). On pSeries with vfio-no-msix-emulation=true, QEMU keeps the MSI-X messages used by ibm,change-msi in its software shadow. The Linux EEH recovery sequence does not necessarily issue ibm,change-msi again. Clearing the shadow with msix_reset() therefore loses the message address/data needed when MSI-X is enabled again.
Preserve the shadow by removing msix_reset(). Read the MSI-X Message Control value from QEMU's cached PCI config space, clear only MSI-X Enable, and write it through pci_host_config_write_common(). This keeps the normal VFIO MSI-X disable/enable transition while avoiding a config read from a PE that may still be frozen.
Also block direct BAR mmap access before changing MSI-X state by calling vfio_pci_mmap_block(). Testing with a VFIO-passed-through BCM5719 showed that restoring direct mmap after VFIO_EEH_PE_CONFIGURE can leave the device inaccessible again during guest resume, while keeping BAR access on the VFIO region read/write path allows the Guest-2 EEH recovery to complete and the device to remain functional. Blocking mmap first also prevents a subsequent MSI-X-to-INTx transition from re-enabling the mmap path through the normal INTx policy.
The generic VFIO helper provides only the persistent mmap-block mechanism; the decision to use it for EEH remains local to the sPAPR recovery code. No post-configure mmap restore is added because the current testing does not establish a safe point for reusing the direct BAR mmap after the PE reset.
No guest-visible ABI is changed.
--- hw/ppc/spapr_pci_vfio.c | 75 ++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 24 deletions(-)
hw/ppc/spapr_pci_vfio.c +51 -24
@@ -32,6 +32,8 @@* Interfaces for IBM EEH (Enhanced Error Handling) */ #ifdef CONFIG_VFIO_PCI +#include "hw/vfio/pci.h" +static bool vfio_eeh_container_ok (VFIOLegacyContainer *container) {/*
@@ -233,47 +235,72 @@ int spapr_phb_vfio_eeh_get_state(SpaprPhbState *sphb, int *state)return RTAS_OUT_SUCCESS ; } -static void spapr_phb_vfio_eeh_clear_dev_msix (PCIBus *bus, -PCIDevice *pdev, -void *opaque) +static void spapr_phb_vfio_eeh_prepare_dev (PCIBus *bus, +PCIDevice *pdev, +void *opaque) { -/* Check if the device is VFIO PCI device */ +VFIOPCIDevice *vdev; +uint16_t flags; +if (!object_dynamic_cast (OBJECT (pdev),"vfio-pci" )) {return ; } + vdev =VFIO_PCI_DEVICE (pdev); +/* -* 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. +* Direct BAR mmap access may not be reliable after an EEH PE reset. +* Block mmap before changing MSI-X state so any resulting interrupt +* mode transition cannot re-enable it, and keep BAR accesses on the +* VFIO pread()/pwrite() path after the reset. This avoids restoring +* the direct mmap path at an unproven point in EEH recovery. */ -if (msix_enabled (pdev)) { -uint16_t flags; - - flags =pci_host_config_read_common (pdev, - pdev->msix_cap +PCI_MSIX_FLAGS , -pci_config_size (pdev),2 ); - flags &= ~PCI_MSIX_FLAGS_ENABLE ; -pci_host_config_write_common (pdev, - pdev->msix_cap +PCI_MSIX_FLAGS , -pci_config_size (pdev), flags,2 ); +vfio_pci_mmap_block (vdev); + +if (!msix_enabled (pdev)) { +return ; } -msix_reset (pdev); +/* +* Disable MSI-X by clearing the Enable bit in the MSI-X Message +* Control register. We use the cached config value to avoid a +* config-space read while the PE may still be frozen. +* +* Writing through pci_host_config_write_common() causes VFIO to: +* - release the active MSI-X vector eventfds, +* - remove the KVM irqchip routes, +* - disable the VFIO MSIX_IRQ_INDEX, +* so that the subsequent MSI-X Enable write from the guest driver +* is seen as a clean disabled-to-enabled transition and all +* vectors are re-armed through vfio_msix_vector_do_use(). +* +* Do NOT call msix_reset() here. On pSeries, QEMU's MSI-X table +* is a software shadow populated by the ibm,change-msi RTAS call. +* The EEH recovery sequence does not necessarily re-issue +* ibm,change-msi, so the shadow contains the only copy of the +* message addresses and data that Linux EEH will restore. +* msix_reset() would zero those shadow entries, leaving +* vfio_msix_enable() with no vectors to pass to +* vfio_msix_vector_do_use() and the device would never recover +* interrupts. +*/ + flags =pci_get_word (pdev->config + pdev->msix_cap +PCI_MSIX_FLAGS ); + flags &= ~PCI_MSIX_FLAGS_ENABLE ; +pci_host_config_write_common (pdev, + pdev->msix_cap +PCI_MSIX_FLAGS , +pci_config_size (pdev), flags,2 ); } -static void spapr_phb_vfio_eeh_clear_bus_msix (PCIBus *bus,void *opaque) +static void spapr_phb_vfio_eeh_prepare_bus (PCIBus *bus,void *opaque) { -pci_for_each_device_under_bus (bus, spapr_phb_vfio_eeh_clear_dev_msix, -NULL ); +pci_for_each_device_under_bus (bus, spapr_phb_vfio_eeh_prepare_dev,NULL ); }static void spapr_phb_vfio_eeh_pre_reset (SpaprPhbState *sphb) { -PCIHostState *phb =PCI_HOST_BRIDGE (sphb); +PCIHostState *phb =PCI_HOST_BRIDGE (sphb); -pci_for_each_bus (phb->bus, spapr_phb_vfio_eeh_clear_bus_msix,NULL ); +pci_for_each_bus (phb->bus, spapr_phb_vfio_eeh_prepare_bus,NULL ); }int spapr_phb_vfio_eeh_reset (SpaprPhbState *sphb,int option)
Signature · 1 line
2.54.0