From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/5] KVM: selftests: Provide an API for getting a random bool from an RNG
Date: Thu, 14 Mar 2024 11:54:55 -0700 [thread overview]
Message-ID: <20240314185459.2439072-3-seanjc@google.com> (raw)
In-Reply-To: <20240314185459.2439072-1-seanjc@google.com>
Move 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.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/include/test_util.h | 11 +++++++++++
tools/testing/selftests/kvm/lib/memstress.c | 2 +-
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index 4b78fb7e539e..3e473058849f 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -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);
diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/selftests/kvm/lib/memstress.c
index f8bfb4988368..2d49a5643b71 100644
--- a/tools/testing/selftests/kvm/lib/memstress.c
+++ b/tools/testing/selftests/kvm/lib/memstress.c
@@ -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);
--
2.44.0.291.gc1ea87d7ee-goog
next prev parent reply other threads:[~2024-03-14 18:55 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-14 18:54 [PATCH 0/5] KVM: selftests: Introduce vcpu_arch_put_guest() Sean Christopherson
2024-03-14 18:54 ` [PATCH 1/5] KVM: selftests: Provide a global pseudo-RNG instance for all tests Sean Christopherson
2024-03-14 18:54 ` Sean Christopherson [this message]
2024-03-14 18:54 ` [PATCH 3/5] KVM: selftests: Add global snapshot of kvm_is_forced_emulation_enabled() Sean Christopherson
2024-03-14 18:54 ` [PATCH 4/5] KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code Sean Christopherson
2024-03-14 18:54 ` [PATCH 5/5] KVM: selftests: Randomly force emulation on x86 " Sean Christopherson
2024-04-29 20:45 ` [PATCH 0/5] KVM: selftests: Introduce vcpu_arch_put_guest() Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240314185459.2439072-3-seanjc@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).