Re: [PATCH] KVM: x86: avoid some unnecessary copy in __x86_set_memory_region()

From: Paolo Bonzini
Date: Thu Jan 23 2020 - 07:15:42 EST


On 23/01/20 04:00, linmiaohe wrote:
> From: Miaohe Lin <linmiaohe@xxxxxxxxxx>
>
> Only userspace_addr and npages are passed to vm_munmap() when remove a
> memory region. So we shouldn't copy the integral kvm_memory_slot struct.

The compiler should be able to do this change, so I prefer to keep the
old code. Also, moving the assignments inside the "if" risks causing
uninitialized variable warnings, even though indeed they are only used
if size == 0.

Thanks,

Paolo

> No functional change intended.
>
> Signed-off-by: Miaohe Lin <linmiaohe@xxxxxxxxxx>
> ---
> arch/x86/kvm/x86.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index d1faa74981d9..767f29877938 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -9735,9 +9735,9 @@ void kvm_arch_sync_events(struct kvm *kvm)
> int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
> {
> int i, r;
> - unsigned long hva;
> + unsigned long hva, uaddr, npages;
> struct kvm_memslots *slots = kvm_memslots(kvm);
> - struct kvm_memory_slot *slot, old;
> + struct kvm_memory_slot *slot;
>
> /* Called with kvm->slots_lock held. */
> if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
> @@ -9761,9 +9761,10 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
> return 0;
>
> hva = 0;
> + uaddr = slot->userspace_addr;
> + npages = slot->npages;
> }
>
> - old = *slot;
> for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
> struct kvm_userspace_memory_region m;
>
> @@ -9778,7 +9779,7 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
> }
>
> if (!size)
> - vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
> + vm_munmap(uaddr, npages * PAGE_SIZE);
>
> return 0;
> }
>