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