KVM: selftests: Introduce vcpu_arch_put_guest()
Sean Christopherson
reviewercover KVM: selftests: Introduce vcpu_arch_put_guest()
The end goal of this series is to add a regression test for commit 910c57dfa4d1 ("KVM: x86: Mark target gfn of emulated atomic instruction as dirty"), *without* polluting the common dirty_log_test.c code with gory x86 details.
The regression test requires forcing KVM to emulate a guest atomic RMW access, which is done via a magic instruction prefix/opcode that is guarded by an off-by-default KVM module param.
To allow x86 to (a) detect the param, (b) shove in the unique instruction, and (c) do all of that conditionally so that selftests doesn't test _only_ the forced emulation path, this series provides a pseudo-RNG instance for all tests, and a new arch hook for doing "interesting" guest writes (vcpu_arch_put_guest()).
Tested on x86 and ARM, compile tested on s390 and RISC-V.
Sean Christopherson (5):
KVM: selftests: Provide a global pseudo-RNG instance for all tests
KVM: selftests: Provide an API for getting a random bool from an RNG
KVM: selftests: Add global snapshot of
kvm_is_forced_emulation_enabled()
KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code
KVM: selftests: Randomly force emulation on x86 writes from guest code
.../selftests/kvm/dirty_log_perf_test.c | 9 ++++---- tools/testing/selftests/kvm/dirty_log_test.c | 22 ++++-------------- .../selftests/kvm/include/kvm_util_base.h | 3 +++ .../testing/selftests/kvm/include/memstress.h | 1 - .../testing/selftests/kvm/include/test_util.h | 19 +++++++++++++++ .../kvm/include/x86_64/kvm_util_arch.h | 23 +++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++++ tools/testing/selftests/kvm/lib/memstress.c | 10 ++------ .../selftests/kvm/lib/x86_64/processor.c | 3 +++ .../selftests/kvm/x86_64/pmu_counters_test.c | 3 --- .../kvm/x86_64/userspace_msr_exit_test.c | 10 ++------ .../selftests/kvm/x86_64/xen_shinfo_test.c | 5 ++-- 12 files changed, 72 insertions(+), 45 deletions(-)
base-commit: e9a2bba476c8332ed547fce485c158d03b0b9659
Signature · 1 line
2.44.0.291.gc1ea87d7ee-goog
Sean Christopherson
reviewerPATCH 1/5 KVM: selftests: Provide a global pseudo-RNG instance for all tests
Add a global guest_random_state instance, i.e. a pseudo-RNG, so that an RNG is available for *all* tests. This will allow randomizing behavior in core library code, e.g. x86 will utilize the pRNG to conditionally force emulation of writes from within common guest code.
To allow for deterministic runs, and to be compatible with existing tests, allow tests to override the seed used to initialize the pRNG.
Note, the seed *must* be overwritten before a VM is created in order for the seed to take effect, though it's perfectly fine for a test to initialize multiple VMs with different seeds.
And as evidenced by memstress_guest_code(), it's also a-ok to instantiate more RNGs using the global seed (or a modified version of it). The goal of the global RNG is purely to ensure that _a_ source of random numbers is available, it doesn't have to be the _only_ RNG.
--- .../testing/selftests/kvm/dirty_log_perf_test.c | 9 ++++----- tools/testing/selftests/kvm/dirty_log_test.c | 17 +---------------- tools/testing/selftests/kvm/include/memstress.h | 1 - tools/testing/selftests/kvm/include/test_util.h | 8 ++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 +++++++++ tools/testing/selftests/kvm/lib/memstress.c | 8 +------- 6 files changed, 23 insertions(+), 29 deletions(-)
tools/testing/selftests/kvm/dirty_log_perf_test.c +4 -5
@@ -132,7 +132,6 @@ struct test_params {enum vm_mem_backing_src_type backing_src;int slots;uint32_t write_percent; -uint32_t random_seed;bool random_access; };
@@ -156,8 +155,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) p->slots, p->backing_src, p->partition_vcpu_memory_access); -pr_info ("Random seed: %u\n" , p->random_seed); -memstress_set_random_seed (vm, p->random_seed);memstress_set_write_percent (vm, p->write_percent); guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift;
@@ -346,11 +343,13 @@ int main(int argc, char *argv[]) .partition_vcpu_memory_access = true, .backing_src = DEFAULT_VM_MEM_SRC, .slots = 1, - .random_seed = 1, .write_percent = 100, };int opt; +/* Override the seed to be deterministic by default. */ + guest_random_seed =1 ; + dirty_log_manual_caps =kvm_check_cap (KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ); dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
@@ -395,7 +394,7 @@ int main(int argc, char *argv[]) p.phys_offset =strtoull (optarg,NULL ,0 );break ;case 'r' : - p.random_seed =atoi_positive ("Random seed" , optarg); + guest_random_seed =atoi_positive ("Random seed" , optarg);break ;case 's' : p.backing_src =parse_backing_src_type (optarg);
tools/testing/selftests/kvm/dirty_log_test.c +1 -16
@@ -76,7 +76,6 @@static uint64_t host_page_size;static uint64_t guest_page_size;static uint64_t guest_num_pages; -static uint64_t random_array[TEST_PAGES_PER_LOOP ];static uint64_t iteration;/*
@@ -115,13 +114,12 @@ static void guest_code(void)while (true) {for (i =0 ; i <TEST_PAGES_PER_LOOP ; i++) { addr = guest_test_virt_mem; - addr += (READ_ONCE (random_array[i]) % guest_num_pages) + addr += (guest_random_u64 (&guest_rng) % guest_num_pages) * guest_page_size; addr =align_down (addr, host_page_size); *(uint64_t *)addr =READ_ONCE (iteration); } -/* Tell the host that we need more random numbers */ GUEST_SYNC (1 ); } }
@@ -508,20 +506,10 @@ static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err) mode->after_vcpu_run (vcpu, ret, err); } -static void generate_random_array (uint64_t *guest_array,uint64_t size) -{ -uint64_t i; - -for (i =0 ; i < size; i++) - guest_array[i] =random (); -} -static void *vcpu_worker (void *data) {int ret;struct kvm_vcpu *vcpu = data; -struct kvm_vm *vm = vcpu->vm; -uint64_t *guest_array;uint64_t pages_count =0 ;struct kvm_signal_mask *sigmask =alloca (offsetof(struct kvm_signal_mask , sigset) +sizeof (sigset_t));
@@ -540,11 +528,8 @@ static void *vcpu_worker(void *data)sigemptyset (sigset);sigaddset (sigset,SIG_IPI ); - guest_array =addr_gva2hva (vm, (vm_vaddr_t )random_array); -while (!READ_ONCE (host_quit)) {/* Clear any existing kick signals */ -generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); pages_count +=TEST_PAGES_PER_LOOP ;/* Let the guest dirty the random pages */ ret =__vcpu_run (vcpu);
tools/testing/selftests/kvm/include/memstress.h +0 -1
@@ -62,7 +62,6 @@ struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,void memstress_destroy_vm (struct kvm_vm *vm);void memstress_set_write_percent (struct kvm_vm *vm,uint32_t write_percent); -void memstress_set_random_seed (struct kvm_vm *vm,uint32_t random_seed);void memstress_set_random_access (struct kvm_vm *vm,bool random_access);void memstress_start_vcpu_threads (int vcpus,void (*vcpu_fn)(struct memstress_vcpu_args *));
tools/testing/selftests/kvm/include/test_util.h +8 -0
@@ -91,9 +91,17 @@ struct guest_random_state {uint32_t seed; }; +extern uint32_t guest_random_seed; +extern struct guest_random_state guest_rng; +struct guest_random_state new_guest_random_state (uint32_t seed);uint32_t guest_random_u32 (struct guest_random_state *state); +static inline uint64_t guest_random_u64 (struct guest_random_state *state) +{ +return ((uint64_t )guest_random_u32 (state) <<32 ) |guest_random_u32 (state); +} +enum vm_mem_backing_src_type {VM_MEM_SRC_ANONYMOUS ,VM_MEM_SRC_ANONYMOUS_THP ,
tools/testing/selftests/kvm/lib/kvm_util.c +9 -0
@@ -20,6 +20,9 @@#define KVM_UTIL_MIN_PFN 2 +uint32_t guest_random_seed; +struct guest_random_state guest_rng; +static int vcpu_mmap_sz (void );int open_path_or_exit (const char *path,int flags)
@@ -433,6 +436,10 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, slot0 =memslot2region (vm,0 );ucall_init (vm, slot0->region.guest_phys_addr + slot0->region.memory_size); +pr_info ("Random seed: 0x%x\n" , guest_random_seed); + guest_rng =new_guest_random_state (guest_random_seed); +sync_global_to_guest (vm, guest_rng); +kvm_arch_vm_post_create (vm);return vm;
@@ -2306,6 +2313,8 @@ void __attribute((constructor)) kvm_selftest_init(void)/* Tell stdout not to buffer its content. */ setbuf (stdout,NULL ); + guest_random_seed =random (); +kvm_selftest_arch_init (); }
tools/testing/selftests/kvm/lib/memstress.c +1 -7
@@ -56,7 +56,7 @@ void memstress_guest_code(uint32_t vcpu_idx)uint64_t page;int i; - rand_state =new_guest_random_state (args->random_seed + vcpu_idx); + rand_state =new_guest_random_state (guest_random_seed + vcpu_idx); gva = vcpu_args->gva; pages = vcpu_args->pages;
@@ -243,12 +243,6 @@ void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent)sync_global_to_guest (vm, memstress_args.write_percent); } -void memstress_set_random_seed (struct kvm_vm *vm,uint32_t random_seed) -{ - memstress_args.random_seed = random_seed; -sync_global_to_guest (vm, memstress_args.random_seed); -} -void memstress_set_random_access (struct kvm_vm *vm,bool random_access) { memstress_args.random_access = random_access;
Signature · 1 line
2.44.0.291.gc1ea87d7ee-goog
Sean Christopherson
reviewerPATCH 2/5 KVM: selftests: Provide an API for getting a random bool from an RNG
In reply to Sean ChristophersonMove memstress' random bool logic into common code to avoid reinventing the wheel for basic yes/no decisions. Provide an outer wrapper to handle the basic/common case of just wanting a 50/50 chance of something happening.
--- tools/testing/selftests/kvm/include/test_util.h | 11 +++++++++++ tools/testing/selftests/kvm/lib/memstress.c | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/test_util.h +11 -0
@@ -97,6 +97,17 @@ extern struct guest_random_state guest_rng;struct guest_random_state new_guest_random_state (uint32_t seed);uint32_t guest_random_u32 (struct guest_random_state *state); +static inline bool __guest_random_bool (struct guest_random_state *state, +uint8_t percent) +{ +return (guest_random_u32 (state) %100 ) < percent; +} + +static inline bool guest_random_bool (struct guest_random_state *state) +{ +return __guest_random_bool (state,50 ); +} +static inline uint64_t guest_random_u64 (struct guest_random_state *state) {return ((uint64_t )guest_random_u32 (state) <<32 ) |guest_random_u32 (state);
tools/testing/selftests/kvm/lib/memstress.c +1 -1
@@ -76,7 +76,7 @@ void memstress_guest_code(uint32_t vcpu_idx) addr = gva + (page * args->guest_page_size); -if (guest_random_u32 (&rand_state) %100 < args->write_percent) +if (__guest_random_bool (&rand_state, args->write_percent)) *(uint64_t *)addr =0x0123456789ABCDEF ;else READ_ONCE (*(uint64_t *)addr);
Signature · 1 line
2.44.0.291.gc1ea87d7ee-goog
Sean Christopherson
reviewerPATCH 3/5 KVM: selftests: Add global snapshot of kvm_is_forced_emulation_enabled()
In reply to Sean ChristophersonAdd a global snapshot of kvm_is_forced_emulation_enabled() and sync it to all VMs by default so that core library code can force emulation, e.g. to allow for easier testing of the intersections between emulation and other features in KVM.
--- .../selftests/kvm/include/x86_64/kvm_util_arch.h | 2 ++ tools/testing/selftests/kvm/lib/x86_64/processor.c | 3 +++ tools/testing/selftests/kvm/x86_64/pmu_counters_test.c | 3 --- .../selftests/kvm/x86_64/userspace_msr_exit_test.c | 10 ++-------- 4 files changed, 7 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +2 -0
@@ -5,6 +5,8 @@#include <stdbool.h> #include <stdint.h> +extern bool is_forced_emulation_enabled; +struct kvm_vm_arch {uint64_t c_bit;uint64_t s_bit;
tools/testing/selftests/kvm/lib/x86_64/processor.c +3 -0
@@ -23,6 +23,7 @@vm_vaddr_t exception_handlers;bool host_cpu_is_amd;bool host_cpu_is_intel; +bool is_forced_emulation_enabled;static void regs_dump (FILE *stream,struct kvm_regs *regs,uint8_t indent) {
@@ -577,6 +578,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)vm_create_irqchip (vm);sync_global_to_guest (vm, host_cpu_is_intel);sync_global_to_guest (vm, host_cpu_is_amd); +sync_global_to_guest (vm, is_forced_emulation_enabled);if (vm->subtype ==VM_SUBTYPE_SEV )sev_vm_init (vm);
@@ -1344,6 +1346,7 @@ void kvm_selftest_arch_init(void) { host_cpu_is_intel =this_cpu_is_intel (); host_cpu_is_amd =this_cpu_is_amd (); + is_forced_emulation_enabled =kvm_is_forced_emulation_enabled (); }bool sys_clocksource_is_based_on_tsc (void )
tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +0 -3
@@ -21,7 +21,6 @@static uint8_t kvm_pmu_version;static bool kvm_has_perf_caps; -static bool is_forced_emulation_enabled; static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code,
@@ -35,7 +34,6 @@ static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,vcpu_init_descriptor_tables (*vcpu);sync_global_to_guest (vm, kvm_pmu_version); -sync_global_to_guest (vm, is_forced_emulation_enabled);/* * Set PERF_CAPABILITIES before PMU version as KVM disallows enabling
@@ -612,7 +610,6 @@ int main(int argc, char *argv[]) kvm_pmu_version =kvm_cpu_property (X86_PROPERTY_PMU_VERSION ); kvm_has_perf_caps =kvm_cpu_has (X86_FEATURE_PDCM ); - is_forced_emulation_enabled =kvm_is_forced_emulation_enabled ();test_intel_counters ();
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c +2 -8
@@ -13,8 +13,6 @@#include "kvm_util.h" #include "vmx.h" -static bool fep_available; -#define MSR_NON_EXISTENT 0x474f4f00static u64 deny_bits =0 ;
@@ -258,7 +256,7 @@ static void guest_code_filter_allow(void)GUEST_ASSERT (data ==2 );GUEST_ASSERT (guest_exception_count ==0 ); -if (fep_available) { +if (is_forced_emulation_enabled) {/* Let userspace know we aren't done. */ GUEST_SYNC (0 );
@@ -520,8 +518,6 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow)uint64_t cmd;int rc; -sync_global_to_guest (vm, fep_available); - rc =kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR );TEST_ASSERT (rc,"KVM_CAP_X86_USER_SPACE_MSR is available" );vm_enable_cap (vm,KVM_CAP_X86_USER_SPACE_MSR ,KVM_MSR_EXIT_REASON_FILTER );
@@ -551,7 +547,7 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow)vcpu_run (vcpu); cmd =process_ucall (vcpu); -if (fep_available) { +if (is_forced_emulation_enabled) {TEST_ASSERT_EQ (cmd,UCALL_SYNC );vm_install_exception_handler (vm,GP_VECTOR , guest_fep_gp_handler);
@@ -774,7 +770,5 @@ KVM_ONE_VCPU_TEST(user_msr, user_exit_msr_flags, NULL)int main (int argc,char *argv[]) { - fep_available =kvm_is_forced_emulation_enabled (); -return test_harness_run (argc, argv); }
Signature · 1 line
2.44.0.291.gc1ea87d7ee-goog
Sean Christopherson
reviewerPATCH 4/5 KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code
In reply to Sean ChristophersonIntroduce a macro, vcpu_arch_put_guest(), for "putting" values to memory from guest code in "interesting" situations, e.g. when writing memory that is being dirty logged. Structure the macro so that arch code can provide a custom implementation, e.g. x86 will use the macro to force emulation of the access.
Use the helper in dirty_log_test, which is of particular interest (see above), and in xen_shinfo_test, which isn't all that interesting, but provides a second usage of the macro with a different size operand (uint8_t versus uint64_t), i.e. to help verify that the macro works for more than just 64-bit values.
Use "put" as the verb to align with the kernel's {get,put}_user() terminology.
--- tools/testing/selftests/kvm/dirty_log_test.c | 5 +++-- tools/testing/selftests/kvm/include/kvm_util_base.h | 3 +++ tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +3 -2
@@ -108,7 +108,7 @@ static void guest_code(void)*/ for (i =0 ; i < guest_num_pages; i++) { addr = guest_test_virt_mem + i * guest_page_size; - *(uint64_t *)addr =READ_ONCE (iteration); +vcpu_arch_put_guest (*(uint64_t *)addr,READ_ONCE (iteration)); }while (true) {
@@ -117,7 +117,8 @@ static void guest_code(void) addr += (guest_random_u64 (&guest_rng) % guest_num_pages) * guest_page_size; addr =align_down (addr, host_page_size); - *(uint64_t *)addr =READ_ONCE (iteration); + +vcpu_arch_put_guest (*(uint64_t *)addr,READ_ONCE (iteration)); }GUEST_SYNC (1 );
tools/testing/selftests/kvm/include/kvm_util_base.h +3 -0
@@ -610,6 +610,9 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);vm_paddr_t addr_hva2gpa (struct kvm_vm *vm,void *hva);void *addr_gpa2alias (struct kvm_vm *vm,vm_paddr_t gpa); +#ifndef vcpu_arch_put_guest +#define vcpu_arch_put_guest (mem, val) do { (mem) = (val); } while (0) +#endif static inline vm_paddr_t vm_untag_gpa (struct kvm_vm *vm,vm_paddr_t gpa) {
tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +3 -2
@@ -171,8 +171,9 @@ static volatile bool guest_saw_irq;static void evtchn_handler (struct ex_regs *regs) {struct vcpu_info *vi = (void *)VCPU_INFO_VADDR ; - vi->evtchn_upcall_pending =0 ; - vi->evtchn_pending_sel =0 ; + +vcpu_arch_put_guest (vi->evtchn_upcall_pending,0 ); +vcpu_arch_put_guest (vi->evtchn_pending_sel,0 ); guest_saw_irq = true;GUEST_SYNC (TEST_GUEST_SAW_IRQ );
Signature · 1 line
2.44.0.291.gc1ea87d7ee-goog
Sean Christopherson
reviewerPATCH 5/5 KVM: selftests: Randomly force emulation on x86 writes from guest code
In reply to Sean ChristophersonOverride vcpu_arch_put_guest() to randomly force emulation on supported accesses. Force emulation of LOCK CMPXCHG as well as a regular MOV to stress KVM's emulation of atomic accesses, which has a unique path in KVM's emulator.
Arbitrarily give all the decisions 50/50 odds; absent much, much more sophisticated infrastructure for generating random numbers, it's highly unlikely that doing more than a coin flip with affect selftests' ability to find KVM bugs.
This is effectively a regression test for commit 910c57dfa4d1 ("KVM: x86:
Mark target gfn of emulated atomic instruction as dirty").
--- .../kvm/include/x86_64/kvm_util_arch.h | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+)
tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +21 -0
@@ -5,6 +5,8 @@#include <stdbool.h> #include <stdint.h> +#include "test_util.h" +extern bool is_forced_emulation_enabled;struct kvm_vm_arch {
@@ -22,4 +24,23 @@ static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch)#define vm_arch_has_protected_memory (vm) \ __vm_arch_has_protected_memory(&(vm)->arch) +#define vcpu_arch_put_guest (mem, __val) \ +do { \ + const typeof(mem) val = (__val); \ + \ + if (!is_forced_emulation_enabled || guest_random_bool(&guest_rng)) { \ + (mem) = val; \ + } else if (guest_random_bool(&guest_rng)) { \ + __asm__ __volatile__(KVM_FEP "mov %1, %0" \ + : "+m" (mem) \ + : "r" (val) : "memory"); \ + } else { \ + uint64_t __old = READ_ONCE(mem); \ + \ + __asm__ __volatile__(KVM_FEP LOCK_PREFIX "cmpxchg %[new], %[ptr]" \ + : [ptr] "+m" (mem), [old] "+a" (__old) \ + : [new]"r" (val) : "memory", "cc"); \ + } \ +} while (0) +#endif // SELFTEST_KVM_UTIL_ARCH_H
Signature · 1 line
2.44.0.291.gc1ea87d7ee-goog
Sean Christopherson
reviewerQuoted 10 lines · Sean Christopherson
The end goal of this series is to add a regression test for commit
910c57dfa4d1("KVM: x86: Mark target gfn of emulated atomic instruction as dirty"), *without* polluting the commondirty_log_test.ccode with gory x86 details.The regression test requires forcing KVM to emulate a guest atomic RMW access, which is done via a magic instruction prefix/opcode that is guarded by an off-by-default KVM module param.
[...]
Applied to kvm-x86 selftests_utils, thanks!
[1/5] KVM: selftests: Provide a global pseudo-RNG instance for all tests
https://github.com/kvm-x86/linux/commit/cb6c6914788f [2/5] KVM: selftests: Provide an API for getting a random bool from an RNG https://github.com/kvm-x86/linux/commit/73369acd9fbd [3/5] KVM: selftests: Add global snapshot of kvm_is_forced_emulation_enabled() https://github.com/kvm-x86/linux/commit/e1ff11525d3c [4/5] KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code https://github.com/kvm-x86/linux/commit/2f2bc6af6aa8 [5/5] KVM: selftests: Randomly force emulation on x86 writes from guest code https://github.com/kvm-x86/linux/commit/87aa264cd89d