KVM: selftests: Cleanups
cover KVM: selftests: Cleanups
This series attempts to clean up demand_paging_test and dirty_log_test by factoring out common code, creating some new API along the way. It's main goal is to prepare for even more factoring that Ben and Peter want to do. The series would have a nice negative diff stat, but it also picks up a few of Peter's patches for his new dirty log test. So, the +/- diff stat is close to equal. It's not as close as an electoral vote count, but it's close.
I've tested on x86 and AArch64 (one config each), but not s390x.
Thanks,
drew
Andrew Jones (8):
KVM: selftests: Add x86_64/tsc_msrs_test to .gitignore
KVM: selftests: Drop pointless vm_create wrapper
KVM: selftests: Make the per vcpu memory size global
KVM: selftests: Make the number of vcpus global
KVM: selftests: Factor out guest mode code
KVM: selftests: Make vm_create_default common
KVM: selftests: Introduce vm_create_[default_]vcpus
KVM: selftests: Remove create_vm
Peter Xu (3):
KVM: selftests: Always clear dirty bitmap after iteration
KVM: selftests: Use a single binary for dirty/clear log test
KVM: selftests: Introduce after_vcpu_run hook for dirty log test
tools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 4 +- .../selftests/kvm/clear_dirty_log_test.c | 6 - .../selftests/kvm/demand_paging_test.c | 213 +++------- tools/testing/selftests/kvm/dirty_log_test.c | 372 ++++++++++-------- .../selftests/kvm/include/aarch64/processor.h | 3 + .../selftests/kvm/include/guest_modes.h | 21 + .../testing/selftests/kvm/include/kvm_util.h | 20 +- .../selftests/kvm/include/s390x/processor.h | 4 + .../selftests/kvm/include/x86_64/processor.h | 4 + .../selftests/kvm/lib/aarch64/processor.c | 17 - tools/testing/selftests/kvm/lib/guest_modes.c | 70 ++++ tools/testing/selftests/kvm/lib/kvm_util.c | 62 ++- .../selftests/kvm/lib/s390x/processor.c | 22 -- .../selftests/kvm/lib/x86_64/processor.c | 32 -- 15 files changed, 445 insertions(+), 407 deletions(-) delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c create mode 100644 tools/testing/selftests/kvm/include/guest_modes.h create mode 100644 tools/testing/selftests/kvm/lib/guest_modes.c
Signature · 1 line
2.26.2
PATCH 1/11 KVM: selftests: Add x86_64/tsc_msrs_test to .gitignore
--- tools/testing/selftests/kvm/.gitignore | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -15,6 +15,7 @@ /x86_64/vmx_preemption_timer_test /x86_64/svm_vmcall_test /x86_64/sync_regs_test +/x86_64/tsc_msrs_test /x86_64/vmx_apic_access_test /x86_64/vmx_close_while_nested_test /x86_64/vmx_dirty_log_test
Signature · 1 line
2.26.2
PATCH 2/11 KVM: selftests: Drop pointless vm_create wrapper
In reply to Andrew Jones--- tools/testing/selftests/kvm/demand_paging_test.c | 2 +- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- tools/testing/selftests/kvm/include/kvm_util.h | 1 - tools/testing/selftests/kvm/lib/kvm_util.c | 7 +------ 4 files changed, 3 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +1 -1
@@ -166,7 +166,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus,pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); - vm =_vm_create (mode, pages,O_RDWR ); + vm =vm_create (mode, pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );#ifdef __x86_64__vm_create_irqchip (vm);
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -252,7 +252,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,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 ); + 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__vm_create_irqchip (vm);
tools/testing/selftests/kvm/include/kvm_util.h +0 -1
@@ -65,7 +65,6 @@ int kvm_check_cap(long cap);int vm_enable_cap (struct kvm_vm *vm,struct kvm_enable_cap *cap);struct kvm_vm *vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm); -struct kvm_vm *_vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm);void kvm_vm_free (struct kvm_vm *vmp);void kvm_vm_restart (struct kvm_vm *vmp,int perm);void kvm_vm_release (struct kvm_vm *vmp);
tools/testing/selftests/kvm/lib/kvm_util.c +1 -6
@@ -152,7 +152,7 @@ _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params)* descriptor to control the created VM is created with the permissions * given by perm (e.g. O_RDWR). */ -struct kvm_vm *_vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm) +struct kvm_vm *vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm) {struct kvm_vm *vm;
@@ -243,11 +243,6 @@ struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)return vm; } -struct kvm_vm *vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm) -{ -return _vm_create (mode, phy_pages, perm); -} -/* * VM Restart *
Signature · 1 line
2.26.2
PATCH 3/11 KVM: selftests: Always clear dirty bitmap after iteration
In reply to Andrew JonesFrom: Peter Xu <[email protected]>
We don't clear the dirty bitmap before because KVM_GET_DIRTY_LOG will clear it for us before copying the dirty log onto it. However we'd still better to clear it explicitly instead of assuming the kernel will always do it for us.
More importantly, in the upcoming dirty ring tests we'll start to fetch dirty pages from a ring buffer, so no one is going to clear the dirty bitmap for us.
--- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -195,7 +195,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) page); } -if (test_bit_le (page, bmap)) { +if (test_and_clear_bit_le (page, bmap)) { host_dirty_count++;/* * If the bit is set, the value written onto
Signature · 1 line
2.26.2
PATCH 4/11 KVM: selftests: Use a single binary for dirty/clear log test
In reply to Andrew JonesFrom: Peter Xu <[email protected]>
Remove the clear_dirty_log test, instead merge it into the existing dirty_log_test. It should be cleaner to use this single binary to do both tests, also it's a preparation for the upcoming dirty ring test.
The default behavior will run all the modes in sequence.
--- tools/testing/selftests/kvm/.gitignore | 1 - tools/testing/selftests/kvm/Makefile | 2 - .../selftests/kvm/clear_dirty_log_test.c | 6 - tools/testing/selftests/kvm/dirty_log_test.c | 187 +++++++++++++++--- 4 files changed, 156 insertions(+), 40 deletions(-) delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c
tools/testing/selftests/kvm/.gitignore +0 -1
@@ -22,7 +22,6 @@ /x86_64/vmx_set_nested_state_test /x86_64/vmx_tsc_adjust_test /x86_64/xss_msr_test -/clear_dirty_log_test /demand_paging_test /dirty_log_test /kvm_create_max_vcpus
tools/testing/selftests/kvm/Makefile +0 -2
@@ -58,14 +58,12 @@ TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test TEST_GEN_PROGS_x86_64 += x86_64/debug_regs TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/user_msr_test -TEST_GEN_PROGS_x86_64 += clear_dirty_log_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time -TEST_GEN_PROGS_aarch64 += clear_dirty_log_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/clear_dirty_log_test.c deleted +0 -6
@@ -1,6 +0,0 @@ -#define USE_CLEAR_DIRTY_LOG -#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0) -#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1) -#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ - KVM_DIRTY_LOG_INITIALLY_SET) -#include "dirty_log_test.c"
tools/testing/selftests/kvm/dirty_log_test.c +156 -31
259 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 5/11 KVM: selftests: Introduce after_vcpu_run hook for dirty log test
In reply to Andrew JonesFrom: Peter Xu <[email protected]>
Provide a hook for the checks after vcpu_run() completes. Preparation for the dirty ring test because we'll need to take care of another exit reason.
--- tools/testing/selftests/kvm/dirty_log_test.c | 36 +++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +24 -12
@@ -178,6 +178,15 @@ static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,kvm_vm_clear_dirty_log (vm, slot, bitmap,0 , num_pages); } +static void default_after_vcpu_run (struct kvm_vm *vm) +{ +struct kvm_run *run =vcpu_state (vm,VCPU_ID ); + +TEST_ASSERT (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC , +"Invalid guest sync status: exit_reason=%s\n" , +exit_reason_str (run->exit_reason)); +} +struct log_mode {const char *name;/* Return true if this mode is supported, otherwise false */
@@ -187,16 +196,20 @@ struct log_mode {/* Hook to collect the dirty pages into the bitmap provided */ void (*collect_dirty_pages)(struct kvm_vm *vm,int slot,void *bitmap,uint32_t num_pages); +/* Hook to call when after each vcpu run */ +void (*after_vcpu_run)(struct kvm_vm *vm); } log_modes[LOG_MODE_NUM] = { { .name = "dirty-log", .collect_dirty_pages = dirty_log_collect_dirty_pages, + .after_vcpu_run = default_after_vcpu_run, }, { .name = "clear-log", .supported = clear_log_supported, .create_vm_done = clear_log_create_vm_done, .collect_dirty_pages = clear_log_collect_dirty_pages, + .after_vcpu_run = default_after_vcpu_run, }, };
@@ -247,6 +260,14 @@ static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot, mode->collect_dirty_pages (vm, slot, bitmap, num_pages); } +static void log_mode_after_vcpu_run (struct kvm_vm *vm) +{ +struct log_mode *mode = &log_modes[host_log_mode]; + +if (mode->after_vcpu_run) + mode->after_vcpu_run (vm); +} +static void generate_random_array (uint64_t *guest_array,uint64_t size) {uint64_t i;
@@ -261,25 +282,16 @@ static void *vcpu_worker(void *data)struct kvm_vm *vm = data;uint64_t *guest_array;uint64_t pages_count =0 ; -struct kvm_run *run; - - run =vcpu_state (vm,VCPU_ID ); guest_array =addr_gva2hva (vm, (vm_vaddr_t )random_array); -generate_random_array (guest_array,TEST_PAGES_PER_LOOP );while (!READ_ONCE (host_quit)) { +generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); + pages_count +=TEST_PAGES_PER_LOOP ;/* Let the guest dirty the random pages */ ret =_vcpu_run (vm,VCPU_ID );TEST_ASSERT (ret ==0 ,"vcpu_run failed: %d\n" , ret); -if (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC ) { - pages_count +=TEST_PAGES_PER_LOOP ; -generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); - }else { -TEST_FAIL ("Invalid guest sync status: " -"exit_reason=%s\n" , -exit_reason_str (run->exit_reason)); - } +log_mode_after_vcpu_run (vm); }pr_info ("Dirtied %" PRIu64" pages\n" , pages_count);
Signature · 1 line
2.26.2
PATCH 6/11 KVM: selftests: Make the per vcpu memory size global
In reply to Andrew JonesRename vcpu_memory_bytes to something with "percpu" in it in order to be less ambiguous. Also make it global to simplify things.
--- .../selftests/kvm/demand_paging_test.c | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +17 -19
@@ -32,8 +32,7 @@/* Default guest test virtual memory offset */ #define DEFAULT_GUEST_TEST_MEM 0xc0000000 - -#define DEFAULT_GUEST_TEST_MEM_SIZE (1 << 30)/* 1G */ +#define DEFAULT_PER_VCPU_MEM_SIZE (1 << 30)/* 1G */ #ifdef PRINT_PER_PAGE_UPDATES #define PER_PAGE_DEBUG (...) printf(__VA_ARGS__)
@@ -72,6 +71,7 @@ static uint64_t guest_test_phys_mem;* Must not conflict with identity mapped test code. */ static uint64_t guest_test_virt_mem =DEFAULT_GUEST_TEST_MEM ; +static uint64_t guest_percpu_mem_size =DEFAULT_PER_VCPU_MEM_SIZE ;struct vcpu_args {uint64_t gva;
@@ -145,7 +145,7 @@ static void *vcpu_worker(void *data)#define PTES_PER_4K_PT 512static struct kvm_vm *create_vm (enum vm_guest_mode mode,int vcpus, -uint64_t vcpu_memory_bytes) +uint64_t guest_percpu_mem_size) {struct kvm_vm *vm;uint64_t pages =DEFAULT_GUEST_PHY_PAGES ;
@@ -160,7 +160,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus,* page size guest will need even less memory for page tables). */ pages += (2 * pages) /PTES_PER_4K_PT ; - pages += ((2 * vcpus * vcpu_memory_bytes) >>PAGE_SHIFT_4K ) / + pages += ((2 * vcpus * guest_percpu_mem_size) >>PAGE_SHIFT_4K ) /PTES_PER_4K_PT ; pages =vm_adjust_num_guest_pages (mode, pages);
@@ -351,8 +351,7 @@ static int setup_demand_paging(struct kvm_vm *vm, }static void run_test (enum vm_guest_mode mode,bool use_uffd, -useconds_t uffd_delay,int vcpus, -uint64_t vcpu_memory_bytes) +useconds_t uffd_delay,int vcpus) {pthread_t *vcpu_threads;pthread_t *uffd_handler_threads =NULL ;
@@ -364,14 +363,14 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,int vcpu_id;int r; - vm =create_vm (mode, vcpus, vcpu_memory_bytes); + vm =create_vm (mode, vcpus, guest_percpu_mem_size); guest_page_size =vm_get_page_size (vm); -TEST_ASSERT (vcpu_memory_bytes % guest_page_size ==0 , +TEST_ASSERT (guest_percpu_mem_size % guest_page_size ==0 ,"Guest memory size is not guest page size aligned." ); - guest_num_pages = (vcpus * vcpu_memory_bytes) / guest_page_size; + guest_num_pages = (vcpus * guest_percpu_mem_size) / guest_page_size; guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages);/*
@@ -382,10 +381,10 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,"Requested more guest memory than address space allows.\n" " guest pages: %lx max gfn: %x vcpus: %d wss: %lx]\n" , guest_num_pages,vm_get_max_gfn (vm), vcpus, - vcpu_memory_bytes); + guest_percpu_mem_size); host_page_size =getpagesize (); -TEST_ASSERT (vcpu_memory_bytes % host_page_size ==0 , +TEST_ASSERT (guest_percpu_mem_size % host_page_size ==0 ,"Guest memory size is not host page size aligned." ); guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
@@ -436,9 +435,9 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,vm_vcpu_add_default (vm, vcpu_id, guest_code); - vcpu_gpa = guest_test_phys_mem + (vcpu_id * vcpu_memory_bytes); + vcpu_gpa = guest_test_phys_mem + (vcpu_id * guest_percpu_mem_size);PER_VCPU_DEBUG ("Added VCPU %d with test mem gpa [%lx, %lx)\n" , - vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_memory_bytes); + vcpu_id, vcpu_gpa, vcpu_gpa + guest_percpu_mem_size);/* Cache the HVA pointer of the region */ vcpu_hva =addr_gpa2hva (vm, vcpu_gpa);
@@ -456,7 +455,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, &uffd_handler_threads[vcpu_id], pipefds[vcpu_id *2 ], uffd_delay, &uffd_args[vcpu_id], - vcpu_hva, vcpu_memory_bytes); + vcpu_hva, guest_percpu_mem_size);if (r <0 )exit (-r); }
@@ -468,8 +467,8 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, vcpu_args[vcpu_id].vm = vm; vcpu_args[vcpu_id].vcpu_id = vcpu_id; vcpu_args[vcpu_id].gva = guest_test_virt_mem + - (vcpu_id * vcpu_memory_bytes); - vcpu_args[vcpu_id].pages = vcpu_memory_bytes / guest_page_size; + (vcpu_id * guest_percpu_mem_size); + vcpu_args[vcpu_id].pages = guest_percpu_mem_size / guest_page_size; }/* Export the shared variables to the guest */
@@ -569,7 +568,6 @@ static void help(char *name)int main (int argc,char *argv[]) {bool mode_selected = false; -uint64_t vcpu_memory_bytes =DEFAULT_GUEST_TEST_MEM_SIZE ;int vcpus =1 ;unsigned int mode;int opt, i;
@@ -619,7 +617,7 @@ int main(int argc, char *argv[]) "A negative UFFD delay is not supported.");break ;case 'b' : - vcpu_memory_bytes =parse_size (optarg); + guest_percpu_mem_size =parse_size (optarg);break ;case 'v' : vcpus =atoi (optarg);
@@ -642,7 +640,7 @@ int main(int argc, char *argv[])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); +run_test (i, use_uffd, uffd_delay, vcpus); }return 0 ;
Signature · 1 line
2.26.2
PATCH 7/11 KVM: selftests: Make the number of vcpus global
In reply to Andrew JonesWe also check the input number of vcpus against the maximum supported.
--- .../selftests/kvm/demand_paging_test.c | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +20 -20
@@ -73,6 +73,9 @@ static uint64_t guest_test_phys_mem;static uint64_t guest_test_virt_mem =DEFAULT_GUEST_TEST_MEM ;static uint64_t guest_percpu_mem_size =DEFAULT_PER_VCPU_MEM_SIZE ; +/* Number of VCPUs for the test */ +static int nr_vcpus =1 ; +struct vcpu_args {uint64_t gva;uint64_t pages;
@@ -351,7 +354,7 @@ static int setup_demand_paging(struct kvm_vm *vm, }static void run_test (enum vm_guest_mode mode,bool use_uffd, -useconds_t uffd_delay,int vcpus) +useconds_t uffd_delay) {pthread_t *vcpu_threads;pthread_t *uffd_handler_threads =NULL ;
@@ -363,14 +366,14 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,int vcpu_id;int r; - vm =create_vm (mode, vcpus, guest_percpu_mem_size); + vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); guest_page_size =vm_get_page_size (vm);TEST_ASSERT (guest_percpu_mem_size % guest_page_size ==0 ,"Guest memory size is not guest page size aligned." ); - guest_num_pages = (vcpus * guest_percpu_mem_size) / guest_page_size; + guest_num_pages = (nr_vcpus * guest_percpu_mem_size) / guest_page_size; guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages);/*
@@ -380,7 +383,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,TEST_ASSERT (guest_num_pages <vm_get_max_gfn (vm),"Requested more guest memory than address space allows.\n" " guest pages: %lx max gfn: %x vcpus: %d wss: %lx]\n" , - guest_num_pages,vm_get_max_gfn (vm), vcpus, + guest_num_pages,vm_get_max_gfn (vm), nr_vcpus, guest_percpu_mem_size); host_page_size =getpagesize ();
@@ -414,22 +417,22 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, "Failed to allocate buffer for guest data pattern");memset (guest_data_prototype,0xAB , host_page_size); - vcpu_threads =malloc (vcpus *sizeof (*vcpu_threads)); + vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" );if (use_uffd) { uffd_handler_threads = -malloc (vcpus *sizeof (*uffd_handler_threads)); +malloc (nr_vcpus *sizeof (*uffd_handler_threads));TEST_ASSERT (uffd_handler_threads,"Memory allocation failed" ); - uffd_args =malloc (vcpus *sizeof (*uffd_args)); + uffd_args =malloc (nr_vcpus *sizeof (*uffd_args));TEST_ASSERT (uffd_args,"Memory allocation failed" ); - pipefds =malloc (sizeof (int ) * vcpus *2 ); + pipefds =malloc (sizeof (int ) * nr_vcpus *2 );TEST_ASSERT (pipefds,"Unable to allocate memory for pipefd" ); } -for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { +for (vcpu_id =0 ; vcpu_id < nr_vcpus; vcpu_id++) {vm_paddr_t vcpu_gpa;void *vcpu_hva;
@@ -480,7 +483,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,clock_gettime (CLOCK_MONOTONIC , &start); -for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { +for (vcpu_id =0 ; vcpu_id < nr_vcpus; vcpu_id++) {pthread_create (&vcpu_threads[vcpu_id],NULL , vcpu_worker, &vcpu_args[vcpu_id]); }
@@ -488,7 +491,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,pr_info ("Started all vCPUs\n" );/* Wait for the vcpu threads to quit */ -for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { +for (vcpu_id =0 ; vcpu_id < nr_vcpus; vcpu_id++) {pthread_join (vcpu_threads[vcpu_id],NULL );PER_VCPU_DEBUG ("Joined thread for vCPU %d\n" , vcpu_id); }
@@ -501,7 +504,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,char c;/* Tell the user fault fd handler threads to quit */ -for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { +for (vcpu_id =0 ; vcpu_id < nr_vcpus; vcpu_id++) { r =write (pipefds[vcpu_id *2 +1 ], &c,1 );TEST_ASSERT (r ==1 ,"Unable to write to pipefd" );
@@ -567,8 +570,8 @@ static void help(char *name)int main (int argc,char *argv[]) { +int max_vcpus =kvm_check_cap (KVM_CAP_MAX_VCPUS );bool mode_selected = false; -int vcpus =1 ;unsigned int mode;int opt, i;bool use_uffd = false;
@@ -620,12 +623,9 @@ int main(int argc, char *argv[]) guest_percpu_mem_size =parse_size (optarg);break ;case 'v' : - vcpus =atoi (optarg); -TEST_ASSERT (vcpus >0 , -"Must have a positive number of vCPUs" ); -TEST_ASSERT (vcpus <=MAX_VCPUS , -"This test does not currently support\n" -"more than %d vCPUs." ,MAX_VCPUS ); + nr_vcpus =atoi (optarg); +TEST_ASSERT (nr_vcpus >0 && nr_vcpus <= max_vcpus, +"Invalid number of vcpus, must be between 1 and %d" , max_vcpus);break ;case 'h' :default :
@@ -640,7 +640,7 @@ int main(int argc, char *argv[])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); +run_test (i, use_uffd, uffd_delay); }return 0 ;
Signature · 1 line
2.26.2
PATCH 8/11 KVM: selftests: Factor out guest mode code
In reply to Andrew Jonesdemand_paging_test and dirty_log_test have redundant guest mode code. Factor it out.
--- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/demand_paging_test.c | 95 ++++---------- tools/testing/selftests/kvm/dirty_log_test.c | 121 ++++++------------ .../selftests/kvm/include/guest_modes.h | 21 +++ tools/testing/selftests/kvm/lib/guest_modes.c | 70 ++++++++++ 5 files changed, 150 insertions(+), 159 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/guest_modes.h create mode 100644 tools/testing/selftests/kvm/lib/guest_modes.c
tools/testing/selftests/kvm/Makefile +1 -1
@@ -33,7 +33,7 @@ ifeq ($(ARCH),s390) UNAME_M := s390x endif -LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c lib/test_util.c +LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c
tools/testing/selftests/kvm/demand_paging_test.c +22 -73
170 lines not on this page · raw message
tools/testing/selftests/kvm/dirty_log_test.c +36 -85
194 lines not on this page · raw message
tools/testing/selftests/kvm/include/guest_modes.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020, Red Hat, Inc. +*/ +#include "kvm_util.h" + +struct guest_mode { +bool supported; +bool enabled; +}; + +extern struct guest_mode guest_modes[NUM_VM_MODES ]; + +#define guest_mode_append (mode, supported, enabled) ({ \ + guest_modes[mode] = (struct guest_mode){ supported, enabled }; \ +}) + +void guest_modes_append_default (void ); +void for_each_guest_mode (void (*func)(enum vm_guest_mode ,void *),void *arg); +void guest_modes_help (void ); +void guest_modes_cmdline (const char *arg);
tools/testing/selftests/kvm/lib/guest_modes.c new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 9/11 KVM: selftests: Make vm_create_default common
In reply to Andrew JonesThe code is almost 100% the same anyway. Just move it to common and add a few arch-specific helpers.
--- .../selftests/kvm/include/aarch64/processor.h | 3 ++ .../selftests/kvm/include/s390x/processor.h | 4 +++ .../selftests/kvm/include/x86_64/processor.h | 4 +++ .../selftests/kvm/lib/aarch64/processor.c | 17 ---------- tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++ .../selftests/kvm/lib/s390x/processor.c | 22 ------------- .../selftests/kvm/lib/x86_64/processor.c | 32 ------------------- 7 files changed, 37 insertions(+), 71 deletions(-)
tools/testing/selftests/kvm/include/aarch64/processor.h +3 -0
@@ -9,6 +9,9 @@#include "kvm_util.h" +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12)#define ARM64_CORE_REG (x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
tools/testing/selftests/kvm/include/s390x/processor.h +4 -0
@@ -5,6 +5,10 @@#ifndef SELFTEST_KVM_PROCESSOR_H #define SELFTEST_KVM_PROCESSOR_H +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12) +/* Bits in the region/segment table entry */ #define REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */ #define REGION_ENTRY_PROTECT 0x200/* region protection bit */
tools/testing/selftests/kvm/include/x86_64/processor.h +4 -0
@@ -13,6 +13,10 @@#include <asm/msr-index.h> +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12) +#define X86_EFLAGS_FIXED (1u << 1)#define X86_CR4_VME (1ul << 0)
tools/testing/selftests/kvm/lib/aarch64/processor.c +0 -17
@@ -5,8 +5,6 @@* Copyright (C) 2018, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <linux/compiler.h> #include "kvm_util.h"
@@ -219,21 +217,6 @@ void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) } } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) -{ -uint64_t ptrs_per_4k_pte =512 ; -uint64_t extra_pg_pages = (extra_mem_pages / ptrs_per_4k_pte) *2 ; -struct kvm_vm *vm; - - vm =vm_create (VM_MODE_DEFAULT ,DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); - -kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); -vm_vcpu_add_default (vm, vcpuid, guest_code); - -return vm; -} -void aarch64_vcpu_setup (struct kvm_vm *vm,int vcpuid,struct kvm_vcpu_init *init) {struct kvm_vcpu_init default_init = { .target =-1 , };
tools/testing/selftests/kvm/lib/kvm_util.c +26 -0
@@ -5,6 +5,7 @@* Copyright (C) 2018, Google LLC. */ +#define _GNU_SOURCE/* for program_invocation_name */ #include "test_util.h" #include "kvm_util.h" #include "kvm_util_internal.h"
@@ -243,6 +244,31 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)return vm; } +struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, +void *guest_code) +{ +/* The maximum page table size for a memory region will be when the +* smallest pages are used. Considering each page contains x page +* table descriptors, the total extra size for page tables (for extra +* N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller +* than N/x*2. +*/ +uint64_t extra_pg_pages = (extra_mem_pages /PTRS_PER_PAGE (min_page_size ())) *2 ; +struct kvm_vm *vm; + + vm =vm_create (VM_MODE_DEFAULT ,DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); + +kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); + +#ifdef __x86_64__ +vm_create_irqchip (vm); +#endif + +vm_vcpu_add_default (vm, vcpuid, guest_code); + +return vm; +} +/* * VM Restart *
tools/testing/selftests/kvm/lib/s390x/processor.c +0 -22
@@ -5,8 +5,6 @@* Copyright (C) 2019, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include "processor.h" #include "kvm_util.h" #include "../kvm_util_internal.h"
@@ -160,26 +158,6 @@ void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)virt_dump_region (stream, vm, indent, vm->pgd); } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) -{ -/* -* The additional amount of pages required for the page tables is: -* 1 * n / 256 + 4 * (n / 256) / 2048 + 4 * (n / 256) / 2048^2 + ... -* which is definitely smaller than (n / 256) * 2. -*/ -uint64_t extra_pg_pages = extra_mem_pages /256 *2 ; -struct kvm_vm *vm; - - vm =vm_create (VM_MODE_DEFAULT , -DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); - -kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); -vm_vcpu_add_default (vm, vcpuid, guest_code); - -return vm; -} -void vm_vcpu_add_default (struct kvm_vm *vm,uint32_t vcpuid,void *guest_code) {size_t stack_size =DEFAULT_STACK_PGS *getpagesize ();
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -32
@@ -5,8 +5,6 @@* Copyright (C) 2018, Google LLC. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include "test_util.h" #include "kvm_util.h" #include "../kvm_util_internal.h"
@@ -721,36 +719,6 @@ void vcpu_set_cpuid(struct kvm_vm *vm, } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) -{ -struct kvm_vm *vm; -/* -* For x86 the maximum page table size for a memory region -* will be when only 4K pages are used. In that case the -* total extra size for page tables (for extra N pages) will -* be: N/512+N/512^2+N/512^3+... which is definitely smaller -* than N/512*2. -*/ -uint64_t extra_pg_pages = extra_mem_pages /512 *2 ; - -/* Create VM */ - vm =vm_create (VM_MODE_DEFAULT , -DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, -O_RDWR ); - -/* Setup guest code */ -kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); - -/* Setup IRQ Chip */ -vm_create_irqchip (vm); - -/* Add the first vCPU. */ -vm_vcpu_add_default (vm, vcpuid, guest_code); - -return vm; -} -/* * VCPU Get MSR *
Signature · 1 line
2.26.2
Re: [PATCH 09/11] KVM: selftests: Make vm_create_default common
Quoted 33 lines · Andrew Jones
The code is almost 100% the same anyway. Just move it to common and add a few arch-specific helpers.
Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/include/aarch64/processor.h | 3 ++ .../selftests/kvm/include/s390x/processor.h | 4 +++ .../selftests/kvm/include/x86_64/processor.h | 4 +++ .../selftests/kvm/lib/aarch64/processor.c | 17 ---------- tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++ .../selftests/kvm/lib/s390x/processor.c | 22 ------------- .../selftests/kvm/lib/x86_64/processor.c | 32 ------------------- 7 files changed, 37 insertions(+), 71 deletions(-)tools/testing/selftests/kvm/include/aarch64/processor.h +3
-0@@ -9,6 +9,9 @@#include "kvm_util.h" +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12)#define ARM64_CORE_REG (x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))tools/testing/selftests/kvm/include/s390x/processor.h +0
-0
tools/testing/selftests/kvm/include/s390x/processor.h line 8
Quoted 4 lines
tools/testing/selftests/kvm/include/s390x/processor.h +1
-0@@ -5,6 +5,10 @@#ifndef SELFTEST_KVM_PROCESSOR_H #define SELFTEST_KVM_PROCESSOR_H +#define PTRS_PER_PAGE (page_size) ((page_size) / 8)
Doh. I think this 8 is supposed to be a 16 for s390x, considering it was dividing by 256 in its version of vm_create_default. I need guidance from s390x gurus as to whether or not I should respin though.
Thanks,
drew
Quoted 193 lines
tools/testing/selftests/kvm/include/s390x/processor.h +3
-0+#define min_page_size () (4096) +#define min_page_shift () (12) +/* Bits in the region/segment table entry */ #define REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */ #define REGION_ENTRY_PROTECT 0x200/* region protection bit */ tools/testing/selftests/kvm/include/x86_64/processor.h +4
-0@@ -13,6 +13,10 @@#include <asm/msr-index.h> +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12) +#define X86_EFLAGS_FIXED (1u << 1)#define X86_CR4_VME (1ul << 0)tools/testing/selftests/kvm/lib/aarch64/processor.c +0
-17@@ -5,8 +5,6 @@* Copyright (C) 2018, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <linux/compiler.h> #include "kvm_util.h" @@ -219,21 +217,6 @@ void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) } } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) -{ -uint64_t ptrs_per_4k_pte =512 ; -uint64_t extra_pg_pages = (extra_mem_pages / ptrs_per_4k_pte) *2 ; -struct kvm_vm *vm; - - vm =vm_create (VM_MODE_DEFAULT ,DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); - -kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); -vm_vcpu_add_default (vm, vcpuid, guest_code); - -return vm; -} -void aarch64_vcpu_setup (struct kvm_vm *vm,int vcpuid,struct kvm_vcpu_init *init) {struct kvm_vcpu_init default_init = { .target =-1 , };tools/testing/selftests/kvm/lib/kvm_util.c +26
-0@@ -5,6 +5,7 @@* Copyright (C) 2018, Google LLC. */ +#define _GNU_SOURCE/* for program_invocation_name */ #include "test_util.h" #include "kvm_util.h" #include "kvm_util_internal.h" @@ -243,6 +244,31 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)return vm; } +struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, +void *guest_code) +{ +/* The maximum page table size for a memory region will be when the +* smallest pages are used. Considering each page contains x page +* table descriptors, the total extra size for page tables (for extra +* N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller +* than N/x*2. +*/ +uint64_t extra_pg_pages = (extra_mem_pages /PTRS_PER_PAGE (min_page_size ())) *2 ; +struct kvm_vm *vm; + + vm =vm_create (VM_MODE_DEFAULT ,DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); + +kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); + +#ifdef __x86_64__ +vm_create_irqchip (vm); +#endif + +vm_vcpu_add_default (vm, vcpuid, guest_code); + +return vm; +} +/* * VM Restart * tools/testing/selftests/kvm/lib/s390x/processor.c +0
-22@@ -5,8 +5,6 @@* Copyright (C) 2019, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include "processor.h" #include "kvm_util.h" #include "../kvm_util_internal.h" @@ -160,26 +158,6 @@ void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)virt_dump_region (stream, vm, indent, vm->pgd); } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) -{ -/* -* The additional amount of pages required for the page tables is: -* 1 * n / 256 + 4 * (n / 256) / 2048 + 4 * (n / 256) / 2048^2 + ... -* which is definitely smaller than (n / 256) * 2. -*/ -uint64_t extra_pg_pages = extra_mem_pages /256 *2 ; -struct kvm_vm *vm; - - vm =vm_create (VM_MODE_DEFAULT , -DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); - -kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); -vm_vcpu_add_default (vm, vcpuid, guest_code); - -return vm; -} -void vm_vcpu_add_default (struct kvm_vm *vm,uint32_t vcpuid,void *guest_code) {size_t stack_size =DEFAULT_STACK_PGS *getpagesize ();tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-32@@ -5,8 +5,6 @@* Copyright (C) 2018, Google LLC. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include "test_util.h" #include "kvm_util.h" #include "../kvm_util_internal.h" @@ -721,36 +719,6 @@ void vcpu_set_cpuid(struct kvm_vm *vm, } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) -{ -struct kvm_vm *vm; -/* -* For x86 the maximum page table size for a memory region -* will be when only 4K pages are used. In that case the -* total extra size for page tables (for extra N pages) will -* be: N/512+N/512^2+N/512^3+... which is definitely smaller -* than N/512*2. -*/ -uint64_t extra_pg_pages = extra_mem_pages /512 *2 ; - -/* Create VM */ - vm =vm_create (VM_MODE_DEFAULT , -DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, -O_RDWR ); - -/* Setup guest code */ -kvm_vm_elf_load (vm, program_invocation_name,0 ,0 ); - -/* Setup IRQ Chip */ -vm_create_irqchip (vm); - -/* Add the first vCPU. */ -vm_vcpu_add_default (vm, vcpuid, guest_code); - -return vm; -} -/* * VCPU Get MSR * Signature · 1 line
2.26.2
Re: [PATCH 09/11] KVM: selftests: Make vm_create_default common
Quoted 37 lines · Andrew Jones
Quoted 36 lines · Andrew Jones
The code is almost 100% the same anyway. Just move it to common and add a few arch-specific helpers.
Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/include/aarch64/processor.h | 3 ++ .../selftests/kvm/include/s390x/processor.h | 4 +++ .../selftests/kvm/include/x86_64/processor.h | 4 +++ .../selftests/kvm/lib/aarch64/processor.c | 17 ---------- tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++ .../selftests/kvm/lib/s390x/processor.c | 22 ------------- .../selftests/kvm/lib/x86_64/processor.c | 32 ------------------- 7 files changed, 37 insertions(+), 71 deletions(-)tools/testing/selftests/kvm/include/aarch64/processor.h +3
-0@@ -9,6 +9,9 @@#include "kvm_util.h" +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12)#define ARM64_CORE_REG (x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))tools/testing/selftests/kvm/include/s390x/processor.h +0
-0@@ -5,6 +5,10 @@#ifndef SELFTEST_KVM_PROCESSOR_H #define SELFTEST_KVM_PROCESSOR_H
Quoted 9 lines
tools/testing/selftests/kvm/include/s390x/processor.h line 8
Quoted 1 line
tools/testing/selftests/kvm/include/s390x/processor.h +1
-0+#define PTRS_PER_PAGE (page_size) ((page_size) / 8)Doh. I think this 8 is supposed to be a 16 for s390x, considering it was dividing by 256 in its version of
vm_create_default. I need guidance from s390x gurus as to whether or not I should respin though.Thanks,
drew
This is kind of tricky. The last level page table is only 2kb (256 entries = 1MB range). Depending on whether the page table allocation is clever or not (you can have 2 page tables in one page) this means that indeed 16 might be better. But then you actually want to change the macro name to PTES_PER_PAGE?
Re: [PATCH 09/11] KVM: selftests: Make vm_create_default common
Quoted 46 lines · Christian Borntraeger
Quoted 43 lines · Andrew Jones
Quoted 33 lines · Andrew Jones
The code is almost 100% the same anyway. Just move it to common and add a few arch-specific helpers.
Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/include/aarch64/processor.h | 3 ++ .../selftests/kvm/include/s390x/processor.h | 4 +++ .../selftests/kvm/include/x86_64/processor.h | 4 +++ .../selftests/kvm/lib/aarch64/processor.c | 17 ---------- tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++ .../selftests/kvm/lib/s390x/processor.c | 22 ------------- .../selftests/kvm/lib/x86_64/processor.c | 32 ------------------- 7 files changed, 37 insertions(+), 71 deletions(-)tools/testing/selftests/kvm/include/aarch64/processor.h +3
-0@@ -9,6 +9,9 @@#include "kvm_util.h" +#define PTRS_PER_PAGE (page_size) ((page_size) / 8) +#define min_page_size () (4096) +#define min_page_shift () (12)#define ARM64_CORE_REG (x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))tools/testing/selftests/kvm/include/s390x/processor.h +0
-0tools/testing/selftests/kvm/include/s390x/processor.h line 8
Quoted 4 lines
tools/testing/selftests/kvm/include/s390x/processor.h +1
-0@@ -5,6 +5,10 @@#ifndef SELFTEST_KVM_PROCESSOR_H #define SELFTEST_KVM_PROCESSOR_H +#define PTRS_PER_PAGE (page_size) ((page_size) / 8)Doh. I think this 8 is supposed to be a 16 for s390x, considering it was dividing by 256 in its version of
vm_create_default. I need guidance from s390x gurus as to whether or not I should respin though.
Quoted 8 lines
Quoted 3 lines
Thanks,
drewThis is kind of tricky. The last level page table is only 2kb (256 entries = 1MB range). Depending on whether the page table allocation is clever or not (you can have 2 page tables in one page) this means that indeed 16 might be better. But then you actually want to change the macro name to
PTES_PER_PAGE?
Thanks Christian,
I'll respin with the macro name change and 16 for s390.
drew
Re: [PATCH 09/11] KVM: selftests: Make vm_create_default common
Quoted 10 lines · Andrew Jones
Quoted 10 lines
Quoted 9 lines
Quoted 1 line
+#define
PTRS_PER_PAGE(page_size)((page_size) / 8)Doh. I think this 8 is supposed to be a 16 for s390x, considering it was dividing by 256 in its version of
vm_create_default. I need guidance from s390x gurus as to whether or not I should respin though.Thanks,
drew
Quoted 8 lines
Quoted 4 lines
This is kind of tricky. The last level page table is only 2kb (256 entries = 1MB range). Depending on whether the page table allocation is clever or not (you can have 2 page tables in one page) this means that indeed 16 might be better. But then you actually want to change the macro name to
PTES_PER_PAGE?Thanks Christian,
I'll respin with the macro name change and 16 for s390.
Maybe it can also be moved to common header, but instead define PTR_SIZE for per-arch? I'm also curious whether PTR_SIZE will equals to "sizeof(void *)", but seems not for s390x.. Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 09/11] KVM: selftests: Make vm_create_default common
Quoted 16 lines · Peter Xu
Quoted 15 lines · Andrew Jones
Quoted 14 lines
Quoted 9 lines
Quoted 1 line
+#define
PTRS_PER_PAGE(page_size)((page_size) / 8)Doh. I think this 8 is supposed to be a 16 for s390x, considering it was dividing by 256 in its version of
vm_create_default. I need guidance from s390x gurus as to whether or not I should respin though.Thanks,
drewThis is kind of tricky. The last level page table is only 2kb (256 entries = 1MB range). Depending on whether the page table allocation is clever or not (you can have 2 page tables in one page) this means that indeed 16 might be better. But then you actually want to change the macro name to
PTES_PER_PAGE?
Quoted 7 lines
Quoted 3 lines
Thanks Christian,
I'll respin with the macro name change and 16 for s390.
Maybe it can also be moved to common header, but instead define
PTR_SIZEfor per-arch? I'm also curious whetherPTR_SIZEwill equals to "sizeof(void *)", but seems not for s390x.. Thanks,
Thats why I want to change the name. It is not about the ptr size. It is about number of page table entries in a page. And as a page table is just 2kb on s390 there is a mismatch. So instead of
#define PTRS_PER_PAGE(page_size) ((page_size) / 8)
let us just to
#define PTES_PER_PAGETABLE 256
for s390
and
#define PTES_PER_PAGETABLE 512
for the others
PATCH 10/11 KVM: selftests: Introduce vm_create_[default_]vcpus
In reply to Andrew JonesIntroduce new vm_create variants that also takes a number of vcpus, an amount of per-vcpu pages, and optionally a list of vcpuids. These variants will create default VMs with enough additional pages to cover the vcpu stacks, per-vcpu pages, and pagetable pages for all. The new 'default' variant uses VM_MODE_DEFAULT, whereas the other new variant accepts the mode as a parameter.
--- .../testing/selftests/kvm/include/kvm_util.h | 10 ++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 36 ++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
@@ -242,6 +242,16 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages,void *guest_code); +/* Same as vm_create_default, but can be used for more than one vcpu */ +struct kvm_vm *vm_create_default_with_vcpus (uint32_t nr_vcpus,uint64_t extra_mem_pages, +uint32_t num_percpu_pages,void *guest_code, +uint32_t vcpuids[]); + +/* Like vm_create_default_with_vcpus, but accepts mode as a parameter */ +struct kvm_vm *vm_create_with_vcpus (enum vm_guest_mode mode,uint32_t nr_vcpus, +uint64_t extra_mem_pages,uint32_t num_percpu_pages, +void *guest_code,uint32_t vcpuids[]); +/* * Adds a vCPU with reasonable defaults (e.g. a stack) *
tools/testing/selftests/kvm/lib/kvm_util.c +31 -5
@@ -244,8 +244,9 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)return vm; } -struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, -void *guest_code) +struct kvm_vm *vm_create_with_vcpus (enum vm_guest_mode mode,uint32_t nr_vcpus, +uint64_t extra_mem_pages,uint32_t num_percpu_pages, +void *guest_code,uint32_t vcpuids[]) {/* The maximum page table size for a memory region will be when the * smallest pages are used. Considering each page contains x page
@@ -253,10 +254,19 @@ struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,* N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller * than N/x*2. */ -uint64_t extra_pg_pages = (extra_mem_pages /PTRS_PER_PAGE (min_page_size ())) *2 ; +uint64_t vcpu_pages = (DEFAULT_STACK_PGS + num_percpu_pages) * nr_vcpus; +uint64_t extra_pg_pages = (extra_mem_pages + vcpu_pages) / +PTRS_PER_PAGE (min_page_size ()) *2 ; +uint64_t pages =vm_adjust_num_guest_pages (mode, +DEFAULT_GUEST_PHY_PAGES + vcpu_pages + extra_pg_pages);struct kvm_vm *vm; +int i; + +TEST_ASSERT (nr_vcpus <=kvm_check_cap (KVM_CAP_MAX_VCPUS ), +"nr_vcpus = %d too large for host, max-vcpus = %d" , + nr_vcpus,kvm_check_cap (KVM_CAP_MAX_VCPUS )); - vm =vm_create (VM_MODE_DEFAULT ,DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,O_RDWR ); + vm =vm_create (mode, pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );
@@ -264,11 +274,27 @@ struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,vm_create_irqchip (vm);#endif -vm_vcpu_add_default (vm, vcpuid, guest_code); +for (i =0 ; i < nr_vcpus; ++i) +vm_vcpu_add_default (vm, vcpuids ? vcpuids[i] : i, guest_code);return vm; } +struct kvm_vm *vm_create_default_with_vcpus (uint32_t nr_vcpus,uint64_t extra_mem_pages, +uint32_t num_percpu_pages,void *guest_code, +uint32_t vcpuids[]) +{ +return vm_create_with_vcpus (VM_MODE_DEFAULT , nr_vcpus, extra_mem_pages, + num_percpu_pages, guest_code, vcpuids); +} + +struct kvm_vm *vm_create_default (uint32_t vcpuid,uint64_t extra_mem_pages, +void *guest_code) +{ +return vm_create_default_with_vcpus (1 , extra_mem_pages,0 , guest_code, + (uint32_t []){ vcpuid }); +} +/* * VM Restart *
Signature · 1 line
2.26.2
PATCH 11/11 KVM: selftests: Remove create_vm
In reply to Andrew JonesRemove create_vm() from demand_paging_test and dirty_log_test by replacing it with vm_create_with_vcpus(). We also make vm_guest_mode_params[] global allowing us to clean up some page calculations and remove redundant asserts.
--- .../selftests/kvm/demand_paging_test.c | 70 ++++--------------- tools/testing/selftests/kvm/dirty_log_test.c | 62 ++++------------ .../testing/selftests/kvm/include/kvm_util.h | 9 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 +-- 4 files changed, 37 insertions(+), 113 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +12 -58
@@ -7,7 +7,7 @@* Copyright (C) 2019, Google, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ +#define _GNU_SOURCE/* for pipe2 */ #include <stdio.h> #include <stdlib.h>
@@ -145,39 +145,6 @@ static void *vcpu_worker(void *data)return NULL ; } -#define PAGE_SHIFT_4K 12 -#define PTES_PER_4K_PT 512 - -static struct kvm_vm *create_vm (enum vm_guest_mode mode,int vcpus, -uint64_t guest_percpu_mem_size) -{ -struct kvm_vm *vm; -uint64_t pages =DEFAULT_GUEST_PHY_PAGES ; - -/* Account for a few pages per-vCPU for stacks */ - pages +=DEFAULT_STACK_PGS * vcpus; - -/* -* Reserve twice the ammount of memory needed to map the test region and -* the page table / stacks region, at 4k, for page tables. Do the -* calculation with 4K page size: the smallest of all archs. (e.g., 64K -* page size guest will need even less memory for page tables). -*/ - pages += (2 * pages) /PTES_PER_4K_PT ; - pages += ((2 * vcpus * guest_percpu_mem_size) >>PAGE_SHIFT_4K ) / -PTES_PER_4K_PT ; - pages =vm_adjust_num_guest_pages (mode, pages); - -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__ -vm_create_irqchip (vm); -#endif -return vm; -} -static int handle_uffd_page_request (int uffd,uint64_t addr) {pid_t tid;
@@ -368,43 +335,32 @@ static void run_test(enum vm_guest_mode mode, void *arg)struct timespec start, end, ts_diff;int *pipefds =NULL ;struct kvm_vm *vm; +uint64_t guest_percpu_pages;uint64_t guest_num_pages;int vcpu_id;int r; - vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); - guest_page_size =vm_get_page_size (vm); + host_page_size =getpagesize (); + guest_page_size = vm_guest_mode_params[mode].page_size; -TEST_ASSERT (guest_percpu_mem_size % guest_page_size ==0 , -"Guest memory size is not guest page size aligned." ); + guest_percpu_pages =vm_calc_num_guest_pages (mode, guest_percpu_mem_size); - guest_num_pages = (nr_vcpus * guest_percpu_mem_size) / guest_page_size; - guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages); +TEST_ASSERT (guest_percpu_pages == guest_percpu_mem_size / guest_page_size, +"Guest memory size is not aligned for guest pages size %ld, try %ld" , + guest_page_size, guest_percpu_pages * guest_page_size); -/* -* If there should be more memory in the guest test region than there -* can be pages in the guest, it will definitely cause problems. -*/ -TEST_ASSERT (guest_num_pages <vm_get_max_gfn (vm), -"Requested more guest memory than address space allows.\n" -" guest pages: %lx max gfn: %x vcpus: %d wss: %lx]\n" , - guest_num_pages,vm_get_max_gfn (vm), nr_vcpus, - guest_percpu_mem_size); + guest_num_pages = nr_vcpus * guest_percpu_pages; - host_page_size =getpagesize (); -TEST_ASSERT (guest_percpu_mem_size % host_page_size ==0 , -"Guest memory size is not host page size aligned." ); + vm =vm_create_with_vcpus (mode, nr_vcpus,0 , guest_num_pages, guest_code,NULL ); - guest_test_phys_mem = (vm_get_max_gfn (vm) - guest_num_pages) * - guest_page_size; + guest_test_phys_mem = (vm_get_max_gfn (vm) - guest_num_pages) * guest_page_size; guest_test_phys_mem &= ~(host_page_size -1 ); -#ifdef __s390x__/* Align to 1M (segment size) */ guest_test_phys_mem &= ~((1 <<20 ) -1 );#endif -pr_info ("guest physical test memory offset: 0x%lx\n" , guest_test_phys_mem);/* Add an extra memory slot for testing demand paging */
@@ -442,8 +398,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)vm_paddr_t vcpu_gpa;void *vcpu_hva; -vm_vcpu_add_default (vm, vcpu_id, guest_code); - vcpu_gpa = guest_test_phys_mem + (vcpu_id * guest_percpu_mem_size);PER_VCPU_DEBUG ("Added VCPU %d with test mem gpa [%lx, %lx)\n" , vcpu_id, vcpu_gpa, vcpu_gpa + guest_percpu_mem_size);
tools/testing/selftests/kvm/dirty_log_test.c +15 -47
123 lines not on this page · raw message
tools/testing/selftests/kvm/include/kvm_util.h +9 -0
@@ -55,6 +55,15 @@ enum vm_guest_mode {#define vm_guest_mode_string (m) vm_guest_mode_string[m]extern const char *const vm_guest_mode_string[]; +struct vm_guest_mode_params { +unsigned int pa_bits; +unsigned int va_bits; +unsigned int page_size; +unsigned int page_shift; +}; + +extern const struct vm_guest_mode_params vm_guest_mode_params[]; +enum vm_mem_backing_src_type {VM_MEM_SRC_ANONYMOUS ,VM_MEM_SRC_ANONYMOUS_THP ,
tools/testing/selftests/kvm/lib/kvm_util.c +1 -8
@@ -115,14 +115,7 @@ 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[] = { +const struct vm_guest_mode_params vm_guest_mode_params[] = { {52 ,48 ,0x1000 ,12 }, {52 ,48 ,0x10000 ,16 }, {48 ,48 ,0x1000 ,12 },
Signature · 1 line
2.26.2
Quoted 9 lines · Andrew Jones
This series attempts to clean up
demand_paging_testanddirty_log_testby factoring out common code, creating some new API along the way. It's main goal is to prepare for even more factoring that Ben and Peter want to do. The series would have a nice negative diff stat, but it also picks up a few of Peter's patches for his new dirty log test. So, the +/- diff stat is close to equal. It's not as close as an electoral vote count, but it's close.I've tested on x86 and AArch64 (one config each), but not s390x.
I see no regression when I run
make TARGETS=kvm kselftest
on an s390 system with these patches applied.
Quoted 39 lines
Thanks,
drewAndrew Jones (8):
KVM: selftests: Add x86_64/tsc_msrs_test to .gitignore
KVM: selftests: Drop pointlessvm_createwrapper
KVM: selftests: Make the per vcpu memory size global
KVM: selftests: Make the number of vcpus global
KVM: selftests: Factor out guest mode code
KVM: selftests: Makevm_create_defaultcommon
KVM: selftests: Introduce vm_create_[default_]vcpus
KVM: selftests: Removecreate_vmPeter Xu (3):
KVM: selftests: Always clear dirty bitmap after iteration
KVM: selftests: Use a single binary for dirty/clear log test
KVM: selftests: Introduceafter_vcpu_runhook for dirty log testtools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 4 +- .../selftests/kvm/clear_dirty_log_test.c | 6 - .../selftests/kvm/demand_paging_test.c | 213 +++------- tools/testing/selftests/kvm/dirty_log_test.c | 372 ++++++++++-------- .../selftests/kvm/include/aarch64/processor.h | 3 + .../selftests/kvm/include/guest_modes.h | 21 + .../testing/selftests/kvm/include/kvm_util.h | 20 +- .../selftests/kvm/include/s390x/processor.h | 4 + .../selftests/kvm/include/x86_64/processor.h | 4 + .../selftests/kvm/lib/aarch64/processor.c | 17 - tools/testing/selftests/kvm/lib/guest_modes.c | 70 ++++ tools/testing/selftests/kvm/lib/kvm_util.c | 62 ++- .../selftests/kvm/lib/s390x/processor.c | 22 -- .../selftests/kvm/lib/x86_64/processor.c | 32 -- 15 files changed, 445 insertions(+), 407 deletions(-) delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c create mode 100644 tools/testing/selftests/kvm/include/guest_modes.h create mode 100644 tools/testing/selftests/kvm/lib/guest_modes.c
Quoted 9 lines · Andrew Jones
This series attempts to clean up
demand_paging_testanddirty_log_testby factoring out common code, creating some new API along the way. It's main goal is to prepare for even more factoring that Ben and Peter want to do. The series would have a nice negative diff stat, but it also picks up a few of Peter's patches for his new dirty log test. So, the +/- diff stat is close to equal. It's not as close as an electoral vote count, but it's close.I've tested on x86 and AArch64 (one config each), but not s390x.
The whole series looks good to me (probably except the PTRS_PER_PAGE one; but that's not hurting much anyways, I think). Thanks for picking up the other patches, even if they made the diff stat much less pretty..
Signature · 1 line
Peter Xu
Quoted 10 lines · Peter Xu
Quoted 8 lines · Andrew Jones
This series attempts to clean up
demand_paging_testanddirty_log_testby factoring out common code, creating some new API along the way. It's main goal is to prepare for even more factoring that Ben and Peter want to do. The series would have a nice negative diff stat, but it also picks up a few of Peter's patches for his new dirty log test. So, the +/- diff stat is close to equal. It's not as close as an electoral vote count, but it's close.
Quoted 5 lines
Quoted 1 line
I've tested on x86 and AArch64 (one config each), but not s390x.
The whole series looks good to me (probably except the
PTRS_PER_PAGEone; but that's not hurting much anyways, I think). Thanks for picking up the other patches, even if they made the diff stat much less pretty..
This series looks good to me too. Thanks for doing this Drew!
Sorry I'm later than I wanted to be in reviewing this series. I learned I was exposed to someone with COVID yesterday, so I've been a bit scattered. The dirty log perf test series v3 might be delayed a bit as a result, but I'll send it out as soon as I can after this series is merged.
Quoted 4 lines
--
Peter Xu
Quoted 17 lines · Ben Gardon
Quoted 10 lines · Peter Xu
Quoted 8 lines · Andrew Jones
This series attempts to clean up
demand_paging_testanddirty_log_testby factoring out common code, creating some new API along the way. It's main goal is to prepare for even more factoring that Ben and Peter want to do. The series would have a nice negative diff stat, but it also picks up a few of Peter's patches for his new dirty log test. So, the +/- diff stat is close to equal. It's not as close as an electoral vote count, but it's close.Quoted 5 lines
Quoted 1 line
I've tested on x86 and AArch64 (one config each), but not s390x.
The whole series looks good to me (probably except the
PTRS_PER_PAGEone; but that's not hurting much anyways, I think). Thanks for picking up the other patches, even if they made the diff stat much less pretty..
Quoted 8 lines
This series looks good to me too. Thanks for doing this Drew!
Sorry I'm later than I wanted to be in reviewing this series. I learned I was exposed to someone with COVID yesterday, so I've been a bit scattered. The dirty log perf test series v3 might be delayed a bit as a result, but I'll send it out as soon as I can after this series is merged.
Yikes! Don't worry about KVM selftests then. Except for one more question? Can I translate your "looks good to me" into a r-b for the series? And, same question for Peter. I'll be respinning witht eh PTES_PER_PAGE change and can add your guys' r-b's if you want to give them.
Thanks,
drew
Quoted 4 lines · Andrew Jones
Yikes! Don't worry about KVM selftests then. Except for one more question? Can I translate your "looks good to me" into a r-b for the series? And, same question for Peter. I'll be respinning witht eh
PTES_PER_PAGEchange and can add your guys' r-b's if you want to give them.
Yes, please feel free to add with mine (if there's a repost). Though I see that Paolo has queued the series already, so maybe it's even easier to directly work on top.
Thanks!
Signature · 1 line
Peter Xu
Quoted 6 lines · Peter Xu
Quoted 4 lines · Andrew Jones
Yikes! Don't worry about KVM selftests then. Except for one more question? Can I translate your "looks good to me" into a r-b for the series? And, same question for Peter. I'll be respinning witht eh
PTES_PER_PAGEchange and can add your guys' r-b's if you want to give them.
Ah yes, please add my reviewed-by as well, sorry that wasn't clear.
Quoted 10 lines
Yes, please feel free to add with mine (if there's a repost). Though I see that Paolo has queued the series already, so maybe it's even easier to directly work on top.
Thanks!
--
Peter Xu
Quoted 45 lines · Andrew Jones
This series attempts to clean up
demand_paging_testanddirty_log_testby factoring out common code, creating some new API along the way. It's main goal is to prepare for even more factoring that Ben and Peter want to do. The series would have a nice negative diff stat, but it also picks up a few of Peter's patches for his new dirty log test. So, the +/- diff stat is close to equal. It's not as close as an electoral vote count, but it's close.I've tested on x86 and AArch64 (one config each), but not s390x.
Thanks,
drewAndrew Jones (8):
KVM: selftests: Add x86_64/tsc_msrs_test to .gitignore
KVM: selftests: Drop pointlessvm_createwrapper
KVM: selftests: Make the per vcpu memory size global
KVM: selftests: Make the number of vcpus global
KVM: selftests: Factor out guest mode code
KVM: selftests: Makevm_create_defaultcommon
KVM: selftests: Introduce vm_create_[default_]vcpus
KVM: selftests: Removecreate_vmPeter Xu (3):
KVM: selftests: Always clear dirty bitmap after iteration
KVM: selftests: Use a single binary for dirty/clear log test
KVM: selftests: Introduceafter_vcpu_runhook for dirty log testtools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 4 +- .../selftests/kvm/clear_dirty_log_test.c | 6 - .../selftests/kvm/demand_paging_test.c | 213 +++------- tools/testing/selftests/kvm/dirty_log_test.c | 372 ++++++++++-------- .../selftests/kvm/include/aarch64/processor.h | 3 + .../selftests/kvm/include/guest_modes.h | 21 + .../testing/selftests/kvm/include/kvm_util.h | 20 +- .../selftests/kvm/include/s390x/processor.h | 4 + .../selftests/kvm/include/x86_64/processor.h | 4 + .../selftests/kvm/lib/aarch64/processor.c | 17 - tools/testing/selftests/kvm/lib/guest_modes.c | 70 ++++ tools/testing/selftests/kvm/lib/kvm_util.c | 62 ++- .../selftests/kvm/lib/s390x/processor.c | 22 -- .../selftests/kvm/lib/x86_64/processor.c | 32 -- 15 files changed, 445 insertions(+), 407 deletions(-)
Quoted 4 lines
delete mode 100644
tools/testing/selftests/kvm/clear_dirty_log_test.ccreate mode 100644tools/testing/selftests/kvm/include/guest_modes.hcreate mode 100644tools/testing/selftests/kvm/lib/guest_modes.c
Queued (or overridden by patches already in queue) patches 1-8, thanks.