mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
synced 2026-04-18 03:23:53 -04:00
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. Link: https://lore.kernel.org/r/20240314185459.2439072-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
@@ -18,6 +18,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)
|
||||
@@ -430,6 +433,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;
|
||||
@@ -2303,6 +2310,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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user