From: Paolo Bonzini <pbonzini@redhat.com>
To: Marcelo Tosatti <mtosatti@redhat.com>, kvm@vger.kernel.org
Subject: Re: KVM: x86: switch KVMCLOCK base to monotonic raw clock
Date: Fri, 1 Nov 2019 01:09:50 +0100 [thread overview]
Message-ID: <08c9fce5-90ef-222d-ed86-e337f912b4a8@redhat.com> (raw)
In-Reply-To: <20191028143619.GA14370@amt.cnet>
On 28/10/19 15:36, Marcelo Tosatti wrote:
>
> Commit 0bc48bea36d1 ("KVM: x86: update master clock before computing
> kvmclock_offset")
> switches the order of operations to avoid the conversion
>
> TSC (without frequency correction) ->
> system_timestamp (with frequency correction),
>
> which might cause a time jump.
>
> However, it leaves any other masterclock update unsafe, which includes,
> at the moment:
>
> * HV_X64_MSR_REFERENCE_TSC MSR write.
> * TSC writes.
> * Host suspend/resume.
>
> Avoid the time jump issue by using frequency uncorrected
> CLOCK_MONOTONIC_RAW clock.
>
> Its the guests time keeping software responsability
> to track and correct a reference clock such as UTC.
>
> This fixes forward time jump (which can result in
> failure to bring up a vCPU) during vCPU hotplug:
>
> Oct 11 14:48:33 storage kernel: CPU2 has been hot-added
> Oct 11 14:48:34 storage kernel: CPU3 has been hot-added
> Oct 11 14:49:22 storage kernel: smpboot: Booting Node 0 Processor 2 APIC 0x2 <-- time jump of almost 1 minute
> Oct 11 14:49:22 storage kernel: smpboot: do_boot_cpu failed(-1) to wakeup CPU#2
> Oct 11 14:49:23 storage kernel: smpboot: Booting Node 0 Processor 3 APIC 0x3
> Oct 11 14:49:23 storage kernel: kvm-clock: cpu 3, msr 0:7ff640c1, secondary cpu clock
>
> Which happens because:
>
> /*
> * Wait 10s total for a response from AP
> */
> boot_error = -1;
> timeout = jiffies + 10*HZ;
> while (time_before(jiffies, timeout)) {
> ...
> }
>
> Analyzed-by: Igor Mammedov <imammedo@redhat.com>
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 661e2bf..ff713a1 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1521,20 +1521,25 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
> }
>
> #ifdef CONFIG_X86_64
> +struct pvclock_clock {
> + int vclock_mode;
> + u64 cycle_last;
> + u64 mask;
> + u32 mult;
> + u32 shift;
> +};
> +
> struct pvclock_gtod_data {
> seqcount_t seq;
>
> - struct { /* extract of a clocksource struct */
> - int vclock_mode;
> - u64 cycle_last;
> - u64 mask;
> - u32 mult;
> - u32 shift;
> - } clock;
> + struct pvclock_clock clock; /* extract of a clocksource struct */
> + struct pvclock_clock raw_clock; /* extract of a clocksource struct */
>
> + u64 boot_ns_raw;
> u64 boot_ns;
> u64 nsec_base;
> u64 wall_time_sec;
> + u64 monotonic_raw_nsec;
> };
>
> static struct pvclock_gtod_data pvclock_gtod_data;
> @@ -1542,10 +1547,20 @@ struct pvclock_gtod_data {
> static void update_pvclock_gtod(struct timekeeper *tk)
> {
> struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
> - u64 boot_ns;
> + u64 boot_ns, boot_ns_raw;
>
> boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
>
> + /*
> + * FIXME: tk->offs_boot should be converted to CLOCK_MONOTONIC_RAW
> + * interval (that is, without frequency adjustment for that interval).
> + *
> + * Lack of this fix can cause system_timestamp to not be equal to
> + * CLOCK_MONOTONIC_RAW (which happen if the host uses
> + * suspend/resume).
> + */
This is scary. Essentially you're saying that you'd want a
CLOCK_BOOTTIME_RAW. But is this true? CLOCK_BOOTTIME only differs by
the suspend time, and that is computed directly in nanoseconds so the
different frequency of CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW does not
affect it.
Thanks,
Paolo
Paolo
next prev parent reply other threads:[~2019-11-01 0:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-28 14:36 KVM: x86: switch KVMCLOCK base to monotonic raw clock Marcelo Tosatti
2019-11-01 0:09 ` Paolo Bonzini [this message]
2019-11-01 1:35 ` Marcelo Tosatti
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=08c9fce5-90ef-222d-ed86-e337f912b4a8@redhat.com \
--to=pbonzini@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=mtosatti@redhat.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