Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Josh Hilke <jrhilke@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 David Matlack <dmatlack@google.com>,
	Alex Williamson <alex@shazbot.org>
Subject: Re: [PATCH v3 05/16] KVM: selftests: Add helper to generate random u64 in range [min,max]
Date: Tue, 26 May 2026 18:58:20 -0700	[thread overview]
Message-ID: <ahZPvCt_8J86Ak3F@google.com> (raw)
In-Reply-To: <20260421231557.1254270-6-jrhilke@google.com>

On Tue, Apr 21, 2026, Josh Hilke wrote:
> Introduce kvm_random_u64_in_range(state, min, max). This function
> returns a random u64 in the inclusive range of [min, max] using a struct
> kvm_random_state.
> 
> Suggested-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Josh Hilke <jrhilke@google.com>
> ---
>  tools/testing/selftests/kvm/include/test_util.h |  4 ++++
>  tools/testing/selftests/kvm/lib/test_util.c     | 16 ++++++++++++++++
>  2 files changed, 20 insertions(+)
> 
> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> index ae39c4293b9a..584a4ab43db5 100644
> --- a/tools/testing/selftests/kvm/include/test_util.h
> +++ b/tools/testing/selftests/kvm/include/test_util.h
> @@ -132,6 +132,10 @@ static inline uint64_t kvm_random_u64(struct kvm_random_state *state)
>  	return ((uint64_t)kvm_random_u32(state) << 32) | kvm_random_u32(state);
>  }
>  
> +/* Returns a random u64 in the inclusive range [min, max] */

Put the comment above the definition (But thank you for writing it!  I had typed
up a response asking for exactly this, before I realized it was there, just on
the declaration).

> +uint64_t kvm_random_u64_in_range(struct kvm_random_state *state, uint64_t min,
> +				 uint64_t max);
> +
>  enum vm_mem_backing_src_type {
>  	VM_MEM_SRC_ANONYMOUS,
>  	VM_MEM_SRC_ANONYMOUS_THP,
> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
> index e342d9fb4771..23d543a9053e 100644
> --- a/tools/testing/selftests/kvm/lib/test_util.c
> +++ b/tools/testing/selftests/kvm/lib/test_util.c
> @@ -42,6 +42,22 @@ uint32_t kvm_random_u32(struct kvm_random_state *state)
>  	return state->seed;
>  }
>  
> +uint64_t kvm_random_u64_in_range(struct kvm_random_state *state, uint64_t min, uint64_t max)

Wrap.

> +{
> +	uint64_t value;
> +	uint64_t range;
> +
> +	TEST_ASSERT(min <= max, "min (0x%lx) cannot be greater than max (0x%lx)", min, max);

LOL, that's a rather hilarious error message.  Partly because it's so pointless
(gotta love TEST_ASSERT()), partly because @min obviously _can_ be greater than
@max :-)

The "min <= max" that gets printed is pretty darn self-explanatory, maybe take
the opportunity to throw shade at the user/developer?

	TEST_ASSERT(min <= max, "PEBKAC, min = 0x%lx, max = 0x%lx", min, max);

> +
> +	value = kvm_random_u64(state);
> +
> +	range = max - min;
> +	if (range == ULLONG_MAX)
> +		return value;
> +
> +	return min + (value % (range + 1));
> +}
> +
>  /*
>   * Parses "[0-9]+[kmgt]?".
>   */
> -- 
> 2.54.0.rc2.533.g4f5dca5207-goog
> 

  reply	other threads:[~2026-05-27  1:58 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-21 23:15 [PATCH v3 00/16] KVM: selftests: Link with VFIO selftests lib and test device interrupts Josh Hilke
2026-04-21 23:15 ` [PATCH v3 01/16] KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests Josh Hilke
2026-04-21 23:15 ` [PATCH v3 02/16] KVM: selftests: Add /proc/interrupts parsing helpers Josh Hilke
2026-04-21 23:15 ` [PATCH v3 03/16] KVM: selftests: Add guest read/write macros Josh Hilke
2026-05-27  1:52   ` Sean Christopherson
2026-05-28 23:01     ` Josh Hilke
2026-05-28 23:08       ` Sean Christopherson
2026-04-21 23:15 ` [PATCH v3 04/16] KVM: selftests: Rename guest_rng to kvm_rng Josh Hilke
2026-04-21 23:15 ` [PATCH v3 05/16] KVM: selftests: Add helper to generate random u64 in range [min,max] Josh Hilke
2026-05-27  1:58   ` Sean Christopherson [this message]
2026-05-28 23:01     ` Josh Hilke
2026-04-21 23:15 ` [PATCH v3 06/16] KVM: selftests: Add IRQ injection test Josh Hilke
2026-05-27  1:59   ` Sean Christopherson
     [not found]     ` <CAAdrzjs37a-hEneORNmzOvOkh4TX4Dmn6bWKEm5L4hgmkUO0wA@mail.gmail.com>
2026-05-28 23:14       ` Sean Christopherson
2026-05-27  2:10   ` Sean Christopherson
2026-05-28 23:02     ` Josh Hilke
2026-04-21 23:15 ` [PATCH v3 07/16] KVM: selftests: Verify device IRQs are routed to vCPUs Josh Hilke
2026-04-21 23:15 ` [PATCH v3 08/16] KVM: selftests: Verify IRQ affinity changes Josh Hilke
2026-04-21 23:15 ` [PATCH v3 09/16] KVM: selftests: Verify IRQs wake up halted vCPUs Josh Hilke
2026-04-21 23:15 ` [PATCH v3 10/16] KVM: selftests: Verify dynamic IRQ routing updates Josh Hilke
2026-04-21 23:15 ` [PATCH v3 11/16] KVM: selftests: Configure number of IRQs Josh Hilke
2026-04-21 23:15 ` [PATCH v3 12/16] KVM: selftests: Verify non-postable IRQ remapping Josh Hilke
2026-05-27  2:13   ` Sean Christopherson
2026-05-28 23:03     ` Josh Hilke
2026-04-21 23:15 ` [PATCH v3 13/16] KVM: selftests: Verify vCPU migration during IRQ delivery Josh Hilke
2026-05-27  2:23   ` Sean Christopherson
2026-05-28 23:05     ` Josh Hilke
2026-05-28 23:24       ` Sean Christopherson
2026-04-21 23:15 ` [PATCH v3 14/16] KVM: selftests: Print vCPU affinity on timeout Josh Hilke
2026-04-21 23:15 ` [PATCH v3 15/16] KVM: selftests: Configure number of vCPUs Josh Hilke
2026-04-21 23:15 ` [PATCH v3 16/16] KVM: selftests: Add xAPIC support Josh Hilke
2026-05-27  1:50 ` [PATCH v3 00/16] KVM: selftests: Link with VFIO selftests lib and test device interrupts 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=ahZPvCt_8J86Ak3F@google.com \
    --to=seanjc@google.com \
    --cc=alex@shazbot.org \
    --cc=dmatlack@google.com \
    --cc=jrhilke@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