vfio/pci: Atomic Ops completer support
v2 cover vfio/pci: Atomic Ops completer support
This RFC proposes to allow a vfio-pci device to manipulate the PCI Express capability of an associated root port to enable Atomic Op completer support as equivalent to host capabilities. This would dynamically change capability bits in the config space of the root port on realize and exit of the vfio-pci device under the following condiations:
- The vfio-pci device is directly connected to the root port and the root port implements a v2 PCIe capability, thereby supporting the DEVCAP2 register.
- The vfio-pci device is exposed as a single-function device.
- Atomic completer support is not otherwise reported on the root port.
- The vfio-pci device reports the VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP capability with a non-zero set of supported atomic completer widths.
This proposal aims to avoid complications of reporting Atomic Ops Routing, which can easily escalate into invalid P2P paths. We also require a specific VM configuration to avoid dependencies between devices which may be sourced from dissimilar host paths.
While it's not exactly standard practice to modify root port device capabilities runtime, it also does not seem to be precluded by the PCIe Spec (6.0.1). The Atomic Op completion bits of the DEVCAP2 register are defined as Read-only:
7.4 Configuration Register Types
Read-only - Register bits are read-only and cannot be altered by software.
Where explicitly defined, these bits are used to reflect changing hardware state, and as a result bit values can be observed to change at run time. Register bit default values and bits that cannot change value at run time, are permitted to be hard-coded, initialized by system/device firmware, or initialized by hardware mechanisms such as pin strapping or nonvolatile storage. Initialization by system firmware is permitted only for system- integrated devices. If the optional feature that would Set the bits is not implemented, the bits must be hardwired to Zero.
Here "altered by software" is relative to guest writes to the config space register, whereas in this implementation we're acting as hardware and the bits are changing to reflect a change in runtime capabilities. The spec does include a HwInit register type which would restrict the value from changing at runtime outside of resets. Therefore while it would not be advised to update these bits arbitrarily, it does seem safe and compatible with guest software to update the value on device attach and detach.
Note that of the Linux in-kernel drivers that make use of Atomic Ops, it's not common for the driver to test Atomic Ops support of the device itself. Support is assumed, therefore it's fruitless to provide masking of support at the device rather than the root port.
Also, by allowing this dynamic support, enabling Atomic Ops becomes transparent to VM management tools. There is no requirement to designate specific Atomic Ops capabilities to a root port and impose a burden on other userspace utilities.
Feedback welcome. Thanks,
Alex
v2:
- Don't require cold-plug device, modify RP bits around realize/exit
Alex Williamson (4):
linux-headers: Update for vfio capability reporting AtomicOps
vfio: Implement a common device info helper
pcie: Add a PCIe capability version helper vfio/pci: Enable AtomicOps completers on root ports
hw/pci/pcie.c | 7 ++++ hw/s390x/s390-pci-vfio.c | 37 +++-------------- hw/vfio/common.c | 46 ++++++++++++++++----- hw/vfio/pci.c | 78 +++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + include/hw/pci/pcie.h | 1 + include/hw/vfio/vfio-common.h | 1 + linux-headers/linux/vfio.h | 14 +++++++ 8 files changed, 142 insertions(+), 43 deletions(-)
Signature · 1 line
2.39.2
RFC PATCH v2 1/4 linux-headers: Update for vfio capability reporting AtomicOps
This is a partial linux-headers update for illustrative and testing purposes only, NOT FOR COMMIT.
--- linux-headers/linux/vfio.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
linux-headers/linux/vfio.h +14 -0
@@ -240,6 +240,20 @@ struct vfio_device_info {#define VFIO_DEVICE_INFO_CAP_ZPCI_UTIL 3#define VFIO_DEVICE_INFO_CAP_ZPCI_PFIP 4 +/* +* The following VFIO_DEVICE_INFO capability reports support for PCIe AtomicOp +* completion to the root bus with supported widths provided via flags. +*/ +#define VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP 5 +struct vfio_device_info_cap_pci_atomic_comp { +struct vfio_info_cap_header header; +__u32 flags; +#define VFIO_PCI_ATOMIC_COMP32 (1 << 0) +#define VFIO_PCI_ATOMIC_COMP64 (1 << 1) +#define VFIO_PCI_ATOMIC_COMP128 (1 << 2) +__u32 reserved; +}; +/** * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8, * struct vfio_region_info)
Signature · 1 line
2.39.2
Re: [RFC PATCH v2 1/4] linux-headers: Update for vfio capability reporting AtomicOps
Quoted 2 lines · Alex Williamson
This is a partial linux-headers update for illustrative and testing purposes only, NOT FOR COMMIT.
Are you planing such an update for v6.5 ?
Thanks,
C.
Quoted 31 lines
Signed-off-by Alex Williamson <[email protected]>--- linux-headers/linux/vfio.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)linux-headers/linux/vfio.h +14
-0@@ -240,6 +240,20 @@ struct vfio_device_info {#define VFIO_DEVICE_INFO_CAP_ZPCI_UTIL 3#define VFIO_DEVICE_INFO_CAP_ZPCI_PFIP 4 +/* +* The following VFIO_DEVICE_INFO capability reports support for PCIe AtomicOp +* completion to the root bus with supported widths provided via flags. +*/ +#define VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP 5 +struct vfio_device_info_cap_pci_atomic_comp { +struct vfio_info_cap_header header; +__u32 flags; +#define VFIO_PCI_ATOMIC_COMP32 (1 << 0) +#define VFIO_PCI_ATOMIC_COMP64 (1 << 1) +#define VFIO_PCI_ATOMIC_COMP128 (1 << 2) +__u32 reserved; +}; +/** * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8, * struct vfio_region_info)
RFC PATCH v2 2/4 vfio: Implement a common device info helper
In reply to Alex WilliamsonA common helper implementing the realloc algorithm for handling capabilities.
--- hw/s390x/s390-pci-vfio.c | 37 ++++------------------------ hw/vfio/common.c | 46 ++++++++++++++++++++++++++--------- include/hw/vfio/vfio-common.h | 1 + 3 files changed, 41 insertions(+), 43 deletions(-)
hw/s390x/s390-pci-vfio.c +5 -32
@@ -289,38 +289,11 @@ static void s390_pci_read_pfip(S390PCIBusDevice *pbdev,memcpy (pbdev->zpci_fn.pfip, cap->pfip,CLP_PFIP_NR_SEGMENTS ); } -static struct vfio_device_info *get_device_info (S390PCIBusDevice *pbdev, -uint32_t argsz) +static struct vfio_device_info *get_device_info (S390PCIBusDevice *pbdev); { -struct vfio_device_info *info =g_malloc0 (argsz); -VFIOPCIDevice *vfio_pci; -int fd; +VFIOPCIDevice *vfio_pci =container_of (pbdev->pdev, VFIOPCIDevice, pdev); - vfio_pci =container_of (pbdev->pdev, VFIOPCIDevice, pdev); - fd = vfio_pci->vbasedev.fd; - -/* -* If the specified argsz is not large enough to contain all capabilities -* it will be updated upon return from the ioctl. Retry until we have -* a big enough buffer to hold the entire capability chain. On error, -* just exit and rely on CLP defaults. -*/ -retry: - info->argsz = argsz; - -if (ioctl (fd,VFIO_DEVICE_GET_INFO , info)) { -trace_s390_pci_clp_dev_info (vfio_pci->vbasedev.name); -g_free (info); -return NULL ; - } - -if (info->argsz > argsz) { - argsz = info->argsz; - info =g_realloc (info, argsz); - goto retry; - } - -return info; +return vfio_get_device_info (vfio_pci->vbasedev.fd); }/*
@@ -335,7 +308,7 @@ bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)assert (fh); - info =get_device_info (pbdev,sizeof (*info)); + info =get_device_info (pbdev);if (!info) {return false; }
@@ -356,7 +329,7 @@ void s390_pci_get_clp_info(S390PCIBusDevice *pbdev) { g_autofree struct vfio_device_info *info = NULL; - info =get_device_info (pbdev,sizeof (*info)); + info =get_device_info (pbdev);if (!info) {return ; }
hw/vfio/common.c +35 -11
71 lines not on this page · raw message
include/hw/vfio/vfio-common.h +1 -0
@@ -212,6 +212,7 @@ void vfio_region_finalize(VFIORegion *region);void vfio_reset_handler (void *opaque);VFIOGroup *vfio_get_group (int groupid,AddressSpace *as,Error **errp);void vfio_put_group (VFIOGroup *group); +struct vfio_device_info *vfio_get_device_info (int fd);int vfio_get_device (VFIOGroup *group,const char *name,VFIODevice *vbasedev,Error **errp);
Signature · 1 line
2.39.2
Re: [RFC PATCH v2 2/4] vfio: Implement a common device info helper
hw/s390x/s390-pci-vfio.c static void s390_pci_read_pfip(S390PCIBusDevice *pbdev, · line 293
Quoted 4 lines · Alex Williamson
hw/s390x/s390-pci-vfio.c +1
-2-static struct vfio_device_info *get_device_info (S390PCIBusDevice *pbdev, -uint32_t argsz) +static struct vfio_device_info *get_device_info (S390PCIBusDevice *pbdev); {
There is an extraneous semicolon here behind the function declaration that should be removed.
Kind regards,
Robin Voetter, Stream HPC
RFC PATCH v2 3/4 pcie: Add a PCIe capability version helper
In reply to Alex WilliamsonReport the PCIe capability version for a device
--- hw/pci/pcie.c | 7 +++++++ include/hw/pci/pcie.h | 1 + 2 files changed, 8 insertions(+)
hw/pci/pcie.c +7 -0
@@ -274,6 +274,13 @@ uint8_t pcie_cap_get_type(const PCIDevice *dev) PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT; } +uint8_t pcie_cap_get_version (const PCIDevice *dev) +{ +uint32_t pos = dev->exp.exp_cap; +assert (pos >0 ); +return pci_get_word (dev->config + pos +PCI_EXP_FLAGS ) &PCI_EXP_FLAGS_VERS ; +} +/* MSI/MSI-X */ /* pci express interrupt message number */ /* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
include/hw/pci/pcie.h +1 -0
@@ -93,6 +93,7 @@ void pcie_cap_exit(PCIDevice *dev);int pcie_endpoint_cap_v1_init (PCIDevice *dev,uint8_t offset);void pcie_cap_v1_exit (PCIDevice *dev);uint8_t pcie_cap_get_type (const PCIDevice *dev); +uint8_t pcie_cap_get_version (const PCIDevice *dev);void pcie_cap_flags_set_vector (PCIDevice *dev,uint8_t vector);uint8_t pcie_cap_flags_get_vector (PCIDevice *dev);
Signature · 1 line
2.39.2
Re: [RFC PATCH v2 3/4] pcie: Add a PCIe capability version helper
Quoted 3 lines · Alex Williamson
Report the PCIe capability version for a device
Signed-off-by Alex Williamson <[email protected]>
Thanks,
C.
Quoted 35 lines
--- hw/pci/pcie.c | 7 +++++++ include/hw/pci/pcie.h | 1 + 2 files changed, 8 insertions(+)hw/pci/pcie.c +7
-0@@ -274,6 +274,13 @@ uint8_t pcie_cap_get_type(const PCIDevice *dev) PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT; } +uint8_t pcie_cap_get_version (const PCIDevice *dev) +{ +uint32_t pos = dev->exp.exp_cap; +assert (pos >0 ); +return pci_get_word (dev->config + pos +PCI_EXP_FLAGS ) &PCI_EXP_FLAGS_VERS ; +} +/* MSI/MSI-X */ /* pci express interrupt message number */ /* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */ include/hw/pci/pcie.h +1
-0@@ -93,6 +93,7 @@ void pcie_cap_exit(PCIDevice *dev);int pcie_endpoint_cap_v1_init (PCIDevice *dev,uint8_t offset);void pcie_cap_v1_exit (PCIDevice *dev);uint8_t pcie_cap_get_type (const PCIDevice *dev); +uint8_t pcie_cap_get_version (const PCIDevice *dev);void pcie_cap_flags_set_vector (PCIDevice *dev,uint8_t vector);uint8_t pcie_cap_flags_get_vector (PCIDevice *dev);
Re: [RFC PATCH v2 3/4] pcie: Add a PCIe capability version helper
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
Report the PCIe capability version for a device
Kind regards,
Robin Voetter
Re: [RFC PATCH v2 3/4] pcie: Add a PCIe capability version helper
Quoted 4 lines · Robin Voetter
Quoted 3 lines · Alex Williamson
Report the PCIe capability version for a device
Signed-off-by Alex Williamson <[email protected]>
Quoted 5 lines
Kind regards,
Robin Voetter
RFC PATCH v2 4/4 vfio/pci: Enable AtomicOps completers on root ports
In reply to Alex WilliamsonDynamically enable Atomic Ops completer support around realize/exit of vfio-pci devices reporting host support for these accesses and adhering to a minimal configuration standard. While the Atomic Ops completer bits in the root port device capabilities2 register are read-only, the PCIe spec does allow RO bits to change to reflect hardware state. We take advantage of that here around the realize and exit functions of the vfio-pci device.
--- hw/vfio/pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + 2 files changed, 79 insertions(+)
hw/vfio/pci.c +78 -0
@@ -1826,6 +1826,81 @@ static void vfio_add_emulated_long(VFIOPCIDevice *vdev, int pos,vfio_set_long_bits (vdev->emulated_config_bits + pos, mask, mask); } +static void vfio_pci_enable_rp_atomics (VFIOPCIDevice *vdev) +{ +struct vfio_device_info_cap_pci_atomic_comp *cap; + g_autofree struct vfio_device_info *info = NULL; +PCIBus *bus =pci_get_bus (&vdev->pdev); +PCIDevice *parent = bus->parent_dev; +struct vfio_info_cap_header *hdr; +uint32_t mask =0 ; +uint8_t *pos; + +/* +* PCIe Atomic Ops completer support is only added automatically for single +* function devices downstream of a root port supporting DEVCAP2. Support +* is added during realize and, if added, removed during device exit. The +* single function requirement avoids conflicting requirements should a +* slot be composed of multiple devices with differing capabilities. +*/ +if (pci_bus_is_root (bus) || !parent || !parent->exp.exp_cap || +pcie_cap_get_type (parent) !=PCI_EXP_TYPE_ROOT_PORT || +pcie_cap_get_version (parent) !=PCI_EXP_FLAGS_VER2 || + vdev->pdev.devfn || + vdev->pdev.cap_present &QEMU_PCI_CAP_MULTIFUNCTION ) { +return ; + } + + pos = parent->config + parent->exp.exp_cap +PCI_EXP_DEVCAP2 ; + +/* Abort if there'a already an Atomic Ops configuration on the root port */ +if (pci_get_long (pos) & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | +PCI_EXP_DEVCAP2_ATOMIC_COMP64 | +PCI_EXP_DEVCAP2_ATOMIC_COMP128 )) { +return ; + } + + info =vfio_get_device_info (vdev->vbasedev.fd); +if (!info) { +return ; + } + + hdr =vfio_get_device_info_cap (info,VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP ); +if (!hdr) { +return ; + } + + cap = (void *)hdr; +if (cap->flags &VFIO_PCI_ATOMIC_COMP32 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP32 ; + } +if (cap->flags &VFIO_PCI_ATOMIC_COMP64 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP64 ; + } +if (cap->flags &VFIO_PCI_ATOMIC_COMP128 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP128 ; + } + +if (!mask) { +return ; + } + +pci_long_test_and_set_mask (pos, mask); + vdev->clear_parent_atomics_on_exit = true; +} + +static void vfio_pci_disable_rp_atomics (VFIOPCIDevice *vdev) +{ +if (vdev->clear_parent_atomics_on_exit) { +PCIDevice *parent =pci_get_bus (&vdev->pdev)->parent_dev; +uint8_t *pos = parent->config + parent->exp.exp_cap +PCI_EXP_DEVCAP2 ; + +pci_long_test_and_clear_mask (pos,PCI_EXP_DEVCAP2_ATOMIC_COMP32 | +PCI_EXP_DEVCAP2_ATOMIC_COMP64 | +PCI_EXP_DEVCAP2_ATOMIC_COMP128 ); + } +} +static int vfio_setup_pcie_cap (VFIOPCIDevice *vdev,int pos,uint8_t size,Error **errp) {
@@ -1929,6 +2004,8 @@ static int vfio_setup_pcie_cap(VFIOPCIDevice *vdev, int pos, uint8_t size, QEMU_PCI_EXP_LNKCAP_MLS(QEMU_PCI_EXP_LNK_2_5GT), ~0);vfio_add_emulated_word (vdev, pos +PCI_EXP_LNKCTL ,0 , ~0 ); } + +vfio_pci_enable_rp_atomics (vdev); }/*
@@ -3265,6 +3342,7 @@ static void vfio_exitfn(PCIDevice *pdev)timer_free (vdev->intx.mmap_timer); }vfio_teardown_msi (vdev); +vfio_pci_disable_rp_atomics (vdev);vfio_bars_exit (vdev);vfio_migration_exit (&vdev->vbasedev); }
hw/vfio/pci.h +1 -0
@@ -174,6 +174,7 @@ struct VFIOPCIDevice {bool no_vfio_ioeventfd;bool enable_ramfb;bool defer_kvm_irq_routing; +bool clear_parent_atomics_on_exit;VFIODisplay *dpy;Notifier irqchip_change_notifier; };
Signature · 1 line
2.39.2
Re: [RFC PATCH v2 4/4] vfio/pci: Enable AtomicOps completers on root ports
Quoted 9 lines · Alex Williamson
Dynamically enable Atomic Ops completer support around realize/exit of vfio-pci devices reporting host support for these accesses and adhering to a minimal configuration standard. While the Atomic Ops completer bits in the root port device capabilities2 register are read-only, the PCIe spec does allow RO bits to change to reflect hardware state. We take advantage of that here around the realize and exit functions of the vfio-pci device.
Signed-off-by Alex Williamson <[email protected]>
LGTM. I am not sure about the single function restriction, may be that's worth a warning ?
Thanks,
C.
Quoted 120 lines
--- hw/vfio/pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + 2 files changed, 79 insertions(+)hw/vfio/pci.c +78
-0@@ -1826,6 +1826,81 @@ static void vfio_add_emulated_long(VFIOPCIDevice *vdev, int pos,vfio_set_long_bits (vdev->emulated_config_bits + pos, mask, mask); } +static void vfio_pci_enable_rp_atomics (VFIOPCIDevice *vdev) +{ +struct vfio_device_info_cap_pci_atomic_comp *cap; + g_autofree struct vfio_device_info *info = NULL; +PCIBus *bus =pci_get_bus (&vdev->pdev); +PCIDevice *parent = bus->parent_dev; +struct vfio_info_cap_header *hdr; +uint32_t mask =0 ; +uint8_t *pos; + +/* +* PCIe Atomic Ops completer support is only added automatically for single +* function devices downstream of a root port supporting DEVCAP2. Support +* is added during realize and, if added, removed during device exit. The +* single function requirement avoids conflicting requirements should a +* slot be composed of multiple devices with differing capabilities. +*/ +if (pci_bus_is_root (bus) || !parent || !parent->exp.exp_cap || +pcie_cap_get_type (parent) !=PCI_EXP_TYPE_ROOT_PORT || +pcie_cap_get_version (parent) !=PCI_EXP_FLAGS_VER2 || + vdev->pdev.devfn || + vdev->pdev.cap_present &QEMU_PCI_CAP_MULTIFUNCTION ) { +return ; + } + + pos = parent->config + parent->exp.exp_cap +PCI_EXP_DEVCAP2 ; + +/* Abort if there'a already an Atomic Ops configuration on the root port */ +if (pci_get_long (pos) & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | +PCI_EXP_DEVCAP2_ATOMIC_COMP64 | +PCI_EXP_DEVCAP2_ATOMIC_COMP128 )) { +return ; + } + + info =vfio_get_device_info (vdev->vbasedev.fd); +if (!info) { +return ; + } + + hdr =vfio_get_device_info_cap (info,VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP ); +if (!hdr) { +return ; + } + + cap = (void *)hdr; +if (cap->flags &VFIO_PCI_ATOMIC_COMP32 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP32 ; + } +if (cap->flags &VFIO_PCI_ATOMIC_COMP64 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP64 ; + } +if (cap->flags &VFIO_PCI_ATOMIC_COMP128 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP128 ; + } + +if (!mask) { +return ; + } + +pci_long_test_and_set_mask (pos, mask); + vdev->clear_parent_atomics_on_exit = true; +} + +static void vfio_pci_disable_rp_atomics (VFIOPCIDevice *vdev) +{ +if (vdev->clear_parent_atomics_on_exit) { +PCIDevice *parent =pci_get_bus (&vdev->pdev)->parent_dev; +uint8_t *pos = parent->config + parent->exp.exp_cap +PCI_EXP_DEVCAP2 ; + +pci_long_test_and_clear_mask (pos,PCI_EXP_DEVCAP2_ATOMIC_COMP32 | +PCI_EXP_DEVCAP2_ATOMIC_COMP64 | +PCI_EXP_DEVCAP2_ATOMIC_COMP128 ); + } +} +static int vfio_setup_pcie_cap (VFIOPCIDevice *vdev,int pos,uint8_t size,Error **errp) {@@ -1929,6 +2004,8 @@ static int vfio_setup_pcie_cap(VFIOPCIDevice *vdev, int pos, uint8_t size, QEMU_PCI_EXP_LNKCAP_MLS(QEMU_PCI_EXP_LNK_2_5GT), ~0);vfio_add_emulated_word (vdev, pos +PCI_EXP_LNKCTL ,0 , ~0 ); } + +vfio_pci_enable_rp_atomics (vdev); }/* @@ -3265,6 +3342,7 @@ static void vfio_exitfn(PCIDevice *pdev)timer_free (vdev->intx.mmap_timer); }vfio_teardown_msi (vdev); +vfio_pci_disable_rp_atomics (vdev);vfio_bars_exit (vdev);vfio_migration_exit (&vdev->vbasedev); }hw/vfio/pci.h +1
-0@@ -174,6 +174,7 @@ struct VFIOPCIDevice {bool no_vfio_ioeventfd;bool enable_ramfb;bool defer_kvm_irq_routing; +bool clear_parent_atomics_on_exit;VFIODisplay *dpy;Notifier irqchip_change_notifier; };
Re: [RFC PATCH v2 4/4] vfio/pci: Enable AtomicOps completers on root ports
In reply to Alex WilliamsonQuoted 9 lines · Alex Williamson
Dynamically enable Atomic Ops completer support around realize/exit of vfio-pci devices reporting host support for these accesses and adhering to a minimal configuration standard. While the Atomic Ops completer bits in the root port device capabilities2 register are read-only, the PCIe spec does allow RO bits to change to reflect hardware state. We take advantage of that here around the realize and exit functions of the vfio-pci device.
Signed-off-by Alex Williamson <[email protected]>
Kind regards,
Robin Voetter
Re: [RFC PATCH v2 4/4] vfio/pci: Enable AtomicOps completers on root ports
In reply to Alex WilliamsonQuoted 33 lines · Alex Williamson
Dynamically enable Atomic Ops completer support around realize/exit of vfio-pci devices reporting host support for these accesses and adhering to a minimal configuration standard. While the Atomic Ops completer bits in the root port device capabilities2 register are read-only, the PCIe spec does allow RO bits to change to reflect hardware state. We take advantage of that here around the realize and exit functions of the vfio-pci device.
Signed-off-by Alex Williamson <[email protected]>--- hw/vfio/pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + 2 files changed, 79 insertions(+)hw/vfio/pci.c +18
-0+static void vfio_pci_enable_rp_atomics (VFIOPCIDevice *vdev) +{ +struct vfio_device_info_cap_pci_atomic_comp *cap; + g_autofree struct vfio_device_info *info = NULL; +PCIBus *bus =pci_get_bus (&vdev->pdev); +PCIDevice *parent = bus->parent_dev; +struct vfio_info_cap_header *hdr; +uint32_t mask =0 ; +uint8_t *pos; + +/* +* PCIe Atomic Ops completer support is only added automatically for single +* function devices downstream of a root port supporting DEVCAP2. Support +* is added during realize and, if added, removed during device exit. The +* single function requirement avoids conflicting requirements should a +* slot be composed of multiple devices with differing capabilities. +*/ + if (pci_bus_is_root(bus) || !parent || !parent->exp.exp_cap ||
hw/vfio/pci.c static void vfio_add_emulated_long(VFIOPCIDevice *vdev, int pos, · line 1856
Quoted 10 lines
hw/vfio/pci.c +10
-0+pcie_cap_get_type (parent) !=PCI_EXP_TYPE_ROOT_PORT || +pcie_cap_get_version (parent) !=PCI_EXP_FLAGS_VER2 || + vdev->pdev.devfn || + vdev->pdev.cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { + return; + } + + pos = parent->config + parent->exp.exp_cap +PCI_EXP_DEVCAP2 ; + +/* Abort if there'a already an Atomic Ops configuration on the root port */
Optional here: trace event logging pci_get_long(pos).
Quoted 20 lines
hw/vfio/pci.c +20
-0+if (pci_get_long (pos) & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | +PCI_EXP_DEVCAP2_ATOMIC_COMP64 | +PCI_EXP_DEVCAP2_ATOMIC_COMP128 )) { +return ; + } + + info =vfio_get_device_info (vdev->vbasedev.fd); +if (!info) { +return ; + } + + hdr =vfio_get_device_info_cap (info,VFIO_DEVICE_INFO_CAP_PCI_ATOMIC_COMP ); +if (!hdr) { +return ; + } + + cap = (void *)hdr; +if (cap->flags &VFIO_PCI_ATOMIC_COMP32 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP32 ; + }
hw/vfio/pci.c static void vfio_add_emulated_long(VFIOPCIDevice *vdev, int pos, · line 1886
Quoted 10 lines
hw/vfio/pci.c +10
-0+if (cap->flags &VFIO_PCI_ATOMIC_COMP64 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP64 ; + } +if (cap->flags &VFIO_PCI_ATOMIC_COMP128 ) { + mask |=PCI_EXP_DEVCAP2_ATOMIC_COMP128 ; + } + +if (!mask) { +return ; + }
Similarly optional, trace event logging (cap->flags, mask).
Quoted 6 lines
hw/vfio/pci.c +6
-0+ +pci_long_test_and_set_mask (pos, mask); + vdev->clear_parent_atomics_on_exit = true; +} + +static void vfio_pci_disable_rp_atomics (VFIOPCIDevice *vdev)
hw/vfio/pci.c static void vfio_add_emulated_long(VFIOPCIDevice *vdev, int pos, · line 1902
Quoted 10 lines
hw/vfio/pci.c +10
-0+{ +if (vdev->clear_parent_atomics_on_exit) { +PCIDevice *parent =pci_get_bus (&vdev->pdev)->parent_dev; +uint8_t *pos = parent->config + parent->exp.exp_cap +PCI_EXP_DEVCAP2 ; + +pci_long_test_and_clear_mask (pos,PCI_EXP_DEVCAP2_ATOMIC_COMP32 | +PCI_EXP_DEVCAP2_ATOMIC_COMP64 | +PCI_EXP_DEVCAP2_ATOMIC_COMP128 ); + } +}
Hi Alex,
Thanks for taking the time to implement support for Atomic Op completer support properly :). I have tested out these patches and the kernel patch, and apart from a minor issue with patch 2 everything works fine; ROCm programs that use device->host atomic operations work properly.
Something that I have been thinking about, are there any implications involved with enabling this feature automatically with no possibility of turning it off? I have no use case for that, though, and I cant really think of a reason other than preventing the guest from finding out hardware details about the host.
Thanks,
Robin Voetter, Stream HPC
Quoted 5 lines · On Wed, 31 May 2023 23:55:41 +0200 Robin Voetter <[email protected]> wrote:
Hi Alex,
Thanks for taking the time to implement support for Atomic Op completer support properly :). I have tested out these patches and the kernel patch, and apart from a minor issue with patch 2 everything works fine;
Yes, Cedric noted the extra semicolon as well, I'm about to post that patch as non-RFC since it has no dependencies on anything else.
Quoted 1 line
ROCm programs that use
device->hostatomic operations work properly.
Great, thanks for testing!
Quoted 5 lines
Something that I have been thinking about, are there any implications involved with enabling this feature automatically with no possibility of turning it off? I have no use case for that, though, and I cant really think of a reason other than preventing the guest from finding out hardware details about the host.
Not sure I follow, are you suggesting that Atomic Ops completion support is proactively enabled in the VM to match the host, regardless of attached assigned devices? An obvious problem there is migration. If I start a VM on a host with Atomic Ops support and want to migrate to a host without Atomic Ops support, config space of the root ports is now different and the migration fails. QEMU would also require elevated privileges to read config space to determine the host support, and then what does it do if only some of the PCIe hierarchy supports Atomic Ops.
Policy decisions like that are generally left to management tools, so there would always be a means to enable or disable the feature. In fact, that's specifically why I test that the Atomic Op completer bits are unset in the root port before changing them so that this automatic enablement could live alongside a command line option to statically enable some bits.
That does however remind me that it is often good with these sorts of "clever" automatic features to have an opt-out, so I'll likely add an x-no-rp-atomics device option in the next version to provide that. Thanks,
Alex
Quoted 20 lines · Alex Williamson
Quoted 5 lines · On Wed, 31 May 2023 23:55:41 +0200 Robin Voetter <[email protected]> wrote:
Hi Alex,
Thanks for taking the time to implement support for Atomic Op completer support properly :). I have tested out these patches and the kernel patch, and apart from a minor issue with patch 2 everything works fine;
Yes, Cedric noted the extra semicolon as well, I'm about to post that patch as non-RFC since it has no dependencies on anything else.
Policy decisions like that are generally left to management tools, so there would always be a means to enable or disable the feature. In fact, that's specifically why I test that the Atomic Op completer bits are unset in the root port before changing them so that this automatic enablement could live alongside a command line option to statically enable some bits.
Quoted 3 lines
That does however remind me that it is often good with these sorts of "clever" automatic features to have an opt-out, so I'll likely add an x-no-rp-atomics device option in the next version to provide that.
I was just going to suggest that, just in case :)
Hi Markus, Marc-André,
Quoted 11 lines · Alex Williamson
Quoted 10 lines
Policy decisions like that are generally left to management tools, so there would always be a means to enable or disable the feature. In fact, that's specifically why I test that the Atomic Op completer bits are unset in the root port before changing them so that this automatic enablement could live alongside a command line option to statically enable some bits.
That does however remind me that it is often good with these sorts of "clever" automatic features to have an opt-out, so I'll likely add an x-no-rp-atomics device option in the next version to provide that.
Still thinking about this series I remembered since commit a3c45b3e62 ("qapi: New special feature flag "unstable"") the "x-" prefix is obsolete (superseded) in QAPI generated code.
I wonder about device properties:
$ git grep -F '"x-' hw | wc -l
130$ git grep -F '"x-' hw/vfio/pci.c
hw/vfio/pci.c:2987: error_setg(errp, "x-balloon-allowed only potentially compatible "
hw/vfio/pci.c:3335: DEFINE_PROP_ON_OFF_AUTO("x-pre-copy-dirty-page-tracking", VFIOPCIDevice,
hw/vfio/pci.c:3342: DEFINE_PROP_UINT32("x-intx-mmap-timeout-ms", VFIOPCIDevice,
hw/vfio/pci.c:3344: DEFINE_PROP_BIT("x-vga", VFIOPCIDevice, features,
hw/vfio/pci.c:3346: DEFINE_PROP_BIT("x-req", VFIOPCIDevice, features,
hw/vfio/pci.c:3348: DEFINE_PROP_BIT("x-igd-opregion", VFIOPCIDevice, features,
hw/vfio/pci.c:3350: DEFINE_PROP_BOOL("x-enable-migration", VFIOPCIDevice,
hw/vfio/pci.c:3352: DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),
hw/vfio/pci.c:3353: DEFINE_PROP_BOOL("x-balloon-allowed", VFIOPCIDevice,
hw/vfio/pci.c:3355: DEFINE_PROP_BOOL("x-no-kvm-intx", VFIOPCIDevice, no_kvm_intx, false),
hw/vfio/pci.c:3356: DEFINE_PROP_BOOL("x-no-kvm-msi", VFIOPCIDevice, no_kvm_msi, false),
hw/vfio/pci.c:3357: DEFINE_PROP_BOOL("x-no-kvm-msix", VFIOPCIDevice, no_kvm_msix, false),
hw/vfio/pci.c:3358: DEFINE_PROP_BOOL("x-no-geforce-quirks", VFIOPCIDevice,
hw/vfio/pci.c:3360: DEFINE_PROP_BOOL("x-no-kvm-ioeventfd", VFIOPCIDevice, no_kvm_ioeventfd,
hw/vfio/pci.c:3362: DEFINE_PROP_BOOL("x-no-vfio-ioeventfd", VFIOPCIDevice, no_vfio_ioeventfd,
hw/vfio/pci.c:3364: DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice, vendor_id, PCI_ANY_ID),
hw/vfio/pci.c:3365: DEFINE_PROP_UINT32("x-pci-device-id", VFIOPCIDevice, device_id, PCI_ANY_ID),
hw/vfio/pci.c:3366: DEFINE_PROP_UINT32("x-pci-sub-vendor-id", VFIOPCIDevice,
hw/vfio/pci.c:3368: DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice,
hw/vfio/pci.c:3370: DEFINE_PROP_UINT32("x-igd-gms", VFIOPCIDevice, igd_gms, 0),
hw/vfio/pci.c:3371: DEFINE_PROP_UNSIGNED_NODEFAULT("x-nv-gpudirect-clique", VFIOPCIDevice,
hw/vfio/pci.c:3374: DEFINE_PROP_OFF_AUTO_PCIBAR("x-msix-relocation", VFIOPCIDevice, msix_relo,Is there a plan to use something similar for QOM properties? Is it OK to keep using the "x-" prefix there?
Thanks,
Phil.
Quoted 25 lines · Alex Williamson
Quoted 5 lines · On Wed, 31 May 2023 23:55:41 +0200 Robin Voetter <[email protected]> wrote:
Something that I have been thinking about, are there any implications involved with enabling this feature automatically with no possibility of turning it off? I have no use case for that, though, and I cant really think of a reason other than preventing the guest from finding out hardware details about the host.
Not sure I follow, are you suggesting that Atomic Ops completion support is proactively enabled in the VM to match the host, regardless of attached assigned devices? An obvious problem there is migration. If I start a VM on a host with Atomic Ops support and want to migrate to a host without Atomic Ops support, config space of the root ports is now different and the migration fails. QEMU would also require elevated privileges to read config space to determine the host support, and then what does it do if only some of the PCIe hierarchy supports Atomic Ops.
Policy decisions like that are generally left to management tools, so there would always be a means to enable or disable the feature. In fact, that's specifically why I test that the Atomic Op completer bits are unset in the root port before changing them so that this automatic enablement could live alongside a command line option to statically enable some bits.
Quoted 3 lines
That does however remind me that it is often good with these sorts of "clever" automatic features to have an opt-out, so I'll likely add an x-no-rp-atomics device option in the next version to provide that.
Yes, something like that that is exactly what I was thinking about.
Thanks,
Robin Voetter
Quoted 3 lines · Alex Williamson
This RFC proposes to allow a vfio-pci device to manipulate the PCI Express capability of an associated root port to enable Atomic Op completer support as equivalent to host capabilities. This would
[...]
Quoted 17 lines
While it's not exactly standard practice to modify root port device capabilities runtime, it also does not seem to be precluded by the PCIe Spec (6.0.1). The Atomic Op completion bits of the DEVCAP2 register are defined as Read-only:
7.4 Configuration Register Types
Read-only - Register bits are read-only and cannot be altered by software.
Where explicitly defined, these bits are used to reflect changing hardware state, and as a result bit values can be observed to change at run time. Register bit default values and bits that cannot change value at run time, are permitted to be hard-coded, initialized by system/device firmware, or initialized by hardware mechanisms such as pin strapping or nonvolatile storage. Initialization by system firmware is permitted only for system- integrated devices. If the optional feature that would Set the bits is not implemented, the bits must be hardwired to Zero.
Quoted 8 lines
Here "altered by software" is relative to guest writes to the config space register, whereas in this implementation we're acting as hardware and the bits are changing to reflect a change in runtime capabilities. The spec does include a HwInit register type which would restrict the value from changing at runtime outside of resets. Therefore while it would not be advised to update these bits arbitrarily, it does seem safe and compatible with guest software to update the value on device attach and detach.
From my previous (short) PCIe experience, this is also my understanding.