Minor improvements to the selftest setup logic
v3 cover Minor improvements to the selftest setup logic
This series is posted in context of the discussion at:
https://lore.kernel.org/lkml/Ywa9T+jKUpaHLu%[email protected]/
Changes in v3:
* Original series is split into two and this v3 version contains the improvements to selftest and VM setup.
* Planning to upload the second series to execute hypercall instruction according to cpu type separately.
* Addressed comments from David and Sean.
link to v2:
https://lore.kernel.org/all/[email protected]/
Changes in v2:
* Addressed comments from Andrew and David
* Common function with constructor attribute used to setup initial state
* Changes are split in more logical granules as per feedback
Major changes:
1) Move common startup logic to a single function in kvm_util.c
2) Introduce following APIs:
kvm_selftest_arch_init: to perform arch specific common startup.
kvm_arch_vm_post_create: to update the guest memory state to convey
common information to guests.
3) For x86, capture cpu type at startup and pass on the cpu type to guest after guest elf is loaded.
Vishal Annapurve (4):
KVM: selftests: move common startup logic to kvm_util.c
KVM: selftests: Add arch specific initialization
KVM: selftests: Add arch specific post vm creation hook
KVM: selftests: x86: Precompute the cpu type
.../selftests/kvm/aarch64/arch_timer.c | 3 --- .../selftests/kvm/aarch64/hypercalls.c | 2 -- .../testing/selftests/kvm/aarch64/vgic_irq.c | 3 --- .../selftests/kvm/include/kvm_util_base.h | 9 ++++++++ .../selftests/kvm/lib/aarch64/processor.c | 18 ++++++++-------- tools/testing/selftests/kvm/lib/kvm_util.c | 21 ++++++++++++++++--- .../selftests/kvm/lib/x86_64/processor.c | 16 ++++++++++++-- .../testing/selftests/kvm/memslot_perf_test.c | 3 --- tools/testing/selftests/kvm/rseq_test.c | 3 --- tools/testing/selftests/kvm/s390x/memop.c | 2 -- tools/testing/selftests/kvm/s390x/resets.c | 2 -- .../selftests/kvm/s390x/sync_regs_test.c | 3 --- .../selftests/kvm/set_memory_region_test.c | 3 --- .../kvm/x86_64/cr4_cpuid_sync_test.c | 3 --- .../kvm/x86_64/emulator_error_test.c | 3 --- .../selftests/kvm/x86_64/hyperv_cpuid.c | 3 --- .../selftests/kvm/x86_64/platform_info_test.c | 3 --- .../kvm/x86_64/pmu_event_filter_test.c | 3 --- .../selftests/kvm/x86_64/set_sregs_test.c | 3 --- .../kvm/x86_64/svm_nested_soft_inject_test.c | 3 --- .../selftests/kvm/x86_64/sync_regs_test.c | 3 --- .../selftests/kvm/x86_64/userspace_io_test.c | 3 --- .../kvm/x86_64/userspace_msr_exit_test.c | 3 --- 23 files changed, 50 insertions(+), 68 deletions(-)
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
PATCH v3 1/4 KVM: selftests: move common startup logic to kvm_util.c
Consolidate common startup logic in one place by implementing a single setup function with __attribute((constructor)) for all selftests within kvm_util.c.
This allows moving logic like:
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);to a single file for all selftests.
This will also allow any required setup at entry in future to be done in common main function.
More context is discussed at:
https://lore.kernel.org/lkml/Ywa9T+jKUpaHLu%[email protected]/
--- tools/testing/selftests/kvm/aarch64/arch_timer.c | 3 --- tools/testing/selftests/kvm/aarch64/hypercalls.c | 2 -- tools/testing/selftests/kvm/aarch64/vgic_irq.c | 3 --- tools/testing/selftests/kvm/lib/kvm_util.c | 6 ++++++ tools/testing/selftests/kvm/memslot_perf_test.c | 3 --- tools/testing/selftests/kvm/rseq_test.c | 3 --- tools/testing/selftests/kvm/s390x/memop.c | 2 -- tools/testing/selftests/kvm/s390x/resets.c | 2 -- tools/testing/selftests/kvm/s390x/sync_regs_test.c | 3 --- tools/testing/selftests/kvm/set_memory_region_test.c | 3 --- tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c | 3 --- tools/testing/selftests/kvm/x86_64/emulator_error_test.c | 3 --- tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 3 --- tools/testing/selftests/kvm/x86_64/platform_info_test.c | 3 --- tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c | 3 --- tools/testing/selftests/kvm/x86_64/set_sregs_test.c | 3 --- .../selftests/kvm/x86_64/svm_nested_soft_inject_test.c | 3 --- tools/testing/selftests/kvm/x86_64/sync_regs_test.c | 3 --- tools/testing/selftests/kvm/x86_64/userspace_io_test.c | 3 --- .../testing/selftests/kvm/x86_64/userspace_msr_exit_test.c | 3 --- 20 files changed, 6 insertions(+), 54 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +0 -3
@@ -462,9 +462,6 @@ int main(int argc, char *argv[]) {struct kvm_vm *vm; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -if (!parse_args (argc, argv))exit (KSFT_SKIP );
tools/testing/selftests/kvm/aarch64/hypercalls.c +0 -2
@@ -306,8 +306,6 @@ static void test_run(void)int main (void ) { -setbuf (stdout,NULL ); -test_run ();return 0 ; }
tools/testing/selftests/kvm/aarch64/vgic_irq.c +0 -3
@@ -818,9 +818,6 @@ int main(int argc, char **argv)int opt;bool eoi_split = false; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -while ((opt =getopt (argc, argv,"hn:e:l:" )) !=-1 ) {switch (opt) {case 'n' :
tools/testing/selftests/kvm/lib/kvm_util.c +6 -0
@@ -2021,3 +2021,9 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,break ; } } + +void __attribute((constructor))kvm_selftest_init (void ) +{ +/* Tell stdout not to buffer its content. */ +setbuf (stdout,NULL ); +}
tools/testing/selftests/kvm/memslot_perf_test.c +0 -3
@@ -1007,9 +1007,6 @@ int main(int argc, char *argv[])struct test_result rbestslottime;int tctr; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -if (!parse_args (argc, argv, &targs))return -1 ;
tools/testing/selftests/kvm/rseq_test.c +0 -3
@@ -205,9 +205,6 @@ int main(int argc, char *argv[])struct kvm_vcpu *vcpu;u32 cpu, rseq_cpu; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); - r =sched_getaffinity (0 ,sizeof (possible_mask), &possible_mask);TEST_ASSERT (!r,"sched_getaffinity failed, errno = %d (%s)" , errno,strerror (errno));
tools/testing/selftests/kvm/s390x/memop.c +0 -2
@@ -760,8 +760,6 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_has_cap (KVM_CAP_S390_MEM_OP )); -setbuf (stdout,NULL );/* Tell stdout not to buffer its content */ -ksft_print_header ();ksft_set_plan (ARRAY_SIZE (testlist));
tools/testing/selftests/kvm/s390x/resets.c +0 -2
@@ -296,8 +296,6 @@ int main(int argc, char *argv[])bool has_s390_vcpu_resets =kvm_check_cap (KVM_CAP_S390_VCPU_RESETS );int idx; -setbuf (stdout,NULL );/* Tell stdout not to buffer its content */ -ksft_print_header ();ksft_set_plan (ARRAY_SIZE (testlist));
tools/testing/selftests/kvm/s390x/sync_regs_test.c +0 -3
@@ -231,9 +231,6 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_has_cap (KVM_CAP_SYNC_REGS )); -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -ksft_print_header ();ksft_set_plan (ARRAY_SIZE (testlist));
tools/testing/selftests/kvm/set_memory_region_test.c +0 -3
@@ -392,9 +392,6 @@ int main(int argc, char *argv[])int i, loops;#endif -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -#ifdef __x86_64__/* * FIXME: the zero-memslot test fails on aarch64 and s390x because
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +0 -3
@@ -57,9 +57,6 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XSAVE )); -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); - vm =vm_create_with_one_vcpu (&vcpu, guest_code); run = vcpu->run;
tools/testing/selftests/kvm/x86_64/emulator_error_test.c +0 -3
@@ -156,9 +156,6 @@ int main(int argc, char *argv[])uint64_t *hva;int rc; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -TEST_REQUIRE (kvm_has_cap (KVM_CAP_SMALLER_MAXPHYADDR )); vm =vm_create_with_one_vcpu (&vcpu, guest_code);
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -3
@@ -134,9 +134,6 @@ int main(int argc, char *argv[])const struct kvm_cpuid2 *hv_cpuid_entries;struct kvm_vcpu *vcpu; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -TEST_REQUIRE (kvm_has_cap (KVM_CAP_HYPERV_CPUID )); vm =vm_create_with_one_vcpu (&vcpu, guest_code);
tools/testing/selftests/kvm/x86_64/platform_info_test.c +0 -3
@@ -72,9 +72,6 @@ int main(int argc, char *argv[])struct kvm_vm *vm;uint64_t msr_platform_info; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -TEST_REQUIRE (kvm_has_cap (KVM_CAP_MSR_PLATFORM_INFO )); vm =vm_create_with_one_vcpu (&vcpu, guest_code);
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +0 -3
@@ -447,9 +447,6 @@ int main(int argc, char *argv[])struct kvm_vcpu *vcpu;struct kvm_vm *vm; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -TEST_REQUIRE (kvm_has_cap (KVM_CAP_PMU_EVENT_FILTER ));TEST_REQUIRE (use_intel_pmu () ||use_amd_pmu ());
tools/testing/selftests/kvm/x86_64/set_sregs_test.c +0 -3
@@ -82,9 +82,6 @@ int main(int argc, char *argv[])uint64_t cr4;int rc; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -/* * Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and * use it to verify all supported CR4 bits can be set prior to defining
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c +0 -3
@@ -194,9 +194,6 @@ static void run_test(bool is_nmi)int main (int argc,char *argv[]) { -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); TEST_ASSERT(kvm_cpu_has(X86_FEATURE_NRIPS),
tools/testing/selftests/kvm/x86_64/sync_regs_test.c +0 -3
@@ -90,9 +90,6 @@ int main(int argc, char *argv[])struct kvm_vcpu_events events;int rv, cap; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); - cap =kvm_check_cap (KVM_CAP_SYNC_REGS );TEST_REQUIRE ((cap &TEST_SYNC_FIELDS ) ==TEST_SYNC_FIELDS );TEST_REQUIRE (!(cap &INVALID_SYNC_FIELD ));
tools/testing/selftests/kvm/x86_64/userspace_io_test.c +0 -3
@@ -56,9 +56,6 @@ int main(int argc, char *argv[])struct kvm_vm *vm;struct ucall uc; -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); - vm =vm_create_with_one_vcpu (&vcpu, guest_code); run = vcpu->run;
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c +0 -3
@@ -735,9 +735,6 @@ static void test_msr_permission_bitmap(void)int main (int argc,char *argv[]) { -/* Tell stdout not to buffer its content */ -setbuf (stdout,NULL ); -test_msr_filter_allow ();test_msr_filter_deny ();
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Re: [V3 PATCH 1/4] KVM: selftests: move common startup logic to kvm_util.c
Quoted 13 lines · Vishal Annapurve
Consolidate common startup logic in one place by implementing a single setup function with __attribute((constructor)) for all selftests within
kvm_util.c.This allows moving logic like:
/* Tell stdout not to buffer its content */ setbuf(stdout, NULL);to a single file for all selftests.
This will also allow any required setup at entry in future to be done in common main function.
Quoted 6 lines
More context is discussed at:
https://lore.kernel.org/lkml/Ywa9T+jKUpaHLu%[email protected]/Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>Reviewed-by Andrew Jones <[email protected]>
PATCH v3 2/4 KVM: selftests: Add arch specific initialization
In reply to Vishal AnnapurveIntroduce arch specific API: kvm_selftest_arch_init to allow each arch to handle initialization before running any selftest logic.
--- .../selftests/kvm/include/kvm_util_base.h | 5 +++++ .../selftests/kvm/lib/aarch64/processor.c | 18 +++++++++--------- tools/testing/selftests/kvm/lib/kvm_util.c | 6 ++++++ 3 files changed, 20 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +5 -0
@@ -838,4 +838,9 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm) return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0); } +/* +* API to execute architecture specific setup before executing main(). +*/ +void kvm_selftest_arch_init (void ); +#endif /* SELFTEST_KVM_UTIL_BASE_H */
tools/testing/selftests/kvm/lib/aarch64/processor.c +9 -9
@@ -495,15 +495,6 @@ void aarch64_get_supported_page_sizes(uint32_t ipa,close (kvm_fd); } -/* -* arm64 doesn't have a true default mode, so start by computing the -* available IPA space and page sizes early. -*/ -void __attribute__((constructor))init_guest_modes (void ) -{ -guest_modes_append_default (); -} -void smccc_hvc (uint32_t function_id,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint64_t arg6,struct arm_smccc_res *res)
@@ -528,3 +519,12 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1, [arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6) : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7"); } + +void kvm_selftest_arch_init(void) +{ +/* +* arm64 doesn't have a true default mode, so start by computing the +* available IPA space and page sizes early. +*/ +guest_modes_append_default (); +}
tools/testing/selftests/kvm/lib/kvm_util.c +6 -0
@@ -2022,8 +2022,14 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, } } +__weak void kvm_selftest_arch_init (void ) +{ +} +void __attribute((constructor))kvm_selftest_init (void ) {/* Tell stdout not to buffer its content. */ setbuf (stdout,NULL ); + +kvm_selftest_arch_init (); }
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Re: [V3 PATCH 2/4] KVM: selftests: Add arch specific initialization
Quoted 11 lines · Vishal Annapurve
Introduce arch specific API:
kvm_selftest_arch_initto allow each arch to handle initialization before running any selftest logic.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 5 +++++ .../selftests/kvm/lib/aarch64/processor.c | 18 +++++++++--------- tools/testing/selftests/kvm/lib/kvm_util.c | 6 ++++++ 3 files changed, 20 insertions(+), 9 deletions(-)
Peter Gonda
Reviewed-byRe: [V3 PATCH 2/4] KVM: selftests: Add arch specific initialization
Quoted 14 lines · Andrew Jones
Quoted 11 lines · Vishal Annapurve
Introduce arch specific API:
kvm_selftest_arch_initto allow each arch to handle initialization before running any selftest logic.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 5 +++++ .../selftests/kvm/lib/aarch64/processor.c | 18 +++++++++--------- tools/testing/selftests/kvm/lib/kvm_util.c | 6 ++++++ 3 files changed, 20 insertions(+), 9 deletions(-)
Quoted 1 line
Reviewed-by Andrew Jones <[email protected]>
PATCH v3 3/4 KVM: selftests: Add arch specific post vm creation hook
In reply to Vishal AnnapurveAdd arch specific API kvm_arch_vm_post_create to perform any required setup after VM creation.
This API will be used in followup commit to convey cpu vendor type to the guest vm.
--- tools/testing/selftests/kvm/include/kvm_util_base.h | 4 ++++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +4 -0
@@ -843,4 +843,8 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)*/ void kvm_selftest_arch_init (void ); +/* +* API to execute architecture specific setup after creating the VM. +*/ +void kvm_arch_vm_post_create (struct kvm_vm *vm);#endif /* SELFTEST_KVM_UTIL_BASE_H */
tools/testing/selftests/kvm/lib/kvm_util.c +6 -3
@@ -340,9 +340,8 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus,kvm_vm_elf_load (vm, program_invocation_name); -#ifdef __x86_64__ -vm_create_irqchip (vm); -#endif +kvm_arch_vm_post_create (vm); +return vm; }
@@ -2022,6 +2021,10 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, } } +__weak void kvm_arch_vm_post_create (struct kvm_vm *vm) +{ +} +__weak void kvm_selftest_arch_init (void ) { }
tools/testing/selftests/kvm/lib/x86_64/processor.c +6 -0
@@ -1327,3 +1327,9 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)return get_kvm_intel_param_bool ("unrestricted_guest" ); } + + +void kvm_arch_vm_post_create (struct kvm_vm *vm) +{ +vm_create_irqchip (vm); +}
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Re: [V3 PATCH 3/4] KVM: selftests: Add arch specific post vm creation hook
Quoted 6 lines · Vishal Annapurve
Add arch specific API
kvm_arch_vm_post_createto perform any required setup after VM creation.This API will be used in followup commit to convey cpu vendor type to the guest vm.
Quoted 8 lines
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>--- tools/testing/selftests/kvm/include/kvm_util_base.h | 4 ++++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-)
Peter Gonda
Reviewed-byRe: [V3 PATCH 3/4] KVM: selftests: Add arch specific post vm creation hook
Quoted 17 lines · Andrew Jones
Quoted 6 lines · Vishal Annapurve
Add arch specific API
kvm_arch_vm_post_createto perform any required setup after VM creation.This API will be used in followup commit to convey cpu vendor type to the guest vm.
Quoted 8 lines
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>--- tools/testing/selftests/kvm/include/kvm_util_base.h | 4 ++++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-)
Quoted 1 line
Reviewed-by Andrew Jones <[email protected]>
PATCH v3 4/4 KVM: selftests: x86: Precompute the cpu type
In reply to Vishal AnnapurveCache the vendor CPU type in a global variable so that multiple calls to is_amd/intel_cpu() do not need to re-execute CPUID.
Sync the global variable is_cpu_amd into the guest so the guest can also avoid executing CPUID instruction.
--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +8 -2
@@ -19,6 +19,7 @@#define MAX_NR_CPUID_ENTRIES 100vm_vaddr_t exception_handlers; +static bool is_cpu_amd;static void regs_dump (FILE *stream,struct kvm_regs *regs,uint8_t indent) {
@@ -1046,7 +1047,7 @@ static bool cpu_vendor_string_is(const char *vendor)bool is_intel_cpu (void ) { -return cpu_vendor_string_is ("GenuineIntel" ); +return !is_cpu_amd; }/*
@@ -1054,7 +1055,7 @@ bool is_intel_cpu(void)*/ bool is_amd_cpu (void ) { -return cpu_vendor_string_is ("AuthenticAMD" ); +return is_cpu_amd; }void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits)
@@ -1328,8 +1329,13 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)return get_kvm_intel_param_bool ("unrestricted_guest" ); } +void kvm_selftest_arch_init (void ) +{ + is_cpu_amd =cpu_vendor_string_is ("AuthenticAMD" ); +}void kvm_arch_vm_post_create (struct kvm_vm *vm) {vm_create_irqchip (vm); +sync_global_to_guest (vm, is_cpu_amd); }
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
reviewerRe: [V3 PATCH 4/4] KVM: selftests: x86: Precompute the cpu type
Quoted 17 lines · Vishal Annapurve
Cache the vendor CPU type in a global variable so that multiple calls to is_amd/intel_cpu() do not need to re-execute CPUID.
Sync the global variable
is_cpu_amdinto the guest so the guest can also avoid executing CPUID instruction.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0
tools/testing/selftests/kvm/lib/x86_64/processor.c line 22
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-0@@ -19,6 +19,7 @@#define MAX_NR_CPUID_ENTRIES 100vm_vaddr_t exception_handlers; +static bool is_cpu_amd;
This should probably have a "host" qualifier, e.g. is_host_cpu_amd. More below.
tools/testing/selftests/kvm/lib/x86_64/processor.c static bool cpu_vendor_string_is(const char *vendor) · line 1048
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0static void regs_dump (FILE *stream,struct kvm_regs *regs,uint8_t indent) {@@ -1046,7 +1047,7 @@ static bool cpu_vendor_string_is(const char *vendor)bool is_intel_cpu (void )
It'll be more churn, but I think we should drop the wrappers in this patch so that we can visually audit all users. There is technically a subtle functional change here, as previously executing is_intel_cpu() and is_amd_cpu() in the guest will consume the _guest_ CPUID, whereas with this change, the guest will now consume the _host_ CPUID.
It just so happens that the existing user and the new user both want to query Intel vs. AMD for VMCALL vs. VMMCALL, i.e. care about the host even when checking from the guest. It's extreme paranoia since I don't think there are any parallel series that are adding is_intel_cpu()/is_amd_cpu() users, not to mention that I don't think any selftests does cross-vendor virtualization, but on the other hand the paranoia doesn't cost much.
tools/testing/selftests/kvm/lib/x86_64/processor.c static bool cpu_vendor_string_is(const char *vendor) · line 1050
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-1{ -return cpu_vendor_string_is ("GenuineIntel" ); +return !is_cpu_amd;
Please keep the explicit "GenuineIntel" check, i.e. add is_host_cpu_intel. KVM technically supports other vendors, e.g. Centaur and Zhaoxin for VMX, and Hygon for AMD, so it's not impossible that someone could run on Centuar or Zhaoxin and get a false positive. Again, extreme paranoia, but doesn't cost much.
Quoted 29 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +6
-1}/* @@ -1054,7 +1055,7 @@ bool is_intel_cpu(void)*/ bool is_amd_cpu (void ) { -return cpu_vendor_string_is ("AuthenticAMD" ); +return is_cpu_amd; }void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits)@@ -1328,8 +1329,13 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)return get_kvm_intel_param_bool ("unrestricted_guest" ); } +void kvm_selftest_arch_init (void ) +{ + is_cpu_amd =cpu_vendor_string_is ("AuthenticAMD" ); +}void kvm_arch_vm_post_create (struct kvm_vm *vm) {vm_create_irqchip (vm); +sync_global_to_guest (vm, is_cpu_amd); }Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Re: [V3 PATCH 4/4] KVM: selftests: x86: Precompute the cpu type
Quoted 21 lines · Sean Christopherson
Quoted 19 lines · Vishal Annapurve
Cache the vendor CPU type in a global variable so that multiple calls to is_amd/intel_cpu() do not need to re-execute CPUID.
Sync the global variable
is_cpu_amdinto the guest so the guest can also avoid executing CPUID instruction.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Vishal Annapurve <[email protected]>--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0@@ -19,6 +19,7 @@#define MAX_NR_CPUID_ENTRIES 100
Quoted 5 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c line 22
Quoted 2 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-0vm_vaddr_t exception_handlers; +static bool is_cpu_amd;This should probably have a "host" qualifier, e.g.
is_host_cpu_amd. More below.
Ack.
Quoted 13 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c static bool cpu_vendor_string_is(const char *vendor) · line 1048
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0static void regs_dump (FILE *stream,struct kvm_regs *regs,uint8_t indent) {@@ -1046,7 +1047,7 @@ static bool cpu_vendor_string_is(const char *vendor)bool is_intel_cpu (void )It'll be more churn, but I think we should drop the wrappers in this patch so that we can visually audit all users. There is technically a subtle functional change here, as previously executing
is_intel_cpu()andis_amd_cpu()in the guest will consume the _guest_ CPUID, whereas with this change, the guest will now consume the _host_ CPUID.
Quoted 7 lines
It just so happens that the existing user and the new user both want to query Intel vs. AMD for VMCALL vs. VMMCALL, i.e. care about the host even when checking from the guest. It's extreme paranoia since I don't think there are any parallel series that are adding is_intel_cpu()/is_amd_cpu() users, not to mention that I don't think any selftests does cross-vendor virtualization, but on the other hand the paranoia doesn't cost much.
Ack. I think this patch should also be the part of a different series which deals with executing hypercall according to the cpu type, there is no immediate need for this change in this series.
Will incorporate your feedback in the next version of this patch.
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c static bool cpu_vendor_string_is(const char *vendor) · line 1050
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-1{ -return cpu_vendor_string_is ("GenuineIntel" ); +return !is_cpu_amd;Please keep the explicit "GenuineIntel" check, i.e. add
is_host_cpu_intel. KVM technically supports other vendors, e.g. Centaur and Zhaoxin for VMX, and Hygon for AMD, so it's not impossible that someone could run on Centuar or Zhaoxin and get a false positive. Again, extreme paranoia, but doesn't cost much.
Ack, makes sense.
Quoted 29 lines
Quoted 29 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +6
-2}/* @@ -1054,7 +1055,7 @@ bool is_intel_cpu(void)*/ bool is_amd_cpu (void ) { -return cpu_vendor_string_is ("AuthenticAMD" ); +return is_cpu_amd; }void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits)@@ -1328,8 +1329,13 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)return get_kvm_intel_param_bool ("unrestricted_guest" ); } +void kvm_selftest_arch_init (void ) +{ + is_cpu_amd =cpu_vendor_string_is ("AuthenticAMD" ); +}void kvm_arch_vm_post_create (struct kvm_vm *vm) {vm_create_irqchip (vm); +sync_global_to_guest (vm, is_cpu_amd); } --2.38.0.rc1.362.ged0d419d3c-goog