Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
vfio/migration: Refactor PRE_COPY and RUNNING state checks
Move the PRE_COPY and RUNNING state checks to helper functions.

This is in preparation for adding P2P VFIO migration support, where
these helpers will also test for PRE_COPY_P2P and RUNNING_P2P states.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
Signed-off-by: Avihai Horon <avihaih@nvidia.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Tested-by: YangHang Liu <yanghliu@redhat.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
  • Loading branch information
jpemartins authored and legoater committed Sep 11, 2023
1 parent 02b2e25 commit 3d4d0f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
22 changes: 18 additions & 4 deletions hw/vfio/common.c
Expand Up @@ -437,6 +437,20 @@ static void vfio_set_migration_error(int err)
}
}

bool vfio_device_state_is_running(VFIODevice *vbasedev)
{
VFIOMigration *migration = vbasedev->migration;

return migration->device_state == VFIO_DEVICE_STATE_RUNNING;
}

bool vfio_device_state_is_precopy(VFIODevice *vbasedev)
{
VFIOMigration *migration = vbasedev->migration;

return migration->device_state == VFIO_DEVICE_STATE_PRE_COPY;
}

static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
{
VFIOGroup *group;
Expand All @@ -457,8 +471,8 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)
}

if (vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF &&
(migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
migration->device_state == VFIO_DEVICE_STATE_PRE_COPY)) {
(vfio_device_state_is_running(vbasedev) ||
vfio_device_state_is_precopy(vbasedev))) {
return false;
}
}
Expand Down Expand Up @@ -503,8 +517,8 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)
return false;
}

if (migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
migration->device_state == VFIO_DEVICE_STATE_PRE_COPY) {
if (vfio_device_state_is_running(vbasedev) ||
vfio_device_state_is_precopy(vbasedev)) {
continue;
} else {
return false;
Expand Down
10 changes: 4 additions & 6 deletions hw/vfio/migration.c
Expand Up @@ -411,7 +411,7 @@ static void vfio_state_pending_estimate(void *opaque, uint64_t *must_precopy,
VFIODevice *vbasedev = opaque;
VFIOMigration *migration = vbasedev->migration;

if (migration->device_state != VFIO_DEVICE_STATE_PRE_COPY) {
if (!vfio_device_state_is_precopy(vbasedev)) {
return;
}

Expand Down Expand Up @@ -444,7 +444,7 @@ static void vfio_state_pending_exact(void *opaque, uint64_t *must_precopy,
vfio_query_stop_copy_size(vbasedev, &stop_copy_size);
*must_precopy += stop_copy_size;

if (migration->device_state == VFIO_DEVICE_STATE_PRE_COPY) {
if (vfio_device_state_is_precopy(vbasedev)) {
vfio_query_precopy_size(migration);

*must_precopy +=
Expand All @@ -459,9 +459,8 @@ static void vfio_state_pending_exact(void *opaque, uint64_t *must_precopy,
static bool vfio_is_active_iterate(void *opaque)
{
VFIODevice *vbasedev = opaque;
VFIOMigration *migration = vbasedev->migration;

return migration->device_state == VFIO_DEVICE_STATE_PRE_COPY;
return vfio_device_state_is_precopy(vbasedev);
}

static int vfio_save_iterate(QEMUFile *f, void *opaque)
Expand Down Expand Up @@ -656,15 +655,14 @@ static const SaveVMHandlers savevm_vfio_handlers = {
static void vfio_vmstate_change(void *opaque, bool running, RunState state)
{
VFIODevice *vbasedev = opaque;
VFIOMigration *migration = vbasedev->migration;
enum vfio_device_mig_state new_state;
int ret;

if (running) {
new_state = VFIO_DEVICE_STATE_RUNNING;
} else {
new_state =
(migration->device_state == VFIO_DEVICE_STATE_PRE_COPY &&
(vfio_device_state_is_precopy(vbasedev) &&
(state == RUN_STATE_FINISH_MIGRATE || state == RUN_STATE_PAUSED)) ?
VFIO_DEVICE_STATE_STOP_COPY :
VFIO_DEVICE_STATE_STOP;
Expand Down
2 changes: 2 additions & 0 deletions include/hw/vfio/vfio-common.h
Expand Up @@ -230,6 +230,8 @@ void vfio_unblock_multiple_devices_migration(void);
bool vfio_viommu_preset(VFIODevice *vbasedev);
int64_t vfio_mig_bytes_transferred(void);
void vfio_reset_bytes_transferred(void);
bool vfio_device_state_is_running(VFIODevice *vbasedev);
bool vfio_device_state_is_precopy(VFIODevice *vbasedev);

#ifdef CONFIG_LINUX
int vfio_get_region_info(VFIODevice *vbasedev, int index,
Expand Down

0 comments on commit 3d4d0f0

Please sign in to comment.