cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-kvmclock-Ensure-time-in-migration-never-goes-backwar.patch

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