[RFC PATCH v3 13/20] vfio/pci: Track PCI recovery state
From: Shameer Kolothum
Date: Thu Nov 12 2026 - 09:34:28 EST
PCI error recovery spans several callbacks. Add state to track its
progress so later patches can keep access blocked until recovery
succeeds and leave it blocked if recovery fails.
Add recovery flags, a sequence counter and saved PCI_COMMAND state.
Initialize per-open state during open and clear it on close. Keep host
recovery state separate so close does not lose an ongoing recovery that
must prevent reopening.
Signed-off-by: Shameer Kolothum <skolothumtho@xxxxxxxxxx>
---
include/linux/vfio_pci_core.h | 18 +++++++++++++++++-
drivers/vfio/pci/vfio_pci_core.c | 7 +++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 88aa7b752231..f2b64420cb07 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -98,6 +98,11 @@ static inline int vfio_pci_core_get_dmabuf_phys(
}
#endif
+#define VFIO_PCI_RECOVERY_IN_PROGRESS BIT(0)
+#define VFIO_PCI_RECOVERY_FROZEN BIT(1)
+#define VFIO_PCI_RECOVERY_RESET BIT(2)
+#define VFIO_PCI_RECOVERY_FAILED BIT(3)
+
struct vfio_pci_access_ops;
struct vfio_pci_core_device {
@@ -159,7 +164,18 @@ struct vfio_pci_core_device {
const struct vfio_pci_access_ops __rcu *access_ops;
/* Set after open completes, cleared before close tears down state. */
bool device_open;
- struct mutex access_lock; /* protects changes to access ops and flags */
+ struct mutex access_lock; /* protects access and recovery state */
+ u32 pci_recovery_flags;
+ u64 pci_recovery_sequence;
+ /* Saved PCI_COMMAND, valid when pci_recovery_command_valid is set. */
+ u16 pci_recovery_command;
+ bool pci_recovery_enabled;
+ bool pci_recovery_command_valid;
+ /*
+ * Host recovery is still running. Protected by access_lock and kept across
+ * close, unlike recovery status for the current open.
+ */
+ bool pci_recovery_host_active;
struct list_head dmabufs;
};
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 0299f5afca91..cdc6bafe9861 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -923,8 +923,11 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)
if (vdev->pci_recovery_supported) {
scoped_guard(mutex, &vdev->access_lock) {
+ WRITE_ONCE(vdev->pci_recovery_enabled, false);
+ vdev->pci_recovery_command_valid = false;
WRITE_ONCE(vdev->device_open, false);
vfio_pci_core_block_access(vdev);
+ WRITE_ONCE(vdev->pci_recovery_flags, 0);
}
}
@@ -963,6 +966,10 @@ void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
if (vdev->pci_recovery_supported) {
guard(mutex)(&vdev->access_lock);
+ WRITE_ONCE(vdev->pci_recovery_flags, 0);
+ vdev->pci_recovery_sequence = 0;
+ vdev->pci_recovery_command_valid = false;
+ WRITE_ONCE(vdev->pci_recovery_enabled, false);
WRITE_ONCE(vdev->device_open, true);
rcu_assign_pointer(vdev->access_ops, &vfio_pci_live_ops);
}
--
2.43.0