vfio/migration: Implement VFIO migration protocol v2
v4 cover vfio/migration: Implement VFIO migration protocol v2
Hello,
A long time has passed since v2 of this series was posted. During this time we had several KVM calls discussing the problems that were needed to be solved in order to move forward.
This version of the series includes quite some changes, and I believe that it addresses all the major problems we have discussed.
Following VFIO migration protocol v2 acceptance in kernel, this series implements VFIO migration according to the new v2 protocol and replaces the now deprecated v1 implementation.
The main differences between v1 and v2 migration protocols are:
1. VFIO device state is represented as a finite state machine instead of a bitmap.
2. The migration interface with kernel is done using VFIO_DEVICE_FEATURE ioctl and normal read() and write() instead of the migration region used in v1.
3. Migration protocol v2 currently doesn't support the pre-copy phase of migration.
Full description of the v2 protocol and the differences from v1 can be found here [1].
Patch list:
Patches 1-2 are patches taken from Juan's RFC [2].
As discussed in the KVM call, since we are going to add a new ioctl to get device data size while it's RUNNING, we don't need the stop and resume VM functionality from the RFC.
Patches 3-8 are prep patches fixing bugs, adding QEMUFile function that will be used later and refactoring v1 protocol code to make it easier to add v2 protocol.
Patches 9-12 implement v2 protocol and remove v1 protocol.
Patches 13-14 are preview patches (which are not for merging yet) that demonstrate how the new ioctl to get device state size will work once added.
Thanks.
Changes from v3 [3]:
- Rebased on latest master branch.
- Dropped patch #1 "migration: Remove res_compatible parameter" as it's not mandatory to this series and needs some further discussion.
- Dropped patch #3 "migration: Block migration comment or code is wrong" as it has been merged already.
- Addressed overlooked corner case reported by Vladimir in patch #4 "migration: Simplify migration_iteration_run()".
- Dropped patch #5 "vfio/migration: Fix wrong enum usage" as it has been merged already.
- In patch #12 "vfio/migration: Implement VFIO migration protocol v2":
1. Changed vfio_save_pending() to update res_precopy_only instead of res_postcopy_only (as VFIO migration doesn’t support postcopy).
2. Moved VFIOMigration->data_buffer allocation to vfio_save_setup() and its de-allocation to vfio_save_cleanup(), so now it's allocated when actually used (during migration and only on source side).
- Addressed Alex's comments:
1. Eliminated code duplication in patch #7 "vfio/migration: Allow migration without VFIO IOMMU dirty tracking support".
2. Removed redundant initialization of vfio_region_info in patch #10 "vfio/migration: Move migration v1 logic to vfio_migration_init()".
3. Added comment about VFIO_MIG_DATA_BUFFER_SIZE heuristic (and renamed to VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE).
4. Cast migration structs to their actual types instead of void *.
5. Return -errno and -EBADF instead of -1 in vfio_migration_set_state().
6. Set migration->device_state to new_state even in case of data_fd out of sync. Although migration will be aborted, setting device state succeeded so we should reflect that.
7. Renamed VFIO_MIG_PENDING_SIZE to VFIO_MIG_STOP_COPY_SIZE, set it to 100G and added a comment about the size choice.
8. Changed vfio_save_block() to return -errno on error.
9. Squashed Patch #14 to patch #12.
10. Adjusted migration data buffer size according to MIG_DATA_SIZE ioctl.
- In preview patch #17 "vfio/migration: Query device data size in vfio_save_pending()" - changed vfio_save_pending() to report VFIO_MIG_STOP_COPY_SIZE on any error.
- Added another preview patch "vfio/migration: Optimize vfio_save_pending()".
- Added ret value on some traces as suggested by David.
- Added Reviewed-By tags.
Changes from v2 [4]:
- Rebased on top of latest master branch.
- Added relevant patches from Juan's RFC [2] with minor changes:
1. Added Reviewed-by tag to patch #3 in the RFC.
2. Adjusted patch #6 to work without patch #4 in the RFC.
- Added a new patch "vfio/migration: Fix wrong enum usage" that fixes a small bug in v1 code. This patch has been sent a few weeks ago [5] but wasn't taken yet.
- Patch #2 (vfio/migration: Skip pre-copy if dirty page tracking is not supported):
1. Dropped this patch and replaced it with "vfio/migration: Allow migration without VFIO IOMMU dirty tracking support".
The new patch uses a different approach – instead of skipping pre-copy phase completely, QEMU VFIO code will mark RAM dirty (instead of kernel). This ensures that current migration behavior is not changed and SLA is taken into account.
- Patch #4 (vfio/common: Change vfio_devices_all_running_and_saving() logic to equivalent one):
1. Improved commit message to better explain the change.
- Patch #7 (vfio/migration: Implement VFIO migration protocol v2):
1. Enhanced vfio_migration_set_state() error reporting.
2. In vfio_save_complete_precopy() of v2 protocol - when changing device state to STOP, set recover state to ERROR instead of STOP as suggested by Joao.
3. Constify SaveVMHandlers of v2 protocol.
4. Modified trace_vfio_vmstate_change and trace_vfio_migration_set_state
to print device state string instead of enum.
5. Replaced qemu_put_buffer_async() with qemu_put_buffer() in vfio_save_block(), as requested by Juan.
6. Implemented v2 protocol version of vfio_save_pending() as requested by Juan. Until ioctl to get device state size is added, we just report some big hard coded value, as agreed in KVM call.
- Patch #9 (vfio/migration: Reset device if setting recover state fails):
1. Enhanced error reporting.
2. Set VFIOMigration->device_state to RUNNING after device reset.
- Patch #11 (docs/devel: Align vfio-migration docs to VFIO migration v2):
1. Adjusted vfio migration documentation to the added vfio_save_pending()
- Added the last patch (which is not for merging yet) that demonstrates how the new ioctl to get device state size will work once added.
Changes from v1 [6]:
- Split the big patch that replaced v1 with v2 into several patches as suggested by Joao, to make review easier.
- Change warn_report to warn_report_once when container doesn't support dirty tracking.
- Add Reviewed-by tag.
[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/T/
[3]
https://lore.kernel.org/qemu-devel/[email protected]/
[4]
https://lore.kernel.org/all/[email protected]/
[5]
https://lore.kernel.org/all/[email protected]/
[6]
https://lore.kernel.org/all/[email protected]/
Avihai Horon (12):
vfio/migration: Fix NULL pointer dereference bug vfio/migration: Allow migration without VFIO IOMMU dirty tracking
support
migration/qemu-file: Add qemu_file_get_to_fd() vfio/common: Change vfio_devices_all_running_and_saving() logic to
equivalent one
vfio/migration: Move migration v1 logic to vfio_migration_init() vfio/migration: Rename functions/structs related to v1 protocol vfio/migration: Implement VFIO migration protocol v2 vfio/migration: Remove VFIO migration protocol v1
vfio: Alphabetize migration section of VFIO trace-events file docs/devel: Align vfio-migration docs to VFIO migration v2 vfio/migration: Use VFIO_DEVICE_FEATURE_MIG_DATA_SIZE ioctl vfio/migration: Optimize vfio_save_pending()
Juan Quintela (2):
migration: No save_live_pending() method uses the QEMUFile parameter
migration: Simplify migration_iteration_run()
docs/devel/vfio-migration.rst | 68 ++- hw/s390x/s390-stattrib.c | 2 +- hw/vfio/common.c | 119 +++--- hw/vfio/migration.c | 727 +++++++++++---------------------- hw/vfio/trace-events | 29 +- include/hw/vfio/vfio-common.h | 10 +- include/migration/register.h | 3 +- linux-headers/linux/vfio.h | 13 + migration/block-dirty-bitmap.c | 3 +- migration/block.c | 2 +- migration/migration.c | 29 +- migration/qemu-file.c | 34 ++ migration/qemu-file.h | 1 + migration/ram.c | 2 +- migration/savevm.c | 7 +- migration/savevm.h | 3 +- 16 files changed, 430 insertions(+), 622 deletions(-)
Signature · 1 line
2.26.3
PATCH v4 1/14 migration: No save_live_pending() method uses the QEMUFile parameter
From: Juan Quintela <[email protected]>
So remove it everywhere.
--- hw/s390x/s390-stattrib.c | 2 +- hw/vfio/migration.c | 3 +-- include/migration/register.h | 3 +-- migration/block-dirty-bitmap.c | 3 +-- migration/block.c | 2 +- migration/migration.c | 4 ++-- migration/ram.c | 2 +- migration/savevm.c | 7 +++---- migration/savevm.h | 3 +-- 9 files changed, 12 insertions(+), 17 deletions(-)
hw/s390x/s390-stattrib.c +1 -1
@@ -182,7 +182,7 @@ static int cmma_save_setup(QEMUFile *f, void *opaque)return 0 ; } -static void cmma_save_pending (QEMUFile *f,void *opaque,uint64_t max_size, +static void cmma_save_pending (void *opaque,uint64_t max_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only)
hw/vfio/migration.c +1 -2
@@ -456,8 +456,7 @@ static void vfio_save_cleanup(void *opaque)trace_vfio_save_cleanup (vbasedev->name); } -static void vfio_save_pending (QEMUFile *f,void *opaque, -uint64_t threshold_size, +static void vfio_save_pending (void *opaque,uint64_t threshold_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only)
include/migration/register.h +1 -2
@@ -46,8 +46,7 @@ typedef struct SaveVMHandlers {/* This runs outside the iothread lock! */ int (*save_setup)(QEMUFile *f,void *opaque); -void (*save_live_pending)(QEMUFile *f,void *opaque, -uint64_t threshold_size, +void (*save_live_pending)(void *opaque,uint64_t threshold_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only);
migration/block-dirty-bitmap.c +1 -2
@@ -761,8 +761,7 @@ static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)return 0 ; } -static void dirty_bitmap_save_pending (QEMUFile *f,void *opaque, -uint64_t max_size, +static void dirty_bitmap_save_pending (void *opaque,uint64_t max_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only)
migration/block.c +1 -1
@@ -862,7 +862,7 @@ static int block_save_complete(QEMUFile *f, void *opaque)return 0 ; } -static void block_save_pending (QEMUFile *f,void *opaque,uint64_t max_size, +static void block_save_pending (void *opaque,uint64_t max_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only)
migration/migration.c +2 -2
@@ -3756,8 +3756,8 @@ static MigIterateState migration_iteration_run(MigrationState *s)uint64_t pending_size, pend_pre, pend_compat, pend_post;bool in_postcopy = s->state ==MIGRATION_STATUS_POSTCOPY_ACTIVE ; -qemu_savevm_state_pending (s->to_dst_file, s->threshold_size, &pend_pre, - &pend_compat, &pend_post); +qemu_savevm_state_pending (s->threshold_size, &pend_pre, &pend_compat, + &pend_post); pending_size = pend_pre + pend_compat + pend_post; trace_migrate_pending(pending_size, s->threshold_size,
migration/ram.c +1 -1
@@ -3441,7 +3441,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)return 0 ; } -static void ram_save_pending (QEMUFile *f,void *opaque,uint64_t max_size, +static void ram_save_pending (void *opaque,uint64_t max_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only)
migration/savevm.c +3 -4
@@ -1471,7 +1471,7 @@ flush:* the result is split into the amount for units that can and * for units that can't do postcopy. */ -void qemu_savevm_state_pending (QEMUFile *f,uint64_t threshold_size, +void qemu_savevm_state_pending (uint64_t threshold_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only)
@@ -1492,9 +1492,8 @@ void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,continue ; } } - se->ops->save_live_pending (f, se->opaque, threshold_size, - res_precopy_only, res_compatible, - res_postcopy_only); + se->ops->save_live_pending (se->opaque, threshold_size, res_precopy_only, + res_compatible, res_postcopy_only); } }
migration/savevm.h +1 -2
@@ -40,8 +40,7 @@ void qemu_savevm_state_cleanup(void);void qemu_savevm_state_complete_postcopy (QEMUFile *f);int qemu_savevm_state_complete_precopy (QEMUFile *f,bool iterable_only,bool inactivate_disks); -void qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size, -uint64_t *res_precopy_only, +void qemu_savevm_state_pending (uint64_t max_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only);void qemu_savevm_send_ping (QEMUFile *f,uint32_t value);
Signature · 1 line
2.26.3
PATCH v4 2/14 migration: Simplify migration_iteration_run()
In reply to Avihai HoronFrom: Juan Quintela <[email protected]>
--- migration/migration.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
migration/migration.c +13 -12
@@ -3763,23 +3763,24 @@ static MigIterateState migration_iteration_run(MigrationState *s)trace_migrate_pending (pending_size, s->threshold_size, pend_pre, pend_compat, pend_post); -if (pending_size && pending_size >= s->threshold_size) { -/* Still a significant amount to transfer */ -if (!in_postcopy && pend_pre <= s->threshold_size && -qatomic_read (&s->start_postcopy)) { -if (postcopy_start (s)) { -error_report ("%s: postcopy failed to start" , __func__); - } -return MIG_ITERATE_SKIP ; - } -/* Just another iteration step */ -qemu_savevm_state_iterate (s->to_dst_file, in_postcopy); - }else { + +if (!pending_size || pending_size < s->threshold_size) {trace_migration_thread_low_pending (pending_size);migration_completion (s);return MIG_ITERATE_BREAK ; } +/* Still a significant amount to transfer */ +if (!in_postcopy && pend_pre <= s->threshold_size && +qatomic_read (&s->start_postcopy)) { +if (postcopy_start (s)) { +error_report ("%s: postcopy failed to start" , __func__); + } +return MIG_ITERATE_SKIP ; + } + +/* Just another iteration step */ +qemu_savevm_state_iterate (s->to_dst_file, in_postcopy);return MIG_ITERATE_RESUME ; }
Signature · 1 line
2.26.3
PATCH v4 3/14 vfio/migration: Fix NULL pointer dereference bug
In reply to Avihai HoronAs part of its error flow, vfio_vmstate_change() accesses MigrationState->to_dst_file without any checks. This can cause a NULL pointer dereference if the error flow is taken and MigrationState->to_dst_file is not set.
For example, this can happen if VM is started or stopped not during migration and vfio_vmstate_change() error flow is taken, as MigrationState->to_dst_file is not set at that time.
Fix it by checking that MigrationState->to_dst_file is set before using it.
--- hw/vfio/migration.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/migration.c +3 -1
@@ -743,7 +743,9 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state)*/ error_report ("%s: Failed to set device state 0x%x" , vbasedev->name, (migration->device_state & mask) | value); -qemu_file_set_error (migrate_get_current ()->to_dst_file, ret); +if (migrate_get_current ()->to_dst_file) { +qemu_file_set_error (migrate_get_current ()->to_dst_file, ret); + } } vbasedev->migration->vm_running = running; trace_vfio_vmstate_change(vbasedev->name, running, RunState_str(state),
Signature · 1 line
2.26.3
PATCH v4 4/14 vfio/migration: Allow migration without VFIO IOMMU dirty tracking support
In reply to Avihai HoronCurrently, if IOMMU of a VFIO container doesn't support dirty page tracking, migration is blocked. This is because a DMA-able VFIO device can dirty RAM pages without updating QEMU about it, thus breaking the migration.
However, this doesn't mean that migration can't be done at all. In such case, allow migration and let QEMU VFIO code mark the entire bitmap dirty.
This guarantees that all pages that might have gotten dirty are reported back, and thus guarantees a valid migration even without VFIO IOMMU dirty tracking support.
The motivation for this patch is the future introduction of iommufd [1]. iommufd will directly implement the /dev/vfio/vfio container IOCTLs by mapping them into its internal ops, allowing the usage of these IOCTLs over iommufd. However, VFIO IOMMU dirty tracking will not be supported by this VFIO compatibility API.
This patch will allow migration by hosts that use the VFIO compatibility API and prevent migration regressions caused by the lack of VFIO IOMMU dirty tracking support.
[1] https://lore.kernel.org/kvm/[email protected]/
--- hw/vfio/common.c | 100 ++++++++++++++++++++++++++------------------ hw/vfio/migration.c | 3 +- 2 files changed, 61 insertions(+), 42 deletions(-)
hw/vfio/common.c +60 -40
@@ -397,51 +397,61 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container, IOMMUTLBEntry *iotlb) {struct vfio_iommu_type1_dma_unmap *unmap; -struct vfio_bitmap *bitmap; +struct vfio_bitmap *vbitmap; +unsigned long *bitmap; +uint64_t bitmap_size;uint64_t pages =REAL_HOST_PAGE_ALIGN (size) /qemu_real_host_page_size ();int ret; - unmap =g_malloc0 (sizeof (*unmap) +sizeof (*bitmap)); + unmap =g_malloc0 (sizeof (*unmap) +sizeof (*vbitmap)); - unmap->argsz =sizeof (*unmap) +sizeof (*bitmap); + unmap->argsz =sizeof (*unmap); unmap->iova = iova; unmap->size = size; - unmap->flags |=VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP ; - bitmap = (struct vfio_bitmap *)&unmap->data; + bitmap_size =ROUND_UP (pages,sizeof (__u64) *BITS_PER_BYTE ) / +BITS_PER_BYTE ; + bitmap =g_try_malloc0 (bitmap_size); +if (!bitmap) { + ret = -ENOMEM ; + goto unmap_exit; + } + +if (!container->dirty_pages_supported) { +bitmap_set (bitmap,0 , pages); + goto do_unmap; + } + + unmap->argsz +=sizeof (*vbitmap); + unmap->flags =VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP ; + + vbitmap = (struct vfio_bitmap *)&unmap->data; + vbitmap->data = (__u64 *)bitmap;/* * cpu_physical_memory_set_dirty_lebitmap() supports pages in bitmap of * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize * to qemu_real_host_page_size. */ + vbitmap->pgsize =qemu_real_host_page_size (); + vbitmap->size = bitmap_size; - bitmap->pgsize =qemu_real_host_page_size (); - bitmap->size =ROUND_UP (pages,sizeof (__u64) *BITS_PER_BYTE ) / -BITS_PER_BYTE ; - -if (bitmap->size > container->max_dirty_bitmap_size) { -error_report ("UNMAP: Size of bitmap too big 0x%" PRIx64, - (uint64_t )bitmap->size); +if (bitmap_size > container->max_dirty_bitmap_size) { +error_report ("UNMAP: Size of bitmap too big 0x%" PRIx64, bitmap_size); ret = -E2BIG ; goto unmap_exit; } - bitmap->data =g_try_malloc0 (bitmap->size); -if (!bitmap->data) { - ret = -ENOMEM ; - goto unmap_exit; - } - +do_unmap: ret =ioctl (container->fd,VFIO_IOMMU_UNMAP_DMA , unmap);if (!ret) { -cpu_physical_memory_set_dirty_lebitmap ((unsigned long *)bitmap->data, - iotlb->translated_addr, pages); +cpu_physical_memory_set_dirty_lebitmap (bitmap, iotlb->translated_addr, + pages); }else {error_report ("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m" ); } -g_free (bitmap->data); unmap_exit: +g_free (bitmap);g_free (unmap);return ret; }
@@ -460,8 +470,7 @@ static int vfio_dma_unmap(VFIOContainer *container, .size = size, }; -if (iotlb && container->dirty_pages_supported && -vfio_devices_all_running_and_saving (container)) { +if (iotlb &&vfio_devices_all_running_and_saving (container)) {return vfio_dma_unmap_bitmap (container, iova, size, iotlb); }
@@ -1201,6 +1210,10 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start) .argsz = sizeof(dirty), }; +if (!container->dirty_pages_supported) { +return ; + } +if (start) { dirty.flags =VFIO_IOMMU_DIRTY_PAGES_FLAG_START ; }else {
@@ -1231,11 +1244,26 @@ static void vfio_listener_log_global_stop(MemoryListener *listener)static int vfio_get_dirty_bitmap (VFIOContainer *container,uint64_t iova,uint64_t size,ram_addr_t ram_addr) { -struct vfio_iommu_type1_dirty_bitmap *dbitmap; +struct vfio_iommu_type1_dirty_bitmap *dbitmap =NULL ;struct vfio_iommu_type1_dirty_bitmap_get *range; +unsigned long *bitmap; +uint64_t bitmap_size;uint64_t pages;int ret; + pages =REAL_HOST_PAGE_ALIGN (size) /qemu_real_host_page_size (); + bitmap_size =ROUND_UP (pages,sizeof (__u64) *BITS_PER_BYTE ) / +BITS_PER_BYTE ; + bitmap =g_try_malloc0 (bitmap_size); +if (!bitmap) { +return -ENOMEM ; + } + +if (!container->dirty_pages_supported) { +bitmap_set (bitmap,0 , pages); + goto set_dirty; + } + dbitmap =g_malloc0 (sizeof (*dbitmap) +sizeof (*range)); dbitmap->argsz =sizeof (*dbitmap) +sizeof (*range);
@@ -1250,15 +1278,8 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,* to qemu_real_host_page_size. */ range->bitmap.pgsize =qemu_real_host_page_size (); - - pages =REAL_HOST_PAGE_ALIGN (range->size) /qemu_real_host_page_size (); - range->bitmap.size =ROUND_UP (pages,sizeof (__u64) *BITS_PER_BYTE ) / -BITS_PER_BYTE ; - range->bitmap.data =g_try_malloc0 (range->bitmap.size); -if (!range->bitmap.data) { - ret = -ENOMEM ; - goto err_out; - } + range->bitmap.size = bitmap_size; + range->bitmap.data = (__u64 *)bitmap; ret =ioctl (container->fd,VFIO_IOMMU_DIRTY_PAGES , dbitmap);if (ret) {
@@ -1268,13 +1289,13 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, goto err_out; } -cpu_physical_memory_set_dirty_lebitmap ((unsigned long *)range->bitmap.data, - ram_addr, pages); +set_dirty: +cpu_physical_memory_set_dirty_lebitmap (bitmap, ram_addr, pages); -trace_vfio_get_dirty_bitmap (container->fd, range->iova, range->size, - range->bitmap.size, ram_addr); +trace_vfio_get_dirty_bitmap (container->fd, iova, size, bitmap_size, + ram_addr); err_out: -g_free (range->bitmap.data); +g_free (bitmap);g_free (dbitmap);return ret;
@@ -1409,8 +1430,7 @@ static void vfio_listener_log_sync(MemoryListener *listener, {VFIOContainer *container =container_of (listener, VFIOContainer, listener); -if (vfio_listener_skipped_section (section) || - !container->dirty_pages_supported) { +if (vfio_listener_skipped_section (section)) {return ; }
hw/vfio/migration.c +1 -2
@@ -860,11 +860,10 @@ int64_t vfio_mig_bytes_transferred(void)int vfio_migration_probe (VFIODevice *vbasedev,Error **errp) { -VFIOContainer *container = vbasedev->group->container;struct vfio_region_info *info =NULL ;int ret = -ENOTSUP ; -if (!vbasedev->enable_migration || !container->dirty_pages_supported) { +if (!vbasedev->enable_migration) { goto add_blocker; }
Signature · 1 line
2.26.3
PATCH v4 5/14 migration/qemu-file: Add qemu_file_get_to_fd()
In reply to Avihai HoronAdd new function qemu_file_get_to_fd() that allows reading data from QEMUFile and writing it straight into a given fd.
This will be used later in VFIO migration code.
--- migration/qemu-file.c | 34 ++++++++++++++++++++++++++++++++++ migration/qemu-file.h | 1 + 2 files changed, 35 insertions(+)
migration/qemu-file.c +34 -0
@@ -940,3 +940,37 @@ QIOChannel *qemu_file_get_ioc(QEMUFile *file) {return file->ioc; } + +/* +* Read size bytes from QEMUFile f and write them to fd. +*/ +int qemu_file_get_to_fd (QEMUFile *f,int fd,size_t size) +{ +while (size) { +size_t pending = f->buf_size - f->buf_index; +ssize_t rc; + +if (!pending) { + rc =qemu_fill_buffer (f); +if (rc <0 ) { +return rc; + } +if (rc ==0 ) { +return -1 ; + } +continue ; + } + + rc =write (fd, f->buf + f->buf_index,MIN (pending, size)); +if (rc <0 ) { +return rc; + } +if (rc ==0 ) { +return -1 ; + } + f->buf_index += rc; + size -= rc; + } + +return 0 ; +}
migration/qemu-file.h +1 -0
@@ -148,6 +148,7 @@ int qemu_file_shutdown(QEMUFile *f);QEMUFile *qemu_file_get_return_path (QEMUFile *f);void qemu_fflush (QEMUFile *f);void qemu_file_set_blocking (QEMUFile *f,bool block); +int qemu_file_get_to_fd (QEMUFile *f,int fd,size_t size);void ram_control_before_iterate (QEMUFile *f,uint64_t flags);void ram_control_after_iterate (QEMUFile *f,uint64_t flags);
Signature · 1 line
2.26.3
PATCH v4 6/14 vfio/common: Change vfio_devices_all_running_and_saving() logic to equivalent one
In reply to Avihai Horonvfio_devices_all_running_and_saving() is used to check if migration is in pre-copy phase. This is done by checking if migration is in setup or active states and if all VFIO devices are in pre-copy state, i.e. _SAVING | _RUNNING.
In VFIO migration protocol v2 pre-copy support is made optional. Hence, a matching v2 protocol pre-copy state can't be used here.
As preparation for adding v2 protocol, change vfio_devices_all_running_and_saving() logic such that it doesn't use the VFIO pre-copy state.
The new equivalent logic checks if migration is in active state and if all VFIO devices are in running state [1]. No functional changes intended.
[1] Note that checking if migration is in setup or active states and if all VFIO devices are in running state doesn't guarantee that we are in pre-copy phase, thus we check if migration is only in active state.
--- hw/vfio/common.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
hw/vfio/common.c +9 -6
@@ -40,6 +40,7 @@#include "trace.h" #include "qapi/error.h" #include "migration/migration.h" +#include "migration/misc.h" #include "sysemu/tpm.h" VFIOGroupList vfio_group_list =
@@ -363,13 +364,16 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)return true; } -static bool vfio_devices_all_running_and_saving (VFIOContainer *container) +/* +* Check if all VFIO devices are running and migration is active, which is +* essentially equivalent to the migration being in pre-copy phase. +*/ +static bool vfio_devices_all_running_and_mig_active (VFIOContainer *container) {VFIOGroup *group;VFIODevice *vbasedev; -MigrationState *ms =migrate_get_current (); -if (!migration_is_setup_or_active (ms->state)) { +if (!migration_is_active (migrate_get_current ())) {return false; }
@@ -381,8 +385,7 @@ static bool vfio_devices_all_running_and_saving(VFIOContainer *container)return false; } -if ((migration->device_state &VFIO_DEVICE_STATE_V1_SAVING ) && - (migration->device_state &VFIO_DEVICE_STATE_V1_RUNNING )) { +if (migration->device_state &VFIO_DEVICE_STATE_V1_RUNNING ) {continue ; }else {return false;
@@ -470,7 +473,7 @@ static int vfio_dma_unmap(VFIOContainer *container, .size = size, }; -if (iotlb &&vfio_devices_all_running_and_saving (container)) { +if (iotlb &&vfio_devices_all_running_and_mig_active (container)) {return vfio_dma_unmap_bitmap (container, iova, size, iotlb); }
Signature · 1 line
2.26.3
PATCH v4 7/14 vfio/migration: Move migration v1 logic to vfio_migration_init()
In reply to Avihai HoronMove vfio_dev_get_region_info() logic from vfio_migration_probe() to vfio_migration_init(). This logic is specific to v1 protocol and moving it will make it easier to add the v2 protocol implementation later. No functional changes intended.
--- hw/vfio/migration.c | 30 +++++++++++++++--------------- hw/vfio/trace-events | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-)
hw/vfio/migration.c +15 -15
@@ -788,14 +788,14 @@ static void vfio_migration_exit(VFIODevice *vbasedev) vbasedev->migration =NULL ; } -static int vfio_migration_init (VFIODevice *vbasedev, -struct vfio_region_info *info) +static int vfio_migration_init (VFIODevice *vbasedev) {int ret;Object *obj;VFIOMigration *migration;char id[256 ] ="" ; g_autofree char *path = NULL, *oid = NULL; +struct vfio_region_info *info;if (!vbasedev->ops->vfio_get_object) {return -EINVAL ;
@@ -806,6 +806,14 @@ static int vfio_migration_init(VFIODevice *vbasedev,return -EINVAL ; } + ret =vfio_get_dev_region_info (vbasedev, +VFIO_REGION_TYPE_MIGRATION_DEPRECATED , +VFIO_REGION_SUBTYPE_MIGRATION_DEPRECATED , + &info); +if (ret) { +return ret; + } + vbasedev->migration =g_new0 (VFIOMigration,1 ); vbasedev->migration->device_state =VFIO_DEVICE_STATE_V1_RUNNING ; vbasedev->migration->vm_running =runstate_is_running ();
@@ -825,6 +833,8 @@ static int vfio_migration_init(VFIODevice *vbasedev, goto err; } +g_free (info); + migration = vbasedev->migration; migration->vbasedev = vbasedev;
@@ -847,6 +857,7 @@ static int vfio_migration_init(VFIODevice *vbasedev,return 0 ; err: +g_free (info);vfio_migration_exit (vbasedev);return ret; }
@@ -860,34 +871,23 @@ int64_t vfio_mig_bytes_transferred(void)int vfio_migration_probe (VFIODevice *vbasedev,Error **errp) { -struct vfio_region_info *info =NULL ;int ret = -ENOTSUP ;if (!vbasedev->enable_migration) { goto add_blocker; } - ret =vfio_get_dev_region_info (vbasedev, -VFIO_REGION_TYPE_MIGRATION_DEPRECATED , -VFIO_REGION_SUBTYPE_MIGRATION_DEPRECATED , - &info); -if (ret) { - goto add_blocker; - } - - ret =vfio_migration_init (vbasedev, info); + ret =vfio_migration_init (vbasedev);if (ret) { goto add_blocker; } -trace_vfio_migration_probe (vbasedev->name, info->index); -g_free (info); +trace_vfio_migration_probe (vbasedev->name);return 0 ; add_blocker:error_setg (&vbasedev->migration_blocker,"VFIO device doesn't support migration" ); -g_free (info); ret =migrate_add_blocker (vbasedev->migration_blocker, errp);if (ret <0 ) {
hw/vfio/trace-events +1 -1
@@ -148,7 +148,7 @@ vfio_display_edid_update(uint32_t prefx, uint32_t prefy) "%ux%u" vfio_display_edid_write_error(void) "" # migration.c -vfio_migration_probe(const char *name, uint32_t index) " (%s) Region %d" +vfio_migration_probe(const char *name) " (%s)" vfio_migration_set_state(const char *name, uint32_t state) " (%s) state %d" vfio_vmstate_change(const char *name, int running, const char *reason, uint32_t dev_state) " (%s) running %d reason %s device state %d" vfio_migration_state_notifier(const char *name, const char *state) " (%s) state %s"
Signature · 1 line
2.26.3
PATCH v4 8/14 vfio/migration: Rename functions/structs related to v1 protocol
In reply to Avihai HoronTo avoid name collisions, rename functions and structs related to VFIO migration protocol v1. This will allow the two protocols to co-exist when v2 protocol is added, until v1 is removed. No functional changes intended.
--- hw/vfio/common.c | 6 +- hw/vfio/migration.c | 104 +++++++++++++++++----------------- hw/vfio/trace-events | 10 ++-- include/hw/vfio/vfio-common.h | 2 +- 4 files changed, 61 insertions(+), 61 deletions(-)
hw/vfio/common.c +3 -3
@@ -355,8 +355,8 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)return false; } -if ((vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) - && (migration->device_state &VFIO_DEVICE_STATE_V1_RUNNING )) { +if ((vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && + (migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING )) {return false; } }
@@ -385,7 +385,7 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)return false; } -if (migration->device_state &VFIO_DEVICE_STATE_V1_RUNNING ) { +if (migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING ) {continue ; }else {return false;
hw/vfio/migration.c +52 -52
263 lines not on this page · raw message
hw/vfio/trace-events +5 -5
@@ -149,20 +149,20 @@ vfio_display_edid_write_error(void) "" # migration.c vfio_migration_probe(const char *name) " (%s)" -vfio_migration_set_state(const char *name, uint32_t state) " (%s) state %d" -vfio_vmstate_change(const char *name, int running, const char *reason, uint32_t dev_state) " (%s) running %d reason %s device state %d" +vfio_migration_v1_set_state(const char *name, uint32_t state) " (%s) state %d" +vfio_v1_vmstate_change(const char *name, int running, const char *reason, uint32_t dev_state) " (%s) running %d reason %s device state %d" vfio_migration_state_notifier(const char *name, const char *state) " (%s) state %s" vfio_save_setup(const char *name) " (%s)" vfio_save_cleanup(const char *name) " (%s)" vfio_save_buffer(const char *name, uint64_t data_offset, uint64_t data_size, uint64_t pending) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64" pending 0x%"PRIx64 vfio_update_pending(const char *name, uint64_t pending) " (%s) pending 0x%"PRIx64 vfio_save_device_config_state(const char *name) " (%s)" -vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64 +vfio_v1_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64 vfio_save_iterate(const char *name, int data_size) " (%s) data_size %d" -vfio_save_complete_precopy(const char *name) " (%s)" +vfio_v1_save_complete_precopy(const char *name) " (%s)" vfio_load_device_config_state(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 -vfio_load_state_device_data(const char *name, uint64_t data_offset, uint64_t data_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 +vfio_v1_load_state_device_data(const char *name, uint64_t data_offset, uint64_t data_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 vfio_load_cleanup(const char *name) " (%s)" vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64 vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64
include/hw/vfio/vfio-common.h +1 -1
@@ -62,7 +62,7 @@ typedef struct VFIOMigration {struct VFIODevice *vbasedev;VMChangeStateEntry *vm_state;VFIORegion region; -uint32_t device_state; +uint32_t device_state_v1;int vm_running;Notifier migration_state;uint64_t pending_bytes;
Signature · 1 line
2.26.3
PATCH v4 9/14 vfio/migration: Implement VFIO migration protocol v2
In reply to Avihai HoronAdd implementation of VFIO migration protocol v2. The two protocols, v1 and v2, will co-exist and in next patch v1 protocol will be removed.
There are several main differences between v1 and v2 protocols:
- VFIO device state is now represented as a finite state machine instead of a bitmap.
- Migration interface with kernel is now done using VFIO_DEVICE_FEATURE ioctl and normal read() and write() instead of the migration region.
- VFIO migration protocol v2 currently doesn't support the pre-copy phase of migration.
Detailed information about VFIO migration protocol v2 and difference compared to v1 can be found here [1].
[1]
https://lore.kernel.org/all/[email protected]/
--- hw/vfio/common.c | 19 +- hw/vfio/migration.c | 420 +++++++++++++++++++++++++++++++--- hw/vfio/trace-events | 6 + include/hw/vfio/vfio-common.h | 5 + 4 files changed, 411 insertions(+), 39 deletions(-)
hw/vfio/common.c +17 -2
@@ -355,10 +355,18 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)return false; } -if ((vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && +if (!migration->v2 && + (vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && (migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING )) {return false; } + +if (migration->v2 && + (vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && + (migration->device_state ==VFIO_DEVICE_STATE_RUNNING || + migration->device_state ==VFIO_DEVICE_STATE_RUNNING_P2P )) { +return false; + } } }return true;
@@ -385,7 +393,14 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)return false; } -if (migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING ) { +if (!migration->v2 && + migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING ) { +continue ; + } + +if (migration->v2 && + (migration->device_state ==VFIO_DEVICE_STATE_RUNNING || + migration->device_state ==VFIO_DEVICE_STATE_RUNNING_P2P )) {continue ; }else {return false;
hw/vfio/migration.c +383 -37
539 lines not on this page · raw message
hw/vfio/trace-events +6 -0
@@ -149,7 +149,9 @@ vfio_display_edid_write_error(void) "" # migration.c vfio_migration_probe(const char *name) " (%s)" +vfio_migration_set_state(const char *name, const char *state) " (%s) state %s" vfio_migration_v1_set_state(const char *name, uint32_t state) " (%s) state %d" +vfio_vmstate_change(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s" vfio_v1_vmstate_change(const char *name, int running, const char *reason, uint32_t dev_state) " (%s) running %d reason %s device state %d" vfio_migration_state_notifier(const char *name, const char *state) " (%s) state %s" vfio_save_setup(const char *name) " (%s)"
@@ -157,12 +159,16 @@ vfio_save_cleanup(const char *name) " (%s)" vfio_save_buffer(const char *name, uint64_t data_offset, uint64_t data_size, uint64_t pending) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64" pending 0x%"PRIx64 vfio_update_pending(const char *name, uint64_t pending) " (%s) pending 0x%"PRIx64 vfio_save_device_config_state(const char *name) " (%s)" +vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible, uint64_t stopcopy) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64" stopcopy size 0x%"PRIx64 vfio_v1_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64 vfio_save_iterate(const char *name, int data_size) " (%s) data_size %d" +vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_v1_save_complete_precopy(const char *name) " (%s)" vfio_load_device_config_state(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_v1_load_state_device_data(const char *name, uint64_t data_offset, uint64_t data_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 +vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size 0x%"PRIx64" ret %d" vfio_load_cleanup(const char *name) " (%s)" vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64 vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 +vfio_save_block(const char *name, int data_size) " (%s) data_size %d"
include/hw/vfio/vfio-common.h +5 -0
@@ -66,6 +66,11 @@ typedef struct VFIOMigration {int vm_running;Notifier migration_state;uint64_t pending_bytes; +enum vfio_device_mig_state device_state; +int data_fd; +void *data_buffer; +size_t data_buffer_size; +bool v2; } VFIOMigration; typedef struct VFIOAddressSpace {
Signature · 1 line
2.26.3
PATCH v4 10/14 vfio/migration: Remove VFIO migration protocol v1
In reply to Avihai HoronNow that v2 protocol implementation has been added, remove the deprecated v1 implementation.
--- hw/vfio/common.c | 19 +- hw/vfio/migration.c | 695 +--------------------------------- hw/vfio/trace-events | 8 - include/hw/vfio/vfio-common.h | 5 - 4 files changed, 22 insertions(+), 705 deletions(-)
hw/vfio/common.c +3 -16
@@ -355,14 +355,7 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container)return false; } -if (!migration->v2 && - (vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && - (migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING )) { -return false; - } - -if (migration->v2 && - (vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && +if ((vbasedev->pre_copy_dirty_page_tracking ==ON_OFF_AUTO_OFF ) && (migration->device_state ==VFIO_DEVICE_STATE_RUNNING || migration->device_state ==VFIO_DEVICE_STATE_RUNNING_P2P )) {return false;
@@ -393,14 +386,8 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container)return false; } -if (!migration->v2 && - migration->device_state_v1 &VFIO_DEVICE_STATE_V1_RUNNING ) { -continue ; - } - -if (migration->v2 && - (migration->device_state ==VFIO_DEVICE_STATE_RUNNING || - migration->device_state ==VFIO_DEVICE_STATE_RUNNING_P2P )) { +if (migration->device_state ==VFIO_DEVICE_STATE_RUNNING || + migration->device_state ==VFIO_DEVICE_STATE_RUNNING_P2P ) {continue ; }else {return false;
hw/vfio/migration.c +19 -676
811 lines not on this page · raw message
hw/vfio/trace-events +0 -8
@@ -150,23 +150,15 @@ vfio_display_edid_write_error(void) "" # migration.c vfio_migration_probe(const char *name) " (%s)" vfio_migration_set_state(const char *name, const char *state) " (%s) state %s" -vfio_migration_v1_set_state(const char *name, uint32_t state) " (%s) state %d" vfio_vmstate_change(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s" -vfio_v1_vmstate_change(const char *name, int running, const char *reason, uint32_t dev_state) " (%s) running %d reason %s device state %d" vfio_migration_state_notifier(const char *name, const char *state) " (%s) state %s" vfio_save_setup(const char *name) " (%s)" vfio_save_cleanup(const char *name) " (%s)" -vfio_save_buffer(const char *name, uint64_t data_offset, uint64_t data_size, uint64_t pending) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64" pending 0x%"PRIx64 -vfio_update_pending(const char *name, uint64_t pending) " (%s) pending 0x%"PRIx64 vfio_save_device_config_state(const char *name) " (%s)" vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible, uint64_t stopcopy) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64" stopcopy size 0x%"PRIx64 -vfio_v1_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64 -vfio_save_iterate(const char *name, int data_size) " (%s) data_size %d" vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" -vfio_v1_save_complete_precopy(const char *name) " (%s)" vfio_load_device_config_state(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 -vfio_v1_load_state_device_data(const char *name, uint64_t data_offset, uint64_t data_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size 0x%"PRIx64" ret %d" vfio_load_cleanup(const char *name) " (%s)" vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64
include/hw/vfio/vfio-common.h +0 -5
@@ -61,16 +61,11 @@ typedef struct VFIORegion {typedef struct VFIOMigration {struct VFIODevice *vbasedev;VMChangeStateEntry *vm_state; -VFIORegion region; -uint32_t device_state_v1; -int vm_running;Notifier migration_state; -uint64_t pending_bytes;enum vfio_device_mig_state device_state;int data_fd;void *data_buffer;size_t data_buffer_size; -bool v2; }VFIOMigration ; typedef struct VFIOAddressSpace {
Signature · 1 line
2.26.3
PATCH v4 11/14 vfio: Alphabetize migration section of VFIO trace-events file
In reply to Avihai HoronSort the migration section of VFIO trace events file alphabetically and move two misplaced traces to common.c section.
--- hw/vfio/trace-events | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
hw/vfio/trace-events +10 -10
@@ -119,6 +119,8 @@ vfio_region_sparse_mmap_header(const char *name, int index, int nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%0x8" vfio_dma_unmap_overflow_workaround(void) "" +vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64 +vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 # platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d"
@@ -148,19 +150,17 @@ vfio_display_edid_update(uint32_t prefx, uint32_t prefy) "%ux%u" vfio_display_edid_write_error(void) "" # migration.c +vfio_load_cleanup(const char *name) " (%s)" +vfio_load_device_config_state(const char *name) " (%s)" +vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 +vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size 0x%"PRIx64" ret %d" vfio_migration_probe(const char *name) " (%s)" vfio_migration_set_state(const char *name, const char *state) " (%s) state %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_migration_state_notifier(const char *name, const char *state) " (%s) state %s" -vfio_save_setup(const char *name) " (%s)" +vfio_save_block(const char *name, int data_size) " (%s) data_size %d" vfio_save_cleanup(const char *name) " (%s)" +vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_save_device_config_state(const char *name) " (%s)" vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible, uint64_t stopcopy) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64" stopcopy size 0x%"PRIx64 -vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" -vfio_load_device_config_state(const char *name) " (%s)" -vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 -vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size 0x%"PRIx64" ret %d" -vfio_load_cleanup(const char *name) " (%s)" -vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64 -vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 -vfio_save_block(const char *name, int data_size) " (%s) data_size %d" +vfio_save_setup(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"
Signature · 1 line
2.26.3
PATCH v4 12/14 docs/devel: Align vfio-migration docs to VFIO migration v2
In reply to Avihai HoronAlign the vfio-migration documentation to VFIO migration protocol v2.
--- docs/devel/vfio-migration.rst | 68 ++++++++++++++++------------------- 1 file changed, 30 insertions(+), 38 deletions(-)
docs/devel/vfio-migration.rst +30 -38
@@ -7,46 +7,39 @@ the guest is running on source host and restoring this saved state on the destination host. This document details how saving and restoring of VFIO devices is done in QEMU. -Migration of VFIO devices consists of two phases: the optional pre-copy phase, -and the stop-and-copy phase. The pre-copy phase is iterative and allows to -accommodate VFIO devices that have a large amount of data that needs to be -transferred. The iterative pre-copy phase of migration allows for the guest to -continue whilst the VFIO device state is transferred to the destination, this -helps to reduce the total downtime of the VM. VFIO devices can choose to skip -the pre-copy phase of migration by returning pending_bytes as zero during the -pre-copy phase. +Migration of VFIO devices currently consists of a single stop-and-copy phase. +During the stop-and-copy phase the guest is stopped and the entire VFIO device +data is transferred to the destination. + +The pre-copy phase of migration is currently not supported for VFIO devices, +so VFIO device data is not transferred during pre-copy phase. A detailed description of the UAPI for VFIO device migration can be found in -the comment for the ``vfio_device_migration_info`` structure in the header -file linux-headers/linux/vfio.h. +the comment for the ``vfio_device_mig_state`` structure in the header file +linux-headers/linux/vfio.h. VFIO implements the device hooks for the iterative approach as follows: -* A ``save_setup`` function that sets up the migration region and sets _SAVING - flag in the VFIO device state. +* A ``save_setup`` function that sets up migration on the source. -* A ``load_setup`` function that sets up the migration region on the - destination and sets _RESUMING flag in the VFIO device state. +* A ``load_setup`` function that sets the VFIO device on the destination in + _RESUMING state. * A ``save_live_pending`` function that reads pending_bytes from the vendor driver, which indicates the amount of data that the vendor driver has yet to save for the VFIO device. -* A ``save_live_iterate`` function that reads the VFIO device's data from the - vendor driver through the migration region during iterative phase. - * A ``save_state`` function to save the device config space if it is present. -* A ``save_live_complete_precopy`` function that resets _RUNNING flag from the - VFIO device state and iteratively copies the remaining data for the VFIO - device until the vendor driver indicates that no data remains (pending bytes - is zero). +* A ``save_live_complete_precopy`` function that sets the VFIO device in + _STOP_COPY state and iteratively copies the data for the VFIO device until + the vendor driver indicates that no data remains. * A ``load_state`` function that loads the config section and the data - sections that are generated by the save functions above + sections that are generated by the save functions above. * ``cleanup`` functions for both save and load that perform any migration - related cleanup, including unmapping the migration region + related cleanup. The VFIO migration code uses a VM state change handler to change the VFIO
@@ -71,13 +64,13 @@ tracking can identify dirtied pages, but any page pinned by the vendor driver can also be written by the device. There is currently no device or IOMMU support for dirty page tracking in hardware. -By default, dirty pages are tracked when the device is in pre-copy as well as -stop-and-copy phase. So, a page pinned by the vendor driver will be copied to -the destination in both phases. Copying dirty pages in pre-copy phase helps -QEMU to predict if it can achieve its downtime tolerances. If QEMU during -pre-copy phase keeps finding dirty pages continuously, then it understands -that even in stop-and-copy phase, it is likely to find dirty pages and can -predict the downtime accordingly. +By default, dirty pages are tracked during pre-copy as well as stop-and-copy +phase. So, a page pinned by the vendor driver will be copied to the destination +in both phases. Copying dirty pages in pre-copy phase helps QEMU to predict if +it can achieve its downtime tolerances. If QEMU during pre-copy phase keeps +finding dirty pages continuously, then it understands that even in stop-and-copy +phase, it is likely to find dirty pages and can predict the downtime +accordingly. QEMU also provides a per device opt-out option ``pre-copy-dirty-page-tracking`` which disables querying the dirty bitmap during pre-copy phase. If it is set to
@@ -111,23 +104,22 @@ Live migration save path | migrate_init spawns migration_thread Migration thread then calls each device's .save_setup() - (RUNNING, _SETUP, _RUNNING|_SAVING) + (RUNNING, _SETUP, _RUNNING) | - (RUNNING, _ACTIVE, _RUNNING|_SAVING) + (RUNNING, _ACTIVE, _RUNNING) If device is active, get pending_bytes by .save_live_pending() If total pending_bytes >= threshold_size, call .save_live_iterate() - Data of VFIO device for pre-copy phase is copied Iterate till total pending bytes converge and are less than threshold | On migration completion, vCPU stops and calls .save_live_complete_precopy for - each active device. The VFIO device is then transitioned into _SAVING state - (FINISH_MIGRATE, _DEVICE, _SAVING) + each active device. The VFIO device is then transitioned into _STOP_COPY state + (FINISH_MIGRATE, _DEVICE, _STOP_COPY) | For the VFIO device, iterate in .save_live_complete_precopy until pending data is 0 - (FINISH_MIGRATE, _DEVICE, _STOPPED) + (FINISH_MIGRATE, _DEVICE, _STOP) | - (FINISH_MIGRATE, _COMPLETED, _STOPPED) + (FINISH_MIGRATE, _COMPLETED, _STOP) Migraton thread schedules cleanup bottom half and exits Live migration resume path
@@ -136,7 +128,7 @@ Live migration resume path :: Incoming migration calls .load_setup for each device - (RESTORE_VM, _ACTIVE, _STOPPED) + (RESTORE_VM, _ACTIVE, _STOP) | For each device, .load_state is called for that device section data (RESTORE_VM, _ACTIVE, _RESUMING)
Signature · 1 line
2.26.3
PATCH v4 13/14 vfio/migration: Use VFIO_DEVICE_FEATURE_MIG_DATA_SIZE ioctl
In reply to Avihai HoronUse VFIO_DEVICE_FEATURE_MIG_DATA_SIZE ioctl to query the device stop copy data size and report this value in vfio_save_pending() instead of the hardcoded value that is currently used.
Use this ioctl in vfio_save_setup() as well, to adjust the migration data buffer size.
--- hw/vfio/migration.c | 49 +++++++++++++++++++++++++++++++------- hw/vfio/trace-events | 2 +- linux-headers/linux/vfio.h | 13 ++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-)
hw/vfio/migration.c +40 -9
@@ -208,13 +208,42 @@ static void vfio_migration_cleanup(VFIODevice *vbasedev)/* ---------------------------------------------------------------------- */ +static int vfio_query_stop_copy_size (VFIODevice *vbasedev, +uint64_t *stop_copy_size) +{ +uint64_t buf[DIV_ROUND_UP (sizeof (struct vfio_device_feature ) + +sizeof (struct vfio_device_feature_mig_data_size ), +sizeof (uint64_t ))] = {}; +struct vfio_device_feature *feature = (struct vfio_device_feature *)buf; +struct vfio_device_feature_mig_data_size *mig_data_size = + (struct vfio_device_feature_mig_data_size *)feature->data; + + feature->argsz =sizeof (buf); + feature->flags = +VFIO_DEVICE_FEATURE_GET |VFIO_DEVICE_FEATURE_MIG_DATA_SIZE ; + +if (ioctl (vbasedev->fd,VFIO_DEVICE_FEATURE , feature)) { +return -errno; + } + + *stop_copy_size = mig_data_size->stop_copy_length; + +return 0 ; +} +static int vfio_save_setup (QEMUFile *f,void *opaque) {VFIODevice *vbasedev = opaque;VFIOMigration *migration = vbasedev->migration; +uint64_t stop_copy_size;qemu_put_be64 (f,VFIO_MIG_FLAG_DEV_SETUP_STATE ); +if (vfio_query_stop_copy_size (vbasedev, &stop_copy_size)) { + stop_copy_size =VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE ; + } + migration->data_buffer_size =MIN (VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE , + stop_copy_size); migration->data_buffer =g_try_malloc0 (migration->data_buffer_size);if (!migration->data_buffer) { error_report("%s: Failed to allocate migration data buffer",
@@ -222,7 +251,7 @@ static int vfio_save_setup(QEMUFile *f, void *opaque)return -ENOMEM ; } -trace_vfio_save_setup (vbasedev->name); +trace_vfio_save_setup (vbasedev->name, migration->data_buffer_size);qemu_put_be64 (f,VFIO_MIG_FLAG_END_OF_STATE );
@@ -251,18 +280,20 @@ static void vfio_save_pending(void *opaque, uint64_t threshold_size, uint64_t *res_postcopy_only) {VFIODevice *vbasedev = opaque; +uint64_t stop_copy_size; -/* -* VFIO migration protocol v2 currently doesn't have an API to get pending -* migration size. Until such an API is introduced, report big pending size -* so the device migration size will be taken into account and downtime -* limit won't be violated. -*/ - *res_precopy_only +=VFIO_MIG_STOP_COPY_SIZE ; +if (vfio_query_stop_copy_size (vbasedev, &stop_copy_size)) { +/* +* Failed to get pending migration size. Report big pending size so +* downtime limit won't be violated. +*/ + stop_copy_size =VFIO_MIG_STOP_COPY_SIZE ; + } + *res_precopy_only += stop_copy_size;trace_vfio_save_pending (vbasedev->name, *res_precopy_only, *res_postcopy_only, *res_compatible, -VFIO_MIG_STOP_COPY_SIZE ); + stop_copy_size); }/* Returns 1 if end-of-stream is reached, 0 if more data and -1 if error */
hw/vfio/trace-events +1 -1
@@ -162,5 +162,5 @@ vfio_save_cleanup(const char *name) " (%s)" vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_save_device_config_state(const char *name) " (%s)" vfio_save_pending(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t compatible, uint64_t stopcopy) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible 0x%"PRIx64" stopcopy size 0x%"PRIx64 -vfio_save_setup(const char *name) " (%s)" +vfio_save_setup(const char *name, uint64_t data_buffer_size) " (%s) data buffer size 0x%"PRIx64 vfio_vmstate_change(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s"
linux-headers/linux/vfio.h +13 -0
@@ -986,6 +986,19 @@ enum vfio_device_mig_state { VFIO_DEVICE_STATE_RUNNING_P2P = 5, }; +/* +* Upon VFIO_DEVICE_FEATURE_GET read back the estimated data length that will +* be required to complete stop copy. +* +* Note: Can be called on each device state. +*/ + +struct vfio_device_feature_mig_data_size { +__aligned_u64 stop_copy_length; +}; + +#define VFIO_DEVICE_FEATURE_MIG_DATA_SIZE 9 +/* -------- API for Type1 VFIO IOMMU -------- */ /**
Signature · 1 line
2.26.3
PATCH v4 14/14 vfio/migration: Optimize vfio_save_pending()
In reply to Avihai HoronDuring pre-copy phase of migration vfio_save_pending() is called repeatedly and queries the VFIO device for its pending data size.
As long as pending RAM size is over the threshold, migration can't converge and be completed. Therefore, during this time there is no point in querying the VFIO device pending data size.
Avoid these unnecessary queries by issuing them in a RAM pre-copy notifier instead of vfio_save_pending().
This way the VFIO device is queried only when RAM pending data is below the threshold, when there is an actual chance for migration to converge.
--- hw/vfio/migration.c | 55 +++++++++++++++++++++++++---------- hw/vfio/trace-events | 1 + include/hw/vfio/vfio-common.h | 2 ++ 3 files changed, 43 insertions(+), 15 deletions(-)
hw/vfio/migration.c +40 -15
@@ -269,31 +269,19 @@ static void vfio_save_cleanup(void *opaque)trace_vfio_save_cleanup (vbasedev->name); } -/* -* Migration size of VFIO devices can be as little as a few KBs or as big as -* many GBs. This value should be big enough to cover the worst case. -*/ -#define VFIO_MIG_STOP_COPY_SIZE (100 * GiB)static void vfio_save_pending (void *opaque,uint64_t threshold_size,uint64_t *res_precopy_only,uint64_t *res_compatible,uint64_t *res_postcopy_only) {VFIODevice *vbasedev = opaque; -uint64_t stop_copy_size; +VFIOMigration *migration = vbasedev->migration; -if (vfio_query_stop_copy_size (vbasedev, &stop_copy_size)) { -/* -* Failed to get pending migration size. Report big pending size so -* downtime limit won't be violated. -*/ - stop_copy_size =VFIO_MIG_STOP_COPY_SIZE ; - } + *res_precopy_only += migration->stop_copy_size; - *res_precopy_only += stop_copy_size;trace_vfio_save_pending (vbasedev->name, *res_precopy_only, *res_postcopy_only, *res_compatible, - stop_copy_size); + migration->stop_copy_size); }/* Returns 1 if end-of-stream is reached, 0 if more data and -1 if error */
@@ -501,6 +489,40 @@ static void vfio_migration_state_notifier(Notifier *notifier, void *data) } } +/* +* Migration size of VFIO devices can be as little as a few KBs or as big as +* many GBs. This value should be big enough to cover the worst case. +*/ +#define VFIO_MIG_STOP_COPY_SIZE (100 * GiB) +static int vfio_migration_data_notifier (NotifierWithReturn *n,void *data) +{ +VFIOMigration *migration =container_of (n, VFIOMigration, migration_data); +VFIODevice *vbasedev = migration->vbasedev; +PrecopyNotifyData *pnd = data; + +if (pnd->reason !=PRECOPY_NOTIFY_AFTER_BITMAP_SYNC ) { +return 0 ; + } + +/* No need to get pending size when finishing migration */ +if (runstate_check (RUN_STATE_FINISH_MIGRATE )) { +return 0 ; + } + +if (vfio_query_stop_copy_size (vbasedev, &migration->stop_copy_size)) { +/* +* Failed to get pending migration size. Report big pending size so +* downtime limit won't be violated. +*/ + migration->stop_copy_size =VFIO_MIG_STOP_COPY_SIZE ; + } + +trace_vfio_migration_data_notifier (vbasedev->name, + migration->stop_copy_size); + +return 0 ; +} +static void vfio_migration_exit (VFIODevice *vbasedev) {g_free (vbasedev->migration);
@@ -578,6 +600,8 @@ static int vfio_migration_init(VFIODevice *vbasedev) vbasedev); migration->migration_state.notify = vfio_migration_state_notifier;add_migration_state_change_notifier (&migration->migration_state); + migration->migration_data.notify = vfio_migration_data_notifier; +precopy_add_notifier (&migration->migration_data);return 0 ; }
@@ -622,6 +646,7 @@ void vfio_migration_finalize(VFIODevice *vbasedev)if (vbasedev->migration) {VFIOMigration *migration = vbasedev->migration; +precopy_remove_notifier (&migration->migration_data);remove_migration_state_change_notifier (&migration->migration_state);qemu_del_vm_change_state_handler (migration->vm_state);unregister_savevm (VMSTATE_IF (vbasedev->dev),"vfio" , vbasedev);
hw/vfio/trace-events +1 -0
@@ -154,6 +154,7 @@ vfio_load_cleanup(const char *name) " (%s)" vfio_load_device_config_state(const char *name) " (%s)" vfio_load_state(const char *name, uint64_t data) " (%s) data 0x%"PRIx64 vfio_load_state_device_data(const char *name, uint64_t data_size, int ret) " (%s) size 0x%"PRIx64" ret %d" +vfio_migration_data_notifier(const char *name, uint64_t stopcopy_size) " (%s) stopcopy size 0x%"PRIx64 vfio_migration_probe(const char *name) " (%s)" vfio_migration_set_state(const char *name, const char *state) " (%s) state %s" vfio_migration_state_notifier(const char *name, const char *state) " (%s) state %s"
include/hw/vfio/vfio-common.h +2 -0
@@ -62,10 +62,12 @@ typedef struct VFIOMigration {struct VFIODevice *vbasedev;VMChangeStateEntry *vm_state;Notifier migration_state; +NotifierWithReturn migration_data;enum vfio_device_mig_state device_state;int data_fd;void *data_buffer;size_t data_buffer_size; +uint64_t stop_copy_size; } VFIOMigration; typedef struct VFIOAddressSpace {
Signature · 1 line
2.26.3