Paolo Bonzini <pbonzini@xxxxxxxxxx> writes: > On 18/03/21 15:52, Vitaly Kuznetsov wrote: >> Paolo Bonzini <pbonzini@xxxxxxxxxx> 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
>From dae56d5d5c5fc7442d4dabf96a9c322acb42f458 Mon Sep 17 00:00:00 2001 From: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx> 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@xxxxxxxxxx> --- .../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