Skip to contentSkip to conversation

vfio-pci/zdev: Improved zPCI Function Measurement Support

PATCH In review Omar Elghoul 3 participants 9 messages May 1 – May 5

Subsystems (5)
KVM s390 VFIO driver s390 PCI +1 more subsystems
lore
Share & tools
lore
#0 lore raw

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

Hi,

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

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 (3):
s390/pci: Preserve FMB state in device re-enablement vfio-pci/zdev: Add VFIO FMB device feature
s390/pci: Fence FMB enable/disable via sysfs for passthrough devices

 arch/s390/include/asm/pci.h      |  1 +
 arch/s390/pci/pci.c              | 71 +++++++++++++++++++++++------
 arch/s390/pci/pci_debug.c        |  3 ++
 drivers/vfio/pci/vfio_pci_core.c |  2 +
 drivers/vfio/pci/vfio_pci_priv.h |  9 ++++
 drivers/vfio/pci/vfio_pci_zdev.c | 77 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 43 ++++++++++++++++++
 7 files changed, 193 insertions(+), 13 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/3 s390/pci: Preserve FMB state in device re-enablement

Introduce a function zpci_fmb_reenable_device() that checks for the state of the FMB and reuses the same buffer where appropriate. If FMB was not previously enabled, it enables it for the device. Call this function during a zPCI device re-enablement, which in turn implicitly ensures that the FMB is enabled for host devices during their KVM registration.

This function also clears out the software counters, so that a program resetting an FMB would see all its counters restart from zero as expected. The function to clear the software counters is also separated into a static function as it is now reused in both zpci_fmb_enable_device() and zpci_fmb_reenable_device().

---
 arch/s390/include/asm/pci.h |  1 +
 arch/s390/pci/pci.c         | 71 ++++++++++++++++++++++++++++++-------
 2 files changed, 59 insertions(+), 13 deletions(-)
arch/s390/include/asm/pci.h +1 -0
index 5dcf35f0f325..65014e52d559 100644
@@ -323,6 +323,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 -13

97 lines not on this page · raw message

Signature · 1 line
2.52.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH v1 1/3] s390/pci: Preserve FMB state in device re-enablement

Quoted 12 lines · Omar Elghoul

Introduce a function zpci_fmb_reenable_device() that checks for the state of the FMB and reuses the same buffer where appropriate. If FMB was not previously enabled, it enables it for the device. Call this function during a zPCI device re-enablement, which in turn implicitly ensures that the FMB is enabled for host devices during their KVM registration.

This function also clears out the software counters, so that a program resetting an FMB would see all its counters restart from zero as expected. The function to clear the software counters is also separated into a static function as it is now reused in both zpci_fmb_enable_device() and zpci_fmb_reenable_device().

Quoted 6 lines
---
 arch/s390/include/asm/pci.h |  1 +
 arch/s390/pci/pci.c         | 71 ++++++++++++++++++++++++++++++-------
 2 files changed, 59 insertions(+), 13 deletions(-)

Looks good to me. I also gave this a quick sniff test on an LPAR but still have to test with the FMB handling for KVM that this enabled so not enough for a Tested-by yet.

Still feel free to add my:

Thanks,
Niklas

maintainer
#3 lore raw

Re: [PATCH v1 1/3] s390/pci: Preserve FMB state in device re-enablement

Quoted 24 lines · Niklas Schnelle
Quoted 12 lines · Omar Elghoul

Introduce a function zpci_fmb_reenable_device() that checks for the state of the FMB and reuses the same buffer where appropriate. If FMB was not previously enabled, it enables it for the device. Call this function during a zPCI device re-enablement, which in turn implicitly ensures that the FMB is enabled for host devices during their KVM registration.

This function also clears out the software counters, so that a program resetting an FMB would see all its counters restart from zero as expected. The function to clear the software counters is also separated into a static function as it is now reused in both zpci_fmb_enable_device() and zpci_fmb_reenable_device().

Quoted 6 lines
---
 arch/s390/include/asm/pci.h |  1 +
 arch/s390/pci/pci.c         | 71 ++++++++++++++++++++++++++++++-------
 2 files changed, 59 insertions(+), 13 deletions(-)

Looks good to me. I also gave this a quick sniff test on an LPAR but still have to test with the FMB handling for KVM that this enabled so not enough for a Tested-by yet.

Quoted 6 lines

Still feel free to add my:

Thanks,
Niklas

Ah, missed one thing, the kernel-test-robot is of course right in that with this patch flags and ctrs are now unused in zpci_fmb_enable_device() so the declarations should be dropped. The same is also true in zpci_fmb_reenable_device() which the test robot did miss!

Thanks,
Niklas

maintainer
#4 lore raw

Re: [PATCH v1 1/3] s390/pci: Preserve FMB state in device re-enablement

Quoted 32 lines · Niklas Schnelle
Quoted 24 lines · Niklas Schnelle
Quoted 12 lines · Omar Elghoul

Introduce a function zpci_fmb_reenable_device() that checks for the state of the FMB and reuses the same buffer where appropriate. If FMB was not previously enabled, it enables it for the device. Call this function during a zPCI device re-enablement, which in turn implicitly ensures that the FMB is enabled for host devices during their KVM registration.

This function also clears out the software counters, so that a program resetting an FMB would see all its counters restart from zero as expected. The function to clear the software counters is also separated into a static function as it is now reused in both zpci_fmb_enable_device() and zpci_fmb_reenable_device().

Quoted 6 lines
---
 arch/s390/include/asm/pci.h |  1 +
 arch/s390/pci/pci.c         | 71 ++++++++++++++++++++++++++++++-------
 2 files changed, 59 insertions(+), 13 deletions(-)

Looks good to me. I also gave this a quick sniff test on an LPAR but still have to test with the FMB handling for KVM that this enabled so not enough for a Tested-by yet.

Quoted 6 lines

Still feel free to add my:

Thanks,
Niklas

Quoted 8 lines

Ah, missed one thing, the kernel-test-robot is of course right in that with this patch flags and ctrs are now unused in zpci_fmb_enable_device() so the declarations should be dropped. The same is also true in zpci_fmb_reenable_device() which the test robot did miss!

Thanks,
Niklas

Missed another issue which thankfully Sashiko found. The use of zpci_fmb_reenable_device() in zpci_reenable_device() leads to the new call chain:

recover_store()
_do_recover()
zpci_reenable_device()
zpci_fmb_reenable_device()

Where we don't take the zdev->fmb_lock. I think it makes sense to add a lockdep_assert_held() to zpci_fmb_reenable_device().

Thanks,
Niklas

#5 lore raw

PATCH 2/3 vfio-pci/zdev: Add VFIO FMB device feature

In reply to Omar Elghoul

Set up a new VFIO feature for zPCI devices to share the latest FMB snapshot with userspace. This feature supports the same 4 FMB formats (0 through 3) that are already supported by the kernel.

With VFIO_DEVICE_FEATURE_GET, allow the user driver to read the latest FMB snapshot as well as query whether the FMB is currently enabled on the function, itself indicating whether the FMB snapshot is valid. On the other hand, with VFIO_DEVICE_FEATURE_SET, the userspace driver can enable or disable the FMB.

---
 drivers/vfio/pci/vfio_pci_core.c |  2 +
 drivers/vfio/pci/vfio_pci_priv.h |  9 ++++
 drivers/vfio/pci/vfio_pci_zdev.c | 77 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        | 43 ++++++++++++++++++
 4 files changed, 131 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +2 -0
index 3f8d093aacf8..63e80b6fa0dc 100644
@@ -1534,6 +1534,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_FMB:
+		return vfio_pci_zdev_feature_fmb(vdev, flags, arg, argsz);
 	default:
 		return -ENOTTY;
 	}
drivers/vfio/pci/vfio_pci_priv.h +9 -0
index fca9d0dfac90..208e05942b48 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_fmb(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)
@@ -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_fmb(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 +77 -0

80 lines not on this page · raw message

include/uapi/linux/vfio.h +43 -0

49 lines not on this page · raw message

Signature · 1 line
2.52.0
#6 lore raw

PATCH 3/3 s390/pci: Fence FMB enable/disable via sysfs for passthrough devices

In reply to Omar Elghoul

Introduce a fence over enabling or disabling FMB via sysfs when the zPCI device is associated with a KVM. This will allow a KVM guest to use FMB passthrough and avoid the edge-case where the host disables FMB while the guest is still using it, which may cause partial counter resets and inconsistent reads which have no parallel in the architecture.

With this patch, the userspace driver, likely QEMU, is still able to enable or disable the FMB using the VFIO device feature introduced in the previous patch, effectively securing what is associated with the VM state and isolating it from other processes on the host.

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 | 3 +++
 1 file changed, 3 insertions(+)
arch/s390/pci/pci_debug.c +3 -0
index c7ed7bf254b5..b28923395d03 100644
@@ -149,6 +149,9 @@ static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
 	if (!zdev)
 		return 0;
 
+	if (zdev->kzdev)
+		return -EPERM;
+
 	rc = kstrtoul_from_user(ubuf, count, 10, &val);
 	if (rc)
 		return rc;
Signature · 1 line
2.52.0
#7 lore raw

Re: [PATCH v1 3/3] s390/pci: Fence FMB enable/disable via sysfs for passthrough devices

kernel test robot noticed the following build warnings:

Hi Omar,

kernel test robot noticed the following build warnings:

[auto build test WARNING on s390/features]
[also build test WARNING on awilliam-vfio/next awilliam-vfio/for-linus kvms390/next linus/master v6.16-rc1 next-20260430] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Omar-Elghoul/s390-pci-Preserve-FMB-state-in-device-re-enablement/20260502-155729
base:   https://git.kernel.org/pub/scm/linux/kernel/git/s390/linux.git features
patch link:    https://lore.kernel.org/r/20260501192530.9429-4-oelghoul%40linux.ibm.com
patch subject: [PATCH v1 3/3] s390/pci: Fence FMB enable/disable via sysfs for passthrough devices
config: s390-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260503/[email protected]/config)
compiler: s390x-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260503/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

arch/s390/pci/pci.c: In function 'zpci_fmb_enable_device':

>> arch/s390/pci/pci.c:191:23: warning: unused variable 'flags' [-Wunused-variable]
     191 |         unsigned long flags;
         |                       ^~~~~
>> arch/s390/pci/pci.c:189:33: warning: unused variable 'ctrs' [-Wunused-variable]
     189 |         struct zpci_iommu_ctrs *ctrs;
         |                                 ^~~~
   arch/s390/pci/pci.c: In function 'zpci_fmb_reenable_device':
   arch/s390/pci/pci.c:244:23: warning: unused variable 'flags' [-Wunused-variable]
     244 |         unsigned long flags;
         |                       ^~~~~
   arch/s390/pci/pci.c:242:33: warning: unused variable 'ctrs' [-Wunused-variable]
     242 |         struct zpci_iommu_ctrs *ctrs;
         |                                 ^~~~

vim +/flags +191 arch/s390/pci/pci.c

029a3d529699a0 Omar Elghoul    2026-05-01  184  
d0b0885316ab7a Jan Glauber     2012-12-11  185  /* Modify PCI: Set PCI function measurement parameters */
d0b0885316ab7a Jan Glauber     2012-12-11  186  int zpci_fmb_enable_device(struct zpci_dev *zdev)
d0b0885316ab7a Jan Glauber     2012-12-11  187  {
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  188  	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
92bce97f0c341d Niklas Schnelle 2023-10-04 @189  	struct zpci_iommu_ctrs *ctrs;
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  190  	struct zpci_fib fib = {0};
ecda483339a515 Matthew Rosato  2024-09-10 @191  	unsigned long flags;
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  192  	u8 cc, status;
d0b0885316ab7a Jan Glauber     2012-12-11  193  
0b7589ecca2b6f Sebastian Ott   2016-06-15  194  	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
d0b0885316ab7a Jan Glauber     2012-12-11  195  		return -EINVAL;
d0b0885316ab7a Jan Glauber     2012-12-11  196  
08b421245692f3 Wei Yongjun     2013-02-25  197  	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
d0b0885316ab7a Jan Glauber     2012-12-11  198  	if (!zdev->fmb)
d0b0885316ab7a Jan Glauber     2012-12-11  199  		return -ENOMEM;
d0b0885316ab7a Jan Glauber     2012-12-11  200  	WARN_ON((u64) zdev->fmb & 0xf);
d0b0885316ab7a Jan Glauber     2012-12-11  201  
029a3d529699a0 Omar Elghoul    2026-05-01  202  	zpci_fmb_clear_iommu_ctrs(zdev);
6001018ae8c659 Sebastian Ott   2015-04-10  203  
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  204  	fib.fmb_addr = virt_to_phys(zdev->fmb);
c68468ed3416ea Matthew Rosato  2022-06-06  205  	fib.gd = zdev->gisa;
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  206  	cc = zpci_mod_fc(req, &fib, &status);
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  207  	if (cc) {
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  208  		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  209  		zdev->fmb = NULL;
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  210  	}
4e5bd7803bd1e8 Sebastian Ott   2017-06-10  211  	return cc ? -EIO : 0;
d0b0885316ab7a Jan Glauber     2012-12-11  212  }
d0b0885316ab7a Jan Glauber     2012-12-11  213  

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#8 lore raw
In reply to Omar Elghoul

For illustration of example usage, linked below is the QEMU RFC that proposes using the feature introduced in this patch series.

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

Quoted 42 lines · Omar Elghoul

Hi,

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

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 (3):
s390/pci: Preserve FMB state in device re-enablement vfio-pci/zdev: Add VFIO FMB device feature
s390/pci: Fence FMB enable/disable via sysfs for passthrough devices

  arch/s390/include/asm/pci.h      |  1 +
  arch/s390/pci/pci.c              | 71 +++++++++++++++++++++++------
  arch/s390/pci/pci_debug.c        |  3 ++
  drivers/vfio/pci/vfio_pci_core.c |  2 +
  drivers/vfio/pci/vfio_pci_priv.h |  9 ++++
  drivers/vfio/pci/vfio_pci_zdev.c | 77 ++++++++++++++++++++++++++++++++
  include/uapi/linux/vfio.h        | 43 ++++++++++++++++++
  7 files changed, 193 insertions(+), 13 deletions(-)