* [PATCH v11 1/5] s390/pci: Hold fmb_lock when enabling or disabling PCI devices
2026-10-28 15:43 [PATCH v11 0/5] vfio-pci/zdev: Improved zPCI Function Measurement Support Omar Elghoul
@ 2026-10-28 15:43 ` Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 2/5] s390/pci: Improve function measurement state management Omar Elghoul
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Omar Elghoul @ 2026-10-28 15:43 UTC (permalink / raw)
To: linux-s390, linux-kernel, kvm
Cc: hca, gor, agordeev, borntraeger, svens, schnelle, mjrosato,
alifm, farman, gbayer, pasic, alex, frankja, imbrenda, stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com>
Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com>
---
arch/s390/pci/pci.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index d64c544b32b0..c055a9ad0972 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -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);
}
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v11 2/5] s390/pci: Improve function measurement state management
2026-10-28 15:43 [PATCH v11 0/5] vfio-pci/zdev: Improved zPCI Function Measurement Support Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 1/5] s390/pci: Hold fmb_lock when enabling or disabling PCI devices Omar Elghoul
@ 2026-10-28 15:43 ` Omar Elghoul
2026-10-28 21:05 ` Niklas Schnelle
2026-10-28 15:43 ` [PATCH v11 3/5] s390/pci: Reuse FMB buffer across enable and disable cycles Omar Elghoul
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Omar Elghoul @ 2026-10-28 15:43 UTC (permalink / raw)
To: linux-s390, linux-kernel, kvm
Cc: hca, gor, agordeev, borntraeger, svens, schnelle, mjrosato,
alifm, farman, gbayer, pasic, alex, frankja, imbrenda
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() and off(), which
defer to zpci_fmb_enable() or disable() to perform the actual firmware
change. This decouples the enablement or disablement step from managing
the user's requested state.
Preserve the state in pcibios_enable_device(), pcibios_disable_device(),
and zpci_reenable_device(), so measurement is restored automatically
after a re-enable. New devices start with measurement on by default,
unchanged from before.
Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
---
arch/s390/include/asm/pci.h | 7 +++--
arch/s390/pci/pci.c | 60 +++++++++++++++++++++++++++++++++----
arch/s390/pci/pci_debug.c | 6 ++--
3 files changed, 63 insertions(+), 10 deletions(-)
diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h
index 88a125b92bdd..dd7a17955362 100644
--- a/arch/s390/include/asm/pci.h
+++ b/arch/s390/include/asm/pci.h
@@ -208,6 +208,7 @@ struct zpci_dev {
struct zpci_fmb *fmb;
u16 fmb_update; /* update interval */
u16 fmb_length;
+ bool statistics_on; /* 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);
diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index c055a9ad0972..5889dd227009 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -164,8 +164,44 @@ int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
return cc;
}
+int zpci_fmb_request_on(struct zpci_dev *zdev)
+{
+ int rc;
+
+ lockdep_assert_held(&zdev->fmb_lock);
+
+ if (zdev->statistics_on)
+ return -EINVAL;
+
+ rc = zpci_fmb_enable(zdev);
+ if (rc)
+ return rc;
+
+ zdev->statistics_on = true;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(zpci_fmb_request_on);
+
+int zpci_fmb_request_off(struct zpci_dev *zdev)
+{
+ int rc;
+
+ lockdep_assert_held(&zdev->fmb_lock);
+
+ if (!zdev->statistics_on)
+ return -EINVAL;
+
+ rc = zpci_fmb_disable(zdev);
+ if (rc)
+ return rc;
+
+ zdev->statistics_on = false;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(zpci_fmb_request_off);
+
/* Modify PCI: Set PCI function measurement parameters */
-int zpci_fmb_enable_device(struct zpci_dev *zdev)
+int zpci_fmb_enable(struct zpci_dev *zdev)
{
u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
struct zpci_iommu_ctrs *ctrs;
@@ -207,7 +243,7 @@ int zpci_fmb_enable_device(struct zpci_dev *zdev)
}
/* Modify PCI: Disable PCI function measurement */
-int zpci_fmb_disable_device(struct zpci_dev *zdev)
+int zpci_fmb_disable(struct zpci_dev *zdev)
{
u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
struct zpci_fib fib = {0};
@@ -644,7 +680,8 @@ int pcibios_enable_device(struct pci_dev *pdev, int mask)
zpci_debug_init_device(zdev, dev_name(&pdev->dev));
mutex_lock(&zdev->fmb_lock);
- zpci_fmb_enable_device(zdev);
+ if (zdev->statistics_on)
+ zpci_fmb_enable(zdev);
mutex_unlock(&zdev->fmb_lock);
return pci_enable_resources(pdev, mask);
@@ -655,7 +692,8 @@ 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);
+ if (zdev->statistics_on)
+ zpci_fmb_disable(zdev);
mutex_unlock(&zdev->fmb_lock);
zpci_debug_exit_device(zdev);
}
@@ -737,8 +775,14 @@ int zpci_reenable_device(struct zpci_dev *zdev)
}
rc = zpci_iommu_register_ioat(zdev, &status);
- if (rc)
+ if (rc) {
zpci_disable_device(zdev);
+ return rc;
+ }
+
+ guard(mutex)(&zdev->fmb_lock);
+ if (zdev->statistics_on)
+ zpci_fmb_enable(zdev);
return rc;
}
@@ -847,6 +891,12 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
goto error;
zdev->state = state;
+ /*
+ * Measurement is on by default for new functions, this will be honored
+ * by pcibios_enable_device().
+ */
+ zdev->statistics_on = true;
+
mutex_init(&zdev->state_lock);
mutex_init(&zdev->fmb_lock);
mutex_init(&zdev->kzdev_lock);
diff --git a/arch/s390/pci/pci_debug.c b/arch/s390/pci/pci_debug.c
index c7ed7bf254b5..e479763858cd 100644
--- a/arch/s390/pci/pci_debug.c
+++ b/arch/s390/pci/pci_debug.c
@@ -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->statistics_on || !zdev->fmb) {
mutex_unlock(&zdev->fmb_lock);
seq_puts(m, "FMB statistics disabled\n");
return 0;
@@ -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);
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v11 2/5] s390/pci: Improve function measurement state management
2026-10-28 15:43 ` [PATCH v11 2/5] s390/pci: Improve function measurement state management Omar Elghoul
@ 2026-10-28 21:05 ` Niklas Schnelle
0 siblings, 0 replies; 7+ messages in thread
From: Niklas Schnelle @ 2026-10-28 21:05 UTC (permalink / raw)
To: Omar Elghoul, linux-s390, linux-kernel, kvm
Cc: hca, gor, agordeev, borntraeger, svens, mjrosato, alifm, farman,
gbayer, pasic, alex, frankja, imbrenda
On Wed, 2026-10-28 at 11:43 -0400, Omar Elghoul wrote:
> 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() and off(), which
> defer to zpci_fmb_enable() or disable() to perform the actual firmware
Super nit: "off()" on its own reads as if that was the actual name. I'd
either use "…_off()", "zpci_fmb_request_on()/_off()" or just the full
name for both. Same for "disable()". If this is all it's probably not
enough of a reason to respin.
> change. This decouples the enablement or disablement step from managing
> the user's requested state.
>
> Preserve the state in pcibios_enable_device(), pcibios_disable_device(),
Nit: Not a native speaker but I think this isn't really "preserve" but
rather "restore" with the word changed you could drop the second half
of the sentence.
> and zpci_reenable_device(), so measurement is restored automatically
> after a re-enable. New devices start with measurement on by default,
> unchanged from before.
>
> Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
> ---
> arch/s390/include/asm/pci.h | 7 +++--
> arch/s390/pci/pci.c | 60 +++++++++++++++++++++++++++++++++----
> arch/s390/pci/pci_debug.c | 6 ++--
> 3 files changed, 63 insertions(+), 10 deletions(-)
>
The patch looks good but I think there is an interaction with patch 3
that may cause a change in this patch. That said, I think one can also
just fix it in patch 3 or maybe me and Sashiko are just confused.
So since this patch itself is fine and may stay unchanged feel free to
add the below:
Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com>
Thanks,
Niklas
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v11 3/5] s390/pci: Reuse FMB buffer across enable and disable cycles
2026-10-28 15:43 [PATCH v11 0/5] vfio-pci/zdev: Improved zPCI Function Measurement Support Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 1/5] s390/pci: Hold fmb_lock when enabling or disabling PCI devices Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 2/5] s390/pci: Improve function measurement state management Omar Elghoul
@ 2026-10-28 15:43 ` Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 4/5] s390/pci: Fence FMB enable/disable via debugfs for passthrough devices Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 5/5] vfio-pci/zdev: Add VFIO FMB device features Omar Elghoul
4 siblings, 0 replies; 7+ messages in thread
From: Omar Elghoul @ 2026-10-28 15:43 UTC (permalink / raw)
To: linux-s390, linux-kernel, kvm
Cc: hca, gor, agordeev, borntraeger, svens, schnelle, mjrosato,
alifm, farman, gbayer, pasic, alex, frankja, imbrenda
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().
Drop the alignment warning after allocating the FMB buffer due to it
already being guaranteed by the kmem_cache.
Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
---
arch/s390/pci/pci.c | 31 +++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index 5889dd227009..a7e548a83bc1 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -211,13 +211,17 @@ int zpci_fmb_enable(struct zpci_dev *zdev)
lockdep_assert_held(&zdev->fmb_lock);
- if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
+ if (sizeof(*zdev->fmb) < zdev->fmb_length)
return -EINVAL;
- zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
- if (!zdev->fmb)
- return -ENOMEM;
- WARN_ON((u64) zdev->fmb & 0xf);
+ if (!zdev->fmb) {
+ zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
+ if (!zdev->fmb)
+ return -ENOMEM;
+ } else {
+ /* reuse the same FMB buffer for as long as the zdev lives */
+ memset(zdev->fmb, 0, sizeof(*zdev->fmb));
+ }
/* reset software counters */
spin_lock_irqsave(&zdev->dom_lock, flags);
@@ -235,10 +239,6 @@ int zpci_fmb_enable(struct zpci_dev *zdev)
fib.fmb_addr = virt_to_phys(zdev->fmb);
fib.gd = zdev->gisa;
cc = zpci_mod_fc(req, &fib, &status);
- if (cc) {
- kmem_cache_free(zdev_fmb_cache, zdev->fmb);
- zdev->fmb = NULL;
- }
return cc ? -EIO : 0;
}
@@ -251,9 +251,6 @@ int zpci_fmb_disable(struct zpci_dev *zdev)
lockdep_assert_held(&zdev->fmb_lock);
- if (!zdev->fmb)
- return -EINVAL;
-
fib.gd = zdev->gisa;
/* Function measurement is disabled if fmb address is zero */
@@ -261,10 +258,6 @@ int zpci_fmb_disable(struct zpci_dev *zdev)
if (cc == 3) /* Function already gone. */
cc = 0;
- if (!cc) {
- kmem_cache_free(zdev_fmb_cache, zdev->fmb);
- zdev->fmb = NULL;
- }
return cc ? -EIO : 0;
}
@@ -1053,6 +1046,12 @@ void zpci_release_device(struct kref *kref)
if (zdev->has_resources)
zpci_cleanup_bus_resources(zdev);
+ if (zdev->fmb) {
+ zdev->statistics_on = false;
+ kmem_cache_free(zdev_fmb_cache, zdev->fmb);
+ zdev->fmb = NULL;
+ }
+
zpci_bus_device_unregister(zdev);
zpci_destroy_iommu(zdev);
zpci_dbg(3, "rem fid:%x\n", zdev->fid);
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v11 4/5] s390/pci: Fence FMB enable/disable via debugfs for passthrough devices
2026-10-28 15:43 [PATCH v11 0/5] vfio-pci/zdev: Improved zPCI Function Measurement Support Omar Elghoul
` (2 preceding siblings ...)
2026-10-28 15:43 ` [PATCH v11 3/5] s390/pci: Reuse FMB buffer across enable and disable cycles Omar Elghoul
@ 2026-10-28 15:43 ` Omar Elghoul
2026-10-28 15:43 ` [PATCH v11 5/5] vfio-pci/zdev: Add VFIO FMB device features Omar Elghoul
4 siblings, 0 replies; 7+ messages in thread
From: Omar Elghoul @ 2026-10-28 15:43 UTC (permalink / raw)
To: linux-s390, linux-kernel, kvm
Cc: hca, gor, agordeev, borntraeger, svens, schnelle, mjrosato,
alifm, farman, gbayer, pasic, alex, frankja, imbrenda
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.
Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com>
Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
arch/s390/pci/pci_debug.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/arch/s390/pci/pci_debug.c b/arch/s390/pci/pci_debug.c
index e479763858cd..21d08410799e 100644
--- a/arch/s390/pci/pci_debug.c
+++ b/arch/s390/pci/pci_debug.c
@@ -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;
}
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v11 5/5] vfio-pci/zdev: Add VFIO FMB device features
2026-10-28 15:43 [PATCH v11 0/5] vfio-pci/zdev: Improved zPCI Function Measurement Support Omar Elghoul
` (3 preceding siblings ...)
2026-10-28 15:43 ` [PATCH v11 4/5] s390/pci: Fence FMB enable/disable via debugfs for passthrough devices Omar Elghoul
@ 2026-10-28 15:43 ` Omar Elghoul
4 siblings, 0 replies; 7+ messages in thread
From: Omar Elghoul @ 2026-10-28 15:43 UTC (permalink / raw)
To: linux-s390, linux-kernel, kvm
Cc: hca, gor, agordeev, borntraeger, svens, schnelle, mjrosato,
alifm, farman, gbayer, pasic, alex, frankja, imbrenda
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.
Signed-off-by: Omar Elghoul <oelghoul@linux.ibm.com>
---
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(+)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2..f2d5b6857925 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -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;
}
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 4e7162234a2e..e04d7e9d0c30 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -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)
diff --git a/drivers/vfio/pci/vfio_pci_zdev.c b/drivers/vfio/pci/vfio_pci_zdev.c
index f47f36314a1c..39921c363892 100644
--- a/drivers/vfio/pci/vfio_pci_zdev.c
+++ b/drivers/vfio/pci/vfio_pci_zdev.c
@@ -219,3 +219,67 @@ void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
if (zpci_kvm_hook.kvm_unregister)
zpci_kvm_hook.kvm_unregister(zdev);
}
+
+int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev, u32 flags,
+ void __user *arg, size_t argsz)
+{
+ struct zpci_dev *zdev;
+ struct vfio_device_feature_zpci_fmb_enable fmb_enable;
+ int ret;
+
+ ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, sizeof(fmb_enable));
+ if (ret != 1)
+ return ret;
+
+ zdev = to_zpci(vdev->pdev);
+ if (!zdev)
+ return -ENODEV;
+
+ if (copy_from_user(&fmb_enable, arg, sizeof(fmb_enable)))
+ return -EFAULT;
+
+ guard(mutex)(&zdev->fmb_lock);
+
+ if (fmb_enable.enabled) {
+ ret = zpci_fmb_request_off(zdev);
+ if (ret && ret != -EINVAL)
+ return ret;
+
+ return zpci_fmb_request_on(zdev);
+ }
+
+ return zpci_fmb_request_off(zdev);
+}
+
+int vfio_pci_zdev_feature_fmb_read(struct vfio_pci_core_device *vdev, u32 flags,
+ void __user *arg, size_t argsz)
+{
+ struct zpci_dev *zdev;
+ struct vfio_device_feature_zpci_fmb_read fmb_read;
+ struct zpci_fmb fmb_bounce;
+ int ret;
+
+ ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, sizeof(fmb_read));
+ if (ret != 1)
+ return ret;
+
+ zdev = to_zpci(vdev->pdev);
+ if (!zdev)
+ return -ENODEV;
+
+ if (copy_from_user(&fmb_read, arg, sizeof(fmb_read)))
+ return -EFAULT;
+ if (!fmb_read.data)
+ return -EINVAL;
+
+ scoped_guard(mutex, &zdev->fmb_lock) {
+ if (!zdev->statistics_on || !zdev->fmb)
+ return -ENOMSG;
+ memcpy(&fmb_bounce, zdev->fmb, zdev->fmb_length);
+ }
+
+ if (copy_to_user(u64_to_user_ptr(fmb_read.data), &fmb_bounce, zdev->fmb_length))
+ return -EFAULT;
+
+ return 0;
+}
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index e41437fa17ad..7e9adb61c5e1 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -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 -------- */
/**
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread