Re: [PATCH 13/13] KVM: selftests: Introduce num-pages conversion utilities

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14/02/20 15:59, Andrew Jones wrote:
> Guests and hosts don't have to have the same page size. This means
> calculations are necessary when selecting the number of guest pages
> to allocate in order to ensure the number is compatible with the
> host. Provide utilities to help with those calculations and apply
> them where appropriate.
> 
> We also revert commit bffed38d4fb5 ("kvm: selftests: aarch64:
> dirty_log_test: fix unaligned memslot size") and then use
> vm_adjust_num_guest_pages() there instead.
> 
> Signed-off-by: Andrew Jones <drjones@xxxxxxxxxx>
> ---
>  .../selftests/kvm/demand_paging_test.c        |  8 ++---
>  tools/testing/selftests/kvm/dirty_log_test.c  | 13 ++++----
>  .../testing/selftests/kvm/include/kvm_util.h  |  8 +++++
>  .../testing/selftests/kvm/include/test_util.h |  2 ++
>  tools/testing/selftests/kvm/lib/kvm_util.c    | 32 +++++++++++++++++++
>  5 files changed, 50 insertions(+), 13 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c
> index a5e57bd63e78..a9289a9386c0 100644
> --- a/tools/testing/selftests/kvm/demand_paging_test.c
> +++ b/tools/testing/selftests/kvm/demand_paging_test.c
> @@ -164,12 +164,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus,
>  	pages += (2 * pages) / PTES_PER_4K_PT;
>  	pages += ((2 * vcpus * vcpu_memory_bytes) >> PAGE_SHIFT_4K) /
>  		 PTES_PER_4K_PT;
> -
> -	/*
> -	 * If the host is uing 64K pages, then we need the number of 4K
> -	 * guest pages to be a multiple of 16.
> -	 */
> -	pages += 16 - pages % 16;
> +	pages = vm_adjust_num_guest_pages(mode, pages);
>  
>  	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
>  
> @@ -382,6 +377,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,
>  		    "Guest memory size is not guest page size aligned.");
>  
>  	guest_num_pages = (vcpus * vcpu_memory_bytes) / guest_page_size;
> +	guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
>  
>  #ifdef __s390x__
>  	/* Round up to multiple of 1M (segment size) */

Moved this part to "fixup! KVM: selftests: Support multiple vCPUs in
demand paging test".

Paolo

> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
> index 12acf90826c1..a723333b138a 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -178,12 +178,11 @@ static void *vcpu_worker(void *data)
>  	return NULL;
>  }
>  
> -static void vm_dirty_log_verify(unsigned long *bmap)
> +static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap)
>  {
> +	uint64_t step = vm_num_host_pages(mode, 1);
>  	uint64_t page;
>  	uint64_t *value_ptr;
> -	uint64_t step = host_page_size >= guest_page_size ? 1 :
> -				guest_page_size / host_page_size;
>  
>  	for (page = 0; page < host_num_pages; page += step) {
>  		value_ptr = host_test_mem + page * host_page_size;
> @@ -291,14 +290,14 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
>  	 * case where the size is not aligned to 64 pages.
>  	 */
>  	guest_num_pages = (1ul << (DIRTY_MEM_BITS -
> -				   vm_get_page_shift(vm))) + 16;
> +				   vm_get_page_shift(vm))) + 3;
> +	guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
>  #ifdef __s390x__
>  	/* Round up to multiple of 1M (segment size) */
>  	guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
>  #endif
>  	host_page_size = getpagesize();
> -	host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
> -			 !!((guest_num_pages * guest_page_size) % host_page_size);
> +	host_num_pages = vm_num_host_pages(mode, guest_num_pages);
>  
>  	if (!phys_offset) {
>  		guest_test_phys_mem = (vm_get_max_gfn(vm) -
> @@ -369,7 +368,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
>  		kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap, 0,
>  				       host_num_pages);
>  #endif
> -		vm_dirty_log_verify(bmap);
> +		vm_dirty_log_verify(mode, bmap);
>  		iteration++;
>  		sync_global_to_guest(vm, iteration);
>  	}
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index 45c6c7ea24c5..bc7c67913fe0 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> @@ -158,6 +158,14 @@ unsigned int vm_get_page_size(struct kvm_vm *vm);
>  unsigned int vm_get_page_shift(struct kvm_vm *vm);
>  unsigned int vm_get_max_gfn(struct kvm_vm *vm);
>  
> +unsigned int vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages);
> +unsigned int vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages);
> +static inline unsigned int
> +vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
> +{
> +	return vm_num_guest_pages(mode, vm_num_host_pages(mode, num_guest_pages));
> +}
> +
>  struct kvm_userspace_memory_region *
>  kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
>  				 uint64_t end);
> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> index c921ea719ae0..a60cf4ffcc3b 100644
> --- a/tools/testing/selftests/kvm/include/test_util.h
> +++ b/tools/testing/selftests/kvm/include/test_util.h
> @@ -19,6 +19,8 @@
>  #include <fcntl.h>
>  #include "kselftest.h"
>  
> +#define getpageshift() (__builtin_ffs(getpagesize()) - 1)
> +
>  static inline int _no_printf(const char *format, ...) { return 0; }
>  
>  #ifdef DEBUG
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 5e26e24bd609..44f1ef064085 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -583,6 +583,10 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
>  	size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
>  	size_t alignment;
>  
> +	TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages,
> +		"Number of guest pages is not compatible with the host. "
> +		"Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages));
> +
>  	TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
>  		"address not on a page boundary.\n"
>  		"  guest_paddr: 0x%lx vm->page_size: 0x%x",
> @@ -1718,3 +1722,31 @@ unsigned int vm_get_max_gfn(struct kvm_vm *vm)
>  {
>  	return vm->max_gfn;
>  }
> +
> +static unsigned int vm_calc_num_pages(unsigned int num_pages,
> +				      unsigned int page_shift,
> +				      unsigned int new_page_shift,
> +				      bool ceil)
> +{
> +	unsigned int n = 1 << (new_page_shift - page_shift);
> +
> +	if (page_shift >= new_page_shift)
> +		return num_pages * (1 << (page_shift - new_page_shift));
> +
> +	return num_pages / n + !!(ceil && num_pages % n);
> +}
> +
> +unsigned int
> +vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
> +{
> +	return vm_calc_num_pages(num_guest_pages,
> +				 vm_guest_mode_params[mode].page_shift,
> +				 getpageshift(), true);
> +}
> +
> +unsigned int
> +vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages)
> +{
> +	return vm_calc_num_pages(num_host_pages, getpageshift(),
> +				 vm_guest_mode_params[mode].page_shift, false);
> +}
> 




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux