KVM: selftests: Cleanups, take 2
v2 cover KVM: selftests: Cleanups, take 2
This series attempts to clean up demand_paging_test, dirty_log_perf_test, and dirty_log_test by factoring out common code, creating some new API along the way. It also splits include/perf_test_util.h into a more conventional header and source pair.
I've tested on x86 and AArch64 (one config each), but not s390x.
v2:
- Use extra_mem instead of per-vcpu mem for the test mem in demand_paging_test and dirty_log_perf_test [Ben]
- Couple more cleanups, like adding perf_test_destroy_vm() to balance create/destroy of a perf test vm [drew]
- Added new patches [drew]:
- Since dirty_log_perf_test works fine on AArch64, "KVM: selftests: Also build dirty_log_perf_test on AArch64"
- More cleanup of redundant code,
"KVM: selftests: x86: Set supported CPUIDs on default VM"
- Keep test skipping consistent,
"KVM: selftests: Make test skipping consistent"
I definitely want to get x86 people's review on "KVM: selftests: x86:
Set supported CPUIDs on default VM" to be sure it's OK to do that.
Thanks,
drew
Andrew Jones (11):
KVM: selftests: Update .gitignore
KVM: selftests: Remove deadcode
KVM: selftests: Factor out guest mode code
KVM: selftests: Make vm_create_default common
KVM: selftests: Introduce vm_create_[default_]_with_vcpus
KVM: selftests: dirty_log_test: Remove create_vm
KVM: selftests: Use vm_create_with_vcpus in create_vm
KVM: selftests: Implement perf_test_util more conventionally
KVM: selftests: Also build dirty_log_perf_test on AArch64
KVM: selftests: x86: Set supported CPUIDs on default VM
KVM: selftests: Make test skipping consistent
tools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 3 +- .../selftests/kvm/demand_paging_test.c | 118 +++-------- .../selftests/kvm/dirty_log_perf_test.c | 183 ++++-------------- tools/testing/selftests/kvm/dirty_log_test.c | 182 +++++------------ .../selftests/kvm/include/guest_modes.h | 21 ++ .../testing/selftests/kvm/include/kvm_util.h | 42 +++- .../selftests/kvm/include/perf_test_util.h | 171 ++-------------- .../selftests/kvm/lib/aarch64/processor.c | 17 -- tools/testing/selftests/kvm/lib/guest_modes.c | 70 +++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 67 ++++++- .../selftests/kvm/lib/perf_test_util.c | 134 +++++++++++++ .../selftests/kvm/lib/s390x/processor.c | 22 --- .../selftests/kvm/lib/x86_64/processor.c | 32 --- .../selftests/kvm/set_memory_region_test.c | 2 - .../kvm/x86_64/cr4_cpuid_sync_test.c | 1 - .../testing/selftests/kvm/x86_64/debug_regs.c | 1 - .../testing/selftests/kvm/x86_64/evmcs_test.c | 2 - .../selftests/kvm/x86_64/kvm_pv_test.c | 4 +- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 - .../testing/selftests/kvm/x86_64/state_test.c | 1 - .../selftests/kvm/x86_64/svm_vmcall_test.c | 1 - .../selftests/kvm/x86_64/tsc_msrs_test.c | 1 - .../selftests/kvm/x86_64/user_msr_test.c | 7 +- .../kvm/x86_64/vmx_apic_access_test.c | 1 - .../kvm/x86_64/vmx_close_while_nested_test.c | 1 - .../selftests/kvm/x86_64/vmx_dirty_log_test.c | 1 - .../kvm/x86_64/vmx_preemption_timer_test.c | 15 +- .../kvm/x86_64/vmx_set_nested_state_test.c | 21 ++ .../kvm/x86_64/vmx_tsc_adjust_test.c | 1 - 30 files changed, 499 insertions(+), 627 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/guest_modes.h create mode 100644 tools/testing/selftests/kvm/lib/guest_modes.c create mode 100644 tools/testing/selftests/kvm/lib/perf_test_util.c
Signature · 1 line
2.26.2
PATCH v2 1/11 KVM: selftests: Update .gitignore
Add x86_64/tsc_msrs_test and remove clear_dirty_log_test.
--- tools/testing/selftests/kvm/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/.gitignore +1 -1
@@ -18,13 +18,13 @@ /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 /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 /dirty_log_perf_test
Signature · 1 line
2.26.2
Re: [PATCH v2 01/11] KVM: selftests: Update .gitignore
Quoted 17 lines · Andrew Jones
Add x86_64/tsc_msrs_test and remove
clear_dirty_log_test.Reviewed-by Ben Gardon <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- tools/testing/selftests/kvm/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/.gitignore +1
-0@@ -18,13 +18,13 @@ /x86_64/vmx_preemption_timer_test /x86_64/svm_vmcall_test /x86_64/sync_regs_test +/x86_64/tsc_msrs_test
tools/testing/selftests/kvm/.gitignore line 30
Quoted 10 lines
tools/testing/selftests/kvm/.gitignore +0
-1/x86_64/vmx_apic_access_test /x86_64/vmx_close_while_nested_test /x86_64/vmx_dirty_log_test /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 /dirty_log_perf_test
This seems to conflict with another patch that are already on kvm/queue, so this series may need a rebase (and, seems this is not the only conflict)...
Signature · 1 line
Peter Xu
Re: [PATCH v2 01/11] KVM: selftests: Update .gitignore
Quoted 23 lines · Peter Xu
Quoted 22 lines · Andrew Jones
Add x86_64/tsc_msrs_test and remove
clear_dirty_log_test.Reviewed-by Ben Gardon <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- tools/testing/selftests/kvm/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/.gitignore +1
-0@@ -18,13 +18,13 @@ /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 /x86_64/vmx_set_nested_state_test /x86_64/vmx_tsc_adjust_test
Quoted 9 lines
tools/testing/selftests/kvm/.gitignore line 30
Quoted 5 lines
tools/testing/selftests/kvm/.gitignore +0
-1/x86_64/xss_msr_test -/clear_dirty_log_test /demand_paging_test /dirty_log_test /dirty_log_perf_testThis seems to conflict with another patch that are already on kvm/queue, so this series may need a rebase (and, seems this is not the only conflict)...
It looks like Paolo added "selftests: kvm: keep .gitignore add to date", which does what this patch does plus re-alphabetizes everything.
So this patch can be dropped.
Thanks,
drew
PATCH v2 2/11 KVM: selftests: Remove deadcode
In reply to Andrew JonesNothing sets USE_CLEAR_DIRTY_LOG anymore, so anything it surrounds is dead code.
--- .../selftests/kvm/dirty_log_perf_test.c | 44 ------------------- 1 file changed, 44 deletions(-)
tools/testing/selftests/kvm/dirty_log_perf_test.c +0 -44
@@ -88,10 +88,6 @@ static void *vcpu_worker(void *data)return NULL ; } -#ifdef USE_CLEAR_DIRTY_LOG -static u64 dirty_log_manual_caps; -#endif -static void run_test (enum vm_guest_mode mode,unsigned long iterations,uint64_t phys_offset,int wr_fract) {
@@ -106,10 +102,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,struct timespec get_dirty_log_total = (struct timespec ){0 };struct timespec vcpu_dirty_total = (struct timespec ){0 };struct timespec avg; -#ifdef USE_CLEAR_DIRTY_LOG -struct kvm_enable_cap cap = {}; -struct timespec clear_dirty_log_total = (struct timespec ){0 }; -#endif vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size);
@@ -120,12 +112,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, host_num_pages =vm_num_host_pages (mode, guest_num_pages); bmap =bitmap_alloc (host_num_pages); -#ifdef USE_CLEAR_DIRTY_LOG - cap.cap =KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ; - cap.args[0 ] = dirty_log_manual_caps; -vm_enable_cap (vm, &cap); -#endif - vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" );
@@ -189,18 +175,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, ts_diff);pr_info ("Iteration %lu get dirty log time: %ld.%.9lds\n" , iteration, ts_diff.tv_sec, ts_diff.tv_nsec); - -#ifdef USE_CLEAR_DIRTY_LOG -clock_gettime (CLOCK_MONOTONIC , &start); -kvm_vm_clear_dirty_log (vm,TEST_MEM_SLOT_INDEX , bmap,0 , - host_num_pages); - - ts_diff =timespec_diff_now (start); - clear_dirty_log_total =timespec_add (clear_dirty_log_total, - ts_diff); -pr_info ("Iteration %lu clear dirty log time: %ld.%.9lds\n" , - iteration, ts_diff.tv_sec, ts_diff.tv_nsec); -#endif }/* Tell the vcpu thread to quit */
@@ -220,13 +194,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, iterations, get_dirty_log_total.tv_sec, get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); -#ifdef USE_CLEAR_DIRTY_LOG - avg =timespec_div (clear_dirty_log_total, iterations); -pr_info ("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n" , - iterations, clear_dirty_log_total.tv_sec, - clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); -#endif -free (bmap);free (vcpu_threads);ucall_uninit (vm);
@@ -284,17 +251,6 @@ int main(int argc, char *argv[])int opt, i;int wr_fract =1 ; -#ifdef USE_CLEAR_DIRTY_LOG - dirty_log_manual_caps = -kvm_check_cap (KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ); -if (!dirty_log_manual_caps) { -print_skip ("KVM_CLEAR_DIRTY_LOG not available" ); -exit (KSFT_SKIP ); - } - dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | -KVM_DIRTY_LOG_INITIALLY_SET ); -#endif -#ifdef __x86_64__guest_mode_init (VM_MODE_PXXV48_4K , true, true);#endif
Signature · 1 line
2.26.2
Re: [PATCH v2 02/11] KVM: selftests: Remove deadcode
Quoted 5 lines · Andrew Jones
Nothing sets
USE_CLEAR_DIRTY_LOGanymore, so anything it surrounds is dead code.Reviewed-by Ben Gardon <[email protected]>Signed-off-by Andrew Jones <[email protected]>
It's kind of a pity that there seem to a few valid measurements for clear dirty log from Ben. I'm just thinking whether clear dirty log should be even more important since imho that should be the right way to use KVM_GET_DIRTY_LOG on a kernel new enough, since it's a total win (not like dirty ring, which depends).
So far, the statement is definitely true above, since we can always work on top. So:
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v2 02/11] KVM: selftests: Remove deadcode
Quoted 5 lines · Peter Xu
Quoted 3 lines · Andrew Jones
Nothing sets
USE_CLEAR_DIRTY_LOGanymore, so anything it surrounds is dead code.
Quoted 7 lines
Quoted 2 lines
Reviewed-by Ben Gardon <[email protected]>Signed-off-by Andrew Jones <[email protected]>It's kind of a pity that there seem to a few valid measurements for clear dirty log from Ben. I'm just thinking whether clear dirty log should be even more important since imho that should be the right way to use
KVM_GET_DIRTY_LOGon a kernel new enough, since it's a total win (not like dirty ring, which depends).
I didn't review this patch closely enough, and assumed the clear dirty log was still being done because ofafdb19600719 KVM: selftests: Use a single binary for dirty/clear log test
Looking back now, I see that that is not the case.
I'd like to retract my endorsement in that case. I'd prefer to leave the dead code in and I'll send another series to actually use it once this series is merged. I've already written the code to use it and time the clearing, so it seems a pity to remove it now.
Alternatively I could just revert this commit in that future series, though I suspect not removing the dead code would reduce the chances of merge conflicts. Either way works.
I can extend the dirty log mode functions from dirty_log_test for dirty_log_perf_test in that series too.
Quoted 11 lines
So far, the statement is definitely true above, since we can always work on top. So:
Reviewed-by Peter Xu <[email protected]>Thanks,
--
Peter Xu
Re: [PATCH v2 02/11] KVM: selftests: Remove deadcode
Quoted 11 lines · Ben Gardon
I didn't review this patch closely enough, and assumed the clear dirty log was still being done because of
afdb19600719KVM: selftests: Use a single binary for dirty/clear log testLooking back now, I see that that is not the case.
I'd like to retract my endorsement in that case. I'd prefer to leave the dead code in and I'll send another series to actually use it once this series is merged. I've already written the code to use it and time the clearing, so it seems a pity to remove it now.
Quoted 6 lines
Alternatively I could just revert this commit in that future series, though I suspect not removing the dead code would reduce the chances of merge conflicts. Either way works.
I can extend the dirty log mode functions from
dirty_log_testfordirty_log_perf_testin that series too.
Or... we can just remove all the "#ifdef" lines but assuming clear dirty log is always there? :) Assuming that is still acceptable as long as the test is matching latest kernel which definitely has the clear dirty log capability.
It's kind of weird to test get-dirty-log perf without clear dirty log, since again if anyone really cares about the perf of that, then imho they should first switch to a new kernel with clear dirty log, rather than measuring the world without clear dirty log..
Signature · 1 line
Peter Xu
Re: [PATCH v2 02/11] KVM: selftests: Remove deadcode
Quoted 19 lines · Peter Xu
Quoted 11 lines · Ben Gardon
I didn't review this patch closely enough, and assumed the clear dirty log was still being done because of
afdb19600719KVM: selftests: Use a single binary for dirty/clear log testLooking back now, I see that that is not the case.
I'd like to retract my endorsement in that case. I'd prefer to leave the dead code in and I'll send another series to actually use it once this series is merged. I've already written the code to use it and time the clearing, so it seems a pity to remove it now.
Quoted 6 lines
Alternatively I could just revert this commit in that future series, though I suspect not removing the dead code would reduce the chances of merge conflicts. Either way works.
I can extend the dirty log mode functions from
dirty_log_testfordirty_log_perf_testin that series too.
Quoted 9 lines
Or... we can just remove all the "#ifdef" lines but assuming clear dirty log is always there? :) Assuming that is still acceptable as long as the test is matching latest kernel which definitely has the clear dirty log capability.
It's kind of weird to test get-dirty-log perf without clear dirty log, since again if anyone really cares about the perf of that, then imho they should first switch to a new kernel with clear dirty log, rather than measuring the world without clear dirty log..
I have no opinion on this other than I'd prefer KVM selftests to not have deadcode. If it makes more sense to fix the deadcode by bringing it back to life than to drop the code, then please send patches to do that, at which point I'd be happy to recommend dropping this patch.
Thanks,
drew
Paolo Bonzini
maintainerRe: [PATCH v2 02/11] KVM: selftests: Remove deadcode
In reply to Ben GardonQuoted 11 lines · Ben Gardon
I didn't review this patch closely enough, and assumed the clear dirty log was still being done because of
afdb19600719KVM: selftests: Use a single binary for dirty/clear log testLooking back now, I see that that is not the case.
I'd like to retract my endorsement in that case. I'd prefer to leave the dead code in and I'll send another series to actually use it once this series is merged. I've already written the code to use it and time the clearing, so it seems a pity to remove it now.
Quoted 6 lines
Alternatively I could just revert this commit in that future series, though I suspect not removing the dead code would reduce the chances of merge conflicts. Either way works.
I can extend the dirty log mode functions from
dirty_log_testfordirty_log_perf_testin that series too.
For now I'll follow Peter's suggestion to always test manual clear:
-------------- 8< ------------
Subject: [PATCH] KVM: selftests: always use manual clear in dirty_log_perf_test
Nothing sets USE_CLEAR_DIRTY_LOG anymore, so anything it surrounds is dead code.
However, it is the recommended way to use the dirty page bitmap for new enough kernel, so use it whenever KVM has the KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 capability.
tools/testing/selftests/kvm/dirty_log_perf_test.c +22 -33
@@ -27,6 +27,7 @@#define TEST_HOST_LOOP_N 2UL/* Host variables */ +static u64 dirty_log_manual_caps;static bool host_quit;static uint64_t iteration;static uint64_t vcpu_last_completed_iteration[MAX_VCPUS ];
@@ -88,10 +89,6 @@ static void *vcpu_worker(void *data)return NULL ; } -#ifdef USE_CLEAR_DIRTY_LOG -static u64 dirty_log_manual_caps; -#endif -static void run_test (enum vm_guest_mode mode,unsigned long iterations,uint64_t phys_offset,int wr_fract) {
@@ -106,10 +103,8 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,struct timespec get_dirty_log_total = (struct timespec ){0 };struct timespec vcpu_dirty_total = (struct timespec ){0 };struct timespec avg; -#ifdef USE_CLEAR_DIRTY_LOG struct kvm_enable_cap cap = {};struct timespec clear_dirty_log_total = (struct timespec ){0 }; -#endif vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size);
@@ -120,11 +115,11 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, host_num_pages =vm_num_host_pages (mode, guest_num_pages); bmap =bitmap_alloc (host_num_pages); -#ifdef USE_CLEAR_DIRTY_LOG - cap.cap =KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ; - cap.args[0 ] = dirty_log_manual_caps; -vm_enable_cap (vm, &cap); -#endif +if (dirty_log_manual_caps) { + cap.cap =KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ; + cap.args[0 ] = dirty_log_manual_caps; +vm_enable_cap (vm, &cap); + } vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" );
@@ -190,17 +185,17 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,pr_info ("Iteration %lu get dirty log time: %ld.%.9lds\n" , iteration, ts_diff.tv_sec, ts_diff.tv_nsec); -#ifdef USE_CLEAR_DIRTY_LOG -clock_gettime (CLOCK_MONOTONIC , &start); -kvm_vm_clear_dirty_log (vm,TEST_MEM_SLOT_INDEX , bmap,0 , - host_num_pages); +if (dirty_log_manual_caps) { +clock_gettime (CLOCK_MONOTONIC , &start); +kvm_vm_clear_dirty_log (vm,TEST_MEM_SLOT_INDEX , bmap,0 , + host_num_pages); - ts_diff =timespec_diff_now (start); - clear_dirty_log_total =timespec_add (clear_dirty_log_total, - ts_diff); -pr_info ("Iteration %lu clear dirty log time: %ld.%.9lds\n" , - iteration, ts_diff.tv_sec, ts_diff.tv_nsec); -#endif + ts_diff =timespec_diff_now (start); + clear_dirty_log_total =timespec_add (clear_dirty_log_total, + ts_diff); +pr_info ("Iteration %lu clear dirty log time: %ld.%.9lds\n" , + iteration, ts_diff.tv_sec, ts_diff.tv_nsec); + } }/* Tell the vcpu thread to quit */
@@ -220,12 +215,12 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, iterations, get_dirty_log_total.tv_sec, get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); -#ifdef USE_CLEAR_DIRTY_LOG - avg =timespec_div (clear_dirty_log_total, iterations); -pr_info ("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n" , - iterations, clear_dirty_log_total.tv_sec, - clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); -#endif +if (dirty_log_manual_caps) { + avg =timespec_div (clear_dirty_log_total, iterations); +pr_info ("Clear dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n" , + iterations, clear_dirty_log_total.tv_sec, + clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); + }free (bmap);free (vcpu_threads);
@@ -284,16 +279,10 @@ int main(int argc, char *argv[])int opt, i;int wr_fract =1 ; -#ifdef USE_CLEAR_DIRTY_LOG dirty_log_manual_caps =kvm_check_cap (KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ); -if (!dirty_log_manual_caps) { -print_skip ("KVM_CLEAR_DIRTY_LOG not available" ); -exit (KSFT_SKIP ); - } dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |KVM_DIRTY_LOG_INITIALLY_SET ); -#endif #ifdef __x86_64__guest_mode_init (VM_MODE_PXXV48_4K , true, true);
PATCH v2 3/11 KVM: selftests: Factor out guest mode code
In reply to Andrew Jonesdemand_paging_test, dirty_log_test, and dirty_log_perf_test have redundant guest mode code. Factor it out.
Also, while adding a new include, remove the ones we don't need.
--- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/demand_paging_test.c | 107 ++++----------- .../selftests/kvm/dirty_log_perf_test.c | 119 +++++------------ tools/testing/selftests/kvm/dirty_log_test.c | 125 ++++++------------ .../selftests/kvm/include/guest_modes.h | 21 +++ tools/testing/selftests/kvm/lib/guest_modes.c | 70 ++++++++++ 6 files changed, 188 insertions(+), 256 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 lib/x86_64/handlers.S 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 +26 -81
184 lines not on this page · raw message
tools/testing/selftests/kvm/dirty_log_perf_test.c +33 -86
202 lines not on this page · raw message
tools/testing/selftests/kvm/dirty_log_test.c +37 -88
203 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
Re: [PATCH v2 03/11] KVM: selftests: Factor out guest mode code
Quoted 7 lines · Andrew Jones
demand_paging_test,dirty_log_test, anddirty_log_perf_testhave redundant guest mode code. Factor it out.Also, while adding a new include, remove the ones we don't need.
Signed-off-by Andrew Jones <[email protected]>
This is a great clean up, thank you for doing this!
Quoted 755 lines
--- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/demand_paging_test.c | 107 ++++----------- .../selftests/kvm/dirty_log_perf_test.c | 119 +++++------------ tools/testing/selftests/kvm/dirty_log_test.c | 125 ++++++------------ .../selftests/kvm/include/guest_modes.h | 21 +++ tools/testing/selftests/kvm/lib/guest_modes.c | 70 ++++++++++ 6 files changed, 188 insertions(+), 256 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/guest_modes.h create mode 100644 tools/testing/selftests/kvm/lib/guest_modes.ctools/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 lib/x86_64/handlers.S LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.ctools/testing/selftests/kvm/demand_paging_test.c +26
-81@@ -7,23 +7,20 @@* Copyright (C) 2019, Google, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ +#define _GNU_SOURCE/* for program_invocation_name and pipe2 */ #include <stdio.h> #include <stdlib.h> -#include <sys/syscall.h> -#include <unistd.h> -#include <asm/unistd.h> #include <time.h> #include <poll.h> #include <pthread.h> -#include <linux/bitmap.h> -#include <linux/bitops.h> #include <linux/userfaultfd.h> +#include <sys/syscall.h> -#include "perf_test_util.h" -#include "processor.h" +#include "kvm_util.h" #include "test_util.h" +#include "perf_test_util.h" +#include "guest_modes.h" #ifdef __NR_userfaultfd@@ -248,9 +245,14 @@ static int setup_demand_paging(struct kvm_vm *vm,return 0 ; } -static void run_test (enum vm_guest_mode mode,bool use_uffd, -useconds_t uffd_delay) +struct test_params { +bool use_uffd; +useconds_t uffd_delay; +}; + +static void run_test (enum vm_guest_mode mode,void *arg) { +struct test_params *p = arg;pthread_t *vcpu_threads;pthread_t *uffd_handler_threads =NULL ;struct uffd_handler_args *uffd_args =NULL ;@@ -275,7 +277,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,add_vcpus (vm, nr_vcpus, guest_percpu_mem_size); -if (use_uffd) { +if (p->use_uffd) { uffd_handler_threads =malloc (nr_vcpus *sizeof (*uffd_handler_threads));TEST_ASSERT (uffd_handler_threads,"Memory allocation failed" );@@ -308,7 +310,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd, r =setup_demand_paging (vm, &uffd_handler_threads[vcpu_id], pipefds[vcpu_id *2 ], - uffd_delay, &uffd_args[vcpu_id], + p->uffd_delay, &uffd_args[vcpu_id], vcpu_hva, guest_percpu_mem_size);if (r <0 )exit (-r);@@ -339,7 +341,7 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,pr_info ("All vCPU threads joined\n" ); -if (use_uffd) { +if (p->use_uffd) {char c;/* Tell the user fault fd handler threads to quit */ @@ -362,38 +364,19 @@ static void run_test(enum vm_guest_mode mode, bool use_uffd,free (guest_data_prototype);free (vcpu_threads); -if (use_uffd) { +if (p->use_uffd) {free (uffd_handler_threads);free (uffd_args);free (pipefds); } } -struct guest_mode { -bool supported; -bool enabled; -}; -static struct guest_mode guest_modes[NUM_VM_MODES ]; - -#define guest_mode_init (mode, supported, enabled) ({ \ - guest_modes[mode] = (struct guest_mode){ supported, enabled }; \ -}) -static void help (char *name) { -int i; -puts ("" );printf ("usage: %s [-h] [-m mode] [-u] [-d uffd_delay_usec]\n" " [-b memory] [-v vcpus]\n" , name); -printf (" -m: specify the guest mode ID to test\n" -" (default: test all supported modes)\n" -" This option may be used multiple times.\n" -" Guest mode IDs:\n" ); -for (i =0 ; i <NUM_VM_MODES ; ++i) { -printf (" %d: %s%s\n" , i,vm_guest_mode_string (i), - guest_modes[i].supported ?" (supported)" :"" ); - } +guest_modes_help ();printf (" -u: use User Fault FD to handle vCPU page\n" " faults.\n" ); printf(" -d: add a delay in usec to the User Fault\n"@@ -410,53 +393,22 @@ 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; -unsigned int mode; -int opt, i; -bool use_uffd = false; -useconds_t uffd_delay =0 ; - -#ifdef __x86_64__ -guest_mode_init (VM_MODE_PXXV48_4K , true, true); -#endif -#ifdef __aarch64__ -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 ) -guest_mode_init (VM_MODE_P52V48_64K , true, true); -if (limit >=48 ) { -guest_mode_init (VM_MODE_P48V48_4K , true, true); -guest_mode_init (VM_MODE_P48V48_64K , true, true); - } - } -#endif -#ifdef __s390x__ -guest_mode_init (VM_MODE_P40V48_4K , true, true); -#endif +struct test_params p = {}; +int opt; + +guest_modes_append_default ();while ((opt =getopt (argc, argv,"hm:ud:b:v:" )) !=-1 ) {switch (opt) {case 'm' : -if (!mode_selected) { -for (i =0 ; i <NUM_VM_MODES ; ++i) - 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); - guest_modes[mode].enabled = true; +guest_modes_cmdline (optarg);break ;case 'u' : - use_uffd = true; + p.use_uffd = true;break ;case 'd' : - uffd_delay =strtoul (optarg,NULL ,0 ); -TEST_ASSERT (uffd_delay >=0 , -"A negative UFFD delay is not supported." ); + p.uffd_delay =strtoul (optarg,NULL ,0 ); +TEST_ASSERT (p.uffd_delay >=0 ,"A negative UFFD delay is not supported." );break ;case 'b' : guest_percpu_mem_size =parse_size (optarg);@@ -473,14 +425,7 @@ int main(int argc, char *argv[]) } } -for (i =0 ; i <NUM_VM_MODES ; ++i) { -if (!guest_modes[i].enabled) -continue ; -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); - } +for_each_guest_mode (run_test, &p);return 0 ; }tools/testing/selftests/kvm/dirty_log_perf_test.c +33
-86@@ -12,16 +12,14 @@#include <stdio.h> #include <stdlib.h> -#include <unistd.h> #include <time.h> #include <pthread.h> #include <linux/bitmap.h> -#include <linux/bitops.h> #include "kvm_util.h" -#include "perf_test_util.h" -#include "processor.h" #include "test_util.h" +#include "perf_test_util.h" +#include "guest_modes.h" /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ #define TEST_HOST_LOOP_N 2UL@@ -88,9 +86,15 @@ static void *vcpu_worker(void *data)return NULL ; } -static void run_test (enum vm_guest_mode mode,unsigned long iterations, -uint64_t phys_offset,int wr_fract) +struct test_params { +unsigned long iterations; +uint64_t phys_offset; +int wr_fract; +}; + +static void run_test (enum vm_guest_mode mode,void *arg) { +struct test_params *p = arg;pthread_t *vcpu_threads;struct kvm_vm *vm;unsigned long *bmap;@@ -105,7 +109,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); - perf_test_args.wr_fract = wr_fract; + perf_test_args.wr_fract = p->wr_fract; guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >>vm_get_page_shift (vm); guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages);@@ -147,7 +151,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,pr_info ("Enabling dirty logging time: %ld.%.9lds\n\n" , ts_diff.tv_sec, ts_diff.tv_nsec); -while (iteration < iterations) { +while (iteration < p->iterations) {/* * Incrementing the iteration number will start the vCPUs * dirtying memory again. @@ -189,9 +193,9 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,pr_info ("Disabling dirty logging time: %ld.%.9lds\n" , ts_diff.tv_sec, ts_diff.tv_nsec); - avg =timespec_div (get_dirty_log_total, iterations); + avg =timespec_div (get_dirty_log_total, p->iterations);pr_info ("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n" , - iterations, get_dirty_log_total.tv_sec, + p->iterations, get_dirty_log_total.tv_sec, get_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec);free (bmap);@@ -200,20 +204,8 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,kvm_vm_free (vm); } -struct guest_mode { -bool supported; -bool enabled; -}; -static struct guest_mode guest_modes[NUM_VM_MODES ]; - -#define guest_mode_init (mode, supported, enabled) ({ \ - guest_modes[mode] = (struct guest_mode){ supported, enabled }; \ -}) -static void help (char *name) { -int i; -puts ("" );printf ("usage: %s [-h] [-i iterations] [-p offset] " "[-m mode] [-b vcpu bytes] [-v vcpus]\n" , name);@@ -222,14 +214,7 @@ static void help(char *name) TEST_HOST_LOOP_N);printf (" -p: specify guest physical test memory offset\n" " Warning: a low offset can conflict with the loaded test code.\n" ); -printf (" -m: specify the guest mode ID to test " -"(default: test all supported modes)\n" -" This option may be used multiple times.\n" -" Guest mode IDs:\n" ); -for (i =0 ; i <NUM_VM_MODES ; ++i) { -printf (" %d: %s%s\n" , i,vm_guest_mode_string (i), - guest_modes[i].supported ?" (supported)" :"" ); - } +guest_modes_help ();printf (" -b: specify the size of the memory region which should be\n" " dirtied by each vCPU. e.g. 10M or 3G.\n" " (default: 1G)\n" );@@ -244,69 +229,38 @@ static void help(char *name)int main (int argc,char *argv[]) { -unsigned long iterations =TEST_HOST_LOOP_N ; -bool mode_selected = false; -uint64_t phys_offset =0 ; -unsigned int mode; -int opt, i; -int wr_fract =1 ; - -#ifdef __x86_64__ -guest_mode_init (VM_MODE_PXXV48_4K , true, true); -#endif -#ifdef __aarch64__ -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 ) -guest_mode_init (VM_MODE_P52V48_64K , true, true); -if (limit >=48 ) { -guest_mode_init (VM_MODE_P48V48_4K , true, true); -guest_mode_init (VM_MODE_P48V48_64K , true, true); - } - } -#endif -#ifdef __s390x__ -guest_mode_init (VM_MODE_P40V48_4K , true, true); -#endif +int max_vcpus =kvm_check_cap (KVM_CAP_MAX_VCPUS ); +struct test_params p = { + .iterations =TEST_HOST_LOOP_N , + .wr_fract =1 , + }; +int opt; + +guest_modes_append_default ();while ((opt =getopt (argc, argv,"hi:p:m:b:f:v:" )) !=-1 ) {switch (opt) {case 'i' : - iterations =strtol (optarg,NULL ,10 ); + p.iterations =strtol (optarg,NULL ,10 );break ;case 'p' : - phys_offset =strtoull (optarg,NULL ,0 ); + p.phys_offset =strtoull (optarg,NULL ,0 );break ;case 'm' : -if (!mode_selected) { -for (i =0 ; i <NUM_VM_MODES ; ++i) - 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); - guest_modes[mode].enabled = true; +guest_modes_cmdline (optarg);break ;case 'b' : guest_percpu_mem_size =parse_size (optarg);break ;case 'f' : - wr_fract =atoi (optarg); -TEST_ASSERT (wr_fract >=1 , + p.wr_fract =atoi (optarg); +TEST_ASSERT (p.wr_fract >=1 ,"Write fraction cannot be less than one" );break ;case 'v' : nr_vcpus =atoi (optarg); -TEST_ASSERT (nr_vcpus >0 , -"Must have a positive number of vCPUs" ); -TEST_ASSERT (nr_vcpus <=MAX_VCPUS , -"This test does not currently support\n" -"more than %d vCPUs." ,MAX_VCPUS ); +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 :@@ -315,18 +269,11 @@ int main(int argc, char *argv[]) } } -TEST_ASSERT (iterations >=2 ,"The test should have at least two iterations" ); +TEST_ASSERT (p.iterations >=2 ,"The test should have at least two iterations" ); -pr_info ("Test iterations: %" PRIu64"\n" , iterations); +pr_info ("Test iterations: %" PRIu64"\n" , p.iterations); -for (i =0 ; i <NUM_VM_MODES ; ++i) { -if (!guest_modes[i].enabled) -continue ; -TEST_ASSERT (guest_modes[i].supported, -"Guest mode ID %d (%s) not supported." , - i,vm_guest_mode_string (i)); -run_test (i, iterations, phys_offset, wr_fract); - } +for_each_guest_mode (run_test, &p);return 0 ; }tools/testing/selftests/kvm/dirty_log_test.c +37
-88@@ -9,14 +9,13 @@#include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <time.h> #include <pthread.h> #include <linux/bitmap.h> #include <linux/bitops.h> -#include "test_util.h" #include "kvm_util.h" +#include "test_util.h" +#include "guest_modes.h" #include "processor.h" #define VCPU_ID 1@@ -375,9 +374,15 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,#define DIRTY_MEM_BITS 30/* 1G */ #define PAGE_SHIFT_4K 12 -static void run_test (enum vm_guest_mode mode,unsigned long iterations, -unsigned long interval,uint64_t phys_offset) +struct test_params { +unsigned long iterations; +unsigned long interval; +uint64_t phys_offset; +}; + +static void run_test (enum vm_guest_mode mode,void *arg) { +struct test_params *p = arg;pthread_t vcpu_thread;struct kvm_vm *vm;unsigned long *bmap;@@ -412,12 +417,12 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations, host_page_size =getpagesize (); host_num_pages =vm_num_host_pages (mode, guest_num_pages); -if (!phys_offset) { +if (!p->phys_offset) { guest_test_phys_mem = (vm_get_max_gfn (vm) - guest_num_pages) * guest_page_size; guest_test_phys_mem &= ~(host_page_size -1 ); }else { - guest_test_phys_mem = phys_offset; + guest_test_phys_mem = p->phys_offset; }#ifdef __s390x__@@ -464,9 +469,9 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,pthread_create (&vcpu_thread,NULL , vcpu_worker, vm); -while (iteration < iterations) { +while (iteration < p->iterations) {/* Give the vcpu thread some time to dirty some pages */ -usleep (interval *1000 ); +usleep (p->interval *1000 );log_mode_collect_dirty_pages (vm,TEST_MEM_SLOT_INDEX , bmap, host_num_pages);vm_dirty_log_verify (mode, bmap);@@ -488,20 +493,8 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,kvm_vm_free (vm); } -struct guest_mode { -bool supported; -bool enabled; -}; -static struct guest_mode guest_modes[NUM_VM_MODES ]; - -#define guest_mode_init (mode, supported, enabled) ({ \ - guest_modes[mode] = (struct guest_mode){ supported, enabled }; \ -}) -static void help (char *name) { -int i; -puts ("" );printf ("usage: %s [-h] [-i iterations] [-I interval] " "[-p offset] [-m mode]\n" , name);@@ -515,70 +508,34 @@ static void help(char *name)printf (" -M: specify the host logging mode " "(default: run all log modes). Supported modes: \n\t" );log_modes_dump (); -printf (" -m: specify the guest mode ID to test " -"(default: test all supported modes)\n" -" This option may be used multiple times.\n" -" Guest mode IDs:\n" ); -for (i =0 ; i <NUM_VM_MODES ; ++i) { -printf (" %d: %s%s\n" , i,vm_guest_mode_string (i), - guest_modes[i].supported ?" (supported)" :"" ); - } +guest_modes_help ();puts ("" );exit (0 ); }int main (int argc,char *argv[]) { -unsigned long iterations =TEST_HOST_LOOP_N ; -unsigned long interval =TEST_HOST_LOOP_INTERVAL ; -bool mode_selected = false; -uint64_t phys_offset =0 ; -unsigned int mode; -int opt, i, j; - -#ifdef __x86_64__ -guest_mode_init (VM_MODE_PXXV48_4K , true, true); -#endif -#ifdef __aarch64__ -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 ); +struct test_params p = { + .iterations =TEST_HOST_LOOP_N , + .interval =TEST_HOST_LOOP_INTERVAL , + }; +int opt, i; -if (limit >=52 ) -guest_mode_init (VM_MODE_P52V48_64K , true, true); -if (limit >=48 ) { -guest_mode_init (VM_MODE_P48V48_4K , true, true); -guest_mode_init (VM_MODE_P48V48_64K , true, true); - } - } -#endif -#ifdef __s390x__ -guest_mode_init (VM_MODE_P40V48_4K , true, true); -#endif +guest_modes_append_default ();while ((opt =getopt (argc, argv,"hi:I:p:m:M:" )) !=-1 ) {switch (opt) {case 'i' : - iterations =strtol (optarg,NULL ,10 ); + p.iterations =strtol (optarg,NULL ,10 );break ;case 'I' : - interval =strtol (optarg,NULL ,10 ); + p.interval =strtol (optarg,NULL ,10 );break ;case 'p' : - phys_offset =strtoull (optarg,NULL ,0 ); + p.phys_offset =strtoull (optarg,NULL ,0 );break ;case 'm' : -if (!mode_selected) { -for (i =0 ; i <NUM_VM_MODES ; ++i) - 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); - guest_modes[mode].enabled = true; +guest_modes_cmdline (optarg);break ;case 'M' :if (!strcmp (optarg,"all" )) {@@ -607,32 +564,24 @@ 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" ); +TEST_ASSERT (p.iterations >2 ,"Iterations must be greater than two" ); +TEST_ASSERT (p.interval >0 ,"Interval must be greater than zero" );pr_info ("Test iterations: %" PRIu64", interval: %" PRIu64" (ms)\n" , - iterations, interval); + p.iterations, p.interval);srandom (time (0 )); -for (i =0 ; i <NUM_VM_MODES ; ++i) { -if (!guest_modes[i].enabled) -continue ; -TEST_ASSERT (guest_modes[i].supported, -"Guest mode ID %d (%s) not supported." , - i,vm_guest_mode_string (i)); -if (host_log_mode_option ==LOG_MODE_ALL ) { -/* Run each log mode */ -for (j =0 ; j <LOG_MODE_NUM ; j++) { -pr_info ("Testing Log Mode '%s'\n" , - log_modes[j].name); - host_log_mode = j; -run_test (i, iterations, interval, phys_offset); - } - }else { - host_log_mode = host_log_mode_option; -run_test (i, iterations, interval, phys_offset); +if (host_log_mode_option ==LOG_MODE_ALL ) { +/* Run each log mode */ +for (i =0 ; i <LOG_MODE_NUM ; i++) { +pr_info ("Testing Log Mode '%s'\n" , log_modes[i].name); + host_log_mode = i; +for_each_guest_mode (run_test, &p); } + }else { + host_log_mode = host_log_mode_option; +for_each_guest_mode (run_test, &p); }return 0 ;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
-1@@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020, Red Hat, Inc. +*/ +#include "guest_modes.h" + +struct guest_mode guest_modes[NUM_VM_MODES ]; + +void guest_modes_append_default (void ) +{ +guest_mode_append (VM_MODE_DEFAULT , true, true); + +#ifdef __aarch64__ +guest_mode_append (VM_MODE_P40V48_64K , true, true); + { +unsigned int limit =kvm_check_cap (KVM_CAP_ARM_VM_IPA_SIZE ); +if (limit >=52 ) +guest_mode_append (VM_MODE_P52V48_64K , true, true); +if (limit >=48 ) { +guest_mode_append (VM_MODE_P48V48_4K , true, true); +guest_mode_append (VM_MODE_P48V48_64K , true, true); + } + } +#endif +} + +void for_each_guest_mode (void (*func)(enum vm_guest_mode ,void *),void *arg) +{ +int i; + +for (i =0 ; i <NUM_VM_MODES ; ++i) { +if (!guest_modes[i].enabled) +continue ; +TEST_ASSERT (guest_modes[i].supported, +"Guest mode ID %d (%s) not supported." , + i,vm_guest_mode_string (i)); +func (i, arg); + } +} + +void guest_modes_help (void ) +{ +int i; + +printf (" -m: specify the guest mode ID to test\n" +" (default: test all supported modes)\n" +" This option may be used multiple times.\n" +" Guest mode IDs:\n" ); +for (i =0 ; i <NUM_VM_MODES ; ++i) { +printf (" %d: %s%s\n" , i,vm_guest_mode_string (i), + guest_modes[i].supported ?" (supported)" :"" ); + } +} + +void guest_modes_cmdline (const char *arg) +{ +static bool mode_selected; +unsigned int mode; +int i; + +if (!mode_selected) { +for (i =0 ; i <NUM_VM_MODES ; ++i) + 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); + guest_modes[mode].enabled = true; +} --2.26.2
Re: [PATCH v2 03/11] KVM: selftests: Factor out guest mode code
In reply to Andrew JonesQuoted 6 lines · Andrew Jones
demand_paging_test,dirty_log_test, anddirty_log_perf_testhave redundant guest mode code. Factor it out.Also, while adding a new include, remove the ones we don't need.
Signed-off-by Andrew Jones <[email protected]>
Signature · 1 line
Peter Xu
PATCH v2 4/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 macros.
--- .../testing/selftests/kvm/include/kvm_util.h | 23 ++++++++++--- .../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 ------------------- 5 files changed, 45 insertions(+), 75 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +19 -4
@@ -45,13 +45,28 @@ enum vm_guest_mode { };#if defined(__aarch64__) -#define VM_MODE_DEFAULT VM_MODE_P40V48_4K + +#define VM_MODE_DEFAULT VM_MODE_P40V48_4K +#define MIN_PAGE_SHIFT 12U +#define ptes_per_page (page_size) ((page_size) / 8) +#elif defined(__x86_64__) -#define VM_MODE_DEFAULT VM_MODE_PXXV48_4K -#else -#define VM_MODE_DEFAULT VM_MODE_P52V48_4K + +#define VM_MODE_DEFAULT VM_MODE_PXXV48_4K +#define MIN_PAGE_SHIFT 12U +#define ptes_per_page (page_size) ((page_size) / 8) + +#elif defined(__s390x__) + +#define VM_MODE_DEFAULT VM_MODE_P52V48_4K +#define MIN_PAGE_SHIFT 12U +#define ptes_per_page (page_size) ((page_size) / 16) +#endif +#define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT) +#define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE) +#define vm_guest_mode_string (m) vm_guest_mode_string[m]extern const char *const vm_guest_mode_string[];
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"
@@ -271,6 +272,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 /PTES_PER_MIN_PAGE ) *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"
@@ -731,36 +729,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
PATCH v2 5/11 KVM: selftests: Introduce vm_create_[default_]_with_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 | 35 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
@@ -261,6 +261,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 +30 -5
@@ -272,8 +272,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
@@ -281,10 +282,18 @@ 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 /PTES_PER_MIN_PAGE ) *2 ; +uint64_t vcpu_pages = (DEFAULT_STACK_PGS + num_percpu_pages) * nr_vcpus; +uint64_t extra_pg_pages = (extra_mem_pages + vcpu_pages) /PTES_PER_MIN_PAGE *2 ; +uint64_t pages =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 ); + pages =vm_adjust_num_guest_pages (mode, pages); + vm =vm_create (mode, pages,O_RDWR );kvm_vm_elf_load (vm, program_invocation_name,0 ,0 );
@@ -292,11 +301,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 v2 6/11 KVM: selftests: dirty_log_test: Remove create_vm
In reply to Andrew JonesUse vm_create_with_vcpus instead of create_vm and do some minor cleanups around it.
--- tools/testing/selftests/kvm/dirty_log_test.c | 56 ++++++-------------- 1 file changed, 16 insertions(+), 40 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +16 -40
@@ -5,8 +5,6 @@* Copyright (C) 2018, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <stdio.h> #include <stdlib.h> #include <pthread.h>
@@ -20,6 +18,9 @@#define VCPU_ID 1 +#define DIRTY_MEM_BITS 30/* 1G */ +#define DIRTY_MEM_SIZE (1UL << 30) +/* The memory slot index to track dirty pages */ #define TEST_MEM_SLOT_INDEX 1
@@ -353,27 +354,6 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) } } -static struct kvm_vm *create_vm (enum vm_guest_mode mode,uint32_t vcpuid, -uint64_t extra_mem_pages,void *guest_code) -{ -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__ -vm_create_irqchip (vm); -#endif -log_mode_create_vm_done (vm); -vm_vcpu_add_default (vm, vcpuid, guest_code); -return vm; -} - -#define DIRTY_MEM_BITS 30/* 1G */ -#define PAGE_SHIFT_4K 12 -struct test_params {unsigned long iterations;unsigned long interval;
@@ -393,43 +373,39 @@ static void run_test(enum vm_guest_mode mode, void *arg)return ; } +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); +/* * We reserve page table for 2 times of extra dirty mem which -* will definitely cover the original (1G+) test range. Here -* we do the calculation with 4K page size which is the -* smallest so the page number will be enough for all archs -* (e.g., 64K page size guest will need even less memory for -* page tables). +* will definitely cover the original (1G+) test range. */ - vm =create_vm (mode,VCPU_ID , -2ul << (DIRTY_MEM_BITS -PAGE_SHIFT_4K ), - guest_code); + vm =vm_create_with_vcpus (mode,1 , +vm_calc_num_guest_pages (mode,DIRTY_MEM_SIZE *2 ), +0 , guest_code, (uint32_t []){VCPU_ID }); + +log_mode_create_vm_done (vm); guest_page_size =vm_get_page_size (vm); + host_page_size =getpagesize (); +/* * A little more than 1G of guest page sized pages. Cover the * case where the size is not aligned to 64 pages. */ - guest_num_pages = (1ul << (DIRTY_MEM_BITS - -vm_get_page_shift (vm))) +3 ; - guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages); - - host_page_size =getpagesize (); + guest_num_pages =vm_adjust_num_guest_pages (mode, + (1ul << (DIRTY_MEM_BITS -vm_get_page_shift (vm))) +3 ); host_num_pages =vm_num_host_pages (mode, guest_num_pages);if (!p->phys_offset) { - 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 ); }else { guest_test_phys_mem = p->phys_offset; } -#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); bmap =bitmap_alloc (host_num_pages);
Signature · 1 line
2.26.2
Re: [PATCH v2 06/11] KVM: selftests: dirty_log_test: Remove create_vm
Quoted 5 lines · Andrew Jones
Use
vm_create_with_vcpusinstead ofcreate_vmand do some minor cleanups around it.Signed-off-by Andrew Jones <[email protected]>
Quoted 115 lines
--- tools/testing/selftests/kvm/dirty_log_test.c | 56 ++++++-------------- 1 file changed, 16 insertions(+), 40 deletions(-)tools/testing/selftests/kvm/dirty_log_test.c +16
-41@@ -5,8 +5,6 @@* Copyright (C) 2018, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <stdio.h> #include <stdlib.h> #include <pthread.h> @@ -20,6 +18,9 @@#define VCPU_ID 1 +#define DIRTY_MEM_BITS 30/* 1G */ +#define DIRTY_MEM_SIZE (1UL << 30) +/* The memory slot index to track dirty pages */ #define TEST_MEM_SLOT_INDEX 1@@ -353,27 +354,6 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) } } -static struct kvm_vm *create_vm (enum vm_guest_mode mode,uint32_t vcpuid, -uint64_t extra_mem_pages,void *guest_code) -{ -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__ -vm_create_irqchip (vm); -#endif -log_mode_create_vm_done (vm); -vm_vcpu_add_default (vm, vcpuid, guest_code); -return vm; -} - -#define DIRTY_MEM_BITS 30/* 1G */ -#define PAGE_SHIFT_4K 12 -struct test_params {unsigned long iterations;unsigned long interval;@@ -393,43 +373,39 @@ static void run_test(enum vm_guest_mode mode, void *arg)return ; } +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); +/* * We reserve page table for 2 times of extra dirty mem which -* will definitely cover the original (1G+) test range. Here -* we do the calculation with 4K page size which is the -* smallest so the page number will be enough for all archs -* (e.g., 64K page size guest will need even less memory for -* page tables). +* will definitely cover the original (1G+) test range. */ - vm =create_vm (mode,VCPU_ID , -2ul << (DIRTY_MEM_BITS -PAGE_SHIFT_4K ), - guest_code); + vm =vm_create_with_vcpus (mode,1 , +vm_calc_num_guest_pages (mode,DIRTY_MEM_SIZE *2 ), +0 , guest_code, (uint32_t []){VCPU_ID }); + +log_mode_create_vm_done (vm); guest_page_size =vm_get_page_size (vm); + host_page_size =getpagesize (); +/* * A little more than 1G of guest page sized pages. Cover the * case where the size is not aligned to 64 pages. */ - guest_num_pages = (1ul << (DIRTY_MEM_BITS - -vm_get_page_shift (vm))) +3 ; - guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages); - - host_page_size =getpagesize (); + guest_num_pages =vm_adjust_num_guest_pages (mode, + (1ul << (DIRTY_MEM_BITS -vm_get_page_shift (vm))) +3 ); host_num_pages =vm_num_host_pages (mode, guest_num_pages);if (!p->phys_offset) { - 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 ); }else { guest_test_phys_mem = p->phys_offset; } -#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); bmap =bitmap_alloc (host_num_pages); --2.26.2
Re: [PATCH v2 06/11] KVM: selftests: dirty_log_test: Remove create_vm
In reply to Andrew Jonestools/testing/selftests/kvm/dirty_log_test.c line 22
Quoted 2 lines · Andrew Jones
tools/testing/selftests/kvm/dirty_log_test.c +2
-0+#define DIRTY_MEM_BITS 30/* 1G */ +#define DIRTY_MEM_SIZE (1UL << 30)
Nit: could do s/30/DIRTY_MEM_BITS/
Signature · 1 line
Peter Xu
Paolo Bonzini
maintainerRe: [PATCH v2 06/11] KVM: selftests: dirty_log_test: Remove create_vm
In reply to Andrew JonesQuoted 107 lines · Andrew Jones
Use
vm_create_with_vcpusinstead ofcreate_vmand do some minor cleanups around it.Signed-off-by Andrew Jones <[email protected]>--- tools/testing/selftests/kvm/dirty_log_test.c | 56 ++++++-------------- 1 file changed, 16 insertions(+), 40 deletions(-)tools/testing/selftests/kvm/dirty_log_test.c +16
-38@@ -5,8 +5,6 @@* Copyright (C) 2018, Red Hat, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <stdio.h> #include <stdlib.h> #include <pthread.h> @@ -20,6 +18,9 @@#define VCPU_ID 1 +#define DIRTY_MEM_BITS 30/* 1G */ +#define DIRTY_MEM_SIZE (1UL << 30) +/* The memory slot index to track dirty pages */ #define TEST_MEM_SLOT_INDEX 1@@ -353,27 +354,6 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) } } -static struct kvm_vm *create_vm (enum vm_guest_mode mode,uint32_t vcpuid, -uint64_t extra_mem_pages,void *guest_code) -{ -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__ -vm_create_irqchip (vm); -#endif -log_mode_create_vm_done (vm); -vm_vcpu_add_default (vm, vcpuid, guest_code); -return vm; -} - -#define DIRTY_MEM_BITS 30/* 1G */ -#define PAGE_SHIFT_4K 12 -struct test_params {unsigned long iterations;unsigned long interval;@@ -393,43 +373,39 @@ static void run_test(enum vm_guest_mode mode, void *arg)return ; } +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); +/* * We reserve page table for 2 times of extra dirty mem which -* will definitely cover the original (1G+) test range. Here -* we do the calculation with 4K page size which is the -* smallest so the page number will be enough for all archs -* (e.g., 64K page size guest will need even less memory for -* page tables). +* will definitely cover the original (1G+) test range. */ - vm =create_vm (mode,VCPU_ID , -2ul << (DIRTY_MEM_BITS -PAGE_SHIFT_4K ), - guest_code); + vm =vm_create_with_vcpus (mode,1 , +vm_calc_num_guest_pages (mode,DIRTY_MEM_SIZE *2 ), +0 , guest_code, (uint32_t []){VCPU_ID }); + +log_mode_create_vm_done (vm); guest_page_size =vm_get_page_size (vm); + host_page_size =getpagesize (); +/* * A little more than 1G of guest page sized pages. Cover the * case where the size is not aligned to 64 pages. */ - guest_num_pages = (1ul << (DIRTY_MEM_BITS - -vm_get_page_shift (vm))) +3 ; - guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages); - - host_page_size =getpagesize (); + guest_num_pages =vm_adjust_num_guest_pages (mode, + (1ul << (DIRTY_MEM_BITS -vm_get_page_shift (vm))) +3 ); host_num_pages =vm_num_host_pages (mode, guest_num_pages);if (!p->phys_offset) { - 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 ); }else { guest_test_phys_mem = p->phys_offset;
tools/testing/selftests/kvm/dirty_log_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 411
Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_test.c +0
-2} -#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); bmap =bitmap_alloc (host_num_pages);
This one (even after fixing conflicts) breaks the dirty ring test.
Paolo
Re: [PATCH v2 06/11] KVM: selftests: dirty_log_test: Remove create_vm
Quoted 4 lines · Paolo Bonzini
Quoted 1 line
This one (even after fixing conflicts) breaks the dirty ring test.
Maybe the problem was patch 3/11 was missing? For me after rebasing 3/11 this patch applied cleaning and worked. The only change I made was to address Peter's nit.
Thanks,
drew
Paolo Bonzini
maintainerRe: [PATCH v2 06/11] KVM: selftests: dirty_log_test: Remove create_vm
Quoted 5 lines · Andrew Jones
Quoted 2 lines
This one (even after fixing conflicts) breaks the dirty ring test.
Maybe the problem was patch 3/11 was missing? For me after rebasing 3/11 this patch applied cleaning and worked. The only change I made was to address Peter's nit.
Yes, the conflicts in patch 3 were a bit too large so I dropped it. It wasn't clear from the commit message that it was required later on.
Paolo
PATCH v2 7/11 KVM: selftests: Use vm_create_with_vcpus in create_vm
In reply to Andrew Jones--- .../selftests/kvm/demand_paging_test.c | 2 +- .../selftests/kvm/dirty_log_perf_test.c | 2 - .../testing/selftests/kvm/include/kvm_util.h | 8 ++++ .../selftests/kvm/include/perf_test_util.h | 47 +++++-------------- tools/testing/selftests/kvm/lib/kvm_util.c | 9 +--- 5 files changed, 21 insertions(+), 47 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +1 -1
@@ -7,7 +7,7 @@* Copyright (C) 2019, Google, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name and pipe2 */ +#define _GNU_SOURCE/* for pipe2 */ #include <stdio.h> #include <stdlib.h>
tools/testing/selftests/kvm/dirty_log_perf_test.c +0 -2
@@ -8,8 +8,6 @@* Copyright (C) 2020, Google, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <stdio.h> #include <stdlib.h> #include <time.h>
tools/testing/selftests/kvm/include/kvm_util.h +8 -0
@@ -70,6 +70,14 @@ 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/include/perf_test_util.h +11 -36
87 lines not on this page · raw message
tools/testing/selftests/kvm/lib/kvm_util.c +1 -8
@@ -143,14 +143,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
Re: [PATCH v2 07/11] KVM: selftests: Use vm_create_with_vcpus in create_vm
Quoted 2 lines · Andrew Jones
Signed-off-by Andrew Jones <[email protected]>
Quoted 171 lines
--- .../selftests/kvm/demand_paging_test.c | 2 +- .../selftests/kvm/dirty_log_perf_test.c | 2 - .../testing/selftests/kvm/include/kvm_util.h | 8 ++++ .../selftests/kvm/include/perf_test_util.h | 47 +++++-------------- tools/testing/selftests/kvm/lib/kvm_util.c | 9 +--- 5 files changed, 21 insertions(+), 47 deletions(-)tools/testing/selftests/kvm/demand_paging_test.c +1
-1@@ -7,7 +7,7 @@* Copyright (C) 2019, Google, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name and pipe2 */ +#define _GNU_SOURCE/* for pipe2 */ #include <stdio.h> #include <stdlib.h> tools/testing/selftests/kvm/dirty_log_perf_test.c +0
-2@@ -8,8 +8,6 @@* Copyright (C) 2020, Google, Inc. */ -#define _GNU_SOURCE/* for program_invocation_name */ -#include <stdio.h> #include <stdlib.h> #include <time.h> tools/testing/selftests/kvm/include/kvm_util.h +8
-0@@ -70,6 +70,14 @@ 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/include/perf_test_util.h +11
-36@@ -13,9 +13,6 @@#define MAX_VCPUS 512 -#define PAGE_SHIFT_4K 12 -#define PTES_PER_4K_PT 512 -#define TEST_MEM_SLOT_INDEX 1/* Default guest test virtual memory offset */ @@ -94,41 +91,26 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus, uint64_t vcpu_memory_bytes) {struct kvm_vm *vm; -uint64_t pages =DEFAULT_GUEST_PHY_PAGES ;uint64_t guest_num_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 * vcpu_memory_bytes) >>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 - - perf_test_args.vm = vm; - perf_test_args.guest_page_size =vm_get_page_size (vm); perf_test_args.host_page_size =getpagesize (); + perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size; + + guest_num_pages =vm_adjust_num_guest_pages (mode, + (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size); +TEST_ASSERT (vcpu_memory_bytes % perf_test_args.host_page_size ==0 , +"Guest memory size is not host page size aligned." );TEST_ASSERT (vcpu_memory_bytes % perf_test_args.guest_page_size ==0 ,"Guest memory size is not guest page size aligned." ); - guest_num_pages = (vcpus * vcpu_memory_bytes) / - perf_test_args.guest_page_size; - guest_num_pages =vm_adjust_num_guest_pages (mode, guest_num_pages); + vm =vm_create_with_vcpus (mode, vcpus, + (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size, +0 , guest_code,NULL ); + + perf_test_args.vm = vm;/* * If there should be more memory in the guest test region than there @@ -140,18 +122,13 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus, guest_num_pages,vm_get_max_gfn (vm), vcpus, vcpu_memory_bytes); -TEST_ASSERT (vcpu_memory_bytes % perf_test_args.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) * perf_test_args.guest_page_size; guest_test_phys_mem &= ~(perf_test_args.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 */ @@ -177,8 +154,6 @@ static void add_vcpus(struct kvm_vm *vm, int vcpus, uint64_t vcpu_memory_bytes)for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { vcpu_args = &perf_test_args.vcpu_args[vcpu_id]; -vm_vcpu_add_default (vm, vcpu_id, guest_code); -#ifdef __x86_64__vcpu_set_cpuid (vm, vcpu_id,kvm_get_supported_cpuid ());#endif tools/testing/selftests/kvm/lib/kvm_util.c +1
-9@@ -143,14 +143,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 }, --2.26.2
PATCH v2 8/11 KVM: selftests: Implement perf_test_util more conventionally
In reply to Andrew JonesIt's not conventional C to put non-inline functions in header files. Create a source file for the functions instead. Also reduce the amount of globals and rename the functions to something less generic.
--- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/demand_paging_test.c | 11 +- .../selftests/kvm/dirty_log_perf_test.c | 20 +-- .../testing/selftests/kvm/include/kvm_util.h | 1 + .../selftests/kvm/include/perf_test_util.h | 146 ++---------------- .../selftests/kvm/lib/perf_test_util.c | 138 +++++++++++++++++ 6 files changed, 169 insertions(+), 149 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/perf_test_util.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 lib/guest_modes.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 lib/perf_test_util.c LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S 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 +6 -5
@@ -36,12 +36,14 @@#define PER_VCPU_DEBUG (...) _no_printf(__VA_ARGS__)#endif +static int nr_vcpus =1 ; +static uint64_t guest_percpu_mem_size =DEFAULT_PER_VCPU_MEM_SIZE ;static char *guest_data_prototype;static void *vcpu_worker (void *data) {int ret; -struct vcpu_args *vcpu_args = (struct vcpu_args *)data; +struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;int vcpu_id = vcpu_args->vcpu_id;struct kvm_vm *vm = perf_test_args.vm;struct kvm_run *run;
@@ -263,7 +265,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)int vcpu_id;int r; - vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); + vm =perf_test_create_vm (mode, nr_vcpus, guest_percpu_mem_size); perf_test_args.wr_fract =1 ;
@@ -275,7 +277,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" ); -add_vcpus (vm, nr_vcpus, guest_percpu_mem_size); +perf_test_add_vcpus (vm, nr_vcpus, guest_percpu_mem_size);if (p->use_uffd) { uffd_handler_threads =
@@ -359,8 +361,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) perf_test_args.vcpu_args[0 ].pages * nr_vcpus / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0)); -ucall_uninit (vm); -kvm_vm_free (vm); +perf_test_destroy_vm (vm);free (guest_data_prototype);free (vcpu_threads);
tools/testing/selftests/kvm/dirty_log_perf_test.c +11 -9
@@ -22,10 +22,13 @@/* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ #define TEST_HOST_LOOP_N 2UL +static int nr_vcpus =1 ; +static uint64_t guest_percpu_mem_size =DEFAULT_PER_VCPU_MEM_SIZE ; +/* Host variables */ static bool host_quit;static uint64_t iteration; -static uint64_t vcpu_last_completed_iteration[MAX_VCPUS ]; +static uint64_t vcpu_last_completed_iteration[KVM_MAX_VCPUS ];static void *vcpu_worker (void *data) {
@@ -37,7 +40,7 @@ static void *vcpu_worker(void *data)struct timespec ts_diff;struct timespec total = (struct timespec ){0 };struct timespec avg; -struct vcpu_args *vcpu_args = (struct vcpu_args *)data; +struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;int vcpu_id = vcpu_args->vcpu_id;vcpu_args_set (vm, vcpu_id,1 , vcpu_id);
@@ -105,7 +108,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)struct timespec vcpu_dirty_total = (struct timespec ){0 };struct timespec avg; - vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); + vm =perf_test_create_vm (mode, nr_vcpus, guest_percpu_mem_size); perf_test_args.wr_fract = p->wr_fract;
@@ -117,7 +120,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" ); -add_vcpus (vm, nr_vcpus, guest_percpu_mem_size); +perf_test_add_vcpus (vm, nr_vcpus, guest_percpu_mem_size);sync_global_to_guest (vm, perf_test_args);
@@ -143,7 +146,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)/* Enable dirty logging */ clock_gettime (CLOCK_MONOTONIC , &start); -vm_mem_region_set_flags (vm,TEST_MEM_SLOT_INDEX , +vm_mem_region_set_flags (vm,PERF_TEST_MEM_SLOT_INDEX ,KVM_MEM_LOG_DIRTY_PAGES ); ts_diff =timespec_diff_now (start); pr_info("Enabling dirty logging time: %ld.%.9lds\n\n",
@@ -170,7 +173,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) iteration, ts_diff.tv_sec, ts_diff.tv_nsec);clock_gettime (CLOCK_MONOTONIC , &start); -kvm_vm_get_dirty_log (vm,TEST_MEM_SLOT_INDEX , bmap); +kvm_vm_get_dirty_log (vm,PERF_TEST_MEM_SLOT_INDEX , bmap); ts_diff =timespec_diff_now (start); get_dirty_log_total = timespec_add(get_dirty_log_total,
@@ -186,7 +189,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)/* Disable dirty logging */ clock_gettime (CLOCK_MONOTONIC , &start); -vm_mem_region_set_flags (vm,TEST_MEM_SLOT_INDEX ,0 ); +vm_mem_region_set_flags (vm,PERF_TEST_MEM_SLOT_INDEX ,0 ); ts_diff =timespec_diff_now (start);pr_info ("Disabling dirty logging time: %ld.%.9lds\n" , ts_diff.tv_sec, ts_diff.tv_nsec);
@@ -198,8 +201,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)free (bmap);free (vcpu_threads); -ucall_uninit (vm); -kvm_vm_free (vm); +perf_test_destroy_vm (vm); }static void help (char *name)
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
@@ -16,6 +16,7 @@#include "sparsebit.h" +#define KVM_MAX_VCPUS 512/* * Callers of kvm_util only have an incomplete/opaque description of the
tools/testing/selftests/kvm/include/perf_test_util.h +12 -134
170 lines not on this page · raw message
tools/testing/selftests/kvm/lib/perf_test_util.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v2 08/11] KVM: selftests: Implement perf_test_util more conventionally
Quoted 7 lines · Andrew Jones
It's not conventional C to put non-inline functions in header files. Create a source file for the functions instead. Also reduce the amount of globals and rename the functions to something less generic.
Signed-off-by Andrew Jones <[email protected]>
Quoted 453 lines
--- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/demand_paging_test.c | 11 +- .../selftests/kvm/dirty_log_perf_test.c | 20 +-- .../testing/selftests/kvm/include/kvm_util.h | 1 + .../selftests/kvm/include/perf_test_util.h | 146 ++---------------- .../selftests/kvm/lib/perf_test_util.c | 138 +++++++++++++++++ 6 files changed, 169 insertions(+), 149 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/perf_test_util.ctools/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 lib/guest_modes.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 lib/perf_test_util.c LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.ctools/testing/selftests/kvm/demand_paging_test.c +6
-5@@ -36,12 +36,14 @@#define PER_VCPU_DEBUG (...) _no_printf(__VA_ARGS__)#endif +static int nr_vcpus =1 ; +static uint64_t guest_percpu_mem_size =DEFAULT_PER_VCPU_MEM_SIZE ;static char *guest_data_prototype;static void *vcpu_worker (void *data) {int ret; -struct vcpu_args *vcpu_args = (struct vcpu_args *)data; +struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;int vcpu_id = vcpu_args->vcpu_id;struct kvm_vm *vm = perf_test_args.vm;struct kvm_run *run;@@ -263,7 +265,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)int vcpu_id;int r; - vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); + vm =perf_test_create_vm (mode, nr_vcpus, guest_percpu_mem_size); perf_test_args.wr_fract =1 ;@@ -275,7 +277,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" ); -add_vcpus (vm, nr_vcpus, guest_percpu_mem_size); +perf_test_add_vcpus (vm, nr_vcpus, guest_percpu_mem_size);if (p->use_uffd) { uffd_handler_threads =@@ -359,8 +361,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) perf_test_args.vcpu_args[0 ].pages * nr_vcpus / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0)); -ucall_uninit (vm); -kvm_vm_free (vm); +perf_test_destroy_vm (vm);free (guest_data_prototype);free (vcpu_threads);tools/testing/selftests/kvm/dirty_log_perf_test.c +11
-9@@ -22,10 +22,13 @@/* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ #define TEST_HOST_LOOP_N 2UL +static int nr_vcpus =1 ; +static uint64_t guest_percpu_mem_size =DEFAULT_PER_VCPU_MEM_SIZE ; +/* Host variables */ static bool host_quit;static uint64_t iteration; -static uint64_t vcpu_last_completed_iteration[MAX_VCPUS ]; +static uint64_t vcpu_last_completed_iteration[KVM_MAX_VCPUS ];static void *vcpu_worker (void *data) {@@ -37,7 +40,7 @@ static void *vcpu_worker(void *data)struct timespec ts_diff;struct timespec total = (struct timespec ){0 };struct timespec avg; -struct vcpu_args *vcpu_args = (struct vcpu_args *)data; +struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;int vcpu_id = vcpu_args->vcpu_id;vcpu_args_set (vm, vcpu_id,1 , vcpu_id);@@ -105,7 +108,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)struct timespec vcpu_dirty_total = (struct timespec ){0 };struct timespec avg; - vm =create_vm (mode, nr_vcpus, guest_percpu_mem_size); + vm =perf_test_create_vm (mode, nr_vcpus, guest_percpu_mem_size); perf_test_args.wr_fract = p->wr_fract;@@ -117,7 +120,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vcpu_threads =malloc (nr_vcpus *sizeof (*vcpu_threads));TEST_ASSERT (vcpu_threads,"Memory allocation failed" ); -add_vcpus (vm, nr_vcpus, guest_percpu_mem_size); +perf_test_add_vcpus (vm, nr_vcpus, guest_percpu_mem_size);sync_global_to_guest (vm, perf_test_args);@@ -143,7 +146,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)/* Enable dirty logging */ clock_gettime (CLOCK_MONOTONIC , &start); -vm_mem_region_set_flags (vm,TEST_MEM_SLOT_INDEX , +vm_mem_region_set_flags (vm,PERF_TEST_MEM_SLOT_INDEX ,KVM_MEM_LOG_DIRTY_PAGES ); ts_diff =timespec_diff_now (start); pr_info("Enabling dirty logging time: %ld.%.9lds\n\n",@@ -170,7 +173,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) iteration, ts_diff.tv_sec, ts_diff.tv_nsec);clock_gettime (CLOCK_MONOTONIC , &start); -kvm_vm_get_dirty_log (vm,TEST_MEM_SLOT_INDEX , bmap); +kvm_vm_get_dirty_log (vm,PERF_TEST_MEM_SLOT_INDEX , bmap); ts_diff =timespec_diff_now (start); get_dirty_log_total = timespec_add(get_dirty_log_total,@@ -186,7 +189,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)/* Disable dirty logging */ clock_gettime (CLOCK_MONOTONIC , &start); -vm_mem_region_set_flags (vm,TEST_MEM_SLOT_INDEX ,0 ); +vm_mem_region_set_flags (vm,PERF_TEST_MEM_SLOT_INDEX ,0 ); ts_diff =timespec_diff_now (start);pr_info ("Disabling dirty logging time: %ld.%.9lds\n" , ts_diff.tv_sec, ts_diff.tv_nsec);@@ -198,8 +201,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)free (bmap);free (vcpu_threads); -ucall_uninit (vm); -kvm_vm_free (vm); +perf_test_destroy_vm (vm); }static void help (char *name)tools/testing/selftests/kvm/include/kvm_util.h +1
-0@@ -16,6 +16,7 @@#include "sparsebit.h" +#define KVM_MAX_VCPUS 512/* * Callers of kvm_util only have an incomplete/opaque description of the tools/testing/selftests/kvm/include/perf_test_util.h +12
-134@@ -9,35 +9,15 @@#define SELFTEST_KVM_PERF_TEST_UTIL_H #include "kvm_util.h" -#include "processor.h" - -#define MAX_VCPUS 512 - -#define TEST_MEM_SLOT_INDEX 1/* Default guest test virtual memory offset */ #define DEFAULT_GUEST_TEST_MEM 0xc0000000#define DEFAULT_PER_VCPU_MEM_SIZE (1 << 30)/* 1G */ -/* -* Guest physical memory offset of the testing memory slot. -* This will be set to the topmost valid physical address minus -* the test memory size. -*/ -static uint64_t guest_test_phys_mem; - -/* -* Guest virtual memory offset of the testing memory slot. -* 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 ; - -/* Number of VCPUs for the test */ -static int nr_vcpus =1 ; +#define PERF_TEST_MEM_SLOT_INDEX 1 -struct vcpu_args { +struct perf_test_vcpu_args {uint64_t gva;uint64_t pages;@@ -51,123 +31,21 @@ struct perf_test_args {uint64_t guest_page_size;int wr_fract; -struct vcpu_args vcpu_args[MAX_VCPUS ]; + struct perf_test_vcpu_args vcpu_args[KVM_MAX_VCPUS]; }; -static struct perf_test_args perf_test_args; +extern struct perf_test_args perf_test_args;/* -* Continuously write to the first 8 bytes of each page in the -* specified region. +* Guest physical memory offset of the testing memory slot. +* This will be set to the topmost valid physical address minus +* the test memory size. */ -static void guest_code (uint32_t vcpu_id) -{ -struct vcpu_args *vcpu_args = &perf_test_args.vcpu_args[vcpu_id]; -uint64_t gva; -uint64_t pages; -int i; - -/* Make sure vCPU args data structure is not corrupt. */ -GUEST_ASSERT (vcpu_args->vcpu_id == vcpu_id); - - gva = vcpu_args->gva; - pages = vcpu_args->pages; - -while (true) { -for (i =0 ; i < pages; i++) { -uint64_t addr = gva + (i * perf_test_args.guest_page_size); - -if (i % perf_test_args.wr_fract ==0 ) - *(uint64_t *)addr =0x0123456789ABCDEF ; -else -READ_ONCE (*(uint64_t *)addr); - } - -GUEST_SYNC (1 ); - } -} - -static struct kvm_vm *create_vm (enum vm_guest_mode mode,int vcpus, -uint64_t vcpu_memory_bytes) -{ -struct kvm_vm *vm; -uint64_t guest_num_pages; - -pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); - - perf_test_args.host_page_size =getpagesize (); - perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size; - - guest_num_pages =vm_adjust_num_guest_pages (mode, - (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size); - -TEST_ASSERT (vcpu_memory_bytes % perf_test_args.host_page_size ==0 , -"Guest memory size is not host page size aligned." ); -TEST_ASSERT (vcpu_memory_bytes % perf_test_args.guest_page_size ==0 , -"Guest memory size is not guest page size aligned." ); - - vm =vm_create_with_vcpus (mode, vcpus, - (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size, -0 , guest_code,NULL ); - - perf_test_args.vm = vm; - -/* -* 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), vcpus, - vcpu_memory_bytes); - - guest_test_phys_mem = (vm_get_max_gfn (vm) - guest_num_pages) * - perf_test_args.guest_page_size; - guest_test_phys_mem &= ~(perf_test_args.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 */ -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , - guest_test_phys_mem, -TEST_MEM_SLOT_INDEX , - guest_num_pages,0 ); - -/* Do mapping for the demand paging memory slot */ -virt_map (vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages,0 ); - -ucall_init (vm,NULL ); - -return vm; -} - -static void add_vcpus (struct kvm_vm *vm,int vcpus,uint64_t vcpu_memory_bytes) -{ -vm_paddr_t vcpu_gpa; -struct vcpu_args *vcpu_args; -int vcpu_id; - -for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { - vcpu_args = &perf_test_args.vcpu_args[vcpu_id]; - -#ifdef __x86_64__ -vcpu_set_cpuid (vm, vcpu_id,kvm_get_supported_cpuid ()); -#endif - - vcpu_args->vcpu_id = vcpu_id; - vcpu_args->gva = guest_test_virt_mem + - (vcpu_id * vcpu_memory_bytes); - vcpu_args->pages = vcpu_memory_bytes / - perf_test_args.guest_page_size; +extern uint64_t guest_test_phys_mem; - vcpu_gpa = guest_test_phys_mem + (vcpu_id * vcpu_memory_bytes); -pr_debug ("Added VCPU %d with test mem gpa [%lx, %lx)\n" , - vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_memory_bytes); - } -} +struct kvm_vm *perf_test_create_vm (enum vm_guest_mode mode,int vcpus, +uint64_t vcpu_memory_bytes); +void perf_test_destroy_vm (struct kvm_vm *vm); +void perf_test_add_vcpus (struct kvm_vm *vm,int vcpus,uint64_t vcpu_memory_bytes);#endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */ tools/testing/selftests/kvm/lib/perf_test_util.c new file +105
-0@@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020, Google LLC. +*/ + +#include "kvm_util.h" +#include "perf_test_util.h" +#include "processor.h" + +struct perf_test_args perf_test_args; + +uint64_t guest_test_phys_mem; + +/* +* Guest virtual memory offset of the testing memory slot. +* Must not conflict with identity mapped test code. +*/ +static uint64_t guest_test_virt_mem =DEFAULT_GUEST_TEST_MEM ; + +/* +* Continuously write to the first 8 bytes of each page in the +* specified region. +*/ +static void guest_code (uint32_t vcpu_id) +{ +struct perf_test_vcpu_args *vcpu_args = &perf_test_args.vcpu_args[vcpu_id]; +uint64_t gva; +uint64_t pages; +int i; + +/* Make sure vCPU args data structure is not corrupt. */ +GUEST_ASSERT (vcpu_args->vcpu_id == vcpu_id); + + gva = vcpu_args->gva; + pages = vcpu_args->pages; + +while (true) { +for (i =0 ; i < pages; i++) { +uint64_t addr = gva + (i * perf_test_args.guest_page_size); + +if (i % perf_test_args.wr_fract ==0 ) + *(uint64_t *)addr =0x0123456789ABCDEF ; +else +READ_ONCE (*(uint64_t *)addr); + } + +GUEST_SYNC (1 ); + } +} + +struct kvm_vm *perf_test_create_vm (enum vm_guest_mode mode,int vcpus, +uint64_t vcpu_memory_bytes) +{ +struct kvm_vm *vm; +uint64_t guest_num_pages; + +pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); + + perf_test_args.host_page_size =getpagesize (); + perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size; + + guest_num_pages =vm_adjust_num_guest_pages (mode, + (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size); + +TEST_ASSERT (vcpu_memory_bytes % perf_test_args.host_page_size ==0 , +"Guest memory size is not host page size aligned." ); +TEST_ASSERT (vcpu_memory_bytes % perf_test_args.guest_page_size ==0 , +"Guest memory size is not guest page size aligned." ); + + vm =vm_create_with_vcpus (mode, vcpus, + (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size, +0 , guest_code,NULL ); + + perf_test_args.vm = vm; + +/* +* 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), vcpus, + vcpu_memory_bytes); + + guest_test_phys_mem = (vm_get_max_gfn (vm) - guest_num_pages) * + perf_test_args.guest_page_size; + guest_test_phys_mem &= ~(perf_test_args.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 */ +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , + guest_test_phys_mem, +PERF_TEST_MEM_SLOT_INDEX , + guest_num_pages,0 ); + +/* Do mapping for the demand paging memory slot */ +virt_map (vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages,0 ); + +ucall_init (vm,NULL ); +
tools/testing/selftests/kvm/lib/perf_test_util.c line 115
Quoted 10 lines
tools/testing/selftests/kvm/lib/perf_test_util.c new file +10
-0+return vm; +} + +void perf_test_destroy_vm (struct kvm_vm *vm) +{ +ucall_uninit (vm); +kvm_vm_free (vm); +} + +void perf_test_add_vcpus (struct kvm_vm *vm,int vcpus,uint64_t vcpu_memory_bytes)
NIT: Since we're actually creating the vcpus in vm_create_with_vcpus, and renaming functions anyway, it might make sense to change this to perf_test_setup_vcpus or similar here.
This could also be called from perf_test_create_vm and made static, but that might be outside the scope of this commit. Either way works.
Quoted 26 lines
tools/testing/selftests/kvm/lib/perf_test_util.c new file +23
-1+{ +vm_paddr_t vcpu_gpa; +struct perf_test_vcpu_args *vcpu_args; +int vcpu_id; + +for (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { + vcpu_args = &perf_test_args.vcpu_args[vcpu_id]; + +#ifdef __x86_64__ +vcpu_set_cpuid (vm, vcpu_id,kvm_get_supported_cpuid ()); +#endif + + vcpu_args->vcpu_id = vcpu_id; + vcpu_args->gva = guest_test_virt_mem + + (vcpu_id * vcpu_memory_bytes); + vcpu_args->pages = vcpu_memory_bytes / + perf_test_args.guest_page_size; + + vcpu_gpa = guest_test_phys_mem + (vcpu_id * vcpu_memory_bytes); +pr_debug ("Added VCPU %d with test mem gpa [%lx, %lx)\n" , + vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_memory_bytes); + } +} --2.26.2
Re: [PATCH v2 08/11] KVM: selftests: Implement perf_test_util more conventionally
Quoted 10 lines · Ben Gardon
Quoted 7 lines · Andrew Jones
It's not conventional C to put non-inline functions in header files. Create a source file for the functions instead. Also reduce the amount of globals and rename the functions to something less generic.
Signed-off-by Andrew Jones <[email protected]>Reviewed-by Ben Gardon <[email protected]>
Thanks!
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +void perf_test_add_vcpus(struct kvm_vm *vm, int vcpus, uint64_t vcpu_memory_bytes)NIT: Since we're actually creating the vcpus in
vm_create_with_vcpus, and renaming functions anyway, it might make sense to change this toperf_test_setup_vcpusor similar here.
This could also be called fromperf_test_create_vmand made static, but that might be outside the scope of this commit. Either way works.
Yup, I agree there's still room for some more cleanup / renaming. I'll be happy to review the patches ;-)
Thanks,
drew
Re: [PATCH v2 08/11] KVM: selftests: Implement perf_test_util more conventionally
In reply to Andrew JonesQuoted 6 lines · Andrew Jones
It's not conventional C to put non-inline functions in header files. Create a source file for the functions instead. Also reduce the amount of globals and rename the functions to something less generic.
Signed-off-by Andrew Jones <[email protected]>
Signature · 1 line
Peter Xu
PATCH v2 9/11 KVM: selftests: Also build dirty_log_perf_test on AArch64
In reply to Andrew Jones--- tools/testing/selftests/kvm/Makefile | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/Makefile +1 -0
@@ -70,6 +70,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list-sve TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test +TEST_GEN_PROGS_aarch64 += dirty_log_perf_test TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus TEST_GEN_PROGS_aarch64 += set_memory_region_test TEST_GEN_PROGS_aarch64 += steal_time
Signature · 1 line
2.26.2
Re: [PATCH v2 09/11] KVM: selftests: Also build dirty_log_perf_test on AArch64
Quoted 1 line · Andrew Jones
Signed-off-by Andrew Jones <[email protected]>
Signature · 1 line
Peter Xu
PATCH v2 10/11 KVM: selftests: x86: Set supported CPUIDs on default VM
In reply to Andrew JonesAlmost all tests do this anyway and the ones that don't don't appear to care. Only vmx_set_nested_state_test assumes that a feature (VMX) is disabled until later setting the supported CPUIDs. It's better to disable that explicitly anyway.
--- tools/testing/selftests/kvm/dirty_log_test.c | 3 --- tools/testing/selftests/kvm/lib/kvm_util.c | 11 ++++++++-- .../selftests/kvm/lib/perf_test_util.c | 4 ---- .../selftests/kvm/set_memory_region_test.c | 2 -- .../kvm/x86_64/cr4_cpuid_sync_test.c | 1 - .../testing/selftests/kvm/x86_64/debug_regs.c | 1 - .../testing/selftests/kvm/x86_64/evmcs_test.c | 2 -- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 -- .../testing/selftests/kvm/x86_64/state_test.c | 1 - .../selftests/kvm/x86_64/svm_vmcall_test.c | 1 - .../selftests/kvm/x86_64/tsc_msrs_test.c | 1 - .../selftests/kvm/x86_64/user_msr_test.c | 1 - .../kvm/x86_64/vmx_apic_access_test.c | 1 - .../kvm/x86_64/vmx_close_while_nested_test.c | 1 - .../selftests/kvm/x86_64/vmx_dirty_log_test.c | 1 - .../kvm/x86_64/vmx_preemption_timer_test.c | 1 - .../kvm/x86_64/vmx_set_nested_state_test.c | 21 +++++++++++++++++++ .../kvm/x86_64/vmx_tsc_adjust_test.c | 1 - 18 files changed, 30 insertions(+), 26 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +0 -3
@@ -424,9 +424,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)/* Cache the HVA pointer of the region */ host_test_mem =addr_gpa2hva (vm, (vm_paddr_t )guest_test_phys_mem); -#ifdef __x86_64__ -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); -#endif ucall_init (vm,NULL );/* Export the shared variables to the guest */
tools/testing/selftests/kvm/lib/kvm_util.c +9 -2
@@ -294,8 +294,15 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,vm_create_irqchip (vm);#endif -for (i =0 ; i < nr_vcpus; ++i) -vm_vcpu_add_default (vm, vcpuids ? vcpuids[i] : i, guest_code); +for (i =0 ; i < nr_vcpus; ++i) { +uint32_t vcpuid = vcpuids ? vcpuids[i] : i; + +vm_vcpu_add_default (vm, vcpuid, guest_code); + +#ifdef __x86_64__ +vcpu_set_cpuid (vm, vcpuid,kvm_get_supported_cpuid ()); +#endif + }return vm; }
tools/testing/selftests/kvm/lib/perf_test_util.c +0 -4
@@ -121,10 +121,6 @@ void perf_test_add_vcpus(struct kvm_vm *vm, int vcpus, uint64_t vcpu_memory_bytefor (vcpu_id =0 ; vcpu_id < vcpus; vcpu_id++) { vcpu_args = &perf_test_args.vcpu_args[vcpu_id]; -#ifdef __x86_64__ -vcpu_set_cpuid (vm, vcpu_id,kvm_get_supported_cpuid ()); -#endif - vcpu_args->vcpu_id = vcpu_id; vcpu_args->gva = guest_test_virt_mem + (vcpu_id * vcpu_memory_bytes);
tools/testing/selftests/kvm/set_memory_region_test.c +0 -2
@@ -121,8 +121,6 @@ static struct kvm_vm *spawn_vm(pthread_t *vcpu_thread, void *guest_code) vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS_THP ,MEM_REGION_GPA ,MEM_REGION_SLOT ,MEM_REGION_SIZE /getpagesize (),0 );
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +0 -1
@@ -81,7 +81,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); run =vcpu_state (vm,VCPU_ID );while (1 ) {
tools/testing/selftests/kvm/x86_64/debug_regs.c +0 -1
@@ -85,7 +85,6 @@ int main(void) } vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); run =vcpu_state (vm,VCPU_ID );/* Test software BPs - int3 */
tools/testing/selftests/kvm/x86_64/evmcs_test.c +0 -2
@@ -92,8 +92,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); -if (!nested_vmx_supported () || !kvm_check_cap (KVM_CAP_NESTED_STATE ) || !kvm_check_cap (KVM_CAP_HYPERV_ENLIGHTENED_VMCS )) {
tools/testing/selftests/kvm/x86_64/smm_test.c +0 -2
@@ -102,8 +102,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); - run =vcpu_state (vm,VCPU_ID ); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
tools/testing/selftests/kvm/x86_64/state_test.c +0 -1
@@ -165,7 +165,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); run =vcpu_state (vm,VCPU_ID );vcpu_regs_get (vm,VCPU_ID , ®s1);
tools/testing/selftests/kvm/x86_64/svm_vmcall_test.c +0 -1
@@ -44,7 +44,6 @@ int main(int argc, char *argv[])nested_svm_check_supported (); vm =vm_create_default (VCPU_ID ,0 , (void *) l1_guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ());vcpu_alloc_svm (vm, &svm_gva);vcpu_args_set (vm,VCPU_ID ,1 , svm_gva);
tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c +0 -1
@@ -107,7 +107,6 @@ int main(void)uint64_t val; vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); val =0 ;ASSERT_EQ (rounded_host_rdmsr (MSR_IA32_TSC ), val);
tools/testing/selftests/kvm/x86_64/user_msr_test.c +0 -1
@@ -205,7 +205,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); run =vcpu_state (vm,VCPU_ID ); rc =kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR );
tools/testing/selftests/kvm/x86_64/vmx_apic_access_test.c +0 -1
@@ -87,7 +87,6 @@ int main(int argc, char *argv[])nested_vmx_check_supported (); vm =vm_create_default (VCPU_ID ,0 , (void *) l1_guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ());kvm_get_cpu_address_width (&paddr_width, &vaddr_width); high_gpa = (1ul << paddr_width) -getpagesize ();
tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c +0 -1
@@ -57,7 +57,6 @@ int main(int argc, char *argv[])nested_vmx_check_supported (); vm =vm_create_default (VCPU_ID ,0 , (void *) l1_guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ());/* Allocate VMX pages and shared descriptors (vmx_pages). */ vcpu_alloc_vmx (vm, &vmx_pages_gva);
tools/testing/selftests/kvm/x86_64/vmx_dirty_log_test.c +0 -1
@@ -82,7 +82,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , l1_guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); vmx =vcpu_alloc_vmx (vm, &vmx_pages_gva);vcpu_args_set (vm,VCPU_ID ,1 , vmx_pages_gva); run =vcpu_state (vm,VCPU_ID );
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c +0 -1
@@ -171,7 +171,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ()); run =vcpu_state (vm,VCPU_ID );vcpu_regs_get (vm,VCPU_ID , ®s1);
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c +21 -0
@@ -244,6 +244,22 @@ void test_vmx_nested_state(struct kvm_vm *vm)free (state); } +void disable_vmx (struct kvm_vm *vm) +{ +struct kvm_cpuid2 *cpuid =kvm_get_supported_cpuid (); +int i; + +for (i =0 ; i < cpuid->nent; ++i) +if (cpuid->entries[i].function ==1 && + cpuid->entries[i].index ==0 ) +break ; +TEST_ASSERT (i != cpuid->nent,"CPUID function 1 not found" ); + + cpuid->entries[i].ecx &= ~CPUID_VMX ; + +vcpu_set_cpuid (vm,VCPU_ID , cpuid); +} +int main (int argc,char *argv[]) {struct kvm_vm *vm;
@@ -264,6 +280,11 @@ int main(int argc, char *argv[]) vm =vm_create_default (VCPU_ID ,0 ,0 ); +/* +* First run tests with VMX disabled to check error handling. +*/ +disable_vmx (vm); +/* Passing a NULL kvm_nested_state causes a EFAULT. */ test_nested_state_expect_efault (vm,NULL );
tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c +0 -1
@@ -132,7 +132,6 @@ int main(int argc, char *argv[])nested_vmx_check_supported (); vm =vm_create_default (VCPU_ID ,0 , (void *) l1_guest_code); -vcpu_set_cpuid (vm,VCPU_ID ,kvm_get_supported_cpuid ());/* Allocate VMX pages and shared descriptors (vmx_pages). */ vcpu_alloc_vmx (vm, &vmx_pages_gva);
Signature · 1 line
2.26.2
Re: [PATCH v2 10/11] KVM: selftests: x86: Set supported CPUIDs on default VM
Quoted 20 lines · Andrew Jones
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c +16
-0+void disable_vmx (struct kvm_vm *vm) +{ +struct kvm_cpuid2 *cpuid =kvm_get_supported_cpuid (); +int i; + +for (i =0 ; i < cpuid->nent; ++i) +if (cpuid->entries[i].function ==1 && + cpuid->entries[i].index ==0 ) +break ; +TEST_ASSERT (i != cpuid->nent,"CPUID function 1 not found" ); + + cpuid->entries[i].ecx &= ~CPUID_VMX ; + +vcpu_set_cpuid (vm,VCPU_ID , cpuid); +} +int main (int argc,char *argv[]) {struct kvm_vm *vm;
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c int main(int argc, char *argv[]) · line 287
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c +5
-0@@ -264,6 +280,11 @@ int main(int argc, char *argv[]) vm =vm_create_default (VCPU_ID ,0 ,0 ); +/* +* First run tests with VMX disabled to check error handling. +*/ +disable_vmx (vm); +
My gut feeling is that we may even drop these complexity, but simply remove the vcpu_set_cpuid() in test_vmx_nested_state(). Since I see that most (or all?) the tests before vcpu_set_cpuid() were a bunch of ioctl testings against KVM_SET_NESTED_STATE - seems not really related to the applied cpuid at all...
Signature · 1 line
Peter Xu
PATCH v2 11/11 KVM: selftests: Make test skipping consistent
In reply to Andrew Jones--- tools/testing/selftests/kvm/x86_64/kvm_pv_test.c | 4 ++-- tools/testing/selftests/kvm/x86_64/user_msr_test.c | 6 +++++- .../kvm/x86_64/vmx_preemption_timer_test.c | 14 +++++++------- 3 files changed, 14 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/x86_64/kvm_pv_test.c +2 -2
@@ -211,8 +211,8 @@ int main(void)struct kvm_vm *vm;if (!kvm_check_cap (KVM_CAP_ENFORCE_PV_FEATURE_CPUID )) { -pr_info ("will skip kvm paravirt restriction tests.\n" ); -return 0 ; +print_skip ("KVM_CAP_ENFORCE_PV_FEATURE_CPUID not supported" ); +exit (KSFT_SKIP ); } vm =vm_create_default (VCPU_ID ,0 , guest_main);
tools/testing/selftests/kvm/x86_64/user_msr_test.c +5 -1
@@ -208,7 +208,11 @@ int main(int argc, char *argv[]) run =vcpu_state (vm,VCPU_ID ); rc =kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR ); -TEST_ASSERT (rc,"KVM_CAP_X86_USER_SPACE_MSR is available" ); +if (!rc) { +print_skip ("KVM_CAP_X86_USER_SPACE_MSR not supported" ); +exit (KSFT_SKIP ); + } +vm_enable_cap (vm, &cap); rc =kvm_check_cap (KVM_CAP_X86_MSR_FILTER );
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c +7 -7
@@ -169,19 +169,19 @@ int main(int argc, char *argv[])*/ nested_vmx_check_supported (); +if (!kvm_check_cap (KVM_CAP_NESTED_STATE )) { +print_skip ("KVM_CAP_NESTED_STATE not supported" ); +exit (KSFT_SKIP ); + } +/* Create VM */ vm =vm_create_default (VCPU_ID ,0 , guest_code); run =vcpu_state (vm,VCPU_ID );vcpu_regs_get (vm,VCPU_ID , ®s1); -if (kvm_check_cap (KVM_CAP_NESTED_STATE )) { -vcpu_alloc_vmx (vm, &vmx_pages_gva); -vcpu_args_set (vm,VCPU_ID ,1 , vmx_pages_gva); - }else { -pr_info ("will skip vmx preemption timer checks\n" ); - goto done; - } +vcpu_alloc_vmx (vm, &vmx_pages_gva); +vcpu_args_set (vm,VCPU_ID ,1 , vmx_pages_gva);for (stage =1 ;; stage++) {_vcpu_run (vm,VCPU_ID );
Signature · 1 line
2.26.2
Re: [PATCH v2 11/11] KVM: selftests: Make test skipping consistent
Quoted 1 line · Andrew Jones
Signed-off-by Andrew Jones <[email protected]>
Signature · 1 line
Peter Xu
Paolo Bonzini
maintainerApplied 4/5/9/10/11 (plus, 1 and 2 have replacements in kvm/queue). I'll push shortly, please rebase and resend.
Paolo
Quoted 80 lines · Andrew Jones
This series attempts to clean up
demand_paging_test,dirty_log_perf_test, anddirty_log_testby factoring out common code, creating some new API along the way. It also splitsinclude/perf_test_util.hinto a more conventional header and source pair.I've tested on x86 and AArch64 (one config each), but not s390x.
v2:
- Useextra_meminstead of per-vcpu mem for the test mem indemand_paging_testanddirty_log_perf_test[Ben]- Couple more cleanups, like adding
perf_test_destroy_vm()to balance create/destroy of a perf test vm [drew]- Added new patches [drew]:
- Since
dirty_log_perf_testworks fine on AArch64, "KVM: selftests: Also builddirty_log_perf_teston AArch64"- More cleanup of redundant code,
"KVM: selftests: x86: Set supported CPUIDs on default VM"- Keep test skipping consistent,
"KVM: selftests: Make test skipping consistent"I definitely want to get x86 people's review on "KVM: selftests: x86:
Set supported CPUIDs on default VM" to be sure it's OK to do that.Thanks,
drewAndrew Jones (11):
KVM: selftests: Update .gitignore
KVM: selftests: Remove deadcode
KVM: selftests: Factor out guest mode code
KVM: selftests: Makevm_create_defaultcommon
KVM: selftests: Introduce vm_create_[default_]_with_vcpus
KVM: selftests:dirty_log_test: Removecreate_vm
KVM: selftests: Usevm_create_with_vcpusincreate_vm
KVM: selftests: Implementperf_test_utilmore conventionally
KVM: selftests: Also builddirty_log_perf_teston AArch64
KVM: selftests: x86: Set supported CPUIDs on default VM
KVM: selftests: Make test skipping consistenttools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 3 +- .../selftests/kvm/demand_paging_test.c | 118 +++-------- .../selftests/kvm/dirty_log_perf_test.c | 183 ++++-------------- tools/testing/selftests/kvm/dirty_log_test.c | 182 +++++------------ .../selftests/kvm/include/guest_modes.h | 21 ++ .../testing/selftests/kvm/include/kvm_util.h | 42 +++- .../selftests/kvm/include/perf_test_util.h | 171 ++-------------- .../selftests/kvm/lib/aarch64/processor.c | 17 -- tools/testing/selftests/kvm/lib/guest_modes.c | 70 +++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 67 ++++++- .../selftests/kvm/lib/perf_test_util.c | 134 +++++++++++++ .../selftests/kvm/lib/s390x/processor.c | 22 --- .../selftests/kvm/lib/x86_64/processor.c | 32 --- .../selftests/kvm/set_memory_region_test.c | 2 - .../kvm/x86_64/cr4_cpuid_sync_test.c | 1 - .../testing/selftests/kvm/x86_64/debug_regs.c | 1 - .../testing/selftests/kvm/x86_64/evmcs_test.c | 2 - .../selftests/kvm/x86_64/kvm_pv_test.c | 4 +- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 - .../testing/selftests/kvm/x86_64/state_test.c | 1 - .../selftests/kvm/x86_64/svm_vmcall_test.c | 1 - .../selftests/kvm/x86_64/tsc_msrs_test.c | 1 - .../selftests/kvm/x86_64/user_msr_test.c | 7 +- .../kvm/x86_64/vmx_apic_access_test.c | 1 - .../kvm/x86_64/vmx_close_while_nested_test.c | 1 - .../selftests/kvm/x86_64/vmx_dirty_log_test.c | 1 - .../kvm/x86_64/vmx_preemption_timer_test.c | 15 +- .../kvm/x86_64/vmx_set_nested_state_test.c | 21 ++ .../kvm/x86_64/vmx_tsc_adjust_test.c | 1 - 30 files changed, 499 insertions(+), 627 deletions(-)create mode 100644
tools/testing/selftests/kvm/include/guest_modes.hcreate mode 100644tools/testing/selftests/kvm/lib/guest_modes.ccreate mode 100644tools/testing/selftests/kvm/lib/perf_test_util.c