* [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-10 17:21 ` Marcelo Tosatti
0 siblings, 0 replies; 22+ messages in thread
From: Marcelo Tosatti @ 2016-12-10 17:21 UTC (permalink / raw)
To: kvm
Cc: qemu-devel, Dr David Alan Gilbert, Paolo Bonzini, Juan Quintela,
Radim Krcmar, Eduardo Habkost, Marcelo Tosatti
[-- Attachment #1: kvmclock-advance-clock.patch --]
[-- Type: text/plain, Size: 8220 bytes --]
Check for KVM_CAP_ADJUST_CLOCK capability KVM_CLOCK_TSC_STABLE, which
indicates that KVM_GET_CLOCK returns a value as seen by the guest at
that moment.
For new machine types, use this value rather than reading
from guest memory.
This reduces kvmclock difference on migration from 5s to 0.1s
(when max_downtime == 5s).
Note: pre_save contradicts the following comment
/*
* If the VM is stopped, declare the clock state valid to
* avoid re-reading it on next vmsave (which would return
* a different value). Will be reset when the VM is continued.
*/
But the comment is bogus: vm_state_change is never called twice in a row
with running=0 or running=1.
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
---
hw/i386/kvm/clock.c | 107 ++++++++++++++++++++++++++++++++++++++++++-------
include/hw/i386/pc.h | 5 ++
target-i386/kvm.c | 7 +++
target-i386/kvm_i386.h | 1
4 files changed, 106 insertions(+), 14 deletions(-)
v2:
- improve variable names (Juan)
- consolidate code on kvm_get_clock function (Paolo)
- return mach_use_reliable_get_clock from needed function (Paolo)
v3:
- simplify check for src_use_reliable_get_clock (Eduardo)
- move src_use_reliable_get_clock initialization to realize (Eduardo)
v4:
- have kvm_get_clock and clock_is_reliable assignments synchronized (Eduardo)
- add comment to the reasoning of kvmclock_pre_save (Eduardo)
Index: qemu-mig-advance-clock/hw/i386/kvm/clock.c
===================================================================
--- qemu-mig-advance-clock.orig/hw/i386/kvm/clock.c 2016-11-17 15:07:11.220632761 -0200
+++ qemu-mig-advance-clock/hw/i386/kvm/clock.c 2016-12-10 13:57:58.115983966 -0200
@@ -36,6 +36,13 @@
uint64_t clock;
bool clock_valid;
+
+ /* whether machine type supports reliable get clock */
+ bool mach_use_reliable_get_clock;
+
+ /* whether the 'clock' value was obtained in a host with
+ * reliable KVM_GET_CLOCK */
+ bool clock_is_reliable;
} KVMClockState;
struct pvclock_vcpu_time_info {
@@ -81,6 +88,19 @@
return nsec + time.system_time;
}
+static uint64_t kvm_get_clock(void)
+{
+ struct kvm_clock_data data;
+ int ret;
+
+ ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
+ if (ret < 0) {
+ fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
+ abort();
+ }
+ return data.clock;
+}
+
static void kvmclock_vm_state_change(void *opaque, int running,
RunState state)
{
@@ -91,15 +111,23 @@
if (running) {
struct kvm_clock_data data = {};
- uint64_t time_at_migration = kvmclock_current_nsec(s);
+ uint64_t pvclock_via_mem = 0;
- s->clock_valid = false;
+ /*
+ * If the host where s->clock was read did not support reliable
+ * KVM_GET_CLOCK, read kvmclock value from memory.
+ */
+ if (!s->clock_is_reliable) {
+ pvclock_via_mem = kvmclock_current_nsec(s);
+ }
- /* We can't rely on the migrated clock value, just discard it */
- if (time_at_migration) {
- s->clock = time_at_migration;
+ /* We can't rely on the saved clock value, just discard it */
+ if (pvclock_via_mem) {
+ s->clock = pvclock_via_mem;
}
+ s->clock_valid = false;
+
data.clock = s->clock;
ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
if (ret < 0) {
@@ -120,8 +148,6 @@
}
}
} else {
- struct kvm_clock_data data;
- int ret;
if (s->clock_valid) {
return;
@@ -129,13 +155,11 @@
kvm_synchronize_all_tsc();
- ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
- if (ret < 0) {
- fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
- abort();
- }
- s->clock = data.clock;
-
+ s->clock = kvm_get_clock();
+ /* any code that sets s->clock needs to ensure clock_is_reliable
+ * is correctly set.
+ */
+ s->clock_is_reliable = kvm_has_adjust_clock_stable();
/*
* If the VM is stopped, declare the clock state valid to
* avoid re-reading it on next vmsave (which would return
@@ -149,25 +173,80 @@
{
KVMClockState *s = KVM_CLOCK(dev);
+ if (kvm_has_adjust_clock_stable()) {
+ s->clock_is_reliable = true;
+ }
+
qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
}
+static bool kvmclock_clock_is_reliable_needed(void *opaque)
+{
+ KVMClockState *s = opaque;
+
+ return s->mach_use_reliable_get_clock;
+}
+
+static const VMStateDescription kvmclock_reliable_get_clock = {
+ .name = "kvmclock/clock_is_reliable",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .needed = kvmclock_clock_is_reliable_needed,
+ .fields = (VMStateField[]) {
+ VMSTATE_BOOL(clock_is_reliable, KVMClockState),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+/*
+ * When migrating, read the clock just before migration,
+ * so that the guest clock counts during the events
+ * between:
+ *
+ * * vm_stop()
+ * *
+ * * pre_save()
+ *
+ * This reduces kvmclock difference on migration from 5s
+ * to 0.1s (when max_downtime == 5s), because sending the
+ * final pages of memory (which happens between vm_stop()
+ * and pre_save()) takes max_downtime.
+ */
+static void kvmclock_pre_save(void *opaque)
+{
+ KVMClockState *s = opaque;
+
+ s->clock = kvm_get_clock();
+}
+
static const VMStateDescription kvmclock_vmsd = {
.name = "kvmclock",
.version_id = 1,
.minimum_version_id = 1,
+ .pre_save = kvmclock_pre_save,
.fields = (VMStateField[]) {
VMSTATE_UINT64(clock, KVMClockState),
VMSTATE_END_OF_LIST()
+ },
+ .subsections = (const VMStateDescription * []) {
+ &kvmclock_reliable_get_clock,
+ NULL
}
};
+static Property kvmclock_properties[] = {
+ DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
+ mach_use_reliable_get_clock, true),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
static void kvmclock_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
dc->realize = kvmclock_realize;
dc->vmsd = &kvmclock_vmsd;
+ dc->props = kvmclock_properties;
}
static const TypeInfo kvmclock_info = {
Index: qemu-mig-advance-clock/include/hw/i386/pc.h
===================================================================
--- qemu-mig-advance-clock.orig/include/hw/i386/pc.h 2016-11-17 15:07:11.220632761 -0200
+++ qemu-mig-advance-clock/include/hw/i386/pc.h 2016-11-17 15:08:58.096791416 -0200
@@ -370,6 +370,11 @@
#define PC_COMPAT_2_7 \
HW_COMPAT_2_7 \
{\
+ .driver = "kvmclock",\
+ .property = "x-mach-use-reliable-get-clock",\
+ .value = "off",\
+ },\
+ {\
.driver = TYPE_X86_CPU,\
.property = "l3-cache",\
.value = "off",\
Index: qemu-mig-advance-clock/target-i386/kvm.c
===================================================================
--- qemu-mig-advance-clock.orig/target-i386/kvm.c 2016-11-17 15:07:11.221632762 -0200
+++ qemu-mig-advance-clock/target-i386/kvm.c 2016-11-17 15:07:15.867639659 -0200
@@ -117,6 +117,13 @@
return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
}
+bool kvm_has_adjust_clock_stable(void)
+{
+ int ret = kvm_check_extension(kvm_state, KVM_CAP_ADJUST_CLOCK);
+
+ return (ret == KVM_CLOCK_TSC_STABLE);
+}
+
bool kvm_allows_irq0_override(void)
{
return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
Index: qemu-mig-advance-clock/target-i386/kvm_i386.h
===================================================================
--- qemu-mig-advance-clock.orig/target-i386/kvm_i386.h 2016-11-17 15:07:11.222632764 -0200
+++ qemu-mig-advance-clock/target-i386/kvm_i386.h 2016-11-17 15:07:15.867639659 -0200
@@ -17,6 +17,7 @@
bool kvm_allows_irq0_override(void);
bool kvm_has_smm(void);
+bool kvm_has_adjust_clock_stable(void);
void kvm_synchronize_all_tsc(void);
void kvm_arch_reset_vcpu(X86CPU *cs);
void kvm_arch_do_init_vcpu(X86CPU *cs);
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-10 17:21 ` [Qemu-devel] " Marcelo Tosatti
(?)
@ 2016-12-12 7:36 ` Pankaj Gupta
2016-12-12 11:22 ` Marcelo Tosatti
-1 siblings, 1 reply; 22+ messages in thread
From: Pankaj Gupta @ 2016-12-12 7:36 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: kvm, Eduardo Habkost, Juan Quintela, Radim Krcmar, qemu-devel,
Dr David Alan Gilbert, Paolo Bonzini
Hello Marcelo,
>
> Check for KVM_CAP_ADJUST_CLOCK capability KVM_CLOCK_TSC_STABLE, which
> indicates that KVM_GET_CLOCK returns a value as seen by the guest at
> that moment.
>
> For new machine types, use this value rather than reading
> from guest memory.
>
> This reduces kvmclock difference on migration from 5s to 0.1s
> (when max_downtime == 5s).
>
> Note: pre_save contradicts the following comment
> /*
> * If the VM is stopped, declare the clock state valid to
> * avoid re-reading it on next vmsave (which would return
> * a different value). Will be reset when the VM is continued.
> */
> But the comment is bogus: vm_state_change is never called twice in a row
> with running=0 or running=1.
>
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
>
> ---
> hw/i386/kvm/clock.c | 107
> ++++++++++++++++++++++++++++++++++++++++++-------
> include/hw/i386/pc.h | 5 ++
> target-i386/kvm.c | 7 +++
> target-i386/kvm_i386.h | 1
> 4 files changed, 106 insertions(+), 14 deletions(-)
>
> v2:
> - improve variable names (Juan)
> - consolidate code on kvm_get_clock function (Paolo)
> - return mach_use_reliable_get_clock from needed function (Paolo)
> v3:
> - simplify check for src_use_reliable_get_clock (Eduardo)
> - move src_use_reliable_get_clock initialization to realize (Eduardo)
>
> v4:
> - have kvm_get_clock and clock_is_reliable assignments synchronized (Eduardo)
> - add comment to the reasoning of kvmclock_pre_save (Eduardo)
>
>
> Index: qemu-mig-advance-clock/hw/i386/kvm/clock.c
> ===================================================================
> --- qemu-mig-advance-clock.orig/hw/i386/kvm/clock.c 2016-11-17
> 15:07:11.220632761 -0200
> +++ qemu-mig-advance-clock/hw/i386/kvm/clock.c 2016-12-10 13:57:58.115983966
> -0200
> @@ -36,6 +36,13 @@
>
> uint64_t clock;
> bool clock_valid;
> +
> + /* whether machine type supports reliable get clock */
> + bool mach_use_reliable_get_clock;
> +
> + /* whether the 'clock' value was obtained in a host with
> + * reliable KVM_GET_CLOCK */
> + bool clock_is_reliable;
> } KVMClockState;
>
> struct pvclock_vcpu_time_info {
> @@ -81,6 +88,19 @@
> return nsec + time.system_time;
> }
>
> +static uint64_t kvm_get_clock(void)
> +{
> + struct kvm_clock_data data;
> + int ret;
> +
> + ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> + if (ret < 0) {
> + fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> + abort();
> + }
> + return data.clock;
> +}
> +
> static void kvmclock_vm_state_change(void *opaque, int running,
> RunState state)
> {
> @@ -91,15 +111,23 @@
>
> if (running) {
> struct kvm_clock_data data = {};
> - uint64_t time_at_migration = kvmclock_current_nsec(s);
> + uint64_t pvclock_via_mem = 0;
>
> - s->clock_valid = false;
> + /*
> + * If the host where s->clock was read did not support reliable
> + * KVM_GET_CLOCK, read kvmclock value from memory.
> + */
> + if (!s->clock_is_reliable) {
> + pvclock_via_mem = kvmclock_current_nsec(s);
> + }
>
> - /* We can't rely on the migrated clock value, just discard it */
> - if (time_at_migration) {
> - s->clock = time_at_migration;
> + /* We can't rely on the saved clock value, just discard it */
> + if (pvclock_via_mem) {
> + s->clock = pvclock_via_mem;
> }
>
> + s->clock_valid = false;
> +
> data.clock = s->clock;
> ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
> if (ret < 0) {
> @@ -120,8 +148,6 @@
> }
> }
> } else {
> - struct kvm_clock_data data;
> - int ret;
>
> if (s->clock_valid) {
> return;
> @@ -129,13 +155,11 @@
>
> kvm_synchronize_all_tsc();
>
> - ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> - if (ret < 0) {
> - fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> - abort();
> - }
> - s->clock = data.clock;
> -
> + s->clock = kvm_get_clock();
> + /* any code that sets s->clock needs to ensure clock_is_reliable
> + * is correctly set.
> + */
> + s->clock_is_reliable = kvm_has_adjust_clock_stable();
Is it required to call 'kvm_has_adjust_clock_stable' here. Is there any chance
of getting different value then in 'realize'?
> /*
> * If the VM is stopped, declare the clock state valid to
> * avoid re-reading it on next vmsave (which would return
> @@ -149,25 +173,80 @@
> {
> KVMClockState *s = KVM_CLOCK(dev);
>
> + if (kvm_has_adjust_clock_stable()) {
> + s->clock_is_reliable = true;
> + }
> +
> qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> }
>
> +static bool kvmclock_clock_is_reliable_needed(void *opaque)
> +{
> + KVMClockState *s = opaque;
> +
> + return s->mach_use_reliable_get_clock;
> +}
> +
> +static const VMStateDescription kvmclock_reliable_get_clock = {
> + .name = "kvmclock/clock_is_reliable",
> + .version_id = 1,
> + .minimum_version_id = 1,
> + .needed = kvmclock_clock_is_reliable_needed,
> + .fields = (VMStateField[]) {
> + VMSTATE_BOOL(clock_is_reliable, KVMClockState),
> + VMSTATE_END_OF_LIST()
> + }
> +};
> +
> +/*
> + * When migrating, read the clock just before migration,
> + * so that the guest clock counts during the events
> + * between:
> + *
> + * * vm_stop()
> + * *
> + * * pre_save()
> + *
> + * This reduces kvmclock difference on migration from 5s
> + * to 0.1s (when max_downtime == 5s), because sending the
> + * final pages of memory (which happens between vm_stop()
> + * and pre_save()) takes max_downtime.
> + */
> +static void kvmclock_pre_save(void *opaque)
> +{
> + KVMClockState *s = opaque;
> +
> + s->clock = kvm_get_clock();
> +}
> +
> static const VMStateDescription kvmclock_vmsd = {
> .name = "kvmclock",
> .version_id = 1,
> .minimum_version_id = 1,
> + .pre_save = kvmclock_pre_save,
> .fields = (VMStateField[]) {
> VMSTATE_UINT64(clock, KVMClockState),
> VMSTATE_END_OF_LIST()
> + },
> + .subsections = (const VMStateDescription * []) {
> + &kvmclock_reliable_get_clock,
> + NULL
> }
> };
>
> +static Property kvmclock_properties[] = {
> + DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
> + mach_use_reliable_get_clock, true),
> + DEFINE_PROP_END_OF_LIST(),
> +};
> +
> static void kvmclock_class_init(ObjectClass *klass, void *data)
> {
> DeviceClass *dc = DEVICE_CLASS(klass);
>
> dc->realize = kvmclock_realize;
> dc->vmsd = &kvmclock_vmsd;
> + dc->props = kvmclock_properties;
> }
>
> static const TypeInfo kvmclock_info = {
> Index: qemu-mig-advance-clock/include/hw/i386/pc.h
> ===================================================================
> --- qemu-mig-advance-clock.orig/include/hw/i386/pc.h 2016-11-17
> 15:07:11.220632761 -0200
> +++ qemu-mig-advance-clock/include/hw/i386/pc.h 2016-11-17 15:08:58.096791416
> -0200
> @@ -370,6 +370,11 @@
> #define PC_COMPAT_2_7 \
> HW_COMPAT_2_7 \
> {\
> + .driver = "kvmclock",\
> + .property = "x-mach-use-reliable-get-clock",\
> + .value = "off",\
> + },\
> + {\
> .driver = TYPE_X86_CPU,\
> .property = "l3-cache",\
> .value = "off",\
> Index: qemu-mig-advance-clock/target-i386/kvm.c
> ===================================================================
> --- qemu-mig-advance-clock.orig/target-i386/kvm.c 2016-11-17
> 15:07:11.221632762 -0200
> +++ qemu-mig-advance-clock/target-i386/kvm.c 2016-11-17 15:07:15.867639659
> -0200
> @@ -117,6 +117,13 @@
> return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
> }
>
> +bool kvm_has_adjust_clock_stable(void)
> +{
> + int ret = kvm_check_extension(kvm_state, KVM_CAP_ADJUST_CLOCK);
> +
> + return (ret == KVM_CLOCK_TSC_STABLE);
> +}
> +
> bool kvm_allows_irq0_override(void)
> {
> return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
> Index: qemu-mig-advance-clock/target-i386/kvm_i386.h
> ===================================================================
> --- qemu-mig-advance-clock.orig/target-i386/kvm_i386.h 2016-11-17
> 15:07:11.222632764 -0200
> +++ qemu-mig-advance-clock/target-i386/kvm_i386.h 2016-11-17
> 15:07:15.867639659 -0200
> @@ -17,6 +17,7 @@
>
> bool kvm_allows_irq0_override(void);
> bool kvm_has_smm(void);
> +bool kvm_has_adjust_clock_stable(void);
> void kvm_synchronize_all_tsc(void);
> void kvm_arch_reset_vcpu(X86CPU *cs);
> void kvm_arch_do_init_vcpu(X86CPU *cs);
>
>
>
>
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-12 7:36 ` Pankaj Gupta
@ 2016-12-12 11:22 ` Marcelo Tosatti
2016-12-12 14:24 ` Pankaj Gupta
0 siblings, 1 reply; 22+ messages in thread
From: Marcelo Tosatti @ 2016-12-12 11:22 UTC (permalink / raw)
To: Pankaj Gupta
Cc: kvm, Eduardo Habkost, Juan Quintela, Radim Krcmar, qemu-devel,
Dr David Alan Gilbert, Paolo Bonzini
On Mon, Dec 12, 2016 at 02:36:55AM -0500, Pankaj Gupta wrote:
>
> Hello Marcelo,
Hi Pankaj,
> > Check for KVM_CAP_ADJUST_CLOCK capability KVM_CLOCK_TSC_STABLE, which
> > indicates that KVM_GET_CLOCK returns a value as seen by the guest at
> > that moment.
> >
> > For new machine types, use this value rather than reading
> > from guest memory.
> >
> > This reduces kvmclock difference on migration from 5s to 0.1s
> > (when max_downtime == 5s).
> >
> > Note: pre_save contradicts the following comment
> > /*
> > * If the VM is stopped, declare the clock state valid to
> > * avoid re-reading it on next vmsave (which would return
> > * a different value). Will be reset when the VM is continued.
> > */
> > But the comment is bogus: vm_state_change is never called twice in a row
> > with running=0 or running=1.
> >
> > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> >
> > ---
> > hw/i386/kvm/clock.c | 107
> > ++++++++++++++++++++++++++++++++++++++++++-------
> > include/hw/i386/pc.h | 5 ++
> > target-i386/kvm.c | 7 +++
> > target-i386/kvm_i386.h | 1
> > 4 files changed, 106 insertions(+), 14 deletions(-)
> >
> > v2:
> > - improve variable names (Juan)
> > - consolidate code on kvm_get_clock function (Paolo)
> > - return mach_use_reliable_get_clock from needed function (Paolo)
> > v3:
> > - simplify check for src_use_reliable_get_clock (Eduardo)
> > - move src_use_reliable_get_clock initialization to realize (Eduardo)
> >
> > v4:
> > - have kvm_get_clock and clock_is_reliable assignments synchronized (Eduardo)
> > - add comment to the reasoning of kvmclock_pre_save (Eduardo)
> >
> >
> > Index: qemu-mig-advance-clock/hw/i386/kvm/clock.c
> > ===================================================================
> > --- qemu-mig-advance-clock.orig/hw/i386/kvm/clock.c 2016-11-17
> > 15:07:11.220632761 -0200
> > +++ qemu-mig-advance-clock/hw/i386/kvm/clock.c 2016-12-10 13:57:58.115983966
> > -0200
> > @@ -36,6 +36,13 @@
> >
> > uint64_t clock;
> > bool clock_valid;
> > +
> > + /* whether machine type supports reliable get clock */
> > + bool mach_use_reliable_get_clock;
> > +
> > + /* whether the 'clock' value was obtained in a host with
> > + * reliable KVM_GET_CLOCK */
> > + bool clock_is_reliable;
> > } KVMClockState;
> >
> > struct pvclock_vcpu_time_info {
> > @@ -81,6 +88,19 @@
> > return nsec + time.system_time;
> > }
> >
> > +static uint64_t kvm_get_clock(void)
> > +{
> > + struct kvm_clock_data data;
> > + int ret;
> > +
> > + ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> > + if (ret < 0) {
> > + fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> > + abort();
> > + }
> > + return data.clock;
> > +}
> > +
> > static void kvmclock_vm_state_change(void *opaque, int running,
> > RunState state)
> > {
> > @@ -91,15 +111,23 @@
> >
> > if (running) {
> > struct kvm_clock_data data = {};
> > - uint64_t time_at_migration = kvmclock_current_nsec(s);
> > + uint64_t pvclock_via_mem = 0;
> >
> > - s->clock_valid = false;
> > + /*
> > + * If the host where s->clock was read did not support reliable
> > + * KVM_GET_CLOCK, read kvmclock value from memory.
> > + */
> > + if (!s->clock_is_reliable) {
> > + pvclock_via_mem = kvmclock_current_nsec(s);
> > + }
> >
> > - /* We can't rely on the migrated clock value, just discard it */
> > - if (time_at_migration) {
> > - s->clock = time_at_migration;
> > + /* We can't rely on the saved clock value, just discard it */
> > + if (pvclock_via_mem) {
> > + s->clock = pvclock_via_mem;
> > }
> >
> > + s->clock_valid = false;
> > +
> > data.clock = s->clock;
> > ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
> > if (ret < 0) {
> > @@ -120,8 +148,6 @@
> > }
> > }
> > } else {
> > - struct kvm_clock_data data;
> > - int ret;
> >
> > if (s->clock_valid) {
> > return;
> > @@ -129,13 +155,11 @@
> >
> > kvm_synchronize_all_tsc();
> >
> > - ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> > - if (ret < 0) {
> > - fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> > - abort();
> > - }
> > - s->clock = data.clock;
> > -
> > + s->clock = kvm_get_clock();
> > + /* any code that sets s->clock needs to ensure clock_is_reliable
> > + * is correctly set.
> > + */
> > + s->clock_is_reliable = kvm_has_adjust_clock_stable();
>
> Is it required to call 'kvm_has_adjust_clock_stable' here. Is there any chance
> of getting different value then in 'realize'?
Yes it can happen: migration overwrites s->clock_is_reliable value (but
you can't see that from grepping).
> > * If the VM is stopped, declare the clock state valid to
> > * avoid re-reading it on next vmsave (which would return
> > @@ -149,25 +173,80 @@
> > {
> > KVMClockState *s = KVM_CLOCK(dev);
> >
> > + if (kvm_has_adjust_clock_stable()) {
> > + s->clock_is_reliable = true;
> > + }
> > +
> > qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> > }
> >
> > +static bool kvmclock_clock_is_reliable_needed(void *opaque)
> > +{
> > + KVMClockState *s = opaque;
> > +
> > + return s->mach_use_reliable_get_clock;
> > +}
> > +
> > +static const VMStateDescription kvmclock_reliable_get_clock = {
> > + .name = "kvmclock/clock_is_reliable",
> > + .version_id = 1,
> > + .minimum_version_id = 1,
> > + .needed = kvmclock_clock_is_reliable_needed,
> > + .fields = (VMStateField[]) {
> > + VMSTATE_BOOL(clock_is_reliable, KVMClockState),
> > + VMSTATE_END_OF_LIST()
> > + }
> > +};
> > +
> > +/*
> > + * When migrating, read the clock just before migration,
> > + * so that the guest clock counts during the events
> > + * between:
> > + *
> > + * * vm_stop()
> > + * *
> > + * * pre_save()
> > + *
> > + * This reduces kvmclock difference on migration from 5s
> > + * to 0.1s (when max_downtime == 5s), because sending the
> > + * final pages of memory (which happens between vm_stop()
> > + * and pre_save()) takes max_downtime.
> > + */
> > +static void kvmclock_pre_save(void *opaque)
> > +{
> > + KVMClockState *s = opaque;
> > +
> > + s->clock = kvm_get_clock();
> > +}
> > +
> > static const VMStateDescription kvmclock_vmsd = {
> > .name = "kvmclock",
> > .version_id = 1,
> > .minimum_version_id = 1,
> > + .pre_save = kvmclock_pre_save,
> > .fields = (VMStateField[]) {
> > VMSTATE_UINT64(clock, KVMClockState),
> > VMSTATE_END_OF_LIST()
> > + },
> > + .subsections = (const VMStateDescription * []) {
> > + &kvmclock_reliable_get_clock,
> > + NULL
> > }
> > };
> >
> > +static Property kvmclock_properties[] = {
> > + DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
> > + mach_use_reliable_get_clock, true),
> > + DEFINE_PROP_END_OF_LIST(),
> > +};
> > +
> > static void kvmclock_class_init(ObjectClass *klass, void *data)
> > {
> > DeviceClass *dc = DEVICE_CLASS(klass);
> >
> > dc->realize = kvmclock_realize;
> > dc->vmsd = &kvmclock_vmsd;
> > + dc->props = kvmclock_properties;
> > }
> >
> > static const TypeInfo kvmclock_info = {
> > Index: qemu-mig-advance-clock/include/hw/i386/pc.h
> > ===================================================================
> > --- qemu-mig-advance-clock.orig/include/hw/i386/pc.h 2016-11-17
> > 15:07:11.220632761 -0200
> > +++ qemu-mig-advance-clock/include/hw/i386/pc.h 2016-11-17 15:08:58.096791416
> > -0200
> > @@ -370,6 +370,11 @@
> > #define PC_COMPAT_2_7 \
> > HW_COMPAT_2_7 \
> > {\
> > + .driver = "kvmclock",\
> > + .property = "x-mach-use-reliable-get-clock",\
> > + .value = "off",\
> > + },\
> > + {\
> > .driver = TYPE_X86_CPU,\
> > .property = "l3-cache",\
> > .value = "off",\
> > Index: qemu-mig-advance-clock/target-i386/kvm.c
> > ===================================================================
> > --- qemu-mig-advance-clock.orig/target-i386/kvm.c 2016-11-17
> > 15:07:11.221632762 -0200
> > +++ qemu-mig-advance-clock/target-i386/kvm.c 2016-11-17 15:07:15.867639659
> > -0200
> > @@ -117,6 +117,13 @@
> > return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
> > }
> >
> > +bool kvm_has_adjust_clock_stable(void)
> > +{
> > + int ret = kvm_check_extension(kvm_state, KVM_CAP_ADJUST_CLOCK);
> > +
> > + return (ret == KVM_CLOCK_TSC_STABLE);
> > +}
> > +
> > bool kvm_allows_irq0_override(void)
> > {
> > return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
> > Index: qemu-mig-advance-clock/target-i386/kvm_i386.h
> > ===================================================================
> > --- qemu-mig-advance-clock.orig/target-i386/kvm_i386.h 2016-11-17
> > 15:07:11.222632764 -0200
> > +++ qemu-mig-advance-clock/target-i386/kvm_i386.h 2016-11-17
> > 15:07:15.867639659 -0200
> > @@ -17,6 +17,7 @@
> >
> > bool kvm_allows_irq0_override(void);
> > bool kvm_has_smm(void);
> > +bool kvm_has_adjust_clock_stable(void);
> > void kvm_synchronize_all_tsc(void);
> > void kvm_arch_reset_vcpu(X86CPU *cs);
> > void kvm_arch_do_init_vcpu(X86CPU *cs);
> >
> >
> >
> >
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-12 11:22 ` Marcelo Tosatti
@ 2016-12-12 14:24 ` Pankaj Gupta
2016-12-13 1:32 ` Pankaj Gupta
0 siblings, 1 reply; 22+ messages in thread
From: Pankaj Gupta @ 2016-12-12 14:24 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: Eduardo Habkost, kvm, Radim Krcmar, Juan Quintela, qemu-devel,
Dr David Alan Gilbert, Paolo Bonzini
>
> On Mon, Dec 12, 2016 at 02:36:55AM -0500, Pankaj Gupta wrote:
> >
> > Hello Marcelo,
>
> Hi Pankaj,
>
> > > Check for KVM_CAP_ADJUST_CLOCK capability KVM_CLOCK_TSC_STABLE, which
> > > indicates that KVM_GET_CLOCK returns a value as seen by the guest at
> > > that moment.
> > >
> > > For new machine types, use this value rather than reading
> > > from guest memory.
> > >
> > > This reduces kvmclock difference on migration from 5s to 0.1s
> > > (when max_downtime == 5s).
> > >
> > > Note: pre_save contradicts the following comment
> > > /*
> > > * If the VM is stopped, declare the clock state valid to
> > > * avoid re-reading it on next vmsave (which would return
> > > * a different value). Will be reset when the VM is continued.
> > > */
> > > But the comment is bogus: vm_state_change is never called twice in a row
> > > with running=0 or running=1.
> > >
> > > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> > >
> > > ---
> > > hw/i386/kvm/clock.c | 107
> > > ++++++++++++++++++++++++++++++++++++++++++-------
> > > include/hw/i386/pc.h | 5 ++
> > > target-i386/kvm.c | 7 +++
> > > target-i386/kvm_i386.h | 1
> > > 4 files changed, 106 insertions(+), 14 deletions(-)
> > >
> > > v2:
> > > - improve variable names (Juan)
> > > - consolidate code on kvm_get_clock function (Paolo)
> > > - return mach_use_reliable_get_clock from needed function (Paolo)
> > > v3:
> > > - simplify check for src_use_reliable_get_clock (Eduardo)
> > > - move src_use_reliable_get_clock initialization to realize (Eduardo)
> > >
> > > v4:
> > > - have kvm_get_clock and clock_is_reliable assignments synchronized
> > > (Eduardo)
> > > - add comment to the reasoning of kvmclock_pre_save (Eduardo)
> > >
> > >
> > > Index: qemu-mig-advance-clock/hw/i386/kvm/clock.c
> > > ===================================================================
> > > --- qemu-mig-advance-clock.orig/hw/i386/kvm/clock.c 2016-11-17
> > > 15:07:11.220632761 -0200
> > > +++ qemu-mig-advance-clock/hw/i386/kvm/clock.c 2016-12-10
> > > 13:57:58.115983966
> > > -0200
> > > @@ -36,6 +36,13 @@
> > >
> > > uint64_t clock;
> > > bool clock_valid;
> > > +
> > > + /* whether machine type supports reliable get clock */
> > > + bool mach_use_reliable_get_clock;
> > > +
> > > + /* whether the 'clock' value was obtained in a host with
> > > + * reliable KVM_GET_CLOCK */
> > > + bool clock_is_reliable;
> > > } KVMClockState;
> > >
> > > struct pvclock_vcpu_time_info {
> > > @@ -81,6 +88,19 @@
> > > return nsec + time.system_time;
> > > }
> > >
> > > +static uint64_t kvm_get_clock(void)
> > > +{
> > > + struct kvm_clock_data data;
> > > + int ret;
> > > +
> > > + ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> > > + if (ret < 0) {
> > > + fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> > > + abort();
> > > + }
> > > + return data.clock;
> > > +}
> > > +
> > > static void kvmclock_vm_state_change(void *opaque, int running,
> > > RunState state)
> > > {
> > > @@ -91,15 +111,23 @@
> > >
> > > if (running) {
> > > struct kvm_clock_data data = {};
> > > - uint64_t time_at_migration = kvmclock_current_nsec(s);
> > > + uint64_t pvclock_via_mem = 0;
> > >
> > > - s->clock_valid = false;
> > > + /*
> > > + * If the host where s->clock was read did not support reliable
> > > + * KVM_GET_CLOCK, read kvmclock value from memory.
> > > + */
> > > + if (!s->clock_is_reliable) {
> > > + pvclock_via_mem = kvmclock_current_nsec(s);
> > > + }
> > >
> > > - /* We can't rely on the migrated clock value, just discard it */
> > > - if (time_at_migration) {
> > > - s->clock = time_at_migration;
> > > + /* We can't rely on the saved clock value, just discard it */
> > > + if (pvclock_via_mem) {
> > > + s->clock = pvclock_via_mem;
> > > }
> > >
> > > + s->clock_valid = false;
> > > +
> > > data.clock = s->clock;
> > > ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
> > > if (ret < 0) {
> > > @@ -120,8 +148,6 @@
> > > }
> > > }
> > > } else {
> > > - struct kvm_clock_data data;
> > > - int ret;
> > >
> > > if (s->clock_valid) {
> > > return;
> > > @@ -129,13 +155,11 @@
> > >
> > > kvm_synchronize_all_tsc();
> > >
> > > - ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> > > - if (ret < 0) {
> > > - fprintf(stderr, "KVM_GET_CLOCK failed: %s\n",
> > > strerror(ret));
> > > - abort();
> > > - }
> > > - s->clock = data.clock;
> > > -
> > > + s->clock = kvm_get_clock();
> > > + /* any code that sets s->clock needs to ensure clock_is_reliable
> > > + * is correctly set.
> > > + */
> > > + s->clock_is_reliable = kvm_has_adjust_clock_stable();
> >
> > Is it required to call 'kvm_has_adjust_clock_stable' here. Is there any
> > chance
> > of getting different value then in 'realize'?
>
> Yes it can happen: migration overwrites s->clock_is_reliable value (but
> you can't see that from grepping).
o.k.
>
> > > * If the VM is stopped, declare the clock state valid to
> > > * avoid re-reading it on next vmsave (which would return
> > > @@ -149,25 +173,80 @@
> > > {
> > > KVMClockState *s = KVM_CLOCK(dev);
> > >
> > > + if (kvm_has_adjust_clock_stable()) {
> > > + s->clock_is_reliable = true;
> > > + }
> > > +
> > > qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> > > }
> > >
> > > +static bool kvmclock_clock_is_reliable_needed(void *opaque)
> > > +{
> > > + KVMClockState *s = opaque;
> > > +
> > > + return s->mach_use_reliable_get_clock;
> > > +}
> > > +
> > > +static const VMStateDescription kvmclock_reliable_get_clock = {
> > > + .name = "kvmclock/clock_is_reliable",
> > > + .version_id = 1,
> > > + .minimum_version_id = 1,
> > > + .needed = kvmclock_clock_is_reliable_needed,
> > > + .fields = (VMStateField[]) {
> > > + VMSTATE_BOOL(clock_is_reliable, KVMClockState),
> > > + VMSTATE_END_OF_LIST()
> > > + }
> > > +};
> > > +
> > > +/*
> > > + * When migrating, read the clock just before migration,
> > > + * so that the guest clock counts during the events
> > > + * between:
> > > + *
> > > + * * vm_stop()
> > > + * *
> > > + * * pre_save()
> > > + *
> > > + * This reduces kvmclock difference on migration from 5s
> > > + * to 0.1s (when max_downtime == 5s), because sending the
> > > + * final pages of memory (which happens between vm_stop()
> > > + * and pre_save()) takes max_downtime.
> > > + */
> > > +static void kvmclock_pre_save(void *opaque)
> > > +{
> > > + KVMClockState *s = opaque;
> > > +
> > > + s->clock = kvm_get_clock();
> > > +}
> > > +
> > > static const VMStateDescription kvmclock_vmsd = {
> > > .name = "kvmclock",
> > > .version_id = 1,
> > > .minimum_version_id = 1,
> > > + .pre_save = kvmclock_pre_save,
> > > .fields = (VMStateField[]) {
> > > VMSTATE_UINT64(clock, KVMClockState),
> > > VMSTATE_END_OF_LIST()
> > > + },
> > > + .subsections = (const VMStateDescription * []) {
> > > + &kvmclock_reliable_get_clock,
> > > + NULL
> > > }
> > > };
> > >
> > > +static Property kvmclock_properties[] = {
> > > + DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
> > > + mach_use_reliable_get_clock, true),
> > > + DEFINE_PROP_END_OF_LIST(),
> > > +};
> > > +
> > > static void kvmclock_class_init(ObjectClass *klass, void *data)
> > > {
> > > DeviceClass *dc = DEVICE_CLASS(klass);
> > >
> > > dc->realize = kvmclock_realize;
> > > dc->vmsd = &kvmclock_vmsd;
> > > + dc->props = kvmclock_properties;
> > > }
> > >
> > > static const TypeInfo kvmclock_info = {
> > > Index: qemu-mig-advance-clock/include/hw/i386/pc.h
> > > ===================================================================
> > > --- qemu-mig-advance-clock.orig/include/hw/i386/pc.h 2016-11-17
> > > 15:07:11.220632761 -0200
> > > +++ qemu-mig-advance-clock/include/hw/i386/pc.h 2016-11-17
> > > 15:08:58.096791416
> > > -0200
> > > @@ -370,6 +370,11 @@
> > > #define PC_COMPAT_2_7 \
> > > HW_COMPAT_2_7 \
> > > {\
> > > + .driver = "kvmclock",\
> > > + .property = "x-mach-use-reliable-get-clock",\
> > > + .value = "off",\
> > > + },\
> > > + {\
> > > .driver = TYPE_X86_CPU,\
> > > .property = "l3-cache",\
> > > .value = "off",\
> > > Index: qemu-mig-advance-clock/target-i386/kvm.c
> > > ===================================================================
> > > --- qemu-mig-advance-clock.orig/target-i386/kvm.c 2016-11-17
> > > 15:07:11.221632762 -0200
> > > +++ qemu-mig-advance-clock/target-i386/kvm.c 2016-11-17
> > > 15:07:15.867639659
> > > -0200
> > > @@ -117,6 +117,13 @@
> > > return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
> > > }
> > >
> > > +bool kvm_has_adjust_clock_stable(void)
> > > +{
> > > + int ret = kvm_check_extension(kvm_state, KVM_CAP_ADJUST_CLOCK);
> > > +
> > > + return (ret == KVM_CLOCK_TSC_STABLE);
> > > +}
> > > +
> > > bool kvm_allows_irq0_override(void)
> > > {
> > > return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
> > > Index: qemu-mig-advance-clock/target-i386/kvm_i386.h
> > > ===================================================================
> > > --- qemu-mig-advance-clock.orig/target-i386/kvm_i386.h 2016-11-17
> > > 15:07:11.222632764 -0200
> > > +++ qemu-mig-advance-clock/target-i386/kvm_i386.h 2016-11-17
> > > 15:07:15.867639659 -0200
> > > @@ -17,6 +17,7 @@
> > >
> > > bool kvm_allows_irq0_override(void);
> > > bool kvm_has_smm(void);
> > > +bool kvm_has_adjust_clock_stable(void);
> > > void kvm_synchronize_all_tsc(void);
> > > void kvm_arch_reset_vcpu(X86CPU *cs);
> > > void kvm_arch_do_init_vcpu(X86CPU *cs);
> > >
> > >
> > >
> > >
>
>
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-12 14:24 ` Pankaj Gupta
@ 2016-12-13 1:32 ` Pankaj Gupta
0 siblings, 0 replies; 22+ messages in thread
From: Pankaj Gupta @ 2016-12-13 1:32 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: Eduardo Habkost, kvm, Radim Krcmar, Juan Quintela, qemu-devel,
Dr David Alan Gilbert, Paolo Bonzini
>
> >
> > On Mon, Dec 12, 2016 at 02:36:55AM -0500, Pankaj Gupta wrote:
> > >
> > > Hello Marcelo,
> >
> > Hi Pankaj,
> >
> > > > Check for KVM_CAP_ADJUST_CLOCK capability KVM_CLOCK_TSC_STABLE, which
> > > > indicates that KVM_GET_CLOCK returns a value as seen by the guest at
> > > > that moment.
> > > >
> > > > For new machine types, use this value rather than reading
> > > > from guest memory.
> > > >
> > > > This reduces kvmclock difference on migration from 5s to 0.1s
> > > > (when max_downtime == 5s).
> > > >
> > > > Note: pre_save contradicts the following comment
> > > > /*
> > > > * If the VM is stopped, declare the clock state valid to
> > > > * avoid re-reading it on next vmsave (which would return
> > > > * a different value). Will be reset when the VM is continued.
> > > > */
> > > > But the comment is bogus: vm_state_change is never called twice in a
> > > > row
> > > > with running=0 or running=1.
> > > >
> > > > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> > > >
> > > > ---
> > > > hw/i386/kvm/clock.c | 107
> > > > ++++++++++++++++++++++++++++++++++++++++++-------
> > > > include/hw/i386/pc.h | 5 ++
> > > > target-i386/kvm.c | 7 +++
> > > > target-i386/kvm_i386.h | 1
> > > > 4 files changed, 106 insertions(+), 14 deletions(-)
> > > >
> > > > v2:
> > > > - improve variable names (Juan)
> > > > - consolidate code on kvm_get_clock function (Paolo)
> > > > - return mach_use_reliable_get_clock from needed function (Paolo)
> > > > v3:
> > > > - simplify check for src_use_reliable_get_clock (Eduardo)
> > > > - move src_use_reliable_get_clock initialization to realize (Eduardo)
> > > >
> > > > v4:
> > > > - have kvm_get_clock and clock_is_reliable assignments synchronized
> > > > (Eduardo)
> > > > - add comment to the reasoning of kvmclock_pre_save (Eduardo)
> > > >
> > > >
> > > > Index: qemu-mig-advance-clock/hw/i386/kvm/clock.c
> > > > ===================================================================
> > > > --- qemu-mig-advance-clock.orig/hw/i386/kvm/clock.c 2016-11-17
> > > > 15:07:11.220632761 -0200
> > > > +++ qemu-mig-advance-clock/hw/i386/kvm/clock.c 2016-12-10
> > > > 13:57:58.115983966
> > > > -0200
> > > > @@ -36,6 +36,13 @@
> > > >
> > > > uint64_t clock;
> > > > bool clock_valid;
> > > > +
> > > > + /* whether machine type supports reliable get clock */
> > > > + bool mach_use_reliable_get_clock;
> > > > +
> > > > + /* whether the 'clock' value was obtained in a host with
> > > > + * reliable KVM_GET_CLOCK */
> > > > + bool clock_is_reliable;
> > > > } KVMClockState;
> > > >
> > > > struct pvclock_vcpu_time_info {
> > > > @@ -81,6 +88,19 @@
> > > > return nsec + time.system_time;
> > > > }
> > > >
> > > > +static uint64_t kvm_get_clock(void)
> > > > +{
> > > > + struct kvm_clock_data data;
> > > > + int ret;
> > > > +
> > > > + ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> > > > + if (ret < 0) {
> > > > + fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> > > > + abort();
> > > > + }
> > > > + return data.clock;
> > > > +}
> > > > +
> > > > static void kvmclock_vm_state_change(void *opaque, int running,
> > > > RunState state)
> > > > {
> > > > @@ -91,15 +111,23 @@
> > > >
> > > > if (running) {
> > > > struct kvm_clock_data data = {};
> > > > - uint64_t time_at_migration = kvmclock_current_nsec(s);
> > > > + uint64_t pvclock_via_mem = 0;
> > > >
> > > > - s->clock_valid = false;
> > > > + /*
> > > > + * If the host where s->clock was read did not support
> > > > reliable
> > > > + * KVM_GET_CLOCK, read kvmclock value from memory.
> > > > + */
> > > > + if (!s->clock_is_reliable) {
> > > > + pvclock_via_mem = kvmclock_current_nsec(s);
> > > > + }
> > > >
> > > > - /* We can't rely on the migrated clock value, just discard it
> > > > */
> > > > - if (time_at_migration) {
> > > > - s->clock = time_at_migration;
> > > > + /* We can't rely on the saved clock value, just discard it */
> > > > + if (pvclock_via_mem) {
> > > > + s->clock = pvclock_via_mem;
> > > > }
> > > >
> > > > + s->clock_valid = false;
> > > > +
> > > > data.clock = s->clock;
> > > > ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
> > > > if (ret < 0) {
> > > > @@ -120,8 +148,6 @@
> > > > }
> > > > }
> > > > } else {
> > > > - struct kvm_clock_data data;
> > > > - int ret;
> > > >
> > > > if (s->clock_valid) {
> > > > return;
> > > > @@ -129,13 +155,11 @@
> > > >
> > > > kvm_synchronize_all_tsc();
> > > >
> > > > - ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> > > > - if (ret < 0) {
> > > > - fprintf(stderr, "KVM_GET_CLOCK failed: %s\n",
> > > > strerror(ret));
> > > > - abort();
> > > > - }
> > > > - s->clock = data.clock;
> > > > -
> > > > + s->clock = kvm_get_clock();
> > > > + /* any code that sets s->clock needs to ensure
> > > > clock_is_reliable
> > > > + * is correctly set.
> > > > + */
> > > > + s->clock_is_reliable = kvm_has_adjust_clock_stable();
> > >
> > > Is it required to call 'kvm_has_adjust_clock_stable' here. Is there any
> > > chance
> > > of getting different value then in 'realize'?
> >
> > Yes it can happen: migration overwrites s->clock_is_reliable value (but
> > you can't see that from grepping).
>
> o.k.
Reviewed-by: Pankaj Gupta <pagupta@redhat.com>
> >
> > > > * If the VM is stopped, declare the clock state valid to
> > > > * avoid re-reading it on next vmsave (which would return
> > > > @@ -149,25 +173,80 @@
> > > > {
> > > > KVMClockState *s = KVM_CLOCK(dev);
> > > >
> > > > + if (kvm_has_adjust_clock_stable()) {
> > > > + s->clock_is_reliable = true;
> > > > + }
> > > > +
> > > > qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> > > > }
> > > >
> > > > +static bool kvmclock_clock_is_reliable_needed(void *opaque)
> > > > +{
> > > > + KVMClockState *s = opaque;
> > > > +
> > > > + return s->mach_use_reliable_get_clock;
> > > > +}
> > > > +
> > > > +static const VMStateDescription kvmclock_reliable_get_clock = {
> > > > + .name = "kvmclock/clock_is_reliable",
> > > > + .version_id = 1,
> > > > + .minimum_version_id = 1,
> > > > + .needed = kvmclock_clock_is_reliable_needed,
> > > > + .fields = (VMStateField[]) {
> > > > + VMSTATE_BOOL(clock_is_reliable, KVMClockState),
> > > > + VMSTATE_END_OF_LIST()
> > > > + }
> > > > +};
> > > > +
> > > > +/*
> > > > + * When migrating, read the clock just before migration,
> > > > + * so that the guest clock counts during the events
> > > > + * between:
> > > > + *
> > > > + * * vm_stop()
> > > > + * *
> > > > + * * pre_save()
> > > > + *
> > > > + * This reduces kvmclock difference on migration from 5s
> > > > + * to 0.1s (when max_downtime == 5s), because sending the
> > > > + * final pages of memory (which happens between vm_stop()
> > > > + * and pre_save()) takes max_downtime.
> > > > + */
> > > > +static void kvmclock_pre_save(void *opaque)
> > > > +{
> > > > + KVMClockState *s = opaque;
> > > > +
> > > > + s->clock = kvm_get_clock();
> > > > +}
> > > > +
> > > > static const VMStateDescription kvmclock_vmsd = {
> > > > .name = "kvmclock",
> > > > .version_id = 1,
> > > > .minimum_version_id = 1,
> > > > + .pre_save = kvmclock_pre_save,
> > > > .fields = (VMStateField[]) {
> > > > VMSTATE_UINT64(clock, KVMClockState),
> > > > VMSTATE_END_OF_LIST()
> > > > + },
> > > > + .subsections = (const VMStateDescription * []) {
> > > > + &kvmclock_reliable_get_clock,
> > > > + NULL
> > > > }
> > > > };
> > > >
> > > > +static Property kvmclock_properties[] = {
> > > > + DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
> > > > + mach_use_reliable_get_clock, true),
> > > > + DEFINE_PROP_END_OF_LIST(),
> > > > +};
> > > > +
> > > > static void kvmclock_class_init(ObjectClass *klass, void *data)
> > > > {
> > > > DeviceClass *dc = DEVICE_CLASS(klass);
> > > >
> > > > dc->realize = kvmclock_realize;
> > > > dc->vmsd = &kvmclock_vmsd;
> > > > + dc->props = kvmclock_properties;
> > > > }
> > > >
> > > > static const TypeInfo kvmclock_info = {
> > > > Index: qemu-mig-advance-clock/include/hw/i386/pc.h
> > > > ===================================================================
> > > > --- qemu-mig-advance-clock.orig/include/hw/i386/pc.h 2016-11-17
> > > > 15:07:11.220632761 -0200
> > > > +++ qemu-mig-advance-clock/include/hw/i386/pc.h 2016-11-17
> > > > 15:08:58.096791416
> > > > -0200
> > > > @@ -370,6 +370,11 @@
> > > > #define PC_COMPAT_2_7 \
> > > > HW_COMPAT_2_7 \
> > > > {\
> > > > + .driver = "kvmclock",\
> > > > + .property = "x-mach-use-reliable-get-clock",\
> > > > + .value = "off",\
> > > > + },\
> > > > + {\
> > > > .driver = TYPE_X86_CPU,\
> > > > .property = "l3-cache",\
> > > > .value = "off",\
> > > > Index: qemu-mig-advance-clock/target-i386/kvm.c
> > > > ===================================================================
> > > > --- qemu-mig-advance-clock.orig/target-i386/kvm.c 2016-11-17
> > > > 15:07:11.221632762 -0200
> > > > +++ qemu-mig-advance-clock/target-i386/kvm.c 2016-11-17
> > > > 15:07:15.867639659
> > > > -0200
> > > > @@ -117,6 +117,13 @@
> > > > return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
> > > > }
> > > >
> > > > +bool kvm_has_adjust_clock_stable(void)
> > > > +{
> > > > + int ret = kvm_check_extension(kvm_state, KVM_CAP_ADJUST_CLOCK);
> > > > +
> > > > + return (ret == KVM_CLOCK_TSC_STABLE);
> > > > +}
> > > > +
> > > > bool kvm_allows_irq0_override(void)
> > > > {
> > > > return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
> > > > Index: qemu-mig-advance-clock/target-i386/kvm_i386.h
> > > > ===================================================================
> > > > --- qemu-mig-advance-clock.orig/target-i386/kvm_i386.h 2016-11-17
> > > > 15:07:11.222632764 -0200
> > > > +++ qemu-mig-advance-clock/target-i386/kvm_i386.h 2016-11-17
> > > > 15:07:15.867639659 -0200
> > > > @@ -17,6 +17,7 @@
> > > >
> > > > bool kvm_allows_irq0_override(void);
> > > > bool kvm_has_smm(void);
> > > > +bool kvm_has_adjust_clock_stable(void);
> > > > void kvm_synchronize_all_tsc(void);
> > > > void kvm_arch_reset_vcpu(X86CPU *cs);
> > > > void kvm_arch_do_init_vcpu(X86CPU *cs);
> > > >
> > > >
> > > >
> > > >
> >
> >
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-10 17:21 ` [Qemu-devel] " Marcelo Tosatti
@ 2016-12-12 18:01 ` Eduardo Habkost
-1 siblings, 0 replies; 22+ messages in thread
From: Eduardo Habkost @ 2016-12-12 18:01 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: kvm, qemu-devel, Dr David Alan Gilbert, Paolo Bonzini,
Juan Quintela, Radim Krcmar
On Sat, Dec 10, 2016 at 03:21:50PM -0200, Marcelo Tosatti wrote:
[...]
> static void kvmclock_realize(DeviceState *dev, Error **errp)
> {
> KVMClockState *s = KVM_CLOCK(dev);
>
> + if (kvm_has_adjust_clock_stable()) {
> + s->clock_is_reliable = true;
> + }
> +
This seems unnecessary, as kvmclock_vm_state_change() makes sure
it is set at the same time as s->clock. Should we just remove it?
> qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> }
>
[...]
--
Eduardo
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-12 18:01 ` Eduardo Habkost
0 siblings, 0 replies; 22+ messages in thread
From: Eduardo Habkost @ 2016-12-12 18:01 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: kvm, qemu-devel, Dr David Alan Gilbert, Paolo Bonzini,
Juan Quintela, Radim Krcmar
On Sat, Dec 10, 2016 at 03:21:50PM -0200, Marcelo Tosatti wrote:
[...]
> static void kvmclock_realize(DeviceState *dev, Error **errp)
> {
> KVMClockState *s = KVM_CLOCK(dev);
>
> + if (kvm_has_adjust_clock_stable()) {
> + s->clock_is_reliable = true;
> + }
> +
This seems unnecessary, as kvmclock_vm_state_change() makes sure
it is set at the same time as s->clock. Should we just remove it?
> qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> }
>
[...]
--
Eduardo
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-12 18:01 ` [Qemu-devel] " Eduardo Habkost
@ 2016-12-12 19:44 ` Marcelo Tosatti
-1 siblings, 0 replies; 22+ messages in thread
From: Marcelo Tosatti @ 2016-12-12 19:44 UTC (permalink / raw)
To: Eduardo Habkost
Cc: kvm, qemu-devel, Dr David Alan Gilbert, Paolo Bonzini,
Juan Quintela, Radim Krcmar
On Mon, Dec 12, 2016 at 04:01:05PM -0200, Eduardo Habkost wrote:
> On Sat, Dec 10, 2016 at 03:21:50PM -0200, Marcelo Tosatti wrote:
> [...]
> > static void kvmclock_realize(DeviceState *dev, Error **errp)
> > {
> > KVMClockState *s = KVM_CLOCK(dev);
> >
> > + if (kvm_has_adjust_clock_stable()) {
> > + s->clock_is_reliable = true;
> > + }
> > +
>
> This seems unnecessary, as kvmclock_vm_state_change() makes sure
> it is set at the same time as s->clock. Should we just remove it?
There is this initialization that goes from ~running -> running
which assumes its initialized:
static void kvmclock_vm_state_change(void *opaque, int running,
RunState state)
{
KVMClockState *s = opaque;
CPUState *cpu;
int cap_clock_ctrl = kvm_check_extension(kvm_state,
KVM_CAP_KVMCLOCK_CTRL);
int ret;
if (running) {
struct kvm_clock_data data = {};
uint64_t pvclock_via_mem = 0;
/*
* If the host where s->clock was read did not support reliable
* KVM_GET_CLOCK, read kvmclock value from memory.
*/
if (!s->clock_is_reliable) {
pvclock_via_mem = kvmclock_current_nsec(s);
}
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-12 19:44 ` Marcelo Tosatti
0 siblings, 0 replies; 22+ messages in thread
From: Marcelo Tosatti @ 2016-12-12 19:44 UTC (permalink / raw)
To: Eduardo Habkost
Cc: kvm, qemu-devel, Dr David Alan Gilbert, Paolo Bonzini,
Juan Quintela, Radim Krcmar
On Mon, Dec 12, 2016 at 04:01:05PM -0200, Eduardo Habkost wrote:
> On Sat, Dec 10, 2016 at 03:21:50PM -0200, Marcelo Tosatti wrote:
> [...]
> > static void kvmclock_realize(DeviceState *dev, Error **errp)
> > {
> > KVMClockState *s = KVM_CLOCK(dev);
> >
> > + if (kvm_has_adjust_clock_stable()) {
> > + s->clock_is_reliable = true;
> > + }
> > +
>
> This seems unnecessary, as kvmclock_vm_state_change() makes sure
> it is set at the same time as s->clock. Should we just remove it?
There is this initialization that goes from ~running -> running
which assumes its initialized:
static void kvmclock_vm_state_change(void *opaque, int running,
RunState state)
{
KVMClockState *s = opaque;
CPUState *cpu;
int cap_clock_ctrl = kvm_check_extension(kvm_state,
KVM_CAP_KVMCLOCK_CTRL);
int ret;
if (running) {
struct kvm_clock_data data = {};
uint64_t pvclock_via_mem = 0;
/*
* If the host where s->clock was read did not support reliable
* KVM_GET_CLOCK, read kvmclock value from memory.
*/
if (!s->clock_is_reliable) {
pvclock_via_mem = kvmclock_current_nsec(s);
}
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-12 19:44 ` [Qemu-devel] " Marcelo Tosatti
@ 2016-12-12 19:57 ` Eduardo Habkost
-1 siblings, 0 replies; 22+ messages in thread
From: Eduardo Habkost @ 2016-12-12 19:57 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: kvm, qemu-devel, Dr David Alan Gilbert, Paolo Bonzini,
Juan Quintela, Radim Krcmar
On Mon, Dec 12, 2016 at 05:44:52PM -0200, Marcelo Tosatti wrote:
> On Mon, Dec 12, 2016 at 04:01:05PM -0200, Eduardo Habkost wrote:
> > On Sat, Dec 10, 2016 at 03:21:50PM -0200, Marcelo Tosatti wrote:
> > [...]
> > > static void kvmclock_realize(DeviceState *dev, Error **errp)
> > > {
> > > KVMClockState *s = KVM_CLOCK(dev);
> > >
> > > + if (kvm_has_adjust_clock_stable()) {
> > > + s->clock_is_reliable = true;
> > > + }
> > > +
> >
> > This seems unnecessary, as kvmclock_vm_state_change() makes sure
> > it is set at the same time as s->clock. Should we just remove it?
>
> There is this initialization that goes from ~running -> running
> which assumes its initialized:
Right: I forgot about the very first time
kvmclock_vm_state_change() is called.
It doesn't seem to make any difference (as both s->clock
kvmclock_current_nsec() will return 0 anyway), but at least it
makes clock_is_reliable consistent with its documented purpose.
I would simplify it to a single line:
s->clock_is_reliable = kvm_has_adjust_clock_stable();
But it is not a big deal, so:
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Thanks!
>
> static void kvmclock_vm_state_change(void *opaque, int running,
> RunState state)
> {
> KVMClockState *s = opaque;
> CPUState *cpu;
> int cap_clock_ctrl = kvm_check_extension(kvm_state,
> KVM_CAP_KVMCLOCK_CTRL);
> int ret;
>
> if (running) {
> struct kvm_clock_data data = {};
> uint64_t pvclock_via_mem = 0;
>
> /*
> * If the host where s->clock was read did not support reliable
> * KVM_GET_CLOCK, read kvmclock value from memory.
> */
> if (!s->clock_is_reliable) {
> pvclock_via_mem = kvmclock_current_nsec(s);
> }
>
--
Eduardo
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-12 19:57 ` Eduardo Habkost
0 siblings, 0 replies; 22+ messages in thread
From: Eduardo Habkost @ 2016-12-12 19:57 UTC (permalink / raw)
To: Marcelo Tosatti
Cc: kvm, qemu-devel, Dr David Alan Gilbert, Paolo Bonzini,
Juan Quintela, Radim Krcmar
On Mon, Dec 12, 2016 at 05:44:52PM -0200, Marcelo Tosatti wrote:
> On Mon, Dec 12, 2016 at 04:01:05PM -0200, Eduardo Habkost wrote:
> > On Sat, Dec 10, 2016 at 03:21:50PM -0200, Marcelo Tosatti wrote:
> > [...]
> > > static void kvmclock_realize(DeviceState *dev, Error **errp)
> > > {
> > > KVMClockState *s = KVM_CLOCK(dev);
> > >
> > > + if (kvm_has_adjust_clock_stable()) {
> > > + s->clock_is_reliable = true;
> > > + }
> > > +
> >
> > This seems unnecessary, as kvmclock_vm_state_change() makes sure
> > it is set at the same time as s->clock. Should we just remove it?
>
> There is this initialization that goes from ~running -> running
> which assumes its initialized:
Right: I forgot about the very first time
kvmclock_vm_state_change() is called.
It doesn't seem to make any difference (as both s->clock
kvmclock_current_nsec() will return 0 anyway), but at least it
makes clock_is_reliable consistent with its documented purpose.
I would simplify it to a single line:
s->clock_is_reliable = kvm_has_adjust_clock_stable();
But it is not a big deal, so:
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Thanks!
>
> static void kvmclock_vm_state_change(void *opaque, int running,
> RunState state)
> {
> KVMClockState *s = opaque;
> CPUState *cpu;
> int cap_clock_ctrl = kvm_check_extension(kvm_state,
> KVM_CAP_KVMCLOCK_CTRL);
> int ret;
>
> if (running) {
> struct kvm_clock_data data = {};
> uint64_t pvclock_via_mem = 0;
>
> /*
> * If the host where s->clock was read did not support reliable
> * KVM_GET_CLOCK, read kvmclock value from memory.
> */
> if (!s->clock_is_reliable) {
> pvclock_via_mem = kvmclock_current_nsec(s);
> }
>
--
Eduardo
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-10 17:21 ` [Qemu-devel] " Marcelo Tosatti
@ 2016-12-16 10:03 ` Paolo Bonzini
-1 siblings, 0 replies; 22+ messages in thread
From: Paolo Bonzini @ 2016-12-16 10:03 UTC (permalink / raw)
To: Marcelo Tosatti, kvm
Cc: Juan Quintela, Radim Krcmar, qemu-devel, Eduardo Habkost,
Dr David Alan Gilbert
I'd like to make a few cleanups and add more documentation:
diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
index eacc9dc..f767ea9 100644
--- a/hw/i386/kvm/clock.c
+++ b/hw/i386/kvm/clock.c
@@ -37,7 +37,7 @@ typedef struct KVMClockState {
uint64_t clock;
bool clock_valid;
- /* whether machine type supports reliable get clock */
+ /* whether machine type supports reliable KVM_GET_CLOCK */
bool mach_use_reliable_get_clock;
/* whether the 'clock' value was obtained in a host with
@@ -88,7 +88,7 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
return nsec + time.system_time;
}
-static uint64_t kvm_get_clock(void)
+static void kvm_update_clock(void)
{
struct kvm_clock_data data;
int ret;
@@ -98,7 +98,48 @@ static uint64_t kvm_get_clock(void)
fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
abort();
}
- return data.clock;
+ s->clock = data.clock;
+
+ /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
+ * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
+ * can drift from the TSC-based value that is computed by the guest,
+ * so we need to go through kvmclock_current_nsec(). If
+ * kvm_has_adjust_clock_stable() is true, and the flags contain
+ * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
+ * and kvmclock_current_nsec() is not necessary.
+ *
+ * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
+ *
+ * - if the host has disabled the kvmclock master clock, the guest already
+ * has protection against time going backwards. This "safety net" is only
+ * absent when kvmclock is stable;
+ *
+ * - therefore, we can replace a check like
+ *
+ * if last KVM_GET_CLOCK was not reliable
+ * read from memory
+ *
+ * with
+ *
+ * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
+ * read from memory
+ *
+ * However:
+ *
+ * - if kvm_has_adjust_clock_stable() returns false, the left side is
+ * always true (KVM_GET_CLOCK is never reliable), and the right side is
+ * unknown (because we don't have data.flags). We must assume it's true
+ * and read from memory.
+ *
+ * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
+ * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
+ *
+ * So we can just use this instead:
+ *
+ * if !kvm_has_adjust_clock_stable() then
+ * read from memory
+ */
+ s->clock_is_reliable = kvm_has_adjust_clock_stable();
}
static void kvmclock_vm_state_change(void *opaque, int running,
@@ -111,19 +153,17 @@ static void kvmclock_vm_state_change(void *opaque, int running,
if (running) {
struct kvm_clock_data data = {};
- uint64_t pvclock_via_mem = 0;
/*
* If the host where s->clock was read did not support reliable
* KVM_GET_CLOCK, read kvmclock value from memory.
*/
if (!s->clock_is_reliable) {
- pvclock_via_mem = kvmclock_current_nsec(s);
- }
-
- /* We can't rely on the saved clock value, just discard it */
- if (pvclock_via_mem) {
- s->clock = pvclock_via_mem;
+ uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
+ /* We can't rely on the saved clock value, just discard it */
+ if (pvclock_via_mem) {
+ s->clock = pvclock_via_mem;
+ }
}
s->clock_valid = false;
@@ -155,11 +195,7 @@ static void kvmclock_vm_state_change(void *opaque, int running,
kvm_synchronize_all_tsc();
- s->clock = kvm_get_clock();
- /* any code that sets s->clock needs to ensure clock_is_reliable
- * is correctly set.
- */
- s->clock_is_reliable = kvm_has_adjust_clock_stable();
+ kvm_update_clock();
/*
* If the VM is stopped, declare the clock state valid to
* avoid re-reading it on next vmsave (which would return
@@ -173,9 +209,7 @@ static void kvmclock_realize(DeviceState *dev, Error **errp)
{
KVMClockState *s = KVM_CLOCK(dev);
- if (kvm_has_adjust_clock_stable()) {
- s->clock_is_reliable = true;
- }
+ kvm_update_clock();
qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
}
@@ -216,7 +250,7 @@ static void kvmclock_pre_save(void *opaque)
{
KVMClockState *s = opaque;
- s->clock = kvm_get_clock();
+ kvm_update_clock();
}
static const VMStateDescription kvmclock_vmsd = {
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-16 10:03 ` Paolo Bonzini
0 siblings, 0 replies; 22+ messages in thread
From: Paolo Bonzini @ 2016-12-16 10:03 UTC (permalink / raw)
To: Marcelo Tosatti, kvm
Cc: qemu-devel, Dr David Alan Gilbert, Juan Quintela, Radim Krcmar,
Eduardo Habkost
I'd like to make a few cleanups and add more documentation:
diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
index eacc9dc..f767ea9 100644
--- a/hw/i386/kvm/clock.c
+++ b/hw/i386/kvm/clock.c
@@ -37,7 +37,7 @@ typedef struct KVMClockState {
uint64_t clock;
bool clock_valid;
- /* whether machine type supports reliable get clock */
+ /* whether machine type supports reliable KVM_GET_CLOCK */
bool mach_use_reliable_get_clock;
/* whether the 'clock' value was obtained in a host with
@@ -88,7 +88,7 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
return nsec + time.system_time;
}
-static uint64_t kvm_get_clock(void)
+static void kvm_update_clock(void)
{
struct kvm_clock_data data;
int ret;
@@ -98,7 +98,48 @@ static uint64_t kvm_get_clock(void)
fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
abort();
}
- return data.clock;
+ s->clock = data.clock;
+
+ /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
+ * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
+ * can drift from the TSC-based value that is computed by the guest,
+ * so we need to go through kvmclock_current_nsec(). If
+ * kvm_has_adjust_clock_stable() is true, and the flags contain
+ * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
+ * and kvmclock_current_nsec() is not necessary.
+ *
+ * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
+ *
+ * - if the host has disabled the kvmclock master clock, the guest already
+ * has protection against time going backwards. This "safety net" is only
+ * absent when kvmclock is stable;
+ *
+ * - therefore, we can replace a check like
+ *
+ * if last KVM_GET_CLOCK was not reliable
+ * read from memory
+ *
+ * with
+ *
+ * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
+ * read from memory
+ *
+ * However:
+ *
+ * - if kvm_has_adjust_clock_stable() returns false, the left side is
+ * always true (KVM_GET_CLOCK is never reliable), and the right side is
+ * unknown (because we don't have data.flags). We must assume it's true
+ * and read from memory.
+ *
+ * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
+ * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
+ *
+ * So we can just use this instead:
+ *
+ * if !kvm_has_adjust_clock_stable() then
+ * read from memory
+ */
+ s->clock_is_reliable = kvm_has_adjust_clock_stable();
}
static void kvmclock_vm_state_change(void *opaque, int running,
@@ -111,19 +153,17 @@ static void kvmclock_vm_state_change(void *opaque, int running,
if (running) {
struct kvm_clock_data data = {};
- uint64_t pvclock_via_mem = 0;
/*
* If the host where s->clock was read did not support reliable
* KVM_GET_CLOCK, read kvmclock value from memory.
*/
if (!s->clock_is_reliable) {
- pvclock_via_mem = kvmclock_current_nsec(s);
- }
-
- /* We can't rely on the saved clock value, just discard it */
- if (pvclock_via_mem) {
- s->clock = pvclock_via_mem;
+ uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
+ /* We can't rely on the saved clock value, just discard it */
+ if (pvclock_via_mem) {
+ s->clock = pvclock_via_mem;
+ }
}
s->clock_valid = false;
@@ -155,11 +195,7 @@ static void kvmclock_vm_state_change(void *opaque, int running,
kvm_synchronize_all_tsc();
- s->clock = kvm_get_clock();
- /* any code that sets s->clock needs to ensure clock_is_reliable
- * is correctly set.
- */
- s->clock_is_reliable = kvm_has_adjust_clock_stable();
+ kvm_update_clock();
/*
* If the VM is stopped, declare the clock state valid to
* avoid re-reading it on next vmsave (which would return
@@ -173,9 +209,7 @@ static void kvmclock_realize(DeviceState *dev, Error **errp)
{
KVMClockState *s = KVM_CLOCK(dev);
- if (kvm_has_adjust_clock_stable()) {
- s->clock_is_reliable = true;
- }
+ kvm_update_clock();
qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
}
@@ -216,7 +250,7 @@ static void kvmclock_pre_save(void *opaque)
{
KVMClockState *s = opaque;
- s->clock = kvm_get_clock();
+ kvm_update_clock();
}
static const VMStateDescription kvmclock_vmsd = {
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-16 10:03 ` [Qemu-devel] " Paolo Bonzini
@ 2016-12-16 13:41 ` Eduardo Habkost
-1 siblings, 0 replies; 22+ messages in thread
From: Eduardo Habkost @ 2016-12-16 13:41 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Radim Krcmar, kvm, Juan Quintela, Marcelo Tosatti, qemu-devel,
Dr David Alan Gilbert
On Fri, Dec 16, 2016 at 11:03:33AM +0100, Paolo Bonzini wrote:
> I'd like to make a few cleanups and add more documentation:
>
Looks good to me.
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
> diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
> index eacc9dc..f767ea9 100644
> --- a/hw/i386/kvm/clock.c
> +++ b/hw/i386/kvm/clock.c
> @@ -37,7 +37,7 @@ typedef struct KVMClockState {
> uint64_t clock;
> bool clock_valid;
>
> - /* whether machine type supports reliable get clock */
> + /* whether machine type supports reliable KVM_GET_CLOCK */
> bool mach_use_reliable_get_clock;
>
> /* whether the 'clock' value was obtained in a host with
> @@ -88,7 +88,7 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
> return nsec + time.system_time;
> }
>
> -static uint64_t kvm_get_clock(void)
> +static void kvm_update_clock(void)
> {
> struct kvm_clock_data data;
> int ret;
> @@ -98,7 +98,48 @@ static uint64_t kvm_get_clock(void)
> fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> abort();
> }
> - return data.clock;
> + s->clock = data.clock;
> +
> + /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
> + * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
> + * can drift from the TSC-based value that is computed by the guest,
> + * so we need to go through kvmclock_current_nsec(). If
> + * kvm_has_adjust_clock_stable() is true, and the flags contain
> + * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
> + * and kvmclock_current_nsec() is not necessary.
> + *
> + * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
> + *
> + * - if the host has disabled the kvmclock master clock, the guest already
> + * has protection against time going backwards. This "safety net" is only
> + * absent when kvmclock is stable;
> + *
> + * - therefore, we can replace a check like
> + *
> + * if last KVM_GET_CLOCK was not reliable
> + * read from memory
> + *
> + * with
> + *
> + * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
> + * read from memory
> + *
> + * However:
> + *
> + * - if kvm_has_adjust_clock_stable() returns false, the left side is
> + * always true (KVM_GET_CLOCK is never reliable), and the right side is
> + * unknown (because we don't have data.flags). We must assume it's true
> + * and read from memory.
> + *
> + * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
> + * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
> + *
> + * So we can just use this instead:
> + *
> + * if !kvm_has_adjust_clock_stable() then
> + * read from memory
> + */
> + s->clock_is_reliable = kvm_has_adjust_clock_stable();
> }
>
> static void kvmclock_vm_state_change(void *opaque, int running,
> @@ -111,19 +153,17 @@ static void kvmclock_vm_state_change(void *opaque, int running,
>
> if (running) {
> struct kvm_clock_data data = {};
> - uint64_t pvclock_via_mem = 0;
>
> /*
> * If the host where s->clock was read did not support reliable
> * KVM_GET_CLOCK, read kvmclock value from memory.
> */
> if (!s->clock_is_reliable) {
> - pvclock_via_mem = kvmclock_current_nsec(s);
> - }
> -
> - /* We can't rely on the saved clock value, just discard it */
> - if (pvclock_via_mem) {
> - s->clock = pvclock_via_mem;
> + uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
> + /* We can't rely on the saved clock value, just discard it */
> + if (pvclock_via_mem) {
> + s->clock = pvclock_via_mem;
> + }
> }
>
> s->clock_valid = false;
> @@ -155,11 +195,7 @@ static void kvmclock_vm_state_change(void *opaque, int running,
>
> kvm_synchronize_all_tsc();
>
> - s->clock = kvm_get_clock();
> - /* any code that sets s->clock needs to ensure clock_is_reliable
> - * is correctly set.
> - */
> - s->clock_is_reliable = kvm_has_adjust_clock_stable();
> + kvm_update_clock();
> /*
> * If the VM is stopped, declare the clock state valid to
> * avoid re-reading it on next vmsave (which would return
> @@ -173,9 +209,7 @@ static void kvmclock_realize(DeviceState *dev, Error **errp)
> {
> KVMClockState *s = KVM_CLOCK(dev);
>
> - if (kvm_has_adjust_clock_stable()) {
> - s->clock_is_reliable = true;
> - }
> + kvm_update_clock();
>
> qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> }
> @@ -216,7 +250,7 @@ static void kvmclock_pre_save(void *opaque)
> {
> KVMClockState *s = opaque;
>
> - s->clock = kvm_get_clock();
> + kvm_update_clock();
> }
>
> static const VMStateDescription kvmclock_vmsd = {
--
Eduardo
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-16 13:41 ` Eduardo Habkost
0 siblings, 0 replies; 22+ messages in thread
From: Eduardo Habkost @ 2016-12-16 13:41 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Marcelo Tosatti, kvm, qemu-devel, Dr David Alan Gilbert,
Juan Quintela, Radim Krcmar
On Fri, Dec 16, 2016 at 11:03:33AM +0100, Paolo Bonzini wrote:
> I'd like to make a few cleanups and add more documentation:
>
Looks good to me.
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
> diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
> index eacc9dc..f767ea9 100644
> --- a/hw/i386/kvm/clock.c
> +++ b/hw/i386/kvm/clock.c
> @@ -37,7 +37,7 @@ typedef struct KVMClockState {
> uint64_t clock;
> bool clock_valid;
>
> - /* whether machine type supports reliable get clock */
> + /* whether machine type supports reliable KVM_GET_CLOCK */
> bool mach_use_reliable_get_clock;
>
> /* whether the 'clock' value was obtained in a host with
> @@ -88,7 +88,7 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
> return nsec + time.system_time;
> }
>
> -static uint64_t kvm_get_clock(void)
> +static void kvm_update_clock(void)
> {
> struct kvm_clock_data data;
> int ret;
> @@ -98,7 +98,48 @@ static uint64_t kvm_get_clock(void)
> fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> abort();
> }
> - return data.clock;
> + s->clock = data.clock;
> +
> + /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
> + * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
> + * can drift from the TSC-based value that is computed by the guest,
> + * so we need to go through kvmclock_current_nsec(). If
> + * kvm_has_adjust_clock_stable() is true, and the flags contain
> + * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
> + * and kvmclock_current_nsec() is not necessary.
> + *
> + * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
> + *
> + * - if the host has disabled the kvmclock master clock, the guest already
> + * has protection against time going backwards. This "safety net" is only
> + * absent when kvmclock is stable;
> + *
> + * - therefore, we can replace a check like
> + *
> + * if last KVM_GET_CLOCK was not reliable
> + * read from memory
> + *
> + * with
> + *
> + * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
> + * read from memory
> + *
> + * However:
> + *
> + * - if kvm_has_adjust_clock_stable() returns false, the left side is
> + * always true (KVM_GET_CLOCK is never reliable), and the right side is
> + * unknown (because we don't have data.flags). We must assume it's true
> + * and read from memory.
> + *
> + * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
> + * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
> + *
> + * So we can just use this instead:
> + *
> + * if !kvm_has_adjust_clock_stable() then
> + * read from memory
> + */
> + s->clock_is_reliable = kvm_has_adjust_clock_stable();
> }
>
> static void kvmclock_vm_state_change(void *opaque, int running,
> @@ -111,19 +153,17 @@ static void kvmclock_vm_state_change(void *opaque, int running,
>
> if (running) {
> struct kvm_clock_data data = {};
> - uint64_t pvclock_via_mem = 0;
>
> /*
> * If the host where s->clock was read did not support reliable
> * KVM_GET_CLOCK, read kvmclock value from memory.
> */
> if (!s->clock_is_reliable) {
> - pvclock_via_mem = kvmclock_current_nsec(s);
> - }
> -
> - /* We can't rely on the saved clock value, just discard it */
> - if (pvclock_via_mem) {
> - s->clock = pvclock_via_mem;
> + uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
> + /* We can't rely on the saved clock value, just discard it */
> + if (pvclock_via_mem) {
> + s->clock = pvclock_via_mem;
> + }
> }
>
> s->clock_valid = false;
> @@ -155,11 +195,7 @@ static void kvmclock_vm_state_change(void *opaque, int running,
>
> kvm_synchronize_all_tsc();
>
> - s->clock = kvm_get_clock();
> - /* any code that sets s->clock needs to ensure clock_is_reliable
> - * is correctly set.
> - */
> - s->clock_is_reliable = kvm_has_adjust_clock_stable();
> + kvm_update_clock();
> /*
> * If the VM is stopped, declare the clock state valid to
> * avoid re-reading it on next vmsave (which would return
> @@ -173,9 +209,7 @@ static void kvmclock_realize(DeviceState *dev, Error **errp)
> {
> KVMClockState *s = KVM_CLOCK(dev);
>
> - if (kvm_has_adjust_clock_stable()) {
> - s->clock_is_reliable = true;
> - }
> + kvm_update_clock();
>
> qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> }
> @@ -216,7 +250,7 @@ static void kvmclock_pre_save(void *opaque)
> {
> KVMClockState *s = opaque;
>
> - s->clock = kvm_get_clock();
> + kvm_update_clock();
> }
>
> static const VMStateDescription kvmclock_vmsd = {
--
Eduardo
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
2016-12-16 13:41 ` [Qemu-devel] " Eduardo Habkost
@ 2016-12-16 15:59 ` Marcelo Tosatti
-1 siblings, 0 replies; 22+ messages in thread
From: Marcelo Tosatti @ 2016-12-16 15:59 UTC (permalink / raw)
To: Eduardo Habkost
Cc: kvm, Juan Quintela, Radim Krcmar, qemu-devel,
Dr David Alan Gilbert, Paolo Bonzini
On Fri, Dec 16, 2016 at 11:41:36AM -0200, Eduardo Habkost wrote:
> On Fri, Dec 16, 2016 at 11:03:33AM +0100, Paolo Bonzini wrote:
> > I'd like to make a few cleanups and add more documentation:
> >
>
> Looks good to me.
>
> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
+1
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [Qemu-devel] [qemu patch V4 2/2] kvmclock: reduce kvmclock difference on migration
@ 2016-12-16 15:59 ` Marcelo Tosatti
0 siblings, 0 replies; 22+ messages in thread
From: Marcelo Tosatti @ 2016-12-16 15:59 UTC (permalink / raw)
To: Eduardo Habkost
Cc: Paolo Bonzini, kvm, qemu-devel, Dr David Alan Gilbert,
Juan Quintela, Radim Krcmar
On Fri, Dec 16, 2016 at 11:41:36AM -0200, Eduardo Habkost wrote:
> On Fri, Dec 16, 2016 at 11:03:33AM +0100, Paolo Bonzini wrote:
> > I'd like to make a few cleanups and add more documentation:
> >
>
> Looks good to me.
>
> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
+1
^ permalink raw reply [flat|nested] 22+ messages in thread