Blame SOURCES/kvm-target-arm-kvm-Implement-virtual-time-adjustment.patch

22c213
From 5388ea3fc0737d1a659256ff3663057bef484c19 Mon Sep 17 00:00:00 2001
22c213
From: Andrew Jones <drjones@redhat.com>
22c213
Date: Fri, 31 Jan 2020 14:23:13 +0000
22c213
Subject: [PATCH 11/15] target/arm/kvm: Implement virtual time adjustment
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Andrew Jones <drjones@redhat.com>
22c213
Message-id: <20200131142314.13175-5-drjones@redhat.com>
22c213
Patchwork-id: 93622
22c213
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 4/5] target/arm/kvm: Implement virtual time adjustment
22c213
Bugzilla: 1647366
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
22c213
RH-Acked-by: Gavin Shan <gshan@redhat.com>
22c213
22c213
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1647366
22c213
22c213
Author: Andrew Jones <drjones@redhat.com>
22c213
Date:   Thu, 30 Jan 2020 16:02:06 +0000
22c213
22c213
    target/arm/kvm: Implement virtual time adjustment
22c213
22c213
    When a VM is stopped (such as when it's paused) guest virtual time
22c213
    should stop counting. Otherwise, when the VM is resumed it will
22c213
    experience time jumps and its kernel may report soft lockups. Not
22c213
    counting virtual time while the VM is stopped has the side effect
22c213
    of making the guest's time appear to lag when compared with real
22c213
    time, and even with time derived from the physical counter. For
22c213
    this reason, this change, which is enabled by default, comes with
22c213
    a KVM CPU feature allowing it to be disabled, restoring legacy
22c213
    behavior.
22c213
22c213
    This patch only provides the implementation of the virtual time
22c213
    adjustment. A subsequent patch will provide the CPU property
22c213
    allowing the change to be enabled and disabled.
22c213
22c213
    Reported-by: Bijan Mottahedeh <bijan.mottahedeh@oracle.com>
22c213
    Signed-off-by: Andrew Jones <drjones@redhat.com>
22c213
    Message-id: 20200120101023.16030-6-drjones@redhat.com
22c213
    Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
22c213
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
22c213
22c213
(cherry picked from commit e5ac4200b4cddf44df9adbef677af0d1f1c579c6)
22c213
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
22c213
---
22c213
 target/arm/cpu.h     |  7 ++++
22c213
 target/arm/kvm.c     | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++
22c213
 target/arm/kvm32.c   |  3 ++
22c213
 target/arm/kvm64.c   |  3 ++
22c213
 target/arm/kvm_arm.h | 38 ++++++++++++++++++++++
22c213
 target/arm/machine.c |  7 ++++
22c213
 6 files changed, 150 insertions(+)
22c213
22c213
diff --git a/target/arm/cpu.h b/target/arm/cpu.h
22c213
index 82dd3cc..fbd8ea0 100644
22c213
--- a/target/arm/cpu.h
22c213
+++ b/target/arm/cpu.h
22c213
@@ -821,6 +821,13 @@ struct ARMCPU {
22c213
     /* KVM init features for this CPU */
22c213
     uint32_t kvm_init_features[7];
22c213
 
22c213
+    /* KVM CPU state */
22c213
+
22c213
+    /* KVM virtual time adjustment */
22c213
+    bool kvm_adjvtime;
22c213
+    bool kvm_vtime_dirty;
22c213
+    uint64_t kvm_vtime;
22c213
+
22c213
     /* Uniprocessor system with MP extensions */
22c213
     bool mp_is_up;
22c213
 
22c213
diff --git a/target/arm/kvm.c b/target/arm/kvm.c
22c213
index 5b82cef..26d7f8b 100644
22c213
--- a/target/arm/kvm.c
22c213
+++ b/target/arm/kvm.c
22c213
@@ -359,6 +359,22 @@ static int compare_u64(const void *a, const void *b)
22c213
     return 0;
22c213
 }
22c213
 
22c213
+/*
22c213
+ * cpreg_values are sorted in ascending order by KVM register ID
22c213
+ * (see kvm_arm_init_cpreg_list). This allows us to cheaply find
22c213
+ * the storage for a KVM register by ID with a binary search.
22c213
+ */
22c213
+static uint64_t *kvm_arm_get_cpreg_ptr(ARMCPU *cpu, uint64_t regidx)
22c213
+{
22c213
+    uint64_t *res;
22c213
+
22c213
+    res = bsearch(&regidx, cpu->cpreg_indexes, cpu->cpreg_array_len,
22c213
+                  sizeof(uint64_t), compare_u64);
22c213
+    assert(res);
22c213
+
22c213
+    return &cpu->cpreg_values[res - cpu->cpreg_indexes];
22c213
+}
22c213
+
22c213
 /* Initialize the ARMCPU cpreg list according to the kernel's
22c213
  * definition of what CPU registers it knows about (and throw away
22c213
  * the previous TCG-created cpreg list).
22c213
@@ -512,6 +528,23 @@ bool write_list_to_kvmstate(ARMCPU *cpu, int level)
22c213
     return ok;
22c213
 }
22c213
 
22c213
+void kvm_arm_cpu_pre_save(ARMCPU *cpu)
22c213
+{
22c213
+    /* KVM virtual time adjustment */
22c213
+    if (cpu->kvm_vtime_dirty) {
22c213
+        *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT) = cpu->kvm_vtime;
22c213
+    }
22c213
+}
22c213
+
22c213
+void kvm_arm_cpu_post_load(ARMCPU *cpu)
22c213
+{
22c213
+    /* KVM virtual time adjustment */
22c213
+    if (cpu->kvm_adjvtime) {
22c213
+        cpu->kvm_vtime = *kvm_arm_get_cpreg_ptr(cpu, KVM_REG_ARM_TIMER_CNT);
22c213
+        cpu->kvm_vtime_dirty = true;
22c213
+    }
22c213
+}
22c213
+
22c213
 void kvm_arm_reset_vcpu(ARMCPU *cpu)
22c213
 {
22c213
     int ret;
22c213
@@ -579,6 +612,50 @@ int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu)
22c213
     return 0;
22c213
 }
22c213
 
22c213
+void kvm_arm_get_virtual_time(CPUState *cs)
22c213
+{
22c213
+    ARMCPU *cpu = ARM_CPU(cs);
22c213
+    struct kvm_one_reg reg = {
22c213
+        .id = KVM_REG_ARM_TIMER_CNT,
22c213
+        .addr = (uintptr_t)&cpu->kvm_vtime,
22c213
+    };
22c213
+    int ret;
22c213
+
22c213
+    if (cpu->kvm_vtime_dirty) {
22c213
+        return;
22c213
+    }
22c213
+
22c213
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®);
22c213
+    if (ret) {
22c213
+        error_report("Failed to get KVM_REG_ARM_TIMER_CNT");
22c213
+        abort();
22c213
+    }
22c213
+
22c213
+    cpu->kvm_vtime_dirty = true;
22c213
+}
22c213
+
22c213
+void kvm_arm_put_virtual_time(CPUState *cs)
22c213
+{
22c213
+    ARMCPU *cpu = ARM_CPU(cs);
22c213
+    struct kvm_one_reg reg = {
22c213
+        .id = KVM_REG_ARM_TIMER_CNT,
22c213
+        .addr = (uintptr_t)&cpu->kvm_vtime,
22c213
+    };
22c213
+    int ret;
22c213
+
22c213
+    if (!cpu->kvm_vtime_dirty) {
22c213
+        return;
22c213
+    }
22c213
+
22c213
+    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®);
22c213
+    if (ret) {
22c213
+        error_report("Failed to set KVM_REG_ARM_TIMER_CNT");
22c213
+        abort();
22c213
+    }
22c213
+
22c213
+    cpu->kvm_vtime_dirty = false;
22c213
+}
22c213
+
22c213
 int kvm_put_vcpu_events(ARMCPU *cpu)
22c213
 {
22c213
     CPUARMState *env = &cpu->env;
22c213
@@ -690,6 +767,21 @@ MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
22c213
     return MEMTXATTRS_UNSPECIFIED;
22c213
 }
22c213
 
22c213
+void kvm_arm_vm_state_change(void *opaque, int running, RunState state)
22c213
+{
22c213
+    CPUState *cs = opaque;
22c213
+    ARMCPU *cpu = ARM_CPU(cs);
22c213
+
22c213
+    if (running) {
22c213
+        if (cpu->kvm_adjvtime) {
22c213
+            kvm_arm_put_virtual_time(cs);
22c213
+        }
22c213
+    } else {
22c213
+        if (cpu->kvm_adjvtime) {
22c213
+            kvm_arm_get_virtual_time(cs);
22c213
+        }
22c213
+    }
22c213
+}
22c213
 
22c213
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
22c213
 {
22c213
diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c
22c213
index 32bf8d6..3a8b437 100644
22c213
--- a/target/arm/kvm32.c
22c213
+++ b/target/arm/kvm32.c
22c213
@@ -16,6 +16,7 @@
22c213
 #include "qemu-common.h"
22c213
 #include "cpu.h"
22c213
 #include "qemu/timer.h"
22c213
+#include "sysemu/runstate.h"
22c213
 #include "sysemu/kvm.h"
22c213
 #include "kvm_arm.h"
22c213
 #include "internals.h"
22c213
@@ -198,6 +199,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
22c213
         return -EINVAL;
22c213
     }
22c213
 
22c213
+    qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
22c213
+
22c213
     /* Determine init features for this CPU */
22c213
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
22c213
     if (cpu->start_powered_off) {
22c213
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
22c213
index 666a81a..d368189 100644
22c213
--- a/target/arm/kvm64.c
22c213
+++ b/target/arm/kvm64.c
22c213
@@ -23,6 +23,7 @@
22c213
 #include "qemu/host-utils.h"
22c213
 #include "qemu/main-loop.h"
22c213
 #include "exec/gdbstub.h"
22c213
+#include "sysemu/runstate.h"
22c213
 #include "sysemu/kvm.h"
22c213
 #include "sysemu/kvm_int.h"
22c213
 #include "kvm_arm.h"
22c213
@@ -735,6 +736,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
22c213
         return -EINVAL;
22c213
     }
22c213
 
22c213
+    qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cs);
22c213
+
22c213
     /* Determine init features for this CPU */
22c213
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
22c213
     if (cpu->start_powered_off) {
22c213
diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h
22c213
index b48a9c9..01a9a18 100644
22c213
--- a/target/arm/kvm_arm.h
22c213
+++ b/target/arm/kvm_arm.h
22c213
@@ -128,6 +128,23 @@ bool write_list_to_kvmstate(ARMCPU *cpu, int level);
22c213
 bool write_kvmstate_to_list(ARMCPU *cpu);
22c213
 
22c213
 /**
22c213
+ * kvm_arm_cpu_pre_save:
22c213
+ * @cpu: ARMCPU
22c213
+ *
22c213
+ * Called after write_kvmstate_to_list() from cpu_pre_save() to update
22c213
+ * the cpreg list with KVM CPU state.
22c213
+ */
22c213
+void kvm_arm_cpu_pre_save(ARMCPU *cpu);
22c213
+
22c213
+/**
22c213
+ * kvm_arm_cpu_post_load:
22c213
+ * @cpu: ARMCPU
22c213
+ *
22c213
+ * Called from cpu_post_load() to update KVM CPU state from the cpreg list.
22c213
+ */
22c213
+void kvm_arm_cpu_post_load(ARMCPU *cpu);
22c213
+
22c213
+/**
22c213
  * kvm_arm_reset_vcpu:
22c213
  * @cpu: ARMCPU
22c213
  *
22c213
@@ -292,6 +309,24 @@ int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu);
22c213
  */
22c213
 int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu);
22c213
 
22c213
+/**
22c213
+ * kvm_arm_get_virtual_time:
22c213
+ * @cs: CPUState
22c213
+ *
22c213
+ * Gets the VCPU's virtual counter and stores it in the KVM CPU state.
22c213
+ */
22c213
+void kvm_arm_get_virtual_time(CPUState *cs);
22c213
+
22c213
+/**
22c213
+ * kvm_arm_put_virtual_time:
22c213
+ * @cs: CPUState
22c213
+ *
22c213
+ * Sets the VCPU's virtual counter to the value stored in the KVM CPU state.
22c213
+ */
22c213
+void kvm_arm_put_virtual_time(CPUState *cs);
22c213
+
22c213
+void kvm_arm_vm_state_change(void *opaque, int running, RunState state);
22c213
+
22c213
 int kvm_arm_vgic_probe(void);
22c213
 
22c213
 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
22c213
@@ -339,6 +374,9 @@ static inline void kvm_arm_pmu_set_irq(CPUState *cs, int irq) {}
22c213
 static inline void kvm_arm_pmu_init(CPUState *cs) {}
22c213
 
22c213
 static inline void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map) {}
22c213
+
22c213
+static inline void kvm_arm_get_virtual_time(CPUState *cs) {}
22c213
+static inline void kvm_arm_put_virtual_time(CPUState *cs) {}
22c213
 #endif
22c213
 
22c213
 static inline const char *gic_class_name(void)
22c213
diff --git a/target/arm/machine.c b/target/arm/machine.c
22c213
index eb28b23..241890a 100644
22c213
--- a/target/arm/machine.c
22c213
+++ b/target/arm/machine.c
22c213
@@ -642,6 +642,12 @@ static int cpu_pre_save(void *opaque)
22c213
             /* This should never fail */
22c213
             abort();
22c213
         }
22c213
+
22c213
+        /*
22c213
+         * kvm_arm_cpu_pre_save() must be called after
22c213
+         * write_kvmstate_to_list()
22c213
+         */
22c213
+        kvm_arm_cpu_pre_save(cpu);
22c213
     } else {
22c213
         if (!write_cpustate_to_list(cpu, false)) {
22c213
             /* This should never fail. */
22c213
@@ -744,6 +750,7 @@ static int cpu_post_load(void *opaque, int version_id)
22c213
          * we're using it.
22c213
          */
22c213
         write_list_to_cpustate(cpu);
22c213
+        kvm_arm_cpu_post_load(cpu);
22c213
     } else {
22c213
         if (!write_list_to_cpustate(cpu)) {
22c213
             return -1;
22c213
-- 
22c213
1.8.3.1
22c213