1===================== 2VFIO device migration 3===================== 4 5Migration of virtual machine involves saving the state for each device that 6the guest is running on source host and restoring this saved state on the 7destination host. This document details how saving and restoring of VFIO 8devices is done in QEMU. 9 10Migration of VFIO devices consists of two phases: the optional pre-copy phase, 11and the stop-and-copy phase. The pre-copy phase is iterative and allows to 12accommodate VFIO devices that have a large amount of data that needs to be 13transferred. The iterative pre-copy phase of migration allows for the guest to 14continue whilst the VFIO device state is transferred to the destination, this 15helps to reduce the total downtime of the VM. VFIO devices opt-in to pre-copy 16support by reporting the VFIO_MIGRATION_PRE_COPY flag in the 17VFIO_DEVICE_FEATURE_MIGRATION ioctl. 18 19When pre-copy is supported, it's possible to further reduce downtime by 20enabling "switchover-ack" migration capability. 21VFIO migration uAPI defines "initial bytes" as part of its pre-copy data stream 22and recommends that the initial bytes are sent and loaded in the destination 23before stopping the source VM. Enabling this migration capability will 24guarantee that and thus, can potentially reduce downtime even further. 25 26To support migration of multiple devices that might do P2P transactions between 27themselves, VFIO migration uAPI defines an intermediate P2P quiescent state. 28While in the P2P quiescent state, P2P DMA transactions cannot be initiated by 29the device, but the device can respond to incoming ones. Additionally, all 30outstanding P2P transactions are guaranteed to have been completed by the time 31the device enters this state. 32 33All the devices that support P2P migration are first transitioned to the P2P 34quiescent state and only then are they stopped or started. This makes migration 35safe P2P-wise, since starting and stopping the devices is not done atomically 36for all the devices together. 37 38Thus, multiple VFIO devices migration is allowed only if all the devices 39support P2P migration. Single VFIO device migration is allowed regardless of 40P2P migration support. 41 42A detailed description of the UAPI for VFIO device migration can be found in 43the comment for the ``vfio_device_mig_state`` structure in the header file 44linux-headers/linux/vfio.h. 45 46VFIO implements the device hooks for the iterative approach as follows: 47 48* A ``save_setup`` function that sets up migration on the source. 49 50* A ``load_setup`` function that sets the VFIO device on the destination in 51 _RESUMING state. 52 53* A ``state_pending_estimate`` function that reports an estimate of the 54 remaining pre-copy data that the vendor driver has yet to save for the VFIO 55 device. 56 57* A ``state_pending_exact`` function that reads pending_bytes from the vendor 58 driver, which indicates the amount of data that the vendor driver has yet to 59 save for the VFIO device. 60 61* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is 62 active only when the VFIO device is in pre-copy states. 63 64* A ``save_live_iterate`` function that reads the VFIO device's data from the 65 vendor driver during iterative pre-copy phase. 66 67* A ``switchover_ack_needed`` function that checks if the VFIO device uses 68 "switchover-ack" migration capability when this capability is enabled. 69 70* A ``switchover_start`` function that in the multifd mode starts a thread that 71 reassembles the multifd received data and loads it in-order into the device. 72 In the non-multifd mode this function is a NOP. 73 74* A ``save_state`` function to save the device config space if it is present. 75 76* A ``save_live_complete_precopy`` function that sets the VFIO device in 77 _STOP_COPY state and iteratively copies the data for the VFIO device until 78 the vendor driver indicates that no data remains. 79 80* A ``load_state`` function that loads the config section and the data 81 sections that are generated by the save functions above. 82 83* A ``load_state_buffer`` function that loads the device state and the device 84 config that arrived via multifd channels. 85 It's used only in the multifd mode. 86 87* ``cleanup`` functions for both save and load that perform any migration 88 related cleanup. 89 90 91The VFIO migration code uses a VM state change handler to change the VFIO 92device state when the VM state changes from running to not-running, and 93vice versa. 94 95Similarly, a migration state change handler is used to trigger a transition of 96the VFIO device state when certain changes of the migration state occur. For 97example, the VFIO device state is transitioned back to _RUNNING in case a 98migration failed or was canceled. 99 100System memory dirty pages tracking 101---------------------------------- 102 103A ``log_global_start`` and ``log_global_stop`` memory listener callback informs 104the VFIO dirty tracking module to start and stop dirty page tracking. A 105``log_sync`` memory listener callback queries the dirty page bitmap from the 106dirty tracking module and marks system memory pages which were DMA-ed by the 107VFIO device as dirty. The dirty page bitmap is queried per container. 108 109Currently there are two ways dirty page tracking can be done: 110(1) Device dirty tracking: 111In this method the device is responsible to log and report its DMAs. This 112method can be used only if the device is capable of tracking its DMAs. 113Discovering device capability, starting and stopping dirty tracking, and 114syncing the dirty bitmaps from the device are done using the DMA logging uAPI. 115More info about the uAPI can be found in the comments of the 116``vfio_device_feature_dma_logging_control`` and 117``vfio_device_feature_dma_logging_report`` structures in the header file 118linux-headers/linux/vfio.h. 119 120(2) VFIO IOMMU module: 121In this method dirty tracking is done by IOMMU. However, there is currently no 122IOMMU support for dirty page tracking. For this reason, all pages are 123perpetually marked dirty, unless the device driver pins pages through external 124APIs in which case only those pinned pages are perpetually marked dirty. 125 126If the above two methods are not supported, all pages are perpetually marked 127dirty by QEMU. 128 129By default, dirty pages are tracked during pre-copy as well as stop-and-copy 130phase. So, a page marked as dirty will be copied to the destination in both 131phases. Copying dirty pages in pre-copy phase helps QEMU to predict if it can 132achieve its downtime tolerances. If QEMU during pre-copy phase keeps finding 133dirty pages continuously, then it understands that even in stop-and-copy phase, 134it is likely to find dirty pages and can predict the downtime accordingly. 135 136QEMU also provides a per device opt-out option ``pre-copy-dirty-page-tracking`` 137which disables querying the dirty bitmap during pre-copy phase. If it is set to 138off, all dirty pages will be copied to the destination in stop-and-copy phase 139only. 140 141System memory dirty pages tracking when vIOMMU is enabled 142--------------------------------------------------------- 143 144With vIOMMU, an IO virtual address range can get unmapped while in pre-copy 145phase of migration. In that case, the unmap ioctl returns any dirty pages in 146that range and QEMU reports corresponding guest physical pages dirty. During 147stop-and-copy phase, an IOMMU notifier is used to get a callback for mapped 148pages and then dirty pages bitmap is fetched from VFIO IOMMU modules for those 149mapped ranges. If device dirty tracking is enabled with vIOMMU, live migration 150will be blocked. 151 152Flow of state changes during Live migration 153=========================================== 154 155Below is the state change flow during live migration for a VFIO device that 156supports both precopy and P2P migration. The flow for devices that don't 157support it is similar, except that the relevant states for precopy and P2P are 158skipped. 159The values in the parentheses represent the VM state, the migration state, and 160the VFIO device state, respectively. 161 162Live migration save path 163------------------------ 164 165:: 166 167 QEMU normal running state 168 (RUNNING, _NONE, _RUNNING) 169 | 170 migrate_init spawns migration_thread 171 Migration thread then calls each device's .save_setup() 172 (RUNNING, _SETUP, _PRE_COPY) 173 | 174 (RUNNING, _ACTIVE, _PRE_COPY) 175 If device is active, get pending_bytes by .state_pending_{estimate,exact}() 176 If total pending_bytes >= threshold_size, call .save_live_iterate() 177 Data of VFIO device for pre-copy phase is copied 178 Iterate till total pending bytes converge and are less than threshold 179 | 180 On migration completion, the vCPUs and the VFIO device are stopped 181 The VFIO device is first put in P2P quiescent state 182 (FINISH_MIGRATE, _ACTIVE, _PRE_COPY_P2P) 183 | 184 Then the VFIO device is put in _STOP_COPY state 185 (FINISH_MIGRATE, _ACTIVE, _STOP_COPY) 186 .save_live_complete_precopy() is called for each active device 187 For the VFIO device, iterate in .save_live_complete_precopy() until 188 pending data is 0 189 | 190 (POSTMIGRATE, _COMPLETED, _STOP_COPY) 191 Migraton thread schedules cleanup bottom half and exits 192 | 193 .save_cleanup() is called 194 (POSTMIGRATE, _COMPLETED, _STOP) 195 196Live migration resume path 197-------------------------- 198 199:: 200 201 Incoming migration calls .load_setup() for each device 202 (RESTORE_VM, _ACTIVE, _STOP) 203 | 204 For each device, .load_state() is called for that device section data 205 transmitted via the main migration channel. 206 For data transmitted via multifd channels .load_state_buffer() is called 207 instead. 208 (RESTORE_VM, _ACTIVE, _RESUMING) 209 | 210 At the end, .load_cleanup() is called for each device and vCPUs are started 211 The VFIO device is first put in P2P quiescent state 212 (RUNNING, _ACTIVE, _RUNNING_P2P) 213 | 214 (RUNNING, _NONE, _RUNNING) 215 216Postcopy 217======== 218 219Postcopy migration is currently not supported for VFIO devices. 220