Skip to contentSkip to conversation

migration: disallow migrate_add_blocker during migration

PATCH Superseded John Snow 3 participants 4 messages Sep 29, 2015 – Oct 1, 2015 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH 1/1 migration: disallow migrate_add_blocker during migration

If a migration is already in progress and somebody attempts to add a migration blocker, this should rightly fail.

Add an errp parameter and a retcode return value to migrate_add_blocker.

This is part one of two for a solution to prohibit e.g. block jobs from running concurrently with migration.

---
 block/qcow.c                  |  5 ++++-
 block/vdi.c                   |  5 ++++-
 block/vhdx.c                  |  5 ++++-
 block/vmdk.c                  | 13 +++++++++----
 block/vpc.c                   |  9 ++++++---
 block/vvfat.c                 | 19 +++++++++++--------
 hw/9pfs/virtio-9p.c           | 15 +++++++++++----
 hw/misc/ivshmem.c             |  5 ++++-
 hw/scsi/vhost-scsi.c          | 11 +++++++----
 hw/virtio/vhost.c             | 31 +++++++++++++++++++------------
 include/migration/migration.h |  4 +++-
 migration/migration.c         | 32 ++++++++++++++++++++++++++++----
 stubs/migr-blocker.c          |  3 ++-
 target-i386/kvm.c             |  6 +++++-
 14 files changed, 117 insertions(+), 46 deletions(-)
block/qcow.c +4 -1
index 6e35db1..1b82dec 100644
@@ -236,7 +236,10 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
 
     qemu_co_mutex_init(&s->lock);
     return 0;
block/vdi.c +4 -1
index 062a654..95b2690 100644
@@ -505,7 +505,10 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail_free_bmap;
+    }
 
     qemu_co_mutex_init(&s->write_lock);
 
block/vhdx.c +4 -1
index d3bb1bd..5bebe34 100644
@@ -1005,7 +1005,10 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
 
     return 0;
 fail:
block/vmdk.c +9 -4
index be0d640..09dcf6b 100644
@@ -943,15 +943,20 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
     if (ret) {
         goto fail;
     }
-    s->cid = vmdk_read_cid(bs, 0);
-    s->parent_cid = vmdk_read_cid(bs, 1);
-    qemu_co_mutex_init(&s->lock);
 
     /* Disable migration when VMDK images are used */
     error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
+
+    s->cid = vmdk_read_cid(bs, 0);
+    s->parent_cid = vmdk_read_cid(bs, 1);
+    qemu_co_mutex_init(&s->lock);
+
     g_free(buf);
     return 0;
 
block/vpc.c +6 -3
index 2b3b518..4c60942 100644
@@ -325,13 +325,16 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 #endif
     }
 
-    qemu_co_mutex_init(&s->lock);
-
     /* Disable migration when VHD images are used */
     error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
+
+    qemu_co_mutex_init(&s->lock);
 
     return 0;
 
block/vvfat.c +11 -8
index 7ddc962..100f42a 100644
@@ -1191,22 +1191,25 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 
     s->sector_count = s->faked_sectors + s->sectors_per_cluster*s->cluster_count;
 
-    if (s->first_sectors_number == 0x40) {
-        init_mbr(s, cyls, heads, secs);
-    }
-
-    //    assert(is_consistent(s));
-    qemu_co_mutex_init(&s->lock);
-
     /* Disable migration when vvfat is used rw */
     if (s->qcow) {
         error_setg(&s->migration_blocker,
                    "The vvfat (rw) format used by node '%s' "
                    "does not support live migration",
                    bdrv_get_device_or_node_name(bs));
-        migrate_add_blocker(s->migration_blocker);
+        ret = migrate_add_blocker(s->migration_blocker, errp);
+        if (ret < 0) {
+            goto fail;
+        }
     }
 
+    if (s->first_sectors_number == 0x40) {
+        init_mbr(s, cyls, heads, secs);
+    }
+
+    //    assert(is_consistent(s));
+    qemu_co_mutex_init(&s->lock);
+
     ret = 0;
 fail:
     qemu_opts_del(opts);
hw/9pfs/virtio-9p.c +11 -4
index f972731..4572e18 100644
@@ -978,20 +978,27 @@ static void v9fs_attach(void *opaque)
         clunk_fid(s, fid);
         goto out;
     }
-    err += offset;
-    trace_v9fs_attach_return(pdu->tag, pdu->id,
-                             qid.type, qid.version, qid.path);
+
     /*
      * disable migration if we haven't done already.
      * attach could get called multiple times for the same export.
      */
     if (!s->migration_blocker) {
+        int ret;
         s->root_fid = fid;
         error_setg(&s->migration_blocker,
                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
-        migrate_add_blocker(s->migration_blocker);
+        ret = migrate_add_blocker(s->migration_blocker, NULL);
+        if (ret < 0) {
+            clunk_fid(s, fid);
+            goto out;
+        }
     }
+
+    err += offset;
+    trace_v9fs_attach_return(pdu->tag, pdu->id,
+                             qid.type, qid.version, qid.path);
 out:
     put_fid(pdu, fidp);
 out_nofid:
hw/misc/ivshmem.c +4 -1
index cc76989..859e844 100644
@@ -740,7 +740,10 @@ static int pci_ivshmem_init(PCIDevice *dev)
     if (s->role_val == IVSHMEM_PEER) {
         error_setg(&s->migration_blocker,
                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
-        migrate_add_blocker(s->migration_blocker);
+        if (migrate_add_blocker(s->migration_blocker, NULL) < 0) {
+            error_report("Unable to prohibit migration during ivshmem init");
+            exit(1);
+        }
     }
 
     pci_conf = dev->config;
hw/scsi/vhost-scsi.c +7 -4
index fb7983d..5c6d7a2 100644
@@ -248,6 +248,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
     s->dev.vq_index = 0;
     s->dev.backend_features = 0;
 
+    error_setg(&s->migration_blocker,
+            "vhost-scsi does not support migration");
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        return;
+    }
+
     ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
                          VHOST_BACKEND_TYPE_KERNEL);
     if (ret < 0) {
@@ -261,10 +268,6 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
     s->lun = 0;
     /* Note: we can also get the minimum tpgt from kernel */
     s->target = vs->conf.boot_tpgt;
-
-    error_setg(&s->migration_blocker,
-            "vhost-scsi does not support migration");
-    migrate_add_blocker(s->migration_blocker);
 }
 
 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
hw/virtio/vhost.c +19 -12
index c0ed5b2..0f27a2d 100644
@@ -926,13 +926,24 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         goto fail;
     }
 
-    for (i = 0; i < hdev->nvqs; ++i) {
-        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
-        if (r < 0) {
-            goto fail_vq;
-        }
-    }
     hdev->features = features;
+    hdev->migration_blocker = NULL;
+
+    if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
+        error_setg(&hdev->migration_blocker,
+                   "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
+        r = migrate_add_blocker(hdev->migration_blocker, NULL);
+        if (r < 0) {
+            goto fail_keep_r;
+        }
+    }
+
+    for (i = 0; i < hdev->nvqs; ++i) {
+        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
+        if (r < 0) {
+            goto fail_vq;
+        }
+    }
 
     hdev->memory_listener = (MemoryListener) {
         .begin = vhost_begin,
@@ -949,12 +960,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         .eventfd_del = vhost_eventfd_del,
         .priority = 10
     };
-    hdev->migration_blocker = NULL;
-    if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
-        error_setg(&hdev->migration_blocker,
-                   "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
-        migrate_add_blocker(hdev->migration_blocker);
-    }
+
     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
     hdev->n_mem_sections = 0;
     hdev->mem_sections = NULL;
@@ -971,6 +977,7 @@ fail_vq:
     }
 fail:
     r = -errno;
+fail_keep_r:
     hdev->vhost_ops->vhost_backend_cleanup(hdev);
     return r;
 }
include/migration/migration.h +3 -1
index 8334621..de7ebb3 100644
@@ -117,6 +117,7 @@ int migrate_fd_close(MigrationState *s);
 void add_migration_state_change_notifier(Notifier *notify);
 void remove_migration_state_change_notifier(Notifier *notify);
 bool migration_in_setup(MigrationState *);
+bool migration_has_started(MigrationState *s);
 bool migration_has_finished(MigrationState *);
 bool migration_has_failed(MigrationState *);
 MigrationState *migrate_get_current(void);
@@ -150,8 +151,9 @@ void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
  * @migrate_add_blocker - prevent migration from proceeding
  *
  * @reason - an error to be returned whenever migration is attempted
+ * @errp - [out] The reason (if any) we cannot block migration right now.
  */
-void migrate_add_blocker(Error *reason);
+int migrate_add_blocker(Error *reason, Error **errp);
 
 /**
  * @migrate_del_blocker - remove a blocking error from migration
migration/migration.c +28 -4
index e48dd13..a457cfc 100644
@@ -632,6 +632,26 @@ bool migration_has_failed(MigrationState *s)
             s->state == MIGRATION_STATUS_FAILED);
 }
 
+bool migration_has_started(MigrationState *s)
+{
+    if (!s) {
+        s = migrate_get_current();
+    }
+
+    switch (s->state) {
+    case MIGRATION_STATUS_NONE:
+    case MIGRATION_STATUS_CANCELLED:
+    case MIGRATION_STATUS_COMPLETED:
+    case MIGRATION_STATUS_FAILED:
+        return false;
+    case MIGRATION_STATUS_SETUP:
+    case MIGRATION_STATUS_CANCELLING:
+    case MIGRATION_STATUS_ACTIVE:
+    default:
+        return true;
+    }
+}
+
 static MigrationState *migrate_init(const MigrationParams *params)
 {
     MigrationState *s = migrate_get_current();
@@ -667,9 +687,15 @@ static MigrationState *migrate_init(const MigrationParams *params)
 
 static GSList *migration_blockers;
 
-void migrate_add_blocker(Error *reason)
+int migrate_add_blocker(Error *reason, Error **errp)
 {
+    if (migration_has_started(NULL)) {
+        error_setg(errp, "Cannot block a migration already in-progress");
+        return -EINPROGRESS;
+    }
+
     migration_blockers = g_slist_prepend(migration_blockers, reason);
+    return 0;
 }
 
 void migrate_del_blocker(Error *reason)
@@ -712,9 +738,7 @@ void qmp_migrate(const char *uri, bool has_blk, bool blk,
     params.blk = has_blk && blk;
     params.shared = has_inc && inc;
 
-    if (s->state == MIGRATION_STATUS_ACTIVE ||
-        s->state == MIGRATION_STATUS_SETUP ||
-        s->state == MIGRATION_STATUS_CANCELLING) {
+    if (migration_has_started(s)) {
         error_setg(errp, QERR_MIGRATION_ACTIVE);
         return;
     }
stubs/migr-blocker.c +2 -1
index 300df6e..06812bd 100644
@@ -1,8 +1,9 @@
 #include "qemu-common.h"
 #include "migration/migration.h"
 
-void migrate_add_blocker(Error *reason)
+int migrate_add_blocker(Error *reason, Error **errp)
 {
+    return 0;
 }
 
 void migrate_del_blocker(Error *reason)
target-i386/kvm.c +5 -1
index 7b0ba17..d535029 100644
@@ -731,7 +731,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
         error_setg(&invtsc_mig_blocker,
                    "State blocked by non-migratable CPU device"
                    " (invtsc flag)");
-        migrate_add_blocker(invtsc_mig_blocker);
+        r = migrate_add_blocker(invtsc_mig_blocker, NULL);
+        if (r < 0) {
+            fprintf(stderr, "migrate_add_blocker failed\n");
+            return r;
+        }
         /* for savevm */
         vmstate_x86_cpu.unmigratable = 1;
     }
Signature · 1 line
2.4.3
#1 lore raw

Am 29.09.2015 um 22:20 hat John Snow geschrieben:

Quoted 9 lines

If a migration is already in progress and somebody attempts to add a migration blocker, this should rightly fail.

Add an errp parameter and a retcode return value to migrate_add_blocker.

This is part one of two for a solution to prohibit e.g. block jobs from running concurrently with migration.

Through which tree should this be merged?

Quoted 21 lines
 block/qcow.c                  |  5 ++++-
 block/vdi.c                   |  5 ++++-
 block/vhdx.c                  |  5 ++++-
 block/vmdk.c                  | 13 +++++++++----
 block/vpc.c                   |  9 ++++++---
 block/vvfat.c                 | 19 +++++++++++--------
 hw/9pfs/virtio-9p.c           | 15 +++++++++++----
 hw/misc/ivshmem.c             |  5 ++++-
 hw/scsi/vhost-scsi.c          | 11 +++++++----
 hw/virtio/vhost.c             | 31 +++++++++++++++++++------------
 include/migration/migration.h |  4 +++-
 migration/migration.c         | 32 ++++++++++++++++++++++++++++----
 stubs/migr-blocker.c          |  3 ++-
 target-i386/kvm.c             |  6 +++++-
 14 files changed, 117 insertions(+), 46 deletions(-)
block/qcow.c +0 -0
index 6e35db1..1b82dec 100644

block/qcow.c static int qcow_open(BlockDriverState *bs, QDict *options, int flags, · line 241

Quoted 7 lines
block/qcow.c +3 -1
@@ -236,7 +236,10 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;

This error path leaks s->migration_blocker.

Quoted 9 lines
block/qcow.c +1 -0
+    }
 
     qemu_co_mutex_init(&s->lock);
     return 0;
block/vdi.c +0 -0
index 062a654..95b2690 100644

block/vdi.c static int vdi_open(BlockDriverState *bs, QDict *options, int flags, · line 510

Quoted 7 lines
block/vdi.c +3 -1
@@ -505,7 +505,10 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail_free_bmap;

Same.

Quoted 10 lines
block/vdi.c +1 -0
+    }
 
     qemu_co_mutex_init(&s->write_lock);
 
block/vhdx.c +0 -0
index d3bb1bd..5bebe34 100644
@@ -1005,7 +1005,10 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "

block/vhdx.c static int vhdx_open(BlockDriverState *bs, QDict *options, int flags, · line 1014

Quoted 10 lines
block/vhdx.c +4 -1
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
 
     return 0;
 fail:

This one happens to be okay because VHDX uses the close function in the failure path (and at last up to now that function even seems to cope with half-initialised images - it just feels a bit brittle).

block/vmdk.c static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, · line 947

Quoted 10 lines
block/vmdk.c +0 -2
index be0d640..09dcf6b 100644
@@ -943,15 +943,20 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
     if (ret) {
         goto fail;
     }
-    s->cid = vmdk_read_cid(bs, 0);
-    s->parent_cid = vmdk_read_cid(bs, 1);

Why do you move this code? It doesn't seem to do anything that you would need to undo on failure.

block/vmdk.c static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, · line 954

Quoted 11 lines
block/vmdk.c +4 -2
-    qemu_co_mutex_init(&s->lock);
 
     /* Disable migration when VMDK images are used */
     error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }

But the usual leak is still there. :-)

Quoted 16 lines
block/vmdk.c +5 -0
+
+    s->cid = vmdk_read_cid(bs, 0);
+    s->parent_cid = vmdk_read_cid(bs, 1);
+    qemu_co_mutex_init(&s->lock);
+
     g_free(buf);
     return 0;
 
block/vpc.c +0 -0
index 2b3b518..4c60942 100644
@@ -325,13 +325,16 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 #endif
     }
 

block/vpc.c static int vpc_open(BlockDriverState *bs, QDict *options, int flags, · line 334

Quoted 10 lines
block/vpc.c +3 -3
-    qemu_co_mutex_init(&s->lock);
-
     /* Disable migration when VHD images are used */
     error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;

And another one.

Quoted 21 lines
block/vpc.c +3 -0
+    }
+
+    qemu_co_mutex_init(&s->lock);
 
     return 0;
 
block/vvfat.c +0 -7
index 7ddc962..100f42a 100644
@@ -1191,22 +1191,25 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 
     s->sector_count = s->faked_sectors + s->sectors_per_cluster*s->cluster_count;
 
-    if (s->first_sectors_number == 0x40) {
-        init_mbr(s, cyls, heads, secs);
-    }
-
-    //    assert(is_consistent(s));
-    qemu_co_mutex_init(&s->lock);
-

block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1202

Quoted 10 lines
block/vvfat.c +3 -1
     /* Disable migration when vvfat is used rw */
     if (s->qcow) {
         error_setg(&s->migration_blocker,
                    "The vvfat (rw) format used by node '%s' "
                    "does not support live migration",
                    bdrv_get_device_or_node_name(bs));
-        migrate_add_blocker(s->migration_blocker);
+        ret = migrate_add_blocker(s->migration_blocker, errp);
+        if (ret < 0) {
+            goto fail;

And the final one in the block drivers.

Quoted 30 lines
block/vvfat.c +8 -0
+        }
     }
 
+    if (s->first_sectors_number == 0x40) {
+        init_mbr(s, cyls, heads, secs);
+    }
+
+    //    assert(is_consistent(s));
+    qemu_co_mutex_init(&s->lock);
+
     ret = 0;
 fail:
     qemu_opts_del(opts);
hw/9pfs/virtio-9p.c +1 -3
index f972731..4572e18 100644
@@ -978,20 +978,27 @@ static void v9fs_attach(void *opaque)
         clunk_fid(s, fid);
         goto out;
     }
-    err += offset;
-    trace_v9fs_attach_return(pdu->tag, pdu->id,
-                             qid.type, qid.version, qid.path);
+
     /*
      * disable migration if we haven't done already.
      * attach could get called multiple times for the same export.
      */
     if (!s->migration_blocker) {

hw/9pfs/virtio-9p.c static void v9fs_attach(void *opaque) · line 995

Quoted 10 lines
hw/9pfs/virtio-9p.c +5 -1
+        int ret;
         s->root_fid = fid;
         error_setg(&s->migration_blocker,
                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
-        migrate_add_blocker(s->migration_blocker);
+        ret = migrate_add_blocker(s->migration_blocker, NULL);
+        if (ret < 0) {
+            clunk_fid(s, fid);
+            goto out;

I think you need to set err instead of ret, so we run the error path in complete_pdu().

The migration blocker seems to be freed in put_fid(), good.

Quoted 14 lines
hw/9pfs/virtio-9p.c +5 -0
+        }
     }
+
+    err += offset;
+    trace_v9fs_attach_return(pdu->tag, pdu->id,
+                             qid.type, qid.version, qid.path);
 out:
     put_fid(pdu, fidp);
 out_nofid:
hw/misc/ivshmem.c +0 -0
index cc76989..859e844 100644

hw/misc/ivshmem.c static int pci_ivshmem_init(PCIDevice *dev) · line 745

Quoted 7 lines
hw/misc/ivshmem.c +3 -1
@@ -740,7 +740,10 @@ static int pci_ivshmem_init(PCIDevice *dev)
     if (s->role_val == IVSHMEM_PEER) {
         error_setg(&s->migration_blocker,
                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
-        migrate_add_blocker(s->migration_blocker);
+        if (migrate_add_blocker(s->migration_blocker, NULL) < 0) {
+            error_report("Unable to prohibit migration during ivshmem init");
+            exit(1);

Seriously? :-/

I see that the whole function does the same and has an int return value only so that it can always return 0. So what you're doing is probably right for this patch, but generally, the error handling in this init function is horrible.

The good thing is that you can't leak anything this way...

Quoted 9 lines
hw/misc/ivshmem.c +1 -0
+        }
     }
 
     pci_conf = dev->config;
hw/scsi/vhost-scsi.c +0 -0
index fb7983d..5c6d7a2 100644

hw/scsi/vhost-scsi.c static void vhost_scsi_realize(DeviceState *dev, Error **errp) · line 255

Quoted 8 lines
hw/scsi/vhost-scsi.c +5 -0
@@ -248,6 +248,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
     s->dev.vq_index = 0;
     s->dev.backend_features = 0;
 
+    error_setg(&s->migration_blocker,
+            "vhost-scsi does not support migration");
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        return;

This leaks not only s->migration_blocker, but also vhostfd.

Quoted 7 lines
hw/scsi/vhost-scsi.c +2 -0
+    }
+
     ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
                          VHOST_BACKEND_TYPE_KERNEL);
     if (ret < 0) {

error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
strerror(-ret));

Now that you moved the migration blocker code before this, this error path leaks s->migration_blocker as well (in this case meaning that both the blocker needs to be removed and the error object be freed).

Quoted 28 lines
         return;
     }

     /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
     s->channel = 0;
     s->lun = 0;
     /* Note: we can also get the minimum tpgt from kernel */
     s->target = vs->conf.boot_tpgt;
hw/scsi/vhost-scsi.c +0 -4
-
-    error_setg(&s->migration_blocker,
-            "vhost-scsi does not support migration");
-    migrate_add_blocker(s->migration_blocker);
 }
 
 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
hw/virtio/vhost.c +0 -5
index c0ed5b2..0f27a2d 100644
@@ -926,13 +926,24 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         goto fail;
     }
 
-    for (i = 0; i < hdev->nvqs; ++i) {
-        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
-        if (r < 0) {
-            goto fail_vq;
-        }

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void *opaque, · line 937

Quoted 10 lines
hw/virtio/vhost.c +8 -1
-    }
     hdev->features = features;
+    hdev->migration_blocker = NULL;
+
+    if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
+        error_setg(&hdev->migration_blocker,
+                   "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
+        r = migrate_add_blocker(hdev->migration_blocker, NULL);
+        if (r < 0) {
+            goto fail_keep_r;

The usual one.

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void *opaque, · line 944

Quoted 7 lines
hw/virtio/vhost.c +7 -0
+        }
+    }
+
+    for (i = 0; i < hdev->nvqs; ++i) {
+        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
+        if (r < 0) {
+            goto fail_vq;

Here as well.

Quoted 82 lines
hw/virtio/vhost.c +4 -6
+        }
+    }
 
     hdev->memory_listener = (MemoryListener) {
         .begin = vhost_begin,
@@ -949,12 +960,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         .eventfd_del = vhost_eventfd_del,
         .priority = 10
     };
-    hdev->migration_blocker = NULL;
-    if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
-        error_setg(&hdev->migration_blocker,
-                   "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
-        migrate_add_blocker(hdev->migration_blocker);
-    }
+
     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
     hdev->n_mem_sections = 0;
     hdev->mem_sections = NULL;
@@ -971,6 +977,7 @@ fail_vq:
     }
 fail:
     r = -errno;
+fail_keep_r:
     hdev->vhost_ops->vhost_backend_cleanup(hdev);
     return r;
 }
include/migration/migration.h +3 -1
index 8334621..de7ebb3 100644
@@ -117,6 +117,7 @@ int migrate_fd_close(MigrationState *s);
 void add_migration_state_change_notifier(Notifier *notify);
 void remove_migration_state_change_notifier(Notifier *notify);
 bool migration_in_setup(MigrationState *);
+bool migration_has_started(MigrationState *s);
 bool migration_has_finished(MigrationState *);
 bool migration_has_failed(MigrationState *);
 MigrationState *migrate_get_current(void);
@@ -150,8 +151,9 @@ void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
  * @migrate_add_blocker - prevent migration from proceeding
  *
  * @reason - an error to be returned whenever migration is attempted
+ * @errp - [out] The reason (if any) we cannot block migration right now.
  */
-void migrate_add_blocker(Error *reason);
+int migrate_add_blocker(Error *reason, Error **errp);
 
 /**
  * @migrate_del_blocker - remove a blocking error from migration
migration/migration.c +20 -0
index e48dd13..a457cfc 100644
@@ -632,6 +632,26 @@ bool migration_has_failed(MigrationState *s)
             s->state == MIGRATION_STATUS_FAILED);
 }
 
+bool migration_has_started(MigrationState *s)
+{
+    if (!s) {
+        s = migrate_get_current();
+    }
+
+    switch (s->state) {
+    case MIGRATION_STATUS_NONE:
+    case MIGRATION_STATUS_CANCELLED:
+    case MIGRATION_STATUS_COMPLETED:
+    case MIGRATION_STATUS_FAILED:
+        return false;
+    case MIGRATION_STATUS_SETUP:
+    case MIGRATION_STATUS_CANCELLING:
+    case MIGRATION_STATUS_ACTIVE:
+    default:
+        return true;
+    }
+}
+
 static MigrationState *migrate_init(const MigrationParams *params)
 {
     MigrationState *s = migrate_get_current();

migration/migration.c static MigrationState *migrate_init(const MigrationParams *params) · line 694

Quoted 9 lines
migration/migration.c +4 -1
@@ -667,9 +687,15 @@ static MigrationState *migrate_init(const MigrationParams *params)
 
 static GSList *migration_blockers;
 
-void migrate_add_blocker(Error *reason)
+int migrate_add_blocker(Error *reason, Error **errp)
 {
+    if (migration_has_started(NULL)) {
+        error_setg(errp, "Cannot block a migration already in-progress");
+        return -EINPROGRESS;

I think -EBUSY is a better error code here. -EINPROGRESS seems to be used when something is still running in the background, so it doesn't really mean failure.

Quoted 38 lines
migration/migration.c +4 -3
+    }
+
     migration_blockers = g_slist_prepend(migration_blockers, reason);
+    return 0;
 }
 
 void migrate_del_blocker(Error *reason)
@@ -712,9 +738,7 @@ void qmp_migrate(const char *uri, bool has_blk, bool blk,
     params.blk = has_blk && blk;
     params.shared = has_inc && inc;
 
-    if (s->state == MIGRATION_STATUS_ACTIVE ||
-        s->state == MIGRATION_STATUS_SETUP ||
-        s->state == MIGRATION_STATUS_CANCELLING) {
+    if (migration_has_started(s)) {
         error_setg(errp, QERR_MIGRATION_ACTIVE);
         return;
     }
stubs/migr-blocker.c +2 -1
index 300df6e..06812bd 100644
@@ -1,8 +1,9 @@
 #include "qemu-common.h"
 #include "migration/migration.h"
 
-void migrate_add_blocker(Error *reason)
+int migrate_add_blocker(Error *reason, Error **errp)
 {
+    return 0;
 }
 
 void migrate_del_blocker(Error *reason)
target-i386/kvm.c +0 -0
index 7b0ba17..d535029 100644

target-i386/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 738

Quoted 9 lines
target-i386/kvm.c +5 -1
@@ -731,7 +731,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
         error_setg(&invtsc_mig_blocker,
                    "State blocked by non-migratable CPU device"
                    " (invtsc flag)");
-        migrate_add_blocker(invtsc_mig_blocker);
+        r = migrate_add_blocker(invtsc_mig_blocker, NULL);
+        if (r < 0) {
+            fprintf(stderr, "migrate_add_blocker failed\n");
+            return r;
+        }

In theory, this is a last leak (and there are some more error returns in the same function that should in theory be considered), but there is no matching migrate_del_blocker() anywhere and failing to initialise a CPU makes qemu exit anyway, so I guess ignoring it is okay.

target-i386/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 741

Quoted 3 lines
target-i386/kvm.c +0 -0
         /* for savevm */
         vmstate_x86_cpu.unmigratable = 1;
     }

Kevin

author
#2 lore raw
Quoted 4 lines · Kevin Wolf

Am 29.09.2015 um 22:20 hat John Snow geschrieben:

Quoted 3 lines

If a migration is already in progress and somebody attempts to add a migration blocker, this should rightly fail.

Quoted 9 lines
Quoted 6 lines

Add an errp parameter and a retcode return value to migrate_add_blocker.

This is part one of two for a solution to prohibit e.g. block jobs from running concurrently with migration.

Through which tree should this be merged?

I think the more sensitive changes actually wind up in the block layer, so perhaps it's best to do it through yours with ACK from the migrational powers that be.

Quoted 68 lines
Quoted 21 lines
 block/qcow.c                  |  5 ++++-
 block/vdi.c                   |  5 ++++-
 block/vhdx.c                  |  5 ++++-
 block/vmdk.c                  | 13 +++++++++----
 block/vpc.c                   |  9 ++++++---
 block/vvfat.c                 | 19 +++++++++++--------
 hw/9pfs/virtio-9p.c           | 15 +++++++++++----
 hw/misc/ivshmem.c             |  5 ++++-
 hw/scsi/vhost-scsi.c          | 11 +++++++----
 hw/virtio/vhost.c             | 31 +++++++++++++++++++------------
 include/migration/migration.h |  4 +++-
 migration/migration.c         | 32 ++++++++++++++++++++++++++++----
 stubs/migr-blocker.c          |  3 ++-
 target-i386/kvm.c             |  6 +++++-
 14 files changed, 117 insertions(+), 46 deletions(-)
block/qcow.c +0 -0
index 6e35db1..1b82dec 100644

block/qcow.c static int qcow_open(BlockDriverState *bs, QDict *options, int flags, · line 241

Quoted 7 lines
block/qcow.c +3 -1
@@ -236,7 +236,10 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;

This error path leaks s->migration_blocker.

Quoted 9 lines
block/qcow.c +1 -0
+    }
 
     qemu_co_mutex_init(&s->lock);
     return 0;
block/vdi.c +0 -0
index 062a654..95b2690 100644

block/vdi.c static int vdi_open(BlockDriverState *bs, QDict *options, int flags, · line 510

Quoted 7 lines
block/vdi.c +3 -1
@@ -505,7 +505,10 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail_free_bmap;

Same.

Quoted 18 lines
block/vdi.c +1 -0
+    }
 
     qemu_co_mutex_init(&s->write_lock);
 
block/vhdx.c +4 -1
index d3bb1bd..5bebe34 100644
@@ -1005,7 +1005,10 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
 
Quoted 7 lines

block/vhdx.c static int vhdx_open(BlockDriverState *bs, QDict *options, int flags, · line 1014

Quoted 2 lines
block/vhdx.c +0 -0
     return 0;
 fail:

This one happens to be okay because VHDX uses the close function in the failure path (and at last up to now that function even seems to cope with half-initialised images - it just feels a bit brittle).

I'll patch it anyway. No need to cause work for people who audit this in the future.

Quoted 5 lines
Quoted 5 lines
block/vmdk.c +0 -0
index be0d640..09dcf6b 100644
@@ -943,15 +943,20 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
Quoted 9 lines

block/vmdk.c static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, · line 947

Quoted 5 lines
block/vmdk.c +0 -2
     if (ret) {
         goto fail;
     }
-    s->cid = vmdk_read_cid(bs, 0);
-    s->parent_cid = vmdk_read_cid(bs, 1);

Why do you move this code? It doesn't seem to do anything that you would need to undo on failure.

It was just the pattern of how I was applying the change. ("Try to position the error check directly after the previous error pathway, leaving any items that are incapable of failing to the rear portion of the function.") I didn't trace vmdk_read_cid here, I only noted it couldn't fail, so I shifted it down.

If in this particular case it's benign, I'll shift it back for the smaller diff.

Quoted 12 lines

block/vmdk.c static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, · line 954

Quoted 11 lines
block/vmdk.c +4 -2
-    qemu_co_mutex_init(&s->lock);
 
     /* Disable migration when VMDK images are used */
     error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;
+    }
Quoted 2 lines

But the usual leak is still there. :-)

Yup :-(

Quoted 98 lines
Quoted 16 lines
block/vmdk.c +5 -0
+
+    s->cid = vmdk_read_cid(bs, 0);
+    s->parent_cid = vmdk_read_cid(bs, 1);
+    qemu_co_mutex_init(&s->lock);
+
     g_free(buf);
     return 0;
 
block/vpc.c +0 -0
index 2b3b518..4c60942 100644
@@ -325,13 +325,16 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 #endif
     }
 

block/vpc.c static int vpc_open(BlockDriverState *bs, QDict *options, int flags, · line 334

Quoted 10 lines
block/vpc.c +3 -3
-    qemu_co_mutex_init(&s->lock);
-
     /* Disable migration when VHD images are used */
     error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    migrate_add_blocker(s->migration_blocker);
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        goto fail;

And another one.

Quoted 21 lines
block/vpc.c +3 -0
+    }
+
+    qemu_co_mutex_init(&s->lock);
 
     return 0;
 
block/vvfat.c +0 -7
index 7ddc962..100f42a 100644
@@ -1191,22 +1191,25 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 
     s->sector_count = s->faked_sectors + s->sectors_per_cluster*s->cluster_count;
 
-    if (s->first_sectors_number == 0x40) {
-        init_mbr(s, cyls, heads, secs);
-    }
-
-    //    assert(is_consistent(s));
-    qemu_co_mutex_init(&s->lock);
-

block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1202

Quoted 10 lines
block/vvfat.c +3 -1
     /* Disable migration when vvfat is used rw */
     if (s->qcow) {
         error_setg(&s->migration_blocker,
                    "The vvfat (rw) format used by node '%s' "
                    "does not support live migration",
                    bdrv_get_device_or_node_name(bs));
-        migrate_add_blocker(s->migration_blocker);
+        ret = migrate_add_blocker(s->migration_blocker, errp);
+        if (ret < 0) {
+            goto fail;

And the final one in the block drivers.

Quoted 35 lines
block/vvfat.c +8 -0
+        }
     }
 
+    if (s->first_sectors_number == 0x40) {
+        init_mbr(s, cyls, heads, secs);
+    }
+
+    //    assert(is_consistent(s));
+    qemu_co_mutex_init(&s->lock);
+
     ret = 0;
 fail:
     qemu_opts_del(opts);
hw/9pfs/virtio-9p.c +2 -3
index f972731..4572e18 100644
@@ -978,20 +978,27 @@ static void v9fs_attach(void *opaque)
         clunk_fid(s, fid);
         goto out;
     }
-    err += offset;
-    trace_v9fs_attach_return(pdu->tag, pdu->id,
-                             qid.type, qid.version, qid.path);
+
     /*
      * disable migration if we haven't done already.
      * attach could get called multiple times for the same export.
      */
     if (!s->migration_blocker) {
+        int ret;
         s->root_fid = fid;
         error_setg(&s->migration_blocker,
                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
Quoted 9 lines

hw/9pfs/virtio-9p.c static void v9fs_attach(void *opaque) · line 995

Quoted 5 lines
hw/9pfs/virtio-9p.c +4 -1
-        migrate_add_blocker(s->migration_blocker);
+        ret = migrate_add_blocker(s->migration_blocker, NULL);
+        if (ret < 0) {
+            clunk_fid(s, fid);
+            goto out;

I think you need to set err instead of ret, so we run the error path in complete_pdu().

I'll look closer at that, thanks.

Quoted 26 lines

The migration blocker seems to be freed in put_fid(), good.

Quoted 14 lines
hw/9pfs/virtio-9p.c +5 -0
+        }
     }
+
+    err += offset;
+    trace_v9fs_attach_return(pdu->tag, pdu->id,
+                             qid.type, qid.version, qid.path);
 out:
     put_fid(pdu, fidp);
 out_nofid:
hw/misc/ivshmem.c +0 -0
index cc76989..859e844 100644

hw/misc/ivshmem.c static int pci_ivshmem_init(PCIDevice *dev) · line 745

Quoted 7 lines
hw/misc/ivshmem.c +3 -1
@@ -740,7 +740,10 @@ static int pci_ivshmem_init(PCIDevice *dev)
     if (s->role_val == IVSHMEM_PEER) {
         error_setg(&s->migration_blocker,
                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
-        migrate_add_blocker(s->migration_blocker);
+        if (migrate_add_blocker(s->migration_blocker, NULL) < 0) {
+            error_report("Unable to prohibit migration during ivshmem init");
+            exit(1);

Seriously? :-/

Quoted 5 lines

I see that the whole function does the same and has an int return value only so that it can always return 0. So what you're doing is probably right for this patch, but generally, the error handling in this init function is horrible.

I know. "When in Rome" is how I wrote this...

Quoted 14 lines

The good thing is that you can't leak anything this way...

Quoted 12 lines
hw/misc/ivshmem.c +1 -0
+        }
     }
 
     pci_conf = dev->config;
hw/scsi/vhost-scsi.c +0 -0
index fb7983d..5c6d7a2 100644
@@ -248,6 +248,13 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
     s->dev.vq_index = 0;
     s->dev.backend_features = 0;
 
Quoted 7 lines

hw/scsi/vhost-scsi.c static void vhost_scsi_realize(DeviceState *dev, Error **errp) · line 255

Quoted 5 lines
hw/scsi/vhost-scsi.c +5 -0
+    error_setg(&s->migration_blocker,
+            "vhost-scsi does not support migration");
+    ret = migrate_add_blocker(s->migration_blocker, errp);
+    if (ret < 0) {
+        return;

This leaks not only s->migration_blocker, but also vhostfd.

Looks like s->dev.vqs is leaked as well. "Not my problem" but I'll fix it up while I'm here.

Quoted 149 lines
Quoted 8 lines
hw/scsi/vhost-scsi.c +2 -0
+    }
+
     ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
                          VHOST_BACKEND_TYPE_KERNEL);
     if (ret < 0) {

error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
strerror(-ret));

Now that you moved the migration blocker code before this, this error path leaks s->migration_blocker as well (in this case meaning that both the blocker needs to be removed and the error object be freed).

Quoted 28 lines
         return;
     }

     /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
     s->channel = 0;
     s->lun = 0;
     /* Note: we can also get the minimum tpgt from kernel */
     s->target = vs->conf.boot_tpgt;
hw/scsi/vhost-scsi.c +0 -4
-
-    error_setg(&s->migration_blocker,
-            "vhost-scsi does not support migration");
-    migrate_add_blocker(s->migration_blocker);
 }
 
 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
hw/virtio/vhost.c +0 -5
index c0ed5b2..0f27a2d 100644
@@ -926,13 +926,24 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         goto fail;
     }
 
-    for (i = 0; i < hdev->nvqs; ++i) {
-        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
-        if (r < 0) {
-            goto fail_vq;
-        }

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void *opaque, · line 937

Quoted 10 lines
hw/virtio/vhost.c +8 -1
-    }
     hdev->features = features;
+    hdev->migration_blocker = NULL;
+
+    if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
+        error_setg(&hdev->migration_blocker,
+                   "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
+        r = migrate_add_blocker(hdev->migration_blocker, NULL);
+        if (r < 0) {
+            goto fail_keep_r;

The usual one.

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void *opaque, · line 944

Quoted 7 lines
hw/virtio/vhost.c +7 -0
+        }
+    }
+
+    for (i = 0; i < hdev->nvqs; ++i) {
+        r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
+        if (r < 0) {
+            goto fail_vq;

Here as well.

Quoted 85 lines
hw/virtio/vhost.c +4 -6
+        }
+    }
 
     hdev->memory_listener = (MemoryListener) {
         .begin = vhost_begin,
@@ -949,12 +960,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         .eventfd_del = vhost_eventfd_del,
         .priority = 10
     };
-    hdev->migration_blocker = NULL;
-    if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
-        error_setg(&hdev->migration_blocker,
-                   "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
-        migrate_add_blocker(hdev->migration_blocker);
-    }
+
     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
     hdev->n_mem_sections = 0;
     hdev->mem_sections = NULL;
@@ -971,6 +977,7 @@ fail_vq:
     }
 fail:
     r = -errno;
+fail_keep_r:
     hdev->vhost_ops->vhost_backend_cleanup(hdev);
     return r;
 }
include/migration/migration.h +3 -1
index 8334621..de7ebb3 100644
@@ -117,6 +117,7 @@ int migrate_fd_close(MigrationState *s);
 void add_migration_state_change_notifier(Notifier *notify);
 void remove_migration_state_change_notifier(Notifier *notify);
 bool migration_in_setup(MigrationState *);
+bool migration_has_started(MigrationState *s);
 bool migration_has_finished(MigrationState *);
 bool migration_has_failed(MigrationState *);
 MigrationState *migrate_get_current(void);
@@ -150,8 +151,9 @@ void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
  * @migrate_add_blocker - prevent migration from proceeding
  *
  * @reason - an error to be returned whenever migration is attempted
+ * @errp - [out] The reason (if any) we cannot block migration right now.
  */
-void migrate_add_blocker(Error *reason);
+int migrate_add_blocker(Error *reason, Error **errp);
 
 /**
  * @migrate_del_blocker - remove a blocking error from migration
migration/migration.c +20 -0
index e48dd13..a457cfc 100644
@@ -632,6 +632,26 @@ bool migration_has_failed(MigrationState *s)
             s->state == MIGRATION_STATUS_FAILED);
 }
 
+bool migration_has_started(MigrationState *s)
+{
+    if (!s) {
+        s = migrate_get_current();
+    }
+
+    switch (s->state) {
+    case MIGRATION_STATUS_NONE:
+    case MIGRATION_STATUS_CANCELLED:
+    case MIGRATION_STATUS_COMPLETED:
+    case MIGRATION_STATUS_FAILED:
+        return false;
+    case MIGRATION_STATUS_SETUP:
+    case MIGRATION_STATUS_CANCELLING:
+    case MIGRATION_STATUS_ACTIVE:
+    default:
+        return true;
+    }
+}
+
 static MigrationState *migrate_init(const MigrationParams *params)
 {
     MigrationState *s = migrate_get_current();
@@ -667,9 +687,15 @@ static MigrationState *migrate_init(const MigrationParams *params)
 
 static GSList *migration_blockers;
 
Quoted 11 lines

migration/migration.c static MigrationState *migrate_init(const MigrationParams *params) · line 694

Quoted 6 lines
migration/migration.c +4 -1
-void migrate_add_blocker(Error *reason)
+int migrate_add_blocker(Error *reason, Error **errp)
 {
+    if (migration_has_started(NULL)) {
+        error_setg(errp, "Cannot block a migration already in-progress");
+        return -EINPROGRESS;

I think -EBUSY is a better error code here. -EINPROGRESS seems to be used when something is still running in the background, so it doesn't really mean failure.

Good point.

Quoted 53 lines
Quoted 38 lines
migration/migration.c +4 -3
+    }
+
     migration_blockers = g_slist_prepend(migration_blockers, reason);
+    return 0;
 }
 
 void migrate_del_blocker(Error *reason)
@@ -712,9 +738,7 @@ void qmp_migrate(const char *uri, bool has_blk, bool blk,
     params.blk = has_blk && blk;
     params.shared = has_inc && inc;
 
-    if (s->state == MIGRATION_STATUS_ACTIVE ||
-        s->state == MIGRATION_STATUS_SETUP ||
-        s->state == MIGRATION_STATUS_CANCELLING) {
+    if (migration_has_started(s)) {
         error_setg(errp, QERR_MIGRATION_ACTIVE);
         return;
     }
stubs/migr-blocker.c +2 -1
index 300df6e..06812bd 100644
@@ -1,8 +1,9 @@
 #include "qemu-common.h"
 #include "migration/migration.h"
 
-void migrate_add_blocker(Error *reason)
+int migrate_add_blocker(Error *reason, Error **errp)
 {
+    return 0;
 }
 
 void migrate_del_blocker(Error *reason)
target-i386/kvm.c +0 -0
index 7b0ba17..d535029 100644

target-i386/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 738

Quoted 9 lines
target-i386/kvm.c +5 -1
@@ -731,7 +731,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
         error_setg(&invtsc_mig_blocker,
                    "State blocked by non-migratable CPU device"
                    " (invtsc flag)");
-        migrate_add_blocker(invtsc_mig_blocker);
+        r = migrate_add_blocker(invtsc_mig_blocker, NULL);
+        if (r < 0) {
+            fprintf(stderr, "migrate_add_blocker failed\n");
+            return r;
+        }

In theory, this is a last leak (and there are some more error returns in the same function that should in theory be considered), but there is no matching migrate_del_blocker() anywhere and failing to initialise a CPU makes qemu exit anyway, so I guess ignoring it is okay.

Quoted 6 lines

target-i386/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 741

Quoted 3 lines
target-i386/kvm.c +0 -0
         /* for savevm */
         vmstate_x86_cpu.unmigratable = 1;
     }

Kevin

Thanks for the review. Sorry I forgot about leaking the migration_blocker.

--js

#3 lore raw
In reply to Kevin Wolf
Quoted 10 lines · Kevin Wolf

Am 29.09.2015 um 22:20 hat John Snow geschrieben:

Quoted 9 lines

If a migration is already in progress and somebody attempts to add a migration blocker, this should rightly fail.

Add an errp parameter and a retcode return value to migrate_add_blocker.

This is part one of two for a solution to prohibit e.g. block jobs from running concurrently with migration.

[...]

Quoted 15 lines

hw/misc/ivshmem.c static int pci_ivshmem_init(PCIDevice *dev) · line 745

Quoted 12 lines
hw/misc/ivshmem.c +3 -1
index cc76989..859e844 100644
@@ -740,7 +740,10 @@ static int pci_ivshmem_init(PCIDevice *dev)
     if (s->role_val == IVSHMEM_PEER) {
         error_setg(&s->migration_blocker,
                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
-        migrate_add_blocker(s->migration_blocker);
+        if (migrate_add_blocker(s->migration_blocker, NULL) < 0) {
+            error_report("Unable to prohibit migration during ivshmem init");
+            exit(1);

Seriously? :-/

Quoted 4 lines

I see that the whole function does the same and has an int return value only so that it can always return 0. So what you're doing is probably right for this patch, but generally, the error handling in this init function is horrible.

Horrible indeed. Marc-André's series to clean it up clocks in at 47 patches right now.

Message-Id: <[email protected]>
http://lists.gnu.org/archive/html/qemu-devel/2015-09/msg06302.html

Quoted 6 lines

The good thing is that you can't leak anything this way...

Quoted 4 lines
hw/misc/ivshmem.c +1 -0
+        }
     }
 
     pci_conf = dev->config;

[...]