Skip to contentSkip to conversation

docs/devel: Add VFIO device migration documentation

PATCH Superseded Tarun Gupta 5 participants 12 messages Mar 10, 2021 – Mar 18, 2021

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 docs/devel: Add VFIO device migration documentation

Document interfaces used for VFIO device migration. Added flow of state changes during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst
MAINTAINERS +1 -0
index 738786146d..a2a80eee59 100644
@@ -1801,6 +1801,7 @@ M: Alex Williamson <[email protected]>
 S: Supported
 F: hw/vfio/*
 F: include/hw/vfio/
+F: docs/devel/vfio-migration.rst
 
 vfio-ccw
 M: Cornelia Huck <[email protected]>
docs/devel/index.rst +1 -0
index ae664da00c..5330f1ca1d 100644
@@ -39,3 +39,4 @@ Contents:
    qom
    block-coroutine-wrapper
    multi-process
+   vfio-migration
docs/devel/vfio-migration.rst new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw
Quoted 20 lines · Tarun Gupta

Document interfaces used for VFIO device migration. Added flow of state changes during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst

docs/devel/vfio-migration.rst line 135

Quoted 4 lines
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

What is the problem here and is there any plan for how to address it ?

Postcopy is essentially the only migration mechanism that can reliably complete, so it really should be considered the default approach to migration for all mgmt apps wanting to do migration, except in special
cases. IOW, if we want VFIO migration to be viable, we need postcopy
support.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
Quoted 21 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 18 lines · Tarun Gupta

Document interfaces used for VFIO device migration. Added flow of state changes during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst
Quoted 6 lines

docs/devel/vfio-migration.rst line 135

Quoted 4 lines
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

What is the problem here and is there any plan for how to address it ?

There's no equivalent to userfaultfd for accesses to RAM made by a device.
There's some potential for this to be doable with an IOMMU or the like, but:
a) IOMMUs and devices aren't currently happy at recovering from
failures
b) the fragementation you get during a postcopy probably isn't pretty
when you get to build IOMMU tables.

Quoted 5 lines

Postcopy is essentially the only migration mechanism that can reliably complete, so it really should be considered the default approach to migration for all mgmt apps wanting to do migration, except in special
cases. IOW, if we want VFIO migration to be viable, we need postcopy
support.

There's lots of other things postcopy doesn't work with; so hmm.

Dave

Quoted 7 lines

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#3 lore raw
Quoted 35 lines

From: Qemu-devel <[email protected]> On Behalf Of Dr. David Alan Gilbert

Quoted 2 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 1 line · Tarun Gupta

Document interfaces used for VFIO device migration. Added flow of state

changes

Quoted 1 line
Quoted 1 line

during live migration with VFIO device. Tested by building docs with the

new

Quoted 8 lines
Quoted 8 lines

vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

Quoted 6 lines
Quoted 6 lines
---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135

++++++++++++++++++++++++++++++++++

Quoted 10 lines

docs/devel/vfio-migration.rst line 135

Quoted 8 lines
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

What is the problem here and is there any plan for how to address it ?

Quoted 8 lines

There's no equivalent to userfaultfd for accesses to RAM made by a device.
There's some potential for this to be doable with an IOMMU or the like, but:
a) IOMMUs and devices aren't currently happy at recovering from
failures
b) the fragementation you get during a postcopy probably isn't pretty
when you get to build IOMMU tables.

To overcome such limitations one may adopt a prefault-and-pull scheme if the vendor driver has the capability to track pending DMA buffers in the migration process (with additional uAPI changes in VFIO or userfaultfd), as discussed here:

https://static.sched.com/hosted_files/kvmforum2019/7a/kvm-forum-postcopy-final.pdf

Quoted 9 lines
Quoted 6 lines

Postcopy is essentially the only migration mechanism that can reliably complete, so it really should be considered the default approach to migration for all mgmt apps wanting to do migration, except in special
cases. IOW, if we want VFIO migration to be viable, we need postcopy
support.

There's lots of other things postcopy doesn't work with; so hmm.

Agree. Also given the amount of work even for pre-copy migration, it makes more sense to do things step-by-step.

Thanks
Kevin

#6 lore raw
In reply to Tarun Gupta
Quoted 84 lines

From: Tarun Gupta <[email protected]>
Sent: Thursday, March 11, 2021 3:20 AM

Document interfaces used for VFIO device migration. Added flow of state changes
during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst
MAINTAINERS +1 -0
index 738786146d..a2a80eee59 100644
@@ -1801,6 +1801,7 @@ M: Alex Williamson <[email protected]>
 S: Supported
 F: hw/vfio/*
 F: include/hw/vfio/
+F: docs/devel/vfio-migration.rst
 
 vfio-ccw
 M: Cornelia Huck <[email protected]>
docs/devel/index.rst +1 -0
index ae664da00c..5330f1ca1d 100644
@@ -39,3 +39,4 @@ Contents:
    qom
    block-coroutine-wrapper
    multi-process
+   vfio-migration
docs/devel/vfio-migration.rst new file +25 -0
new file mode 100644
index 0000000000..6196fb132c
@@ -0,0 +1,135 @@
+=====================
+VFIO device Migration
+=====================
+
+VFIO devices use an iterative approach for migration because certain VFIO
+devices (e.g. GPU) have large amount of data to be transfered. 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.
+
+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.
+
+VFIO device hooks for iterative approach:
+
+* A ``save_setup`` function that sets up the migration region, sets _SAVING
+  flag in the VFIO device state and informs the VFIO IOMMU module to start
+  dirty page tracking.
+
+* A ``load_setup`` function that sets up the migration region on the
+  destination and sets _RESUMING flag in the VFIO device 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

docs/devel/vfio-migration.rst new file +6 -0
+  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_live_complete_precopy`` function that resets _RUNNING flag
Quoted 2 lines

from the
+ VFIO device state, saves the device config space, if any, and iteratively

and if any,

Quoted 23 lines
docs/devel/vfio-migration.rst new file +10 -0
+  copies the remaining data for the VFIO device untill the vendor driver
+  indicates that no data remains (pending bytes is zero).
+
+* A ``load_state`` function that loads the config section and the data
+  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
+
+A VM state change handler is registered to change the VFIO device state

when

docs/devel/vfio-migration.rst new file +11 -0
+the VM state changes.
+
+Similarly, a migration state change notifier is registered to get a
+notification on migration state change. These states are translated to VFIO
+device state and conveyed to vendor driver.
+
+System memory dirty pages tracking
+----------------------------------
+
+A ``log_sync`` memory listener callback marks those system memory pages
+as dirty which are used for DMA by the VFIO device. The dirty pages bitmap

is

docs/devel/vfio-migration.rst line 56

Quoted 2 lines
docs/devel/vfio-migration.rst new file +2 -0
+queried per container. All pages pinned by the vendor driver through
+vfio_pin_pages() external API have to be marked as dirty during migration.

why mention kernel internal functions in an userspace doc?

Quoted 5 lines

+When there are CPU writes, CPU dirty page tracking can identify dirtied pages,
+but any page pinned by the vendor driver can also be written by device. There
+is currently no device which has hardware support for dirty page tracking.

no device or IOMMU support

Quoted 2 lines

So
+all pages which are pinned by vendor driver are considered as dirty.

Similarly, why do we care about how the kernel identifies whether a page is dirty. It could be dirtied due to pinning, or due to IOMMU dirty bit, or due to IOMMU page fault. Here we'd better just focus on user-tangible effect, e.g. a large/non-converging dirty map might be returned then how to handle such situation...

docs/devel/vfio-migration.rst line 66

Quoted 6 lines
docs/devel/vfio-migration.rst new file +6 -0
+
+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 using
+vfio_pin_pages() will be copied to destination in both the phases. Copying
+dirty pages in pre-copy phase helps QEMU to predict if it can achieve its
+downtime tolerances.

worthy of some elaboration on the last sentence.

Quoted 4 lines
docs/devel/vfio-migration.rst new file +2 -0
+
+QEMU also provides a per device opt-out option ``pre-copy-dirty-page-

tracking``
+to disable dirty page tracking during pre-copy phase. If it is set to off, all

IIUC dirty page tracking is always enabled in vfio_save_setup. What this option does is to skip sync-ing dirty bitmap in vfio_listerner_log_sync.

Quoted 10 lines
docs/devel/vfio-migration.rst new file +6 -0
+pinned pages will be copied to destination in stop-and-copy phase only.
+
+System memory dirty pages tracking when vIOMMU is enabled
+---------------------------------------------------------
+
+With vIOMMU, an IO virtual address range can get unmapped while in pre-

copy
+phase of migration. In that case, the unmap ioctl returns any pinned pages in
+that range and QEMU reports corresponding guest physical pages dirty.

pinned pages -> dirty pages

Quoted 64 lines

During
+stop-and-copy phase, an IOMMU notifier is used to get a callback for mapped
+pages and then dirty pages bitmap is fetched from VFIO IOMMU modules for those

docs/devel/vfio-migration.rst new file +28 -0
+mapped ranges.
+
+Flow of state changes during Live migration
+===========================================
+
+Below is the flow of state change during live migration.
+The values in the brackets represent the VM state, the migration state, and
+the VFIO device state, respectively.
+
+Live migration save path
+------------------------
+
+::
+
+                        QEMU normal running state
+                        (RUNNING, _NONE, _RUNNING)
+                                  |
+                     migrate_init spawns migration_thread
+                Migration thread then calls each device's .save_setup()
+                    (RUNNING, _SETUP, _RUNNING|_SAVING)
+                                  |
+                    (RUNNING, _ACTIVE, _RUNNING|_SAVING)
+             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

docs/devel/vfio-migration.rst new file +27 -1
+                   (FINISH_MIGRATE, _DEVICE, _SAVING)
+                                  |
+     For the VFIO device, iterate in .save_live_complete_precopy until
+                         pending data is 0
+                   (FINISH_MIGRATE, _DEVICE, _STOPPED)
+                                  |
+                 (FINISH_MIGRATE, _COMPLETED, _STOPPED)
+             Migraton thread schedules cleanup bottom half and exits
+
+Live migration resume path
+--------------------------
+
+::
+
+              Incoming migration calls .load_setup for each device
+                       (RESTORE_VM, _ACTIVE, _STOPPED)
+                                 |
+       For each device, .load_state is called for that device section data
+                       (RESTORE_VM, _ACTIVE, _RESUMING)
+                                 |
+    At the end, .load_cleanup is called for each device and vCPUs are started
+                       (RUNNING, _NONE, _RUNNING)
+
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.
--
Quoted 1 line

2.27.0

Thanks
Kevin

#9 lore raw
In reply to Tarun Gupta
Quoted 19 lines · On Thu, 11 Mar 2021 00:50:09 +0530 Tarun Gupta <[email protected]> wrote:

Document interfaces used for VFIO device migration. Added flow of state changes during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst

(...)

docs/devel/vfio-migration.rst line 3

Quoted 9 lines
docs/devel/vfio-migration.rst new file +3 -0
new file mode 100644
index 0000000000..6196fb132c
@@ -0,0 +1,135 @@
+=====================
+VFIO device Migration
+=====================

Maybe add an introductory sentence or two describing the general approach? I.e. we have a general framework, and specific support for devices needs to be hooked up.

docs/devel/vfio-migration.rst line 10

Quoted 7 lines
docs/devel/vfio-migration.rst new file +7 -0
+
+VFIO devices use an iterative approach for migration because certain VFIO
+devices (e.g. GPU) have large amount of data to be transfered. 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.

What about something like:

"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..."

Quoted 14 lines
docs/devel/vfio-migration.rst new file +14 -0
+
+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.
+
+VFIO device hooks for iterative approach:
+
+* A ``save_setup`` function that sets up the migration region, sets _SAVING
+  flag in the VFIO device state and informs the VFIO IOMMU module to start
+  dirty page tracking.
+
+* A ``load_setup`` function that sets up the migration region on the
+  destination and sets _RESUMING flag in the VFIO device state.
+

docs/devel/vfio-migration.rst line 34

Quoted 10 lines
docs/devel/vfio-migration.rst new file +10 -0
+* 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_live_complete_precopy`` function that resets _RUNNING flag from the
+  VFIO device state, saves the device config space, if any, and iteratively
+  copies the remaining data for the VFIO device untill the vendor driver

s/untill/until/

docs/devel/vfio-migration.rst line 47

Quoted 13 lines
docs/devel/vfio-migration.rst new file +13 -0
+  indicates that no data remains (pending bytes is zero).
+
+* A ``load_state`` function that loads the config section and the data
+  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
+
+A VM state change handler is registered to change the VFIO device state when
+the VM state changes.
+
+Similarly, a migration state change notifier is registered to get a
+notification on migration state change. These states are translated to VFIO

s/to/to the corresponding/

docs/devel/vfio-migration.rst line 48

Quoted 1 line
docs/devel/vfio-migration.rst new file +1 -0
+device state and conveyed to vendor driver.

s/to/to the/

(...)

docs/devel/vfio-migration.rst line 135

Quoted 4 lines
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

s/is not/is currently not/ ?

#7 lore raw
In reply to Tian, Kevin
Quoted 81 lines · Kevin
Quoted 81 lines

From: Tarun Gupta <[email protected]>
Sent: Thursday, March 11, 2021 3:20 AM

Document interfaces used for VFIO device migration. Added flow of state changes
during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
  MAINTAINERS                   |   1 +
  docs/devel/index.rst          |   1 +
  docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
  3 files changed, 137 insertions(+)

create mode 100644 docs/devel/vfio-migration.rst

MAINTAINERS +1 -0
index 738786146d..a2a80eee59 100644
@@ -1801,6 +1801,7 @@ M: Alex Williamson <[email protected]>
  S: Supported
  F: hw/vfio/*
  F: include/hw/vfio/
+F: docs/devel/vfio-migration.rst
 
  vfio-ccw
  M: Cornelia Huck <[email protected]>
docs/devel/index.rst +1 -0
index ae664da00c..5330f1ca1d 100644
@@ -39,3 +39,4 @@ Contents:
     qom
     block-coroutine-wrapper
     multi-process
+   vfio-migration
docs/devel/vfio-migration.rst new file +25 -0
new file mode 100644
index 0000000000..6196fb132c
@@ -0,0 +1,135 @@
+=====================
+VFIO device Migration
+=====================
+
+VFIO devices use an iterative approach for migration because certain VFIO
+devices (e.g. GPU) have large amount of data to be transfered. 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.
+
+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.
+
+VFIO device hooks for iterative approach:
+
+* A ``save_setup`` function that sets up the migration region, sets _SAVING
+  flag in the VFIO device state and informs the VFIO IOMMU module to start
+  dirty page tracking.
+
+* A ``load_setup`` function that sets up the migration region on the
+  destination and sets _RESUMING flag in the VFIO device 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

docs/devel/vfio-migration.rst new file +2 -0
+  save for the VFIO device.
+
Quoted 8 lines
Quoted 6 lines
docs/devel/vfio-migration.rst new file +4 -0
+* 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_live_complete_precopy`` function that resets _RUNNING flag

from the
+ VFIO device state, saves the device config space, if any, and iteratively

and if any,

I didn't get this. I intended to say that it will save the device config space only if it is present. So, used "saves device config space, if any".

Quoted 20 lines
Quoted 20 lines
docs/devel/vfio-migration.rst new file +10 -0
+  copies the remaining data for the VFIO device untill the vendor driver
+  indicates that no data remains (pending bytes is zero).
+
+* A ``load_state`` function that loads the config section and the data
+  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
+
+A VM state change handler is registered to change the VFIO device state

when

docs/devel/vfio-migration.rst new file +8 -0
+the VM state changes.
+
+Similarly, a migration state change notifier is registered to get a
+notification on migration state change. These states are translated to VFIO
+device state and conveyed to vendor driver.
+
+System memory dirty pages tracking
+----------------------------------
Quoted 8 lines

docs/devel/vfio-migration.rst line 56

Quoted 6 lines
docs/devel/vfio-migration.rst new file +3 -0
+
+A ``log_sync`` memory listener callback marks those system memory pages
+as dirty which are used for DMA by the VFIO device. The dirty pages bitmap

is

docs/devel/vfio-migration.rst new file +2 -0
+queried per container. All pages pinned by the vendor driver through
+vfio_pin_pages() external API have to be marked as dirty during migration.

why mention kernel internal functions in an userspace doc?

I'll remove the mention of vfio_pin_pages() and just mention "external API".

Quoted 8 lines
Quoted 6 lines

+When there are CPU writes, CPU dirty page tracking can identify dirtied pages,
+but any page pinned by the vendor driver can also be written by device. There
+is currently no device which has hardware support for dirty page tracking.

no device or IOMMU support

Right, will update it.

Quoted 9 lines
Quoted 3 lines

So
+all pages which are pinned by vendor driver are considered as dirty.

Similarly, why do we care about how the kernel identifies whether a page is dirty. It could be dirtied due to pinning, or due to IOMMU dirty bit, or due to IOMMU page fault. Here we'd better just focus on user-tangible effect, e.g. a large/non-converging dirty map might be returned then how to handle such situation...

Since VFIO migration feature is not just implemented in userspace but also involves implementation in kernel space as well, have documented here what is implemented as of now.

Quoted 9 lines

docs/devel/vfio-migration.rst line 66

Quoted 7 lines
docs/devel/vfio-migration.rst new file +6 -0
+
+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 using
+vfio_pin_pages() will be copied to destination in both the phases. Copying
+dirty pages in pre-copy phase helps QEMU to predict if it can achieve its
+downtime tolerances.

worthy of some elaboration on the last sentence.

How about below?
"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."

Quoted 8 lines
Quoted 5 lines
docs/devel/vfio-migration.rst new file +2 -0
+
+QEMU also provides a per device opt-out option ``pre-copy-dirty-page-

tracking``
+to disable dirty page tracking during pre-copy phase. If it is set to off, all

IIUC dirty page tracking is always enabled in vfio_save_setup. What this option does is to skip sync-ing dirty bitmap in vfio_listerner_log_sync.

I'll update it as below?

"QEMU also provides a per device opt-out option ``pre-copy-dirty-page-tracking`` which disables querying dirty bitmap during pre-copy phase. If it is set to off, all dirty pages will be copied to destination in stop-and-copy phase only."

Quoted 13 lines
Quoted 11 lines
docs/devel/vfio-migration.rst new file +6 -0
+pinned pages will be copied to destination in stop-and-copy phase only.
+
+System memory dirty pages tracking when vIOMMU is enabled
+---------------------------------------------------------
+
+With vIOMMU, an IO virtual address range can get unmapped while in pre-

copy
+phase of migration. In that case, the unmap ioctl returns any pinned pages in
+that range and QEMU reports corresponding guest physical pages dirty.

pinned pages -> dirty pages

Currently, all pinned pages are dirty pages.
But, agreed that dirty pages might be more accurate here, will update it.

Thanks,
Tarun

Quoted 70 lines
Quoted 65 lines

During
+stop-and-copy phase, an IOMMU notifier is used to get a callback for mapped
+pages and then dirty pages bitmap is fetched from VFIO IOMMU modules for those

docs/devel/vfio-migration.rst new file +28 -0
+mapped ranges.
+
+Flow of state changes during Live migration
+===========================================
+
+Below is the flow of state change during live migration.
+The values in the brackets represent the VM state, the migration state, and
+the VFIO device state, respectively.
+
+Live migration save path
+------------------------
+
+::
+
+                        QEMU normal running state
+                        (RUNNING, _NONE, _RUNNING)
+                                  |
+                     migrate_init spawns migration_thread
+                Migration thread then calls each device's .save_setup()
+                    (RUNNING, _SETUP, _RUNNING|_SAVING)
+                                  |
+                    (RUNNING, _ACTIVE, _RUNNING|_SAVING)
+             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

docs/devel/vfio-migration.rst new file +27 -1
+                   (FINISH_MIGRATE, _DEVICE, _SAVING)
+                                  |
+     For the VFIO device, iterate in .save_live_complete_precopy until
+                         pending data is 0
+                   (FINISH_MIGRATE, _DEVICE, _STOPPED)
+                                  |
+                 (FINISH_MIGRATE, _COMPLETED, _STOPPED)
+             Migraton thread schedules cleanup bottom half and exits
+
+Live migration resume path
+--------------------------
+
+::
+
+              Incoming migration calls .load_setup for each device
+                       (RESTORE_VM, _ACTIVE, _STOPPED)
+                                 |
+       For each device, .load_state is called for that device section data
+                       (RESTORE_VM, _ACTIVE, _RESUMING)
+                                 |
+    At the end, .load_cleanup is called for each device and vCPUs are started
+                       (RUNNING, _NONE, _RUNNING)
+
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.
--
Quoted 1 line

2.27.0

Thanks
Kevin

#4 lore raw
In reply to Tian, Kevin
Quoted 44 lines · * Tian, Kevin ([email protected]) wrote:
Quoted 35 lines

From: Qemu-devel <[email protected]> On Behalf Of Dr. David Alan Gilbert

Quoted 2 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 1 line · Tarun Gupta

Document interfaces used for VFIO device migration. Added flow of state

changes

Quoted 1 line
Quoted 1 line

during live migration with VFIO device. Tested by building docs with the

new

Quoted 8 lines
Quoted 8 lines

vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

Quoted 6 lines
Quoted 6 lines
---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135

++++++++++++++++++++++++++++++++++

Quoted 10 lines

docs/devel/vfio-migration.rst line 135

Quoted 8 lines
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

What is the problem here and is there any plan for how to address it ?

Quoted 8 lines

There's no equivalent to userfaultfd for accesses to RAM made by a device.
There's some potential for this to be doable with an IOMMU or the like, but:
a) IOMMUs and devices aren't currently happy at recovering from
failures
b) the fragementation you get during a postcopy probably isn't pretty
when you get to build IOMMU tables.

Quoted 6 lines

To overcome such limitations one may adopt a prefault-and-pull scheme if the vendor driver has the capability to track pending DMA buffers in the migration process (with additional uAPI changes in VFIO or userfaultfd), as discussed here:

https://static.sched.com/hosted_files/kvmforum2019/7a/kvm-forum-postcopy-final.pdf

Did that get any further?

I can imagine that might be tricikier for a GPU than a network card; the shaders in a GPU are pretty random as to what they go off and access, so I can't see how you could prefault.

Dave

Quoted 16 lines
Quoted 9 lines
Quoted 6 lines

Postcopy is essentially the only migration mechanism that can reliably complete, so it really should be considered the default approach to migration for all mgmt apps wanting to do migration, except in special
cases. IOW, if we want VFIO migration to be viable, we need postcopy
support.

There's lots of other things postcopy doesn't work with; so hmm.

Agree. Also given the amount of work even for pre-copy migration, it makes more sense to do things step-by-step.

Thanks
Kevin

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#10 lore raw
In reply to Cornelia Huck
Quoted 26 lines · Cornelia Huck
Quoted 19 lines · On Thu, 11 Mar 2021 00:50:09 +0530 Tarun Gupta <[email protected]> wrote:

Document interfaces used for VFIO device migration. Added flow of state changes during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
  MAINTAINERS                   |   1 +
  docs/devel/index.rst          |   1 +
  docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
  3 files changed, 137 insertions(+)

create mode 100644 docs/devel/vfio-migration.rst

(...)

Quoted 13 lines

docs/devel/vfio-migration.rst line 3

Quoted 9 lines
docs/devel/vfio-migration.rst new file +3 -0
new file mode 100644
index 0000000000..6196fb132c
@@ -0,0 +1,135 @@
+=====================
+VFIO device Migration
+=====================

Maybe add an introductory sentence or two describing the general approach? I.e. we have a general framework, and specific support for devices needs to be hooked up.

Ummm, the below paragraph does describe the approach we're using for the migration framework involving pre-copy and stop-and-copy phase. Can you help elaborate more on the general approach you'd like to have?

Quoted 11 lines

docs/devel/vfio-migration.rst line 10

Quoted 8 lines
docs/devel/vfio-migration.rst new file +7 -0
+
+VFIO devices use an iterative approach for migration because certain VFIO
+devices (e.g. GPU) have large amount of data to be transfered. 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.

What about something like:

Quoted 6 lines

"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..."

Thanks, this looks better. I'll update it in next version incorporating the other comments too below.

Thanks,
Tarun

Quoted 56 lines
Quoted 14 lines
docs/devel/vfio-migration.rst new file +14 -0
+
+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.
+
+VFIO device hooks for iterative approach:
+
+* A ``save_setup`` function that sets up the migration region, sets _SAVING
+  flag in the VFIO device state and informs the VFIO IOMMU module to start
+  dirty page tracking.
+
+* A ``load_setup`` function that sets up the migration region on the
+  destination and sets _RESUMING flag in the VFIO device state.
+

docs/devel/vfio-migration.rst line 34

Quoted 10 lines
docs/devel/vfio-migration.rst new file +10 -0
+* 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_live_complete_precopy`` function that resets _RUNNING flag from the
+  VFIO device state, saves the device config space, if any, and iteratively
+  copies the remaining data for the VFIO device untill the vendor driver

s/untill/until/

docs/devel/vfio-migration.rst line 47

Quoted 13 lines
docs/devel/vfio-migration.rst new file +13 -0
+  indicates that no data remains (pending bytes is zero).
+
+* A ``load_state`` function that loads the config section and the data
+  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
+
+A VM state change handler is registered to change the VFIO device state when
+the VM state changes.
+
+Similarly, a migration state change notifier is registered to get a
+notification on migration state change. These states are translated to VFIO

s/to/to the corresponding/

docs/devel/vfio-migration.rst line 48

Quoted 1 line
docs/devel/vfio-migration.rst new file +1 -0
+device state and conveyed to vendor driver.

s/to/to the/

(...)

docs/devel/vfio-migration.rst line 135

Quoted 4 lines
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

s/is not/is currently not/ ?

#5 lore raw
In reply to Dr. David Alan Gilbert
Quoted 56 lines

From: Dr. David Alan Gilbert <[email protected]>
Sent: Tuesday, March 16, 2021 11:47 PM

Quoted 1 line · * Tian, Kevin ([email protected]) wrote:
Quoted 1 line

From: Qemu-devel <qemu-devel-

[email protected]>

Quoted 5 lines
Quoted 5 lines

On Behalf Of Dr. David Alan Gilbert

Quoted 2 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 1 line · Tarun Gupta

Document interfaces used for VFIO device migration. Added flow of

state

Quoted 2 lines
Quoted 2 lines

changes

Quoted 1 line
Quoted 1 line

during live migration with VFIO device. Tested by building docs with

the

Quoted 41 lines
Quoted 27 lines

new

Quoted 8 lines
Quoted 8 lines

vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

Quoted 6 lines
Quoted 6 lines
---
 MAINTAINERS                   |   1 +
 docs/devel/index.rst          |   1 +
 docs/devel/vfio-migration.rst | 135

++++++++++++++++++++++++++++++++++

Quoted 10 lines

docs/devel/vfio-migration.rst line 135

Quoted 8 lines
 3 files changed, 137 insertions(+)
 create mode 100644 docs/devel/vfio-migration.rst
docs/devel/vfio-migration.rst new file +4 -0
+Postcopy
+========
+
+Postcopy migration is not supported for VFIO devices.

What is the problem here and is there any plan for how to address it ?

Quoted 8 lines

There's no equivalent to userfaultfd for accesses to RAM made by a device.
There's some potential for this to be doable with an IOMMU or the like, but:
a) IOMMUs and devices aren't currently happy at recovering from
failures
b) the fragementation you get during a postcopy probably isn't pretty
when you get to build IOMMU tables.

To overcome such limitations one may adopt a prefault-and-pull scheme if the vendor driver has the capability to track pending DMA buffers in the migration process (with additional uAPI changes in VFIO or userfaultfd), as discussed here:

Quoted 4 lines
Quoted 1 line

https://static.sched.com/hosted_files/kvmforum2019/7a/kvm-forum-

postcopy-final.pdf

Did that get any further?

Not yet. As you may see in another thread, even the precopy side still needs some time to land.

Quoted 4 lines

I can imagine that might be tricikier for a GPU than a network card; the shaders in a GPU are pretty random as to what they go off and access, so I can't see how you could prefault.

trickier but not impossible. e.g. any page accessed by shaders must be mapped into GPU page table which could be the coarse-grained interface to track in-use DMA buffers. However this could be large and prefaulting a large set of pages weakens the benefit of postcopy. or there may be vendor specific method to do fine-grained tracking. nevertheless, it does provide an postcopy option for many devices which don't support I/O page fault but actual feasibility is vendor specific.

Thanks
Kevin

#8 lore raw
In reply to Tarun Gupta (SW-GPU)
Quoted 15 lines

From: Tarun Gupta (SW-GPU) <[email protected]>
Sent: Tuesday, March 16, 2021 9:35 PM

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+* A ``save_live_iterate`` function that reads the VFIO device's data from

the

Quoted 7 lines
Quoted 5 lines
docs/devel/vfio-migration.rst new file +3 -0
+  vendor driver through the migration region during iterative phase.
+
+* A ``save_live_complete_precopy`` function that resets _RUNNING flag

from the
+ VFIO device state, saves the device config space, if any, and iteratively

and if any,

Quoted 3 lines

I didn't get this. I intended to say that it will save the device config space only if it is present.
So, used "saves device config space, if any".

I misread it, with the impression that 'if any' is for 'iteratively copy'.

Quoted 36 lines
Quoted 5 lines

docs/devel/vfio-migration.rst

Quoted 5 lines
docs/devel/vfio-migration.rst new file +5 -0
+
+System memory dirty pages tracking
+----------------------------------
+
+A ``log_sync`` memory listener callback marks those system memory

pages

Quoted 1 line
Quoted 1 line

+as dirty which are used for DMA by the VFIO device. The dirty pages

bitmap

Quoted 3 lines

Review

Quoted 3 lines

is

(quoted diff) +2 -0
+queried per container. All pages pinned by the vendor driver through
+vfio_pin_pages() external API have to be marked as dirty during

migration.

Quoted 2 lines

why mention kernel internal functions in an userspace doc?

I'll remove the mention of vfio_pin_pages() and just mention "external API".

Quoted 8 lines
Quoted 6 lines

+When there are CPU writes, CPU dirty page tracking can identify dirtied pages,
+but any page pinned by the vendor driver can also be written by device. There
+is currently no device which has hardware support for dirty page tracking.

no device or IOMMU support

Right, will update it.

Quoted 8 lines
Quoted 3 lines

So
+all pages which are pinned by vendor driver are considered as dirty.

Similarly, why do we care about how the kernel identifies whether a page is dirty. It could be dirtied due to pinning, or due to IOMMU dirty bit, or due to IOMMU page fault. Here we'd better just focus on user-tangible effect, e.g. a large/non-converging dirty map might be returned then how to

Quoted 6 lines

handle

Quoted 1 line

such situation...

Since VFIO migration feature is not just implemented in userspace but also involves implementation in kernel space as well, have documented here what is implemented as of now.

but userpace only needs to care about the implications of the uAPI, instead of the internal detail in the kernel. You might take above pinning detail as one example to explain that the size of the dirty page set might be big and static, and then explain how userspace should cope with this situation. but don't describe it as the assumed fact.

Thanks
Kevin

#11 lore raw
In reply to Tarun Gupta (SW-GPU)
Quoted 41 lines · On Tue, 16 Mar 2021 21:48:38 +0530 "Tarun Gupta (SW-GPU)"…
Quoted 26 lines · Cornelia Huck
Quoted 19 lines · On Thu, 11 Mar 2021 00:50:09 +0530 Tarun Gupta <[email protected]> wrote:

Document interfaces used for VFIO device migration. Added flow of state changes during live migration with VFIO device. Tested by building docs with the new vfio-migration.rst file.

v2:
- Included the new vfio-migration.rst file in index.rst
- Updated dirty page tracking section, also added details about 'pre-copy-dirty-page-tracking' opt-out option.
- Incorporated comments around wording of doc.

---
  MAINTAINERS                   |   1 +
  docs/devel/index.rst          |   1 +
  docs/devel/vfio-migration.rst | 135 ++++++++++++++++++++++++++++++++++
  3 files changed, 137 insertions(+)

create mode 100644 docs/devel/vfio-migration.rst

(...)

Quoted 13 lines

docs/devel/vfio-migration.rst line 3

Quoted 9 lines
docs/devel/vfio-migration.rst new file +3 -0
new file mode 100644
index 0000000000..6196fb132c
@@ -0,0 +1,135 @@
+=====================
+VFIO device Migration
+=====================  

Maybe add an introductory sentence or two describing the general approach? I.e. we have a general framework, and specific support for devices needs to be hooked up.

Quoted 3 lines

Ummm, the below paragraph does describe the approach we're using for the migration framework involving pre-copy and stop-and-copy phase. Can you help elaborate more on the general approach you'd like to have?

The document dives right in with how vfio devices are using an iterative approach etc. A quick overview of the general setup (before you are getting to the different phases) might be helpful, i.e. who does what. Not sure what we expect a reader of this document to know already.

Quoted 21 lines
Quoted 12 lines

docs/devel/vfio-migration.rst line 10

Quoted 9 lines
docs/devel/vfio-migration.rst new file +7 -0
+
+VFIO devices use an iterative approach for migration because certain VFIO
+devices (e.g. GPU) have large amount of data to be transfered. 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.  

What about something like:

Quoted 6 lines

"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..."

Thanks, this looks better. I'll update it in next version incorporating the other comments too below.