958e1b
From db50fd1dfca6131e29def9ed3cfd120b30894356 Mon Sep 17 00:00:00 2001
958e1b
From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
958e1b
Date: Wed, 22 Oct 2014 09:46:37 +0200
958e1b
Subject: [PATCH 6/6] kvmclock: Ensure time in migration never goes backward
958e1b
958e1b
Message-id: <1413971197-4624-6-git-send-email-dgilbert@redhat.com>
958e1b
Patchwork-id: 61792
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH 5/5] kvmclock: Ensure time in migration never goes backward
958e1b
Bugzilla: 1098602 1130428
958e1b
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
958e1b
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
958e1b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
958e1b
From: Alexander Graf <agraf@suse.de>
958e1b
958e1b
When we migrate we ask the kernel about its current belief on what the guest
958e1b
time would be. However, I've seen cases where the kvmclock guest structure
958e1b
indicates a time more recent than the kvm returned time.
958e1b
958e1b
To make sure we never go backwards, calculate what the guest would have seen as time at the point of migration and use that value instead of the kernel returned one when it's more recent.
958e1b
This bases the view of the kvmclock after migration on the
958e1b
same foundation in host as well as guest.
958e1b
958e1b
Signed-off-by: Alexander Graf <agraf@suse.de>
958e1b
Cc: qemu-stable@nongnu.org
958e1b
Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>
958e1b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
(cherry picked from commit 9a48bcd1b82494671c111109b0eefdb882581499)
958e1b
958e1b
dgilbert: Rework kvmclock_current_nsec code to get 'env' since the types
958e1b
      have changed
958e1b
958e1b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 hw/i386/kvm/clock.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 1 file changed, 48 insertions(+)
958e1b
958e1b
diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
958e1b
index 53056d4..4bac13e 100644
958e1b
--- a/hw/i386/kvm/clock.c
958e1b
+++ b/hw/i386/kvm/clock.c
958e1b
@@ -14,6 +14,7 @@
958e1b
  */
958e1b
 
958e1b
 #include "qemu-common.h"
958e1b
+#include "qemu/host-utils.h"
958e1b
 #include "sysemu/sysemu.h"
958e1b
 #include "sysemu/kvm.h"
958e1b
 #include "sysemu/cpus.h"
958e1b
@@ -29,6 +30,47 @@ typedef struct KVMClockState {
958e1b
     bool clock_valid;
958e1b
 } KVMClockState;
958e1b
 
958e1b
+struct pvclock_vcpu_time_info {
958e1b
+    uint32_t   version;
958e1b
+    uint32_t   pad0;
958e1b
+    uint64_t   tsc_timestamp;
958e1b
+    uint64_t   system_time;
958e1b
+    uint32_t   tsc_to_system_mul;
958e1b
+    int8_t     tsc_shift;
958e1b
+    uint8_t    flags;
958e1b
+    uint8_t    pad[2];
958e1b
+} __attribute__((__packed__)); /* 32 bytes */
958e1b
+
958e1b
+static uint64_t kvmclock_current_nsec(KVMClockState *s)
958e1b
+{
958e1b
+    CPUX86State *env = first_cpu;
958e1b
+    hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
958e1b
+    uint64_t migration_tsc = env->tsc;
958e1b
+    struct pvclock_vcpu_time_info time;
958e1b
+    uint64_t delta;
958e1b
+    uint64_t nsec_lo;
958e1b
+    uint64_t nsec_hi;
958e1b
+    uint64_t nsec;
958e1b
+
958e1b
+    if (!(env->system_time_msr & 1ULL)) {
958e1b
+        /* KVM clock not active */
958e1b
+        return 0;
958e1b
+    }
958e1b
+
958e1b
+    cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
958e1b
+
958e1b
+    assert(time.tsc_timestamp <= migration_tsc);
958e1b
+    delta = migration_tsc - time.tsc_timestamp;
958e1b
+    if (time.tsc_shift < 0) {
958e1b
+        delta >>= -time.tsc_shift;
958e1b
+    } else {
958e1b
+        delta <<= time.tsc_shift;
958e1b
+    }
958e1b
+
958e1b
+    mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
958e1b
+    nsec = (nsec_lo >> 32) | (nsec_hi << 32);
958e1b
+    return nsec + time.system_time;
958e1b
+}
958e1b
 
958e1b
 static void kvmclock_vm_state_change(void *opaque, int running,
958e1b
                                      RunState state)
958e1b
@@ -40,9 +82,15 @@ static void kvmclock_vm_state_change(void *opaque, int running,
958e1b
 
958e1b
     if (running) {
958e1b
         struct kvm_clock_data data;
958e1b
+        uint64_t time_at_migration = kvmclock_current_nsec(s);
958e1b
 
958e1b
         s->clock_valid = false;
958e1b
 
958e1b
+        /* We can't rely on the migrated clock value, just discard it */
958e1b
+        if (time_at_migration) {
958e1b
+            s->clock = time_at_migration;
958e1b
+        }
958e1b
+
958e1b
         data.clock = s->clock;
958e1b
         data.flags = 0;
958e1b
         ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
958e1b
-- 
958e1b
1.8.3.1
958e1b