cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
958e1b
From 4b6035c2a739bc4c086abbb36f0883a1178a8f1c Mon Sep 17 00:00:00 2001
958e1b
From: Marcelo Tosatti <mtosatti@redhat.com>
958e1b
Date: Thu, 26 Jun 2014 15:06:14 +0200
958e1b
Subject: [PATCH 12/13] kvmclock: Ensure time in migration never goes backward
958e1b
958e1b
RH-Author: Marcelo Tosatti <mtosatti@redhat.com>
958e1b
Message-id: <20140626150716.560273759@amt.cnet>
958e1b
Patchwork-id: 59388
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH 1/2] kvmclock: Ensure time in migration never goes backward
958e1b
Bugzilla: 1098602
958e1b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
958e1b
RH-Acked-by: Andrew Jones <drjones@redhat.com>
958e1b
958e1b
commit a096b3a6732f846ec57dc28b47ee9435aa0609bf upstream
958e1b
Author: Alexander Graf <agraf@suse.de>
958e1b
Date:   Fri May 16 17:15:21 2014 +0200
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
958e1b
as time at the point of migration and use that value instead of the kernel
958e1b
returned one when it's more recent.  This bases the view of the kvmclock
958e1b
after migration on the 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
BZ: 1098602
958e1b
958e1b
---
958e1b
 hw/i386/kvm/clock.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 1 file changed, 49 insertions(+)
958e1b
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 hw/i386/kvm/clock.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
958e1b
 1 files changed, 49 insertions(+), 0 deletions(-)
958e1b
958e1b
diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
958e1b
index 6d6f3a7..1f2a26e 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 "hw/sysbus.h"
958e1b
@@ -28,6 +29,48 @@ 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
+    CPUArchState *acpu = first_cpu;
958e1b
+    CPUState *cpu = ENV_GET_CPU(acpu);
958e1b
+    CPUX86State *env = cpu->env_ptr;
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
+    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
@@ -39,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.7.1
958e1b