All of lore.kernel.org
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes
@ 2021-08-12 22:46 Babu Moger
  2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 1/2] x86: access: Fix timeout failure by limiting number of tests Babu Moger
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Babu Moger @ 2021-08-12 22:46 UTC (permalink / raw)
  To: pbonzini; +Cc: seanjc, thuth, drjones, kvm, babu.moger

This series fixes couple of unittest failures for SVM.
1.The test ./x86/access is failing with timeout.
2.The test ./x86/svm failure with infinite loop.
---
v2:
 1. Modified the check in ac_test_legal to limit the number of test
    combinations based on comments from Paolo Bonzini and Sean Christopherson.
 2. Changed the rdrand function's retry method. Kept the retry outside the
    function. Tom Lendacky commented that RDRAND instruction can sometimes
    loop forever without setting the carry flag.
  
v1:
 https://lore.kernel.org/kvm/162826604263.32391.7580736822527851972.stgit@bmoger-ubuntu/

Babu Moger (2):
      x86: access: Fix timeout failure by limiting number of tests
      nSVM: Fix NPT reserved bits test hang


 lib/x86/processor.h |   11 +++++++++++
 x86/access.c        |   11 +++++++----
 x86/svm_tests.c     |   28 ++++++++++++++++++++++++----
 3 files changed, 42 insertions(+), 8 deletions(-)

--

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [kvm-unit-tests PATCH v2 1/2] x86: access: Fix timeout failure by limiting number of tests
  2021-08-12 22:46 [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Babu Moger
@ 2021-08-12 22:47 ` Babu Moger
  2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 2/2] nSVM: Fix NPT reserved bits test hang Babu Moger
  2021-08-13 10:38 ` [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Paolo Bonzini
  2 siblings, 0 replies; 5+ messages in thread
From: Babu Moger @ 2021-08-12 22:47 UTC (permalink / raw)
  To: pbonzini; +Cc: seanjc, thuth, drjones, kvm, babu.moger

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

The test ./x86/access fails with a timeout. This is due to the number test
combination. The test cases increase exponentially as the features get
enabled. The new machine adds the feature AC_CPU_CR4_PKE. The default
timeout is 180 seconds. Seen this problem both on AMD and Intel machines.

Test fails with following messages.
#./tests/access
qemu-system-x86_64: terminating on signal 15 from pid 20050 (timeout)
FAIL access (timeout; duration=180)

This test can take about 7 minutes without timeout.
time ./tests/access
58982405 tests, 0 failures
PASS access

real    7m10.063s
user    7m9.063s
sys     0m0.309s

Fix the problem by adding a new check to limit to the number of tests. The
new a check limits the combinations with more than one reserved bits. With
this limit, the runtime goes down from 7 minutes to approximately 2 minutes.

Signed-off-by: Babu Moger <Babu.Moger@amd.com>
---
 x86/access.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/x86/access.c b/x86/access.c
index 47807cc..c71f39d 100644
--- a/x86/access.c
+++ b/x86/access.c
@@ -315,11 +315,14 @@ static _Bool ac_test_legal(ac_test_t *at)
         return false;
 
     /*
-     * Shorten the test by avoiding testing too many reserved bit combinations
+     * Shorten the test by avoiding testing too many reserved bit combinations.
+     * Skip testing multiple reserved bits to shorten the test. Reserved bit
+     * page faults are terminal and multiple reserved bits do not affect the
+     * error code; the odds of a KVM bug are super low, and the odds of actually
+     * being able to detect a bug are even lower.
      */
-    if ((F(AC_PDE_BIT51) + F(AC_PDE_BIT36) + F(AC_PDE_BIT13)) > 1)
-        return false;
-    if ((F(AC_PTE_BIT51) + F(AC_PTE_BIT36)) > 1)
+    if ((F(AC_PDE_BIT51) + F(AC_PDE_BIT36) + F(AC_PDE_BIT13) +
+        F(AC_PTE_BIT51) + F(AC_PTE_BIT36)) > 1)
         return false;
 
     return true;


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [kvm-unit-tests PATCH v2 2/2] nSVM: Fix NPT reserved bits test hang
  2021-08-12 22:46 [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Babu Moger
  2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 1/2] x86: access: Fix timeout failure by limiting number of tests Babu Moger
@ 2021-08-12 22:47 ` Babu Moger
  2021-08-13 10:38 ` [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Paolo Bonzini
  2 siblings, 0 replies; 5+ messages in thread
From: Babu Moger @ 2021-08-12 22:47 UTC (permalink / raw)
  To: pbonzini; +Cc: seanjc, thuth, drjones, kvm, babu.moger

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 |   11 +++++++++++
 x86/svm_tests.c     |   28 ++++++++++++++++++++++++----
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index a08ea1f..1e10cc3 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -531,6 +531,17 @@ static inline void sti(void)
     asm volatile ("sti");
 }
 
+static inline unsigned long long rdrand(void)
+{
+	long long r;
+
+	asm volatile("rdrand %0\n\t"
+		     "jc 1f\n\t"
+		     "mov $0, %0\n\t"
+		     "1:\n\t" : "=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..b998b24 100644
--- a/x86/svm_tests.c
+++ b/x86/svm_tests.c
@@ -2671,6 +2671,14 @@ static void _svm_npt_rsvd_bits_test(u64 *pxe, u64 pxe_rsvd_bits,  u64 efer,
 	u64 rsvd_bits;
 	int i;
 
+	/*
+	 * RDTSC or RDRAND can sometimes fail to generate a valid reserved bits
+	 */
+	if (!pxe_rsvd_bits) {
+		report_skip("svm_npt_rsvd_bits_test: Reserved bits are not valid");
+		return;
+	}
+
 	/*
 	 * Test all combinations of guest/host EFER.NX and CR4.SMEP.  If host
 	 * EFER.NX=0, use NX as the reserved bit, otherwise use the passed in
@@ -2704,11 +2712,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;
 }


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes
  2021-08-12 22:46 [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Babu Moger
  2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 1/2] x86: access: Fix timeout failure by limiting number of tests Babu Moger
  2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 2/2] nSVM: Fix NPT reserved bits test hang Babu Moger
@ 2021-08-13 10:38 ` Paolo Bonzini
  2021-08-13 14:14   ` Babu Moger
  2 siblings, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2021-08-13 10:38 UTC (permalink / raw)
  To: Babu Moger; +Cc: seanjc, thuth, drjones, kvm

On 13/08/21 00:46, Babu Moger wrote:
> This series fixes couple of unittest failures for SVM.
> 1.The test ./x86/access is failing with timeout.
> 2.The test ./x86/svm failure with infinite loop.
> ---
> v2:
>   1. Modified the check in ac_test_legal to limit the number of test
>      combinations based on comments from Paolo Bonzini and Sean Christopherson.
>   2. Changed the rdrand function's retry method. Kept the retry outside the
>      function. Tom Lendacky commented that RDRAND instruction can sometimes
>      loop forever without setting the carry flag.
>    
> v1:
>   https://lore.kernel.org/kvm/162826604263.32391.7580736822527851972.stgit@bmoger-ubuntu/
> 
> Babu Moger (2):
>        x86: access: Fix timeout failure by limiting number of tests
>        nSVM: Fix NPT reserved bits test hang
> 
> 
>   lib/x86/processor.h |   11 +++++++++++
>   x86/access.c        |   11 +++++++----
>   x86/svm_tests.c     |   28 ++++++++++++++++++++++++----
>   3 files changed, 42 insertions(+), 8 deletions(-)
> 
> --
> 

Applied, thanks.  I'm looking at a few more limits to the number of 
tests as well as optimizations to ac_emulate_access, which should reduce 
the runtime further.

Paolo


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes
  2021-08-13 10:38 ` [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Paolo Bonzini
@ 2021-08-13 14:14   ` Babu Moger
  0 siblings, 0 replies; 5+ messages in thread
From: Babu Moger @ 2021-08-13 14:14 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: seanjc, thuth, drjones, kvm



On 8/13/21 5:38 AM, Paolo Bonzini wrote:
> On 13/08/21 00:46, Babu Moger wrote:
>> This series fixes couple of unittest failures for SVM.
>> 1.The test ./x86/access is failing with timeout.
>> 2.The test ./x86/svm failure with infinite loop.
>> ---
>> v2:
>>   1. Modified the check in ac_test_legal to limit the number of test
>>      combinations based on comments from Paolo Bonzini and Sean
>> Christopherson.
>>   2. Changed the rdrand function's retry method. Kept the retry outside the
>>      function. Tom Lendacky commented that RDRAND instruction can sometimes
>>      loop forever without setting the carry flag.
>>    v1:
>>  
>> https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fkvm%2F162826604263.32391.7580736822527851972.stgit%40bmoger-ubuntu%2F&amp;data=04%7C01%7Cbabu.moger%40amd.com%7C37359356e8fa4a0c33bf08d95e4690ae%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637644479455847518%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=JFA5zk9xqFpdbASQlHyTVtpn7yBhF1mk9NFaKrllYjU%3D&amp;reserved=0
>>
>>
>> Babu Moger (2):
>>        x86: access: Fix timeout failure by limiting number of tests
>>        nSVM: Fix NPT reserved bits test hang
>>
>>
>>   lib/x86/processor.h |   11 +++++++++++
>>   x86/access.c        |   11 +++++++----
>>   x86/svm_tests.c     |   28 ++++++++++++++++++++++++----
>>   3 files changed, 42 insertions(+), 8 deletions(-)
>>
>> -- 
>>
> 
> Applied, thanks.  I'm looking at a few more limits to the number of tests
> as well as optimizations to ac_emulate_access, which should reduce the
> runtime further.

Paolo, Thanks for applying. I will test your patches and let you know.
thanks
Babu

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-13 14:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-12 22:46 [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Babu Moger
2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 1/2] x86: access: Fix timeout failure by limiting number of tests Babu Moger
2021-08-12 22:47 ` [kvm-unit-tests PATCH v2 2/2] nSVM: Fix NPT reserved bits test hang Babu Moger
2021-08-13 10:38 ` [kvm-unit-tests PATCH v2 0/2] Couple of SVM unit test fixes Paolo Bonzini
2021-08-13 14:14   ` Babu Moger

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.