Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Subject: Re: [PATCH v12 16/24] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration
Date: Mon, 09 Nov 2026 01:44:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-204316@kernel.org> (raw)
In-Reply-To: <20261109012112.3415328-17-dwmw2@infradead.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] KVM: x86: doc per-vCPU KVM_SET_TSC_KHZ order breaks
  KVM_SET_CLOCK_GUEST
- [Low] KVM: selftests: duplicated pvclock and TSC scaling helpers in
  test
--

commit 83ddc67114d3c0896cacdd30d2c4eb5ecc38fbfe
Author: David Woodhouse <dwmw@amazon.co.uk>

KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration

This commit adds the KVM_VCPU_TSC_SCALE vCPU device attribute to expose the
guest TSC scaling ratio and fractional bits to userspace, updates the TSC
migration documentation, and adds a selftest for pvclock and TSC migration.

> diff --git a/Documentation/virt/kvm/devices/vcpu.rst b/Documentation/virt/kvm/devices/vcpu.rst
> index deb5c51bc00c8..8dc6ad904429d 100644
> --- a/Documentation/virt/kvm/devices/vcpu.rst
> +++ b/Documentation/virt/kvm/devices/vcpu.rst
[ ... ]
> @@ -261,44 +265,117 @@ following describes a possible algorithm to use for this purpose.
[ ... ]
> +From the destination VMM process:
> +
> +4. Invoke the KVM_SET_TSC_KHZ ioctl to set the scaled frequency of the
> +   guest's TSC (freq[i]) — either on the VM before creating the vCPUs, or
> +   on each vCPU after creating it (as the exemplar test does). When using
> +   the per-vCPU form, set the frequency before performing step 7 on that
> +   vCPU: KVM_VCPU_TSC_SCALE reports the ratio for the currently
> +   configured frequency.
> +
> +5. Invoke the KVM_GET_CLOCK ioctl to record the host TSC (host_tsc_dst) and
> +   host CLOCK_REALTIME nanoseconds (time_dst) at a given moment (Tdst),
> +   again checking KVM_CLOCK_HOST_TSC in the returned flags.
> +
> +6. Calculate the number of nanoseconds elapsed between Tsrc and Tdst:
> +
> +   ΔT = time_dst - time_src
> +
> +7. As each vCPU[i] is created:

[Severity: Low]
When using the per-vCPU KVM_SET_TSC_KHZ ioctl, does creating and
configuring vCPUs one at a time disable the master clock?

Without the VM-scoped KVM_SET_TSC_KHZ ioctl, each KVM_CREATE_VCPU runs
kvm_arch_vcpu_postcreate() -> kvm_synchronize_tsc() at default_tsc_khz.
If a VMM sets freq[0] on vCPU[0] and then creates vCPU[1], vCPU[1]'s
creation bumps cur_tsc_freq_generation back to default_tsc_khz and resets
nr_vcpus_matched_freq, and setting freq[1] on vCPU[1] bumps
cur_tsc_freq_generation again, leaving vCPU[0] in an older generation:

arch/x86/kvm/x86.c:__kvm_synchronize_tsc() {
	...
	if (vcpu->arch.virtual_tsc_khz != kvm->arch.cur_tsc_khz) {
		kvm->arch.cur_tsc_freq_generation++;
		kvm->arch.all_vcpus_matched_freq = false;
		kvm->arch.nr_vcpus_matched_freq = 0;
	}

	/* Count each vCPU once per freq generation */
	if (vcpu->arch.this_tsc_freq_generation != kvm->arch.cur_tsc_freq_generation) {
		vcpu->arch.this_tsc_freq_generation = kvm->arch.cur_tsc_freq_generation;
		kvm->arch.nr_vcpus_matched_freq++;
	}
	...
}

Because all_vcpus_matched_freq stays false, KVM_GET_CLOCK in step 5 will
not set KVM_CLOCK_HOST_TSC (or KVM_SET_CLOCK_GUEST in step 8 will fail
with -ENODATA if vCPUs are created during step 7).  Should step 4 note
that when using the per-vCPU KVM_SET_TSC_KHZ ioctl, all vCPUs must be
created before setting the frequency on any of them (as create_vm() in
pvclock_migration_test.c does)?

> diff --git a/tools/testing/selftests/kvm/x86/pvclock_migration_test.c b/tools/testing/selftests/kvm/x86/pvclock_migration_test.c
> new file mode 100644
> index 0000000000000..c18c9132308d6
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86/pvclock_migration_test.c
> @@ -0,0 +1,521 @@
[ ... ]
> +static u64 pvclock_read_cycles(struct pvclock_vcpu_time_info *src,
> +				    u64 tsc)
> +{
> +	u64 delta = tsc - src->tsc_timestamp;
> +	u64 ns;
> +
> +	if (src->tsc_shift >= 0)
> +		delta <<= src->tsc_shift;
> +	else
> +		delta >>= -(s32)src->tsc_shift;
> +
> +	ns = (unsigned __int128)delta * src->tsc_to_system_mul >> 32;
> +	return src->system_time + ns;
> +}
[ ... ]
> +	if (scale_dst.tsc_frac_bits)
> +		raw_dst = (unsigned __int128)host_tsc_dst * scale_dst.tsc_ratio
> +			  >> scale_dst.tsc_frac_bits;
> +	else
> +		raw_dst = host_tsc_dst;

[Severity: Low]
This isn't a bug, but pvclock_read_cycles() duplicates
__pvclock_read_cycles() from tools/arch/x86/include/asm/pvclock.h, and
raw_dst open-codes host_to_guest_tsc(&scale_dst, 0, host_tsc_dst) defined
earlier in this file.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261109012112.3415328-1-dwmw2@infradead.org?part=16

  reply	other threads:[~2026-11-09  1:44 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09  1:20 [PATCH v12 00/24] KVM: x86: Cleaning up the KVM clock mess, part 2 David Woodhouse
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:53   ` sashiko-bot
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:45   ` sashiko-bot
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:53   ` sashiko-bot
2026-11-09  1:20 ` [PATCH v12 08/24] KVM: x86: Re-synchronize TSC after KVM_SET_TSC_KHZ David Woodhouse
2026-11-09  1:53   ` sashiko-bot
2026-11-09  6:24   ` kernel test robot
2026-11-10  5:21   ` 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:56   ` sashiko-bot
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:53   ` sashiko-bot
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:44   ` sashiko-bot [this message]
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:47   ` sashiko-bot
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=sashiko-outbox-204316@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