Blame SOURCES/kvm-migration-Wait-for-semaphore-before-completing-migra.patch

4a2fec
From 926144680056dd6773d9ef8ad64c4a593dd597a9 Mon Sep 17 00:00:00 2001
4a2fec
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
4a2fec
Date: Wed, 25 Oct 2017 18:28:34 +0200
4a2fec
Subject: [PATCH 15/19] migration: Wait for semaphore before completing
4a2fec
 migration
4a2fec
4a2fec
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
4a2fec
Message-id: <20171025182838.31829-4-dgilbert@redhat.com>
4a2fec
Patchwork-id: 77439
4a2fec
O-Subject: [RHV-7.5 qemu-kvm-rhev PATCH 3/7] migration: Wait for semaphore before completing migration
4a2fec
Bugzilla: 1497120
4a2fec
RH-Acked-by: Peter Xu <peterx@redhat.com>
4a2fec
RH-Acked-by: Juan Quintela <quintela@redhat.com>
4a2fec
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
4a2fec
4a2fec
Wait for a semaphore before completing the migration,
4a2fec
if the previously added capability was enabled.
4a2fec
4a2fec
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
4a2fec
Reviewed-by: Peter Xu <peterx@redhat.com>
4a2fec
Reviewed-by: Juan Quintela <quintela@redhat.com>
4a2fec
Signed-off-by: Juan Quintela <quintela@redhat.com>
4a2fec
(cherry picked from commit e91d8951d59d483f085f7650381b8e55a1a55e4c)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 migration/migration.c | 38 ++++++++++++++++++++++++++++++++++++++
4a2fec
 migration/migration.h |  3 +++
4a2fec
 2 files changed, 41 insertions(+)
4a2fec
4a2fec
diff --git a/migration/migration.c b/migration/migration.c
4a2fec
index 54db29f..436e122 100644
4a2fec
--- a/migration/migration.c
4a2fec
+++ b/migration/migration.c
4a2fec
@@ -1847,6 +1847,39 @@ fail:
4a2fec
 }
4a2fec
 
4a2fec
 /**
4a2fec
+ * migration_maybe_pause: Pause if required to by
4a2fec
+ * migrate_pause_before_switchover called with the iothread locked
4a2fec
+ * Returns: 0 on success
4a2fec
+ */
4a2fec
+static int migration_maybe_pause(MigrationState *s, int *current_active_state)
4a2fec
+{
4a2fec
+    if (!migrate_pause_before_switchover()) {
4a2fec
+        return 0;
4a2fec
+    }
4a2fec
+
4a2fec
+    /* Since leaving this state is not atomic with posting the semaphore
4a2fec
+     * it's possible that someone could have issued multiple migrate_continue
4a2fec
+     * and the semaphore is incorrectly positive at this point;
4a2fec
+     * the docs say it's undefined to reinit a semaphore that's already
4a2fec
+     * init'd, so use timedwait to eat up any existing posts.
4a2fec
+     */
4a2fec
+    while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
4a2fec
+        /* This block intentionally left blank */
4a2fec
+    }
4a2fec
+
4a2fec
+    qemu_mutex_unlock_iothread();
4a2fec
+    migrate_set_state(&s->state, *current_active_state,
4a2fec
+                      MIGRATION_STATUS_PRE_SWITCHOVER);
4a2fec
+    qemu_sem_wait(&s->pause_sem);
4a2fec
+    migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
4a2fec
+                      MIGRATION_STATUS_DEVICE);
4a2fec
+    *current_active_state = MIGRATION_STATUS_DEVICE;
4a2fec
+    qemu_mutex_lock_iothread();
4a2fec
+
4a2fec
+    return s->state == MIGRATION_STATUS_DEVICE ? 0 : -EINVAL;
4a2fec
+}
4a2fec
+
4a2fec
+/**
4a2fec
  * migration_completion: Used by migration_thread when there's not much left.
4a2fec
  *   The caller 'breaks' the loop when this returns.
4a2fec
  *
4a2fec
@@ -1872,6 +1905,9 @@ static void migration_completion(MigrationState *s, int current_active_state,
4a2fec
             bool inactivate = !migrate_colo_enabled();
4a2fec
             ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
4a2fec
             if (ret >= 0) {
4a2fec
+                ret = migration_maybe_pause(s, &current_active_state);
4a2fec
+            }
4a2fec
+            if (ret >= 0) {
4a2fec
                 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
4a2fec
                 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
4a2fec
                                                          inactivate);
4a2fec
@@ -2239,6 +2275,7 @@ static void migration_instance_finalize(Object *obj)
4a2fec
 
4a2fec
     g_free(params->tls_hostname);
4a2fec
     g_free(params->tls_creds);
4a2fec
+    qemu_sem_destroy(&ms->pause_sem);
4a2fec
 }
4a2fec
 
4a2fec
 static void migration_instance_init(Object *obj)
4a2fec
@@ -2249,6 +2286,7 @@ static void migration_instance_init(Object *obj)
4a2fec
     ms->state = MIGRATION_STATUS_NONE;
4a2fec
     ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
4a2fec
     ms->mbps = -1;
4a2fec
+    qemu_sem_init(&ms->pause_sem, 0);
4a2fec
 
4a2fec
     params->tls_hostname = g_strdup("");
4a2fec
     params->tls_creds = g_strdup("");
4a2fec
diff --git a/migration/migration.h b/migration/migration.h
4a2fec
index 2eebad8..895cc34 100644
4a2fec
--- a/migration/migration.h
4a2fec
+++ b/migration/migration.h
4a2fec
@@ -120,6 +120,9 @@ struct MigrationState
4a2fec
     /* Flag set once the migration thread called bdrv_inactivate_all */
4a2fec
     bool block_inactive;
4a2fec
 
4a2fec
+    /* Migration is paused due to pause-before-switchover */
4a2fec
+    QemuSemaphore pause_sem;
4a2fec
+
4a2fec
     /* The semaphore is used to notify COLO thread that failover is finished */
4a2fec
     QemuSemaphore colo_exit_sem;
4a2fec
 
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec