Re: [PATCH] KVM: x86/tsc: Update guest tsc_offset again before vcpu first runs

Oliver Upton
2023-06-30T17:24:46Z

On Fri, Jun 30, 2023 at 05:21:41PM +0000, Oliver Upton wrote:
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index c30364152fe6..43d40f058a41 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -2721,14 +2721,14 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
> > * kvm_clock stable after CPU hotplug
> > */
> > synchronizing = true;
> > - } else {
> > + } else if (kvm_vcpu_has_run(vcpu)) {
> > u64 tsc_exp = kvm->arch.last_tsc_write +
> > nsec_to_cycles(vcpu, elapsed);
> > u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
> > /*
> > * Special case: TSC write with a small delta (1 second)
> > - * of virtual cycle time against real time is
> > - * interpreted as an attempt to synchronize the CPU.
> > + * of virtual cycle time against real time on a running
> > + * vCPU is interpreted as an attempt to synchronize.
> > */
> > synchronizing = data < tsc_exp + tsc_hz &&
> > data + tsc_hz > tsc_exp;
>
> This would break existing save/restore patterns for the TSC. QEMU relies
> on KVM synchronizing the TSCs when restoring a VM, since it cannot
> snapshot the TSC values of all the vCPUs in a single instant. It instead
> tries to save the TSCs at roughly the same time [*], which KVM detects
> on the target and gets everything back in sync. Can't wait to see when
> this heuristic actually breaks :)

Of course, forgot to actually include the link.

[*] https://gitlab.com/qemu-project/qemu/-/blob/408015a97dbe48a9dde8c0d2526c9312691952e7/target/i386/kvm/kvm.c#L249

--
Thanks,
Oliver