diff mbox series

KVM: x86: avoid some unnecessary copy in __x86_set_memory_region()

Message ID 1579748413-432-1-git-send-email-linmiaohe@huawei.com (mailing list archive)
State New, archived
Headers show
Series KVM: x86: avoid some unnecessary copy in __x86_set_memory_region() | expand

Commit Message

Miaohe Lin Jan. 23, 2020, 3 a.m. UTC
From: Miaohe Lin <linmiaohe@huawei.com>

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.

No functional change intended.

Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
 arch/x86/kvm/x86.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Paolo Bonzini Jan. 23, 2020, 12:15 p.m. UTC | #1
On 23/01/20 04:00, linmiaohe wrote:
> From: Miaohe Lin <linmiaohe@huawei.com>
> 
> 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@huawei.com>
> ---
>  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;
>  }
>
kernel test robot Jan. 26, 2020, 12:39 a.m. UTC | #2
Hi linmiaohe,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on kvm/linux-next]
[also build test WARNING on v5.5-rc7 next-20200124]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/linmiaohe/KVM-x86-avoid-some-unnecessary-copy-in-__x86_set_memory_region/20200125-020112
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: i386-allyesconfig (attached as .config)
compiler: gcc-7 (Debian 7.5.0-3) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>

Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

All warnings (new ones prefixed by >>):

   In file included from arch/x86/kvm/x86.c:25:0:
   arch/x86/kvm/x86.h: In function 'kvm_dr7_valid':
   arch/x86/kvm/x86.h:363:16: warning: right shift count >= width of type [-Wshift-count-overflow]
     return !(data >> 32);
                   ^~
   arch/x86/kvm/x86.c: In function '__x86_set_memory_region':
>> arch/x86/kvm/x86.c:9730:27: warning: 'npages' may be used uninitialized in this function [-Wmaybe-uninitialized]
      vm_munmap(uaddr, npages * PAGE_SIZE);
>> arch/x86/kvm/x86.c:9730:3: warning: 'uaddr' may be used uninitialized in this function [-Wmaybe-uninitialized]
      vm_munmap(uaddr, npages * PAGE_SIZE);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/npages +9730 arch/x86/kvm/x86.c

  9682	
  9683	int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
  9684	{
  9685		int i, r;
  9686		unsigned long hva, uaddr, npages;
  9687		struct kvm_memslots *slots = kvm_memslots(kvm);
  9688		struct kvm_memory_slot *slot;
  9689	
  9690		/* Called with kvm->slots_lock held.  */
  9691		if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
  9692			return -EINVAL;
  9693	
  9694		slot = id_to_memslot(slots, id);
  9695		if (size) {
  9696			if (slot->npages)
  9697				return -EEXIST;
  9698	
  9699			/*
  9700			 * MAP_SHARED to prevent internal slot pages from being moved
  9701			 * by fork()/COW.
  9702			 */
  9703			hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
  9704				      MAP_SHARED | MAP_ANONYMOUS, 0);
  9705			if (IS_ERR((void *)hva))
  9706				return PTR_ERR((void *)hva);
  9707		} else {
  9708			if (!slot->npages)
  9709				return 0;
  9710	
  9711			hva = 0;
  9712			uaddr = slot->userspace_addr;
  9713			npages = slot->npages;
  9714		}
  9715	
  9716		for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  9717			struct kvm_userspace_memory_region m;
  9718	
  9719			m.slot = id | (i << 16);
  9720			m.flags = 0;
  9721			m.guest_phys_addr = gpa;
  9722			m.userspace_addr = hva;
  9723			m.memory_size = size;
  9724			r = __kvm_set_memory_region(kvm, &m);
  9725			if (r < 0)
  9726				return r;
  9727		}
  9728	
  9729		if (!size)
> 9730			vm_munmap(uaddr, npages * PAGE_SIZE);
  9731	
  9732		return 0;
  9733	}
  9734	EXPORT_SYMBOL_GPL(__x86_set_memory_region);
  9735	

---
0-DAY kernel test infrastructure                 Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org Intel Corporation
diff mbox series

Patch

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;
 }