VFIO device migration parallel state transitions
cover VFIO device migration parallel state transitions
From: "Maciej S. Szmigiero" <[email protected]>
When multiple VFIO devices are present in a VM the fact that their state transitions on migration happen sequentially has a visible impact on migration downtime.
This is because both PRE_COPY -> PRE_COPY_P2P -> STOP_COPY transitions on the source and RESUMING -> RUNNING_P2P -> RUNNING transitions on the target happen during the switchover phase.
During this phase the VM is stopped so the downtime is ticking.
These device state transitions are performed by VM state change handlers registered by the VFIO device migration code.
Instead of performing such state transition synchronously launch a thread performing the state change in parallel with other VFIO devices and other VM state change handlers at the particular VFIO device qdev tree depth. Only wait for this thread to finish *after* all other handlers at this tree depth finish doing their jobs.
To implement the above allow adjustment of priority for VM state change handlers - specifically allow registering qdev VM state change handlers below and above the normal priority level for the registering device qdev tree depth, but still properly ordered with respect to handlers registered at other tree depths.
This way these state transitions can happen in parallel not only with respect to other VFIO device instances but also ordinary (serialized) handlers for other devices at this qdev tree depth.
Downtime results:
4 VFs 2 VFs 1 VF
Disabled: 1385 ms 758 ms 497 ms Enabled: 986 ms 653 ms 493 ms IMPROVEMENT: ~29 % ~14 % ~0 %
Test VM shape:
vCPU 12 cores x 2 threads, 15 GiB RAM.
VFIO devices in the source and target machine:
Mellanox ConnectX-7 with 100GbE link and ~100 MiB of device state per VF.
Maciej S. Szmigiero (2):
system/runstate: Allow adjustment of priority for VM state change
handlers
vfio/migration: Parallelize device state transitions
hw/core/vm-change-state-handler.c | 22 ++-- hw/vfio/migration.c | 174 ++++++++++++++++++++++++++++-- hw/vfio/pci.c | 2 + hw/vfio/vfio-migration-internal.h | 4 +- include/hw/vfio/vfio-device.h | 1 + include/system/runstate.h | 2 +- 6 files changed, 189 insertions(+), 16 deletions(-)
PATCH 1/2 system/runstate: Allow adjustment of priority for VM state change handlers
From: "Maciej S. Szmigiero" <[email protected]>
A future patch will need an ability to register qdev VM state change handlers below and above the normal priority level for the registering device qdev tree depth, but still properly ordered with respect to handlers registered at other tree depths.
To implement this split the priority argument passed to qemu_add_vm_change_state_handler_prio_full() into two parts:
its 15 most significant bits will now carry the actual qdev tree depth while the 16 least significant bits will now carry the caller provided priority adjustment value.
Although this will limit the qdev tree to a depth of 32k such high limit shouldn't be a problem in practice.
--- hw/core/vm-change-state-handler.c | 22 +++++++++++++++------- hw/vfio/migration.c | 2 +- include/system/runstate.h | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-)
hw/core/vm-change-state-handler.c +15 -7
@@ -19,9 +19,9 @@#include "hw/core/qdev.h" #include "system/runstate.h" -static int qdev_get_dev_tree_depth (DeviceState *dev) +static unsigned int qdev_get_dev_tree_depth (DeviceState *dev) { -int depth; +unsigned int depth;for (depth =0 ; dev; depth++) {BusState *bus = dev->parent_bus;
@@ -61,20 +61,28 @@ VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, void *opaque) {assert (!cb || !cb_ret); -return qdev_add_vm_change_state_handler_full (dev, cb,NULL , cb_ret, opaque); +return qdev_add_vm_change_state_handler_full (dev, cb,NULL , cb_ret, opaque,0 ); }/* * Exactly like qdev_add_vm_change_state_handler() but passes a prepare_cb -* and the cb_ret arguments too. +* and the cb_ret arguments too and allows for adjustment of priority. */ VMChangeStateEntry *qdev_add_vm_change_state_handler_full (DeviceState *dev,VMChangeStateHandler *cb,VMChangeStateHandler *prepare_cb, -VMChangeStateHandlerWithRet *cb_ret,void *opaque) +VMChangeStateHandlerWithRet *cb_ret,void *opaque,int adj) { -int depth =qdev_get_dev_tree_depth (dev); +unsigned int depth =qdev_get_dev_tree_depth (dev); +int prio; + +/* 32k depth should be enough for everyone */ +assert (depth <=INT16_MAX ); + +/* encode depth on 15 MSB and adj on 16 LSB */ +assert (adj >=INT16_MIN && adj <=INT16_MAX ); + prio = (depth <<16 ) + (adj -INT16_MIN );assert (!cb || !cb_ret);return qemu_add_vm_change_state_handler_prio_full (cb, prepare_cb, cb_ret, - opaque, depth); + opaque, prio); }
hw/vfio/migration.c +1 -1
@@ -1227,7 +1227,7 @@ static int vfio_migration_init(VFIODevice *vbasedev, Error **errp) vfio_vmstate_change_prepare :NULL ; migration->vm_state =qdev_add_vm_change_state_handler_full ( - vbasedev->dev, vfio_vmstate_change, prepare_cb,NULL , vbasedev); + vbasedev->dev, vfio_vmstate_change, prepare_cb,NULL , vbasedev,0 );migration_add_notifier (&migration->migration_state, vfio_migration_state_notifier);
include/system/runstate.h +1 -1
@@ -69,7 +69,7 @@ VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, void *opaque);VMChangeStateEntry *qdev_add_vm_change_state_handler_full (DeviceState *dev,VMChangeStateHandler *cb,VMChangeStateHandler *prepare_cb, -VMChangeStateHandlerWithRet *cb_ret,void *opaque); +VMChangeStateHandlerWithRet *cb_ret,void *opaque,int adj);void qemu_del_vm_change_state_handler (VMChangeStateEntry *e);/** * vm_state_notify: Notify the state of the VM
Re: [PATCH 1/2] system/runstate: Allow adjustment of priority for VM state change handlers
Quoted 13 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
A future patch will need an ability to register qdev VM state change handlers below and above the normal priority level for the registering device qdev tree depth, but still properly ordered with respect to handlers registered at other tree depths.
To implement this split the priority argument passed to
qemu_add_vm_change_state_handler_prio_full()into two parts:
its 15 most significant bits will now carry the actual qdev tree depth while the 16 least significant bits will now carry the caller provided priority adjustment value.
Quoted 4 lines
Although this will limit the qdev tree to a depth of 32k such high limit shouldn't be a problem in practice.
Signed-off-by Maciej S. Szmigiero <[email protected]>
PATCH 2/2 vfio/migration: Parallelize device state transitions
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
When multiple VFIO devices are present in a VM the fact that their state transitions on migration happen sequentially has a visible impact on migration downtime.
This is because both PRE_COPY -> PRE_COPY_P2P -> STOP_COPY transitions on the source and RESUMING -> RUNNING_P2P -> RUNNING transitions on the target happen during the switchover phase.
During this phase the VM is stopped so the downtime is ticking.
These device state transitions are performed by VM state change handlers registered by the VFIO device migration code.
Instead of performing such state transition synchronously use the priority adjustment mechanism from the previous patch to launch a thread performing the state change at the priority level *before* all other VM state change handlers at the particular VFIO device qdev tree depth.
Only wait for this thread to finish at the priority level ordered *after* all other handlers at this tree depth.
This way these state transitions can happen in parallel not only with respect to other VFIO device instances but also ordinary (serialized) handlers for other devices at this qdev tree depth while still being properly ordered with respect to handlers registered at other tree depths.
Unfortunately, the order in which VM state handlers are called depends on whether the VM is starting or stopping.
Because of this, one extra layer of indirection is necessary to make the (first, second) ordering of these handlers constant.
Enable the feature by default since it has no impact on the migration bit stream protocol - it shouldn't need disabling for anything else but debugging scenarios.
--- hw/vfio/migration.c | 174 ++++++++++++++++++++++++++++-- hw/vfio/pci.c | 2 + hw/vfio/vfio-migration-internal.h | 4 +- include/hw/vfio/vfio-device.h | 1 + 4 files changed, 173 insertions(+), 8 deletions(-)
hw/vfio/migration.c +167 -7
@@ -1047,6 +1047,135 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state) mig_state_to_str(new_state)); } +typedef struct VMStateChangeThreadData { +VFIODevice *vbasedev; +bool is_prepare; +bool running; +RunState state; +}VMStateChangeThreadData ; + +static void *vfio_vmstate_change_thread (void *opaque) +{ + g_autofree VMStateChangeThreadData *data = opaque; + +if (data->is_prepare) { +vfio_vmstate_change_prepare (data->vbasedev, data->running, data->state); + }else { +vfio_vmstate_change (data->vbasedev, data->running, data->state); + } + +return NULL ; +} + +static void vfio_vmstate_change_thread_launch (VFIODevice *vbasedev, +bool is_prepare, +bool running, +RunState state) +{ +VFIOMigration *migration = vbasedev->migration; +VMStateChangeThreadData *data =g_new (VMStateChangeThreadData,1 ); + + data->vbasedev = vbasedev; + data->is_prepare = is_prepare; + data->running = running; + data->state = state; + +assert (!migration->vm_state_thread_running); + migration->vm_state_thread_running = true; + +qemu_thread_create (&migration->vm_state_thread, + is_prepare ?"vfio_vmstate_change_prepare" : +"vfio_vmstate_change" , + vfio_vmstate_change_thread, data, +QEMU_THREAD_JOINABLE ); +} + +static void vfio_vmstate_change_thread_join (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; + +assert (migration->vm_state_thread_running); + +qemu_thread_join (&migration->vm_state_thread); + + migration->vm_state_thread_running = false; +} + +/* +* The first handler called during a vmstate change at a particular depth - +* launch the VFIO device state change thread. +*/ +static void vfio_vmstate_change_first (VFIODevice *vbasedev, +bool is_prepare, +bool running,RunState state) +{ +vfio_vmstate_change_thread_launch (vbasedev, + is_prepare, + running, + state); +} + +/* +* The last handler called during a vmstate change at a particular depth - +* wait for the VFIO device state change thread to finish. +*/ +static void vfio_vmstate_change_second (VFIODevice *vbasedev) +{ +vfio_vmstate_change_thread_join (vbasedev); +} + +/* +* Lower priority number handler: +* Called before higher number handler when VM is starting +* but after higher number handler when VM is stopping. +*/ +static void vfio_vmstate_change_prepare_lower_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_first (opaque, true, running, state); + }else { +vfio_vmstate_change_second (opaque); + } +} + +/* +* Higher priority number handler: +* Called after lower number handler when VM is starting +* but before lower number handler when VM is stopping. +*/ +static void vfio_vmstate_change_prepare_higher_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_second (opaque); + }else { +vfio_vmstate_change_first (opaque, true, running, state); + } +} + +/* Same ordering issues as for vfio_vmstate_change_prepare_lower_prio() */ +static void vfio_vmstate_change_lower_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_first (opaque, false, running, state); + }else { +vfio_vmstate_change_second (opaque); + } +} + +/* Same ordering issues as for vfio_vmstate_change_prepare_higher_prio() */ +static void vfio_vmstate_change_higher_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_second (opaque); + }else { +vfio_vmstate_change_first (opaque, false, running, state); + } +} +static int vfio_migration_state_notifier (NotifierWithReturn *notifier,MigrationEvent *e,Error **errp) {
@@ -1063,6 +1192,8 @@ static int vfio_migration_state_notifier(NotifierWithReturn *notifier,* MigrationNotifyFunc may not return an error code and an Error * object for MIG_EVENT_FAILED. Hence, report the error * locally and ignore the errp argument. +* This state change is not parallelized as it is not expected to be +* performance critical. */ ret = vfio_migration_set_state_or_reset(vbasedev, VFIO_DEVICE_STATE_RUNNING,
@@ -1143,7 +1274,7 @@ static int vfio_migration_init(VFIODevice *vbasedev, Error **errp)char id[256 ] ="" ; g_autofree char *path = NULL, *oid = NULL;uint64_t mig_flags =0 ; -VMChangeStateHandler *prepare_cb; +VMChangeStateHandler *prepare_cb, *prepare_cb_lower, *prepare_cb_higher;if (!vbasedev->ops->vfio_get_object) { ret = -EINVAL ;
@@ -1223,11 +1354,34 @@ static int vfio_migration_init(VFIODevice *vbasedev, Error **errp)register_savevm_live (id,VMSTATE_INSTANCE_ID_ANY ,1 , &savevm_vfio_handlers, vbasedev); - prepare_cb = migration->mig_flags &VFIO_MIGRATION_P2P ? - vfio_vmstate_change_prepare : -NULL ; - migration->vm_state =qdev_add_vm_change_state_handler_full ( - vbasedev->dev, vfio_vmstate_change, prepare_cb,NULL , vbasedev,0 ); +if (vbasedev->migration_parallel_states) { +/* +* Unfortunately, the order in which vmstate handlers are called depends +* on whether the VM is starting or stopping. +* Because of this, one extra layer of indirection is necessary +* to make the (first, second) ordering of these handlers constant. +*/ + prepare_cb_lower = migration->mig_flags &VFIO_MIGRATION_P2P ? + vfio_vmstate_change_prepare_lower_prio :NULL ; + prepare_cb_higher = migration->mig_flags &VFIO_MIGRATION_P2P ? + vfio_vmstate_change_prepare_higher_prio :NULL ; + migration->vm_state_lower_prio =qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_lower_prio, prepare_cb_lower, +NULL , vbasedev,-1 ); + migration->vm_state_higher_prio =qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_higher_prio, prepare_cb_higher, +NULL , vbasedev,1 ); + }else { + prepare_cb = migration->mig_flags &VFIO_MIGRATION_P2P ? + vfio_vmstate_change_prepare :NULL ; +/* Arbitrarily use lower_prio field to store non-parallel handler */ + migration->vm_state_lower_prio = +qdev_add_vm_change_state_handler_full (vbasedev->dev, + vfio_vmstate_change, + prepare_cb,NULL , + vbasedev,0 ); + } +migration_add_notifier (&migration->migration_state, vfio_migration_state_notifier);
@@ -1302,7 +1456,13 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)VFIOMigration *migration = vbasedev->migration;migration_remove_notifier (&migration->migration_state); -qemu_del_vm_change_state_handler (migration->vm_state); + +if (vbasedev->migration_parallel_states) { +qemu_del_vm_change_state_handler (migration->vm_state_higher_prio); + } +/* Non-parallel state change uses lower_prio field to store its handler */ +qemu_del_vm_change_state_handler (migration->vm_state_lower_prio); +unregister_savevm (VMSTATE_IF (vbasedev->dev),"vfio" , vbasedev);vfio_migration_free (vbasedev);vfio_unblock_multiple_devices_migration ();
hw/vfio/pci.c +2 -0
@@ -3777,6 +3777,8 @@ static const Property vfio_pci_properties[] = { ON_OFF_AUTO_AUTO),DEFINE_PROP_SIZE ("x-migration-max-queued-buffers-size" , VFIOPCIDevice, vbasedev.migration_max_queued_buffers_size,UINT64_MAX ), +DEFINE_PROP_BOOL ("x-migration-parallel-states" , VFIOPCIDevice, + vbasedev.migration_parallel_states, true),DEFINE_PROP_BOOL ("migration-events" , VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),
hw/vfio/vfio-migration-internal.h +3 -1
@@ -38,7 +38,9 @@ typedef struct VFIOMultifd VFIOMultifd; typedef struct VFIOMigration { struct VFIODevice *vbasedev; - VMChangeStateEntry *vm_state; + VMChangeStateEntry *vm_state_lower_prio, *vm_state_higher_prio; + QemuThread vm_state_thread; + bool vm_state_thread_running; NotifierWithReturn migration_state; uint32_t device_state; int data_fd;
include/hw/vfio/vfio-device.h +1 -0
@@ -69,6 +69,7 @@ typedef struct VFIODevice {OnOffAuto migration_multifd_transfer;OnOffAuto migration_load_config_after_iter;uint64_t migration_max_queued_buffers_size; +bool migration_parallel_states;bool migration_events;bool use_region_fds;VFIODeviceOps *ops;
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 289 lines · Maciej S. Szmigiero
From: "Maciej S. Szmigiero" <[email protected]>
When multiple VFIO devices are present in a VM the fact that their state transitions on migration happen sequentially has a visible impact on migration downtime.
This is because both
PRE_COPY->PRE_COPY_P2P->STOP_COPYtransitions on the source and RESUMING ->RUNNING_P2P-> RUNNING transitions on the target happen during the switchover phase.
During this phase the VM is stopped so the downtime is ticking.These device state transitions are performed by VM state change handlers registered by the VFIO device migration code.
Instead of performing such state transition synchronously use the priority adjustment mechanism from the previous patch to launch a thread performing the state change at the priority level *before* all other VM state change handlers at the particular VFIO device qdev tree depth.
Only wait for this thread to finish at the priority level ordered *after* all other handlers at this tree depth.This way these state transitions can happen in parallel not only with respect to other VFIO device instances but also ordinary (serialized) handlers for other devices at this qdev tree depth while still being properly ordered with respect to handlers registered at other tree depths.
Unfortunately, the order in which VM state handlers are called depends on whether the VM is starting or stopping.
Because of this, one extra layer of indirection is necessary to make the (first, second) ordering of these handlers constant.Enable the feature by default since it has no impact on the migration bit stream protocol - it shouldn't need disabling for anything else but debugging scenarios.
Signed-off-by Maciej S. Szmigiero <[email protected]>--- hw/vfio/migration.c | 174 ++++++++++++++++++++++++++++-- hw/vfio/pci.c | 2 + hw/vfio/vfio-migration-internal.h | 4 +- include/hw/vfio/vfio-device.h | 1 + 4 files changed, 173 insertions(+), 8 deletions(-)hw/vfio/migration.c +167
-7@@ -1047,6 +1047,135 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state) mig_state_to_str(new_state)); } +typedef struct VMStateChangeThreadData { +VFIODevice *vbasedev; +bool is_prepare; +bool running; +RunState state; +}VMStateChangeThreadData ; + +static void *vfio_vmstate_change_thread (void *opaque) +{ + g_autofree VMStateChangeThreadData *data = opaque; + +if (data->is_prepare) { +vfio_vmstate_change_prepare (data->vbasedev, data->running, data->state); + }else { +vfio_vmstate_change (data->vbasedev, data->running, data->state); + } + +return NULL ; +} + +static void vfio_vmstate_change_thread_launch (VFIODevice *vbasedev, +bool is_prepare, +bool running, +RunState state) +{ +VFIOMigration *migration = vbasedev->migration; +VMStateChangeThreadData *data =g_new (VMStateChangeThreadData,1 ); + + data->vbasedev = vbasedev; + data->is_prepare = is_prepare; + data->running = running; + data->state = state; + +assert (!migration->vm_state_thread_running); + migration->vm_state_thread_running = true; + +qemu_thread_create (&migration->vm_state_thread, + is_prepare ?"vfio_vmstate_change_prepare" : +"vfio_vmstate_change" , + vfio_vmstate_change_thread, data, +QEMU_THREAD_JOINABLE ); +} + +static void vfio_vmstate_change_thread_join (VFIODevice *vbasedev) +{ +VFIOMigration *migration = vbasedev->migration; + +assert (migration->vm_state_thread_running); + +qemu_thread_join (&migration->vm_state_thread); + + migration->vm_state_thread_running = false; +} + +/* +* The first handler called during a vmstate change at a particular depth - +* launch the VFIO device state change thread. +*/ +static void vfio_vmstate_change_first (VFIODevice *vbasedev, +bool is_prepare, +bool running,RunState state) +{ +vfio_vmstate_change_thread_launch (vbasedev, + is_prepare, + running, + state); +} + +/* +* The last handler called during a vmstate change at a particular depth - +* wait for the VFIO device state change thread to finish. +*/ +static void vfio_vmstate_change_second (VFIODevice *vbasedev) +{ +vfio_vmstate_change_thread_join (vbasedev); +} + +/* +* Lower priority number handler: +* Called before higher number handler when VM is starting +* but after higher number handler when VM is stopping. +*/ +static void vfio_vmstate_change_prepare_lower_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_first (opaque, true, running, state); + }else { +vfio_vmstate_change_second (opaque); + } +} + +/* +* Higher priority number handler: +* Called after lower number handler when VM is starting +* but before lower number handler when VM is stopping. +*/ +static void vfio_vmstate_change_prepare_higher_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_second (opaque); + }else { +vfio_vmstate_change_first (opaque, true, running, state); + } +} + +/* Same ordering issues as for vfio_vmstate_change_prepare_lower_prio() */ +static void vfio_vmstate_change_lower_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_first (opaque, false, running, state); + }else { +vfio_vmstate_change_second (opaque); + } +} + +/* Same ordering issues as for vfio_vmstate_change_prepare_higher_prio() */ +static void vfio_vmstate_change_higher_prio (void *opaque,bool running, +RunState state) +{ +if (running) { +vfio_vmstate_change_second (opaque); + }else { +vfio_vmstate_change_first (opaque, false, running, state); + } +} +static int vfio_migration_state_notifier (NotifierWithReturn *notifier,MigrationEvent *e,Error **errp) {@@ -1063,6 +1192,8 @@ static int vfio_migration_state_notifier(NotifierWithReturn *notifier,* MigrationNotifyFunc may not return an error code and an Error * object for MIG_EVENT_FAILED. Hence, report the error * locally and ignore the errp argument. +* This state change is not parallelized as it is not expected to be +* performance critical. */ ret = vfio_migration_set_state_or_reset(vbasedev, VFIO_DEVICE_STATE_RUNNING,@@ -1143,7 +1274,7 @@ static int vfio_migration_init(VFIODevice *vbasedev, Error **errp)char id[256 ] ="" ; g_autofree char *path = NULL, *oid = NULL;uint64_t mig_flags =0 ; -VMChangeStateHandler *prepare_cb; +VMChangeStateHandler *prepare_cb, *prepare_cb_lower, *prepare_cb_higher;if (!vbasedev->ops->vfio_get_object) { ret = -EINVAL ;@@ -1223,11 +1354,34 @@ static int vfio_migration_init(VFIODevice *vbasedev, Error **errp)register_savevm_live (id,VMSTATE_INSTANCE_ID_ANY ,1 , &savevm_vfio_handlers, vbasedev); - prepare_cb = migration->mig_flags &VFIO_MIGRATION_P2P ? - vfio_vmstate_change_prepare : -NULL ; - migration->vm_state =qdev_add_vm_change_state_handler_full ( - vbasedev->dev, vfio_vmstate_change, prepare_cb,NULL , vbasedev,0 ); +if (vbasedev->migration_parallel_states) { +/* +* Unfortunately, the order in which vmstate handlers are called depends +* on whether the VM is starting or stopping. +* Because of this, one extra layer of indirection is necessary +* to make the (first, second) ordering of these handlers constant. +*/ + prepare_cb_lower = migration->mig_flags &VFIO_MIGRATION_P2P ? + vfio_vmstate_change_prepare_lower_prio :NULL ; + prepare_cb_higher = migration->mig_flags &VFIO_MIGRATION_P2P ? + vfio_vmstate_change_prepare_higher_prio :NULL ; + migration->vm_state_lower_prio =qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_lower_prio, prepare_cb_lower, +NULL , vbasedev,-1 ); + migration->vm_state_higher_prio =qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_higher_prio, prepare_cb_higher, +NULL , vbasedev,1 ); + }else { + prepare_cb = migration->mig_flags &VFIO_MIGRATION_P2P ? + vfio_vmstate_change_prepare :NULL ; +/* Arbitrarily use lower_prio field to store non-parallel handler */ + migration->vm_state_lower_prio = +qdev_add_vm_change_state_handler_full (vbasedev->dev, + vfio_vmstate_change, + prepare_cb,NULL , + vbasedev,0 ); + } +migration_add_notifier (&migration->migration_state, vfio_migration_state_notifier);@@ -1302,7 +1456,13 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)VFIOMigration *migration = vbasedev->migration;migration_remove_notifier (&migration->migration_state); -qemu_del_vm_change_state_handler (migration->vm_state); + +if (vbasedev->migration_parallel_states) { +qemu_del_vm_change_state_handler (migration->vm_state_higher_prio); + } +/* Non-parallel state change uses lower_prio field to store its handler */ +qemu_del_vm_change_state_handler (migration->vm_state_lower_prio); +unregister_savevm (VMSTATE_IF (vbasedev->dev),"vfio" , vbasedev);vfio_migration_free (vbasedev);vfio_unblock_multiple_devices_migration ();hw/vfio/pci.c +2
-0@@ -3777,6 +3777,8 @@ static const Property vfio_pci_properties[] = { ON_OFF_AUTO_AUTO),DEFINE_PROP_SIZE ("x-migration-max-queued-buffers-size" , VFIOPCIDevice, vbasedev.migration_max_queued_buffers_size,UINT64_MAX ), +DEFINE_PROP_BOOL ("x-migration-parallel-states" , VFIOPCIDevice, + vbasedev.migration_parallel_states, true),DEFINE_PROP_BOOL ("migration-events" , VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),hw/vfio/vfio-migration-internal.h +3
-1@@ -38,7 +38,9 @@ typedef struct VFIOMultifd VFIOMultifd; typedef struct VFIOMigration { struct VFIODevice *vbasedev; - VMChangeStateEntry *vm_state; + VMChangeStateEntry *vm_state_lower_prio, *vm_state_higher_prio; + QemuThread vm_state_thread; + bool vm_state_thread_running; NotifierWithReturn migration_state; uint32_t device_state; int data_fd;include/hw/vfio/vfio-device.h +0
-0
include/hw/vfio/vfio-device.h typedef struct VFIODevice { · line 75
Quoted 7 lines
include/hw/vfio/vfio-device.h +1
-0@@ -69,6 +69,7 @@ typedef struct VFIODevice {OnOffAuto migration_multifd_transfer;OnOffAuto migration_load_config_after_iter;uint64_t migration_max_queued_buffers_size; +bool migration_parallel_states;bool migration_events;bool use_region_fds;VFIODeviceOps *ops;
A bit idiosyncratic, but I don't have any better suggestions.
Also, pre-existing but maybe you'll want to fix it in this patch, 'vmstate_change' is the wrong term. This is vm_change_state or vm_state_change.
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 1 line · Fabiano Rosas
A bit idiosyncratic, but I don't have any better suggestions.
One thing I don't think proper with the current approach: if it's a real "priority" it should be invoked always with high->low order, no matter if running=true or not.. but that's not what will happen if we encode it with the "depth" field.
That is an implication that we have two demands, and they aren't the same, but this solution wrongly packed them together.
In 2023, VFIO already introduced VMChangeStateEntry.prepare_cb(). That is a real impl of prority queues with only two: prepare_cb() always has higher priority than the cb() itself.
Can VFIO simply leverage this existing interface, instead of hijacking "depth" in an unwanted way?
I believe the _P2P states still need to happen first for all vfio devices, then it can do the next step. Logically I think it can also be done internally within VFIO by proper impl of these two callbacks, to achieve both:
- Proper transition to P2P states first for all devices, meanwhile,
- Proper concurrency of all VFIO devices on device state transitions
Would that work in a cleaner way?
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 23 lines · Peter Xu
Quoted 1 line · Fabiano Rosas
A bit idiosyncratic, but I don't have any better suggestions.
One thing I don't think proper with the current approach: if it's a real "priority" it should be invoked always with
high->loworder, no matter if running=true or not.. but that's not what will happen if we encode it with the "depth" field.That is an implication that we have two demands, and they aren't the same, but this solution wrongly packed them together.
In 2023, VFIO already introduced
VMChangeStateEntry.prepare_cb(). That is a real impl of prority queues with only two:prepare_cb()always has higher priority than thecb()itself.Can VFIO simply leverage this existing interface, instead of hijacking "depth" in an unwanted way?
I believe the
_P2Pstates still need to happen first for all vfio devices, then it can do the next step. Logically I think it can also be done internally within VFIO by proper impl of these two callbacks, to achieve both:
Quoted 4 lines
- Proper transition to P2P states first for all devices, meanwhile,
- Proper concurrency of all VFIO devices on device state transitionsWould that work in a cleaner way?
In the VFIO migration save/source code we have two transitions:PRE_COPY -> PRE_COPY_P2P done via prepare_cb() and PRE_COPY_P2P -> STOP_COPY done via cb().
On the load/target side the situation is similar, only states are different.
For each of these transitions we need two callbacks: one has to launch per-VFIO device state change thread and the other one has to "collect" (or join) these threads.
All handlers of the first callback have to be complete before calling any handler of the second callback since otherwise there would be VFIO device vs VFIO device serialization.
Moreover, all the prepare_cb()-related transitions have to be done before any of the cb()-related is started.
I presume what you suggest above is to launch per-VFIO device state transition thread in prepare_cb() which would then be responsible for doing *both* state transitions for this VFIO device.
And then collect this thread in cb().
However, this would need some extra interlocking:
- A lock inside VFIO code shared by all VFIO devices making sure no VFIO device starts the second transition (the cb() one) before all VFIO devices finish the first transition (the prepare_cb() one).
- Looking at the current QEMU code, there are some non-VFIO cb() handlers registered via qdev_add_vm_change_state_handler() and quite a few handlers registered via qemu_add_vm_change_state_handler().
I presume these VFIO prepare_cb()-related transitions need to finish before calling any of these handlers to maintain the current ordering so some kind of interlocking in vm_state_notify() between calling prepare_cb() and cb() may be necessary since any "prio" based ordering will have the same problem of reverse calling order on VM start vs stop.
To be clear, I'm not insisting on the implementation from this patch - any alternative one that preserves the aforementioned ordering rules and has similar downtime performance is probably okay too.
Quoted 2 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 43 lines · Maciej S. Szmigiero
Quoted 23 lines · Peter Xu
Quoted 1 line · Fabiano Rosas
A bit idiosyncratic, but I don't have any better suggestions.
One thing I don't think proper with the current approach: if it's a real "priority" it should be invoked always with
high->loworder, no matter if running=true or not.. but that's not what will happen if we encode it with the "depth" field.That is an implication that we have two demands, and they aren't the same, but this solution wrongly packed them together.
In 2023, VFIO already introduced
VMChangeStateEntry.prepare_cb(). That is a real impl of prority queues with only two:prepare_cb()always has higher priority than thecb()itself.Can VFIO simply leverage this existing interface, instead of hijacking "depth" in an unwanted way?
I believe the
_P2Pstates still need to happen first for all vfio devices, then it can do the next step. Logically I think it can also be done internally within VFIO by proper impl of these two callbacks, to achieve both:Quoted 4 lines
- Proper transition to P2P states first for all devices, meanwhile,
- Proper concurrency of all VFIO devices on device state transitionsWould that work in a cleaner way?
In the VFIO migration save/source code we have two transitions:
PRE_COPY->PRE_COPY_P2Pdone viaprepare_cb()andPRE_COPY_P2P->STOP_COPYdone viacb().
On the load/target side the situation is similar, only states are different.For each of these transitions we need two callbacks: one has to launch per-VFIO device state change thread and the other one has to "collect" (or join) these threads.
All handlers of the first callback have to be complete before calling any handler of the second callback since otherwise there would be VFIO device vs VFIO device serialization.
Quoted 8 lines
Moreover, all the prepare_cb()-related transitions have to be done before any of the cb()-related is started.
I presume what you suggest above is to launch per-VFIO device state transition thread in
prepare_cb()which would then be responsible for doing *both* state transitions for this VFIO device.
And then collect this thread incb().
Yes.
Quoted 5 lines
However, this would need some extra interlocking:
- A lock inside VFIO code shared by all VFIO devices making sure no VFIO device starts the second transition (thecb()one) before all VFIO devices finish the first transition (theprepare_cb()one).
Yes, more below.
Quoted 7 lines
- Looking at the current QEMU code, there are some non-VFIO
cb()handlers registered viaqdev_add_vm_change_state_handler()and quite a few handlers registered viaqemu_add_vm_change_state_handler().I presume these VFIO prepare_cb()-related transitions need to finish before calling any of these handlers to maintain the current ordering
IIUC, we don't necessarily need to keep this ordering; or do you see any real demand of that?
What I see is that the order is only introduced due to P2P state of VFIO, it seems very specific to me.
Quoted 9 lines
so some kind of interlocking in
vm_state_notify()between callingprepare_cb()andcb()may be necessary since any "prio" based ordering will have the same problem of reverse calling order on VM start vs stop.To be clear, I'm not insisting on the implementation from this patch - any alternative one that preserves the aforementioned ordering rules and has similar downtime performance is probably okay too.
The problem is, IIUC patch 1 needs better justification on its own, and I feel like it's not doing the right thing, as discussed previously. So even if we want to introduce a new priority-based approach, we may want to rethink patch 1.
IMHO it'll be perfect if we can simply reuse prepare_cb(). It seems working.
Due to this demand, I got to learn we have pthread_barrier_init(), but.. personally I like a simple atomic+event approach:
vfio thread:
# Do step 1, ->P2P
if (qatomic_fetch_inc(&vfio_p2p_done) + 1 == vfio_total_devices) {
qemu_event_set(&vfio_p2p_done_event);
}
qemu_event_wait(&vfio_p2p_done_event);
# Do step 2, ->STOPCOPYcb() only join()s.
Would this work?
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 68 lines · Peter Xu
Quoted 43 lines · Maciej S. Szmigiero
Quoted 23 lines · Peter Xu
Quoted 1 line · Fabiano Rosas
A bit idiosyncratic, but I don't have any better suggestions.
One thing I don't think proper with the current approach: if it's a real "priority" it should be invoked always with
high->loworder, no matter if running=true or not.. but that's not what will happen if we encode it with the "depth" field.That is an implication that we have two demands, and they aren't the same, but this solution wrongly packed them together.
In 2023, VFIO already introduced
VMChangeStateEntry.prepare_cb(). That is a real impl of prority queues with only two:prepare_cb()always has higher priority than thecb()itself.Can VFIO simply leverage this existing interface, instead of hijacking "depth" in an unwanted way?
I believe the
_P2Pstates still need to happen first for all vfio devices, then it can do the next step. Logically I think it can also be done internally within VFIO by proper impl of these two callbacks, to achieve both:Quoted 4 lines
- Proper transition to P2P states first for all devices, meanwhile,
- Proper concurrency of all VFIO devices on device state transitionsWould that work in a cleaner way?
In the VFIO migration save/source code we have two transitions:
PRE_COPY->PRE_COPY_P2Pdone viaprepare_cb()andPRE_COPY_P2P->STOP_COPYdone viacb().
On the load/target side the situation is similar, only states are different.For each of these transitions we need two callbacks: one has to launch per-VFIO device state change thread and the other one has to "collect" (or join) these threads.
All handlers of the first callback have to be complete before calling any handler of the second callback since otherwise there would be VFIO device vs VFIO device serialization.
Quoted 8 lines
Moreover, all the prepare_cb()-related transitions have to be done before any of the cb()-related is started.
I presume what you suggest above is to launch per-VFIO device state transition thread in
prepare_cb()which would then be responsible for doing *both* state transitions for this VFIO device.
And then collect this thread incb().Yes.
Quoted 5 lines
However, this would need some extra interlocking:
- A lock inside VFIO code shared by all VFIO devices making sure no VFIO device starts the second transition (thecb()one) before all VFIO devices finish the first transition (theprepare_cb()one).Yes, more below.
Quoted 5 lines
- Looking at the current QEMU code, there are some non-VFIO
cb()handlers registered viaqdev_add_vm_change_state_handler()and quite a few handlers registered viaqemu_add_vm_change_state_handler().
Quoted 8 lines
Quoted 2 lines
I presume these VFIO prepare_cb()-related transitions need to finish before calling any of these handlers to maintain the current ordering
IIUC, we don't necessarily need to keep this ordering; or do you see any real demand of that?
What I see is that the order is only introduced due to P2P state of VFIO, it seems very specific to me.
While I don't have a specific example why relaxing this ordering is unsafe I am not certain that it is safe either - we can end up introducing subtle race condition and possible guest memory corruption.
Maybe Alex and Cédric can provide some additional input here.
If nobody can authoritatively say that calling non-VFIO cb() VM state change handlers before being certain that all VFIO devices reached PRE_COPY_P2P / RUNNING_P2P state is indeed safe thing to do I think we'll have to play safe here.
Quoted 31 lines
Quoted 10 lines
so some kind of interlocking in
vm_state_notify()between callingprepare_cb()andcb()may be necessary since any "prio" based ordering will have the same problem of reverse calling order on VM start vs stop.To be clear, I'm not insisting on the implementation from this patch - any alternative one that preserves the aforementioned ordering rules and has similar downtime performance is probably okay too.
The problem is, IIUC patch 1 needs better justification on its own, and I feel like it's not doing the right thing, as discussed previously. So even if we want to introduce a new priority-based approach, we may want to rethink patch 1.
IMHO it'll be perfect if we can simply reuse
prepare_cb(). It seems working.Due to this demand, I got to learn we have
pthread_barrier_init(), but.. personally I like a simple atomic+event approach:vfio thread:
# Do step 1, ->P2P if (qatomic_fetch_inc(&vfio_p2p_done) + 1 == vfio_total_devices) { qemu_event_set(&vfio_p2p_done_event); } qemu_event_wait(&vfio_p2p_done_event); # Do step 2, ->STOPCOPY
Quoted 3 lines
cb()only join()s.Would this work?
At the first glance it should work - of course, we'd need to introduce the vfio_total_devices counter too.
However the issue about synchronization with other (non-VFIO) cb() handlers described above still (possibly) remains.
Quoted 2 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 82 lines · Maciej S. Szmigiero
Quoted 68 lines · Peter Xu
Quoted 43 lines · Maciej S. Szmigiero
Quoted 23 lines · Peter Xu
Quoted 1 line · Fabiano Rosas
A bit idiosyncratic, but I don't have any better suggestions.
One thing I don't think proper with the current approach: if it's a real "priority" it should be invoked always with
high->loworder, no matter if running=true or not.. but that's not what will happen if we encode it with the "depth" field.That is an implication that we have two demands, and they aren't the same, but this solution wrongly packed them together.
In 2023, VFIO already introduced VMChangeStateEntry.prepare_cb(). That is a real impl of prority queues with only two:
prepare_cb()always has higher priority than thecb()itself.Can VFIO simply leverage this existing interface, instead of hijacking "depth" in an unwanted way?
I believe the
_P2Pstates still need to happen first for all vfio devices, then it can do the next step. Logically I think it can also be done internally within VFIO by proper impl of these two callbacks, to achieve both:Quoted 4 lines
- Proper transition to P2P states first for all devices, meanwhile,
- Proper concurrency of all VFIO devices on device state transitionsWould that work in a cleaner way?
In the VFIO migration save/source code we have two transitions:
PRE_COPY->PRE_COPY_P2Pdone viaprepare_cb()andPRE_COPY_P2P->STOP_COPYdone viacb().
On the load/target side the situation is similar, only states are different.For each of these transitions we need two callbacks: one has to launch per-VFIO device state change thread and the other one has to "collect" (or join) these threads.
All handlers of the first callback have to be complete before calling any handler of the second callback since otherwise there would be VFIO device vs VFIO device serialization.
Quoted 8 lines
Moreover, all the prepare_cb()-related transitions have to be done before any of the cb()-related is started.
I presume what you suggest above is to launch per-VFIO device state transition thread in
prepare_cb()which would then be responsible for doing *both* state transitions for this VFIO device.
And then collect this thread incb().Yes.
Quoted 5 lines
However, this would need some extra interlocking:
- A lock inside VFIO code shared by all VFIO devices making sure no VFIO device starts the second transition (thecb()one) before all VFIO devices finish the first transition (theprepare_cb()one).Yes, more below.
Quoted 5 lines
- Looking at the current QEMU code, there are some non-VFIO
cb()handlers registered viaqdev_add_vm_change_state_handler()and quite a few handlers registered viaqemu_add_vm_change_state_handler().Quoted 8 lines
Quoted 2 lines
I presume these VFIO prepare_cb()-related transitions need to finish before calling any of these handlers to maintain the current ordering
IIUC, we don't necessarily need to keep this ordering; or do you see any real demand of that?
What I see is that the order is only introduced due to P2P state of VFIO, it seems very specific to me.
While I don't have a specific example why relaxing this ordering is unsafe I am not certain that it is safe either - we can end up introducing subtle race condition and possible guest memory corruption.
Quoted 7 lines
Maybe Alex and Cédric can provide some additional input here.
If nobody can authoritatively say that calling non-VFIO
cb()VM state change handlers before being certain that all VFIO devices reachedPRE_COPY_P2P/RUNNING_P2Pstate is indeed safe thing to do I think we'll have to play safe here.
I looked more deeply into this and even prototyped a similar new design.
However, in the end I think that running VFIO state transitions in parallel with other non-VFIO cb() handlers just isn't safe.
For example, on the migration source, the second transition is PRE_COPY_P2P -> STOP_COPY.PRE_COPY_P2P is the last state during migration that still allows the device to accept DMA and similar interactions, while STOP_COPY is already the "quiescent" state.
So if a VFIO has already reached STOP_COPY but a non-VFIO device callback still hasn't finished, then any DMA/MMIO/P2P transaction from that "unfinished" device to the VFIO device would now hit a VFIO device that no longer can accept such transactions.
This may result in hard-to-debug memory corruption during live migration.
For a similar reason I think we need to preserve qdev tree depth based ordering as its whole point is to make sure these transitions happen in the correct order between different devices.
This is even more pressing considering that the VFIO hardware needed to work on these issues isn't very widespread.
What I think we can do do instead, is to use a use an early prepare_cb() handler registered by a VFIO code to launch a state changing thread inside VFIO code.
Then a late prepare_cb() handler also registered by a VFIO code would synchronize with these state changing threads at their first sync point:
completion of the first state change.
This could be achieved for example by doing an event wait similar to the code snippet you had earlier suggested/provided.
The next sync point will be at an early cb() handler registered by VFIO code - this will let these waiting state changing VFIO threads start the second transition.
The final wait and joining of these threads will be at a late cb() handler registered by the VFIO code.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
so some kind of interlocking in
vm_state_notify()between callingprepare_cb()andcb()may be necessary since any "prio" based ordering will have the same problem of reverse calling order on VM start vs stop.
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
To be clear, I'm not insisting on the implementation from this patch - any alternative one that preserves the aforementioned ordering rules and has similar downtime performance is probably okay too.
The problem is, IIUC patch 1 needs better justification on its own, and I feel like it's not doing the right thing, as discussed previously. So even if we want to introduce a new priority-based approach, we may want to rethink patch 1.
Sure, any implementation that preserved the ordering I described above would be good.
But just using existing single-priority-per-qdev-depth prepare_cb() and cb() handlers unfortunately won't do it.
I think if there's a good idea how to implement such ordering in the VM state change handlers then the VFIO side of patch can be adjusted accordingly.
I also agree that adding the description of the issue being solved as justification for patch 1 and maybe even as a comment somewhere in the code would be kind to people looking at this code in the future and would prevent future issues being accidentally introduced in this area.
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 1 line · Maciej S. Szmigiero
I looked more deeply into this and even prototyped a similar new design.
Sorry for a late response; off work most of last week.
Quoted 10 lines
However, in the end I think that running VFIO state transitions in parallel with other non-VFIO
cb()handlers just isn't safe.For example, on the migration source, the second transition is
PRE_COPY_P2P->STOP_COPY.PRE_COPY_P2Pis the last state during migration that still allows the device to accept DMA and similar interactions, whileSTOP_COPYis already the "quiescent" state.
Quoted 5 lines
So if a VFIO has already reached
STOP_COPYbut a non-VFIO device callback still hasn't finished, then any DMA/MMIO/P2P transaction from that "unfinished" device to the VFIO device would now hit a VFIO device that no longer can accept such transactions.
This may result in hard-to-debug memory corruption during live migration.
Do you have any solid example of this problem?
AFAIU, P2P in VFIO state's term only means P2P DMAs _between_ host VFIO devices, nothing to do with emulated.
I am not aware of any P2P that can be initiated from emulated devices, even if it exists, it is only P2P from guest perspective not host: it will become host operations finally from processor side. I really don't think it's a concern.
My limited understanding of VFIO's P2P state is: when switching to P2P state, the VFIO device can still accept P2P DMA from other devices, but never initiating P2P DMAs, and the latter sentence is critical, it means the real quiecent state of the whole system happens after the last VFIO device switching to P2P state: then it means none of the VFIO devices can initiate P2P DMA anymore, whole system is quiesced. That happens earlier than reaching any of the cb()s here, because VFIO's prepare_cb() does that P2P state switch.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 4 lines · Peter Xu
Quoted 1 line · Maciej S. Szmigiero
I looked more deeply into this and even prototyped a similar new design.
Sorry for a late response; off work most of last week.
Also sorry for my late response - was extremely busy the entire previous week.
Quoted 16 lines
Quoted 10 lines
However, in the end I think that running VFIO state transitions in parallel with other non-VFIO
cb()handlers just isn't safe.For example, on the migration source, the second transition is
PRE_COPY_P2P->STOP_COPY.PRE_COPY_P2Pis the last state during migration that still allows the device to accept DMA and similar interactions, whileSTOP_COPYis already the "quiescent" state.Quoted 5 lines
So if a VFIO has already reached
STOP_COPYbut a non-VFIO device callback still hasn't finished, then any DMA/MMIO/P2P transaction from that "unfinished" device to the VFIO device would now hit a VFIO device that no longer can accept such transactions.
This may result in hard-to-debug memory corruption during live migration.
Quoted 4 lines
Do you have any solid example of this problem?
AFAIU, P2P in VFIO state's term only means P2P DMAs _between_ host VFIO devices, nothing to do with emulated.
I presume that quiescent VFIO devices can't accept *any* DMA, the spec (vfio.h) even says that:
Quoted 3 lines
The user should take steps to restrict access to vfio device regions while the device is in
STOP_COPYor risk corruption of the device migration data stream.
Correspondingly, I think the same goes for RESUMING state on the migration target since it is described there as:
Quoted 1 line
The device is *stopped* and is loading a new internal state
While you are right that the spec only talks about P2P DMA I don't see how host-initiated DMA would be different here - after all, it's about changing internal device state in a state where the device can't accept that anymore/yet.
Quoted 4 lines
I am not aware of any P2P that can be initiated from emulated devices, even if it exists, it is only P2P from guest perspective not host: it will become host operations finally from processor side. I really don't think it's a concern.
Yeah, it's normal DMA from the VFIO device perspective.
Quoted 6 lines
My limited understanding of VFIO's P2P state is: when switching to P2P state, the VFIO device can still accept P2P DMA from other devices, but never initiating P2P DMAs, and the latter sentence is critical, it means the real quiecent state of the whole system happens after the last VFIO device switching to P2P state: then it means none of the VFIO devices can initiate P2P DMA anymore, whole system is quiesced.
Just having the VFIO devices in the system prevented from being able to initiate DMA is not enough for them to reach "quiecent" or fully stable state as long as there are other possible sources of DMA transactions targeting them in the system.
Glancing at the existing vm_state_change handlers there are some concerning ones there:
* vhost-net (called via virtio_vmstate_change() in base virtio-net),
In the vhost-net case the vm_state_change handler for this device ultimately reaches vhost_net_stop() and do_vhost_dev_stop() and these seem to stop this network device rings at this point.
Now, having vhost-net packet buffers in a VFIO device MMIO BAR would be rather atypical configuration, but nevertheless letting VFIO device possibly reach "quiecent" state before this handler finishes would theoretically be a regression.
* virtio-blk virtio_blk_dma_restart_cb() vm_state_change handler submits some block requests to its (possibly dedicated) IOThread.
Again, having block device request buffers in a VFIO device MMIO BAR would not be a typical configuration, however letting this handler run before making sure that VFIO devices can accept DMA would theoretically make a regression too.
* same for scsi-bus scsi_dma_restart_cb() that does scsi_dma_restart_req() for each pending request.
The above is not an exhaustive list, these are only the cases I was able to identify relatively easily by going through the code.
And I think that with possible memory corruption issues its better to be safe than sorry, as they are pain to debug.
The only real non-VFIO cost of being safe about all of this is ~15 code lines addition to vm-change-state-handler.c, all the remaining complexity is already on the VFIO side of things.
Quoted 3 lines
That happens earlier
than reaching any of the cb()s here, because VFIO'sprepare_cb()does that P2P state switch.
I thought your proposed design was to launch (per-VFIO device) device state changing thread in prepare_cb(), make it do *both* VFIO device state transitions and collect it in cb()?
In this case the transitions are *NOT* ordered with respect to other cb() handlers (besides them being finished by the time the VFIO device qdev tree depth cb() handlers are reached by virtue of the threads being collected at this point - this is actually necessary in order to preserve proper ordering with respect to some types of interrupt controllers on the source).
So on the migration source the thread could already put device into "quiecent" state by the time other cb() handlers could run and on the migration target the device could still be
in an "unprepared" state by the time other cb() handlers are called.
Quoted 3 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 65 lines · Maciej S. Szmigiero
Quoted 4 lines · Peter Xu
Quoted 1 line · Maciej S. Szmigiero
I looked more deeply into this and even prototyped a similar new design.
Sorry for a late response; off work most of last week.
Also sorry for my late response - was extremely busy the entire previous week.
Quoted 16 lines
Quoted 10 lines
However, in the end I think that running VFIO state transitions in parallel with other non-VFIO
cb()handlers just isn't safe.For example, on the migration source, the second transition is
PRE_COPY_P2P->STOP_COPY.PRE_COPY_P2Pis the last state during migration that still allows the device to accept DMA and similar interactions, whileSTOP_COPYis already the "quiescent" state.Quoted 5 lines
So if a VFIO has already reached
STOP_COPYbut a non-VFIO device callback still hasn't finished, then any DMA/MMIO/P2P transaction from that "unfinished" device to the VFIO device would now hit a VFIO device that no longer can accept such transactions.
This may result in hard-to-debug memory corruption during live migration.Quoted 4 lines
Do you have any solid example of this problem?
AFAIU, P2P in VFIO state's term only means P2P DMAs _between_ host VFIO devices, nothing to do with emulated.
I presume that quiescent VFIO devices can't accept *any* DMA, the spec (
vfio.h) even says that:Quoted 3 lines
The user should take steps to restrict access to vfio device regions while the device is in
STOP_COPYor risk corruption of the device migration data stream.Correspondingly, I think the same goes for RESUMING state on the migration target since it is described there as:
Quoted 1 line
The device is *stopped* and is loading a new internal state
While you are right that the spec only talks about P2P DMA I don't see how host-initiated DMA would be different here - after all, it's about changing internal device state in a state where the device can't accept that anymore/yet.
Quoted 4 lines
I am not aware of any P2P that can be initiated from emulated devices, even if it exists, it is only P2P from guest perspective not host: it will become host operations finally from processor side. I really don't think it's a concern.
Yeah, it's normal DMA from the VFIO device perspective.
Quoted 6 lines
My limited understanding of VFIO's P2P state is: when switching to P2P state, the VFIO device can still accept P2P DMA from other devices, but never initiating P2P DMAs, and the latter sentence is critical, it means the real quiecent state of the whole system happens after the last VFIO device switching to P2P state: then it means none of the VFIO devices can initiate P2P DMA anymore, whole system is quiesced.
Just having the VFIO devices in the system prevented from being able to initiate DMA is not enough for them to reach "quiecent" or fully stable state as long as there are other possible sources of DMA transactions targeting them in the system.
Glancing at the existing
vm_state_changehandlers there are some concerning ones there:
* vhost-net (called viavirtio_vmstate_change()in base virtio-net),
Quoted 8 lines
In the vhost-net case the
vm_state_changehandler for this device ultimately reachesvhost_net_stop()anddo_vhost_dev_stop()and these seem to stop this network device rings at this point.Now, having vhost-net packet buffers in a VFIO device MMIO BAR would be rather atypical configuration, but nevertheless letting VFIO device possibly reach "quiecent" state before this handler finishes would theoretically be a regression.
If this is an issue, it was an issue for master branch too, right? Because the master branch (with prepare_cb()s) already switch to P2P_DMA state for all VFIO devices before invoking any cb().
It is also an issue then even before P2P_DMA state introduced, because before that all cb()s can be run at random orders.
What you said might be an issue indeed in that extremely corner case, but I think it still needs some justification, and it's IMHO orthogonal to what we're talking about. If it's an issue, maybe we want to fix it first before attempting to move further to any concurrent approaches, but is it really an issue is to be better justified.
Applies to all below.
Quoted 22 lines
* virtio-blk
virtio_blk_dma_restart_cb()vm_state_changehandler submits some block requests to its (possibly dedicated) IOThread.Again, having block device request buffers in a VFIO device MMIO BAR would not be a typical configuration, however letting this handler run before making sure that VFIO devices can accept DMA would theoretically make a regression too.
* same for scsi-bus
scsi_dma_restart_cb()that doesscsi_dma_restart_req()for each pending request.The above is not an exhaustive list, these are only the cases I was able to identify relatively easily by going through the code.
And I think that with possible memory corruption issues its better to be safe than sorry, as they are pain to debug.The only real non-VFIO cost of being safe about all of this is ~15 code lines addition to
vm-change-state-handler.c, all the remaining complexity is already on the VFIO side of things.
Quoted 7 lines
Quoted 3 lines
That happens earlier
than reaching any of the cb()s here, because VFIO'sprepare_cb()does that P2P state switch.I thought your proposed design was to launch (per-VFIO device) device state changing thread in
prepare_cb(), make it do *both* VFIO device state transitions and collect it incb()?
The idea was only about trying to leverage prepare_cb() to achieve real "prioritized callbacks" for a device, based on patch 1 being problematic.
What prepare_cb() does can be anything. Certainly, we can make it only do step 1 then in cb() kick all threads to move on to step 2, and sync again.
Quoted 8 lines
In this case the transitions are *NOT* ordered with respect to other
cb()handlers (besides them being finished by the time the VFIO device qdev tree depthcb()handlers are reached by virtue of the threads being collected at this point - this is actually necessary in order to preserve proper ordering with respect to some types of interrupt controllers on the source).
Quoted 4 lines
So on the migration source the thread could already put device into "quiecent" state by the time other
cb()handlers could run and on the migration target the device could still be
in an "unprepared" state by the time othercb()handlers are called.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 70 lines · Peter Xu
Quoted 69 lines · Maciej S. Szmigiero
Quoted 4 lines · Peter Xu
Quoted 1 line · Maciej S. Szmigiero
I looked more deeply into this and even prototyped a similar new design.
Sorry for a late response; off work most of last week.
Also sorry for my late response - was extremely busy the entire previous week.
Quoted 16 lines
Quoted 10 lines
However, in the end I think that running VFIO state transitions in parallel with other non-VFIO
cb()handlers just isn't safe.For example, on the migration source, the second transition is
PRE_COPY_P2P->STOP_COPY.PRE_COPY_P2Pis the last state during migration that still allows the device to accept DMA and similar interactions, whileSTOP_COPYis already the "quiescent" state.Quoted 5 lines
So if a VFIO has already reached
STOP_COPYbut a non-VFIO device callback still hasn't finished, then any DMA/MMIO/P2P transaction from that "unfinished" device to the VFIO device would now hit a VFIO device that no longer can accept such transactions.
This may result in hard-to-debug memory corruption during live migration.Quoted 4 lines
Do you have any solid example of this problem?
AFAIU, P2P in VFIO state's term only means P2P DMAs _between_ host VFIO devices, nothing to do with emulated.
I presume that quiescent VFIO devices can't accept *any* DMA, the spec (
vfio.h) even says that:Quoted 3 lines
The user should take steps to restrict access to vfio device regions while the device is in
STOP_COPYor risk corruption of the device migration data stream.Correspondingly, I think the same goes for RESUMING state on the migration target since it is described there as:
Quoted 1 line
The device is *stopped* and is loading a new internal state
While you are right that the spec only talks about P2P DMA I don't see how host-initiated DMA would be different here - after all, it's about changing internal device state in a state where the device can't accept that anymore/yet.
Quoted 4 lines
I am not aware of any P2P that can be initiated from emulated devices, even if it exists, it is only P2P from guest perspective not host: it will become host operations finally from processor side. I really don't think it's a concern.
Yeah, it's normal DMA from the VFIO device perspective.
Quoted 6 lines
My limited understanding of VFIO's P2P state is: when switching to P2P state, the VFIO device can still accept P2P DMA from other devices, but never initiating P2P DMAs, and the latter sentence is critical, it means the real quiecent state of the whole system happens after the last VFIO device switching to P2P state: then it means none of the VFIO devices can initiate P2P DMA anymore, whole system is quiesced.
Just having the VFIO devices in the system prevented from being able to initiate DMA is not enough for them to reach "quiecent" or fully stable state as long as there are other possible sources of DMA transactions targeting them in the system.
Glancing at the existing
vm_state_changehandlers there are some concerning ones there:
* vhost-net (called viavirtio_vmstate_change()in base virtio-net),In the vhost-net case the
vm_state_changehandler for this device ultimately reachesvhost_net_stop()anddo_vhost_dev_stop()and these seem to stop this network device rings at this point.
Quoted 8 lines
Quoted 4 lines
Now, having vhost-net packet buffers in a VFIO device MMIO BAR would be rather atypical configuration, but nevertheless letting VFIO device possibly reach "quiecent" state before this handler finishes would theoretically be a regression.
If this is an issue, it was an issue for master branch too, right? Because the master branch (with prepare_cb()s) already switch to
P2P_DMAstate for all VFIO devices before invoking anycb().
The VFIO devices in {PRE_COPY,RUNNING}_P2P states can {still,already} accept DMA so in the master branch by the time prepare_cb() handlers exit and cb() ones start running the VFIO devices are {still,already} capable of accepting incoming DMA.
On the other hand, if you meant the state transition into STOP_COPY on the migration source currently initiated from cb() handler then there can be an ordering issue in principle.
It looks like the current master branch mostly avoids this by registering vhost-net vm_state_change handler from VirtIODevice and not from its parent PCI device, so with the usual PCI topology vhost-net cb() handler will run before the VFIO PCI device one (and so still hit DMA-capable VFIO device) due to the vhost-net VirtIODevice being one level deeper in the qdev tree. It could theoretically be problem with more complex PCI topology though.
Quoted 2 lines
It is also an issue then even before
P2P_DMAstate introduced, because before that all cb()s can be run at random orders.
Technically, the order is based on the device qdev tree depth and within the same qdev tree depth level it looks like the handler calling order will be dependent on the order the registering devices were instantiated.
This means that even before these P2P states were introduced VM configurations that instantiated VFIO/virtio devices in the certain "right" order or topology weren't affected.
So maybe adding these P2P states mostly fixed this issue too?
Quoted 5 lines
What you said might be an issue indeed in that extremely corner case, but I think it still needs some justification, and it's IMHO orthogonal to what we're talking about. If it's an issue, maybe we want to fix it first before attempting to move further to any concurrent approaches, but is it really an issue is to be better justified.
As I wrote above, I don't think there's a major ordering issue in the current master branch, at least not for P2P-state-capable VFIO devices.
Quoted 1 line
Applies to all below.
The above example is about what happens on the migration source, however the examples below are about the migration target.
Here, in the current master branch these non-VFIO cb() handlers are guaranteed to hit DMA-capable VFIO device since this switch happened in its prepare_cb() handler and VFIO device cb() handler does not change its DMA acceptance qualities so its ordering with respect to other cb() handlers shouldn't matter.
Quoted 44 lines
Quoted 22 lines
* virtio-blk
virtio_blk_dma_restart_cb()vm_state_changehandler submits some block requests to its (possibly dedicated) IOThread.Again, having block device request buffers in a VFIO device MMIO BAR would not be a typical configuration, however letting this handler run before making sure that VFIO devices can accept DMA would theoretically make a regression too.
* same for scsi-bus
scsi_dma_restart_cb()that doesscsi_dma_restart_req()for each pending request.The above is not an exhaustive list, these are only the cases I was able to identify relatively easily by going through the code.
And I think that with possible memory corruption issues its better to be safe than sorry, as they are pain to debug.The only real non-VFIO cost of being safe about all of this is ~15 code lines addition to
vm-change-state-handler.c, all the remaining complexity is already on the VFIO side of things.Quoted 7 lines
Quoted 3 lines
That happens earlier
than reaching any of the cb()s here, because VFIO'sprepare_cb()does that P2P state switch.I thought your proposed design was to launch (per-VFIO device) device state changing thread in
prepare_cb(), make it do *both* VFIO device state transitions and collect it incb()?The idea was only about trying to leverage
prepare_cb()to achieve real "prioritized callbacks" for a device, based on patch 1 being problematic.What
prepare_cb()does can be anything. Certainly, we can make it only do step 1 then incb()kick all threads to move on to step 2, and sync again.Quoted 8 lines
In this case the transitions are *NOT* ordered with respect to other
cb()handlers (besides them being finished by the time the VFIO device qdev tree depthcb()handlers are reached by virtue of the threads being collected at this point - this is actually necessary in order to preserve proper ordering with respect to some types of interrupt controllers on the source).
Quoted 7 lines
Quoted 4 lines
So on the migration source the thread could already put device into "quiecent" state by the time other
cb()handlers could run and on the migration target the device could still be
in an "unprepared" state by the time othercb()handlers are called.Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 9 lines · Maciej S. Szmigiero
The VFIO devices in {PRE_COPY,RUNNING}_P2P states can {still,already} accept DMA so in the master branch by the time
prepare_cb()handlers exit andcb()ones start running the VFIO devices are {still,already} capable of accepting incoming DMA.On the other hand, if you meant the state transition into
STOP_COPYon the migration source currently initiated fromcb()handler then there can be an ordering issue in principle.
Quoted 6 lines
It looks like the current master branch mostly avoids this by registering vhost-net
vm_state_changehandler from VirtIODevice and not from its parent PCI device, so with the usual PCI topology vhost-netcb()handler will run before the VFIO PCI device one (and so still hit DMA-capable VFIO device) due to the vhost-net VirtIODevice being one level deeper in the qdev tree. It could theoretically be problem with more complex PCI topology though.
That's an interesting observation, but neither do I think it's intentional, nor do I think it guarantees the depth ordering. Consider when a VFIO device is put under a few layers of PCIe switches, or PCI bridges. In that case the VFIO device notifier can have larger depth value v.s. the virtio device when the virtio device is attached to the root complex.
Quoted 8 lines
Quoted 3 lines
It is also an issue then even before
P2P_DMAstate introduced, because before that all cb()s can be run at random orders.Technically, the order is based on the device qdev tree depth and within the same qdev tree depth level it looks like the handler calling order will be dependent on the order the registering devices were instantiated.
Quoted 5 lines
This means that even before these P2P states were introduced VM configurations that instantiated VFIO/virtio devices in the certain "right" order or topology weren't affected.
So maybe adding these P2P states mostly fixed this issue too?
I still think they're two different issues, and I still think with/without P2P the issue is the same, now I tend to agree this issue exists, especially if virtio-blk have similar behavior like virtio-net.
For one example of virtio-blk DMA to VFIO MMIO regions:
https://lore.kernel.org/r/[email protected]
Said that, I don't know it's 100% triggerable in this case with notifiers, but sounds relevant. For virtio-net and others, I have less idea.
In all cases, I still think there are two issues to fix, and I think we don't need to fix this problem in one shot. I still don't think they're directly relevant, at least on the goals.
So with what I suggested previously relying on prepare_cb() I believe we can still do the concurrency issue you're looking for, making sure when reaching VFIO's cb() it's still at least accepting DMAs (P2P is fine).
For the other issue you reported, even if existed, I am not yet sure VFIO is the only special case that got affected. I think it's possible some other emulated devices suffer the same even if it has no direct hardware attached. Say, there can be device backends got stopped in cb() when VM stopped, further DMA to it may cause assertions, then it's the same issue that needs solving, where essentially we may require all such devices to provide similar P2P states like VFIO to make sure they initiate DMAs and flush them in a prepare_cb(), then if all devices' prepare_cb() will make sure no DMA to be initiated anymore, cb()s will have no ordering constraint on DMAs.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 17 lines · Peter Xu
Quoted 9 lines · Maciej S. Szmigiero
The VFIO devices in {PRE_COPY,RUNNING}_P2P states can {still,already} accept DMA so in the master branch by the time
prepare_cb()handlers exit andcb()ones start running the VFIO devices are {still,already} capable of accepting incoming DMA.On the other hand, if you meant the state transition into
STOP_COPYon the migration source currently initiated fromcb()handler then there can be an ordering issue in principle.Quoted 6 lines
It looks like the current master branch mostly avoids this by registering vhost-net
vm_state_changehandler from VirtIODevice and not from its parent PCI device, so with the usual PCI topology vhost-netcb()handler will run before the VFIO PCI device one (and so still hit DMA-capable VFIO device) due to the vhost-net VirtIODevice being one level deeper in the qdev tree. It could theoretically be problem with more complex PCI topology though.
Quoted 5 lines
That's an interesting observation, but neither do I think it's intentional, nor do I think it guarantees the depth ordering. Consider when a VFIO device is put under a few layers of PCIe switches, or PCI bridges. In that case the VFIO device notifier can have larger depth value v.s. the virtio device when the virtio device is attached to the root complex.
Yeah, that's why I wrote that it could be a problem with "more complex PCI topology".
Nevertheless, that's probably uncommon enough setup to not generate visible complaints about things being broken.
Quoted 19 lines
Quoted 9 lines
Quoted 4 lines
It is also an issue then even before
P2P_DMAstate introduced, because before that all cb()s can be run at random orders.Technically, the order is based on the device qdev tree depth and within the same qdev tree depth level it looks like the handler calling order will be dependent on the order the registering devices were instantiated.
Quoted 5 lines
This means that even before these P2P states were introduced VM configurations that instantiated VFIO/virtio devices in the certain "right" order or topology weren't affected.
So maybe adding these P2P states mostly fixed this issue too?
I still think they're two different issues, and I still think with/without P2P the issue is the same, now I tend to agree this issue exists, especially if virtio-blk have similar behavior like virtio-net.
Quoted 6 lines
For one example of virtio-blk DMA to VFIO MMIO regions:
https://lore.kernel.org/r/[email protected]
Said that, I don't know it's 100% triggerable in this case with notifiers, but sounds relevant.
It does look like it could trigger this issue indeed since it's DMA done to VFIO device MMIO BAR.
However, how easy would be to reproduce this is another question.
Quoted 5 lines
For virtio-net and others, I have less idea.
In all cases, I still think there are two issues to fix, and I think we don't need to fix this problem in one shot. I still don't think they're directly relevant, at least on the goals.
Sure, let's just not make the possible race situation worse.
Quoted 3 lines
So with what I suggested previously relying on
prepare_cb()I believe we can still do the concurrency issue you're looking for, making sure when reaching VFIO'scb()it's still at least accepting DMAs (P2P is fine).
But that's more or less the design of this (original) patch set - make the state change to RUNNING_P2P on the target finish already in the prepare_cb() time, make the state change to STOP_COPY on the source begin only in the cb() time?
Quoted 10 lines
For the other issue you reported, even if existed, I am not yet sure VFIO is the only special case that got affected. I think it's possible some other emulated devices suffer the same even if it has no direct hardware attached. Say, there can be device backends got stopped in
cb()when VM stopped, further DMA to it may cause assertions, then it's the same issue that needs solving, where essentially we may require all such devices to provide similar P2P states like VFIO to make sure they initiate DMAs and flush them in aprepare_cb(), then if all devices'prepare_cb()will make sure no DMA to be initiated anymore, cb()s will have no ordering constraint on DMAs.
Although the description above is about the migration source the situation on the migration target is essentially a mirror image of it so I will refer to both cases below.
Specifically:
* on the source no device should reach non DMA accepting state (like STOP_COPY for VFIO) before all devices reach non DMA generating state (PRE_COPY_P2P-equivalent).
* on the target no device should reach DMA generating state (RUNNING-equivalent) before all devices reach DMA accepting state (RUNNING_P2P-like).
That's why even in the case where non-VFIO devices introduce the equivalent of VFIO P2P states and implement a prepare_cb() callback we still need a (VM-wide) sync point between these callbacks for the benefit of async state changes/threads since no cb()-like async state change should start until all prepare_cb()-like changes finish (including these for other devices).
Quoted 2 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 18 lines · Maciej S. Szmigiero
Quoted 17 lines · Peter Xu
Quoted 15 lines · Maciej S. Szmigiero
The VFIO devices in {PRE_COPY,RUNNING}_P2P states can {still,already} accept DMA so in the master branch by the time
prepare_cb()handlers exit andcb()ones start running the VFIO devices are {still,already} capable of accepting incoming DMA.On the other hand, if you meant the state transition into
STOP_COPYon the migration source currently initiated fromcb()handler then there can be an ordering issue in principle.It looks like the current master branch mostly avoids this by registering vhost-net
vm_state_changehandler from VirtIODevice and not from its parent PCI device, so with the usual PCI topology vhost-netcb()handler will run before the VFIO PCI device one (and so still hit DMA-capable VFIO device) due to the vhost-net VirtIODevice being one level deeper in the qdev tree. It could theoretically be problem with more complex PCI topology though.
Quoted 7 lines
Quoted 5 lines
That's an interesting observation, but neither do I think it's intentional, nor do I think it guarantees the depth ordering. Consider when a VFIO device is put under a few layers of PCIe switches, or PCI bridges. In that case the VFIO device notifier can have larger depth value v.s. the virtio device when the virtio device is attached to the root complex.
Yeah, that's why I wrote that it could be a problem with "more complex PCI topology".
Ah yes.
Quoted 42 lines
Nevertheless, that's probably uncommon enough setup to not generate visible complaints about things being broken.
Quoted 19 lines
Quoted 9 lines
Quoted 4 lines
It is also an issue then even before
P2P_DMAstate introduced, because before that all cb()s can be run at random orders.Technically, the order is based on the device qdev tree depth and within the same qdev tree depth level it looks like the handler calling order will be dependent on the order the registering devices were instantiated.
Quoted 5 lines
This means that even before these P2P states were introduced VM configurations that instantiated VFIO/virtio devices in the certain "right" order or topology weren't affected.
So maybe adding these P2P states mostly fixed this issue too?
I still think they're two different issues, and I still think with/without P2P the issue is the same, now I tend to agree this issue exists, especially if virtio-blk have similar behavior like virtio-net.
Quoted 6 lines
For one example of virtio-blk DMA to VFIO MMIO regions:
https://lore.kernel.org/r/[email protected]
Said that, I don't know it's 100% triggerable in this case with notifiers, but sounds relevant.
It does look like it could trigger this issue indeed since it's DMA done to VFIO device MMIO BAR.
However, how easy would be to reproduce this is another question.Quoted 5 lines
For virtio-net and others, I have less idea.
In all cases, I still think there are two issues to fix, and I think we don't need to fix this problem in one shot. I still don't think they're directly relevant, at least on the goals.
Sure, let's just not make the possible race situation worse.
Quoted 8 lines
Quoted 3 lines
So with what I suggested previously relying on
prepare_cb()I believe we can still do the concurrency issue you're looking for, making sure when reaching VFIO'scb()it's still at least accepting DMAs (P2P is fine).But that's more or less the design of this (original) patch set - make the state change to
RUNNING_P2Pon the target finish already in theprepare_cb()time, make the state change toSTOP_COPYon the source begin only in thecb()time?
I am not against the idea in general. I'm against patch 1 where it hijacks "depth" into something even more complicated.
The "depth" itself makes sense to be recorded as a value, but I think it's already a bit confusing to call it "priority" in VMChangeStateEntry, because it isn't: when something has higher "priority", it should always be executed before lower priority items. Here, the current code invert the order for !RUN case, so it's in reality "depth", not "priority".
Now patch 1 added yet another extra "adj" concept on top of qdev hierachy's "depth", which in fact the caller needs "priority" and callers need to explicitly handle that by checking RUNNING and invert things.
That's the part I'm definitely against. Such code is IMHO not maintainable.
So my point is if we can implement your idea with existing prepare_cb() we should just use it. Even if we want to enhance the priority support for cb()s, we shouldn't use patch 1. I never thought further on that because I still assumed relying on prepare_cb() works.
Quoted 20 lines
Quoted 11 lines
For the other issue you reported, even if existed, I am not yet sure VFIO is the only special case that got affected. I think it's possible some other emulated devices suffer the same even if it has no direct hardware attached. Say, there can be device backends got stopped in
cb()when VM stopped, further DMA to it may cause assertions, then it's the same issue that needs solving, where essentially we may require all such devices to provide similar P2P states like VFIO to make sure they initiate DMAs and flush them in aprepare_cb(), then if all devices'prepare_cb()will make sure no DMA to be initiated anymore, cb()s will have no ordering constraint on DMAs.Although the description above is about the migration source the situation on the migration target is essentially a mirror image of it so I will refer to both cases below.
Specifically:
* on the source no device should reach non DMA accepting state (likeSTOP_COPYfor VFIO) before all devices reach non DMA generating state (PRE_COPY_P2P-equivalent).
Quoted 8 lines
* on the target no device should reach DMA generating state (RUNNING-equivalent) before all devices reach DMA accepting state (RUNNING_P2P-like).
That's why even in the case where non-VFIO devices introduce the equivalent of VFIO P2P states and implement a
prepare_cb()callback we still need a (VM-wide) sync point between these callbacks for the benefit of async state changes/threads since no cb()-like async state change should start until all prepare_cb()-like changes finish (including these for other devices).
IIUC, prepare_cb() + cb() is already that "sync point". After all prepare_cb() done, the VM should achieve "DMA quiesced" across the system?
What's missing is we should move all problematic device cb()s "let's flush pending DMAs" logic into a prepare_cb(), leaving the real stop logic in cb()s. That's the 2nd problem which I think we can skip until a reproducer pops up at least.
VFIO is only special here when it wants to concurrent process of state changes. If you create threads in prepare_cb(), then in cb() you have chance to synchronize everything correctly satisfying the "sync point". That's not a requirement for most of the rest devices.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 93 lines · Peter Xu
Quoted 18 lines · Maciej S. Szmigiero
Quoted 17 lines · Peter Xu
Quoted 15 lines · Maciej S. Szmigiero
The VFIO devices in {PRE_COPY,RUNNING}_P2P states can {still,already} accept DMA so in the master branch by the time
prepare_cb()handlers exit andcb()ones start running the VFIO devices are {still,already} capable of accepting incoming DMA.On the other hand, if you meant the state transition into
STOP_COPYon the migration source currently initiated fromcb()handler then there can be an ordering issue in principle.It looks like the current master branch mostly avoids this by registering vhost-net
vm_state_changehandler from VirtIODevice and not from its parent PCI device, so with the usual PCI topology vhost-netcb()handler will run before the VFIO PCI device one (and so still hit DMA-capable VFIO device) due to the vhost-net VirtIODevice being one level deeper in the qdev tree. It could theoretically be problem with more complex PCI topology though.Quoted 7 lines
Quoted 5 lines
That's an interesting observation, but neither do I think it's intentional, nor do I think it guarantees the depth ordering. Consider when a VFIO device is put under a few layers of PCIe switches, or PCI bridges. In that case the VFIO device notifier can have larger depth value v.s. the virtio device when the virtio device is attached to the root complex.
Yeah, that's why I wrote that it could be a problem with "more complex PCI topology".
Ah yes.
Quoted 42 lines
Nevertheless, that's probably uncommon enough setup to not generate visible complaints about things being broken.
Quoted 19 lines
Quoted 9 lines
Quoted 4 lines
It is also an issue then even before
P2P_DMAstate introduced, because before that all cb()s can be run at random orders.Technically, the order is based on the device qdev tree depth and within the same qdev tree depth level it looks like the handler calling order will be dependent on the order the registering devices were instantiated.
Quoted 5 lines
This means that even before these P2P states were introduced VM configurations that instantiated VFIO/virtio devices in the certain "right" order or topology weren't affected.
So maybe adding these P2P states mostly fixed this issue too?
I still think they're two different issues, and I still think with/without P2P the issue is the same, now I tend to agree this issue exists, especially if virtio-blk have similar behavior like virtio-net.
Quoted 6 lines
For one example of virtio-blk DMA to VFIO MMIO regions:
https://lore.kernel.org/r/[email protected]
Said that, I don't know it's 100% triggerable in this case with notifiers, but sounds relevant.
It does look like it could trigger this issue indeed since it's DMA done to VFIO device MMIO BAR.
However, how easy would be to reproduce this is another question.Quoted 5 lines
For virtio-net and others, I have less idea.
In all cases, I still think there are two issues to fix, and I think we don't need to fix this problem in one shot. I still don't think they're directly relevant, at least on the goals.
Sure, let's just not make the possible race situation worse.
Quoted 8 lines
Quoted 3 lines
So with what I suggested previously relying on
prepare_cb()I believe we can still do the concurrency issue you're looking for, making sure when reaching VFIO'scb()it's still at least accepting DMAs (P2P is fine).But that's more or less the design of this (original) patch set - make the state change to
RUNNING_P2Pon the target finish already in theprepare_cb()time, make the state change toSTOP_COPYon the source begin only in thecb()time?I am not against the idea in general. I'm against patch 1 where it hijacks "depth" into something even more complicated.
The "depth" itself makes sense to be recorded as a value, but I think it's already a bit confusing to call it "priority" in VMChangeStateEntry, because it isn't: when something has higher "priority", it should always be executed before lower priority items. Here, the current code invert the order for !RUN case, so it's in reality "depth", not "priority".
Now patch 1 added yet another extra "adj" concept on top of qdev hierachy's "depth", which in fact the caller needs "priority" and callers need to explicitly handle that by checking RUNNING and invert things.
Quoted 6 lines
That's the part I'm definitely against. Such code is IMHO not maintainable.
So my point is if we can implement your idea with existing
prepare_cb()we should just use it. Even if we want to enhance the priority support for cb()s, we shouldn't use patch 1. I never thought further on that because I still assumed relying onprepare_cb()works.
If term "priority" is misleading due to reverse order of calling these handlers on start vs stop we can change this term to "order".
Then the new adjustment becomes "order adjustment".
On the other hand, if the need for "additional indirection" inside VFIO code to make the start vs stop handler ordering constant and the priority/order adjustment mechanism from patch 1 are the most controversial parts of this approach then there's another way to have prepare_cb()/cb() called at the beginning and end of vm_change_state priority level:
Simply add another data structure besides vm_change_state_head with these "pre" and "post" handlers (probably a hash table) and then call these handlers before and after entering each priority value inside the existing QTAILQ_FOREACH_SAFE() and QTAILQ_FOREACH_REVERSE_SAFE() in vm_state_notify().
This way the VFIO code could register these "pre" and "post" handlers directly for its prepare_cb() and cb() state transitions.
Quoted 29 lines
Quoted 20 lines
Quoted 11 lines
For the other issue you reported, even if existed, I am not yet sure VFIO is the only special case that got affected. I think it's possible some other emulated devices suffer the same even if it has no direct hardware attached. Say, there can be device backends got stopped in
cb()when VM stopped, further DMA to it may cause assertions, then it's the same issue that needs solving, where essentially we may require all such devices to provide similar P2P states like VFIO to make sure they initiate DMAs and flush them in aprepare_cb(), then if all devices'prepare_cb()will make sure no DMA to be initiated anymore, cb()s will have no ordering constraint on DMAs.Although the description above is about the migration source the situation on the migration target is essentially a mirror image of it so I will refer to both cases below.
Specifically:
* on the source no device should reach non DMA accepting state (likeSTOP_COPYfor VFIO) before all devices reach non DMA generating state (PRE_COPY_P2P-equivalent).Quoted 8 lines
* on the target no device should reach DMA generating state (RUNNING-equivalent) before all devices reach DMA accepting state (RUNNING_P2P-like).
That's why even in the case where non-VFIO devices introduce the equivalent of VFIO P2P states and implement a
prepare_cb()callback we still need a (VM-wide) sync point between these callbacks for the benefit of async state changes/threads since no cb()-like async state change should start until all prepare_cb()-like changes finish (including these for other devices).
Quoted 7 lines
IIUC,
prepare_cb()+cb()is already that "sync point". After allprepare_cb()done, the VM should achieve "DMA quiesced" across the system?What's missing is we should move all problematic device cb()s "let's flush pending DMAs" logic into a
prepare_cb(), leaving the real stop logic in cb()s. That's the 2nd problem which I think we can skip until a reproducer pops up at least.
Judging by the fact that you mention "flushing the pending DMA" I think you refer to situation on the source.
On the source there's the vhost-net issue, which one can indeed be broken even in the current git master by atypical PCI topology.
But there's similar but mirror problem on the target (described in my second bullet point in the quote above) - the one with virtio-blk and scsi-bus. Even today, this issue should not depend on VM PCI topology.
In the target case the "sync point" is about all devices being "DMA ready" (but not yet launching DMA - I wouldn't call it "DMA quiescent" since there's nothing to quiesce yet at this point).
So this would be a true regression with VFIO parallel state change threads.
Quoted 4 lines
VFIO is only special here when it wants to concurrent process of state changes. If you create threads in
prepare_cb(), then incb()you have chance to synchronize everything correctly satisfying the "sync point". That's not a requirement for most of the rest devices.
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
Quoted 2 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 7 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially prepare_cb() is the priority mechanism here, we only have HIGH and NORMAL priority, where cb() is the NORMAL priority.
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
- Rename VMChangeStateEntry.priority to depth
- Normalize prepare_cb() into VM_CHANGE_NOTIFY_PRI_HIGH, making cb() to be NORMAL, OTOH. With this, prepare_cb() needs to be registered separately with qemu_add_vm_change_state_handler_prio_full(). The function now should drop prepare_cb() but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.
- Introduce VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.
- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into PRI_HIGH to either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.
Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.
And where the qdev tree depth will go in a call to qemu_add_vm_change_state_handler_prio_full() - as an additional "depth" parameter?
Quoted 9 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.
In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of its cb() handler separated and moved into prepare_cb(),
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from cb() at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,
* The postponed dirty memory logging stop handler also runs from cb() at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from cb() at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from cb() to a new priority *before* VM_CHANGE_NOTIFY_PRI_SYNC (maybe called VM_CHANGE_NOTIFY_PRI_SYNC_BEFORE or similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads from VM_CHANGE_NOTIFY_PRI_HIGH.
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting cb() handlers, so it should really run at VM_CHANGE_NOTIFY_PRI_LOW (below PRI_NORMAL) for best parallelism.
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
Quoted 2 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 47 lines · Maciej S. Szmigiero
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.
Quoted 3 lines
And where the qdev tree depth will go in a call to
qemu_add_vm_change_state_handler_prio_full()- as an additional "depth" parameter?
Yes. The rename change I mentioned above should have done so.
Quoted 6 lines
Quoted 6 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
Quoted 8 lines
Quoted 4 lines
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of itscb()handler separated and moved intoprepare_cb(),
I thought this is required with whatever priority impl we will have, even if I don't think we need to fix it right now unless you wanted to..
Quoted 6 lines
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from
cb()at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,
Is anyone using VFIO migration on PPC that you're aware of? If not, maybe we don't need to worry too much for now. Meanwhile, this looks like an existing issue as well.
For the fix whenever wanted, IIUC the intr controller should register both HIGH and NORMAL priority notifiers, then when VM stop, disable the controller only in NORMAL notifier, and when VM starts, enable the controller only in HIGH notifier (we could think about better names for the three steps, maybe it shouldn't be called as "priority" as well even if the executions will be strictly ordered).
Quoted 7 lines
* The postponed dirty memory logging stop handler also runs from
cb()at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?
It's only stopping the tracking, results of the tracking shouldn't matter anymore on whether some DMA was trapped. It looks safe no matter where we put it.
Quoted 5 lines
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from
cb()at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.
Doesn't look relevant. IIUC, only anything that is DMA capable can be relevant.
Quoted 7 lines
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from
cb()to a new priority *before*VM_CHANGE_NOTIFY_PRI_SYNC(maybe calledVM_CHANGE_NOTIFY_PRI_SYNC_BEFOREor similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads fromVM_CHANGE_NOTIFY_PRI_HIGH.
IIUC we don't need a 4th step here, correct me otherwise, while the 3rd SYNC was only needed for VFIO's threaded approach.
Quoted 4 lines
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting
cb()handlers, so it should really run atVM_CHANGE_NOTIFY_PRI_LOW(belowPRI_NORMAL) for best parallelism.
Yes. VFIO device only need its SYNC / MEDIUM priority notifier be ordered with NORMAL notifiers, to make sure all VFIO devices switched to P2P state before the NORMAL notifiers.
Quoted 8 lines
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
We shouldn't justify a solution by "how many subsystems it touches".
I was suggesting to solve one problem at a time, but then prepare_cb() already does it. You pointed out there might be real regression on VM start path, I agreed.
I thought you wanted to fix all things, which I'm ok with and agree it is better, then it's unavoidable to touch all modules. But yes, we need to reproduce the problems and verify the fix.
The middle ground is we define the notifiers to suite for the solution we think will fix everything, but you only need to fix VFIO and leave the rest for later. We can discuss the problems in the cover letter or commit logs for future works on top. After all, in this case of VFIO to do the right things it does need three steps / notifiers.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 65 lines · Peter Xu
Quoted 47 lines · Maciej S. Szmigiero
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.Quoted 3 lines
And where the qdev tree depth will go in a call to
qemu_add_vm_change_state_handler_prio_full()- as an additional "depth" parameter?Yes. The rename change I mentioned above should have done so.
Quoted 11 lines
Quoted 10 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.
Quoted 6 lines
Quoted 3 lines
In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of itscb()handler separated and moved intoprepare_cb(),I thought this is required with whatever priority impl we will have, even if I don't think we need to fix it right now unless you wanted to..
Theoretically this depends on VM's PCI topology but indeed one can argue it's a "pre-existing condition".
Quoted 8 lines
Quoted 6 lines
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from
cb()at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,Is anyone using VFIO migration on PPC that you're aware of?
No, but at the other hand it's hard to be sure of that.
Also, see the next paragraphs about one or two additional possibly affected interrupt controllers.
Quoted 3 lines
If not, maybe
we don't need to worry too much for now. Meanwhile, this looks like an existing issue as well.
It's an existing issue *on the target* but for a slightly different reason:
reading VFIO uAPI it seems that a device is allowed to generate interrupts already in RUNNING_P2P state.
And this state is/was reached already after prepare_cb() handlers, so before that PPC interrupt controller handler running from cb() had chance to restore it.
However, it does not seem to be an existing issue *on the source* since there INTC cb() callback are currently always guaranteed to run after the VFIO device has already reached STOP_COPY and so was prevented from generating further interrupts.
Here, it would be a (at least theoretical) regression.
What's more, there seem to be one more interrupt controller in two variants/implementations that also do similar things on the source from their cb() handlers: ARM GICv3 basic flavor and ITS flavor.
Quoted 6 lines
For the fix whenever wanted, IIUC the intr controller should register both HIGH and NORMAL priority notifiers, then when VM stop, disable the controller only in NORMAL notifier, and when VM starts, enable the controller only in HIGH notifier (we could think about better names for the three steps, maybe it shouldn't be called as "priority" as well even if the executions will be strictly ordered).
As I wrote above, it looks like a device is already allowed to generate interrupts once it reaches RUNNING_P2P state.
Correspondingly, PRE_COPY_P2P only quiesces *peer-to-peer* DMA, there's no mention in the specs that it prevents MSI DMA traffic, let alone INTx interrupt generation.
In other words, to implement this properly an interrupt controller would need to have its start/restore handler called at some VERY_HIGH priority (called before VFIO device state change threads are launched) and have its stop/suspend handler called at a VERY_LOW priority (called after VFIO threads are collected) - this would be the equivalent of running these INTC cb() at prio 0 during VM stop now.
Assuming, of course that the interrupt controller actually needs to do some work on VM start to be able to accept interrupts and that it needs to do some saving work on VM stop which can't be done until no new interrupts could came.
Quoted 14 lines
Quoted 8 lines
* The postponed dirty memory logging stop handler also runs from
cb()at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?It's only stopping the tracking, results of the tracking shouldn't matter anymore on whether some DMA was trapped. It looks safe no matter where we put it.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from
cb()at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.Doesn't look relevant. IIUC, only anything that is DMA capable can be relevant.
It's relevant since a VFIO device could generate interrupts as soon as it is in RUNNING_P2P state.
However, this one here is start-side so it would be an existing issue rather than a regression.
Quoted 9 lines
Quoted 7 lines
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from
cb()to a new priority *before*VM_CHANGE_NOTIFY_PRI_SYNC(maybe calledVM_CHANGE_NOTIFY_PRI_SYNC_BEFOREor similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads fromVM_CHANGE_NOTIFY_PRI_HIGH.IIUC we don't need a 4th step here, correct me otherwise,
Depends whether we want to fix the existing issues or not. If not, then we don't need the extra priority level on the target.
However, I think we still need a possible extra level (VERY_LOW or similar) on the source to prevent possible regression with respect to the current code.
But that's a minor detail as I presume adding additional level would be essentially only a small data addition in some array and/or enum.
Quoted 2 lines
while the 3rd
SYNC was only needed for VFIO's threaded approach.
I assume that we're designing the "plumbing" that allows doing async/threaded VFIO state change safely here.
Quoted 28 lines
Quoted 4 lines
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting
cb()handlers, so it should really run atVM_CHANGE_NOTIFY_PRI_LOW(belowPRI_NORMAL) for best parallelism.Yes. VFIO device only need its SYNC / MEDIUM priority notifier be ordered with NORMAL notifiers, to make sure all VFIO devices switched to P2P state before the NORMAL notifiers.
Quoted 8 lines
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
We shouldn't justify a solution by "how many subsystems it touches".
I was suggesting to solve one problem at a time, but then
prepare_cb()already does it. You pointed out there might be real regression on VM start path, I agreed.I thought you wanted to fix all things, which I'm ok with and agree it is better, then it's unavoidable to touch all modules. But yes, we need to reproduce the problems and verify the fix.
Quoted 5 lines
The middle ground is we define the notifiers to suite for the solution we think will fix everything, but you only need to fix VFIO and leave the rest for later. We can discuss the problems in the cover letter or commit logs for future works on top. After all, in this case of VFIO to do the right things it does need three steps / notifiers.
I think if there are existing issues then it's probably okay just to document these for future reference.
However, I would be vary of creating any clear regression, especially on a hardware that I can't test.
Quoted 2 lines
Thanks,
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 101 lines · Maciej S. Szmigiero
Quoted 65 lines · Peter Xu
Quoted 47 lines · Maciej S. Szmigiero
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.Quoted 3 lines
And where the qdev tree depth will go in a call to
qemu_add_vm_change_state_handler_prio_full()- as an additional "depth" parameter?Yes. The rename change I mentioned above should have done so.
Quoted 11 lines
Quoted 10 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.Quoted 6 lines
Quoted 3 lines
In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of itscb()handler separated and moved intoprepare_cb(),I thought this is required with whatever priority impl we will have, even if I don't think we need to fix it right now unless you wanted to..
Theoretically this depends on VM's PCI topology but indeed one can argue it's a "pre-existing condition".
Quoted 8 lines
Quoted 6 lines
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from
cb()at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,Is anyone using VFIO migration on PPC that you're aware of?
No, but at the other hand it's hard to be sure of that.
Also, see the next paragraphs about one or two additional possibly affected interrupt controllers.
Quoted 3 lines
If not, maybe
we don't need to worry too much for now. Meanwhile, this looks like an existing issue as well.It's an existing issue *on the target* but for a slightly different reason:
reading VFIO uAPI it seems that a device is allowed to generate interrupts already inRUNNING_P2Pstate.
And this state is/was reached already afterprepare_cb()handlers, so before that PPC interrupt controller handler running fromcb()had chance to restore it.
Quoted 4 lines
However, it does not seem to be an existing issue *on the source* since there INTC
cb()callback are currently always guaranteed to run after the VFIO device has already reachedSTOP_COPYand so was prevented from generating further interrupts.
If we rely on intc have very small (or, 0 by default) depth, then yes.
Quoted 2 lines
Here, it would be a (at least theoretical) regression.
I don't yet get what is referred as "regression" here. If what you said is correct (which it seems true, that IRQ can be generated during PRECOPY_P2P and RUNNING_P2P), then dest QEMU should be broken already with VFIO being able to prepare_cb() converting devices to RUNNING_P2P.
That's not a regression by either your current patch or what I proposed previously. This is then a bug existed since prepare_cb(). Meanwhile, it was also an accident if it wasn't a bug even before it, after all intr chips are still using qemu_* helpers to register, where depth isn't accounted.
In reality maybe we should have done that; in case of an irq controller we should make sure the depth should be very small, at least smaller than the rest / normal devices to make sure its order of operation, otherwise even emulated devices seem to be prone to irq lost during vm start/stop or migration.
Quoted 12 lines
What's more, there seem to be one more interrupt controller in two variants/implementations that also do similar things on the source from their
cb()handlers: ARM GICv3 basic flavor and ITS flavor.Quoted 6 lines
For the fix whenever wanted, IIUC the intr controller should register both HIGH and NORMAL priority notifiers, then when VM stop, disable the controller only in NORMAL notifier, and when VM starts, enable the controller only in HIGH notifier (we could think about better names for the three steps, maybe it shouldn't be called as "priority" as well even if the executions will be strictly ordered).
Quoted 8 lines
As I wrote above, it looks like a device is already allowed to generate interrupts once it reaches
RUNNING_P2Pstate.
Correspondingly,PRE_COPY_P2Ponly quiesces *peer-to-peer* DMA, there's no mention in the specs that it prevents MSI DMA traffic, let alone INTx interrupt generation.In other words, to implement this properly an interrupt controller would need to have its start/restore handler called at some
VERY_HIGHpriority (called before
IIUC, my solution proposed should be fine as long as the INTR chip will split its vm state handler into two, "running" handler inserted into HIGH prority, but "!running" handler inserted into NORMAL priority, as I discussed above. This also depends on that we will notify each queue (one for each priority) with the depth in mind, so we will make sure to disable / quiesce the irq chip last (NORMAL, depth=0) when stop, and enable it first (HIGH, depth=0) when start.
NOTE: we have HIGH/SYNC/NORMAL, so NORMAL is the lowest priority, if we call it like that. We can rename them..
Quoted 7 lines
VFIO device state change threads are launched) and have its stop/suspend handler called at a
VERY_LOWpriority (called after VFIO threads are collected) - this would be the equivalent of running these INTCcb()at prio 0 during VM stop now.Assuming, of course that the interrupt controller actually needs to do some work on VM start to be able to accept interrupts and that it needs to do some saving work on VM stop which can't be done until no new interrupts could came.
Yes, I also didn't read into all irq chip code yet.
Quoted 37 lines
Quoted 15 lines
Quoted 9 lines
* The postponed dirty memory logging stop handler also runs from
cb()at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?It's only stopping the tracking, results of the tracking shouldn't matter anymore on whether some DMA was trapped. It looks safe no matter where we put it.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from
cb()at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.Doesn't look relevant. IIUC, only anything that is DMA capable can be relevant.
It's relevant since a VFIO device could generate interrupts as soon as it is in
RUNNING_P2Pstate.However, this one here is start-side so it would be an existing issue rather than a regression.
Quoted 8 lines
Quoted 7 lines
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from
cb()to a new priority *before*VM_CHANGE_NOTIFY_PRI_SYNC(maybe calledVM_CHANGE_NOTIFY_PRI_SYNC_BEFOREor similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads fromVM_CHANGE_NOTIFY_PRI_HIGH.
Quoted 7 lines
Quoted 1 line
IIUC we don't need a 4th step here, correct me otherwise,
Depends whether we want to fix the existing issues or not. If not, then we don't need the extra priority level on the target.
However, I think we still need a possible extra level (
VERY_LOWor similar) on the source to prevent possible regression with respect to the current code.
I'll re-comment after you read above. In short, we have 3 priorities (HIGH/SYNC/NORMAL, but again, prone to be renamed) but that's not the only way we order things: for each priority we will still notify with "depth" order, it's just that we will revert the depth order depending on running or not. irq chip issue is better solved with "depth" setting right all over the places, because it has a semantic of "depth" not real "priority" (e.g. turns off last, turns on first).
Quoted 44 lines
But that's a minor detail as I presume adding additional level would be essentially only a small data addition in some array and/or enum.
Quoted 2 lines
while the 3rd
SYNC was only needed for VFIO's threaded approach.I assume that we're designing the "plumbing" that allows doing async/threaded VFIO state change safely here.
Quoted 28 lines
Quoted 4 lines
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting
cb()handlers, so it should really run atVM_CHANGE_NOTIFY_PRI_LOW(belowPRI_NORMAL) for best parallelism.Yes. VFIO device only need its SYNC / MEDIUM priority notifier be ordered with NORMAL notifiers, to make sure all VFIO devices switched to P2P state before the NORMAL notifiers.
Quoted 8 lines
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
We shouldn't justify a solution by "how many subsystems it touches".
I was suggesting to solve one problem at a time, but then
prepare_cb()already does it. You pointed out there might be real regression on VM start path, I agreed.I thought you wanted to fix all things, which I'm ok with and agree it is better, then it's unavoidable to touch all modules. But yes, we need to reproduce the problems and verify the fix.
Quoted 5 lines
The middle ground is we define the notifiers to suite for the solution we think will fix everything, but you only need to fix VFIO and leave the rest for later. We can discuss the problems in the cover letter or commit logs for future works on top. After all, in this case of VFIO to do the right things it does need three steps / notifiers.
Quoted 5 lines
I think if there are existing issues then it's probably okay just to document these for future reference.
However, I would be vary of creating any clear regression, especially on a hardware that I can't test.
Understood, I have no good idea on testing. IIUC what needs to be tested for irq chips do not need VFIO involvement, maybe some regression test should be enough on either migration or even stop/cont. May need some help from arch developers indeed if we want some better coverage.
If you can think of anything that doesn't need to mess up the depth as patch 1 does (so if you want to reuse depth, make it a real "depth" would be fine, not a mixture of depth+prio), then maybe we can check that. I just can't think of anything else yet.
Thanks,
Quoted 7 lines
Quoted 3 lines
Thanks,
Thanks,
Maciej
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 102 lines · Peter Xu
Quoted 101 lines · Maciej S. Szmigiero
Quoted 65 lines · Peter Xu
Quoted 47 lines · Maciej S. Szmigiero
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.Quoted 3 lines
And where the qdev tree depth will go in a call to
qemu_add_vm_change_state_handler_prio_full()- as an additional "depth" parameter?Yes. The rename change I mentioned above should have done so.
Quoted 11 lines
Quoted 10 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.Quoted 6 lines
Quoted 3 lines
In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of itscb()handler separated and moved intoprepare_cb(),I thought this is required with whatever priority impl we will have, even if I don't think we need to fix it right now unless you wanted to..
Theoretically this depends on VM's PCI topology but indeed one can argue it's a "pre-existing condition".
Quoted 8 lines
Quoted 6 lines
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from
cb()at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,Is anyone using VFIO migration on PPC that you're aware of?
No, but at the other hand it's hard to be sure of that.
Also, see the next paragraphs about one or two additional possibly affected interrupt controllers.
Quoted 3 lines
If not, maybe
we don't need to worry too much for now. Meanwhile, this looks like an existing issue as well.It's an existing issue *on the target* but for a slightly different reason:
reading VFIO uAPI it seems that a device is allowed to generate interrupts already inRUNNING_P2Pstate.
And this state is/was reached already afterprepare_cb()handlers, so before that PPC interrupt controller handler running fromcb()had chance to restore it.
Quoted 6 lines
Quoted 4 lines
However, it does not seem to be an existing issue *on the source* since there INTC
cb()callback are currently always guaranteed to run after the VFIO device has already reachedSTOP_COPYand so was prevented from generating further interrupts.If we rely on intc have very small (or, 0 by default) depth, then yes.
These interrupt controllers don't use the vm state change handler registration functions that account for depth - qdev_add_vm_change_state_handler*().
Instead, they use qemu_add_vm_change_state_handler(), which explicitly uses/forces priority of 0.
I presume that's intentional to guarantee correct ordering - the commit that added ability to specify other priority values (60dbc5a1c517 - "add qemu_add_vm_change_state_handler_prio()") even said that this new priority value is "necessary when handlers depend on each other".
Quoted 10 lines
Quoted 2 lines
Here, it would be a (at least theoretical) regression.
I don't yet get what is referred as "regression" here. If what you said is correct (which it seems true, that IRQ can be generated during
PRECOPY_P2PandRUNNING_P2P), then dest QEMU should be broken already with VFIO being able toprepare_cb()converting devices toRUNNING_P2P.That's not a regression by either your current patch or what I proposed previously. This is then a bug existed since
prepare_cb().
By "regression" above I meant the situation that was described in the above paragraph, that is *on the source*.
I think there's confusion here because the situation with respect to prepare_cb() that you are describing above happens *on the target*:
as I wrote above on the target one can argue it's indeed a "pre-existing condition" - so *not* a regression.
Not so much on the source where the current code *guarantees* the correct ordering.
Also, the registered handler code is obviously doing very different thing on start vs stop so having no ordering requirements/issues on start does *not* imply there aren't any on stop.
A trivial example: the ARM GICv3 controllers handlers are even NOP on start/target but they still do certain sync on stop/source.
Quoted 3 lines
Meanwhile, it was also an accident if it wasn't a bug even before it, after all intr chips are still using qemu_* helpers to register, where depth isn't accounted.
But "depth" in the current code is a qdev thing, the core system/runstate.c only know "priority" and the priority of these INTC *stop* handlers guarantees they are run after all VFIO devices are in STOP_COPY state.
In other words, not using "depth" for registration does *not* mean unordered with respect to qdev handlers - actually quite the opposite.
Quoted 5 lines
In reality maybe we should have done that; in case of an irq controller we should make sure the depth should be very small, at least smaller than the rest / normal devices to make sure its order of operation, otherwise even emulated devices seem to be prone to irq lost during vm start/stop or migration.
The ordering is already correct on *on the source*, but as you wrote above, due to the existence of prepare_cb() handlers it might not be *on the target*.
Quoted 18 lines
Quoted 18 lines
What's more, there seem to be one more interrupt controller in two variants/implementations that also do similar things on the source from their
cb()handlers: ARM GICv3 basic flavor and ITS flavor.Quoted 6 lines
For the fix whenever wanted, IIUC the intr controller should register both HIGH and NORMAL priority notifiers, then when VM stop, disable the controller only in NORMAL notifier, and when VM starts, enable the controller only in HIGH notifier (we could think about better names for the three steps, maybe it shouldn't be called as "priority" as well even if the executions will be strictly ordered).
As I wrote above, it looks like a device is already allowed to generate interrupts once it reaches
RUNNING_P2Pstate.
Correspondingly,PRE_COPY_P2Ponly quiesces *peer-to-peer* DMA, there's no mention in the specs that it prevents MSI DMA traffic, let alone INTx interrupt generation.
Quoted 6 lines
Quoted 2 lines
In other words, to implement this properly an interrupt controller would need to have its start/restore handler called at some
VERY_HIGHpriority (called beforeIIUC, my solution proposed should be fine as long as the INTR chip will split its vm state handler into two, "running" handler inserted into HIGH prority,
This needs this INTC handler to be run before VFIO launches its device state changing threads so INTC handler should be registered with what will in the new code be called "depth" = 0 (or what is called "priority" in the old code) at HIGH priority, while VFIO threads will be launched at their qdev tree depth. So far so good.
Quoted 2 lines
but "!running" handler inserted into NORMAL priority, as I discussed above.
This needs this INTC handler to be run after VFIO collects its device state changing threads so again with "depth" = 0.
This would be fine - as long as VFIO thread collection *on stop/source* happens at some "depth" > 0 - see the caveat down below.
Quoted 4 lines
This also depends on that we will notify each queue (one
for each priority) with the depth in mind, so we will make sure to disable / quiesce the irq chip last (NORMAL, depth=0) when stop, and enable it first (HIGH, depth=0) when start.
Yeah.
Quoted 59 lines
NOTE: we have HIGH/SYNC/NORMAL, so NORMAL is the lowest priority, if we call it like that. We can rename them..
Quoted 7 lines
VFIO device state change threads are launched) and have its stop/suspend handler called at a
VERY_LOWpriority (called after VFIO threads are collected) - this would be the equivalent of running these INTCcb()at prio 0 during VM stop now.Assuming, of course that the interrupt controller actually needs to do some work on VM start to be able to accept interrupts and that it needs to do some saving work on VM stop which can't be done until no new interrupts could came.
Yes, I also didn't read into all irq chip code yet.
Quoted 37 lines
Quoted 15 lines
Quoted 9 lines
* The postponed dirty memory logging stop handler also runs from
cb()at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?It's only stopping the tracking, results of the tracking shouldn't matter anymore on whether some DMA was trapped. It looks safe no matter where we put it.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from
cb()at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.Doesn't look relevant. IIUC, only anything that is DMA capable can be relevant.
It's relevant since a VFIO device could generate interrupts as soon as it is in
RUNNING_P2Pstate.However, this one here is start-side so it would be an existing issue rather than a regression.
Quoted 8 lines
Quoted 7 lines
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from
cb()to a new priority *before*VM_CHANGE_NOTIFY_PRI_SYNC(maybe calledVM_CHANGE_NOTIFY_PRI_SYNC_BEFOREor similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads fromVM_CHANGE_NOTIFY_PRI_HIGH.Quoted 7 lines
Quoted 1 line
IIUC we don't need a 4th step here, correct me otherwise,
Depends whether we want to fix the existing issues or not. If not, then we don't need the extra priority level on the target.
However, I think we still need a possible extra level (
VERY_LOWor similar) on the source to prevent possible regression with respect to the current code.
Quoted 7 lines
I'll re-comment after you read above. In short, we have 3 priorities (HIGH/SYNC/NORMAL, but again, prone to be renamed) but that's not the only way we order things: for each priority we will still notify with "depth" order, it's just that we will revert the depth order depending on running or not. irq chip issue is better solved with "depth" setting right all over the places, because it has a semantic of "depth" not real "priority" (e.g. turns off last, turns on first).
As I wrote above, if we do INTC at "depth" = 0 then we'll want to collect VFIO threads *on stop/source* at "depth" > 0.
At the same time, we want to have these threads running in parallel with other cb() handlers at NORMAL priority for as long as possible.
Here, it seems that just using "depth" of 1 for VFIO threads collection *on stop/source* seems to work since every other existing cb() handler seems to sit at a minimum "depth" of 2-3.
However, on the *target/start* the call order vs "depth" is inverted, so ordering VFIO threads collection after every other existing cb() handler needs them registered at INT_MAX or other very high depth.
Quoted 56 lines
Quoted 45 lines
But that's a minor detail as I presume adding additional level would be essentially only a small data addition in some array and/or enum.
Quoted 2 lines
while the 3rd
SYNC was only needed for VFIO's threaded approach.I assume that we're designing the "plumbing" that allows doing async/threaded VFIO state change safely here.
Quoted 28 lines
Quoted 4 lines
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting
cb()handlers, so it should really run atVM_CHANGE_NOTIFY_PRI_LOW(belowPRI_NORMAL) for best parallelism.Yes. VFIO device only need its SYNC / MEDIUM priority notifier be ordered with NORMAL notifiers, to make sure all VFIO devices switched to P2P state before the NORMAL notifiers.
Quoted 8 lines
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
We shouldn't justify a solution by "how many subsystems it touches".
I was suggesting to solve one problem at a time, but then
prepare_cb()already does it. You pointed out there might be real regression on VM start path, I agreed.I thought you wanted to fix all things, which I'm ok with and agree it is better, then it's unavoidable to touch all modules. But yes, we need to reproduce the problems and verify the fix.
Quoted 5 lines
The middle ground is we define the notifiers to suite for the solution we think will fix everything, but you only need to fix VFIO and leave the rest for later. We can discuss the problems in the cover letter or commit logs for future works on top. After all, in this case of VFIO to do the right things it does need three steps / notifiers.
Quoted 5 lines
I think if there are existing issues then it's probably okay just to document these for future reference.
However, I would be vary of creating any clear regression, especially on a hardware that I can't test.
Understood, I have no good idea on testing. IIUC what needs to be tested for irq chips do not need VFIO involvement, maybe some regression test should be enough on either migration or even stop/cont. May need some help from arch developers indeed if we want some better coverage.
Quoted 4 lines
If you can think of anything that doesn't need to mess up the depth as patch 1 does (so if you want to reuse depth, make it a real "depth" would be fine, not a mixture of depth+prio), then maybe we can check that. I just can't think of anything else yet.
Overall, I think that the design that we discussed above is workable, barring some unforeseen troubles with its implementation or runtime testing issues.
I also feel that at this point it will be much better to prepare a v2 and then tweak it as/if necessary.
Agree?
Thanks,
Maciej
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 114 lines · Maciej S. Szmigiero
Quoted 102 lines · Peter Xu
Quoted 101 lines · Maciej S. Szmigiero
Quoted 65 lines · Peter Xu
Quoted 47 lines · Maciej S. Szmigiero
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.Quoted 3 lines
And where the qdev tree depth will go in a call to
qemu_add_vm_change_state_handler_prio_full()- as an additional "depth" parameter?Yes. The rename change I mentioned above should have done so.
Quoted 11 lines
Quoted 10 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.Quoted 6 lines
Quoted 3 lines
In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of itscb()handler separated and moved intoprepare_cb(),I thought this is required with whatever priority impl we will have, even if I don't think we need to fix it right now unless you wanted to..
Theoretically this depends on VM's PCI topology but indeed one can argue it's a "pre-existing condition".
Quoted 8 lines
Quoted 6 lines
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from
cb()at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,Is anyone using VFIO migration on PPC that you're aware of?
No, but at the other hand it's hard to be sure of that.
Also, see the next paragraphs about one or two additional possibly affected interrupt controllers.
Quoted 3 lines
If not, maybe
we don't need to worry too much for now. Meanwhile, this looks like an existing issue as well.It's an existing issue *on the target* but for a slightly different reason:
reading VFIO uAPI it seems that a device is allowed to generate interrupts already inRUNNING_P2Pstate.
And this state is/was reached already afterprepare_cb()handlers, so before that PPC interrupt controller handler running fromcb()had chance to restore it.Quoted 6 lines
Quoted 4 lines
However, it does not seem to be an existing issue *on the source* since there INTC
cb()callback are currently always guaranteed to run after the VFIO device has already reachedSTOP_COPYand so was prevented from generating further interrupts.If we rely on intc have very small (or, 0 by default) depth, then yes.
These interrupt controllers don't use the vm state change handler registration functions that account for depth - qdev_add_vm_change_state_handler*().
Quoted 7 lines
Instead, they use
qemu_add_vm_change_state_handler(), which explicitly uses/forces priority of 0.I presume that's intentional to guarantee correct ordering - the commit that added ability to specify other priority values (
60dbc5a1c517- "addqemu_add_vm_change_state_handler_prio()") even said that this new priority value is "necessary when handlers depend on each other".
It was for ordering scsi bus and virtio device only at that point:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 32 lines
Quoted 11 lines
Quoted 3 lines
Here, it would be a (at least theoretical) regression.
I don't yet get what is referred as "regression" here. If what you said is correct (which it seems true, that IRQ can be generated during
PRECOPY_P2PandRUNNING_P2P), then dest QEMU should be broken already with VFIO being able toprepare_cb()converting devices toRUNNING_P2P.That's not a regression by either your current patch or what I proposed previously. This is then a bug existed since
prepare_cb().By "regression" above I meant the situation that was described in the above paragraph, that is *on the source*.
I think there's confusion here because the situation with respect to
prepare_cb()that you are describing above happens *on the target*:
as I wrote above on the target one can argue it's indeed a "pre-existing condition" - so *not* a regression.
Not so much on the source where the current code *guarantees* the correct ordering.Also, the registered handler code is obviously doing very different thing on start vs stop so having no ordering requirements/issues on start does *not* imply there aren't any on stop.
A trivial example: the ARM GICv3 controllers handlers are even NOP on start/target but they still do certain sync on stop/source.Quoted 3 lines
Meanwhile, it was also an accident if it wasn't a bug even before it, after all intr chips are still using qemu_* helpers to register, where depth isn't accounted.
Quoted 6 lines
But "depth" in the current code is a qdev thing, the core
system/runstate.conly know "priority" and the priority of these INTC *stop* handlers guarantees they are run after all VFIO devices are inSTOP_COPYstate.In other words, not using "depth" for registration does *not* mean unordered with respect to qdev handlers - actually quite the opposite.
IIUC if irq chips want to order against any device that uses irqs, it should at least have all of them registered via qdev API to provide such depth info. My feeling is the irq chip order isn't intentional, but I didn't check further.
Quoted 127 lines
Quoted 6 lines
In reality maybe we should have done that; in case of an irq controller we should make sure the depth should be very small, at least smaller than the rest / normal devices to make sure its order of operation, otherwise even emulated devices seem to be prone to irq lost during vm start/stop or migration.
The ordering is already correct on *on the source*, but as you wrote above, due to the existence of
prepare_cb()handlers it might not be *on the target*.Quoted 18 lines
Quoted 18 lines
What's more, there seem to be one more interrupt controller in two variants/implementations that also do similar things on the source from their
cb()handlers: ARM GICv3 basic flavor and ITS flavor.Quoted 6 lines
For the fix whenever wanted, IIUC the intr controller should register both HIGH and NORMAL priority notifiers, then when VM stop, disable the controller only in NORMAL notifier, and when VM starts, enable the controller only in HIGH notifier (we could think about better names for the three steps, maybe it shouldn't be called as "priority" as well even if the executions will be strictly ordered).
As I wrote above, it looks like a device is already allowed to generate interrupts once it reaches
RUNNING_P2Pstate.
Correspondingly,PRE_COPY_P2Ponly quiesces *peer-to-peer* DMA, there's no mention in the specs that it prevents MSI DMA traffic, let alone INTx interrupt generation.Quoted 6 lines
Quoted 2 lines
In other words, to implement this properly an interrupt controller would need to have its start/restore handler called at some
VERY_HIGHpriority (called beforeIIUC, my solution proposed should be fine as long as the INTR chip will split its vm state handler into two, "running" handler inserted into HIGH prority,
This needs this INTC handler to be run before VFIO launches its device state changing threads so INTC handler should be registered with what will in the new code be called "depth" = 0 (or what is called "priority" in the old code) at HIGH priority, while VFIO threads will be launched at their qdev tree depth. So far so good.
Quoted 2 lines
but "!running" handler inserted into NORMAL priority, as I discussed above.
This needs this INTC handler to be run after VFIO collects its device state changing threads so again with "depth" = 0.
This would be fine - as long as VFIO thread collection *on stop/source* happens at some "depth" > 0 - see the caveat down below.
Quoted 4 lines
This also depends on that we will notify each queue (one
for each priority) with the depth in mind, so we will make sure to disable / quiesce the irq chip last (NORMAL, depth=0) when stop, and enable it first (HIGH, depth=0) when start.Yeah.
Quoted 59 lines
NOTE: we have HIGH/SYNC/NORMAL, so NORMAL is the lowest priority, if we call it like that. We can rename them..
Quoted 7 lines
VFIO device state change threads are launched) and have its stop/suspend handler called at a
VERY_LOWpriority (called after VFIO threads are collected) - this would be the equivalent of running these INTCcb()at prio 0 during VM stop now.Assuming, of course that the interrupt controller actually needs to do some work on VM start to be able to accept interrupts and that it needs to do some saving work on VM stop which can't be done until no new interrupts could came.
Yes, I also didn't read into all irq chip code yet.
Quoted 37 lines
Quoted 15 lines
Quoted 9 lines
* The postponed dirty memory logging stop handler also runs from
cb()at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?It's only stopping the tracking, results of the tracking shouldn't matter anymore on whether some DMA was trapped. It looks safe no matter where we put it.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from
cb()at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.Doesn't look relevant. IIUC, only anything that is DMA capable can be relevant.
It's relevant since a VFIO device could generate interrupts as soon as it is in
RUNNING_P2Pstate.However, this one here is start-side so it would be an existing issue rather than a regression.
Quoted 8 lines
Quoted 7 lines
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from
cb()to a new priority *before*VM_CHANGE_NOTIFY_PRI_SYNC(maybe calledVM_CHANGE_NOTIFY_PRI_SYNC_BEFOREor similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads fromVM_CHANGE_NOTIFY_PRI_HIGH.Quoted 7 lines
Quoted 1 line
IIUC we don't need a 4th step here, correct me otherwise,
Depends whether we want to fix the existing issues or not. If not, then we don't need the extra priority level on the target.
However, I think we still need a possible extra level (
VERY_LOWor similar) on the source to prevent possible regression with respect to the current code.Quoted 7 lines
I'll re-comment after you read above. In short, we have 3 priorities (HIGH/SYNC/NORMAL, but again, prone to be renamed) but that's not the only way we order things: for each priority we will still notify with "depth" order, it's just that we will revert the depth order depending on running or not. irq chip issue is better solved with "depth" setting right all over the places, because it has a semantic of "depth" not real "priority" (e.g. turns off last, turns on first).
As I wrote above, if we do INTC at "depth" = 0 then we'll want to collect VFIO threads *on stop/source* at "depth" > 0.
Quoted 6 lines
At the same time, we want to have these threads running in parallel with other
cb()handlers at NORMAL priority for as long as possible.Here, it seems that just using "depth" of 1 for VFIO threads collection *on stop/source* seems to work since every other existing
cb()handler seems to sit at a minimum "depth" of 2-3.
Why VFIO needs to adjust "depth"? Can't it always be the depth of qdev tree?
On source side:
- Phase 1 HIGH should turn all devices (VFIO or emulated) to be into P2P state. Things like buses or irq chip not included; they need to be ordered hierachically / properly. Other that that I don't see why VFIO cares about other devices if it's only about "do not initiate any more DMA at this stage".
- Phase 2 SYNC vfio should be the only user sync with HIGH's threads making sure P2P conversions complete for vfios, then kick off the 2nd phase for all vfio devices.
- Phase 3 NORMAL should turn all devices STOPPED. Again, I don't see why we need specific ordering here amoung all the normal devices.
Quoted 65 lines
However, on the *target/start* the call order vs "depth" is inverted, so ordering VFIO threads collection after every other existing
cb()handler needs them registered atINT_MAXor other very high depth.Quoted 56 lines
Quoted 45 lines
But that's a minor detail as I presume adding additional level would be essentially only a small data addition in some array and/or enum.
Quoted 2 lines
while the 3rd
SYNC was only needed for VFIO's threaded approach.I assume that we're designing the "plumbing" that allows doing async/threaded VFIO state change safely here.
Quoted 28 lines
Quoted 4 lines
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting
cb()handlers, so it should really run atVM_CHANGE_NOTIFY_PRI_LOW(belowPRI_NORMAL) for best parallelism.Yes. VFIO device only need its SYNC / MEDIUM priority notifier be ordered with NORMAL notifiers, to make sure all VFIO devices switched to P2P state before the NORMAL notifiers.
Quoted 8 lines
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
We shouldn't justify a solution by "how many subsystems it touches".
I was suggesting to solve one problem at a time, but then
prepare_cb()already does it. You pointed out there might be real regression on VM start path, I agreed.I thought you wanted to fix all things, which I'm ok with and agree it is better, then it's unavoidable to touch all modules. But yes, we need to reproduce the problems and verify the fix.
Quoted 5 lines
The middle ground is we define the notifiers to suite for the solution we think will fix everything, but you only need to fix VFIO and leave the rest for later. We can discuss the problems in the cover letter or commit logs for future works on top. After all, in this case of VFIO to do the right things it does need three steps / notifiers.
Quoted 5 lines
I think if there are existing issues then it's probably okay just to document these for future reference.
However, I would be vary of creating any clear regression, especially on a hardware that I can't test.
Understood, I have no good idea on testing. IIUC what needs to be tested for irq chips do not need VFIO involvement, maybe some regression test should be enough on either migration or even stop/cont. May need some help from arch developers indeed if we want some better coverage.
Quoted 4 lines
If you can think of anything that doesn't need to mess up the depth as patch 1 does (so if you want to reuse depth, make it a real "depth" would be fine, not a mixture of depth+prio), then maybe we can check that. I just can't think of anything else yet.
Quoted 7 lines
Overall, I think that the design that we discussed above is workable, barring some unforeseen troubles with its implementation or runtime testing issues.
I also feel that at this point it will be much better to prepare a v2 and then tweak it as/if necessary.
Agree?
I always prefer having some code and do some real tests to support discussions, rather than pure discussions. Please send it whenever you feel comfortable. E.g. I treat the irq issues still theoretical until we can provide a reproducer.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 2/2] vfio/migration: Parallelize device state transitions
Quoted 163 lines · Peter Xu
Quoted 114 lines · Maciej S. Szmigiero
Quoted 102 lines · Peter Xu
Quoted 101 lines · Maciej S. Szmigiero
Quoted 65 lines · Peter Xu
Quoted 47 lines · Maciej S. Szmigiero
Quoted 5 lines · Peter Xu
Quoted 4 lines · Maciej S. Szmigiero
Inside the VFIO code such sync point for VFIO devices could/should indeed be created, but then it's not available for these other (non-VFIO) devices for the purpose of avoiding the regression from the previous paragraph.
Quoted 7 lines
Quoted 3 lines
However, replacing the order priority/adjustment mechanism from patch 1 with the "pre" and "post" handlers I described above would avoid having that regression (and help fix the vhost-net case too).
I'm not sure I fully get the pre/post handlers idea, I think it sounds working, but in all cases I want to decouple it with patch 1: I don't think it requires patch 1, am I right?
It wouldn't require the *current* patch 1 but it would need to be implemented in some preparatory patch so it would effectively be a new patch 1 (or a set of patches replacing patch 1).
Assuming that we would go this route instead of the one you described below.
Quoted 6 lines
Now if we want to avoid this "theoretical regression" and solve both things together.. I think we may need to refactor the notifiers mechanism.
Firstly, I hope we're on the same page that essentially
prepare_cb()is the priority mechanism here, we only have HIGH and NORMAL priority, wherecb()is the NORMAL priority.Yeah, I think the current situation could be described this way.
Quoted 10 lines
We also need to persist depth concept per-notifier, I think we should start by renaming VMChangeStateEntry.priority to depth, add a comment explaining it (on different order of invokations on VM start/shutdown).
But then, I don't think we need anything as complex as pre/post hooks with hashes. I think you're right then we need SYNC point which can be essentially a priority notifier that is in the middle of HIGH and NORMAL.
Hence, I want to see if below should be the easiest:
Quoted 7 lines
- Rename VMChangeStateEntry.priority to depth
- Normalize
prepare_cb()intoVM_CHANGE_NOTIFY_PRI_HIGH, makingcb()to be NORMAL, OTOH. With this,prepare_cb()needs to be registered separately withqemu_add_vm_change_state_handler_prio_full(). The function now should dropprepare_cb()but instead take a real "priority" value of VM_CHANGE_NOTIFY_PRI_*.Quoted 3 lines
And where the qdev tree depth will go in a call to
qemu_add_vm_change_state_handler_prio_full()- as an additional "depth" parameter?Yes. The rename change I mentioned above should have done so.
Quoted 11 lines
Quoted 10 lines
- Introduce
VM_CHANGE_NOTIFY_PRI_SYNC, in the middle of PREPARE / NORMAL.- VFIO can now register its 3rd notifier against SYNC.
All rest devices will need to shift part of its logic into
PRI_HIGHto either quiesce DMA or enable the backend to accept DMA (when on dest QEMU). None of them will need SYNC only if they'll also switch to an async thread model.Quoted 6 lines
Quoted 3 lines
In principle, this *should* work but with some caveats:
* vhost-net would need the DMA-stopping part of itscb()handler separated and moved intoprepare_cb(),I thought this is required with whatever priority impl we will have, even if I don't think we need to fix it right now unless you wanted to..
Theoretically this depends on VM's PCI topology but indeed one can argue it's a "pre-existing condition".
Quoted 8 lines
Quoted 6 lines
* There's some restore code in "PowerPC sPAPR XIVE interrupt controller" running from
cb()at prio 0, so originally always completing before VFIO device was switched into RUNNING state.
I'm not 100% sure if it's okay to make VFIO device reach this state before that interrupt controller restore code finishes,Is anyone using VFIO migration on PPC that you're aware of?
No, but at the other hand it's hard to be sure of that.
Also, see the next paragraphs about one or two additional possibly affected interrupt controllers.
Quoted 3 lines
If not, maybe
we don't need to worry too much for now. Meanwhile, this looks like an existing issue as well.It's an existing issue *on the target* but for a slightly different reason:
reading VFIO uAPI it seems that a device is allowed to generate interrupts already inRUNNING_P2Pstate.
And this state is/was reached already afterprepare_cb()handlers, so before that PPC interrupt controller handler running fromcb()had chance to restore it.Quoted 6 lines
Quoted 4 lines
However, it does not seem to be an existing issue *on the source* since there INTC
cb()callback are currently always guaranteed to run after the VFIO device has already reachedSTOP_COPYand so was prevented from generating further interrupts.If we rely on intc have very small (or, 0 by default) depth, then yes.
These interrupt controllers don't use the vm state change handler registration functions that account for depth - qdev_add_vm_change_state_handler*().
Quoted 7 lines
Instead, they use
qemu_add_vm_change_state_handler(), which explicitly uses/forces priority of 0.I presume that's intentional to guarantee correct ordering - the commit that added ability to specify other priority values (
60dbc5a1c517- "addqemu_add_vm_change_state_handler_prio()") even said that this new priority value is "necessary when handlers depend on each other".It was for ordering scsi bus and virtio device only at that point:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 36 lines
Quoted 11 lines
Quoted 3 lines
Here, it would be a (at least theoretical) regression.
I don't yet get what is referred as "regression" here. If what you said is correct (which it seems true, that IRQ can be generated during
PRECOPY_P2PandRUNNING_P2P), then dest QEMU should be broken already with VFIO being able toprepare_cb()converting devices toRUNNING_P2P.That's not a regression by either your current patch or what I proposed previously. This is then a bug existed since
prepare_cb().By "regression" above I meant the situation that was described in the above paragraph, that is *on the source*.
I think there's confusion here because the situation with respect to
prepare_cb()that you are describing above happens *on the target*:
as I wrote above on the target one can argue it's indeed a "pre-existing condition" - so *not* a regression.
Not so much on the source where the current code *guarantees* the correct ordering.Also, the registered handler code is obviously doing very different thing on start vs stop so having no ordering requirements/issues on start does *not* imply there aren't any on stop.
A trivial example: the ARM GICv3 controllers handlers are even NOP on start/target but they still do certain sync on stop/source.Quoted 3 lines
Meanwhile, it was also an accident if it wasn't a bug even before it, after all intr chips are still using qemu_* helpers to register, where depth isn't accounted.
But "depth" in the current code is a qdev thing, the core
system/runstate.conly know "priority" and the priority of these INTC *stop* handlers guarantees they are run after all VFIO devices are inSTOP_COPYstate.
Quoted 137 lines
Quoted 2 lines
In other words, not using "depth" for registration does *not* mean unordered with respect to qdev handlers - actually quite the opposite.
IIUC if irq chips want to order against any device that uses irqs, it should at least have all of them registered via qdev API to provide such depth info. My feeling is the irq chip order isn't intentional, but I didn't check further.
Quoted 129 lines
Quoted 6 lines
In reality maybe we should have done that; in case of an irq controller we should make sure the depth should be very small, at least smaller than the rest / normal devices to make sure its order of operation, otherwise even emulated devices seem to be prone to irq lost during vm start/stop or migration.
The ordering is already correct on *on the source*, but as you wrote above, due to the existence of
prepare_cb()handlers it might not be *on the target*.Quoted 18 lines
Quoted 18 lines
What's more, there seem to be one more interrupt controller in two variants/implementations that also do similar things on the source from their
cb()handlers: ARM GICv3 basic flavor and ITS flavor.Quoted 6 lines
For the fix whenever wanted, IIUC the intr controller should register both HIGH and NORMAL priority notifiers, then when VM stop, disable the controller only in NORMAL notifier, and when VM starts, enable the controller only in HIGH notifier (we could think about better names for the three steps, maybe it shouldn't be called as "priority" as well even if the executions will be strictly ordered).
As I wrote above, it looks like a device is already allowed to generate interrupts once it reaches
RUNNING_P2Pstate.
Correspondingly,PRE_COPY_P2Ponly quiesces *peer-to-peer* DMA, there's no mention in the specs that it prevents MSI DMA traffic, let alone INTx interrupt generation.Quoted 6 lines
Quoted 2 lines
In other words, to implement this properly an interrupt controller would need to have its start/restore handler called at some
VERY_HIGHpriority (called beforeIIUC, my solution proposed should be fine as long as the INTR chip will split its vm state handler into two, "running" handler inserted into HIGH prority,
This needs this INTC handler to be run before VFIO launches its device state changing threads so INTC handler should be registered with what will in the new code be called "depth" = 0 (or what is called "priority" in the old code) at HIGH priority, while VFIO threads will be launched at their qdev tree depth. So far so good.
Quoted 2 lines
but "!running" handler inserted into NORMAL priority, as I discussed above.
This needs this INTC handler to be run after VFIO collects its device state changing threads so again with "depth" = 0.
This would be fine - as long as VFIO thread collection *on stop/source* happens at some "depth" > 0 - see the caveat down below.
Quoted 4 lines
This also depends on that we will notify each queue (one
for each priority) with the depth in mind, so we will make sure to disable / quiesce the irq chip last (NORMAL, depth=0) when stop, and enable it first (HIGH, depth=0) when start.Yeah.
Quoted 59 lines
NOTE: we have HIGH/SYNC/NORMAL, so NORMAL is the lowest priority, if we call it like that. We can rename them..
Quoted 7 lines
VFIO device state change threads are launched) and have its stop/suspend handler called at a
VERY_LOWpriority (called after VFIO threads are collected) - this would be the equivalent of running these INTCcb()at prio 0 during VM stop now.Assuming, of course that the interrupt controller actually needs to do some work on VM start to be able to accept interrupts and that it needs to do some saving work on VM stop which can't be done until no new interrupts could came.
Yes, I also didn't read into all irq chip code yet.
Quoted 37 lines
Quoted 15 lines
Quoted 9 lines
* The postponed dirty memory logging stop handler also runs from
cb()at prio 0, so currently always finishes before VFIO device was switched into RUNNING state.
This handler also stops DMA logging for VFIO device or its container, but here I presume it should be safe to do so while the VFIO device has already started dirtying memory even though this couldn't happen in the existing code?It's only stopping the tracking, results of the tracking shouldn't matter anymore on whether some DMA was trapped. It looks safe no matter where we put it.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
* There's some sync/setup of x86 vAPIC state (for 32-bit guests?) also running from
cb()at prio 0.
Not sure if this needs to finish before VFIO device gets switched into RUNNING state.Doesn't look relevant. IIUC, only anything that is DMA capable can be relevant.
It's relevant since a VFIO device could generate interrupts as soon as it is in
RUNNING_P2Pstate.However, this one here is start-side so it would be an existing issue rather than a regression.
Quoted 8 lines
Quoted 7 lines
These 3 possible issues above (besides vhost-net move/refactor) could be theoretically worked around by moving these callbacks from
cb()to a new priority *before*VM_CHANGE_NOTIFY_PRI_SYNC(maybe calledVM_CHANGE_NOTIFY_PRI_SYNC_BEFOREor similar) as I presume they won't need the VFIO device to reach DMA-accepting state but at the same time they shouldn't delay the launch of VFIO device state changing threads fromVM_CHANGE_NOTIFY_PRI_HIGH.Quoted 7 lines
Quoted 1 line
IIUC we don't need a 4th step here, correct me otherwise,
Depends whether we want to fix the existing issues or not. If not, then we don't need the extra priority level on the target.
However, I think we still need a possible extra level (
VERY_LOWor similar) on the source to prevent possible regression with respect to the current code.Quoted 7 lines
I'll re-comment after you read above. In short, we have 3 priorities (HIGH/SYNC/NORMAL, but again, prone to be renamed) but that's not the only way we order things: for each priority we will still notify with "depth" order, it's just that we will revert the depth order depending on running or not. irq chip issue is better solved with "depth" setting right all over the places, because it has a semantic of "depth" not real "priority" (e.g. turns off last, turns on first).
As I wrote above, if we do INTC at "depth" = 0 then we'll want to collect VFIO threads *on stop/source* at "depth" > 0.
At the same time, we want to have these threads running in parallel with other
cb()handlers at NORMAL priority for as long as possible.
[1] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 15 lines
Quoted 3 lines
Here, it seems that just using "depth" of 1 for VFIO threads collection *on stop/source* seems to work since every other existing
cb()handler seems to sit at a minimum "depth" of 2-3.Why VFIO needs to adjust "depth"? Can't it always be the depth of qdev tree?
On source side:
- Phase 1 HIGH should turn all devices (VFIO or emulated) to be into P2P state. Things like buses or irq chip not included; they need to be ordered hierachically / properly. Other that that I don't see why VFIO cares about other devices if it's only about "do not initiate any more DMA at this stage".
Quoted 6 lines
- Phase 2 SYNC vfio should be the only user sync with HIGH's threads making sure P2P conversions complete for vfios, then kick off the 2nd phase for all vfio devices.
- Phase 3 NORMAL should turn all devices STOPPED. Again, I don't see why we need specific ordering here amoung all the normal devices.
The key sentence is the one marked as [1] above.
VFIO does not *need* collection threads to be at a different depth but *benefits* from them being collected only after other cb() handlers at this priority since this allows running these other handlers in parallel with performing VFIO device state change (in other words, *not* serializing with them).
This is the case both on the source AND on the target, just due to the fact that handlers are run in a different order on start vs stop the VFIO "depth" on start vs stop also differs.
However, the exact depth value is that kind of detail that can be discussed during the review of v2, when the whole design is visible.
Quoted 74 lines
Quoted 66 lines
However, on the *target/start* the call order vs "depth" is inverted, so ordering VFIO threads collection after every other existing
cb()handler needs them registered atINT_MAXor other very high depth.Quoted 56 lines
Quoted 45 lines
But that's a minor detail as I presume adding additional level would be essentially only a small data addition in some array and/or enum.
Quoted 2 lines
while the 3rd
SYNC was only needed for VFIO's threaded approach.I assume that we're designing the "plumbing" that allows doing async/threaded VFIO state change safely here.
Quoted 28 lines
Quoted 4 lines
Also, the final VFIO thread joining/collection does *not* need to be ordered with exiting
cb()handlers, so it should really run atVM_CHANGE_NOTIFY_PRI_LOW(belowPRI_NORMAL) for best parallelism.Yes. VFIO device only need its SYNC / MEDIUM priority notifier be ordered with NORMAL notifiers, to make sure all VFIO devices switched to P2P state before the NORMAL notifiers.
Quoted 8 lines
The disadvantages of this overall design are that it would need coordination between different sub-maintainers, increase the "blast radius" of the patch set to different parts of QEMU code, and so obviously would be more regression-risky than the aforementioned "pre" and "post" handlers design.
Especially that probably few people would be able to test, for example, the PPC XIVE case with VFIO.
We shouldn't justify a solution by "how many subsystems it touches".
I was suggesting to solve one problem at a time, but then
prepare_cb()already does it. You pointed out there might be real regression on VM start path, I agreed.I thought you wanted to fix all things, which I'm ok with and agree it is better, then it's unavoidable to touch all modules. But yes, we need to reproduce the problems and verify the fix.
Quoted 5 lines
The middle ground is we define the notifiers to suite for the solution we think will fix everything, but you only need to fix VFIO and leave the rest for later. We can discuss the problems in the cover letter or commit logs for future works on top. After all, in this case of VFIO to do the right things it does need three steps / notifiers.
Quoted 5 lines
I think if there are existing issues then it's probably okay just to document these for future reference.
However, I would be vary of creating any clear regression, especially on a hardware that I can't test.
Understood, I have no good idea on testing. IIUC what needs to be tested for irq chips do not need VFIO involvement, maybe some regression test should be enough on either migration or even stop/cont. May need some help from arch developers indeed if we want some better coverage.
Quoted 4 lines
If you can think of anything that doesn't need to mess up the depth as patch 1 does (so if you want to reuse depth, make it a real "depth" would be fine, not a mixture of depth+prio), then maybe we can check that. I just can't think of anything else yet.
Quoted 7 lines
Overall, I think that the design that we discussed above is workable, barring some unforeseen troubles with its implementation or runtime testing issues.
I also feel that at this point it will be much better to prepare a v2 and then tweak it as/if necessary.
Agree?
Quoted 7 lines
I always prefer having some code and do some real tests to support discussions, rather than pure discussions. Please send it whenever you feel comfortable. E.g. I treat the irq issues still theoretical until we can provide a reproducer.
Thanks,
Thanks,
Maciej