From: David Gibson <david@gibson.dropbear.id.au>
To: alex.williamson@redhat.com, aik@ozlabs.ru
Cc: qemu-devel@nongnu.org, David Gibson <david@gibson.dropbear.id.au>
Subject: [Qemu-devel] [PATCH 5/5] vfio: Only use memory listeners when appropriate
Date: Wed, 24 Apr 2013 22:01:21 +1000 [thread overview]
Message-ID: <1366804881-553-6-git-send-email-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <1366804881-553-1-git-send-email-david@gibson.dropbear.id.au>
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(-)
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;
--
1.7.10.4
prev parent reply other threads:[~2013-04-24 12:12 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-24 12:01 [Qemu-devel] [0/5] RFC: Preparations for supporting VFIO with guest IOMMUs David Gibson
2013-04-24 12:01 ` [Qemu-devel] [PATCH 1/5] pci: Simpler implementation of PCI_COMMAND_MASTER bit David Gibson
2013-04-24 12:36 ` Paolo Bonzini
2013-04-24 13:06 ` David Gibson
2013-04-24 12:01 ` [Qemu-devel] [PATCH 2/5] pci: Don't create an address space object for every PCI device David Gibson
2013-04-24 12:34 ` Paolo Bonzini
2013-04-24 13:07 ` David Gibson
2013-04-24 12:01 ` [Qemu-devel] [PATCH 3/5] vfio: Associate VFIO groups with DMAContexts David Gibson
2013-04-24 12:01 ` [Qemu-devel] [PATCH 4/5] vfio: Move container list to DMAContext David Gibson
2013-04-24 15:12 ` Alex Williamson
2013-04-24 16:33 ` Paolo Bonzini
2013-04-25 6:36 ` David Gibson
2013-04-26 8:44 ` Paolo Bonzini
2013-04-26 8:46 ` Alexey Kardashevskiy
2013-04-26 8:52 ` Paolo Bonzini
2013-04-26 8:56 ` Alexey Kardashevskiy
2013-04-26 9:08 ` Paolo Bonzini
2013-04-25 6:35 ` David Gibson
2013-04-24 12:01 ` David Gibson [this message]
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=1366804881-553-6-git-send-email-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=aik@ozlabs.ru \
--cc=alex.williamson@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).