mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: paul@xen.org, kvm@vger.kernel.org,
	linux-kernel <linux-kernel@vger.kernel.org>
Cc: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH v3] KVM: x86/xen: improve accuracy of Xen timers
Date: Tue, 16 Jan 2024 12:17:42 +0100	[thread overview]
Message-ID: <78d43f46d3a0e6368cdcc28a67a102da8599034f.camel@infradead.org> (raw)
In-Reply-To: <f80753b3-2e68-487c-8304-fe801534748a@gmail.com>

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

On Fri, 2023-12-15 at 09:07 +0000, Durrant, Paul wrote:
> On 14/12/2023 16:54, David Woodhouse wrote:
> > From: David Woodhouse <dwmw@amazon.co.uk>
> > 
> > A test program such as http://david.woodhou.se/timerlat.c confirms user
> > reports that timers are increasingly inaccurate as the lifetime of a
> > guest increases. Reporting the actual delay observed when asking for
> > 100µs of sleep, it starts off OK on a newly-launched guest but gets
> > worse over time, giving incorrect sleep times:
> > 
> > root@ip-10-0-193-21:~# ./timerlat -c -n 5
> > 00000000 latency 103243/100000 (3.2430%)
> > 00000001 latency 103243/100000 (3.2430%)
> > 00000002 latency 103242/100000 (3.2420%)
> > 00000003 latency 103245/100000 (3.2450%)
> > 00000004 latency 103245/100000 (3.2450%)
> > 
> > The biggest problem is that get_kvmclock_ns() returns inaccurate values
> > when the guest TSC is scaled. The guest sees a TSC value scaled from the
> > host TSC by a mul/shift conversion (hopefully done in hardware). The
> > guest then converts that guest TSC value into nanoseconds using the
> > mul/shift conversion given to it by the KVM pvclock information.
> > 
> > But get_kvmclock_ns() performs only a single conversion directly from
> > host TSC to nanoseconds, giving a different result. A test program at
> > http://david.woodhou.se/tsdrift.c demonstrates the cumulative error
> > over a day.
> > 
> > It's non-trivial to fix get_kvmclock_ns(), although I'll come back to
> > that. The actual guest hv_clock is per-CPU, and *theoretically* each
> > vCPU could be running at a *different* frequency. But this patch is
> > needed anyway because...
> > 
> > The other issue with Xen timers was that the code would snapshot the
> > host CLOCK_MONOTONIC at some point in time, and then... after a few
> > interrupts may have occurred, some preemption perhaps... would also read
> > the guest's kvmclock. Then it would proceed under the false assumption
> > that those two happened at the *same* time. Any time which *actually*
> > elapsed between reading the two clocks was introduced as inaccuracies
> > in the time at which the timer fired.
> > 
> > Fix it to use a variant of kvm_get_time_and_clockread(), which reads the
> > host TSC just *once*, then use the returned TSC value to calculate the
> > kvmclock (making sure to do that the way the guest would instead of
> > making the same mistake get_kvmclock_ns() does).
> > 
> > Sadly, hrtimers based on CLOCK_MONOTONIC_RAW are not supported, so Xen
> > timers still have to use CLOCK_MONOTONIC. In practice the difference
> > between the two won't matter over the timescales involved, as the
> > *absolute* values don't matter; just the delta.
> > 
> > This does mean a new variant of kvm_get_time_and_clockread() is needed;
> > called kvm_get_monotonic_and_clockread() because that's what it does.
> > 
> > Fixes: 536395260582 ("KVM: x86/xen: handle PV timers oneshot mode")
> > Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
> > ---
> > v3:
> >     • Rebase and repost.
> > 
> > v2:
> >     • Fall back to get_kvmclock_ns() if vcpu-arch.hv_clock isn't set up
> >       yet, with a big comment explaining why that's actually OK.
> >     • Fix do_monotonic() *not* to add the boot time offset.
> >     • Rename do_monotonic_raw() → do_kvmclock_base() and add a comment
> >       to make it clear that it *does* add the boot time offset. That
> >       was just left as a bear trap for the unwary developer, wasn't it?
> > 
> >    arch/x86/kvm/x86.c |  61 +++++++++++++++++++++--
> >    arch/x86/kvm/x86.h |   1 +
> >    arch/x86/kvm/xen.c | 121 ++++++++++++++++++++++++++++++++++-----------
> >    3 files changed, 149 insertions(+), 34 deletions(-)
> > 
> 
> Reviewed-by: Paul Durrant <paul@xen.org>

Ping?


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]

  reply	other threads:[~2024-01-16 11:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 16:54 David Woodhouse
2023-12-15  9:07 ` Durrant, Paul
2024-01-16 11:17   ` David Woodhouse [this message]
2024-01-16 16:53     ` Sean Christopherson
2024-02-07 16:08 ` Sean Christopherson

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=78d43f46d3a0e6368cdcc28a67a102da8599034f.camel@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paul@xen.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®