This test proves that there is an inherent KVM/PV clock drift away from the guest TSC when KVM decides to update the PV time information structure due to a KVM_REQ_MASTERCLOCK_UPDATE. This drift is exascerbated when a guest is using TSC scaling and running at a different frequency to the host TSC [1]. It also proves that KVM_[GS]ET_CLOCK_GUEST API is working to mitigate the drift from TSC to within ±1ns.
The test simply records the PVTI (PV time information) at time of guest creation, after KVM has updated it's mapped PVTI structure and once the correction has taken place.
A singular point in time is then recorded via the guest TSC and is used to calculate the a PV clock value using each of the 3 PVTI structures.
As seen below a drift of ~3500ns is observed if no correction has taken place after KVM has updated the PVTI via master clock update. However, after the correction a delta of at most 1ns can be seen.
* selftests: kvm: pvclock_test * scaling tsc from 2999999KHz to 1499999KHz * before=5038374946 uncorrected=5038371437 corrected=5038374945 * delta_uncorrected=3509 delta_corrected=1
Clocksource check code has been borrowed from [2].
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?i... [2]: https://lore.kernel.org/kvm/20240106083346.29180-1-dongli.zhang@oracle.com/
Signed-off-by: Jack Allister jalliste@amazon.com CC: David Woodhouse dwmw2@infradead.org CC: Paul Durrant paul@xen.org --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/pvclock_test.c | 223 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/pvclock_test.c
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 741c7dc16afc..02ee1205bbed 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -87,6 +87,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/pmu_counters_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test +TEST_GEN_PROGS_x86_64 += x86_64/pvclock_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/pvclock_test.c b/tools/testing/selftests/kvm/x86_64/pvclock_test.c new file mode 100644 index 000000000000..172ef4d19c60 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/pvclock_test.c @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright © 2024, Amazon.com, Inc. or its affiliates. + * + * Tests for pvclock API + * KVM_SET_CLOCK_GUEST/KVM_GET_CLOCK_GUEST + */ +#include <asm/pvclock.h> +#include <asm/pvclock-abi.h> +#include <sys/stat.h> +#include <stdint.h> +#include <stdio.h> + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" + +enum { + STAGE_FIRST_BOOT, + STAGE_UNCORRECTED, + STAGE_CORRECTED, + NUM_STAGES +}; + +#define KVMCLOCK_GPA 0xc0000000ull +#define KVMCLOCK_SIZE sizeof(struct pvclock_vcpu_time_info) + +static void trigger_pvti_update(vm_paddr_t pvti_pa) +{ + /* + * We need a way to trigger KVM to update the fields + * in the PV time info. The easiest way to do this is + * to temporarily switch to the old KVM system time + * method and then switch back to the new one. + */ + wrmsr(MSR_KVM_SYSTEM_TIME, pvti_pa | KVM_MSR_ENABLED); + wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED); +} + +static void guest_code(vm_paddr_t pvti_pa) +{ + struct pvclock_vcpu_time_info *pvti_va = + (struct pvclock_vcpu_time_info *)pvti_pa; + + struct pvclock_vcpu_time_info pvti_boot; + struct pvclock_vcpu_time_info pvti_uncorrected; + struct pvclock_vcpu_time_info pvti_corrected; + uint64_t cycles_boot; + uint64_t cycles_uncorrected; + uint64_t cycles_corrected; + uint64_t tsc_guest; + + /* + * Setup the KVMCLOCK in the guest & store the original + * PV time structure that is used. + */ + wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED); + pvti_boot = *pvti_va; + GUEST_SYNC(STAGE_FIRST_BOOT); + + /* + * Trigger an update of the PVTI, if we calculate + * the KVM clock using this structure we'll see + * a drift from the TSC. + */ + trigger_pvti_update(pvti_pa); + pvti_uncorrected = *pvti_va; + GUEST_SYNC(STAGE_UNCORRECTED); + + /* + * The test should have triggered the correction by this + * point in time. We have a copy of each of the PVTI structs + * at each stage now. + * + * Let's sample the timestamp at a SINGLE point in time and + * then calculate what the KVM clock would be using the PVTI + * from each stage. + * + * Then return each of these values to the tester. + */ + pvti_corrected = *pvti_va; + tsc_guest = rdtsc(); + + cycles_boot = __pvclock_read_cycles(&pvti_boot, tsc_guest); + cycles_uncorrected = __pvclock_read_cycles(&pvti_uncorrected, tsc_guest); + cycles_corrected = __pvclock_read_cycles(&pvti_corrected, tsc_guest); + + GUEST_SYNC_ARGS(STAGE_CORRECTED, cycles_boot, cycles_uncorrected, + cycles_corrected, 0); +} + +static void run_test(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{ + struct ucall uc; + uint64_t ucall_reason; + struct pvclock_vcpu_time_info pvti_before; + uint64_t before, uncorrected, corrected; + int64_t delta_uncorrected, delta_corrected; + + /* Loop through each stage of the test. */ + while (true) { + + /* Start/restart the running vCPU code. */ + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + /* Retrieve and verify our stage. */ + ucall_reason = get_ucall(vcpu, &uc); + TEST_ASSERT(ucall_reason == UCALL_SYNC, + "Unhandled ucall reason=%lu", + ucall_reason); + + /* Run host specific code relating to stage. */ + switch (uc.args[1]) { + case STAGE_FIRST_BOOT: + /* Store the KVM clock values before an update. */ + vm_ioctl(vm, KVM_GET_CLOCK_GUEST, &pvti_before); + + /* Sleep for a set amount of time to induce drift. */ + sleep(5); + break; + + case STAGE_UNCORRECTED: + /* Restore the KVM clock values. */ + vm_ioctl(vm, KVM_SET_CLOCK_GUEST, &pvti_before); + break; + + case STAGE_CORRECTED: + /* Query the clock information and verify delta. */ + before = uc.args[2]; + uncorrected = uc.args[3]; + corrected = uc.args[4]; + + delta_uncorrected = before - uncorrected; + delta_corrected = before - corrected; + + pr_info("before=%lu uncorrected=%lu corrected=%lu\n", + before, uncorrected, corrected); + + pr_info("delta_uncorrected=%ld delta_corrected=%ld\n", + delta_uncorrected, delta_corrected); + + TEST_ASSERT((delta_corrected <= 1) && (delta_corrected >= -1), + "larger than expected delta detected = %ld", delta_corrected); + return; + } + } +} + +#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource" + +static void check_clocksource(void) +{ + char *clk_name; + struct stat st; + FILE *fp; + + fp = fopen(CLOCKSOURCE_PATH, "r"); + if (!fp) { + pr_info("failed to open clocksource file: %d; assuming TSC.\n", + errno); + return; + } + + if (fstat(fileno(fp), &st)) { + pr_info("failed to stat clocksource file: %d; assuming TSC.\n", + errno); + goto out; + } + + clk_name = malloc(st.st_size); + TEST_ASSERT(clk_name, "failed to allocate buffer to read file\n"); + + if (!fgets(clk_name, st.st_size, fp)) { + pr_info("failed to read clocksource file: %d; assuming TSC.\n", + ferror(fp)); + goto out; + } + + TEST_ASSERT(!strncmp(clk_name, "tsc\n", st.st_size), + "clocksource not supported: %s", clk_name); +out: + fclose(fp); +} + +static void configure_pvclock(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{ + unsigned int gpages; + + gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, KVMCLOCK_SIZE); + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + KVMCLOCK_GPA, 1, gpages, 0); + virt_map(vm, KVMCLOCK_GPA, KVMCLOCK_GPA, gpages); + + vcpu_args_set(vcpu, 1, KVMCLOCK_GPA); +} + +static void configure_scaled_tsc(struct kvm_vcpu *vcpu) +{ + uint64_t tsc_khz; + + tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL); + pr_info("scaling tsc from %ldKHz to %ldKHz\n", tsc_khz, tsc_khz / 2); + tsc_khz /= 2; + vcpu_ioctl(vcpu, KVM_SET_TSC_KHZ, (void *)tsc_khz); +} + +int main(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + check_clocksource(); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + + configure_pvclock(vm, vcpu); + configure_scaled_tsc(vcpu); + + run_test(vm, vcpu); + + return 0; +}
Hi Jack,
On 4/8/24 15:07, Jack Allister wrote:
This test proves that there is an inherent KVM/PV clock drift away from the guest TSC when KVM decides to update the PV time information structure due to a KVM_REQ_MASTERCLOCK_UPDATE. This drift is exascerbated when a guest is
Typo: exacerbated
using TSC scaling and running at a different frequency to the host TSC [1]. It also proves that KVM_[GS]ET_CLOCK_GUEST API is working to mitigate the drift from TSC to within ±1ns.
The test simply records the PVTI (PV time information) at time of guest creation, after KVM has updated it's mapped PVTI structure and once the correction has taken place.
A singular point in time is then recorded via the guest TSC and is used to calculate the a PV clock value using each of the 3 PVTI structures.
Typo: "the a"
As seen below a drift of ~3500ns is observed if no correction has taken place after KVM has updated the PVTI via master clock update. However, after the correction a delta of at most 1ns can be seen.
- selftests: kvm: pvclock_test
- scaling tsc from 2999999KHz to 1499999KHz
- before=5038374946 uncorrected=5038371437 corrected=5038374945
- delta_uncorrected=3509 delta_corrected=1
Clocksource check code has been borrowed from [2].
Signed-off-by: Jack Allister jalliste@amazon.com CC: David Woodhouse dwmw2@infradead.org CC: Paul Durrant paul@xen.org
tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/pvclock_test.c | 223 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/pvclock_test.c
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 741c7dc16afc..02ee1205bbed 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -87,6 +87,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/pmu_counters_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test +TEST_GEN_PROGS_x86_64 += x86_64/pvclock_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/pvclock_test.c b/tools/testing/selftests/kvm/x86_64/pvclock_test.c new file mode 100644 index 000000000000..172ef4d19c60 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/pvclock_test.c @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0-only +/*
- Copyright © 2024, Amazon.com, Inc. or its affiliates.
- Tests for pvclock API
- KVM_SET_CLOCK_GUEST/KVM_GET_CLOCK_GUEST
- */
+#include <asm/pvclock.h> +#include <asm/pvclock-abi.h> +#include <sys/stat.h> +#include <stdint.h> +#include <stdio.h>
+#include "test_util.h" +#include "kvm_util.h" +#include "processor.h"
+enum {
- STAGE_FIRST_BOOT,
- STAGE_UNCORRECTED,
- STAGE_CORRECTED,
- NUM_STAGES
+};
+#define KVMCLOCK_GPA 0xc0000000ull +#define KVMCLOCK_SIZE sizeof(struct pvclock_vcpu_time_info)
+static void trigger_pvti_update(vm_paddr_t pvti_pa) +{
- /*
* We need a way to trigger KVM to update the fields
* in the PV time info. The easiest way to do this is
* to temporarily switch to the old KVM system time
* method and then switch back to the new one.
*/
- wrmsr(MSR_KVM_SYSTEM_TIME, pvti_pa | KVM_MSR_ENABLED);
- wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED);
+}
+static void guest_code(vm_paddr_t pvti_pa) +{
- struct pvclock_vcpu_time_info *pvti_va =
(struct pvclock_vcpu_time_info *)pvti_pa;
- struct pvclock_vcpu_time_info pvti_boot;
- struct pvclock_vcpu_time_info pvti_uncorrected;
- struct pvclock_vcpu_time_info pvti_corrected;
- uint64_t cycles_boot;
- uint64_t cycles_uncorrected;
- uint64_t cycles_corrected;
- uint64_t tsc_guest;
- /*
* Setup the KVMCLOCK in the guest & store the original
* PV time structure that is used.
*/
- wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED);
- pvti_boot = *pvti_va;
- GUEST_SYNC(STAGE_FIRST_BOOT);
- /*
* Trigger an update of the PVTI, if we calculate
* the KVM clock using this structure we'll see
* a drift from the TSC.
*/
- trigger_pvti_update(pvti_pa);
- pvti_uncorrected = *pvti_va;
- GUEST_SYNC(STAGE_UNCORRECTED);
- /*
* The test should have triggered the correction by this
* point in time. We have a copy of each of the PVTI structs
* at each stage now.
*
* Let's sample the timestamp at a SINGLE point in time and
* then calculate what the KVM clock would be using the PVTI
* from each stage.
*
* Then return each of these values to the tester.
*/
- pvti_corrected = *pvti_va;
- tsc_guest = rdtsc();
- cycles_boot = __pvclock_read_cycles(&pvti_boot, tsc_guest);
- cycles_uncorrected = __pvclock_read_cycles(&pvti_uncorrected, tsc_guest);
- cycles_corrected = __pvclock_read_cycles(&pvti_corrected, tsc_guest);
- GUEST_SYNC_ARGS(STAGE_CORRECTED, cycles_boot, cycles_uncorrected,
cycles_corrected, 0);
+}
+static void run_test(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{
- struct ucall uc;
- uint64_t ucall_reason;
- struct pvclock_vcpu_time_info pvti_before;
- uint64_t before, uncorrected, corrected;
- int64_t delta_uncorrected, delta_corrected;
- /* Loop through each stage of the test. */
- while (true) {
/* Start/restart the running vCPU code. */
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
/* Retrieve and verify our stage. */
ucall_reason = get_ucall(vcpu, &uc);
TEST_ASSERT(ucall_reason == UCALL_SYNC,
"Unhandled ucall reason=%lu",
ucall_reason);
/* Run host specific code relating to stage. */
switch (uc.args[1]) {
case STAGE_FIRST_BOOT:
/* Store the KVM clock values before an update. */
vm_ioctl(vm, KVM_GET_CLOCK_GUEST, &pvti_before);
/* Sleep for a set amount of time to induce drift. */
sleep(5);
break;
case STAGE_UNCORRECTED:
/* Restore the KVM clock values. */
vm_ioctl(vm, KVM_SET_CLOCK_GUEST, &pvti_before);
break;
case STAGE_CORRECTED:
/* Query the clock information and verify delta. */
before = uc.args[2];
uncorrected = uc.args[3];
corrected = uc.args[4];
delta_uncorrected = before - uncorrected;
delta_corrected = before - corrected;
pr_info("before=%lu uncorrected=%lu corrected=%lu\n",
before, uncorrected, corrected);
pr_info("delta_uncorrected=%ld delta_corrected=%ld\n",
delta_uncorrected, delta_corrected);
TEST_ASSERT((delta_corrected <= 1) && (delta_corrected >= -1),
"larger than expected delta detected = %ld", delta_corrected);
return;
}
- }
+}
+#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource"
+static void check_clocksource(void) +{
AFAIR, I copied check_clocksource() from existing code during that time.
The commit e440c5f2e ("KVM: selftests: Generalize check_clocksource() from kvm_clock_test") has introduced sys_clocksource_is_tsc(). Later it is renamed to sys_clocksource_is_based_on_tsc().
Any chance to re-use sys_clocksource_is_based_on_tsc()?
- char *clk_name;
- struct stat st;
- FILE *fp;
- fp = fopen(CLOCKSOURCE_PATH, "r");
- if (!fp) {
pr_info("failed to open clocksource file: %d; assuming TSC.\n",
errno);
return;
- }
- if (fstat(fileno(fp), &st)) {
pr_info("failed to stat clocksource file: %d; assuming TSC.\n",
errno);
goto out;
- }
- clk_name = malloc(st.st_size);
- TEST_ASSERT(clk_name, "failed to allocate buffer to read file\n");
- if (!fgets(clk_name, st.st_size, fp)) {
pr_info("failed to read clocksource file: %d; assuming TSC.\n",
ferror(fp));
goto out;
- }
- TEST_ASSERT(!strncmp(clk_name, "tsc\n", st.st_size),
"clocksource not supported: %s", clk_name);
+out:
- fclose(fp);
+}
+static void configure_pvclock(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{
- unsigned int gpages;
- gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, KVMCLOCK_SIZE);
- vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
KVMCLOCK_GPA, 1, gpages, 0);
- virt_map(vm, KVMCLOCK_GPA, KVMCLOCK_GPA, gpages);
- vcpu_args_set(vcpu, 1, KVMCLOCK_GPA);
+}
+static void configure_scaled_tsc(struct kvm_vcpu *vcpu) +{
- uint64_t tsc_khz;
- tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
- pr_info("scaling tsc from %ldKHz to %ldKHz\n", tsc_khz, tsc_khz / 2);
- tsc_khz /= 2;
- vcpu_ioctl(vcpu, KVM_SET_TSC_KHZ, (void *)tsc_khz);
+}
Is configure_scaled_tsc() anecessary? Or how about to make it an option/arg? Then I will be able to test it on a VM/server without TSC scaling.
Thank you very much!
Dongli Zhang
+int main(void) +{
- struct kvm_vm *vm;
- struct kvm_vcpu *vcpu;
- check_clocksource();
- vm = vm_create_with_one_vcpu(&vcpu, guest_code);
- configure_pvclock(vm, vcpu);
- configure_scaled_tsc(vcpu);
- run_test(vm, vcpu);
- return 0;
+}
On Mon, 2024-04-08 at 17:43 -0700, Dongli Zhang wrote:
Hi Jack,
On 4/8/24 15:07, Jack Allister wrote:
This test proves that there is an inherent KVM/PV clock drift away from the guest TSC when KVM decides to update the PV time information structure due to a KVM_REQ_MASTERCLOCK_UPDATE. This drift is exascerbated when a guest is
Typo: exacerbated
using TSC scaling and running at a different frequency to the host TSC [1]. It also proves that KVM_[GS]ET_CLOCK_GUEST API is working to mitigate the drift from TSC to within ±1ns.
No, the KVM_[GS}ET_CLOCK_GUEST API is not about mitigating kernel bugs.
We *fix* kernel bugs, we don't make userspace work around them.
The KVM_[GS}ET_CLOCK_GUEST API allows userspace to perform accurate live update and live migration without disrupting the relationship between guest TSC and KVM clock.
Since a bombing run on KVM_REQ_MASTERCLOCK_UPDATE users is on my TODO list, it's worth noting the *reason* that switching to the obsolete MSR_KVM_SYSTEM_TIME forces ka->use_master_clock mode off.
It's not documented at all as far as I can tell, but in commit 54750f2cf042 (“KVM: x86: workaround SuSE's 2.6.16 pvclock vs masterclock issue“) in 2015, it was done to work around a guest bug where the guest *expected* the reference point to keep being updated and never be too far in the past.
Is configure_scaled_tsc() anecessary? Or how about to make it an option/arg? Then I will be able to test it on a VM/server without TSC scaling.
As discussed, TSC scaling shouldn't be needed. It *should* be part of the unit test if possible, because there is a class of bugs it'll trigger, but it should be optional.
In particular, the existing KVM_GET_CLOCK will return extra-wrong results if TSC scaling is in force. But that isn't being tested here yet because we haven't *fixed* it yet :)
For reference, here's my TODO list which Jack is working from...
• Add KVM unit test to validate that KVM clock does not change when provoked (including by simulated live update). It’s OK for the reference point at { tsc_timestamp, system_time } in the pvclock structure to change, but only such that it gives the same results for a given guest TSC — that is, if system_time changes, then tsc_timestamp must change by a delta which precisely corresponds in terms of the advertised guest TSC frequency. Perhaps allow a slop of 1ns for rounding, but no more.
• Audit and fix (i.e. remove) KVM_REQ_MASTERCLOCK_UPDATE usage, starting with kvm_xen_shared_info_init(). And work out whether it should be sent to all vCPUs, as some call sites do, or just one?
• Add KVM_VCPU_TSC_SCALE attribute to allow userspace to know the precise host→guest TSC scaling. (cf. https://lore.kernel.org/all/13f256ad95de186e3b6bcfcc1f88da5d0ad0cb71.camel@i...)
• Expose guest’s view of KVM clock to userspace via KVM_GET_CLOCK_GUEST ioctl. Perhaps also a memory-mapped version, as the gfn_to_pfn_cache allows writing to userspace HVAs. With this, userspace has fast and accurate way to calculate the KVM clock at any given moment in time. (Currently, userspace calls the KVM_GET_CLOCK ioctl which is slow and returns inaccurate results). Then userspace can base other things like PIT and HPET emulation on the KVM clock and simplify timekeeping over migration for those too.
• Add a KVM_SET_CLOCK_GUEST ioctl which consumes the pvclock information back again. This should not only set the kvmclock_offset field, but also set the reference point { master_cycle_now, master_kernel_ns } as follows: • Sample the kernel’s CLOCK_MONOTONIC_RAW to create a new master_kernel_ns and master_cycle_now. • Convert the new master_cycle_now to a guest TSC. • Calculate the intended KVM clock with that guest TSC from the provided pvclock information. • Calculate the current KVM clock with that guest TSC using the new master_cycle_now and master_kernel_ns and kvmclock_offset as usual. • Adjust kvmclock_offset to correct for the delta between current and intended values. • Raise KVM_REQ_CLOCK_UPDATE on all vCPUs.
• Fix the broken __get_kvmclock() function to scale via the guest’s TSC frequency as it should. There isn’t necessarily a vCPU to use for this, so it’s OK for this to work only when the frequency has been set of the whole VM rather than only for individual vCPUs. Likewise kvm_get_wall_clock_epoch() which has the same bug.
• Fix all other cases where KVM reads the time in two places separately and then treats them as simultaneous.
• Fix the discontinuities in KVM_REQ_MASTERCLOCK_UPDATE by allowing kvmclock_offset to vary while the VM is running in master clock mode. Perhaps every call to pvclock_update_vm_gtod_copy() which starts in master clock mode should follow the same process as the proposed KVM_SET_CLOCK_GUEST to adjust the kvmclock_offset value which corresponds with the new reference point. As long as we don’t break in the case where something weird (host hibernation, etc.) happened to the TSC, and we actually want to trust kvmclock_offset. Maybe we should have a periodic work queue which keeps kvmclock_offset in sync with the KVM clock while the VM is in master clock mode?
• Correct the KVM documentation for TSC migration to take TSC scaling into account. Something like...
(SOURCE) • Sample both TAI and the (source) host TSC at an arbitrary time we shall call Tsrc: • Use adjtimex() to obtain tai_offset. • Use KVM_GET_CLOCK to read UTC time and host TSC (ignoring the actual kvm clock). These represent time Tsrc. • Use adjtimex() to obtain tai_offset again, looping back to the beginning if it changes. • Convert the UTC time to TAI by adding the tai_offset.
• ∀ vCPU: • Read the scaling information with the KVM_CPU_TSC_SCALE attribute. • Read the offset with the KVM_CPU_TSC_OFFSET attribute. • Calculate this vCPU’s TSC at time Tsrc, from the host TSC value.
• Use KVM_GET_CLOCK_GUEST to read the KVM clock (on vCPU0).
(DESTINATION) • Sample both TAI and the (destination) host TSC at a time we shall call Tdst: • Use adjtimex() to obtain tai_offset. • Use KVM_GET_CLOCK to read UTC time and host TSC. • Use adjtimex() to obtain tai_offset again, looping back to the beginning if it changes. • Convert the UTC time to TAI by adding the tai_offset.
• Calculate the time (in the TAI clock) elapsed between Tsrc and Tdst. Call this ΔT.
• ∀ vCPU: • Calculate this vCPU’s intended TSC value at time Tdst: • Given this vCPU’s TSC frequency, calculate the number of TSC ticks correponding to ΔT. • Add this to the vCPU TSC value calculated on the source • Read the scaling information on the current host with the KVM_CPU_TSC_SCALE attribute • Calculate this vCPU’s scaled TSC value corresponding to the host TSC at time Tdst without taking offsetting into account. • Set KVM_CPU_TSC_OFFSET to the delta between that and the intended TSC value.
• Use KVM_SET_CLOCK_GUEST to set the KVM clock (on vCPU0).
AFAIR, I copied check_clocksource() from existing code during that >
time.
The commit e440c5f2e ("KVM: selftests: Generalize check_clocksource() from kvm_clock_test") has introduced sys_clocksource_is_tsc(). Later it is renamed to sys_clocksource_is_based_on_tsc(). Any chance to re-use sys_clocksource_is_based_on_tsc()?
Yes I'm more than happy to change it to that. I was using your original mail as a reference and did not realise there was a utility present for this.
Is configure_scaled_tsc() anecessary? Or how about to make it an >
option/arg?
Then I will be able to test it on a VM/server without TSC scaling.
So if TSC scaling from 3GHz (host) -> 1.5GHz (guest) I do see a skew of ~3500ns after the update. Where as without scaling a delta can be seen but is roughly ~180ns.
In V2 I've adjusted the test so that now by default scaling won't take place, however if someone wants to test with it enabled they can pass "-s/--scale-tsc" to induce the greater delta.
Thanks you for the feedback, Jack Allister
On Wed, 2024-04-10 at 10:15 +0000, Allister, Jack wrote:
AFAIR, I copied check_clocksource() from existing code during that >
time.
The commit e440c5f2e ("KVM: selftests: Generalize check_clocksource() from kvm_clock_test") has introduced sys_clocksource_is_tsc(). Later it is renamed to sys_clocksource_is_based_on_tsc(). Any chance to re-use sys_clocksource_is_based_on_tsc()?
Yes I'm more than happy to change it to that. I was using your original mail as a reference and did not realise there was a utility present for this.
Is configure_scaled_tsc() anecessary? Or how about to make it an >
option/arg?
Then I will be able to test it on a VM/server without TSC scaling.
So if TSC scaling from 3GHz (host) -> 1.5GHz (guest) I do see a skew of ~3500ns after the update. Where as without scaling a delta can be seen but is roughly ~180ns.
I don't think it's as simple as "TSC scaling makes the drift larger". I suspect that's just the way the arithmetic precision works out for those frequencies. With other frequencies of host and guest you might find that it works out closer *with* the scaling.
Consider a graph of "time" in the Y axis, against the host TSC as the X axis. As an example, let's assume the host has a TSC frequency of 3GHz.
Each of the three definitions of the KVM clock (A based on CLOCK_MONOTONIC_RAW, B based on the guest TSC, C based directly on the host TSC) will have a gradient of *roughly* 1 ns per three ticks.
Due to arithmetic precision, the gradient of each is going to vary slightly. We hope that CLOCK_MONOTONIC_RAW is going to do the best, as the other two are limited by the precision of the pvclock ABI that's exposed to the guest. You can use http://david.woodhou.se/tsdrift.c to see where the latter two land, for different TSC frequencies.
$ ./tsdrift 2500000000 3000000000 | tail -1 TSC 259200000000000, guest TSC 215999999979883, guest ns 86399999971836 host ns 86399999979883 (delta -8047) $ ./tsdrift 2700000000 3000000000 | tail -1 TSC 259200000000000, guest TSC 233279999975860, guest ns 86399999983012 host ns 86399999979883 (delta 3129)
So after a day, let's assume CLOCK_MONOTONIC_RAW will have advanced by 86400 seconds. The KVM clock based on the host TSC will be 20µs slow, while a KVM clock based on a guest TSC frequency of 2.5GHz would be an *additional* 8µs slower. But a guest TSC frequency of 2.7GHz would actually run *faster* than the host-based one, and would only be 17µs behind reality.
Your test is measuring how *much* the host CLOCK_MONOTONIC_RAW (my definition A) drifts from definition B which is derived from the guest TSC.
It demonstrates the discontinuity that KVM_REQ_MASTERCLOCK_UPDATE introduces, by clamping the KVM clock back to the 'definition A' line.
Fixing that is in the TODO list I shared. Basically it involves realising that in use_master_clock mode, the delta between the KVM clock and CLOCK_MONOTONIC_RAW (ka->kvmclock_offset) is *varying* over time. So instead of just blindly using kvmclock_offset, we should *recalculate* it in precisely the way that your KVM_SET_CLOCK_GUEST does.
Having said all that... scaling from 3GHz to 1.5GHz *doesn't* lose any precision; it shouldn't make any difference. But I guess your host TSC isn't *really* 3GHz, it's measured against the PIT or something awful, and comes out at a shade above or below 3GHz, leading to a more interesting scaling factor?
In V2 I've adjusted the test so that now by default scaling won't take place, however if someone wants to test with it enabled they can pass "-s/--scale-tsc" to induce the greater delta.
Please do it automatically based on the availability of the feature.
On 4/8/2024 3:07 PM, Jack Allister wrote:
This test proves that there is an inherent KVM/PV clock drift away from the guest TSC when KVM decides to update the PV time information structure due to a KVM_REQ_MASTERCLOCK_UPDATE. This drift is exascerbated when a guest is using TSC scaling and running at a different frequency to the host TSC [1]. It also proves that KVM_[GS]ET_CLOCK_GUEST API is working to mitigate the drift from TSC to within ±1ns.
The test simply records the PVTI (PV time information) at time of guest creation, after KVM has updated it's mapped PVTI structure and once the correction has taken place.
A singular point in time is then recorded via the guest TSC and is used to calculate the a PV clock value using each of the 3 PVTI structures.
As seen below a drift of ~3500ns is observed if no correction has taken place after KVM has updated the PVTI via master clock update. However, after the correction a delta of at most 1ns can be seen.
- selftests: kvm: pvclock_test
- scaling tsc from 2999999KHz to 1499999KHz
- before=5038374946 uncorrected=5038371437 corrected=5038374945
- delta_uncorrected=3509 delta_corrected=1
Clocksource check code has been borrowed from [2].
Signed-off-by: Jack Allister jalliste@amazon.com CC: David Woodhouse dwmw2@infradead.org CC: Paul Durrant paul@xen.org
tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/pvclock_test.c | 223 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/pvclock_test.c
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 741c7dc16afc..02ee1205bbed 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -87,6 +87,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/pmu_counters_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test +TEST_GEN_PROGS_x86_64 += x86_64/pvclock_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/pvclock_test.c b/tools/testing/selftests/kvm/x86_64/pvclock_test.c new file mode 100644 index 000000000000..172ef4d19c60 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/pvclock_test.c @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0-only +/*
- Copyright © 2024, Amazon.com, Inc. or its affiliates.
- Tests for pvclock API
- KVM_SET_CLOCK_GUEST/KVM_GET_CLOCK_GUEST
- */
+#include <asm/pvclock.h> +#include <asm/pvclock-abi.h> +#include <sys/stat.h> +#include <stdint.h> +#include <stdio.h>
+#include "test_util.h" +#include "kvm_util.h" +#include "processor.h"
+enum {
- STAGE_FIRST_BOOT,
- STAGE_UNCORRECTED,
- STAGE_CORRECTED,
- NUM_STAGES
+};
+#define KVMCLOCK_GPA 0xc0000000ull +#define KVMCLOCK_SIZE sizeof(struct pvclock_vcpu_time_info)
+static void trigger_pvti_update(vm_paddr_t pvti_pa) +{
- /*
* We need a way to trigger KVM to update the fields
* in the PV time info. The easiest way to do this is
* to temporarily switch to the old KVM system time
* method and then switch back to the new one.
*/
- wrmsr(MSR_KVM_SYSTEM_TIME, pvti_pa | KVM_MSR_ENABLED);
- wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED);
+}
+static void guest_code(vm_paddr_t pvti_pa) +{
- struct pvclock_vcpu_time_info *pvti_va =
(struct pvclock_vcpu_time_info *)pvti_pa;
- struct pvclock_vcpu_time_info pvti_boot;
- struct pvclock_vcpu_time_info pvti_uncorrected;
- struct pvclock_vcpu_time_info pvti_corrected;
- uint64_t cycles_boot;
- uint64_t cycles_uncorrected;
- uint64_t cycles_corrected;
- uint64_t tsc_guest;
- /*
* Setup the KVMCLOCK in the guest & store the original
* PV time structure that is used.
*/
- wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED);
- pvti_boot = *pvti_va;
- GUEST_SYNC(STAGE_FIRST_BOOT);
- /*
* Trigger an update of the PVTI, if we calculate
* the KVM clock using this structure we'll see
* a drift from the TSC.
*/
- trigger_pvti_update(pvti_pa);
- pvti_uncorrected = *pvti_va;
- GUEST_SYNC(STAGE_UNCORRECTED);
- /*
* The test should have triggered the correction by this
* point in time. We have a copy of each of the PVTI structs
* at each stage now.
*
* Let's sample the timestamp at a SINGLE point in time and
* then calculate what the KVM clock would be using the PVTI
* from each stage.
*
* Then return each of these values to the tester.
*/
- pvti_corrected = *pvti_va;
- tsc_guest = rdtsc();
- cycles_boot = __pvclock_read_cycles(&pvti_boot, tsc_guest);
- cycles_uncorrected = __pvclock_read_cycles(&pvti_uncorrected, tsc_guest);
- cycles_corrected = __pvclock_read_cycles(&pvti_corrected, tsc_guest);
- GUEST_SYNC_ARGS(STAGE_CORRECTED, cycles_boot, cycles_uncorrected,
cycles_corrected, 0);
+}
+static void run_test(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{
- struct ucall uc;
- uint64_t ucall_reason;
- struct pvclock_vcpu_time_info pvti_before;
- uint64_t before, uncorrected, corrected;
- int64_t delta_uncorrected, delta_corrected;
- /* Loop through each stage of the test. */
- while (true) {
/* Start/restart the running vCPU code. */
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
/* Retrieve and verify our stage. */
ucall_reason = get_ucall(vcpu, &uc);
TEST_ASSERT(ucall_reason == UCALL_SYNC,
"Unhandled ucall reason=%lu",
ucall_reason);
/* Run host specific code relating to stage. */
switch (uc.args[1]) {
case STAGE_FIRST_BOOT:
/* Store the KVM clock values before an update. */
vm_ioctl(vm, KVM_GET_CLOCK_GUEST, &pvti_before);
/* Sleep for a set amount of time to induce drift. */
sleep(5);
break;
case STAGE_UNCORRECTED:
/* Restore the KVM clock values. */
vm_ioctl(vm, KVM_SET_CLOCK_GUEST, &pvti_before);
break;
case STAGE_CORRECTED:
/* Query the clock information and verify delta. */
before = uc.args[2];
uncorrected = uc.args[3];
corrected = uc.args[4];
delta_uncorrected = before - uncorrected;
delta_corrected = before - corrected;
pr_info("before=%lu uncorrected=%lu corrected=%lu\n",
before, uncorrected, corrected);
pr_info("delta_uncorrected=%ld delta_corrected=%ld\n",
delta_uncorrected, delta_corrected);
TEST_ASSERT((delta_corrected <= 1) && (delta_corrected >= -1),
"larger than expected delta detected = %ld", delta_corrected);
I'm wondering what's the underling theory that we definitely can achieve ±1ns accuracy? I tested it on a Sapphire Rapids @2100MHz TSC frequency, and I can see delta_corrected=2 in ~2% cases.
On 19 April 2024 18:13:16 BST, "Chen, Zide" zide.chen@intel.com wrote:
I'm wondering what's the underling theory that we definitely can achieve ±1ns accuracy? I tested it on a Sapphire Rapids @2100MHz TSC frequency, and I can see delta_corrected=2 in ~2% cases.
Hm. Thanks for testing!
So the KVM clock is based on the guest TSC. Given a delta between the guest TSC T and some reference point in time R, the KVM clock is expressed as a(T-R)+r, where little r is the value of the KVM clock when the guest TSC was R, and (a) is the rate of the guest TSC.
When set the clock with KVM_SET_CLOCK_GUEST, we are changing the values of R and r to a new point in time. Call the new ones Q and q respectively.
But we calculate precisely (within 1ns at least) what the KVM clock would have been with the *old* formula, and adjust our new offset (q) so that at our new reference TSC value Q, the formulae give exactly the same result.
And because the *rates* are the same, they should continue to give the same results, ±1ns.
Or such *was* my theory, at least.
Would be interesting to see it disproven with actual numbers for the old+new pvclock structs, so I can understand where the logic goes wrong.
Were you using frequency scaling?
On 4/19/2024 12:34 PM, David Woodhouse wrote:
On 19 April 2024 18:13:16 BST, "Chen, Zide" zide.chen@intel.com wrote:
I'm wondering what's the underling theory that we definitely can achieve ±1ns accuracy? I tested it on a Sapphire Rapids @2100MHz TSC frequency, and I can see delta_corrected=2 in ~2% cases.
Hm. Thanks for testing!
So the KVM clock is based on the guest TSC. Given a delta between the guest TSC T and some reference point in time R, the KVM clock is expressed as a(T-R)+r, where little r is the value of the KVM clock when the guest TSC was R, and (a) is the rate of the guest TSC.
When set the clock with KVM_SET_CLOCK_GUEST, we are changing the values of R and r to a new point in time. Call the new ones Q and q respectively.
But we calculate precisely (within 1ns at least) what the KVM clock would have been with the *old* formula, and adjust our new offset (q) so that at our new reference TSC value Q, the formulae give exactly the same result.
And because the *rates* are the same, they should continue to give the same results, ±1ns.
Or such *was* my theory, at least.
Thanks for the explanation.
Would be interesting to see it disproven with actual numbers for the old+new pvclock structs, so I can understand where the logic goes wrong.
Were you using frequency scaling?
I can see similar ~2% failure ratio w/ or w/o commenting out configure_scaled_tsc().
linux-kselftest-mirror@lists.linaro.org