7 changes: 2 additions & 5 deletions hw/remote/proxy.c
Expand Up @@ -107,8 +107,7 @@ static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)

error_setg(&dev->migration_blocker, "%s does not support migration",
TYPE_PCI_PROXY_DEV);
if (migrate_add_blocker(dev->migration_blocker, errp) < 0) {
error_free(dev->migration_blocker);
if (migrate_add_blocker(&dev->migration_blocker, errp) < 0) {
object_unref(dev->ioc);
return;
}
Expand All @@ -134,9 +133,7 @@ static void pci_proxy_dev_exit(PCIDevice *pdev)
qio_channel_close(dev->ioc, NULL);
}

migrate_del_blocker(dev->migration_blocker);

error_free(dev->migration_blocker);
migrate_del_blocker(&dev->migration_blocker);

proxy_memory_listener_deconfigure(&dev->proxy_listener);

Expand Down
9 changes: 3 additions & 6 deletions hw/s390x/s390-virtio-ccw.c
Expand Up @@ -344,8 +344,7 @@ static void s390_machine_unprotect(S390CcwMachineState *ms)
s390_pv_vm_disable();
}
ms->pv = false;
migrate_del_blocker(pv_mig_blocker);
error_free_or_abort(&pv_mig_blocker);
migrate_del_blocker(&pv_mig_blocker);
ram_block_discard_disable(false);
}

Expand All @@ -368,20 +367,18 @@ static int s390_machine_protect(S390CcwMachineState *ms)

error_setg(&pv_mig_blocker,
"protected VMs are currently not migratable.");
rc = migrate_add_blocker(pv_mig_blocker, &local_err);
rc = migrate_add_blocker(&pv_mig_blocker, &local_err);
if (rc) {
ram_block_discard_disable(false);
error_report_err(local_err);
error_free_or_abort(&pv_mig_blocker);
return rc;
}

/* Create SE VM */
rc = s390_pv_vm_enable();
if (rc) {
ram_block_discard_disable(false);
migrate_del_blocker(pv_mig_blocker);
error_free_or_abort(&pv_mig_blocker);
migrate_del_blocker(&pv_mig_blocker);
return rc;
}

Expand Down
8 changes: 3 additions & 5 deletions hw/scsi/vhost-scsi.c
Expand Up @@ -210,7 +210,7 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
"When external environment supports it (Orchestrator migrates "
"target SCSI device state or use shared storage over network), "
"set 'migratable' property to true to enable migration.");
if (migrate_add_blocker(vsc->migration_blocker, errp) < 0) {
if (migrate_add_blocker(&vsc->migration_blocker, errp) < 0) {
goto free_virtio;
}
}
Expand Down Expand Up @@ -243,10 +243,9 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
free_vqs:
g_free(vqs);
if (!vsc->migratable) {
migrate_del_blocker(vsc->migration_blocker);
migrate_del_blocker(&vsc->migration_blocker);
}
free_virtio:
error_free(vsc->migration_blocker);
virtio_scsi_common_unrealize(dev);
close_fd:
if (vhostfd >= 0) {
Expand All @@ -262,8 +261,7 @@ static void vhost_scsi_unrealize(DeviceState *dev)
struct vhost_virtqueue *vqs = vsc->dev.vqs;

if (!vsc->migratable) {
migrate_del_blocker(vsc->migration_blocker);
error_free(vsc->migration_blocker);
migrate_del_blocker(&vsc->migration_blocker);
}

/* This will stop vhost backend. */
Expand Down
10 changes: 2 additions & 8 deletions hw/vfio/common.c
Expand Up @@ -129,11 +129,7 @@ int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
error_setg(&multiple_devices_migration_blocker,
"Multiple VFIO devices migration is supported only if all of "
"them support P2P migration");
ret = migrate_add_blocker(multiple_devices_migration_blocker, errp);
if (ret < 0) {
error_free(multiple_devices_migration_blocker);
multiple_devices_migration_blocker = NULL;
}
ret = migrate_add_blocker(&multiple_devices_migration_blocker, errp);

return ret;
}
Expand All @@ -145,9 +141,7 @@ void vfio_unblock_multiple_devices_migration(void)
return;
}

migrate_del_blocker(multiple_devices_migration_blocker);
error_free(multiple_devices_migration_blocker);
multiple_devices_migration_blocker = NULL;
migrate_del_blocker(&multiple_devices_migration_blocker);
}

bool vfio_viommu_preset(VFIODevice *vbasedev)
Expand Down
22 changes: 5 additions & 17 deletions hw/vfio/migration.c
Expand Up @@ -872,8 +872,8 @@ static int vfio_migration_init(VFIODevice *vbasedev)
NULL;
migration->vm_state = qdev_add_vm_change_state_handler_full(
vbasedev->dev, vfio_vmstate_change, prepare_cb, vbasedev);
migration->migration_state.notify = vfio_migration_state_notifier;
add_migration_state_change_notifier(&migration->migration_state);
migration_add_notifier(&migration->migration_state,
vfio_migration_state_notifier);

return 0;
}
Expand All @@ -882,7 +882,7 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)
{
VFIOMigration *migration = vbasedev->migration;

remove_migration_state_change_notifier(&migration->migration_state);
migration_remove_notifier(&migration->migration_state);
qemu_del_vm_change_state_handler(migration->vm_state);
unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
vfio_migration_free(vbasedev);
Expand All @@ -891,8 +891,6 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)

static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
{
int ret;

if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
error_propagate(errp, err);
return -EINVAL;
Expand All @@ -901,13 +899,7 @@ static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
vbasedev->migration_blocker = error_copy(err);
error_free(err);

ret = migrate_add_blocker(vbasedev->migration_blocker, errp);
if (ret < 0) {
error_free(vbasedev->migration_blocker);
vbasedev->migration_blocker = NULL;
}

return ret;
return migrate_add_blocker(&vbasedev->migration_blocker, errp);
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -994,9 +986,5 @@ void vfio_migration_exit(VFIODevice *vbasedev)
vfio_migration_deinit(vbasedev);
}

if (vbasedev->migration_blocker) {
migrate_del_blocker(vbasedev->migration_blocker);
error_free(vbasedev->migration_blocker);
vbasedev->migration_blocker = NULL;
}
migrate_del_blocker(&vbasedev->migration_blocker);
}
8 changes: 2 additions & 6 deletions hw/virtio/vhost.c
Expand Up @@ -1527,9 +1527,8 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
}

if (hdev->migration_blocker != NULL) {
r = migrate_add_blocker(hdev->migration_blocker, errp);
r = migrate_add_blocker(&hdev->migration_blocker, errp);
if (r < 0) {
error_free(hdev->migration_blocker);
goto fail_busyloop;
}
}
Expand Down Expand Up @@ -1597,10 +1596,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
memory_listener_unregister(&hdev->memory_listener);
QLIST_REMOVE(hdev, entry);
}
if (hdev->migration_blocker) {
migrate_del_blocker(hdev->migration_blocker);
error_free(hdev->migration_blocker);
}
migrate_del_blocker(&hdev->migration_blocker);
g_free(hdev->mem);
g_free(hdev->mem_sections);
if (hdev->vhost_ops) {
Expand Down
24 changes: 17 additions & 7 deletions include/migration/blocker.h
Expand Up @@ -17,19 +17,23 @@
/**
* @migrate_add_blocker - prevent migration from proceeding
*
* @reason - an error to be returned whenever migration is attempted
* @reasonp - address of an error to be returned whenever migration is attempted
*
* @errp - [out] The reason (if any) we cannot block migration right now.
*
* @returns - 0 on success, -EBUSY/-EACCES on failure, with errp set.
*
* *@reasonp is freed and set to NULL if failure is returned.
* On success, the caller must not free @reasonp, except by
* calling migrate_del_blocker.
*/
int migrate_add_blocker(Error *reason, Error **errp);
int migrate_add_blocker(Error **reasonp, Error **errp);

/**
* @migrate_add_blocker_internal - prevent migration from proceeding without
* only-migrate implications
*
* @reason - an error to be returned whenever migration is attempted
* @reasonp - address of an error to be returned whenever migration is attempted
*
* @errp - [out] The reason (if any) we cannot block migration right now.
*
Expand All @@ -38,14 +42,20 @@ int migrate_add_blocker(Error *reason, Error **errp);
* Some of the migration blockers can be temporary (e.g., for a few seconds),
* so it shouldn't need to conflict with "-only-migratable". For those cases,
* we can call this function rather than @migrate_add_blocker().
*
* *@reasonp is freed and set to NULL if failure is returned.
* On success, the caller must not free @reasonp, except by
* calling migrate_del_blocker.
*/
int migrate_add_blocker_internal(Error *reason, Error **errp);
int migrate_add_blocker_internal(Error **reasonp, Error **errp);

/**
* @migrate_del_blocker - remove a blocking error from migration
* @migrate_del_blocker - remove a blocking error from migration and free it.
*
* @reasonp - address of the error blocking migration
*
* @reason - the error blocking migration
* This function frees *@reasonp and sets it to NULL.
*/
void migrate_del_blocker(Error *reason);
void migrate_del_blocker(Error **reasonp);

#endif
6 changes: 4 additions & 2 deletions include/migration/misc.h
Expand Up @@ -60,8 +60,10 @@ void migration_object_init(void);
void migration_shutdown(void);
bool migration_is_idle(void);
bool migration_is_active(MigrationState *);
void add_migration_state_change_notifier(Notifier *notify);
void remove_migration_state_change_notifier(Notifier *notify);
void migration_add_notifier(Notifier *notify,
void (*func)(Notifier *notifier, void *data));
void migration_remove_notifier(Notifier *notify);
void migration_call_notifiers(MigrationState *s);
bool migration_in_setup(MigrationState *);
bool migration_has_finished(MigrationState *);
bool migration_has_failed(MigrationState *);
Expand Down
44 changes: 30 additions & 14 deletions migration/migration.c
Expand Up @@ -1207,7 +1207,7 @@ static void migrate_fd_cleanup(MigrationState *s)
/* It is used on info migrate. We can't free it */
error_report_err(error_copy(s->error));
}
notifier_list_notify(&migration_state_notifiers, s);
migration_call_notifiers(s);
block_cleanup_parameters();
yank_unregister_instance(MIGRATION_YANK_INSTANCE);
}
Expand Down Expand Up @@ -1311,14 +1311,24 @@ static void migrate_fd_cancel(MigrationState *s)
}
}

void add_migration_state_change_notifier(Notifier *notify)
void migration_add_notifier(Notifier *notify,
void (*func)(Notifier *notifier, void *data))
{
notify->notify = func;
notifier_list_add(&migration_state_notifiers, notify);
}

void remove_migration_state_change_notifier(Notifier *notify)
void migration_remove_notifier(Notifier *notify)
{
notifier_remove(notify);
if (notify->notify) {
notifier_remove(notify);
notify->notify = NULL;
}
}

void migration_call_notifiers(MigrationState *s)
{
notifier_list_notify(&migration_state_notifiers, s);
}

bool migration_in_setup(MigrationState *s)
Expand Down Expand Up @@ -1465,35 +1475,41 @@ int migrate_init(MigrationState *s, Error **errp)
return 0;
}

int migrate_add_blocker_internal(Error *reason, Error **errp)
int migrate_add_blocker_internal(Error **reasonp, Error **errp)
{
/* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */
if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) {
error_propagate_prepend(errp, error_copy(reason),
error_propagate_prepend(errp, *reasonp,
"disallowing migration blocker "
"(migration/snapshot in progress) for: ");
*reasonp = NULL;
return -EBUSY;
}

migration_blockers = g_slist_prepend(migration_blockers, reason);
migration_blockers = g_slist_prepend(migration_blockers, *reasonp);
return 0;
}

int migrate_add_blocker(Error *reason, Error **errp)
int migrate_add_blocker(Error **reasonp, Error **errp)
{
if (only_migratable) {
error_propagate_prepend(errp, error_copy(reason),
error_propagate_prepend(errp, *reasonp,
"disallowing migration blocker "
"(--only-migratable) for: ");
*reasonp = NULL;
return -EACCES;
}

return migrate_add_blocker_internal(reason, errp);
return migrate_add_blocker_internal(reasonp, errp);
}

void migrate_del_blocker(Error *reason)
void migrate_del_blocker(Error **reasonp)
{
migration_blockers = g_slist_remove(migration_blockers, reason);
if (*reasonp) {
migration_blockers = g_slist_remove(migration_blockers, *reasonp);
error_free(*reasonp);
*reasonp = NULL;
}
}

void qmp_migrate_incoming(const char *uri, Error **errp)
Expand Down Expand Up @@ -2227,7 +2243,7 @@ static int postcopy_start(MigrationState *ms, Error **errp)
* spice needs to trigger a transition now
*/
ms->postcopy_after_devices = true;
notifier_list_notify(&migration_state_notifiers, ms);
migration_call_notifiers(ms);

ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;

Expand Down Expand Up @@ -3307,7 +3323,7 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
rate_limit = migrate_max_bandwidth();

/* Notify before starting migration thread */
notifier_list_notify(&migration_state_notifiers, s);
migration_call_notifiers(s);
}

migration_rate_set(rate_limit);
Expand Down
3 changes: 0 additions & 3 deletions migration/multifd.c
Expand Up @@ -743,9 +743,6 @@ static void *multifd_send_thread(void *opaque)
if (flags & MULTIFD_FLAG_SYNC) {
qemu_sem_post(&p->sem_sync);
}
} else if (p->quit) {
qemu_mutex_unlock(&p->mutex);
break;
} else {
qemu_mutex_unlock(&p->mutex);
/* sometimes there are spurious wakeups */
Expand Down
5 changes: 3 additions & 2 deletions migration/ram.c
Expand Up @@ -3873,6 +3873,7 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
ret = qemu_ram_resize(block, length, &local_err);
if (local_err) {
error_report_err(local_err);
return ret;
}
}
/* For postcopy we need to check hugepage sizes match */
Expand All @@ -3883,7 +3884,7 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
error_report("Mismatched RAM page size %s "
"(local) %zd != %" PRId64, block->idstr,
block->page_size, remote_page_size);
ret = -EINVAL;
return -EINVAL;
}
}
if (migrate_ignore_shared()) {
Expand All @@ -3893,7 +3894,7 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
error_report("Mismatched GPAs for block %s "
"%" PRId64 "!= %" PRId64, block->idstr,
(uint64_t)addr, (uint64_t)block->mr->addr);
ret = -EINVAL;
return -EINVAL;
}
}
ret = rdma_block_notification_handle(f, block->idstr);
Expand Down
7 changes: 4 additions & 3 deletions net/vhost-vdpa.c
Expand Up @@ -339,7 +339,8 @@ static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)
{
struct vhost_vdpa *v = &s->vhost_vdpa;

add_migration_state_change_notifier(&s->migration_state);
migration_add_notifier(&s->migration_state,
vdpa_net_migration_state_notifier);
if (v->shadow_vqs_enabled) {
v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
v->iova_range.last);
Expand Down Expand Up @@ -399,7 +400,7 @@ static void vhost_vdpa_net_client_stop(NetClientState *nc)
assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);

if (s->vhost_vdpa.index == 0) {
remove_migration_state_change_notifier(&s->migration_state);
migration_remove_notifier(&s->migration_state);
}

dev = s->vhost_vdpa.dev;
Expand Down Expand Up @@ -1566,7 +1567,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
s->vhost_vdpa.device_fd = vdpa_device_fd;
s->vhost_vdpa.index = queue_pair_index;
s->always_svq = svq;
s->migration_state.notify = vdpa_net_migration_state_notifier;
s->migration_state.notify = NULL;
s->vhost_vdpa.shadow_vqs_enabled = svq;
s->vhost_vdpa.iova_range = iova_range;
s->vhost_vdpa.shadow_data = svq;
Expand Down
4 changes: 2 additions & 2 deletions stubs/migr-blocker.c
@@ -1,11 +1,11 @@
#include "qemu/osdep.h"
#include "migration/blocker.h"

int migrate_add_blocker(Error *reason, Error **errp)
int migrate_add_blocker(Error **reasonp, Error **errp)
{
return 0;
}

void migrate_del_blocker(Error *reason)
void migrate_del_blocker(Error **reasonp)
{
}
8 changes: 4 additions & 4 deletions target/i386/kvm/kvm.c
Expand Up @@ -1603,7 +1603,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
error_setg(&hv_passthrough_mig_blocker,
"'hv-passthrough' CPU flag prevents migration, use explicit"
" set of hv-* flags instead");
ret = migrate_add_blocker(hv_passthrough_mig_blocker, &local_err);
ret = migrate_add_blocker(&hv_passthrough_mig_blocker, &local_err);
if (ret < 0) {
error_report_err(local_err);
return ret;
Expand All @@ -1617,7 +1617,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
" use explicit 'hv-no-nonarch-coresharing=on' instead (but"
" make sure SMT is disabled and/or that vCPUs are properly"
" pinned)");
ret = migrate_add_blocker(hv_no_nonarch_cs_mig_blocker, &local_err);
ret = migrate_add_blocker(&hv_no_nonarch_cs_mig_blocker, &local_err);
if (ret < 0) {
error_report_err(local_err);
return ret;
Expand Down Expand Up @@ -2213,7 +2213,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
error_setg(&invtsc_mig_blocker,
"State blocked by non-migratable CPU device"
" (invtsc flag)");
r = migrate_add_blocker(invtsc_mig_blocker, &local_err);
r = migrate_add_blocker(&invtsc_mig_blocker, &local_err);
if (r < 0) {
error_report_err(local_err);
return r;
Expand Down Expand Up @@ -2271,7 +2271,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
return 0;

fail:
migrate_del_blocker(invtsc_mig_blocker);
migrate_del_blocker(&invtsc_mig_blocker);

return r;
}
Expand Down
3 changes: 1 addition & 2 deletions target/i386/nvmm/nvmm-all.c
Expand Up @@ -929,9 +929,8 @@ nvmm_init_vcpu(CPUState *cpu)
error_setg(&nvmm_migration_blocker,
"NVMM: Migration not supported");

if (migrate_add_blocker(nvmm_migration_blocker, &local_error) < 0) {
if (migrate_add_blocker(&nvmm_migration_blocker, &local_error) < 0) {
error_report_err(local_error);
error_free(nvmm_migration_blocker);
return -EINVAL;
}
}
Expand Down
2 changes: 1 addition & 1 deletion target/i386/sev.c
Expand Up @@ -891,7 +891,7 @@ sev_launch_finish(SevGuestState *sev)
/* add migration blocker */
error_setg(&sev_mig_blocker,
"SEV: Migration is not implemented");
migrate_add_blocker(sev_mig_blocker, &error_fatal);
migrate_add_blocker(&sev_mig_blocker, &error_fatal);
}

static void
Expand Down
3 changes: 1 addition & 2 deletions target/i386/whpx/whpx-all.c
Expand Up @@ -2160,9 +2160,8 @@ int whpx_init_vcpu(CPUState *cpu)
"State blocked due to non-migratable CPUID feature support,"
"dirty memory tracking support, and XSAVE/XRSTOR support");

if (migrate_add_blocker(whpx_migration_blocker, &local_error) < 0) {
if (migrate_add_blocker(&whpx_migration_blocker, &local_error) < 0) {
error_report_err(local_error);
error_free(whpx_migration_blocker);
ret = -EINVAL;
goto error;
}
Expand Down
98 changes: 83 additions & 15 deletions tests/qtest/libqtest.c
Expand Up @@ -91,6 +91,7 @@ struct QTestState

static GHookList abrt_hooks;
static void (*sighandler_old)(int);
static bool silence_spawn_log;

static int qtest_query_target_endianness(QTestState *s);

Expand Down Expand Up @@ -336,10 +337,17 @@ void qtest_remove_abrt_handler(void *data)
}
}

static const char *qtest_qemu_binary(void)
static const char *qtest_qemu_binary(const char *var)
{
const char *qemu_bin;

if (var) {
qemu_bin = getenv(var);
if (qemu_bin) {
return qemu_bin;
}
}

qemu_bin = getenv("QTEST_QEMU_BINARY");
if (!qemu_bin) {
fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
Expand Down Expand Up @@ -381,7 +389,8 @@ static pid_t qtest_create_process(char *cmd)
}
#endif /* _WIN32 */

static QTestState *G_GNUC_PRINTF(1, 2) qtest_spawn_qemu(const char *fmt, ...)
static QTestState *G_GNUC_PRINTF(2, 3) qtest_spawn_qemu(const char *qemu_bin,
const char *fmt, ...)
{
va_list ap;
QTestState *s = g_new0(QTestState, 1);
Expand All @@ -391,14 +400,15 @@ static QTestState *G_GNUC_PRINTF(1, 2) qtest_spawn_qemu(const char *fmt, ...)
g_autoptr(GString) command = g_string_new("");

va_start(ap, fmt);
g_string_append_printf(command, CMD_EXEC "%s %s",
qtest_qemu_binary(), tracearg);
g_string_append_printf(command, CMD_EXEC "%s %s", qemu_bin, tracearg);
g_string_append_vprintf(command, fmt, ap);
va_end(ap);

qtest_add_abrt_handler(kill_qemu_hook_func, s);

g_test_message("starting QEMU: %s", command->str);
if (!silence_spawn_log) {
g_test_message("starting QEMU: %s", command->str);
}

#ifndef _WIN32
s->qemu_pid = fork();
Expand Down Expand Up @@ -431,7 +441,8 @@ static QTestState *G_GNUC_PRINTF(1, 2) qtest_spawn_qemu(const char *fmt, ...)
return s;
}

QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
static QTestState *qtest_init_internal(const char *qemu_bin,
const char *extra_args)
{
QTestState *s;
int sock, qmpsock, i;
Expand All @@ -456,7 +467,8 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
sock = init_socket(socket_path);
qmpsock = init_socket(qmp_socket_path);

s = qtest_spawn_qemu("-qtest unix:%s "
s = qtest_spawn_qemu(qemu_bin,
"-qtest unix:%s "
"-qtest-log %s "
"-chardev socket,path=%s,id=char0 "
"-mon chardev=char0,mode=control "
Expand Down Expand Up @@ -509,9 +521,14 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
return s;
}

QTestState *qtest_init(const char *extra_args)
QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
{
QTestState *s = qtest_init_without_qmp_handshake(extra_args);
return qtest_init_internal(qtest_qemu_binary(NULL), extra_args);
}

QTestState *qtest_init_with_env(const char *var, const char *extra_args)
{
QTestState *s = qtest_init_internal(qtest_qemu_binary(var), extra_args);
QDict *greeting;

/* Read the QMP greeting and then do the handshake */
Expand All @@ -522,6 +539,11 @@ QTestState *qtest_init(const char *extra_args)
return s;
}

QTestState *qtest_init(const char *extra_args)
{
return qtest_init_with_env(NULL, extra_args);
}

QTestState *qtest_vinitf(const char *fmt, va_list ap)
{
char *args = g_strdup_vprintf(fmt, ap);
Expand Down Expand Up @@ -905,7 +927,7 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...)

const char *qtest_get_arch(void)
{
const char *qemu = qtest_qemu_binary();
const char *qemu = qtest_qemu_binary(NULL);
const char *end = strrchr(qemu, '-');

if (!end) {
Expand Down Expand Up @@ -1449,13 +1471,26 @@ struct MachInfo {
char *alias;
};

static void qtest_free_machine_list(struct MachInfo *machines)
{
if (machines) {
for (int i = 0; machines[i].name != NULL; i++) {
g_free(machines[i].name);
g_free(machines[i].alias);
}

g_free(machines);
}
}

/*
* Returns an array with pointers to the available machine names.
* The terminating entry has the name set to NULL.
*/
static struct MachInfo *qtest_get_machines(void)
static struct MachInfo *qtest_get_machines(const char *var)
{
static struct MachInfo *machines;
static char *qemu_var;
QDict *response, *minfo;
QList *list;
const QListEntry *p;
Expand All @@ -1464,11 +1499,21 @@ static struct MachInfo *qtest_get_machines(void)
QTestState *qts;
int idx;

if (g_strcmp0(qemu_var, var)) {
qemu_var = g_strdup(var);

/* new qemu, clear the cache */
qtest_free_machine_list(machines);
machines = NULL;
}

if (machines) {
return machines;
}

qts = qtest_init("-machine none");
silence_spawn_log = !g_test_verbose();

qts = qtest_init_with_env(qemu_var, "-machine none");
response = qtest_qmp(qts, "{ 'execute': 'query-machines' }");
g_assert(response);
list = qdict_get_qlist(response, "return");
Expand Down Expand Up @@ -1499,6 +1544,8 @@ static struct MachInfo *qtest_get_machines(void)
qtest_quit(qts);
qobject_unref(response);

silence_spawn_log = false;

memset(&machines[idx], 0, sizeof(struct MachInfo)); /* Terminating entry */
return machines;
}
Expand All @@ -1509,7 +1556,7 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
struct MachInfo *machines;
int i;

machines = qtest_get_machines();
machines = qtest_get_machines(NULL);

for (i = 0; machines[i].name != NULL; i++) {
/* Ignore machines that cannot be used for qtests */
Expand All @@ -1525,12 +1572,28 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
}
}

bool qtest_has_machine(const char *machine)
char *qtest_resolve_machine_alias(const char *var, const char *alias)
{
struct MachInfo *machines;
int i;

machines = qtest_get_machines();
machines = qtest_get_machines(var);

for (i = 0; machines[i].name != NULL; i++) {
if (machines[i].alias && g_str_equal(alias, machines[i].alias)) {
return g_strdup(machines[i].name);
}
}

return NULL;
}

bool qtest_has_machine_with_env(const char *var, const char *machine)
{
struct MachInfo *machines;
int i;

machines = qtest_get_machines(var);

for (i = 0; machines[i].name != NULL; i++) {
if (g_str_equal(machine, machines[i].name) ||
Expand All @@ -1542,6 +1605,11 @@ bool qtest_has_machine(const char *machine)
return false;
}

bool qtest_has_machine(const char *machine)
{
return qtest_has_machine_with_env(NULL, machine);
}

bool qtest_has_device(const char *device)
{
static QList *list;
Expand Down
32 changes: 32 additions & 0 deletions tests/qtest/libqtest.h
Expand Up @@ -55,6 +55,19 @@ QTestState *qtest_vinitf(const char *fmt, va_list ap) G_GNUC_PRINTF(1, 0);
*/
QTestState *qtest_init(const char *extra_args);

/**
* qtest_init_with_env:
* @var: Environment variable from where to take the QEMU binary
* @extra_args: Other arguments to pass to QEMU. CAUTION: these
* arguments are subject to word splitting and shell evaluation.
*
* Like qtest_init(), but use a different environment variable for the
* QEMU binary.
*
* Returns: #QTestState instance.
*/
QTestState *qtest_init_with_env(const char *var, const char *extra_args);

/**
* qtest_init_without_qmp_handshake:
* @extra_args: other arguments to pass to QEMU. CAUTION: these
Expand Down Expand Up @@ -909,6 +922,16 @@ void qtest_qmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds,
void qtest_cb_for_every_machine(void (*cb)(const char *machine),
bool skip_old_versioned);

/**
* qtest_resolve_machine_alias:
* @var: Environment variable from where to take the QEMU binary
* @alias: The alias to resolve
*
* Returns: the machine type corresponding to the alias if any,
* otherwise NULL.
*/
char *qtest_resolve_machine_alias(const char *var, const char *alias);

/**
* qtest_has_machine:
* @machine: The machine to look for
Expand All @@ -917,6 +940,15 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
*/
bool qtest_has_machine(const char *machine);

/**
* qtest_has_machine_with_env:
* @var: Environment variable from where to take the QEMU binary
* @machine: The machine to look for
*
* Returns: true if the machine is available in the specified binary.
*/
bool qtest_has_machine_with_env(const char *var, const char *machine);

/**
* qtest_has_device:
* @device: The device to look for
Expand Down
52 changes: 52 additions & 0 deletions tests/qtest/migration-helpers.c
Expand Up @@ -11,6 +11,7 @@
*/

#include "qemu/osdep.h"
#include "qemu/ctype.h"
#include "qapi/qmp/qjson.h"

#include "migration-helpers.h"
Expand Down Expand Up @@ -240,3 +241,54 @@ void wait_for_migration_fail(QTestState *from, bool allow_active)
g_assert(qdict_get_bool(rsp_return, "running"));
qobject_unref(rsp_return);
}

char *find_common_machine_version(const char *mtype, const char *var1,
const char *var2)
{
g_autofree char *type1 = qtest_resolve_machine_alias(var1, mtype);
g_autofree char *type2 = qtest_resolve_machine_alias(var2, mtype);

g_assert(type1 && type2);

if (g_str_equal(type1, type2)) {
/* either can be used */
return g_strdup(type1);
}

if (qtest_has_machine_with_env(var2, type1)) {
return g_strdup(type1);
}

if (qtest_has_machine_with_env(var1, type2)) {
return g_strdup(type2);
}

g_test_message("No common machine version for machine type '%s' between "
"binaries %s and %s", mtype, getenv(var1), getenv(var2));
g_assert_not_reached();
}

char *resolve_machine_version(const char *alias, const char *var1,
const char *var2)
{
const char *mname = g_getenv("QTEST_QEMU_MACHINE_TYPE");
g_autofree char *machine_name = NULL;

if (mname) {
const char *dash = strrchr(mname, '-');
const char *dot = strrchr(mname, '.');

machine_name = g_strdup(mname);

if (dash && dot) {
assert(qtest_has_machine(machine_name));
return g_steal_pointer(&machine_name);
}
/* else: probably an alias, let it be resolved below */
} else {
/* use the hardcoded alias */
machine_name = g_strdup(alias);
}

return find_common_machine_version(machine_name, var1, var2);
}
4 changes: 4 additions & 0 deletions tests/qtest/migration-helpers.h
Expand Up @@ -43,4 +43,8 @@ void wait_for_migration_complete(QTestState *who);

void wait_for_migration_fail(QTestState *from, bool allow_active);

char *find_common_machine_version(const char *mtype, const char *var1,
const char *var2);
char *resolve_machine_version(const char *alias, const char *var1,
const char *var2);
#endif /* MIGRATION_HELPERS_H */
50 changes: 44 additions & 6 deletions tests/qtest/migration-test.c
Expand Up @@ -71,6 +71,8 @@ static bool got_dst_resume;
#define QEMU_VM_FILE_MAGIC 0x5145564d
#define FILE_TEST_FILENAME "migfile"
#define FILE_TEST_OFFSET 0x1000
#define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
#define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"

#if defined(__linux__)
#include <sys/syscall.h>
Expand Down Expand Up @@ -743,6 +745,8 @@ static int test_migrate_start(QTestState **from, QTestState **to,
const char *kvm_opts = NULL;
const char *arch = qtest_get_arch();
const char *memory_size;
const char *machine_alias, *machine_opts = "";
g_autofree char *machine = NULL;

if (args->use_shmem) {
if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
Expand All @@ -755,11 +759,20 @@ static int test_migrate_start(QTestState **from, QTestState **to,
got_dst_resume = false;
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
memory_size = "150M";
arch_opts = g_strdup_printf("-drive file=%s,format=raw", bootpath);

if (g_str_equal(arch, "i386")) {
machine_alias = "pc";
} else {
machine_alias = "q35";
}
arch_opts = g_strdup_printf(
"-drive if=none,id=d0,file=%s,format=raw "
"-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
start_address = X86_TEST_MEM_START;
end_address = X86_TEST_MEM_END;
} else if (g_str_equal(arch, "s390x")) {
memory_size = "128M";
machine_alias = "s390-ccw-virtio";
arch_opts = g_strdup_printf("-bios %s", bootpath);
start_address = S390_TEST_MEM_START;
end_address = S390_TEST_MEM_END;
Expand All @@ -771,11 +784,14 @@ static int test_migrate_start(QTestState **from, QTestState **to,
"'nvramrc=hex .\" _\" begin %x %x "
"do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
"until'", end_address, start_address);
arch_opts = g_strdup("-nodefaults -machine vsmt=8");
machine_alias = "pseries";
machine_opts = "vsmt=8";
arch_opts = g_strdup("-nodefaults");
} else if (strcmp(arch, "aarch64") == 0) {
memory_size = "150M";
arch_opts = g_strdup_printf("-machine virt,gic-version=max -cpu max "
"-kernel %s", bootpath);
machine_alias = "virt";
machine_opts = "gic-version=max";
arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
start_address = ARM_TEST_MEM_START;
end_address = ARM_TEST_MEM_END;
} else {
Expand Down Expand Up @@ -809,39 +825,48 @@ static int test_migrate_start(QTestState **from, QTestState **to,
kvm_opts = ",dirty-ring-size=4096";
}

machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
QEMU_ENV_DST);

g_test_message("Using machine type: %s", machine);

cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
"-machine %s,%s "
"-name source,debug-threads=on "
"-m %s "
"-serial file:%s/src_serial "
"%s %s %s %s %s",
kvm_opts ? kvm_opts : "",
machine, machine_opts,
memory_size, tmpfs,
arch_opts ? arch_opts : "",
arch_source ? arch_source : "",
shmem_opts ? shmem_opts : "",
args->opts_source ? args->opts_source : "",
ignore_stderr);
if (!args->only_target) {
*from = qtest_init(cmd_source);
*from = qtest_init_with_env(QEMU_ENV_SRC, cmd_source);
qtest_qmp_set_event_callback(*from,
migrate_watch_for_stop,
&got_src_stop);
}

cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
"-machine %s,%s "
"-name target,debug-threads=on "
"-m %s "
"-serial file:%s/dest_serial "
"-incoming %s "
"%s %s %s %s %s",
kvm_opts ? kvm_opts : "",
machine, machine_opts,
memory_size, tmpfs, uri,
arch_opts ? arch_opts : "",
arch_target ? arch_target : "",
shmem_opts ? shmem_opts : "",
args->opts_target ? args->opts_target : "",
ignore_stderr);
*to = qtest_init(cmd_target);
*to = qtest_init_with_env(QEMU_ENV_DST, cmd_target);
qtest_qmp_set_event_callback(*to,
migrate_watch_for_resume,
&got_dst_resume);
Expand Down Expand Up @@ -2972,10 +2997,23 @@ int main(int argc, char **argv)
bool has_uffd;
const char *arch;
g_autoptr(GError) err = NULL;
const char *qemu_src = getenv(QEMU_ENV_SRC);
const char *qemu_dst = getenv(QEMU_ENV_DST);
int ret;

g_test_init(&argc, &argv, NULL);

/*
* The default QTEST_QEMU_BINARY must always be provided because
* that is what helpers use to query the accel type and
* architecture.
*/
if (qemu_src && qemu_dst) {
g_test_message("Only one of %s, %s is allowed",
QEMU_ENV_SRC, QEMU_ENV_DST);
exit(1);
}

has_kvm = qtest_has_accel("kvm");
has_tcg = qtest_has_accel("tcg");

Expand Down
3 changes: 1 addition & 2 deletions ui/spice-core.c
Expand Up @@ -821,8 +821,7 @@ static void qemu_spice_init(void)
};
using_spice = 1;

migration_state.notify = migration_state_notifier;
add_migration_state_change_notifier(&migration_state);
migration_add_notifier(&migration_state, migration_state_notifier);
spice_migrate.base.sif = &migrate_interface.base;
qemu_spice.add_interface(&spice_migrate.base);

Expand Down
5 changes: 2 additions & 3 deletions ui/vdagent.c
Expand Up @@ -671,7 +671,7 @@ static void vdagent_chr_open(Chardev *chr,
return;
#endif

if (migrate_add_blocker(vd->migration_blocker, errp) != 0) {
if (migrate_add_blocker(&vd->migration_blocker, errp) != 0) {
return;
}

Expand Down Expand Up @@ -924,13 +924,12 @@ static void vdagent_chr_fini(Object *obj)
{
VDAgentChardev *vd = QEMU_VDAGENT_CHARDEV(obj);

migrate_del_blocker(vd->migration_blocker);
migrate_del_blocker(&vd->migration_blocker);
vdagent_disconnect(vd);
if (vd->mouse_hs) {
qemu_input_handler_unregister(vd->mouse_hs);
}
buffer_free(&vd->outbuf);
error_free(vd->migration_blocker);
}

static const TypeInfo vdagent_chr_type_info = {
Expand Down