On 6/27/2019 3:31 PM, Dr. David Alan Gilbert wrote:
> * Kirti Wankhede (kwankh...@nvidia.com) wrote:
>> Define flags to be used as delimeter in migration file stream.
>> Added .save_setup and .save_cleanup functions. Mapped & unmapped migration
>> region from these functions at source during saving or pre-copy phase.
>> Set VFIO device state depending on VM's state. During live migration, VM is
>> running when .save_setup is called, _SAVING | _RUNNING state is set for VFIO
>> device. During save-restore, VM is paused, _SAVING state is set for VFIO
>> device.
>>
>> Signed-off-by: Kirti Wankhede <kwankh...@nvidia.com>
>> Reviewed-by: Neo Jia <c...@nvidia.com>
>> ---
>> hw/vfio/migration.c | 76
>> ++++++++++++++++++++++++++++++++++++++++++++++++++++-
>> 1 file changed, 75 insertions(+), 1 deletion(-)
>>
>> diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c
>> index 7f9858e6c995..fe0887c27664 100644
>> --- a/hw/vfio/migration.c
>> +++ b/hw/vfio/migration.c
>> @@ -22,6 +22,17 @@
>> #include "exec/ram_addr.h"
>> #include "pci.h"
>>
>> +/*
>> + * Flags used as delimiter:
>> + * 0xffffffff => MSB 32-bit all 1s
>> + * 0xef10 => emulated (virtual) function IO
>> + * 0x0000 => 16-bits reserved for flags
>> + */
>> +#define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL)
>> +#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL)
>> +#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL)
>> +#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL)
>> +
>> static void vfio_migration_region_exit(VFIODevice *vbasedev)
>> {
>> VFIOMigration *migration = vbasedev->migration;
>> @@ -96,6 +107,69 @@ static int vfio_migration_set_state(VFIODevice
>> *vbasedev, uint32_t state)
>> return 0;
>> }
>>
>> +/* ---------------------------------------------------------------------- */
>> +
>> +static int vfio_save_setup(QEMUFile *f, void *opaque)
>> +{
>> + VFIODevice *vbasedev = opaque;
>> + VFIOMigration *migration = vbasedev->migration;
>> + int ret;
>> +
>> + qemu_put_be64(f, VFIO_MIG_FLAG_DEV_SETUP_STATE);
>> +
>> + if (migration->region.buffer.mmaps) {
>> + qemu_mutex_lock_iothread();
>> + ret = vfio_region_mmap(&migration->region.buffer);
>> + qemu_mutex_unlock_iothread();
>> + if (ret) {
>> + error_report("Failed to mmap VFIO migration region %d: %s",
>> + migration->region.index, strerror(-ret));
>> + return ret;
>> + }
>> + }
>> +
>> + if (vbasedev->vm_running) {
>> + ret = vfio_migration_set_state(vbasedev,
>> + VFIO_DEVICE_STATE_RUNNING |
>> VFIO_DEVICE_STATE_SAVING);
>> + if (ret) {
>> + error_report("Failed to set state RUNNING and SAVING");
>> + return ret;
>> + }
>> + } else {
>> + ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_SAVING);
>> + if (ret) {
>> + error_report("Failed to set state STOP and SAVING");
>> + return ret;
>> + }
>> + }
>> +
>> + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
>> +
>> + ret = qemu_file_get_error(f);
>> + if (ret) {
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static void vfio_save_cleanup(void *opaque)
>> +{
>> + VFIODevice *vbasedev = opaque;
>> + VFIOMigration *migration = vbasedev->migration;
>> +
>> + if (migration->region.buffer.mmaps) {
>> + vfio_region_unmap(&migration->region.buffer);
>> + }
>> +}
>> +
>> +static SaveVMHandlers savevm_vfio_handlers = {
>> + .save_setup = vfio_save_setup,
>> + .save_cleanup = vfio_save_cleanup,
>> +};
>> +
>> +/* ---------------------------------------------------------------------- */
>> +
>> static void vfio_vmstate_change(void *opaque, int running, RunState state)
>> {
>> VFIODevice *vbasedev = opaque;
>> @@ -169,7 +243,7 @@ static int vfio_migration_init(VFIODevice *vbasedev,
>> }
>>
>> qemu_mutex_init(&vbasedev->migration->lock);
>> -
>> + register_savevm_live(NULL, "vfio", -1, 1, &savevm_vfio_handlers,
>> vbasedev);
>
> Does this work OK with multiple devices?
Yes. Tested with multiple vGPU devices.
> I think I'd expected you to pass a DeviceState as the first parameter
> for a real device like vfio.
> 'ram' and 'block' don't need to because they iterate over all RAM
> devices inside their save_setup's and similar handlers; for vfio I'd
> expect it to be per-device.
I do see handlers called per-device. I'll check passing DeviceState as
first parameter.
Thanks,
Kirti
>
> Dave
>
>> vbasedev->vm_state =
>> qemu_add_vm_change_state_handler(vfio_vmstate_change,
>> vbasedev);
>>
>> --
>> 2.7.0
>>
> --
> Dr. David Alan Gilbert / dgilb...@redhat.com / Manchester, UK
>