qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: wexu@redhat.com
To: qemu-devel@nongnu.org
Cc: jasowang@redhat.com, marcel@redhat.com,
	alex.williamson@redhat.com, Wei Xu <wexu@redhat.com>
Subject: [Qemu-devel] [Patch 1/3] vfio: reusing address space for the same IOMMU group devices
Date: Tue, 12 Sep 2017 02:56:30 +0800	[thread overview]
Message-ID: <1505156192-18994-2-git-send-email-wexu@redhat.com> (raw)
In-Reply-To: <1505156192-18994-1-git-send-email-wexu@redhat.com>

From: Wei Xu <wexu@redhat.com>

Currently address space of a vfio device is selected by directly
looking up pci device IOMMU address space during realizing, this
usually works for most none separate address space targeted cases
since they are using the system address space, i.e. a q35 machine
without virtual IOMMU. Unfortunately, when it comes down to the case
having a virtual IOMMU(x86 vtd in this case) and two vfio devices in
the same IOMMU group, the virtual IOMMU(vtd) creates two separate
address space for each device, this breaks the minimum granularity for
vfio, and the device fails realizing by prompting 'group xxx used in
multiple address spaces'.

This patch is a helper looking up the same IOMMU device before
invoking creating an new address space for a device, thus fixes the issue.

As a side work for the all groups/devices loop, also it checks if the device
has been assigned to the guest twice before creating an extra group and
removing it later which is not necessary.

Signed-off-by: Wei Xu <wexu@redhat.com>
---
 hw/vfio/common.c              | 28 ++++++++++++++++++++++++++++
 include/hw/vfio/vfio-common.h |  1 +
 2 files changed, 29 insertions(+)

diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 7b2924c..63c3609 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -35,6 +35,7 @@
 #include "sysemu/kvm.h"
 #include "trace.h"
 #include "qapi/error.h"
+#include "hw/vfio/pci.h"
 
 struct vfio_group_head vfio_group_list =
     QLIST_HEAD_INITIALIZER(vfio_group_list);
@@ -1183,6 +1184,33 @@ static void vfio_disconnect_container(VFIOGroup *group)
     }
 }
 
+AddressSpace *vfio_lookup_as(int groupid, PCIDevice *pdev, Error **errp)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev_iter;
+    VFIOPCIDevice *vdev, *vd;
+
+    vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
+    QLIST_FOREACH(group, &vfio_group_list, next) {
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
+            if (strcmp(vbasedev_iter->name, vdev->vbasedev.name) == 0) {
+                error_setg(errp, "device is already attached");
+                return 0;
+            }
+
+            if (vbasedev_iter->group->groupid == groupid) {
+                vd = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
+
+                if (vd->pdev.bus == pdev->bus) {
+                    return vbasedev_iter->group->container->space->as;
+                }
+            }
+        }
+    }
+
+    return pci_device_iommu_address_space(pdev);
+}
+
 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
 {
     VFIOGroup *group;
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index f3a2ac9..5b4827b 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -157,6 +157,7 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
 void vfio_region_exit(VFIORegion *region);
 void vfio_region_finalize(VFIORegion *region);
 void vfio_reset_handler(void *opaque);
+AddressSpace *vfio_lookup_as(int groupid, PCIDevice *pdev, Error **errp);
 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
 void vfio_put_group(VFIOGroup *group);
 int vfio_get_device(VFIOGroup *group, const char *name,
-- 
1.8.3.1

  parent reply	other threads:[~2017-09-11 18:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-11 18:56 [Qemu-devel] [Patch 0/3] vfio: reusing address space for the same iommu group devices wexu
2017-09-11 18:51 ` no-reply
2017-09-11 18:56 ` wexu [this message]
2017-09-11 18:56 ` [Qemu-devel] [Patch 2/3] vfio: invoke looking up address space wexu
2017-09-11 18:56 ` [Qemu-devel] [Patch 3/3] vfio: remove checking duplicated vfio device wexu
2017-09-11 19:18 ` [Qemu-devel] [Patch 0/3] vfio: reusing address space for the same iommu group devices Alex Williamson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1505156192-18994-2-git-send-email-wexu@redhat.com \
    --to=wexu@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=marcel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).