VFIO device migration parallel state transitions
v2 cover VFIO device migration parallel state transitions
From: "Maciej S. Szmigiero" <[email protected]>
This is an updated v2 patch series of the v1 series located at:
https://lore.kernel.org/qemu-devel/[email protected]/
v1 also contains detailed description and benchmarks of downtime performance improvements brought by this parallelization.
The main change in v2 is that the VFIO parallel state changing threads are implemented/managed differently.
Specifically, a design based on one that Peter has suggested during the review of v1 in [1] was implemented:
* The "priority" of existing VM change state handlers is renamed "depth",
* The now-freed "priority" term is used to implement an explicit, three-level VM_CHANGE_STATE_PRI_* priority system:
NORMAL priority is the old cb(), HIGH priority is the old prepare_cb(), while a new SYNC priority level is introduced between them.
* Devices that can produce DMA have their VM stop handlers to HIGH level to stop them before VFIO devices start reaching states not capable of accepting incoming DMA.
This affected virtio, net and audio VM state change handlers.
* Interrupt controllers have their handlers moved so they are started before VFIO devices at VM_CHANGE_STATE_PRI_HIGH on VM start and stopped only after VFIO devices at VM_CHANGE_STATE_PRI_NORMAL.
To further reinforce this ordering and make sure that it isn't lost in some future code refactor, an explicit VMChangeStateHandlerDepth system for these controllers is introduced - much like migration already had explicit MigrationPriority values for them.
* The VFIO device state changing threads are now launched and joined just once during VM start or VM stop sequence, instead of having been launched and joined per each transition.
* The priority/qdev depth adjustment mechanism from v1 and an extra layer of indirection for VFIO VM change state handlers are no longer required - so were removed.
Note: the PPC and ARM changes are compile-tested only. However, they are almost trivial so no significant risk is expected from that.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
runstate: Rename VM change state handlers "priority" to "depth"
runstate: Introduce VM change state handlers explicit priority levels
runstate: Add an intermediate VM change state handlers SYNC priority hw/vfio: Introduce VFIOPCIDeviceClass
vfio/migration: Parallelize device state transitions spapr/xive: Split VM state change handler into start and stop parts
runstate: Start/stop interrupt controllers before/after ordinary
devices
audio: Split VM state change handler into start and stop parts
audio: Make sure to stop audio before VFIO gets non-DMA-capable
net: Split VM state change handler into start and stop parts
net: Make sure to stop net before VFIO gets non-DMA-capable
virtio: Split VM state change handler into start and stop parts
virtio: Make sure to stop virtio before VFIO gets non-DMA-capable
audio/audio-mixeng-be.c | 42 ++++-- audio/audio_int.h | 3 +- hw/core/vm-change-state-handler.c | 16 ++- hw/i386/vapic.c | 5 +- hw/intc/arm_gicv3_its_kvm.c | 4 +- hw/intc/arm_gicv3_kvm.c | 4 +- hw/intc/spapr_xive_kvm.c | 80 ++++++----- hw/vfio/migration.c | 230 +++++++++++++++++++++++++++++- hw/vfio/pci.c | 7 + hw/vfio/pci.h | 10 +- hw/vfio/trace-events | 2 + hw/vfio/vfio-migration-internal.h | 3 + hw/virtio/virtio.c | 32 ++++- include/hw/ppc/spapr_xive.h | 3 +- include/hw/vfio/vfio-device.h | 1 + include/hw/virtio/virtio.h | 3 +- include/system/runstate.h | 55 ++++--- net/net.c | 52 ++++--- system/runstate.c | 115 ++++++++------- tests/audio/audio-stubs.c | 7 + 20 files changed, 522 insertions(+), 152 deletions(-)
PATCH v2 1/13 runstate: Rename VM change state handlers "priority" to "depth"
From: "Maciej S. Szmigiero" <[email protected]>
This frees the term "priority" for implementing an explicit multi-level priority system of these handlers instead of the current two-level implicit prepare_cb() vs cb() system.
--- include/system/runstate.h | 12 ++++++------ system/runstate.c | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-)
include/system/runstate.h +6 -6
@@ -30,8 +30,8 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,* qemu_add_vm_change_state_handler_prio: * @cb: the callback to invoke * @opaque: user data passed to the callback -* @priority: low priorities execute first when the vm runs and the reverse is -* true when the vm stops +* @depth: low depths execute first when the vm runs and the reverse is true +* when the vm stops * * Register a callback function that is invoked when the vm starts or stops * running.
@@ -39,7 +39,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,* Returns: an entry to be freed using qemu_del_vm_change_state_handler() */ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio ( -VMChangeStateHandler *cb,void *opaque,int priority); +VMChangeStateHandler *cb,void *opaque,int depth); VMChangeStateEntry */** * qemu_add_vm_change_state_handler_prio_full:
@@ -47,8 +47,8 @@ VMChangeStateEntry ** @prepare_cb: a callback to invoke before the main callback * @cb_ret: the main callback to invoke with return value * @opaque: user data passed to the callbacks -* @priority: low priorities execute first when the vm runs and the reverse is -* true when the vm stops +* @depth: low depths execute first when the vm runs and the reverse is true +* when the vm stops * * Register a main callback function and an optional prepare callback function * that are invoked when the vm starts or stops running. The main callback and
@@ -62,7 +62,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio_full (VMChangeStateHandler *cb, VMChangeStateHandler *prepare_cb, VMChangeStateHandlerWithRet *cb_ret, - void *opaque, int priority); + void *opaque, int depth); VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandlerWithRet *cb_ret,
system/runstate.c +7 -7
@@ -304,24 +304,24 @@ struct VMChangeStateEntry {VMChangeStateHandlerWithRet *cb_ret;void *opaque;QTAILQ_ENTRY (VMChangeStateEntry) entries; -int priority; +int depth; }; static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =QTAILQ_HEAD_INITIALIZER (vm_change_state_head);VMChangeStateEntry *qemu_add_vm_change_state_handler_prio ( -VMChangeStateHandler *cb,void *opaque,int priority) +VMChangeStateHandler *cb,void *opaque,int depth) {return qemu_add_vm_change_state_handler_prio_full (cb,NULL ,NULL , - opaque, priority); + opaque, depth); }VMChangeStateEntry *qemu_add_vm_change_state_handler_prio_full (VMChangeStateHandler *cb,VMChangeStateHandler *prepare_cb,VMChangeStateHandlerWithRet *cb_ret, -void *opaque,int priority) +void *opaque,int depth) {VMChangeStateEntry *e;VMChangeStateEntry *other;
@@ -331,11 +331,11 @@ qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, e->prepare_cb = prepare_cb; e->cb_ret = cb_ret; e->opaque = opaque; - e->priority = priority; + e->depth = depth; -/* Keep list sorted in ascending priority order */ +/* Keep list sorted in ascending depth order */ QTAILQ_FOREACH(other, &vm_change_state_head, entries) { -if (priority < other->priority) { +if (depth < other->depth) {QTAILQ_INSERT_BEFORE (other, e, entries);return e; }
PATCH v2 2/13 runstate: Introduce VM change state handlers explicit priority levels
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Replace the current two-level implicit priority system of prepare_cb() and cb() with explicit multi-level priority system.
The old prepare_cb() maps to VM_CHANGE_STATE_PRI_HIGH in this new system, while the old cb() maps to VM_CHANGE_STATE_PRI_NORMAL.
--- hw/core/vm-change-state-handler.c | 16 +++-- hw/vfio/migration.c | 14 ++-- hw/vfio/vfio-migration-internal.h | 1 + include/system/runstate.h | 38 ++++++----- system/runstate.c | 106 ++++++++++++++++-------------- 5 files changed, 98 insertions(+), 77 deletions(-)
hw/core/vm-change-state-handler.c +9 -7
@@ -61,20 +61,22 @@ 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, cb_ret, opaque,VM_CHANGE_STATE_PRI_NORMAL ); }/* -* Exactly like qdev_add_vm_change_state_handler() but passes a prepare_cb -* and the cb_ret arguments too. +* Exactly like qdev_add_vm_change_state_handler() but allows selecting the +* notification priority. */ VMChangeStateEntry *qdev_add_vm_change_state_handler_full ( -DeviceState *dev,VMChangeStateHandler *cb,VMChangeStateHandler *prepare_cb, -VMChangeStateHandlerWithRet *cb_ret,void *opaque) +DeviceState *dev,VMChangeStateHandler *cb, +VMChangeStateHandlerWithRet *cb_ret,void *opaque, +VMChangeStateHandlerPri priority) {int depth =qdev_get_dev_tree_depth (dev);assert (!cb || !cb_ret); -return qemu_add_vm_change_state_handler_prio_full (cb, prepare_cb, cb_ret, - opaque, depth); +return qemu_add_vm_change_state_handler_prio_full (cb, cb_ret, opaque, + priority, depth); }
hw/vfio/migration.c +8 -6
@@ -1149,7 +1149,6 @@ static bool vfio_migration_init(VFIODevice *vbasedev, Error **errp) g_autofree char *path = NULL, *oid = NULL;uint64_t mig_flags =0 ;bool precopy_info_v2_used = false; -VMChangeStateHandler *prepare_cb;if (!vbasedev->ops->vfio_get_object) {error_setg (errp,"no vfio_get_object handler" );
@@ -1210,11 +1209,13 @@ static bool 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); +if (migration->mig_flags &VFIO_MIGRATION_P2P ) { + migration->vm_state_prepare =qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_prepare,NULL , vbasedev, +VM_CHANGE_STATE_PRI_HIGH ); + } + migration->vm_state =qdev_add_vm_change_state_handler ( + vbasedev->dev, vfio_vmstate_change,NULL , vbasedev);migration_add_notifier (&migration->migration_state, vfio_migration_state_notifier);
@@ -1289,6 +1290,7 @@ 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_prepare);qemu_del_vm_change_state_handler (migration->vm_state);unregister_savevm (VMSTATE_IF (vbasedev->dev),"vfio" , vbasedev);vfio_migration_free (vbasedev);
hw/vfio/vfio-migration-internal.h +1 -0
@@ -38,6 +38,7 @@ typedef struct VFIOMultifd VFIOMultifd; typedef struct VFIOMigration { struct VFIODevice *vbasedev; + VMChangeStateEntry *vm_state_prepare; VMChangeStateEntry *vm_state; NotifierWithReturn migration_state; uint32_t device_state;
include/system/runstate.h +22 -16
@@ -14,6 +14,12 @@ void runstate_replay_enable(void);typedef void VMChangeStateHandler (void *opaque,bool running,RunState state);typedef int VMChangeStateHandlerWithRet (void *opaque,bool running,RunState state); +typedef enum VMChangeStateHandlerPri { +VM_CHANGE_STATE_PRI_HIGH , +VM_CHANGE_STATE_PRI_NORMAL , +VM_CHANGE_STATE_PRI_MAX , +}VMChangeStateHandlerPri ; +/** * qemu_add_vm_change_state_handler: * @cb: the callback to invoke
@@ -30,8 +36,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,* qemu_add_vm_change_state_handler_prio: * @cb: the callback to invoke * @opaque: user data passed to the callback -* @depth: low depths execute first when the vm runs and the reverse is true -* when the vm stops +* @priority: priority at which to invoke the callback * * Register a callback function that is invoked when the vm starts or stops * running.
@@ -39,37 +44,38 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,* Returns: an entry to be freed using qemu_del_vm_change_state_handler() */ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio ( -VMChangeStateHandler *cb,void *opaque,int depth); +VMChangeStateHandler *cb,void *opaque, +VMChangeStateHandlerPri priority);VMChangeStateEntry */** * qemu_add_vm_change_state_handler_prio_full: -* @cb: the main callback to invoke -* @prepare_cb: a callback to invoke before the main callback -* @cb_ret: the main callback to invoke with return value +* @cb: the callback to invoke +* @cb_ret: the callback to invoke with return value * @opaque: user data passed to the callbacks +* @priority: priority at which to invoke the callback * @depth: low depths execute first when the vm runs and the reverse is true * when the vm stops * -* Register a main callback function and an optional prepare callback function -* that are invoked when the vm starts or stops running. The main callback and -* the prepare callback are called in two separate phases: First all prepare -* callbacks are called and only then all main callbacks are called. As its -* name suggests, the prepare callback can be used to do some preparatory work -* before invoking the main callback. +* Register a callback function that is invoked when the vm starts or stops +* running. All higher priority notifications are completed before calling any +* lower priority ones. In addition to that, depth ordering is maintained within +* each priority level. * * Returns: an entry to be freed using qemu_del_vm_change_state_handler() */ qemu_add_vm_change_state_handler_prio_full (VMChangeStateHandler *cb, -VMChangeStateHandler *prepare_cb,VMChangeStateHandlerWithRet *cb_ret, -void *opaque,int depth); +void *opaque, +VMChangeStateHandlerPri priority, +int depth);VMChangeStateEntry *qdev_add_vm_change_state_handler (DeviceState *dev,VMChangeStateHandler *cb,VMChangeStateHandlerWithRet *cb_ret,void *opaque);VMChangeStateEntry *qdev_add_vm_change_state_handler_full ( -DeviceState *dev,VMChangeStateHandler *cb,VMChangeStateHandler *prepare_cb, -VMChangeStateHandlerWithRet *cb_ret,void *opaque); +DeviceState *dev,VMChangeStateHandler *cb, +VMChangeStateHandlerWithRet *cb_ret,void *opaque, +VMChangeStateHandlerPri priority);void qemu_del_vm_change_state_handler (VMChangeStateEntry *e);/** * vm_state_notify: Notify the state of the VM
system/runstate.c +58 -48
167 lines not on this page · raw message
PATCH v2 3/13 runstate: Add an intermediate VM change state handlers SYNC priority
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
VFIO code needs to register a VM change state handler between NORMAL and HIGH priorities as a synchronization point/barrier between the first state transition of its devices and their second state transition.
Add an intermediate SYNC priority of VM change state handlers for that.
--- include/system/runstate.h | 2 +- system/runstate.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
include/system/runstate.h +1 -1
@@ -16,6 +16,7 @@ typedef int VMChangeStateHandlerWithRet(void *opaque, bool running, RunState statypedef enum VMChangeStateHandlerPri {VM_CHANGE_STATE_PRI_HIGH , +VM_CHANGE_STATE_PRI_SYNC ,VM_CHANGE_STATE_PRI_NORMAL ,VM_CHANGE_STATE_PRI_MAX , }VMChangeStateHandlerPri ;
@@ -163,4 +164,3 @@ void qemu_system_guest_pvshutdown(void);bool qemu_system_dump_in_progress (void );#endif -
system/runstate.c +3 -0
@@ -313,6 +313,9 @@ static VMChangeStateHead vm_change_state_head[VM_CHANGE_STATE_PRI_MAX] = { [VM_CHANGE_STATE_PRI_HIGH] =QTAILQ_HEAD_INITIALIZER ( vm_change_state_head[VM_CHANGE_STATE_PRI_HIGH]), + [VM_CHANGE_STATE_PRI_SYNC ] = +QTAILQ_HEAD_INITIALIZER ( + vm_change_state_head[VM_CHANGE_STATE_PRI_SYNC]), [VM_CHANGE_STATE_PRI_NORMAL ] =QTAILQ_HEAD_INITIALIZER ( vm_change_state_head[VM_CHANGE_STATE_PRI_NORMAL]),
PATCH v2 4/13 hw/vfio: Introduce VFIOPCIDeviceClass
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
It will soon be needed to store a pointer to data shared between all VFIOPCIDevice instances.
--- hw/vfio/pci.c | 1 + hw/vfio/pci.h | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-)
hw/vfio/pci.c +1 -0
@@ -3853,6 +3853,7 @@ static const TypeInfo vfio_pci_device_info = { .name = TYPE_VFIO_PCI_DEVICE, .parent = TYPE_PCI_DEVICE, .instance_size = sizeof(VFIOPCIDevice), + .class_size = sizeof(VFIOPCIDeviceClass), .abstract = true, .class_init = vfio_pci_device_class_init, .interfaces = (const InterfaceInfo []) {
hw/vfio/pci.h +5 -1
@@ -120,7 +120,11 @@ typedef struct VFIOMSIXInfo {MemoryRegion *pba_region; } VFIOMSIXInfo; -OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_DEVICE) +OBJECT_DECLARE_TYPE(VFIOPCIDevice, VFIOPCIDeviceClass, VFIO_PCI_DEVICE) + +struct VFIOPCIDeviceClass { +PCIDeviceClass parent_class; +};struct VFIOPCIDevice {PCIDevice parent_obj;
PATCH v2 5/13 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 these state transitions synchronously launch a per-device thread performing them at VM_CHANGE_STATE_PRI_HIGH.
Since no VFIO device should start the second state transition until all VFIO devices have completed the first transition also register a single VM_CHANGE_STATE_PRI_SYNC handler common for all VFIO devices that serves as a synchronization point/barrier between these transitions.
This synchronization point also serves to fulfill similar requirement with respect to non-VFIO devices that require particular sequencing with respect to running their VM state change handlers versus VFIO devices. In particular, this requirement applies to DMA-generating devices and interrupt controllers.
These VFIO device state change threads are joined (waited for) from a per-device VM_CHANGE_STATE_PRI_NORMAL handler.
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 other qdev tree depths - as long as these handlers at other qdev tree depths would be previously ordered earlier than the synchronous VFIO device state change handler.
Data used by common VM_CHANGE_STATE_PRI_SYNC handler is shared between all TYPE_VFIO_PCI_DEVICE instances, so it is kept in its VFIOPCIDeviceClass struct as new "vm_state_change_sync" field.
This data is allocated on demand during initialization of the migration part of the first migratable VFIO device.
Since derived classes (like TYPE_VFIO_PCI and TYPE_VFIO_PCI_NOHOTPLUG) inherit this class field as a shallow copy, one extra level of pointer indirection is necessary for this data.
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 | 228 +++++++++++++++++++++++++++++- hw/vfio/pci.c | 6 + hw/vfio/pci.h | 4 + hw/vfio/trace-events | 2 + hw/vfio/vfio-migration-internal.h | 2 + include/hw/vfio/vfio-device.h | 1 + 6 files changed, 237 insertions(+), 6 deletions(-)
hw/vfio/migration.c +222 -6
@@ -1053,6 +1053,202 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state) mig_state_to_str(new_state)); } +typedef struct VMStateChangeThreadData { +VFIODevice *vbasedev; +VFIOVMStateChangeSync *sync; +bool running; +RunState state; +}VMStateChangeThreadData ; + +struct VFIOVMStateChangeSync { +VMChangeStateEntry *vm_state; + +/* Only counts devices with parallel state change ON */ +unsigned int device_count; +unsigned int thread_count; + +unsigned int p2p_done_count; +QemuEvent p2p_all_done; +QemuEvent final_transition_start; +}; + +static void vfio_vmstate_change_sync_handler (void *opaque,bool running, +RunState state) +{ +VFIOVMStateChangeSync *sync = opaque; + +assert (qatomic_read (&sync->thread_count) == +qatomic_read (&sync->device_count)); + +trace_vfio_vmstate_change_sync_wait (); +qemu_event_wait (&sync->p2p_all_done); + +trace_vfio_vmstate_change_sync_done (); +qemu_event_set (&sync->final_transition_start); +} + +static VFIOVMStateChangeSync ** +vfio_vmstate_change_sync_ptr (VFIODevice *vbasedev) +{ +VFIOPCIDevice *vdev =vfio_pci_from_vfio_device (vbasedev); +VFIOPCIDeviceClass *vpc; + +assert (vdev); + vpc =VFIO_PCI_DEVICE_GET_CLASS (vdev); +assert (vpc); +assert (vpc->vm_state_change_sync); + +return vpc->vm_state_change_sync; +} + +static VFIOVMStateChangeSync * +vfio_vmstate_change_sync_ref (VFIODevice *vbasedev) +{ +VFIOVMStateChangeSync **syncp =vfio_vmstate_change_sync_ptr (vbasedev); +VFIOVMStateChangeSync *sync = *syncp; + +if (!sync) { + sync =g_new0 (VFIOVMStateChangeSync,1 ); +qemu_event_init (&sync->p2p_all_done, false); +qemu_event_init (&sync->final_transition_start, false); + + sync->vm_state =qemu_add_vm_change_state_handler_prio ( + vfio_vmstate_change_sync_handler, sync, +VM_CHANGE_STATE_PRI_SYNC ); + + *syncp = sync; + } + +qatomic_fetch_inc (&sync->device_count); + +return sync; +} + +static void vfio_vmstate_change_sync_unref (VFIODevice *vbasedev) +{ +VFIOVMStateChangeSync **syncp =vfio_vmstate_change_sync_ptr (vbasedev); +VFIOVMStateChangeSync *sync = *syncp; +unsigned int device_count; + +assert (sync); + + device_count =qatomic_fetch_dec (&sync->device_count); +assert (device_count >0 ); + +if (device_count !=1 ) { +return ; + } + +assert (qatomic_read (&sync->thread_count) ==0 ); + +qemu_del_vm_change_state_handler (sync->vm_state); +qemu_event_destroy (&sync->final_transition_start); +qemu_event_destroy (&sync->p2p_all_done); + +g_clear_pointer (syncp, g_free); +} + +static VFIOVMStateChangeSync * +vfio_vmstate_change_sync_get (VFIODevice *vbasedev) +{ +VFIOVMStateChangeSync *sync = *vfio_vmstate_change_sync_ptr (vbasedev); + +assert (sync); + +return sync; +} + +static void +vfio_vmstate_change_sync_thread_pre_start (VFIOVMStateChangeSync *sync) +{ +if (qatomic_fetch_inc (&sync->thread_count) ==0 ) { +/* +* Reset the sync data at the first thread launch time since +* the whole struct survives VM start/stop sequence. +*/ +qatomic_set (&sync->p2p_done_count,0 ); +qemu_event_reset (&sync->p2p_all_done); +qemu_event_reset (&sync->final_transition_start); + } +} + +static void +vfio_vmstate_change_sync_p2p_done (VFIOVMStateChangeSync *sync) +{ +unsigned int done; + + done =qatomic_fetch_inc (&sync->p2p_done_count) +1 ; +if (done ==qatomic_read (&sync->device_count)) { +qemu_event_set (&sync->p2p_all_done); + } + +qemu_event_wait (&sync->final_transition_start); +} + +static void +vfio_vmstate_change_sync_thread_finished (VFIOVMStateChangeSync *sync) +{ +unsigned int thread_count; + + thread_count =qatomic_fetch_dec (&sync->thread_count); +assert (thread_count >0 ); +} + +static void *vfio_vmstate_change_thread (void *opaque) +{ + g_autofree VMStateChangeThreadData *data = opaque; +VFIOMigration *migration = data->vbasedev->migration; + +if (migration->mig_flags &VFIO_MIGRATION_P2P ) { +vfio_vmstate_change_prepare (data->vbasedev, data->running, data->state); + } + +vfio_vmstate_change_sync_p2p_done (data->sync); + +vfio_vmstate_change (data->vbasedev, data->running, data->state); + +return NULL ; +} + +static void vfio_vmstate_change_thread_launch (void *opaque,bool running, +RunState state) +{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VMStateChangeThreadData *data =g_new (VMStateChangeThreadData,1 ); +VFIOVMStateChangeSync *sync =vfio_vmstate_change_sync_get (vbasedev); + + data->vbasedev = vbasedev; + data->sync = sync; + data->running = running; + data->state = state; + +assert (!migration->vm_state_thread_running); + migration->vm_state_thread_running = true; + +vfio_vmstate_change_sync_thread_pre_start (sync); + +qemu_thread_create (&migration->vm_state_thread, +"vfio_vmstate_change" , + vfio_vmstate_change_thread, data, +QEMU_THREAD_JOINABLE ); +} + +static void vfio_vmstate_change_thread_join (void *opaque,bool running, +RunState state) +{ +VFIODevice *vbasedev = opaque; +VFIOMigration *migration = vbasedev->migration; +VFIOVMStateChangeSync *sync =vfio_vmstate_change_sync_get (vbasedev); + +assert (migration->vm_state_thread_running); + +qemu_thread_join (&migration->vm_state_thread); + migration->vm_state_thread_running = false; + +vfio_vmstate_change_sync_thread_finished (sync); +} +static int vfio_migration_state_notifier (NotifierWithReturn *notifier,MigrationEvent *e,Error **errp) {
@@ -1069,6 +1265,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,
@@ -1209,13 +1407,25 @@ static bool vfio_migration_init(VFIODevice *vbasedev, Error **errp)register_savevm_live (id,VMSTATE_INSTANCE_ID_ANY ,1 , &savevm_vfio_handlers, vbasedev); -if (migration->mig_flags &VFIO_MIGRATION_P2P ) { - migration->vm_state_prepare =qdev_add_vm_change_state_handler_full ( - vbasedev->dev, vfio_vmstate_change_prepare,NULL , vbasedev, -VM_CHANGE_STATE_PRI_HIGH ); +if (vbasedev->migration_parallel_states) { +vfio_vmstate_change_sync_ref (vbasedev); + migration->vm_state_prepare = +qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_thread_launch,NULL , + vbasedev,VM_CHANGE_STATE_PRI_HIGH ); + migration->vm_state =qdev_add_vm_change_state_handler ( + vbasedev->dev, vfio_vmstate_change_thread_join,NULL , vbasedev); + }else { +if (migration->mig_flags &VFIO_MIGRATION_P2P ) { + migration->vm_state_prepare = +qdev_add_vm_change_state_handler_full ( + vbasedev->dev, vfio_vmstate_change_prepare,NULL , + vbasedev,VM_CHANGE_STATE_PRI_HIGH ); + } + migration->vm_state =qdev_add_vm_change_state_handler ( + vbasedev->dev, vfio_vmstate_change,NULL , vbasedev); } - migration->vm_state =qdev_add_vm_change_state_handler ( - vbasedev->dev, vfio_vmstate_change,NULL , vbasedev); +migration_add_notifier (&migration->migration_state, vfio_migration_state_notifier);
@@ -1292,6 +1502,12 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)migration_remove_notifier (&migration->migration_state);qemu_del_vm_change_state_handler (migration->vm_state_prepare);qemu_del_vm_change_state_handler (migration->vm_state); + +if (vbasedev->migration_parallel_states) { +assert (!migration->vm_state_thread_running); +vfio_vmstate_change_sync_unref (vbasedev); + } +unregister_savevm (VMSTATE_IF (vbasedev->dev),"vfio" , vbasedev);vfio_migration_free (vbasedev);vfio_unblock_multiple_devices_migration ();
hw/vfio/pci.c +6 -0
@@ -3839,9 +3839,13 @@ static void vfio_pci_init(Object *obj)static void vfio_pci_device_class_init (ObjectClass *klass,const void *data) { +VFIOPCIDeviceClass *vpc =VFIO_PCI_DEVICE_CLASS (klass);DeviceClass *dc =DEVICE_CLASS (klass);PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); +/* Derived classes inherit this field as a copy */ + vpc->vm_state_change_sync = g_new0(VFIOVMStateChangeSync *, 1); + dc->desc ="VFIO PCI base device" ;set_bit (DEVICE_CATEGORY_MISC , dc->categories); pdc->exit = vfio_exitfn;
@@ -3902,6 +3906,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/pci.h +4 -0
@@ -120,10 +120,14 @@ typedef struct VFIOMSIXInfo {MemoryRegion *pba_region; } VFIOMSIXInfo; +typedef struct VFIOVMStateChangeSync VFIOVMStateChangeSync ; + OBJECT_DECLARE_TYPE(VFIOPCIDevice, VFIOPCIDeviceClass, VFIO_PCI_DEVICE)struct VFIOPCIDeviceClass {PCIDeviceClass parent_class; + +VFIOVMStateChangeSync **vm_state_change_sync; };struct VFIOPCIDevice {
hw/vfio/trace-events +2 -0
@@ -183,6 +183,8 @@ vfio_state_pending(const char *name, uint64_t stopcopy_size, uint64_t precopy_in vfio_send_init_data_flag(const char *name) " (%s)" vfio_vmstate_change(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s" vfio_vmstate_change_prepare(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s" +vfio_vmstate_change_sync_wait(void) "" +vfio_vmstate_change_sync_done(void) "" #iommufd.c
hw/vfio/vfio-migration-internal.h +2 -0
@@ -40,6 +40,8 @@ typedef struct VFIOMigration {struct VFIODevice *vbasedev;VMChangeStateEntry *vm_state_prepare;VMChangeStateEntry *vm_state; +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;
PATCH v2 6/13 spapr/xive: Split VM state change handler into start and stop parts
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This will allow moving the VM start part of this handler into VM_CHANGE_STATE_PRI_HIGH.
--- hw/intc/spapr_xive_kvm.c | 74 ++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 26 deletions(-)
hw/intc/spapr_xive_kvm.c +48 -26
@@ -498,39 +498,51 @@ static int kvmppc_xive_get_queues(SpaprXive *xive, Error **errp)* runs again. If an interrupt was queued while the VM was stopped, * simply generate a trigger. */ -static void kvmppc_xive_change_state_handler (void *opaque,bool running, -RunState state) +static void kvmppc_xive_change_state_start (void *opaque,bool running, +RunState state) +{ +SpaprXive *xive = opaque; +XiveSource *xsrc = &xive->source; +int i; + +if (!running) { +return ; + } + +/* +* Restore the sources to their initial state. This is called when +* the VM resumes after a stop or a migration. +*/ +for (i =0 ; i < xsrc->nr_irqs; i++) { +uint8_t pq; +uint8_t old_pq; + +if (!xive_eas_is_valid (&xive->eat[i])) { +continue ; + } + + pq =xive_source_esb_get (xsrc, i); + old_pq =xive_esb_read (xsrc, i,XIVE_ESB_SET_PQ_00 + (pq <<8 )); + +/* +* An interrupt was queued while the VM was stopped, +* generate a trigger. +*/ +if (pq ==XIVE_ESB_RESET && old_pq ==XIVE_ESB_QUEUED ) { +kvmppc_xive_esb_trigger (xsrc, i); + } + } +} + +static void kvmppc_xive_change_state_stop (void *opaque,bool running, +RunState state) {SpaprXive *xive = opaque;XiveSource *xsrc = &xive->source;Error *local_err =NULL ;int i; -/* -* Restore the sources to their initial state. This is called when -* the VM resumes after a stop or a migration. -*/ if (running) { -for (i =0 ; i < xsrc->nr_irqs; i++) { -uint8_t pq; -uint8_t old_pq; - -if (!xive_eas_is_valid (&xive->eat[i])) { -continue ; - } - - pq =xive_source_esb_get (xsrc, i); - old_pq =xive_esb_read (xsrc, i,XIVE_ESB_SET_PQ_00 + (pq <<8 )); - -/* -* An interrupt was queued while the VM was stopped, -* generate a trigger. -*/ -if (pq ==XIVE_ESB_RESET && old_pq ==XIVE_ESB_QUEUED ) { -kvmppc_xive_esb_trigger (xsrc, i); - } - } -return ; }
@@ -573,6 +585,16 @@ static void kvmppc_xive_change_state_handler(void *opaque, bool running, } } +static void kvmppc_xive_change_state_handler (void *opaque,bool running, +RunState state) +{ +if (running) { +kvmppc_xive_change_state_start (opaque, running, state); + }else { +kvmppc_xive_change_state_stop (opaque, running, state); + } +} +void kvmppc_xive_synchronize_state (SpaprXive *xive,Error **errp) {assert (xive->fd !=-1 );
PATCH v2 7/13 runstate: Start/stop interrupt controllers before/after ordinary devices
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
The VFIO spec allows VFIO devices to generate interrupts already after their first state transition at VM start (done at VM_CHANGE_STATE_PRI_HIGH) until the second (last) state transition at VM stop (done at VM_CHANGE_STATE_PRI_NORMAL).
In principle, the same may also apply to other/emulated devices.
That means in order to avoid potentially lost interrupts any interrupt controllers with VM state change handlers need to be started before VFIO devices at VM_CHANGE_STATE_PRI_HIGH on VM start and stopped only after VFIO devices at VM_CHANGE_STATE_PRI_NORMAL.
To implement that ordering and to make sure that it is not lost in a future code refactor, introduce an explicit VMChangeStateHandlerDepth - much like migration has explicit MigrationPriority values.
Since normal qdev tree depths are non-negative these earlier-on-start / later-on-stop depths need to be increasingly negative.
Preserve the old ITS vs GIC ordering.
This happens to also match the MigrationPriority of these controllers.
In terms of implementation, the kvm-tpr-opt VAPIC VM state change handler is already NOP on VM stop, while ITS and GIC handlers are NOPs on VM start. So for these devices the change amounts to trivial adjustment of their handlers priorities and/or depths.
For the remaining sPAPR XIVE interrupt controller make use of the start/stop split of its VM state change handler introduced by the previous commit.
--- hw/i386/vapic.c | 5 +++-- hw/intc/arm_gicv3_its_kvm.c | 4 +++- hw/intc/arm_gicv3_kvm.c | 4 +++- hw/intc/spapr_xive_kvm.c | 26 ++++++++++---------------- include/hw/ppc/spapr_xive.h | 3 ++- include/system/runstate.h | 11 +++++++++++ system/runstate.c | 2 +- 7 files changed, 33 insertions(+), 22 deletions(-)
hw/i386/vapic.c +3 -2
@@ -822,8 +822,9 @@ static int vapic_post_load(void *opaque, int version_id) }if (!s->vmsentry) { - s->vmsentry = -qemu_add_vm_change_state_handler (vapic_vm_state_change, s); + s->vmsentry =qemu_add_vm_change_state_handler_prio_full ( + vapic_vm_state_change,NULL , s,VM_CHANGE_STATE_PRI_HIGH , +VM_CHANGE_STATE_DEPTH_APIC ); }return 0 ; }
hw/intc/arm_gicv3_its_kvm.c +3 -1
@@ -119,7 +119,9 @@ static void kvm_arm_its_realize(DeviceState *dev, Error **errp)return ; } }else { -qemu_add_vm_change_state_handler (vm_change_state_handler, s); +qemu_add_vm_change_state_handler_prio_full ( + vm_change_state_handler,NULL , s,VM_CHANGE_STATE_PRI_NORMAL , +VM_CHANGE_STATE_DEPTH_GICV3_ITS ); } kvm_msi_use_devid = true;
hw/intc/arm_gicv3_kvm.c +3 -1
@@ -925,7 +925,9 @@ static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp) }if (kvm_device_check_attr (s->dev_fd,KVM_DEV_ARM_VGIC_GRP_CTRL ,KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES )) { -qemu_add_vm_change_state_handler (vm_change_state_handler, s); +qemu_add_vm_change_state_handler_prio_full ( + vm_change_state_handler,NULL , s,VM_CHANGE_STATE_PRI_NORMAL , +VM_CHANGE_STATE_DEPTH_GICV3 );migration_add_notifier_mode (&s->cpr_notifier, kvm_arm_gicv3_notifier,MIG_MODE_CPR_TRANSFER );
hw/intc/spapr_xive_kvm.c +10 -16
@@ -585,16 +585,6 @@ static void kvmppc_xive_change_state_stop(void *opaque, bool running, } } -static void kvmppc_xive_change_state_handler (void *opaque,bool running, -RunState state) -{ -if (running) { -kvmppc_xive_change_state_start (opaque, running, state); - }else { -kvmppc_xive_change_state_stop (opaque, running, state); - } -} -void kvmppc_xive_synchronize_state (SpaprXive *xive,Error **errp) {assert (xive->fd !=-1 );
@@ -813,8 +803,12 @@ int kvmppc_xive_connect(SpaprInterruptController *intc, uint32_t nr_servers,memory_region_add_subregion_overlap (&xive->tm_mmio,0 , &xive->tm_mmio_kvm,1 ); - xive->change =qemu_add_vm_change_state_handler ( - kvmppc_xive_change_state_handler, xive); + xive->change_start =qemu_add_vm_change_state_handler_prio_full ( + kvmppc_xive_change_state_start,NULL , xive, +VM_CHANGE_STATE_PRI_HIGH ,VM_CHANGE_STATE_DEPTH_XIVE ); + xive->change_stop =qemu_add_vm_change_state_handler_prio_full ( + kvmppc_xive_change_state_stop,NULL , xive, +VM_CHANGE_STATE_PRI_NORMAL ,VM_CHANGE_STATE_DEPTH_XIVE );/* Connect the presenters to the initial VCPUs of the machine */ CPU_FOREACH (cs) {
@@ -884,8 +878,8 @@ void kvmppc_xive_disconnect(SpaprInterruptController *intc)kvm_cpu_disable_all ();/* VM Change state handler is not needed anymore */ -if (xive->change) { -qemu_del_vm_change_state_handler (xive->change); - xive->change =NULL ; - } +qemu_del_vm_change_state_handler (xive->change_start); + xive->change_start =NULL ; +qemu_del_vm_change_state_handler (xive->change_stop); + xive->change_stop =NULL ; }
include/hw/ppc/spapr_xive.h +2 -1
@@ -48,7 +48,8 @@ typedef struct SpaprXive {int fd;void *tm_mmap;MemoryRegion tm_mmio_kvm; -VMChangeStateEntry *change; +VMChangeStateEntry *change_start; +VMChangeStateEntry *change_stop;uint8_t hv_prio; } SpaprXive;
include/system/runstate.h +11 -0
@@ -21,6 +21,17 @@ typedef enum VMChangeStateHandlerPri {VM_CHANGE_STATE_PRI_MAX , } VMChangeStateHandlerPri; +typedef enum VMChangeStateHandlerDepth { +VM_CHANGE_STATE_DEPTH_GICV3 =-3 , +VM_CHANGE_STATE_DEPTH_GICV3_ITS =-2 , + +VM_CHANGE_STATE_DEPTH_APIC =-1 , +VM_CHANGE_STATE_DEPTH_XIVE =-1 , + +/* qdev tree depths start here */ +VM_CHANGE_STATE_DEPTH_DEFAULT =0 , +}VMChangeStateHandlerDepth ; +/** * qemu_add_vm_change_state_handler: * @cb: the callback to invoke
system/runstate.c +1 -1
@@ -326,7 +326,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( VMChangeStateHandlerPri priority) {return qemu_add_vm_change_state_handler_prio_full ( - cb,NULL , opaque, priority,0 ); + cb,NULL , opaque, priority,VM_CHANGE_STATE_DEPTH_DEFAULT ); } VMChangeStateEntry *
PATCH v2 8/13 audio: Split VM state change handler into start and stop parts
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This will allow moving the VM stop part of this handler into VM_CHANGE_STATE_PRI_HIGH.
--- audio/audio-mixeng-be.c | 42 +++++++++++++++++++++++++++++++++-------- audio/audio_int.h | 3 ++- 2 files changed, 36 insertions(+), 9 deletions(-)
audio/audio-mixeng-be.c +34 -8
@@ -1381,10 +1381,8 @@ static bool audio_mixeng_backend_realize(AudioBackend *abe,return true; } -static void audio_vm_change_state_handler (void *opaque,bool running, -RunState state) +static void audio_vm_change_state (AudioMixengBackend *s,bool running) { -AudioMixengBackend *s = opaque;AudioMixengBackendClass *k =AUDIO_MIXENG_BACKEND_GET_CLASS (s);HWVoiceOut *hwo =NULL ;HWVoiceIn *hwi =NULL ;
@@ -1403,6 +1401,26 @@ static void audio_vm_change_state_handler (void *opaque, bool running,audio_reset_timer (s); } +static void audio_vm_change_state_start (void *opaque,bool running, +RunState state) +{ +if (!running) { +return ; + } + +audio_vm_change_state (opaque, true); +} + +static void audio_vm_change_state_stop (void *opaque,bool running, +RunState state) +{ +if (running) { +return ; + } + +audio_vm_change_state (opaque, false); +} +static const VMStateDescription vmstate_audio;static const char *audio_mixeng_backend_get_id (AudioBackend *be)
@@ -1459,8 +1477,12 @@ static void audio_mixeng_backend_init(Object *obj) s->ts =timer_new_ns (QEMU_CLOCK_VIRTUAL , audio_timer, s); s->run_timer =g_timer_new (); - s->vmse =qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s); -assert (s->vmse !=NULL ); + s->vmse_start =qemu_add_vm_change_state_handler ( + audio_vm_change_state_start, s); +assert (s->vmse_start !=NULL ); + s->vmse_stop =qemu_add_vm_change_state_handler ( + audio_vm_change_state_stop, s); +assert (s->vmse_stop !=NULL );vmstate_register_any (NULL , &vmstate_audio, s); }
@@ -1511,9 +1533,13 @@ static void audio_mixeng_backend_finalize(Object *obj)g_clear_pointer (&s->run_timer, g_timer_destroy); -if (s->vmse) { -qemu_del_vm_change_state_handler (s->vmse); - s->vmse =NULL ; +if (s->vmse_start) { +qemu_del_vm_change_state_handler (s->vmse_start); + s->vmse_start =NULL ; + } +if (s->vmse_stop) { +qemu_del_vm_change_state_handler (s->vmse_stop); + s->vmse_stop =NULL ; }vmstate_unregister (NULL , &vmstate_audio, s);
audio/audio_int.h +2 -1
@@ -229,7 +229,8 @@ struct AudioMixengBackend {bool timer_running;uint64_t timer_last; -VMChangeStateEntry *vmse; +VMChangeStateEntry *vmse_start; +VMChangeStateEntry *vmse_stop; };extern const struct mixeng_volume nominal_volume;
PATCH v2 9/13 audio: Make sure to stop audio before VFIO gets non-DMA-capable
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Although a VM state change normally happens under BQL some state change handlers may internally temporarily drop BQL.
On the migration source, the VM state change happens in the migration thread so this potentially allows main loop thread to acquire BQL and dispatch audio backend handlers.
In turn, these audio backend handlers may call audio_run() which then may call into emulated sound card to do DMA.
Since VFIO devices start their transition into a state where they no longer can accept incoming DMA at VM_CHANGE_STATE_PRI_SYNC if audio is stopped at VM_CHANGE_STATE_PRI_NORMAL in an unlikely but possible case that the aforementioned sound card DMA targets a VFIO device BAR it may reach a VFIO device that has already switched into a non-DMA-capable state.
Fix this by making sure that at VM stop audio is stopped at HIGH priority level, so before VFIO devices start reaching states not capable of accepting incoming DMA.
--- audio/audio-mixeng-be.c | 4 ++-- tests/audio/audio-stubs.c | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-)
audio/audio-mixeng-be.c +2 -2
@@ -1480,8 +1480,8 @@ static void audio_mixeng_backend_init(Object *obj) s->vmse_start =qemu_add_vm_change_state_handler ( audio_vm_change_state_start, s);assert (s->vmse_start !=NULL ); - s->vmse_stop =qemu_add_vm_change_state_handler ( - audio_vm_change_state_stop, s); + s->vmse_stop =qemu_add_vm_change_state_handler_prio ( + audio_vm_change_state_stop, s,VM_CHANGE_STATE_PRI_HIGH );assert (s->vmse_stop !=NULL );vmstate_register_any (NULL , &vmstate_audio, s);
tests/audio/audio-stubs.c +7 -0
@@ -59,6 +59,13 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,return (VMChangeStateEntry *)&dummy_vmse; } +VMChangeStateEntry *qemu_add_vm_change_state_handler_prio ( +VMChangeStateHandler *cb,void *opaque, +VMChangeStateHandlerPri priority) +{ +return qemu_add_vm_change_state_handler (cb, opaque); +} +void qemu_del_vm_change_state_handler (VMChangeStateEntry *e) { }
PATCH v2 10/13 net: Split VM state change handler into start and stop parts
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This will allow moving the VM stop part of this handler into VM_CHANGE_STATE_PRI_HIGH.
--- net/net.c | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-)
net/net.c +36 -16
@@ -64,7 +64,8 @@# define CONFIG_NET_BRIDGE #endif -static VMChangeStateEntry *net_change_state_entry; +static VMChangeStateEntry *net_change_state_start_entry; +static VMChangeStateEntry *net_change_state_stop_entry;NetClientStateList net_clients; typedef struct NetdevQueueEntry {
@@ -1729,27 +1730,43 @@ void qmp_set_link(const char *name, bool up, Error **errp)net_client_set_link (ncs, queues, up); } -static void net_vm_change_state_handler (void *opaque,bool running, -RunState state) +static void net_vm_change_state_start (void *opaque,bool running, +RunState state) {NetClientState *nc;NetClientState *tmp; +if (!running) { +return ; + } + QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { -if (running) { -/* Flush queued packets and wake up backends. */ -if (nc->peer &&qemu_can_send_packet (nc)) { -qemu_flush_queued_packets (nc->peer); - } - }else { -/* Complete all queued packets, to guarantee we don't modify -* state later when VM is not running. -*/ -qemu_flush_or_purge_queued_packets (nc, true); +/* Flush queued packets and wake up backends. */ +if (nc->peer &&qemu_can_send_packet (nc)) { +qemu_flush_queued_packets (nc->peer); } } } +static void net_vm_change_state_stop (void *opaque,bool running, +RunState state) +{ +NetClientState *nc; +NetClientState *tmp; + +if (running) { +return ; + } + + QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { +/* +* Complete all queued packets, to guarantee we don't modify +* state later when VM is not running. +*/ +qemu_flush_or_purge_queued_packets (nc, true); + } +} +void net_cleanup (void ) {NetClientState *nc, **p = &QTAILQ_FIRST (&net_clients);
@@ -1799,7 +1816,8 @@ void net_cleanup(void) } } -qemu_del_vm_change_state_handler (net_change_state_entry); +qemu_del_vm_change_state_handler (net_change_state_start_entry); +qemu_del_vm_change_state_handler (net_change_state_stop_entry); }void net_check_clients (void )
@@ -1957,8 +1975,10 @@ static void netdev_init_modern(void)void net_init_clients (void ) { - net_change_state_entry = -qemu_add_vm_change_state_handler (net_vm_change_state_handler,NULL ); + net_change_state_start_entry = +qemu_add_vm_change_state_handler (net_vm_change_state_start,NULL ); + net_change_state_stop_entry = +qemu_add_vm_change_state_handler (net_vm_change_state_stop,NULL );QTAILQ_INIT (&net_clients);
PATCH v2 11/13 net: Make sure to stop net before VFIO gets non-DMA-capable
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
Although a VM state change normally happens under BQL some state change handlers may internally temporarily drop BQL.
On the migration source, the VM state change happens in the migration thread so this potentially allows main loop thread to acquire BQL and dispatch network backend handlers, like (at least) TAP.
In turn, these network backend handlers may call into emulated NIC TX path to do DMA.
Since VFIO devices start their transition into a state where they no longer can accept incoming DMA at VM_CHANGE_STATE_PRI_SYNC if net is stopped at VM_CHANGE_STATE_PRI_NORMAL in an unlikely but possible case that the aforementioned NIC DMA targets a VFIO device BAR it may reach a VFIO device that has already switched into a non-DMA-capable state.
Fix this by making sure that at VM stop network is stopped at HIGH priority level, so before VFIO devices start reaching states not capable of accepting incoming DMA.
--- net/net.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/net.c +2 -2
@@ -1977,8 +1977,8 @@ void net_init_clients(void) { net_change_state_start_entry =qemu_add_vm_change_state_handler (net_vm_change_state_start,NULL ); - net_change_state_stop_entry = -qemu_add_vm_change_state_handler (net_vm_change_state_stop,NULL ); + net_change_state_stop_entry =qemu_add_vm_change_state_handler_prio ( + net_vm_change_state_stop,NULL ,VM_CHANGE_STATE_PRI_HIGH );QTAILQ_INIT (&net_clients);
PATCH v2 12/13 virtio: Split VM state change handler into start and stop parts
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
This will allow moving the VM stop part of this handler into VM_CHANGE_STATE_PRI_HIGH.
--- hw/virtio/virtio.c | 32 +++++++++++++++++++++++++++----- include/hw/virtio/virtio.h | 3 ++- 2 files changed, 29 insertions(+), 6 deletions(-)
hw/virtio/virtio.c +27 -5
@@ -3733,12 +3733,12 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)void virtio_cleanup (VirtIODevice *vdev) { -qemu_del_vm_change_state_handler (vdev->vmstate); +qemu_del_vm_change_state_handler (vdev->vmstate_start); +qemu_del_vm_change_state_handler (vdev->vmstate_stop); } -static int virtio_vmstate_change (void *opaque,bool running,RunState state) +static int virtio_vmstate_change (VirtIODevice *vdev,bool running) { -VirtIODevice *vdev = opaque;BusState *qbus =qdev_get_parent_bus (DEVICE (vdev));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);bool backend_run = running &&virtio_device_started (vdev, vdev->status);
@@ -3761,6 +3761,26 @@ static int virtio_vmstate_change(void *opaque, bool running, RunState state)return 0 ; } +static int virtio_vmstate_change_start (void *opaque,bool running, +RunState state) +{ +if (!running) { +return 0 ; + } + +return virtio_vmstate_change (opaque, true); +} + +static int virtio_vmstate_change_stop (void *opaque,bool running, +RunState state) +{ +if (running) { +return 0 ; + } + +return virtio_vmstate_change (opaque, false); +} +void virtio_instance_init_common (Object *proxy_obj,void *data,size_t vdev_size,const char *vdev_name) {
@@ -3809,8 +3829,10 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) }else { vdev->config =NULL ; } - vdev->vmstate =qdev_add_vm_change_state_handler (DEVICE (vdev), -NULL , virtio_vmstate_change, vdev); + vdev->vmstate_start =qdev_add_vm_change_state_handler (DEVICE (vdev), +NULL , virtio_vmstate_change_start, vdev); + vdev->vmstate_stop =qdev_add_vm_change_state_handler (DEVICE (vdev), +NULL , virtio_vmstate_change_stop, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true;QSIMPLEQ_INIT (&vdev->shmem_list);
include/hw/virtio/virtio.h +2 -1
@@ -191,7 +191,8 @@ struct VirtIODevicebool start_on_kick;/* when virtio 1.0 feature has not been negotiated */ bool disable_legacy_check;bool vhost_started; -VMChangeStateEntry *vmstate; +VMChangeStateEntry *vmstate_start; +VMChangeStateEntry *vmstate_stop;char *bus_name;uint8_t device_endian;/**
PATCH v2 13/13 virtio: Make sure to stop virtio before VFIO gets non-DMA-capable
In reply to Maciej S. SzmigieroFrom: "Maciej S. Szmigiero" <[email protected]>
On a VM stop various virtio backends stop their requests from functions ultimately called from the generic virtio VM state change handler.
For example, virtio-blk flushes all of its in-flight I/O and waits for virtio_blk_dma_restart_bh() to complete from its stop_ioeventfd method. virtio-scsi-dataplane has similar in-flight I/O flushing code in its stop_ioeventfd method.
That stop_ioeventfd method is called from (for example) virtio_pci_vmstate_change(), which in turn is called from the generic virtio VM state change handler.
Since VFIO devices start their transition into a state where they no longer can accept incoming DMA at VM_CHANGE_STATE_PRI_SYNC if these backends are stopped at VM_CHANGE_STATE_PRI_NORMAL in an unlikely but possible case that DMA from these virtio devices target a VFIO device BAR it may reach a VFIO device that has already switched into a non-DMA-capable state.
Fix this by making sure that at VM stop virtio is stopped at HIGH priority level, so before VFIO devices start reaching states not capable of accepting incoming DMA.
--- hw/virtio/virtio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +2 -2
@@ -3831,8 +3831,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) } vdev->vmstate_start =qdev_add_vm_change_state_handler (DEVICE (vdev),NULL , virtio_vmstate_change_start, vdev); - vdev->vmstate_stop =qdev_add_vm_change_state_handler (DEVICE (vdev), -NULL , virtio_vmstate_change_stop, vdev); + vdev->vmstate_stop =qdev_add_vm_change_state_handler_full (DEVICE (vdev), +NULL , virtio_vmstate_change_stop, vdev,VM_CHANGE_STATE_PRI_HIGH ); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true;QSIMPLEQ_INIT (&vdev->shmem_list);