Skip to contentSkip to conversation

spapr_pci: Merge spapr-vfio-phb into spapr-phb

PATCH In review Alexey Kardashevskiy 3 participants 9 messages Sep 3, 2015 – Sep 14, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover spapr_pci: Merge spapr-vfio-phb into spapr-phb

This allows sharing the same PHB for both emulated and VFIO devices. This used to be a part of the Dynamic DMA window patchset. Since it was moved out and reworked quite a lot, all reviewed-by's got outdated.

This is based on dgibson/spapr-next sha1 f91ffef.

Please comment. Thanks.

Changes:
v2:
* fixed bus_offset
* moved DMA setup to a spapr_phb_dma_reset() helper - DDW patches would add it anyway
* verified that migration still works

Alexey Kardashevskiy (2):
spapr_pci_vfio: Remove redundant spapr-pci-vfio-host-bridge
spapr_pci: Remove constraints about VFIO-PCI devices

 hw/ppc/Makefile.objs        |   5 +-
 hw/ppc/spapr_pci.c          | 195 +++++++++++++++++++++++++++-----------------
 hw/ppc/spapr_pci_vfio.c     | 147 +++++++++++++--------------------
 hw/vfio/common.c            |  22 ++---
 include/hw/pci-host/spapr.h |  23 +++---
 include/hw/vfio/vfio.h      |   3 +-
 6 files changed, 195 insertions(+), 200 deletions(-)
Signature · 1 line
2.4.0.rc3.8.gfb3e7d5
#1 lore raw

PATCH v2 1/2 spapr_pci_vfio: Remove redundant spapr-pci-vfio-host-bridge

sPAPRTCETable is handling 2 TCE tables already:

1) guest view of the TCE table - emulated devices use only this table;

2) hardware IOMMU table - VFIO PCI devices use it for actual work but it does not replace 1) and it is not visible to the guest. The initialization of this table is driven by vfio-pci device, DMA map/unmap requests are handled via MemoryListener so there is very little to do in spapr-pci-vfio-host-bridge.

This moves VFIO bits to the generic spapr-pci-host-bridge which allows putting emulated and VFIO devices on the same PHB. It is still possible to create multiple PHBs and avoid sharing PHB resouces for emulated and VFIO devices.

If there is no VFIO-PCI device attaches, no special ioctls will be called. If there are some VFIO-PCI devices attached, PHB may refuse to attach another VFIO-PCI device if a VFIO container on the host kernel side does not support container sharing.

This makes spapr-pci-vfio-host-bridge type equal to spapr-pci-host-bridge except it has an additional "iommu" property so spapr-pci-vfio-host-bridge still should be used for VFIO devices. The next patch will remove IOMMU ID property and allow putting VFIO-PCI devices onto spapr-pci-host-bridge.

This adds a number of VFIO-PCI devices currently attached to a PHB as PHB needs to know whether to do DMA setup for VFIO or not. Since at the moment of the PHB's realize() invocation we cannot tell yet how many VFIO-PCI devices are there (they are not attached yet), this moves DMA setup to the reset handler (which is also called when QEMU is about to receive migration).

This moves PCI device lookup from spapr_phb_vfio_eeh_set_option() to rtas_ibm_set_eeh_option() as we need to know if the device is "vfio-pci" and decide whether to call spapr_phb_vfio_eeh_set_option() or not.

This should cause no behavioural change.

---
Changes:
v2:
* used tcet->bus_offset in the common PHB code as this is what VFIO PHB needs

---
 hw/ppc/Makefile.objs        |   5 +-
 hw/ppc/spapr_pci.c          | 195 +++++++++++++++++++++++++++-----------------
 hw/ppc/spapr_pci_vfio.c     | 144 +++++++++++++-------------------
 include/hw/pci-host/spapr.h |  23 +++---
 4 files changed, 189 insertions(+), 178 deletions(-)
hw/ppc/Makefile.objs +1 -4
index c8ab06e..6c06fcf 100644
@@ -3,10 +3,7 @@ obj-y += ppc.o ppc_booke.o
 # IBM pSeries (sPAPR)
 obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
-obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_rtc.o spapr_drc.o
-ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
-obj-y += spapr_pci_vfio.o
-endif
+obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_pci_vfio.o spapr_rtc.o spapr_drc.o
 # PowerPC 4xx boards
 obj-y += ppc405_boards.o ppc4xx_devs.o ppc405_uc.o ppc440_bamboo.o
 obj-y += ppc4xx_pci.o
hw/ppc/spapr_pci.c +120 -75

323 lines not on this page · raw message

hw/ppc/spapr_pci_vfio.c +56 -88

242 lines not on this page · raw message

include/hw/pci-host/spapr.h +12 -11
index 5322b56..7f3c712 100644
@@ -47,12 +47,6 @@ typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState;
 
 struct sPAPRPHBClass {
     PCIHostBridgeClass parent_class;
-
-    void (*finish_realize)(sPAPRPHBState *sphb, Error **errp);
-    int (*eeh_set_option)(sPAPRPHBState *sphb, unsigned int addr, int option);
-    int (*eeh_get_state)(sPAPRPHBState *sphb, int *state);
-    int (*eeh_reset)(sPAPRPHBState *sphb, int option);
-    int (*eeh_configure)(sPAPRPHBState *sphb);
 };
 
 typedef struct spapr_pci_msi {
@@ -91,12 +85,11 @@ struct sPAPRPHBState {
     spapr_pci_msi_mig *msi_devs;
 
     QLIST_ENTRY(sPAPRPHBState) list;
-};
 
-struct sPAPRPHBVFIOState {
-    sPAPRPHBState phb;
-
-    int32_t iommugroupid;
+    int32_t iommugroupid; /* obsolete */
+    uint32_t dma32_window_start;
+    uint32_t dma32_window_size;
+    unsigned vfio_num;
 };
 
 #define SPAPR_PCI_MAX_INDEX          255
@@ -138,4 +131,12 @@ sPAPRPHBState *spapr_pci_find_phb(sPAPRMachineState *spapr, uint64_t buid);
 PCIDevice *spapr_pci_find_dev(sPAPRMachineState *spapr, uint64_t buid,
                               uint32_t config_addr);
 
+int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb);
+int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
+                                  PCIDevice *pdev, int option);
+int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state);
+int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option);
+int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb);
+void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb);
+
 #endif /* __HW_SPAPR_PCI_H__ */
Signature · 1 line
2.4.0.rc3.8.gfb3e7d5
#2 lore raw

PATCH v2 2/2 spapr_pci: Remove constraints about VFIO-PCI devices

In reply to Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
 hw/ppc/spapr_pci.c      | 10 +++++-----
 hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
 hw/vfio/common.c        | 22 ++++++----------------
 include/hw/vfio/vfio.h  |  3 +--
 4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
     pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
     if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
         ret = spapr_phb_vfio_dma_capabilities_update(sphb);
         if (ret) {
             error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
     if (sphb->index != (uint32_t)-1) {
         hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
     struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
     int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
     if (ret) {
         return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
         .op    = VFIO_EEH_PE_ENABLE
     };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
 }
 
 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
     int ret;
 
     op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
     int ret;
 
     op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
hw/vfio/common.c +6 -16
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
     close(vbasedev->fd);
 }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
 {
-    VFIOGroup *group;
     VFIOContainer *container;
     int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
         ret = ioctl(container->fd, req, param);
         if (ret < 0) {
             error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                          _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
         }
     }
 
-    vfio_put_group(group);
-
     return ret;
 }
 
-int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
-                         int req, void *param)
+int vfio_container_ioctl(AddressSpace *as, int req, void *param)
 {
     /* We allow only certain ioctls to the container */
     switch (req) {
@@ -968,5 +958,5 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
         return -1;
     }
 
-    return vfio_container_do_ioctl(as, groupid, req, param);
+    return vfio_container_do_ioctl(as, req, param);
 }
include/hw/vfio/vfio.h +1 -2
index 0b26cd8..e076c14 100644
@@ -3,7 +3,6 @@
 
 #include "qemu/typedefs.h"
 
-extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
-                                int req, void *param);
+extern int vfio_container_ioctl(AddressSpace *as, int req, void *param);
 
 #endif
Signature · 1 line
2.4.0.rc3.8.gfb3e7d5
#3 lore raw

Re: [Qemu-devel] [PATCH qemu v2 2/2] spapr_pci: Remove constraints about VFIO-PCI devices

Quoted 151 lines · Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
 hw/ppc/spapr_pci.c      | 10 +++++-----
 hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
 hw/vfio/common.c        | 22 ++++++----------------
 include/hw/vfio/vfio.h  |  3 +--
 4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
     pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
     if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
         ret = spapr_phb_vfio_dma_capabilities_update(sphb);
         if (ret) {
             error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
     if (sphb->index != (uint32_t)-1) {
         hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
     struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
     int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
     if (ret) {
         return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
         .op    = VFIO_EEH_PE_ENABLE
     };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
 }
 
 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
     int ret;
 
     op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
     int ret;
 
     op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
hw/vfio/common.c +2 -9
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
     close(vbasedev->fd);
 }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
 {
-    VFIOGroup *group;
     VFIOContainer *container;
     int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 942

Quoted 10 lines
hw/vfio/common.c +2 -2
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
         ret = ioctl(container->fd, req, param);
         if (ret < 0) {
             error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                          _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
         }
     }

This highlights how terrible this ioctl passthrough interface is; what's a caller supposed to do on error? Here we don't have visibility into the ioctl being called in order to do any unwind on error. The caller doesn't have the context to unwind only the failed containers. Is returning errno here really sufficient for anyone to figure out how to proceed or should we just cut our loses and abort()? What's the least horrible way we can realistically handle a failure here? Thanks,

Alex

Quoted 32 lines
hw/vfio/common.c +2 -5
-    vfio_put_group(group);
-
     return ret;
 }
 
-int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
-                         int req, void *param)
+int vfio_container_ioctl(AddressSpace *as, int req, void *param)
 {
     /* We allow only certain ioctls to the container */
     switch (req) {
@@ -968,5 +958,5 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
         return -1;
     }
 
-    return vfio_container_do_ioctl(as, groupid, req, param);
+    return vfio_container_do_ioctl(as, req, param);
 }
include/hw/vfio/vfio.h +1 -2
index 0b26cd8..e076c14 100644
@@ -3,7 +3,6 @@
 
 #include "qemu/typedefs.h"
 
-extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
-                                int req, void *param);
+extern int vfio_container_ioctl(AddressSpace *as, int req, void *param);
 
 #endif
#4 lore raw

Re: [Qemu-devel] [PATCH qemu v2 2/2] spapr_pci: Remove constraints about VFIO-PCI devices

Quoted 163 lines · Alex Williamson
Quoted 151 lines · Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
 hw/ppc/spapr_pci.c      | 10 +++++-----
 hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
 hw/vfio/common.c        | 22 ++++++----------------
 include/hw/vfio/vfio.h  |  3 +--
 4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
     pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
     if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
         ret = spapr_phb_vfio_dma_capabilities_update(sphb);
         if (ret) {
             error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
     if (sphb->index != (uint32_t)-1) {
         hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
     struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
     int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
     if (ret) {
         return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
         .op    = VFIO_EEH_PE_ENABLE
     };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
 }
 
 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
     int ret;
 
     op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
     int ret;
 
     op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
hw/vfio/common.c +2 -9
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
     close(vbasedev->fd);
 }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
 {
-    VFIOGroup *group;
     VFIOContainer *container;
     int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 942

Quoted 10 lines
hw/vfio/common.c +2 -2
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
         ret = ioctl(container->fd, req, param);
         if (ret < 0) {
             error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                          _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
         }
     }
Quoted 7 lines

This highlights how terrible this ioctl passthrough interface is; what's a caller supposed to do on error? Here we don't have visibility into the ioctl being called in order to do any unwind on error. The caller doesn't have the context to unwind only the failed containers. Is returning errno here really sufficient for anyone to figure out how to proceed or should we just cut our loses and abort()? What's the least horrible way we can realistically handle a failure here? Thanks,

It seemed like I asked this before and it turns out that I did:

https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07399.html

"Gavin says yes" is not really a supportable solution when I stumbled on it again and David doesn't know why it's safe either (nor does it answer my question of how does this work). At a minimum, the reasoning why this is safe for the ioctls we currently allow here needs to be spelled out in a comment. We could easily make the mistake of adding another ioctl to the passthrough for which those assumptions are not valid. We may even want to abort if it's not one of the ioctls we've evaluated so we don't overlook this problem later. Thanks,

Alex

Quoted 34 lines
Quoted 32 lines
hw/vfio/common.c +2 -5
-    vfio_put_group(group);
-
     return ret;
 }
 
-int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
-                         int req, void *param)
+int vfio_container_ioctl(AddressSpace *as, int req, void *param)
 {
     /* We allow only certain ioctls to the container */
     switch (req) {
@@ -968,5 +958,5 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
         return -1;
     }
 
-    return vfio_container_do_ioctl(as, groupid, req, param);
+    return vfio_container_do_ioctl(as, req, param);
 }
include/hw/vfio/vfio.h +1 -2
index 0b26cd8..e076c14 100644
@@ -3,7 +3,6 @@
 
 #include "qemu/typedefs.h"
 
-extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
-                                int req, void *param);
+extern int vfio_container_ioctl(AddressSpace *as, int req, void *param);
 
 #endif
#5 lore raw

Re: [Qemu-devel] [PATCH qemu v2 2/2] spapr_pci: Remove constraints about VFIO-PCI devices

Quoted 176 lines · Alex Williamson
Quoted 163 lines · Alex Williamson
Quoted 151 lines · Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
 hw/ppc/spapr_pci.c      | 10 +++++-----
 hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
 hw/vfio/common.c        | 22 ++++++----------------
 include/hw/vfio/vfio.h  |  3 +--
 4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
     pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
     if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
         ret = spapr_phb_vfio_dma_capabilities_update(sphb);
         if (ret) {
             error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
     if (sphb->index != (uint32_t)-1) {
         hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
     struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
     int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
     if (ret) {
         return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
         .op    = VFIO_EEH_PE_ENABLE
     };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
 }
 
 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
     int ret;
 
     op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
     int ret;
 
     op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
hw/vfio/common.c +2 -9
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
     close(vbasedev->fd);
 }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
 {
-    VFIOGroup *group;
     VFIOContainer *container;
     int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 942

Quoted 10 lines
hw/vfio/common.c +2 -2
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
         ret = ioctl(container->fd, req, param);
         if (ret < 0) {
             error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                          _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
         }
     }
Quoted 7 lines

This highlights how terrible this ioctl passthrough interface is; what's a caller supposed to do on error? Here we don't have visibility into the ioctl being called in order to do any unwind on error. The caller doesn't have the context to unwind only the failed containers. Is returning errno here really sufficient for anyone to figure out how to proceed or should we just cut our loses and abort()? What's the least horrible way we can realistically handle a failure here? Thanks,

It seemed like I asked this before and it turns out that I did:

https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07399.html

Quoted 8 lines

"Gavin says yes" is not really a supportable solution when I stumbled on it again and David doesn't know why it's safe either (nor does it answer my question of how does this work). At a minimum, the reasoning why this is safe for the ioctls we currently allow here needs to be spelled out in a comment. We could easily make the mistake of adding another ioctl to the passthrough for which those assumptions are not valid. We may even want to abort if it's not one of the ioctls we've evaluated so we don't overlook this problem later. Thanks,

Yeah, you're right. This is a mess.

What we need to do here is to make vfio_container_ioctl() take a VFIOContainer object as the name suggests. The the callers will need to either use it on a specific container, or iterate across all the containers in the VFIOAddressSpace as appropriate for the specific operation.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#6 lore raw

Re: [Qemu-devel] [PATCH qemu v2 2/2] spapr_pci: Remove constraints about VFIO-PCI devices

Quoted 186 lines · David Gibson
Quoted 176 lines · Alex Williamson
Quoted 163 lines · Alex Williamson
Quoted 151 lines · Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
  hw/ppc/spapr_pci.c      | 10 +++++-----
  hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
  hw/vfio/common.c        | 22 ++++++----------------
  include/hw/vfio/vfio.h  |  3 +--
  4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
      pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
      if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
          ret = spapr_phb_vfio_dma_capabilities_update(sphb);
          if (ret) {
              error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
      PCIBus *bus;
      uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
      if (sphb->index != (uint32_t)-1) {
          hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
      struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
      int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                 VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
      if (ret) {
          return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
          .op    = VFIO_EEH_PE_ENABLE
      };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
  }
 
  int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
          return RTAS_OUT_PARAM_ERROR;
      }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_HW_ERROR;
      }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
      int ret;
 
      op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_PARAM_ERROR;
      }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
          return RTAS_OUT_PARAM_ERROR;
      }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_HW_ERROR;
      }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
      int ret;
 
      op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_PARAM_ERROR;
      }
hw/vfio/common.c +2 -9
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
      close(vbasedev->fd);
  }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
  {
-    VFIOGroup *group;
      VFIOContainer *container;
      int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 942

Quoted 10 lines
hw/vfio/common.c +2 -2
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
          ret = ioctl(container->fd, req, param);
          if (ret < 0) {
              error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                           _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
          }
      }
Quoted 7 lines

This highlights how terrible this ioctl passthrough interface is; what's a caller supposed to do on error? Here we don't have visibility into the ioctl being called in order to do any unwind on error. The caller doesn't have the context to unwind only the failed containers. Is returning errno here really sufficient for anyone to figure out how to proceed or should we just cut our loses and abort()? What's the least horrible way we can realistically handle a failure here? Thanks,

It seemed like I asked this before and it turns out that I did:

https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07399.html

Quoted 8 lines

"Gavin says yes" is not really a supportable solution when I stumbled on it again and David doesn't know why it's safe either (nor does it answer my question of how does this work). At a minimum, the reasoning why this is safe for the ioctls we currently allow here needs to be spelled out in a comment. We could easily make the mistake of adding another ioctl to the passthrough for which those assumptions are not valid. We may even want to abort if it's not one of the ioctls we've evaluated so we don't overlook this problem later. Thanks,

Quoted 1 line

Yeah, you're right. This is a mess.

Right. I have been testing/studying this lately a lot, will fix this too.

Quoted 5 lines

What we need to do here is to make vfio_container_ioctl() take a VFIOContainer object as the name suggests. The the callers will need to either use it on a specific container, or iterate across all the containers in the VFIOAddressSpace as appropriate for the specific operation.

The callers - spapr code - do not deal with containers, these are a VFIO internal thing managed by hw/vfio/common.c and attached to VFIOAddressSpace.

So s/vfio_container_ioctl/vfio_address_space_ioctl/ (or vfio_as_ioctl()?) makes more sense here. Which I can make a patch for. Or change vfio_container_ioctl() as you suggest (+make is static) and add public vfio_as_ioctl(). Which one to pick?

Signature · 1 line
Alexey
#7 lore raw

Re: [Qemu-devel] [PATCH qemu v2 2/2] spapr_pci: Remove constraints about VFIO-PCI devices

Quoted 199 lines · Alexey Kardashevskiy
Quoted 186 lines · David Gibson
Quoted 176 lines · Alex Williamson
Quoted 163 lines · Alex Williamson
Quoted 151 lines · Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
 hw/ppc/spapr_pci.c      | 10 +++++-----
 hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
 hw/vfio/common.c        | 22 ++++++----------------
 include/hw/vfio/vfio.h  |  3 +--
 4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
     pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
     if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
         ret = spapr_phb_vfio_dma_capabilities_update(sphb);
         if (ret) {
             error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     PCIBus *bus;
     uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
     if (sphb->index != (uint32_t)-1) {
         hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
     struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
     int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
     if (ret) {
         return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
         .op    = VFIO_EEH_PE_ENABLE
     };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
 }
 
 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
     int ret;
 
     op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
     int ret;
 
     op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_PARAM_ERROR;
     }
hw/vfio/common.c +2 -9
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
     close(vbasedev->fd);
 }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
 {
-    VFIOGroup *group;
     VFIOContainer *container;
     int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 942

Quoted 10 lines
hw/vfio/common.c +2 -2
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
         ret = ioctl(container->fd, req, param);
         if (ret < 0) {
             error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                          _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
         }
     }
Quoted 7 lines

This highlights how terrible this ioctl passthrough interface is; what's a caller supposed to do on error? Here we don't have visibility into the ioctl being called in order to do any unwind on error. The caller doesn't have the context to unwind only the failed containers. Is returning errno here really sufficient for anyone to figure out how to proceed or should we just cut our loses and abort()? What's the least horrible way we can realistically handle a failure here? Thanks,

It seemed like I asked this before and it turns out that I did:

https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07399.html

Quoted 8 lines

"Gavin says yes" is not really a supportable solution when I stumbled on it again and David doesn't know why it's safe either (nor does it answer my question of how does this work). At a minimum, the reasoning why this is safe for the ioctls we currently allow here needs to be spelled out in a comment. We could easily make the mistake of adding another ioctl to the passthrough for which those assumptions are not valid. We may even want to abort if it's not one of the ioctls we've evaluated so we don't overlook this problem later. Thanks,

Quoted 1 line

Yeah, you're right. This is a mess.

Right. I have been testing/studying this lately a lot, will fix this too.

Quoted 5 lines

What we need to do here is to make vfio_container_ioctl() take a VFIOContainer object as the name suggests. The the callers will need to either use it on a specific container, or iterate across all the containers in the VFIOAddressSpace as appropriate for the specific operation.

Quoted 7 lines

The callers - spapr code - do not deal with containers, these are a VFIO internal thing managed by hw/vfio/common.c and attached to VFIOAddressSpace.

So s/vfio_container_ioctl/vfio_address_space_ioctl/ (or vfio_as_ioctl()?) makes more sense here. Which I can make a patch for. Or change vfio_container_ioctl() as you suggest (+make is static) and add public vfio_as_ioctl(). Which one to pick?

No, that doesn't address the problem. The ioctl is operating on the container, the wrapper needs to take the container. Whether the ioctl can be safely replicated across all containers in the VFIOAddressSpace, or can only be used if you have additional information to select a specific container *cannot* be known unless you know exactly what ioctl() you're talking about. That decision has to move to the callers, which know what they're doing.

If those callers don't know about individual containers now, then they have to learn.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#8 lore raw

Re: [Qemu-devel] [PATCH qemu v2 2/2] spapr_pci: Remove constraints about VFIO-PCI devices

Quoted 216 lines · David Gibson
Quoted 199 lines · Alexey Kardashevskiy
Quoted 186 lines · David Gibson
Quoted 176 lines · Alex Williamson
Quoted 163 lines · Alex Williamson
Quoted 151 lines · Alexey Kardashevskiy

So far there were 2 limitations enforced on an emulated PHB regarding VFIO:
1) only one IOMMU group per IOMMU container was allowed and the spapr-pci-vfio-host-bridge device has an IOMMU ID property for this;
2) only one IOMMU container per PHB was allowed as a group can only be attached to one container.

However these are not really necessary so we are removing them here.

This deprecates IOMMU group ID and changes vfio_container_do_ioctl() not to receive it. Instead of getting a container from a group ID, this calls ioctl() for every container attached to the PHB address space. This allows multiple containers on the same PHB which means multiple groups per PHB. Note that this will lead to every H_PUT_TCE&etc call to be passed to every container separately which will affect the performance. For 32bit devices it is still recommended to put every group to a separate PHB.

Since the existing VFIO code is already trying to share a container for multiple groups, just removing a group id from
the vfio_container_do_ioctl() allows the existing code to share a container if it is supported by the host kernel.

This replaces the check for a group id to be set correctly with the check that it is not set.

This removes casts to sPAPRPHBVFIOState as none of sPAPRPHBVFIOState members is accessed here.

---
  hw/ppc/spapr_pci.c      | 10 +++++-----
  hw/ppc/spapr_pci_vfio.c | 17 ++++++-----------
  hw/vfio/common.c        | 22 ++++++----------------
  include/hw/vfio/vfio.h  |  3 +--
  4 files changed, 18 insertions(+), 34 deletions(-)
hw/ppc/spapr_pci.c +5 -5
index 4e289cb..1b7559d 100644
@@ -810,11 +810,6 @@ static int spapr_phb_dma_capabilities_update(sPAPRPHBState *sphb)
      pci_for_each_device(bus, pci_bus_num(bus), spapr_phb_walk_devices, sphb);
 
      if (sphb->vfio_num) {
-        if (sphb->iommugroupid == -1) {
-            error_report("Wrong IOMMU group ID %d", sphb->iommugroupid);
-            return -1;
-        }
-
          ret = spapr_phb_vfio_dma_capabilities_update(sphb);
          if (ret) {
              error_report("Unable to get DMA32 info from VFIO");
@@ -1282,6 +1277,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
      PCIBus *bus;
      uint64_t msi_window_size = 4096;
 
+    if ((sphb->iommugroupid != -1) &&
+        object_dynamic_cast(OBJECT(sphb), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)) {
+        error_report("Warning: iommugroupid is deprecated and will be ignored");
+    }
+
      if (sphb->index != (uint32_t)-1) {
          hwaddr windows_base;
 
hw/ppc/spapr_pci_vfio.c +6 -11
index f94d8a4..48137d5 100644
@@ -35,7 +35,7 @@ int spapr_phb_vfio_dma_capabilities_update(sPAPRPHBState *sphb)
      struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
      int ret;
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
+    ret = vfio_container_ioctl(&sphb->iommu_as,
                                 VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
      if (ret) {
          return ret;
@@ -54,8 +54,7 @@ void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
          .op    = VFIO_EEH_PE_ENABLE
      };
 
-    vfio_container_ioctl(&sphb->iommu_as,
-                         sphb->iommugroupid, VFIO_EEH_PE_OP, &op);
+    vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
  }
 
  int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
@@ -81,8 +80,7 @@ int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
          return RTAS_OUT_PARAM_ERROR;
      }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_HW_ERROR;
      }
@@ -96,8 +94,7 @@ int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
      int ret;
 
      op.op = VFIO_EEH_PE_GET_STATE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_PARAM_ERROR;
      }
@@ -170,8 +167,7 @@ int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
          return RTAS_OUT_PARAM_ERROR;
      }
 
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_HW_ERROR;
      }
@@ -185,8 +181,7 @@ int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
      int ret;
 
      op.op = VFIO_EEH_PE_CONFIGURE;
-    ret = vfio_container_ioctl(&sphb->iommu_as, sphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
+    ret = vfio_container_ioctl(&sphb->iommu_as, VFIO_EEH_PE_OP, &op);
      if (ret < 0) {
          return RTAS_OUT_PARAM_ERROR;
      }
hw/vfio/common.c +2 -9
index 85ee9b0..a00644e 100644
@@ -926,35 +926,25 @@ void vfio_put_base_device(VFIODevice *vbasedev)
      close(vbasedev->fd);
  }
 
-static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
-                                   int req, void *param)
+static int vfio_container_do_ioctl(AddressSpace *as, int req, void *param)
  {
-    VFIOGroup *group;
      VFIOContainer *container;
      int ret = -1;
+    VFIOAddressSpace *space = vfio_get_address_space(as);
 
-    group = vfio_get_group(groupid, as);
-    if (!group) {
-        error_report("vfio: group %d not registered", groupid);
-        return ret;
-    }
-

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 942

Quoted 10 lines
hw/vfio/common.c +2 -2
-    container = group->container;
-    if (group->container) {
+    QLIST_FOREACH(container, &space->containers, next) {
          ret = ioctl(container->fd, req, param);
          if (ret < 0) {
              error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
                           _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
+            return -errno;
          }
      }
Quoted 7 lines

This highlights how terrible this ioctl passthrough interface is; what's a caller supposed to do on error? Here we don't have visibility into the ioctl being called in order to do any unwind on error. The caller doesn't have the context to unwind only the failed containers. Is returning errno here really sufficient for anyone to figure out how to proceed or should we just cut our loses and abort()? What's the least horrible way we can realistically handle a failure here? Thanks,

It seemed like I asked this before and it turns out that I did:

https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07399.html

Quoted 8 lines

"Gavin says yes" is not really a supportable solution when I stumbled on it again and David doesn't know why it's safe either (nor does it answer my question of how does this work). At a minimum, the reasoning why this is safe for the ioctls we currently allow here needs to be spelled out in a comment. We could easily make the mistake of adding another ioctl to the passthrough for which those assumptions are not valid. We may even want to abort if it's not one of the ioctls we've evaluated so we don't overlook this problem later. Thanks,

Quoted 1 line

Yeah, you're right. This is a mess.

Right. I have been testing/studying this lately a lot, will fix this too.

Quoted 5 lines

What we need to do here is to make vfio_container_ioctl() take a VFIOContainer object as the name suggests. The the callers will need to either use it on a specific container, or iterate across all the containers in the VFIOAddressSpace as appropriate for the specific operation.

Quoted 7 lines

The callers - spapr code - do not deal with containers, these are a VFIO internal thing managed by hw/vfio/common.c and attached to VFIOAddressSpace.

So s/vfio_container_ioctl/vfio_address_space_ioctl/ (or vfio_as_ioctl()?) makes more sense here. Which I can make a patch for. Or change vfio_container_ioctl() as you suggest (+make is static) and add public vfio_as_ioctl(). Which one to pick?

No, that doesn't address the problem. The ioctl is operating on the container, the wrapper needs to take the container. Whether the ioctl can be safely replicated across all containers in the VFIOAddressSpace, or can only be used if you have additional information to select a specific container *cannot* be known unless you know exactly what ioctl() you're talking about. That decision has to move to the callers, which know what they're doing.

Quoted 2 lines

If those callers don't know about individual containers now, then they have to learn.

I lost you here. Guests (and therefore the vfio_container_ioctl() callers which are RTAS handlers) work with either LIOBNs or phbid+bus+devfn (where bus+devfn are not used) so it is LIOBN or PHB. If there are multiple containers, there is no way (and no need) to call these ioctls on some containers only. If that really matters, the user is supposed to use a separate PHB.

If bus+devfn are used (which I'll double check), then we might want another helper in QEMU - vfio_pci_ioctl(PCIDevice *pdev) and new vfio-pci-spapr PCI driver on the host (of #ifdef in the existing vfio-pci driver) but I doubt this is the case.

Signature · 1 line
Alexey