From: Sean Christopherson <seanjc@google.com>
To: David Woodhouse <dwmw2@infradead.org>
Cc: kvm@vger.kernel.org, sveith@amazon.de,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Paul Durrant <paul@xen.org>,
inux-kernel@vger.kernel.org
Subject: Re: [PATCH] KVM: x86: Refine calculation of guest wall clock to use a single TSC read
Date: Wed, 4 Oct 2023 17:00:17 -0700 [thread overview]
Message-ID: <ZR38kaj4UIqdWwJr@google.com> (raw)
In-Reply-To: <ee446c823002dc92c8ea525f21d00a9f5d27de59.camel@infradead.org>
On Sun, Oct 01, 2023, David Woodhouse wrote:
> +uint64_t kvm_get_wall_clock_epoch(struct kvm *kvm)
> +{
> + /*
> + * The guest calculates current wall clock time by adding
> + * system time (updated by kvm_guest_time_update below) to the
> + * wall clock specified here. We do the reverse here.
I would much rather this be a function comment that first explains what "epoch"
means in this context. "epoch" is a perfect fit, but I suspect it won't be all
that intuitive for many readers (definitely wasn't for me).
> + */
> +#ifdef CONFIG_X86_64
> + struct pvclock_vcpu_time_info hv_clock;
> + struct kvm_arch *ka = &kvm->arch;
> + unsigned long seq, local_tsc_khz = 0;
> + struct timespec64 ts;
> + uint64_t host_tsc;
> +
> + do {
> + seq = read_seqcount_begin(&ka->pvclock_sc);
> +
> + if (!ka->use_master_clock)
> + break;
This needs to zero local_tsc_khz, no? E.g. read everything on loop 1, but the
pvclock_sc changes because use_master_clock is disabled, and so loop 2 will bail
and the code below will consume garbage TSC/masterclock information from loop 1.
> + /* It all has to happen on the same CPU */
Define "it all", e.g. explain exactly why the cutoff for reenabling preemption is
after reading master_kernel_ns and not before, or not after kvm_get_time_scale().
> + get_cpu();
> +
> + local_tsc_khz = get_cpu_tsc_khz();
> +
> + if (local_tsc_khz &&
> + !kvm_get_walltime_and_clockread(&ts, &host_tsc))
> + local_tsc_khz = 0; /* Fall back to old method */
> +
> + hv_clock.tsc_timestamp = ka->master_cycle_now;
> + hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
> +
> + put_cpu();
> + } while (read_seqcount_retry(&ka->pvclock_sc, seq));
> +
> + /*
> + * If the conditions were right, and obtaining the wallclock+TSC was
> + * successful, calculate the KVM clock at the corresponding time and
> + * subtract one from the other to get the epoch in nanoseconds.
> + */
> + if (local_tsc_khz) {
> + kvm_get_time_scale(NSEC_PER_SEC, local_tsc_khz * 1000LL,
s/1000LL/NSEC_PER_USEC?
> + &hv_clock.tsc_shift,
> + &hv_clock.tsc_to_system_mul);
> + return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec -
> + __pvclock_read_cycles(&hv_clock, host_tsc);
> + }
> +#endif
prev parent reply other threads:[~2023-10-05 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-01 17:54 [PATCH] KVM: x86: Refine calculation of guest wall clock to use a single TSC read David Woodhouse
2023-10-02 5:14 ` Dongli Zhang
2023-10-02 7:24 ` David Woodhouse
2023-10-05 0:00 ` Sean Christopherson [this message]
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=ZR38kaj4UIqdWwJr@google.com \
--to=seanjc@google.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=hpa@zytor.com \
--cc=inux-kernel@vger.kernel.org \
--cc=kvm@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=sveith@amazon.de \
--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 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.