Skip to content

Commit ea09ace

Browse files
committed
KVM: selftests: Print the seed for the guest pRNG iff it has changed
Print the guest's random seed during VM creation if and only if the seed has changed since the seed was last printed. The vast majority of tests, if not all tests at this point, set the seed during test initialization and never change the seed, i.e. printing it every time a VM is created is useless noise. Snapshot and print the seed during early selftest init to play nice with tests that use the kselftests harness, at the cost of printing an unused seed for tests that change the seed during test-specific initialization, e.g. dirty_log_perf_test. The kselftests harness runs each testcase in a separate process that is forked from the original process before creating each testcase's VM, i.e. waiting until first VM creation will result in the seed being printed by each testcase despite it never changing. And long term, the hope/goal is that setting the seed will be handled by the core framework, i.e. that the dirty_log_perf_test wart will naturally go away. Reported-by: Yi Lai <[email protected]> Reported-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 17019d5 commit ea09ace

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
uint32_t guest_random_seed;
2323
struct guest_random_state guest_rng;
24+
static uint32_t last_guest_seed;
2425

2526
static int vcpu_mmap_sz(void);
2627

@@ -434,7 +435,10 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
434435
slot0 = memslot2region(vm, 0);
435436
ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size);
436437

437-
pr_info("Random seed: 0x%x\n", guest_random_seed);
438+
if (guest_random_seed != last_guest_seed) {
439+
pr_info("Random seed: 0x%x\n", guest_random_seed);
440+
last_guest_seed = guest_random_seed;
441+
}
438442
guest_rng = new_guest_random_state(guest_random_seed);
439443
sync_global_to_guest(vm, guest_rng);
440444

@@ -2319,7 +2323,8 @@ void __attribute((constructor)) kvm_selftest_init(void)
23192323
/* Tell stdout not to buffer its content. */
23202324
setbuf(stdout, NULL);
23212325

2322-
guest_random_seed = random();
2326+
guest_random_seed = last_guest_seed = random();
2327+
pr_info("Random seed: 0x%x\n", guest_random_seed);
23232328

23242329
kvm_selftest_arch_init();
23252330
}

0 commit comments

Comments
 (0)