linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: Sean Christopherson <seanjc@google.com>,
	Paul Durrant <pdurrant@amazon.com>,
	 "Griffoul, Fred" <fgriffo@amazon.com>
Cc: Colin Percival <cperciva@tarsnap.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>,
	 Vitaly Kuznetsov <vkuznets@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	graf@amazon.de, Ajay Kaher <ajay.kaher@broadcom.com>,
	Alexey Makhalov <alexey.makhalov@broadcom.com>
Subject: Re: [PATCH v2 0/3] Support "generic" CPUID timing leaf as KVM guest and host
Date: Fri, 29 Aug 2025 10:50:01 +0100	[thread overview]
Message-ID: <ea0d7f43d910cee9600b254e303f468722fa355b.camel@infradead.org> (raw)
In-Reply-To: <aLDo3F3KKW0MzlcH@google.com>

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

On Thu, 2025-08-28 at 16:40 -0700, Sean Christopherson wrote:
> On Wed, Aug 27, 2025, David Woodhouse wrote:
> > when there's an *existing* hypervisor leaf which just gives the information
> > directly, which is implemented in QEMU and EC2, as well as various guests.
> 
> Can we just have the VMM do the work then?  I.e. carve out the bit and the
> leaf in KVM's ABI, but leave it to the VMM to fill in?  I'd strongly prefer not
> to hook kvm_cpuid(), as I don't like overriding userspace's CPUID entries, and
> I especially don't like that hooking kvm_cpuid() means the value can change
> throughout the lifetime of the VM, at least in theory, but in practice will only
> ever be checked by the guest during early boot.

The problem is that VMM doesn't know what TSC frequency the guest
actually gets. VMM only knows what it *asked* for, not what KVM
actually ended up configuring — which depends on the capabilities of
the hardware and the host's idea of what its actual TSC frequency is.

Hence https://git.kernel.org/torvalds/c/f422f853af036 in which we
allowed KVM to populate the value in the Xen TSC info CPUID leaves. I
was just following that precedent.

I am not *entirely* averse to ripping that out, and doing things
differently. We would have to:

 • Declare that exposing the TSC frequency to guests via CPUID is
   nonsense on crappy old hardware where it actually varies at runtime
   anyway. Partly because the guest will only check it at boot, and
   partly because that TSC has to be advertised as unreliable anyway.

 • Add a new API for the VMM to extract the actual effective frequency,
   only on 'sane' hosts.

 • Declare that we don't care that it's strictly an ABI change, and
   VMMs which used to just populate the leaf and let KVM fill it in
   for Xen guests now *have* to use the new API.

I'm actually OK with that, even the last one, because I've just noticed
that KVM is updating the *wrong* Xen leaf. 0x40000x03/2 EAX is supposed
to be the *host* TSC frequency, and the guest frequency is supposed to
be in 0x40000x03/0 ECX. And Linux as a Xen guest doesn't even use it
anyway, AFAICT.

Paul, it was your code originally; are you happy with removing it?


As we look at a new API for exposing the precise TSC scaling, I'd like
to make sure it works for VMClock (for which I am still working on
writing up proper documentation but in the meantime 
https://gitlab.com/qemu-project/qemu/-/commit/3634039b93cc5 serves as a
decent reference). In short, VMClock allows the hypervisor to provide a
pvclock-style clock with microsecond accuracy to its guests, solving
the problems of
 • All guests using external precision clocks to repeat the *same* work
   of calibrating the *same* underlying oscillator
 • ...badly, experiencing imprecision due to steal time as they do so.
 • Live migration completely disrupting the clock and causing actual
   data corruption, where precision timestamps are required for e.g.
   distributed database coherency.

In its initial implementation, the VMClock in QEMU (and EC2) only
resolves the last issue, by advertising a 'disruption' on live
migration so that the guest can know that its clock is hosed until it
manages to resync.

Now I'm trying to plumb in the actual clock information from the host,
so that migrated guests can have precision time from the moment they
arrive on the new host. There are two major use cases to consider...

1. Dedicated hosting setups will calibrate the host TSC *directly*
   against the external clock, and maybe feed it into the host kernel's
   adjtimex() almost as an afterthought. So userspace will be able to
   produce a system-wide VMClock data structure which can then be
   advertised to each guest with the appropriate TSC offset and scaling
   factor.

   For this I think we want the *actual* scaling factor to be exposed
   by KVM to userspace, not just the resulting estimated frequency.
   Unless we allow userspace just to provide the host's view and let
   KVM apply the offset/scale. Which maybe doesn't make as much sense
   in *this* setup but we might end up wanting that anyway for...

2. More traditional hosts just running Chrony/ntpd to feed the host's
   CLOCK_REALTIME with adjtimex(). For this case, there is probably
   more of an argument for letting the kernel generate the vmclock
   data — KVM already has the gtod notifier which is invoked every time
   the apparent frequency changes, and userspace has none of what it
   needs.

So... if we need KVM to be able to apply the per-VM scaling/offset
because we're going to do it all in-kernel in that second case, then we
might as well let KVM apply the per-VM scaling/offset even in the
dedicated hosting case. And then the API we use for the original CPUID
problem only needs to expose the actual effective frequency.

But if we want userspace to do more for itself, we'd need to expose the
scaling factors directly. I think...




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

  reply	other threads:[~2025-08-29  9:50 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-16 10:09 [PATCH v2 0/3] Support "generic" CPUID timing leaf as KVM guest and host David Woodhouse
2025-08-16 10:10 ` [PATCH v2 1/3] KVM: x86: Restore caching of KVM CPUID base David Woodhouse
2025-08-16 10:10 ` [PATCH v2 2/3] KVM: x86: Provide TSC frequency in "generic" timing infomation CPUID leaf David Woodhouse
2025-08-16 10:10 ` [PATCH v2 3/3] x86/kvm: Obtain TSC frequency from CPUID if present David Woodhouse
2025-08-21 16:26 ` [PATCH v2 0/3] Support "generic" CPUID timing leaf as KVM guest and host Sean Christopherson
2025-08-21 17:37   ` David Woodhouse
2025-08-21 19:27     ` Sean Christopherson
2025-08-21 20:42       ` David Woodhouse
2025-08-21 20:48         ` Sean Christopherson
2025-08-21 21:10           ` David Woodhouse
2025-08-22  1:57             ` Colin Percival
2025-08-26 19:30               ` Sean Christopherson
2025-08-27  9:30                 ` David Woodhouse
2025-08-28 23:40                   ` Sean Christopherson
2025-08-29  9:50                     ` David Woodhouse [this message]
2025-08-29 11:08                       ` Durrant, Paul
2025-08-29 11:19                         ` David Woodhouse
2025-08-29 20:36                           ` Sean Christopherson
2025-09-02  8:31                             ` David Woodhouse
2025-09-02 17:49                               ` Sean Christopherson
2025-09-02 18:23                                 ` David Woodhouse
2025-09-04 11:59                                   ` Sean Christopherson
2025-09-04 12:14                                     ` David Woodhouse
2025-09-04 13:25                                       ` Sean Christopherson
2025-09-04 13:51                                         ` David Woodhouse
2025-09-05  7:57                                           ` 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=ea0d7f43d910cee9600b254e303f468722fa355b.camel@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=ajay.kaher@broadcom.com \
    --cc=alexey.makhalov@broadcom.com \
    --cc=bp@alien8.de \
    --cc=cperciva@tarsnap.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=fgriffo@amazon.com \
    --cc=graf@amazon.de \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=pdurrant@amazon.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=vkuznets@redhat.com \
    --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;
as well as URLs for NNTP newsgroup(s).