Commit b7f69c555ca430129b6cde81e9f0927531420c5c

Authored by Alex Williamson
Committed by Marcelo Tosatti
1 parent e40f193f5b

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: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Showing 1 changed file with 14 additions and 7 deletions Side-by-side Diff

... ... @@ -716,7 +716,7 @@
716 716 unsigned long npages;
717 717 struct kvm_memory_slot *memslot, *slot;
718 718 struct kvm_memory_slot old, new;
719   - struct kvm_memslots *slots, *old_memslots;
  719 + struct kvm_memslots *slots = NULL, *old_memslots;
720 720  
721 721 r = check_memory_region_flags(mem);
722 722 if (r)
723 723  
724 724  
... ... @@ -832,18 +832,25 @@
832 832 * - kvm_is_visible_gfn (mmu_check_roots)
833 833 */
834 834 kvm_arch_flush_shadow_memslot(kvm, slot);
835   - kfree(old_memslots);
  835 + slots = old_memslots;
836 836 }
837 837  
838 838 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
839 839 if (r)
840   - goto out_free;
  840 + goto out_slots;
841 841  
842 842 r = -ENOMEM;
843   - slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
844   - GFP_KERNEL);
845   - if (!slots)
846   - goto out_free;
  843 + /*
  844 + * We can re-use the old_memslots from above, the only difference
  845 + * from the currently installed memslots is the invalid flag. This
  846 + * will get overwritten by update_memslots anyway.
  847 + */
  848 + if (!slots) {
  849 + slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  850 + GFP_KERNEL);
  851 + if (!slots)
  852 + goto out_free;
  853 + }
847 854  
848 855 /* map new memory slot into the iommu */
849 856 if (npages) {