Skip to content

vfio-pci/zdev: Improved zPCI Function Measurement Support

Omar Elghoul kvm linux-s390 lkml sashiko-reviews stable 13 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v13 Latest5 patches 4 repliesIn review
  2. v125 patches 5 repliesSuperseded
  3. v115 patches 10 repliesSuperseded
Earlier versions · 10
VersionPostedPatchesRepliesStatus
v105 patches1 replySuperseded
v94 patches4 repliesSuperseded
v84 patches4 repliesSuperseded
v74 patches13 repliesSuperseded
v64 patches6 repliesSuperseded
v54 patches1 replySuperseded
v44 patches5 repliesSuperseded
v34 patches7 repliesSuperseded
v23 patches12 repliesSuperseded
v1 RFC5 patches1 replySuperseded

Changes in v13 from the cover letter

* 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

Patches in v13 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5s390/pci: Hold fmb_lock when enabling or disabling PCI devicesarch/s390/pci/pci.c+8 -00 replies
2/5s390/pci: Improve function measurement state managementarch/s390/include/asm/pci.h arch/s390/pci/pci.c arch/s390/pci/pci_debug.c+56 -92 replies
3/5s390/pci: Improve FMB memory managementarch/s390/include/asm/pci.h arch/s390/pci/pci.c arch/s390/pci/pci_debug.c+37 -271 reply
4/5s390/pci: Fence FMB enable/disable via debugfs for passthrough devicesarch/s390/pci/pci_debug.c+9 -00 replies
5/5vfio-pci/zdev: Add VFIO FMB device featuresdrivers/vfio/pci/vfio_pci_core.c drivers/vfio/pci/vfio_pci_priv.h drivers/vfio/pci/vfio_pci_zdev.c +1 more+116 -00 replies

Reviews trailers per patch

All 5 patches have a Reviewed-by or Acked-by.

PatchNiklas SchnelleMatthew RosatoGerd BayerAlex Williamson
1/5R-b R-b
2/5R-b R-b
3/5R-b
4/5R-b R-b
5/5R-b R-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v13 thread

Cover letter [PATCH v13 0/5] 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