All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vitaly Kuznetsov <vkuznets@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>,
	Marcelo Tosatti <mtosatti@redhat.com>
Subject: Re: [PATCH v2 6/4] selftests: kvm: Add basic Hyper-V clocksources tests
Date: Thu, 18 Mar 2021 16:23:35 +0100	[thread overview]
Message-ID: <87zgz05vuw.fsf@vitty.brq.redhat.com> (raw)
In-Reply-To: <41897f61-9d1a-519b-e1cb-e19efa34ab55@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1315 bytes --]

Paolo Bonzini <pbonzini@redhat.com> writes:

> On 18/03/21 15:52, Vitaly Kuznetsov wrote:
>> Paolo Bonzini <pbonzini@redhat.com> writes:
>> 
>>> On 18/03/21 15:09, Vitaly Kuznetsov wrote:
>>>> +static inline void check_tsc_msr_tsc_page(struct ms_hyperv_tsc_page *tsc_page)
>>>> +{
>>>> +	u64 r1, r2, t1, t2;
>>>> +	s64 delta_ns;
>>>> +
>>>> +	/* Compare TSC page clocksource with HV_X64_MSR_TIME_REF_COUNT */
>>>> +	t1 = mul_u64_u64_shr64(rdtsc(), tsc_page->tsc_scale) + tsc_page->tsc_offset;
>>>> +	r1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
>>>> +	nop_loop();
>>>> +	t2 = mul_u64_u64_shr64(rdtsc(), tsc_page->tsc_scale) + tsc_page->tsc_offset;
>>>> +	r2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
>>>> +
>>>> +	delta_ns = ((r2 - r1) - (t2 - t1)) * 100;
>>>> +	if (delta_ns < 0)
>>>> +		delta_ns = -delta_ns;
>>>> +
>>>> +	/* 1% tolerance */
>>>> +	GUEST_ASSERT(delta_ns * 100 < (t2 - t1) * 100);
>>>> +}
>>>> +
>>>
>>> I think you should also be able to check r1 and r2 individually, not
>>> just r1 and r2.  Is that correct?
>> 
>> Right, we could've checked r1 == t1 and r2 == t2 actually (with some
>> tiny margin of course). Let me try that.
>
> np, I can do that too.  Just checking my recollection of the TLFS.
>

I already hacked it, patch attached :-) Feel free to squash it or put
your own version in.

-- 
Vitaly


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-selftests-KVM-hyper-v-check_tsc_msr_tsc_page-fix.patch --]
[-- Type: text/x-patch, Size: 1846 bytes --]

From dae56d5d5c5fc7442d4dabf96a9c322acb42f458 Mon Sep 17 00:00:00 2001
From: Vitaly Kuznetsov <vkuznets@redhat.com>
Date: Thu, 18 Mar 2021 16:21:18 +0100
Subject: [PATCH] selftests: KVM: hyper-v: check_tsc_msr_tsc_page() fix.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 .../selftests/kvm/x86_64/hyperv_clock.c       | 25 ++++++++++---------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
index 39d6491d8458..aacb6d2697da 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
@@ -82,22 +82,23 @@ static inline void check_tsc_msr_rdtsc(void)
 
 static inline void check_tsc_msr_tsc_page(struct ms_hyperv_tsc_page *tsc_page)
 {
-	u64 r1, r2, t1, t2;
-	s64 delta_ns;
+	u64 t1, t2;
 
-	/* Compare TSC page clocksource with HV_X64_MSR_TIME_REF_COUNT */
-	t1 = mul_u64_u64_shr64(rdtsc(), tsc_page->tsc_scale) + tsc_page->tsc_offset;
-	r1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
+	/*
+	 * Make TSC run for a while to make sure clocksources don't diverge
+	 * over time.
+	 */
 	nop_loop();
-	t2 = mul_u64_u64_shr64(rdtsc(), tsc_page->tsc_scale) + tsc_page->tsc_offset;
-	r2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
 
-	delta_ns = ((r2 - r1) - (t2 - t1)) * 100;
-	if (delta_ns < 0)
-		delta_ns = -delta_ns;
+	/*
+	 * Get readings from TSC page and Reference TSC clocksources and make
+	 * sure they match.
+	 */
+	t1 = mul_u64_u64_shr64(rdtsc(), tsc_page->tsc_scale) + tsc_page->tsc_offset;
+	t2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
 
-	/* 1% tolerance */
-	GUEST_ASSERT(delta_ns * 100 < (t2 - t1) * 100);
+	/* 10us tolerance */
+	GUEST_ASSERT(t2 - t1 <= 100);
 }
 
 static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_gpa)
-- 
2.30.2


  reply	other threads:[~2021-03-18 15:24 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-16 14:37 [PATCH v2 0/4] KVM: x86: hyper-v: TSC page fixes Vitaly Kuznetsov
2021-03-16 14:37 ` [PATCH v2 1/4] KVM: x86: hyper-v: Limit guest to writing zero to HV_X64_MSR_TSC_EMULATION_STATUS Vitaly Kuznetsov
2021-03-16 14:37 ` [PATCH v2 2/4] KVM: x86: hyper-v: Prevent using not-yet-updated TSC page by secondary CPUs Vitaly Kuznetsov
2021-03-18 17:02   ` Marcelo Tosatti
2021-03-18 18:04     ` Marcelo Tosatti
2021-03-18 18:05       ` Paolo Bonzini
2021-03-18 18:30         ` Marcelo Tosatti
2021-03-19  9:29           ` Vitaly Kuznetsov
2021-03-16 14:37 ` [PATCH v2 3/4] KVM: x86: hyper-v: Track Hyper-V TSC page status Vitaly Kuznetsov
2021-03-17  8:07   ` Paolo Bonzini
2021-03-17 11:19     ` [PATCH v2 5/4] KVM: x86: hyper-v: Briefly document enum hv_tsc_page_status Vitaly Kuznetsov
2021-03-16 14:37 ` [PATCH v2 4/4] KVM: x86: hyper-v: Don't touch TSC page values when guest opted for re-enlightenment Vitaly Kuznetsov
2021-03-18 14:09 ` [PATCH v2 6/4] selftests: kvm: Add basic Hyper-V clocksources tests Vitaly Kuznetsov
2021-03-18 14:26   ` Paolo Bonzini
2021-03-18 14:52     ` Vitaly Kuznetsov
2021-03-18 15:01       ` Paolo Bonzini
2021-03-18 15:23         ` Vitaly Kuznetsov [this message]
2021-03-18 15:27   ` Paolo Bonzini
2021-03-18 16:57   ` Marcelo Tosatti
2021-03-18 17:50     ` Paolo Bonzini
2021-03-18 17:55       ` Marcelo Tosatti
2021-03-19  9:35         ` Vitaly Kuznetsov

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=87zgz05vuw.fsf@vitty.brq.redhat.com \
    --to=vkuznets@redhat.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=wanpengli@tencent.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.