Commit b7f69c55 authored by Alex Williamson's avatar Alex Williamson Committed by Marcelo Tosatti

KVM: Minor memory slot optimization

If a slot is removed or moved in the guest physical address space, we
first allocate and install a new slot array with the invalidated
entry.  The old array is then freed.  We then proceed to allocate yet
another slot array to install the permanent replacement.  Re-use the
original array when this occurs and avoid the extra kfree/kmalloc.
Reviewed-by: default avatarGleb Natapov <gleb@redhat.com>
Signed-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
parent e40f193f
...@@ -716,7 +716,7 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -716,7 +716,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
unsigned long npages; unsigned long npages;
struct kvm_memory_slot *memslot, *slot; struct kvm_memory_slot *memslot, *slot;
struct kvm_memory_slot old, new; struct kvm_memory_slot old, new;
struct kvm_memslots *slots, *old_memslots; struct kvm_memslots *slots = NULL, *old_memslots;
r = check_memory_region_flags(mem); r = check_memory_region_flags(mem);
if (r) if (r)
...@@ -832,18 +832,25 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -832,18 +832,25 @@ int __kvm_set_memory_region(struct kvm *kvm,
* - kvm_is_visible_gfn (mmu_check_roots) * - kvm_is_visible_gfn (mmu_check_roots)
*/ */
kvm_arch_flush_shadow_memslot(kvm, slot); kvm_arch_flush_shadow_memslot(kvm, slot);
kfree(old_memslots); slots = old_memslots;
} }
r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc); r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
if (r) if (r)
goto out_free; goto out_slots;
r = -ENOMEM; r = -ENOMEM;
/*
* We can re-use the old_memslots from above, the only difference
* from the currently installed memslots is the invalid flag. This
* will get overwritten by update_memslots anyway.
*/
if (!slots) {
slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots), slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
GFP_KERNEL); GFP_KERNEL);
if (!slots) if (!slots)
goto out_free; goto out_free;
}
/* map new memory slot into the iommu */ /* map new memory slot into the iommu */
if (npages) { if (npages) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment