[5/5] vfio: Only use memory listeners when appropriate

Submitted by David Gibson on April 24, 2013, 12:01 p.m.

Details

Message ID 1366804881-553-6-git-send-email-david@gibson.dropbear.id.au
State New
Headers show

Commit Message

David Gibson April 24, 2013, 12:01 p.m.
Currently, vfio registers a MemoryListener for every vfio container we
create, to keep the container's mappings in sync with main system memory.
That's only correct though, if the context the container is attached to
represents a dma address space which actually matches main system memory -
roughly speaking that means that there is no guest side IOMMU above the
vfio device in question.

This patch corrects the code, by only registering the MemoryListener when
the container belongs to a DMAContext which does not include an IOMMU (i.e.
which has no ->translate function).  In other cases we given an error; that
will change when vfio support for guest side IOMMUs is added.

In addition, this generalizes the code slightly, by attaching the
MemoryListener to the DMAContext's underlying AddressSpace, rather than
just assuming that it is main system memory.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 hw/misc/vfio.c |   79 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 33 deletions(-)

Patch hide | download patch | download mbox

diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
index ab870a8..dce6189 100644
--- a/hw/misc/vfio.c
+++ b/hw/misc/vfio.c
@@ -2616,28 +2616,11 @@  void dma_context_init_vfio(DMAContext *dma)
     QLIST_INIT(&dma->vfio.containers);
 }
 
-static int vfio_connect_context(VFIOGroup *group, DMAContext *dma)
+static int vfio_connect_container_address_space(VFIOGroup *group,
+                                                DMAContext *dma)
 {
     VFIOContainer *container;
-    int ret, fd;
-
-    if (group->container) {
-        if (group->container->dma == dma) {
-            return 0;
-        } else {
-            error_report("vfio: group %d used in multiple DMA contexts",
-                         group->groupid);
-            return -EBUSY;
-        }
-    }
-
-    QLIST_FOREACH(container, &dma->vfio.containers, next) {
-        if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
-            group->container = container;
-            QLIST_INSERT_HEAD(&container->group_list, group, container_next);
-            return 0;
-        }
-    }
+    int fd, ret;
 
     fd = qemu_open("/dev/vfio/vfio", O_RDWR);
     if (fd < 0) {
@@ -2657,15 +2640,15 @@  static int vfio_connect_context(VFIOGroup *group, DMAContext *dma)
     container->dma = dma;
     container->fd = fd;
 
-    if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
-        ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
-        if (ret) {
-            error_report("vfio: failed to set group container: %m");
-            g_free(container);
-            close(fd);
-            return -errno;
-        }
+    ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
+    if (ret) {
+        error_report("vfio: failed to set group container: %m");
+        g_free(container);
+        close(fd);
+        return -errno;
+    }
 
+    if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
         ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
         if (ret) {
             error_report("vfio: failed to set iommu for container: %m");
@@ -2673,11 +2656,6 @@  static int vfio_connect_context(VFIOGroup *group, DMAContext *dma)
             close(fd);
             return -errno;
         }
-
-        container->iommu_data.listener = vfio_memory_listener;
-        container->iommu_data.release = vfio_listener_release;
-
-        memory_listener_register(&container->iommu_data.listener, &address_space_memory);
     } else {
         error_report("vfio: No available IOMMU models");
         g_free(container);
@@ -2685,6 +2663,11 @@  static int vfio_connect_context(VFIOGroup *group, DMAContext *dma)
         return -EINVAL;
     }
 
+    container->iommu_data.listener = vfio_memory_listener;
+    container->iommu_data.release = vfio_listener_release;
+
+    memory_listener_register(&container->iommu_data.listener, dma->as);
+
     QLIST_INIT(&container->group_list);
     QLIST_INSERT_HEAD(&dma->vfio.containers, container, next);
 
@@ -2694,6 +2677,36 @@  static int vfio_connect_context(VFIOGroup *group, DMAContext *dma)
     return 0;
 }
 
+static int vfio_connect_context(VFIOGroup *group, DMAContext *dma)
+{
+    VFIOContainer *container;
+
+    if (group->container) {
+        if (group->container->dma == dma) {
+            return 0;
+        } else {
+            error_report("vfio: group %d used in multiple DMA contexts",
+                         group->groupid);
+            return -EBUSY;
+        }
+    }
+
+    QLIST_FOREACH(container, &dma->vfio.containers, next) {
+        if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
+            group->container = container;
+            QLIST_INSERT_HEAD(&container->group_list, group, container_next);
+            return 0;
+        }
+    }
+
+    if (!dma->translate) {
+        return vfio_connect_container_address_space(group, dma);
+    }
+
+    error_report("vfio: no support for guest side IOMMU");
+    return -ENODEV;
+}
+
 static void vfio_disconnect_context(VFIOGroup *group)
 {
     VFIOContainer *container = group->container;