Skip to content

Commit 32f6daa

Browse files
Alex Williamsonmatosatti
authored andcommitted
KVM: unmap pages from the iommu when slots are removed
We've been adding new mappings, but not destroying old mappings. This can lead to a page leak as pages are pinned using get_user_pages, but only unpinned with put_page if they still exist in the memslots list on vm shutdown. A memslot that is destroyed while an iommu domain is enabled for the guest will therefore result in an elevated page reference count that is never cleared. Additionally, without this fix, the iommu is only programmed with the first translation for a gpa. This can result in peer-to-peer errors if a mapping is destroyed and replaced by a new mapping at the same gpa as the iommu will still be pointing to the original, pinned memory address. Signed-off-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent f19a0c2 commit 32f6daa

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

include/linux/kvm_host.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
596596

597597
#ifdef CONFIG_IOMMU_API
598598
int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
599+
void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
599600
int kvm_iommu_map_guest(struct kvm *kvm);
600601
int kvm_iommu_unmap_guest(struct kvm *kvm);
601602
int kvm_assign_device(struct kvm *kvm,
@@ -609,6 +610,11 @@ static inline int kvm_iommu_map_pages(struct kvm *kvm,
609610
return 0;
610611
}
611612

613+
static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
614+
struct kvm_memory_slot *slot)
615+
{
616+
}
617+
612618
static inline int kvm_iommu_map_guest(struct kvm *kvm)
613619
{
614620
return -ENODEV;

virt/kvm/iommu.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ static void kvm_iommu_put_pages(struct kvm *kvm,
310310
}
311311
}
312312

313+
void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
314+
{
315+
kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
316+
}
317+
313318
static int kvm_iommu_unmap_memslots(struct kvm *kvm)
314319
{
315320
int idx;
@@ -320,7 +325,7 @@ static int kvm_iommu_unmap_memslots(struct kvm *kvm)
320325
slots = kvm_memslots(kvm);
321326

322327
kvm_for_each_memslot(memslot, slots)
323-
kvm_iommu_put_pages(kvm, memslot->base_gfn, memslot->npages);
328+
kvm_iommu_unmap_pages(kvm, memslot);
324329

325330
srcu_read_unlock(&kvm->srcu, idx);
326331

virt/kvm/kvm_main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,13 @@ int __kvm_set_memory_region(struct kvm *kvm,
808808
if (r)
809809
goto out_free;
810810

811-
/* map the pages in iommu page table */
811+
/* map/unmap the pages in iommu page table */
812812
if (npages) {
813813
r = kvm_iommu_map_pages(kvm, &new);
814814
if (r)
815815
goto out_free;
816-
}
816+
} else
817+
kvm_iommu_unmap_pages(kvm, &old);
817818

818819
r = -ENOMEM;
819820
slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),

0 commit comments

Comments
 (0)