mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-12 10:44:06 -04:00
KVM: selftests: Rename guest_rng to kvm_rng
Rename functions prefixed with 'guest_random_' to 'kvm_random_' and the global random state variable 'guest_rng' to 'kvm_rng', as the pRNG isn't strictly limited to guest code. This will allow using the pRNG in host code without creating confusing/misleading function calls. No functional changes are intended. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Josh Hilke <jrhilke@google.com> [sean: massage changelog] Link: https://patch.msgid.link/20260626213534.3866178-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
committed by
Sean Christopherson
parent
ac050f2adc
commit
e0bd29eddf
@@ -311,7 +311,7 @@ int main(int argc, char *argv[])
|
||||
int opt;
|
||||
|
||||
/* Override the seed to be deterministic by default. */
|
||||
guest_random_seed = 1;
|
||||
kvm_random_seed = 1;
|
||||
|
||||
dirty_log_manual_caps =
|
||||
kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
|
||||
@@ -357,7 +357,7 @@ int main(int argc, char *argv[])
|
||||
p.phys_offset = strtoull(optarg, NULL, 0);
|
||||
break;
|
||||
case 'r':
|
||||
guest_random_seed = atoi_positive("Random seed", optarg);
|
||||
kvm_random_seed = atoi_positive("Random seed", optarg);
|
||||
break;
|
||||
case 's':
|
||||
p.backing_src = parse_backing_src_type(optarg);
|
||||
|
||||
@@ -121,7 +121,7 @@ static void guest_code(void)
|
||||
while (true) {
|
||||
while (!READ_ONCE(vcpu_stop)) {
|
||||
addr = guest_test_virt_mem;
|
||||
addr += (guest_random_u64(&guest_rng) % guest_num_pages)
|
||||
addr += (kvm_random_u64(&kvm_rng) % guest_num_pages)
|
||||
* guest_page_size;
|
||||
addr = align_down(addr, host_page_size);
|
||||
|
||||
|
||||
@@ -108,30 +108,30 @@ struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
|
||||
struct timespec timespec_elapsed(struct timespec start);
|
||||
struct timespec timespec_div(struct timespec ts, int divisor);
|
||||
|
||||
struct guest_random_state {
|
||||
struct kvm_random_state {
|
||||
u32 seed;
|
||||
};
|
||||
|
||||
extern u32 guest_random_seed;
|
||||
extern struct guest_random_state guest_rng;
|
||||
extern u32 kvm_random_seed;
|
||||
extern struct kvm_random_state kvm_rng;
|
||||
|
||||
struct guest_random_state new_guest_random_state(u32 seed);
|
||||
u32 guest_random_u32(struct guest_random_state *state);
|
||||
struct kvm_random_state new_kvm_random_state(u32 seed);
|
||||
u32 kvm_random_u32(struct kvm_random_state *state);
|
||||
|
||||
static inline bool __guest_random_bool(struct guest_random_state *state,
|
||||
static inline bool __kvm_random_bool(struct kvm_random_state *state,
|
||||
u8 percent)
|
||||
{
|
||||
return (guest_random_u32(state) % 100) < percent;
|
||||
return (kvm_random_u32(state) % 100) < percent;
|
||||
}
|
||||
|
||||
static inline bool guest_random_bool(struct guest_random_state *state)
|
||||
static inline bool kvm_random_bool(struct kvm_random_state *state)
|
||||
{
|
||||
return __guest_random_bool(state, 50);
|
||||
return __kvm_random_bool(state, 50);
|
||||
}
|
||||
|
||||
static inline u64 guest_random_u64(struct guest_random_state *state)
|
||||
static inline u64 kvm_random_u64(struct kvm_random_state *state)
|
||||
{
|
||||
return ((u64)guest_random_u32(state) << 32) | guest_random_u32(state);
|
||||
return ((u64)kvm_random_u32(state) << 32) | kvm_random_u32(state);
|
||||
}
|
||||
|
||||
enum vm_mem_backing_src_type {
|
||||
|
||||
@@ -55,9 +55,9 @@ static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch)
|
||||
do { \
|
||||
const typeof(mem) val = (__val); \
|
||||
\
|
||||
if (!is_forced_emulation_enabled || guest_random_bool(&guest_rng)) { \
|
||||
if (!is_forced_emulation_enabled || kvm_random_bool(&kvm_rng)) { \
|
||||
(mem) = val; \
|
||||
} else if (guest_random_bool(&guest_rng)) { \
|
||||
} else if (kvm_random_bool(&kvm_rng)) { \
|
||||
__asm__ __volatile__(KVM_FEP "mov %1, %0" \
|
||||
: "+m" (mem) \
|
||||
: "r" (val) : "memory"); \
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
|
||||
#define KVM_UTIL_MIN_PFN 2
|
||||
|
||||
u32 guest_random_seed;
|
||||
struct guest_random_state guest_rng;
|
||||
static u32 last_guest_seed;
|
||||
u32 kvm_random_seed;
|
||||
struct kvm_random_state kvm_rng;
|
||||
static u32 last_kvm_seed;
|
||||
|
||||
static size_t vcpu_mmap_sz(void);
|
||||
|
||||
@@ -515,12 +515,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus,
|
||||
slot0 = memslot2region(vm, 0);
|
||||
ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size);
|
||||
|
||||
if (guest_random_seed != last_guest_seed) {
|
||||
pr_info("Random seed: 0x%x\n", guest_random_seed);
|
||||
last_guest_seed = guest_random_seed;
|
||||
if (kvm_random_seed != last_kvm_seed) {
|
||||
pr_info("Random seed: 0x%x\n", kvm_random_seed);
|
||||
last_kvm_seed = kvm_random_seed;
|
||||
}
|
||||
guest_rng = new_guest_random_state(guest_random_seed);
|
||||
sync_global_to_guest(vm, guest_rng);
|
||||
kvm_rng = new_kvm_random_state(kvm_random_seed);
|
||||
sync_global_to_guest(vm, kvm_rng);
|
||||
|
||||
kvm_arch_vm_post_create(vm, nr_runnable_vcpus);
|
||||
|
||||
@@ -2279,8 +2279,8 @@ void __attribute((constructor)) kvm_selftest_init(void)
|
||||
sigaction(SIGILL, &sig_sa, NULL);
|
||||
sigaction(SIGFPE, &sig_sa, NULL);
|
||||
|
||||
guest_random_seed = last_guest_seed = random();
|
||||
pr_info("Random seed: 0x%x\n", guest_random_seed);
|
||||
kvm_random_seed = last_kvm_seed = random();
|
||||
pr_info("Random seed: 0x%x\n", kvm_random_seed);
|
||||
|
||||
kvm_selftest_arch_init();
|
||||
}
|
||||
|
||||
@@ -48,14 +48,14 @@ void memstress_guest_code(u32 vcpu_idx)
|
||||
{
|
||||
struct memstress_args *args = &memstress_args;
|
||||
struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx];
|
||||
struct guest_random_state rand_state;
|
||||
struct kvm_random_state rand_state;
|
||||
gva_t gva;
|
||||
u64 pages;
|
||||
u64 addr;
|
||||
u64 page;
|
||||
int i;
|
||||
|
||||
rand_state = new_guest_random_state(guest_random_seed + vcpu_idx);
|
||||
rand_state = new_kvm_random_state(kvm_random_seed + vcpu_idx);
|
||||
|
||||
gva = vcpu_args->gva;
|
||||
pages = vcpu_args->pages;
|
||||
@@ -69,13 +69,13 @@ void memstress_guest_code(u32 vcpu_idx)
|
||||
|
||||
for (i = 0; i < pages; i++) {
|
||||
if (args->random_access)
|
||||
page = guest_random_u32(&rand_state) % pages;
|
||||
page = kvm_random_u32(&rand_state) % pages;
|
||||
else
|
||||
page = i;
|
||||
|
||||
addr = gva + (page * args->guest_page_size);
|
||||
|
||||
if (__guest_random_bool(&rand_state, args->write_percent))
|
||||
if (__kvm_random_bool(&rand_state, args->write_percent))
|
||||
*(u64 *)addr = 0x0123456789ABCDEF;
|
||||
else
|
||||
READ_ONCE(*(u64 *)addr);
|
||||
|
||||
@@ -30,13 +30,13 @@ void __attribute__((used)) expect_sigbus_handler(int signum)
|
||||
* Park-Miller LCG using standard constants.
|
||||
*/
|
||||
|
||||
struct guest_random_state new_guest_random_state(u32 seed)
|
||||
struct kvm_random_state new_kvm_random_state(u32 seed)
|
||||
{
|
||||
struct guest_random_state s = {.seed = seed};
|
||||
struct kvm_random_state s = {.seed = seed};
|
||||
return s;
|
||||
}
|
||||
|
||||
u32 guest_random_u32(struct guest_random_state *state)
|
||||
u32 kvm_random_u32(struct kvm_random_state *state)
|
||||
{
|
||||
state->seed = (u64)state->seed * 48271 % ((u32)(1 << 31) - 1);
|
||||
return state->seed;
|
||||
|
||||
@@ -34,7 +34,7 @@ static void validate_buffers(void)
|
||||
|
||||
static void ____test_sev_dbg(struct kvm_vm *vm, int i, int j, int nr_bytes)
|
||||
{
|
||||
u8 pattern = guest_random_u32(&guest_rng);
|
||||
u8 pattern = kvm_random_u32(&kvm_rng);
|
||||
|
||||
if (i + nr_bytes > BUFFER_SIZE || j + nr_bytes > BUFFER_SIZE)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user