From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CF623C2BB9 for ; Fri, 26 Jun 2026 21:35:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782509746; cv=none; b=TvI0YbMQLWNKcidIPNWGtmHYvPXzHwdnGt2ayukGxgMaIj67MKkt2f8joQ3RKlIbdTh1DbHlj1+s+CUzOodlJPXFDuTiu1zjirZiXBA72jXmWuk9cRjd7KhGZAPWJMbO1suAkGoxw5yqXe8FzHA9exAeYJmjAsPx3sLpnfU0RxU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782509746; c=relaxed/simple; bh=DpuOXkwzCOgbf4lBzoy432Js8GwAiDpEe96hUsDd1ns=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d96W16ofNBGecf9Lm9onOsdep4hM8kfI+x6NLq0KfUKyKzBYiCDReW6fYZpUYBr0p3/UZn+YJlryLXFY4PB7wfbhBmcYnn3NnKWgkLAy5D2Yf3dr4417mzp4wr/CtuBjh/kVENTixaD+3ZC186PlzHvKbFai7t9dVDLruSFWH5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qhpZYjne; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qhpZYjne" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-845d3a248f1so264402b3a.0 for ; Fri, 26 Jun 2026 14:35:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782509744; x=1783114544; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=pWc4GlXVIsd14e/yCg7QaIqalab335GasYtgDQrZMX8=; b=qhpZYjnett3p46KGJgWL360EH41kXAGvq5bPtvHbyIf++bL/uulZskhbHkLL/VJ6Sf KIZbCb6dZVmq5ArdAkU8s5CNz96mrVKBBP6fkv1So1FIWQpH+DIaxZ8Y8eGW1FLhedsD fVBVqptx/KPFpVN+huCPUQN9xzJlH2b41Ub1R83hBfAFyeX5aIyAtrDDOxnxmeM8V/sF rLLfcx92Wxelh812qmC8e7etJqSKpTPonxJnUwZSziiMLuqfQ2dWOgL1qGuJ4qWYCWaI XJCjH1pHlRnX5ABsko104vA46ItdXQP8o6VzT0hRUmw7Ll1ZWh7gEXRyIsWAeDofH+GA VBUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782509744; x=1783114544; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pWc4GlXVIsd14e/yCg7QaIqalab335GasYtgDQrZMX8=; b=ODD3vqanwHsFPfMLTyK37VA90RQ/eVJftgX/DuWlsvb4feWN643PZjWvBobIr2nOq0 vLwR/uWKoXaE2ycTVuB8+X+iCgn4Dhl8S0kUkZatX4k2q6H1W2HbB4sOZL/782qYm7qM 1BcwAKYkjpc71rm9/biNzmaAC3aA8Y+gUQiHsogDwiLqibTuLtXj5jeWMwQcAXWa07hi TKd8GMwLVgFXmIIVQ23MhWnJDhm+oY9TLVuQ8o2wSGOYfBDdODT+3zmjhm6OdHO/eWpC xlqe8NAx7b6BaXqT1mjpkc3z03x01qyjrYu5G2KQi+t+MCvcTPxI/kp20kJfpzwOXlCi bGjg== X-Forwarded-Encrypted: i=1; AHgh+RpHEz+DzkXKEemAC23Xc+mFhJG/KwQUwTjubhqyKNB4d2ZkHd1Z6iW42IYhzI1hFIerPX5257ffsyHeTsc=@vger.kernel.org X-Gm-Message-State: AOJu0YwtFrsr+sgEyvCojGQ4iFB2d32URNO2HT8SfbC35dpQbi54RpK8 bru1f8WbyNRt668V8eU/QUPrwHITJPxJ7ohHWQec+bz6VF6gwb6rs4maDsMKgjDePcD0uJIRH42 P51PnOg== X-Received: from pfbmb6.prod.google.com ([2002:a05:6a00:7606:b0:842:723a:7919]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:189b:b0:83e:f228:b112 with SMTP id d2e1a72fcca58-845b3b8e2ddmr9010923b3a.34.1782509743991; Fri, 26 Jun 2026 14:35:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 26 Jun 2026 14:35:19 -0700 In-Reply-To: <20260626213534.3866178-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260626213534.3866178-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260626213534.3866178-7-seanjc@google.com> Subject: [PATCH v8 06/20] KVM: selftests: Add helper to generate random u64 in range [min,max] From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, David Matlack , Josh Hilke Content-Type: text/plain; charset="UTF-8" From: Josh Hilke 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 Signed-off-by: Josh Hilke Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/test_util.h | 3 +++ tools/testing/selftests/kvm/lib/test_util.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index 44c0104d60ac..d64c8a228207 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -134,6 +134,9 @@ static inline u64 kvm_random_u64(struct kvm_random_state *state) return ((u64)kvm_random_u32(state) << 32) | kvm_random_u32(state); } +u64 kvm_random_u64_in_range(struct kvm_random_state *state, u64 min, + u64 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 e98ca7ef439c..e208a57f190c 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -42,6 +42,24 @@ u32 kvm_random_u32(struct kvm_random_state *state) return state->seed; } +/* Returns a random u64 in the inclusive range [min, max] */ +u64 kvm_random_u64_in_range(struct kvm_random_state *state, u64 min, + u64 max) +{ + u64 value; + u64 range; + + 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.55.0.rc0.799.gd6f94ed593-goog