Skip to contentSkip to conversation

Error recovery for vfio-pci devices on s390x

PATCH Superseded Farhan Ali 4 participants 11 messages Jun 15 – Jun 25

Subsystems (5)
PCI s390 VFIO driver s390 PCI +1 more subsystems

v19 of 23 Superseded Latest is v23 →

lore
Share & tools
lore

Farhan Ali

author maintainer
#0 lore raw

v19 cover Error recovery for vfio-pci devices on s390x

Hi Alex,

This patch set includes only the VFIO patches of the original series for error recovery for vfio-pci devices on s390x [1]. Breaking up the patch series into PCI and VFIO only patches to make merging easier based on our discussion [2].

Thanks
Farhan

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

ChangeLog
---------
v18 https://lore.kernel.org/all/[email protected]/
v18 -> v19
- Fix hole in struct vfio_device_feature_zpci_err.
- Remove reset_done() callback for vfio-pci driver.
- Rebase on 7.1-rc7

v17 -> v18
- Print a warn with dev_warn_ratelimited() for copy_to_user failure.
- Rebase on 7.1-rc6.
- Break patch series into VFIO patch set.

Farhan Ali (3):
s390/pci: Store PCI error information for passthrough devices vfio-pci/zdev: Add a device feature for error information vfio/pci: Remove the pcie check for VFIO_PCI_ERR_IRQ_INDEX

 arch/s390/include/asm/pci.h       |  32 +++++++
 arch/s390/pci/pci.c               |   1 +
 arch/s390/pci/pci_event.c         | 137 +++++++++++++++++++-----------
 drivers/vfio/pci/vfio_pci_core.c  |  10 +--
 drivers/vfio/pci/vfio_pci_intrs.c |   3 +-
 drivers/vfio/pci/vfio_pci_priv.h  |   9 ++
 drivers/vfio/pci/vfio_pci_zdev.c  |  61 ++++++++++++-
 include/uapi/linux/vfio.h         |  30 +++++++
 8 files changed, 221 insertions(+), 62 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v19 1/3 s390/pci: Store PCI error information for passthrough devices

For a passthrough device we need co-operation from user space to recover the device. This would require to bubble up any error information to user space. Let's store this error information for passthrough devices, so it can be retrieved later.

We can now have userspace drivers (vfio-pci based) on s390x. The userspace drivers will not have any KVM fd and so no kzdev associated with them. So we need to update the logic for detecting passthrough devices to not depend on struct kvm_zdev.

---
 arch/s390/include/asm/pci.h      |  30 ++++++++
 arch/s390/pci/pci.c              |   1 +
 arch/s390/pci/pci_event.c        | 118 ++++++++++++++++++-------------
 drivers/vfio/pci/vfio_pci_zdev.c |  18 ++++-
 4 files changed, 113 insertions(+), 54 deletions(-)
arch/s390/include/asm/pci.h +30 -0
index 5dcf35f0f325..016386f7ef4a 100644
@@ -118,6 +118,32 @@ struct zpci_bus {
 	enum pci_bus_speed	max_bus_speed;
 };
 
+/* Content Code Description for PCI Function Error */
+struct zpci_ccdf_err {
+	u32 reserved1;
+	u32 fh;                         /* function handle */
+	u32 fid;                        /* function id */
+	u32 ett         :  4;           /* expected table type */
+	u32 mvn         : 12;           /* MSI vector number */
+	u32 dmaas       :  8;           /* DMA address space */
+	u32 reserved2   :  6;
+	u32 q           :  1;           /* event qualifier */
+	u32 rw          :  1;           /* read/write */
+	u64 faddr;                      /* failing address */
+	u32 reserved3;
+	u16 reserved4;
+	u16 pec;                        /* PCI event code */
+} __packed;
+
+#define ZPCI_ERR_PENDING_MAX 4
+struct zpci_ccdf_pending {
+	bool mediated_recovery;
+	u8 count;
+	u8 head;
+	u8 tail;
+	struct zpci_ccdf_err err[ZPCI_ERR_PENDING_MAX];
+};
+
 /* Private data per function */
 struct zpci_dev {
 	struct zpci_bus *zbus;
@@ -192,6 +218,8 @@ struct zpci_dev {
 	struct iommu_domain *s390_domain; /* attached IOMMU domain */
 	struct kvm_zdev *kzdev;
 	struct mutex kzdev_lock;
+	struct zpci_ccdf_pending pending_errs;
+	struct mutex pending_errs_lock;
 	spinlock_t dom_lock;		/* protect s390_domain change */
 };
 
@@ -334,6 +362,8 @@ void zpci_debug_exit_device(struct zpci_dev *);
 int zpci_report_error(struct pci_dev *, struct zpci_report_error_header *);
 int zpci_clear_error_state(struct zpci_dev *zdev);
 int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
+void zpci_start_mediated_recovery(struct zpci_dev *zdev);
+void zpci_stop_mediated_recovery(struct zpci_dev *zdev);
 
 #ifdef CONFIG_NUMA
 
arch/s390/pci/pci.c +1 -0
index 39bd2adfc240..2d377c2e194d 100644
@@ -842,6 +842,7 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
 	mutex_init(&zdev->state_lock);
 	mutex_init(&zdev->fmb_lock);
 	mutex_init(&zdev->kzdev_lock);
+	mutex_init(&zdev->pending_errs_lock);
 
 	return zdev;
 
arch/s390/pci/pci_event.c +67 -51

188 lines not on this page · raw message

drivers/vfio/pci/vfio_pci_zdev.c +15 -3
index 0990fdb146b7..78a28db00c6d 100644
@@ -144,24 +144,36 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 {
 	struct zpci_dev *zdev = to_zpci(vdev->pdev);
+	int ret;
 
 	if (!zdev)
 		return -ENODEV;
 
+	zpci_start_mediated_recovery(zdev);
+
 	if (!vdev->vdev.kvm)
 		return 0;
 
+	ret = -ENOENT;
 	if (zpci_kvm_hook.kvm_register)
-		return zpci_kvm_hook.kvm_register(zdev, vdev->vdev.kvm);
+		ret = zpci_kvm_hook.kvm_register(zdev, vdev->vdev.kvm);
 
-	return -ENOENT;
+	if (ret)
+		zpci_stop_mediated_recovery(zdev);
+
+	return ret;
 }
 
 void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
 {
 	struct zpci_dev *zdev = to_zpci(vdev->pdev);
 
-	if (!zdev || !vdev->vdev.kvm)
+	if (!zdev)
+		return;
+
+	zpci_stop_mediated_recovery(zdev);
+
+	if (!vdev->vdev.kvm)
 		return;
 
 	if (zpci_kvm_hook.kvm_unregister)
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v19 2/3 vfio-pci/zdev: Add a device feature for error information

In reply to Farhan Ali

For zPCI devices, we have platform specific error information. The platform firmware provides this error information to the operating system in an architecture specific mechanism. To enable recovery from userspace for these devices, we want to expose this error information to userspace. Add a new device feature to expose this information.

---
 arch/s390/include/asm/pci.h      |  2 ++
 arch/s390/pci/pci_event.c        | 19 ++++++++++++++
 drivers/vfio/pci/vfio_pci_core.c |  2 ++
 drivers/vfio/pci/vfio_pci_priv.h |  9 +++++++
 drivers/vfio/pci/vfio_pci_zdev.c | 43 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 30 ++++++++++++++++++++++
 6 files changed, 105 insertions(+)
arch/s390/include/asm/pci.h +2 -0
index 016386f7ef4a..88a125b92bdd 100644
@@ -364,6 +364,8 @@ int zpci_clear_error_state(struct zpci_dev *zdev);
 int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
 void zpci_start_mediated_recovery(struct zpci_dev *zdev);
 void zpci_stop_mediated_recovery(struct zpci_dev *zdev);
+int zpci_get_pending_error(struct zpci_dev *zdev,
+			   struct zpci_ccdf_err *ccdf);
 
 #ifdef CONFIG_NUMA
 
arch/s390/pci/pci_event.c +19 -0
index 8934b640a095..3ad16038e005 100644
@@ -77,6 +77,25 @@ static int zpci_store_pci_error(struct pci_dev *pdev,
 	return 0;
 }
 
+int zpci_get_pending_error(struct zpci_dev *zdev,
+			   struct zpci_ccdf_err *ccdf)
+{
+	int head = 0;
+
+	guard(mutex)(&zdev->pending_errs_lock);
+
+	if (!zdev->pending_errs.count)
+		return -ENOMSG;
+
+	head = zdev->pending_errs.head % ZPCI_ERR_PENDING_MAX;
+	memcpy(ccdf, &zdev->pending_errs.err[head],
+	       sizeof(struct zpci_ccdf_err));
+	zdev->pending_errs.head++;
+	zdev->pending_errs.count--;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(zpci_get_pending_error);
+
 void zpci_start_mediated_recovery(struct zpci_dev *zdev)
 {
 	guard(mutex)(&zdev->pending_errs_lock);
drivers/vfio/pci/vfio_pci_core.c +2 -0
index 050e7542952e..27642f10fe97 100644
@@ -1569,6 +1569,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
 		return vfio_pci_core_feature_token(vdev, flags, arg, argsz);
 	case VFIO_DEVICE_FEATURE_DMA_BUF:
 		return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz);
+	case VFIO_DEVICE_FEATURE_ZPCI_ERROR:
+		return vfio_pci_zdev_feature_err(device, flags, arg, argsz);
 	default:
 		return -ENOTTY;
 	}
drivers/vfio/pci/vfio_pci_priv.h +9 -0
index fca9d0dfac90..4e7162234a2e 100644
@@ -93,6 +93,8 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 				struct vfio_info_cap *caps);
 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev);
 void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev);
+int vfio_pci_zdev_feature_err(struct vfio_device *device, u32 flags,
+			      void __user *arg, size_t argsz);
 #else
 static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 					      struct vfio_info_cap *caps)
@@ -107,6 +109,13 @@ static inline int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 
 static inline void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
 {}
+
+static inline int vfio_pci_zdev_feature_err(struct vfio_device *device,
+					    u32 flags, void __user *arg,
+					    size_t argsz)
+{
+	return -ENOTTY;
+}
 #endif
 
 static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
drivers/vfio/pci/vfio_pci_zdev.c +43 -0

49 lines not on this page · raw message

include/uapi/linux/vfio.h +30 -0
index 5de618a3a5ee..83c93193b22d 100644
@@ -1534,6 +1534,36 @@ struct vfio_device_feature_dma_buf {
  */
 #define VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2  12
 
+/**
+ * VFIO_DEVICE_FEATURE_ZPCI_ERROR feature provides PCI error information to
+ * userspace for vfio-pci devices on s390. On s390, PCI error recovery
+ * involves platform firmware and notification to operating systems is done
+ * by architecture specific mechanism. Exposing this information to
+ * userspace allows it to take appropriate actions to handle an
+ * error on the device. The ioctl returns -ENOMSG if there are no pending
+ * PCI errors.
+ */
+
+struct vfio_device_feature_zpci_err {
+	__u32 feature_flags;		/* Indicate future features */
+	__u32 reserved1;
+	__u32 fh;			/* function handle */
+	__u32 fid;			/* function id */
+	__u64 faddr;                    /* failing address */
+	__u32 ett		:  4;	/* expected table type */
+	__u32 mvn		: 12;	/* MSI vector number */
+	__u32 dmaas		:  8;	/* DMA address space */
+	__u32 reserved2		:  6;
+	__u32 q			:  1;	/* event qualifier */
+	__u32 rw		:  1;	/* read/write */
+	__u32 reserved3;
+	__u16 reserved4;
+	__u16 pec;			/* PCI event code */
+	__u8 reserved5[28];		/* Allow for future expansion */
+};
+
+#define VFIO_DEVICE_FEATURE_ZPCI_ERROR 13
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH v19 3/3 vfio/pci: Remove the pcie check for VFIO_PCI_ERR_IRQ_INDEX

In reply to Farhan Ali

The error signaling is configured for the vast majority of devices and it's extremely rare that it fires anyway. Removing the pcie check will allow userspace to be notified on errors for legacy PCI devices. The Internal Shared Memory (ISM) device on s390 is one such device. For PCI devices on IBM s390 error recovery involves platform firmware and notification to operating system is done by architecture specific way. So the ISM device can still be recovered when notified of an error.

---
 drivers/vfio/pci/vfio_pci_core.c  | 8 ++------
 drivers/vfio/pci/vfio_pci_intrs.c | 3 +--
 2 files changed, 3 insertions(+), 8 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +2 -6
index 27642f10fe97..cd3ce30d22f5 100644
@@ -821,8 +821,7 @@ static int vfio_pci_get_irq_count(struct vfio_pci_core_device *vdev, int irq_typ
 			return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
 		}
 	} else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
-		if (pci_is_pcie(vdev->pdev))
-			return 1;
+		return 1;
 	} else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
 		return 1;
 	}
@@ -1198,11 +1197,8 @@ static int vfio_pci_ioctl_get_irq_info(struct vfio_pci_core_device *vdev,
 	switch (info.index) {
 	case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
 	case VFIO_PCI_REQ_IRQ_INDEX:
-		break;
 	case VFIO_PCI_ERR_IRQ_INDEX:
-		if (pci_is_pcie(vdev->pdev))
-			break;
-		fallthrough;
+		break;
 	default:
 		return -EINVAL;
 	}
drivers/vfio/pci/vfio_pci_intrs.c +1 -2
index 33944d4d9dc4..64f80f64ff57 100644
@@ -859,8 +859,7 @@ int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev, uint32_t flags,
 	case VFIO_PCI_ERR_IRQ_INDEX:
 		switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
 		case VFIO_IRQ_SET_ACTION_TRIGGER:
-			if (pci_is_pcie(vdev->pdev))
-				func = vfio_pci_set_err_trigger;
+			func = vfio_pci_set_err_trigger;
 			break;
 		}
 		break;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v19 1/3] s390/pci: Store PCI error information for passthrough devices

In reply to Farhan Ali
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

For a passthrough device we need co-operation from user space to recover the device. This would require to bubble up any error information to user space. Let's store this error information for passthrough devices, so it can be retrieved later.

Quoted 8 lines

We can now have userspace drivers (vfio-pci based) on s390x. The userspace drivers will not have any KVM fd and so no kzdev associated with them. So we need to update the logic for detecting passthrough devices to not depend on struct kvm_zdev.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v19 2/3] vfio-pci/zdev: Add a device feature for error information

In reply to Farhan Ali
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The UAPI struct `vfio_device_feature_zpci_err` uses C bitfields, which violates UAPI design…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The UAPI struct vfio_device_feature_zpci_err uses C bitfields, which violates UAPI design principles and breaks cross-architecture/cross-compiler compatibility. --

commit 894c3dcf471b795ab55da889778d75f212c1eee2
Author: Farhan Ali <[email protected]>

vfio-pci/zdev: Add a device feature for error information

For zPCI devices, platform specific error information is provided by firmware. This commit adds a new device feature to expose this error information to userspace to enable recovery.

Quoted 4 lines
include/uapi/linux/vfio.h +0 -0
index 5de618a3a5eee..83c93193b22de 100644

[ ... ]

Quoted 16 lines
include/uapi/linux/vfio.h +12 -0
@@ -1534,6 +1534,36 @@ struct vfio_device_feature_dma_buf {
  */
 #define VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2  12
 
+/**
+ * VFIO_DEVICE_FEATURE_ZPCI_ERROR feature provides PCI error information to
+ * userspace for vfio-pci devices on s390. On s390, PCI error recovery
+ * involves platform firmware and notification to operating systems is done
+ * by architecture specific mechanism. Exposing this information to
+ * userspace allows it to take appropriate actions to handle an
+ * error on the device. The ioctl returns -ENOMSG if there are no pending
+ * PCI errors.
+ */
+
+struct vfio_device_feature_zpci_err {
+	__u32 feature_flags;		/* Indicate future features */

include/uapi/linux/vfio.h struct vfio_device_feature_dma_buf { · line 1558

Quoted 10 lines
include/uapi/linux/vfio.h +10 -0
+	__u32 reserved1;
+	__u32 fh;			/* function handle */
+	__u32 fid;			/* function id */
+	__u64 faddr;                    /* failing address */
+	__u32 ett		:  4;	/* expected table type */
+	__u32 mvn		: 12;	/* MSI vector number */
+	__u32 dmaas		:  8;	/* DMA address space */
+	__u32 reserved2		:  6;
+	__u32 q			:  1;	/* event qualifier */
+	__u32 rw		:  1;	/* read/write */

[Severity: Medium]
Does this UAPI struct violate kernel UAPI design principles by using C bitfields?

The C standard does not specify the memory layout of bitfields, leaving it entirely up to the compiler and target endianness. Placing bitfields directly in a globally exposed UAPI header could mean that generic userspace tools or hypervisors compiled with different compilers or on architectures with different endianness might map these fields to the wrong bits when interpreting the struct.

Could these be replaced with a standard __u32 variable alongside explicit #define bitmasks and shifts?

Quoted 9 lines
include/uapi/linux/vfio.h +8 -0
+	__u32 reserved3;
+	__u16 reserved4;
+	__u16 pec;			/* PCI event code */
+	__u8 reserved5[28];		/* Allow for future expansion */
+};
+
+#define VFIO_DEVICE_FEATURE_ZPCI_ERROR 13
+
 /* -------- API for Type1 VFIO IOMMU -------- */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH v19 3/3] vfio/pci: Remove the pcie check for VFIO_PCI_ERR_IRQ_INDEX

In reply to Farhan Ali
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

The error signaling is configured for the vast majority of devices and it's extremely rare that it fires anyway. Removing the pcie check will allow userspace to be notified on errors for legacy PCI devices. The Internal Shared Memory (ISM) device on s390 is one such device. For PCI devices on IBM s390 error recovery involves platform firmware and notification to operating system is done by architecture specific way. So the ISM device can still be recovered when notified of an error.

Quoted 4 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#9 lore raw
In reply to Farhan Ali

Polite ping for this series :)

Thanks

Farhan

Quoted 41 lines · Farhan Ali

Hi Alex,

This patch set includes only the VFIO patches of the original series for error recovery for vfio-pci devices on s390x [1]. Breaking up the patch series into PCI and VFIO only patches to make merging easier based on our discussion [2].

Thanks
Farhan

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

ChangeLog
---------
v18 https://lore.kernel.org/all/[email protected]/
v18 -> v19
- Fix hole in struct vfio_device_feature_zpci_err.
- Remove reset_done() callback for vfio-pci driver.
- Rebase on 7.1-rc7

v17 -> v18
- Print a warn with dev_warn_ratelimited() for copy_to_user failure.
- Rebase on 7.1-rc6.
- Break patch series into VFIO patch set.

Farhan Ali (3):
s390/pci: Store PCI error information for passthrough devices vfio-pci/zdev: Add a device feature for error information vfio/pci: Remove the pcie check for VFIO_PCI_ERR_IRQ_INDEX

  arch/s390/include/asm/pci.h       |  32 +++++++
  arch/s390/pci/pci.c               |   1 +
  arch/s390/pci/pci_event.c         | 137 +++++++++++++++++++-----------
  drivers/vfio/pci/vfio_pci_core.c  |  10 +--
  drivers/vfio/pci/vfio_pci_intrs.c |   3 +-
  drivers/vfio/pci/vfio_pci_priv.h  |   9 ++
  drivers/vfio/pci/vfio_pci_zdev.c  |  61 ++++++++++++-
  include/uapi/linux/vfio.h         |  30 +++++++
  8 files changed, 221 insertions(+), 62 deletions(-)
#10 lore raw
Quoted 1 line · Farhan Ali

Polite ping for this series :)

We're more than halfway through the v7.2 merge window, so it's too late to add things for v7.2. We'll get back to this after v7.2-rc1, i.e., next week.

Quoted 42 lines · Farhan Ali
Quoted 41 lines

Hi Alex,

This patch set includes only the VFIO patches of the original series for error recovery for vfio-pci devices on s390x [1]. Breaking up the patch series into PCI and VFIO only patches to make merging easier based on our discussion [2].

Thanks
Farhan

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

ChangeLog
---------
v18 https://lore.kernel.org/all/[email protected]/
v18 -> v19
- Fix hole in struct vfio_device_feature_zpci_err.
- Remove reset_done() callback for vfio-pci driver.
- Rebase on 7.1-rc7

v17 -> v18
- Print a warn with dev_warn_ratelimited() for copy_to_user failure.
- Rebase on 7.1-rc6.
- Break patch series into VFIO patch set.

Farhan Ali (3):
s390/pci: Store PCI error information for passthrough devices vfio-pci/zdev: Add a device feature for error information vfio/pci: Remove the pcie check for VFIO_PCI_ERR_IRQ_INDEX

  arch/s390/include/asm/pci.h       |  32 +++++++
  arch/s390/pci/pci.c               |   1 +
  arch/s390/pci/pci_event.c         | 137 +++++++++++++++++++-----------
  drivers/vfio/pci/vfio_pci_core.c  |  10 +--
  drivers/vfio/pci/vfio_pci_intrs.c |   3 +-
  drivers/vfio/pci/vfio_pci_priv.h  |   9 ++
  drivers/vfio/pci/vfio_pci_zdev.c  |  61 ++++++++++++-
  include/uapi/linux/vfio.h         |  30 +++++++
  8 files changed, 221 insertions(+), 62 deletions(-)
maintainer
#5 lore raw

Re: [PATCH v19 2/3] vfio-pci/zdev: Add a device feature for error information

In reply to Farhan Ali
Quoted 180 lines · On Mon, 15 Jun 2026 11:36:15 -0700 Farhan Ali <[email protected]> wrote:

For zPCI devices, we have platform specific error information. The platform firmware provides this error information to the operating system in an architecture specific mechanism. To enable recovery from userspace for these devices, we want to expose this error information to userspace. Add a new device feature to expose this information.

---
 arch/s390/include/asm/pci.h      |  2 ++
 arch/s390/pci/pci_event.c        | 19 ++++++++++++++
 drivers/vfio/pci/vfio_pci_core.c |  2 ++
 drivers/vfio/pci/vfio_pci_priv.h |  9 +++++++
 drivers/vfio/pci/vfio_pci_zdev.c | 43 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 30 ++++++++++++++++++++++
 6 files changed, 105 insertions(+)
arch/s390/include/asm/pci.h +2 -0
index 016386f7ef4a..88a125b92bdd 100644
@@ -364,6 +364,8 @@ int zpci_clear_error_state(struct zpci_dev *zdev);
 int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
 void zpci_start_mediated_recovery(struct zpci_dev *zdev);
 void zpci_stop_mediated_recovery(struct zpci_dev *zdev);
+int zpci_get_pending_error(struct zpci_dev *zdev,
+			   struct zpci_ccdf_err *ccdf);
 
 #ifdef CONFIG_NUMA
 
arch/s390/pci/pci_event.c +19 -0
index 8934b640a095..3ad16038e005 100644
@@ -77,6 +77,25 @@ static int zpci_store_pci_error(struct pci_dev *pdev,
 	return 0;
 }
 
+int zpci_get_pending_error(struct zpci_dev *zdev,
+			   struct zpci_ccdf_err *ccdf)
+{
+	int head = 0;
+
+	guard(mutex)(&zdev->pending_errs_lock);
+
+	if (!zdev->pending_errs.count)
+		return -ENOMSG;
+
+	head = zdev->pending_errs.head % ZPCI_ERR_PENDING_MAX;
+	memcpy(ccdf, &zdev->pending_errs.err[head],
+	       sizeof(struct zpci_ccdf_err));
+	zdev->pending_errs.head++;
+	zdev->pending_errs.count--;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(zpci_get_pending_error);
+
 void zpci_start_mediated_recovery(struct zpci_dev *zdev)
 {
 	guard(mutex)(&zdev->pending_errs_lock);
drivers/vfio/pci/vfio_pci_core.c +2 -0
index 050e7542952e..27642f10fe97 100644
@@ -1569,6 +1569,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
 		return vfio_pci_core_feature_token(vdev, flags, arg, argsz);
 	case VFIO_DEVICE_FEATURE_DMA_BUF:
 		return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz);
+	case VFIO_DEVICE_FEATURE_ZPCI_ERROR:
+		return vfio_pci_zdev_feature_err(device, flags, arg, argsz);
 	default:
 		return -ENOTTY;
 	}
drivers/vfio/pci/vfio_pci_priv.h +9 -0
index fca9d0dfac90..4e7162234a2e 100644
@@ -93,6 +93,8 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 				struct vfio_info_cap *caps);
 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev);
 void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev);
+int vfio_pci_zdev_feature_err(struct vfio_device *device, u32 flags,
+			      void __user *arg, size_t argsz);
 #else
 static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 					      struct vfio_info_cap *caps)
@@ -107,6 +109,13 @@ static inline int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 
 static inline void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
 {}
+
+static inline int vfio_pci_zdev_feature_err(struct vfio_device *device,
+					    u32 flags, void __user *arg,
+					    size_t argsz)
+{
+	return -ENOTTY;
+}
 #endif
 
 static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
drivers/vfio/pci/vfio_pci_zdev.c +43 -0
index 78a28db00c6d..c0d6362cc4eb 100644
@@ -141,6 +141,49 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 	return ret;
 }
 
+int vfio_pci_zdev_feature_err(struct vfio_device *device, u32 flags,
+			      void __user *arg, size_t argsz)
+{
+	struct vfio_device_feature_zpci_err err = {};
+	struct vfio_pci_core_device *vdev;
+	struct zpci_ccdf_err ccdf = {};
+	struct zpci_dev *zdev;
+	int ret;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	zdev = to_zpci(vdev->pdev);
+	if (!zdev)
+		return -ENODEV;
+
+	ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
+				 sizeof(err));
+	if (ret != 1)
+		return ret;
+
+	ret = zpci_get_pending_error(zdev, &ccdf);
+	if (ret)
+		return ret;
+
+	err.fh = ccdf.fh;
+	err.fid = ccdf.fid;
+	err.ett = ccdf.ett;
+	err.mvn = ccdf.mvn;
+	err.dmaas = ccdf.dmaas;
+	err.q = ccdf.q;
+	err.rw = ccdf.rw;
+	err.faddr = ccdf.faddr;
+	err.pec = ccdf.pec;
+
+	if (copy_to_user(arg, &err, sizeof(err))) {
+		dev_warn_ratelimited(device->dev,
+				     "Failed to handle PCI error event for PCI function 0x%x",
+				     zdev->fid);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 {
 	struct zpci_dev *zdev = to_zpci(vdev->pdev);
include/uapi/linux/vfio.h +17 -0
index 5de618a3a5ee..83c93193b22d 100644
@@ -1534,6 +1534,36 @@ struct vfio_device_feature_dma_buf {
  */
 #define VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2  12
 
+/**
+ * VFIO_DEVICE_FEATURE_ZPCI_ERROR feature provides PCI error information to
+ * userspace for vfio-pci devices on s390. On s390, PCI error recovery
+ * involves platform firmware and notification to operating systems is done
+ * by architecture specific mechanism. Exposing this information to
+ * userspace allows it to take appropriate actions to handle an
+ * error on the device. The ioctl returns -ENOMSG if there are no pending
+ * PCI errors.
+ */
+
+struct vfio_device_feature_zpci_err {
+	__u32 feature_flags;		/* Indicate future features */
+	__u32 reserved1;
+	__u32 fh;			/* function handle */
+	__u32 fid;			/* function id */
+	__u64 faddr;                    /* failing address */
+	__u32 ett		:  4;	/* expected table type */

include/uapi/linux/vfio.h struct vfio_device_feature_dma_buf { · line 1563

Quoted 10 lines
include/uapi/linux/vfio.h +10 -0
+	__u32 mvn		: 12;	/* MSI vector number */
+	__u32 dmaas		:  8;	/* DMA address space */
+	__u32 reserved2		:  6;
+	__u32 q			:  1;	/* event qualifier */
+	__u32 rw		:  1;	/* read/write */
+	__u32 reserved3;
+	__u16 reserved4;
+	__u16 pec;			/* PCI event code */
+	__u8 reserved5[28];		/* Allow for future expansion */
+};

In v18 I probed whether this should be passed as opaque data as we're doing for FMB, which was declined because the user doesn't know the size. However, the fmb_length is provided through a device info cap, where we could extend the existing zpci info cap with a v3 that does report this fixed size. That doesn't seem like an insurmountable barrier.

In the implementation here, we have the worst combination of trade-offs, reserved fields for some vague notion of future expansion and bitfields that mimic the architecture specification but don't fit well into a uAPI as their position is compiler dependent.

This should either fully abandon the architecture layout and create a well defined, naturally aligned uAPI structure, or extend the zpci info cap to report the size and pass the error data as opaque.

As Bjorn notes, we're already into the merge window, so this is at best v7.3 material, and as we've designed the split, merging the vfio changes will be dependent on a branch provided with the PCI changes. Thanks,

Alex

author maintainer
#6 lore raw

Re: [PATCH v19 2/3] vfio-pci/zdev: Add a device feature for error information

Quoted 200 lines · Alex Williamson
Quoted 180 lines · On Mon, 15 Jun 2026 11:36:15 -0700 Farhan Ali <[email protected]> wrote:

For zPCI devices, we have platform specific error information. The platform firmware provides this error information to the operating system in an architecture specific mechanism. To enable recovery from userspace for these devices, we want to expose this error information to userspace. Add a new device feature to expose this information.

---
  arch/s390/include/asm/pci.h      |  2 ++
  arch/s390/pci/pci_event.c        | 19 ++++++++++++++
  drivers/vfio/pci/vfio_pci_core.c |  2 ++
  drivers/vfio/pci/vfio_pci_priv.h |  9 +++++++
  drivers/vfio/pci/vfio_pci_zdev.c | 43 ++++++++++++++++++++++++++++++++
  include/uapi/linux/vfio.h        | 30 ++++++++++++++++++++++
  6 files changed, 105 insertions(+)
arch/s390/include/asm/pci.h +2 -0
index 016386f7ef4a..88a125b92bdd 100644
@@ -364,6 +364,8 @@ int zpci_clear_error_state(struct zpci_dev *zdev);
  int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
  void zpci_start_mediated_recovery(struct zpci_dev *zdev);
  void zpci_stop_mediated_recovery(struct zpci_dev *zdev);
+int zpci_get_pending_error(struct zpci_dev *zdev,
+			   struct zpci_ccdf_err *ccdf);
 
  #ifdef CONFIG_NUMA
 
arch/s390/pci/pci_event.c +19 -0
index 8934b640a095..3ad16038e005 100644
@@ -77,6 +77,25 @@ static int zpci_store_pci_error(struct pci_dev *pdev,
  	return 0;
  }
 
+int zpci_get_pending_error(struct zpci_dev *zdev,
+			   struct zpci_ccdf_err *ccdf)
+{
+	int head = 0;
+
+	guard(mutex)(&zdev->pending_errs_lock);
+
+	if (!zdev->pending_errs.count)
+		return -ENOMSG;
+
+	head = zdev->pending_errs.head % ZPCI_ERR_PENDING_MAX;
+	memcpy(ccdf, &zdev->pending_errs.err[head],
+	       sizeof(struct zpci_ccdf_err));
+	zdev->pending_errs.head++;
+	zdev->pending_errs.count--;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(zpci_get_pending_error);
+
  void zpci_start_mediated_recovery(struct zpci_dev *zdev)
  {
  	guard(mutex)(&zdev->pending_errs_lock);
drivers/vfio/pci/vfio_pci_core.c +2 -0
index 050e7542952e..27642f10fe97 100644
@@ -1569,6 +1569,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
  		return vfio_pci_core_feature_token(vdev, flags, arg, argsz);
  	case VFIO_DEVICE_FEATURE_DMA_BUF:
  		return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz);
+	case VFIO_DEVICE_FEATURE_ZPCI_ERROR:
+		return vfio_pci_zdev_feature_err(device, flags, arg, argsz);
  	default:
  		return -ENOTTY;
  	}
drivers/vfio/pci/vfio_pci_priv.h +9 -0
index fca9d0dfac90..4e7162234a2e 100644
@@ -93,6 +93,8 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
  				struct vfio_info_cap *caps);
  int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev);
  void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev);
+int vfio_pci_zdev_feature_err(struct vfio_device *device, u32 flags,
+			      void __user *arg, size_t argsz);
  #else
  static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
  					      struct vfio_info_cap *caps)
@@ -107,6 +109,13 @@ static inline int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 
  static inline void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
  {}
+
+static inline int vfio_pci_zdev_feature_err(struct vfio_device *device,
+					    u32 flags, void __user *arg,
+					    size_t argsz)
+{
+	return -ENOTTY;
+}
  #endif
 
  static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
drivers/vfio/pci/vfio_pci_zdev.c +43 -0
index 78a28db00c6d..c0d6362cc4eb 100644
@@ -141,6 +141,49 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
  	return ret;
  }
 
+int vfio_pci_zdev_feature_err(struct vfio_device *device, u32 flags,
+			      void __user *arg, size_t argsz)
+{
+	struct vfio_device_feature_zpci_err err = {};
+	struct vfio_pci_core_device *vdev;
+	struct zpci_ccdf_err ccdf = {};
+	struct zpci_dev *zdev;
+	int ret;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	zdev = to_zpci(vdev->pdev);
+	if (!zdev)
+		return -ENODEV;
+
+	ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
+				 sizeof(err));
+	if (ret != 1)
+		return ret;
+
+	ret = zpci_get_pending_error(zdev, &ccdf);
+	if (ret)
+		return ret;
+
+	err.fh = ccdf.fh;
+	err.fid = ccdf.fid;
+	err.ett = ccdf.ett;
+	err.mvn = ccdf.mvn;
+	err.dmaas = ccdf.dmaas;
+	err.q = ccdf.q;
+	err.rw = ccdf.rw;
+	err.faddr = ccdf.faddr;
+	err.pec = ccdf.pec;
+
+	if (copy_to_user(arg, &err, sizeof(err))) {
+		dev_warn_ratelimited(device->dev,
+				     "Failed to handle PCI error event for PCI function 0x%x",
+				     zdev->fid);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
  int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
  {
  	struct zpci_dev *zdev = to_zpci(vdev->pdev);
include/uapi/linux/vfio.h +17 -0
index 5de618a3a5ee..83c93193b22d 100644
@@ -1534,6 +1534,36 @@ struct vfio_device_feature_dma_buf {
   */
  #define VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2  12
 
+/**
+ * VFIO_DEVICE_FEATURE_ZPCI_ERROR feature provides PCI error information to
+ * userspace for vfio-pci devices on s390. On s390, PCI error recovery
+ * involves platform firmware and notification to operating systems is done
+ * by architecture specific mechanism. Exposing this information to
+ * userspace allows it to take appropriate actions to handle an
+ * error on the device. The ioctl returns -ENOMSG if there are no pending
+ * PCI errors.
+ */
+
+struct vfio_device_feature_zpci_err {
+	__u32 feature_flags;		/* Indicate future features */
+	__u32 reserved1;
+	__u32 fh;			/* function handle */
+	__u32 fid;			/* function id */
+	__u64 faddr;                    /* failing address */
+	__u32 ett		:  4;	/* expected table type */

include/uapi/linux/vfio.h struct vfio_device_feature_dma_buf { · line 1563

Quoted 10 lines
include/uapi/linux/vfio.h +10 -0
+	__u32 mvn		: 12;	/* MSI vector number */
+	__u32 dmaas		:  8;	/* DMA address space */
+	__u32 reserved2		:  6;
+	__u32 q			:  1;	/* event qualifier */
+	__u32 rw		:  1;	/* read/write */
+	__u32 reserved3;
+	__u16 reserved4;
+	__u16 pec;			/* PCI event code */
+	__u8 reserved5[28];		/* Allow for future expansion */
+};

In v18 I probed whether this should be passed as opaque data as we're doing for FMB, which was declined because the user doesn't know the size. However, the fmb_length is provided through a device info cap, where we could extend the existing zpci info cap with a v3 that does report this fixed size. That doesn't seem like an insurmountable barrier.

Quoted 8 lines

In the implementation here, we have the worst combination of trade-offs, reserved fields for some vague notion of future expansion and bitfields that mimic the architecture specification but don't fit well into a uAPI as their position is compiler dependent.

This should either fully abandon the architecture layout and create a well defined, naturally aligned uAPI structure, or extend the zpci info cap to report the size and pass the error data as opaque.

I think it should be doable by extending the zpci info cap and providing a size for userspace to provide a buffer. I think making the error data opaque makes sense, and it provides a mechanism (along with size) to possibly extend the data returned to user. I will look into implementing that in the next version.

Thanks

Farhan

Quoted 6 lines

As Bjorn notes, we're already into the merge window, so this is at best v7.3 material, and as we've designed the split, merging the vfio changes will be dependent on a branch provided with the PCI changes. Thanks,

Alex