Skip to content

Commit 201142d

Browse files
vittyvksean-jc
authored andcommitted
KVM: selftests: Compare wall time from xen shinfo against KVM_GET_CLOCK
xen_shinfo_test is observed to be flaky failing sporadically with "VM time too old". With min_ts/max_ts debug print added: Wall clock (v 3269818) 1704906491.986255664 Time info 1: v 1282712 tsc 33530585736 time 14014430025 mul 3587552223 shift 4294967295 flags 1 Time info 2: v 1282712 tsc 33530585736 time 14014430025 mul 3587552223 shift 4294967295 flags 1 min_ts: 1704906491.986312153 max_ts: 1704906506.001006963 ==== Test Assertion Failure ==== x86_64/xen_shinfo_test.c:1003: cmp_timespec(&min_ts, &vm_ts) <= 0 pid=32724 tid=32724 errno=4 - Interrupted system call 1 0x00000000004030ad: main at xen_shinfo_test.c:1003 2 0x00007fca6b23feaf: ?? ??:0 3 0x00007fca6b23ff5f: ?? ??:0 4 0x0000000000405e04: _start at ??:? VM time too old The test compares wall clock data from shinfo (which is the output of kvm_get_wall_clock_epoch()) against clock_gettime(CLOCK_REALTIME) in the host system before the VM is created. In the example above, it compares shinfo: 1704906491.986255664 vs min_ts: 1704906491.986312153 and fails as the later is greater than the former. While this sounds like a sane test, it doesn't pass reality check: kvm_get_wall_clock_epoch() calculates guest's epoch (realtime when the guest was created) by subtracting kvmclock from the current realtime and the calculation happens when shinfo is setup. The problem is that kvmclock is a raw clock and realtime clock is affected by NTP. This means that if realtime ticks with a slightly reduced frequency, "guest's epoch" calculated by kvm_get_wall_clock_epoch() will actually tick backwards! This is not a big issue from guest's perspective as the guest can't really observe this but this epoch can't be compared with a fixed clock_gettime() on the host. Replace the check with comparing wall clock data from shinfo to KVM_GET_CLOCK. The later gives both realtime and kvmclock so guest's epoch can be calculated by subtraction. Note, CLOCK_REALTIME is susceptible to leap seconds jumps but there's no better alternative in KVM at this moment. Leave a comment and accept 1s delta. Reported-by: Jan Richter <[email protected]> Signed-off-by: Vitaly Kuznetsov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent d85465f commit 201142d

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -380,20 +380,6 @@ static void guest_code(void)
380380
GUEST_SYNC(TEST_DONE);
381381
}
382382

383-
static int cmp_timespec(struct timespec *a, struct timespec *b)
384-
{
385-
if (a->tv_sec > b->tv_sec)
386-
return 1;
387-
else if (a->tv_sec < b->tv_sec)
388-
return -1;
389-
else if (a->tv_nsec > b->tv_nsec)
390-
return 1;
391-
else if (a->tv_nsec < b->tv_nsec)
392-
return -1;
393-
else
394-
return 0;
395-
}
396-
397383
static struct shared_info *shinfo;
398384
static struct vcpu_info *vinfo;
399385
static struct kvm_vcpu *vcpu;
@@ -449,7 +435,6 @@ static void *juggle_shinfo_state(void *arg)
449435

450436
int main(int argc, char *argv[])
451437
{
452-
struct timespec min_ts, max_ts, vm_ts;
453438
struct kvm_xen_hvm_attr evt_reset;
454439
struct kvm_vm *vm;
455440
pthread_t thread;
@@ -468,8 +453,6 @@ int main(int argc, char *argv[])
468453
bool do_evtchn_tests = do_eventfd_tests && !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND);
469454
bool has_shinfo_hva = !!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA);
470455

471-
clock_gettime(CLOCK_REALTIME, &min_ts);
472-
473456
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
474457

475458
/* Map a region for the shared_info page */
@@ -1010,7 +993,6 @@ int main(int argc, char *argv[])
1010993
vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset);
1011994

1012995
alarm(0);
1013-
clock_gettime(CLOCK_REALTIME, &max_ts);
1014996

1015997
/*
1016998
* Just a *really* basic check that things are being put in the
@@ -1019,6 +1001,8 @@ int main(int argc, char *argv[])
10191001
*/
10201002
struct pvclock_wall_clock *wc;
10211003
struct pvclock_vcpu_time_info *ti, *ti2;
1004+
struct kvm_clock_data kcdata;
1005+
long long delta;
10221006

10231007
wc = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0xc00);
10241008
ti = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0x40 + 0x20);
@@ -1034,12 +1018,34 @@ int main(int argc, char *argv[])
10341018
ti2->tsc_shift, ti2->flags);
10351019
}
10361020

1037-
vm_ts.tv_sec = wc->sec;
1038-
vm_ts.tv_nsec = wc->nsec;
10391021
TEST_ASSERT(wc->version && !(wc->version & 1),
10401022
"Bad wallclock version %x", wc->version);
1041-
TEST_ASSERT(cmp_timespec(&min_ts, &vm_ts) <= 0, "VM time too old");
1042-
TEST_ASSERT(cmp_timespec(&max_ts, &vm_ts) >= 0, "VM time too new");
1023+
1024+
vm_ioctl(vm, KVM_GET_CLOCK, &kcdata);
1025+
1026+
if (kcdata.flags & KVM_CLOCK_REALTIME) {
1027+
if (verbose) {
1028+
printf("KVM_GET_CLOCK clock: %lld.%09lld\n",
1029+
kcdata.clock / NSEC_PER_SEC, kcdata.clock % NSEC_PER_SEC);
1030+
printf("KVM_GET_CLOCK realtime: %lld.%09lld\n",
1031+
kcdata.realtime / NSEC_PER_SEC, kcdata.realtime % NSEC_PER_SEC);
1032+
}
1033+
1034+
delta = (wc->sec * NSEC_PER_SEC + wc->nsec) - (kcdata.realtime - kcdata.clock);
1035+
1036+
/*
1037+
* KVM_GET_CLOCK gives CLOCK_REALTIME which jumps on leap seconds updates but
1038+
* unfortunately KVM doesn't currently offer a CLOCK_TAI alternative. Accept 1s
1039+
* delta as testing clock accuracy is not the goal here. The test just needs to
1040+
* check that the value in shinfo is somewhat sane.
1041+
*/
1042+
TEST_ASSERT(llabs(delta) < NSEC_PER_SEC,
1043+
"Guest's epoch from shinfo %d.%09d differs from KVM_GET_CLOCK %lld.%lld",
1044+
wc->sec, wc->nsec, (kcdata.realtime - kcdata.clock) / NSEC_PER_SEC,
1045+
(kcdata.realtime - kcdata.clock) % NSEC_PER_SEC);
1046+
} else {
1047+
pr_info("Missing KVM_CLOCK_REALTIME, skipping shinfo epoch sanity check\n");
1048+
}
10431049

10441050
TEST_ASSERT(ti->version && !(ti->version & 1),
10451051
"Bad time_info version %x", ti->version);

0 commit comments

Comments
 (0)