From nobody Fri Apr 26 16:08:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513353066285149.63687289957534; Fri, 15 Dec 2017 07:51:06 -0800 (PST) Received: from localhost ([::1]:47249 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePsGF-0005St-PW for importer@patchew.org; Fri, 15 Dec 2017 10:50:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54566) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePsAs-00022T-Bo for qemu-devel@nongnu.org; Fri, 15 Dec 2017 10:45:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePsAn-0006Q5-Ca for qemu-devel@nongnu.org; Fri, 15 Dec 2017 10:45:22 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58454) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ePsAn-0006Pb-0c for qemu-devel@nongnu.org; Fri, 15 Dec 2017 10:45:17 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ED4284E334 for ; Fri, 15 Dec 2017 15:45:15 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-102.ams2.redhat.com [10.36.117.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D2A960851; Fri, 15 Dec 2017 15:45:08 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, kashyap@redhat.com, quintela@redhat.com, berrange@redhat.com Date: Fri, 15 Dec 2017 15:45:07 +0000 Message-Id: <20171215154507.20299-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 15 Dec 2017 15:45:16 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2] docs: Convert migration.txt to rst X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Mostly just manual conversion with very minor fixes. Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Daniel P. Berrange Reviewed-by: Kashyap Chamarthy Reviewed-by: Peter Xu --- v2 Fixed issues found by Peter and Kashyap. Remove the detailed QEMUFile explanation and point to the header and QIOChannel code [after discussion with Daniel on Jay Zhou's patch] docs/devel/{migration.txt =3D> migration.rst} | 476 +++++++++++++++-------= ------ 1 file changed, 250 insertions(+), 226 deletions(-) rename docs/devel/{migration.txt =3D> migration.rst} (58%) diff --git a/docs/devel/migration.txt b/docs/devel/migration.rst similarity index 58% rename from docs/devel/migration.txt rename to docs/devel/migration.rst index 4030703726..bf97080dac 100644 --- a/docs/devel/migration.txt +++ b/docs/devel/migration.rst @@ -1,4 +1,6 @@ -=3D Migration =3D +=3D=3D=3D=3D=3D=3D=3D=3D=3D +Migration +=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 QEMU has code to load/save the state of the guest that it is running. These are two complementary operations. Saving the state just does @@ -26,7 +28,8 @@ the guest to be stopped. Typically the time that the gue= st is unresponsive during live migration is the low hundred of milliseconds (notice that this depends on a lot of things). =20 -=3D=3D=3D Types of migration =3D=3D=3D +Types of migration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 Now that we have talked about live migration, there are several ways to do migration: @@ -41,49 +44,21 @@ All these four migration protocols use the same infrast= ructure to save/restore state devices. This infrastructure is shared with the savevm/loadvm functionality. =20 -=3D=3D=3D State Live Migration =3D=3D=3D +State Live Migration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 This is used for RAM and block devices. It is not yet ported to vmstate. =20 -=3D=3D=3D What is the common infrastructure =3D=3D=3D +Common infrastructure +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 -QEMU uses a QEMUFile abstraction to be able to do migration. Any type -of migration that wants to use QEMU infrastructure has to create a -QEMUFile with: +The files, sockets or fd's that carry the migration stream are abstracted = by +the ``QEMUFile`` type (see `migration/qemu-file.h`). In most cases this +is connected to a subtype of ``QIOChannel`` (see `io/`). =20 -QEMUFile *qemu_fopen_ops(void *opaque, - QEMUFilePutBufferFunc *put_buffer, - QEMUFileGetBufferFunc *get_buffer, - QEMUFileCloseFunc *close); - -The functions have the following functionality: - -This function writes a chunk of data to a file at the given position. -The pos argument can be ignored if the file is only used for -streaming. The handler should try to write all of the data it can. - -typedef int (QEMUFilePutBufferFunc)(void *opaque, const uint8_t *buf, - int64_t pos, int size); - -Read a chunk of data from a file at the given position. The pos argument -can be ignored if the file is only be used for streaming. The number of -bytes actually read should be returned. - -typedef int (QEMUFileGetBufferFunc)(void *opaque, uint8_t *buf, - int64_t pos, int size); - -Close a file and return an error code. - -typedef int (QEMUFileCloseFunc)(void *opaque); - -You can use any internal state that you need using the opaque void * -pointer that is passed to all functions. - -The important functions for us are put_buffer()/get_buffer() that -allow to write/read a buffer into the QEMUFile. - -=3D=3D=3D How to save the state of one device =3D=3D=3D +Saving the state of one device +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D =20 The state of a device is saved using intermediate buffers. There are some helper functions to assist this saving. @@ -93,34 +68,38 @@ version. When we migrate a device, we save/load the st= ate as a series of fields. Some times, due to bugs or new functionality, we need to change the state to store more/different information. We use the version to identify each time that we do a change. Each version is -associated with a series of fields saved. The save_state always saves -the state as the newer version. But load_state sometimes is able to +associated with a series of fields saved. The `save_state` always saves +the state as the newer version. But `load_state` sometimes is able to load state from an older version. =20 -=3D=3D=3D Legacy way =3D=3D=3D +Legacy way +---------- =20 This way is going to disappear as soon as all current users are ported to = VMSTATE. =20 Each device has to register two functions, one to save the state and another to load the state back. =20 -int register_savevm(DeviceState *dev, - const char *idstr, - int instance_id, - int version_id, - SaveStateHandler *save_state, - LoadStateHandler *load_state, - void *opaque); +.. code:: c + + int register_savevm(DeviceState *dev, + const char *idstr, + int instance_id, + int version_id, + SaveStateHandler *save_state, + LoadStateHandler *load_state, + void *opaque); =20 -typedef void SaveStateHandler(QEMUFile *f, void *opaque); -typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id); + typedef void SaveStateHandler(QEMUFile *f, void *opaque); + typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id); =20 -The important functions for the device state format are the save_state -and load_state. Notice that load_state receives a version_id -parameter to know what state format is receiving. save_state doesn't +The important functions for the device state format are the `save_state` +and `load_state`. Notice that `load_state` receives a version_id +parameter to know what state format is receiving. `save_state` doesn't have a version_id parameter because it always uses the latest version. =20 -=3D=3D=3D VMState =3D=3D=3D +VMState +------- =20 The legacy way of saving/loading state of the device had the problem that we have to maintain two functions in sync. If we did one change @@ -135,31 +114,36 @@ save/load functions. =20 An example (from hw/input/pckbd.c) =20 -static const VMStateDescription vmstate_kbd =3D { - .name =3D "pckbd", - .version_id =3D 3, - .minimum_version_id =3D 3, - .fields =3D (VMStateField[]) { - VMSTATE_UINT8(write_cmd, KBDState), - VMSTATE_UINT8(status, KBDState), - VMSTATE_UINT8(mode, KBDState), - VMSTATE_UINT8(pending, KBDState), - VMSTATE_END_OF_LIST() - } -}; +.. code:: c + + static const VMStateDescription vmstate_kbd =3D { + .name =3D "pckbd", + .version_id =3D 3, + .minimum_version_id =3D 3, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8(write_cmd, KBDState), + VMSTATE_UINT8(status, KBDState), + VMSTATE_UINT8(mode, KBDState), + VMSTATE_UINT8(pending, KBDState), + VMSTATE_END_OF_LIST() + } + }; =20 We are declaring the state with name "pckbd". -The version_id is 3, and the fields are 4 uint8_t in a KBDState structure. +The `version_id` is 3, and the fields are 4 uint8_t in a KBDState structur= e. We registered this with: =20 +.. code:: c + vmstate_register(NULL, 0, &vmstate_kbd, s); =20 Note: talk about how vmstate <-> qdev interact, and what the instance ids = mean. =20 -You can search for VMSTATE_* macros for lots of types used in QEMU in +You can search for ``VMSTATE_*`` macros for lots of types used in QEMU in include/hw/hw.h. =20 -=3D=3D=3D More about versions =3D=3D=3D +More about versions +------------------- =20 Version numbers are intended for major incompatible changes to the migration of a device, and using them breaks backwards-migration @@ -168,22 +152,23 @@ compatibility; in general most changes can be made by= adding Subsections =20 You can see that there are several version fields: =20 -- version_id: the maximum version_id supported by VMState for that device. -- minimum_version_id: the minimum version_id that VMState is able to under= stand +- `version_id`: the maximum version_id supported by VMState for that devic= e. +- `minimum_version_id`: the minimum version_id that VMState is able to und= erstand for that device. -- minimum_version_id_old: For devices that were not able to port to vmstat= e, we can +- `minimum_version_id_old`: For devices that were not able to port to vmst= ate, we can assign a function that knows how to read this old state. This field is - ignored if there is no load_state_old handler. + ignored if there is no `load_state_old` handler. =20 So, VMState is able to read versions from minimum_version_id to -version_id. And the function load_state_old() (if present) is able to +version_id. And the function ``load_state_old()`` (if present) is able to load state from minimum_version_id_old to minimum_version_id. This function is deprecated and will be removed when no more users are left. =20 Saving state will always create a section with the 'version_id' value and thus can't be loaded by any older QEMU. =20 -=3D=3D=3D Massaging functions =3D=3D=3D +Massaging functions +------------------- =20 Sometimes, it is not enough to be able to save the state directly from one structure, we need to fill the correct values there. One @@ -194,24 +179,24 @@ load the state for the cpu that we have just loaded f= rom the QEMUFile. =20 The functions to do that are inside a vmstate definition, and are called: =20 -- int (*pre_load)(void *opaque); +- ``int (*pre_load)(void *opaque);`` =20 This function is called before we load the state of one device. =20 -- int (*post_load)(void *opaque, int version_id); +- ``int (*post_load)(void *opaque, int version_id);`` =20 This function is called after we load the state of one device. =20 -- int (*pre_save)(void *opaque); +- ``int (*pre_save)(void *opaque);`` =20 This function is called before we save the state of one device. =20 Example: You can look at hpet.c, that uses the three function to - massage the state that is transferred. +massage the state that is transferred. =20 If you use memory API functions that update memory layout outside initialization (i.e., in response to a guest action), this is a strong -indication that you need to call these functions in a post_load callback. +indication that you need to call these functions in a `post_load` callback. Examples of such memory API functions are: =20 - memory_region_add_subregion() @@ -221,7 +206,8 @@ Examples of such memory API functions are: - memory_region_set_address() - memory_region_set_alias_offset() =20 -=3D=3D=3D Subsections =3D=3D=3D +Subsections +----------- =20 The use of version_id allows to be able to migrate from older versions to newer versions of a device. But not the other way around. This @@ -251,52 +237,54 @@ value that it uses. =20 Example: =20 -static bool ide_drive_pio_state_needed(void *opaque) -{ - IDEState *s =3D opaque; - - return ((s->status & DRQ_STAT) !=3D 0) - || (s->bus->error_status & BM_STATUS_PIO_RETRY); -} - -const VMStateDescription vmstate_ide_drive_pio_state =3D { - .name =3D "ide_drive/pio_state", - .version_id =3D 1, - .minimum_version_id =3D 1, - .pre_save =3D ide_drive_pio_pre_save, - .post_load =3D ide_drive_pio_post_load, - .needed =3D ide_drive_pio_state_needed, - .fields =3D (VMStateField[]) { - VMSTATE_INT32(req_nb_sectors, IDEState), - VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1, - vmstate_info_uint8, uint8_t), - VMSTATE_INT32(cur_io_buffer_offset, IDEState), - VMSTATE_INT32(cur_io_buffer_len, IDEState), - VMSTATE_UINT8(end_transfer_fn_idx, IDEState), - VMSTATE_INT32(elementary_transfer_size, IDEState), - VMSTATE_INT32(packet_transfer_size, IDEState), - VMSTATE_END_OF_LIST() - } -}; - -const VMStateDescription vmstate_ide_drive =3D { - .name =3D "ide_drive", - .version_id =3D 3, - .minimum_version_id =3D 0, - .post_load =3D ide_drive_post_load, - .fields =3D (VMStateField[]) { - .... several fields .... - VMSTATE_END_OF_LIST() - }, - .subsections =3D (const VMStateDescription*[]) { - &vmstate_ide_drive_pio_state, - NULL - } -}; +.. code:: c + + static bool ide_drive_pio_state_needed(void *opaque) + { + IDEState *s =3D opaque; + + return ((s->status & DRQ_STAT) !=3D 0) + || (s->bus->error_status & BM_STATUS_PIO_RETRY); + } + + const VMStateDescription vmstate_ide_drive_pio_state =3D { + .name =3D "ide_drive/pio_state", + .version_id =3D 1, + .minimum_version_id =3D 1, + .pre_save =3D ide_drive_pio_pre_save, + .post_load =3D ide_drive_pio_post_load, + .needed =3D ide_drive_pio_state_needed, + .fields =3D (VMStateField[]) { + VMSTATE_INT32(req_nb_sectors, IDEState), + VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1, + vmstate_info_uint8, uint8_t), + VMSTATE_INT32(cur_io_buffer_offset, IDEState), + VMSTATE_INT32(cur_io_buffer_len, IDEState), + VMSTATE_UINT8(end_transfer_fn_idx, IDEState), + VMSTATE_INT32(elementary_transfer_size, IDEState), + VMSTATE_INT32(packet_transfer_size, IDEState), + VMSTATE_END_OF_LIST() + } + }; + + const VMStateDescription vmstate_ide_drive =3D { + .name =3D "ide_drive", + .version_id =3D 3, + .minimum_version_id =3D 0, + .post_load =3D ide_drive_post_load, + .fields =3D (VMStateField[]) { + .... several fields .... + VMSTATE_END_OF_LIST() + }, + .subsections =3D (const VMStateDescription*[]) { + &vmstate_ide_drive_pio_state, + NULL + } + }; =20 Here we have a subsection for the pio state. We only need to save/send this state when we are in the middle of a pio operation -(that is what ide_drive_pio_state_needed() checks). If DRQ_STAT is +(that is what ``ide_drive_pio_state_needed()`` checks). If DRQ_STAT is not enabled, the values on that fields are garbage and don't need to be sent. =20 @@ -304,11 +292,12 @@ Using a condition function that checks a 'property' t= o determine whether to send a subsection allows backwards migration compatibility when new subsections are added. =20 -For example; - a) Add a new property using DEFINE_PROP_BOOL - e.g. support-foo and +For example: + + a) Add a new property using ``DEFINE_PROP_BOOL`` - e.g. support-foo and default it to true. - b) Add an entry to the HW_COMPAT_ for the previous version - that sets the property to false. + b) Add an entry to the ``HW_COMPAT_`` for the previous version that sets + the property to false. c) Add a static bool support_foo function that tests the property. d) Add a subsection with a .needed set to the support_foo function e) (potentially) Add a pre_load that sets up a default value for 'foo' @@ -332,25 +321,30 @@ in most cases. In general the preference is to tie t= he subsection to the machine type, and allow reliable migrations, unless the behaviour from omission of the subsection is really bad. =20 -=3D Not sending existing elements =3D +Not sending existing elements +----------------------------- + +Sometimes members of the VMState are no longer needed: =20 -Sometimes members of the VMState are no longer needed; - removing them will break migration compatibility - making them version dependent and bumping the version will break backwar= ds - migration compatibility. + - removing them will break migration compatibility + + - making them version dependent and bumping the version will break backw= ards migration compatibility. =20 The best way is to: - a) Add a new property/compatibility/function in the same way for subsect= ions - above. + + a) Add a new property/compatibility/function in the same way for subsect= ions above. b) replace the VMSTATE macro with the _TEST version of the macro, e.g.: - VMSTATE_UINT32(foo, barstruct) + + ``VMSTATE_UINT32(foo, barstruct)`` + becomes - VMSTATE_UINT32_TEST(foo, barstruct, pre_version_baz) =20 - Sometime in the future when we no longer care about the ancient -versions these can be killed off. + ``VMSTATE_UINT32_TEST(foo, barstruct, pre_version_baz)`` + + Sometime in the future when we no longer care about the ancient version= s these can be killed off. =20 -=3D Return path =3D +Return path +----------- =20 In most migration scenarios there is only a single data path that runs from the source VM to the destination, typically along a single fd (althou= gh @@ -360,19 +354,23 @@ However, some uses need two way communication; in par= ticular the Postcopy destination needs to be able to request pages on demand from the source. =20 For these scenarios there is a 'return path' from the destination to the s= ource; -qemu_file_get_return_path(QEMUFile* fwdpath) gives the QEMUFile* for the r= eturn +``qemu_file_get_return_path(QEMUFile* fwdpath)`` gives the QEMUFile* for t= he return path. =20 Source side + Forward path - written by migration thread Return path - opened by main thread, read by return-path thread =20 Destination side + Forward path - read by main thread Return path - opened by main thread, written by main thread AND post= copy - thread (protected by rp_mutex) + thread (protected by rp_mutex) + +Postcopy +=3D=3D=3D=3D=3D=3D=3D=3D =20 -=3D Postcopy =3D 'Postcopy' migration is a way to deal with migrations that refuse to conve= rge (or take too long to converge) its plus side is that there is an upper bou= nd on the amount of migration traffic and time it takes, the down side is that d= uring @@ -386,27 +384,30 @@ a fault that's translated by QEMU into a request to t= he source QEMU. Postcopy can be combined with precopy (i.e. normal migration) so that if p= recopy doesn't finish in a given time the switch is made to postcopy. =20 -=3D=3D=3D Enabling postcopy =3D=3D=3D +Enabling postcopy +----------------- =20 To enable postcopy, issue this command on the monitor prior to the start of migration: =20 -migrate_set_capability postcopy-ram on +``migrate_set_capability postcopy-ram on`` =20 The normal commands are then used to start a migration, which is still started in precopy mode. Issuing: =20 -migrate_start_postcopy +``migrate_start_postcopy`` =20 will now cause the transition from precopy to postcopy. It can be issued immediately after migration is started or any time later on. Issuing it after the end of a migration is harmless. =20 -Note: During the postcopy phase, the bandwidth limits set using -migrate_set_speed is ignored (to avoid delaying requested pages that -the destination is waiting for). +.. note:: + During the postcopy phase, the bandwidth limits set using + ``migrate_set_speed`` is ignored (to avoid delaying requested pages that + the destination is waiting for). =20 -=3D=3D=3D Postcopy device transfer =3D=3D=3D +Postcopy device transfer +------------------------ =20 Loading of device data may cause the device emulation to access guest RAM that may trigger faults that have to be resolved by the source, as such @@ -416,6 +417,7 @@ before the device load begins to free the stream up. T= his is achieved by 'packaging' the device data into a blob that's read in one go. =20 Source behaviour +---------------- =20 Until postcopy is entered the migration stream is identical to normal precopy, except for the addition of a 'postcopy advise' command at @@ -423,13 +425,14 @@ the beginning, to tell the destination that postcopy = might happen. When postcopy starts the source sends the page discard data and then forms the 'package' containing: =20 - Command: 'postcopy listen' - The device state - A series of sections, identical to the precopy streams device state = stream - containing everything except postcopiable devices (i.e. RAM) - Command: 'postcopy run' + - Command: 'postcopy listen' + - The device state =20 -The 'package' is sent as the data part of a Command: 'CMD_PACKAGED', and t= he + A series of sections, identical to the precopy streams device state s= tream + containing everything except postcopiable devices (i.e. RAM) + - Command: 'postcopy run' + +The 'package' is sent as the data part of a Command: ``CMD_PACKAGED``, and= the contents are formatted in the same way as the main migration stream. =20 During postcopy the source scans the list of dirty pages and sends them @@ -441,82 +444,100 @@ to be sent quickly in the hope that those pages are = likely to be used by the destination soon. =20 Destination behaviour +--------------------- =20 Initially the destination looks the same as precopy, with a single thread reading the migration stream; the 'postcopy advise' and 'discard' commands are processed to change the way RAM is managed, but don't affect the stream processing. =20 ---------------------------------------------------------------------------= ---- - 1 2 3 4 5 6 7 -main -----DISCARD-CMD_PACKAGED ( LISTEN DEVICE DEVICE DEVICE RUN ) -thread | | - | (page request) - | \___ - v \ -listen thread: --- page -- page -- page -- page -- pag= e -- - - a b c ---------------------------------------------------------------------------= ---- - -On receipt of CMD_PACKAGED (1) - All the data associated with the package - the ( ... ) section in the -diagram - is read into memory, and the main thread recurses into -qemu_loadvm_state_main to process the contents of the package (2) -which contains commands (3,6) and devices (4...) - -On receipt of 'postcopy listen' - 3 -(i.e. the 1st command in the package) -a new thread (a) is started that takes over servicing the migration stream, -while the main thread carries on loading the package. It loads normal -background page data (b) but if during a device load a fault happens (5) t= he -returned page (c) is loaded by the listen thread allowing the main threads -device load to carry on. - -The last thing in the CMD_PACKAGED is a 'RUN' command (6) letting the dest= ination -CPUs start running. -At the end of the CMD_PACKAGED (7) the main thread returns to normal runni= ng behaviour -and is no longer used by migration, while the listen thread carries -on servicing page data until the end of migration. - -=3D=3D=3D Postcopy states =3D=3D=3D +:: + + ------------------------------------------------------------------------= ------ + 1 2 3 4 5 6 7 + main -----DISCARD-CMD_PACKAGED ( LISTEN DEVICE DEVICE DEVICE RUN ) + thread | | + | (page request) + | \___ + v \ + listen thread: --- page -- page -- page -- page -- p= age -- + + a b c + ------------------------------------------------------------------------= ------ + +- On receipt of ``CMD_PACKAGED`` (1) + + All the data associated with the package - the ( ... ) section in the d= iagram - + is read into memory, and the main thread recurses into qemu_loadvm_stat= e_main + to process the contents of the package (2) which contains commands (3,6= ) and + devices (4...) + +- On receipt of 'postcopy listen' - 3 -(i.e. the 1st command in the packag= e) + + a new thread (a) is started that takes over servicing the migration str= eam, + while the main thread carries on loading the package. It loads normal + background page data (b) but if during a device load a fault happens (5) + the returned page (c) is loaded by the listen thread allowing the main + threads device load to carry on. + +- The last thing in the ``CMD_PACKAGED`` is a 'RUN' command (6) + + letting the destination CPUs start running. At the end of the + ``CMD_PACKAGED`` (7) the main thread returns to normal running behaviou= r and + is no longer used by migration, while the listen thread carries on serv= icing + page data until the end of migration. + +Postcopy states +--------------- =20 Postcopy moves through a series of states (see postcopy_state) from ADVISE->DISCARD->LISTEN->RUNNING->END =20 - Advise: Set at the start of migration if postcopy is enabled, even - if it hasn't had the start command; here the destination - checks that its OS has the support needed for postcopy, and per= forms - setup to ensure the RAM mappings are suitable for later postcop= y. - The destination will fail early in migration at this point if t= he - required OS support is not present. - (Triggered by reception of POSTCOPY_ADVISE command) - - Discard: Entered on receipt of the first 'discard' command; prior to - the first Discard being performed, hugepages are switched off - (using madvise) to ensure that no new huge pages are created - during the postcopy phase, and to cause any huge pages that - have discards on them to be broken. - - Listen: The first command in the package, POSTCOPY_LISTEN, switches - the destination state to Listen, and starts a new thread - (the 'listen thread') which takes over the job of receiving - pages off the migration stream, while the main thread carries - on processing the blob. With this thread able to process page - reception, the destination now 'sensitises' the RAM to detect - any access to missing pages (on Linux using the 'userfault' - system). - - Running: POSTCOPY_RUN causes the destination to synchronise all - state and start the CPUs and IO devices running. The main - thread now finishes processing the migration package and - now carries on as it would for normal precopy migration - (although it can't do the cleanup it would do as it - finishes a normal migration). - - End: The listen thread can now quit, and perform the cleanup of migr= ation - state, the migration is now complete. - -=3D=3D=3D Source side page maps =3D=3D=3D + - Advise + + Set at the start of migration if postcopy is enabled, even + if it hasn't had the start command; here the destination + checks that its OS has the support needed for postcopy, and performs + setup to ensure the RAM mappings are suitable for later postcopy. + The destination will fail early in migration at this point if the + required OS support is not present. + (Triggered by reception of POSTCOPY_ADVISE command) + + - Discard + + Entered on receipt of the first 'discard' command; prior to + the first Discard being performed, hugepages are switched off + (using madvise) to ensure that no new huge pages are created + during the postcopy phase, and to cause any huge pages that + have discards on them to be broken. + + - Listen + + The first command in the package, POSTCOPY_LISTEN, switches + the destination state to Listen, and starts a new thread + (the 'listen thread') which takes over the job of receiving + pages off the migration stream, while the main thread carries + on processing the blob. With this thread able to process page + reception, the destination now 'sensitises' the RAM to detect + any access to missing pages (on Linux using the 'userfault' + system). + + - Running + + POSTCOPY_RUN causes the destination to synchronise all + state and start the CPUs and IO devices running. The main + thread now finishes processing the migration package and + now carries on as it would for normal precopy migration + (although it can't do the cleanup it would do as it + finishes a normal migration). + + - End + + The listen thread can now quit, and perform the cleanup of migration + state, the migration is now complete. + +Source side page maps +--------------------- =20 The source side keeps two bitmaps during postcopy; 'the migration bitmap' and 'unsent map'. The 'migration bitmap' is basically the same as in @@ -529,6 +550,7 @@ The 'unsent map' is used for the transition to postcopy= . It is a bitmap that has a bit cleared whenever a page is sent to the destination, however duri= ng the transition to postcopy mode it is combined with the migration bitmap to form a set of pages that: + a) Have been sent but then redirtied (which must be discarded) b) Have not yet been sent - which also must be discarded to cause any transparent huge pages built during precopy to be broken. @@ -540,15 +562,17 @@ request for a page that has already been sent is igno= red. Duplicate requests such as this can happen as a page is sent at about the same time the destination accesses it. =20 -=3D=3D=3D Postcopy with hugepages =3D=3D=3D +Postcopy with hugepages +----------------------- =20 Postcopy now works with hugetlbfs backed memory: + a) The linux kernel on the destination must support userfault on hugepag= es. b) The huge-page configuration on the source and destination VMs must be identical; i.e. RAMBlocks on both sides must use the same page size. - c) Note that -mem-path /dev/hugepages will fall back to allocating norm= al + c) Note that ``-mem-path /dev/hugepages`` will fall back to allocating = normal RAM if it doesn't have enough hugepages, triggering (b) to fail. - Using -mem-prealloc enforces the allocation using hugepages. + Using ``-mem-prealloc`` enforces the allocation using hugepages. d) Care should be taken with the size of hugepage used; postcopy with 2MB hugepages works well, however 1GB hugepages are likely to be problema= tic since it takes ~1 second to transfer a 1GB hugepage across a 10Gbps l= ink, --=20 2.14.3