12a578133STarun Gupta===================== 2f6bbac98SPeter XuVFIO device migration 32a578133STarun Gupta===================== 42a578133STarun Gupta 52a578133STarun GuptaMigration of virtual machine involves saving the state for each device that 62a578133STarun Guptathe guest is running on source host and restoring this saved state on the 72a578133STarun Guptadestination host. This document details how saving and restoring of VFIO 82a578133STarun Guptadevices is done in QEMU. 92a578133STarun Gupta 10eda7362aSAvihai HoronMigration of VFIO devices consists of two phases: the optional pre-copy phase, 11eda7362aSAvihai Horonand the stop-and-copy phase. The pre-copy phase is iterative and allows to 12eda7362aSAvihai Horonaccommodate VFIO devices that have a large amount of data that needs to be 13eda7362aSAvihai Horontransferred. The iterative pre-copy phase of migration allows for the guest to 14eda7362aSAvihai Horoncontinue whilst the VFIO device state is transferred to the destination, this 15eda7362aSAvihai Horonhelps to reduce the total downtime of the VM. VFIO devices opt-in to pre-copy 16eda7362aSAvihai Horonsupport by reporting the VFIO_MIGRATION_PRE_COPY flag in the 17eda7362aSAvihai HoronVFIO_DEVICE_FEATURE_MIGRATION ioctl. 182b0ab9e9SAvihai Horon 19745c4291SAvihai HoronWhen pre-copy is supported, it's possible to further reduce downtime by 20745c4291SAvihai Horonenabling "switchover-ack" migration capability. 21745c4291SAvihai HoronVFIO migration uAPI defines "initial bytes" as part of its pre-copy data stream 22745c4291SAvihai Horonand recommends that the initial bytes are sent and loaded in the destination 23745c4291SAvihai Horonbefore stopping the source VM. Enabling this migration capability will 24745c4291SAvihai Horonguarantee that and thus, can potentially reduce downtime even further. 25745c4291SAvihai Horon 2694f775e4SAvihai HoronTo support migration of multiple devices that might do P2P transactions between 2794f775e4SAvihai Horonthemselves, VFIO migration uAPI defines an intermediate P2P quiescent state. 2894f775e4SAvihai HoronWhile in the P2P quiescent state, P2P DMA transactions cannot be initiated by 2994f775e4SAvihai Horonthe device, but the device can respond to incoming ones. Additionally, all 3094f775e4SAvihai Horonoutstanding P2P transactions are guaranteed to have been completed by the time 3194f775e4SAvihai Horonthe device enters this state. 3294f775e4SAvihai Horon 3394f775e4SAvihai HoronAll the devices that support P2P migration are first transitioned to the P2P 3494f775e4SAvihai Horonquiescent state and only then are they stopped or started. This makes migration 3594f775e4SAvihai Horonsafe P2P-wise, since starting and stopping the devices is not done atomically 3694f775e4SAvihai Horonfor all the devices together. 3794f775e4SAvihai Horon 3894f775e4SAvihai HoronThus, multiple VFIO devices migration is allowed only if all the devices 3994f775e4SAvihai Horonsupport P2P migration. Single VFIO device migration is allowed regardless of 4094f775e4SAvihai HoronP2P migration support. 412a578133STarun Gupta 422a578133STarun GuptaA detailed description of the UAPI for VFIO device migration can be found in 432b0ab9e9SAvihai Horonthe comment for the ``vfio_device_mig_state`` structure in the header file 442b0ab9e9SAvihai Horonlinux-headers/linux/vfio.h. 452a578133STarun Gupta 462a578133STarun GuptaVFIO implements the device hooks for the iterative approach as follows: 472a578133STarun Gupta 482b0ab9e9SAvihai Horon* A ``save_setup`` function that sets up migration on the source. 492a578133STarun Gupta 502b0ab9e9SAvihai Horon* A ``load_setup`` function that sets the VFIO device on the destination in 512b0ab9e9SAvihai Horon _RESUMING state. 522a578133STarun Gupta 53eda7362aSAvihai Horon* A ``state_pending_estimate`` function that reports an estimate of the 54eda7362aSAvihai Horon remaining pre-copy data that the vendor driver has yet to save for the VFIO 55eda7362aSAvihai Horon device. 56eda7362aSAvihai Horon 57c8df4a7aSJuan Quintela* A ``state_pending_exact`` function that reads pending_bytes from the vendor 582a578133STarun Gupta driver, which indicates the amount of data that the vendor driver has yet to 592a578133STarun Gupta save for the VFIO device. 602a578133STarun Gupta 61eda7362aSAvihai Horon* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is 62eda7362aSAvihai Horon active only when the VFIO device is in pre-copy states. 63eda7362aSAvihai Horon 64eda7362aSAvihai Horon* A ``save_live_iterate`` function that reads the VFIO device's data from the 65eda7362aSAvihai Horon vendor driver during iterative pre-copy phase. 66eda7362aSAvihai Horon 67745c4291SAvihai Horon* A ``switchover_ack_needed`` function that checks if the VFIO device uses 68745c4291SAvihai Horon "switchover-ack" migration capability when this capability is enabled. 69745c4291SAvihai Horon 70c59748c1SMaciej S. Szmigiero* A ``switchover_start`` function that in the multifd mode starts a thread that 71c59748c1SMaciej S. Szmigiero reassembles the multifd received data and loads it in-order into the device. 72c59748c1SMaciej S. Szmigiero In the non-multifd mode this function is a NOP. 73c59748c1SMaciej S. Szmigiero 746d644baeSMaciej S. Szmigiero* A ``save_state`` function to save the device config space if it is present 756d644baeSMaciej S. Szmigiero in the non-multifd mode. 766d644baeSMaciej S. Szmigiero In the multifd mode it just emits either a dummy EOS marker. 772a578133STarun Gupta 782b0ab9e9SAvihai Horon* A ``save_live_complete_precopy`` function that sets the VFIO device in 792b0ab9e9SAvihai Horon _STOP_COPY state and iteratively copies the data for the VFIO device until 802b0ab9e9SAvihai Horon the vendor driver indicates that no data remains. 816d644baeSMaciej S. Szmigiero In the multifd mode it just emits a dummy EOS marker. 826d644baeSMaciej S. Szmigiero 836d644baeSMaciej S. Szmigiero* A ``save_live_complete_precopy_thread`` function that in the multifd mode 846d644baeSMaciej S. Szmigiero provides thread handler performing multifd device state transfer. 856d644baeSMaciej S. Szmigiero It sets the VFIO device to _STOP_COPY state, iteratively reads the data 866d644baeSMaciej S. Szmigiero from the VFIO device and queues it for multifd transmission until the vendor 876d644baeSMaciej S. Szmigiero driver indicates that no data remains. 886d644baeSMaciej S. Szmigiero After that, it saves the device config space and queues it for multifd 896d644baeSMaciej S. Szmigiero transfer too. 906d644baeSMaciej S. Szmigiero In the non-multifd mode this thread is a NOP. 912a578133STarun Gupta 922a578133STarun Gupta* A ``load_state`` function that loads the config section and the data 932b0ab9e9SAvihai Horon sections that are generated by the save functions above. 942a578133STarun Gupta 953228d311SMaciej S. Szmigiero* A ``load_state_buffer`` function that loads the device state and the device 963228d311SMaciej S. Szmigiero config that arrived via multifd channels. 973228d311SMaciej S. Szmigiero It's used only in the multifd mode. 983228d311SMaciej S. Szmigiero 992a578133STarun Gupta* ``cleanup`` functions for both save and load that perform any migration 1002b0ab9e9SAvihai Horon related cleanup. 1012a578133STarun Gupta 1022a578133STarun Gupta 1032a578133STarun GuptaThe VFIO migration code uses a VM state change handler to change the VFIO 1042a578133STarun Guptadevice state when the VM state changes from running to not-running, and 1052a578133STarun Guptavice versa. 1062a578133STarun Gupta 1072a578133STarun GuptaSimilarly, a migration state change handler is used to trigger a transition of 1082a578133STarun Guptathe VFIO device state when certain changes of the migration state occur. For 1092a578133STarun Guptaexample, the VFIO device state is transitioned back to _RUNNING in case a 1102a578133STarun Guptamigration failed or was canceled. 1112a578133STarun Gupta 1122a578133STarun GuptaSystem memory dirty pages tracking 1132a578133STarun Gupta---------------------------------- 1142a578133STarun Gupta 1152a578133STarun GuptaA ``log_global_start`` and ``log_global_stop`` memory listener callback informs 116333f988dSAvihai Horonthe VFIO dirty tracking module to start and stop dirty page tracking. A 117333f988dSAvihai Horon``log_sync`` memory listener callback queries the dirty page bitmap from the 118333f988dSAvihai Horondirty tracking module and marks system memory pages which were DMA-ed by the 119333f988dSAvihai HoronVFIO device as dirty. The dirty page bitmap is queried per container. 120333f988dSAvihai Horon 121333f988dSAvihai HoronCurrently there are two ways dirty page tracking can be done: 122333f988dSAvihai Horon(1) Device dirty tracking: 123333f988dSAvihai HoronIn this method the device is responsible to log and report its DMAs. This 124333f988dSAvihai Horonmethod can be used only if the device is capable of tracking its DMAs. 125333f988dSAvihai HoronDiscovering device capability, starting and stopping dirty tracking, and 126333f988dSAvihai Horonsyncing the dirty bitmaps from the device are done using the DMA logging uAPI. 127333f988dSAvihai HoronMore info about the uAPI can be found in the comments of the 128333f988dSAvihai Horon``vfio_device_feature_dma_logging_control`` and 129333f988dSAvihai Horon``vfio_device_feature_dma_logging_report`` structures in the header file 130333f988dSAvihai Horonlinux-headers/linux/vfio.h. 131333f988dSAvihai Horon 132333f988dSAvihai Horon(2) VFIO IOMMU module: 133333f988dSAvihai HoronIn this method dirty tracking is done by IOMMU. However, there is currently no 134333f988dSAvihai HoronIOMMU support for dirty page tracking. For this reason, all pages are 135333f988dSAvihai Horonperpetually marked dirty, unless the device driver pins pages through external 136333f988dSAvihai HoronAPIs in which case only those pinned pages are perpetually marked dirty. 137333f988dSAvihai Horon 138333f988dSAvihai HoronIf the above two methods are not supported, all pages are perpetually marked 139333f988dSAvihai Horondirty by QEMU. 1402a578133STarun Gupta 1412b0ab9e9SAvihai HoronBy default, dirty pages are tracked during pre-copy as well as stop-and-copy 142333f988dSAvihai Horonphase. So, a page marked as dirty will be copied to the destination in both 143333f988dSAvihai Horonphases. Copying dirty pages in pre-copy phase helps QEMU to predict if it can 144333f988dSAvihai Horonachieve its downtime tolerances. If QEMU during pre-copy phase keeps finding 145333f988dSAvihai Horondirty pages continuously, then it understands that even in stop-and-copy phase, 146333f988dSAvihai Horonit is likely to find dirty pages and can predict the downtime accordingly. 1472a578133STarun Gupta 1482a578133STarun GuptaQEMU also provides a per device opt-out option ``pre-copy-dirty-page-tracking`` 1492a578133STarun Guptawhich disables querying the dirty bitmap during pre-copy phase. If it is set to 1502a578133STarun Guptaoff, all dirty pages will be copied to the destination in stop-and-copy phase 1512a578133STarun Guptaonly. 1522a578133STarun Gupta 1532a578133STarun GuptaSystem memory dirty pages tracking when vIOMMU is enabled 1542a578133STarun Gupta--------------------------------------------------------- 1552a578133STarun Gupta 1562a578133STarun GuptaWith vIOMMU, an IO virtual address range can get unmapped while in pre-copy 1572a578133STarun Guptaphase of migration. In that case, the unmap ioctl returns any dirty pages in 1582a578133STarun Guptathat range and QEMU reports corresponding guest physical pages dirty. During 1592a578133STarun Guptastop-and-copy phase, an IOMMU notifier is used to get a callback for mapped 1602a578133STarun Guptapages and then dirty pages bitmap is fetched from VFIO IOMMU modules for those 161333f988dSAvihai Horonmapped ranges. If device dirty tracking is enabled with vIOMMU, live migration 162333f988dSAvihai Horonwill be blocked. 1632a578133STarun Gupta 1642a578133STarun GuptaFlow of state changes during Live migration 1652a578133STarun Gupta=========================================== 1662a578133STarun Gupta 16794f775e4SAvihai HoronBelow is the state change flow during live migration for a VFIO device that 16894f775e4SAvihai Horonsupports both precopy and P2P migration. The flow for devices that don't 16994f775e4SAvihai Horonsupport it is similar, except that the relevant states for precopy and P2P are 17094f775e4SAvihai Horonskipped. 171eda7362aSAvihai HoronThe values in the parentheses represent the VM state, the migration state, and 1722a578133STarun Guptathe VFIO device state, respectively. 1732a578133STarun Gupta 1742a578133STarun GuptaLive migration save path 1752a578133STarun Gupta------------------------ 1762a578133STarun Gupta 1772a578133STarun Gupta:: 1782a578133STarun Gupta 1792a578133STarun Gupta QEMU normal running state 1802a578133STarun Gupta (RUNNING, _NONE, _RUNNING) 1812a578133STarun Gupta | 1822a578133STarun Gupta migrate_init spawns migration_thread 1832a578133STarun Gupta Migration thread then calls each device's .save_setup() 18494f775e4SAvihai Horon (RUNNING, _SETUP, _PRE_COPY) 1852a578133STarun Gupta | 18694f775e4SAvihai Horon (RUNNING, _ACTIVE, _PRE_COPY) 187eda7362aSAvihai Horon If device is active, get pending_bytes by .state_pending_{estimate,exact}() 1882a578133STarun Gupta If total pending_bytes >= threshold_size, call .save_live_iterate() 18994f775e4SAvihai Horon Data of VFIO device for pre-copy phase is copied 1902a578133STarun Gupta Iterate till total pending bytes converge and are less than threshold 1912a578133STarun Gupta | 19294f775e4SAvihai Horon On migration completion, the vCPUs and the VFIO device are stopped 19394f775e4SAvihai Horon The VFIO device is first put in P2P quiescent state 19494f775e4SAvihai Horon (FINISH_MIGRATE, _ACTIVE, _PRE_COPY_P2P) 1952a578133STarun Gupta | 19694f775e4SAvihai Horon Then the VFIO device is put in _STOP_COPY state 19794f775e4SAvihai Horon (FINISH_MIGRATE, _ACTIVE, _STOP_COPY) 19894f775e4SAvihai Horon .save_live_complete_precopy() is called for each active device 1996d644baeSMaciej S. Szmigiero For the VFIO device: in the non-multifd mode iterate in 2006d644baeSMaciej S. Szmigiero .save_live_complete_precopy() until 2012a578133STarun Gupta pending data is 0 2026d644baeSMaciej S. Szmigiero In the multifd mode this iteration is done in 2036d644baeSMaciej S. Szmigiero .save_live_complete_precopy_thread() instead. 2042a578133STarun Gupta | 20594f775e4SAvihai Horon (POSTMIGRATE, _COMPLETED, _STOP_COPY) 2062a578133STarun Gupta Migraton thread schedules cleanup bottom half and exits 20794f775e4SAvihai Horon | 20894f775e4SAvihai Horon .save_cleanup() is called 20994f775e4SAvihai Horon (POSTMIGRATE, _COMPLETED, _STOP) 2102a578133STarun Gupta 2112a578133STarun GuptaLive migration resume path 2122a578133STarun Gupta-------------------------- 2132a578133STarun Gupta 2142a578133STarun Gupta:: 2152a578133STarun Gupta 21694f775e4SAvihai Horon Incoming migration calls .load_setup() for each device 2172b0ab9e9SAvihai Horon (RESTORE_VM, _ACTIVE, _STOP) 2182a578133STarun Gupta | 21994f775e4SAvihai Horon For each device, .load_state() is called for that device section data 2203228d311SMaciej S. Szmigiero transmitted via the main migration channel. 2213228d311SMaciej S. Szmigiero For data transmitted via multifd channels .load_state_buffer() is called 2223228d311SMaciej S. Szmigiero instead. 2232a578133STarun Gupta (RESTORE_VM, _ACTIVE, _RESUMING) 2242a578133STarun Gupta | 22594f775e4SAvihai Horon At the end, .load_cleanup() is called for each device and vCPUs are started 22694f775e4SAvihai Horon The VFIO device is first put in P2P quiescent state 22794f775e4SAvihai Horon (RUNNING, _ACTIVE, _RUNNING_P2P) 22894f775e4SAvihai Horon | 2292a578133STarun Gupta (RUNNING, _NONE, _RUNNING) 2302a578133STarun Gupta 2312a578133STarun GuptaPostcopy 2322a578133STarun Gupta======== 2332a578133STarun Gupta 2342a578133STarun GuptaPostcopy migration is currently not supported for VFIO devices. 235*623af41dSMaciej S. Szmigiero 236*623af41dSMaciej S. SzmigieroMultifd 237*623af41dSMaciej S. Szmigiero======= 238*623af41dSMaciej S. Szmigiero 239*623af41dSMaciej S. SzmigieroStarting from QEMU version 10.0 there's a possibility to transfer VFIO device 240*623af41dSMaciej S. Szmigiero_STOP_COPY state via multifd channels. This helps reduce downtime - especially 241*623af41dSMaciej S. Szmigierowith multiple VFIO devices or with devices having a large migration state. 242*623af41dSMaciej S. SzmigieroAs an additional benefit, setting the VFIO device to _STOP_COPY state and 243*623af41dSMaciej S. Szmigierosaving its config space is also parallelized (run in a separate thread) in 244*623af41dSMaciej S. Szmigierosuch migration mode. 245*623af41dSMaciej S. Szmigiero 246*623af41dSMaciej S. SzmigieroThe multifd VFIO device state transfer is controlled by 247*623af41dSMaciej S. Szmigiero"x-migration-multifd-transfer" VFIO device property. This property defaults to 248*623af41dSMaciej S. SzmigieroAUTO, which means that VFIO device state transfer via multifd channels is 249*623af41dSMaciej S. Szmigieroattempted in configurations that otherwise support it. 250