Postcopy implementation
v6 cover Postcopy implementation
From: "Dr. David Alan Gilbert" <[email protected]>
This is the 6th cut of my version of postcopy; it is designed for use with
the Linux kernel additions posted by Andrea Arcangeli here:
git clone --reference linux -b userfault18git://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git
(Note this is a different API from the last version)
This qemu series can be found at:
https://github.com/orbitfp7/qemu.git
on the wp3-postcopy-v6 tag.
It addresses some but not yet all of the previous review comments; however there are a couple of large simplifications, so it seems worth posting to meet the new kernel API and to stop people reviewing deadcode.
Note: That the userfaultfd.h header is no longer included in this tree:
- if you're building with the appropriate kernel headers it should find it
- if you're building on a host that doesn't have the kernel headers installed in the right place then:
configure with: --extra-cflags="-D__NR_userfaultfd=323"
cp include/uapi/linux/userfaultfd.h into somewhere in the include path, e.g. /usr/local/include/linux
v6
Removed the PMI bitmaps
- Andrea updated the kernel API so that userspace doesn't need to do wakeups, and thus QEMU doesn't need to keep track of which pages it's received; there is a price - which is we end up sending more dupes to the source, but it simplifies stuff a lot and makes the normal paths a lot quicker.
(10s of line change in kernel, 10%-ish simplification in this code!)
Changed discard message format to a simpler start/end address scheme
and rework discard and chunking code to work in long's to match bitmap
'qemu_get_buffer_less_copy' for postcopy pages
- avoids a userspace copy since the kernel now does it
- the new qemufile interface might also be useful for other places that don't need a copy (maybe xbzrle?)
Changed the blockingness of the incoming fd
it was incorrectly blocking during the precopy phase after a postcopy was enabled, causing the HMP to be unavailable. It's now blocking only once the postcopy thread starts up, since it's not a coroutine it can't deal with the yields in qemu_file.
An error on the return-path now marks the migration as failed
Fixups from Dave Gibson's comments
Removed can_postcopy, renamed save_complete to save_complete_precopy
added save_complete_postcopy
Simplified loadvm loop exits
discard message format changes above
and many more smaller changes.
small fixups for RCU
This work has been partially funded by the EU Orbit project:
see http://www.orbitproject.eu/about/
TODO:
The major work is to rework the page send/receive loops so that supporting larger host pages doesn't make it quite as messy.
Dr. David Alan Gilbert (47):
Start documenting how postcopy works.
Split header writing out of qemu_savevm_state_begin
qemu_ram_foreach_block: pass up error value, and down the ramblock
name
Add qemu_get_counted_string to read a string prefixed by a count byte Create MigrationIncomingState
Provide runtime Target page information
Move copy out of qemu_peek_buffer
Add qemu_get_buffer_less_copy to avoid copies some of the time Add wrapper for setting blocking status on a QEMUFile Rename save_live_complete to save_live_complete_precopy Return path: Open a return path on QEMUFile for sockets Return path: socket_writev_buffer: Block even on non-blocking fd's Migration commands
Return path: Control commands
Return path: Send responses from destination to source Return path: Source handling of return path
ram_debug_dump_bitmap: Dump a migration bitmap as text Move loadvm_handlers into MigrationIncomingState
Rework loadvm path for subloops
Add migration-capability boolean for postcopy-ram.
Add wrappers and handlers for sending/receiving the postcopy-ram
migration messages.
MIG_CMD_PACKAGED: Send a packaged chunk of migration stream
migrate_init: Call from savevm
Modify save_live_pending for postcopy
postcopy: OS support test
migrate_start_postcopy: Command to trigger transition to postcopy MIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state Add qemu_savevm_state_complete_postcopy
Postcopy: Maintain sentmap and calculate discard
postcopy: Incoming initialisation
postcopy: ram_enable_notify to switch on userfault
Postcopy: Postcopy startup in migration thread
Postcopy end in migration_thread
Page request: Add MIG_RP_MSG_REQ_PAGES reverse command Page request: Process incoming page request
Page request: Consume pages off the post-copy queue postcopy_ram.c: place_page and helpers
Postcopy: Use helpers to map pages during migration qemu_ram_block_from_host
Don't sync dirty bitmaps in postcopy
Host page!=target page: Cleanup bitmaps
Postcopy; Handle userfault requests
Start up a postcopy/listener thread ready for incoming page data
postcopy: Wire up loadvm_postcopy_handle_ commands
End of migration for postcopy
Disable mlock around incoming postcopy
Inhibit ballooning during postcopy
arch_init.c | 868 ++++++++++++++++++++++++++++++++++++--- balloon.c | 11 + docs/migration.txt | 167 ++++++++ exec.c | 74 +++- hmp-commands.hx | 15 + hmp.c | 7 + hmp.h | 1 + hw/ppc/spapr.c | 2 +- hw/virtio/virtio-balloon.c | 4 +- include/exec/cpu-all.h | 2 - include/exec/cpu-common.h | 7 +- include/migration/migration.h | 126 +++++- include/migration/postcopy-ram.h | 88 ++++ include/migration/qemu-file.h | 15 +- include/migration/vmstate.h | 10 +- include/qemu/typedefs.h | 5 + include/sysemu/balloon.h | 2 + include/sysemu/sysemu.h | 45 +- migration/Makefile.objs | 2 +- migration/block.c | 9 +- migration/migration.c | 743 +++++++++++++++++++++++++++++++-- migration/postcopy-ram.c | 715 ++++++++++++++++++++++++++++++++ migration/qemu-file-unix.c | 106 ++++- migration/qemu-file.c | 100 ++++- migration/rdma.c | 4 +- migration/vmstate.c | 5 +- qapi-schema.json | 19 +- qmp-commands.hx | 19 + savevm.c | 809 ++++++++++++++++++++++++++++++++---- trace-events | 77 +++- 30 files changed, 3832 insertions(+), 225 deletions(-) create mode 100644 include/migration/postcopy-ram.h create mode 100644 migration/postcopy-ram.c
Signature · 1 line
2.1.0
PATCH v6 1/47 Start documenting how postcopy works.
From: "Dr. David Alan Gilbert" <[email protected]>
--- docs/migration.txt | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+)
docs/migration.txt +167 -0
@@ -294,3 +294,170 @@ 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 not enabled, the values on that fields are garbage and don't need to be sent. + += Return path = + +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 (although +possibly with another fd or similar for some fast way of throwing pages across). + +However, some uses need two way communication; in particular the Postcopy destination +needs to be able to request pages on demand from the source. + +For these scenarios there is a 'return path' from the destination to the source; +qemu_file_get_return_path(QEMUFile* fwdpath) gives the QEMUFile* for the return +path. + + Source side + Forward path - written by migration thread + Return path - opened by main thread, read by return-path thread + + Destination side + Forward path - read by main thread + Return path - opened by main thread, written by main thread AND postcopy + thread (protected by rp_mutex) + += Postcopy = +'Postcopy' migration is a way to deal with migrations that refuse to converge; +its plus side is that there is an upper bound on the amount of migration traffic +and time it takes, the down side is that during the postcopy phase, a failure of +*either* side or the network connection causes the guest to be lost. + +In postcopy the destination CPUs are started before all the memory has been +transferred, and accesses to pages that are yet to be transferred cause +a fault that's translated by QEMU into a request to the source QEMU. + +Postcopy can be combined with precopy (i.e. normal migration) so that if precopy +doesn't finish in a given time the switch is made to postcopy. + +=== Enabling postcopy === + +To enable postcopy (prior to the start of migration): + +migrate_set_capability x-postcopy-ram on + +The migration will still start in precopy mode, however issuing: + +migrate_start_postcopy + +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. + +=== Postcopy device transfer === + +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 +the migration stream has to be able to respond with page data *during* the +device load, and hence the device data has to be read from the stream completely +before the device load begins to free the stream up. This is achieved by +'packaging' the device data into a blob that's read in one go. + +Source behaviour + +Until postcopy is entered the migration stream is identical to normal +precopy, except for the addition of a 'postcopy advise' command at +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: + + 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' + +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. + +Destination behaviour + +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. + +------------------------------------------------------------------------------ + 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 -- page -- + + 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 (into a QEMUSizedBuffer), 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) 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 behaviour +and is no longer used by migration, while the listen thread carries +on servicing page data until the end of migration. + +=== Postcopy states === + +Postcopy moves through a series of states (see postcopy_state) from +ADVISE->LISTEN->RUNNING->END + + 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. + (Triggered by reception of POSTCOPY_ADVISE command) + + 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 === + +The source side keeps two bitmaps during postcopy; 'the migration bitmap' +and 'sent map'. The 'migration bitmap' is basically the same as in +the precopy case, and holds a bit to indicate that page is 'dirty' - +i.e. needs sending. During the precopy phase this is updated as the CPU +dirties pages, however during postcopy the CPUs are stopped and nothing +should dirty anything any more. + +The 'sent map' is used for the transition to postcopy. It is a bitmap that +has a bit set whenever a page is sent to the destination, however during +the transition to postcopy mode it is masked against the migration bitmap +(sentmap &= migrationbitmap) to generate a bitmap recording pages that +have been previously been sent but are now dirty again. This masked +sentmap is sent to the destination which discards those now dirty pages +before starting the CPUs. + +Note that the contents of the sentmap are sacrificed during the calculation +of the discard set and thus aren't valid once in postcopy. The dirtymap +is still valid and is used to ensure that no page is sent more than once. Any +request for a page that has already been sent is ignored. Duplicate requests +such as this can happen as a page is sent at about the same time the +destination accesses it.
Signature · 1 line
2.1.0
PATCH v6 2/47 Split header writing out of qemu_savevm_state_begin
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Split qemu_savevm_state_begin to:
qemu_savevm_state_header That writes the initial file header.
qemu_savevm_state_begin That sets up devices and does the first
device pass.
Used later in postcopy.
--- include/sysemu/sysemu.h | 1 + migration/migration.c | 1 + savevm.c | 11 ++++++++--- trace-events | 1 + 4 files changed, 11 insertions(+), 3 deletions(-)
include/sysemu/sysemu.h +1 -0
@@ -84,6 +84,7 @@ void qemu_announce_self(void);bool qemu_savevm_state_blocked (Error **errp);void qemu_savevm_state_begin (QEMUFile *f,const MigrationParams *params); +void qemu_savevm_state_header (QEMUFile *f);int qemu_savevm_state_iterate (QEMUFile *f);void qemu_savevm_state_complete (QEMUFile *f);void qemu_savevm_state_cancel (void );
migration/migration.c +1 -0
@@ -617,6 +617,7 @@ static void *migration_thread(void *opaque)int64_t start_time = initial_time;bool old_vm_running = false; +qemu_savevm_state_header (s->file);qemu_savevm_state_begin (s->file, &s->params); s->setup_time =qemu_clock_get_ms (QEMU_CLOCK_HOST ) - setup_start;
savevm.c +8 -3
@@ -616,6 +616,13 @@ bool qemu_savevm_state_blocked(Error **errp)return false; } +void qemu_savevm_state_header (QEMUFile *f) +{ +trace_savevm_state_header (); +qemu_put_be32 (f,QEMU_VM_FILE_MAGIC ); +qemu_put_be32 (f,QEMU_VM_FILE_VERSION ); +} +void qemu_savevm_state_begin (QEMUFile *f,const MigrationParams *params) {
@@ -630,9 +637,6 @@ void qemu_savevm_state_begin(QEMUFile *f, se->ops->set_params (params, se->opaque); } -qemu_put_be32 (f,QEMU_VM_FILE_MAGIC ); -qemu_put_be32 (f,QEMU_VM_FILE_VERSION ); - QTAILQ_FOREACH(se, &savevm_handlers, entry) {int len;
@@ -842,6 +846,7 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp) }qemu_mutex_unlock_iothread (); +qemu_savevm_state_header (f);qemu_savevm_state_begin (f, ¶ms);qemu_mutex_lock_iothread ();
trace-events +1 -0
@@ -1174,6 +1174,7 @@ qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint savevm_section_start(const char *id, unsigned int section_id) "%s, section_id %u" savevm_section_end(const char *id, unsigned int section_id, int ret) "%s, section_id %u -> %d" savevm_state_begin(void) "" +savevm_state_header(void) "" savevm_state_iterate(void) "" savevm_state_complete(void) "" savevm_state_cancel(void) ""
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 02/47] Split header writing out of qemu_savevm_state_begin
Quoted 10 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Split
qemu_savevm_state_beginto:
qemu_savevm_state_headerThat writes the initial file header.
qemu_savevm_state_beginThat sets up devices and does the first
device pass.Used later in postcopy.
Signed-off-by Dr. David Alan Gilbert <[email protected]>
Function names sound silly now - savevm_state_header() and savevm_state_begin().
Amit
PATCH v6 3/47 qemu_ram_foreach_block: pass up error value, and down the ramblock name
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
check the return value of the function it calls and error if it's non-0 Fixup qemu_rdma_init_one_block that is the only current caller,
and rdma_add_block the only function it calls using it.
Pass the name of the ramblock to the function; helps in debugging.
--- exec.c | 10 ++++++++-- include/exec/cpu-common.h | 4 ++-- migration/rdma.c | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-)
exec.c +8 -2
@@ -3067,14 +3067,20 @@ bool cpu_physical_memory_is_io(hwaddr phys_addr) memory_region_is_romd(mr)); } -void qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque) +int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque) {RAMBlock *block; +int ret =0 ;rcu_read_lock (); QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { -func (block->host, block->offset, block->used_length, opaque); + ret =func (block->idstr, block->host, block->offset, + block->used_length, opaque); +if (ret) { +break ; + } }rcu_read_unlock (); +return ret; }#endif
include/exec/cpu-common.h +2 -2
@@ -125,10 +125,10 @@ void cpu_flush_icache_range(hwaddr start, int len);extern struct MemoryRegion io_mem_rom;extern struct MemoryRegion io_mem_notdirty; -typedef void (RAMBlockIterFunc )(void *host_addr, +typedef int (RAMBlockIterFunc )(const char *block_name,void *host_addr,ram_addr_t offset,ram_addr_t length,void *opaque); -void qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque); +int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque);#endif
migration/rdma.c +2 -2
@@ -570,10 +570,10 @@ static int rdma_add_block(RDMAContext *rdma, void *host_addr,* in advanced before the migration starts. This tells us where the RAM blocks * are so that we can register them individually. */ -static void qemu_rdma_init_one_block (void *host_addr, +static int qemu_rdma_init_one_block (const char *block_name,void *host_addr,ram_addr_t block_offset,ram_addr_t length,void *opaque) { -rdma_add_block (opaque, host_addr, block_offset, length); +return rdma_add_block (opaque, host_addr, block_offset, length); }/*
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 03/47] qemu_ram_foreach_block: pass up error value, and down the ramblock name
Quoted 7 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
check the return value of the function it calls and error if it's non-0 Fixup
qemu_rdma_init_one_blockthat is the only current caller,
andrdma_add_blockthe only function it calls using it.Pass the name of the ramblock to the function; helps in debugging.
Amit
PATCH v6 4/47 Add qemu_get_counted_string to read a string prefixed by a count byte
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
and use it in loadvm_state and ram_load.
--- arch_init.c | 5 +---- include/migration/qemu-file.h | 3 +++ migration/qemu-file.c | 16 ++++++++++++++++ savevm.c | 11 ++++++----- 4 files changed, 26 insertions(+), 9 deletions(-)
arch_init.c +1 -4
@@ -1145,13 +1145,10 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) total_ram_bytes = addr;while (!ret && total_ram_bytes) {RAMBlock *block; -uint8_t len;char id[256 ];ram_addr_t length; - len =qemu_get_byte (f); -qemu_get_buffer (f, (uint8_t *)id, len); - id[len] =0 ; +qemu_get_counted_string (f, id); length =qemu_get_be64 (f); QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
include/migration/qemu-file.h +3 -0
@@ -309,4 +309,7 @@ static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv) {qemu_get_be64s (f, (uint64_t *)pv); } + +int qemu_get_counted_string (QEMUFile *f,char buf[256 ]); +#endif
migration/qemu-file.c +16 -0
@@ -546,3 +546,19 @@ uint64_t qemu_get_be64(QEMUFile *f) v |=qemu_get_be32 (f);return v; } + +/* +* Get a string whose length is determined by a single preceding byte +* A preallocated 256 byte buffer must be passed in. +* Returns: 0 on success and a 0 terminated string in the buffer +*/ +int qemu_get_counted_string (QEMUFile *f,char buf[256 ]) +{ +unsigned int len =qemu_get_byte (f); +int res =qemu_get_buffer (f, (uint8_t *)buf, len); + + buf[len] =0 ; + +return res != len; +} +
savevm.c +6 -5
@@ -969,8 +969,7 @@ int qemu_loadvm_state(QEMUFile *f)while ((section_type =qemu_get_byte (f)) !=QEMU_VM_EOF ) {uint32_t instance_id, version_id, section_id;SaveStateEntry *se; -char idstr[257 ]; -int len; +char idstr[256 ];trace_qemu_loadvm_state_section (section_type);switch (section_type) {
@@ -978,9 +977,11 @@ int qemu_loadvm_state(QEMUFile *f)case QEMU_VM_SECTION_FULL :/* Read section start */ section_id =qemu_get_be32 (f); - len =qemu_get_byte (f); -qemu_get_buffer (f, (uint8_t *)idstr, len); - idstr[len] =0 ; +if (qemu_get_counted_string (f, idstr)) { +error_report ("Unable to read ID string for section %u" , + section_id); +return -EINVAL ; + } instance_id =qemu_get_be32 (f); version_id =qemu_get_be32 (f);
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 04/47] Add qemu_get_counted_string to read a string prefixed by a count byte
Quoted 3 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
and use it in
loadvm_stateandram_load.
This patch is doing several things at once:
- reducing size of a buffer from 257 to 256 (it's safe, but not mentioned in the commit log)
- adding an error return to one calling site (again not mentioned here)
Quoted 5 lines
arch_init.c +0
-1@@ -1145,13 +1145,10 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) total_ram_bytes = addr;while (!ret && total_ram_bytes) {RAMBlock *block; -uint8_t len;
arch_init.c static int ram_load(QEMUFile *f, void *opaque, int version_id) · line 1154
Quoted 10 lines
arch_init.c +1
-3char id[256 ];ram_addr_t length; - len =qemu_get_byte (f); -qemu_get_buffer (f, (uint8_t *)id, len); - id[len] =0 ; +qemu_get_counted_string (f, id); length =qemu_get_be64 (f); QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
- ... while not doing that for the other calling site. In fact we really should check return value there too, isn't it? buf[len] is set to 0, not buf[0] in case of an error, and ram_load could happily start using string functions on the bogus data in id[].
Can you please split the patches up, or write a verbose commit message?
Also, I think you should just post these preparatory patches in a separate series so they can be applied as they're good on their own.
Postcopy patches themselves can come as another series, and that also makes reviewing easier.
Also:
Quoted 4 lines
migration/qemu-file.c +4
-0+ +/* +* Get a string whose length is determined by a single preceding byte +* A preallocated 256 byte buffer must be passed in.
migration/qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 562
Quoted 10 lines
migration/qemu-file.c +10
-0+* Returns: 0 on success and a 0 terminated string in the buffer +*/ +int qemu_get_counted_string (QEMUFile *f,char buf[256 ]) +{ +unsigned int len =qemu_get_byte (f); +int res =qemu_get_buffer (f, (uint8_t *)buf, len); + + buf[len] =0 ; + +return res != len;
since you're returning bool, how about making this bool? Though I'd like it if this was
return res == len ? res : 0;
BTW I'd like it if everything (return value, res, len) were all unsigned. The operations are safe, but it sucks we use signed values for counting things all over the place.
Thanks,
Amit
Re: [Qemu-devel] [PATCH v6 04/47] Add qemu_get_counted_string to read a string prefixed by a count byte
Quoted 34 lines · * Amit Shah ([email protected]) wrote:
Quoted 3 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
and use it in
loadvm_stateandram_load.This patch is doing several things at once:
- reducing size of a buffer from 257 to 256 (it's safe, but not mentioned in the commit log)
- adding an error return to one calling site (again not mentioned here)
Quoted 5 lines
arch_init.c +0
-1@@ -1145,13 +1145,10 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) total_ram_bytes = addr;while (!ret && total_ram_bytes) {RAMBlock *block; -uint8_t len;arch_init.c static int ram_load(QEMUFile *f, void *opaque, int version_id) · line 1154
Quoted 10 lines
arch_init.c +1
-3char id[256 ];ram_addr_t length; - len =qemu_get_byte (f); -qemu_get_buffer (f, (uint8_t *)id, len); - id[len] =0 ; +qemu_get_counted_string (f, id); length =qemu_get_be64 (f); QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {- ... while not doing that for the other calling site. In fact we really should check return value there too, isn't it? buf[len] is set to 0, not buf[0] in case of an error, and
ram_loadcould happily start using string functions on the bogus data in id[].
Quoted 5 lines
Can you please split the patches up, or write a verbose commit message?
Also, I think you should just post these preparatory patches in a separate series so they can be applied as they're good on their own.
Yep; OK, I can split them out on an individual basis.
Quoted 23 lines
Postcopy patches themselves can come as another series, and that also makes reviewing easier.
Also:
Quoted 4 lines
migration/qemu-file.c +4
-0+ +/* +* Get a string whose length is determined by a single preceding byte +* A preallocated 256 byte buffer must be passed in. migration/qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 562
Quoted 10 lines
migration/qemu-file.c +10
-0+* Returns: 0 on success and a 0 terminated string in the buffer +*/ +int qemu_get_counted_string (QEMUFile *f,char buf[256 ]) +{ +unsigned int len =qemu_get_byte (f); +int res =qemu_get_buffer (f, (uint8_t *)buf, len); + + buf[len] =0 ; + +return res != len;since you're returning bool, how about making this bool? Though I'd like it if this was
Quoted 5 lines
return res == len ? res : 0;
BTW I'd like it if everything (return value, res, len) were all unsigned. The operations are safe, but it sucks we use signed values for counting things all over the place.
Yes, I can do that (I intend some day to fix qemu_get_buffer and co to use size_t).
Thanks,
Dave
Quoted 3 lines
Thanks,
Amit
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v6 5/47 Create MigrationIncomingState
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
There are currently lots of pieces of incoming migration state scattered around, and postcopy is adding more, and it seems better to try and keep it together.
allocate MIS in process_incoming_migration_co
--- include/migration/migration.h | 9 +++++++++ include/qemu/typedefs.h | 1 + migration/migration.c | 28 ++++++++++++++++++++++++++++ savevm.c | 2 ++ 4 files changed, 40 insertions(+)
include/migration/migration.h +9 -0
@@ -42,6 +42,15 @@ struct MigrationParams {typedef struct MigrationState MigrationState ; +/* State for the incoming migration */ +struct MigrationIncomingState { +QEMUFile *file; +}; + +MigrationIncomingState *migration_incoming_get_current (void ); +MigrationIncomingState *migration_incoming_state_new (QEMUFile *f); +void migration_incoming_state_destroy (void ); +struct MigrationState {int64_t bandwidth_limit;
include/qemu/typedefs.h +1 -0
@@ -38,6 +38,7 @@ typedef struct MemoryListener MemoryListener;typedef struct MemoryMappingList MemoryMappingList ;typedef struct MemoryRegion MemoryRegion ;typedef struct MemoryRegionSection MemoryRegionSection ; +typedef struct MigrationIncomingState MigrationIncomingState ;typedef struct MigrationParams MigrationParams ;typedef struct Monitor Monitor ;typedef struct MouseTransformInfo MouseTransformInfo ;
migration/migration.c +28 -0
@@ -45,6 +45,7 @@ static bool deferred_incoming;migrations at once. For now we don't need to add dynamic creation of migration */ +/* For outgoing */ MigrationState *migrate_get_current (void ) {static MigrationState current_migration = {
@@ -57,6 +58,28 @@ MigrationState *migrate_get_current(void)return ¤t_migration; } +/* For incoming */ +static MigrationIncomingState *mis_current; + +MigrationIncomingState *migration_incoming_get_current (void ) +{ +return mis_current; +} + +MigrationIncomingState *migration_incoming_state_new (QEMUFile * f) +{ + mis_current =g_malloc0 (sizeof (MigrationIncomingState)); + mis_current->file = f; + +return mis_current; +} + +void migration_incoming_state_destroy (void ) +{ +g_free (mis_current); + mis_current =NULL ; +} +/* * Called on -incoming with a defer: uri. * The migration can be started later after any parameters have been
@@ -101,9 +124,14 @@ static void process_incoming_migration_co(void *opaque)Error *local_err =NULL ;int ret; +migration_incoming_state_new (f); + ret =qemu_loadvm_state (f); +qemu_fclose (f);free_xbzrle_decoded_buf (); +migration_incoming_state_destroy (); +if (ret <0 ) {error_report ("load of migration failed: %s" ,strerror (-ret));exit (EXIT_FAILURE );
savevm.c +2 -0
@@ -1320,9 +1320,11 @@ int load_vmstate(const char *name) }qemu_system_reset (VMRESET_SILENT ); +migration_incoming_state_new (f); ret =qemu_loadvm_state (f);qemu_fclose (f); +migration_incoming_state_destroy ();if (ret <0 ) {error_report ("Error %d while loading VM state" , ret);return ret;
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 05/47] Create MigrationIncomingState
Quoted 4 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
There are currently lots of pieces of incoming migration state scattered around,
next few patches don't move state into this new struct -- not sure if you're doing that here, but would be nice to have them ordered together (also in the separate series).
Quoted 6 lines
and postcopy is adding more, and it seems better to try and keep it together.
allocate MIS in
process_incoming_migration_coSigned-off-by Dr. David Alan Gilbert <[email protected]>
Amit
PATCH v6 6/47 Provide runtime Target page information
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The migration code generally is built target-independent, however there are a few places where knowing the target page size would avoid artificially moving stuff into arch_init.
Provide 'qemu_target_page_bits()' that returns TARGET_PAGE_BITS to other bits of code so that they can stay target-independent.
--- exec.c | 10 ++++++++++ include/sysemu/sysemu.h | 1 + 2 files changed, 11 insertions(+)
exec.c +10 -0
@@ -3038,6 +3038,16 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr, }return 0 ; } + +/* +* Allows code that needs to deal with migration bitmaps etc to still be built +* target independent. +*/ +size_t qemu_target_page_bits (void ) +{ +return TARGET_PAGE_BITS ; +} +#endif /*
include/sysemu/sysemu.h +1 -0
@@ -68,6 +68,7 @@ int qemu_reset_requested_get(void);void qemu_system_killed (int signal,pid_t pid);void qemu_devices_reset (void );void qemu_system_reset (bool report); +size_t qemu_target_page_bits (void );void qemu_add_exit_notifier (Notifier *notify);void qemu_remove_exit_notifier (Notifier *notify);
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 06/47] Provide runtime Target page information
Quoted 10 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
The migration code generally is built target-independent, however there are a few places where knowing the target page size would avoid artificially moving stuff into
arch_init.Provide '
qemu_target_page_bits()' that returnsTARGET_PAGE_BITSto other bits of code so that they can stay target-independent.Signed-off-by Dr. David Alan Gilbert <[email protected]>
Amit
PATCH v6 7/47 Move copy out of qemu_peek_buffer
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
qemu_peek_buffer currently copies the data it reads into a buffer, however the next patch wants access to the buffer without the copy, hence rework to remove the copy to the layer above.
--- include/migration/qemu-file.h | 2 +- migration/qemu-file.c | 12 +++++++----- migration/vmstate.c | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-)
include/migration/qemu-file.h +1 -1
@@ -157,7 +157,7 @@ static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)void qemu_put_be16 (QEMUFile *f,unsigned int v);void qemu_put_be32 (QEMUFile *f,unsigned int v);void qemu_put_be64 (QEMUFile *f,uint64_t v); -int qemu_peek_buffer (QEMUFile *f,uint8_t *buf,int size,size_t offset); +int qemu_peek_buffer (QEMUFile *f,uint8_t **buf,int size,size_t offset);int qemu_get_buffer (QEMUFile *f,uint8_t *buf,int size);/* * Note that you can only peek continuous bytes from where the current pointer
migration/qemu-file.c +7 -5
@@ -348,14 +348,14 @@ void qemu_file_skip(QEMUFile *f, int size) }/* -* Read 'size' bytes from file (at 'offset') into buf without moving the -* pointer. +* Read 'size' bytes from file (at 'offset') without moving the +* pointer and set 'buf' to point to that data. * * It will return size bytes unless there was an error, in which case it will * return as many as it managed to read (assuming blocking fd's which * all current QEMUFile are) */ -int qemu_peek_buffer (QEMUFile *f,uint8_t *buf,int size,size_t offset) +int qemu_peek_buffer (QEMUFile *f,uint8_t **buf,int size,size_t offset) {int pending;int index;
@@ -391,7 +391,7 @@ int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset) size = pending; } -memcpy (buf, f->buf + index, size); + *buf = f->buf + index;return size; }
@@ -410,11 +410,13 @@ int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)while (pending >0 ) {int res; +uint8_t *src; - res =qemu_peek_buffer (f, buf,MIN (pending,IO_BUF_SIZE ),0 ); + res =qemu_peek_buffer (f, &src,MIN (pending,IO_BUF_SIZE ),0 );if (res ==0 ) {return done; } +memcpy (buf, src, res);qemu_file_skip (f, res); buf += res; pending -= res;
migration/vmstate.c +3 -2
@@ -358,7 +358,7 @@ static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,trace_vmstate_subsection_load (vmsd->name);while (qemu_peek_byte (f,0 ) ==QEMU_VM_SUBSECTION ) { -char idstr[256 ]; +char idstr[256 ], *idstr_ret;int ret;uint8_t version_id, len, size;const VMStateDescription *sub_vmsd;
@@ -369,11 +369,12 @@ static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,trace_vmstate_subsection_load_bad (vmsd->name,"(short)" );return 0 ; } - size =qemu_peek_buffer (f, (uint8_t *)idstr, len,2 ); + size =qemu_peek_buffer (f, (uint8_t **)&idstr_ret, len,2 );if (size != len) {trace_vmstate_subsection_load_bad (vmsd->name,"(peek fail)" );return 0 ; } +memcpy (idstr, idstr_ret, size); idstr[size] =0 ;if (strncmp (vmsd->name, idstr,strlen (vmsd->name)) !=0 ) {
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 07/47] Move copy out of qemu_peek_buffer
Quoted 7 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
qemu_peek_buffercurrently copies the data it reads into a buffer, however the next patch wants access to the buffer without the copy, hence rework to remove the copy to the layer above.Signed-off-by Dr. David Alan Gilbert <[email protected]>
It's silly to do a copy in a peek function, so this is a good thing.
Amit
PATCH v6 8/47 Add qemu_get_buffer_less_copy to avoid copies some of the time
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
qemu_get_buffer always copies the data it reads to a users buffer, however in many cases the file buffer inside qemu_file could be given back to the caller, avoiding the copy. This isn't always possible depending on the size and alignment of the data.
Thus 'qemu_get_buffer_less_copy' either copies the data to a supplied buffer or updates a pointer to the internal buffer if convenient.
--- include/migration/qemu-file.h | 2 ++ migration/qemu-file.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
include/migration/qemu-file.h +2 -0
@@ -159,6 +159,8 @@ void qemu_put_be32(QEMUFile *f, unsigned int v);void qemu_put_be64 (QEMUFile *f,uint64_t v);int qemu_peek_buffer (QEMUFile *f,uint8_t **buf,int size,size_t offset);int qemu_get_buffer (QEMUFile *f,uint8_t *buf,int size); +int qemu_get_buffer_less_copy (QEMUFile *f,uint8_t **buf,int size); +/* * Note that you can only peek continuous bytes from where the current pointer * is; you aren't guaranteed to be able to peak to +n bytes unless you've
migration/qemu-file.c +45 -0
51 lines not on this page · raw message
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 08/47] Add qemu_get_buffer_less_copy to avoid copies some of the time
Quoted 37 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
qemu_get_bufferalways copies the data it reads to a users buffer, however in many cases the file buffer insideqemu_filecould be given back to the caller, avoiding the copy. This isn't always possible depending on the size and alignment of the data.Thus '
qemu_get_buffer_less_copy' either copies the data to a supplied buffer or updates a pointer to the internal buffer if convenient.Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/qemu-file.h | 2 ++ migration/qemu-file.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)include/migration/qemu-file.h +2
-0@@ -159,6 +159,8 @@ void qemu_put_be32(QEMUFile *f, unsigned int v);void qemu_put_be64 (QEMUFile *f,uint64_t v);int qemu_peek_buffer (QEMUFile *f,uint8_t **buf,int size,size_t offset);int qemu_get_buffer (QEMUFile *f,uint8_t *buf,int size); +int qemu_get_buffer_less_copy (QEMUFile *f,uint8_t **buf,int size); +/* * Note that you can only peek continuous bytes from where the current pointer * is; you aren't guaranteed to be able to peak to +n bytes unless you've migration/qemu-file.c +0
-0@@ -426,6 +426,51 @@ int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) }/*
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 438
Quoted 10 lines
migration/qemu-file.c +10
-0+* Read 'size' bytes of data from the file. +* 'size' can be larger than the internal buffer. +* +* The data: +* may be held on an internal buffer (in which case *buf is updated +* to point to it) that is valid until the next qemu_file operation. +* OR +* will be copied to the *buf that was passed in. +* +* The code tries to avoid the copy if possible.
So is it expected that callers will store the originally-allocated start location, so that g_free can be called on the correct location in either case? If that's a requirement, this text needs to be updated.
If not (alternative idea below), text needs to be updated as well.
Quoted 13 lines
migration/qemu-file.c +13
-0+* It will return size bytes unless there was an error, in which case it will +* return as many as it managed to read (assuming blocking fd's which +* all current QEMUFile are) +*/ +int qemu_get_buffer_less_copy (QEMUFile *f,uint8_t **buf,int size) +{ +int pending = size; +int done =0 ; +bool first = true; + +while (pending >0 ) { +int res; +uint8_t *src;
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 462
Quoted 10 lines
migration/qemu-file.c +10
-0+ + res =qemu_peek_buffer (f, &src,MIN (pending,IO_BUF_SIZE ),0 ); +if (res ==0 ) { +return done; + } +qemu_file_skip (f, res); + done += res; + pending -= res; +if (first && res == size) { + *buf = src;
So we've got to assume that buf was allocated by the calling function, and since we're modifying the pointer (alternative idea to one above), should we unallocate it here? Can lead to a bad g_free later, or a memleak.
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 462
Quoted 1 line
migration/qemu-file.c +1
-0+return done;
How about just 'break' instead of return?
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 467
Quoted 4 lines
migration/qemu-file.c +4
-0+ }else { + first = false; +memcpy (buf, src, res); + buf += res;
In either case (break or return), the 'else' can be dropped..
Amit
Re: [Qemu-devel] [PATCH v6 08/47] Add qemu_get_buffer_less_copy to avoid copies some of the time
Quoted 54 lines · * Amit Shah ([email protected]) wrote:
Quoted 37 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
qemu_get_bufferalways copies the data it reads to a users buffer, however in many cases the file buffer insideqemu_filecould be given back to the caller, avoiding the copy. This isn't always possible depending on the size and alignment of the data.Thus '
qemu_get_buffer_less_copy' either copies the data to a supplied buffer or updates a pointer to the internal buffer if convenient.Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/qemu-file.h | 2 ++ migration/qemu-file.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)include/migration/qemu-file.h +2
-0@@ -159,6 +159,8 @@ void qemu_put_be32(QEMUFile *f, unsigned int v);void qemu_put_be64 (QEMUFile *f,uint64_t v);int qemu_peek_buffer (QEMUFile *f,uint8_t **buf,int size,size_t offset);int qemu_get_buffer (QEMUFile *f,uint8_t *buf,int size); +int qemu_get_buffer_less_copy (QEMUFile *f,uint8_t **buf,int size); +/* * Note that you can only peek continuous bytes from where the current pointer * is; you aren't guaranteed to be able to peak to +n bytes unless you've migration/qemu-file.c +0
-0@@ -426,6 +426,51 @@ int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) }/* migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 438
Quoted 10 lines
migration/qemu-file.c +10
-0+* Read 'size' bytes of data from the file. +* 'size' can be larger than the internal buffer. +* +* The data: +* may be held on an internal buffer (in which case *buf is updated +* to point to it) that is valid until the next qemu_file operation. +* OR +* will be copied to the *buf that was passed in. +* +* The code tries to avoid the copy if possible. So is it expected that callers will store the originally-allocated start location, so that
g_freecan be called on the correct location in either case? If that's a requirement, this text needs to be updated.
Quoted 1 line
If not (alternative idea below), text needs to be updated as well.
see reply below.
Quoted 21 lines
Quoted 21 lines
migration/qemu-file.c +20
-0+* It will return size bytes unless there was an error, in which case it will +* return as many as it managed to read (assuming blocking fd's which +* all current QEMUFile are) +*/ +int qemu_get_buffer_less_copy (QEMUFile *f,uint8_t **buf,int size) +{ +int pending = size; +int done =0 ; +bool first = true; + +while (pending >0 ) { +int res; +uint8_t *src; + + res =qemu_peek_buffer (f, &src,MIN (pending,IO_BUF_SIZE ),0 ); +if (res ==0 ) { +return done; + } +qemu_file_skip (f, res); + done += res;
Quoted 8 lines
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 462
Quoted 3 lines
migration/qemu-file.c +3
-0+ pending -= res; +if (first && res == size) { + *buf = src;So we've got to assume that buf was allocated by the calling function, and since we're modifying the pointer (alternative idea to one above), should we unallocate it here? Can lead to a bad
g_freelater, or a memleak.
My use tends to involve a buffer allocated once:
uint8_t *mybuffer = g_malloc(...)
while (aloop) {
uint8_t *ourdata = mybuffer;
if (qemu_get_buffer_less_copy(f, &ourdata, size)...) {
do something with *ourdata
}
}
g_free(mybuffer);The pointer that's passed into qemu_get_buffer_less_copy is only a copy of the allocation pointer, and thus you're not losing anything when it changes it.
I've added the following text, does this make it clearer?
* Note: Since **buf may get changed, the caller should take care to
* keep a pointer to the original buffer if it needs to deallocate it.
Quoted 3 lines
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 462
Quoted 1 line
migration/qemu-file.c +1
-0+return done;How about just 'break' instead of return?
Changed.
Quoted 7 lines
migration/qemu-file.c int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) · line 467
Quoted 5 lines
migration/qemu-file.c +4
-0+ }else { + first = false; +memcpy (buf, src, res); + buf += res;In either case (break or return), the 'else' can be dropped..
Changed.
Thanks,
Quoted 2 lines
Amit
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v6 08/47] Add qemu_get_buffer_less_copy to avoid copies some of the time
Quoted 21 lines · Dr. David Alan Gilbert
Quoted 4 lines · * Amit Shah ([email protected]) wrote:
So we've got to assume that buf was allocated by the calling function, and since we're modifying the pointer (alternative idea to one above), should we unallocate it here? Can lead to a bad
g_freelater, or a memleak.My use tends to involve a buffer allocated once:
uint8_t *mybuffer = g_malloc(...) while (aloop) { uint8_t *ourdata = mybuffer; if (qemu_get_buffer_less_copy(f, &ourdata, size)...) { do something with *ourdata } } g_free(mybuffer);
Quoted 8 lines
The pointer that's passed into
qemu_get_buffer_less_copyis only a copy of the allocation pointer, and thus you're not losing anything when it changes it.I've added the following text, does this make it clearer?
* Note: Since **buf may get changed, the caller should take care to
* keep a pointer to the original buffer if it needs to deallocate it.
Yes, thanks.
Amit
PATCH v6 9/47 Add wrapper for setting blocking status on a QEMUFile
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add a wrapper to change the blocking status on a QEMUFile rather than having to use qemu_set_block(qemu_get_fd(f)); it seems best to avoid exposing the fd since not all QEMUFile's really have one. With this wrapper we could move the implementation down to be different on different transports.
--- include/migration/qemu-file.h | 1 + migration/qemu-file.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+)
include/migration/qemu-file.h +1 -0
@@ -190,6 +190,7 @@ int qemu_file_get_error(QEMUFile *f);void qemu_file_set_error (QEMUFile *f,int ret);int qemu_file_shutdown (QEMUFile *f);void qemu_fflush (QEMUFile *f); +void qemu_file_change_blocking (QEMUFile *f,bool block);static inline void qemu_put_be64s (QEMUFile *f,const uint64_t *pv) {
migration/qemu-file.c +15 -0
@@ -609,3 +609,18 @@ int qemu_get_counted_string(QEMUFile *f, char buf[256])return res != len; } +/* +* Change the blocking state of the QEMUFile. +* Note: On some transports the OS only keeps a single blocking state for +* both directions, and thus changing the blocking on the main +* QEMUFile can also affect the return path. +*/ +void qemu_file_change_blocking (QEMUFile *f,bool block) +{ +if (block) { +qemu_set_block (qemu_get_fd (f)); + }else { +qemu_set_nonblock (qemu_get_fd (f)); + } +} +
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 09/47] Add wrapper for setting blocking status on a QEMUFile
Quoted 9 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Add a wrapper to change the blocking status on a QEMUFile rather than having to use qemu_set_block(qemu_get_fd(f)); it seems best to avoid exposing the fd since not all QEMUFile's really have one. With this wrapper we could move the implementation down to be different on different transports.
Signed-off-by Dr. David Alan Gilbert <[email protected]>
Amit
PATCH v6 10/47 Rename save_live_complete to save_live_complete_precopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
In postcopy we're going to need to perform the complete phase for postcopiable devices at a different point, start out by renaming all of the 'complete's to make the difference obvious.
--- arch_init.c | 2 +- hw/ppc/spapr.c | 2 +- include/migration/vmstate.h | 2 +- include/sysemu/sysemu.h | 2 +- migration/block.c | 2 +- migration/migration.c | 2 +- savevm.c | 10 +++++----- trace-events | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-)
arch_init.c +1 -1
@@ -1233,7 +1233,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)static SaveVMHandlers savevm_ram_handlers = { .save_live_setup = ram_save_setup, .save_live_iterate = ram_save_iterate, - .save_live_complete = ram_save_complete, + .save_live_complete_precopy = ram_save_complete, .save_live_pending = ram_save_pending, .load_state = ram_load, .cancel = ram_migration_cancel,
hw/ppc/spapr.c +1 -1
@@ -1373,7 +1373,7 @@ static int htab_load(QEMUFile *f, void *opaque, int version_id)static SaveVMHandlers savevm_htab_handlers = { .save_live_setup = htab_save_setup, .save_live_iterate = htab_save_iterate, - .save_live_complete = htab_save_complete, + .save_live_complete_precopy = htab_save_complete, .load_state = htab_load, };
include/migration/vmstate.h +1 -1
@@ -40,7 +40,7 @@ typedef struct SaveVMHandlers {SaveStateHandler *save_state;void (*cancel)(void *opaque); -int (*save_live_complete)(QEMUFile *f,void *opaque); +int (*save_live_complete_precopy)(QEMUFile *f,void *opaque);/* This runs both outside and inside the iothread lock. */ bool (*is_active)(void *opaque);
include/sysemu/sysemu.h +1 -1
@@ -87,7 +87,7 @@ void qemu_savevm_state_begin(QEMUFile *f, const MigrationParams *params);void qemu_savevm_state_header (QEMUFile *f);int qemu_savevm_state_iterate (QEMUFile *f); -void qemu_savevm_state_complete (QEMUFile *f); +void qemu_savevm_state_complete_precopy (QEMUFile *f);void qemu_savevm_state_cancel (void );uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size);int qemu_loadvm_state (QEMUFile *f);
migration/block.c +1 -1
@@ -878,7 +878,7 @@ static SaveVMHandlers savevm_block_handlers = { .set_params = block_set_params, .save_live_setup = block_save_setup, .save_live_iterate = block_save_iterate, - .save_live_complete = block_save_complete, + .save_live_complete_precopy = block_save_complete, .save_live_pending = block_save_pending, .load_state = block_load, .cancel = block_migration_cancel,
migration/migration.c +1 -1
@@ -671,7 +671,7 @@ static void *migration_thread(void *opaque) ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE );if (ret >=0 ) {qemu_file_set_rate_limit (s->file,INT64_MAX ); -qemu_savevm_state_complete (s->file); +qemu_savevm_state_complete_precopy (s->file); }qemu_mutex_unlock_iothread ();
savevm.c +5 -5
@@ -720,19 +720,19 @@ static bool should_send_vmdesc(void)return !machine->suppress_vmdesc; } -void qemu_savevm_state_complete (QEMUFile *f) +void qemu_savevm_state_complete_precopy (QEMUFile *f) {QJSON *vmdesc;int vmdesc_len;SaveStateEntry *se;int ret; -trace_savevm_state_complete (); +trace_savevm_state_complete_precopy ();cpu_synchronize_all_states (); QTAILQ_FOREACH(se, &savevm_handlers, entry) { -if (!se->ops || !se->ops->save_live_complete) { +if (!se->ops || !se->ops->save_live_complete_precopy) {continue ; }if (se->ops && se->ops->is_active) {
@@ -745,7 +745,7 @@ void qemu_savevm_state_complete(QEMUFile *f)qemu_put_byte (f,QEMU_VM_SECTION_END );qemu_put_be32 (f, se->section_id); - ret = se->ops->save_live_complete (f, se->opaque); + ret = se->ops->save_live_complete_precopy (f, se->opaque);trace_savevm_section_end (se->idstr, se->section_id, ret);if (ret <0 ) {qemu_file_set_error (f, ret);
@@ -858,7 +858,7 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp) ret =qemu_file_get_error (f);if (ret ==0 ) { -qemu_savevm_state_complete (f); +qemu_savevm_state_complete_precopy (f); ret =qemu_file_get_error (f); }if (ret !=0 ) {
trace-events +1 -1
@@ -1176,7 +1176,7 @@ savevm_section_end(const char *id, unsigned int section_id, int ret) "%s, sectio savevm_state_begin(void) "" savevm_state_header(void) "" savevm_state_iterate(void) "" -savevm_state_complete(void) "" +savevm_state_complete_precopy(void) "" savevm_state_cancel(void) "" vmstate_save(const char *idstr, const char *vmsd_name) "%s, %s" vmstate_load(const char *idstr, const char *vmsd_name) "%s, %s"
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 10/47] Rename save_live_complete to save_live_complete_precopy
Quoted 7 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
In postcopy we're going to need to perform the complete phase for postcopiable devices at a different point, start out by renaming all of the 'complete's to make the difference obvious.
Signed-off-by Dr. David Alan Gilbert <[email protected]>
Amit
PATCH v6 11/47 Return path: Open a return path on QEMUFile for sockets
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs a method to send messages from the destination back to the source, this is the 'return path'.
Wire it up for 'socket' QEMUFile's using a dup'd fd.
--- include/migration/qemu-file.h | 7 +++++ migration/qemu-file-unix.c | 65 +++++++++++++++++++++++++++++++++++++------ migration/qemu-file.c | 12 ++++++++ 3 files changed, 75 insertions(+), 9 deletions(-)
include/migration/qemu-file.h +7 -0
@@ -85,6 +85,11 @@ typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque, uint64_t *bytes_sent);/* +* Return a QEMUFile for comms in the opposite direction +*/ +typedef QEMUFile *(QEMURetPathFunc )(void *opaque); + +/* * Stop any read or write (depending on flags) on the underlying * transport on the QEMUFile. * Existing blocking reads/writes must be woken
@@ -102,6 +107,7 @@ typedef struct QEMUFileOps {QEMURamHookFunc *after_ram_iterate;QEMURamHookFunc *hook_ram_load;QEMURamSaveFunc *save_page; +QEMURetPathFunc *get_return_path;QEMUFileShutdownFunc *shut_down; } QEMUFileOps;
@@ -189,6 +195,7 @@ int64_t qemu_file_get_rate_limit(QEMUFile *f);int qemu_file_get_error (QEMUFile *f);void qemu_file_set_error (QEMUFile *f,int ret);int qemu_file_shutdown (QEMUFile *f); +QEMUFile *qemu_file_get_return_path (QEMUFile *f);void qemu_fflush (QEMUFile *f);void qemu_file_change_blocking (QEMUFile *f,bool block);
migration/qemu-file-unix.c +56 -9
80 lines not on this page · raw message
migration/qemu-file.c +12 -0
@@ -42,6 +42,18 @@ int qemu_file_shutdown(QEMUFile *f)return f->ops->shut_down (f->opaque, true, true); } +/* +* Result: QEMUFile* for a 'return path' for comms in the opposite direction +* NULL if not available +*/ +QEMUFile *qemu_file_get_return_path (QEMUFile *f) +{ +if (!f->ops->get_return_path) { +return NULL ; + } +return f->ops->get_return_path (f->opaque); +} +bool qemu_file_mode_is_not_valid (const char *mode) { if (mode == NULL ||
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 11/47] Return path: Open a return path on QEMUFile for sockets
Quoted 6 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs a method to send messages from the destination back to the source, this is the 'return path'.
Wire it up for 'socket' QEMUFile's using a dup'd fd.
I'm not too sure we should create a new fd; just use the same one since sockets are supposed to be used that way.
I suspect, though, that using an entirely new connection could simplify a few things (like the next patch).
In a private chat, Dave mentioned he'd give this some thought (and give it a go) -- hope we don't end up needing to do things the dup way.
Amit
PATCH v6 12/47 Return path: socket_writev_buffer: Block even on non-blocking fd's
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The destination sets the fd to non-blocking on incoming migrations; this also affects the return path from the destination, and thus we need to make sure we can safely write to the return path.
--- migration/qemu-file-unix.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-)
migration/qemu-file-unix.c +36 -5
@@ -39,12 +39,43 @@ static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,QEMUFileSocket *s = opaque;ssize_t len;ssize_t size =iov_size (iov, iovcnt); +ssize_t offset =0 ; +int err; - len =iov_send (s->fd, iov, iovcnt,0 , size); -if (len < size) { - len = -socket_error (); - } -return len; +while (size >0 ) { + len =iov_send (s->fd, iov, iovcnt, offset, size); + +if (len >0 ) { + size -= len; + offset += len; + } + +if (size >0 ) { + err =socket_error (); + +if (err !=EAGAIN && err !=EWOULDBLOCK ) { +error_report ("socket_writev_buffer: Got err=%d for (%zd/%zd)" , + err, size, len); +/* +* If I've already sent some but only just got the error, I +* could return the amount validly sent so far and wait for the +* next call to report the error, but I'd rather flag the error +* immediately. +*/ +return -err; + } + +/* Emulate blocking */ +GPollFD pfd; + + pfd.fd = s->fd; + pfd.events =G_IO_OUT |G_IO_ERR ; + pfd.revents =0 ; +g_poll (&pfd,1 /* 1 fd */ ,-1 /* no timeout */ ); + } + } + +return offset; }static int socket_get_fd (void *opaque)
Signature · 1 line
2.1.0
PATCH v6 13/47 Migration commands
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Create QEMU_VM_COMMAND section type for sending commands from source to destination. These commands are not intended to convey guest state but to control the migration process.
For use in postcopy.
--- include/migration/migration.h | 1 + include/sysemu/sysemu.h | 7 +++++++ savevm.c | 47 +++++++++++++++++++++++++++++++++++++++++++ trace-events | 1 + 4 files changed, 56 insertions(+)
include/migration/migration.h +1 -0
@@ -34,6 +34,7 @@#define QEMU_VM_SECTION_FULL 0x04#define QEMU_VM_SUBSECTION 0x05#define QEMU_VM_VMDESCRIPTION 0x06 +#define QEMU_VM_COMMAND 0x07struct MigrationParams {bool blk;
include/sysemu/sysemu.h +7 -0
@@ -82,6 +82,11 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict);void qemu_announce_self (void ); +/* Subcommands for QEMU_VM_COMMAND */ +enum qemu_vm_cmd { +MIG_CMD_INVALID =0 ,/* Must be 0 */ +}; +bool qemu_savevm_state_blocked (Error **errp);void qemu_savevm_state_begin (QEMUFile *f,const MigrationParams *params);
@@ -90,6 +95,8 @@ int qemu_savevm_state_iterate(QEMUFile *f);void qemu_savevm_state_complete_precopy (QEMUFile *f);void qemu_savevm_state_cancel (void );uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size); +void qemu_savevm_command_send (QEMUFile *f,enum qemu_vm_cmd command, +uint16_t len,uint8_t *data);int qemu_loadvm_state (QEMUFile *f); typedef enum DisplayType
savevm.c +47 -0
65 lines not on this page · raw message
trace-events +1 -0
@@ -1171,6 +1171,7 @@ vmware_setmode(uint32_t w, uint32_t h, uint32_t bpp) "%dx%d @ %d bpp" qemu_loadvm_state_section(unsigned int section_type) "%d" qemu_loadvm_state_section_partend(uint32_t section_id) "%u" qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint32_t instance_id, uint32_t version_id) "%u(%s) %u %u" +loadvm_process_command(uint16_t com, uint16_t len) "com=0x%x len=%d" savevm_section_start(const char *id, unsigned int section_id) "%s, section_id %u" savevm_section_end(const char *id, unsigned int section_id, int ret) "%s, section_id %u -> %d" savevm_state_begin(void) ""
Signature · 1 line
2.1.0
PATCH v6 14/47 Return path: Control commands
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add two src->dest commands:
* OPEN_RETURN_PATH - To request that the destination open the return path
* PING - Request an acknowledge from the destination
--- include/migration/migration.h | 2 ++ include/sysemu/sysemu.h | 6 ++++- savevm.c | 59 +++++++++++++++++++++++++++++++++++++++++++ trace-events | 2 ++ 4 files changed, 68 insertions(+), 1 deletion(-)
include/migration/migration.h +2 -0
@@ -46,6 +46,8 @@ typedef struct MigrationState MigrationState;/* State for the incoming migration */ struct MigrationIncomingState {QEMUFile *file; + +QEMUFile *return_path; };MigrationIncomingState *migration_incoming_get_current (void );
include/sysemu/sysemu.h +5 -1
@@ -84,7 +84,9 @@ void qemu_announce_self(void);/* Subcommands for QEMU_VM_COMMAND */ enum qemu_vm_cmd { -MIG_CMD_INVALID =0 ,/* Must be 0 */ +MIG_CMD_INVALID =0 ,/* Must be 0 */ +MIG_CMD_OPEN_RETURN_PATH ,/* Tell the dest to open the Return path */ +MIG_CMD_PING ,/* Request a PONG on the RP */ };bool qemu_savevm_state_blocked (Error **errp);
@@ -97,6 +99,8 @@ void qemu_savevm_state_cancel(void);uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size);void qemu_savevm_command_send (QEMUFile *f,enum qemu_vm_cmd command,uint16_t len,uint8_t *data); +void qemu_savevm_send_ping (QEMUFile *f,uint32_t value); +void qemu_savevm_send_open_return_path (QEMUFile *f);int qemu_loadvm_state (QEMUFile *f); typedef enum DisplayType
savevm.c +59 -0
85 lines not on this page · raw message
trace-events +2 -0
@@ -1172,8 +1172,10 @@ qemu_loadvm_state_section(unsigned int section_type) "%d" qemu_loadvm_state_section_partend(uint32_t section_id) "%u" qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint32_t instance_id, uint32_t version_id) "%u(%s) %u %u" loadvm_process_command(uint16_t com, uint16_t len) "com=0x%x len=%d" +loadvm_process_command_ping(uint32_t val) "%x" savevm_section_start(const char *id, unsigned int section_id) "%s, section_id %u" savevm_section_end(const char *id, unsigned int section_id, int ret) "%s, section_id %u -> %d" +savevm_send_ping(uint32_t val) "%x" savevm_state_begin(void) "" savevm_state_header(void) "" savevm_state_iterate(void) ""
Signature · 1 line
2.1.0
PATCH v6 15/47 Return path: Send responses from destination to source
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add migrate_send_rp_message to send a message from destination to source along the return path.
(It uses a mutex to let it be called from multiple threads)
Add migrate_send_rp_shut to send a 'shut' message to indicate
the destination is finished with the RP.
Add migrate_send_rp_ack to send a 'PONG' message in response to a PING
Use it in the MSG_RP_PING handler
--- include/migration/migration.h | 17 ++++++++++++++++ migration/migration.c | 45 +++++++++++++++++++++++++++++++++++++++++++ savevm.c | 2 +- trace-events | 1 + 4 files changed, 64 insertions(+), 1 deletion(-)
include/migration/migration.h +17 -0
@@ -41,6 +41,13 @@ struct MigrationParams {bool shared; }; +/* Messages sent on the return path from destination to source */ +enum mig_rp_message_type { +MIG_RP_MSG_INVALID =0 ,/* Must be 0 */ +MIG_RP_MSG_SHUT ,/* sibling will not send any more RP messages */ +MIG_RP_MSG_PONG ,/* Response to a PING; data (seq: be32 ) */ +}; +typedef struct MigrationState MigrationState ;/* State for the incoming migration */
@@ -48,6 +55,7 @@ struct MigrationIncomingState {QEMUFile *file;QEMUFile *return_path; +QemuMutex rp_mutex;/* We send replies from multiple threads */ };MigrationIncomingState *migration_incoming_get_current (void );
@@ -164,6 +172,15 @@ int64_t migrate_xbzrle_cache_size(void);int64_t xbzrle_cache_resize (int64_t new_size); +/* Sending on the return path - generic and then for each message type */ +void migrate_send_rp_message (MigrationIncomingState *mis, +enum mig_rp_message_type message_type, +uint16_t len,void *data); +void migrate_send_rp_shut (MigrationIncomingState *mis, +uint32_t value); +void migrate_send_rp_pong (MigrationIncomingState *mis, +uint32_t value); +void ram_control_before_iterate (QEMUFile *f,uint64_t flags);void ram_control_after_iterate (QEMUFile *f,uint64_t flags);void ram_control_load_hook (QEMUFile *f,uint64_t flags);
migration/migration.c +45 -0
57 lines not on this page · raw message
savevm.c +1 -1
@@ -1016,7 +1016,7 @@ static int loadvm_process_command(QEMUFile *f) tmp32);return -1 ; } -/* migrate_send_rp_pong(mis, tmp32); TODO: gets added later */ +migrate_send_rp_pong (mis, tmp32);break ;default :
trace-events +1 -0
@@ -1383,6 +1383,7 @@ migrate_fd_cleanup(void) "" migrate_fd_error(void) "" migrate_fd_cancel(void) "" migrate_pending(uint64_t size, uint64_t max) "pending size %" PRIu64 " max %" PRIu64 +migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" migrate_transferred(uint64_t tranferred, uint64_t time_spent, double bandwidth, uint64_t size) "transferred %" PRIu64 " time_spent %" PRIu64 " bandwidth %g max_size %" PRId64 # migration/rdma.c
Signature · 1 line
2.1.0
PATCH v6 16/47 Return path: Source handling of return path
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Open a return path, and handle messages that are received upon it.
--- include/migration/migration.h | 8 ++ migration/migration.c | 177 +++++++++++++++++++++++++++++++++++++++++- trace-events | 12 +++ 3 files changed, 196 insertions(+), 1 deletion(-)
include/migration/migration.h +8 -0
@@ -73,6 +73,14 @@ struct MigrationStateint state;MigrationParams params; + +/* State related to return path */ +struct { +QEMUFile *file; +QemuThread rp_thread; +bool error; + } rp_state; +double mbps;int64_t total_time;int64_t downtime;
migration/migration.c +176 -1
208 lines not on this page · raw message
trace-events +12 -0
@@ -1378,12 +1378,24 @@ flic_no_device_api(int err) "flic: no Device Contral API support %d" flic_reset_failed(int err) "flic: reset failed %d" # migration.c +await_return_path_close_on_source_close(void) "" +await_return_path_close_on_source_joining(void) "" migrate_set_state(int new_state) "new state %d" migrate_fd_cleanup(void) "" +migrate_fd_cleanup_src_rp(void) "" migrate_fd_error(void) "" migrate_fd_cancel(void) "" migrate_pending(uint64_t size, uint64_t max) "pending size %" PRIu64 " max %" PRIu64 migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" +open_return_path_on_source(void) "" +open_return_path_on_source_continue(void) "" +source_return_path_thread_bad_end(void) "" +source_return_path_thread_end(void) "" +source_return_path_thread_entry(void) "" +source_return_path_thread_failed_read_cmd_data(void) "" +source_return_path_thread_loop_top(void) "" +source_return_path_thread_pong(uint32_t val) "%x" +source_return_path_thread_shut(uint32_t val) "%x" migrate_transferred(uint64_t tranferred, uint64_t time_spent, double bandwidth, uint64_t size) "transferred %" PRIu64 " time_spent %" PRIu64 " bandwidth %g max_size %" PRId64 # migration/rdma.c
Signature · 1 line
2.1.0
PATCH v6 17/47 ram_debug_dump_bitmap: Dump a migration bitmap as text
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Misses out lines that are all the expected value so the output can be quite compact depending on the circumstance.
--- arch_init.c | 40 +++++++++++++++++++++++++++++++++++++++- include/migration/migration.h | 1 + 2 files changed, 40 insertions(+), 1 deletion(-)
arch_init.c +39 -1
@@ -833,13 +833,51 @@ static void reset_ram_globals(void)#define MAX_WAIT 50/* ms, half buffered_file limit */ -/* Each of ram_save_setup, ram_save_iterate and ram_save_complete has * long-running RCU critical section. When rcu-reclaims in the code * start to become numerous it will be necessary to reduce the * granularity of these critical sections. */ +/* +* 'expected' is the value you expect the bitmap mostly to be full +* of and it won't bother printing lines that are all this value +* if 'todump' is null the migration bitmap is dumped. +*/ +void ram_debug_dump_bitmap (unsigned long *todump,bool expected) +{ +int64_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +int64_t cur; +int64_t linelen =128 ; +char linebuf[129 ]; + +if (!todump) { + todump = migration_bitmap; + } + +for (cur =0 ; cur < ram_pages; cur += linelen) { +int64_t curb; +bool found = false; +/* +* Last line; catch the case where the line length +* is longer than remaining ram +*/ +if (cur+linelen > ram_pages) { + linelen = ram_pages - cur; + } +for (curb =0 ; curb < linelen; curb++) { +bool thisbit =test_bit (cur+curb, todump); + linebuf[curb] = thisbit ?'1' :'.' ; + found = found || (thisbit != expected); + } +if (found) { + linebuf[curb] ='\0' ; +fprintf (stderr,"0x%08" PRIx64" : %s\n" , cur, linebuf); + } + } +} +static int ram_save_setup (QEMUFile *f,void *opaque) {RAMBlock *block;
include/migration/migration.h +1 -0
@@ -152,6 +152,7 @@ uint64_t xbzrle_mig_pages_cache_miss(void);double xbzrle_mig_cache_miss_rate (void );void ram_handle_compressed (void *host,uint8_t ch,uint64_t size); +void ram_debug_dump_bitmap (unsigned long *todump,bool expected);/** * @migrate_add_blocker - prevent migration from proceeding
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 17/47] ram_debug_dump_bitmap: Dump a migration bitmap as text
Quoted 4 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Misses out lines that are all the expected value so the output can be quite compact depending on the circumstance.
s/Misses out lines/Prints out lines to stderr/
s/the expected/an expected/
?
Also, some sentence explaining why this is being added - like helps with debugging when <something> goes wrong, and how it saves time. I really think this is a good function to have, and if we can somehow use this output to even create a visualisation of how far ahead we are in the migration process, users can see fancy output which tells them how fast things are converging (in precopy), and how fast the guest is updating the memory vs throttling applied, etc.
Quoted 11 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- arch_init.c | 40 +++++++++++++++++++++++++++++++++++++++- include/migration/migration.h | 1 + 2 files changed, 40 insertions(+), 1 deletion(-)arch_init.c +0
-0
arch_init.c static void reset_ram_globals(void) · line 840
Quoted 9 lines
arch_init.c +0
-1@@ -833,13 +833,51 @@ static void reset_ram_globals(void)#define MAX_WAIT 50/* ms, half buffered_file limit */ -/* Each of ram_save_setup, ram_save_iterate and ram_save_complete has * long-running RCU critical section. When rcu-reclaims in the code * start to become numerous it will be necessary to reduce the * granularity of these critical sections. */
This new function should go above this comment.
arch_init.c static void reset_ram_globals(void) · line 845
Quoted 4 lines
arch_init.c +4
-0+/* +* 'expected' is the value you expect the bitmap mostly to be full +* of and it won't bother printing lines that are all this value +* if 'todump' is null the migration bitmap is dumped.
Missing punctuation? The last line there is a new sentence, isn't it?
Quoted 11 lines
arch_init.c +11
-0+*/ +void ram_debug_dump_bitmap (unsigned long *todump,bool expected) +{ +int64_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +int64_t cur; +int64_t linelen =128 ; +char linebuf[129 ]; + +if (!todump) { + todump = migration_bitmap;
arch_init.c static void reset_ram_globals(void) · line 866
Quoted 10 lines
arch_init.c +10
-0+ } + +for (cur =0 ; cur < ram_pages; cur += linelen) { +int64_t curb; +bool found = false; +/* +* Last line; catch the case where the line length +* is longer than remaining ram +*/ +if (cur+linelen > ram_pages) {
spacing is off: whitespace around '+'. (I didn't run checkpatch, though.) Similar below too.
arch_init.c static void reset_ram_globals(void) · line 870
Quoted 4 lines
arch_init.c +4
-0+ linelen = ram_pages - cur; + } +for (curb =0 ; curb < linelen; curb++) { +bool thisbit =test_bit (cur+curb, todump);
whitespace around +
Amit
Re: [Qemu-devel] [PATCH v6 17/47] ram_debug_dump_bitmap: Dump a migration bitmap as text
Quoted 12 lines · * Amit Shah ([email protected]) wrote:
Quoted 4 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Misses out lines that are all the expected value so the output can be quite compact depending on the circumstance.
s/Misses out lines/Prints out lines to stderr/
s/the expected/an expected/
?
Quoted 7 lines
Also, some sentence explaining why this is being added - like helps with debugging when <something> goes wrong, and how it saves time. I really think this is a good function to have, and if we can somehow use this output to even create a visualisation of how far ahead we are in the migration process, users can see fancy output which tells them how fast things are converging (in precopy), and how fast the guest is updating the memory vs throttling applied, etc.
Changed to:
----ram_debug_dump_bitmap: Dump a migration bitmap as text
Useful for debugging the migration bitmap and other bitmaps of the same format (including the sentmap in postcopy).
The bitmap is printed to stderr.
Lines that are all the expected value are excluded so the output can be quite compact for many bitmaps.
----
I'm not sure it's that useful to endusers; during the migration the bitmap can be quite a mix and thus the 'exclude' doesn't help much so the output can be quite big. Sanidhya Kashyap's series that did repeated logging is more useful as a tool to find out why things aren't converging.
Quoted 15 lines
Quoted 15 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- arch_init.c | 40 +++++++++++++++++++++++++++++++++++++++- include/migration/migration.h | 1 + 2 files changed, 40 insertions(+), 1 deletion(-)arch_init.c +0
-0@@ -833,13 +833,51 @@ static void reset_ram_globals(void)#define MAX_WAIT 50/* ms, half buffered_file limit */
Quoted 8 lines
arch_init.c static void reset_ram_globals(void) · line 840
Quoted 6 lines
arch_init.c +0
-1-/* Each of ram_save_setup, ram_save_iterate and ram_save_complete has * long-running RCU critical section. When rcu-reclaims in the code * start to become numerous it will be necessary to reduce the * granularity of these critical sections. */ This new function should go above this comment.
Done.
Quoted 6 lines
arch_init.c static void reset_ram_globals(void) · line 845
Quoted 4 lines
arch_init.c +4
-0+/* +* 'expected' is the value you expect the bitmap mostly to be full +* of and it won't bother printing lines that are all this value +* if 'todump' is null the migration bitmap is dumped. Missing punctuation? The last line there is a new sentence, isn't it?
Replaced by:
/*
* 'expected' is the value you expect the bitmap mostly to be full
* of; it won't bother printing lines that are all this value.
* If 'todump' is null the migration bitmap is dumped. */
Quoted 16 lines
Quoted 16 lines
arch_init.c +16
-0+*/ +void ram_debug_dump_bitmap (unsigned long *todump,bool expected) +{ +int64_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +int64_t cur; +int64_t linelen =128 ; +char linebuf[129 ]; + +if (!todump) { + todump = migration_bitmap; + } + +for (cur =0 ; cur < ram_pages; cur += linelen) { +int64_t curb; +bool found = false;
Quoted 8 lines
arch_init.c static void reset_ram_globals(void) · line 866
Quoted 5 lines
arch_init.c +5
-0+/* +* Last line; catch the case where the line length +* is longer than remaining ram +*/ +if (cur+linelen > ram_pages) {spacing is off: whitespace around '+'. (I didn't run checkpatch, though.) Similar below too.
Done, oddly checkpatch didn't moan - I don't see why.
Quoted 6 lines
arch_init.c static void reset_ram_globals(void) · line 870
Quoted 4 lines
arch_init.c +4
-0+ linelen = ram_pages - cur; + } +for (curb =0 ; curb < linelen; curb++) { +bool thisbit =test_bit (cur+curb, todump);whitespace around +
Done; again checkpatch doesn't moan.
Thanks,
Dave
Quoted 2 lines
Amit
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v6 18/47 Move loadvm_handlers into MigrationIncomingState
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
In postcopy we need the loadvm_handlers to be used in a couple of different instances of the loadvm loop/routine, and thus it can't be local any more.
--- include/migration/migration.h | 5 +++++ include/migration/vmstate.h | 2 ++ include/qemu/typedefs.h | 1 + migration/migration.c | 2 ++ savevm.c | 28 ++++++++++++++++------------ 5 files changed, 26 insertions(+), 12 deletions(-)
include/migration/migration.h +5 -0
@@ -50,10 +50,15 @@ enum mig_rp_message_type {typedef struct MigrationState MigrationState ; +typedef QLIST_HEAD(, LoadStateEntry) LoadStateEntry_Head; +/* State for the incoming migration */ struct MigrationIncomingState {QEMUFile *file; +/* See savevm.c */ +LoadStateEntry_Head loadvm_handlers; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ };
include/migration/vmstate.h +2 -0
@@ -812,6 +812,8 @@ extern const VMStateInfo vmstate_info_bitmap;#define SELF_ANNOUNCE_ROUNDS 5 +void loadvm_free_handlers (MigrationIncomingState *mis); +int vmstate_load_state (QEMUFile *f,const VMStateDescription *vmsd,void *opaque,int version_id); void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
include/qemu/typedefs.h +1 -0
@@ -31,6 +31,7 @@ typedef struct I2CBus I2CBus;typedef struct I2SCodec I2SCodec ;typedef struct ISABus ISABus ;typedef struct ISADevice ISADevice ; +typedef struct LoadStateEntry LoadStateEntry ;typedef struct MACAddr MACAddr ;typedef struct MachineClass MachineClass ;typedef struct MachineState MachineState ;
migration/migration.c +2 -0
@@ -70,6 +70,7 @@ MigrationIncomingState *migration_incoming_state_new(QEMUFile* f) { mis_current =g_malloc0 (sizeof (MigrationIncomingState)); mis_current->file = f; +QLIST_INIT (&mis_current->loadvm_handlers);qemu_mutex_init (&mis_current->rp_mutex);return mis_current;
@@ -77,6 +78,7 @@ MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)void migration_incoming_state_destroy (void ) { +loadvm_free_handlers (mis_current);g_free (mis_current); mis_current =NULL ; }
savevm.c +16 -12
@@ -1027,18 +1027,26 @@ static int loadvm_process_command(QEMUFile *f)return 0 ; } -typedef struct LoadStateEntry { +struct LoadStateEntry {QLIST_ENTRY (LoadStateEntry) entry;SaveStateEntry *se;int section_id;int version_id; -}LoadStateEntry ; +}; -int qemu_loadvm_state (QEMUFile *f) +void loadvm_free_handlers (MigrationIncomingState *mis) { - QLIST_HEAD(, LoadStateEntry) loadvm_handlers = -QLIST_HEAD_INITIALIZER (loadvm_handlers);LoadStateEntry *le, *new_le; + + QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) { +QLIST_REMOVE (le, entry); +g_free (le); + } +} + +int qemu_loadvm_state (QEMUFile *f) +{ +MigrationIncomingState *mis =migration_incoming_get_current ();Error *local_err =NULL ;uint8_t section_type;unsigned int v;
@@ -1069,6 +1077,7 @@ int qemu_loadvm_state(QEMUFile *f)while ((section_type =qemu_get_byte (f)) !=QEMU_VM_EOF ) {uint32_t instance_id, version_id, section_id;SaveStateEntry *se; +LoadStateEntry *le;char idstr[256 ];trace_qemu_loadvm_state_section (section_type);
@@ -1110,7 +1119,7 @@ int qemu_loadvm_state(QEMUFile *f) le->se = se; le->section_id = section_id; le->version_id = version_id; -QLIST_INSERT_HEAD (&loadvm_handlers, le, entry); +QLIST_INSERT_HEAD (&mis->loadvm_handlers, le, entry); ret =vmstate_load (f, le->se, le->version_id);if (ret <0 ) {
@@ -1124,7 +1133,7 @@ int qemu_loadvm_state(QEMUFile *f) section_id =qemu_get_be32 (f);trace_qemu_loadvm_state_section_partend (section_id); - QLIST_FOREACH(le, &loadvm_handlers, entry) { + QLIST_FOREACH(le, &mis->loadvm_handlers, entry) {if (le->section_id == section_id) {break ; }
@@ -1178,11 +1187,6 @@ int qemu_loadvm_state(QEMUFile *f) ret =0 ; out: - QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { -QLIST_REMOVE (le, entry); -g_free (le); - } -if (ret ==0 ) {/* We may not have a VMDESC section, so ignore relative errors */ ret = file_error_after_eof;
Signature · 1 line
2.1.0
PATCH v6 19/47 Rework loadvm path for subloops
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Split the core of qemu_loadvm_state out so we can use it for both.
Allow the inner loadvm loop to quit and cause the parent loops to exit as well.
--- include/migration/migration.h | 6 ++ migration/migration.c | 2 + savevm.c | 125 +++++++++++++++++++++++------------------- trace-events | 4 ++ 4 files changed, 81 insertions(+), 56 deletions(-)
include/migration/migration.h +6 -0
@@ -59,6 +59,12 @@ struct MigrationIncomingState {/* See savevm.c */ LoadStateEntry_Head loadvm_handlers; +/* +* Free at the start of the main state load, set as the main thread finishes +* loading state. +*/ +QemuEvent main_thread_load_event; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ };
migration/migration.c +2 -0
@@ -72,12 +72,14 @@ MigrationIncomingState *migration_incoming_state_new(QEMUFile* f) mis_current->file = f;QLIST_INIT (&mis_current->loadvm_handlers);qemu_mutex_init (&mis_current->rp_mutex); +qemu_event_init (&mis_current->main_thread_load_event, false);return mis_current; }void migration_incoming_state_destroy (void ) { +qemu_event_destroy (&mis_current->main_thread_load_event);loadvm_free_handlers (mis_current);g_free (mis_current); mis_current =NULL ;
savevm.c +69 -56
195 lines not on this page · raw message
trace-events +4 -0
@@ -1169,7 +1169,11 @@ vmware_setmode(uint32_t w, uint32_t h, uint32_t bpp) "%dx%d @ %d bpp" # savevm.c qemu_loadvm_state_section(unsigned int section_type) "%d" +qemu_loadvm_state_section_command(int ret) "%d" qemu_loadvm_state_section_partend(uint32_t section_id) "%u" +qemu_loadvm_state_main(void) "" +qemu_loadvm_state_main_quit_parent(void) "" +qemu_loadvm_state_post_main(int ret) "%d" qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint32_t instance_id, uint32_t version_id) "%u(%s) %u %u" loadvm_process_command(uint16_t com, uint16_t len) "com=0x%x len=%d" loadvm_process_command_ping(uint32_t val) "%x"
Signature · 1 line
2.1.0
PATCH v6 20/47 Add migration-capability boolean for postcopy-ram.
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The 'postcopy ram' capability allows postcopy migration of RAM; note that the migration starts off in precopy mode until postcopy mode is triggered (see the migrate_start_postcopy patch later in the series).
--- include/migration/migration.h | 1 + migration/migration.c | 9 +++++++++ qapi-schema.json | 7 ++++++- 3 files changed, 16 insertions(+), 1 deletion(-)
include/migration/migration.h +1 -0
@@ -179,6 +179,7 @@ void migrate_add_blocker(Error *reason);*/ void migrate_del_blocker (Error *reason); +bool migrate_postcopy_ram (void );bool migrate_zero_blocks (void );bool migrate_auto_converge (void );
migration/migration.c +9 -0
@@ -685,6 +685,15 @@ void qmp_migrate_set_downtime(double value, Error **errp) max_downtime = (uint64_t )value; } +bool migrate_postcopy_ram (void ) +{ +MigrationState *s; + + s =migrate_get_current (); + +return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM ]; +} +bool migrate_auto_converge (void ) {MigrationState *s;
qapi-schema.json +6 -1
@@ -518,10 +518,15 @@ # @auto-converge: If enabled, QEMU will automatically throttle down the guest # to speed up convergence of RAM migration. (since 1.6) # +# @x-postcopy-ram: Start executing on the migration target before all of RAM has +# been migrated, pulling the remaining pages along as needed. NOTE: If +# the migration fails during postcopy the VM will fail. (since 2.4) +# # Since: 1.2 ## { 'enum': 'MigrationCapability', - 'data': ['xbzrle', 'rdma-pin-all', 'auto-converge', 'zero-blocks'] } + 'data': ['xbzrle', 'rdma-pin-all', 'auto-converge', 'zero-blocks', + 'x-postcopy-ram'] } ## # @MigrationCapabilityStatus
Signature · 1 line
2.1.0
PATCH v6 21/47 Add wrappers and handlers for sending/receiving the postcopy-ram migration messages.
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The state of the postcopy process is managed via a series of messages;
* Add wrappers and handlers for sending/receiving these messages
* Add state variable that track the current state of postcopy
--- include/migration/migration.h | 15 +++ include/sysemu/sysemu.h | 20 ++++ migration/migration.c | 13 +++ savevm.c | 247 ++++++++++++++++++++++++++++++++++++++++++ trace-events | 10 ++ 5 files changed, 305 insertions(+)
include/migration/migration.h +15 -0
@@ -52,6 +52,14 @@ typedef struct MigrationState MigrationState; typedef QLIST_HEAD(, LoadStateEntry) LoadStateEntry_Head; +typedef enum { +POSTCOPY_INCOMING_NONE =0 ,/* Initial state - no postcopy */ +POSTCOPY_INCOMING_ADVISE , +POSTCOPY_INCOMING_LISTENING , +POSTCOPY_INCOMING_RUNNING , +POSTCOPY_INCOMING_END +}PostcopyState ; +/* State for the incoming migration */ struct MigrationIncomingState {QEMUFile *file;
@@ -59,6 +67,8 @@ struct MigrationIncomingState {/* See savevm.c */ LoadStateEntry_Head loadvm_handlers; +PostcopyState postcopy_state; +/* * Free at the start of the main state load, set as the main thread finishes * loading state.
@@ -220,4 +230,9 @@ size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, ram_addr_t offset, size_t size, uint64_t *bytes_sent); +PostcopyState postcopy_state_get (MigrationIncomingState *mis); + +/* Set the state and return the old state */ +PostcopyState postcopy_state_set (MigrationIncomingState *mis, +PostcopyState new_state);#endif
include/sysemu/sysemu.h +20 -0
@@ -87,6 +87,17 @@ enum qemu_vm_cmd {MIG_CMD_INVALID =0 ,/* Must be 0 */ MIG_CMD_OPEN_RETURN_PATH ,/* Tell the dest to open the Return path */ MIG_CMD_PING ,/* Request a PONG on the RP */ + +MIG_CMD_POSTCOPY_ADVISE =20 ,/* Prior to any page transfers, just +warn we might want to do PC */ +MIG_CMD_POSTCOPY_LISTEN ,/* Start listening for incoming +pages as it's running. */ +MIG_CMD_POSTCOPY_RUN ,/* Start execution */ + +MIG_CMD_POSTCOPY_RAM_DISCARD ,/* A list of pages to discard that +were previously sent during +precopy but are dirty. */ + }; bool qemu_savevm_state_blocked(Error **errp);
@@ -101,6 +112,15 @@ void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command, uint16_t len, uint8_t *data);void qemu_savevm_send_ping (QEMUFile *f,uint32_t value);void qemu_savevm_send_open_return_path (QEMUFile *f); +void qemu_savevm_send_postcopy_advise (QEMUFile *f); +void qemu_savevm_send_postcopy_listen (QEMUFile *f); +void qemu_savevm_send_postcopy_run (QEMUFile *f); + +void qemu_savevm_send_postcopy_ram_discard (QEMUFile *f,const char *name, +uint16_t len, +uint64_t *start_list, +uint64_t *end_list); +int qemu_loadvm_state (QEMUFile *f); typedef enum DisplayType
migration/migration.c +13 -0
@@ -995,3 +995,16 @@ void migrate_fd_connect(MigrationState *s)qemu_thread_create (&s->thread,"migration" , migration_thread, s,QEMU_THREAD_JOINABLE ); } + +PostcopyState postcopy_state_get (MigrationIncomingState *mis) +{ +return atomic_fetch_add (&mis->postcopy_state,0 ); +} + +/* Set the state and return the old state */ +PostcopyState postcopy_state_set (MigrationIncomingState *mis, +PostcopyState new_state) +{ +return atomic_xchg (&mis->postcopy_state, new_state); +} +
savevm.c +247 -0
277 lines not on this page · raw message
trace-events +10 -0
@@ -1175,11 +1175,21 @@ qemu_loadvm_state_main(void) "" qemu_loadvm_state_main_quit_parent(void) "" qemu_loadvm_state_post_main(int ret) "%d" qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint32_t instance_id, uint32_t version_id) "%u(%s) %u %u" +loadvm_postcopy_handle_advise(void) "" +loadvm_postcopy_handle_listen(void) "" +loadvm_postcopy_handle_run(void) "" +loadvm_postcopy_ram_handle_discard(void) "" +loadvm_postcopy_ram_handle_discard_end(void) "" +loadvm_postcopy_ram_handle_discard_header(const char *ramid, uint16_t len) "%s: %ud" loadvm_process_command(uint16_t com, uint16_t len) "com=0x%x len=%d" loadvm_process_command_ping(uint32_t val) "%x" +qemu_savevm_send_postcopy_advise(void) "" +qemu_savevm_send_postcopy_ram_discard(const char *id, uint16_t len) "%s: %ud" savevm_section_start(const char *id, unsigned int section_id) "%s, section_id %u" savevm_section_end(const char *id, unsigned int section_id, int ret) "%s, section_id %u -> %d" savevm_send_ping(uint32_t val) "%x" +savevm_send_postcopy_listen(void) "" +savevm_send_postcopy_run(void) "" savevm_state_begin(void) "" savevm_state_header(void) "" savevm_state_iterate(void) ""
Signature · 1 line
2.1.0
PATCH v6 22/47 MIG_CMD_PACKAGED: Send a packaged chunk of migration stream
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
MIG_CMD_PACKAGED is a migration command that wraps a chunk of migration stream inside a package whose length can be determined purely by reading its header. The destination guarantees that the whole MIG_CMD_PACKAGED is read off the stream prior to parsing the contents.
This is used by postcopy to load device state (from the package) while leaving the main stream free to receive memory pages.
--- include/sysemu/sysemu.h | 4 +++ savevm.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ trace-events | 4 +++ 3 files changed, 102 insertions(+)
include/sysemu/sysemu.h +4 -0
@@ -87,6 +87,7 @@ enum qemu_vm_cmd {MIG_CMD_INVALID =0 ,/* Must be 0 */ MIG_CMD_OPEN_RETURN_PATH ,/* Tell the dest to open the Return path */ MIG_CMD_PING ,/* Request a PONG on the RP */ +MIG_CMD_PACKAGED ,/* Send a wrapped stream within this stream */ MIG_CMD_POSTCOPY_ADVISE = 20, /* Prior to any page transfers, justwarn we might want to do PC */
@@ -100,6 +101,8 @@ enum qemu_vm_cmd { }; +#define MAX_VM_CMD_PACKAGED_SIZE (1ul << 24) +bool qemu_savevm_state_blocked (Error **errp);void qemu_savevm_state_begin (QEMUFile *f,const MigrationParams *params);
@@ -112,6 +115,7 @@ void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command, uint16_t len, uint8_t *data);void qemu_savevm_send_ping (QEMUFile *f,uint32_t value);void qemu_savevm_send_open_return_path (QEMUFile *f); +int qemu_savevm_send_packaged (QEMUFile *f,const QEMUSizedBuffer *qsb);void qemu_savevm_send_postcopy_advise (QEMUFile *f);void qemu_savevm_send_postcopy_listen (QEMUFile *f);void qemu_savevm_send_postcopy_run (QEMUFile *f);
savevm.c +94 -0
112 lines not on this page · raw message
trace-events +4 -0
@@ -1175,6 +1175,10 @@ qemu_loadvm_state_main(void) "" qemu_loadvm_state_main_quit_parent(void) "" qemu_loadvm_state_post_main(int ret) "%d" qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint32_t instance_id, uint32_t version_id) "%u(%s) %u %u" +qemu_savevm_send_packaged(void) "" +loadvm_handle_cmd_packaged(unsigned int length) "%u" +loadvm_handle_cmd_packaged_main(int ret) "%d" +loadvm_handle_cmd_packaged_received(int ret) "%d" loadvm_postcopy_handle_advise(void) "" loadvm_postcopy_handle_listen(void) "" loadvm_postcopy_handle_run(void) ""
Signature · 1 line
2.1.0
PATCH v6 23/47 migrate_init: Call from savevm
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Suspend to file is very much like a migrate, and it makes life easier if we have the Migration state available, so initialise it in the savevm.c code for suspending.
--- include/migration/migration.h | 3 +-- include/qemu/typedefs.h | 1 + migration/migration.c | 2 +- savevm.c | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-)
include/migration/migration.h +1 -2
@@ -48,8 +48,6 @@ enum mig_rp_message_type { MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ }; -typedef struct MigrationState MigrationState ; - typedef QLIST_HEAD(, LoadStateEntry) LoadStateEntry_Head; typedef enum {
@@ -148,6 +146,7 @@ int migrate_fd_close(MigrationState *s);void add_migration_state_change_notifier (Notifier *notify);void remove_migration_state_change_notifier (Notifier *notify); +MigrationState *migrate_init (const MigrationParams *params);bool migration_in_setup (MigrationState *);bool migration_has_finished (MigrationState *);bool migration_has_failed (MigrationState *);
include/qemu/typedefs.h +1 -0
@@ -41,6 +41,7 @@ typedef struct MemoryRegion MemoryRegion;typedef struct MemoryRegionSection MemoryRegionSection ;typedef struct MigrationIncomingState MigrationIncomingState ;typedef struct MigrationParams MigrationParams ; +typedef struct MigrationState MigrationState ;typedef struct Monitor Monitor ;typedef struct MouseTransformInfo MouseTransformInfo ;typedef struct MSIMessage MSIMessage ;
migration/migration.c +1 -1
@@ -500,7 +500,7 @@ bool migration_has_failed(MigrationState *s) s->state == MIGRATION_STATUS_FAILED); } -static MigrationState *migrate_init (const MigrationParams *params) +MigrationState *migrate_init (const MigrationParams *params) {MigrationState *s =migrate_get_current ();int64_t bandwidth_limit = s->bandwidth_limit;
savevm.c +2 -0
@@ -988,6 +988,8 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp) .blk = 0, .shared = 0 }; +MigrationState *ms =migrate_init (¶ms); + ms->file = f;if (qemu_savevm_state_blocked (errp)) {return -EINVAL ;
Signature · 1 line
2.1.0
PATCH v6 24/47 Modify save_live_pending for postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Modify save_live_pending to return separate postcopiable and non-postcopiable counts.
--- arch_init.c | 8 ++++++-- include/migration/vmstate.h | 5 +++-- include/sysemu/sysemu.h | 4 +++- migration/block.c | 7 +++++-- migration/migration.c | 9 +++++++-- savevm.c | 21 +++++++++++++++++---- trace-events | 2 +- 7 files changed, 42 insertions(+), 14 deletions(-)
arch_init.c +6 -2
@@ -1053,7 +1053,9 @@ static int ram_save_complete(QEMUFile *f, void *opaque)return 0 ; } -static uint64_t ram_save_pending (QEMUFile *f,void *opaque,uint64_t max_size) +static void ram_save_pending (QEMUFile *f,void *opaque,uint64_t max_size, +uint64_t *non_postcopiable_pending, +uint64_t *postcopiable_pending) {uint64_t remaining_size;
@@ -1067,7 +1069,9 @@ static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)qemu_mutex_unlock_iothread (); remaining_size =ram_save_remaining () *TARGET_PAGE_SIZE ; } -return remaining_size; + + *non_postcopiable_pending =0 ; + *postcopiable_pending = remaining_size; }static int load_xbzrle (QEMUFile *f,ram_addr_t addr,void *host)
include/migration/vmstate.h +3 -2
@@ -54,8 +54,9 @@ typedef struct SaveVMHandlers {/* This runs outside the iothread lock! */ int (*save_live_setup)(QEMUFile *f,void *opaque); -uint64_t (*save_live_pending)(QEMUFile *f,void *opaque,uint64_t max_size); - +void (*save_live_pending)(QEMUFile *f,void *opaque,uint64_t max_size, +uint64_t *non_postcopiable_pending, +uint64_t *postcopiable_pending);LoadStateHandler *load_state; } SaveVMHandlers;
include/sysemu/sysemu.h +3 -1
@@ -110,7 +110,9 @@ void qemu_savevm_state_header(QEMUFile *f);int qemu_savevm_state_iterate (QEMUFile *f);void qemu_savevm_state_complete_precopy (QEMUFile *f);void qemu_savevm_state_cancel (void ); -uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size); +void qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size, +uint64_t *res_non_postcopiable, +uint64_t *res_postcopiable);void qemu_savevm_command_send (QEMUFile *f,enum qemu_vm_cmd command,uint16_t len,uint8_t *data);void qemu_savevm_send_ping (QEMUFile *f,uint32_t value);
migration/block.c +5 -2
@@ -755,7 +755,9 @@ static int block_save_complete(QEMUFile *f, void *opaque)return 0 ; } -static uint64_t block_save_pending (QEMUFile *f,void *opaque,uint64_t max_size) +static void block_save_pending (QEMUFile *f,void *opaque,uint64_t max_size, +uint64_t *non_postcopiable_pending, +uint64_t *postcopiable_pending) {/* Estimate pending number of bytes to send */ uint64_t pending;
@@ -774,7 +776,8 @@ static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)qemu_mutex_unlock_iothread ();DPRINTF ("Enter save live pending %" PRIu64"\n" , pending); -return pending; + *non_postcopiable_pending = pending; + *postcopiable_pending =0 ; }static int block_load (QEMUFile *f,void *opaque,int version_id)
migration/migration.c +7 -2
@@ -889,8 +889,13 @@ static void *migration_thread(void *opaque)uint64_t pending_size;if (!qemu_file_rate_limit (s->file)) { - pending_size =qemu_savevm_state_pending (s->file, max_size); -trace_migrate_pending (pending_size, max_size); +uint64_t pend_post, pend_nonpost; + +qemu_savevm_state_pending (s->file, max_size, &pend_nonpost, + &pend_post); + pending_size = pend_nonpost + pend_post; +trace_migrate_pending (pending_size, max_size, + pend_post, pend_nonpost);if (pending_size && pending_size >= max_size) {qemu_savevm_state_iterate (s->file); }else {
savevm.c +17 -4
@@ -950,10 +950,20 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f)qemu_fflush (f); } -uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size) +/* Give an estimate of the amount left to be transferred, +* the result is split into the amount for units that can and +* for units that can't do postcopy. +*/ +void qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size, +uint64_t *res_non_postcopiable, +uint64_t *res_postcopiable) {SaveStateEntry *se; -uint64_t ret =0 ; +uint64_t tmp_non_postcopiable, tmp_postcopiable; + + *res_non_postcopiable =0 ; + *res_postcopiable =0 ; + QTAILQ_FOREACH(se, &savevm_handlers, entry) {if (!se->ops || !se->ops->save_live_pending) {
@@ -964,9 +974,12 @@ uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)continue ; } } - ret += se->ops->save_live_pending (f, se->opaque, max_size); + se->ops->save_live_pending (f, se->opaque, max_size, + &tmp_non_postcopiable, &tmp_postcopiable); + + *res_postcopiable += tmp_postcopiable; + *res_non_postcopiable += tmp_non_postcopiable; } -return ret; }void qemu_savevm_state_cancel (void )
trace-events +1 -1
@@ -1403,7 +1403,7 @@ migrate_fd_cleanup(void) "" migrate_fd_cleanup_src_rp(void) "" migrate_fd_error(void) "" migrate_fd_cancel(void) "" -migrate_pending(uint64_t size, uint64_t max) "pending size %" PRIu64 " max %" PRIu64 +migrate_pending(uint64_t size, uint64_t max, uint64_t post, uint64_t nonpost) "pending size %" PRIu64 " max %" PRIu64 " (post=%" PRIu64 " nonpost=%" PRIu64 ")" migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" open_return_path_on_source(void) "" open_return_path_on_source_continue(void) ""
Signature · 1 line
2.1.0
PATCH v6 25/47 postcopy: OS support test
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Provide a check to see if the OS we're running on has all the bits needed for postcopy.
Creates postcopy-ram.c which will get most of the other helpers we need.
--- include/migration/postcopy-ram.h | 19 +++++ migration/Makefile.objs | 2 +- migration/postcopy-ram.c | 157 +++++++++++++++++++++++++++++++++++++++ savevm.c | 5 ++ 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 include/migration/postcopy-ram.h create mode 100644 migration/postcopy-ram.c
include/migration/postcopy-ram.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* Postcopy migration for RAM +* +* Copyright 2013 Red Hat, Inc. and/or its affiliates +* +* Authors: +* Dave Gilbert <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ +#ifndef QEMU_POSTCOPY_RAM_H +#define QEMU_POSTCOPY_RAM_H + +/* Return true if the host supports everything we need to do postcopy-ram */ +bool postcopy_ram_supported_by_host (void ); + +#endif
migration/Makefile.objs +1 -1
@@ -1,7 +1,7 @@ common-obj-y += migration.o tcp.o common-obj-y += vmstate.o common-obj-y += qemu-file.o qemu-file-buf.o qemu-file-unix.o qemu-file-stdio.o -common-obj-y += xbzrle.o +common-obj-y += xbzrle.o postcopy-ram.o common-obj-$(CONFIG_RDMA) += rdma.o common-obj-$(CONFIG_POSIX) += exec.o unix.o fd.o
migration/postcopy-ram.c new file +157 -0
157 lines not on this page · raw message
savevm.c +5 -0
@@ -33,6 +33,7 @@#include "qemu/timer.h" #include "audio/audio.h" #include "migration/migration.h" +#include "migration/postcopy-ram.h" #include "qemu/sockets.h" #include "qemu/queue.h" #include "sysemu/cpus.h"
@@ -1113,6 +1114,10 @@ static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,return -1 ; } +if (!postcopy_ram_supported_by_host ()) { +return -1 ; + } +if (remote_hps !=getpagesize ()) {/* * Some combinations of mismatch are probably possible but it gets
Signature · 1 line
2.1.0
PATCH v6 26/47 migrate_start_postcopy: Command to trigger transition to postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Once postcopy is enabled (with migrate_set_capability), the migration will still start on precopy mode. To cause a transition into postcopy the:
migrate_start_postcopy
command must be issued. Postcopy will start sometime after this (when it's next checked in the migration loop).
Issuing the command before migration has started will error, and issuing after it has finished is ignored.
--- hmp-commands.hx | 15 +++++++++++++++ hmp.c | 7 +++++++ hmp.h | 1 + include/migration/migration.h | 3 +++ migration/migration.c | 22 ++++++++++++++++++++++ qapi-schema.json | 8 ++++++++ qmp-commands.hx | 19 +++++++++++++++++++ 7 files changed, 75 insertions(+)
hmp-commands.hx +15 -0
@@ -993,6 +993,21 @@ Enable/Disable the usage of a capability @var{capability} for migration. ETEXI { + .name = "migrate_start_postcopy", + .args_type = "", + .params = "", + .help = "Switch migration to postcopy mode", + .mhandler.cmd = hmp_migrate_start_postcopy, + }, + +STEXI +@item migrate_start_postcopy +@findex migrate_start_postcopy +Switch in-progress migration to postcopy mode. Ignored after the end of +migration (or once already in postcopy). +ETEXI + + { .name = "client_migrate_info", .args_type = "protocol:s,hostname:s,port:i?,tls-port:i?,cert-subject:s?", .params = "protocol hostname port tls-port cert-subject",
hmp.c +7 -0
@@ -1184,6 +1184,13 @@ void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict) } } +void hmp_migrate_start_postcopy (Monitor *mon,const QDict *qdict) +{ +Error *err =NULL ; +qmp_migrate_start_postcopy (&err); +hmp_handle_error (mon, &err); +} +void hmp_set_password (Monitor *mon,const QDict *qdict) {const char *protocol =qdict_get_str (qdict,"protocol" );
hmp.h +1 -0
@@ -65,6 +65,7 @@ void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict);void hmp_migrate_set_speed (Monitor *mon,const QDict *qdict);void hmp_migrate_set_capability (Monitor *mon,const QDict *qdict);void hmp_migrate_set_cache_size (Monitor *mon,const QDict *qdict); +void hmp_migrate_start_postcopy (Monitor *mon,const QDict *qdict);void hmp_set_password (Monitor *mon,const QDict *qdict);void hmp_expire_password (Monitor *mon,const QDict *qdict);void hmp_eject (Monitor *mon,const QDict *qdict);
include/migration/migration.h +3 -0
@@ -110,6 +110,9 @@ struct MigrationStateint64_t xbzrle_cache_size;int64_t setup_time;int64_t dirty_sync_count; + +/* Flag set once the migration has been asked to enter postcopy */ +bool start_postcopy; };void process_incoming_migration (QEMUFile *f);
migration/migration.c +22 -0
@@ -377,6 +377,28 @@ void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, } } +void qmp_migrate_start_postcopy (Error **errp) +{ +MigrationState *s =migrate_get_current (); + +if (!migrate_postcopy_ram ()) { +error_setg (errp,"Enable postcopy with migration_set_capability before" +" the start of migration" ); +return ; + } + +if (s->state ==MIGRATION_STATUS_NONE ) { +error_setg (errp,"Postcopy must be started after migration has been" +" started" ); +return ; + } +/* +* we don't error if migration has finished since that would be racy +* with issuing this command. +*/ +atomic_set (&s->start_postcopy, true); +} +/* shared migration helpers */ static void migrate_set_state (MigrationState *s,int old_state,int new_state)
qapi-schema.json +8 -0
@@ -566,6 +566,14 @@ { 'command': 'query-migrate-capabilities', 'returns': ['MigrationCapabilityStatus']} ## +# @migrate-start-postcopy +# +# Switch migration to postcopy mode +# +# Since: 2.3 +{ 'command': 'migrate-start-postcopy' } + +## # @MouseInfo: # # Information about a mouse device.
qmp-commands.hx +19 -0
@@ -713,6 +713,25 @@ Example: EQMP { + .name = "migrate-start-postcopy", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_migrate_start_postcopy, + }, + +SQMP +migrate-start-postcopy +---------------------- + +Switch an in-progress migration to postcopy mode. Ignored after the end of +migration (or once already in postcopy). + +Example: +-> { "execute": "migrate-start-postcopy" } +<- { "return": {} } + +EQMP + + { .name = "query-migrate-cache-size", .args_type = "", .mhandler.cmd_new = qmp_marshal_input_query_migrate_cache_size,
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 26/47] migrate_start_postcopy: Command to trigger transition to postcopy
Quoted 20 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Once postcopy is enabled (with
migrate_set_capability), the migration will still start on precopy mode. To cause a transition into postcopy the:
migrate_start_postcopycommand must be issued. Postcopy will start sometime after this (when it's next checked in the migration loop).
Issuing the command before migration has started will error, and issuing after it has finished is ignored.
Signed-off-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Eric Blake <[email protected]>---
+++
b/qapi-schema.json
qapi-schema.json line 573
Quoted 8 lines
qapi-schema.json +5
-0@@ -566,6 +566,14 @@ { 'command': 'query-migrate-capabilities', 'returns': ['MigrationCapabilityStatus']} ## +# @migrate-start-postcopy +# +# Switch migration to postcopy mode +# +# Since: 2.3
2.4
qapi-schema.json line 575
Quoted 2 lines
qapi-schema.json +2
-0+{ 'command': 'migrate-start-postcopy' } +
As that's easily fixable by the maintainer, my R-b stands.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v6 26/47] migrate_start_postcopy: Command to trigger transition to postcopy
Quoted 23 lines · * Eric Blake ([email protected]) wrote:
Quoted 22 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Once postcopy is enabled (with
migrate_set_capability), the migration will still start on precopy mode. To cause a transition into postcopy the:
migrate_start_postcopycommand must be issued. Postcopy will start sometime after this (when it's next checked in the migration loop).
Issuing the command before migration has started will error, and issuing after it has finished is ignored.
Signed-off-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Eric Blake <[email protected]>---
+++
b/qapi-schema.jsonqapi-schema.json +0
-0@@ -566,6 +566,14 @@ { 'command': 'query-migrate-capabilities', 'returns': ['MigrationCapabilityStatus']}
Quoted 8 lines
qapi-schema.json line 573
Quoted 6 lines
qapi-schema.json +5
-0## +# @migrate-start-postcopy +# +# Switch migration to postcopy mode +# +# Since: 2.32.4
Thanks, fixed.
Dave
Quoted 9 lines
qapi-schema.json line 575
Quoted 2 lines
qapi-schema.json +2
-0+{ 'command': 'migrate-start-postcopy' } +As that's easily fixable by the maintainer, my R-b stands.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v6 27/47 MIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
'MIGRATION_STATUS_POSTCOPY_ACTIVE' is entered after migrate_start_postcopy
'migration_postcopy_phase' is provided for other sections to know if they're in postcopy.
--- include/migration/migration.h | 2 ++ migration/migration.c | 56 ++++++++++++++++++++++++++++++++++++------- qapi-schema.json | 4 +++- trace-events | 1 + 4 files changed, 54 insertions(+), 9 deletions(-)
include/migration/migration.h +2 -0
@@ -153,6 +153,8 @@ MigrationState *migrate_init(const MigrationParams *params);bool migration_in_setup (MigrationState *);bool migration_has_finished (MigrationState *);bool migration_has_failed (MigrationState *); +/* True if outgoing migration has entered postcopy phase */ +bool migration_postcopy_phase (MigrationState *);MigrationState *migrate_get_current (void );uint64_t ram_bytes_remaining (void );
migration/migration.c +48 -8
104 lines not on this page · raw message
qapi-schema.json +3 -1
@@ -424,6 +424,8 @@ # # @active: in the process of doing migration. # +# @postcopy-active: as active, but now in postcopy mode. +# # @completed: migration is finished. # # @failed: some error occurred during migration process.
@@ -433,7 +435,7 @@ ## { 'enum': 'MigrationStatus', 'data': [ 'none', 'setup', 'cancelling', 'cancelled', - 'active', 'completed', 'failed' ] } + 'active', 'postcopy-active', 'completed', 'failed' ] } ## # @MigrationInfo
trace-events +1 -0
@@ -1405,6 +1405,7 @@ migrate_fd_error(void) "" migrate_fd_cancel(void) "" migrate_pending(uint64_t size, uint64_t max, uint64_t post, uint64_t nonpost) "pending size %" PRIu64 " max %" PRIu64 " (post=%" PRIu64 " nonpost=%" PRIu64 ")" migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" +migration_thread_setup_complete(void) "" open_return_path_on_source(void) "" open_return_path_on_source_continue(void) "" source_return_path_thread_bad_end(void) ""
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 27/47] MIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state
Quoted 19 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
'
MIGRATION_STATUS_POSTCOPY_ACTIVE' is entered aftermigrate_start_postcopy'
migration_postcopy_phase' is provided for other sections to know if they're in postcopy.Signed-off-by Dr. David Alan Gilbert <[email protected]>Reviewed-by David Gibson <[email protected]>--- include/migration/migration.h | 2 ++ migration/migration.c | 56 ++++++++++++++++++++++++++++++++++++------- qapi-schema.json | 4 +++- trace-events | 1 + 4 files changed, 54 insertions(+), 9 deletions(-)+++
b/qapi-schema.json
qapi-schema.json line 428
Quoted 5 lines
qapi-schema.json +2
-0@@ -424,6 +424,8 @@ # # @active: in the process of doing migration. # +# @postcopy-active: as active, but now in postcopy mode. +#
s/as/like/
Needs a (since 2.4) designation.
Minor enough that I'm okay if you fix them and add:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v6 27/47] MIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state
Quoted 29 lines · * Eric Blake ([email protected]) wrote:
Quoted 19 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
'
MIGRATION_STATUS_POSTCOPY_ACTIVE' is entered aftermigrate_start_postcopy'
migration_postcopy_phase' is provided for other sections to know if they're in postcopy.Signed-off-by Dr. David Alan Gilbert <[email protected]>Reviewed-by David Gibson <[email protected]>--- include/migration/migration.h | 2 ++ migration/migration.c | 56 ++++++++++++++++++++++++++++++++++++------- qapi-schema.json | 4 +++- trace-events | 1 + 4 files changed, 54 insertions(+), 9 deletions(-)+++
b/qapi-schema.jsonqapi-schema.json line 428
Quoted 5 lines
qapi-schema.json +2
-0@@ -424,6 +424,8 @@ # # @active: in the process of doing migration. # +# @postcopy-active: as active, but now in postcopy mode. +#s/as/like/
Needs a (since 2.4) designation.
Quoted 2 lines
Minor enough that I'm okay if you fix them and add:
Reviewed-by Eric Blake <[email protected]>
Done. Thanks.
Dave
Quoted 5 lines
Signature · 3 lines
-- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v6 28/47 Add qemu_savevm_state_complete_postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add qemu_savevm_state_complete_postcopy to complement qemu_savevm_state_complete_precopy together with a new save_live_complete_postcopy method on devices.
The save_live_complete_precopy method is called on all devices during a precopy migration, and all non-postcopy devices during a postcopy migration at the transition.
The save_live_complete_postcopy method is called at the end of postcopy for all postcopiable devices.
--- arch_init.c | 1 + include/migration/vmstate.h | 1 + include/sysemu/sysemu.h | 1 + savevm.c | 50 ++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 3 deletions(-)
arch_init.c +1 -0
@@ -1275,6 +1275,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)static SaveVMHandlers savevm_ram_handlers = { .save_live_setup = ram_save_setup, .save_live_iterate = ram_save_iterate, + .save_live_complete_postcopy = ram_save_complete, .save_live_complete_precopy = ram_save_complete, .save_live_pending = ram_save_pending, .load_state = ram_load,
include/migration/vmstate.h +1 -0
@@ -40,6 +40,7 @@ typedef struct SaveVMHandlers {SaveStateHandler *save_state;void (*cancel)(void *opaque); +int (*save_live_complete_postcopy)(QEMUFile *f,void *opaque);int (*save_live_complete_precopy)(QEMUFile *f,void *opaque);/* This runs both outside and inside the iothread lock. */
include/sysemu/sysemu.h +1 -0
@@ -108,6 +108,7 @@ void qemu_savevm_state_begin(QEMUFile *f, const MigrationParams *params);void qemu_savevm_state_header (QEMUFile *f);int qemu_savevm_state_iterate (QEMUFile *f); +void qemu_savevm_state_complete_postcopy (QEMUFile *f);void qemu_savevm_state_complete_precopy (QEMUFile *f);void qemu_savevm_state_cancel (void ); void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
savevm.c +47 -3
74 lines not on this page · raw message
Signature · 1 line
2.1.0
PATCH v6 29/47 Postcopy: Maintain sentmap and calculate discard
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Where postcopy is preceeded by a period of precopy, the destination will have received pages that may have been dirtied on the source after the page was sent. The destination must throw these pages away before starting it's CPUs.
Maintain a 'sentmap' of pages that have already been sent. Calculate list of sent & dirty pages
Provide helpers on the destination side to discard these.
--- arch_init.c | 202 ++++++++++++++++++++++++++++++++++++++- include/migration/migration.h | 12 +++ include/migration/postcopy-ram.h | 35 +++++++ include/qemu/typedefs.h | 1 + migration/migration.c | 1 + migration/postcopy-ram.c | 108 +++++++++++++++++++++ savevm.c | 2 - trace-events | 5 + 8 files changed, 362 insertions(+), 4 deletions(-)
arch_init.c +200 -2
@@ -40,6 +40,7 @@#include "hw/audio/audio.h" #include "sysemu/kvm.h" #include "migration/migration.h" +#include "migration/postcopy-ram.h" #include "hw/i386/smbios.h" #include "exec/address-spaces.h" #include "hw/audio/pcspk.h"
@@ -443,9 +444,17 @@ static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data,return 1 ; } +/* mr: The region to search for dirty pages in +* start: Start address (typically so we can continue from previous page) +* ram_addr_abs: Pointer into which to store the address of the dirty page +* within the global ram_addr space +* +* Returns: byte offset within memory region of the start of a dirty page +*/ static inline ram_addr_t migration_bitmap_find_and_reset_dirty (MemoryRegion *mr, -ram_addr_t start) +ram_addr_t start, +ram_addr_t *ram_addr_abs) {unsigned long base = mr->ram_addr >>TARGET_PAGE_BITS ;unsigned long nr = base + (start >>TARGET_PAGE_BITS );
@@ -464,6 +473,7 @@ ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,clear_bit (next, migration_bitmap); migration_dirty_pages--; } + *ram_addr_abs = next <<TARGET_PAGE_BITS ;return (next - base) <<TARGET_PAGE_BITS ; }
@@ -603,6 +613,19 @@ static void migration_bitmap_sync(void) } } +static RAMBlock *ram_find_block (const char *id) +{ +RAMBlock *block; + + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { +if (!strcmp (id, block->idstr)) { +return block; + } + } + +return NULL ; +} +/** * ram_save_page: Send the given page to the stream *
@@ -713,13 +736,16 @@ static int ram_find_and_save_block(QEMUFile *f, bool last_stage,bool complete_round = false;int pages =0 ;MemoryRegion *mr; +ram_addr_t dirty_ram_abs;/* Address of the start of the dirty page in +ram_addr_t space */ if (!block) block =QLIST_FIRST_RCU (&ram_list.blocks);while (true) { mr = block->mr; - offset =migration_bitmap_find_and_reset_dirty (mr, offset); + offset =migration_bitmap_find_and_reset_dirty (mr, offset, + &dirty_ram_abs);if (complete_round && block == last_seen_block && offset >= last_offset) {break ;
@@ -738,6 +764,11 @@ static int ram_find_and_save_block(QEMUFile *f, bool last_stage,/* if page is unmodified, continue to the next */ if (pages >0 ) { +MigrationState *ms =migrate_get_current (); +if (ms->sentmap) { +set_bit (dirty_ram_abs >>TARGET_PAGE_BITS , ms->sentmap); + } + last_sent_block = block;break ; }
@@ -799,12 +830,19 @@ void free_xbzrle_decoded_buf(void)static void migration_end (void ) { +MigrationState *s =migrate_get_current (); +if (migration_bitmap) {memory_global_dirty_log_stop ();g_free (migration_bitmap); migration_bitmap =NULL ; } +if (s->sentmap) { +g_free (s->sentmap); + s->sentmap =NULL ; + } +XBZRLE_cache_lock ();if (XBZRLE .cache) {cache_fini (XBZRLE .cache);
@@ -878,6 +916,160 @@ void ram_debug_dump_bitmap(unsigned long *todump, bool expected) } } +/* **** functions for postcopy ***** */ + +/* +* Callback from postcopy_each_ram_send_discard for each RAMBlock +* start,end: Indexes into the bitmap for the first and last bit +* representing the named block +*/ +static int postcopy_send_discard_bm_ram (MigrationState *ms, +PostcopyDiscardState *pds, +unsigned long start,unsigned long end) +{ +unsigned long current; + +for (current = start; current <= end; ) { +unsigned long set =find_next_bit (ms->sentmap, end +1 , current); + +if (set <= end) { +unsigned long zero =find_next_zero_bit (ms->sentmap, + end +1 , set +1 ); + +if (zero > end) { + zero = end +1 ; + } +postcopy_discard_send_range (ms, pds, set, zero -1 ); + current = zero +1 ; + }else { + current = set; + } + } + +return 0 ; +} + +/* +* Utility for the outgoing postcopy code. +* Calls postcopy_send_discard_bm_ram for each RAMBlock +* passing it bitmap indexes and name. +* Returns: 0 on success +* (qemu_ram_foreach_block ends up passing unscaled lengths +* which would mean postcopy code would have to deal with target page) +*/ +static int postcopy_each_ram_send_discard (MigrationState *ms) +{ +struct RAMBlock *block; +int ret; + + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { +unsigned long first = block->offset >>TARGET_PAGE_BITS ; +unsigned long last = (block->offset + (block->max_length-1 )) + >>TARGET_PAGE_BITS ; +PostcopyDiscardState *pds =postcopy_discard_send_init (ms, + first, + block->idstr); + +/* +* Postcopy sends chunks of bitmap over the wire, but it +* just needs indexes at this point, avoids it having +* target page specific code. +*/ + ret =postcopy_send_discard_bm_ram (ms, pds, first, last); +postcopy_discard_send_finish (ms, pds); +if (ret) { +return ret; + } + } + +return 0 ; +} + +/* +* Transmit the set of pages to be discarded after precopy to the target +* these are pages that have been sent previously but have been dirtied +* Hopefully this is pretty sparse +*/ +int ram_postcopy_send_discard_bitmap (MigrationState *ms) +{ +int ret; + +rcu_read_lock (); +/* This should be our last sync, the src is now paused */ +migration_bitmap_sync (); + +/* +* Update the sentmap to be sentmap&=dirty +*/ +bitmap_and (ms->sentmap, ms->sentmap, migration_bitmap, +last_ram_offset () >>TARGET_PAGE_BITS ); + + +trace_ram_postcopy_send_discard_bitmap (); +#ifdef DEBUG_POSTCOPY +ram_debug_dump_bitmap (ms->sentmap, false); +#endif + + ret =postcopy_each_ram_send_discard (ms); +rcu_read_unlock (); + +return ret; +} + +/* +* At the start of the postcopy phase of migration, any now-dirty +* precopied pages are discarded. +* +* start..end is an inclusive byte address range within the RAMBlock +* +* Returns 0 on success. +*/ +int ram_discard_range (MigrationIncomingState *mis, +const char *block_name, +uint64_t start,uint64_t end) +{ +int ret =-1 ; + +assert (end >= start); + +rcu_read_lock (); +RAMBlock *rb =ram_find_block (block_name); + +if (!rb) { +error_report ("ram_discard_range: Failed to find block '%s'" , + block_name); + goto err; + } + +uint8_t *host_startaddr = rb->host + start; +uint8_t *host_endaddr; + +if ((uintptr_t )host_startaddr & (qemu_host_page_size -1 )) { +error_report ("ram_discard_range: Unaligned start address: %p" , + host_startaddr); + goto err; + } + +if (end <= rb->used_length) { + host_endaddr = rb->host + end; +if (((uintptr_t )host_endaddr +1 ) & (qemu_host_page_size -1 )) { +error_report ("ram_discard_range: Unaligned end address: %p" , + host_endaddr); + goto err; + } + ret =postcopy_ram_discard_range (mis, host_startaddr, host_endaddr); + }else { +error_report ("ram_discard_range: Overrun block '%s' (%" PRIu64 +"/%" PRIu64"/%zu)" , + block_name, start, end, rb->used_length); + } + +err: +rcu_read_unlock (); + +return ret; +} +static int ram_save_setup (QEMUFile *f,void *opaque) {RAMBlock *block;
@@ -929,6 +1121,12 @@ static int ram_save_setup(QEMUFile *f, void *opaque) migration_bitmap =bitmap_new (ram_bitmap_pages);bitmap_set (migration_bitmap,0 , ram_bitmap_pages); +if (migrate_postcopy_ram ()) { +MigrationState *s =migrate_get_current (); + s->sentmap =bitmap_new (ram_bitmap_pages); +bitmap_clear (s->sentmap,0 , ram_bitmap_pages); + } +/* * Count the total number of pages used by ram blocks not including any * gaps due to alignment or unplugs.
include/migration/migration.h +12 -0
@@ -113,6 +113,13 @@ struct MigrationState/* Flag set once the migration has been asked to enter postcopy */ bool start_postcopy; + +/* bitmap of pages that have been sent at least once +* only maintained and used in postcopy at the moment +* where it's used to send the dirtymap at the start +* of the postcopy phase +*/ +unsigned long *sentmap; };void process_incoming_migration (QEMUFile *f);
@@ -178,6 +185,11 @@ double xbzrle_mig_cache_miss_rate(void);void ram_handle_compressed (void *host,uint8_t ch,uint64_t size);void ram_debug_dump_bitmap (unsigned long *todump,bool expected); +/* For outgoing discard bitmap */ +int ram_postcopy_send_discard_bitmap (MigrationState *ms); +/* For incoming postcopy discard */ +int ram_discard_range (MigrationIncomingState *mis,const char *block_name, +uint64_t start,uint64_t end);/** * @migrate_add_blocker - prevent migration from proceeding
include/migration/postcopy-ram.h +35 -0
@@ -16,4 +16,39 @@/* Return true if the host supports everything we need to do postcopy-ram */ bool postcopy_ram_supported_by_host (void ); +/* +* Discard the contents of memory start..end inclusive. +* We can assume that if we've been called postcopy_ram_hosttest returned true +*/ +int postcopy_ram_discard_range (MigrationIncomingState *mis,uint8_t *start, +uint8_t *end); + + +/* +* Called at the start of each RAMBlock by the bitmap code +* 'offset' is the bitmap offset of the named RAMBlock in the migration +* bitmap. +* Returns a new PDS +*/ +PostcopyDiscardState *postcopy_discard_send_init (MigrationState *ms, +unsigned long offset, +const char *name); + +/* +* Called by the bitmap code for each chunk to discard +* May send a discard message, may just leave it queued to +* be sent later +* 'start' and 'end' describe an inclusive range of pages in the +* migration bitmap in the RAM block passed to postcopy_discard_send_init +*/ +void postcopy_discard_send_range (MigrationState *ms,PostcopyDiscardState *pds, +unsigned long start,unsigned long end); + +/* +* Called at the end of each RAMBlock by the bitmap code +* Sends any outstanding discard messages, frees the PDS +*/ +void postcopy_discard_send_finish (MigrationState *ms, +PostcopyDiscardState *pds); +#endif
include/qemu/typedefs.h +1 -0
@@ -61,6 +61,7 @@ typedef struct PCIExpressHost PCIExpressHost;typedef struct PCIHostState PCIHostState ;typedef struct PCMCIACardState PCMCIACardState ;typedef struct PixelFormat PixelFormat ; +typedef struct PostcopyDiscardState PostcopyDiscardState ;typedef struct PropertyInfo PropertyInfo ;typedef struct Property Property ;typedef struct QEMUBH QEMUBH ;
migration/migration.c +1 -0
@@ -22,6 +22,7 @@#include "block/block.h" #include "qemu/sockets.h" #include "migration/block.h" +#include "migration/postcopy-ram.h" #include "qemu/thread.h" #include "qmp-commands.h" #include "trace.h"
migration/postcopy-ram.c +108 -0
125 lines not on this page · raw message
savevm.c +0 -2
@@ -1242,7 +1242,6 @@ static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis, }trace_loadvm_postcopy_ram_handle_discard_header (ramid, len);while (len) { -/* TODO - ram_discard_range gets added in a later patch uint64_t start_addr, end_addr; start_addr =qemu_get_be64 (mis->file); end_addr =qemu_get_be64 (mis->file);
@@ -1252,7 +1251,6 @@ static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,if (ret) {return ret; } -*/ }trace_loadvm_postcopy_ram_handle_discard_end ();
trace-events +5 -0
@@ -1219,6 +1219,7 @@ qemu_file_fclose(void) "" migration_bitmap_sync_start(void) "" migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64"" migration_throttle(void) "" +ram_postcopy_send_discard_bitmap(void) "" # hw/display/qxl.c disable qxl_interface_set_mm_time(int qid, uint32_t mm_time) "%d %d"
@@ -1479,6 +1480,10 @@ rdma_start_incoming_migration_after_rdma_listen(void) "" rdma_start_outgoing_migration_after_rdma_connect(void) "" rdma_start_outgoing_migration_after_rdma_source_init(void) "" +# migration/postcopy-ram.c +postcopy_discard_send_finish(const char *ramblock, int nwords, int ncmds) "%s mask words sent=%d in %d commands" +postcopy_ram_discard_range(void *start, void *end) "%p,%p" + # kvm-all.c kvm_ioctl(int type, void *arg) "type 0x%x, arg %p" kvm_vm_ioctl(int type, void *arg) "type 0x%x, arg %p"
Signature · 1 line
2.1.0
PATCH v6 30/47 postcopy: Incoming initialisation
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- arch_init.c | 11 ++++ include/migration/migration.h | 3 + include/migration/postcopy-ram.h | 12 ++++ migration/postcopy-ram.c | 116 +++++++++++++++++++++++++++++++++++++++ savevm.c | 4 ++ trace-events | 2 + 6 files changed, 148 insertions(+)
arch_init.c +11 -0
@@ -1353,6 +1353,17 @@ void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) } } +/* +* Allocate data structures etc needed by incoming migration with postcopy-ram +* postcopy-ram's similarly names postcopy_ram_incoming_init does the work +*/ +int ram_postcopy_incoming_init (MigrationIncomingState *mis) +{ +size_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +return postcopy_ram_incoming_init (mis, ram_pages); +} +static int ram_load (QEMUFile *f,void *opaque,int version_id) {int flags =0 , ret =0 ;
include/migration/migration.h +3 -0
@@ -73,6 +73,8 @@ struct MigrationIncomingState {*/ QemuEvent main_thread_load_event; +/* For the kernel to send us notifications */ +int userfault_fd;QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ };
@@ -190,6 +192,7 @@ int ram_postcopy_send_discard_bitmap(MigrationState *ms);/* For incoming postcopy discard */ int ram_discard_range (MigrationIncomingState *mis,const char *block_name,uint64_t start,uint64_t end); +int ram_postcopy_incoming_init (MigrationIncomingState *mis);/** * @migrate_add_blocker - prevent migration from proceeding
include/migration/postcopy-ram.h +12 -0
@@ -17,6 +17,18 @@bool postcopy_ram_supported_by_host (void );/* +* Initialise postcopy-ram, setting the RAM to a state where we can go into +* postcopy later; must be called prior to any precopy. +* called from arch_init's similarly named ram_postcopy_incoming_init +*/ +int postcopy_ram_incoming_init (MigrationIncomingState *mis,size_t ram_pages); + +/* +* At the end of a migration where postcopy_ram_incoming_init was called. +*/ +int postcopy_ram_incoming_cleanup (MigrationIncomingState *mis); + +/* * Discard the contents of memory start..end inclusive. * We can assume that if we've been called postcopy_ram_hosttest returned true */
migration/postcopy-ram.c +116 -0
128 lines not on this page · raw message
savevm.c +4 -0
@@ -1185,6 +1185,10 @@ static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,return -1 ; } +if (ram_postcopy_incoming_init (mis)) { +return -1 ; + } +postcopy_state_set (mis,POSTCOPY_INCOMING_ADVISE );return 0 ;
trace-events +2 -0
@@ -1482,7 +1482,9 @@ rdma_start_outgoing_migration_after_rdma_source_init(void) "" # migration/postcopy-ram.c postcopy_discard_send_finish(const char *ramblock, int nwords, int ncmds) "%s mask words sent=%d in %d commands" +postcopy_cleanup_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" postcopy_ram_discard_range(void *start, void *end) "%p,%p" +postcopy_init_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" # kvm-all.c kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
Signature · 1 line
2.1.0
PATCH v6 31/47 postcopy: ram_enable_notify to switch on userfault
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Mark the area of RAM as 'userfault'
Start up a fault-thread to handle any userfaults we might receive from it (to be filled in later)
--- include/migration/migration.h | 3 ++ include/migration/postcopy-ram.h | 6 ++++ migration/postcopy-ram.c | 69 +++++++++++++++++++++++++++++++++++++++- savevm.c | 9 ++++++ 4 files changed, 86 insertions(+), 1 deletion(-)
include/migration/migration.h +3 -0
@@ -73,6 +73,9 @@ struct MigrationIncomingState {*/ QemuEvent main_thread_load_event; +QemuThread fault_thread; +QemuSemaphore fault_thread_sem; +/* For the kernel to send us notifications */ int userfault_fd;QEMUFile *return_path;
include/migration/postcopy-ram.h +6 -0
@@ -17,6 +17,12 @@bool postcopy_ram_supported_by_host (void );/* +* Make all of RAM sensitive to accesses to areas that haven't yet been written +* and wire up anything necessary to deal with it. +*/ +int postcopy_ram_enable_notify (MigrationIncomingState *mis); + +/* * Initialise postcopy-ram, setting the RAM to a state where we can go into * postcopy later; must be called prior to any precopy. * called from arch_init's similarly named ram_postcopy_incoming_init
migration/postcopy-ram.c +68 -1
83 lines not on this page · raw message
savevm.c +9 -0
@@ -1271,6 +1271,15 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)return -1 ; } +/* +* Sensitise RAM - can now generate requests for blocks that don't exist +* However, at this point the CPU shouldn't be running, and the IO +* shouldn't be doing anything yet so don't actually expect requests +*/ +if (postcopy_ram_enable_notify (mis)) { +return -1 ; + } +/* TODO start up the postcopy listening thread */ return 0 ; }
Signature · 1 line
2.1.0
PATCH v6 32/47 Postcopy: Postcopy startup in migration thread
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Rework the migration thread to setup and start postcopy.
--- include/migration/migration.h | 3 + migration/migration.c | 163 ++++++++++++++++++++++++++++++++++++++++-- trace-events | 4 ++ 3 files changed, 165 insertions(+), 5 deletions(-)
include/migration/migration.h +3 -0
@@ -119,6 +119,9 @@ struct MigrationState/* Flag set once the migration has been asked to enter postcopy */ bool start_postcopy; +/* Flag set once the migration thread is running (and needs joining) */ +bool started_migration_thread; +/* bitmap of pages that have been sent at least once * only maintained and used in postcopy at the moment * where it's used to send the dirtymap at the start
migration/migration.c +158 -5
229 lines not on this page · raw message
trace-events +4 -0
@@ -1406,9 +1406,13 @@ migrate_fd_error(void) "" migrate_fd_cancel(void) "" migrate_pending(uint64_t size, uint64_t max, uint64_t post, uint64_t nonpost) "pending size %" PRIu64 " max %" PRIu64 " (post=%" PRIu64 " nonpost=%" PRIu64 ")" migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" +migration_thread_after_loop(void) "" +migration_thread_file_err(void) "" migration_thread_setup_complete(void) "" open_return_path_on_source(void) "" open_return_path_on_source_continue(void) "" +postcopy_start(void) "" +postcopy_start_set_run(void) "" source_return_path_thread_bad_end(void) "" source_return_path_thread_end(void) "" source_return_path_thread_entry(void) ""
Signature · 1 line
2.1.0
PATCH v6 33/47 Postcopy end in migration_thread
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The end of migration in postcopy is a bit different since some of the things normally done at the end of migration have already been done on the transition to postcopy.
The end of migration code is getting a bit complciated now, so move out into its own function.
--- migration/migration.c | 91 +++++++++++++++++++++++++++++++++++++-------------- trace-events | 6 ++++ 2 files changed, 72 insertions(+), 25 deletions(-)
migration/migration.c +66 -25
@@ -906,7 +906,6 @@ static int open_return_path_on_source(MigrationState *ms)return 0 ; } -__attribute__ (( unused ))/* Until later in patch series */ /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ static int await_return_path_close_on_source (MigrationState *ms) {
@@ -1024,6 +1023,68 @@ fail: }/* +* Used by migration_thread when there's not much left pending. +* The caller 'breaks' the loop when this returns. +*/ +static void migration_thread_end_of_iteration (MigrationState *s, +int current_active_state, +bool *old_vm_running, +int64_t *start_time) +{ +int ret; +if (s->state ==MIGRATION_STATUS_ACTIVE ) { +qemu_mutex_lock_iothread (); + *start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); + *old_vm_running =runstate_is_running (); + + ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); +if (ret >=0 ) { +qemu_file_set_rate_limit (s->file,INT64_MAX ); +qemu_savevm_state_complete_precopy (s->file); + } +qemu_mutex_unlock_iothread (); + +if (ret <0 ) { + goto fail; + } + }else if (s->state ==MIGRATION_STATUS_POSTCOPY_ACTIVE ) { +trace_migration_thread_end_of_iteration_postcopy_end (); + +qemu_savevm_state_complete_postcopy (s->file); +trace_migration_thread_end_of_iteration_postcopy_end_after_complete (); + } + +/* +* If rp was opened we must clean up the thread before +* cleaning everything else up (since if there are no failures +* it will wait for the destination to send it's status in +* a SHUT command). +* Postcopy opens rp if enabled (even if it's not avtivated) +*/ +if (migrate_postcopy_ram ()) { +int rp_error; +trace_migration_thread_end_of_iteration_postcopy_end_before_rp (); + rp_error =await_return_path_close_on_source (s); +trace_migration_thread_end_of_iteration_postcopy_end_after_rp (rp_error); +if (rp_error) { + goto fail; + } + } + +if (qemu_file_get_error (s->file)) { +trace_migration_thread_end_of_iteration_file_err (); + goto fail; + } + +migrate_set_state (s, current_active_state,MIGRATION_STATUS_COMPLETED ); +return ; + +fail: +migrate_set_state (s, current_active_state,MIGRATION_STATUS_FAILED ); +} + +/* * Master migration thread on the source VM. * It drives the migration and pumps the data down the outgoing channel. */
@@ -1098,31 +1159,11 @@ static void *migration_thread(void *opaque)/* Just another iteration step */ qemu_savevm_state_iterate (s->file); }else { -int ret; - -qemu_mutex_lock_iothread (); - start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); -qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); - old_vm_running =runstate_is_running (); - - ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); -if (ret >=0 ) { -qemu_file_set_rate_limit (s->file,INT64_MAX ); -qemu_savevm_state_complete_precopy (s->file); - } -qemu_mutex_unlock_iothread (); +trace_migration_thread_low_pending (pending_size); -if (ret <0 ) { -migrate_set_state (s,MIGRATION_STATUS_ACTIVE , -MIGRATION_STATUS_FAILED ); -break ; - } - -if (!qemu_file_get_error (s->file)) { -migrate_set_state (s,MIGRATION_STATUS_ACTIVE , -MIGRATION_STATUS_COMPLETED ); -break ; - } +migration_thread_end_of_iteration (s, current_active_type, + &old_vm_running, &start_time); +break ; } }
trace-events +6 -0
@@ -1409,6 +1409,12 @@ migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" migration_thread_after_loop(void) "" migration_thread_file_err(void) "" migration_thread_setup_complete(void) "" +migration_thread_low_pending(uint64_t pending) "%" PRIu64 +migration_thread_end_of_iteration_file_err(void) "" +migration_thread_end_of_iteration_postcopy_end(void) "" +migration_thread_end_of_iteration_postcopy_end_after_complete(void) "" +migration_thread_end_of_iteration_postcopy_end_before_rp(void) "" +migration_thread_end_of_iteration_postcopy_end_after_rp(int rp_error) "%d" open_return_path_on_source(void) "" open_return_path_on_source_continue(void) "" postcopy_start(void) ""
Signature · 1 line
2.1.0
PATCH v6 34/47 Page request: Add MIG_RP_MSG_REQ_PAGES reverse command
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add MIG_RP_MSG_REQ_PAGES command on Return path for the postcopy destination to request a page from the source.
--- include/migration/migration.h | 4 +++ migration/migration.c | 70 +++++++++++++++++++++++++++++++++++++++++++ trace-events | 1 + 3 files changed, 75 insertions(+)
include/migration/migration.h +4 -0
@@ -46,6 +46,8 @@ enum mig_rp_message_type {MIG_RP_MSG_INVALID =0 ,/* Must be 0 */ MIG_RP_MSG_SHUT ,/* sibling will not send any more RP messages */ MIG_RP_MSG_PONG ,/* Response to a PING; data (seq: be32 ) */ + + MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be64) */ }; typedef QLIST_HEAD(, LoadStateEntry) LoadStateEntry_Head;
@@ -236,6 +238,8 @@ void migrate_send_rp_shut(MigrationIncomingState *mis, uint32_t value);void migrate_send_rp_pong (MigrationIncomingState *mis,uint32_t value); +void migrate_send_rp_req_pages (MigrationIncomingState *mis,const char * rbname, +ram_addr_t start,ram_addr_t len);void ram_control_before_iterate (QEMUFile *f,uint64_t flags);void ram_control_after_iterate (QEMUFile *f,uint64_t flags);
migration/migration.c +70 -0
100 lines not on this page · raw message
trace-events +1 -0
@@ -1406,6 +1406,7 @@ migrate_fd_error(void) "" migrate_fd_cancel(void) "" migrate_pending(uint64_t size, uint64_t max, uint64_t post, uint64_t nonpost) "pending size %" PRIu64 " max %" PRIu64 " (post=%" PRIu64 " nonpost=%" PRIu64 ")" migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" +migrate_handle_rp_req_pages(const char *rbname, size_t start, size_t len) "in %s at %zx len %zx" migration_thread_after_loop(void) "" migration_thread_file_err(void) "" migration_thread_setup_complete(void) ""
Signature · 1 line
2.1.0
PATCH v6 35/47 Page request: Process incoming page request
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
On receiving MIG_RPCOMM_REQ_PAGES look up the address and queue the page.
--- arch_init.c | 64 ++++++++++++++++++++++++++++++++++++++++++- include/exec/cpu-all.h | 2 -- include/migration/migration.h | 21 ++++++++++++++ include/qemu/typedefs.h | 1 + migration/migration.c | 31 +++++++++++++++++++++ trace-events | 1 + 6 files changed, 117 insertions(+), 3 deletions(-)
arch_init.c +63 -1
@@ -715,7 +715,69 @@ static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset,return pages; } -/** +/* +* Queue the pages for transmission, e.g. a request from postcopy destination +* ms: MigrationStatus in which the queue is held +* rbname: The RAMBlock the request is for - may be NULL (to mean reuse last) +* start: Offset from the start of the RAMBlock +* len: Length (in bytes) to send +* Return: 0 on success +*/ +int ram_save_queue_pages (MigrationState *ms,const char *rbname, +ram_addr_t start,ram_addr_t len) +{ +RAMBlock *ramblock; + +rcu_read_lock (); +if (!rbname) { +/* Reuse last RAMBlock */ + ramblock = ms->last_req_rb; + +if (!ramblock) { +/* +* Shouldn't happen, we can't reuse the last RAMBlock if +* it's the 1st request. +*/ +error_report ("ram_save_queue_pages no previous block" ); + goto err; + } + }else { + ramblock =ram_find_block (rbname); + +if (!ramblock) { +/* We shouldn't be asked for a non-existent RAMBlock */ +error_report ("ram_save_queue_pages no block '%s'" , rbname); + goto err; + } + } +trace_ram_save_queue_pages (ramblock->idstr, start, len); +if (start+len > ramblock->used_length) { +error_report ("%s request overrun start=%zx len=%zx blocklen=%zx" , + __func__, start, len, ramblock->used_length); + goto err; + } + +struct MigrationSrcPageRequest *new_entry = +g_malloc0 (sizeof (struct MigrationSrcPageRequest )); + new_entry->rb = ramblock; + new_entry->offset = start; + new_entry->len = len; + ms->last_req_rb = ramblock; + +qemu_mutex_lock (&ms->src_page_req_mutex); +memory_region_ref (ramblock->mr); +QSIMPLEQ_INSERT_TAIL (&ms->src_page_requests, new_entry, next_req); +qemu_mutex_unlock (&ms->src_page_req_mutex); +rcu_read_unlock (); + +return 0 ; + +err: +rcu_read_unlock (); +return -1 ; +} + +/* * ram_find_and_save_block: Finds a dirty page and sends it to f * * Called within an RCU critical section.
include/exec/cpu-all.h +0 -2
@@ -266,8 +266,6 @@ CPUArchState *cpu_copy(CPUArchState *env);/* memory API */ -typedef struct RAMBlock RAMBlock ; -struct RAMBlock {struct rcu_head rcu;struct MemoryRegion *mr;
include/migration/migration.h +21 -0
@@ -88,6 +88,18 @@ MigrationIncomingState *migration_incoming_get_current(void);MigrationIncomingState *migration_incoming_state_new (QEMUFile *f);void migration_incoming_state_destroy (void ); +/* +* An outstanding page request, on the source, having been received +* and queued +*/ +struct MigrationSrcPageRequest { +RAMBlock *rb; +hwaddr offset; +hwaddr len; + +QSIMPLEQ_ENTRY (MigrationSrcPageRequest) next_req; +}; +struct MigrationState {int64_t bandwidth_limit;
@@ -130,6 +142,12 @@ struct MigrationState* of the postcopy phase */ unsigned long *sentmap; + +/* Queue of outstanding page requests from the destination */ +QemuMutex src_page_req_mutex; + QSIMPLEQ_HEAD(src_page_requests, MigrationSrcPageRequest) src_page_requests; +/* The RAMBlock used in the last src_page_request */ +RAMBlock *last_req_rb; };void process_incoming_migration (QEMUFile *f);
@@ -259,6 +277,9 @@ size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, ram_addr_t offset, size_t size, uint64_t *bytes_sent); +int ram_save_queue_pages (MigrationState *ms,const char *rbname, +ram_addr_t start,ram_addr_t len); +PostcopyState postcopy_state_get (MigrationIncomingState *mis);/* Set the state and return the old state */
include/qemu/typedefs.h +1 -0
@@ -72,6 +72,7 @@ typedef struct QEMUSGList QEMUSGList;typedef struct QEMUSizedBuffer QEMUSizedBuffer ;typedef struct QEMUTimerListGroup QEMUTimerListGroup ;typedef struct QEMUTimer QEMUTimer ; +typedef struct RAMBlock RAMBlock ;typedef struct Range Range ;typedef struct SerialState SerialState ;typedef struct SHPCDevice SHPCDevice ;
migration/migration.c +31 -0
@@ -26,6 +26,8 @@#include "qemu/thread.h" #include "qmp-commands.h" #include "trace.h" +#include "exec/memory.h" +#include "exec/address-spaces.h" #define MAX_THROTTLE (32 << 20)/* Migration speed throttling */
@@ -496,6 +498,15 @@ static void migrate_fd_cleanup(void *opaque)migrate_fd_cleanup_src_rp (s); +/* This queue generally should be empty - but in the case of a failed +* migration might have some droppings in. +*/ +struct MigrationSrcPageRequest *mspr, *next_mspr; + QSIMPLEQ_FOREACH_SAFE(mspr, &s->src_page_requests, next_req, next_mspr) { +QSIMPLEQ_REMOVE_HEAD (&s->src_page_requests, next_req); +g_free (mspr); + } +if (s->file) {trace_migrate_fd_cleanup ();qemu_mutex_unlock_iothread ();
@@ -614,6 +625,9 @@ MigrationState *migrate_init(const MigrationParams *params) s->state =MIGRATION_STATUS_SETUP ;trace_migrate_set_state (MIGRATION_STATUS_SETUP ); +qemu_mutex_init (&s->src_page_req_mutex); +QSIMPLEQ_INIT (&s->src_page_requests); + s->total_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );return s; }
@@ -842,6 +856,23 @@ static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, ram_addr_t start, ram_addr_t len) {trace_migrate_handle_rp_req_pages (rbname ,start ,len ); + +/* Round everything up to our host page size */ +long our_host_ps =getpagesize (); +if (start & (our_host_ps-1 )) { +long roundings = start & (our_host_ps-1 ); + start -= roundings; + len += roundings; + } +if (len & (our_host_ps-1 )) { +long roundings = len & (our_host_ps-1 ); + len -= roundings; + len += our_host_ps; + } + +if (ram_save_queue_pages (ms, rbname, start, len)) { +source_return_path_bad (ms); + } }/*
trace-events +1 -0
@@ -1220,6 +1220,7 @@ migration_bitmap_sync_start(void) "" migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64"" migration_throttle(void) "" ram_postcopy_send_discard_bitmap(void) "" +ram_save_queue_pages(const char *rbname, size_t start, size_t len) "%s: start: %zx len: %zx" # hw/display/qxl.c disable qxl_interface_set_mm_time(int qid, uint32_t mm_time) "%d %d"
Signature · 1 line
2.1.0
PATCH v6 36/47 Page request: Consume pages off the post-copy queue
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
When transmitting RAM pages, consume pages that have been queued by MIG_RPCOMM_REQPAGE commands and send them ahead of normal page scanning.
Note:
a) After a queued page the linear walk carries on from after the
unqueued page; there is a reasonable chance that the destination was about to ask for other closeby pages anyway.
b) We have to be careful of any assumptions that the page walking
code makes, in particular it does some short cuts on its first linear walk that break as soon as we do a queued page.
--- arch_init.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++---------- trace-events | 2 + 2 files changed, 132 insertions(+), 26 deletions(-)
arch_init.c +130 -26
@@ -312,6 +312,7 @@ static RAMBlock *last_seen_block;/* This is the last block from where we have sent data */ static RAMBlock *last_sent_block;static ram_addr_t last_offset; +static bool last_was_from_queue;static unsigned long *migration_bitmap;static uint64_t migration_dirty_pages;static uint32_t last_version;
@@ -490,6 +491,19 @@ static inline bool migration_bitmap_set_dirty(ram_addr_t addr)return ret; } +static inline bool migration_bitmap_clear_dirty (ram_addr_t addr) +{ +bool ret; +int nr = addr >>TARGET_PAGE_BITS ; + + ret =test_and_clear_bit (nr, migration_bitmap); + +if (ret) { + migration_dirty_pages--; + } +return ret; +} +static void migration_bitmap_sync_range (ram_addr_t start,ram_addr_t length) {ram_addr_t addr;
@@ -716,6 +730,40 @@ static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset, }/* +* Unqueue a page from the queue fed by postcopy page requests +* +* Returns: The RAMBlock* to transmit from (or NULL if the queue is empty) +* ms: MigrationState in +* offset: the byte offset within the RAMBlock for the start of the page +* ram_addr_abs: global offset in the dirty/sent bitmaps +*/ +static RAMBlock *ram_save_unqueue_page (MigrationState *ms,ram_addr_t *offset, +ram_addr_t *ram_addr_abs) +{ +RAMBlock *result =NULL ; +qemu_mutex_lock (&ms->src_page_req_mutex); +if (!QSIMPLEQ_EMPTY (&ms->src_page_requests)) { +struct MigrationSrcPageRequest *entry = +QSIMPLEQ_FIRST (&ms->src_page_requests); + result = entry->rb; + *offset = entry->offset; + *ram_addr_abs = (entry->offset + entry->rb->offset) &TARGET_PAGE_MASK ; + +if (entry->len >TARGET_PAGE_SIZE ) { + entry->len -=TARGET_PAGE_SIZE ; + entry->offset +=TARGET_PAGE_SIZE ; + }else { +memory_region_unref (result->mr); +QSIMPLEQ_REMOVE_HEAD (&ms->src_page_requests, next_req); +g_free (entry); + } + } +qemu_mutex_unlock (&ms->src_page_req_mutex); + +return result; +} + +/* * Queue the pages for transmission, e.g. a request from postcopy destination * ms: MigrationStatus in which the queue is held * rbname: The RAMBlock the request is for - may be NULL (to mean reuse last)
@@ -793,47 +841,102 @@ err:static int ram_find_and_save_block (QEMUFile *f,bool last_stage,uint64_t *bytes_transferred) { +MigrationState *ms =migrate_get_current ();RAMBlock *block = last_seen_block; +RAMBlock *tmpblock;ram_addr_t offset = last_offset; +ram_addr_t tmpoffset;bool complete_round = false;int pages =0 ; -MemoryRegion *mr;ram_addr_t dirty_ram_abs;/* Address of the start of the dirty page in ram_addr_t space */ +unsigned long hps =sysconf (_SC_PAGESIZE); -if (!block) +if (!block) { block =QLIST_FIRST_RCU (&ram_list.blocks); + last_was_from_queue = false; + } -while (true) { - mr = block->mr; - offset =migration_bitmap_find_and_reset_dirty (mr, offset, - &dirty_ram_abs); -if (complete_round && block == last_seen_block && - offset >= last_offset) { -break ; +while (true) {/* Until we send a block or run out of stuff to send */ + tmpblock =NULL ; + +/* +* Don't break host-page chunks up with queue items +* so only unqueue if, +* a) The last item came from the queue anyway +* b) The last sent item was the last target-page in a host page +*/ +if (last_was_from_queue || !last_sent_block || + ((last_offset & (hps -1 )) == (hps -TARGET_PAGE_SIZE ))) { + tmpblock =ram_save_unqueue_page (ms, &tmpoffset, &dirty_ram_abs); } -if (offset >= block->used_length) { - offset =0 ; - block =QLIST_NEXT_RCU (block, next); -if (!block) { - block =QLIST_FIRST_RCU (&ram_list.blocks); - complete_round = true; - ram_bulk_stage = false; + +if (tmpblock) { +/* We've got a block from the postcopy queue */ +trace_ram_find_and_save_block_postcopy (tmpblock->idstr, + (uint64_t )tmpoffset, + (uint64_t )dirty_ram_abs); +/* +* We're sending this page, and since it's postcopy nothing else +* will dirty it, and we must make sure it doesn't get sent again. +*/ +if (!migration_bitmap_clear_dirty (dirty_ram_abs)) { +trace_ram_find_and_save_block_postcopy_not_dirty ( + tmpblock->idstr, (uint64_t )tmpoffset, + (uint64_t )dirty_ram_abs, +test_bit (dirty_ram_abs >>TARGET_PAGE_BITS , ms->sentmap)); + +continue ; } +/* +* As soon as we start servicing pages out of order, then we have +* to kill the bulk stage, since the bulk stage assumes +* in (migration_bitmap_find_and_reset_dirty) that every page is +* dirty, that's no longer true. +*/ + ram_bulk_stage = false; +/* +* We mustn't change block/offset unless it's to a valid one +* otherwise we can go down some of the exit cases in the normal +* path. +*/ + block = tmpblock; + offset = tmpoffset; + last_was_from_queue = true; }else { - pages =ram_save_page (f, block, offset, last_stage, - bytes_transferred); - -/* if page is unmodified, continue to the next */ -if (pages >0 ) { -MigrationState *ms =migrate_get_current (); -if (ms->sentmap) { -set_bit (dirty_ram_abs >>TARGET_PAGE_BITS , ms->sentmap); +MemoryRegion *mr; +/* priority queue empty, so just search for something dirty */ + mr = block->mr; + offset =migration_bitmap_find_and_reset_dirty (mr, offset, + &dirty_ram_abs); +if (complete_round && block == last_seen_block && + offset >= last_offset) { +break ; + } +if (offset >= block->used_length) { + offset =0 ; + block =QLIST_NEXT_RCU (block, next); +if (!block) { + block =QLIST_FIRST_RCU (&ram_list.blocks); + complete_round = true; + ram_bulk_stage = false; } +continue ;/* pick an offset in the new block */ + } + last_was_from_queue = false; + } - last_sent_block = block; -break ; +/* We have a page to send, so send it */ + pages =ram_save_page (f, block, offset, last_stage, + bytes_transferred); + +/* if page is unmodified, continue to the next */ +if (pages >0 ) { +if (ms->sentmap) { +set_bit (dirty_ram_abs >>TARGET_PAGE_BITS , ms->sentmap); } + +break ; } }
@@ -929,6 +1032,7 @@ static void reset_ram_globals(void) last_offset =0 ; last_version = ram_list.version; ram_bulk_stage = true; + last_was_from_queue = false; }#define MAX_WAIT 50/* ms, half buffered_file limit */
trace-events +2 -0
@@ -1219,6 +1219,8 @@ qemu_file_fclose(void) "" migration_bitmap_sync_start(void) "" migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64"" migration_throttle(void) "" +ram_find_and_save_block_postcopy(const char *block_name, uint64_t tmp_offset, uint64_t ram_addr) "%s/%" PRIx64 " ram_addr=%" PRIx64 +ram_find_and_save_block_postcopy_not_dirty(const char *block_name, uint64_t tmp_offset, uint64_t ram_addr, int sent) "%s/%" PRIx64 " ram_addr=%" PRIx64 " (sent=%d)" ram_postcopy_send_discard_bitmap(void) "" ram_save_queue_pages(const char *rbname, size_t start, size_t len) "%s: start: %zx len: %zx"
Signature · 1 line
2.1.0
PATCH v6 37/47 postcopy_ram.c: place_page and helpers
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
postcopy_place_page (etc) provide a way for postcopy to place a page into guests memory atomically (using the copy ioctl on the ufd).
--- include/migration/migration.h | 1 + include/migration/postcopy-ram.h | 16 ++++++++ migration/postcopy-ram.c | 87 ++++++++++++++++++++++++++++++++++++++++ trace-events | 1 + 4 files changed, 105 insertions(+)
include/migration/migration.h +1 -0
@@ -82,6 +82,7 @@ struct MigrationIncomingState {int userfault_fd;QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ +void *postcopy_tmp_page; };MigrationIncomingState *migration_incoming_get_current (void );
include/migration/postcopy-ram.h +16 -0
@@ -69,4 +69,20 @@ void postcopy_discard_send_range(MigrationState *ms, PostcopyDiscardState *pds,void postcopy_discard_send_finish (MigrationState *ms,PostcopyDiscardState *pds); +/* +* Place a page (from) at (host) efficiently +* There are restrictions on how 'from' must be mapped, in general best +* to use other postcopy_ routines to allocate. +* returns 0 on success +*/ +int postcopy_place_page (MigrationIncomingState *mis,void *host,void *from, +bool all_zero); + +/* +* Allocate a page of memory that can be mapped at a later point in time +* using postcopy_place_page +* Returns: Pointer to allocated page +*/ +void *postcopy_get_tmp_page (MigrationIncomingState *mis); +#endif
migration/postcopy-ram.c +87 -0
105 lines not on this page · raw message
trace-events +1 -0
@@ -1499,6 +1499,7 @@ postcopy_discard_send_finish(const char *ramblock, int nwords, int ncmds) "%s ma postcopy_cleanup_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" postcopy_ram_discard_range(void *start, void *end) "%p,%p" postcopy_init_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" +postcopy_place_page(void *host_addr, bool all_zero) "host=%p all_zero=%d" # kvm-all.c kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
Signature · 1 line
2.1.0
PATCH v6 38/47 Postcopy: Use helpers to map pages during migration
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
In postcopy, the destination guest is running at the same time as it's receiving pages; as we receive new pages we must put them into the guests address space atomically to avoid a running CPU accessing a partially written page.
Use the helpers in postcopy-ram.c to map these pages.
qemu_get_buffer_less_copy is used to avoid a copy out of qemu_file in the case that postcopy is going to do a copy anyway.
--- arch_init.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 20 deletions(-)
arch_init.c +97 -20
@@ -1476,7 +1476,17 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)/* Must be called from within a rcu critical section. * Returns a pointer from within the RCU-protected ram_list. */ +/* +* Read a RAMBlock ID from the stream f, find the host address of the +* start of that block and add on 'offset' +* +* f: Stream to read from +* mis: MigrationIncomingState +* offset: Offset within the block +* flags: Page flags (mostly to see if it's a continuation of previous block) +*/ static inline void *host_from_stream_offset (QEMUFile *f, +MigrationIncomingState *mis,ram_addr_t offset,int flags) {
@@ -1489,7 +1499,6 @@ static inline void *host_from_stream_offset(QEMUFile *f,error_report ("Ack, bad migration stream!" );return NULL ; } -return memory_region_get_ram_ptr (block->mr) + offset; }
@@ -1534,6 +1543,16 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) {int flags =0 , ret =0 ;static uint64_t seq_iter; +/* +* System is running in postcopy mode, page inserts to host memory must be +* atomic +*/ +MigrationIncomingState *mis =migration_incoming_get_current (); +bool postcopy_running =postcopy_state_get (mis) >= +POSTCOPY_INCOMING_LISTENING ; +void *postcopy_host_page =NULL ; +bool postcopy_place_needed = false; +bool matching_page_sizes = qemu_host_page_size ==TARGET_PAGE_SIZE ; seq_iter++;
@@ -1549,13 +1568,57 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)rcu_read_lock ();while (!ret && !(flags &RAM_SAVE_FLAG_EOS )) {ram_addr_t addr, total_ram_bytes; -void *host; +void *host =0 ; +void *page_buffer =0 ; +void *postcopy_place_source =0 ;uint8_t ch; +bool all_zero = false; addr =qemu_get_be64 (f); flags = addr & ~TARGET_PAGE_MASK ; addr &=TARGET_PAGE_MASK ; +if (flags & (RAM_SAVE_FLAG_COMPRESS |RAM_SAVE_FLAG_PAGE | +RAM_SAVE_FLAG_XBZRLE )) { + host =host_from_stream_offset (f, mis, addr, flags); +if (!host) { +error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); + ret = -EINVAL ; +break ; + } +if (!postcopy_running) { + page_buffer = host; + }else { +/* +* Postcopy requires that we place whole host pages atomically. +* To make it atomic, the data is read into a temporary page +* that's moved into place later. +* The migration protocol uses, possibly smaller, target-pages +* however the source ensures it always sends all the components +* of a host page in order. +*/ +if (!postcopy_host_page) { + postcopy_host_page =postcopy_get_tmp_page (mis); + } + page_buffer = postcopy_host_page + + ((uintptr_t )host & ~qemu_host_page_mask); +/* If all TP are zero then we can optimise the place */ +if (!((uintptr_t )host & ~qemu_host_page_mask)) { + all_zero = true; + } + +/* +* If it's the last part of a host page then we place the host +* page +*/ + postcopy_place_needed = (((uintptr_t )host +TARGET_PAGE_SIZE ) & + ~qemu_host_page_mask) ==0 ; + postcopy_place_source = postcopy_host_page; + } + }else { + postcopy_place_needed = false; + } +switch (flags & ~RAM_SAVE_FLAG_CONTINUE ) {case RAM_SAVE_FLAG_MEM_SIZE :/* Synchronize RAM block list */
@@ -1592,30 +1655,36 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) }break ;case RAM_SAVE_FLAG_COMPRESS : - host =host_from_stream_offset (f, addr, flags); -if (!host) { -error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); - ret = -EINVAL ; -break ; - } ch =qemu_get_byte (f); -ram_handle_compressed (host, ch,TARGET_PAGE_SIZE ); +if (!postcopy_running) { +ram_handle_compressed (host, ch,TARGET_PAGE_SIZE ); + }else { +memset (page_buffer, ch,TARGET_PAGE_SIZE ); +if (ch) { + all_zero = false; + } + }break ; +case RAM_SAVE_FLAG_PAGE : - host =host_from_stream_offset (f, addr, flags); -if (!host) { -error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); - ret = -EINVAL ; -break ; + all_zero = false; +if (!postcopy_place_needed || !matching_page_sizes) { +qemu_get_buffer (f, page_buffer,TARGET_PAGE_SIZE ); + }else { +/* Avoids the qemu_file copy during postcopy, which is +* going to do a copy later; can only do it when we +* do this read in one go (matching page sizes) +*/ +qemu_get_buffer_less_copy (f, (uint8_t **)&postcopy_place_source, +TARGET_PAGE_SIZE ); } -qemu_get_buffer (f, host,TARGET_PAGE_SIZE );break ; +case RAM_SAVE_FLAG_XBZRLE : - host =host_from_stream_offset (f, addr, flags); -if (!host) { -error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); - ret = -EINVAL ; -break ; + all_zero = false; +if (postcopy_running) { +error_report ("XBZRLE RAM block in postcopy mode @%zx\n" , addr); +return -EINVAL ; }if (load_xbzrle (f, addr, host) <0 ) { error_report("Failed to decompress XBZRLE page at "
@@ -1636,6 +1705,14 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) ret = -EINVAL ; } } + +if (postcopy_place_needed) { +/* This gets called at the last target page in the host page */ + ret =postcopy_place_page (mis, host +TARGET_PAGE_SIZE - + qemu_host_page_size, + postcopy_place_source, + all_zero); + }if (!ret) { ret =qemu_file_get_error (f); }
Signature · 1 line
2.1.0
PATCH v6 39/47 qemu_ram_block_from_host
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy sends RAMBlock names and offsets over the wire (since it can't rely on the order of ramaddr being the same), and it starts out with HVA fault addresses from the kernel.
qemu_ram_block_from_host translates a HVA into a RAMBlock, an offset in the RAMBlock and the global ram_addr_t value.
Rewrite qemu_ram_addr_from_host to use qemu_ram_block_from_host.
Provide qemu_ram_get_idstr since its the actual name text sent on the wire.
--- exec.c | 54 +++++++++++++++++++++++++++++++++++++++-------- include/exec/cpu-common.h | 3 +++ 2 files changed, 48 insertions(+), 9 deletions(-)
exec.c +45 -9
@@ -1280,6 +1280,11 @@ static RAMBlock *find_ram_block(ram_addr_t addr)return NULL ; } +const char *qemu_ram_get_idstr (RAMBlock *rb) +{ +return rb->idstr; +} +/* Called with iothread lock held. */ void qemu_ram_set_idstr (ram_addr_t addr,const char *name,DeviceState *dev) {
@@ -1768,8 +1773,16 @@ static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size) } } -/* Some of the softmmu routines need to translate from a host pointer -* (typically a TLB entry) back to a ram offset. +/* +* Translates a host ptr back to a RAMBlock, a ram_addr and an offset +* in that RAMBlock. +* +* ptr: Host pointer to look up +* round_offset: If true round the result offset down to a page boundary +* *ram_addr: set to result ram_addr +* *offset: set to result offset within the RAMBlock +* +* Returns: RAMBlock (or NULL if not found) * * By the time this function returns, the returned pointer is not protected * by RCU anymore. If the caller is not within an RCU critical section and
@@ -1777,18 +1790,22 @@ static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size)* pointer, such as a reference to the region that includes the incoming * ram_addr_t. */ -MemoryRegion *qemu_ram_addr_from_host (void *ptr,ram_addr_t *ram_addr) +RAMBlock *qemu_ram_block_from_host (void *ptr,bool round_offset, +ram_addr_t *ram_addr, +ram_addr_t *offset) {RAMBlock *block;uint8_t *host = ptr; -MemoryRegion *mr;if (xen_enabled ()) {rcu_read_lock (); *ram_addr =xen_ram_addr_from_mapcache (ptr); - mr =qemu_get_ram_block (*ram_addr)->mr; + block =qemu_get_ram_block (*ram_addr); +if (block) { + *offset = (host - block->host); + }rcu_read_unlock (); -return mr; +return block; }rcu_read_lock ();
@@ -1811,10 +1828,29 @@ MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)return NULL ; found: - *ram_addr = block->offset + (host - block->host); - mr = block->mr; + *offset = (host - block->host); +if (round_offset) { + *offset &=TARGET_PAGE_MASK ; + } + *ram_addr = block->offset + *offset;rcu_read_unlock (); -return mr; +return block; +} + +/* Some of the softmmu routines need to translate from a host pointer +(typically a TLB entry) back to a ram offset. */ +MemoryRegion *qemu_ram_addr_from_host (void *ptr,ram_addr_t *ram_addr) +{ +RAMBlock *block; +ram_addr_t offset;/* Not used */ + + block =qemu_ram_block_from_host (ptr, false, ram_addr, &offset); + +if (!block) { +return NULL ; + } + +return block->mr; } static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
include/exec/cpu-common.h +3 -0
@@ -62,8 +62,11 @@ typedef uint32_t CPUReadMemoryFunc(void *opaque, hwaddr addr);void qemu_ram_remap (ram_addr_t addr,ram_addr_t length);/* This should not be used by devices. */ MemoryRegion *qemu_ram_addr_from_host (void *ptr,ram_addr_t *ram_addr); +RAMBlock *qemu_ram_block_from_host (void *ptr,bool round_offset, +ram_addr_t *ram_addr,ram_addr_t *offset);void qemu_ram_set_idstr (ram_addr_t addr,const char *name,DeviceState *dev);void qemu_ram_unset_idstr (ram_addr_t addr); +const char *qemu_ram_get_idstr (RAMBlock *rb);void cpu_physical_memory_rw (hwaddr addr,uint8_t *buf,int len,int is_write);
Signature · 1 line
2.1.0
PATCH v6 40/47 Don't sync dirty bitmaps in postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Once we're in postcopy the source processors are stopped and memory shouldn't change any more, so there's no need to look at the dirty map.
There are two notes to this:
1) If we do resync and a page had changed then the page would get sent again, which the destination wouldn't allow (since it might have also modified the page)
2) Before disabling this I'd seen very rare cases where a page had been marked dirtied although the memory contents are apparently identical
--- arch_init.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch_init.c +5 -2
@@ -1391,7 +1391,9 @@ static int ram_save_complete(QEMUFile *f, void *opaque) {rcu_read_lock (); -migration_bitmap_sync (); +if (!migration_postcopy_phase (migrate_get_current ())) { +migration_bitmap_sync (); + }ram_control_before_iterate (f,RAM_CONTROL_FINISH );
@@ -1425,7 +1427,8 @@ static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, remaining_size =ram_save_remaining () *TARGET_PAGE_SIZE ; -if (remaining_size < max_size) { +if (!migration_postcopy_phase (migrate_get_current ()) && + remaining_size < max_size) {qemu_mutex_lock_iothread ();rcu_read_lock ();migration_bitmap_sync ();
Signature · 1 line
2.1.0
PATCH v6 41/47 Host page!=target page: Cleanup bitmaps
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Prior to the start of postcopy, ensure that everything that will be transferred later is a whole host-page in size.
This is accomplished by discarding partially transferred host pages and marking any that are partially dirty as fully dirty.
--- arch_init.c | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 269 insertions(+), 2 deletions(-)
arch_init.c +269 -2
@@ -850,7 +850,6 @@ static int ram_find_and_save_block(QEMUFile *f, bool last_stage,int pages =0 ;ram_addr_t dirty_ram_abs;/* Address of the start of the dirty page in ram_addr_t space */ -unsigned long hps =sysconf (_SC_PAGESIZE);if (!block) { block =QLIST_FIRST_RCU (&ram_list.blocks);
@@ -867,7 +866,8 @@ static int ram_find_and_save_block(QEMUFile *f, bool last_stage,* b) The last sent item was the last target-page in a host page */ if (last_was_from_queue || !last_sent_block || - ((last_offset & (hps -1 )) == (hps -TARGET_PAGE_SIZE ))) { + ((last_offset & ~qemu_host_page_mask) == + (qemu_host_page_size -TARGET_PAGE_SIZE ))) { tmpblock =ram_save_unqueue_page (ms, &tmpoffset, &dirty_ram_abs); }
@@ -1152,6 +1152,265 @@ static int postcopy_each_ram_send_discard(MigrationState *ms) }/* +* Helper for postcopy_chunk_hostpages where HPS/TPS >= bits-in-long +* +* !! Untested !! +*/ +static int hostpage_big_chunk_helper (const char *block_name,void *host_addr, +ram_addr_t offset,ram_addr_t length, +void *opaque) +{ +MigrationState *ms = opaque; +unsigned long long_bits =sizeof (long ) *8 ; +unsigned int host_len = (qemu_host_page_size /TARGET_PAGE_SIZE ) / + long_bits; +unsigned long first_long, last_long, cur_long, current_hp; +unsigned long first = offset >>TARGET_PAGE_BITS ; +unsigned long last = (offset + (length -1 )) >>TARGET_PAGE_BITS ; + +PostcopyDiscardState *pds =postcopy_discard_send_init (ms, + first, + block_name); + first_long = first / long_bits; + last_long = last / long_bits; + +/* +* I'm assuming RAMBlocks must start at the start of host pages, +* but I guess they might not use the whole of the host page +*/ + +/* Work along one host page at a time */ +for (current_hp = first_long; current_hp <= last_long; + current_hp += host_len) { +bool discard =0 ; +bool redirty =0 ; +bool has_some_dirty = false; +bool has_some_undirty = false; +bool has_some_sent = false; +bool has_some_unsent = false; + +/* +* Check each long of mask for this hp, and see if anything +* needs updating. +*/ +for (cur_long = current_hp; cur_long < (current_hp + host_len); + cur_long++) { +/* a chunk of sent pages */ +unsigned long sdata = ms->sentmap[cur_long]; +/* a chunk of dirty pages */ +unsigned long ddata = migration_bitmap[cur_long]; + +if (sdata) { + has_some_sent = true; + } +if (sdata != ~0ul ) { + has_some_unsent = true; + } +if (ddata) { + has_some_dirty = true; + } +if (ddata != ~0ul ) { + has_some_undirty = true; + } + + } + +if (has_some_sent && has_some_unsent) { +/* Partially sent host page */ + discard = true; + redirty = true; + } + +if (has_some_dirty && has_some_undirty) { +/* Partially dirty host page */ + redirty = true; + } + +if (!discard && !redirty) { +/* All consistent - next host page */ +continue ; + } + + +/* Now walk the chunks again, sending discards etc */ +for (cur_long = current_hp; cur_long < (current_hp + host_len); + cur_long++) { +unsigned long cur_bits = cur_long * long_bits; + +/* a chunk of sent pages */ +unsigned long sdata = ms->sentmap[cur_long]; +/* a chunk of dirty pages */ +unsigned long ddata = migration_bitmap[cur_long]; + +if (discard && sdata) { +/* Tell the destination to discard these pages */ +postcopy_discard_send_range (ms, pds, cur_bits, + cur_bits + long_bits -1 ); +/* And clear them in the sent data structure */ + ms->sentmap[cur_long] =0 ; + } + +if (redirty) { + migration_bitmap[cur_long] = ~0ul ; +/* Inc the count of dirty pages */ + migration_dirty_pages +=ctpopl (~ddata); + } + } + } + +postcopy_discard_send_finish (ms, pds); + +return 0 ; +} + +/* +* When working on long chunks of a bitmap where the only valid section +* is between start..end (inclusive), generate a mask with only those +* valid bits set for the current long word within that bitmask. +*/ +static unsigned long make_long_mask (unsigned long start,unsigned long end, +unsigned long cur_long) +{ +unsigned long long_bits =sizeof (long ) *8 ; +unsigned long long_bits_mask = long_bits -1 ; +unsigned long first_long, last_long; +unsigned long mask = ~(unsigned long )0 ; + first_long = start / long_bits ; + last_long = end / long_bits; + +if ((cur_long == first_long) && (start & long_bits_mask)) { +/* e.g. (start & 31) = 3 +* 1 << . -> 2^3 +* . - 1 -> 2^3 - 1 i.e. mask 2..0 +* ~. -> mask 31..3 +*/ + mask &= ~((((unsigned long )1 ) << (start & long_bits_mask)) -1 ); + } + +if ((cur_long == last_long) && ((end & long_bits_mask) != long_bits_mask)) { +/* e.g. (end & 31) = 3 +* . +1 -> 4 +* 1 << . -> 2^4 +* . -1 -> 2^4 - 1 +* = mask set 3..0 +*/ + mask &= (((unsigned long )1 ) << ((end & long_bits_mask) +1 )) -1 ; + } + +return mask; +} + +/* +* Utility for the outgoing postcopy code. +* +* Discard any partially sent host-page size chunks, mark any partially +* dirty host-page size chunks as all dirty. +* +* Returns: 0 on success +*/ +static int postcopy_chunk_hostpages (MigrationState *ms) +{ +struct RAMBlock *block; +unsigned int host_bits = qemu_host_page_size /TARGET_PAGE_SIZE ; +unsigned long long_bits =sizeof (long ) *8 ; +unsigned long host_mask; + +assert (is_power_of_2 (host_bits)); + +if (qemu_host_page_size ==TARGET_PAGE_SIZE ) { +/* Easy case - TPS==HPS - nothing to be done */ +return 0 ; + } + +/* Easiest way to make sure we don't resume in the middle of a host-page */ + last_seen_block =NULL ; + last_sent_block =NULL ; + +/* +* The currently worst known ratio is ARM that has 1kB target pages, and +* can have 64kB host pages, which is thus inconveniently larger than a long +* on ARM (32bits), and a long is the underlying element of the migration +* bitmaps. +*/ +if (host_bits >= long_bits) { +/* Deal with the odd case separately */ +return qemu_ram_foreach_block (hostpage_big_chunk_helper, ms); + }else { + host_mask = (1ul << host_bits) -1 ; + } + +rcu_read_lock (); + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { +unsigned long first_long, last_long, cur_long; +unsigned long first = block->offset >>TARGET_PAGE_BITS ; +unsigned long last = (block->offset + (block->used_length -1 )) + >>TARGET_PAGE_BITS ; +PostcopyDiscardState *pds =postcopy_discard_send_init (ms, + first, + block->idstr); + + first_long = first / long_bits; + last_long = last / long_bits; +for (cur_long = first_long; cur_long <= last_long; cur_long++) { +unsigned long current_hp; +/* Deal with start/end not on alignment */ +unsigned long mask =make_long_mask (first, last, cur_long); + +/* a chunk of sent pages */ +unsigned long sdata = ms->sentmap[cur_long]; +/* a chunk of dirty pages */ +unsigned long ddata = migration_bitmap[cur_long]; +unsigned long discard =0 ; +unsigned long redirty =0 ; + sdata &= mask; + ddata &= mask; + +for (current_hp =0 ; current_hp < long_bits; + current_hp += host_bits) { +unsigned long host_sent = (sdata >> current_hp) & host_mask; +unsigned long host_dirty = (ddata >> current_hp) & host_mask; + +if (host_sent && (host_sent != host_mask)) { +/* Partially sent host page */ + redirty |= host_mask << current_hp; + discard |= host_mask << current_hp; + +/* Tell the destination to discard this page */ +postcopy_discard_send_range (ms, pds, + cur_long * long_bits + current_hp/* start */ , + cur_long * long_bits + current_hp + + host_bits -1 /* end */ ); + }else if (host_dirty && (host_dirty != host_mask)) { +/* Partially dirty host page */ + redirty |= host_mask << current_hp; + } + } +if (discard) { +/* clear the page in the sentmap */ + ms->sentmap[cur_long] &= ~discard; + } +if (redirty) { +/* +* Reread original dirty bits and OR in ones we clear; we +* must reread since we might be at the start or end of +* a RAMBlock that the original 'mask' discarded some +* bits from +*/ + ddata = migration_bitmap[cur_long]; + migration_bitmap[cur_long] = ddata | redirty; +/* Inc the count of dirty pages */ + migration_dirty_pages +=ctpopl (redirty - (ddata & redirty)); + } + } + +postcopy_discard_send_finish (ms, pds); + } + +rcu_read_unlock (); +return 0 ; +} + +/* * Transmit the set of pages to be discarded after precopy to the target * these are pages that have been sent previously but have been dirtied * Hopefully this is pretty sparse
@@ -1161,9 +1420,17 @@ int ram_postcopy_send_discard_bitmap(MigrationState *ms)int ret;rcu_read_lock (); +/* This should be our last sync, the src is now paused */ migration_bitmap_sync (); +/* Deal with TPS != HPS */ + ret =postcopy_chunk_hostpages (ms); +if (ret) { +rcu_read_unlock (); +return ret; + } +/* * Update the sentmap to be sentmap&=dirty */
Signature · 1 line
2.1.0
PATCH v6 42/47 Postcopy; Handle userfault requests
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
userfaultfd is a Linux syscall that gives an fd that receives a stream of notifications of accesses to pages registered with it and allows the program to acknowledge those stalls and tell the accessing thread to carry on.
--- include/migration/migration.h | 4 + migration/postcopy-ram.c | 165 +++++++++++++++++++++++++++++++++++++++--- trace-events | 9 +++ 3 files changed, 169 insertions(+), 9 deletions(-)
include/migration/migration.h +4 -0
@@ -75,11 +75,15 @@ struct MigrationIncomingState {*/ QemuEvent main_thread_load_event; +bool have_fault_thread;QemuThread fault_thread;QemuSemaphore fault_thread_sem;/* For the kernel to send us notifications */ int userfault_fd; +/* To tell the fault_thread to quit */ +int userfault_quit_fd; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ void *postcopy_tmp_page;
migration/postcopy-ram.c +156 -9
208 lines not on this page · raw message
trace-events +9 -0
@@ -1500,6 +1500,15 @@ postcopy_cleanup_area(const char *ramblock, void *host_addr, size_t offset, size postcopy_ram_discard_range(void *start, void *end) "%p,%p" postcopy_init_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" postcopy_place_page(void *host_addr, bool all_zero) "host=%p all_zero=%d" +postcopy_ram_enable_notify(void) "" +postcopy_ram_fault_thread_entry(void) "" +postcopy_ram_fault_thread_exit(void) "" +postcopy_ram_fault_thread_quit(void) "" +postcopy_ram_fault_thread_request(uint64_t hostaddr, const char *ramblock, size_t offset) "Request for HVA=%" PRIx64 " rb=%s offset=%zx" +postcopy_ram_incoming_cleanup_closeuf(void) "" +postcopy_ram_incoming_cleanup_entry(void) "" +postcopy_ram_incoming_cleanup_exit(void) "" +postcopy_ram_incoming_cleanup_join(void) "" # kvm-all.c kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v6 42/47] Postcopy; Handle userfault requests
Quoted 110 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
userfaultfd is a Linux syscall that gives an fd that receives a stream of notifications of accesses to pages registered with it and allows the program to acknowledge those stalls and tell the accessing thread to carry on.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/migration.h | 4 + migration/postcopy-ram.c | 165 +++++++++++++++++++++++++++++++++++++++--- trace-events | 9 +++ 3 files changed, 169 insertions(+), 9 deletions(-)include/migration/migration.h +4
-0@@ -75,11 +75,15 @@ struct MigrationIncomingState {*/ QemuEvent main_thread_load_event; +bool have_fault_thread;QemuThread fault_thread;QemuSemaphore fault_thread_sem;/* For the kernel to send us notifications */ int userfault_fd; +/* To tell the fault_thread to quit */ +int userfault_quit_fd; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ void *postcopy_tmp_page;migration/postcopy-ram.c +39
-8@@ -49,6 +49,8 @@ struct PostcopyDiscardState {*/ #if defined(__linux__) +#include <poll.h> +#include <sys/eventfd.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <sys/syscall.h> @@ -273,15 +275,41 @@ int postcopy_ram_incoming_init(MigrationIncomingState *mis, size_t ram_pages)*/ int postcopy_ram_incoming_cleanup (MigrationIncomingState *mis) { -/* TODO: Join the fault thread once we're sure it will exit */ -if (qemu_ram_foreach_block (cleanup_area, mis)) { -return -1 ; +trace_postcopy_ram_incoming_cleanup_entry (); + +if (mis->have_fault_thread) { +uint64_t tmp64; + +if (qemu_ram_foreach_block (cleanup_area, mis)) { +return -1 ; + } +/* +* Tell the fault_thread to exit, it's an eventfd that should +* currently be at 0, we're going to inc it to 1 +*/ + tmp64 =1 ; +if (write (mis->userfault_quit_fd, &tmp64,8 ) ==8 ) { +trace_postcopy_ram_incoming_cleanup_join (); +qemu_thread_join (&mis->fault_thread); + }else { +/* Not much we can do here, but may as well report it */ +error_report ("%s: incing userfault_quit_fd: %s" , __func__, +strerror (errno)); + } +trace_postcopy_ram_incoming_cleanup_closeuf (); +close (mis->userfault_fd); +close (mis->userfault_quit_fd); + mis->have_fault_thread = false; } +postcopy_state_set (mis,POSTCOPY_INCOMING_END ); +migrate_send_rp_shut (mis,qemu_file_get_error (mis->file) !=0 ); +if (mis->postcopy_tmp_page) {munmap (mis->postcopy_tmp_page,getpagesize ()); mis->postcopy_tmp_page =NULL ; } +trace_postcopy_ram_incoming_cleanup_exit ();return 0 ; }@@ -320,31 +348,150 @@ static int ram_block_enable_notify(const char *block_name, void *host_addr,static void *postcopy_ram_fault_thread (void *opaque) {MigrationIncomingState *mis = (MigrationIncomingState *)opaque; - -fprintf (stderr,"postcopy_ram_fault_thread\n" ); -/* TODO: In later patch */ +uint64_t hostaddr;/* The kernel always gives us 64 bit, not a pointer */ +int ret; +size_t hostpagesize =getpagesize (); +RAMBlock *rb =NULL ; +RAMBlock *last_rb =NULL ;/* last RAMBlock we sent part of */ +uint8_t *local_tmp_page; + +trace_postcopy_ram_fault_thread_entry ();qemu_sem_post (&mis->fault_thread_sem); -while (1 ) { -/* TODO: In later patch */
migration/postcopy-ram.c static int ram_block_enable_notify(const char *block_name, void *host_addr, · line 369
Quoted 10 lines
migration/postcopy-ram.c +9
-0+ + local_tmp_page =mmap (NULL ,getpagesize (), +PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS , +-1 ,0 ); +if (!local_tmp_page) { +error_report ("%s mapping local tmp page: %s" , __func__, +strerror (errno)); +return NULL ; } +if (madvise (local_tmp_page,getpagesize (),MADV_DONTFORK )) {
What's this 'local_tmp_page' used for ? I don't find where it is used in this function. Besides, there is a helper function qemu_madvise() in qemu, maybe you should use it :)
Thanks.
Quoted 150 lines
migration/postcopy-ram.c +108
-1+munmap (local_tmp_page,getpagesize ()); +error_report ("%s postcopy local page DONTFORK: %s" , __func__, +strerror (errno)); +return NULL ; + } + +while (true) { +ram_addr_t rb_offset; +ram_addr_t in_raspace; +struct pollfd pfd[2 ]; + +/* +* We're mainly waiting for the kernel to give us a faulting HVA, +* however we can be told to quit via userfault_quit_fd which is +* an eventfd +*/ + pfd[0 ].fd = mis->userfault_fd; + pfd[0 ].events =POLLIN ; + pfd[0 ].revents =0 ; + pfd[1 ].fd = mis->userfault_quit_fd; + pfd[1 ].events =POLLIN ;/* Waiting for eventfd to go positive */ + pfd[1 ].revents =0 ; + +if (poll (pfd,2 ,-1 /* Wait forever */ ) ==-1 ) { +error_report ("%s: userfault poll: %s" , __func__,strerror (errno)); +break ; + } + +if (pfd[1 ].revents) { +trace_postcopy_ram_fault_thread_quit (); +break ; + } + + ret =read (mis->userfault_fd, &hostaddr,sizeof (hostaddr)); +if (ret !=sizeof (hostaddr)) { +if (errno ==EAGAIN ) { +/* +* if a wake up happens on the other thread just after +* the poll, there is nothing to read. +*/ +continue ; + } +if (ret <0 ) { +error_report ("%s: Failed to read full userfault hostaddr: %s" , + __func__,strerror (errno)); +break ; + }else { +error_report ("%s: Read %d bytes from userfaultfd expected %zd" , + __func__, ret,sizeof (hostaddr)); +break ;/* Lost alignment, don't know what we'd read next */ + } + } + rb =qemu_ram_block_from_host ((void *)(uintptr_t )hostaddr, true, + &in_raspace, &rb_offset); +if (!rb) { +error_report ("postcopy_ram_fault_thread: Fault outside guest: %" + PRIx64, hostaddr); +break ; + } + +trace_postcopy_ram_fault_thread_request (hostaddr, +qemu_ram_get_idstr (rb), + rb_offset); + +/* +* Send the request to the source - we want to request one +* of our host page sizes (which is >= TPS) +*/ +if (rb != last_rb) { + last_rb = rb; +migrate_send_rp_req_pages (mis,qemu_ram_get_idstr (rb), + rb_offset, hostpagesize); + }else { +/* Save some space */ +migrate_send_rp_req_pages (mis,NULL , + rb_offset, hostpagesize); + } + } +munmap (local_tmp_page,getpagesize ()); +trace_postcopy_ram_fault_thread_exit ();return NULL ; }int postcopy_ram_enable_notify (MigrationIncomingState *mis) { -/* Create the fault handler thread and wait for it to be ready */ +/* Open the fd for the kernel to give us userfaults */ + mis->userfault_fd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (mis->userfault_fd ==-1 ) { +error_report ("%s: Failed to open userfault fd: %s" , __func__, +strerror (errno)); +return -1 ; + } + +/* +* Although the host check already tested the API, we need to +* do the check again as an ABI handshake on the new fd. +*/ +if (!ufd_version_check (mis->userfault_fd)) { +return -1 ; + } + +/* Now an eventfd we use to tell the fault-thread to quit */ + mis->userfault_quit_fd =eventfd (0 ,EFD_CLOEXEC ); +if (mis->userfault_quit_fd ==-1 ) { +error_report ("%s: Opening userfault_quit_fd: %s" , __func__, +strerror (errno)); +close (mis->userfault_fd); +return -1 ; + } +qemu_sem_init (&mis->fault_thread_sem,0 );qemu_thread_create (&mis->fault_thread,"postcopy/fault" , postcopy_ram_fault_thread, mis,QEMU_THREAD_JOINABLE );qemu_sem_wait (&mis->fault_thread_sem);qemu_sem_destroy (&mis->fault_thread_sem); + mis->have_fault_thread = true;/* Mark so that we get notified of accesses to unwritten areas */ if (qemu_ram_foreach_block (ram_block_enable_notify, mis)) {return -1 ; } +trace_postcopy_ram_enable_notify (); +return 0 ; }trace-events +9
-0@@ -1500,6 +1500,15 @@ postcopy_cleanup_area(const char *ramblock, void *host_addr, size_t offset, size postcopy_ram_discard_range(void *start, void *end) "%p,%p" postcopy_init_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" postcopy_place_page(void *host_addr, bool all_zero) "host=%p all_zero=%d" +postcopy_ram_enable_notify(void) "" +postcopy_ram_fault_thread_entry(void) "" +postcopy_ram_fault_thread_exit(void) "" +postcopy_ram_fault_thread_quit(void) "" +postcopy_ram_fault_thread_request(uint64_t hostaddr, const char *ramblock, size_t offset) "Request for HVA=%" PRIx64 " rb=%s offset=%zx" +postcopy_ram_incoming_cleanup_closeuf(void) "" +postcopy_ram_incoming_cleanup_entry(void) "" +postcopy_ram_incoming_cleanup_exit(void) "" +postcopy_ram_incoming_cleanup_join(void) "" # kvm-all.c kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
Re: [Qemu-devel] [PATCH v6 42/47] Postcopy; Handle userfault requests
Quoted 115 lines · * zhanghailiang ([email protected]) wrote:
Quoted 114 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
userfaultfd is a Linux syscall that gives an fd that receives a stream of notifications of accesses to pages registered with it and allows the program to acknowledge those stalls and tell the accessing thread to carry on.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/migration.h | 4 + migration/postcopy-ram.c | 165 +++++++++++++++++++++++++++++++++++++++--- trace-events | 9 +++ 3 files changed, 169 insertions(+), 9 deletions(-)include/migration/migration.h +4
-0@@ -75,11 +75,15 @@ struct MigrationIncomingState {*/ QemuEvent main_thread_load_event; +bool have_fault_thread;QemuThread fault_thread;QemuSemaphore fault_thread_sem;/* For the kernel to send us notifications */ int userfault_fd; +/* To tell the fault_thread to quit */ +int userfault_quit_fd; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ void *postcopy_tmp_page;migration/postcopy-ram.c +43
-8@@ -49,6 +49,8 @@ struct PostcopyDiscardState {*/ #if defined(__linux__) +#include <poll.h> +#include <sys/eventfd.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <sys/syscall.h> @@ -273,15 +275,41 @@ int postcopy_ram_incoming_init(MigrationIncomingState *mis, size_t ram_pages)*/ int postcopy_ram_incoming_cleanup (MigrationIncomingState *mis) { -/* TODO: Join the fault thread once we're sure it will exit */ -if (qemu_ram_foreach_block (cleanup_area, mis)) { -return -1 ; +trace_postcopy_ram_incoming_cleanup_entry (); + +if (mis->have_fault_thread) { +uint64_t tmp64; + +if (qemu_ram_foreach_block (cleanup_area, mis)) { +return -1 ; + } +/* +* Tell the fault_thread to exit, it's an eventfd that should +* currently be at 0, we're going to inc it to 1 +*/ + tmp64 =1 ; +if (write (mis->userfault_quit_fd, &tmp64,8 ) ==8 ) { +trace_postcopy_ram_incoming_cleanup_join (); +qemu_thread_join (&mis->fault_thread); + }else { +/* Not much we can do here, but may as well report it */ +error_report ("%s: incing userfault_quit_fd: %s" , __func__, +strerror (errno)); + } +trace_postcopy_ram_incoming_cleanup_closeuf (); +close (mis->userfault_fd); +close (mis->userfault_quit_fd); + mis->have_fault_thread = false; } +postcopy_state_set (mis,POSTCOPY_INCOMING_END ); +migrate_send_rp_shut (mis,qemu_file_get_error (mis->file) !=0 ); +if (mis->postcopy_tmp_page) {munmap (mis->postcopy_tmp_page,getpagesize ()); mis->postcopy_tmp_page =NULL ; } +trace_postcopy_ram_incoming_cleanup_exit ();return 0 ; }@@ -320,31 +348,150 @@ static int ram_block_enable_notify(const char *block_name, void *host_addr,static void *postcopy_ram_fault_thread (void *opaque) {MigrationIncomingState *mis = (MigrationIncomingState *)opaque; - -fprintf (stderr,"postcopy_ram_fault_thread\n" ); -/* TODO: In later patch */ +uint64_t hostaddr;/* The kernel always gives us 64 bit, not a pointer */ +int ret; +size_t hostpagesize =getpagesize (); +RAMBlock *rb =NULL ; +RAMBlock *last_rb =NULL ;/* last RAMBlock we sent part of */ +uint8_t *local_tmp_page; + +trace_postcopy_ram_fault_thread_entry ();qemu_sem_post (&mis->fault_thread_sem); -while (1 ) { -/* TODO: In later patch */ + + local_tmp_page =mmap (NULL ,getpagesize (), +PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS , +-1 ,0 );
Quoted 8 lines
migration/postcopy-ram.c static int ram_block_enable_notify(const char *block_name, void *host_addr, · line 369
Quoted 6 lines
migration/postcopy-ram.c +5
-0+if (!local_tmp_page) { +error_report ("%s mapping local tmp page: %s" , __func__, +strerror (errno)); +return NULL ; } +if (madvise (local_tmp_page,getpagesize (),MADV_DONTFORK )) {What's this '
local_tmp_page' used for ? I don't find where it is used in this function.
Yes, that was old code; it's already gone.
Quoted 1 line
Besides, there is a helper function
qemu_madvise()in qemu, maybe you should use it :)
Thanks; I could do for this one but I've got to be careful; qemu_madvise is mainly for portability, and this code is Linux specific anyway, and some of the QEMU_MADV_ flags aren't valid for all of the OSs anyway.
Dave
Quoted 150 lines
Thanks.
Quoted 147 lines
migration/postcopy-ram.c +108
-1+munmap (local_tmp_page,getpagesize ()); +error_report ("%s postcopy local page DONTFORK: %s" , __func__, +strerror (errno)); +return NULL ; + } + +while (true) { +ram_addr_t rb_offset; +ram_addr_t in_raspace; +struct pollfd pfd[2 ]; + +/* +* We're mainly waiting for the kernel to give us a faulting HVA, +* however we can be told to quit via userfault_quit_fd which is +* an eventfd +*/ + pfd[0 ].fd = mis->userfault_fd; + pfd[0 ].events =POLLIN ; + pfd[0 ].revents =0 ; + pfd[1 ].fd = mis->userfault_quit_fd; + pfd[1 ].events =POLLIN ;/* Waiting for eventfd to go positive */ + pfd[1 ].revents =0 ; + +if (poll (pfd,2 ,-1 /* Wait forever */ ) ==-1 ) { +error_report ("%s: userfault poll: %s" , __func__,strerror (errno)); +break ; + } + +if (pfd[1 ].revents) { +trace_postcopy_ram_fault_thread_quit (); +break ; + } + + ret =read (mis->userfault_fd, &hostaddr,sizeof (hostaddr)); +if (ret !=sizeof (hostaddr)) { +if (errno ==EAGAIN ) { +/* +* if a wake up happens on the other thread just after +* the poll, there is nothing to read. +*/ +continue ; + } +if (ret <0 ) { +error_report ("%s: Failed to read full userfault hostaddr: %s" , + __func__,strerror (errno)); +break ; + }else { +error_report ("%s: Read %d bytes from userfaultfd expected %zd" , + __func__, ret,sizeof (hostaddr)); +break ;/* Lost alignment, don't know what we'd read next */ + } + } + rb =qemu_ram_block_from_host ((void *)(uintptr_t )hostaddr, true, + &in_raspace, &rb_offset); +if (!rb) { +error_report ("postcopy_ram_fault_thread: Fault outside guest: %" + PRIx64, hostaddr); +break ; + } + +trace_postcopy_ram_fault_thread_request (hostaddr, +qemu_ram_get_idstr (rb), + rb_offset); + +/* +* Send the request to the source - we want to request one +* of our host page sizes (which is >= TPS) +*/ +if (rb != last_rb) { + last_rb = rb; +migrate_send_rp_req_pages (mis,qemu_ram_get_idstr (rb), + rb_offset, hostpagesize); + }else { +/* Save some space */ +migrate_send_rp_req_pages (mis,NULL , + rb_offset, hostpagesize); + } + } +munmap (local_tmp_page,getpagesize ()); +trace_postcopy_ram_fault_thread_exit ();return NULL ; }int postcopy_ram_enable_notify (MigrationIncomingState *mis) { -/* Create the fault handler thread and wait for it to be ready */ +/* Open the fd for the kernel to give us userfaults */ + mis->userfault_fd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (mis->userfault_fd ==-1 ) { +error_report ("%s: Failed to open userfault fd: %s" , __func__, +strerror (errno)); +return -1 ; + } + +/* +* Although the host check already tested the API, we need to +* do the check again as an ABI handshake on the new fd. +*/ +if (!ufd_version_check (mis->userfault_fd)) { +return -1 ; + } + +/* Now an eventfd we use to tell the fault-thread to quit */ + mis->userfault_quit_fd =eventfd (0 ,EFD_CLOEXEC ); +if (mis->userfault_quit_fd ==-1 ) { +error_report ("%s: Opening userfault_quit_fd: %s" , __func__, +strerror (errno)); +close (mis->userfault_fd); +return -1 ; + } +qemu_sem_init (&mis->fault_thread_sem,0 );qemu_thread_create (&mis->fault_thread,"postcopy/fault" , postcopy_ram_fault_thread, mis,QEMU_THREAD_JOINABLE );qemu_sem_wait (&mis->fault_thread_sem);qemu_sem_destroy (&mis->fault_thread_sem); + mis->have_fault_thread = true;/* Mark so that we get notified of accesses to unwritten areas */ if (qemu_ram_foreach_block (ram_block_enable_notify, mis)) {return -1 ; } +trace_postcopy_ram_enable_notify (); +return 0 ; }trace-events +9
-0@@ -1500,6 +1500,15 @@ postcopy_cleanup_area(const char *ramblock, void *host_addr, size_t offset, size postcopy_ram_discard_range(void *start, void *end) "%p,%p" postcopy_init_area(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=%zx length=%zx" postcopy_place_page(void *host_addr, bool all_zero) "host=%p all_zero=%d" +postcopy_ram_enable_notify(void) "" +postcopy_ram_fault_thread_entry(void) "" +postcopy_ram_fault_thread_exit(void) "" +postcopy_ram_fault_thread_quit(void) "" +postcopy_ram_fault_thread_request(uint64_t hostaddr, const char *ramblock, size_t offset) "Request for HVA=%" PRIx64 " rb=%s offset=%zx" +postcopy_ram_incoming_cleanup_closeuf(void) "" +postcopy_ram_incoming_cleanup_entry(void) "" +postcopy_ram_incoming_cleanup_exit(void) "" +postcopy_ram_incoming_cleanup_join(void) ""
Quoted 5 lines
trace-events postcopy_cleanup_area(const char *ramblock, void *host_addr, size_t offset, size · line 1514
Quoted 3 lines
trace-events +0
-0# kvm-all.c kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v6 43/47 Start up a postcopy/listener thread ready for incoming page data
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The loading of a device state (during postcopy) may access guest memory that's still on the source machine and thus might need a page fill; split off a separate thread that handles the incoming page data so that the original incoming migration code can finish off the device data.
--- include/migration/migration.h | 4 +++ migration/migration.c | 6 ++++ savevm.c | 79 ++++++++++++++++++++++++++++++++++++++++++- trace-events | 2 ++ 4 files changed, 90 insertions(+), 1 deletion(-)
include/migration/migration.h +4 -0
@@ -79,6 +79,10 @@ struct MigrationIncomingState {QemuThread fault_thread;QemuSemaphore fault_thread_sem; +bool have_listen_thread; +QemuThread listen_thread; +QemuSemaphore listen_thread_sem; +/* For the kernel to send us notifications */ int userfault_fd;/* To tell the fault_thread to quit */
migration/migration.c +6 -0
@@ -1082,6 +1082,12 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running) goto fail; } +/* +* Make sure the receiver can get incoming pages before we send the rest +* of the state +*/ +qemu_savevm_send_postcopy_listen (fb); +qemu_savevm_state_complete_precopy (fb);qemu_savevm_send_ping (fb,3 );
savevm.c +78 -1
97 lines not on this page · raw message
trace-events +2 -0
@@ -1187,6 +1187,8 @@ loadvm_postcopy_ram_handle_discard_end(void) "" loadvm_postcopy_ram_handle_discard_header(const char *ramid, uint16_t len) "%s: %ud" loadvm_process_command(uint16_t com, uint16_t len) "com=0x%x len=%d" loadvm_process_command_ping(uint32_t val) "%x" +postcopy_ram_listen_thread_exit(void) "" +postcopy_ram_listen_thread_start(void) "" qemu_savevm_send_postcopy_advise(void) "" qemu_savevm_send_postcopy_ram_discard(const char *id, uint16_t len) "%s: %ud" savevm_section_start(const char *id, unsigned int section_id) "%s, section_id %u"
Signature · 1 line
2.1.0
PATCH v6 44/47 postcopy: Wire up loadvm_postcopy_handle_ commands
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Wire up more of the handlers for the commands on the destination side, in particular loadvm_postcopy_handle_run now has enough to start the guest running.
--- savevm.c | 29 ++++++++++++++++++++++++++++- trace-events | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-)
savevm.c +28 -1
@@ -1360,12 +1360,34 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)static int loadvm_postcopy_handle_run (MigrationIncomingState *mis) {PostcopyState ps =postcopy_state_set (mis,POSTCOPY_INCOMING_RUNNING ); +Error *local_err =NULL ; +trace_loadvm_postcopy_handle_run ();if (ps !=POSTCOPY_INCOMING_LISTENING ) {error_report ("CMD_POSTCOPY_RUN in wrong postcopy state (%d)" , ps);return -1 ; } +/* TODO we should move all of this lot into postcopy_ram.c or a shared code +* in migration.c +*/ +cpu_synchronize_all_post_init (); + +qemu_announce_self (); + +/* Make sure all file formats flush their mutable metadata */ +bdrv_invalidate_cache_all (&local_err); +if (local_err) { +qerror_report_err (local_err); +error_free (local_err); +return -1 ; + } + +trace_loadvm_postcopy_handle_run_cpu_sync (); +cpu_synchronize_all_post_init (); + +trace_loadvm_postcopy_handle_run_vmstart (); +if (autostart) {/* Hold onto your hats, starting the CPU */ vm_start ();
@@ -1374,7 +1396,12 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)runstate_set (RUN_STATE_PAUSED ); } -return 0 ; +/* We need to finish reading the stream from the package +* and also stop reading anything more from the stream that loaded the +* package (since it's now being read by the listener thread). +* LOADVM_QUIT will quit all the layers of nested loadvm loops. +*/ +return LOADVM_QUIT ; } static int loadvm_process_command_simple_lencheck(const char *name,
trace-events +2 -0
@@ -1182,6 +1182,8 @@ loadvm_handle_cmd_packaged_received(int ret) "%d" loadvm_postcopy_handle_advise(void) "" loadvm_postcopy_handle_listen(void) "" loadvm_postcopy_handle_run(void) "" +loadvm_postcopy_handle_run_cpu_sync(void) "" +loadvm_postcopy_handle_run_vmstart(void) "" loadvm_postcopy_ram_handle_discard(void) "" loadvm_postcopy_ram_handle_discard_end(void) "" loadvm_postcopy_ram_handle_discard_header(const char *ramid, uint16_t len) "%s: %ud"
Signature · 1 line
2.1.0
PATCH v6 45/47 End of migration for postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Tweak the end of migration cleanup; we don't want to close stuff down at the end of the main stream, since the postcopy is still sending pages on the other thread.
--- migration/migration.c | 25 ++++++++++++++++++++++++- trace-events | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-)
migration/migration.c +24 -1
@@ -160,12 +160,35 @@ static void process_incoming_migration_co(void *opaque) {QEMUFile *f = opaque;Error *local_err =NULL ; +MigrationIncomingState *mis; +PostcopyState ps;int ret; -migration_incoming_state_new (f); + mis =migration_incoming_state_new (f); ret =qemu_loadvm_state (f); + ps =postcopy_state_get (mis); +trace_process_incoming_migration_co_end (ret, ps); +if (ps !=POSTCOPY_INCOMING_NONE ) { +if (ps ==POSTCOPY_INCOMING_ADVISE ) { +/* +* Where a migration had postcopy enabled (and thus went to advise) +* but managed to complete within the precopy period, we can use +* the normal exit. +*/ +postcopy_ram_incoming_cleanup (mis); + }else if (ret >=0 ) { +/* +* Postcopy was started, cleanup should happen at the end of the +* postcopy thread. +*/ +trace_process_incoming_migration_co_postcopy_end_main (); +return ; + } +/* Else if something went wrong then just fall out of the normal exit */ + } +qemu_fclose (f);free_xbzrle_decoded_buf ();migration_incoming_state_destroy ();
trace-events +2 -0
@@ -1435,6 +1435,8 @@ source_return_path_thread_loop_top(void) "" source_return_path_thread_pong(uint32_t val) "%x" source_return_path_thread_shut(uint32_t val) "%x" migrate_transferred(uint64_t tranferred, uint64_t time_spent, double bandwidth, uint64_t size) "transferred %" PRIu64 " time_spent %" PRIu64 " bandwidth %g max_size %" PRId64 +process_incoming_migration_co_end(int ret, int ps) "ret=%d postcopy-state=%d" +process_incoming_migration_co_postcopy_end_main(void) "" # migration/rdma.c qemu_dma_accept_incoming_migration(void) ""
Signature · 1 line
2.1.0
PATCH v6 46/47 Disable mlock around incoming postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Userfault doesn't work with mlock; mlock is designed to nail down pages so they don't move, userfault is designed to tell you when they're not there.
munlock the pages we userfault protect before postcopy. mlock everything again at the end if mlock is enabled.
--- include/sysemu/sysemu.h | 1 + migration/postcopy-ram.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
include/sysemu/sysemu.h +1 -0
@@ -171,6 +171,7 @@ extern int boot_menu;extern bool boot_strict;extern uint8_t *boot_splash_filedata;extern size_t boot_splash_filedata_size; +extern bool enable_mlock;extern uint8_t qemu_extra_params_fw[2 ];extern QEMUClockType rtc_clock;extern const char *mem_path;
migration/postcopy-ram.c +24 -0
@@ -84,6 +84,11 @@ static bool ufd_version_check(int ufd)return true; } +/* +* Note: This has the side effect of munlock'ing all of RAM, that's +* normally fine since if the postcopy succeeds it gets turned back on at the +* end. +*/ bool postcopy_ram_supported_by_host (void ) {long pagesize =getpagesize ();
@@ -112,6 +117,15 @@ bool postcopy_ram_supported_by_host(void) }/* +* userfault and mlock don't go together; we'll put it back later if +* it was enabled. +*/ +if (munlockall ()) { +error_report ("%s: munlockall: %s" , __func__,strerror (errno)); +return -1 ; + } + +/* * We need to check that the ops we need are supported on anon memory * To do that we need to register a chunk and see the flags that * are returned.
@@ -302,6 +316,16 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) mis->have_fault_thread = false; } +if (enable_mlock) { +if (os_mlock () <0 ) { +error_report ("mlock: %s" ,strerror (errno)); +/* +* It doesn't feel right to fail at this point, we have a valid +* VM state. +*/ + } + } +postcopy_state_set (mis,POSTCOPY_INCOMING_END );migrate_send_rp_shut (mis,qemu_file_get_error (mis->file) !=0 );
Signature · 1 line
2.1.0
PATCH v6 47/47 Inhibit ballooning during postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy detects accesses to pages that haven't been transferred yet using userfaultfd, and it causes exceptions on pages that are 'not present'.
Ballooning also causes pages to be marked as 'not present' when the guest inflates the balloon.
Potentially a balloon could be inflated to discard pages that are currently inflight during postcopy and that may be arriving at about the same time.
To avoid this confusion, disable ballooning during postcopy.
When disabled we drop balloon requests from the guest. Since ballooning is generally initiated by the host, the management system should avoid initiating any balloon instructions to the guest during migration, although it's not possible to know how long it would take a guest to process a request made prior to the start of migration.
Queueing the requests until after migration would be nice, but is non-trivial, since the set of inflate/deflate requests have to be compared with the state of the page to know what the final outcome is allowed to be.
--- balloon.c | 11 +++++++++++ hw/virtio/virtio-balloon.c | 4 +++- include/sysemu/balloon.h | 2 ++ migration/postcopy-ram.c | 9 +++++++++ 4 files changed, 25 insertions(+), 1 deletion(-)
balloon.c +11 -0
@@ -35,6 +35,17 @@static QEMUBalloonEvent *balloon_event_fn;static QEMUBalloonStatus *balloon_stat_fn;static void *balloon_opaque; +static bool balloon_inhibited; + +bool qemu_balloon_is_inhibited (void ) +{ +return balloon_inhibited; +} + +void qemu_balloon_inhibit (bool state) +{ + balloon_inhibited = state; +}static bool have_balloon (Error **errp) {
hw/virtio/virtio-balloon.c +3 -1
@@ -37,9 +37,11 @@static void balloon_page (void *addr,int deflate) {#if defined(__linux__) -if (!kvm_enabled () ||kvm_has_sync_mmu ()) +if (!qemu_balloon_is_inhibited () && (!kvm_enabled () || +kvm_has_sync_mmu ())) {qemu_madvise (addr,TARGET_PAGE_SIZE , deflate ?QEMU_MADV_WILLNEED :QEMU_MADV_DONTNEED ); + }#endif }
include/sysemu/balloon.h +2 -0
@@ -23,5 +23,7 @@ typedef void (QEMUBalloonStatus)(void *opaque, BalloonInfo *info);int qemu_add_balloon_handler (QEMUBalloonEvent *event_func,QEMUBalloonStatus *stat_func,void *opaque);void qemu_remove_balloon_handler (void *opaque); +bool qemu_balloon_is_inhibited (void ); +void qemu_balloon_inhibit (bool state);#endif
migration/postcopy-ram.c +9 -0
@@ -24,6 +24,7 @@#include "migration/migration.h" #include "migration/postcopy-ram.h" #include "sysemu/sysemu.h" +#include "sysemu/balloon.h" #include "qemu/error-report.h" #include "trace.h"
@@ -316,6 +317,8 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) mis->have_fault_thread = false; } +qemu_balloon_inhibit (false); +if (enable_mlock) {if (os_mlock () <0 ) {error_report ("mlock: %s" ,strerror (errno));
@@ -514,6 +517,12 @@ int postcopy_ram_enable_notify(MigrationIncomingState *mis)return -1 ; } +/* +* Ballooning can mark pages as absent while we're postcopying +* that would cause false userfaults. +*/ +qemu_balloon_inhibit (true); +trace_postcopy_ram_enable_notify ();return 0 ;
Signature · 1 line
2.1.0
Hi David,
I have tired your v6 postcopy patches and found it doesn't work. When I tried to start the postcopy in live migration, some errors were printed. I just did the following things:
On destination side, started the qemu like this:
/root/vt-sync/post_copy_v6_qemu.git/x86_64-softmmu/qemu-system-x86_64 -enable-kvm -smp 2 -m 1024 -net none /mnt/jinshi_ia32e_rhel6u5.qcow2 -monitor stdio -incoming tcp:0:4444
On source side, started the qemu like this:
/root/vt-sync/post_copy_v6_qemu.git/x86_64-softmmu/qemu-system-x86_64 -enable-kvm -smp 2 -m 1024 -net none /mnt/jinshi_ia32e_rhel6u5.qcow2 -monitor stdio
and then
(qemu) migrate_set_capability x-postcopy-ram on
When I started the post copy with
(qemu) migrate -d tcp:localhost:4444
I got the error message on the source side:
(qemu) qemu-system-x86_64: socket_writev_buffer: Got err=104 for (131552/-1)
qemu-system-x86_64: RP: Received invalid message 0x0000 length 0x0000
and the following error on the destination side:
(qemu) qemu-system-x86_64: postcopy_ram_supported_by_host: No OS support
qemu-system-x86_64: load of migration failed: Operation not permitted
the dmesg printed:
[ 233.456545] kvm: zapping shadow pages for mmio generation wraparound [ 239.785916] kvm [11926]: vcpu0 disabled perfctr wrmsr: 0xc1 data 0xabcd
The v5 patches have no such errors. Do you have any suggestion?
Liang
Quoted 167 lines
-----Original Message-----
From: [email protected] [mailto:qemu- [email protected]] On Behalf Of Dr. David Alan Gilbert (git)
Sent: Wednesday, April 15, 2015 1:03 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [Qemu-devel] [PATCH v6 00/47] Postcopy implementationFrom: "Dr. David Alan Gilbert" <[email protected]>
This is the 6th cut of my version of postcopy; it is designed for use with the
Linux kernel additions posted by Andrea Arcangeli here:git clone --reference linux -b userfault18
git://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git(Note this is a different API from the last version)
This qemu series can be found at:
https://github.com/orbitfp7/qemu.git
on the wp3-postcopy-v6 tag.It addresses some but not yet all of the previous review comments; however there are a couple of large simplifications, so it seems worth posting to meet the new kernel API and to stop people reviewing deadcode.
Note: That the
userfaultfd.hheader is no longer included in this tree:
- if you're building with the appropriate kernel headers it should find it
- if you're building on a host that doesn't have the kernel headers installed in the right place then:
configure with: --extra-cflags="-D__NR_userfaultfd=323"
cpinclude/uapi/linux/userfaultfd.hinto somewhere in the include path, e.g./usr/local/include/linuxv6
Removed the PMI bitmaps
- Andrea updated the kernel API so that userspace doesn't need to do wakeups, and thus QEMU doesn't need to keep track of which pages it's received; there is a price - which is we end up sending more dupes to the source, but it simplifies stuff a lot and makes the normal paths a lot quicker.
(10s of line change in kernel, 10%-ish simplification in this code!)
Changed discard message format to a simpler start/end address scheme
and rework discard and chunking code to work in long's to match bitmap
'qemu_get_buffer_less_copy' for postcopy pages
- avoids a userspace copy since the kernel now does it
- the new qemufile interface might also be useful for other places that don't need a copy (maybe xbzrle?)
Changed the blockingness of the incoming fd
it was incorrectly blocking during the precopy phase after a postcopy was enabled, causing the HMP to be unavailable. It's now blocking only once the postcopy thread starts up, since it's not a coroutine it can't deal with the yields inqemu_file.
An error on the return-path now marks the migration as failedFixups from Dave Gibson's comments
Removedcan_postcopy, renamedsave_completetosave_complete_precopy
addedsave_complete_postcopy
Simplified loadvm loop exits
discard message format changes above and many more smaller changes.small fixups for RCU
This work has been partially funded by the EU Orbit project:
see http://www.orbitproject.eu/about/TODO:
The major work is to rework the page send/receive loops so that supporting larger host pages doesn't make it quite as messy.Dr. David Alan Gilbert (47):
Start documenting how postcopy works.
Split header writing out ofqemu_savevm_state_begin
qemu_ram_foreach_block: pass up error value, and down the ramblock
name
Addqemu_get_counted_stringto read a string prefixed by a count byte Create MigrationIncomingState
Provide runtime Target page information
Move copy out ofqemu_peek_buffer
Addqemu_get_buffer_less_copyto avoid copies some of the time Add wrapper for setting blocking status on a QEMUFile Renamesave_live_completetosave_live_complete_precopyReturn path: Open a return path on QEMUFile for sockets Return path:socket_writev_buffer: Block even on non-blocking fd's Migration commands
Return path: Control commands
Return path: Send responses from destination to source Return path: Source handling of return path
ram_debug_dump_bitmap: Dump a migration bitmap as text Moveloadvm_handlersinto MigrationIncomingState
Rework loadvm path for subloops
Add migration-capability boolean for postcopy-ram.
Add wrappers and handlers for sending/receiving the postcopy-ram
migration messages.
MIG_CMD_PACKAGED: Send a packaged chunk of migration stream
migrate_init: Call from savevm
Modifysave_live_pendingfor postcopy
postcopy: OS support test
migrate_start_postcopy: Command to trigger transition to postcopyMIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state Addqemu_savevm_state_complete_postcopy
Postcopy: Maintain sentmap and calculate discard
postcopy: Incoming initialisation
postcopy:ram_enable_notifyto switch on userfault
Postcopy: Postcopy startup in migration thread
Postcopy end inmigration_thread
Page request: AddMIG_RP_MSG_REQ_PAGESreverse command Page request: Process incoming page request
Page request: Consume pages off the post-copy queuepostcopy_ram.c:place_pageand helpers
Postcopy: Use helpers to map pages during migrationqemu_ram_block_from_host
Don't sync dirty bitmaps in postcopy
Host page!=target page: Cleanup bitmaps
Postcopy; Handle userfault requests
Start up a postcopy/listener thread ready for incoming page data
postcopy: Wire uploadvm_postcopy_handle_commands
End of migration for postcopy
Disable mlock around incoming postcopy
Inhibit ballooning during postcopyarch_init.c | 868 ++++++++++++++++++++++++++++++++++++--- balloon.c | 11 + docs/migration.txt | 167 ++++++++ exec.c | 74 +++- hmp-commands.hx | 15 + hmp.c | 7 + hmp.h | 1 + hw/ppc/spapr.c | 2 +- hw/virtio/virtio-balloon.c | 4 +- include/exec/cpu-all.h | 2 - include/exec/cpu-common.h | 7 +- include/migration/migration.h | 126 +++++- include/migration/postcopy-ram.h | 88 ++++ include/migration/qemu-file.h | 15 +- include/migration/vmstate.h | 10 +- include/qemu/typedefs.h | 5 + include/sysemu/balloon.h | 2 + include/sysemu/sysemu.h | 45 +- migration/Makefile.objs | 2 +- migration/block.c | 9 +- migration/migration.c | 743 +++++++++++++++++++++++++++++++-- migration/postcopy-ram.c | 715 ++++++++++++++++++++++++++++++++ migration/qemu-file-unix.c | 106 ++++- migration/qemu-file.c | 100 ++++- migration/rdma.c | 4 +- migration/vmstate.c | 5 +- qapi-schema.json | 19 +- qmp-commands.hx | 19 + savevm.c | 809 ++++++++++++++++++++++++++++++++---- trace-events | 77 +++- 30 files changed, 3832 insertions(+), 225 deletions(-) create mode 100644
include/migration/postcopy-ram.hcreate mode 100644 migration/postcopy-ram.c--
2.1.0
Quoted 25 lines · * Li, Liang Z ([email protected]) wrote:
Hi David,
I have tired your v6 postcopy patches and found it doesn't work. When I tried to start the postcopy in live migration, some errors were printed. I just did the following things:
On destination side, started the qemu like this:
/root/vt-sync/post_copy_v6_qemu.git/x86_64-softmmu/qemu-system-x86_64(quoted diff) +0
-2-enable-kvm -smp 2 -m 1024 -net none /mnt/jinshi_ia32e_rhel6u5.qcow2 -monitor stdio -incoming tcp:0:4444On source side, started the qemu like this:
/root/vt-sync/post_copy_v6_qemu.git/x86_64-softmmu/qemu-system-x86_64(quoted diff) +0
-2-enable-kvm -smp 2 -m 1024 -net none /mnt/jinshi_ia32e_rhel6u5.qcow2 -monitor stdioand then
(qemu)migrate_set_capabilityx-postcopy-ram onWhen I started the post copy with
(qemu) migrate -d tcp:localhost:4444I got the error message on the source side:
Quoted 7 lines
(qemu) qemu-system-x86_64:
socket_writev_buffer: Got err=104 for (131552/-1)
qemu-system-x86_64: RP: Received invalid message0x0000length0x0000and the following error on the destination side:
(qemu) qemu-system-x86_64:
postcopy_ram_supported_by_host: No OS support
qemu-system-x86_64: load of migration failed: Operation not permitted
OK, the important error here is:
postcopy_ram_supported_by_host: No OS support
that's saying that the destination OS either:
1) The kernel isn't the correct kernel with Andrea's userfault code compiled in (check that userfaultfd is configured into the kernel as well)
2) That when you built the QEMU it didn't find the syscall definition for the userfaultfd in the header as it compiled it.
I think from that error it is (2) - so make sure that when you built the qemu that you're using the headers from that kernel, or use the extra-cflags hack that I mentioned in the cover letter.
Note that you need to use the kernel tree which I point to in the first message. (The older kernel from v5 wont work).
DaveP.S. I'm on holiday this week, so not checking work email much.
Quoted 180 lines
the dmesg printed:
[ 233.456545] kvm: zapping shadow pages for mmio generation wraparound [ 239.785916] kvm [11926]: vcpu0 disabled perfctr wrmsr: 0xc1 data 0xabcdThe v5 patches have no such errors. Do you have any suggestion?
Liang
Quoted 167 lines
-----Original Message-----
From: [email protected] [mailto:qemu- [email protected]] On Behalf Of Dr. David Alan Gilbert (git)
Sent: Wednesday, April 15, 2015 1:03 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [Qemu-devel] [PATCH v6 00/47] Postcopy implementationFrom: "Dr. David Alan Gilbert" <[email protected]>
This is the 6th cut of my version of postcopy; it is designed for use with the
Linux kernel additions posted by Andrea Arcangeli here:git clone --reference linux -b userfault18
git://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git(Note this is a different API from the last version)
This qemu series can be found at:
https://github.com/orbitfp7/qemu.git
on the wp3-postcopy-v6 tag.It addresses some but not yet all of the previous review comments; however there are a couple of large simplifications, so it seems worth posting to meet the new kernel API and to stop people reviewing deadcode.
Note: That the
userfaultfd.hheader is no longer included in this tree:
- if you're building with the appropriate kernel headers it should find it
- if you're building on a host that doesn't have the kernel headers installed in the right place then:
configure with: --extra-cflags="-D__NR_userfaultfd=323"
cpinclude/uapi/linux/userfaultfd.hinto somewhere in the include path, e.g./usr/local/include/linuxv6
Removed the PMI bitmaps
- Andrea updated the kernel API so that userspace doesn't need to do wakeups, and thus QEMU doesn't need to keep track of which pages it's received; there is a price - which is we end up sending more dupes to the source, but it simplifies stuff a lot and makes the normal paths a lot quicker.
(10s of line change in kernel, 10%-ish simplification in this code!)
Changed discard message format to a simpler start/end address scheme
and rework discard and chunking code to work in long's to match bitmap
'qemu_get_buffer_less_copy' for postcopy pages
- avoids a userspace copy since the kernel now does it
- the new qemufile interface might also be useful for other places that don't need a copy (maybe xbzrle?)
Changed the blockingness of the incoming fd
it was incorrectly blocking during the precopy phase after a postcopy was enabled, causing the HMP to be unavailable. It's now blocking only once the postcopy thread starts up, since it's not a coroutine it can't deal with the yields inqemu_file.
An error on the return-path now marks the migration as failedFixups from Dave Gibson's comments
Removedcan_postcopy, renamedsave_completetosave_complete_precopy
addedsave_complete_postcopy
Simplified loadvm loop exits
discard message format changes above and many more smaller changes.small fixups for RCU
This work has been partially funded by the EU Orbit project:
see http://www.orbitproject.eu/about/TODO:
The major work is to rework the page send/receive loops so that supporting larger host pages doesn't make it quite as messy.Dr. David Alan Gilbert (47):
Start documenting how postcopy works.
Split header writing out ofqemu_savevm_state_begin
qemu_ram_foreach_block: pass up error value, and down the ramblock
name
Addqemu_get_counted_stringto read a string prefixed by a count byte Create MigrationIncomingState
Provide runtime Target page information
Move copy out ofqemu_peek_buffer
Addqemu_get_buffer_less_copyto avoid copies some of the time Add wrapper for setting blocking status on a QEMUFile Renamesave_live_completetosave_live_complete_precopyReturn path: Open a return path on QEMUFile for sockets Return path:socket_writev_buffer: Block even on non-blocking fd's Migration commands
Return path: Control commands
Return path: Send responses from destination to source Return path: Source handling of return path
ram_debug_dump_bitmap: Dump a migration bitmap as text Moveloadvm_handlersinto MigrationIncomingState
Rework loadvm path for subloops
Add migration-capability boolean for postcopy-ram.
Add wrappers and handlers for sending/receiving the postcopy-ram
migration messages.
MIG_CMD_PACKAGED: Send a packaged chunk of migration stream
migrate_init: Call from savevm
Modifysave_live_pendingfor postcopy
postcopy: OS support test
migrate_start_postcopy: Command to trigger transition to postcopyMIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state Addqemu_savevm_state_complete_postcopy
Postcopy: Maintain sentmap and calculate discard
postcopy: Incoming initialisation
postcopy:ram_enable_notifyto switch on userfault
Postcopy: Postcopy startup in migration thread
Postcopy end inmigration_thread
Page request: AddMIG_RP_MSG_REQ_PAGESreverse command Page request: Process incoming page request
Page request: Consume pages off the post-copy queuepostcopy_ram.c:place_pageand helpers
Postcopy: Use helpers to map pages during migrationqemu_ram_block_from_host
Don't sync dirty bitmaps in postcopy
Host page!=target page: Cleanup bitmaps
Postcopy; Handle userfault requests
Start up a postcopy/listener thread ready for incoming page data
postcopy: Wire uploadvm_postcopy_handle_commands
End of migration for postcopy
Disable mlock around incoming postcopy
Inhibit ballooning during postcopyarch_init.c | 868 ++++++++++++++++++++++++++++++++++++--- balloon.c | 11 + docs/migration.txt | 167 ++++++++ exec.c | 74 +++- hmp-commands.hx | 15 + hmp.c | 7 + hmp.h | 1 + hw/ppc/spapr.c | 2 +- hw/virtio/virtio-balloon.c | 4 +- include/exec/cpu-all.h | 2 - include/exec/cpu-common.h | 7 +- include/migration/migration.h | 126 +++++- include/migration/postcopy-ram.h | 88 ++++ include/migration/qemu-file.h | 15 +- include/migration/vmstate.h | 10 +- include/qemu/typedefs.h | 5 + include/sysemu/balloon.h | 2 + include/sysemu/sysemu.h | 45 +- migration/Makefile.objs | 2 +- migration/block.c | 9 +- migration/migration.c | 743 +++++++++++++++++++++++++++++++-- migration/postcopy-ram.c | 715 ++++++++++++++++++++++++++++++++ migration/qemu-file-unix.c | 106 ++++- migration/qemu-file.c | 100 ++++- migration/rdma.c | 4 +- migration/vmstate.c | 5 +- qapi-schema.json | 19 +- qmp-commands.hx | 19 + savevm.c | 809 ++++++++++++++++++++++++++++++++---- trace-events | 77 +++- 30 files changed, 3832 insertions(+), 225 deletions(-) create mode 100644
include/migration/postcopy-ram.hcreate mode 100644 migration/postcopy-ram.c--
2.1.0
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 50 lines · * Li, Liang Z ([email protected]) wrote:
Quoted 4 lines
Hi David,
I have tired your v6 postcopy patches and found it doesn't work. When I tried to start the postcopy in live migration, some errors were printed. I
just did the following things:
Quoted 4 lines
On destination side, started the qemu like this:
/root/vt-sync/post_copy_v6_qemu.git/x86_64-softmmu/qemu-system-
x86_64Quoted 6 lines
(quoted diff) +0
-2-enable-kvm -smp 2 -m 1024 -net none /mnt/jinshi_ia32e_rhel6u5.qcow2 -monitor stdio -incoming tcp:0:4444On source side, started the qemu like this:
/root/vt-sync/post_copy_v6_qemu.git/x86_64-softmmu/qemu-system-
x86_64Quoted 6 lines
(quoted diff) +0
-2-enable-kvm -smp 2 -m 1024 -net none /mnt/jinshi_ia32e_rhel6u5.qcow2 -monitor stdioand then
(qemu)migrate_set_capabilityx-postcopy-ram onQuoted 6 lines
When I started the post copy with
(qemu) migrate -d tcp:localhost:4444I got the error message on the source side:
(qemu) qemu-system-x86_64:
socket_writev_buffer: Got err=104 for(131552/-1)
Quoted 1 line
qemu-system-x86_64: RP: Received invalid message
0x0000length
0x0000Quoted 6 lines
and the following error on the destination side:
(qemu) qemu-system-x86_64:
postcopy_ram_supported_by_host: No OS support
qemu-system-x86_64: load of migration failed: Operation not permittedOK, the important error here is:
postcopy_ram_supported_by_host: No OS supportthat's saying that the destination OS either:
1) The kernel isn't the correct kernel with Andrea's userfault code compiled
in
(check that userfaultfd is configured into the kernel as well)
2) That when you built the QEMU it didn't find the syscall definition for the userfaultfd in the header as it compiled it.
Quoted 8 lines
I think from that error it is (2) - so make sure that when you built the qemu that you're using the headers from that kernel, or use the extra-cflags hack that I mentioned in the cover letter.
Note that you need to use the kernel tree which I point to in the first message.
(The older kernel from v5 wont work).
Thanks Dave, I will retry according to your suggestion.
Quoted 195 lines
Dave
P.S. I'm on holiday this week, so not checking work email much.Quoted 44 lines
the dmesg printed:
[ 233.456545] kvm: zapping shadow pages for mmio generationwraparound [ 239.785916] kvm [11926]: vcpu0 disabled perfctr wrmsr:
0xc1data0xabcdThe v5 patches have no such errors. Do you have any suggestion?
Liang
Quoted 31 lines
-----Original Message-----
From: [email protected] [mailto:qemu- [email protected]] On Behalf Of Dr. devel-bounces+David Alan
Gilbert (git)
Sent: Wednesday, April 15, 2015 1:03 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [Qemu-devel] [PATCH v6 00/47] Postcopy implementationFrom: "Dr. David Alan Gilbert" <[email protected]>
This is the 6th cut of my version of postcopy; it is designed for
use with the Linux kernel additions posted by Andrea Arcangeli here:git clone --reference linux -b userfault18
git://git.kernel.org/pub/scm/linux/kernel/git/andrea/aa.git(Note this is a different API from the last version)
This qemu series can be found at:
https://github.com/orbitfp7/qemu.git
on the wp3-postcopy-v6 tag.It addresses some but not yet all of the previous review comments; however there are a couple of large simplifications, so it seems worth posting to meet the new kernel API and to stop people reviewing
deadcode.
Quoted 10 lines
Quoted 10 lines
Note: That the
userfaultfd.hheader is no longer included in this tree:
- if you're building with the appropriate kernel headers it should find it
- if you're building on a host that doesn't have the kernel headers installed in the right place then:
configure with: --extra-cflags="-D__NR_userfaultfd=323"
cpinclude/uapi/linux/userfaultfd.hinto somewhere in the include path, e.g./usr/local/include/linuxQuoted 10 lines
Quoted 10 lines
v6
Removed the PMI bitmaps
- Andrea updated the kernel API so that userspace doesn't need to do wakeups, and thus QEMU doesn't need to keep track of which pages it's received; there is a price - which is we end up sending more dupes to the source, but it simplifies stuff a lot and makes the normal paths a lot quicker. (10s of line change in kernel, 10%-ish simplification in this code!)
Changed discard message format to a simpler start/end address scheme
and rework discard and chunking code to work in long's to matchbitmap
Quoted 6 lines
Quoted 6 lines
'
qemu_get_buffer_less_copy' for postcopy pages
- avoids a userspace copy since the kernel now does it
- the new qemufile interface might also be useful for other places that don't need a copy (maybe xbzrle?)
Changed the blockingness of the incoming fd
it was incorrectly blocking during the precopy phase after a postcopywas
Quoted 1 line
Quoted 1 line
enabled, causing the HMP to be unavailable. It's now blocking only
once
Quoted 12 lines
Quoted 12 lines
the postcopy thread starts up, since it's not a coroutine it can't deal with the yields in
qemu_file.
An error on the return-path now marks the migration as failedFixups from Dave Gibson's comments
Removedcan_postcopy, renamedsave_completetosave_complete_precopy
addedsave_complete_postcopy
Simplified loadvm loop exits
discard message format changes above and many more smaller changes.Quoted 8 lines
Quoted 8 lines
small fixups for RCU
This work has been partially funded by the EU Orbit project:
see http://www.orbitproject.eu/about/TODO:
The major work is to rework the page send/receive loops so thatsupporting
Quoted 53 lines
Quoted 53 lines
larger host pages doesn't make it quite as messy.
Dr. David Alan Gilbert (47):
Start documenting how postcopy works.
Split header writing out ofqemu_savevm_state_begin
qemu_ram_foreach_block: pass up error value, and down the ramblock
name
Addqemu_get_counted_stringto read a string prefixed by a count byte Create MigrationIncomingState
Provide runtime Target page information
Move copy out ofqemu_peek_buffer
Addqemu_get_buffer_less_copyto avoid copies some of the time Add wrapper for setting blocking status on a QEMUFile Renamesave_live_completetosave_live_complete_precopyReturn path: Open a return path on QEMUFile for sockets Return path:socket_writev_buffer: Block even on non-blocking fd's Migration commands
Return path: Control commands
Return path: Send responses from destination to source Return path: Source handling of return path
ram_debug_dump_bitmap: Dump a migration bitmap as text Moveloadvm_handlersinto MigrationIncomingState
Rework loadvm path for subloops
Add migration-capability boolean for postcopy-ram.
Add wrappers and handlers for sending/receiving the postcopy-ram
migration messages.
MIG_CMD_PACKAGED: Send a packaged chunk of migration stream
migrate_init: Call from savevm
Modifysave_live_pendingfor postcopy
postcopy: OS support test
migrate_start_postcopy: Command to trigger transition to postcopyMIGRATION_STATUS_POSTCOPY_ACTIVE: Add new migration state Addqemu_savevm_state_complete_postcopy
Postcopy: Maintain sentmap and calculate discard
postcopy: Incoming initialisation
postcopy:ram_enable_notifyto switch on userfault
Postcopy: Postcopy startup in migration thread
Postcopy end inmigration_thread
Page request: AddMIG_RP_MSG_REQ_PAGESreverse command Page request: Process incoming page request
Page request: Consume pages off the post-copy queuepostcopy_ram.c:place_pageand helpers
Postcopy: Use helpers to map pages during migrationqemu_ram_block_from_host
Don't sync dirty bitmaps in postcopy
Host page!=target page: Cleanup bitmaps
Postcopy; Handle userfault requests
Start up a postcopy/listener thread ready for incoming page data
postcopy: Wire uploadvm_postcopy_handle_commands
End of migration for postcopy
Disable mlock around incoming postcopy
Inhibit ballooning during postcopyQuoted 1 line
Quoted 1 line
arch_init.c | 868++++++++++++++++++++++++++++++++++++---
Quoted 12 lines
Quoted 12 lines
balloon.c | 11 + docs/migration.txt | 167 ++++++++ exec.c | 74 +++- hmp-commands.hx | 15 + hmp.c | 7 + hmp.h | 1 + hw/ppc/spapr.c | 2 +- hw/virtio/virtio-balloon.c | 4 +- include/exec/cpu-all.h | 2 - include/exec/cpu-common.h | 7 +- include/migration/migration.h | 126 +++++- include/migration/postcopy-ram.h | 88 ++++Quoted 8 lines
Quoted 8 lines
include/migration/qemu-file.h | 15 +- include/migration/vmstate.h | 10 +- include/qemu/typedefs.h | 5 + include/sysemu/balloon.h | 2 + include/sysemu/sysemu.h | 45 +- migration/Makefile.objs | 2 +- migration/block.c | 9 +- migration/migration.c | 743+++++++++++++++++++++++++++++++--
Quoted 14 lines
Quoted 14 lines
migration/postcopy-ram.c | 715 ++++++++++++++++++++++++++++++++ migration/qemu-file-unix.c | 106 ++++- migration/qemu-file.c | 100 ++++- migration/rdma.c | 4 +- migration/vmstate.c | 5 +- qapi-schema.json | 19 +- qmp-commands.hx | 19 + savevm.c | 809 ++++++++++++++++++++++++++++++++---- trace-events | 77 +++- 30 files changed, 3832 insertions(+), 225 deletions(-) create mode100644
include/migration/postcopy-ram.hcreate mode 100644 migration/postcopy-ram.cQuoted 4 lines
Quoted 3 lines
--
2.1.0--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
A message that is not in the archive (sent off-list or not yet received): 20150505150112.GM2126@work-vm
A message that is not in the archive (sent off-list or not yet received): F2CBF3009FA73547804AE4C663CAB28E50F0E1@shsmsx102.ccr.corp.intel.com
A message that is not in the archive (sent off-list or not yet received): 20150506083056.GB2204@work-vm
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Thanks Dave, I will retry according to your suggestion.
Did that work for you?
Yes, it works.
Great.
Quoted 8 lines
Quoted 2 lines
Bye the way, I found that the source guest will resume after about 15 minuets if there are some network errors happened during post copy. Is it
the expected behavior?
Quoted 1 line
And have you any plan about handing such errors?
Interesting; it shouldn't do that. I think it's best for the source to stay in paused following an error. Were you driving it directly or via libvirt?
Drive it directly.
Quoted 11 lines · * Li, Liang Z ([email protected]) wrote:
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Thanks Dave, I will retry according to your suggestion.
Did that work for you?
Yes, it works.
Great.
Quoted 2 lines
Bye the way, I found that the source guest will resume after about 15 minuets if there are some network errors happened during post copy. Is it
Quoted 8 lines
Quoted 6 lines
the expected behavior?
Quoted 1 line
And have you any plan about handing such errors?
Interesting; it shouldn't do that. I think it's best for the source to stay in paused following an error. Were you driving it directly or via libvirt?
Drive it directly.
OK, thanks, I'll have a look at it.
Dave
Quoted 1 line
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK