* [PATCH 1/2] kvm: unmap pages from the iommu when slots are removed
2012-03-29 20:10 [PATCH 0/2] kvm: iommu cleanup Alex Williamson
@ 2012-03-29 20:10 ` Alex Williamson
2012-03-29 20:10 ` [PATCH 2/2] kvm: unpin guest and free iommu domain after deassign last device Alex Williamson
1 sibling, 0 replies; 3+ messages in thread
From: Alex Williamson @ 2012-03-29 20:10 UTC (permalink / raw)
To: kvm; +Cc: alex.williamson, avi, jan.kiszka
We've been adding new mappings, but not destroying old mappings.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
include/linux/kvm_host.h | 6 ++++++
virt/kvm/iommu.c | 7 ++++++-
virt/kvm/kvm_main.c | 5 +++--
3 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 900c763..bc21720 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -593,6 +593,7 @@ void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
#ifdef CONFIG_IOMMU_API
int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
+void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
int kvm_iommu_map_guest(struct kvm *kvm);
int kvm_iommu_unmap_guest(struct kvm *kvm);
int kvm_assign_device(struct kvm *kvm,
@@ -606,6 +607,11 @@ static inline int kvm_iommu_map_pages(struct kvm *kvm,
return 0;
}
+static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
+ struct kvm_memory_slot *slot)
+{
+}
+
static inline int kvm_iommu_map_guest(struct kvm *kvm)
{
return -ENODEV;
diff --git a/virt/kvm/iommu.c b/virt/kvm/iommu.c
index a457d21..fec1723 100644
--- a/virt/kvm/iommu.c
+++ b/virt/kvm/iommu.c
@@ -310,6 +310,11 @@ static void kvm_iommu_put_pages(struct kvm *kvm,
}
}
+void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
+{
+ kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
+}
+
static int kvm_iommu_unmap_memslots(struct kvm *kvm)
{
int idx;
@@ -320,7 +325,7 @@ static int kvm_iommu_unmap_memslots(struct kvm *kvm)
slots = kvm_memslots(kvm);
kvm_for_each_memslot(memslot, slots)
- kvm_iommu_put_pages(kvm, memslot->base_gfn, memslot->npages);
+ kvm_iommu_unmap_pages(kvm, memslot);
srcu_read_unlock(&kvm->srcu, idx);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index a91f980..c4ac57e 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -873,12 +873,13 @@ skip_lpage:
if (r)
goto out_free;
- /* map the pages in iommu page table */
+ /* map/unmap the pages in iommu page table */
if (npages) {
r = kvm_iommu_map_pages(kvm, &new);
if (r)
goto out_free;
- }
+ } else
+ kvm_iommu_unmap_pages(kvm, &old);
r = -ENOMEM;
slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] kvm: unpin guest and free iommu domain after deassign last device
2012-03-29 20:10 [PATCH 0/2] kvm: iommu cleanup Alex Williamson
2012-03-29 20:10 ` [PATCH 1/2] kvm: unmap pages from the iommu when slots are removed Alex Williamson
@ 2012-03-29 20:10 ` Alex Williamson
1 sibling, 0 replies; 3+ messages in thread
From: Alex Williamson @ 2012-03-29 20:10 UTC (permalink / raw)
To: kvm; +Cc: alex.williamson, avi, jan.kiszka
Unpin the guest and free the iommu domain if there are no longer
any devices attached.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
virt/kvm/assigned-dev.c | 3 +++
virt/kvm/iommu.c | 1 +
2 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/virt/kvm/assigned-dev.c b/virt/kvm/assigned-dev.c
index 758e3b3..9d6b31a 100644
--- a/virt/kvm/assigned-dev.c
+++ b/virt/kvm/assigned-dev.c
@@ -673,6 +673,9 @@ static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
kvm_free_assigned_device(kvm, match);
+ if (list_empty(&kvm->arch.assigned_dev_head))
+ kvm_iommu_unmap_guest(kvm);
+
out:
mutex_unlock(&kvm->lock);
return r;
diff --git a/virt/kvm/iommu.c b/virt/kvm/iommu.c
index fec1723..ee4c236 100644
--- a/virt/kvm/iommu.c
+++ b/virt/kvm/iommu.c
@@ -342,5 +342,6 @@ int kvm_iommu_unmap_guest(struct kvm *kvm)
kvm_iommu_unmap_memslots(kvm);
iommu_domain_free(domain);
+ kvm->arch.iommu_domain = NULL;
return 0;
}
^ permalink raw reply related [flat|nested] 3+ messages in thread