migration queue
From: "Dr. David Alan Gilbert" <[email protected]>
The following changes since commit b5b6b2b912bbcd3953407da938a8f969577ad3a1:
Merge remote-tracking branch 'remotes/amarkovic/tags/mips-queue-mar-05-2019' into staging (2019-03-05 21:07:29 +0000)
are available in the Git repository at:
git://github.com/dagrh/qemu.git tags/pull-migration-20190306a
for you to fetch changes up to b5922fc5891261153f1a0f20e814c620aabeb6ac:
qapi/migration.json: Remove a variable that doesn't exist in example (2019-03-06 10:49:18 +0000)
----------------------------------------------------------------
Migation pull 2019-03-06
(This replaces the pull sent yesterday)
a) 4 small fixes including the cancel problem
that caused the ahci migration test to fail
intermittently
b) Yury's ignore-shared feature
c) Juan's extra tests
d) Wei Wang's free page hinting
e) Some Colo fixes from Zhang Chen
Diff from yesterdays pull:
1) A missing fix of mine (cleanup during exit)
2) Changes from Eric/Markus on 'Create socket-address parameter'
----------------------------------------------------------------
Dr. David Alan Gilbert (3):
migration: Fix cancel state migration/rdma: Fix qemu_rdma_cleanup null check
migration: Cleanup during exit
Juan Quintela (3):
tests: Add migration xbzrle test
migration: Create socket-address parameter
tests: Add basic migration precopy tcp test
Marcel Apfelbaum (1):
migration/rdma: clang compilation fix
Wei Wang (7):
bitmap: fix bitmap_count_one
bitmap: bitmap_count_one_with_offset
migration: use bitmap_mutex in migration_bitmap_clear_dirty
migration: API to clear bits of guest free pages from the dirty bitmap migration/ram.c: add a notifier chain for precopy migration/ram.c: add the free page optimization enable flag
virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
Yury Kotov (5):
exec: Change RAMBlockIterFunc definition
migration: Introduce ignore-shared capability
migration: Add an ability to ignore shared RAM blocks tests/migration-test: Add a test for ignore-shared capability
migration: Add capabilities validation
Zhang Chen (3):
Migration/colo.c: Fix double close bug when occur COLO failover Migration/colo.c: Make COLO node running after failover qapi/migration.json: Remove a variable that doesn't exist in example
exec.c | 38 ++--- hmp.c | 33 ++++ hw/virtio/virtio-balloon.c | 263 +++++++++++++++++++++++++++++++ include/exec/cpu-common.h | 7 +- include/hw/virtio/virtio-balloon.h | 28 +++- include/migration/misc.h | 24 ++- include/qemu/bitmap.h | 17 ++ migration/colo.c | 2 +- migration/migration.c | 62 +++++++- migration/migration.h | 9 +- migration/postcopy-ram.c | 48 +++--- migration/ram.c | 231 ++++++++++++++++++++++++---- migration/rdma.c | 18 ++- migration/savevm.c | 152 ++++++++++++++++++ migration/socket.c | 11 ++ qapi/migration.json | 13 +- stubs/ram-block.c | 15 ++ tests/migration-test.c | 308 +++++++++++++++++++++++++++++++++---- util/vfio-helpers.c | 6 +- vl.c | 8 +- 20 files changed, 1167 insertions(+), 126 deletions(-)
[Qemu-devel] [PULL 01/22] migration: Fix cancel state
From: "Dr. David Alan Gilbert" <[email protected]>
During a cancelled migration there's a race where the fd can go into an error state before we get back around the migration loop and migration_detect_error transitions from cancelling->failed.
Check for cancelled/cancelling and don't change the state.
Red Hat bug: https://bugzilla.redhat.com/show_bug.cgi?id=1608649
Message-Id: <[email protected]>
--- migration/migration.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
migration/migration.c +9 -2
@@ -2911,6 +2911,13 @@ static MigThrError postcopy_pause(MigrationState *s)static MigThrError migration_detect_error (MigrationState *s) {int ret; +int state = s->state; + +if (state ==MIGRATION_STATUS_CANCELLING || + state ==MIGRATION_STATUS_CANCELLED ) { +/* End the migration, but don't set the state to failed */ +return MIG_THR_ERR_FATAL ; + }/* Try to detect any file errors */ ret =qemu_file_get_error (s->to_dst_file);
@@ -2920,7 +2927,7 @@ static MigThrError migration_detect_error(MigrationState *s)return MIG_THR_ERR_NONE ; } -if (s->state ==MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO ) { +if (state ==MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO ) {/* * For postcopy, we allow the network to be down for a * while. After that, it can be continued by a
@@ -2932,7 +2939,7 @@ static MigThrError migration_detect_error(MigrationState *s)* For precopy (or postcopy with error outside IO), we fail * with no time. */ -migrate_set_state (&s->state, s->state,MIGRATION_STATUS_FAILED ); +migrate_set_state (&s->state, state,MIGRATION_STATUS_FAILED );trace_migration_thread_file_err ();/* Time to stop the migration, now. */
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 02/22] migration/rdma: Fix qemu_rdma_cleanup null check
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
If the migration fails before the channel is open (e.g. a bad address) we end up in the cleanup with rdma->channel==NULL.
Spotted by Coverity: CID 1398634
Message-Id: <[email protected]>
--- migration/rdma.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
migration/rdma.c +3 -1
@@ -2321,7 +2321,9 @@ static void qemu_rdma_cleanup(RDMAContext *rdma) rdma->connected = false; } -qemu_set_fd_handler (rdma->channel->fd,NULL ,NULL ,NULL ); +if (rdma->channel) { +qemu_set_fd_handler (rdma->channel->fd,NULL ,NULL ,NULL ); + }g_free (rdma->dest_blocks); rdma->dest_blocks =NULL ;
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 03/22] migration: Cleanup during exit
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Currently we cleanup the migration object as we exit main after the main_loop finishes; however if there's a migration running things get messy and we can end up with the migration thread still trying to access freed structures.
We now take a ref to the object around the migration thread itself, so the act of dropping the ref during exit doesn't cause us to lose the state until the thread quits.
Cancelling the migration during migration also tries to get the thread to quit.
We do this a bit earlier; so hopefully migration gets out of the way before all the devices etc are freed.
Message-Id: <[email protected]>
--- include/migration/misc.h | 2 +- migration/migration.c | 10 +++++++++- vl.c | 7 ++++++- 3 files changed, 16 insertions(+), 3 deletions(-)
include/migration/misc.h +1 -1
@@ -36,7 +36,7 @@ void dump_vmstate_json_to_file(FILE *out_fp);/* migration/migration.c */ void migration_object_init (void ); -void migration_object_finalize (void ); +void migration_shutdown (void );void qemu_start_incoming_migration (const char *uri,Error **errp);bool migration_is_idle (void );void add_migration_state_change_notifier (Notifier *notify);
migration/migration.c +9 -1
@@ -126,6 +126,7 @@ static bool migration_object_check(MigrationState *ms, Error **errp);static int migration_maybe_pause (MigrationState *s,int *current_active_state,int new_state); +static void migrate_fd_cancel (MigrationState *s);void migration_object_init (void ) {
@@ -167,8 +168,13 @@ void migration_object_init(void) } } -void migration_object_finalize (void ) +void migration_shutdown (void ) { +/* +* Cancel the current migration - that will (eventually) +* stop the migration using this structure +*/ +migrate_fd_cancel (current_migration);object_unref (OBJECT (current_migration)); }
@@ -3134,6 +3140,7 @@ static void *migration_thread(void *opaque)rcu_register_thread (); +object_ref (OBJECT (s)); s->iteration_start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );qemu_savevm_state_header (s->to_dst_file);
@@ -3230,6 +3237,7 @@ static void *migration_thread(void *opaque)trace_migration_thread_after_loop ();migration_iteration_finish (s); +object_unref (OBJECT (s));rcu_unregister_thread ();return NULL ; }
vl.c +6 -1
@@ -4579,6 +4579,12 @@ int main(int argc, char **argv, char **envp)gdbserver_cleanup (); +/* +* cleaning up the migration object cancels any existing migration +* try to do this early so that it also stops using devices. +*/ +migration_shutdown (); +/* No more vcpu or device emulation activity beyond this point */ vm_shutdown ();
@@ -4594,7 +4600,6 @@ int main(int argc, char **argv, char **envp)monitor_cleanup ();qemu_chr_cleanup ();user_creatable_cleanup (); -migration_object_finalize ();/* TODO: unref root container, check all devices are ok */ return 0 ;
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 04/22] migration/rdma: clang compilation fix
In reply to Dr. David Alan Gilbert (git)From: Marcel Apfelbaum <[email protected]>
Configuring QEMU with:
../configure --cc=clang --enable-rdma
Leads to compilation error:
CC migration/rdma.o
CC migration/block.o
qemu/migration/rdma.c:3615:58: error: taking address of packed member 'rkey' of class or structure
'RDMARegisterResult' may result in an unaligned pointer value [-Werror,-Waddress-of-packed-member]
(uintptr_t)host_addr, NULL, ®_result->rkey,
^~~~~~~~~~~~~~~~Fix it by using a temp local variable.
Message-Id: <[email protected]>
--- migration/rdma.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
migration/rdma.c +4 -1
@@ -3613,13 +3613,16 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque) } chunk_start =ram_chunk_start (block, chunk); chunk_end =ram_chunk_end (block, chunk + reg->chunks); +/* avoid "-Waddress-of-packed-member" warning */ +uint32_t tmp_rkey =0 ;if (qemu_rdma_register_and_get_keys (rdma, block, - (uintptr_t )host_addr,NULL , ®_result->rkey, + (uintptr_t )host_addr,NULL , &tmp_rkey, chunk, chunk_start, chunk_end)) {error_report ("cannot get rkey" ); ret = -EINVAL ; goto out; } + reg_result->rkey = tmp_rkey; reg_result->host_addr = (uintptr_t )block->local_host_addr;
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 05/22] exec: Change RAMBlockIterFunc definition
In reply to Dr. David Alan Gilbert (git)From: Yury Kotov <[email protected]>
Currently, qemu_ram_foreach_* calls RAMBlockIterFunc with many block-specific arguments. But often iter func needs RAMBlock*. This refactoring is needed for fast access to RAMBlock flags from qemu_ram_foreach_block's callback. The only way to achieve this now is to call qemu_ram_block_from_host (which also enumerates blocks).
So, this patch reduces complexity of qemu_ram_foreach_block() -> cb() -> qemu_ram_block_from_host() from O(n^2) to O(n).
Fix RAMBlockIterFunc definition and add some functions to read RAMBlock* fields witch were passed.
Message-Id: <[email protected]>
--- exec.c | 21 +++++++++++++++++---- include/exec/cpu-common.h | 6 ++++-- migration/postcopy-ram.c | 36 +++++++++++++++++++++--------------- migration/rdma.c | 7 +++++-- stubs/ram-block.c | 15 +++++++++++++++ util/vfio-helpers.c | 6 +++--- 6 files changed, 65 insertions(+), 26 deletions(-)
exec.c +17 -4
@@ -1972,6 +1972,21 @@ const char *qemu_ram_get_idstr(RAMBlock *rb)return rb->idstr; } +void *qemu_ram_get_host_addr (RAMBlock *rb) +{ +return rb->host; +} + +ram_addr_t qemu_ram_get_offset (RAMBlock *rb) +{ +return rb->offset; +} + +ram_addr_t qemu_ram_get_used_length (RAMBlock *rb) +{ +return rb->used_length; +} +bool qemu_ram_is_shared (RAMBlock *rb) {return rb->flags &RAM_SHARED ;
@@ -3961,8 +3976,7 @@ int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)rcu_read_lock ();RAMBLOCK_FOREACH (block) { - ret =func (block->idstr, block->host, block->offset, - block->used_length, opaque); + ret =func (block, opaque);if (ret) {break ; }
@@ -3981,8 +3995,7 @@ int qemu_ram_foreach_migratable_block(RAMBlockIterFunc func, void *opaque)if (!qemu_ram_is_migratable (block)) {continue ; } - ret =func (block->idstr, block->host, block->offset, - block->used_length, opaque); + ret =func (block, opaque);if (ret) {break ; }
include/exec/cpu-common.h +4 -2
@@ -72,6 +72,9 @@ ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, void *host);void qemu_ram_set_idstr (RAMBlock *block,const char *name,DeviceState *dev);void qemu_ram_unset_idstr (RAMBlock *block);const char *qemu_ram_get_idstr (RAMBlock *rb); +void *qemu_ram_get_host_addr (RAMBlock *rb); +ram_addr_t qemu_ram_get_offset (RAMBlock *rb); +ram_addr_t qemu_ram_get_used_length (RAMBlock *rb);bool qemu_ram_is_shared (RAMBlock *rb);bool qemu_ram_is_uf_zeroable (RAMBlock *rb);void qemu_ram_set_uf_zeroable (RAMBlock *rb);
@@ -116,8 +119,7 @@ void cpu_flush_icache_range(hwaddr start, hwaddr len);extern struct MemoryRegion io_mem_rom;extern struct MemoryRegion io_mem_notdirty; -typedef int (RAMBlockIterFunc )(const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length,void *opaque); +typedef int (RAMBlockIterFunc )(RAMBlock *rb,void *opaque);int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque);int qemu_ram_foreach_migratable_block (RAMBlockIterFunc func,void *opaque);
migration/postcopy-ram.c +21 -15
@@ -319,10 +319,10 @@ static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)/* Callback from postcopy_ram_supported_by_host block iterator. */ -static int test_ramblock_postcopiable (const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length,void *opaque) +static int test_ramblock_postcopiable (RAMBlock *rb,void *opaque) { -RAMBlock *rb =qemu_ram_block_by_name (block_name); +const char *block_name =qemu_ram_get_idstr (rb); +ram_addr_t length =qemu_ram_get_used_length (rb);size_t pagesize =qemu_ram_pagesize (rb);if (length % pagesize) {
@@ -443,9 +443,12 @@ out:* must be done right at the start prior to pre-copy. * opaque should be the MIS. */ -static int init_range (const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length,void *opaque) +static int init_range (RAMBlock *rb,void *opaque) { +const char *block_name =qemu_ram_get_idstr (rb); +void *host_addr =qemu_ram_get_host_addr (rb); +ram_addr_t offset =qemu_ram_get_offset (rb); +ram_addr_t length =qemu_ram_get_used_length (rb);trace_postcopy_init_range (block_name, host_addr, offset, length);/*
@@ -465,9 +468,12 @@ static int init_range(const char *block_name, void *host_addr,* At the end of migration, undo the effects of init_range * opaque should be the MIS. */ -static int cleanup_range (const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length,void *opaque) +static int cleanup_range (RAMBlock *rb,void *opaque) { +const char *block_name =qemu_ram_get_idstr (rb); +void *host_addr =qemu_ram_get_host_addr (rb); +ram_addr_t offset =qemu_ram_get_offset (rb); +ram_addr_t length =qemu_ram_get_used_length (rb);MigrationIncomingState *mis = opaque;struct uffdio_range range_struct;trace_postcopy_cleanup_range (block_name, host_addr, offset, length);
@@ -586,9 +592,12 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)/* * Disable huge pages on an area */ -static int nhp_range (const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length,void *opaque) +static int nhp_range (RAMBlock *rb,void *opaque) { +const char *block_name =qemu_ram_get_idstr (rb); +void *host_addr =qemu_ram_get_host_addr (rb); +ram_addr_t offset =qemu_ram_get_offset (rb); +ram_addr_t length =qemu_ram_get_used_length (rb);trace_postcopy_nhp_range (block_name, host_addr, offset, length);/*
@@ -626,15 +635,13 @@ int postcopy_ram_prepare_discard(MigrationIncomingState *mis)* opaque: MigrationIncomingState pointer * Returns 0 on success */ -static int ram_block_enable_notify (const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length, -void *opaque) +static int ram_block_enable_notify (RAMBlock *rb,void *opaque) {MigrationIncomingState *mis = opaque;struct uffdio_register reg_struct; - reg_struct.range.start = (uintptr_t )host_addr; - reg_struct.range.len = length; + reg_struct.range.start = (uintptr_t )qemu_ram_get_host_addr (rb); + reg_struct.range.len =qemu_ram_get_used_length (rb); reg_struct.mode =UFFDIO_REGISTER_MODE_MISSING ;/* Now tell our userfault_fd that it's responsible for this area */
@@ -647,7 +654,6 @@ static int ram_block_enable_notify(const char *block_name, void *host_addr,return -1 ; }if (reg_struct.ioctls & ((__u64 )1 << _UFFDIO_ZEROPAGE)) { -RAMBlock *rb =qemu_ram_block_by_name (block_name);qemu_ram_set_uf_zeroable (rb); }
migration/rdma.c +5 -2
@@ -624,9 +624,12 @@ static int rdma_add_block(RDMAContext *rdma, const char *block_name,* in advanced before the migration starts. This tells us where the RAM blocks * are so that we can register them individually. */ -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) +static int qemu_rdma_init_one_block (RAMBlock *rb,void *opaque) { +const char *block_name =qemu_ram_get_idstr (rb); +void *host_addr =qemu_ram_get_host_addr (rb); +ram_addr_t block_offset =qemu_ram_get_offset (rb); +ram_addr_t length =qemu_ram_get_used_length (rb);return rdma_add_block (opaque, block_name, host_addr, block_offset, length); }
stubs/ram-block.c +15 -0
@@ -2,6 +2,21 @@#include "exec/ramlist.h" #include "exec/cpu-common.h" +void *qemu_ram_get_host_addr (RAMBlock *rb) +{ +return 0 ; +} + +ram_addr_t qemu_ram_get_offset (RAMBlock *rb) +{ +return 0 ; +} + +ram_addr_t qemu_ram_get_used_length (RAMBlock *rb) +{ +return 0 ; +} +void ram_block_notifier_add (RAMBlockNotifier *n) { }
util/vfio-helpers.c +3 -3
@@ -391,10 +391,10 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, } } -static int qemu_vfio_init_ramblock (const char *block_name,void *host_addr, -ram_addr_t offset,ram_addr_t length, -void *opaque) +static int qemu_vfio_init_ramblock (RAMBlock *rb,void *opaque) { +void *host_addr =qemu_ram_get_host_addr (rb); +ram_addr_t length =qemu_ram_get_used_length (rb);int ret;QEMUVFIOState *s = opaque;
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 06/22] migration: Introduce ignore-shared capability
In reply to Dr. David Alan Gilbert (git)From: Yury Kotov <[email protected]>
We want to use local migration to update QEMU for running guests. In this case we don't need to migrate shared (file backed) RAM. So, add a capability to ignore such blocks during live migration.
Message-Id: <[email protected]>
--- migration/migration.c | 14 ++++++++++++++ migration/migration.h | 1 + qapi/migration.json | 5 ++++- 3 files changed, 19 insertions(+), 1 deletion(-)
migration/migration.c +14 -0
@@ -995,6 +995,11 @@ static bool migrate_caps_check(bool *cap_list,error_setg (errp,"Postcopy is not supported" );return false; } + +if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED ]) { +error_setg (errp,"Postcopy is not compatible with ignore-shared" ); +return false; + } }return true;
@@ -2074,6 +2079,15 @@ bool migrate_dirty_bitmaps(void)return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS ]; } +bool migrate_ignore_shared (void ) +{ +MigrationState *s; + + s =migrate_get_current (); + +return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED ]; +} +bool migrate_use_events (void ) {MigrationState *s;
migration/migration.h +1 -0
@@ -265,6 +265,7 @@ bool migrate_release_ram(void);bool migrate_postcopy_ram (void );bool migrate_zero_blocks (void );bool migrate_dirty_bitmaps (void ); +bool migrate_ignore_shared (void );bool migrate_auto_converge (void );bool migrate_use_multifd (void );
qapi/migration.json +4 -1
@@ -409,13 +409,16 @@ # devices (and thus take locks) immediately at the end of migration. # (since 3.0) # +# @x-ignore-shared: If enabled, QEMU will not migrate shared memory (since 4.0) +# # Since: 1.2 ## { 'enum': 'MigrationCapability', 'data': ['xbzrle', 'rdma-pin-all', 'auto-converge', 'zero-blocks', 'compress', 'events', 'postcopy-ram', 'x-colo', 'release-ram', 'block', 'return-path', 'pause-before-switchover', 'x-multifd', - 'dirty-bitmaps', 'postcopy-blocktime', 'late-block-activate' ] } + 'dirty-bitmaps', 'postcopy-blocktime', 'late-block-activate', + 'x-ignore-shared' ] } ## # @MigrationCapabilityStatus:
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 07/22] migration: Add an ability to ignore shared RAM blocks
In reply to Dr. David Alan Gilbert (git)From: Yury Kotov <[email protected]>
If ignore-shared capability is set then skip shared RAMBlocks during the RAM migration.
Also, move qemu_ram_foreach_migratable_block (and rename) to the migration code, because it requires access to the migration capabilities.
Message-Id: <[email protected]>
--- exec.c | 19 ------- include/exec/cpu-common.h | 1 - migration/migration.h | 4 +- migration/postcopy-ram.c | 12 ++--- migration/ram.c | 110 +++++++++++++++++++++++++++++--------- migration/rdma.c | 2 +- 6 files changed, 94 insertions(+), 54 deletions(-)
exec.c +0 -19
@@ -3985,25 +3985,6 @@ int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)return ret; } -int qemu_ram_foreach_migratable_block (RAMBlockIterFunc func,void *opaque) -{ -RAMBlock *block; -int ret =0 ; - -rcu_read_lock (); -RAMBLOCK_FOREACH (block) { -if (!qemu_ram_is_migratable (block)) { -continue ; - } - ret =func (block, opaque); -if (ret) { -break ; - } - } -rcu_read_unlock (); -return ret; -} -/* * Unmap pages of memory from start to start+length such that * they a) read as 0, b) Trigger whatever fault mechanism
include/exec/cpu-common.h +0 -1
@@ -122,7 +122,6 @@ extern struct MemoryRegion io_mem_notdirty;typedef int (RAMBlockIterFunc )(RAMBlock *rb,void *opaque);int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque); -int qemu_ram_foreach_migratable_block (RAMBlockIterFunc func,void *opaque);int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length);#endif
migration/migration.h +3 -1
@@ -306,8 +306,10 @@ void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);void dirty_bitmap_mig_before_vm_start (void );void init_dirty_bitmap_incoming_migration (void ); +int foreach_not_ignored_block (RAMBlockIterFunc func,void *opaque); +#define qemu_ram_foreach_block \ - #warning "Use qemu_ram_foreach_block_migratable in migration code" + #warning "Use foreach_not_ignored_block in migration code"void migration_make_urgent_request (void );void migration_consume_urgent_request (void );
migration/postcopy-ram.c +6 -6
@@ -374,7 +374,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingState *mis) }/* We don't support postcopy with shared RAM yet */ -if (qemu_ram_foreach_migratable_block (test_ramblock_postcopiable,NULL )) { +if (foreach_not_ignored_block (test_ramblock_postcopiable,NULL )) { goto out; }
@@ -508,7 +508,7 @@ static int cleanup_range(RAMBlock *rb, void *opaque)*/ int postcopy_ram_incoming_init (MigrationIncomingState *mis) { -if (qemu_ram_foreach_migratable_block (init_range,NULL )) { +if (foreach_not_ignored_block (init_range,NULL )) {return -1 ; }
@@ -550,7 +550,7 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)return -1 ; } -if (qemu_ram_foreach_migratable_block (cleanup_range, mis)) { +if (foreach_not_ignored_block (cleanup_range, mis)) {return -1 ; }
@@ -617,7 +617,7 @@ static int nhp_range(RAMBlock *rb, void *opaque)*/ int postcopy_ram_prepare_discard (MigrationIncomingState *mis) { -if (qemu_ram_foreach_migratable_block (nhp_range, mis)) { +if (foreach_not_ignored_block (nhp_range, mis)) {return -1 ; }
@@ -628,7 +628,7 @@ int postcopy_ram_prepare_discard(MigrationIncomingState *mis)/* * Mark the given area of RAM as requiring notification to unwritten areas -* Used as a callback on qemu_ram_foreach_migratable_block. +* Used as a callback on foreach_not_ignored_block. * host_addr: Base of area to mark * offset: Offset in the whole ram arena * length: Length of the section
@@ -1122,7 +1122,7 @@ int postcopy_ram_enable_notify(MigrationIncomingState *mis) mis->have_fault_thread = true;/* Mark so that we get notified of accesses to unwritten areas */ -if (qemu_ram_foreach_migratable_block (ram_block_enable_notify, mis)) { +if (foreach_not_ignored_block (ram_block_enable_notify, mis)) {error_report ("ram_block_enable_notify failed" );return -1 ; }
migration/ram.c +84 -26
287 lines not on this page · raw message
migration/rdma.c +1 -1
@@ -644,7 +644,7 @@ static int qemu_rdma_init_ram_blocks(RDMAContext *rdma)assert (rdma->blockmap ==NULL );memset (local,0 ,sizeof *local); -qemu_ram_foreach_migratable_block (qemu_rdma_init_one_block, rdma); +foreach_not_ignored_block (qemu_rdma_init_one_block, rdma);trace_qemu_rdma_init_ram_blocks (local->nb_blocks); rdma->dest_blocks =g_new0 (RDMADestBlock, rdma->local_ram_blocks.nb_blocks);
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 08/22] tests/migration-test: Add a test for ignore-shared capability
In reply to Dr. David Alan Gilbert (git)From: Yury Kotov <[email protected]>
Message-Id: <[email protected]>
dgilbert: Disabled the test for now, not happy on aarch64
--- tests/migration-test.c | 134 +++++++++++++++++++++++++++++++++-------- 1 file changed, 109 insertions(+), 25 deletions(-)
tests/migration-test.c +109 -25
@@ -215,10 +215,10 @@ static gchar *migrate_query_status(QTestState *who)* events suddenly appearing confuse the qmp()/hmp() responses. */ -static uint64_t get_migration_pass (QTestState *who) +static int64_t read_ram_property_int (QTestState *who,const char *property) {QDict *rsp_return, *rsp_ram; -uint64_t result; +int64_t result; rsp_return =migrate_query (who);if (!qdict_haskey (rsp_return,"ram" )) {
@@ -226,12 +226,17 @@ static uint64_t get_migration_pass(QTestState *who) result =0 ; }else { rsp_ram =qdict_get_qdict (rsp_return,"ram" ); - result =qdict_get_try_int (rsp_ram,"dirty-sync-count" ,0 ); + result =qdict_get_try_int (rsp_ram, property,0 ); }qobject_unref (rsp_return);return result; } +static uint64_t get_migration_pass (QTestState *who) +{ +return read_ram_property_int (who,"dirty-sync-count" ); +} +static void read_blocktime (QTestState *who) {QDict *rsp_return;
@@ -332,6 +337,13 @@ static void cleanup(const char *filename)g_free (path); } +static char *get_shmem_opts (const char *mem_size,const char *shmem_path) +{ +return g_strdup_printf ("-object memory-backend-file,id=mem0,size=%s" +",mem-path=%s,share=on -numa node,memdev=mem0" , + mem_size, shmem_path); +} +static void migrate_check_parameter (QTestState *who,const char *parameter,long long value) {
@@ -430,73 +442,95 @@ static void migrate_postcopy_start(QTestState *from, QTestState *to) }static int test_migrate_start (QTestState **from,QTestState **to, -const char *uri,bool hide_stderr) +const char *uri,bool hide_stderr, +bool use_shmem) {gchar *cmd_src, *cmd_dst; -char *bootpath =g_strdup_printf ("%s/bootsect" , tmpfs); +char *bootpath =NULL ; +char *extra_opts =NULL ; +char *shmem_path =NULL ;const char *arch =qtest_get_arch ();const char *accel ="kvm:tcg" ; - got_stop = false; +if (use_shmem) { +if (!g_file_test ("/dev/shm" ,G_FILE_TEST_IS_DIR )) { +g_test_skip ("/dev/shm is not supported" ); +return -1 ; + } + shmem_path =g_strdup_printf ("/dev/shm/qemu-%d" ,getpid ()); + } + got_stop = false; + bootpath =g_strdup_printf ("%s/bootsect" , tmpfs);if (strcmp (arch,"i386" ) ==0 ||strcmp (arch,"x86_64" ) ==0 ) {init_bootfile (bootpath, x86_bootsect); + extra_opts = use_shmem ?get_shmem_opts ("150M" , shmem_path) :NULL ; cmd_src =g_strdup_printf ("-machine accel=%s -m 150M" " -name source,debug-threads=on" " -serial file:%s/src_serial" -" -drive file=%s,format=raw" , - accel, tmpfs, bootpath); +" -drive file=%s,format=raw %s" , + accel, tmpfs, bootpath, + extra_opts ? extra_opts :"" ); cmd_dst =g_strdup_printf ("-machine accel=%s -m 150M" " -name target,debug-threads=on" " -serial file:%s/dest_serial" " -drive file=%s,format=raw" -" -incoming %s" , - accel, tmpfs, bootpath, uri); +" -incoming %s %s" , + accel, tmpfs, bootpath, uri, + extra_opts ? extra_opts :"" ); start_address =X86_TEST_MEM_START ; end_address =X86_TEST_MEM_END ; }else if (g_str_equal (arch,"s390x" )) {init_bootfile_s390x (bootpath); + extra_opts = use_shmem ?get_shmem_opts ("128M" , shmem_path) :NULL ; cmd_src =g_strdup_printf ("-machine accel=%s -m 128M" " -name source,debug-threads=on" -" -serial file:%s/src_serial -bios %s" , - accel, tmpfs, bootpath); +" -serial file:%s/src_serial -bios %s %s" , + accel, tmpfs, bootpath, + extra_opts ? extra_opts :"" ); cmd_dst =g_strdup_printf ("-machine accel=%s -m 128M" " -name target,debug-threads=on" " -serial file:%s/dest_serial -bios %s" -" -incoming %s" , - accel, tmpfs, bootpath, uri); +" -incoming %s %s" , + accel, tmpfs, bootpath, uri, + extra_opts ? extra_opts :"" ); start_address =S390_TEST_MEM_START ; end_address =S390_TEST_MEM_END ; }else if (strcmp (arch,"ppc64" ) ==0 ) { + extra_opts = use_shmem ?get_shmem_opts ("256M" , shmem_path) :NULL ; cmd_src =g_strdup_printf ("-machine accel=%s -m 256M -nodefaults" " -name source,debug-threads=on" " -serial file:%s/src_serial" " -prom-env 'use-nvramrc?=true' -prom-env " "'nvramrc=hex .\" _\" begin %x %x " "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " -"until'" , accel, tmpfs, end_address, - start_address); +"until' %s" , accel, tmpfs, end_address, + start_address, extra_opts ? extra_opts :"" ); cmd_dst =g_strdup_printf ("-machine accel=%s -m 256M" " -name target,debug-threads=on" " -serial file:%s/dest_serial" -" -incoming %s" , - accel, tmpfs, uri); +" -incoming %s %s" , + accel, tmpfs, uri, + extra_opts ? extra_opts :"" ); start_address =PPC_TEST_MEM_START ; end_address =PPC_TEST_MEM_END ; }else if (strcmp (arch,"aarch64" ) ==0 ) {init_bootfile (bootpath, aarch64_kernel); + extra_opts = use_shmem ?get_shmem_opts ("150M" , shmem_path) :NULL ; cmd_src =g_strdup_printf ("-machine virt,accel=%s,gic-version=max " "-name vmsource,debug-threads=on -cpu max " "-m 150M -serial file:%s/src_serial " -"-kernel %s " , - accel, tmpfs, bootpath); +"-kernel %s %s" , + accel, tmpfs, bootpath, + extra_opts ? extra_opts :"" ); cmd_dst =g_strdup_printf ("-machine virt,accel=%s,gic-version=max " "-name vmdest,debug-threads=on -cpu max " "-m 150M -serial file:%s/dest_serial " "-kernel %s " -"-incoming %s " , - accel, tmpfs, bootpath, uri); +"-incoming %s %s" , + accel, tmpfs, bootpath, uri, + extra_opts ? extra_opts :"" ); start_address =ARM_TEST_MEM_START ; end_address =ARM_TEST_MEM_END ;
@@ -507,6 +541,7 @@ static int test_migrate_start(QTestState **from, QTestState **to, }g_free (bootpath); +g_free (extra_opts);if (hide_stderr) {gchar *tmp;
@@ -524,6 +559,16 @@ static int test_migrate_start(QTestState **from, QTestState **to, *to =qtest_init (cmd_dst);g_free (cmd_dst); + +/* +* Remove shmem file immediately to avoid memory leak in test failed case. +* It's valid becase QEMU has already opened this file +*/ +if (use_shmem) { +unlink (shmem_path); +g_free (shmem_path); + } +return 0 ; }
@@ -603,7 +648,7 @@ static int migrate_postcopy_prepare(QTestState **from_ptr,char *uri =g_strdup_printf ("unix:%s/migsocket" , tmpfs);QTestState *from, *to; -if (test_migrate_start (&from, &to, uri, hide_error)) { +if (test_migrate_start (&from, &to, uri, hide_error, false)) {return -1 ; }
@@ -720,7 +765,7 @@ static void test_baddest(void)char *status;bool failed; -if (test_migrate_start (&from, &to,"tcp:0:0" , true)) { +if (test_migrate_start (&from, &to,"tcp:0:0" , true, false)) {return ; }migrate (from,"tcp:0:0" ,"{}" );
@@ -745,7 +790,7 @@ static void test_precopy_unix(void)char *uri =g_strdup_printf ("unix:%s/migsocket" , tmpfs);QTestState *from, *to; -if (test_migrate_start (&from, &to, uri, false)) { +if (test_migrate_start (&from, &to, uri, false, false)) {return ; }
@@ -781,6 +826,44 @@ static void test_precopy_unix(void)g_free (uri); } +#if 0 +/* Currently upset on aarch64 TCG */ +static void test_ignore_shared (void ) +{ +char *uri =g_strdup_printf ("unix:%s/migsocket" , tmpfs); +QTestState *from, *to; + +if (test_migrate_start (&from, &to, uri, false, true)) { +return ; + } + +migrate_set_capability (from,"x-ignore-shared" , true); +migrate_set_capability (to,"x-ignore-shared" , true); + +/* Wait for the first serial output from the source */ +wait_for_serial ("src_serial" ); + +migrate (from, uri,"{}" ); + +wait_for_migration_pass (from); + +if (!got_stop) { +qtest_qmp_eventwait (from,"STOP" ); + } + +qtest_qmp_eventwait (to,"RESUME" ); + +wait_for_serial ("dest_serial" ); +wait_for_migration_complete (from); + +/* Check whether shared RAM has been really skipped */ + g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024); + +test_migrate_end (from, to, true); +g_free (uri); +} +#endif +int main (int argc,char **argv) {char template[] ="/tmp/migration-test-XXXXXX" ;
@@ -832,6 +915,7 @@ int main(int argc, char **argv)qtest_add_func ("/migration/deprecated" , test_deprecated);qtest_add_func ("/migration/bad_dest" , test_baddest);qtest_add_func ("/migration/precopy/unix" , test_precopy_unix); +/* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ ret =g_test_run ();
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 09/22] migration: Add capabilities validation
In reply to Dr. David Alan Gilbert (git)From: Yury Kotov <[email protected]>
Currently we don't check which capabilities set in the source QEMU. We just expect that the target QEMU has the same enabled capabilities.
Add explicit validation for capabilities to make sure that the target VM has them too. This is enabled for only new capabilities to keep compatibily.
Message-Id: <[email protected]>
dgilbert: Manual merge
--- migration/savevm.c | 137 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+)
migration/savevm.c +137 -0
@@ -57,6 +57,7 @@#include "sysemu/replay.h" #include "qjson.h" #include "migration/colo.h" +#include "qemu/bitmap.h" #include "net/announce.h" const unsigned int postcopy_ram_discard_version =0 ;
@@ -249,6 +250,8 @@ typedef struct SaveState {uint32_t len;const char *name;uint32_t target_page_bits; +uint32_t caps_count; +MigrationCapability *capabilities; } SaveState;static SaveState savevm_state = {
@@ -256,15 +259,51 @@ static SaveState savevm_state = { .global_section_id = 0, }; +static bool should_validate_capability (int capability) +{ +assert (capability >=0 && capability <MIGRATION_CAPABILITY__MAX ); +/* Validate only new capabilities to keep compatibility. */ +switch (capability) { +case MIGRATION_CAPABILITY_X_IGNORE_SHARED : +return true; +default : +return false; + } +} + +static uint32_t get_validatable_capabilities_count (void ) +{ +MigrationState *s =migrate_get_current (); +uint32_t result =0 ; +int i; +for (i =0 ; i <MIGRATION_CAPABILITY__MAX ; i++) { +if (should_validate_capability (i) && s->enabled_capabilities[i]) { + result++; + } + } +return result; +} +static int configuration_pre_save (void *opaque) {SaveState *state = opaque;const char *current_name =MACHINE_GET_CLASS (current_machine)->name; +MigrationState *s =migrate_get_current (); +int i, j; state->len =strlen (current_name); state->name = current_name; state->target_page_bits =qemu_target_page_bits (); + state->caps_count =get_validatable_capabilities_count (); + state->capabilities =g_renew (MigrationCapability, state->capabilities, + state->caps_count); +for (i = j =0 ; i <MIGRATION_CAPABILITY__MAX ; i++) { +if (should_validate_capability (i) && s->enabled_capabilities[i]) { + state->capabilities[j++] = i; + } + } +return 0 ; }
@@ -280,6 +319,40 @@ static int configuration_pre_load(void *opaque)return 0 ; } +static bool configuration_validate_capabilities (SaveState *state) +{ +bool ret = true; +MigrationState *s =migrate_get_current (); +unsigned long *source_caps_bm; +int i; + + source_caps_bm =bitmap_new (MIGRATION_CAPABILITY__MAX ); +for (i =0 ; i < state->caps_count; i++) { +MigrationCapability capability = state->capabilities[i]; +set_bit (capability, source_caps_bm); + } + +for (i =0 ; i <MIGRATION_CAPABILITY__MAX ; i++) { +bool source_state, target_state; +if (!should_validate_capability (i)) { +continue ; + } + source_state =test_bit (i, source_caps_bm); + target_state = s->enabled_capabilities[i]; +if (source_state != target_state) { +error_report ("Capability %s is %s, but received capability is %s" , +MigrationCapability_str (i), + target_state ?"on" :"off" , + source_state ?"on" :"off" ); + ret = false; +/* Don't break here to report all failed capabilities */ + } + } + +g_free (source_caps_bm); +return ret; +} +static int configuration_post_load (void *opaque,int version_id) {SaveState *state = opaque;
@@ -297,9 +370,53 @@ static int configuration_post_load(void *opaque, int version_id)return -EINVAL ; } +if (!configuration_validate_capabilities (state)) { +return -EINVAL ; + } +return 0 ; } +static int get_capability (QEMUFile *f,void *pv,size_t size, +const VMStateField *field) +{ +MigrationCapability *capability = pv; +char capability_str[UINT8_MAX +1 ]; +uint8_t len; +int i; + + len =qemu_get_byte (f); +qemu_get_buffer (f, (uint8_t *)capability_str, len); + capability_str[len] ='\0' ; +for (i =0 ; i <MIGRATION_CAPABILITY__MAX ; i++) { +if (!strcmp (MigrationCapability_str (i), capability_str)) { + *capability = i; +return 0 ; + } + } +error_report ("Received unknown capability %s" , capability_str); +return -EINVAL ; +} + +static int put_capability (QEMUFile *f,void *pv,size_t size, +const VMStateField *field,QJSON *vmdesc) +{ +MigrationCapability *capability = pv; +const char *capability_str =MigrationCapability_str (*capability); +size_t len =strlen (capability_str); +assert (len <=UINT8_MAX ); + +qemu_put_byte (f, len); +qemu_put_buffer (f, (uint8_t *)capability_str, len); +return 0 ; +} + +static const VMStateInfo vmstate_info_capability = { + .name ="capability" , + .get = get_capability, + .put = put_capability, +}; +/* The target-page-bits subsection is present only if the * target page size is not the same as the default (ie the * minimum page size for a variable-page-size guest CPU).
@@ -324,6 +441,25 @@ static const VMStateDescription vmstate_target_page_bits = { } }; +static bool vmstate_capabilites_needed (void *opaque) +{ +return get_validatable_capabilities_count () >0 ; +} + +static const VMStateDescription vmstate_capabilites = { + .name ="configuration/capabilities" , + .version_id =1 , + .minimum_version_id =1 , + .needed = vmstate_capabilites_needed, + .fields = (VMStateField []) { +VMSTATE_UINT32_V (caps_count, SaveState,1 ), +VMSTATE_VARRAY_UINT32_ALLOC (capabilities, SaveState, caps_count,1 , + vmstate_info_capability, + MigrationCapability), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_configuration = { .name ="configuration" , .version_id =1 ,
@@ -337,6 +473,7 @@ static const VMStateDescription vmstate_configuration = { }, .subsections = (const VMStateDescription*[]) { &vmstate_target_page_bits, + &vmstate_capabilites,NULL } };
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 10/22] tests: Add migration xbzrle test
In reply to Dr. David Alan Gilbert (git)From: Juan Quintela <[email protected]>
Message-Id: <[email protected]>
dgilbert: Fixup for class with Yury's series
--- tests/migration-test.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
tests/migration-test.c +64 -0
@@ -629,6 +629,17 @@ static void deprecated_set_speed(QTestState *who, long long value)migrate_check_parameter (who,"max-bandwidth" , value); } +static void deprecated_set_cache_size (QTestState *who,long long value) +{ +QDict *rsp; + + rsp =qtest_qmp (who,"{ 'execute': 'migrate-set-cache-size'," +"'arguments': { 'value': %lld } }" , value); +g_assert (qdict_haskey (rsp,"return" )); +qobject_unref (rsp); +migrate_check_parameter (who,"xbzrle-cache-size" , value); +} +static void test_deprecated (void ) {QTestState *from;
@@ -637,6 +648,7 @@ static void test_deprecated(void)deprecated_set_downtime (from,0.12345 );deprecated_set_speed (from,12345 ); +deprecated_set_cache_size (from,4096 );qtest_quit (from); }
@@ -864,6 +876,57 @@ static void test_ignore_shared(void) }#endif +static void test_xbzrle (const char *uri) +{ +QTestState *from, *to; + +if (test_migrate_start (&from, &to, uri, false, false)) { +return ; + } + +/* +* We want to pick a speed slow enough that the test completes +* quickly, but that it doesn't complete precopy even on a slow +* machine, so also set the downtime. +*/ +/* 1 ms should make it not converge*/ +migrate_set_parameter (from,"downtime-limit" ,1 ); +/* 1GB/s */ +migrate_set_parameter (from,"max-bandwidth" ,1000000000 ); + +migrate_set_parameter (from,"xbzrle-cache-size" ,33554432 ); + +migrate_set_capability (from,"xbzrle" ,"true" ); +migrate_set_capability (to,"xbzrle" ,"true" ); +/* Wait for the first serial output from the source */ +wait_for_serial ("src_serial" ); + +migrate (from, uri,"{}" ); + +wait_for_migration_pass (from); + +/* 300ms should converge */ +migrate_set_parameter (from,"downtime-limit" ,300 ); + +if (!got_stop) { +qtest_qmp_eventwait (from,"STOP" ); + } +qtest_qmp_eventwait (to,"RESUME" ); + +wait_for_serial ("dest_serial" ); +wait_for_migration_complete (from); + +test_migrate_end (from, to, true); +} + +static void test_xbzrle_unix (void ) +{ +char *uri =g_strdup_printf ("unix:%s/migsocket" , tmpfs); + +test_xbzrle (uri); +g_free (uri); +} +int main (int argc,char **argv) {char template[] ="/tmp/migration-test-XXXXXX" ;
@@ -916,6 +979,7 @@ int main(int argc, char **argv)qtest_add_func ("/migration/bad_dest" , test_baddest);qtest_add_func ("/migration/precopy/unix" , test_precopy_unix);/* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ +qtest_add_func ("/migration/xbzrle/unix" , test_xbzrle_unix); ret =g_test_run ();
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 11/22] migration: Create socket-address parameter
In reply to Dr. David Alan Gilbert (git)From: Juan Quintela <[email protected]>
It will be used to store the uri parameters. We want this only for tcp, so we don't set it for other uris. We need it to know what port is migration running.
dgilbert: Removed DummyStruct as suggested by Eric & Markus
--
--- hmp.c | 33 +++++++++++++++++++++++++++++++++ migration/migration.c | 24 ++++++++++++++++++++++++ migration/migration.h | 4 ++++ migration/socket.c | 11 +++++++++++ qapi/migration.json | 6 +++++- 5 files changed, 77 insertions(+), 1 deletion(-)
hmp.c +33 -0
@@ -166,6 +166,27 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)qapi_free_MouseInfoList (mice_list); } +static char *SocketAddress_to_str (SocketAddress *addr) +{ +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_INET : +return g_strdup_printf ("tcp:%s:%s" , + addr->u.inet.host, + addr->u.inet.port); +case SOCKET_ADDRESS_TYPE_UNIX : +return g_strdup_printf ("unix:%s" , + addr->u.q_unix.path); +case SOCKET_ADDRESS_TYPE_FD : +return g_strdup_printf ("fd:%s" , addr->u.fd.str); +case SOCKET_ADDRESS_TYPE_VSOCK : +return g_strdup_printf ("tcp:%s:%s" , + addr->u.vsock.cid, + addr->u.vsock.port); +default : +return g_strdup ("unknown address type" ); + } +} +void hmp_info_migrate (Monitor *mon,const QDict *qdict) {MigrationInfo *info;
@@ -306,6 +327,18 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict)g_free (str);visit_free (v); } +if (info->has_socket_address) { +SocketAddressList *addr; + +monitor_printf (mon,"socket address: [\n" ); + +for (addr = info->socket_address; addr; addr = addr->next) { +char *s =SocketAddress_to_str (addr->value); +monitor_printf (mon,"\t%s\n" , s); +g_free (s); + } +monitor_printf (mon,"]\n" ); + }qapi_free_MigrationInfo (info);qapi_free_MigrationCapabilityStatusList (caps); }
migration/migration.c +24 -0
@@ -31,6 +31,8 @@#include "migration/vmstate.h" #include "block/block.h" #include "qapi/error.h" +#include "qapi/clone-visitor.h" +#include "qapi/qapi-visit-sockets.h" #include "qapi/qapi-commands-migration.h" #include "qapi/qapi-events-migration.h" #include "qapi/qmp/qerror.h"
@@ -213,6 +215,11 @@ void migration_incoming_state_destroy(void) }qemu_event_reset (&mis->main_thread_load_event); + +if (mis->socket_address_list) { +qapi_free_SocketAddressList (mis->socket_address_list); + mis->socket_address_list =NULL ; + } }static void migrate_generate_event (int new_state)
@@ -328,6 +335,17 @@ void migration_incoming_enable_colo(void) migration_colo_enabled = true; } +void migrate_add_address (SocketAddress *address) +{ +MigrationIncomingState *mis =migration_incoming_get_current (); +SocketAddressList *addrs; + + addrs =g_new0 (SocketAddressList,1 ); + addrs->next = mis->socket_address_list; + mis->socket_address_list = addrs; + addrs->value =QAPI_CLONE (SocketAddress, address); +} +void qemu_start_incoming_migration (const char *uri,Error **errp) {const char *p;
@@ -1009,6 +1027,12 @@ static void fill_destination_migration_info(MigrationInfo *info) {MigrationIncomingState *mis =migration_incoming_get_current (); +if (mis->socket_address_list) { + info->has_socket_address = true; + info->socket_address = +QAPI_CLONE (SocketAddressList, mis->socket_address_list); + } +switch (mis->state) {case MIGRATION_STATUS_NONE :return ;
migration/migration.h +4 -0
@@ -84,6 +84,9 @@ struct MigrationIncomingState {bool postcopy_recover_triggered;QemuSemaphore postcopy_pause_sem_dst;QemuSemaphore postcopy_pause_sem_fault; + +/* List of listening socket addresses */ +SocketAddressList *socket_address_list; };MigrationIncomingState *migration_incoming_get_current (void );
@@ -305,6 +308,7 @@ void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);void dirty_bitmap_mig_before_vm_start (void );void init_dirty_bitmap_incoming_migration (void ); +void migrate_add_address (SocketAddress *address);int foreach_not_ignored_block (RAMBlockIterFunc func,void *opaque);
migration/socket.c +11 -0
@@ -15,6 +15,7 @@*/ #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "qemu-common.h" #include "qemu/error-report.h"
@@ -177,6 +178,7 @@ static void socket_start_incoming_migration(SocketAddress *saddr, Error **errp) {QIONetListener *listener =qio_net_listener_new (); +size_t i;qio_net_listener_set_name (listener,"migration-socket-listener" );
@@ -189,6 +191,15 @@ static void socket_start_incoming_migration(SocketAddress *saddr, socket_accept_incoming_migration,NULL ,NULL , g_main_context_get_thread_default()); + +for (i =0 ; i < listener->nsioc; i++) { +SocketAddress *address = +qio_channel_socket_get_local_address (listener->sioc[i], errp); +if (!address) { +return ; + } +migrate_add_address (address); + } }void tcp_start_incoming_migration (const char *host_port,Error **errp)
qapi/migration.json +5 -1
@@ -6,6 +6,7 @@ ## { 'include': 'common.json' } +{ 'include': 'sockets.json' } ## # @MigrationStats:
@@ -199,6 +200,8 @@ # @compression: migration compression statistics, only returned if compression # feature is on and status is 'active' or 'completed' (Since 3.1) # +# @socket-address: Only used for tcp, to know what the real port is (Since 4.0) +# # Since: 0.14.0 ## { 'struct': 'MigrationInfo',
@@ -213,7 +216,8 @@ '*error-desc': 'str', '*postcopy-blocktime' : 'uint32', '*postcopy-vcpu-blocktime': ['uint32'], - '*compression': 'CompressionStats'} } + '*compression': 'CompressionStats', + '*socket-address': ['SocketAddress'] } } ## # @query-migrate:
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 12/22] tests: Add basic migration precopy tcp test
In reply to Dr. David Alan Gilbert (git)From: Juan Quintela <[email protected]>
Not sharing code from precopy/unix because we have to read back the tcp parameter.
Message-Id: <[email protected]>
dgilbert: Fixup for clash with Yury's
--- tests/migration-test.c | 110 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 5 deletions(-)
tests/migration-test.c +105 -5
@@ -20,6 +20,9 @@#include "qemu/sockets.h" #include "chardev/char.h" #include "sysemu/sysemu.h" +#include "qapi/qapi-visit-sockets.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qobject-output-visitor.h" #include "migration/migration-test.h"
@@ -344,15 +347,68 @@ static char *get_shmem_opts(const char *mem_size, const char *shmem_path) mem_size, shmem_path); } +static char *SocketAddress_to_str (SocketAddress *addr) +{ +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_INET : +return g_strdup_printf ("tcp:%s:%s" , + addr->u.inet.host, + addr->u.inet.port); +case SOCKET_ADDRESS_TYPE_UNIX : +return g_strdup_printf ("unix:%s" , + addr->u.q_unix.path); +case SOCKET_ADDRESS_TYPE_FD : +return g_strdup_printf ("fd:%s" , addr->u.fd.str); +case SOCKET_ADDRESS_TYPE_VSOCK : +return g_strdup_printf ("tcp:%s:%s" , + addr->u.vsock.cid, + addr->u.vsock.port); +default : +return g_strdup ("unknown address type" ); + } +} + +static char *migrate_get_socket_address (QTestState *who,const char *parameter) +{ +QDict *rsp; +char *result; +Error *local_err =NULL ; +SocketAddressList *addrs; +Visitor *iv =NULL ; +QObject *object; + + rsp =migrate_query (who); + object =qdict_get (rsp, parameter); + + iv =qobject_input_visitor_new (object); +visit_type_SocketAddressList (iv,NULL , &addrs, &local_err); + +/* we are only using a single address */ + result =g_strdup_printf ("%s" ,SocketAddress_to_str (addrs->value)); + +qapi_free_SocketAddressList (addrs); +qobject_unref (rsp); +return result; +} + +static long long migrate_get_parameter (QTestState *who,const char *parameter) +{ +QDict *rsp; +long long result; + + rsp =wait_command (who,"{ 'execute': 'query-migrate-parameters' }" ); + result =qdict_get_int (rsp, parameter); +qobject_unref (rsp); +return result; +} +static void migrate_check_parameter (QTestState *who,const char *parameter,long long value) { -QDict *rsp_return; +long long result; - rsp_return =wait_command (who, -"{ 'execute': 'query-migrate-parameters' }" ); - g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value); -qobject_unref (rsp_return); + result =migrate_get_parameter (who, parameter); + g_assert_cmpint(result, ==, value); } static void migrate_set_parameter(QTestState *who, const char *parameter,
@@ -927,6 +983,49 @@ static void test_xbzrle_unix(void)g_free (uri); } +static void test_precopy_tcp (void ) +{ +char *uri; +QTestState *from, *to; + +if (test_migrate_start (&from, &to,"tcp:127.0.0.1:0" , false, false)) { +return ; + } + +/* +* We want to pick a speed slow enough that the test completes +* quickly, but that it doesn't complete precopy even on a slow +* machine, so also set the downtime. +*/ +/* 1 ms should make it not converge*/ +migrate_set_parameter (from,"downtime-limit" ,1 ); +/* 1GB/s */ +migrate_set_parameter (from,"max-bandwidth" ,1000000000 ); + +/* Wait for the first serial output from the source */ +wait_for_serial ("src_serial" ); + + uri =migrate_get_socket_address (to,"socket-address" ); + +migrate (from, uri,"{}" ); + +wait_for_migration_pass (from); + +/* 300ms should converge */ +migrate_set_parameter (from,"downtime-limit" ,300 ); + +if (!got_stop) { +qtest_qmp_eventwait (from,"STOP" ); + } +qtest_qmp_eventwait (to,"RESUME" ); + +wait_for_serial ("dest_serial" ); +wait_for_migration_complete (from); + +test_migrate_end (from, to, true); +g_free (uri); +} +int main (int argc,char **argv) {char template[] ="/tmp/migration-test-XXXXXX" ;
@@ -978,6 +1077,7 @@ int main(int argc, char **argv)qtest_add_func ("/migration/deprecated" , test_deprecated);qtest_add_func ("/migration/bad_dest" , test_baddest);qtest_add_func ("/migration/precopy/unix" , test_precopy_unix); +qtest_add_func ("/migration/precopy/tcp" , test_precopy_tcp);/* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ qtest_add_func ("/migration/xbzrle/unix" , test_xbzrle_unix);
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 13/22] bitmap: fix bitmap_count_one
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
BITMAP_LAST_WORD_MASK(nbits) returns 0xffffffff when "nbits=0", which makes bitmap_count_one fail to handle the "nbits=0" case. It appears to be preferred to remain BITMAP_LAST_WORD_MASK identical to the kernel implementation that it is ported from.
So this patch fixes bitmap_count_one to handle the nbits=0 case.
Inital Discussion Link:
https://www.mail-archive.com/[email protected]/msg554316.html
Message-Id: <[email protected]>
--- include/qemu/bitmap.h | 4 ++++ 1 file changed, 4 insertions(+)
include/qemu/bitmap.h +4 -0
@@ -221,6 +221,10 @@ static inline int bitmap_intersects(const unsigned long *src1,static inline long bitmap_count_one (const unsigned long *bitmap,long nbits) { +if (unlikely (!nbits)) { +return 0 ; + } +if (small_nbits (nbits)) {return ctpopl (*bitmap &BITMAP_LAST_WORD_MASK (nbits)); }else {
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 15/22] migration: use bitmap_mutex in migration_bitmap_clear_dirty
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
The bitmap mutex is used to synchronize threads to update the dirty bitmap and the migration_dirty_pages counter. For example, the free page optimization clears bits of free pages from the bitmap in an iothread context. This patch makes migration_bitmap_clear_dirty update the bitmap and counter under the mutex.
Message-Id: <[email protected]>
--- migration/ram.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
migration/ram.c +4 -1
@@ -342,7 +342,7 @@ struct RAMState {uint64_t target_page_count;/* number of dirty bits in the bitmap */ uint64_t migration_dirty_pages; -/* protects modification of the bitmap */ +/* Protects modification of the bitmap and migration dirty pages */ QemuMutex bitmap_mutex;/* The RAMBlock used in the last src_page_requests */ RAMBlock *last_req_rb;
@@ -1590,11 +1590,14 @@ static inline bool migration_bitmap_clear_dirty(RAMState *rs, {bool ret; +qemu_mutex_lock (&rs->bitmap_mutex); ret =test_and_clear_bit (page, rb->bmap);if (ret) { rs->migration_dirty_pages--; } +qemu_mutex_unlock (&rs->bitmap_mutex); +return ret; }
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 14/22] bitmap: bitmap_count_one_with_offset
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
Count the number of 1s in a bitmap starting from an offset.
Message-Id: <[email protected]>
--- include/qemu/bitmap.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/qemu/bitmap.h +13 -0
@@ -232,6 +232,19 @@ static inline long bitmap_count_one(const unsigned long *bitmap, long nbits) } } +static inline long bitmap_count_one_with_offset (const unsigned long *bitmap, +long offset,long nbits) +{ +long aligned_offset =QEMU_ALIGN_DOWN (offset,BITS_PER_LONG ); +long redundant_bits = offset - aligned_offset; +long bits_to_count = nbits + redundant_bits; +const unsigned long *bitmap_start = bitmap + + aligned_offset /BITS_PER_LONG ; + +return bitmap_count_one (bitmap_start, bits_to_count) - +bitmap_count_one (bitmap_start, redundant_bits); +} +void bitmap_set (unsigned long *map,long i,long len);void bitmap_set_atomic (unsigned long *map,long i,long len);void bitmap_clear (unsigned long *map,long start,long nr);
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 16/22] migration: API to clear bits of guest free pages from the dirty bitmap
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
This patch adds an API to clear bits corresponding to guest free pages from the dirty bitmap. Spilt the free page block if it crosses the QEMU RAMBlock boundary.
Message-Id: <[email protected]>
--- include/migration/misc.h | 2 ++ migration/ram.c | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)
include/migration/misc.h +2 -0
@@ -14,12 +14,14 @@#ifndef MIGRATION_MISC_H #define MIGRATION_MISC_H +#include "exec/cpu-common.h" #include "qemu/notify.h" #include "qapi/qapi-types-net.h" /* migration/ram.c */ void ram_mig_init (void ); +void qemu_guest_free_page_hint (void *addr,size_t len);/* migration/block.c */
migration/ram.c +47 -0
53 lines not on this page · raw message
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 17/22] migration/ram.c: add a notifier chain for precopy
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
This patch adds a notifier chain for the memory precopy. This enables various precopy optimizations to be invoked at specific places.
Message-Id: <[email protected]>
--- include/migration/misc.h | 19 +++++++++++++++ migration/ram.c | 51 +++++++++++++++++++++++++++++++++++++--- migration/savevm.c | 15 ++++++++++++ vl.c | 1 + 4 files changed, 83 insertions(+), 3 deletions(-)
include/migration/misc.h +19 -0
@@ -20,6 +20,25 @@/* migration/ram.c */ +typedef enum PrecopyNotifyReason { +PRECOPY_NOTIFY_SETUP =0 , +PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC =1 , +PRECOPY_NOTIFY_AFTER_BITMAP_SYNC =2 , +PRECOPY_NOTIFY_COMPLETE =3 , +PRECOPY_NOTIFY_CLEANUP =4 , +PRECOPY_NOTIFY_MAX =5 , +}PrecopyNotifyReason ; + +typedef struct PrecopyNotifyData { +enum PrecopyNotifyReason reason; +Error **errp; +}PrecopyNotifyData ; + +void precopy_infrastructure_init (void ); +void precopy_add_notifier (NotifierWithReturn *n); +void precopy_remove_notifier (NotifierWithReturn *n); +int precopy_notify (PrecopyNotifyReason reason,Error **errp); +void ram_mig_init (void );void qemu_guest_free_page_hint (void *addr,size_t len);
migration/ram.c +48 -3
81 lines not on this page · raw message
migration/savevm.c +15 -0
@@ -1088,6 +1088,7 @@ void qemu_savevm_state_header(QEMUFile *f)void qemu_savevm_state_setup (QEMUFile *f) {SaveStateEntry *se; +Error *local_err =NULL ;int ret;trace_savevm_state_setup ();
@@ -1109,6 +1110,10 @@ void qemu_savevm_state_setup(QEMUFile *f)break ; } } + +if (precopy_notify (PRECOPY_NOTIFY_SETUP , &local_err)) { +error_report_err (local_err); + } }int qemu_savevm_state_resume_prepare (MigrationState *s)
@@ -1251,6 +1256,11 @@ int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,SaveStateEntry *se;int ret;bool in_postcopy =migration_in_postcopy (); +Error *local_err =NULL ; + +if (precopy_notify (PRECOPY_NOTIFY_COMPLETE , &local_err)) { +error_report_err (local_err); + }trace_savevm_state_complete_precopy ();
@@ -1383,6 +1393,11 @@ void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,void qemu_savevm_state_cleanup (void ) {SaveStateEntry *se; +Error *local_err =NULL ; + +if (precopy_notify (PRECOPY_NOTIFY_CLEANUP , &local_err)) { +error_report_err (local_err); + }trace_savevm_state_cleanup (); QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
vl.c +1 -0
@@ -3039,6 +3039,7 @@ int main(int argc, char **argv, char **envp)module_call_init (MODULE_INIT_OPTS );runstate_init (); +precopy_infrastructure_init ();postcopy_infrastructure_init ();monitor_init_globals ();
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 18/22] migration/ram.c: add the free page optimization enable flag
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
This patch adds the free page optimization enable flag, and a function to set this flag. When the free page optimization is enabled, not all the pages are needed to be sent in the bulk stage.
Why using a new flag, instead of directly disabling ram_bulk_stage when the optimization is running?
Thanks for Peter Xu's reminder that disabling ram_bulk_stage will affect the use of compression. Please see save_page_use_compression. When xbzrle and compression are used, if free page optimizaion causes the ram_bulk_stage to be disabled, save_page_use_compression will return false, which disables the use of compression. That is, if free page optimization avoids the sending of half of the guest pages, the other half of pages loses the benefits of compression in the meantime. Using a new flag to let migration_bitmap_find_dirty skip the free pages in the bulk stage will avoid the above issue.
Message-Id: <[email protected]>
--- include/migration/misc.h | 1 + migration/ram.c | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-)
include/migration/misc.h +1 -0
@@ -38,6 +38,7 @@ void precopy_infrastructure_init(void);void precopy_add_notifier (NotifierWithReturn *n);void precopy_remove_notifier (NotifierWithReturn *n);int precopy_notify (PrecopyNotifyReason reason,Error **errp); +void precopy_enable_free_page_optimization (void );void ram_mig_init (void );void qemu_guest_free_page_hint (void *addr,size_t len);
migration/ram.c +17 -1
@@ -316,6 +316,8 @@ struct RAMState {uint32_t last_version;/* We are in the first round */ bool ram_bulk_stage; +/* The free page optimization is enabled */ +bool fpo_enabled;/* How many times we have dirty too many pages */ int dirty_rate_high_cnt;/* these variables are used for bitmap sync */
@@ -380,6 +382,15 @@ int precopy_notify(PrecopyNotifyReason reason, Error **errp)return notifier_with_return_list_notify (&precopy_notifier_list, &pnd); } +void precopy_enable_free_page_optimization (void ) +{ +if (!ram_state) { +return ; + } + + ram_state->fpo_enabled = true; +} +uint64_t ram_bytes_remaining (void ) { return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) :
@@ -1601,7 +1612,11 @@ unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb,return size; } -if (rs->ram_bulk_stage && start >0 ) { +/* +* When the free page optimization is enabled, we need to check the bitmap +* to send the non-free pages rather than all the pages in the bulk stage. +*/ +if (!rs->fpo_enabled && rs->ram_bulk_stage && start >0 ) { next = start +1 ; }else { next =find_next_bit (bitmap, size, start);
@@ -2651,6 +2666,7 @@ static void ram_state_reset(RAMState *rs) rs->last_page =0 ; rs->last_version = ram_list.version; rs->ram_bulk_stage = true; + rs->fpo_enabled = false; }#define MAX_WAIT 50/* ms, half buffered_file limit */
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 19/22] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
In reply to Dr. David Alan Gilbert (git)From: Wei Wang <[email protected]>
The new feature enables the virtio-balloon device to receive hints of guest free pages from the free page vq.
A notifier is registered to the migration precopy notifier chain. The notifier calls free_page_start after the migration thread syncs the dirty bitmap, so that the free page optimization starts to clear bits of free pages from the bitmap. It calls the free_page_stop before the migration thread syncs the bitmap, which is the end of the current round of ram save. The free_page_stop is also called to stop the optimization in the case when there is an error occurred in the process of ram saving.
Note: balloon will report pages which were free at the time of this call. As the reporting happens asynchronously, dirty bit logging must be enabled before this free_page_start call is made. Guest reporting must be disabled before the migration dirty bitmap is synchronized.
Message-Id: <[email protected]>
dgilbert: Dropped kernel header update, fixed up CMD_ID_* name change
--- hw/virtio/virtio-balloon.c | 263 +++++++++++++++++++++++++++++ include/hw/virtio/virtio-balloon.h | 28 ++- 2 files changed, 290 insertions(+), 1 deletion(-)
hw/virtio/virtio-balloon.c +263 -0
@@ -27,6 +27,7 @@#include "qapi/visitor.h" #include "trace.h" #include "qemu/error-report.h" +#include "migration/misc.h" #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h"
@@ -378,6 +379,184 @@ out: } } +static void virtio_balloon_handle_free_page_vq (VirtIODevice *vdev, +VirtQueue *vq) +{ +VirtIOBalloon *s =VIRTIO_BALLOON (vdev); +qemu_bh_schedule (s->free_page_bh); +} + +static bool get_free_page_hints (VirtIOBalloon *dev) +{ +VirtQueueElement *elem; +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VirtQueue *vq = dev->free_page_vq; + +while (dev->block_iothread) { +qemu_cond_wait (&dev->free_page_cond, &dev->free_page_lock); + } + + elem =virtqueue_pop (vq,sizeof (VirtQueueElement)); +if (!elem) { +return false; + } + +if (elem->out_num) { +uint32_t id; +size_t size =iov_to_buf (elem->out_sg, elem->out_num,0 , + &id,sizeof (id)); +virtqueue_push (vq, elem, size); +g_free (elem); + +virtio_tswap32s (vdev, &id); +if (unlikely (size !=sizeof (id))) { +virtio_error (vdev,"received an incorrect cmd id" ); +return false; + } +if (id == dev->free_page_report_cmd_id) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_START ; + }else { +/* +* Stop the optimization only when it has started. This +* avoids a stale stop sign for the previous command. +*/ +if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_STOP ; + } + } + } + +if (elem->in_num) { +if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { +qemu_guest_free_page_hint (elem->in_sg[0 ].iov_base, + elem->in_sg[0 ].iov_len); + } +virtqueue_push (vq, elem,1 ); +g_free (elem); + } + +return true; +} + +static void virtio_ballloon_get_free_page_hints (void *opaque) +{ +VirtIOBalloon *dev = opaque; +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VirtQueue *vq = dev->free_page_vq; +bool continue_to_get_hints; + +do { +qemu_mutex_lock (&dev->free_page_lock); +virtio_queue_set_notification (vq,0 ); + continue_to_get_hints =get_free_page_hints (dev); +qemu_mutex_unlock (&dev->free_page_lock); +virtio_notify (vdev, vq); +/* +* Start to poll the vq once the reporting started. Otherwise, continue +* only when there are entries on the vq, which need to be given back. +*/ + }while (continue_to_get_hints || + dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ); +virtio_queue_set_notification (vq,1 ); +} + +static bool virtio_balloon_free_page_support (void *opaque) +{ +VirtIOBalloon *s = opaque; +VirtIODevice *vdev =VIRTIO_DEVICE (s); + +return virtio_vdev_has_feature (vdev,VIRTIO_BALLOON_F_FREE_PAGE_HINT ); +} + +static void virtio_balloon_free_page_start (VirtIOBalloon *s) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (s); + +/* For the stop and copy phase, we don't need to start the optimization */ +if (!vdev->vm_running) { +return ; + } + +if (s->free_page_report_cmd_id ==UINT_MAX ) { + s->free_page_report_cmd_id = +VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN ; + }else { + s->free_page_report_cmd_id++; + } + + s->free_page_report_status =FREE_PAGE_REPORT_S_REQUESTED ; +virtio_notify_config (vdev); +} + +static void virtio_balloon_free_page_stop (VirtIOBalloon *s) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (s); + +if (s->free_page_report_status !=FREE_PAGE_REPORT_S_STOP ) { +/* +* The lock also guarantees us that the +* virtio_ballloon_get_free_page_hints exits after the +* free_page_report_status is set to S_STOP. +*/ +qemu_mutex_lock (&s->free_page_lock); +/* +* The guest hasn't done the reporting, so host sends a notification +* to the guest to actively stop the reporting. +*/ + s->free_page_report_status =FREE_PAGE_REPORT_S_STOP ; +qemu_mutex_unlock (&s->free_page_lock); +virtio_notify_config (vdev); + } +} + +static void virtio_balloon_free_page_done (VirtIOBalloon *s) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (s); + + s->free_page_report_status =FREE_PAGE_REPORT_S_DONE ; +virtio_notify_config (vdev); +} + +static int +virtio_balloon_free_page_report_notify (NotifierWithReturn *n,void *data) +{ +VirtIOBalloon *dev =container_of (n, VirtIOBalloon, + free_page_report_notify); +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +PrecopyNotifyData *pnd = data; + +if (!virtio_balloon_free_page_support (dev)) { +/* +* This is an optimization provided to migration, so just return 0 to +* have the normal migration process not affected when this feature is +* not supported. +*/ +return 0 ; + } + +switch (pnd->reason) { +case PRECOPY_NOTIFY_SETUP : +precopy_enable_free_page_optimization (); +break ; +case PRECOPY_NOTIFY_COMPLETE : +case PRECOPY_NOTIFY_CLEANUP : +case PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC : +virtio_balloon_free_page_stop (dev); +break ; +case PRECOPY_NOTIFY_AFTER_BITMAP_SYNC : +if (vdev->vm_running) { +virtio_balloon_free_page_start (dev); + }else { +virtio_balloon_free_page_done (dev); + } +break ; +default : +virtio_error (vdev,"%s: %d reason unknown" , __func__, pnd->reason); + } + +return 0 ; +} +static void virtio_balloon_get_config (VirtIODevice *vdev,uint8_t *config_data) {VirtIOBalloon *dev =VIRTIO_BALLOON (vdev);
@@ -386,6 +565,17 @@ static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) config.num_pages =cpu_to_le32 (dev->num_pages); config.actual =cpu_to_le32 (dev->actual); +if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_REQUESTED ) { + config.free_page_report_cmd_id = +cpu_to_le32 (dev->free_page_report_cmd_id); + }else if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_STOP ) { + config.free_page_report_cmd_id = +cpu_to_le32 (VIRTIO_BALLOON_CMD_ID_STOP ); + }else if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_DONE ) { + config.free_page_report_cmd_id = +cpu_to_le32 (VIRTIO_BALLOON_CMD_ID_DONE ); + } +trace_virtio_balloon_get_config (config.num_pages, config.actual);memcpy (config_data, &config,sizeof (struct virtio_balloon_config )); }
@@ -446,6 +636,7 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,VirtIOBalloon *dev =VIRTIO_BALLOON (vdev); f |= dev->host_features;virtio_add_feature (&f,VIRTIO_BALLOON_F_STATS_VQ ); +return f; }
@@ -482,6 +673,18 @@ static int virtio_balloon_post_load_device(void *opaque, int version_id)return 0 ; } +static const VMStateDescription vmstate_virtio_balloon_free_page_report = { + .name ="virtio-balloon-device/free-page-report" , + .version_id =1 , + .minimum_version_id =1 , + .needed = virtio_balloon_free_page_support, + .fields = (VMStateField []) { +VMSTATE_UINT32 (free_page_report_cmd_id, VirtIOBalloon), +VMSTATE_UINT32 (free_page_report_status, VirtIOBalloon), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_virtio_balloon_device = { .name ="virtio-balloon-device" , .version_id =1 ,
@@ -492,6 +695,10 @@ static const VMStateDescription vmstate_virtio_balloon_device = { VMSTATE_UINT32(actual, VirtIOBalloon),VMSTATE_END_OF_LIST () }, + .subsections = (const VMStateDescription * []) { + &vmstate_virtio_balloon_free_page_report, +NULL + } };static void virtio_balloon_device_realize (DeviceState *dev,Error **errp)
@@ -516,6 +723,29 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) s->dvq =virtio_add_queue (vdev,128 , virtio_balloon_handle_output); s->svq =virtio_add_queue (vdev,128 , virtio_balloon_receive_stats); +if (virtio_has_feature (s->host_features, +VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { + s->free_page_vq =virtio_add_queue (vdev,VIRTQUEUE_MAX_SIZE , + virtio_balloon_handle_free_page_vq); + s->free_page_report_status =FREE_PAGE_REPORT_S_STOP ; + s->free_page_report_cmd_id = +VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN ; + s->free_page_report_notify.notify = + virtio_balloon_free_page_report_notify; +precopy_add_notifier (&s->free_page_report_notify); +if (s->iothread) { +object_ref (OBJECT (s->iothread)); + s->free_page_bh =aio_bh_new (iothread_get_aio_context (s->iothread), + virtio_ballloon_get_free_page_hints, s); +qemu_mutex_init (&s->free_page_lock); +qemu_cond_init (&s->free_page_cond); + s->block_iothread = false; + }else { +/* Simply disable this feature if the iothread wasn't created. */ + s->host_features &= ~(1 <<VIRTIO_BALLOON_F_FREE_PAGE_HINT ); +virtio_error (vdev,"iothread is missing" ); + } + }reset_stats (s); }
@@ -524,6 +754,11 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)VirtIODevice *vdev =VIRTIO_DEVICE (dev);VirtIOBalloon *s =VIRTIO_BALLOON (dev); +if (virtio_balloon_free_page_support (s)) { +qemu_bh_delete (s->free_page_bh); +virtio_balloon_free_page_stop (s); +precopy_remove_notifier (&s->free_page_report_notify); + }balloon_stats_destroy_timer (s);qemu_remove_balloon_handler (s);virtio_cleanup (vdev);
@@ -533,6 +768,10 @@ static void virtio_balloon_device_reset(VirtIODevice *vdev) {VirtIOBalloon *s =VIRTIO_BALLOON (vdev); +if (virtio_balloon_free_page_support (s)) { +virtio_balloon_free_page_stop (s); + } +if (s->stats_vq_elem !=NULL ) {virtqueue_unpop (s->svq, s->stats_vq_elem,0 );g_free (s->stats_vq_elem);
@@ -550,6 +789,26 @@ static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)* was stopped */ virtio_balloon_receive_stats (vdev, s->svq); } + +if (virtio_balloon_free_page_support (s)) { +/* +* The VM is woken up and the iothread was blocked, so signal it to +* continue. +*/ +if (vdev->vm_running && s->block_iothread) { +qemu_mutex_lock (&s->free_page_lock); + s->block_iothread = false; +qemu_cond_signal (&s->free_page_cond); +qemu_mutex_unlock (&s->free_page_lock); + } + +/* The VM is stopped, block the iothread. */ +if (!vdev->vm_running) { +qemu_mutex_lock (&s->free_page_lock); + s->block_iothread = true; +qemu_mutex_unlock (&s->free_page_lock); + } + } }static void virtio_balloon_instance_init (Object *obj)
@@ -578,6 +837,10 @@ static const VMStateDescription vmstate_virtio_balloon = {static Property virtio_balloon_properties[] = {DEFINE_PROP_BIT ("deflate-on-oom" , VirtIOBalloon, host_features,VIRTIO_BALLOON_F_DEFLATE_ON_OOM , false), +DEFINE_PROP_BIT ("free-page-hint" , VirtIOBalloon, host_features, +VIRTIO_BALLOON_F_FREE_PAGE_HINT , false), +DEFINE_PROP_LINK ("iothread" , VirtIOBalloon, iothread,TYPE_IOTHREAD , + IOThread *),DEFINE_PROP_END_OF_LIST (), };
include/hw/virtio/virtio-balloon.h +27 -1
@@ -17,11 +17,14 @@#include "standard-headers/linux/virtio_balloon.h" #include "hw/virtio/virtio.h" +#include "sysemu/iothread.h" #define TYPE_VIRTIO_BALLOON "virtio-balloon-device"#define VIRTIO_BALLOON (obj) \ OBJECT_CHECK(VirtIOBalloon, (obj), TYPE_VIRTIO_BALLOON) +#define VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN 0x80000000 +typedef struct virtio_balloon_stat VirtIOBalloonStat ; typedef struct virtio_balloon_stat_modern {
@@ -32,15 +35,38 @@ typedef struct virtio_balloon_stat_modern {typedef struct PartiallyBalloonedPage PartiallyBalloonedPage ; +enum virtio_balloon_free_page_report_status { +FREE_PAGE_REPORT_S_STOP =0 , +FREE_PAGE_REPORT_S_REQUESTED =1 , +FREE_PAGE_REPORT_S_START =2 , +FREE_PAGE_REPORT_S_DONE =3 , +}; + typedef struct VirtIOBalloon { VirtIODevice parent_obj; - VirtQueue *ivq, *dvq, *svq; + VirtQueue *ivq, *dvq, *svq, *free_page_vq; + uint32_t free_page_report_status; uint32_t num_pages; uint32_t actual; + uint32_t free_page_report_cmd_id; uint64_t stats[VIRTIO_BALLOON_S_NR]; VirtQueueElement *stats_vq_elem; size_t stats_vq_offset; QEMUTimer *stats_timer; + IOThread *iothread; + QEMUBH *free_page_bh; + /* + * Lock to synchronize threads to access the free page reporting related + * fields (e.g. free_page_report_status). + */ + QemuMutex free_page_lock; + QemuCond free_page_cond; + /* + * Set to block iothread to continue reading free page hints as the VM is + * stopped. + */ + bool block_iothread; + NotifierWithReturn free_page_report_notify; int64_t stats_last_update; int64_t stats_poll_interval; uint32_t host_features;
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 20/22] Migration/colo.c: Fix double close bug when occur COLO failover
In reply to Dr. David Alan Gilbert (git)From: Zhang Chen <[email protected]>
In migration_incoming_state_destroy(void) will check the mis->to_src_file to double close the mis->to_src_file when occur COLO failover.
Message-Id: <[email protected]>
--- migration/colo.c | 1 + 1 file changed, 1 insertion(+)
migration/colo.c +1 -0
@@ -872,6 +872,7 @@ out:/* Must be called after failover BH is completed */ if (mis->to_src_file) {qemu_fclose (mis->to_src_file); + mis->to_src_file =NULL ; }migration_incoming_disable_colo ();
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 21/22] Migration/colo.c: Make COLO node running after failover
In reply to Dr. David Alan Gilbert (git)From: Zhang Chen <[email protected]>
Delay to close COLO for auto start VM after failover.
Message-Id: <[email protected]>
--- migration/colo.c | 1 - migration/migration.c | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-)
migration/colo.c +0 -1
@@ -874,7 +874,6 @@ out:qemu_fclose (mis->to_src_file); mis->to_src_file =NULL ; } -migration_incoming_disable_colo ();rcu_unregister_thread ();return NULL ;
migration/migration.c +3 -0
@@ -417,6 +417,9 @@ static void process_incoming_migration_bh(void *opaque) }else {runstate_set (RUN_STATE_PAUSED ); } + }else if (migration_incoming_colo_enabled ()) { +migration_incoming_disable_colo (); +vm_start (); }else {runstate_set (global_state_get_runstate ()); }
Signature · 1 line
2.20.1
[Qemu-devel] [PULL 22/22] qapi/migration.json: Remove a variable that doesn't exist in example
In reply to Dr. David Alan Gilbert (git)From: Zhang Chen <[email protected]>
Remove the "active" variable in example for query-colo-status. It is a doc bug from commit f56c0065
Message-Id: <[email protected]>
--- qapi/migration.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qapi/migration.json +1 -1
@@ -1393,7 +1393,7 @@ # Example: # # -> { "execute": "query-colo-status" } -# <- { "return": { "mode": "primary", "active": true, "reason": "request" } } +# <- { "return": { "mode": "primary", "reason": "request" } } # # Since: 3.1 ##
Signature · 1 line
2.20.1
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PULL 00/22] migration queue
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'060f501bdc qapi/migration.json: Remove a variable that doesn't exist in example 79985c89a1 Migration/colo.c: Make COLO node running after failover 04fa41f3fe Migration/colo.c: Fix double close bug when occur COLO failover fad6a7a2fe virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT ea055843a6 migration/ram.c: add the free page optimization enable flag 04f12f4493 migration/ram.c: add a notifier chain for precopy acffe8fac2 migration: API to clear bits of guest free pages from the dirty bitmap 28ba7920b5 migration: use bitmap_mutex in migration_bitmap_clear_dirty 50d141e21b bitmap: bitmap_count_one_with_offsetd09d8c34fe bitmap: fix bitmap_count_one0b3f876f73 tests: Add basic migration precopy tcp testb9b8858224 migration: Create socket-address parameterc3e10e38d0 tests: Add migration xbzrle test9891a178e1 migration: Add capabilities validationa3ba078ea3 tests/migration-test: Add a test for ignore-shared capability 40a11f6d72 migration: Add an ability to ignore shared RAM blocks b08a8bcb8f migration: Introduce ignore-shared capability502f3e7690 exec: Change RAMBlockIterFunc definition1419fc4f68 migration/rdma: clang compilation fixaf4a8e34f7 migration: Cleanup during exit
6078675984 migration/rdma: Fix qemu_rdma_cleanup null check f414a0d58c migration: Fix cancel state
=== OUTPUT BEGIN ===
1/22 Checking commit f414a0d58c61 (migration: Fix cancel state)
2/22 Checking commit 60786759846a (migration/rdma: Fix qemu_rdma_cleanup null check) 3/22 Checking commit af4a8e34f76a (migration: Cleanup during exit)
4/22 Checking commit 1419fc4f68a6 (migration/rdma: clang compilation fix) 5/22 Checking commit 502f3e769033 (exec: Change RAMBlockIterFunc definition) 6/22 Checking commit b08a8bcb8f55 (migration: Introduce ignore-shared capability) 7/22 Checking commit 40a11f6d720b (migration: Add an ability to ignore shared RAM blocks)
ERROR: Macros with multiple statements should be enclosed in a do - while loop #149: FILE: migration/ram.c:169:
+#define RAMBLOCK_FOREACH_NOT_IGNORED(block) \
+ INTERNAL_RAMBLOCK_FOREACH(block) \
+ if (ramblock_is_ignored(block)) {} else
ERROR: trailing statements should be on next line
#151: FILE: migration/ram.c:171:
+ if (ramblock_is_ignored(block)) {} else
total: 2 errors, 0 warnings, 386 lines checked
Patch 7/22 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
8/22 Checking commit a3ba078ea311 (tests/migration-test: Add a test for ignore-shared capability)
ERROR: if this code is redundant consider removing it
#235: FILE: tests/migration-test.c:829:
+#if 0
total: 1 errors, 0 warnings, 255 lines checked
Patch 8/22 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
9/22 Checking commit 9891a178e1cb (migration: Add capabilities validation)
10/22 Checking commit c3e10e38d0a5 (tests: Add migration xbzrle test)
11/22 Checking commit b9b885822408 (migration: Create socket-address parameter)
12/22 Checking commit 0b3f876f7352 (tests: Add basic migration precopy tcp test)
13/22 Checking commit d09d8c34fe5f (bitmap: fix bitmap_count_one)
14/22 Checking commit 50d141e21bda (bitmap: bitmap_count_one_with_offset)
15/22 Checking commit 28ba7920b5e4 (migration: use bitmap_mutex in migration_bitmap_clear_dirty) 16/22 Checking commit acffe8fac2f4 (migration: API to clear bits of guest free pages from the dirty bitmap) 17/22 Checking commit 04f12f44931e (migration/ram.c: add a notifier chain for precopy) 18/22 Checking commit ea055843a6f6 (migration/ram.c: add the free page optimization enable flag) 19/22 Checking commit fad6a7a2fe5e (virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT)
20/22 Checking commit 04fa41f3fed6 (Migration/colo.c: Fix double close bug when occur COLO failover) 21/22 Checking commit 79985c89a1be (Migration/colo.c: Make COLO node running after failover) 22/22 Checking commit 060f501bdcb2 (qapi/migration.json: Remove a variable that doesn't exist in example) === OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]
Peter Maydell
AppliedQuoted 28 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
The following changes since commit
b5b6b2b912bbcd3953407da938a8f969577ad3a1:Merge remote-tracking branch 'remotes/amarkovic/tags/mips-queue-mar-05-2019' into staging (2019-03-05 21:07:29 +0000)
are available in the Git repository at:
git://github.com/dagrh/qemu.gittags/pull-migration-20190306afor you to fetch changes up to
b5922fc5891261153f1a0f20e814c620aabeb6ac:
qapi/migration.json: Remove a variable that doesn't exist in example (2019-03-06 10:49:18 +0000)----------------------------------------------------------------
Migation pull 2019-03-06(This replaces the pull sent yesterday)
a) 4 small fixes including the cancel problem
that caused the ahci migration test to fail
intermittently
b) Yury's ignore-shared feature
c) Juan's extra tests
d) Wei Wang's free page hinting
e) Some Colo fixes from Zhang Chen
Quoted 5 lines
Diff from yesterdays pull:
1) A missing fix of mine (cleanup during exit)
2) Changes from Eric/Markus on 'Create socket-address parameter'----------------------------------------------------------------
Applied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/4.0
for any user-visible changes.
-- PMM
Re: [Qemu-devel] [PULL 07/22] migration: Add an ability to ignore shared RAM blocks
In reply to Dr. David Alan Gilbert (git)Quoted 12 lines · Dr. David Alan Gilbert (git)
From: Yury Kotov <[email protected]>
If ignore-shared capability is set then skip shared RAMBlocks during the RAM migration.
Also, moveqemu_ram_foreach_migratable_block(and rename) to the migration code, because it requires access to the migration capabilities.
migration/rdma.c static int qemu_rdma_init_ram_blocks(RDMAContext *rdma) · line 650
Quoted 8 lines
migration/rdma.c +1
-1@@ -644,7 +644,7 @@ static int qemu_rdma_init_ram_blocks(RDMAContext *rdma)assert (rdma->blockmap ==NULL );memset (local,0 ,sizeof *local); -qemu_ram_foreach_migratable_block (qemu_rdma_init_one_block, rdma); +foreach_not_ignored_block (qemu_rdma_init_one_block, rdma);trace_qemu_rdma_init_ram_blocks (local->nb_blocks); rdma->dest_blocks =g_new0 (RDMADestBlock, rdma->local_ram_blocks.nb_blocks);
Hi. This change causes Coverity to gripe (CID 1399413) because the return value from foreach_not_ignored_block() is ignored here but it is checked on every other use of the function.
This is one of those Coverity errors where it's just using a sometimes-wrong heuristic, so we could just mark it as a false positive (AFAICT qemu_rdma_init_one_block() always returns 0), but OTOH rdma_add_block() and qemu_rdma_init_one_block() carefully pipe through a return value, so maybe it's worth assert()ing in case somebody changes rdma_add_block() to maybe fail later?
I don't think there's much in it -- let me know if you just want me to mark the issue as a false positive.
thanks
-- PMM
Re: [Qemu-devel] [PULL 19/22] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
In reply to Dr. David Alan Gilbert (git)Quoted 24 lines · Dr. David Alan Gilbert (git)
From: Wei Wang <[email protected]>
The new feature enables the virtio-balloon device to receive hints of guest free pages from the free page vq.
A notifier is registered to the migration precopy notifier chain. The notifier calls
free_page_startafter the migration thread syncs the dirty bitmap, so that the free page optimization starts to clear bits of free pages from the bitmap. It calls thefree_page_stopbefore the migration thread syncs the bitmap, which is the end of the current round of ram save. Thefree_page_stopis also called to stop the optimization in the case when there is an error occurred in the process of ram saving.Note: balloon will report pages which were free at the time of this call. As the reporting happens asynchronously, dirty bit logging must be enabled before this
free_page_startcall is made. Guest reporting must be disabled before the migration dirty bitmap is synchronized.Signed-off-by Wei Wang <[email protected]>CC Michael S. Tsirkin <[email protected]>CC Dr. David Alan Gilbert <[email protected]>CC Juan Quintela <[email protected]>CC Peter Xu <[email protected]>
Quoted 5 lines
Message-Id: <[email protected]>
Reviewed-by Michael S. Tsirkin <[email protected]>Signed-off-by Dr. David Alan Gilbert <[email protected]>dgilbert: Dropped kernel header update, fixed up CMD_ID_* name change
---
Hi -- Coverity points out a use-after-free here (CID 1399412):
Quoted 11 lines
hw/virtio/virtio-balloon.c +11
-0+static bool get_free_page_hints (VirtIOBalloon *dev) +{ +VirtQueueElement *elem; +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VirtQueue *vq = dev->free_page_vq; + +while (dev->block_iothread) { +qemu_cond_wait (&dev->free_page_cond, &dev->free_page_lock); + } + + elem =virtqueue_pop (vq,sizeof (VirtQueueElement));
hw/virtio/virtio-balloon.c out: · line 409
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+if (!elem) { +return false; + } + +if (elem->out_num) { +uint32_t id; +size_t size =iov_to_buf (elem->out_sg, elem->out_num,0 , + &id,sizeof (id)); +virtqueue_push (vq, elem, size); +g_free (elem);
Here we free elem...
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+ +virtio_tswap32s (vdev, &id); +if (unlikely (size !=sizeof (id))) { +virtio_error (vdev,"received an incorrect cmd id" ); +return false; + } +if (id == dev->free_page_report_cmd_id) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_START ; + }else { +/*
hw/virtio/virtio-balloon.c out: · line 429
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+* Stop the optimization only when it has started. This +* avoids a stale stop sign for the previous command. +*/ +if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_STOP ; + } + } + } + +if (elem->in_num) {
...but we can fall through here and try to dereference elem...
hw/virtio/virtio-balloon.c out: · line 435
Quoted 6 lines
hw/virtio/virtio-balloon.c +6
-0+if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { +qemu_guest_free_page_hint (elem->in_sg[0 ].iov_base, + elem->in_sg[0 ].iov_len); + } +virtqueue_push (vq, elem,1 ); +g_free (elem);
...and then free it again.
hw/virtio/virtio-balloon.c out: · line 439
Quoted 4 lines
hw/virtio/virtio-balloon.c +4
-0+ } + +return true; +}
thanks
-- PMM
Re: [Qemu-devel] [PULL 19/22] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
Quoted 4 lines · * Peter Maydell ([email protected]) wrote:
Quoted 2 lines · Dr. David Alan Gilbert (git)
From: Wei Wang <[email protected]>
Wei: Can you look at this....
Quoted 76 lines
Quoted 21 lines
The new feature enables the virtio-balloon device to receive hints of guest free pages from the free page vq.
A notifier is registered to the migration precopy notifier chain. The notifier calls
free_page_startafter the migration thread syncs the dirty bitmap, so that the free page optimization starts to clear bits of free pages from the bitmap. It calls thefree_page_stopbefore the migration thread syncs the bitmap, which is the end of the current round of ram save. Thefree_page_stopis also called to stop the optimization in the case when there is an error occurred in the process of ram saving.Note: balloon will report pages which were free at the time of this call. As the reporting happens asynchronously, dirty bit logging must be enabled before this
free_page_startcall is made. Guest reporting must be disabled before the migration dirty bitmap is synchronized.Signed-off-by Wei Wang <[email protected]>CC Michael S. Tsirkin <[email protected]>CC Dr. David Alan Gilbert <[email protected]>CC Juan Quintela <[email protected]>CC Peter Xu <[email protected]>Quoted 5 lines
Message-Id: <[email protected]>
Reviewed-by Michael S. Tsirkin <[email protected]>Signed-off-by Dr. David Alan Gilbert <[email protected]>dgilbert: Dropped kernel header update, fixed up CMD_ID_* name change
---Hi -- Coverity points out a use-after-free here (CID 1399412):
Quoted 11 lines
hw/virtio/virtio-balloon.c +11
-0+static bool get_free_page_hints (VirtIOBalloon *dev) +{ +VirtQueueElement *elem; +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VirtQueue *vq = dev->free_page_vq; + +while (dev->block_iothread) { +qemu_cond_wait (&dev->free_page_cond, &dev->free_page_lock); + } + + elem =virtqueue_pop (vq,sizeof (VirtQueueElement));hw/virtio/virtio-balloon.c out: · line 409
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+if (!elem) { +return false; + } + +if (elem->out_num) { +uint32_t id; +size_t size =iov_to_buf (elem->out_sg, elem->out_num,0 , + &id,sizeof (id)); +virtqueue_push (vq, elem, size); +g_free (elem);Here we free elem...
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+ +virtio_tswap32s (vdev, &id); +if (unlikely (size !=sizeof (id))) { +virtio_error (vdev,"received an incorrect cmd id" ); +return false; + } +if (id == dev->free_page_report_cmd_id) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_START ; + }else { +/* hw/virtio/virtio-balloon.c out: · line 429
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+* Stop the optimization only when it has started. This +* avoids a stale stop sign for the previous command. +*/ +if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_STOP ; + } + } + } + +if (elem->in_num) {...but we can fall through here and try to dereference elem...
Quoted 8 lines
hw/virtio/virtio-balloon.c out: · line 435
Quoted 6 lines
hw/virtio/virtio-balloon.c +6
-0+if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { +qemu_guest_free_page_hint (elem->in_sg[0 ].iov_base, + elem->in_sg[0 ].iov_len); + } +virtqueue_push (vq, elem,1 ); +g_free (elem);...and then free it again.
OK, so the question here is:
Is it allowed to have both in and out in the same queue element; if
it's not then we need to error the device.
If it is allowed then we need to fix up the out_num case.
Dave
Quoted 7 lines
hw/virtio/virtio-balloon.c out: · line 439
Quoted 4 lines
hw/virtio/virtio-balloon.c +4
-0+ } + +return true; +}thanks
-- PMM
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PULL 07/22] migration: Add an ability to ignore shared RAM blocks
In reply to Peter MaydellQuoted 34 lines · * Peter Maydell ([email protected]) wrote:
Quoted 12 lines · Dr. David Alan Gilbert (git)
From: Yury Kotov <[email protected]>
If ignore-shared capability is set then skip shared RAMBlocks during the RAM migration.
Also, moveqemu_ram_foreach_migratable_block(and rename) to the migration code, because it requires access to the migration capabilities.migration/rdma.c static int qemu_rdma_init_ram_blocks(RDMAContext *rdma) · line 650
Quoted 8 lines
migration/rdma.c +1
-1@@ -644,7 +644,7 @@ static int qemu_rdma_init_ram_blocks(RDMAContext *rdma)assert (rdma->blockmap ==NULL );memset (local,0 ,sizeof *local); -qemu_ram_foreach_migratable_block (qemu_rdma_init_one_block, rdma); +foreach_not_ignored_block (qemu_rdma_init_one_block, rdma);trace_qemu_rdma_init_ram_blocks (local->nb_blocks); rdma->dest_blocks =g_new0 (RDMADestBlock, rdma->local_ram_blocks.nb_blocks);Hi. This change causes Coverity to gripe (CID 1399413) because the return value from
foreach_not_ignored_block()is ignored here but it is checked on every other use of the function.This is one of those Coverity errors where it's just using a sometimes-wrong heuristic, so we could just mark it as a false positive (AFAICT
qemu_rdma_init_one_block()always returns 0), but OTOHrdma_add_block()andqemu_rdma_init_one_block()carefully pipe through a return value, so maybe it's worth assert()ing in case somebody changesrdma_add_block()to maybe fail later?
Quoted 2 lines
I don't think there's much in it -- let me know if you just want me to mark the issue as a false positive.
I'll patch it to check it, it's an easy enough check. (Although how well a failure cleans up in practice could be fun).
Dave
Quoted 2 lines
thanks
-- PMM
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PULL 19/22] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
In reply to Dr. David Alan GilbertQuoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines · * Peter Maydell ([email protected]) wrote:
Quoted 1 line · Dr. David Alan Gilbert (git)
From: Wei Wang <[email protected]>
Wei: Can you look at this....
Sure, I'll send a followup patch to fix this.
Quoted 80 lines
Quoted 80 lines
Quoted 22 lines
The new feature enables the virtio-balloon device to receive hints of guest free pages from the free page vq.
A notifier is registered to the migration precopy notifier chain. The notifier calls
free_page_startafter the migration thread syncs the dirty bitmap, so that the free page optimization starts to clear bits of free pages from the bitmap. It calls thefree_page_stopbefore the migration thread syncs the bitmap, which is the end of the current round of ram save. Thefree_page_stopis also called to stop the optimization in the case when there is an error occurred in the process of ram saving.Note: balloon will report pages which were free at the time of this call. As the reporting happens asynchronously, dirty bit logging must be enabled before this
free_page_startcall is made. Guest reporting must be disabled before the migration dirty bitmap is synchronized.Signed-off-by Wei Wang <[email protected]>CC Michael S. Tsirkin <[email protected]>CC Dr. David Alan Gilbert <[email protected]>CC Juan Quintela <[email protected]>CC Peter Xu <[email protected]>Quoted 5 lines
Message-Id: <[email protected]>
Reviewed-by Michael S. Tsirkin <[email protected]>Signed-off-by Dr. David Alan Gilbert <[email protected]>dgilbert: Dropped kernel header update, fixed up CMD_ID_* name change
---Hi -- Coverity points out a use-after-free here (CID 1399412):
Quoted 11 lines
hw/virtio/virtio-balloon.c +11
-0+static bool get_free_page_hints (VirtIOBalloon *dev) +{ +VirtQueueElement *elem; +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VirtQueue *vq = dev->free_page_vq; + +while (dev->block_iothread) { +qemu_cond_wait (&dev->free_page_cond, &dev->free_page_lock); + } + + elem =virtqueue_pop (vq,sizeof (VirtQueueElement));hw/virtio/virtio-balloon.c out: · line 409
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+if (!elem) { +return false; + } + +if (elem->out_num) { +uint32_t id; +size_t size =iov_to_buf (elem->out_sg, elem->out_num,0 , + &id,sizeof (id)); +virtqueue_push (vq, elem, size); +g_free (elem);Here we free elem...
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+ +virtio_tswap32s (vdev, &id); +if (unlikely (size !=sizeof (id))) { +virtio_error (vdev,"received an incorrect cmd id" ); +return false; + } +if (id == dev->free_page_report_cmd_id) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_START ; + }else { +/* hw/virtio/virtio-balloon.c out: · line 429
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+* Stop the optimization only when it has started. This +* avoids a stale stop sign for the previous command. +*/ +if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { + dev->free_page_report_status =FREE_PAGE_REPORT_S_STOP ; + } + } + } + +if (elem->in_num) {...but we can fall through here and try to dereference elem...
Quoted 6 lines
hw/virtio/virtio-balloon.c +6
-0+if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_START ) { +qemu_guest_free_page_hint (elem->in_sg[0 ].iov_base, + elem->in_sg[0 ].iov_len); + } +virtqueue_push (vq, elem,1 ); +g_free (elem);
Quoted 6 lines
Quoted 1 line
...and then free it again.
OK, so the question here is:
Is it allowed to have both in and out in the same queue element; if
it's not then we need to error the device.
If it is allowed then we need to fix up theout_numcase.
I think it is allowed. From virtqueue_pop, an elem could consist of several inbufs and outbufs.
Probably we could just delay the free till the end of this function..will post the fix patch for review soon.
Best,
Wei