Skip to contentSkip to conversation

vfio-pci/zdev: Improved zPCI Function Measurement Support

PATCH In review Omar Elghoul 4 participants 15 messages Nov 11 – Nov 19

Subsystems (6)
Stable branch KVM s390 VFIO driver +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v13 cover vfio-pci/zdev: Improved zPCI Function Measurement Support

Hi,

This patch series improves support for function measurement for zPCI passthrough devices on s390.

Changelog
=========
v12 -> v13:
* Patch 3/5:
- Track FMB enablement in zpci_update_fh() to account for the case where the PCI function was disabled from under the kernel

v11 -> v12:
* Patch 2/5:
- Replace the statistics_on bool with a bitfield

* Patch 3/5:
- Add the fmb_enabled bitfield to struct zpci_dev to track the actual FMB enablement
- Replace the kmem_cache allocator with kzalloc/kfree after learning it guarantees alignment for power-of-two sizes

* Patch 5/5:
- Use fmb_enabled as the FMB liveness check

v10 -> v11:
* Patches 2/5 and 3/5:
- Swap their ordering within the series
- Set/unset statistics_on after an enable/disable succeeds instead of before

* Patch 5/5:
- On FMB re-enable, use an intermediate request-off step instead of directly touching the device state

v9 -> v10:
* Patches 2/5 and 3/5:
- Split the old 2/4 into two patches; the new patch 2/5 reuses one FMB buffer per device, and 3/5 improves FMB state management
- Rework FMB management functions to decouple requesting enablement or disablement from managing the user's intended state
- Preserve measurement state in pcibios_enable_device()

v8 -> v9:
* Patch 2/4:
- Remove the intermediate disable step from zpci_fmb_reenable_device() for more uniform semantics
- Rename fmb_enabled to fmb_requested with the intent of tracking the desired status of FMB enablement
- Make zpci_fmb_enable_device() delegate to zpci_fmb_reenable_device()

* Patch 4/4:
- Set fmb_requested when the user enables measurement

v7 -> v8:
* Patch 2/4:
- Replace the fmb_enabled bitfield in struct zpci_dev with a dedicated bool to avoid future tearing bugs
- Rewrite commit message for a more natural flow

* Patch 4/4:
- Replace mutex_lock/unlock() in vfio_pci_zdev_feature_fmb_read() with a scoped_guard()

v6 -> v7:
* Patch 2/4:
- Don't re-enable FMB if it wasn't already enabled

v5 -> v6:
* Patch 2/4:
- Rework the FMB re-enablement code to reuse the same buffer again
- Make the FMB buffer persistent once allocated for as long as the device's lifetime to accommodate an architectural quirk
* Patch 4/4:
- Update the liveness check to use the new FMB enabled bool

v4 -> v5:
* Typo in the cover letter
* Swap the ordering of patches 3/4 and 4/4 to ease merging (i.e., to ensure the three s390 patches are ordered before the VFIO patch)
* Patch 2/4:
- Drop the refactor of zpci_fmb_enable_device() and the separation of zpci_fmb_clear_iommu_ctrs() and zpci_fmb_do_enable()
- Allocate a new buffer in zpci_fmb_reenable_device() rather than reusing the same buffer to avoid firmware edge cases

* Patch 3/4 (previously 4/4):
- Avoid reading from userspace while holding kzdev_lock unnecessarily

* Patch 4/4 (previously 3/4):
- Drop allowing usercopy of the FMB when initializing the kmem_cache
- Avoid copying to userspace while holding fmb_lock unnecessarily
- Restore the FMB bounce buffer to achieve this one
- Clarify uAPI documentation and ensure it accurately describes the behavior of the VFIO features

v3 -> v4:
* Patch 2/4:
- Replace mutex_lock/unlock in zpci_reenable_device() with a guard

* Patch 3/4:
- Allow usercopy of the FMB when initializing its kmem_cache
- Move the guard in vfio_pci_zdev_feature_fmb_enable() lower to only protect the FMB
- Ensure vfio_pci_zdev_feature_fmb_enable() fails on double-enable for consistency with the documentation
- Eliminate the bounce buffer in vfio_pci_zdev_feature_fmb_read()
- Replace the void pointer with __aligned_u64 in the FMB read uAPI structure

v2 -> v3:
* Patch 1/4 (new patch):
- Fix race conditions in pcibios_enable/disable_device() with regard to the FMB enable/disable
- Assert that fmb_lock is held within zpci_fmb_enable_device() and zpci_fmb_disable_device()

* Patch 2/4 (previously 1/3):
- Move the FMB enable logic into a static function zpci_fmb_do_enable() to reduce code duplication between zpci_fmb_enable_device() and zpci_fmb_reenable_device()
- Reword commit message to use the imperative voice more consistently

* Patch 3/4 (previously 2/3):
- Split the previous VFIO feature into a SET-only and a GET-only feature for enabling/disabling and reading the FMB respectively
- Remove FMB definitions from the VFIO uAPI and instead treat it as an opaque structure

* Patch 4/4 (previously 3/3):
- Clarify goto label name to reduce misunderstandings

v1 -> v2:
* Patch 1/3:
- Address a possible race condition in zpci_reenable_device() caused by calling zpci_fmb_reenable_device() without holding fmb_lock
- Assert that fmb_lock is held within zpci_fmb_reenable_device()

* Patch 3/3:
- Address a possible race condition in pci_perf_seq_write() caused by

    consuming zdev->kzdev without holding kzdev_lock

Motivation
==========
The firmware on s390x machines allows for tracking a variety of statistics relating to zPCI devices in a function measurement block (FMB). However, the kernel currently lacks a structured mechanism of sharing this information with userspace, beyond /sys/kernel/debug/pci/ID/statistics. This can lead to shortcomings when running a guest on KVM with PCI passthrough devices, as QEMU is unable to provide an accurate FMB snapshot to the guest.

Proposal
========
We propose adding a new VFIO device feature to zPCI passthrough devices, allowing userspace programs to read the latest FMB snapshot as it is written by the firmware. We ensure that function measurement enablement is preserved across device resets on the host. Furthermore, we guard against host tampering with the FMB via sysfs when the zPCI device is in passthrough to protect the VM's state.

I'd appreciate some feedback on these patches.

Thanks in advance.

Omar Elghoul (5):
s390/pci: Hold fmb_lock when enabling or disabling PCI devices s390/pci: Improve function measurement state management s390/pci: Improve FMB memory management
s390/pci: Fence FMB enable/disable via debugfs for passthrough devices vfio-pci/zdev: Add VFIO FMB device features

 arch/s390/include/asm/pci.h      |   8 +-
 arch/s390/pci/pci.c              | 123 +++++++++++++++++++++++--------
 arch/s390/pci/pci_debug.c        |  15 +++-
 drivers/vfio/pci/vfio_pci_core.c |   4 +
 drivers/vfio/pci/vfio_pci_priv.h |  18 +++++
 drivers/vfio/pci/vfio_pci_zdev.c |  64 ++++++++++++++++
 include/uapi/linux/vfio.h        |  30 ++++++++
 7 files changed, 226 insertions(+), 36 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH v13 1/5 s390/pci: Hold fmb_lock when enabling or disabling PCI devices

Ensure that fmb_lock is held by pcibios_enable_device() and pcibios_disable_device() when calling zpci_fmb_enable_device() or zpci_fmb_disable_device(), respectively. Additionally, assert that the fmb_lock is held within the latter two functions to prevent future race conditions regarding new callers.

Fixes af0a8a8453f7 ("s390/pci: implement pcibios_add_device")
Fixes 944239c59e93 ("s390/pci: implement pcibios_release_device")
---
 arch/s390/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/s390/pci/pci.c +8 -0
index 1192fb167d3f..ee1b1759655a 100644
@@ -173,6 +173,8 @@ int zpci_fmb_enable_device(struct zpci_dev *zdev)
 	unsigned long flags;
 	u8 cc, status;
 
+	lockdep_assert_held(&zdev->fmb_lock);
+
 	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
 		return -EINVAL;
 
@@ -211,6 +213,8 @@ int zpci_fmb_disable_device(struct zpci_dev *zdev)
 	struct zpci_fib fib = {0};
 	u8 cc, status;
 
+	lockdep_assert_held(&zdev->fmb_lock);
+
 	if (!zdev->fmb)
 		return -EINVAL;
 
@@ -639,7 +643,9 @@ int pcibios_enable_device(struct pci_dev *pdev, int mask)
 	struct zpci_dev *zdev = to_zpci(pdev);
 
 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
+	mutex_lock(&zdev->fmb_lock);
 	zpci_fmb_enable_device(zdev);
+	mutex_unlock(&zdev->fmb_lock);
 
 	return pci_enable_resources(pdev, mask);
 }
@@ -648,7 +654,9 @@ void pcibios_disable_device(struct pci_dev *pdev)
 {
 	struct zpci_dev *zdev = to_zpci(pdev);
 
+	mutex_lock(&zdev->fmb_lock);
 	zpci_fmb_disable_device(zdev);
+	mutex_unlock(&zdev->fmb_lock);
 	zpci_debug_exit_device(zdev);
 }
 
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v13 1/5] s390/pci: Hold fmb_lock when enabling or disabling PCI devices

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

Ensure that fmb_lock is held by pcibios_enable_device() and pcibios_disable_device() when calling zpci_fmb_enable_device() or zpci_fmb_disable_device(), respectively. Additionally, assert that the fmb_lock is held within the latter two functions to prevent future race conditions regarding new callers.

Fixes af0a8a8453f7 ("s390/pci: implement pcibios_add_device")
Fixes 944239c59e93 ("s390/pci: implement pcibios_release_device")

Sashiko has reviewed this patch and found no issues.

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

PATCH v13 2/5 s390/pci: Improve function measurement state management

In reply to Omar Elghoul

When a zPCI function is disabled and re-enabled, the firmware implicitly disables function measurement while the kernel still believes it is enabled, leading to a disconnect between the user-visible state and the actual device state.

Add the statistics_on field to struct zpci_dev to track the desired FMB state. Introduce the functions zpci_fmb_request_on()/_off(), which defer to zpci_fmb_enable()/_disable() to perform the actual firmware change. This decouples the enablement or disablement step from managing the user's requested state.

Restore the state in pcibios_enable_device(), pcibios_disable_device(), and zpci_reenable_device(). New devices start with measurement on by default, unchanged from before.

---
 arch/s390/include/asm/pci.h |  7 +++--
 arch/s390/pci/pci.c         | 54 +++++++++++++++++++++++++++++++++----
 arch/s390/pci/pci_debug.c   |  4 +--
 3 files changed, 56 insertions(+), 9 deletions(-)
arch/s390/include/asm/pci.h +5 -2
index 88a125b92bdd..dd58c48ac2ed 100644
@@ -208,6 +208,7 @@ struct zpci_dev {
 	struct zpci_fmb *fmb;
 	u16		fmb_update;	/* update interval */
 	u16		fmb_length;
+	u8		statistics_on	: 1; /* desired FMB enablement */
 
 	u8		version;
 	enum pci_bus_speed max_bus_speed;
@@ -349,8 +350,10 @@ int zpci_create_parent_msi_domain(struct zpci_bus *zbus);
 void zpci_remove_parent_msi_domain(struct zpci_bus *zbus);
 
 /* FMB */
-int zpci_fmb_enable_device(struct zpci_dev *);
-int zpci_fmb_disable_device(struct zpci_dev *);
+int zpci_fmb_request_on(struct zpci_dev *zdev);
+int zpci_fmb_request_off(struct zpci_dev *zdev);
+int zpci_fmb_enable(struct zpci_dev *zdev);
+int zpci_fmb_disable(struct zpci_dev *zdev);
 
 /* Debug */
 int zpci_debug_init(void);
arch/s390/pci/pci.c +49 -5

92 lines not on this page · raw message

arch/s390/pci/pci_debug.c +2 -2
index c7ed7bf254b5..06e2e8bb2f82 100644
@@ -156,10 +156,10 @@ static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
 	mutex_lock(&zdev->fmb_lock);
 	switch (val) {
 	case 0:
-		rc = zpci_fmb_disable_device(zdev);
+		rc = zpci_fmb_request_off(zdev);
 		break;
 	case 1:
-		rc = zpci_fmb_enable_device(zdev);
+		rc = zpci_fmb_request_on(zdev);
 		break;
 	}
 	mutex_unlock(&zdev->fmb_lock);
Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH v13 2/5] s390/pci: Improve function measurement state management

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] s390/pci: zpci_reenable_device() enables FMB on disabled PCI devices --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] s390/pci: zpci_reenable_device() enables FMB on disabled PCI devices
--

commit fa84ea4339ffa82b899be0638304928f5c7daa03
Author: Omar Elghoul <[email protected]>

s390/pci: Improve function measurement state management

This commit tracks the desired function measurement state in zdev->statistics_on and restores it when enabling, disabling, or re-enabling a zPCI device.

Quoted 4 lines
arch/s390/pci/pci.c +0 -0
index c055a9ad09724..f49736386eb7c 100644

[ ... ]

Quoted 6 lines
arch/s390/pci/pci.c +1 -1
@@ -737,8 +769,14 @@ int zpci_reenable_device(struct zpci_dev *zdev)
 	}
 
 	rc = zpci_iommu_register_ioat(zdev, &status);
-	if (rc)
+	if (rc) {

arch/s390/pci/pci.c int zpci_reenable_device(struct zpci_dev *zdev) · line 782

Quoted 10 lines
arch/s390/pci/pci.c +6 -0
 		zpci_disable_device(zdev);
+		return rc;
+	}
+
+	guard(mutex)(&zdev->fmb_lock);
+	if (zdev->statistics_on)
+		zpci_fmb_enable(zdev);
 
 	return rc;
 }

[Severity: Medium]
Does this enable FMB measurement in firmware when the PCI device is disabled?

zdev->statistics_on is initialized to 1 in zpci_create_device() and remains 1 after pcibios_disable_device() disables FMB. Callers such as _do_recover() and vfio_pci_core_disable() -> ... -> zpci_hot_reset_device() disable or remove the struct pci_dev before calling zpci_reenable_device(zdev):

arch/s390/pci/pci_sysfs.c:_do_recover() {
	pci_stop_and_remove_bus_device(pdev);
	if (zdev_enabled(zdev)) {
		ret = zpci_disable_device(zdev);
		...
	}

	ret = zpci_reenable_device(zdev);
}

Because zdev->statistics_on is still 1, zpci_reenable_device() allocates zdev->fmb and enables FMB DMA in firmware even though pcibios_disable_device() already ran. If no driver re-enables the PCI device, firmware FMB measurement stays active on a disabled device, and at this commit zpci_release_device() leaks zdev->fmb if the device is later deconfigured (though commit "s390/pci: Improve FMB memory management" later in the series frees zdev->fmb on release). When a driver does later enable the device, pcibios_enable_device() -> zpci_fmb_enable() sees zdev->fmb already allocated and returns -EINVAL instead of zeroing zdev->fmb and resetting the software IOMMU counters.

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

Re: [PATCH v13 2/5] s390/pci: Improve function measurement state management

Quoted 59 lines · On Wed, 2026-11-11 at 17:20 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] s390/pci: zpci_reenable_device() enables FMB on disabled PCI devices
--

commit fa84ea4339ffa82b899be0638304928f5c7daa03
Author: Omar Elghoul <[email protected]>

s390/pci: Improve function measurement state management

This commit tracks the desired function measurement state in zdev->statistics_on and restores it when enabling, disabling, or re-enabling a zPCI device.

Quoted 4 lines
arch/s390/pci/pci.c +0 -0
index c055a9ad09724..f49736386eb7c 100644

[ ... ]

Quoted 6 lines
arch/s390/pci/pci.c +1 -1
@@ -737,8 +769,14 @@ int zpci_reenable_device(struct zpci_dev *zdev)
 	}
 
 	rc = zpci_iommu_register_ioat(zdev, &status);
-	if (rc)
+	if (rc) {

arch/s390/pci/pci.c int zpci_reenable_device(struct zpci_dev *zdev) · line 782

Quoted 10 lines
arch/s390/pci/pci.c +6 -0
 		zpci_disable_device(zdev);
+		return rc;
+	}
+
+	guard(mutex)(&zdev->fmb_lock);
+	if (zdev->statistics_on)
+		zpci_fmb_enable(zdev);
 
 	return rc;
 }

[Severity: Medium]
Does this enable FMB measurement in firmware when the PCI device is disabled?

zdev->statistics_on is initialized to 1 in zpci_create_device() and remains 1 after pcibios_disable_device() disables FMB. Callers such as _do_recover() and vfio_pci_core_disable() -> ... -> zpci_hot_reset_device() disable or remove the struct pci_dev before calling zpci_reenable_device(zdev):

arch/s390/pci/pci_sysfs.c:_do_recover() {
	pci_stop_and_remove_bus_device(pdev);
	if (zdev_enabled(zdev)) {
		ret = zpci_disable_device(zdev);
		...
	}

	ret = zpci_reenable_device(zdev);
}
Quoted 10 lines

Because zdev->statistics_on is still 1, zpci_reenable_device() allocates zdev->fmb and enables FMB DMA in firmware even though pcibios_disable_device() already ran. If no driver re-enables the PCI device, firmware FMB measurement stays active on a disabled device, and at this commit zpci_release_device() leaks zdev->fmb if the device is later deconfigured (though commit "s390/pci: Improve FMB memory management" later in the series frees zdev->fmb on release). When a driver does later enable the device, pcibios_enable_device() -> zpci_fmb_enable() sees zdev->fmb already allocated and returns -EINVAL instead of zeroing zdev->fmb and resetting the software IOMMU counters.

The enablement that is relevant for firmware is the zpci_enable_device() that occurred earlier in zpci_reenable_device() so firmware is ready for us to enable the FMB measurements and the sequencing is correct here.

Thanks,
Niklas

maintainer Reviewed-by on v13 2/5
#6 lore raw

Re: [PATCH v13 2/5] s390/pci: Improve function measurement state management

In reply to Omar Elghoul
Quoted 15 lines · Omar Elghoul

When a zPCI function is disabled and re-enabled, the firmware implicitly disables function measurement while the kernel still believes it is enabled, leading to a disconnect between the user-visible state and the actual device state.

Add the statistics_on field to struct zpci_dev to track the desired FMB state. Introduce the functions zpci_fmb_request_on()/_off(), which defer to zpci_fmb_enable()/_disable() to perform the actual firmware change. This decouples the enablement or disablement step from managing the user's requested state.

Restore the state in pcibios_enable_device(), pcibios_disable_device(), and zpci_reenable_device(). New devices start with measurement on by default, unchanged from before.

Quoted 8 lines
---
 arch/s390/include/asm/pci.h |  7 +++--
 arch/s390/pci/pci.c         | 54 +++++++++++++++++++++++++++++++++----
 arch/s390/pci/pci_debug.c   |  4 +--
 3 files changed, 56 insertions(+), 9 deletions(-)

Looks good to me. Thank you for the great work and endurance of iterating on this as many times as it took.

#7 lore raw

PATCH v13 3/5 s390/pci: Improve FMB memory management

In reply to Omar Elghoul

Refactor the FMB memory management code to use kzalloc() instead of a dedicated kmem_cache. kzalloc() already guarantees adequate alignment for power-of-two sizes, rendering the kmem_cache approach unnecessary. This simplifies both the initialization and cleanup procedures.

Allocate the FMB once upon first enable and keep it for the lifetime of the zdev instead of freeing it on every disable. Defer freeing the buffer until device teardown in zpci_release_device(). Add the field fmb_enabled to struct zpci_dev as the new FMB liveness bool, since a non-null FMB pointer no longer implies FMB enablement in firmware.

---
 arch/s390/include/asm/pci.h |  1 +
 arch/s390/pci/pci.c         | 61 +++++++++++++++++++++----------------
 arch/s390/pci/pci_debug.c   |  2 +-
 3 files changed, 37 insertions(+), 27 deletions(-)
arch/s390/include/asm/pci.h +1 -0
index dd58c48ac2ed..6d3c1b8ac75b 100644
@@ -209,6 +209,7 @@ struct zpci_dev {
 	u16		fmb_update;	/* update interval */
 	u16		fmb_length;
 	u8		statistics_on	: 1; /* desired FMB enablement */
+	u8		fmb_enabled	: 1; /* actual FMB enablement */
 
 	u8		version;
 	enum pci_bus_speed max_bus_speed;
arch/s390/pci/pci.c +35 -26

129 lines not on this page · raw message

arch/s390/pci/pci_debug.c +1 -1
index 06e2e8bb2f82..454121bc274c 100644
@@ -97,7 +97,7 @@ static int pci_perf_show(struct seq_file *m, void *v)
 		return 0;
 
 	mutex_lock(&zdev->fmb_lock);
-	if (!zdev->fmb) {
+	if (!zdev->fmb_enabled) {
 		mutex_unlock(&zdev->fmb_lock);
 		seq_puts(m, "FMB statistics disabled\n");
 		return 0;
Signature · 1 line
2.56.0
#8 lore raw

Re: [PATCH v13 3/5] s390/pci: Improve FMB memory management

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

Refactor the FMB memory management code to use kzalloc() instead of a dedicated kmem_cache. kzalloc() already guarantees adequate alignment for power-of-two sizes, rendering the kmem_cache approach unnecessary. This simplifies both the initialization and cleanup procedures.

Quoted 7 lines

Allocate the FMB once upon first enable and keep it for the lifetime of the zdev instead of freeing it on every disable. Defer freeing the buffer until device teardown in zpci_release_device(). Add the field fmb_enabled to struct zpci_dev as the new FMB liveness bool, since a non-null FMB pointer no longer implies FMB enablement in firmware.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v13 3/5
#9 lore raw

Re: [PATCH v13 3/5] s390/pci: Improve FMB memory management

In reply to Omar Elghoul
Quoted 11 lines · Omar Elghoul

Refactor the FMB memory management code to use kzalloc() instead of a dedicated kmem_cache. kzalloc() already guarantees adequate alignment for power-of-two sizes, rendering the kmem_cache approach unnecessary. This simplifies both the initialization and cleanup procedures.

Allocate the FMB once upon first enable and keep it for the lifetime of the zdev instead of freeing it on every disable. Defer freeing the buffer until device teardown in zpci_release_device(). Add the field fmb_enabled to struct zpci_dev as the new FMB liveness bool, since a non-null FMB pointer no longer implies FMB enablement in firmware.

Quoted 6 lines
---
 arch/s390/include/asm/pci.h |  1 +
 arch/s390/pci/pci.c         | 61 +++++++++++++++++++++----------------
 arch/s390/pci/pci_debug.c   |  2 +-
 3 files changed, 37 insertions(+), 27 deletions(-)

And this one too looks good now. In the beginning I had really guessed that this would have been a series with few hurdles but the devil really is in the detail. Thanks for the great work.

Thanks,
Niklas

#10 lore raw

PATCH v13 4/5 s390/pci: Fence FMB enable/disable via debugfs for passthrough devices

In reply to Omar Elghoul

Introduce a fence over enabling or disabling FMB via debugfs when the zPCI device is associated with a KVM. This will prevent processes on the host from tampering with the FMB while the guest is still using it, which may cause partial counter resets and inconsistent reads which have no parallel in the architecture.

For VFIO devices that are not associated with a KVM (i.e., for userspace drivers other than QEMU), this fence does not take effect.

---
 arch/s390/pci/pci_debug.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/s390/pci/pci_debug.c +9 -0
index 454121bc274c..02da077b5170 100644
@@ -153,6 +153,12 @@ static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
 	if (rc)
 		return rc;
 
+	mutex_lock(&zdev->kzdev_lock);
+	if (zdev->kzdev) {
+		rc = -EPERM;
+		goto out_unlock_kzdev;
+	}
+
 	mutex_lock(&zdev->fmb_lock);
 	switch (val) {
 	case 0:
@@ -163,6 +169,9 @@ static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
 		break;
 	}
 	mutex_unlock(&zdev->fmb_lock);
+
+out_unlock_kzdev:
+	mutex_unlock(&zdev->kzdev_lock);
 	return rc ? rc : count;
 }
 
Signature · 1 line
2.56.0
#11 lore raw

Re: [PATCH v13 4/5] s390/pci: Fence FMB enable/disable via debugfs for passthrough devices

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

Introduce a fence over enabling or disabling FMB via debugfs when the zPCI device is associated with a KVM. This will prevent processes on the host from tampering with the FMB while the guest is still using it, which may cause partial counter resets and inconsistent reads which have no parallel in the architecture.

For VFIO devices that are not associated with a KVM (i.e., for userspace drivers other than QEMU), this fence does not take effect.

Sashiko has reviewed this patch and found no issues.

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

PATCH v13 5/5 vfio-pci/zdev: Add VFIO FMB device features

In reply to Omar Elghoul

Introduce new VFIO features for zPCI devices to provide FMB passthrough to userspace.

Allow the user to enable or disable the FMB using the SET-only feature VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE. Likewise allow the user to read the latest FMB using the GET-only feature VFIO_DEVICE_FEATURE_ZPCI_FMB_READ in the case when the FMB is enabled.

---
 drivers/vfio/pci/vfio_pci_core.c |  4 ++
 drivers/vfio/pci/vfio_pci_priv.h | 18 +++++++++
 drivers/vfio/pci/vfio_pci_zdev.c | 64 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 30 +++++++++++++++
 4 files changed, 116 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +4 -0
index fcbb2da860e2..f2d5b6857925 100644
@@ -1614,6 +1614,10 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
 		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);
+	case VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE:
+		return vfio_pci_zdev_feature_fmb_enable(vdev, flags, arg, argsz);
+	case VFIO_DEVICE_FEATURE_ZPCI_FMB_READ:
+		return vfio_pci_zdev_feature_fmb_read(vdev, flags, arg, argsz);
 	default:
 		return -ENOTTY;
 	}
drivers/vfio/pci/vfio_pci_priv.h +18 -0
index 4e7162234a2e..e04d7e9d0c30 100644
@@ -95,6 +95,10 @@ 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);
+int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev, u32 flags,
+				     void __user *arg, size_t argsz);
+int vfio_pci_zdev_feature_fmb_read(struct vfio_pci_core_device *vdev, 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)
@@ -116,6 +120,20 @@ static inline int vfio_pci_zdev_feature_err(struct vfio_device *device,
 {
 	return -ENOTTY;
 }
+
+static inline int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev,
+						   u32 flags, void __user *arg,
+						   size_t argsz)
+{
+	return -ENOTTY;
+}
+
+static inline int vfio_pci_zdev_feature_fmb_read(struct vfio_pci_core_device *vdev,
+						 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 +64 -0

67 lines not on this page · raw message

include/uapi/linux/vfio.h +30 -0
index e41437fa17ad..7e9adb61c5e1 100644
@@ -1555,6 +1555,36 @@ struct vfio_device_feature_zpci_err {
 
 #define VFIO_DEVICE_FEATURE_ZPCI_ERROR 13
 
+/**
+ * Upon VFIO_DEVICE_FEATURE_SET, enable or disable FMB for the VFIO zPCI device.
+ *
+ * enabled is treated as a bool, so any non-zero value evaluates to true. Nested
+ * enabling disables and re-enables the FMB, while nested disabling fails with
+ * EINVAL.
+ *
+ * Returns: 0 on success, -1 and errno set appropriately on error.
+ */
+#define VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE 14
+
+struct vfio_device_feature_zpci_fmb_enable {
+	__u8 enabled;
+};
+
+/**
+ * Upon VFIO_DEVICE_FEATURE_GET, provide FMB passthrough for VFIO zPCI devices.
+ *
+ * The user-provided buffer must be at least fmb_length large, where fmb_length
+ * is reported in VFIO_DEVICE_INFO_CAP_ZPCI_BASE. If measurement is not enabled,
+ * fail with ENOMSG.
+ *
+ * Returns: 0 on success, -1 and errno set appropriately on error.
+ */
+#define VFIO_DEVICE_FEATURE_ZPCI_FMB_READ 15
+
+struct vfio_device_feature_zpci_fmb_read {
+	__aligned_u64 data;
+};
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
Signature · 1 line
2.56.0
#13 lore raw

Re: [PATCH v13 5/5] vfio-pci/zdev: Add VFIO FMB device features

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Introduce new VFIO features for zPCI devices to provide FMB passthrough to userspace.

Allow the user to enable or disable the FMB using the SET-only feature VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE. Likewise allow the user to read the latest FMB using the GET-only feature VFIO_DEVICE_FEATURE_ZPCI_FMB_READ in the case when the FMB is enabled.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#14 lore raw
In reply to Omar Elghoul
Quoted 2 lines · Omar Elghoul

This patch series improves support for function measurement for zPCI passthrough devices on s390.

...

Quoted 30 lines

Motivation
==========
The firmware on s390x machines allows for tracking a variety of statistics relating to zPCI devices in a function measurement block (FMB). However, the kernel currently lacks a structured mechanism of sharing this information with userspace, beyond /sys/kernel/debug/pci/ID/statistics. This can lead to shortcomings when running a guest on KVM with PCI passthrough devices, as QEMU is unable to provide an accurate FMB snapshot to the guest.

Proposal
========
We propose adding a new VFIO device feature to zPCI passthrough devices, allowing userspace programs to read the latest FMB snapshot as it is written by the firmware. We ensure that function measurement enablement is preserved across device resets on the host. Furthermore, we guard against host tampering with the FMB via sysfs when the zPCI device is in passthrough to protect the VM's state.

I'd appreciate some feedback on these patches.

Thanks in advance.

Omar Elghoul (5):
s390/pci: Hold fmb_lock when enabling or disabling PCI devices s390/pci: Improve function measurement state management s390/pci: Improve FMB memory management
s390/pci: Fence FMB enable/disable via debugfs for passthrough devices vfio-pci/zdev: Add VFIO FMB device features

Quoted 8 lines
 arch/s390/include/asm/pci.h      |   8 +-
 arch/s390/pci/pci.c              | 123 +++++++++++++++++++++++--------
 arch/s390/pci/pci_debug.c        |  15 +++-
 drivers/vfio/pci/vfio_pci_core.c |   4 +
 drivers/vfio/pci/vfio_pci_priv.h |  18 +++++
 drivers/vfio/pci/vfio_pci_zdev.c |  64 ++++++++++++++++
 include/uapi/linux/vfio.h        |  30 ++++++++
 7 files changed, 226 insertions(+), 36 deletions(-)

Applied and put on a branch, as agreed:

git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux.git zpci-meas-enh

Thanks!