All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>
Subject: Re: [PATCH v2 6/4] selftests: kvm: Add basic Hyper-V clocksources tests
Date: Thu, 18 Mar 2021 13:57:56 -0300	[thread overview]
Message-ID: <20210318165756.GA36190@fuller.cnet> (raw)
In-Reply-To: <20210318140949.1065740-1-vkuznets@redhat.com>

On Thu, Mar 18, 2021 at 03:09:49PM +0100, Vitaly Kuznetsov wrote:
> Introduce a new selftest for Hyper-V clocksources (MSR-based reference TSC
> and TSC page). As a starting point, test the following:
> 1) Reference TSC is 1Ghz clock.
> 2) Reference TSC and TSC page give the same reading.
> 3) TSC page gets updated upon KVM_SET_CLOCK call.
> 4) TSC page does not get updated when guest opted for reenlightenment.
> 5) Disabled TSC page doesn't get updated.
> 
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
>  tools/testing/selftests/kvm/.gitignore        |   1 +
>  tools/testing/selftests/kvm/Makefile          |   1 +
>  .../selftests/kvm/x86_64/hyperv_clock.c       | 233 ++++++++++++++++++
>  3 files changed, 235 insertions(+)
>  create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> 
> diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
> index 32b87cc77c8e..22be05c55f13 100644
> --- a/tools/testing/selftests/kvm/.gitignore
> +++ b/tools/testing/selftests/kvm/.gitignore
> @@ -9,6 +9,7 @@
>  /x86_64/evmcs_test
>  /x86_64/get_cpuid_test
>  /x86_64/kvm_pv_test
> +/x86_64/hyperv_clock
>  /x86_64/hyperv_cpuid
>  /x86_64/mmio_warning_test
>  /x86_64/platform_info_test
> diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
> index a6d61f451f88..c3672e9087d3 100644
> --- a/tools/testing/selftests/kvm/Makefile
> +++ b/tools/testing/selftests/kvm/Makefile
> @@ -41,6 +41,7 @@ LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_ha
>  TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test
>  TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test
>  TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test
> +TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
>  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
>  TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
>  TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> new file mode 100644
> index 000000000000..39d6491d8458
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> @@ -0,0 +1,233 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2021, Red Hat, Inc.
> + *
> + * Tests for Hyper-V clocksources
> + */
> +#include "test_util.h"
> +#include "kvm_util.h"
> +#include "processor.h"
> +
> +struct ms_hyperv_tsc_page {
> +	volatile u32 tsc_sequence;
> +	u32 reserved1;
> +	volatile u64 tsc_scale;
> +	volatile s64 tsc_offset;
> +} __packed;
> +
> +#define HV_X64_MSR_GUEST_OS_ID			0x40000000
> +#define HV_X64_MSR_TIME_REF_COUNT		0x40000020
> +#define HV_X64_MSR_REFERENCE_TSC		0x40000021
> +#define HV_X64_MSR_TSC_FREQUENCY		0x40000022
> +#define HV_X64_MSR_REENLIGHTENMENT_CONTROL	0x40000106
> +#define HV_X64_MSR_TSC_EMULATION_CONTROL	0x40000107
> +
> +/* Simplified mul_u64_u64_shr() */
> +static inline u64 mul_u64_u64_shr64(u64 a, u64 b)
> +{
> +	union {
> +		u64 ll;
> +		struct {
> +			u32 low, high;
> +		} l;
> +	} rm, rn, rh, a0, b0;
> +	u64 c;
> +
> +	a0.ll = a;
> +	b0.ll = b;
> +
> +	rm.ll = (u64)a0.l.low * b0.l.high;
> +	rn.ll = (u64)a0.l.high * b0.l.low;
> +	rh.ll = (u64)a0.l.high * b0.l.high;
> +
> +	rh.l.low = c = rm.l.high + rn.l.high + rh.l.low;
> +	rh.l.high = (c >> 32) + rh.l.high;
> +
> +	return rh.ll;
> +}
> +
> +static inline void nop_loop(void)
> +{
> +	int i;
> +
> +	for (i = 0; i < 1000000; i++)
> +		asm volatile("nop");
> +}
> +
> +static inline void check_tsc_msr_rdtsc(void)
> +{
> +	u64 tsc_freq, r1, r2, t1, t2;
> +	s64 delta_ns;
> +
> +	tsc_freq = rdmsr(HV_X64_MSR_TSC_FREQUENCY);
> +	GUEST_ASSERT(tsc_freq > 0);
> +
> +	/* First, check MSR-based clocksource */
> +	r1 = rdtsc();
> +	t1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
> +	nop_loop();
> +	r2 = rdtsc();
> +	t2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
> +
> +	GUEST_ASSERT(t2 > t1);
> +
> +	/* HV_X64_MSR_TIME_REF_COUNT is in 100ns */
> +	delta_ns = ((t2 - t1) * 100) - ((r2 - r1) * 1000000000 / tsc_freq);
> +	if (delta_ns < 0)
> +		delta_ns = -delta_ns;

I think this should be monotonically increasing: 

1.	r1 = rdtsc();
2.	t1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
3.	nop_loop();
4.	r2 = rdtsc();
5.	t2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);	

	=>

	r1 <= t1 <= r2 <= t2

> +
> +	/* 1% tolerance */
> +	GUEST_ASSERT(delta_ns * 100 < (t2 - t1) * 100);
> +}

Doesnt an unbounded schedule-out/schedule-in (which resembles
overloaded host) of the qemu-kvm vcpu in any of the 
points 1,2,3,4,5 break the assertion above?



  parent reply	other threads:[~2021-03-18 17:33 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
2021-03-18 15:27   ` Paolo Bonzini
2021-03-18 16:57   ` Marcelo Tosatti [this message]
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=20210318165756.GA36190@fuller.cnet \
    --to=mtosatti@redhat.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=vkuznets@redhat.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.