All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: David Woodhouse <dwmw2@infradead.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Jonathan Corbet <corbet@lwn.net>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Thomas Gleixner <tglx@kernel.org>,
	 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>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Juergen Gross <jgross@suse.com>,
	 Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	Paul Durrant <paul@xen.org>,  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, kvm@vger.kernel.org,
	 linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	 xen-devel@lists.xenproject.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH v7 33/36] KVM: selftests: Add KVM/PV clock selftest to prove timer correction
Date: Fri, 31 Jul 2026 16:32:58 -0700	[thread overview]
Message-ID: <am0wquLHtd28syfx@google.com> (raw)
In-Reply-To: <20260728144954.355376-34-dwmw2@infradead.org>

> +int main(int argc, char *argv[])
> +{
> +	static const struct option long_opts[] = {
> +		{ "sleep", required_argument, NULL, 's' },
> +		{ "help",  no_argument,       NULL, 'h' },
> +		{ NULL,    0,                  NULL,  0  },
> +	};
> +	unsigned int sleep_sec = 2;
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_vm *vm;
> +	uint64_t host_khz;
> +	uint64_t freq;
> +	int opt;
> +
> +	while ((opt = getopt_long(argc, argv, "s:h", long_opts, NULL)) != -1) {
> +		switch (opt) {
> +		case 's':
> +			sleep_sec = atoi(optarg);
> +			break;
> +		case 'h':
> +		default:
> +			usage(argv[0]);
> +			return opt == 'h' ? 0 : 1;
> +		}
> +	}
> +
> +	TEST_REQUIRE(sys_clocksource_is_based_on_tsc());
> +	TEST_REQUIRE(kvm_has_cap(KVM_CAP_TSC_CONTROL));
> +
> +	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
> +	configure_pvclock(vm);
> +
> +	/* Check KVM_GET_CLOCK_GUEST is supported */
> +	{
> +		struct pvclock_vcpu_time_info tmp;
> +		int ret = __vcpu_ioctl(vcpu, KVM_GET_CLOCK_GUEST, &tmp);
> +		TEST_REQUIRE(ret == 0);

It will likely be a moot point since we should have a CAP, but don't do TEST_REQUIRE()
on a local variable like this, it completely defeates the purpose of the macro
shenanigans.  Becuase this:

  1..0 # SKIP - Requirement not met: ret == 0

is useless information, whereas this:

  1..0 # SKIP - Requirement not met: !__vcpu_ioctl(vcpu, KVM_GET_CLOCK_GUEST, &tmp)

gives the user a starting point without having to go search through the test code.

> +	}

...

> +static volatile uint32_t vcpu_counter;
> +static void guest_code_stable_bit(void)
> +{
> +	uint32_t idx = __atomic_fetch_add(&vcpu_counter, 1, __ATOMIC_SEQ_CST);
> +	uint64_t gpa = KVMCLOCK_GPA + idx * sizeof(struct pvclock_vcpu_time_info);

This series needs to be updated to catch up to upstream.  Selftests now use
u32, u64, etc.  And at least one patch missed an obvious opportunity for guard(). 

> +	wrmsr(MSR_KVM_SYSTEM_TIME_NEW, gpa | KVM_MSR_ENABLED);
> +	GUEST_SYNC(0);
> +	GUEST_SYNC(0);
> +	GUEST_SYNC(0);
> +}
> +
> +static void set_tsc_offset(struct kvm_vcpu *vcpu, uint64_t offset)
> +{
> +	struct kvm_device_attr attr = {
> +		.group = KVM_VCPU_TSC_CTRL,
> +		.attr = KVM_VCPU_TSC_OFFSET,
> +		.addr = (__u64)(uintptr_t)&offset,
> +	};
> +
> +	TEST_REQUIRE(__vcpu_has_device_attr(vcpu, KVM_VCPU_TSC_CTRL,
> +					    KVM_VCPU_TSC_OFFSET) == 0);
> +	vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &attr);

This quite clearly belongs in library code.

> +}
> +
> +static void run_vcpu_once(struct kvm_vcpu *vcpu)
> +{
> +	struct ucall uc;
> +
> +	vcpu_run(vcpu);
> +	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
> +	switch (get_ucall(vcpu, &uc)) {
> +	case UCALL_ABORT:
> +		REPORT_GUEST_ASSERT(uc);

Gah, we really need to have vcpu_run() handle guest asserts.

> +		break;
> +	case UCALL_SYNC:
> +		break;
> +	default:
> +		TEST_FAIL("Unexpected ucall");
> +	}
> +}
> +
> +static void test_tsc_stable_bit(void)
> +{
> +	struct pvclock_vcpu_time_info pvti;
> +	struct kvm_vcpu *vcpus[2];
> +	struct kvm_vm *vm;
> +	int ret;
> +
> +	pr_info("Testing PVCLOCK_TSC_STABLE_BIT with matched/unmatched TSCs\n");
> +
> +	vm = vm_create_with_vcpus(2, guest_code_stable_bit, vcpus);
> +	configure_pvclock(vm);
> +
> +	/*
> +	 * Case 1: All TSCs matched (same frequency and offset).
> +	 * Master clock should be active, PVCLOCK_TSC_STABLE_BIT set.
> +	 */
> +	run_vcpu_once(vcpus[0]);
> +
> +	ret = __vcpu_ioctl(vcpus[0], KVM_GET_CLOCK_GUEST, &pvti);
> +	TEST_ASSERT(!ret, "GET_CLOCK_GUEST should succeed with matched TSCs");
> +	TEST_ASSERT(pvti.flags & PVCLOCK_TSC_STABLE_BIT,
> +		    "PVCLOCK_TSC_STABLE_BIT should be set with matched TSCs");
> +
> +	/*
> +	 * Case 2: Different TSC offset, same frequency.
> +	 * Master clock should still be active (frequency matches), but
> +	 * PVCLOCK_TSC_STABLE_BIT should be cleared (offsets differ).
> +	 */
> +	set_tsc_offset(vcpus[1], 12345678);
> +	run_vcpu_once(vcpus[1]);
> +	run_vcpu_once(vcpus[0]);
> +
> +	ret = __vcpu_ioctl(vcpus[0], KVM_GET_CLOCK_GUEST, &pvti);
> +	if (ret) {
> +		/* Master clock disabled by offset mismatch — old kernel */
> +		pr_info("  Skipping offset tests (master clock requires matched offsets)\n");
> +		goto out_stable;
> +	}
> +	TEST_ASSERT(!(pvti.flags & PVCLOCK_TSC_STABLE_BIT),
> +		    "PVCLOCK_TSC_STABLE_BIT should be clear with offset-mismatched TSCs");
> +
> +	/*
> +	 * Case 3: Different TSC frequency.
> +	 * Master clock should be disabled entirely.
> +	 */
> +	vcpu_ioctl(vcpus[1], KVM_SET_TSC_KHZ,
> +		   (void *)(unsigned long)(__vcpu_ioctl(vcpus[1], KVM_GET_TSC_KHZ, NULL) / 2));
> +	/* Write TSC to trigger kvm_synchronize_tsc / kvm_track_tsc_matching */
> +	vcpu_set_msr(vcpus[1], MSR_IA32_TSC, 0);
> +	run_vcpu_once(vcpus[1]);
> +
> +	ret = __vcpu_ioctl(vcpus[0], KVM_GET_CLOCK_GUEST, &pvti);
> +	TEST_ASSERT(ret && errno == EINVAL,
> +		    "GET_CLOCK_GUEST should fail with frequency-mismatched TSCs, got %d (errno %d)",
> +		    ret, errno);
> +
> +out_stable:
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_clock_guest_with_offsets(void)
> +{
> +	struct pvclock_vcpu_time_info pvti0, pvti1, pvti1_after;
> +	struct kvm_vcpu *vcpus[2];
> +	struct kvm_vm *vm;
> +	int64_t delta;
> +	int ret;
> +
> +	pr_info("Testing KVM_[GS]ET_CLOCK_GUEST with different TSC offsets\n");
> +
> +	vm = vm_create_with_vcpus(2, guest_code_stable_bit, vcpus);
> +	configure_pvclock(vm);
> +
> +	/* Set different TSC offsets on the two vCPUs */
> +	set_tsc_offset(vcpus[0], 0);
> +	set_tsc_offset(vcpus[1], 1000000000ull);
> +
> +	/* Run both to establish kvmclock */
> +	run_vcpu_once(vcpus[0]);
> +	run_vcpu_once(vcpus[1]);
> +
> +	/* GET_CLOCK_GUEST on both — should succeed (master clock active) */
> +	ret = __vcpu_ioctl(vcpus[0], KVM_GET_CLOCK_GUEST, &pvti0);
> +	if (ret) {
> +		pr_info("  Skipping (master clock requires matched offsets on this kernel)\n");
> +		kvm_vm_free(vm);
> +		return;
> +	}
> +	ret = __vcpu_ioctl(vcpus[1], KVM_GET_CLOCK_GUEST, &pvti1);
> +	TEST_ASSERT(!ret, "GET_CLOCK_GUEST on vcpu1 failed");
> +
> +	/* The tsc_timestamps should differ (different offsets) */
> +	TEST_ASSERT(pvti0.tsc_timestamp != pvti1.tsc_timestamp,
> +		    "tsc_timestamps should differ with different offsets");
> +
> +	/* Sleep to let time elapse, then restore vcpu0's clock */
> +	sleep(1);
> +	vcpu_ioctl(vcpus[0], KVM_SET_CLOCK_GUEST, &pvti0);
> +
> +	/* Run vcpu0 to process the clock update */
> +	run_vcpu_once(vcpus[0]);
> +
> +	/* GET_CLOCK_GUEST on vcpu1 — should reflect the correction */
> +	ret = __vcpu_ioctl(vcpus[1], KVM_GET_CLOCK_GUEST, &pvti1_after);
> +	TEST_ASSERT(!ret, "GET_CLOCK_GUEST on vcpu1 after SET failed");

Please add proper APIs instead of copy+pasting the same code everywhere.  E.g.
this should really be something like

	vcpu_get_clock_guest(vcpus[1], ...);

where vcpu_ioctl() asserts success.


  reply	other threads:[~2026-07-31 23:33 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28 14:39 [PATCH v7 00/36] Cleaning up the KVM clock mess David Woodhouse
2026-07-28 14:39 ` [PATCH v7 01/36] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force David Woodhouse
2026-07-28 14:39 ` [PATCH v7 02/36] KVM: x86: Explicitly disable TSC scaling without CONSTANT_TSC David Woodhouse
2026-07-28 14:39 ` [PATCH v7 03/36] KVM: x86: Activate master clock immediately on vCPU creation David Woodhouse
2026-07-28 15:17   ` sashiko-bot
2026-07-28 14:39 ` [PATCH v7 04/36] KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host David Woodhouse
2026-07-28 14:39 ` [PATCH v7 05/36] KVM: x86: Fold __get_kvmclock() into get_kvmclock() David Woodhouse
2026-07-28 23:06   ` Sean Christopherson
2026-07-28 14:39 ` [PATCH v7 06/36] KVM: x86: Drop CPU pinning in get_kvmclock() David Woodhouse
2026-07-28 14:39 ` [PATCH v7 07/36] KVM: x86: Restructure get_kvmclock() David Woodhouse
2026-07-28 15:11   ` sashiko-bot
2026-07-28 23:02     ` Sean Christopherson
2026-07-28 23:04   ` Sean Christopherson
2026-07-28 14:39 ` [PATCH v7 08/36] KVM: x86: Fix KVM clock precision in get_kvmclock() with TSC scaling David Woodhouse
2026-07-28 14:39 ` [PATCH v7 09/36] KVM: x86: Use get_kvmclock() in kvm_get_wall_clock_epoch() David Woodhouse
2026-07-28 14:39 ` [PATCH v7 10/36] KVM: x86: Fix compute_guest_tsc() to handle negative time deltas David Woodhouse
2026-07-28 14:39 ` [PATCH v7 11/36] KVM: x86: Restructure kvm_guest_time_update() for TSC upscaling David Woodhouse
2026-07-28 15:11   ` sashiko-bot
2026-07-28 14:39 ` [PATCH v7 12/36] KVM: x86: Simplify and comment kvm_get_time_scale() David Woodhouse
2026-07-28 14:39 ` [PATCH v7 13/36] KVM: x86: Remove implicit rdtsc() from kvm_compute_l1_tsc_offset() David Woodhouse
2026-07-28 14:39 ` [PATCH v7 14/36] KVM: x86: Improve synchronization in kvm_synchronize_tsc() David Woodhouse
2026-07-28 15:08   ` sashiko-bot
2026-07-28 14:39 ` [PATCH v7 15/36] KVM: x86: Kill last_tsc_{nsec,write,offset} fields David Woodhouse
2026-07-28 15:09   ` sashiko-bot
2026-07-28 14:39 ` [PATCH v7 16/36] KVM: x86: Replace nr_vcpus_matched_tsc count with all_vcpus_matched_tsc bool David Woodhouse
2026-07-28 14:39 ` [PATCH v7 17/36] KVM: x86: Allow KVM master clock mode when TSCs are offset from each other David Woodhouse
2026-07-28 14:39 ` [PATCH v7 18/36] KVM: x86: Factor out kvm_use_master_clock() David Woodhouse
2026-07-28 14:39 ` [PATCH v7 19/36] KVM: x86: Avoid gratuitous global clock updates David Woodhouse
2026-07-28 14:40 ` [PATCH v7 20/36] KVM: x86/xen: Prevent runstate times from becoming negative David Woodhouse
2026-07-28 15:15   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 21/36] KVM: x86: Avoid redundant masterclock updates from multiple vCPUs David Woodhouse
2026-07-28 15:23   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 22/36] KVM: x86: Remove runtime Xen TSC frequency CPUID update David Woodhouse
2026-07-28 15:18   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 23/36] KVM: x86: Re-synchronize TSC after KVM_SET_TSC_KHZ David Woodhouse
2026-07-28 15:19   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 24/36] KVM: x86: Use ktime_get_snapshot_id() for master clock David Woodhouse
2026-07-28 15:21   ` sashiko-bot
2026-07-28 22:59     ` Sean Christopherson
2026-07-28 14:40 ` [PATCH v7 25/36] KVM: x86: Compute kvmclock base without pvclock_gtod_data David Woodhouse
2026-07-28 14:40 ` [PATCH v7 26/36] KVM: x86: Cache host vclock_mode for masterclock eligibility checks David Woodhouse
2026-07-28 15:26   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 27/36] KVM: x86: Remove pvclock_gtod_data and private timekeeping code David Woodhouse
2026-07-28 15:25   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 28/36] KVM: x86: Activate master clock from kvm_arch_init_vm() David Woodhouse
2026-07-28 15:31   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 29/36] UAPI: x86: Move pvclock-abi to UAPI for x86 platforms David Woodhouse
2026-07-28 14:40 ` [PATCH v7 30/36] KVM: selftests: Use UAPI pvclock-abi.h in xen_shinfo_test David Woodhouse
2026-07-28 14:40 ` [PATCH v7 31/36] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration David Woodhouse
2026-07-31 23:24   ` Sean Christopherson
2026-07-28 14:40 ` [PATCH v7 32/36] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration David Woodhouse
2026-07-28 15:26   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 33/36] KVM: selftests: Add KVM/PV clock selftest to prove timer correction David Woodhouse
2026-07-31 23:32   ` Sean Christopherson [this message]
2026-07-28 14:40 ` [PATCH v7 34/36] KVM: selftests: Add master clock offset test David Woodhouse
2026-07-28 15:22   ` sashiko-bot
2026-07-31 23:38   ` Sean Christopherson
2026-07-28 14:40 ` [PATCH v7 35/36] KVM: selftests: Add Xen/generic CPUID timing leaf test David Woodhouse
2026-07-28 15:23   ` sashiko-bot
2026-07-28 14:40 ` [PATCH v7 36/36] KVM: selftests: Add Xen runstate migration test David Woodhouse
2026-07-28 23:18 ` [PATCH v7 00/36] Cleaning up the KVM clock mess Sean Christopherson
2026-07-29 10:42   ` 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=am0wquLHtd28syfx@google.com \
    --to=seanjc@google.com \
    --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=dwmw2@infradead.org \
    --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=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 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.