From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Subject: Re: [PATCH v11 21/23] KVM: selftests: Test that the first user TSC write is honoured exactly
Date: Thu, 05 Nov 2026 20:50:12 +0000 [thread overview]
Message-ID: <sashiko-outbox-200154@kernel.org> (raw)
In-Reply-To: <20261105154955.2525419-22-dwmw2@infradead.org>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] KVM: selftests: misplaced timing window and missing KVM_SET_TSC_KHZ in main()
- [Low] KVM: selftests: duplicate MSR_IA32_TSC test binary and helpers
--
commit c39adcb632ae1981c065df1ae8906c472090451f
Author: David Woodhouse <dwmw@amazon.co.uk>
KVM: selftests: Test that the first user TSC write is honoured exactly
This commit adds an x86 KVM selftest that creates a vCPU, writes a guest TSC
value corresponding to 500ms of runtime via MSR_IA32_TSC, and checks that the
value reads back without being snapped back to the default generation by the
1-second slop heuristic in kvm_synchronize_tsc().
> diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
> index 1edc78845fd35..fd063806a7fb9 100644
> --- a/tools/testing/selftests/kvm/Makefile.kvm
> +++ b/tools/testing/selftests/kvm/Makefile.kvm
> @@ -151,6 +151,7 @@ TEST_GEN_PROGS_x86 += x86/xcr0_cpuid_test
> TEST_GEN_PROGS_x86 += x86/xss_msr_test
> TEST_GEN_PROGS_x86 += x86/debug_regs
> TEST_GEN_PROGS_x86 += x86/tsc_msrs_test
> +TEST_GEN_PROGS_x86 += x86/tsc_user_set_test
[Severity: Low]
This isn't a bug, but tools/testing/selftests/kvm/x86/tsc_msrs_test.c already
tests host and guest reads and writes to MSR_IA32_TSC and MSR_IA32_TSC_ADJUST.
Should this narrow MSR_IA32_TSC host-write check be added as a test case in
x86/tsc_msrs_test.c rather than introducing a separate standalone test binary?
> diff --git a/tools/testing/selftests/kvm/x86/tsc_user_set_test.c b/tools/testing/selftests/kvm/x86/tsc_user_set_test.c
> new file mode 100644
> index 0000000000000..6ab76e817f9fe
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86/tsc_user_set_test.c
> @@ -0,0 +1,86 @@
[ ... ]
> +int main(void)
> +{
> + struct kvm_vcpu *vcpu;
> + struct kvm_vm *vm;
> + u64 freq_khz, t_write, t_read, slack_cycles, mono_ns;
> + struct timespec ts;
> + int r;
> +
> + /*
> + * Start the measured window before vCPU creation: a snapped-back
> + * value tracks time since the kernel's default generation (i.e.
> + * since creation), so the window the slack is derived from must
> + * include everything from creation onwards.
> + */
> + clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
> + mono_ns = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
> +
> + vm = vm_create_with_one_vcpu(&vcpu, NULL);
> +
> + r = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
> + TEST_ASSERT(r > 0, "KVM_GET_TSC_KHZ failed");
> + freq_khz = r;
[Severity: Low]
This isn't a bug, but vm_create_with_one_vcpu() calls
kvm_arch_vm_post_create(), which already invokes KVM_GET_TSC_KHZ, asserts that
it succeeded, and stores the result in the global guest_tsc_khz. In addition,
test_util.h (included via kvm_util.h) provides timespec_to_ns() and
timespec_sub(). Could main() reuse guest_tsc_khz and timespec_to_ns() instead
of re-querying KVM_GET_TSC_KHZ and open-coding the timespec conversion?
> +
> + /*
> + * The first user write: a guest TSC corresponding to 500ms of
> + * runtime. This is within 1s (of virtual cycle time) of the
> + * kernel's default generation, so it is exactly the case that
> + * the slop heuristic would destroy if it were (incorrectly)
> + * armed by the kernel's own internal TSC synchronization.
> + */
> + t_write = freq_khz * 500ULL; /* khz * ms = cycles */
> +
> + vcpu_set_msr(vcpu, MSR_IA32_TSC, t_write);
> +
> + t_read = vcpu_get_msr(vcpu, MSR_IA32_TSC);
> +
> + clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
> + mono_ns = (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - mono_ns;
> +
> + /*
> + * The TSC advances between the write and the read; bound the
> + * difference by the measured elapsed time around the two ioctls
> + * plus 10ms of slop, so a descheduled test cannot flake. If the
> + * write was snapped to the kernel's default generation, the
> + * value read back is ~500ms low (i.e. below t_write), which is
> + * unambiguous.
> + */
> + slack_cycles = freq_khz * (mono_ns / 1000000ULL + 10);
> +
> + TEST_ASSERT(t_read >= t_write,
> + "TSC went backwards from explicit write: wrote %lu, read %lu"
> + " (first userspace write was subjected to 1-second slop?)",
> + t_write, t_read);
> + TEST_ASSERT(t_read - t_write < slack_cycles,
> + "TSC read back too far from written value: wrote %lu, read %lu",
> + t_write, t_read);
[Severity: Medium]
Should the initial clock_gettime(CLOCK_MONOTONIC_RAW, &ts) call in main() be
placed immediately before vcpu_set_msr(vcpu, MSR_IA32_TSC, t_write) rather
than before vm_create_with_one_vcpu(&vcpu, NULL)?
When the kernel honours vcpu_set_msr(vcpu, MSR_IA32_TSC, t_write), the guest
TSC is set to t_write at the time of vcpu_set_msr() and only advances between
vcpu_set_msr() and vcpu_get_msr(), matching the second comment that
slack_cycles bounds the elapsed time around the two MSR ioctls.
Conversely, if a buggy kernel snaps the write back to the default generation
established during vCPU creation, t_read tracks the elapsed time since vCPU
creation. Because mono_ns is measured from before vm_create_with_one_vcpu(),
t_read is always less than mono_ns, so t_read - t_write < slack_cycles is
guaranteed to hold for any snapped-back value. If vm_create_with_one_vcpu()
takes between 500ms and 1500ms on a loaded host, a snapped-back TSC will
satisfy both t_read >= t_write and t_read - t_write < slack_cycles, causing
the test to pass on a buggy kernel.
Also, both the commit message and the file header comment note that
kernel-internal TSC synchronization on vCPU creation and frequency changes
must not arm user_set_tsc. During kvm_arch_vcpu_create(), kvm_set_tsc_khz()
runs while l1_tsc_scaling_ratio and l1_tsc_offset are still zero, whereas an
explicit KVM_SET_TSC_KHZ ioctl on an existing vCPU runs with non-zero
l1_tsc_scaling_ratio and l1_tsc_offset. Should main() also test calling
KVM_SET_TSC_KHZ before the first MSR_IA32_TSC write?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261105154955.2525419-1-dwmw2@infradead.org?part=21
next prev parent reply other threads:[~2026-11-05 20:50 UTC|newest]
Thread overview: 33+ 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
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 18:00 ` sashiko-bot
2026-11-05 15:49 ` [PATCH v11 08/23] KVM: x86: Re-synchronize TSC after KVM_SET_TSC_KHZ David Woodhouse
2026-11-05 18:22 ` sashiko-bot
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 19:15 ` sashiko-bot
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 19:51 ` sashiko-bot
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 20:06 ` sashiko-bot
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 20:25 ` sashiko-bot
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 20:50 ` sashiko-bot [this message]
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-200154@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