Skip to content

Commit 261874b

Browse files
Alex WilliamsonGleb Natapov
authored andcommitted
kvm: Force IOMMU remapping on memory slot read-only flag changes
Memory slot flags can be altered without changing other parameters of the slot. The read-only attribute is the only one the IOMMU cares about, so generate an un-map, re-map when this occurs. This also avoid unnecessarily re-mapping the slot when no IOMMU visible changes are made. Reviewed-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> Signed-off-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Gleb Natapov <gleb@redhat.com>
1 parent 3f0c3d0 commit 261874b

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

‎virt/kvm/kvm_main.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
731731
struct kvm_memory_slot *slot;
732732
struct kvm_memory_slot old, new;
733733
struct kvm_memslots *slots = NULL, *old_memslots;
734+
bool old_iommu_mapped;
734735

735736
r = check_memory_region_flags(mem);
736737
if (r)
@@ -772,6 +773,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
772773
new.npages = npages;
773774
new.flags = mem->flags;
774775

776+
old_iommu_mapped = old.npages;
777+
775778
/*
776779
* Disallow changing a memory slot's size or changing anything about
777780
* zero sized slots that doesn't involve making them non-zero.
@@ -835,6 +838,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
835838

836839
/* slot was deleted or moved, clear iommu mapping */
837840
kvm_iommu_unmap_pages(kvm, &old);
841+
old_iommu_mapped = false;
838842
/* From this point no new shadow pages pointing to a deleted,
839843
* or moved, memslot will be created.
840844
*
@@ -863,11 +867,27 @@ int __kvm_set_memory_region(struct kvm *kvm,
863867
goto out_free;
864868
}
865869

866-
/* map new memory slot into the iommu */
870+
/*
871+
* IOMMU mapping: New slots need to be mapped. Old slots need to be
872+
* un-mapped and re-mapped if their base changes or if flags that the
873+
* iommu cares about change (read-only). Base change unmapping is
874+
* handled above with slot deletion, so we only unmap incompatible
875+
* flags here. Anything else the iommu might care about for existing
876+
* slots (size changes, userspace addr changes) is disallowed above,
877+
* so any other attribute changes getting here can be skipped.
878+
*/
867879
if (npages) {
868-
r = kvm_iommu_map_pages(kvm, &new);
869-
if (r)
870-
goto out_slots;
880+
if (old_iommu_mapped &&
881+
((new.flags ^ old.flags) & KVM_MEM_READONLY)) {
882+
kvm_iommu_unmap_pages(kvm, &old);
883+
old_iommu_mapped = false;
884+
}
885+
886+
if (!old_iommu_mapped) {
887+
r = kvm_iommu_map_pages(kvm, &new);
888+
if (r)
889+
goto out_slots;
890+
}
871891
}
872892

873893
/* actual memory is freed via old in kvm_free_physmem_slot below */

0 commit comments

Comments
 (0)