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

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