KVM: selftests: Various fixes and cleanups
Subsystems (3)
cover KVM: selftests: Various fixes and cleanups
This series has several parts:
* First, a hack to get x86 to compile. The missing __NR_userfaultfd define should be fixed a better way.
* Then, fixups for several commits in kvm/queue. These fixups correspond to review comments that didn't have a chance to get addressed before the commits were applied.
* Next, a few unnecessary #define/#ifdef deletions.
* Then, a rework of debug and info message printing.
* Finally, an addition to the API, num-pages conversion utilities, which cleans up all the num-pages calculations.
Andrew Jones (13):
HACK: Ensure __NR_userfaultfd is defined
fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and
s390x
fixup! KVM: selftests: Support multiple vCPUs in demand paging test fixup! KVM: selftests: Add memory size parameter to the demand paging
test
fixup! KVM: selftests: Time guest demand paging
KVM: selftests: Remove unnecessary defines
KVM: selftests: aarch64: Remove unnecessary ifdefs
KVM: selftests: aarch64: Use stream when given
KVM: selftests: Rework debug message printing
KVM: selftests: Convert some printf's to pr_info's
KVM: selftests: Rename vm_guest_mode_params
KVM: selftests: Introduce vm_guest_mode_params
KVM: selftests: Introduce num-pages conversion utilities
.../selftests/kvm/demand_paging_test.c | 148 ++++++++---------- tools/testing/selftests/kvm/dirty_log_test.c | 78 +++++---- .../testing/selftests/kvm/include/kvm_util.h | 14 +- .../testing/selftests/kvm/include/test_util.h | 18 +++ .../selftests/kvm/kvm_create_max_vcpus.c | 8 +- .../selftests/kvm/lib/aarch64/processor.c | 30 +--- tools/testing/selftests/kvm/lib/kvm_util.c | 89 +++++++---- .../selftests/kvm/lib/kvm_util_internal.h | 11 -- tools/testing/selftests/kvm/lib/test_util.c | 90 ++++++----- tools/testing/selftests/kvm/s390x/resets.c | 6 +- .../selftests/kvm/x86_64/mmio_warning_test.c | 2 +- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 +- .../testing/selftests/kvm/x86_64/state_test.c | 2 +- .../kvm/x86_64/vmx_tsc_adjust_test.c | 4 +- .../selftests/kvm/x86_64/xss_msr_test.c | 2 +- 15 files changed, 258 insertions(+), 246 deletions(-)
Signature · 1 line
2.21.1
PATCH 1/13 HACK: Ensure __NR_userfaultfd is defined
Without this hack kvm/queue kvm selftests don't compile for x86_64.
--- tools/testing/selftests/kvm/demand_paging_test.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/kvm/demand_paging_test.c +4 -0
@@ -20,6 +20,10 @@#include <linux/bitops.h> #include <linux/userfaultfd.h> +#ifndef __NR_userfaultfd +#define __NR_userfaultfd 282 +#endif +#include "test_util.h" #include "kvm_util.h" #include "processor.h"
Signature · 1 line
2.21.1
Paolo Bonzini
maintainerRe: [PATCH 01/13] HACK: Ensure __NR_userfaultfd is defined
Quoted 11 lines · Andrew Jones
Without this hack kvm/queue kvm selftests don't compile for
x86_64.--- tools/testing/selftests/kvm/demand_paging_test.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/kvm/demand_paging_test.c +0
-0
tools/testing/selftests/kvm/demand_paging_test.c line 29
Quoted 10 lines
tools/testing/selftests/kvm/demand_paging_test.c +4
-0@@ -20,6 +20,10 @@#include <linux/bitops.h> #include <linux/userfaultfd.h> +#ifndef __NR_userfaultfd +#define __NR_userfaultfd 282 +#endif +#include "test_util.h" #include "kvm_util.h" #include "processor.h"
This is because we're getting a limited version of asm/unistd.h from tools/arch/x86/include/asm/unistd_64.h. So:
------------ 8< ----------------
From: Paolo Bonzini <[email protected]>
Subject: [PATCH] fixup! KVM: selftests: Add demand paging content to the demand paging test
Without this, kvm selftests don't compile for x86_64 on old-enough userspace.
tools/arch/x86/include/asm/unistd_64.h +3 -0
@@ -1,4 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NR_userfaultfd +#define __NR_userfaultfd 282 +#endif #ifndef __NR_perf_event_open# define __NR_perf_event_open 298#endif
tools/testing/selftests/kvm/demand_paging_test.c +15 -0
@@ -13,6 +13,7 @@#include <stdlib.h> #include <sys/syscall.h> #include <unistd.h> +#include <asm/unistd.h> #include <time.h> #include <poll.h> #include <pthread.h>
@@ -24,6 +25,8 @@#include "kvm_util.h" #include "processor.h" +#ifdef __NR_userfaultfd +/* The memory slot index demand page */ #define TEST_MEM_SLOT_INDEX 1
@@ -678,3 +681,15 @@ int main(int argc, char *argv[]) return 0; } + +#else /* __NR_userfaultfd */ + +#warning "missing __NR_userfaultfd definition" + +int main (void ) +{ +printf ("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n" ); +return KSFT_SKIP ; +} + +#endif /* __NR_userfaultfd */
Paolo
PATCH 2/13 fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x
In reply to Andrew Jones--- .../selftests/kvm/lib/aarch64/processor.c | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/lib/aarch64/processor.c +4 -20
@@ -334,36 +334,20 @@ void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)aarch64_vcpu_add_default (vm, vcpuid,NULL , guest_code); } -/* VM VCPU Args Set -* -* Input Args: -* vm - Virtual Machine -* vcpuid - VCPU ID -* num - number of arguments -* ... - arguments, each of type uint64_t -* -* Output Args: None -* -* Return: None -* -* Sets the first num function input arguments to the values -* given as variable args. Each of the variable args is expected to -* be of type uint64_t. The registers set by this function are r0-r7. -*/ void vcpu_args_set (struct kvm_vm *vm,uint32_t vcpuid,unsigned int num, ...) {va_list ap;int i;TEST_ASSERT (num >=1 && num <=8 ,"Unsupported number of args,\n" -" num: %u\n" , - num); +" num: %u\n" , num);va_start (ap, num); -for (i =0 ; i < num; i++) -set_reg (vm, vcpuid,ARM64_CORE_REG (regs.regs[num]), +for (i =0 ; i < num; i++) { +set_reg (vm, vcpuid,ARM64_CORE_REG (regs.regs[i]),va_arg (ap, uint64_t)); + }va_end (ap); }
Signature · 1 line
2.21.1
Re: [PATCH 02/13] fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x
Quoted 4 lines · Andrew Jones
[Fixed array index (num => i) and made some style changes.]Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/lib/aarch64/processor.c | 24 ++++---------------
subject says s390, the patch not.
Re: [PATCH 02/13] fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x
Quoted 10 lines · Christian Borntraeger
Quoted 4 lines · Andrew Jones
[Fixed array index (num => i) and made some style changes.]Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/lib/aarch64/processor.c | 24 ++++---------------subject says s390, the patch not.
It's a "fixup!" patch. The original subject has s390, so this one must as well.
Thanks,
drew
Re: [PATCH 02/13] fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x
In reply to Andrew JonesQuoted 21 lines · Andrew Jones
[Fixed array index (num => i) and made some style changes.]Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/lib/aarch64/processor.c | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-)tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-6@@ -334,36 +334,20 @@ void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)aarch64_vcpu_add_default (vm, vcpuid,NULL , guest_code); } -/* VM VCPU Args Set -* -* Input Args: -* vm - Virtual Machine -* vcpuid - VCPU ID -* num - number of arguments
tools/testing/selftests/kvm/lib/aarch64/processor.c void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code) · line 352
Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-10-* ... - arguments, each of type uint64_t -* -* Output Args: None -* -* Return: None -* -* Sets the first num function input arguments to the values -* given as variable args. Each of the variable args is expected to -* be of type uint64_t. The registers set by this function are r0-r7. -*/
I'm sad to see this comment go. I realize it might be more verbose than necessary, but calling out that the args will all be interpreted as uint_64s and which registers are set feels like useful context to have here.
Quoted 8 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-2void vcpu_args_set (struct kvm_vm *vm,uint32_t vcpuid,unsigned int num, ...) {va_list ap;int i; TEST_ASSERT(num >= 1 && num <= 8, "Unsupported number of args,\n" -" num: %u\n" , - num);
tools/testing/selftests/kvm/lib/aarch64/processor.c void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code) · line 350
Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +4
-2+ " num: %u\n", num);va_start (ap, num); -for (i =0 ; i < num; i++) -set_reg (vm, vcpuid,ARM64_CORE_REG (regs.regs[num]), +for (i =0 ; i < num; i++) { +set_reg (vm, vcpuid,ARM64_CORE_REG (regs.regs[i]),va_arg (ap, uint64_t)); + }
Woops, I should have caught this in the original demand paging test series, but didn't notice because this function was only ever called with one argument.
Thank you for fixing this.
Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-1va_end (ap); } --2.21.1
Re: [PATCH 02/13] fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x
Quoted 28 lines · Ben Gardon
Quoted 27 lines · Andrew Jones
[Fixed array index (num => i) and made some style changes.]Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/lib/aarch64/processor.c | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-)tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-12@@ -334,36 +334,20 @@ void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)aarch64_vcpu_add_default (vm, vcpuid,NULL , guest_code); } -/* VM VCPU Args Set -* -* Input Args: -* vm - Virtual Machine -* vcpuid - VCPU ID -* num - number of arguments -* ... - arguments, each of type uint64_t -* -* Output Args: None -* -* Return: None -*
Quoted 8 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code) · line 352
Quoted 4 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-4-* Sets the first num function input arguments to the values -* given as variable args. Each of the variable args is expected to -* be of type uint64_t. The registers set by this function are r0-r7. -*/ I'm sad to see this comment go. I realize it might be more verbose than necessary, but calling out that the args will all be interpreted as
uint_64sand which registers are set feels like useful context to have here.
For me the code makes that super obvious, and I prefer not to describe what code does. Also, I'd put these type of comment blocks, written more generally, in the header files if they're functions that are implemented by all architectures, rather than duplicating them in each source file.
Quoted 19 lines
Quoted 9 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-2void vcpu_args_set (struct kvm_vm *vm,uint32_t vcpuid,unsigned int num, ...) {va_list ap;int i; TEST_ASSERT(num >= 1 && num <= 8, "Unsupported number of args,\n" -" num: %u\n" , - num);tools/testing/selftests/kvm/lib/aarch64/processor.c void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code) · line 350
Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +4
-2+ " num: %u\n", num);va_start (ap, num); -for (i =0 ; i < num; i++) -set_reg (vm, vcpuid,ARM64_CORE_REG (regs.regs[num]), +for (i =0 ; i < num; i++) { +set_reg (vm, vcpuid,ARM64_CORE_REG (regs.regs[i]),va_arg (ap, uint64_t)); + }
Quoted 12 lines
Woops, I should have caught this in the original demand paging test series, but didn't notice because this function was only ever called with one argument.
Thank you for fixing this.Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-1va_end (ap); } --2.21.1
Thanks,
drew
Paolo Bonzini
maintainerRe: [PATCH 02/13] fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x
Quoted 4 lines · Andrew Jones
For me the code makes that super obvious, and I prefer not to describe what code does. Also, I'd put these type of comment blocks, written more generally, in the header files if they're functions that are implemented by all architectures, rather than duplicating them in each source file.
This makes sense. For now I've restored the comment, but moving them to the .h files is a good idea.
Paolo
PATCH 3/13 fixup! KVM: selftests: Support multiple vCPUs in demand paging test
In reply to Andrew Jones[guest_code() can't return, use GUEST_ASSERT(). Ensure the number
of guests pages is compatible with the host.]
--- tools/testing/selftests/kvm/demand_paging_test.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +8 -3
@@ -115,9 +115,8 @@ static void guest_code(uint32_t vcpu_id)uint64_t pages;int i; -/* Return to signal error if vCPU args data structure is courrupt. */ -if (vcpu_args[vcpu_id].vcpu_id != vcpu_id) -return ; +/* Make sure vCPU args data structure is not corrupt. */ +GUEST_ASSERT (vcpu_args[vcpu_id].vcpu_id == vcpu_id); gva = vcpu_args[vcpu_id].gva; pages = vcpu_args[vcpu_id].pages;
@@ -186,6 +185,12 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus, 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 ; + vm =_vm_create (mode, pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );#ifdef __x86_64__
Signature · 1 line
2.21.1
Re: [PATCH 03/13] fixup! KVM: selftests: Support multiple vCPUs in demand paging test
Quoted 25 lines · Andrew Jones
[
guest_code()can't return, useGUEST_ASSERT(). Ensure the number
of guests pages is compatible with the host.]Signed-off-by Andrew Jones <[email protected]>--- tools/testing/selftests/kvm/demand_paging_test.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)tools/testing/selftests/kvm/demand_paging_test.c +2
-3@@ -115,9 +115,8 @@ static void guest_code(uint32_t vcpu_id)uint64_t pages;int i; -/* Return to signal error if vCPU args data structure is courrupt. */ -if (vcpu_args[vcpu_id].vcpu_id != vcpu_id) -return ; +/* Make sure vCPU args data structure is not corrupt. */ +GUEST_ASSERT (vcpu_args[vcpu_id].vcpu_id == vcpu_id); gva = vcpu_args[vcpu_id].gva; pages = vcpu_args[vcpu_id].pages;
tools/testing/selftests/kvm/demand_paging_test.c static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus, · line 189
Quoted 5 lines
tools/testing/selftests/kvm/demand_paging_test.c +2
-0@@ -186,6 +185,12 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus, 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
s/uing/using
tools/testing/selftests/kvm/demand_paging_test.c static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus, · line 193
Quoted 4 lines
tools/testing/selftests/kvm/demand_paging_test.c +4
-0+* guest pages to be a multiple of 16. +*/ + pages +=16 - pages %16 ; +
Could we use some derivative of getpagesize() here instead? e.g.ASSERT(getpagesize() >= (1 << PAGE_SHIFT_4K)); ASSERT(!(getpagesize() % (1 << PAGE_SHIFT_4K))); pages_4k_per_host_page = getpagesize() / (1 << PAGE_SHIFT_4K); pages += pages_4k_per_host_page - pages % pages_4k_per_host_page;
Quoted 6 lines
tools/testing/selftests/kvm/demand_paging_test.c +0
-1vm =_vm_create (mode, pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );#ifdef __x86_64__ --2.21.1
PATCH 4/13 fixup! KVM: selftests: Add memory size parameter to the demand paging test
In reply to Andrew Jones[Rewrote parse_size() to simplify and provide user more flexibility as
to how sizes are input. Also fixed size overflow assert.]
--- tools/testing/selftests/kvm/lib/test_util.c | 76 +++++++++------------ 1 file changed, 33 insertions(+), 43 deletions(-)
tools/testing/selftests/kvm/lib/test_util.c +33 -43
@@ -4,58 +4,48 @@* * Copyright (C) 2020, Google LLC. */ - -#include "test_util.h" - +#include <stdlib.h> #include <ctype.h> +#include <limits.h> +#include "test_util.h" /* * Parses "[0-9]+[kmgt]?". */ size_t parse_size (const char *size) { -size_t len =strlen (size); -size_t i; -size_t scale_shift =0 ;size_t base; - -TEST_ASSERT (len >0 ,"Need at least 1 digit in '%s'" , size); - -/* Find the first letter in the string, indicating scale. */ -for (i =0 ; i < len; i++) { -if (!isdigit (size[i])) { -TEST_ASSERT (i >0 ,"Need at least 1 digit in '%s'" , - size); -TEST_ASSERT (i == len -1 , -"Expected letter at the end in '%s'." , - size); -switch (tolower (size[i])) { -case 't' : - scale_shift =40 ; -break ; -case 'g' : - scale_shift =30 ; -break ; -case 'm' : - scale_shift =20 ; -break ; -case 'k' : - scale_shift =10 ; -break ; -default : -TEST_ASSERT (false,"Unknown size letter %c" , - size[i]); - } - } +char *scale; +int shift =0 ; + +TEST_ASSERT (size &&isdigit (size[0 ]),"Need at least one digit in '%s'" , size); + + base =strtoull (size, &scale,0 ); + +TEST_ASSERT (base !=ULLONG_MAX ,"Overflow parsing size!" ); + +switch (tolower (*scale)) { +case 't' : + shift =40 ; +break ; +case 'g' : + shift =30 ; +break ; +case 'm' : + shift =20 ; +break ; +case 'k' : + shift =10 ; +break ; +case 'b' : +case '\0' : + shift =0 ; +break ; +default : +TEST_ASSERT (false,"Unknown size letter %c" , *scale); } -TEST_ASSERT (scale_shift <8 *sizeof (size_t ), -"Overflow parsing scale!" ); - - base =atoi (size); - -TEST_ASSERT (!(base & ~((1 << (sizeof (size_t ) - scale_shift)) -1 )), -"Overflow parsing size!" ); +TEST_ASSERT ((base << shift) >> shift == base,"Overflow scaling size!" ); -return base << scale_shift; +return base << shift; }
Signature · 1 line
2.21.1
Re: [PATCH 04/13] fixup! KVM: selftests: Add memory size parameter to the demand paging test
Quoted 4 lines · Andrew Jones
[Rewrote
parse_size()to simplify and provide user more flexibility as
to how sizes are input. Also fixed size overflow assert.]Signed-off-by Andrew Jones <[email protected]>
Great cleanup and fix, thank you.
Quoted 103 lines
--- tools/testing/selftests/kvm/lib/test_util.c | 76 +++++++++------------ 1 file changed, 33 insertions(+), 43 deletions(-)tools/testing/selftests/kvm/lib/test_util.c +33
-44@@ -4,58 +4,48 @@* * Copyright (C) 2020, Google LLC. */ - -#include "test_util.h" - +#include <stdlib.h> #include <ctype.h> +#include <limits.h> +#include "test_util.h" /* * Parses "[0-9]+[kmgt]?". */ size_t parse_size (const char *size) { -size_t len =strlen (size); -size_t i; -size_t scale_shift =0 ;size_t base; - -TEST_ASSERT (len >0 ,"Need at least 1 digit in '%s'" , size); - -/* Find the first letter in the string, indicating scale. */ -for (i =0 ; i < len; i++) { -if (!isdigit (size[i])) { -TEST_ASSERT (i >0 ,"Need at least 1 digit in '%s'" , - size); -TEST_ASSERT (i == len -1 , -"Expected letter at the end in '%s'." , - size); -switch (tolower (size[i])) { -case 't' : - scale_shift =40 ; -break ; -case 'g' : - scale_shift =30 ; -break ; -case 'm' : - scale_shift =20 ; -break ; -case 'k' : - scale_shift =10 ; -break ; -default : -TEST_ASSERT (false,"Unknown size letter %c" , - size[i]); - } - } +char *scale; +int shift =0 ; + +TEST_ASSERT (size &&isdigit (size[0 ]),"Need at least one digit in '%s'" , size); + + base =strtoull (size, &scale,0 ); + +TEST_ASSERT (base !=ULLONG_MAX ,"Overflow parsing size!" ); + +switch (tolower (*scale)) { +case 't' : + shift =40 ; +break ; +case 'g' : + shift =30 ; +break ; +case 'm' : + shift =20 ; +break ; +case 'k' : + shift =10 ; +break ; +case 'b' : +case '\0' : + shift =0 ; +break ; +default : +TEST_ASSERT (false,"Unknown size letter %c" , *scale); } -TEST_ASSERT (scale_shift <8 *sizeof (size_t ), -"Overflow parsing scale!" ); - - base =atoi (size); - -TEST_ASSERT (!(base & ~((1 << (sizeof (size_t ) - scale_shift)) -1 )), -"Overflow parsing size!" ); +TEST_ASSERT ((base << shift) >> shift == base,"Overflow scaling size!" ); -return base << scale_shift; +return base << shift; } --2.21.1
PATCH 5/13 fixup! KVM: selftests: Time guest demand paging
In reply to Andrew Jones--- .../selftests/kvm/demand_paging_test.c | 42 +++++-------------- .../testing/selftests/kvm/include/test_util.h | 3 ++ tools/testing/selftests/kvm/lib/test_util.c | 20 +++++++++ 3 files changed, 34 insertions(+), 31 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +11 -31
@@ -74,26 +74,6 @@ static uint64_t guest_test_phys_mem;*/ static uint64_t guest_test_virt_mem =DEFAULT_GUEST_TEST_MEM ; -int64_t to_ns (struct timespec ts) -{ -return (int64_t )ts.tv_nsec +1000000000LL * (int64_t )ts.tv_sec; -} - -struct timespec diff (struct timespec start,struct timespec end) -{ -struct timespec temp; - -if ((end.tv_nsec-start.tv_nsec) <0 ) { - temp.tv_sec = end.tv_sec - start.tv_sec -1 ; - temp.tv_nsec =1000000000 + end.tv_nsec - start.tv_nsec; - }else { - temp.tv_sec = end.tv_sec - start.tv_sec; - temp.tv_nsec = end.tv_nsec - start.tv_nsec; - } - -return temp; -} -struct vcpu_args {uint64_t gva;uint64_t pages;
@@ -157,8 +137,8 @@ static void *vcpu_worker(void *data)clock_gettime (CLOCK_MONOTONIC , &end);PER_VCPU_DEBUG ("vCPU %d execution time: %lld.%.9lds\n" , vcpu_id, - (long long )(diff (start, end).tv_sec), -diff (start, end).tv_nsec); + (long long )(timespec_diff (start, end).tv_sec), +timespec_diff (start, end).tv_nsec);return NULL ; }
@@ -226,7 +206,7 @@ static int handle_uffd_page_request(int uffd, uint64_t addr)clock_gettime (CLOCK_MONOTONIC , &end);PER_PAGE_DEBUG ("UFFDIO_COPY %d \t%lld ns\n" , tid, - (long long )to_ns (diff (start, end))); + (long long )timespec_to_ns (timespec_diff (start, end)));PER_PAGE_DEBUG ("Paged in %ld bytes at 0x%lx from thread %d\n" , host_page_size, addr, tid);
@@ -321,10 +301,10 @@ static void *uffd_handler_thread_fn(void *arg)clock_gettime (CLOCK_MONOTONIC , &end);PER_VCPU_DEBUG ("userfaulted %ld pages over %lld.%.9lds. (%f/sec)\n" , - pages, (long long )(diff (start, end).tv_sec), -diff (start, end).tv_nsec, pages / - ((double )diff (start, end).tv_sec + - (double )diff (start, end).tv_nsec /100000000.0 )); + pages, (long long )(timespec_diff (start, end).tv_sec), +timespec_diff (start, end).tv_nsec, pages / + ((double )timespec_diff (start, end).tv_sec + + (double )timespec_diff (start, end).tv_nsec /100000000.0 ));return NULL ; }
@@ -432,7 +412,6 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,DEBUG ("guest physical test memory offset: 0x%lx\n" , guest_test_phys_mem); -/* Add an extra memory slot for testing demand paging */ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, guest_test_phys_mem,
@@ -548,10 +527,11 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, }DEBUG ("Total guest execution time: %lld.%.9lds\n" , - (long long )(diff (start, end).tv_sec),diff (start, end).tv_nsec); + (long long )(timespec_diff (start, end).tv_sec), +timespec_diff (start, end).tv_nsec);DEBUG ("Overall demand paging rate: %f pgs/sec\n" , - guest_num_pages / ((double )diff (start, end).tv_sec + - (double )diff (start, end).tv_nsec /100000000.0 )); + guest_num_pages / ((double )timespec_diff (start, end).tv_sec + + (double )timespec_diff (start, end).tv_nsec /100000000.0 ));ucall_uninit (vm);kvm_vm_free (vm);
tools/testing/selftests/kvm/include/test_util.h +3 -0
@@ -41,4 +41,7 @@ void test_assert(bool exp, const char *exp_str,size_t parse_size (const char *size); +int64_t timespec_to_ns (struct timespec ts); +struct timespec timespec_diff (struct timespec start,struct timespec end); +#endif /* SELFTEST_KVM_TEST_UTIL_H */
tools/testing/selftests/kvm/lib/test_util.c +20 -0
@@ -49,3 +49,23 @@ size_t parse_size(const char *size)return base << shift; } + +int64_t timespec_to_ns (struct timespec ts) +{ +return (int64_t )ts.tv_nsec +1000000000LL * (int64_t )ts.tv_sec; +} + +struct timespec timespec_diff (struct timespec start,struct timespec end) +{ +struct timespec temp; + +if ((end.tv_nsec - start.tv_nsec) <0 ) { + temp.tv_sec = end.tv_sec - start.tv_sec -1 ; + temp.tv_nsec =1000000000LL + end.tv_nsec - start.tv_nsec; + }else { + temp.tv_sec = end.tv_sec - start.tv_sec; + temp.tv_nsec = end.tv_nsec - start.tv_nsec; + } + +return temp; +}
Signature · 1 line
2.21.1
PATCH 6/13 KVM: selftests: Remove unnecessary defines
In reply to Andrew JonesBITS_PER_LONG and friends are provided by linux/bitops.h
--- tools/testing/selftests/kvm/lib/kvm_util_internal.h | 11 ----------- 1 file changed, 11 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util_internal.h +0 -11
@@ -12,17 +12,6 @@#define KVM_DEV_PATH "/dev/kvm" -#ifndef BITS_PER_BYTE -#define BITS_PER_BYTE 8 -#endif - -#ifndef BITS_PER_LONG -#define BITS_PER_LONG (BITS_PER_BYTE * sizeof(long)) -#endif - -#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d)) -#define BITS_TO_LONGS (nr) DIV_ROUND_UP(nr, BITS_PER_LONG) -struct userspace_mem_region {struct userspace_mem_region *next, *prev;struct kvm_userspace_memory_region region;
Signature · 1 line
2.21.1
PATCH 7/13 KVM: selftests: aarch64: Remove unnecessary ifdefs
In reply to Andrew Jones--- .../selftests/kvm/demand_paging_test.c | 21 ++++++++----------- tools/testing/selftests/kvm/dirty_log_test.c | 21 ++++++++----------- 2 files changed, 18 insertions(+), 24 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +9 -12
@@ -422,9 +422,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,virt_map (vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages * guest_page_size,0 ); -#ifdef __aarch64__ucall_init (vm,NULL ); -#endif guest_data_prototype =malloc (host_page_size); TEST_ASSERT(guest_data_prototype,
@@ -594,9 +592,6 @@ int main(int argc, char *argv[])int opt, i;bool use_uffd = false;useconds_t uffd_delay =0 ; -#ifdef __aarch64__ -unsigned int host_ipa_limit; -#endif #ifdef __x86_64__vm_guest_mode_params_init (VM_MODE_PXXV48_4K , true, true);
@@ -604,13 +599,15 @@ int main(int argc, char *argv[])#ifdef __aarch64__vm_guest_mode_params_init (VM_MODE_P40V48_4K , true, true);vm_guest_mode_params_init (VM_MODE_P40V48_64K , true, true); - - host_ipa_limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE ); -if (host_ipa_limit >=52 ) -vm_guest_mode_params_init (VM_MODE_P52V48_64K , true, true); -if (host_ipa_limit >=48 ) { -vm_guest_mode_params_init (VM_MODE_P48V48_4K , true, true); -vm_guest_mode_params_init (VM_MODE_P48V48_64K , true, true); + { +unsigned int limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE ); + +if (limit >=52 ) +vm_guest_mode_params_init (VM_MODE_P52V48_64K , true, true); +if (limit >=48 ) { +vm_guest_mode_params_init (VM_MODE_P48V48_4K , true, true); +vm_guest_mode_params_init (VM_MODE_P48V48_64K , true, true); + } }#endif #ifdef __s390x__
tools/testing/selftests/kvm/dirty_log_test.c +9 -12
@@ -341,9 +341,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,#ifdef __x86_64__vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ());#endif -#ifdef __aarch64__ucall_init (vm,NULL ); -#endif /* Export the shared variables to the guest */ sync_global_to_guest (vm, host_page_size);
@@ -433,9 +431,6 @@ int main(int argc, char *argv[])uint64_t phys_offset =0 ;unsigned int mode;int opt, i; -#ifdef __aarch64__ -unsigned int host_ipa_limit; -#endif #ifdef USE_CLEAR_DIRTY_LOG if (!kvm_check_cap (KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 )) {
@@ -450,13 +445,15 @@ int main(int argc, char *argv[])#ifdef __aarch64__vm_guest_mode_params_init (VM_MODE_P40V48_4K , true, true);vm_guest_mode_params_init (VM_MODE_P40V48_64K , true, true); - - host_ipa_limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE ); -if (host_ipa_limit >=52 ) -vm_guest_mode_params_init (VM_MODE_P52V48_64K , true, true); -if (host_ipa_limit >=48 ) { -vm_guest_mode_params_init (VM_MODE_P48V48_4K , true, true); -vm_guest_mode_params_init (VM_MODE_P48V48_64K , true, true); + { +unsigned int limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE ); + +if (limit >=52 ) +vm_guest_mode_params_init (VM_MODE_P52V48_64K , true, true); +if (limit >=48 ) { +vm_guest_mode_params_init (VM_MODE_P48V48_4K , true, true); +vm_guest_mode_params_init (VM_MODE_P48V48_64K , true, true); + } }#endif #ifdef __s390x__
Signature · 1 line
2.21.1
PATCH 8/13 KVM: selftests: aarch64: Use stream when given
In reply to Andrew JonesI'm not sure how we ended up using printf instead of fprintf in virt_dump(). Fix it.
--- tools/testing/selftests/kvm/lib/aarch64/processor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/aarch64/processor.c +2 -2
@@ -197,7 +197,7 @@ static void pte_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent, uint64_t p ptep =addr_gpa2hva (vm, pte);if (!*ptep)continue ; -printf ("%*s%s: %lx: %lx at %p\n" , indent,"" , type[level], pte, *ptep, ptep); +fprintf (stream,"%*s%s: %lx: %lx at %p\n" , indent,"" , type[level], pte, *ptep, ptep); pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level + 1); }#endif
@@ -215,7 +215,7 @@ void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) ptep =addr_gpa2hva (vm, pgd);if (!*ptep)continue ; -printf ("%*spgd: %lx: %lx at %p\n" , indent,"" , pgd, *ptep, ptep); +fprintf (stream,"%*spgd: %lx: %lx at %p\n" , indent,"" , pgd, *ptep, ptep);pte_dump (stream, vm, indent +1 ,pte_addr (vm, *ptep), level); } }
Signature · 1 line
2.21.1
PATCH 9/13 KVM: selftests: Rework debug message printing
In reply to Andrew JonesThere were a few problems with the way we output "debug" messages. The first is that we used DEBUG() which is defined when NDEBUG is not defined, but NDEBUG will never be defined for kselftests because it relies too much on assert(). The next is that most of the DEBUG() messages were actually "info" messages, which users may want to turn off if they just want a silent test that either completes or asserts. Finally, a debug message output from a library function, and thus for all tests, was annoying when its information wasn't interesting for a test.
Rework these messages so debug messages only output when DEBUG is defined and info messages output unless QUIET is defined. Also name the functions pr_debug and pr_info and make sure that when they're disabled we eat all the inputs. The later avoids unused variable warnings when the variables were only defined for the purpose of printing.
--- .../selftests/kvm/demand_paging_test.c | 54 +++++++++---------- tools/testing/selftests/kvm/dirty_log_test.c | 16 +++--- .../testing/selftests/kvm/include/kvm_util.h | 6 --- .../testing/selftests/kvm/include/test_util.h | 13 +++++ .../selftests/kvm/lib/aarch64/processor.c | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 7 +-- 6 files changed, 54 insertions(+), 44 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +27 -27
@@ -37,15 +37,15 @@#define DEFAULT_GUEST_TEST_MEM_SIZE (1 << 30)/* 1G */ #ifdef PRINT_PER_PAGE_UPDATES -#define PER_PAGE_DEBUG (...) DEBUG(__VA_ARGS__) +#define PER_PAGE_DEBUG (...) printf(__VA_ARGS__)#else -#define PER_PAGE_DEBUG (...) +#define PER_PAGE_DEBUG (...) _no_printf(__VA_ARGS__)#endif #ifdef PRINT_PER_VCPU_UPDATES -#define PER_VCPU_DEBUG (...) DEBUG(__VA_ARGS__) +#define PER_VCPU_DEBUG (...) printf(__VA_ARGS__)#else -#define PER_VCPU_DEBUG (...) +#define PER_VCPU_DEBUG (...) _no_printf(__VA_ARGS__)#endif #define MAX_VCPUS 512
@@ -171,6 +171,8 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus,*/ pages +=16 - pages %16 ; +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); + vm =_vm_create (mode, pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );#ifdef __x86_64__
@@ -198,8 +200,8 @@ static int handle_uffd_page_request(int uffd, uint64_t addr) r =ioctl (uffd,UFFDIO_COPY , ©);if (r ==-1 ) { -DEBUG ("Failed Paged in 0x%lx from thread %d with errno: %d\n" , - addr, tid, errno); +pr_info ("Failed Paged in 0x%lx from thread %d with errno: %d\n" , + addr, tid, errno);return r; }
@@ -247,19 +249,19 @@ static void *uffd_handler_thread_fn(void *arg) r =poll (pollfd,2 ,-1 );switch (r) {case -1 : -DEBUG ("poll err" ); +pr_info ("poll err" );continue ;case 0 :continue ;case 1 :break ;default : -DEBUG ("Polling uffd returned %d" , r); +pr_info ("Polling uffd returned %d" , r);return NULL ; }if (pollfd[0 ].revents &POLLERR ) { -DEBUG ("uffd revents has POLLERR" ); +pr_info ("uffd revents has POLLERR" );return NULL ; }
@@ -277,13 +279,12 @@ static void *uffd_handler_thread_fn(void *arg)if (r ==-1 ) {if (errno ==EAGAIN )continue ; -DEBUG ("Read of uffd gor errno %d" , errno); +pr_info ("Read of uffd gor errno %d" , errno);return NULL ; }if (r !=sizeof (msg)) { -DEBUG ("Read on uffd returned unexpected size: %d bytes" , - r); +pr_info ("Read on uffd returned unexpected size: %d bytes" , r);return NULL ; }
@@ -321,14 +322,14 @@ static int setup_demand_paging(struct kvm_vm *vm, uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK );if (uffd ==-1 ) { -DEBUG ("uffd creation failed\n" ); +pr_info ("uffd creation failed\n" );return -1 ; } uffdio_api.api =UFFD_API ; uffdio_api.features =0 ;if (ioctl (uffd,UFFDIO_API , &uffdio_api) ==-1 ) { -DEBUG ("ioctl uffdio_api failed\n" ); +pr_info ("ioctl uffdio_api failed\n" );return -1 ; }
@@ -336,13 +337,13 @@ static int setup_demand_paging(struct kvm_vm *vm, uffdio_register.range.len = len; uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) { -DEBUG ("ioctl uffdio_register failed\n" ); +pr_info ("ioctl uffdio_register failed\n" );return -1 ; }if ((uffdio_register.ioctls &UFFD_API_RANGE_IOCTLS ) !=UFFD_API_RANGE_IOCTLS ) { -DEBUG ("unexpected userfaultfd ioctl set\n" ); +pr_info ("unexpected userfaultfd ioctl set\n" );return -1 ; }
@@ -409,8 +410,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, guest_test_phys_mem &= ~((1 <<20 ) -1 );#endif -DEBUG ("guest physical test memory offset: 0x%lx\n" , - guest_test_phys_mem); +pr_info ("guest physical test memory offset: 0x%lx\n" , guest_test_phys_mem);/* Add an extra memory slot for testing demand paging */ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
@@ -491,7 +491,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,sync_global_to_guest (vm, guest_page_size);sync_global_to_guest (vm, vcpu_args); -DEBUG ("Finished creating vCPUs and starting uffd threads\n" ); +pr_info ("Finished creating vCPUs and starting uffd threads\n" );clock_gettime (CLOCK_MONOTONIC , &start);
@@ -500,7 +500,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, &vcpu_args[vcpu_id]); } -DEBUG ("Started all vCPUs\n" ); +pr_info ("Started all vCPUs\n" );/* Wait for the vcpu threads to quit */ for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) {
@@ -508,7 +508,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,PER_VCPU_DEBUG ("Joined thread for vCPU %d\n" , vcpu_id); } -DEBUG ("All vCPU threads joined\n" ); +pr_info ("All vCPU threads joined\n" );clock_gettime (CLOCK_MONOTONIC , &end);
@@ -524,12 +524,12 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, } } -DEBUG ("Total guest execution time: %lld.%.9lds\n" , - (long long )(timespec_diff (start, end).tv_sec), -timespec_diff (start, end).tv_nsec); -DEBUG ("Overall demand paging rate: %f pgs/sec\n" , - guest_num_pages / ((double )timespec_diff (start, end).tv_sec + - (double )timespec_diff (start, end).tv_nsec /100000000.0 )); +pr_info ("Total guest execution time: %lld.%.9lds\n" , + (long long )(timespec_diff (start, end).tv_sec), +timespec_diff (start, end).tv_nsec); +pr_info ("Overall demand paging rate: %f pgs/sec\n" , + guest_num_pages / ((double )timespec_diff (start, end).tv_sec + + (double )timespec_diff (start, end).tv_nsec /100000000.0 ));ucall_uninit (vm);kvm_vm_free (vm);
tools/testing/selftests/kvm/dirty_log_test.c +9 -7
@@ -173,7 +173,7 @@ static void *vcpu_worker(void *data) } } -DEBUG ("Dirtied %" PRIu64" pages\n" , pages_count); +pr_info ("Dirtied %" PRIu64" pages\n" , pages_count);return NULL ; }
@@ -252,6 +252,8 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,struct kvm_vm *vm;uint64_t extra_pg_pages = extra_mem_pages /512 *2 ; +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); + vm =_vm_create (mode,DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );#ifdef __x86_64__
@@ -311,7 +313,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, guest_test_phys_mem &= ~((1 <<20 ) -1 );#endif -DEBUG ("guest physical test memory offset: 0x%lx\n" , guest_test_phys_mem); +pr_info ("guest physical test memory offset: 0x%lx\n" , guest_test_phys_mem); bmap =bitmap_alloc (host_num_pages); host_bmap_track =bitmap_alloc (host_num_pages);
@@ -376,9 +378,9 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, host_quit = true;pthread_join (vcpu_thread,NULL ); -DEBUG ("Total bits checked: dirty (%" PRIu64"), clear (%" PRIu64"), " -"track_next (%" PRIu64")\n" , host_dirty_count, host_clear_count, - host_track_next_count); +pr_info ("Total bits checked: dirty (%" PRIu64"), clear (%" PRIu64"), " +"track_next (%" PRIu64")\n" , host_dirty_count, host_clear_count, + host_track_next_count);free (bmap);free (host_bmap_track);
@@ -492,8 +494,8 @@ int main(int argc, char *argv[])TEST_ASSERT (iterations >2 ,"Iterations must be greater than two" );TEST_ASSERT (interval >0 ,"Interval must be greater than zero" ); -DEBUG ("Test iterations: %" PRIu64", interval: %" PRIu64" (ms)\n" , - iterations, interval); +pr_info ("Test iterations: %" PRIu64", interval: %" PRIu64" (ms)\n" , + iterations, interval);srandom (time (0 ));
tools/testing/selftests/kvm/include/kvm_util.h +0 -6
@@ -24,12 +24,6 @@ struct kvm_vm;typedef uint64_t vm_paddr_t ;/* Virtual Machine (Guest) physical address */ typedef uint64_t vm_vaddr_t ;/* Virtual Machine (Guest) virtual address */ -#ifndef NDEBUG -#define DEBUG (...) printf(__VA_ARGS__); -#else -#define DEBUG (...) -#endif -/* Minimum allocated guest virtual and physical addresses */ #define KVM_UTIL_MIN_VADDR 0x2000
tools/testing/selftests/kvm/include/test_util.h +13 -0
@@ -19,6 +19,19 @@#include <fcntl.h> #include "kselftest.h" +static inline int _no_printf (const char *format, ...) {return 0 ; } + +#ifdef DEBUG +#define pr_debug (...) printf(__VA_ARGS__) +#else +#define pr_debug (...) _no_printf(__VA_ARGS__) +#endif +#ifndef QUIET +#define pr_info (...) printf(__VA_ARGS__) +#else +#define pr_info (...) _no_printf(__VA_ARGS__) +#endif +ssize_t test_write (int fd,const void *buf,size_t count);ssize_t test_read (int fd,void *buf,size_t count);int test_seq_read (const char *path,char **bufp,size_t *sizep);
tools/testing/selftests/kvm/lib/aarch64/processor.c +1 -1
@@ -186,7 +186,7 @@ vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)static void pte_dump (FILE *stream,struct kvm_vm *vm,uint8_t indent,uint64_t page,int level) { -#ifdef DEBUG_VM +#ifdef DEBUG static const char *const type[] = {"" ,"pud" ,"pmd" ,"pte" };uint64_t pte, *ptep;
tools/testing/selftests/kvm/lib/kvm_util.c +4 -3
@@ -138,7 +138,8 @@ struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm) {struct kvm_vm *vm; -DEBUG ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); +pr_debug ("%s: mode='%s' pages='%ld' perm='%d'\n" , __func__, +vm_guest_mode_string (mode), phy_pages, perm); vm =calloc (1 ,sizeof (*vm));TEST_ASSERT (vm !=NULL ,"Insufficient Memory" );
@@ -198,8 +199,8 @@ struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm) vm->pgtable_levels =4 ; vm->page_size =0x1000 ; vm->page_shift =12 ; -DEBUG ("Guest physical address width detected: %d\n" , - vm->pa_bits); +pr_debug ("Guest physical address width detected: %d\n" , + vm->pa_bits);#else TEST_ASSERT (false,"VM_MODE_PXXV48_4K not supported on " "non-x86 platforms" );
Signature · 1 line
2.21.1
PATCH 10/13 KVM: selftests: Convert some printf's to pr_info's
In reply to Andrew JonesWe leave some printf's because they inform the user the test is being skipped. QUIET should not disable those. We also leave the printf's used for help text.
--- tools/testing/selftests/kvm/kvm_create_max_vcpus.c | 8 ++++---- tools/testing/selftests/kvm/s390x/resets.c | 6 +++--- tools/testing/selftests/kvm/x86_64/mmio_warning_test.c | 2 +- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 +- tools/testing/selftests/kvm/x86_64/state_test.c | 2 +- tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c | 4 ++-- tools/testing/selftests/kvm/x86_64/xss_msr_test.c | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/kvm_create_max_vcpus.c +4 -4
@@ -24,8 +24,8 @@ void test_vcpu_creation(int first_vcpu_id, int num_vcpus)struct kvm_vm *vm;int i; -printf ("Testing creating %d vCPUs, with IDs %d...%d.\n" , - num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus -1 ); +pr_info ("Testing creating %d vCPUs, with IDs %d...%d.\n" , + num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus -1 ); vm =vm_create (VM_MODE_DEFAULT ,DEFAULT_GUEST_PHY_PAGES ,O_RDWR );
@@ -41,8 +41,8 @@ int main(int argc, char *argv[])int kvm_max_vcpu_id =kvm_check_cap (KVM_CAP_MAX_VCPU_ID );int kvm_max_vcpus =kvm_check_cap (KVM_CAP_MAX_VCPUS ); -printf ("KVM_CAP_MAX_VCPU_ID: %d\n" , kvm_max_vcpu_id); -printf ("KVM_CAP_MAX_VCPUS: %d\n" , kvm_max_vcpus); +pr_info ("KVM_CAP_MAX_VCPU_ID: %d\n" , kvm_max_vcpu_id); +pr_info ("KVM_CAP_MAX_VCPUS: %d\n" , kvm_max_vcpus);/* * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
tools/testing/selftests/kvm/s390x/resets.c +3 -3
@@ -134,7 +134,7 @@ static void inject_irq(int cpu_id)static void test_normal (void ) { -printf ("Testing normal reset\n" ); +pr_info ("Testing normal reset\n" );/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code_initial); run =vcpu_state (vm,VCPU_ID );
@@ -151,7 +151,7 @@ static void test_normal(void)static void test_initial (void ) { -printf ("Testing initial reset\n" ); +pr_info ("Testing initial reset\n" ); vm =vm_create_default (VCPU_ID ,0 , guest_code_initial); run =vcpu_state (vm,VCPU_ID ); regs = &run->s.regs;
@@ -168,7 +168,7 @@ static void test_initial(void)static void test_clear (void ) { -printf ("Testing clear reset\n" ); +pr_info ("Testing clear reset\n" ); vm =vm_create_default (VCPU_ID ,0 , guest_code_initial); run =vcpu_state (vm,VCPU_ID ); regs = &run->s.regs;
tools/testing/selftests/kvm/x86_64/mmio_warning_test.c +1 -1
@@ -44,7 +44,7 @@ void *thr(void *arg)struct kvm_run *run = tc->run; res =ioctl (kvmcpu,KVM_RUN ,0 ); -printf ("ret1=%d exit_reason=%d suberror=%d\n" , +pr_info ("ret1=%d exit_reason=%d suberror=%d\n" , res, run->exit_reason, run->internal.suberror);return 0 ;
tools/testing/selftests/kvm/x86_64/smm_test.c +1 -1
@@ -117,7 +117,7 @@ int main(int argc, char *argv[])vcpu_alloc_vmx (vm, &vmx_pages_gva);vcpu_args_set (vm,VCPU_ID ,1 , vmx_pages_gva); }else { -printf ("will skip SMM test with VMX enabled\n" ); +pr_info ("will skip SMM test with VMX enabled\n" );vcpu_args_set (vm,VCPU_ID ,1 ,0 ); }
tools/testing/selftests/kvm/x86_64/state_test.c +1 -1
@@ -139,7 +139,7 @@ int main(int argc, char *argv[])vcpu_alloc_vmx (vm, &vmx_pages_gva);vcpu_args_set (vm,VCPU_ID ,1 , vmx_pages_gva); }else { -printf ("will skip nested state checks\n" ); +pr_info ("will skip nested state checks\n" );vcpu_args_set (vm,VCPU_ID ,1 ,0 ); }
tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c +2 -2
@@ -121,8 +121,8 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)static void report (int64_t val) { -printf ("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n" , - val, val /TSC_ADJUST_VALUE , val %TSC_ADJUST_VALUE ); +pr_info ("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n" , + val, val /TSC_ADJUST_VALUE , val %TSC_ADJUST_VALUE ); }int main (int argc,char *argv[])
tools/testing/selftests/kvm/x86_64/xss_msr_test.c +1 -1
@@ -51,7 +51,7 @@ int main(int argc, char *argv[]) xss_supported = entry && !!(entry->eax &X86_FEATURE_XSAVES ); }if (!xss_supported) { -printf ("IA32_XSS is not supported by the vCPU.\n" ); +printf ("IA32_XSS is not supported by the vCPU, skipping test\n" );exit (KSFT_SKIP ); }
Signature · 1 line
2.21.1
PATCH 11/13 KVM: selftests: Rename vm_guest_mode_params
In reply to Andrew JonesWe're going to want this name in the library code, so use a shorter name in the tests.
--- .../selftests/kvm/demand_paging_test.c | 34 +++++++++---------- tools/testing/selftests/kvm/dirty_log_test.c | 34 +++++++++---------- 2 files changed, 33 insertions(+), 35 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +16 -18
@@ -543,16 +543,14 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, } } -struct vm_guest_mode_params { +struct guest_mode {bool supported;bool enabled; }; -struct vm_guest_mode_params vm_guest_mode_params[NUM_VM_MODES ]; +static struct guest_mode guest_modes[NUM_VM_MODES ]; -#define vm_guest_mode_params_init (mode, supported, enabled) \ -({ \ - vm_guest_mode_params[mode] = \ - (struct vm_guest_mode_params){ supported, enabled }; \ +#define guest_mode_init (mode, supported, enabled) ({ \ + guest_modes[mode] = (struct guest_mode){ supported, enabled }; \ })static void help (char *name)
@@ -568,7 +566,7 @@ static void help(char *name) " Guest mode IDs:\n");for (i =0 ; i <NUM_VM_MODES ; ++i) {printf (" %d: %s%s\n" , i,vm_guest_mode_string (i), - vm_guest_mode_params[i].supported ?" (supported)" :"" ); + guest_modes[i].supported ?" (supported)" :"" ); }printf (" -u: use User Fault FD to handle vCPU page\n" " faults.\n" );
@@ -594,24 +592,24 @@ int main(int argc, char *argv[])useconds_t uffd_delay =0 ;#ifdef __x86_64__ -vm_guest_mode_params_init (VM_MODE_PXXV48_4K , true, true); +guest_mode_init (VM_MODE_PXXV48_4K , true, true);#endif #ifdef __aarch64__ -vm_guest_mode_params_init (VM_MODE_P40V48_4K , true, true); -vm_guest_mode_params_init (VM_MODE_P40V48_64K , true, true); +guest_mode_init (VM_MODE_P40V48_4K , true, true); +guest_mode_init (VM_MODE_P40V48_64K , true, true); {unsigned int limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE );if (limit >=52 ) -vm_guest_mode_params_init (VM_MODE_P52V48_64K , true, true); +guest_mode_init (VM_MODE_P52V48_64K , true, true);if (limit >=48 ) { -vm_guest_mode_params_init (VM_MODE_P48V48_4K , true, true); -vm_guest_mode_params_init (VM_MODE_P48V48_64K , true, true); +guest_mode_init (VM_MODE_P48V48_4K , true, true); +guest_mode_init (VM_MODE_P48V48_64K , true, true); } }#endif #ifdef __s390x__ -vm_guest_mode_params_init (VM_MODE_P40V48_4K , true, true); +guest_mode_init (VM_MODE_P40V48_4K , true, true);#endif while ((opt =getopt (argc, argv,"hm:ud:b:v:" )) !=-1 ) {
@@ -619,13 +617,13 @@ int main(int argc, char *argv[])case 'm' :if (!mode_selected) {for (i =0 ; i <NUM_VM_MODES ; ++i) - vm_guest_mode_params[i].enabled = false; + guest_modes[i].enabled = false; mode_selected = true; } mode =strtoul (optarg,NULL ,10 );TEST_ASSERT (mode <NUM_VM_MODES ,"Guest mode ID %d too big" , mode); - vm_guest_mode_params[mode].enabled = true; + guest_modes[mode].enabled = true;break ;case 'u' : use_uffd = true;
@@ -654,9 +652,9 @@ int main(int argc, char *argv[]) }for (i =0 ; i <NUM_VM_MODES ; ++i) { -if (!vm_guest_mode_params[i].enabled) +if (!guest_modes[i].enabled)continue ; -TEST_ASSERT (vm_guest_mode_params[i].supported, +TEST_ASSERT (guest_modes[i].supported,"Guest mode ID %d (%s) not supported." , i,vm_guest_mode_string (i));run_test (i, use_uffd, uffd_delay, vcpus, vcpu_memory_bytes);
tools/testing/selftests/kvm/dirty_log_test.c +17 -17
@@ -388,15 +388,14 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,kvm_vm_free (vm); } -struct vm_guest_mode_params { +struct guest_mode {bool supported;bool enabled; }; -struct vm_guest_mode_params vm_guest_mode_params[NUM_VM_MODES ]; +static struct guest_mode guest_modes[NUM_VM_MODES ]; -#define vm_guest_mode_params_init (mode, supported, enabled) \ -({ \ - vm_guest_mode_params[mode] = (struct vm_guest_mode_params){ supported, enabled }; \ +#define guest_mode_init (mode, supported, enabled) ({ \ + guest_modes[mode] = (struct guest_mode){ supported, enabled }; \ })static void help (char *name)
@@ -419,7 +418,7 @@ static void help(char *name) " Guest mode IDs:\n");for (i =0 ; i <NUM_VM_MODES ; ++i) {printf (" %d: %s%s\n" , i,vm_guest_mode_string (i), - vm_guest_mode_params[i].supported ?" (supported)" :"" ); + guest_modes[i].supported ?" (supported)" :"" ); }puts ("" );exit (0 );
@@ -442,24 +441,25 @@ int main(int argc, char *argv[])#endif #ifdef __x86_64__ -vm_guest_mode_params_init (VM_MODE_PXXV48_4K , true, true); +guest_mode_init (VM_MODE_PXXV48_4K , true, true);#endif #ifdef __aarch64__ -vm_guest_mode_params_init (VM_MODE_P40V48_4K , true, true); -vm_guest_mode_params_init (VM_MODE_P40V48_64K , true, true); +guest_mode_init (VM_MODE_P40V48_4K , true, true); +guest_mode_init (VM_MODE_P40V48_64K , true, true); + {unsigned int limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE );if (limit >=52 ) -vm_guest_mode_params_init (VM_MODE_P52V48_64K , true, true); +guest_mode_init (VM_MODE_P52V48_64K , true, true);if (limit >=48 ) { -vm_guest_mode_params_init (VM_MODE_P48V48_4K , true, true); -vm_guest_mode_params_init (VM_MODE_P48V48_64K , true, true); +guest_mode_init (VM_MODE_P48V48_4K , true, true); +guest_mode_init (VM_MODE_P48V48_64K , true, true); } }#endif #ifdef __s390x__ -vm_guest_mode_params_init (VM_MODE_P40V48_4K , true, true); +guest_mode_init (VM_MODE_P40V48_4K , true, true);#endif while ((opt =getopt (argc, argv,"hi:I:p:m:" )) !=-1 ) {
@@ -476,13 +476,13 @@ int main(int argc, char *argv[])case 'm' :if (!mode_selected) {for (i =0 ; i <NUM_VM_MODES ; ++i) - vm_guest_mode_params[i].enabled = false; + guest_modes[i].enabled = false; mode_selected = true; } mode =strtoul (optarg,NULL ,10 );TEST_ASSERT (mode <NUM_VM_MODES ,"Guest mode ID %d too big" , mode); - vm_guest_mode_params[mode].enabled = true; + guest_modes[mode].enabled = true;break ;case 'h' :default :
@@ -500,9 +500,9 @@ int main(int argc, char *argv[])srandom (time (0 ));for (i =0 ; i <NUM_VM_MODES ; ++i) { -if (!vm_guest_mode_params[i].enabled) +if (!guest_modes[i].enabled)continue ; -TEST_ASSERT (vm_guest_mode_params[i].supported, +TEST_ASSERT (guest_modes[i].supported,"Guest mode ID %d (%s) not supported." , i,vm_guest_mode_string (i));run_test (i, iterations, interval, phys_offset);
Signature · 1 line
2.21.1
PATCH 12/13 KVM: selftests: Introduce vm_guest_mode_params
In reply to Andrew JonesThis array will allow us to easily translate modes to their parameter values.
--- tools/testing/selftests/kvm/lib/kvm_util.c | 52 +++++++++++----------- 1 file changed, 25 insertions(+), 27 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +25 -27
@@ -113,6 +113,25 @@ const char * const vm_guest_mode_string[] = {_Static_assert (sizeof (vm_guest_mode_string)/sizeof (char *) ==NUM_VM_MODES ,"Missing new mode strings?" ); +struct vm_guest_mode_params { +unsigned int pa_bits; +unsigned int va_bits; +unsigned int page_size; +unsigned int page_shift; +}; + +static const struct vm_guest_mode_params vm_guest_mode_params[] = { + {52 ,48 ,0x1000 ,12 }, + {52 ,48 ,0x10000 ,16 }, + {48 ,48 ,0x1000 ,12 }, + {48 ,48 ,0x10000 ,16 }, + {40 ,48 ,0x1000 ,12 }, + {40 ,48 ,0x10000 ,16 }, + {0 ,0 ,0x1000 ,12 }, +}; +_Static_assert (sizeof (vm_guest_mode_params)/sizeof (struct vm_guest_mode_params ) ==NUM_VM_MODES , +"Missing new mode params?" ); +#define KVM_INTERNAL_MEMSLOTS_START_PADDR 0xfe000000ULL#define KVM_INTERNAL_MEMSLOTS_END_PADDR (4ULL << 30)/*
@@ -147,60 +166,39 @@ struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm) vm->mode = mode; vm->type =0 ; + vm->pa_bits = vm_guest_mode_params[mode].pa_bits; + vm->va_bits = vm_guest_mode_params[mode].va_bits; + vm->page_size = vm_guest_mode_params[mode].page_size; + vm->page_shift = vm_guest_mode_params[mode].page_shift; +/* Setup mode specific traits. */ switch (vm->mode) {case VM_MODE_P52V48_4K : vm->pgtable_levels =4 ; - vm->pa_bits =52 ; - vm->va_bits =48 ; - vm->page_size =0x1000 ; - vm->page_shift =12 ;break ;case VM_MODE_P52V48_64K : vm->pgtable_levels =3 ; - vm->pa_bits =52 ; - vm->va_bits =48 ; - vm->page_size =0x10000 ; - vm->page_shift =16 ;break ;case VM_MODE_P48V48_4K : vm->pgtable_levels =4 ; - vm->pa_bits =48 ; - vm->va_bits =48 ; - vm->page_size =0x1000 ; - vm->page_shift =12 ;break ;case VM_MODE_P48V48_64K : vm->pgtable_levels =3 ; - vm->pa_bits =48 ; - vm->va_bits =48 ; - vm->page_size =0x10000 ; - vm->page_shift =16 ;break ;case VM_MODE_P40V48_4K : vm->pgtable_levels =4 ; - vm->pa_bits =40 ; - vm->va_bits =48 ; - vm->page_size =0x1000 ; - vm->page_shift =12 ;break ;case VM_MODE_P40V48_64K : vm->pgtable_levels =3 ; - vm->pa_bits =40 ; - vm->va_bits =48 ; - vm->page_size =0x10000 ; - vm->page_shift =16 ;break ;case VM_MODE_PXXV48_4K :#ifdef __x86_64__kvm_get_cpu_address_width (&vm->pa_bits, &vm->va_bits);TEST_ASSERT (vm->va_bits ==48 ,"Linear address width " "(%d bits) not supported" , vm->va_bits); - vm->pgtable_levels =4 ; - vm->page_size =0x1000 ; - vm->page_shift =12 ;pr_debug ("Guest physical address width detected: %d\n" , vm->pa_bits); + vm->pgtable_levels =4 ;#else TEST_ASSERT (false,"VM_MODE_PXXV48_4K not supported on " "non-x86 platforms" );
Signature · 1 line
2.21.1
PATCH 13/13 KVM: selftests: Introduce num-pages conversion utilities
In reply to Andrew JonesGuests 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.
--- .../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(-)
tools/testing/selftests/kvm/demand_paging_test.c +2 -6
@@ -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) */
tools/testing/selftests/kvm/dirty_log_test.c +6 -7
@@ -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); }
tools/testing/selftests/kvm/include/kvm_util.h +8 -0
@@ -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);
tools/testing/selftests/kvm/include/test_util.h +2 -0
@@ -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
tools/testing/selftests/kvm/lib/kvm_util.c +32 -0
@@ -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); +}
Signature · 1 line
2.21.1
Paolo Bonzini
maintainerRe: [PATCH 13/13] KVM: selftests: Introduce num-pages conversion utilities
Quoted 38 lines · Andrew Jones
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 usevm_adjust_num_guest_pages()there instead.Signed-off-by Andrew Jones <[email protected]>--- .../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(-)tools/testing/selftests/kvm/demand_paging_test.c +1
-6@@ -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));
tools/testing/selftests/kvm/demand_paging_test.c static void run_test(enum vm_guest_mode mode, bool use_uffd, · line 383
Quoted 7 lines
tools/testing/selftests/kvm/demand_paging_test.c +1
-0@@ -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
Quoted 126 lines
tools/testing/selftests/kvm/dirty_log_test.c +6
-7@@ -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); }tools/testing/selftests/kvm/include/kvm_util.h +8
-0@@ -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);tools/testing/selftests/kvm/include/test_util.h +2
-0@@ -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 tools/testing/selftests/kvm/lib/kvm_util.c +32
-0@@ -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); +}
Paolo Bonzini
maintainerQuoted 8 lines · Andrew Jones
This series has several parts:
* First, a hack to get x86 to compile. The missing
__NR_userfaultfddefine should be fixed a better way.* Then, fixups for several commits in kvm/queue. These fixups correspond to review comments that didn't have a chance to get addressed before the commits were applied.
Right, that's why they're in kvm/queue only. :) Did you test this series on aarch64?
Paolo
Quoted 7 lines
* Next, a few unnecessary #define/#ifdef deletions.
* Then, a rework of debug and info message printing.
* Finally, an addition to the API, num-pages conversion utilities, which cleans up all the num-pages calculations.
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Andrew Jones
This series has several parts:
* First, a hack to get x86 to compile. The missing
__NR_userfaultfddefine should be fixed a better way.
Quoted 6 lines
Quoted 3 lines
* Then, fixups for several commits in kvm/queue. These fixups correspond to review comments that didn't have a chance to get addressed before the commits were applied.
Right, that's why they're in kvm/queue only. :) Did you test this series on aarch64?
Yup
Quoted 11 lines
Paolo
Quoted 7 lines
* Next, a few unnecessary #define/#ifdef deletions.
* Then, a rework of debug and info message printing.
* Finally, an addition to the API, num-pages conversion utilities, which cleans up all the num-pages calculations.
Quoted 4 lines · Andrew Jones
This series has several parts:
* First, a hack to get x86 to compile. The missing
__NR_userfaultfddefine should be fixed a better way.
Yeh otherwise I think it will only compile on x86_64.
My gut feeling is we've got an artificial unistd_{32|64}.h under tools that is included rather than the real one that we should include (which should locate under $LINUX_ROOT/usr/include/asm/). Below patch worked for me, but I'm not 100% sure whether I fixed all the current users of that artifact header just in case I'll break some (what I saw is only this evsel.c and another setns.c, while that setns.c has syscall.h included correct so it seems fine):
tools/arch/x86/include/asm/unistd_32.h deleted +0 -16
@@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __NR_perf_event_open -# define __NR_perf_event_open 336 -#endif -#ifndef __NR_futex -# define __NR_futex 240 -#endif -#ifndef __NR_gettid -# define __NR_gettid 224 -#endif -#ifndef __NR_getcpu -# define __NR_getcpu 318 -#endif -#ifndef __NR_setns -# define __NR_setns 346 -#endif
tools/arch/x86/include/asm/unistd_64.h deleted +0 -16
@@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __NR_perf_event_open -# define __NR_perf_event_open 298 -#endif -#ifndef __NR_futex -# define __NR_futex 202 -#endif -#ifndef __NR_gettid -# define __NR_gettid 186 -#endif -#ifndef __NR_getcpu -# define __NR_getcpu 309 -#endif -#ifndef __NR_setns -#define __NR_setns 308 -#endif
tools/perf/util/evsel.c +1 -0
@@ -21,6 +21,7 @@#include <sys/ioctl.h> #include <sys/resource.h> #include <sys/types.h> +#include <sys/syscall.h> #include <dirent.h> #include <stdlib.h> #include <perf/evsel.h>
Signature · 1 line
Peter Xu
Quoted 7 lines · Peter Xu
Quoted 4 lines · Andrew Jones
This series has several parts:
* First, a hack to get x86 to compile. The missing
__NR_userfaultfddefine should be fixed a better way.Yeh otherwise I think it will only compile on
x86_64.
The opposite for me. I could compile on AArch64 without this hack, but on x86 (my Fedora 30 laptop) I could not.
Quoted 8 lines
My gut feeling is we've got an artificial unistd_{32|64}.h under tools that is included rather than the real one that we should include (which should locate under $LINUX_ROOT/usr/include/asm/). Below patch worked for me, but I'm not 100% sure whether I fixed all the current users of that artifact header just in case I'll break some (what I saw is only this
evsel.cand anothersetns.c, while thatsetns.chassyscall.hincluded correct so it seems fine):
Yeah, there's something strange about it because I saw the definition in the tools includes.
Thanks,
drew
Quoted 61 lines
tools/arch/x86/include/asm/unistd_32.h deleted +0
-16@@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __NR_perf_event_open -# define __NR_perf_event_open 336 -#endif -#ifndef __NR_futex -# define __NR_futex 240 -#endif -#ifndef __NR_gettid -# define __NR_gettid 224 -#endif -#ifndef __NR_getcpu -# define __NR_getcpu 318 -#endif -#ifndef __NR_setns -# define __NR_setns 346 -#endif tools/arch/x86/include/asm/unistd_64.h deleted +0
-16@@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __NR_perf_event_open -# define __NR_perf_event_open 298 -#endif -#ifndef __NR_futex -# define __NR_futex 202 -#endif -#ifndef __NR_gettid -# define __NR_gettid 186 -#endif -#ifndef __NR_getcpu -# define __NR_getcpu 309 -#endif -#ifndef __NR_setns -#define __NR_setns 308 -#endif tools/perf/util/evsel.c +1
-0@@ -21,6 +21,7 @@#include <sys/ioctl.h> #include <sys/resource.h> #include <sys/types.h> +#include <sys/syscall.h> #include <dirent.h> #include <stdlib.h> #include <perf/evsel.h> Signature · 2 lines
-- Peter Xu
Quoted 11 lines · Andrew Jones
Quoted 7 lines · Peter Xu
Quoted 4 lines · Andrew Jones
This series has several parts:
* First, a hack to get x86 to compile. The missing
__NR_userfaultfddefine should be fixed a better way.Yeh otherwise I think it will only compile on
x86_64.The opposite for me. I could compile on AArch64 without this hack, but on x86 (my Fedora 30 laptop) I could not.
Ah, then probably because ARM does not have that artificial unisted*.h defined (tools/arch/x86/include/asm/unistd_{32|64}.h for x86), then <sys/syscall.h> can find the correct headers.
And I have said it wrong above... with patch 1 compilation should always work, but IIUC the syscall number will be wrong on 32bit systems. Maybe we still need the other solution to make the test runnable on all platforms by removing those two artificial headers.
Thanks,
Signature · 1 line
Peter Xu