From: Sean Christopherson <seanjc@google.com>
To: Simon Veith <sveith@amazon.de>
Cc: dwmw2@infradead.org, dff@amazon.com, jmattson@google.com,
joro@8bytes.org, kvm@vger.kernel.org, oupton@google.com,
pbonzini@redhat.com, tglx@linutronix.de, vkuznets@redhat.com,
wanpengli@tencent.com, David Woodhouse <dwmw@amazon.co.uk>
Subject: Re: [PATCH 1/2] KVM: x86: add KVM clock time reference arg to kvm_write_tsc()
Date: Fri, 29 Jul 2022 21:14:19 +0000 [thread overview]
Message-ID: <YuRNq1I8X5QsCW5l@google.com> (raw)
In-Reply-To: <20220707164326.394601-1-sveith@amazon.de>
On Thu, Jul 07, 2022, Simon Veith wrote:
> The Time Stamp Counter (TSC) value register can be set to an absolute
> value using the KVM_SET_MSRS ioctl, which calls kvm_synchronize_tsc()
> internally.
>
> Since this is a per-vCPU register, and vCPUs are often managed by
> separate threads, setting a uniform TSC value across all vCPUs is
> challenging: After live migration, for example, the TSC value may need
> to be adjusted to account for the migration downtime. Ideally, we would
> want each vCPU to be adjusted by the same offset; but if we compute the
> offset centrally, the TSC value may become out of date due to scheduling
> delays by the time that each vCPU thread gets around to issuing
> KVM_SET_MSRS.
>
> In preparation for the next patch, this change adds an optional, KVM
> clock based time reference argument to kvm_synchronize_tsc(). This
> argument, if present, is understood to mean "the TSC value being written
> was valid at this corresponding KVM clock time point".
Given that commit 828ca89628bf ("KVM: x86: Expose TSC offset controls to userspace")
was merged less than a year ago, it would be helpful to explicitly call out why
KVM_VCPU_TSC_CTRL doesn't work, and why that sub-ioctl can't be extended/modified to
make it work.
> kvm_synchronize_tsc() will then use this clock reference to adjust the
> TSC value being written for any delays that have been incurred since the
> provided TSC value was valid.
>
> Co-developed-by: David Woodhouse <dwmw@amazon.co.uk>
Needs David's SOB. And this patch should be squashed with the next patch. The
kvm_ns != NULL path is dead code here, e.g. even if the logic is wildly broken,
bisection will blame the patch that actually passes a non-null reference. Neither
patch is big enough to warrant splitting in this way.
And more importantly, the next patch's changelog provides a thorough description
of what it's doing, but there's very little in there that describes _why_ KVM
wants to provide this functionality.
> Signed-off-by: Simon Veith <sveith@amazon.de>
> ---
> arch/x86/kvm/x86.c | 22 +++++++++++++++++-----
> 1 file changed, 17 insertions(+), 5 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 1910e1e78b15..a44d083f1bf9 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -2629,7 +2629,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc,
> kvm_track_tsc_matching(vcpu);
> }
>
> -static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
> +static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data, u64 *kvm_ns)
> {
> struct kvm *kvm = vcpu->kvm;
> u64 offset, ns, elapsed;
> @@ -2638,12 +2638,24 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data)
> bool synchronizing = false;
>
> raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
> - offset = kvm_compute_l1_tsc_offset(vcpu, data);
> ns = get_kvmclock_base_ns();
> +
> + if (kvm_ns) {
> + /*
> + * We have been provided a KVM clock reference time point at
Avoid pronouns, e.g. there's no need to say "We have been provided", just state
what kvm_ns is and how it's used.
> + * which this TSC value was correct.
> + * Use this time point to compensate for any delays that were
> + * incurred since that TSC value was valid.
> + */
> + s64 delta_ns = ns + vcpu->kvm->arch.kvmclock_offset - *kvm_ns;
> + data += nsec_to_cycles(vcpu, (u64)delta_ns);
> + }
next prev parent reply other threads:[~2022-07-29 21:14 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-22 19:18 [PATCH] Documentation: KVM: Describe guest TSC scaling in migration algorithm Franke, Daniel
2022-03-22 21:53 ` Oliver Upton
2022-03-23 12:35 ` David Woodhouse
2022-03-23 16:21 ` Oliver Upton
2022-03-25 9:03 ` David Woodhouse
2022-03-25 17:47 ` Oliver Upton
2022-03-29 14:19 ` Thomas Gleixner
2022-03-29 16:02 ` Oliver Upton
2022-03-29 19:34 ` Thomas Gleixner
2022-06-30 11:58 ` David Woodhouse
2022-07-05 14:43 ` David Woodhouse
2022-07-07 16:43 ` [PATCH 1/2] KVM: x86: add KVM clock time reference arg to kvm_write_tsc() Simon Veith
2022-07-07 16:43 ` [PATCH 2/2] KVM: x86: add KVM_VCPU_TSC_VALUE attribute Simon Veith
2022-07-29 21:21 ` Sean Christopherson
2022-07-29 21:14 ` Sean Christopherson [this message]
2023-02-02 16:35 ` [PATCH 1/2] KVM: x86: add KVM clock time reference arg to kvm_write_tsc() David Woodhouse
2023-02-02 16:59 ` [PATCH v2] KVM: x86: add KVM_VCPU_TSC_VALUE attribute Simon Veith
2023-03-15 19:57 ` Sean Christopherson
2023-03-23 23:26 ` David Woodhouse
2023-03-24 11:22 ` Paolo Bonzini
2023-03-24 13:08 ` David Woodhouse
2023-09-13 14:08 ` David Woodhouse
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=YuRNq1I8X5QsCW5l@google.com \
--to=seanjc@google.com \
--cc=dff@amazon.com \
--cc=dwmw2@infradead.org \
--cc=dwmw@amazon.co.uk \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=oupton@google.com \
--cc=pbonzini@redhat.com \
--cc=sveith@amazon.de \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.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 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.