live migration: Allow cleanup after cancellation or error

Introduce qemu_savevm_state_cancel and inject a stage -1 to cancel a
live migration. This gives the involved subsystems a chance to clean up
dynamically allocated resources. Namely, the block migration layer can
now free its device descriptors and pending blocks.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Jan Kiszka 2009-11-30 18:21:21 +01:00 committed by Anthony Liguori
parent 9a743e5ba3
commit 4ec7fcc7da
5 changed files with 54 additions and 8 deletions

View File

@ -317,11 +317,37 @@ static int is_stage2_completed(void)
return 1;
}
static void blk_mig_cleanup(void)
{
BlkMigDevState *bmds, *next_bmds;
BlkMigBlock *blk, *next_blk;
QTAILQ_FOREACH_SAFE(bmds, &block_mig_state.dev_list, entry, next_bmds) {
QTAILQ_REMOVE(&block_mig_state.dev_list, bmds, entry);
qemu_free(bmds);
}
QTAILQ_FOREACH_SAFE(blk, &block_mig_state.blk_list, entry, next_blk) {
QTAILQ_REMOVE(&block_mig_state.blk_list, blk, entry);
qemu_free(blk->buf);
qemu_free(blk);
}
set_dirty_tracking(0);
printf("\n");
}
static int block_save_live(QEMUFile *f, int stage, void *opaque)
{
dprintf("Enter save live stage %d submitted %d transferred %d\n",
stage, block_mig_state.submitted, block_mig_state.transferred);
if (stage < 0) {
blk_mig_cleanup();
return 0;
}
if (block_mig_state.blk_enable != 1) {
/* no need to migrate storage */
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
@ -338,7 +364,7 @@ static int block_save_live(QEMUFile *f, int stage, void *opaque)
flush_blks(f);
if (qemu_file_has_error(f)) {
set_dirty_tracking(0);
blk_mig_cleanup();
return 0;
}
@ -355,7 +381,7 @@ static int block_save_live(QEMUFile *f, int stage, void *opaque)
flush_blks(f);
if (qemu_file_has_error(f)) {
set_dirty_tracking(0);
blk_mig_cleanup();
return 0;
}
@ -365,15 +391,13 @@ static int block_save_live(QEMUFile *f, int stage, void *opaque)
}
blk_mig_save_dirty_blocks(f);
/* stop track dirty blocks */
set_dirty_tracking(0);
blk_mig_cleanup();
if (qemu_file_has_error(f)) {
return 0;
}
printf("\nBlock migration completed\n");
printf("Block migration completed\n");
}
qemu_put_be64(f, BLK_MIG_FLAG_EOS);

View File

@ -324,6 +324,7 @@ void migrate_fd_cancel(MigrationState *mig_state)
dprintf("cancelling migration\n");
s->state = MIG_STATE_CANCELLED;
qemu_savevm_state_cancel(s->file);
migrate_fd_cleanup(s);
}

View File

@ -1299,8 +1299,10 @@ int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared)
se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
}
if (qemu_file_has_error(f))
if (qemu_file_has_error(f)) {
qemu_savevm_state_cancel(f);
return -EIO;
}
return 0;
}
@ -1324,8 +1326,10 @@ int qemu_savevm_state_iterate(QEMUFile *f)
if (ret)
return 1;
if (qemu_file_has_error(f))
if (qemu_file_has_error(f)) {
qemu_savevm_state_cancel(f);
return -EIO;
}
return 0;
}
@ -1374,6 +1378,17 @@ int qemu_savevm_state_complete(QEMUFile *f)
return 0;
}
void qemu_savevm_state_cancel(QEMUFile *f)
{
SaveStateEntry *se;
QTAILQ_FOREACH(se, &savevm_handlers, entry) {
if (se->save_live_state) {
se->save_live_state(f, -1, se->opaque);
}
}
}
int qemu_savevm_state(QEMUFile *f)
{
int saved_vm_running;

View File

@ -65,6 +65,7 @@ void main_loop_wait(int timeout);
int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared);
int qemu_savevm_state_iterate(QEMUFile *f);
int qemu_savevm_state_complete(QEMUFile *f);
void qemu_savevm_state_cancel(QEMUFile *f);
int qemu_savevm_state(QEMUFile *f);
int qemu_loadvm_state(QEMUFile *f);

5
vl.c
View File

@ -2934,6 +2934,11 @@ static int ram_save_live(QEMUFile *f, int stage, void *opaque)
double bwidth = 0;
uint64_t expected_time = 0;
if (stage < 0) {
cpu_physical_memory_set_dirty_tracking(0);
return 0;
}
if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
qemu_file_set_error(f);
return 0;