public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Dongli Zhang <dongli.zhang@oracle.com>
Cc: David Woodhouse <dwmw2@infradead.org>,
	Joe Jin <joe.jin@oracle.com>,
	x86@kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, pbonzini@redhat.com,
	tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
	dave.hansen@linux.intel.com
Subject: Re: [PATCH RFC 1/1] KVM: x86: add param to update master clock periodically
Date: Mon, 16 Oct 2023 15:48:21 -0700	[thread overview]
Message-ID: <ZS29teniU3xer0Xu@google.com> (raw)
In-Reply-To: <b3721f33-d5b0-79db-8500-d6b93dded0c1@oracle.com>

On Mon, Oct 16, 2023, Dongli Zhang wrote:
> Hi Sean,
> 
> On 10/16/23 11:49, Sean Christopherson wrote:
> > Compile tested only, but the below should fix the vCPU hotplug case.  Then
> > someone (not me) just needs to figure out why kvm_xen_shared_info_init() forces
> > a masterclock update.
> > 
> > I still think we should clean up the periodic sync code, but I don't think we
> > need to periodically sync the masterclock.
> 
> This looks good to me. The core idea is to not update master clock for the
> synchronized cases.
> 
> 
> How about the negative value case? I see in the linux code it is still there?

See below.  

> (It is out of the scope of my expectation as I do not need to run vCPUs in
> different tsc freq as host)
> 
> Thank you very much!
> 
> Dongli Zhang
> 
> > 
> > ---
> >  arch/x86/kvm/x86.c | 29 ++++++++++++++++-------------
> >  1 file changed, 16 insertions(+), 13 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index c54e1133e0d3..f0a607b6fc31 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -2510,26 +2510,29 @@ static inline int gtod_is_based_on_tsc(int mode)
> >  }
> >  #endif
> >  
> > -static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
> > +static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool new_generation)
> >  {
> >  #ifdef CONFIG_X86_64
> > -	bool vcpus_matched;
> >  	struct kvm_arch *ka = &vcpu->kvm->arch;
> >  	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
> >  
> > -	vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
> > -			 atomic_read(&vcpu->kvm->online_vcpus));
> > +	/*
> > +	 * To use the masterclock, the host clocksource must be based on TSC
> > +	 * and all vCPUs must have matching TSCs.  Note, the count for matching
> > +	 * vCPUs doesn't include the reference vCPU, hence "+1".
> > +	 */
> > +	bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 ==
> > +				 atomic_read(&vcpu->kvm->online_vcpus)) &&
> > +				gtod_is_based_on_tsc(gtod->clock.vclock_mode);
> >  
> >  	/*
> > -	 * Once the masterclock is enabled, always perform request in
> > -	 * order to update it.
> > -	 *
> > -	 * In order to enable masterclock, the host clocksource must be TSC
> > -	 * and the vcpus need to have matched TSCs.  When that happens,
> > -	 * perform request to enable masterclock.
> > +	 * Request a masterclock update if the masterclock needs to be toggled
> > +	 * on/off, or when starting a new generation and the masterclock is
> > +	 * enabled (compute_guest_tsc() requires the masterclock snaphot to be
> > +	 * taken _after_ the new generation is created).
> >  	 */
> > -	if (ka->use_master_clock ||
> > -	    (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
> > +	if ((ka->use_master_clock && new_generation) ||
> > +	    (ka->use_master_clock != use_master_clock))
> >  		kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
> >  
> >  	trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
> > @@ -2706,7 +2709,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc,
> >  	vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
> >  	vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
> >  
> > -	kvm_track_tsc_matching(vcpu);
> > +	kvm_track_tsc_matching(vcpu, !matched);

If my analysis of how the negative timestamp occurred is correct, the problematic
scenario was if cur_tsc_nsec/cur_tsc_write were updated without a masterclock update.
Passing !matched for @new_generation means that KVM will force a masterclock update
if cur_tsc_nsec/cur_tsc_write are changed, i.e. prevent the negative timestamp bug.

  reply	other threads:[~2023-10-16 22:48 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-26 23:06 [PATCH RFC 1/1] KVM: x86: add param to update master clock periodically Dongli Zhang
2023-09-27  0:29 ` Joe Jin
2023-09-27  0:36   ` Dongli Zhang
2023-09-28 16:18     ` Sean Christopherson
2023-09-29 20:15       ` Dongli Zhang
2023-10-02  8:33         ` David Woodhouse
2023-10-02 16:37           ` Sean Christopherson
2023-10-02 17:17             ` Dongli Zhang
2023-10-02 18:18               ` Sean Christopherson
2023-10-02 21:06                 ` Peter Zijlstra
2023-10-02 21:16                   ` Peter Zijlstra
2023-10-02 18:16             ` David Woodhouse
2023-10-03  0:53               ` Sean Christopherson
2023-10-03  1:32                 ` Dongli Zhang
2023-10-03  1:49                   ` Sean Christopherson
2023-10-03  2:07                     ` Dongli Zhang
2023-10-03 21:00                       ` Sean Christopherson
2023-10-03  5:54                 ` David Woodhouse
2023-10-04  0:04                   ` Sean Christopherson
2023-10-04 10:01                     ` David Woodhouse
2023-10-04 18:06                       ` Sean Christopherson
2023-10-04 19:13                         ` Dongli Zhang
2023-10-11  0:20                           ` Sean Christopherson
2023-10-11  7:18                             ` David Woodhouse
2023-10-13 18:07                               ` Sean Christopherson
2023-10-13 18:21                                 ` David Woodhouse
2023-10-13 19:02                                   ` Sean Christopherson
2023-10-13 19:12                                     ` David Woodhouse
2023-10-13 20:03                                       ` Sean Christopherson
2023-10-13 20:12                                 ` Dongli Zhang
2023-10-13 23:26                                   ` Sean Christopherson
2023-10-14  9:49                                     ` David Woodhouse
2023-10-16 15:47                                       ` Dongli Zhang
2023-10-16 16:25                                         ` David Woodhouse
2023-10-16 17:04                                           ` Dongli Zhang
2023-10-16 18:49                                           ` Sean Christopherson
2023-10-16 22:04                                             ` Dongli Zhang
2023-10-16 22:48                                               ` Sean Christopherson [this message]
2023-10-17 16:18                                                 ` Dongli Zhang
2023-10-03  9:12                 ` David Woodhouse
2023-10-04  0:07                   ` Sean Christopherson
2023-10-04  8:06                     ` David Woodhouse
2023-10-03 14:29                 ` David Woodhouse
2023-10-04  0:10                   ` 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=ZS29teniU3xer0Xu@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dongli.zhang@oracle.com \
    --cc=dwmw2@infradead.org \
    --cc=joe.jin@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.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