kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Zelin Deng <zelin.deng@linux.alibaba.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	Wanpeng Li <wanpengli@tencent.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, x86@kernel.org
Subject: Re: [PATCH] Guest system time jumps when new vCPUs is hot-added
Date: Thu, 29 Apr 2021 10:46:49 +0200	[thread overview]
Message-ID: <875z057a12.ffs@nanos.tec.linutronix.de> (raw)
In-Reply-To: <e33920a0-24bc-fa40-0a23-c2eb5693f85d@linux.alibaba.com>

On Thu, Apr 29 2021 at 07:24, Zelin Deng wrote:
> On 2021/4/28 下午5:00, Thomas Gleixner wrote:
>> On Wed, Apr 28 2021 at 10:22, Zelin Deng wrote:
>>> [   85.101228] TSC ADJUST compensate: CPU1 observed 169175101528 warp. Adjust: 169175101528
>>> [  141.513496] TSC ADJUST compensate: CPU1 observed 166 warp. Adjust: 169175101694
>> Why is TSC_ADJUST on CPU1 different from CPU0 in the first place?
>
> Per my understanding when vCPU is created by KVM, it's tsc_offset = 0 - 
> host rdtsc() meanwhile TSC_ADJUST is 0.
>
> Assume vCPU0 boots up with tsc_offset0, after 10000 tsc cycles, hotplug 
> via "virsh setvcpus" creates a new vCPU1 whose tsc_offset1 should be 
> about tsc_offset0 - 10000.  Therefore there's 10000 tsc warp between 
> rdtsc() in guest of vCPU0 and vCPU1, check_tsc_sync_target() when vCPU1 
> gets online will set TSC_ADJUST for vCPU1.
>
> Did I miss something?

Yes. The above is wrong.

The host has to ensure that the TSC of the vCPUs is in sync and if it
exposes TSC_ADJUST then that should be 0 and nothing else. The TSC
in a guest vCPU is

  hostTSC + host_TSC_ADJUST + vcpu_TSC_OFFSET + vcpu_guest_TSC_ADJUST

The mechanism the host has to use to ensure that the guest vCPUs are
exposing the same time is vcpu_TSC_OFFSET and nothing else. And
vcpu_TSC_OFFSET is the same for all vCPUs of a guest.

Now there is another issue when vCPU0 and vCPU1 are on different
'sockets' via the topology information provided by the hypervisor.

Because we had quite some issues in the past where TSCs on a single
socket were perfectly fine, but between sockets they were skewed, we
have a sanity check there. What it does is:

     if (cpu_is_first_on_non_boot_socket(cpu))
     	validate_synchronization_with_boot_socket()

And that validation expects that the CPUs involved run in a tight loop
concurrently so the TSC readouts which happen on both can be reliably
compared.

But this cannot be guaranteed on vCPUs at all, because the host can
schedule out one or both at any point during that synchronization check.

A two socket guest setup needs to have information from the host that
TSC is usable and that the socket sync check can be skipped. Anything
else is just doomed to fail in hard to diagnose ways.

Thanks,

        tglx

  reply	other threads:[~2021-04-29  8:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-28  2:22 [PATCH] Guest system time jumps when new vCPUs is hot-added Zelin Deng
2021-04-28  2:22 ` [PATCH] KVM: x86: Update vCPU's hv_clock before back to guest when tsc_offset is adjusted Zelin Deng
2021-04-28  9:00 ` [PATCH] Guest system time jumps when new vCPUs is hot-added Thomas Gleixner
2021-04-28  9:09   ` Thomas Gleixner
2021-04-28 23:24   ` Zelin Deng
2021-04-29  8:46     ` Thomas Gleixner [this message]
2021-04-29  9:38       ` Zelin Deng
2021-04-29 16:02         ` Thomas Gleixner
2021-04-29 22:40           ` Zelin Deng
2021-09-06 11:22 ` Paolo Bonzini

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=875z057a12.ffs@nanos.tec.linutronix.de \
    --to=tglx@linutronix.de \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=wanpengli@tencent.com \
    --cc=x86@kernel.org \
    --cc=zelin.deng@linux.alibaba.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 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).