From: Sean Christopherson <seanjc@google.com>
To: David Woodhouse <dwmw2@infradead.org>
Cc: Oliver Upton <oupton@google.com>,
kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu,
Paolo Bonzini <pbonzini@redhat.com>,
Marc Zyngier <maz@kernel.org>, Peter Shier <pshier@google.com>,
Jim Mattson <jmattson@google.com>,
David Matlack <dmatlack@google.com>,
Ricardo Koller <ricarkol@google.com>,
Jing Zhang <jingzhangos@google.com>,
Raghavendra Rao Anata <rananta@google.com>,
James Morse <james.morse@arm.com>,
Alexandru Elisei <alexandru.elisei@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
linux-arm-kernel@lists.infradead.org,
Andrew Jones <drjones@redhat.com>, Will Deacon <will@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>
Subject: Re: [PATCH v8 4/7] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK'
Date: Wed, 24 Jul 2024 15:24:39 -0700 [thread overview]
Message-ID: <ZqF_J0I7kSBjQYW6@google.com> (raw)
In-Reply-To: <efc0a84f2498c47579620ccdf53c7ccd93ca981e.camel@infradead.org>
/cast <Raise Skeleton>
On Wed, Jan 17, 2024, David Woodhouse wrote:
> On Thu, 2021-09-16 at 18:15 +0000, Oliver Upton wrote:
> >
> > @@ -5878,11 +5888,21 @@ static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp)
> > * is slightly ahead) here we risk going negative on unsigned
> > * 'system_time' when 'data.clock' is very small.
> > */
> > - if (kvm->arch.use_master_clock)
> > - now_ns = ka->master_kernel_ns;
> > + if (data.flags & KVM_CLOCK_REALTIME) {
> > + u64 now_real_ns = ktime_get_real_ns();
> > +
> > + /*
> > + * Avoid stepping the kvmclock backwards.
> > + */
> > + if (now_real_ns > data.realtime)
> > + data.clock += now_real_ns - data.realtime;
> > + }
> > +
> > + if (ka->use_master_clock)
> > + now_raw_ns = ka->master_kernel_ns;
>
> This looks wrong to me.
>
> > else
> > - now_ns = get_kvmclock_base_ns();
> > - ka->kvmclock_offset = data.clock - now_ns;
> > + now_raw_ns = get_kvmclock_base_ns();
> > + ka->kvmclock_offset = data.clock - now_raw_ns;
> > kvm_end_pvclock_update(kvm);
> > return 0;
> > }
>
> We use the host CLOCK_MONOTONIC_RAW plus the boot offset, as a
> 'kvmclock base clock', and get_kvmclock_base_ns() returns that. The KVM
> clocks for each VMs are based on this 'kvmclock base clock', each
> offset by a ka->kvmclock_offset which represents the time at which that
> VM was started — so each VM's clock starts from zero.
>
> The values of ka->master_kernel_ns and ka->master_cycle_now represent a
> single point in time, the former being the value of
> get_kvmclock_base_ns() at that moment and the latter being the host TSC
> value. In pvclock_update_vm_gtod_copy(), kvm_get_time_and_clockread()
> is used to return both values at precisely the same moment, from the
> *same* rdtsc().
>
> This allows the current 'kvmclock base clock' to be calculated at any
> moment by reading the TSC, calculating a delta to that reading from
> ka->master_cycle_now to determine how much time has elapsed since
> ka->master_kernel_ns. We can then add ka->kvmclock_offset to get the
> kvmclock for this particular VM.
>
> Now, looking at the code quoted above. It's given a kvm_clock_data
> struct which contains a value of the KVM clock which is to be set as
> the time "now", and all it does is adjust ka->kvmclock_offset
> accordingly. Which is really simple:
>
> now_raw_ns = get_kvmclock_base_ns();
> ka->kvmclock_offset = data.clock - now_raw_ns;
>
> Et voilà, now get_kvmclock_base_ns() + ka->kvmclock_offset at any given
> moment in time will result in a kvmclock value according to what was
> just set. Yay!
>
> Except... in the case where the TSC is constant, we actually set
> 'now_raw_ns' to a value that doesn't represent *now*. Instead, we set
> it to ka->master_kernel_ns which represents some point in the *past*.
> We should add the number of TSC ticks since ka->master_cycle_now if
> we're going to use that, surely?
Somewhat ironically, without the KVM_CLOCK_REALTIME goo, there's no need to
re-read TSC, because the rdtsc() in pvclock_update_vm_gtod_copy() *just* happened.
But the call to ktime_get_real_ns() could theoretically spin for a non-trivial
amount of time if the clock is being refreshed.
next prev parent reply other threads:[~2024-07-24 22:25 UTC|newest]
Thread overview: 115+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-16 18:15 [PATCH v8 0/7] KVM: x86: Add idempotent controls for migrating system counter state Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` [PATCH v8 1/7] kvm: x86: abstract locking around pvclock_update_vm_gtod_copy Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` [PATCH v8 2/7] KVM: x86: extract KVM_GET_CLOCK/KVM_SET_CLOCK to separate functions Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` [PATCH v8 3/7] KVM: x86: Fix potential race in KVM_GET_CLOCK Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-29 13:33 ` Marcelo Tosatti
2021-09-29 13:33 ` Marcelo Tosatti
2021-09-29 13:33 ` Marcelo Tosatti
2021-09-16 18:15 ` [PATCH v8 4/7] KVM: x86: Report host tsc and realtime values " Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-28 18:53 ` Marcelo Tosatti
2021-09-28 18:53 ` Marcelo Tosatti
2021-09-28 18:53 ` Marcelo Tosatti
2021-09-29 11:20 ` Paolo Bonzini
2021-09-29 11:20 ` Paolo Bonzini
2021-09-29 11:20 ` Paolo Bonzini
2021-09-29 18:56 ` Marcelo Tosatti
2021-09-29 18:56 ` Marcelo Tosatti
2021-09-29 18:56 ` Marcelo Tosatti
2021-09-30 19:21 ` Marcelo Tosatti
2021-09-30 19:21 ` Marcelo Tosatti
2021-09-30 19:21 ` Marcelo Tosatti
2021-09-30 23:02 ` Thomas Gleixner
2021-09-30 23:02 ` Thomas Gleixner
2021-09-30 23:02 ` Thomas Gleixner
2021-10-01 12:05 ` Marcelo Tosatti
2021-10-01 12:05 ` Marcelo Tosatti
2021-10-01 12:05 ` Marcelo Tosatti
2021-10-01 12:10 ` Marcelo Tosatti
2021-10-01 12:10 ` Marcelo Tosatti
2021-10-01 12:10 ` Marcelo Tosatti
2021-10-01 19:59 ` Thomas Gleixner
2021-10-01 19:59 ` Thomas Gleixner
2021-10-01 19:59 ` Thomas Gleixner
2021-10-01 21:03 ` Oliver Upton
2021-10-01 21:03 ` Oliver Upton
2021-10-01 21:03 ` Oliver Upton
2021-10-01 14:17 ` Paolo Bonzini
2021-10-01 14:17 ` Paolo Bonzini
2021-10-01 14:17 ` Paolo Bonzini
2021-10-01 14:39 ` Paolo Bonzini
2021-10-01 14:39 ` Paolo Bonzini
2021-10-01 14:39 ` Paolo Bonzini
2021-10-01 14:41 ` Paolo Bonzini
2021-10-01 14:41 ` Paolo Bonzini
2021-10-01 14:41 ` Paolo Bonzini
2021-10-01 15:39 ` Oliver Upton
2021-10-01 15:39 ` Oliver Upton
2021-10-01 15:39 ` Oliver Upton
2021-10-01 16:42 ` Paolo Bonzini
2021-10-01 16:42 ` Paolo Bonzini
2021-10-01 16:42 ` Paolo Bonzini
2024-01-17 14:28 ` David Woodhouse
2024-01-17 14:28 ` David Woodhouse
2024-07-24 22:24 ` Sean Christopherson [this message]
2024-07-25 8:24 ` [PATCH v8 4/7] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK' David Woodhouse
2021-09-16 18:15 ` [PATCH v8 5/7] kvm: x86: protect masterclock with a seqcount Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-24 16:42 ` Paolo Bonzini
2021-09-24 16:42 ` Paolo Bonzini
2021-09-24 16:42 ` Paolo Bonzini
2021-09-30 17:51 ` Marcelo Tosatti
2021-09-30 17:51 ` Marcelo Tosatti
2021-09-30 17:51 ` Marcelo Tosatti
2021-10-01 16:48 ` Paolo Bonzini
2021-10-01 16:48 ` Paolo Bonzini
2021-10-01 16:48 ` Paolo Bonzini
2021-09-16 18:15 ` [PATCH v8 6/7] KVM: x86: Refactor tsc synchronization code Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` [PATCH v8 7/7] KVM: x86: Expose TSC offset controls to userspace Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-16 18:15 ` Oliver Upton
2021-09-30 19:14 ` Marcelo Tosatti
2021-09-30 19:14 ` Marcelo Tosatti
2021-09-30 19:14 ` Marcelo Tosatti
2021-10-01 9:17 ` Paolo Bonzini
2021-10-01 9:17 ` Paolo Bonzini
2021-10-01 9:17 ` Paolo Bonzini
2021-10-01 10:32 ` Marcelo Tosatti
2021-10-01 10:32 ` Marcelo Tosatti
2021-10-01 10:32 ` Marcelo Tosatti
2021-10-01 15:12 ` Paolo Bonzini
2021-10-01 15:12 ` Paolo Bonzini
2021-10-01 15:12 ` Paolo Bonzini
2021-10-01 19:11 ` Marcelo Tosatti
2021-10-01 19:11 ` Marcelo Tosatti
2021-10-01 19:11 ` Marcelo Tosatti
2021-10-01 19:33 ` Oliver Upton
2021-10-01 19:33 ` Oliver Upton
2021-10-01 19:33 ` Oliver Upton
2021-10-04 14:30 ` Marcelo Tosatti
2021-10-04 14:30 ` Marcelo Tosatti
2021-10-04 14:30 ` Marcelo Tosatti
2021-10-04 11:44 ` Paolo Bonzini
2021-10-04 11:44 ` Paolo Bonzini
2021-10-04 11:44 ` Paolo Bonzini
2021-10-05 15:22 ` Sean Christopherson
2021-10-05 15:22 ` Sean Christopherson
2021-10-05 15:22 ` Sean Christopherson
2022-02-23 10:02 ` David Woodhouse
2022-02-23 10:02 ` David Woodhouse
2022-02-23 10:02 ` David Woodhouse
2021-09-24 16:43 ` [PATCH v8 0/7] KVM: x86: Add idempotent controls for migrating system counter state Paolo Bonzini
2021-09-24 16:43 ` Paolo Bonzini
2021-09-24 16:43 ` 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=ZqF_J0I7kSBjQYW6@google.com \
--to=seanjc@google.com \
--cc=alexandru.elisei@arm.com \
--cc=catalin.marinas@arm.com \
--cc=dmatlack@google.com \
--cc=drjones@redhat.com \
--cc=dwmw2@infradead.org \
--cc=james.morse@arm.com \
--cc=jingzhangos@google.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=maz@kernel.org \
--cc=oupton@google.com \
--cc=pbonzini@redhat.com \
--cc=pshier@google.com \
--cc=rananta@google.com \
--cc=ricarkol@google.com \
--cc=suzuki.poulose@arm.com \
--cc=will@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.