All of lore.kernel.org
 help / color / mirror / Atom feed
From: Babu Moger <babu.moger@amd.com>
To: unlisted-recipients:; (no To-header on input)
Cc: pbonzini@redhat.com, thuth@redhat.com, drjones@redhat.com,
	kvm@vger.kernel.org, babu.moger@amd.com
Subject: [kvm-unit-tests PATCH 2/2] nSVM: Fix NPT reserved bits test hang
Date: Fri, 06 Aug 2021 11:08:44 -0500	[thread overview]
Message-ID: <162826612490.32391.15921978067790218645.stgit@bmoger-ubuntu> (raw)
In-Reply-To: <162826604263.32391.7580736822527851972.stgit@bmoger-ubuntu>

From: Babu Moger <Babu.Moger@amd.com>

SVM reserved bits tests hangs in a infinite loop. The test uses the
instruction 'rdtsc' to generate the random reserved bits. It hangs
while generating the valid reserved bits.

The AMD64 Architecture Programmers Manual Volume 2: System
Programming manual says, When using the TSC to measure elapsed time,
programmers must be aware that for some implementations, the rate at
which the TSC is incremented varies based on the processor power
management state (Pstate). For other implementations, the TSC
increment rate is fixed and is not subject to power-management
related changes in processor frequency.

In AMD gen3 machine, the rdtsc value is a P state multiplier.
Here are the rdtsc value in 10 sucessive reads.
0 rdtsc = 0x1ec92919b9710
1 rdtsc = 0x1ec92919c01f0
2 rdtsc = 0x1ec92919c0f70
3 rdtsc = 0x1ec92919c18d0
4 rdtsc = 0x1ec92919c2060
5 rdtsc = 0x1ec92919c28d0
6 rdtsc = 0x1ec92919c30b0
7 rdtsc = 0x1ec92919c5660
8 rdtsc = 0x1ec92919c6150
9 rdtsc = 0x1ec92919c7c80

This test uses the lower nibble and right shifts to generate the
valid reserved bit. It loops forever because the lower nibble is always
zero.

Fixing the issue with replacing rdrand instruction if available or
skipping the test if we cannot generate the valid reserved bits.

Signed-off-by: Babu Moger <Babu.Moger@amd.com>
---
 lib/x86/processor.h |   10 ++++++++++
 x86/svm_tests.c     |   25 +++++++++++++++++++------
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index a08ea1f..974077a 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -531,6 +531,16 @@ static inline void sti(void)
     asm volatile ("sti");
 }
 
+static inline unsigned long long rdrand(void)
+{
+	long long r;
+
+	asm volatile("1:;\n\
+	              rdrand %0;\n\
+		      jnc 1b;\n":"=r"(r));
+	return r;
+}
+
 static inline unsigned long long rdtsc(void)
 {
 	long long r;
diff --git a/x86/svm_tests.c b/x86/svm_tests.c
index 79ed48e..a2963c0 100644
--- a/x86/svm_tests.c
+++ b/x86/svm_tests.c
@@ -2704,11 +2704,23 @@ static void _svm_npt_rsvd_bits_test(u64 *pxe, u64 pxe_rsvd_bits,  u64 efer,
 
 static u64 get_random_bits(u64 hi, u64 low)
 {
-	u64 rsvd_bits;
+	unsigned retry = 5;
+	u64 rsvd_bits = 0;
+
+	if (this_cpu_has(X86_FEATURE_RDRAND)) {
+		do {
+			rsvd_bits = (rdrand() << low) & GENMASK_ULL(hi, low);
+			retry--;
+		} while (!rsvd_bits && retry);
+	}
 
-	do {
-		rsvd_bits = (rdtsc() << low) & GENMASK_ULL(hi, low);
-	} while (!rsvd_bits);
+	if (!rsvd_bits) {
+		retry = 5;
+		do {
+			rsvd_bits = (rdtsc() << low) & GENMASK_ULL(hi, low);
+			retry--;
+		} while (!rsvd_bits && retry);
+	}
 
 	return rsvd_bits;
 }
@@ -2733,10 +2745,11 @@ static void svm_npt_rsvd_bits_test(void)
 
 	/*
 	 * 4k PTEs don't have reserved bits if MAXPHYADDR >= 52, just skip the
-	 * sub-test.  The NX test is still valid, but the extra bit of coverage
+	 * sub-test. Also skip if cannot generate the valid random reserved bits.
+	 * The NX test is still valid, but the extra bit of coverage
 	 * isn't worth the extra complexity.
 	 */
-	if (cpuid_maxphyaddr() >= 52)
+	if ((cpuid_maxphyaddr() >= 52) || !get_random_bits(51, cpuid_maxphyaddr()))
 		goto skip_pte_test;
 
 	_svm_npt_rsvd_bits_test(npt_get_pte((u64)basic_guest_main),


      parent reply	other threads:[~2021-08-06 16:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-06 16:08 [kvm-unit-tests PATCH 0/2] Couple of SVM fixes Babu Moger
2021-08-06 16:08 ` [kvm-unit-tests PATCH 1/2] x86: access: Fix timeout failure by limiting number of flag combinations Babu Moger
     [not found]   ` <YQ1pA9nN6DP0veQ1@google.com>
2021-08-09 19:43     ` Babu Moger
2021-08-10 16:59       ` Babu Moger
2021-08-10 23:38         ` Babu Moger
2021-08-11  7:09           ` Paolo Bonzini
2021-08-11 16:03             ` Babu Moger
2021-08-11 16:13               ` Sean Christopherson
2021-08-11 16:43                 ` Babu Moger
2021-08-06 16:08 ` Babu Moger [this message]

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=162826612490.32391.15921978067790218645.stgit@bmoger-ubuntu \
    --to=babu.moger@amd.com \
    --cc=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=thuth@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.