s390x/pci: Improved zPCI Function Measurement Support
cover s390x/pci: Improved zPCI Function Measurement Support
Hi,
This patch series improves support for function measurement on s390x for zPCI passthrough devices. It is dependent on the eponymous kernel patch series linked below. [1]
Motivation
==========
The firmware on s390x machines allows for tracking a variety of statistics relating to zPCI devices in a function measurement block (FMB). At the time of writing, the kernel does not provide QEMU with a structured mechanism to fetch the FMB of passthrough devices. QEMU also does not report the PFT to the guest, which can cause issues with tools such as smc-rnics that depend on the PFT to detect ISM and RoCE devices. Furthermore, the FMB itself can take on different formats depending on the PFT, while QEMU's implementation is limited to only the generic section that prefixes the format-specific structures.
Proposal
========
The aforementioned kernel series proposes a VFIO feature that allows QEMU to read the latest snapshot of the FMB. In this patch series, we propose using that mechanism to share the FMB with the guest, as seen from the host. We also inform the guest of the PFT and the FMB length as detected from the host's CLP. Furthermore, we implement the format-0 FMB (DMA counters) for virtio-pci devices.
I'd appreciate any comments on this approach.
Thanks in advance.
[1] https://lore.kernel.org/all/[email protected]/
Omar Elghoul (5):
[do-not-merge] linux-headers: Add definitions for VFIO zPCI FMB
passthrough
hw/s390x: Clarified FMB format for emulated and passthrough devices hw/virtio: Add DMA counters for virtio-pci devices hw/s390x: Internal support for FMB passthrough
hw/s390x: Report FMB passthrough and/or FMB DMA counters to the guest
hw/s390x/s390-pci-bus.c | 17 +++- hw/s390x/s390-pci-inst.c | 65 ++++++++++++++- hw/s390x/s390-pci-vfio-stubs.c | 16 ++++ hw/s390x/s390-pci-vfio.c | 133 ++++++++++++++++++++++++++----- hw/virtio/virtio.c | 16 ++++ include/hw/pci/pci_device.h | 4 + include/hw/s390x/s390-pci-bus.h | 29 ++++++- include/hw/s390x/s390-pci-vfio.h | 4 + include/hw/virtio/virtio.h | 1 + linux-headers/linux/vfio.h | 43 ++++++++++ 10 files changed, 301 insertions(+), 27 deletions(-)
Signature · 1 line
2.52.0
RFC PATCH 1/5 linux-headers: Add definitions for VFIO zPCI FMB passthrough
This is a temporary patch intended for testing purposes only. It defines the new VFIO device feature for FMB passthrough on zPCI.
--- linux-headers/linux/vfio.h | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
linux-headers/linux/vfio.h +43 -0
@@ -1506,6 +1506,49 @@ struct vfio_device_feature_dma_buf { struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges); }; +/** +* Upon VFIO_DEVICE_FEATURE_GET, provide FMB passthrough for VFIO zPCI devices. +* +* Upon VFIO_DEVICE_FEATURE_SET, only the flags field is read while the +* remainder of the structure is ignored. This allows the driver to enable or +* disable the FMB while also leaving reserved bits for future flag expansion. +* All reserved fields should be zero for future compatibility. +*/ +#define VFIO_DEVICE_FEATURE_ZPCI_FMB 13 +#define VFIO_DEVICE_FEATURE_ZPCI_FMB_FLAGS_ENABLED 0x1 + +struct vfio_device_feature_zpci_fmb { +__u64 flags; +__u32 format:8 ; +__u32 fmt_ind:24 ; +__u32 samples; +__u64 last_update; +__u64 ld_ops; +__u64 st_ops; +__u64 stb_ops; +__u64 rpcit_ops; +union { +struct { +__u64 dma_rbytes; +__u64 dma_wbytes; + } fmt0; +struct { +__u64 rx_bytes; +__u64 rx_packets; +__u64 tx_bytes; +__u64 tx_packets; + } fmt1; +struct { +__u64 consumed_work_units; +__u64 max_work_units; + } fmt2; +struct { +__u64 tx_bytes; + } fmt3; + }; +__u64 reserved[16 ]; +}; +/* -------- API for Type1 VFIO IOMMU -------- */ /**
Signature · 1 line
2.52.0
RFC PATCH 2/5 hw/s390x: Clarified FMB format for emulated and passthrough devices
In reply to Omar ElghoulRenamed the default FMB format macro and added a comment explaining that it will be overwritten in the case of PCI passthrough devices when paired with a compatible kernel.
--- hw/s390x/s390-pci-bus.c | 8 +++++++- include/hw/s390x/s390-pci-bus.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-bus.c +7 -1
@@ -1486,7 +1486,13 @@ static void s390_pci_device_realize(DeviceState *dev, Error **errp) } zpci->state =ZPCI_FS_RESERVED ; - zpci->fmb.format =ZPCI_FMB_FORMAT ; + +/* +* For emulated devices, we use FMB format-0. For PCI passthrough +* devices paired with a compatible kernel, this will get overwritten +* by the VFIO-provided FMB. +*/ + zpci->fmb.format =ZPCI_FMB_DEFAULT_FORMAT ; }static void s390_pci_device_reset (DeviceState *dev)
include/hw/s390x/s390-pci-bus.h +1 -1
@@ -308,7 +308,7 @@ typedef struct ZpciFmbFmt0 {#define ZPCI_FMB_CNT_RPCIT 3#define ZPCI_FMB_CNT_MAX 4 -#define ZPCI_FMB_FORMAT 0 +#define ZPCI_FMB_DEFAULT_FORMAT 0 typedef struct ZpciFmb { uint32_t format;
Signature · 1 line
2.52.0
RFC PATCH 3/5 hw/virtio: Add DMA counters for virtio-pci devices
In reply to Omar ElghoulInternally track DMA read/write byte counters for virtio-pci devices by assigning them a DMA owner bus. This will allow an implementation of the FMB format-0 for zPCI. The assignment of an owner bus is also done from s390x-specific realization such that other architectures do not need to internally track unutilized counters.
--- hw/s390x/s390-pci-bus.c | 7 +++++++ hw/virtio/virtio.c | 16 ++++++++++++++++ include/hw/pci/pci_device.h | 4 ++++ include/hw/virtio/virtio.h | 1 + 4 files changed, 28 insertions(+)
hw/s390x/s390-pci-bus.c +7 -0
@@ -20,6 +20,7 @@#include "hw/s390x/s390-pci-kvm.h" #include "hw/s390x/s390-pci-vfio.h" #include "hw/s390x/s390-virtio-ccw.h" +#include "hw/virtio/virtio-pci.h" #include "hw/core/boards.h" #include "hw/pci/pci_bus.h" #include "hw/core/qdev-properties.h"
@@ -1192,6 +1193,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev, pbdev->rtr_avail = false; } +if (object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_PCI )) { +VirtIOPCIProxy *virtio_proxy =VIRTIO_PCI (pbdev->pdev); +VirtIODevice *vdev =virtio_bus_get_device (&virtio_proxy->bus); + vdev->dma_owner =OBJECT (virtio_proxy); + } +if (s390_pci_msix_init (pbdev) && !pbdev->interp) {error_setg (errp,"MSI-X support is mandatory " "in the S390 architecture" );
hw/virtio/virtio.c +16 -0
@@ -24,6 +24,7 @@#include "qom/object_interfaces.h" #include "hw/core/cpu.h" #include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-pci.h" #include "hw/virtio/vhost.h" #include "migration/qemu-file-types.h" #include "qemu/atomic.h"
@@ -1617,9 +1618,16 @@ static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg, hwaddr pa, size_t sz) { bool ok = false; +PCIDevice *pdev =NULL ;unsigned num_sg = *p_num_sg;assert (num_sg <= max_num_sg); +if (vdev->dma_owner && +object_dynamic_cast (vdev->dma_owner,TYPE_VIRTIO_PCI )) { +VirtIOPCIProxy *proxy =VIRTIO_PCI (vdev->dma_owner); + pdev = &proxy->pci_dev; + } +if (!sz) {virtio_error (vdev,"virtio: zero sized buffers are not allowed" ); goto out;
@@ -1650,6 +1658,14 @@ static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg, sz -= len; pa += len; num_sg++; + +if (pdev) { +if (is_write) { + pdev->dma_wbytes += len; + }else { + pdev->dma_rbytes += len; + } + } } ok = true;
include/hw/pci/pci_device.h +4 -0
@@ -98,6 +98,10 @@ struct PCIDevice {MemoryRegion bus_master_container_region;MemoryRegion bus_master_enable_region; +/* DMA statistics */ +uint64_t dma_rbytes; +uint64_t dma_wbytes; +/* do not access the following fields */ PCIConfigReadFunc *config_read;PCIConfigWriteFunc *config_write;
include/hw/virtio/virtio.h +1 -0
@@ -161,6 +161,7 @@ struct VirtIODevice*/ bool use_guest_notifier_mask;AddressSpace *dma_as; +Object *dma_owner; QLIST_HEAD(, VirtQueue) *vector_queues;QTAILQ_ENTRY (VirtIODevice) next;/**
Signature · 1 line
2.52.0
RFC PATCH 4/5 hw/s390x: Internal support for FMB passthrough
In reply to Omar ElghoulDetect the FMB passthrough at runtime by probing for the VFIO FMB device feature and setting a flag indicating its presence. If present, during an FMB update cycle, read the device feature and add the VFIO-provided counters to QEMU's intercepted statistics, to account for operations that may be run without interpretation.
If the device feature is not found, gracefully and silently fall back to QEMU's pre-existing intercepted FMB implementation.
Introduce helper functions to allow enabling or disabling the FMB of the host device. These functions will allow QEMU to cascade FMB state and guarantee that the counters restart from zero when the guest expects them to after a new FMB (re-)enablement.
Finally, remove the reduced aperture size for PCI ISM devices. This is necessary due to the kernel working around a firmware bug and incrementing the DMA limit by one full page when the PFT is reported correctly.
--- hw/s390x/s390-pci-bus.c | 2 + hw/s390x/s390-pci-inst.c | 7 ++ hw/s390x/s390-pci-vfio-stubs.c | 16 ++++ hw/s390x/s390-pci-vfio.c | 128 ++++++++++++++++++++++++++----- include/hw/s390x/s390-pci-bus.h | 27 ++++++- include/hw/s390x/s390-pci-vfio.h | 4 + 6 files changed, 165 insertions(+), 19 deletions(-)
hw/s390x/s390-pci-bus.c +2 -0
@@ -1185,6 +1185,8 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev, pbdev->shutdown_notifier.notify = s390_pci_shutdown_notifier;qemu_register_shutdown_notifier (&pbdev->shutdown_notifier); } +/* Detect VFIO FMB passthrough */ +s390_pci_get_fmb_info (pbdev); }else { pbdev->fh |=FH_SHM_EMUL ;/* Always intercept emulated devices */
hw/s390x/s390-pci-inst.c +7 -0
@@ -1313,9 +1313,16 @@ int mpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar,if (!fmb_addr) {/* Stop updating FMB. */ fmb_timer_free (pbdev); +if (pbdev->has_vfio_fmb) { +s390_pci_disable_vfio_fmb (pbdev); + }break ; } +if (pbdev->has_vfio_fmb) { +s390_pci_enable_vfio_fmb (pbdev); + } +if (!pbdev->fmb_timer) { pbdev->fmb_timer =timer_new_ms (QEMU_CLOCK_VIRTUAL , fmb_update, pbdev);
hw/s390x/s390-pci-vfio-stubs.c +16 -0
@@ -30,3 +30,19 @@ bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)void s390_pci_get_clp_info (S390PCIBusDevice *pbdev) { } + +void s390_pci_get_fmb_info (S390PCIBusDevice *pbdev) +{ +} + +void s390_pci_update_vfio_fmb (S390PCIBusDevice *pbdev) +{ +} + +void s390_pci_disable_vfio_fmb (S390PCIBusDevice *pbdev) +{ +} + +void s390_pci_enable_vfio_fmb (S390PCIBusDevice *pbdev) +{ +}
hw/s390x/s390-pci-vfio.c +110 -18
155 lines not on this page · raw message
include/hw/s390x/s390-pci-bus.h +26 -1
@@ -302,6 +302,22 @@ typedef struct ZpciFmbFmt0 {uint64_t dma_wbytes; } ZpciFmbFmt0; +typedef struct ZpciFmbFmt1 { +uint64_t rx_bytes; +uint64_t rx_packets; +uint64_t tx_bytes; +uint64_t tx_packets; +}ZpciFmbFmt1 ; + +typedef struct ZpciFmbFmt2 { +uint64_t consumed_work_units; +uint64_t max_work_units; +}ZpciFmbFmt2 ; + +typedef struct ZpciFmbFmt3 { +uint64_t tx_bytes; +}ZpciFmbFmt3 ; +#define ZPCI_FMB_CNT_LD 0#define ZPCI_FMB_CNT_ST 1#define ZPCI_FMB_CNT_STB 2
@@ -309,13 +325,21 @@ typedef struct ZpciFmbFmt0 {#define ZPCI_FMB_CNT_MAX 4#define ZPCI_FMB_DEFAULT_FORMAT 0 +#define ZPCI_FMB_COMMON_SIZE 48 +#define ZPCI_FMB_FORMAT (f) ((f >> 24) & 0xFF) +#define ZPCI_FMB_DMA_COUNTER_VALID (1 << 23)typedef struct ZpciFmb {uint32_t format;uint32_t sample;uint64_t last_update;uint64_t counter[ZPCI_FMB_CNT_MAX ]; -ZpciFmbFmt0 fmt0; +union { +ZpciFmbFmt0 fmt0; +ZpciFmbFmt1 fmt1; +ZpciFmbFmt2 fmt2; +ZpciFmbFmt3 fmt3; + }; }ZpciFmb ;QEMU_BUILD_BUG_MSG (offsetof(ZpciFmb , fmt0) !=48 ,"padding in ZpciFmb" );
@@ -364,6 +388,7 @@ struct S390PCIBusDevice {bool forwarding_assist;bool aif;bool rtr_avail; +bool has_vfio_fmb;QTAILQ_ENTRY (S390PCIBusDevice) link; };
include/hw/s390x/s390-pci-vfio.h +4 -0
@@ -20,5 +20,9 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,void s390_pci_end_dma_count (S390pciState *s,S390PCIDMACount *cnt);bool s390_pci_get_host_fh (S390PCIBusDevice *pbdev,uint32_t *fh);void s390_pci_get_clp_info (S390PCIBusDevice *pbdev); +void s390_pci_get_fmb_info (S390PCIBusDevice *pbdev); +void s390_pci_update_vfio_fmb (S390PCIBusDevice *pbdev); +void s390_pci_disable_vfio_fmb (S390PCIBusDevice *pbdev); +void s390_pci_enable_vfio_fmb (S390PCIBusDevice *pbdev);#endif
Signature · 1 line
2.52.0
RFC PATCH 5/5 hw/s390x: Report FMB passthrough and/or FMB DMA counters to the guest
In reply to Omar ElghoulReport the correct FMB length to the guest and report a more accurate snapshot of the FMB using the VFIO feature in the PCI passthrough case or the emulated counters in the emulated case. To preserve the behavior of earlier QEMU versions, do not report the PFT or the FMB length to the guest in the case where FMB passthrough is unavailable.
--- hw/s390x/s390-pci-inst.c | 58 +++++++++++++++++++++++++++++++++++++--- hw/s390x/s390-pci-vfio.c | 5 ++-- 2 files changed, 57 insertions(+), 6 deletions(-)
hw/s390x/s390-pci-inst.c +55 -3
@@ -20,6 +20,7 @@#include "system/hw_accel.h" #include "hw/core/boards.h" #include "hw/pci/pci_device.h" +#include "hw/vfio/pci.h" #include "hw/s390x/s390-pci-inst.h" #include "hw/s390x/s390-pci-bus.h" #include "hw/s390x/s390-pci-kvm.h"
@@ -296,8 +297,13 @@ int clp_service_call(S390CPU *cpu, uint8_t r2, uintptr_t ra)stw_be_p (&resquery->vfn, pbdev->zpci_fn.vfn); resquery->flags = pbdev->zpci_fn.flags; resquery->pfgid = pbdev->zpci_fn.pfgid; - resquery->pft = pbdev->zpci_fn.pft; - resquery->fmbl = pbdev->zpci_fn.fmbl; +if (pbdev->has_vfio_fmb) { + resquery->pft = pbdev->zpci_fn.pft; + resquery->fmbl = pbdev->zpci_fn.fmbl >>2 ; + }else { + resquery->pft =0 ; + resquery->fmbl =0 ; + }stl_be_p (&resquery->fid, pbdev->zpci_fn.fid);stl_be_p (&resquery->uid, pbdev->zpci_fn.uid);memcpy (resquery->pfip, pbdev->zpci_fn.pfip,CLP_PFIP_NR_SEGMENTS );
@@ -1096,6 +1102,9 @@ static void fmb_update(void *opaque) {S390PCIBusDevice *pbdev = opaque;int64_t t =qemu_clock_get_ms (QEMU_CLOCK_VIRTUAL ); +VFIOPCIDevice *vpdev; +PCIDevice *pci = pbdev->pdev; +uint8_t format =ZPCI_FMB_FORMAT (pbdev->fmb.format);int i;/* Update U bit */
@@ -1107,6 +1116,48 @@ static void fmb_update(void *opaque)return ; } +/* Check for VFIO device */ + vpdev = (VFIOPCIDevice *)object_dynamic_cast (OBJECT (pbdev->pdev), +TYPE_VFIO_PCI_DEVICE ); + +if (vpdev && pbdev->has_vfio_fmb) { +s390_pci_update_vfio_fmb (pbdev); + goto update_timer; + } + +/* Update FMB format-specific counters */ +switch (format) { +case 0 : +if (pci->dma_rbytes || pci->dma_wbytes) { + pbdev->fmb.fmt0.dma_rbytes = pci->dma_rbytes; + pbdev->fmb.fmt0.dma_wbytes = pci->dma_wbytes; + pbdev->fmb.format |=ZPCI_FMB_DMA_COUNTER_VALID ; + } + +if (fmb_do_update (pbdev, + offsetof(ZpciFmbFmt0 , dma_rbytes) + offsetof(ZpciFmb , fmt0), + pbdev->fmb.fmt0.dma_rbytes, +sizeof (pbdev->fmb.fmt0.dma_rbytes))) { +return ; + } + +if (fmb_do_update (pbdev, + offsetof(ZpciFmbFmt0 , dma_wbytes) + offsetof(ZpciFmb , fmt0), + pbdev->fmb.fmt0.dma_wbytes, +sizeof (pbdev->fmb.fmt0.dma_wbytes))) { +return ; + } +default : +break ; + } + +/* Update format and DMA validity bit */ +if (fmb_do_update (pbdev, offsetof(ZpciFmb , format), + pbdev->fmb.format, +sizeof (pbdev->fmb.format))) { +return ; + } +/* Update FMB sample count */ if (fmb_do_update(pbdev, offsetof(ZpciFmb, sample), pbdev->fmb.sample++,
@@ -1114,7 +1165,7 @@ static void fmb_update(void *opaque)return ; } -/* Update FMB counters */ +/* Update FMB generic counters */ for (i =0 ; i <ZPCI_FMB_CNT_MAX ; i++) { if (fmb_do_update(pbdev, offsetof(ZpciFmb, counter[i]), pbdev->fmb.counter[i],
@@ -1123,6 +1174,7 @@ static void fmb_update(void *opaque) } } +update_timer:/* Clear U bit and update the time */ pbdev->fmb.last_update =time2tod (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL )); pbdev->fmb.last_update *=2 ;
hw/s390x/s390-pci-vfio.c +2 -3
@@ -127,9 +127,8 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev, pbdev->zpci_fn.pchid = cap->pchid; pbdev->zpci_fn.vfn = cap->vfn; pbdev->zpci_fn.pfgid = cap->gid; -/* The following values remain 0 until we support other FMB formats */ - pbdev->zpci_fn.fmbl =0 ; - pbdev->zpci_fn.pft =0 ; + pbdev->zpci_fn.fmbl = cap->fmb_length; + pbdev->zpci_fn.pft = cap->pft;/* Store function type separately for type-specific behavior */ pbdev->pft = cap->pft;
Signature · 1 line
2.52.0
Am 01.05.26 um 22:00 schrieb Omar Elghoul:
Quoted 27 lines
Hi,
This patch series improves support for function measurement on s390x for zPCI passthrough devices. It is dependent on the eponymous kernel patch series linked below. [1]
Motivation
==========
The firmware on s390x machines allows for tracking a variety of statistics relating to zPCI devices in a function measurement block (FMB). At the time of writing, the kernel does not provide QEMU with a structured mechanism to fetch the FMB of passthrough devices. QEMU also does not report the PFT to the guest, which can cause issues with tools such as smc-rnics that depend on the PFT to detect ISM and RoCE devices. Furthermore, the FMB itself can take on different formats depending on the PFT, while QEMU's implementation is limited to only the generic section that prefixes the format-specific structures.Proposal
========
The aforementioned kernel series proposes a VFIO feature that allows QEMU to read the latest snapshot of the FMB. In this patch series, we propose using that mechanism to share the FMB with the guest, as seen from the host. We also inform the guest of the PFT and the FMB length as detected from the host's CLP. Furthermore, we implement the format-0 FMB (DMA counters) for virtio-pci devices.
Quoted 5 lines
I'd appreciate any comments on this approach.
Thanks in advance.
CCing Niklas and Gerd for their Linux zpci expertise.
Quoted 21 lines
Omar Elghoul (5):
[do-not-merge] linux-headers: Add definitions for VFIO zPCI FMB
passthrough
hw/s390x: Clarified FMB format for emulated and passthrough devices hw/virtio: Add DMA counters for virtio-pci devices hw/s390x: Internal support for FMB passthrough
hw/s390x: Report FMB passthrough and/or FMB DMA counters to the guesthw/s390x/s390-pci-bus.c | 17 +++- hw/s390x/s390-pci-inst.c | 65 ++++++++++++++- hw/s390x/s390-pci-vfio-stubs.c | 16 ++++ hw/s390x/s390-pci-vfio.c | 133 ++++++++++++++++++++++++++----- hw/virtio/virtio.c | 16 ++++ include/hw/pci/pci_device.h | 4 + include/hw/s390x/s390-pci-bus.h | 29 ++++++- include/hw/s390x/s390-pci-vfio.h | 4 + include/hw/virtio/virtio.h | 1 + linux-headers/linux/vfio.h | 43 ++++++++++ 10 files changed, 301 insertions(+), 27 deletions(-)