Blame SOURCES/kvm-s390x-tod-Properly-stop-the-KVM-TOD-while-the-guest-.patch

1359fb
From f9494422b9c4abe8f7cfea0ecee729bbc618de02 Mon Sep 17 00:00:00 2001
1359fb
From: David Hildenbrand <david@redhat.com>
1359fb
Date: Fri, 21 Dec 2018 15:39:56 +0100
1359fb
Subject: [PATCH 12/14] s390x/tod: Properly stop the KVM TOD while the guest is
1359fb
 not running
1359fb
1359fb
RH-Author: David Hildenbrand <david@redhat.com>
1359fb
Message-id: <20181221153957.28183-12-david@redhat.com>
1359fb
Patchwork-id: 83765
1359fb
O-Subject: [RHEL-7.6.z qemu-kvm-ma PATCH 11/12] s390x/tod: Properly stop the KVM TOD while the guest is not running
1359fb
Bugzilla: 1672920
1359fb
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
1359fb
RH-Acked-by: Thomas Huth <thuth@redhat.com>
1359fb
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
1359fb
1359fb
Just like on other architectures, we should stop the clock while the guest
1359fb
is not running. This is already properly done for TCG. Right now, doing an
1359fb
offline migration (stop, migrate, cont) can easily trigger stalls in the
1359fb
guest.
1359fb
1359fb
Even doing a
1359fb
    (hmp) stop
1359fb
    ... wait 2 minutes ...
1359fb
    (hmp) cont
1359fb
will already trigger stalls.
1359fb
1359fb
So whenever the guest stops, backup the KVM TOD. When continuing to run
1359fb
the guest, restore the KVM TOD.
1359fb
1359fb
One special case is starting a simple VM: Reading the TOD from KVM to
1359fb
stop it right away until the guest is actually started means that the
1359fb
time of any simple VM will already differ to the host time. We can
1359fb
simply leave the TOD running and the guest won't be able to recognize
1359fb
it.
1359fb
1359fb
For migration, we actually want to keep the TOD stopped until really
1359fb
starting the guest. To be able to catch most errors, we should however
1359fb
try to set the TOD in addition to simply storing it. So we can still
1359fb
catch basic migration problems.
1359fb
1359fb
If anything goes wrong while backing up/restoring the TOD, we have to
1359fb
ignore it (but print a warning). This is then basically a fallback to
1359fb
old behavior (TOD remains running).
1359fb
1359fb
I tested this very basically with an initrd:
1359fb
    1. Start a simple VM. Observed that the TOD is kept running. Old
1359fb
       behavior.
1359fb
    2. Ordinary live migration. Observed that the TOD is temporarily
1359fb
       stopped on the destination when setting the new value and
1359fb
       correctly started when finally starting the guest.
1359fb
    3. Offline live migration. (stop, migrate, cont). Observed that the
1359fb
       TOD will be stopped on the source with the "stop" command. On the
1359fb
       destination, the TOD is temporarily stopped when setting the new
1359fb
       value and correctly started when finally starting the guest via
1359fb
       "cont".
1359fb
    4. Simple stop/cont correctly stops/starts the TOD. (multiple stops
1359fb
       or conts in a row have no effect, so works as expected)
1359fb
1359fb
In the future, we might want to send the guest a special kind of time sync
1359fb
interrupt under some conditions, so it can synchronize its tod to the
1359fb
host tod. This is interesting for migration scenarios but also when we
1359fb
get time sync interrupts ourselves. This however will most probably have
1359fb
to be handled in KVM (e.g. when the tods differ too much) and is not
1359fb
desired e.g. when debugging the guest (single stepping should not
1359fb
result in permanent time syncs). I consider something like that an add-on
1359fb
on top of this basic "don't break the guest" handling.
1359fb
1359fb
Signed-off-by: David Hildenbrand <david@redhat.com>
1359fb
Message-Id: <20181130094957.4121-1-david@redhat.com>
1359fb
Acked-by: Christian Borntraeger <borntraeger@de.ibm.com>
1359fb
Reviewed-by: Thomas Huth <thuth@redhat.com>
1359fb
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
1359fb
(cherry picked from commit 9bc9d3d1ae3bcd1caaad1946494726b52f58b291)
1359fb
Signed-off-by: David Hildenbrand <david@redhat.com>
1359fb
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1359fb
---
1359fb
 hw/s390x/tod-kvm.c     | 102 ++++++++++++++++++++++++++++++++++++++++++++++++-
1359fb
 include/hw/s390x/tod.h |   8 +++-
1359fb
 2 files changed, 107 insertions(+), 3 deletions(-)
1359fb
1359fb
diff --git a/hw/s390x/tod-kvm.c b/hw/s390x/tod-kvm.c
1359fb
index df564ab..2456bf7 100644
1359fb
--- a/hw/s390x/tod-kvm.c
1359fb
+++ b/hw/s390x/tod-kvm.c
1359fb
@@ -10,10 +10,11 @@
1359fb
 
1359fb
 #include "qemu/osdep.h"
1359fb
 #include "qapi/error.h"
1359fb
+#include "sysemu/sysemu.h"
1359fb
 #include "hw/s390x/tod.h"
1359fb
 #include "kvm_s390x.h"
1359fb
 
1359fb
-static void kvm_s390_tod_get(const S390TODState *td, S390TOD *tod, Error **errp)
1359fb
+static void kvm_s390_get_tod_raw(S390TOD *tod, Error **errp)
1359fb
 {
1359fb
     int r;
1359fb
 
1359fb
@@ -27,7 +28,17 @@ static void kvm_s390_tod_get(const S390TODState *td, S390TOD *tod, Error **errp)
1359fb
     }
1359fb
 }
1359fb
 
1359fb
-static void kvm_s390_tod_set(S390TODState *td, const S390TOD *tod, Error **errp)
1359fb
+static void kvm_s390_tod_get(const S390TODState *td, S390TOD *tod, Error **errp)
1359fb
+{
1359fb
+    if (td->stopped) {
1359fb
+        *tod = td->base;
1359fb
+        return;
1359fb
+    }
1359fb
+
1359fb
+    kvm_s390_get_tod_raw(tod, errp);
1359fb
+}
1359fb
+
1359fb
+static void kvm_s390_set_tod_raw(const S390TOD *tod, Error **errp)
1359fb
 {
1359fb
     int r;
1359fb
 
1359fb
@@ -41,18 +52,105 @@ static void kvm_s390_tod_set(S390TODState *td, const S390TOD *tod, Error **errp)
1359fb
     }
1359fb
 }
1359fb
 
1359fb
+static void kvm_s390_tod_set(S390TODState *td, const S390TOD *tod, Error **errp)
1359fb
+{
1359fb
+    Error *local_err = NULL;
1359fb
+
1359fb
+    /*
1359fb
+     * Somebody (e.g. migration) set the TOD. We'll store it into KVM to
1359fb
+     * properly detect errors now but take a look at the runstate to decide
1359fb
+     * whether really to keep the tod running. E.g. during migration, this
1359fb
+     * is the point where we want to stop the initially running TOD to fire
1359fb
+     * it back up when actually starting the migrated guest.
1359fb
+     */
1359fb
+    kvm_s390_set_tod_raw(tod, &local_err);
1359fb
+    if (local_err) {
1359fb
+        error_propagate(errp, local_err);
1359fb
+        return;
1359fb
+    }
1359fb
+
1359fb
+    if (runstate_is_running()) {
1359fb
+        td->stopped = false;
1359fb
+    } else {
1359fb
+        td->stopped = true;
1359fb
+        td->base = *tod;
1359fb
+    }
1359fb
+}
1359fb
+
1359fb
+static void kvm_s390_tod_vm_state_change(void *opaque, int running,
1359fb
+                                         RunState state)
1359fb
+{
1359fb
+    S390TODState *td = opaque;
1359fb
+    Error *local_err = NULL;
1359fb
+
1359fb
+    if (running && td->stopped) {
1359fb
+        /* Set the old TOD when running the VM - start the TOD clock. */
1359fb
+        kvm_s390_set_tod_raw(&td->base, &local_err);
1359fb
+        if (local_err) {
1359fb
+            warn_report_err(local_err);
1359fb
+        }
1359fb
+        /* Treat errors like the TOD was running all the time. */
1359fb
+        td->stopped = false;
1359fb
+    } else if (!running && !td->stopped) {
1359fb
+        /* Store the TOD when stopping the VM - stop the TOD clock. */
1359fb
+        kvm_s390_get_tod_raw(&td->base, &local_err);
1359fb
+        if (local_err) {
1359fb
+            /* Keep the TOD running in case we could not back it up. */
1359fb
+            warn_report_err(local_err);
1359fb
+        } else {
1359fb
+            td->stopped = true;
1359fb
+        }
1359fb
+    }
1359fb
+}
1359fb
+
1359fb
+static void kvm_s390_tod_realize(DeviceState *dev, Error **errp)
1359fb
+{
1359fb
+    S390TODState *td = S390_TOD(dev);
1359fb
+    S390TODClass *tdc = S390_TOD_GET_CLASS(td);
1359fb
+    Error *local_err = NULL;
1359fb
+
1359fb
+    tdc->parent_realize(dev, &local_err);
1359fb
+    if (local_err) {
1359fb
+        error_propagate(errp, local_err);
1359fb
+        return;
1359fb
+    }
1359fb
+
1359fb
+    /*
1359fb
+     * We need to know when the VM gets started/stopped to start/stop the TOD.
1359fb
+     * As we can never have more than one TOD instance (and that will never be
1359fb
+     * removed), registering here and never unregistering is good enough.
1359fb
+     */
1359fb
+    qemu_add_vm_change_state_handler(kvm_s390_tod_vm_state_change, td);
1359fb
+}
1359fb
+
1359fb
 static void kvm_s390_tod_class_init(ObjectClass *oc, void *data)
1359fb
 {
1359fb
     S390TODClass *tdc = S390_TOD_CLASS(oc);
1359fb
 
1359fb
+    device_class_set_parent_realize(DEVICE_CLASS(oc), kvm_s390_tod_realize,
1359fb
+                                    &tdc->parent_realize);
1359fb
     tdc->get = kvm_s390_tod_get;
1359fb
     tdc->set = kvm_s390_tod_set;
1359fb
 }
1359fb
 
1359fb
+static void kvm_s390_tod_init(Object *obj)
1359fb
+{
1359fb
+    S390TODState *td = S390_TOD(obj);
1359fb
+
1359fb
+    /*
1359fb
+     * The TOD is initially running (value stored in KVM). Avoid needless
1359fb
+     * loading/storing of the TOD when starting a simple VM, so let it
1359fb
+     * run although the (never started) VM is stopped. For migration, we
1359fb
+     * will properly set the TOD later.
1359fb
+     */
1359fb
+    td->stopped = false;
1359fb
+}
1359fb
+
1359fb
 static TypeInfo kvm_s390_tod_info = {
1359fb
     .name = TYPE_KVM_S390_TOD,
1359fb
     .parent = TYPE_S390_TOD,
1359fb
     .instance_size = sizeof(S390TODState),
1359fb
+    .instance_init = kvm_s390_tod_init,
1359fb
     .class_init = kvm_s390_tod_class_init,
1359fb
     .class_size = sizeof(S390TODClass),
1359fb
 };
1359fb
diff --git a/include/hw/s390x/tod.h b/include/hw/s390x/tod.h
1359fb
index 413c0d7..cbd7552 100644
1359fb
--- a/include/hw/s390x/tod.h
1359fb
+++ b/include/hw/s390x/tod.h
1359fb
@@ -31,13 +31,19 @@ typedef struct S390TODState {
1359fb
     /* private */
1359fb
     DeviceState parent_obj;
1359fb
 
1359fb
-    /* unused by KVM implementation */
1359fb
+    /*
1359fb
+     * Used by TCG to remember the time base. Used by KVM to backup the TOD
1359fb
+     * while the TOD is stopped.
1359fb
+     */
1359fb
     S390TOD base;
1359fb
+    /* Used by KVM to remember if the TOD is stopped and base is valid. */
1359fb
+    bool stopped;
1359fb
 } S390TODState;
1359fb
 
1359fb
 typedef struct S390TODClass {
1359fb
     /* private */
1359fb
     DeviceClass parent_class;
1359fb
+    void (*parent_realize)(DeviceState *dev, Error **errp);
1359fb
 
1359fb
     /* public */
1359fb
     void (*get)(const S390TODState *td, S390TOD *tod, Error **errp);
1359fb
-- 
1359fb
1.8.3.1
1359fb