From: David Woodhouse <dwmw2@infradead.org>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-doc@vger.kernel.org, xen-devel@lists.xenproject.org,
x86@kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Paul Durrant <paul@xen.org>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H . Peter Anvin" <hpa@zytor.com>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Juergen Gross <jgross@suse.com>,
Boris Ostrovsky <boris.ostrovsky@oracle.com>,
Jonathan Cameron <jic23@kernel.org>,
Sascha Bischoff <Sascha.Bischoff@arm.com>,
Marc Zyngier <maz@kernel.org>, Joey Gouly <joey.gouly@arm.com>,
Jack Allister <jalliste@amazon.com>,
Dongli Zhang <dongli.zhang@oracle.com>,
joe.jin@oracle.com, David Woodhouse <dwmw@amazon.co.uk>
Subject: [PATCH v12 00/24] KVM: x86: Cleaning up the KVM clock mess, part 2
Date: Sun, 8 Nov 2026 17:20:46 -0800 [thread overview]
Message-ID: <20261109012112.3415328-1-dwmw2@infradead.org> (raw)
From: David Woodhouse <dwmw@amazon.co.uk>
I love it when sashiko-bot finds things that my own local sashiko run didn't.
This is the second half of the KVM clock series which has been churning
since about 2024. The first half¹ is upstream as of 7.4-rc1 (yay!). This is
the remainder — the masterclock overhaul, and new API for fixing migration.
In v12 I've finally removed the last reason why a *guest* can force the KVM
clock out of masterclock mode (by using the old SYSTEM_TIME MSR). Even if we
do still care about that SUSE kernel from 2015, we can satisfy it just by
updating the master clock when it rewrites the MSR.
Tree: https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/kvmclock12
Since v11², fixing what the on-list review found, and more that arose out of
that:
KVM_SET_TSC_KHZ:
• Synchronizing during vCPU creation, before the vCPU is counted in
online_vcpus, corrupted the VM-wide matching state.
• Because the user_set_tsc flag was VM-wide, restoring one vCPU's TSC made
a sibling's *unrestored* value be preserved across a frequency change.
• Time spent in the ioctl between sampling the guest TSC and reinstalling
it was silently subtracted from the vCPU's clock.
• Setting the unchanged frequency perturbed the generation tracking, and a
failed call committed the rejected frequency anyway.
• An above-host-rate frequency on scaling-less hardware left the vCPU in
catchup mode forever.
The master clock:
• Making the first vCPU match the TSC generation established at VM
creation was a silent guest-visible change (its TSC became the cycles
elapsed since KVM_CREATE_VM), and handed it a stale offset if the host
suspended while the VM had no vCPUs.
• KVM_GET_CLOCK trusted a master snapshot which nothing refreshes for a
VM with no vCPUs across a host suspend, returning a discontinuity
flagged KVM_CLOCK_TSC_STABLE.
• A TSC synchronization which changed the vCPU's offset left the guest's
pvclock unrefreshed, now that offset changes no longer turn the master
clock off.
• When the last vCPU reached a new common frequency, nothing refreshed
the masterclock scaling factors.
• A pending masterclock update on a newly created vCPU wasn't serviced
until it first ran, and a vCPU whose creation was aborted part-way
stayed counted as matched forever.
• Guests using the legacy MSR_KVM_SYSTEM_TIME lost the master clock
entirely — the 2015 workaround for SUSE's 2.6.16 freshness heuristic,
which is better satisfied by sliding the reference point forward when
the legacy MSR is rewritten (new patch 24).
KVM_[GS]ET_CLOCK_GUEST:
• The guest TSC comparison mishandled wraparound.
• The frequency sanity check could underflow, rejected two hosts
calibrated exactly 1kHz apart, and hvclock_to_hz() could wrap on
absurd user-supplied mul/shift.
• The -EBUSY case was unreachable, and GET returned from inside the
seqcount read section.
Elsewhere:
• kvm_vm_ioctl_set_clock() wrapped if the host clock had legally been
set to within moments of the 1970 epoch.
• clang couldn't see that guest_now was always initialized in
kvm_xen_start_timer() on 32-bit builds (kernel test robot³).
• Comments and commit messages overclaimed in places, the
kvm_update_master_clock tracepoint still said "offsetmatched", the
tools/ uapi header mirror had drifted, and api.rst didn't document the
new KVM_SET_TSC_KHZ behaviour.
• Selftests skipped on any errno rather than just -ENODATA, could pass
on a buggy kernel under adverse scheduling, lost a whole frequency's
coverage when hardware scaling wasn't engaged, and carried a third
private copy of the pvclock arithmetic.
¹ https://lore.kernel.org/all/20260826213303.914988-1-seanjc@google.com
² https://lore.kernel.org/all/20261105154955.2525419-1-dwmw2@infradead.org
³ https://lore.kernel.org/all/202611072152.FL5QBzjH-lkp@intel.com
David Woodhouse (21):
KVM: x86: Improve synchronization in kvm_synchronize_tsc()
KVM: x86: Kill last_tsc_{nsec,write,offset} fields
KVM: x86: Replace nr_vcpus_matched_tsc count with all_vcpus_matched_tsc bool
KVM: x86: Allow KVM master clock mode when TSCs are offset from each other
KVM: x86: Factor out kvm_use_master_clock()
KVM: x86: Avoid gratuitous global clock updates
KVM: x86: Avoid redundant masterclock updates from multiple vCPUs
KVM: x86: Re-synchronize TSC after KVM_SET_TSC_KHZ
KVM: x86: Cache host vclock_mode for masterclock eligibility checks
KVM: x86: Remove pvclock_gtod_data and private timekeeping code
KVM: x86: Activate master clock from kvm_arch_init_vm()
KVM: selftests: Use UAPI pvclock-abi.h in xen_shinfo_test
KVM: selftests: Add library helpers for clock and TSC-offset ioctls
KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration
KVM: selftests: Add master clock offset test
KVM: selftests: Add Xen/generic CPUID timing leaf test
KVM: selftests: Add Xen runstate migration test
KVM: selftests: Test that the first user TSC write is honoured exactly
KVM: x86/xen: Drop open-coded fallback clock read in kvm_xen_start_timer()
KVM: x86: Use a single time snapshot in kvm_vm_ioctl_set_clock()
KVM: x86: Keep master clock for guests using legacy MSR_KVM_SYSTEM_TIME
Jack Allister (3):
UAPI: x86: Move pvclock-abi to UAPI for x86 platforms
KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration
KVM: selftests: Add KVM/PV clock selftest to prove timer correction
Documentation/virt/kvm/api.rst | 61 ++
Documentation/virt/kvm/devices/vcpu.rst | 137 ++-
Documentation/virt/kvm/locking.rst | 2 +-
MAINTAINERS | 4 +-
arch/x86/include/asm/kvm_host.h | 14 +-
arch/x86/include/uapi/asm/kvm.h | 7 +
arch/x86/include/{ => uapi}/asm/pvclock-abi.h | 27 +-
arch/x86/kvm/msrs.c | 28 +-
arch/x86/kvm/trace.h | 12 +-
arch/x86/kvm/x86.c | 957 ++++++++++++++----
arch/x86/kvm/x86.h | 2 +-
arch/x86/kvm/xen.c | 48 +-
include/uapi/linux/kvm.h | 4 +
scripts/xen-hypercalls.sh | 2 +-
tools/arch/x86/include/asm/pvclock-abi.h | 27 +-
tools/arch/x86/include/uapi/asm/kvm.h | 6 +
tools/testing/selftests/kvm/Makefile.kvm | 6 +
.../selftests/kvm/include/x86/processor.h | 50 +
.../kvm/x86/masterclock_offset_test.c | 203 ++++
.../kvm/x86/pvclock_migration_test.c | 521 ++++++++++
.../testing/selftests/kvm/x86/pvclock_test.c | 518 ++++++++++
.../selftests/kvm/x86/tsc_user_set_test.c | 153 +++
.../selftests/kvm/x86/xen_cpuid_timing_test.c | 271 +++++
.../selftests/kvm/x86/xen_migration_test.c | 222 ++++
.../selftests/kvm/x86/xen_shinfo_test.c | 31 +-
25 files changed, 3010 insertions(+), 303 deletions(-)
rename arch/x86/include/{ => uapi}/asm/pvclock-abi.h (82%)
create mode 100644 tools/testing/selftests/kvm/x86/masterclock_offset_test.c
create mode 100644 tools/testing/selftests/kvm/x86/pvclock_migration_test.c
create mode 100644 tools/testing/selftests/kvm/x86/pvclock_test.c
create mode 100644 tools/testing/selftests/kvm/x86/tsc_user_set_test.c
create mode 100644 tools/testing/selftests/kvm/x86/xen_cpuid_timing_test.c
create mode 100644 tools/testing/selftests/kvm/x86/xen_migration_test.c
base-commit: dea4f6380f4ec96e8f3b0aaf2343764bd0d25ef4
--
2.43.0
next reply other threads:[~2026-11-09 1:21 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-09 1:20 David Woodhouse [this message]
2026-11-09 1:20 ` [PATCH v12 01/24] KVM: x86: Improve synchronization in kvm_synchronize_tsc() David Woodhouse
2026-11-09 1:20 ` [PATCH v12 02/24] KVM: x86: Kill last_tsc_{nsec,write,offset} fields David Woodhouse
2026-11-09 1:20 ` [PATCH v12 03/24] KVM: x86: Replace nr_vcpus_matched_tsc count with all_vcpus_matched_tsc bool David Woodhouse
2026-11-09 1:20 ` [PATCH v12 04/24] KVM: x86: Allow KVM master clock mode when TSCs are offset from each other David Woodhouse
2026-11-09 1:20 ` [PATCH v12 05/24] KVM: x86: Factor out kvm_use_master_clock() David Woodhouse
2026-11-09 1:20 ` [PATCH v12 06/24] KVM: x86: Avoid gratuitous global clock updates David Woodhouse
2026-11-09 1:20 ` [PATCH v12 07/24] KVM: x86: Avoid redundant masterclock updates from multiple vCPUs David Woodhouse
2026-11-09 1:20 ` [PATCH v12 08/24] KVM: x86: Re-synchronize TSC after KVM_SET_TSC_KHZ David Woodhouse
2026-11-09 6:24 ` kernel test robot
2026-11-09 1:20 ` [PATCH v12 09/24] KVM: x86: Cache host vclock_mode for masterclock eligibility checks David Woodhouse
2026-11-09 1:20 ` [PATCH v12 10/24] KVM: x86: Remove pvclock_gtod_data and private timekeeping code David Woodhouse
2026-11-09 1:20 ` [PATCH v12 11/24] KVM: x86: Activate master clock from kvm_arch_init_vm() David Woodhouse
2026-11-09 1:20 ` [PATCH v12 12/24] UAPI: x86: Move pvclock-abi to UAPI for x86 platforms David Woodhouse
2026-11-09 1:20 ` [PATCH v12 13/24] KVM: selftests: Use UAPI pvclock-abi.h in xen_shinfo_test David Woodhouse
2026-11-09 1:21 ` [PATCH v12 14/24] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration David Woodhouse
2026-11-09 1:21 ` [PATCH v12 15/24] KVM: selftests: Add library helpers for clock and TSC-offset ioctls David Woodhouse
2026-11-09 1:21 ` [PATCH v12 16/24] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration David Woodhouse
2026-11-09 1:21 ` [PATCH v12 17/24] KVM: selftests: Add KVM/PV clock selftest to prove timer correction David Woodhouse
2026-11-09 1:21 ` [PATCH v12 18/24] KVM: selftests: Add master clock offset test David Woodhouse
2026-11-09 1:21 ` [PATCH v12 19/24] KVM: selftests: Add Xen/generic CPUID timing leaf test David Woodhouse
2026-11-09 1:21 ` [PATCH v12 20/24] KVM: selftests: Add Xen runstate migration test David Woodhouse
2026-11-09 1:21 ` [PATCH v12 21/24] KVM: selftests: Test that the first user TSC write is honoured exactly David Woodhouse
2026-11-09 1:21 ` [PATCH v12 22/24] KVM: x86/xen: Drop open-coded fallback clock read in kvm_xen_start_timer() David Woodhouse
2026-11-09 1:21 ` [PATCH v12 23/24] KVM: x86: Use a single time snapshot in kvm_vm_ioctl_set_clock() David Woodhouse
2026-11-09 1:21 ` [PATCH v12 24/24] KVM: x86: Keep master clock for guests using legacy MSR_KVM_SYSTEM_TIME 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=20261109012112.3415328-1-dwmw2@infradead.org \
--to=dwmw2@infradead.org \
--cc=Sascha.Bischoff@arm.com \
--cc=boris.ostrovsky@oracle.com \
--cc=bp@alien8.de \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=dongli.zhang@oracle.com \
--cc=dwmw@amazon.co.uk \
--cc=hpa@zytor.com \
--cc=jalliste@amazon.com \
--cc=jgross@suse.com \
--cc=jic23@kernel.org \
--cc=joe.jin@oracle.com \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maz@kernel.org \
--cc=mingo@redhat.com \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=skhan@linuxfoundation.org \
--cc=tglx@kernel.org \
--cc=vkuznets@redhat.com \
--cc=x86@kernel.org \
--cc=xen-devel@lists.xenproject.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®