Skip to contentSkip to conversation

vfio-pci/zdev: Improved zPCI Function Measurement Support

PATCH Superseded Omar Elghoul 3 participants 15 messages Sep 16 – Sep 17

Subsystems (6)
Stable branch KVM s390 VFIO driver +2 more subsystems

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v6 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
=========
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 (4):
s390/pci: Hold fmb_lock when enabling or disabling PCI devices s390/pci: Reuse FMB buffer and preserve state in device re-enablement 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      |  2 +
 arch/s390/pci/pci.c              | 83 +++++++++++++++++++++++++-------
 arch/s390/pci/pci_debug.c        | 11 ++++-
 drivers/vfio/pci/vfio_pci_core.c |  4 ++
 drivers/vfio/pci/vfio_pci_priv.h | 18 +++++++
 drivers/vfio/pci/vfio_pci_zdev.c | 60 +++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 29 +++++++++++
 7 files changed, 189 insertions(+), 18 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v6 1/4 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 d64c544b32b0..c055a9ad0972 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.55.0
#2 lore raw

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

Sashiko has reviewed this patch and found no issues. It looks great!
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. It looks great!

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

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

In reply to Omar Elghoul
Quoted 11 lines · Omar Elghoul

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")
#4 lore raw

PATCH v6 2/4 s390/pci: Reuse FMB buffer and preserve state in device re-enablement

In reply to Omar Elghoul

Introduce the function zpci_fmb_reenable_device() that checks the state of function measurement and ensures it is enabled. Reset the counters to zero, disable, and re-enable the FMB if it was already enabled. Call this function from zpci_reenable_device().

Don't free the FMB buffer during disabling and reuse it when re-enabling measurement. Instead, free the buffer upon device teardown, allowing the same buffer to be reused in the enable path and add the bit fmb_enabled to struct zpci_dev. Audit the only consumer of zdev->fmb and update it to reflect the change in semantics.

---
 arch/s390/include/asm/pci.h |  2 +
 arch/s390/pci/pci.c         | 75 ++++++++++++++++++++++++++++---------
 arch/s390/pci/pci_debug.c   |  2 +-
 3 files changed, 61 insertions(+), 18 deletions(-)
arch/s390/include/asm/pci.h +2 -0
index 88a125b92bdd..b8162f7a8968 100644
@@ -175,6 +175,7 @@ struct zpci_dev {
 	u8		util_str_avail	: 1;
 	u8		tid_avail	: 1;
 	u8		rtr_avail	: 1; /* Relaxed translation allowed */
+	u8		fmb_enabled	: 1;
 	unsigned int	devfn;		/* DEVFN part of the RID*/
 
 	u8 pfip[CLP_PFIP_NR_SEGMENTS];	/* pci function internal path */
@@ -351,6 +352,7 @@ 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_reenable_device(struct zpci_dev *zdev);
 
 /* Debug */
 int zpci_debug_init(void);
arch/s390/pci/pci.c +58 -17

117 lines not on this page · raw message

arch/s390/pci/pci_debug.c +1 -1
index c7ed7bf254b5..44f026ead414 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.55.0
#5 lore raw

Re: [PATCH v6 2/4] s390/pci: Reuse FMB buffer and preserve state in device re-enablement

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] s390/pci: zpci_fmb_reenable_device() unconditionally enables FMB --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] s390/pci: zpci_fmb_reenable_device() unconditionally enables FMB --

commit 7c558501997618a3d6ef7ed7b44346ed809628a3
Author: Omar Elghoul <[email protected]>

s390/pci: Reuse FMB buffer and preserve state in device re-enablement

This commit introduces zpci_fmb_reenable_device() to check and ensure the function measurement is enabled. It avoids freeing the FMB buffer during disabling, allowing it to be reused when re-enabling measurement. To track this, it adds an fmb_enabled flag to the zpci_dev structure.

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

[ ... ]

Quoted 1 line
arch/s390/pci/pci.c +0 -0
@@ -224,13 +229,39 @@ int zpci_fmb_disable_device(struct zpci_dev *zdev)

[ ... ]

arch/s390/pci/pci.c int zpci_fmb_disable_device(struct zpci_dev *zdev) · line 249

Quoted 10 lines
arch/s390/pci/pci.c +10 -0
+int zpci_fmb_reenable_device(struct zpci_dev *zdev)
+{
+	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
+	struct zpci_fib fib = {0};
+	u8 cc, status;
+
+	lockdep_assert_held(&zdev->fmb_lock);
+
+	if (!zdev->fmb_enabled)
+		return zpci_fmb_enable_device(zdev);

[Severity: Medium]
If FMB is explicitly disabled by a user via debugfs or by a guest VM via the VFIO FMB feature, will a subsequent device hot reset silently force FMB back on?

During a hot reset, a sequence like zpci_hot_reset_device() -> zpci_reenable_device() -> zpci_fmb_reenable_device() is triggered. If a guest VM or user previously disabled FMB, zdev->fmb_enabled will be 0.

Does returning zpci_fmb_enable_device() here unconditionally enable it again, overriding the user or guest VM configuration and going against the commit's stated goal to preserve state?

Quoted 3 lines
arch/s390/pci/pci.c +3 -0
+
+	fib.gd = zdev->gisa;
+	cc = zpci_mod_fc(req, &fib, &status); /* Disable function measurement */

[ ... ]

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

Re: [PATCH v6 2/4] s390/pci: Reuse FMB buffer and preserve state in device re-enablement

In reply to Omar Elghoul

arch/s390/pci/pci.c int zpci_fmb_disable_device(struct zpci_dev *zdev) · line 249

Quoted 11 lines
arch/s390/pci/pci.c +10 -0
+int zpci_fmb_reenable_device(struct zpci_dev *zdev)
+{
+	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
+	struct zpci_fib fib = {0};
+	u8 cc, status;
+
+	lockdep_assert_held(&zdev->fmb_lock);
+
+	if (!zdev->fmb_enabled)
+		return zpci_fmb_enable_device(zdev);

Sashiko has a point here, won't all paths that call zpci_reenable_device now unconditionally turn on FMB here even if it was purposely disabled?

The pattern of callers for zpci_reenable_device() is:

zpci_disable_device()
...
zpci_reenable_device()

We only care to re-enable the FMB during device re-enable if the device had FMB enabled at the time zpci_disable_device() was called.

Thanks,
Matt

Quoted 15 lines
arch/s390/pci/pci.c +15 -0
+
+	fib.gd = zdev->gisa;
+	cc = zpci_mod_fc(req, &fib, &status); /* Disable function measurement */
+
+	/* Unlike in zpci_fmb_disable_device(), cc == 3 is not a valid state here
+	 * because we are re-enabling function measurement for the same function
+	 * handle.
+	 */
+	if (cc)
+		return -EIO;
+
+	zdev->fmb_enabled = 0;
+	return zpci_fmb_enable_device(zdev);
+}
+EXPORT_SYMBOL_GPL(zpci_fmb_reenable_device);
#7 lore raw

Re: [PATCH v6 2/4] s390/pci: Reuse FMB buffer and preserve state in device re-enablement

Quoted 15 lines · Matthew Rosato

arch/s390/pci/pci.c int zpci_fmb_disable_device(struct zpci_dev *zdev) · line 249

Quoted 11 lines
arch/s390/pci/pci.c +10 -0
+int zpci_fmb_reenable_device(struct zpci_dev *zdev)
+{
+	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
+	struct zpci_fib fib = {0};
+	u8 cc, status;
+
+	lockdep_assert_held(&zdev->fmb_lock);
+
+	if (!zdev->fmb_enabled)
+		return zpci_fmb_enable_device(zdev);

Sashiko has a point here, won't all paths that call zpci_reenable_device now unconditionally turn on FMB here even if it was purposely disabled?

Quoted 8 lines

The pattern of callers for zpci_reenable_device() is:

zpci_disable_device()
...
zpci_reenable_device()

We only care to re-enable the FMB during device re-enable if the device had FMB enabled at the time zpci_disable_device() was called.

The way I thought of it was pcibios_enable_device() already enables measurement, so we might as well ensure it is enabled when the zdev is re-enabled. That default state is also helpful for the later patches because kvm_s390_pci_register_kvm() calls zpci_reenable_device().

But I guess if we didn't want to conflate the two, we could also add an unconditional zpci_fmb_reenable_device() within the KVM register hook, to run after the zpci_reenable_device(), whichever you'd prefer.

Thanks

Quoted 20 lines

Thanks,
Matt

Quoted 15 lines
arch/s390/pci/pci.c +15 -0
+
+	fib.gd = zdev->gisa;
+	cc = zpci_mod_fc(req, &fib, &status); /* Disable function measurement */
+
+	/* Unlike in zpci_fmb_disable_device(), cc == 3 is not a valid state here
+	 * because we are re-enabling function measurement for the same function
+	 * handle.
+	 */
+	if (cc)
+		return -EIO;
+
+	zdev->fmb_enabled = 0;
+	return zpci_fmb_enable_device(zdev);
+}
+EXPORT_SYMBOL_GPL(zpci_fmb_reenable_device);
maintainer
#8 lore raw

Re: [PATCH v6 2/4] s390/pci: Reuse FMB buffer and preserve state in device re-enablement

Quoted 22 lines · Omar Elghoul
Quoted 21 lines · Matthew Rosato

arch/s390/pci/pci.c int zpci_fmb_disable_device(struct zpci_dev *zdev) · line 249

Quoted 11 lines
arch/s390/pci/pci.c +10 -0
+int zpci_fmb_reenable_device(struct zpci_dev *zdev)
+{
+    u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
+    struct zpci_fib fib = {0};
+    u8 cc, status;
+
+    lockdep_assert_held(&zdev->fmb_lock);
+
+    if (!zdev->fmb_enabled)
+        return zpci_fmb_enable_device(zdev);

Sashiko has a point here, won't all paths that call zpci_reenable_device now unconditionally turn on FMB here even if it was purposely disabled?

The pattern of callers for zpci_reenable_device() is:

zpci_disable_device()
...
zpci_reenable_device()

Quoted 5 lines
Quoted 2 lines

We only care to re-enable the FMB during device re-enable if the device had FMB enabled at the time zpci_disable_device() was called.

The way I thought of it was pcibios_enable_device() already enables measurement, so we might as well ensure it is enabled when the zdev is

Yes, FMB starts enabled by default but then it could be disabled intentionally via sysfs; if a recovery action occurs on the device sometime after I manually disabled FMB for a device, I'd expect it to still be disabled after the recovery action but AFAICT with this codepath it will always get re-enabled.

Completely removing the device and re-introducing it to the system is a different matter; in that case yeah we'd be back to FMB-enabled because that's our default starting state via pcibios_enable_device().

Quoted 6 lines

re-enabled. That default state is also helpful for the later patches because kvm_s390_pci_register_kvm() calls zpci_reenable_device().

But I guess if we didn't want to conflate the two, we could also add an unconditional zpci_fmb_reenable_device() within the KVM register hook, to run after the zpci_reenable_device(), whichever you'd prefer.

Do we even want it to be part of the KVM register hook at all, or could we leave it off until we get a VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE?

That way if a guest isn't using FMB (we never intercepted the associated MPCIFC to enable measurement), QEMU would never issue VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE and then we just leave the host FMB off.

In practice, yes, FMB is always on by default for a linux guest today but if we were to ever move to a model where we make it opt-in, it would be nice to only enable the host FMB when the guest actually wants it.

#9 lore raw

Re: [PATCH v6 2/4] s390/pci: Reuse FMB buffer and preserve state in device re-enablement

Quoted 35 lines · Matthew Rosato
Quoted 22 lines · Omar Elghoul
Quoted 21 lines · Matthew Rosato

arch/s390/pci/pci.c int zpci_fmb_disable_device(struct zpci_dev *zdev) · line 249

Quoted 11 lines
arch/s390/pci/pci.c +10 -0
+int zpci_fmb_reenable_device(struct zpci_dev *zdev)
+{
+    u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
+    struct zpci_fib fib = {0};
+    u8 cc, status;
+
+    lockdep_assert_held(&zdev->fmb_lock);
+
+    if (!zdev->fmb_enabled)
+        return zpci_fmb_enable_device(zdev);

Sashiko has a point here, won't all paths that call zpci_reenable_device now unconditionally turn on FMB here even if it was purposely disabled?

The pattern of callers for zpci_reenable_device() is:

zpci_disable_device()
...
zpci_reenable_device()

Quoted 5 lines
Quoted 2 lines

We only care to re-enable the FMB during device re-enable if the device had FMB enabled at the time zpci_disable_device() was called.

The way I thought of it was pcibios_enable_device() already enables measurement, so we might as well ensure it is enabled when the zdev is

Yes, FMB starts enabled by default but then it could be disabled intentionally via sysfs; if a recovery action occurs on the device sometime after I manually disabled FMB for a device, I'd expect it to still be disabled after the recovery action but AFAICT with this codepath it will always get re-enabled.

Quoted 3 lines

Completely removing the device and re-introducing it to the system is a different matter; in that case yeah we'd be back to FMB-enabled because that's our default starting state via pcibios_enable_device().

Makes sense, I will fix this in the next version.

Quoted 18 lines
Quoted 7 lines

re-enabled. That default state is also helpful for the later patches because kvm_s390_pci_register_kvm() calls zpci_reenable_device().

But I guess if we didn't want to conflate the two, we could also add an unconditional zpci_fmb_reenable_device() within the KVM register hook, to run after the zpci_reenable_device(), whichever you'd prefer.

Do we even want it to be part of the KVM register hook at all, or could we leave it off until we get a VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE?

That way if a guest isn't using FMB (we never intercepted the associated MPCIFC to enable measurement), QEMU would never issue VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE and then we just leave the host FMB off.

In practice, yes, FMB is always on by default for a linux guest today but if we were to ever move to a model where we make it opt-in, it would be nice to only enable the host FMB when the guest actually wants it.

#10 lore raw

PATCH v6 3/4 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 44f026ead414..afc52bd5271a 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.55.0
#11 lore raw

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

Sashiko has reviewed this patch and found no issues. It looks great!
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. It looks great!

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

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

In reply to Omar Elghoul
Quoted 10 lines · 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.

Quoted 31 lines
---
 arch/s390/pci/pci_debug.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/s390/pci/pci_debug.c +9 -0
index 44f026ead414..afc52bd5271a 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;
 }
 
#13 lore raw

PATCH v6 4/4 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 | 60 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 29 +++++++++++++++
 4 files changed, 111 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +4 -0
index 6757054e9d87..3c827a77725b 100644
@@ -1627,6 +1627,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 +60 -0

63 lines not on this page · raw message

include/uapi/linux/vfio.h +29 -0
index e41437fa17ad..a8d501e42ef9 100644
@@ -1555,6 +1555,35 @@ 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 resets the FMB counters to zero, and nested disabling fails.
+ *
+ * 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.
+ *
+ * Returns: 0 on success, -1 and errno set appropriately on error. errno==ENOMSG
+ * when the FMB is not enabled.
+ */
+#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.55.0
#14 lore raw

Re: [PATCH v6 4/4] vfio-pci/zdev: Add VFIO FMB device features

Sashiko has reviewed this patch and found no issues. It looks great!
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. It looks great!

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