From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Subject: Re: [PATCH v11 04/23] KVM: x86: Allow KVM master clock mode when TSCs are offset from each other
Date: Thu, 05 Nov 2026 17:27:06 +0000 [thread overview]
Message-ID: <sashiko-outbox-199616@kernel.org> (raw)
In-Reply-To: <20261105154955.2525419-5-dwmw2@infradead.org>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] KVM: x86: stale pvclock tsc_timestamp after __kvm_synchronize_tsc()
- [Medium] KVM: x86: missing masterclock update in kvm_track_tsc_matching()
--
commit e48b3906906a2154ee31b39c2d8cc74f2ed71a9f
Author: David Woodhouse <dwmw@amazon.co.uk>
KVM: x86: Allow KVM master clock mode when TSCs are offset from each other
This commit decouples master clock eligibility from TSC offset matching so
that KVM remains in master clock mode as long as all vCPUs share the same TSC
frequency. Per-vCPU TSC offset matching is tracked separately and used only
to gate PVCLOCK_TSC_STABLE_BIT.
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index ec8693f4827fc..38fcb1d756611 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1114,30 +1114,63 @@ static inline bool gtod_is_based_on_tsc(int mode)
> }
> #endif
>
> -static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool new_generation)
> +static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool update_mclock)
> {
> #ifdef CONFIG_X86_64
> struct kvm_arch *ka = &vcpu->kvm->arch;
> struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
> + bool prev_matched_tsc = ka->all_vcpus_matched_tsc;
>
> /*
> - * 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".
> + * Track whether all vCPUs have matching TSC offsets (for
> + * PVCLOCK_TSC_STABLE_BIT) and matching frequencies (for
> + * master clock eligibility).
> + */
> +
> + /*
> + * A new vCPU might already have incremented ->online_vcpus
> + * and cause a temporary false negative here. But will then
> + * call kvm_synchronize_tsc() from kvm_arch_vcpu_postcreate()
> + * and finish the job.
> + */
> + int online = atomic_read(&vcpu->kvm->online_vcpus);
> + /*
> + * WRITE_ONCE() pairs with the READ_ONCE() in kvm_guest_time_update(),
> + * which samples the flag locklessly (outside both tsc_write_lock and
> + * its pvclock seqcount read section); the race is benign (a stale
> + * value merely delays the PVCLOCK_TSC_STABLE_BIT broadcast to the
> + * masterclock update which is requested below).
> */
> - ka->all_vcpus_matched_tsc = (ka->nr_vcpus_matched_tsc + 1 ==
> - atomic_read(&vcpu->kvm->online_vcpus));
> + WRITE_ONCE(ka->all_vcpus_matched_tsc,
> + ka->nr_vcpus_matched_tsc >= online);
>
> - bool use_master_clock = ka->all_vcpus_matched_tsc &&
> + update_mclock |= (prev_matched_tsc != ka->all_vcpus_matched_tsc);
> +
> + /*
> + * all_vcpus_matched_freq starts true and is cleared when
> + * __kvm_synchronize_tsc() detects a frequency mismatch.
> + * Re-enable when all vCPUs have synced with matching frequency.
> + * If all offsets also match, that implies frequencies match too.
> + */
> + if (ka->all_vcpus_matched_tsc ||
> + ka->nr_vcpus_matched_freq >= online)
> + ka->all_vcpus_matched_freq = true;
> +
> + /*
> + * To use the masterclock, the host clocksource must be based on TSC
> + * and all vCPUs must have matching TSC *frequency*. Different offsets
> + * are fine — each vCPU's pvclock has its own tsc_timestamp that
> + * accounts for its offset.
> + */
> + bool use_master_clock = ka->all_vcpus_matched_freq &&
> gtod_is_based_on_tsc(gtod->clock.vclock_mode);
>
> /*
> - * 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 snapshot to be
> - * taken _after_ the new generation is created).
> + * Request a masterclock update if needed: toggling master clock,
> + * new generation requiring a fresh snapshot, or a change in
> + * all_vcpus_matched_tsc (to broadcast PVCLOCK_TSC_STABLE_BIT).
> */
> - if ((ka->use_master_clock && new_generation) ||
> + if ((ka->use_master_clock && update_mclock) ||
> (ka->use_master_clock != use_master_clock))
> kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
[Severity: Medium]
In arch/x86/kvm/x86.c, when ka->all_vcpus_matched_freq transitions from false
to true in kvm_track_tsc_matching(), should that also set update_mclock?
Suppose a VM with 3 online vCPUs already has ka->use_master_clock set to true
from vCPU creation, and userspace updates the TSC frequency on all vCPUs
before entering KVM_RUN:
1. vCPU 0 and vCPU 1 call __kvm_synchronize_tsc() at the new frequency with
divergent offsets (matched == false). ka->all_vcpus_matched_freq becomes
false and KVM_REQ_MASTERCLOCK_UPDATE is queued on vCPU 0 and vCPU 1, but
ka->use_master_clock remains true until a vCPU enters the guest and
services the request.
2. vCPU 2 then calls __kvm_synchronize_tsc() at the new frequency with an
offset matching vCPU 1 (matched == true). ka->nr_vcpus_matched_freq
reaches online (3), so ka->all_vcpus_matched_freq transitions from false
to true and local use_master_clock is true. However, ka->nr_vcpus_matched_tsc
is 2 (< 3), so ka->all_vcpus_matched_tsc stays false and update_mclock
remains false.
Because ka->use_master_clock is still true and local use_master_clock is also
true, both (ka->use_master_clock && update_mclock) and
(ka->use_master_clock != use_master_clock) evaluate to false. Could vCPU 2
then enter KVM_RUN without KVM_REQ_MASTERCLOCK_UPDATE set and use stale
master_tsc_scaling_ratio, master_tsc_mul, and master_tsc_shift values from the
old frequency?
[ ... ]
> @@ -1365,17 +1413,18 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc,
> * exact software computation in compute_guest_tsc()
> */
> kvm->arch.cur_tsc_generation++;
> + WRITE_ONCE(kvm->arch.all_vcpus_matched_tsc, false);
> + kvm->arch.nr_vcpus_matched_tsc = 0;
> kvm->arch.cur_tsc_nsec = ns;
> kvm->arch.cur_tsc_write = tsc;
> kvm->arch.cur_tsc_offset = offset;
> - kvm->arch.nr_vcpus_matched_tsc = 0;
> - kvm->arch.all_vcpus_matched_tsc = false;
> - } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) {
> + }
> +
> + if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) {
> + vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
> kvm->arch.nr_vcpus_matched_tsc++;
> }
>
> - /* Keep track of which generation this VCPU has synchronized to */
> - vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
> 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, !matched);
> }
[Severity: High]
Can __kvm_synchronize_tsc() in arch/x86/kvm/x86.c leave a vCPU's guest
pvclock tsc_timestamp stale when matched is true?
In kvm_guest_time_update(), master clock mode populates each vCPU's
hv_clock.tsc_timestamp using its own l1_tsc_offset:
if (use_master_clock) {
hv_clock.tsc_timestamp = kvm_read_l1_tsc(v, master_tsc);
hv_clock.system_time = master_ns + v->kvm->arch.kvmclock_offset;
}
When __kvm_synchronize_tsc() is called via kvm_synchronize_tsc() or
kvm_arch_tsc_set_attr(), it updates vcpu->arch.l1_tsc_offset through
kvm_vcpu_write_tsc_offset(vcpu, offset) without requesting
KVM_REQ_CLOCK_UPDATE on vcpu, and then calls:
kvm_track_tsc_matching(vcpu, !matched);
With this change, ka->use_master_clock remains true even when vCPUs have
different TSC offsets. If matched is true and ka->all_vcpus_matched_tsc does
not change its boolean value in kvm_track_tsc_matching(), update_mclock stays
false and neither KVM_REQ_MASTERCLOCK_UPDATE nor KVM_REQ_CLOCK_UPDATE is
requested.
For example, this can happen when:
1. A VM has 3 or more online vCPUs with divergent offsets: vCPU 0 sets a new
offset (matched == false), vCPU 1 synchronizes to vCPU 0's offset
(matched == true, nr_vcpus_matched_tsc becomes 2 < 3 so
all_vcpus_matched_tsc remains false), while vCPU 2 stays at a different
offset.
2. A vCPU whose l1_tsc_offset was modified by the guest via
MSR_IA32_TSC_ADJUST or MSR_IA32_TSC (which do not clear
ka->all_vcpus_matched_tsc) is later synchronized back to cur_tsc_offset by
host userspace (matched == true, while all_vcpus_matched_tsc was already
true).
Since kvm_arch_vcpu_load() also skips requesting KVM_REQ_CLOCK_UPDATE when
ka->use_master_clock is true and vcpu->cpu != -1, wouldn't the vCPU re-enter
the guest with the new hardware l1_tsc_offset in rdtsc while its pvclock
struct in guest memory still holds the old tsc_timestamp?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261105154955.2525419-1-dwmw2@infradead.org?part=4
next prev parent reply other threads:[~2026-11-05 17:27 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 15:49 [PATCH v11 00/23] KVM: x86: Cleaning up the KVM clock mess, part 2 David Woodhouse
2026-11-05 15:49 ` [PATCH v11 01/23] KVM: x86: Improve synchronization in kvm_synchronize_tsc() David Woodhouse
2026-11-05 15:49 ` [PATCH v11 02/23] KVM: x86: Kill last_tsc_{nsec,write,offset} fields David Woodhouse
2026-11-05 16:47 ` sashiko-bot
2026-11-05 15:49 ` [PATCH v11 03/23] KVM: x86: Replace nr_vcpus_matched_tsc count with all_vcpus_matched_tsc bool David Woodhouse
2026-11-05 15:49 ` [PATCH v11 04/23] KVM: x86: Allow KVM master clock mode when TSCs are offset from each other David Woodhouse
2026-11-05 17:27 ` sashiko-bot [this message]
2026-11-05 15:49 ` [PATCH v11 05/23] KVM: x86: Factor out kvm_use_master_clock() David Woodhouse
2026-11-05 15:49 ` [PATCH v11 06/23] KVM: x86: Avoid gratuitous global clock updates David Woodhouse
2026-11-05 15:49 ` [PATCH v11 07/23] KVM: x86: Avoid redundant masterclock updates from multiple vCPUs David Woodhouse
2026-11-05 15:49 ` [PATCH v11 08/23] KVM: x86: Re-synchronize TSC after KVM_SET_TSC_KHZ David Woodhouse
2026-11-05 15:49 ` [PATCH v11 09/23] KVM: x86: Cache host vclock_mode for masterclock eligibility checks David Woodhouse
2026-11-05 15:49 ` [PATCH v11 10/23] KVM: x86: Remove pvclock_gtod_data and private timekeeping code David Woodhouse
2026-11-05 15:49 ` [PATCH v11 11/23] KVM: x86: Activate master clock from kvm_arch_init_vm() David Woodhouse
2026-11-05 15:49 ` [PATCH v11 12/23] UAPI: x86: Move pvclock-abi to UAPI for x86 platforms David Woodhouse
2026-11-05 15:49 ` [PATCH v11 13/23] KVM: selftests: Use UAPI pvclock-abi.h in xen_shinfo_test David Woodhouse
2026-11-05 15:49 ` [PATCH v11 14/23] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration David Woodhouse
2026-11-05 15:49 ` [PATCH v11 15/23] KVM: selftests: Add library helpers for clock and TSC-offset ioctls David Woodhouse
2026-11-05 15:49 ` [PATCH v11 16/23] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration David Woodhouse
2026-11-05 15:49 ` [PATCH v11 17/23] KVM: selftests: Add KVM/PV clock selftest to prove timer correction David Woodhouse
2026-11-05 15:49 ` [PATCH v11 18/23] KVM: selftests: Add master clock offset test David Woodhouse
2026-11-05 15:49 ` [PATCH v11 19/23] KVM: selftests: Add Xen/generic CPUID timing leaf test David Woodhouse
2026-11-05 15:49 ` [PATCH v11 20/23] KVM: selftests: Add Xen runstate migration test David Woodhouse
2026-11-05 15:49 ` [PATCH v11 21/23] KVM: selftests: Test that the first user TSC write is honoured exactly David Woodhouse
2026-11-05 15:49 ` [PATCH v11 22/23] KVM: x86/xen: Drop open-coded fallback clock read in kvm_xen_start_timer() David Woodhouse
2026-11-05 15:49 ` [PATCH v11 23/23] KVM: x86: Use a single time snapshot in kvm_vm_ioctl_set_clock() 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=sashiko-outbox-199616@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dwmw2@infradead.org \
--cc=kvm@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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