a7b9285
From bce1e693cdf25a98c120882197c9fcce975fb039 Mon Sep 17 00:00:00 2001
Alon Levy 408bdb5
From: Paolo Bonzini <pbonzini@redhat.com>
Alon Levy 408bdb5
Date: Fri, 22 Feb 2013 17:36:21 +0100
a7b9285
Subject: [PATCH] migration: cleanup migration (including thread) in the
a7b9285
 iothread
Alon Levy 408bdb5
Alon Levy 408bdb5
Perform final cleanup in a bottom half, and add joining the thread to
Alon Levy 408bdb5
the series of cleanup actions.
Alon Levy 408bdb5
Alon Levy 408bdb5
migrate_fd_error remains for connection error, but it doesn't need
Alon Levy 408bdb5
to cleanup anything anymore.
Alon Levy 408bdb5
Alon Levy 408bdb5
Reviewed-by: Orit Wasserman <owasserm@redhat.com>
Alon Levy 408bdb5
Reviewed-by: Juan Quintela <quintela@redhat.com>
Alon Levy 408bdb5
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Alon Levy 408bdb5
Signed-off-by: Juan Quintela <quintela@redhat.com>
a7b9285
(cherry picked from commit bb1fadc444ff967554c41d96cb9dde110e8aece9)
Alon Levy 408bdb5
---
Alon Levy 408bdb5
 include/migration/migration.h |  1 +
Alon Levy 408bdb5
 migration.c                   | 38 ++++++++++++++++++++------------------
Alon Levy 408bdb5
 2 files changed, 21 insertions(+), 18 deletions(-)
Alon Levy 408bdb5
Alon Levy 408bdb5
diff --git a/include/migration/migration.h b/include/migration/migration.h
Alon Levy 408bdb5
index 3e680af..ed20bed 100644
Alon Levy 408bdb5
--- a/include/migration/migration.h
Alon Levy 408bdb5
+++ b/include/migration/migration.h
Alon Levy 408bdb5
@@ -38,6 +38,7 @@ struct MigrationState
Alon Levy 408bdb5
     size_t buffer_size;
Alon Levy 408bdb5
     size_t buffer_capacity;
Alon Levy 408bdb5
     QemuThread thread;
Alon Levy 408bdb5
+    QEMUBH *cleanup_bh;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     QEMUFile *file;
Alon Levy 408bdb5
     int fd;
Alon Levy 408bdb5
diff --git a/migration.c b/migration.c
Alon Levy 408bdb5
index e844b2c..437475b 100644
Alon Levy 408bdb5
--- a/migration.c
Alon Levy 408bdb5
+++ b/migration.c
Alon Levy 408bdb5
@@ -261,8 +261,13 @@ void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 /* shared migration helpers */
Alon Levy 408bdb5
 
Alon Levy 408bdb5
-static void migrate_fd_cleanup(MigrationState *s)
Alon Levy 408bdb5
+static void migrate_fd_cleanup(void *opaque)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
+    MigrationState *s = opaque;
Alon Levy 408bdb5
+
Alon Levy 408bdb5
+    qemu_bh_delete(s->cleanup_bh);
Alon Levy 408bdb5
+    s->cleanup_bh = NULL;
Alon Levy 408bdb5
+
Alon Levy 408bdb5
     if (s->file) {
Alon Levy 408bdb5
         DPRINTF("closing file\n");
Alon Levy 408bdb5
         qemu_fclose(s->file);
Alon Levy 408bdb5
@@ -290,15 +295,10 @@ static void migrate_finish_set_state(MigrationState *s, int new_state)
Alon Levy 408bdb5
 void migrate_fd_error(MigrationState *s)
Alon Levy 408bdb5
 {
Alon Levy 408bdb5
     DPRINTF("setting error state\n");
Alon Levy 408bdb5
-    migrate_finish_set_state(s, MIG_STATE_ERROR);
Alon Levy 408bdb5
-    migrate_fd_cleanup(s);
Alon Levy 408bdb5
-}
Alon Levy 408bdb5
-
Alon Levy 408bdb5
-static void migrate_fd_completed(MigrationState *s)
Alon Levy 408bdb5
-{
Alon Levy 408bdb5
-    DPRINTF("setting completed state\n");
Alon Levy 408bdb5
-    migrate_finish_set_state(s, MIG_STATE_COMPLETED);
Alon Levy 408bdb5
-    migrate_fd_cleanup(s);
Alon Levy 408bdb5
+    assert(s->file == NULL);
Alon Levy 408bdb5
+    s->state = MIG_STATE_ERROR;
Alon Levy 408bdb5
+    trace_migrate_set_state(MIG_STATE_ERROR);
Alon Levy 408bdb5
+    notifier_list_notify(&migration_state_notifiers, s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 static ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
Alon Levy 408bdb5
@@ -325,7 +325,6 @@ static void migrate_fd_cancel(MigrationState *s)
Alon Levy 408bdb5
     DPRINTF("cancelling migration\n");
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     migrate_finish_set_state(s, MIG_STATE_CANCELLED);
Alon Levy 408bdb5
-    migrate_fd_cleanup(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
 int migrate_fd_close(MigrationState *s)
Alon Levy 408bdb5
@@ -590,6 +589,11 @@ static int buffered_close(void *opaque)
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     DPRINTF("closing\n");
Alon Levy 408bdb5
 
Alon Levy 408bdb5
+    qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
+    qemu_thread_join(&s->thread);
Alon Levy 408bdb5
+    qemu_mutex_lock_iothread();
Alon Levy 408bdb5
+    assert(s->state != MIG_STATE_ACTIVE);
Alon Levy 408bdb5
+
Alon Levy 408bdb5
     return migrate_fd_close(s);
Alon Levy 408bdb5
 }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
@@ -712,13 +716,9 @@ static void *buffered_file_thread(void *opaque)
Alon Levy 408bdb5
         }
Alon Levy 408bdb5
         buffered_flush(s);
Alon Levy 408bdb5
         if (qemu_file_get_error(s->file)) {
Alon Levy 408bdb5
-            qemu_mutex_lock_iothread();
Alon Levy 408bdb5
-            migrate_fd_error(s);
Alon Levy 408bdb5
-            qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
+            migrate_finish_set_state(s, MIG_STATE_ERROR);
Alon Levy 408bdb5
         } else if (last_round && s->buffer_size == 0) {
Alon Levy 408bdb5
-            qemu_mutex_lock_iothread();
Alon Levy 408bdb5
-            migrate_fd_completed(s);
Alon Levy 408bdb5
-            qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
+            migrate_finish_set_state(s, MIG_STATE_COMPLETED);
Alon Levy 408bdb5
         }
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
 
Alon Levy 408bdb5
@@ -734,6 +734,7 @@ static void *buffered_file_thread(void *opaque)
Alon Levy 408bdb5
             vm_start();
Alon Levy 408bdb5
         }
Alon Levy 408bdb5
     }
Alon Levy 408bdb5
+    qemu_bh_schedule(s->cleanup_bh);
Alon Levy 408bdb5
     qemu_mutex_unlock_iothread();
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     g_free(s->buffer);
Alon Levy 408bdb5
@@ -763,9 +764,10 @@ void migrate_fd_connect(MigrationState *s)
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
Alon Levy 408bdb5
 
Alon Levy 408bdb5
+    s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Alon Levy 408bdb5
     s->file = qemu_fopen_ops(s, &buffered_file_ops);
Alon Levy 408bdb5
 
Alon Levy 408bdb5
     qemu_thread_create(&s->thread, buffered_file_thread, s,
Alon Levy 408bdb5
-                       QEMU_THREAD_DETACHED);
Alon Levy 408bdb5
+                       QEMU_THREAD_JOINABLE);
Alon Levy 408bdb5
     notifier_list_notify(&migration_state_notifiers, s);
Alon Levy 408bdb5
 }