migration: Bug fixes (prepare for preempt-full)
v2 cover migration: Bug fixes (prepare for preempt-full)
v2:
- Drop patch "migration: Disallow xbzrle with postcopy" [Dave]
- Added patch "migration: Disable multifd explicitly with compression" (according to the comment in the other series) [Dave]
- s/deadloop/infinite loop/ in patch 1 subject [Dave]
v1: https://lore.kernel.org/qemu-devel/[email protected]
This patchset does bug fixes that I found when testing preempt-full. Please refer to each of the patch on the purpose. Thanks,
Peter Xu (5):
migration: Fix possible infinite loop of ram save process
migration: Fix race on qemu_file_shutdown()
migration: Disallow postcopy preempt to be used with compress
migration: Use non-atomic ops for clear log bitmap
migration: Disable multifd explicitly with compression
include/exec/ram_addr.h | 11 +++++----- include/exec/ramblock.h | 3 +++ include/qemu/bitmap.h | 1 + migration/migration.c | 18 +++++++++++++++++ migration/qemu-file.c | 27 ++++++++++++++++++++++--- migration/ram.c | 27 ++++++++++++++++--------- util/bitmap.c | 45 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 114 insertions(+), 18 deletions(-)
Signature · 1 line
2.37.3
PATCH v2 1/5 migration: Fix possible infinite loop of ram save process
When starting ram saving procedure (especially at the completion phase), always set last_seen_block to non-NULL to make sure we can always correctly detect the case where "we've migrated all the dirty pages".
Then we'll guarantee both last_seen_block and pss.block will be valid always before the loop starts.
See the comment in the code for some details.
--- migration/ram.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
migration/ram.c +12 -4
@@ -2546,14 +2546,22 @@ static int ram_find_and_save_block(RAMState *rs)return pages; } +/* +* Always keep last_seen_block/last_page valid during this procedure, +* because find_dirty_block() relies on these values (e.g., we compare +* last_seen_block with pss.block to see whether we searched all the +* ramblocks) to detect the completion of migration. Having NULL value +* of last_seen_block can conditionally cause below loop to run forever. +*/ +if (!rs->last_seen_block) { + rs->last_seen_block =QLIST_FIRST_RCU (&ram_list.blocks); + rs->last_page =0 ; + } + pss.block = rs->last_seen_block; pss.page = rs->last_page; pss.complete_round = false; -if (!pss.block) { - pss.block =QLIST_FIRST_RCU (&ram_list.blocks); - } - do { again = true; found = get_queued_page(rs, &pss);
Signature · 1 line
2.37.3
Re: [PATCH v2 1/5] migration: Fix possible infinite loop of ram save process
Quoted 11 lines · Peter Xu
When starting ram saving procedure (especially at the completion phase), always set
last_seen_blockto non-NULL to make sure we can always correctly detect the case where "we've migrated all the dirty pages".Then we'll guarantee both
last_seen_blockand pss.block will be valid always before the loop starts.See the comment in the code for some details.
Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by Peter Xu <[email protected]>
queued
PATCH v2 2/5 migration: Fix race on qemu_file_shutdown()
In reply to Peter XuIn qemu_file_shutdown(), there's a possible race if with current order of operation. There're two major things to do:
(1) Do real shutdown() (e.g. shutdown() syscall on socket)
(2) Update qemufile's last_error
We must do (2) before (1) otherwise there can be a race condition like:
page receiver other thread
------------- ------------
qemu_get_buffer()
do shutdown()
returns 0 (buffer all zero) (meanwhile we didn't check this retcode)
try to detect IO error
last_error==NULL, IO okay
install ALL-ZERO page
set last_error
--> guest crash!
To fix this, we can also check retval of qemu_get_buffer(), but not all APIs can be properly checked and ultimately we still need to go back to qemu_file_get_error(). E.g. qemu_get_byte() doesn't return error.
Maybe some day a rework of qemufile API is really needed, but for now keep using qemu_file_get_error() and fix it by not allowing that race condition to happen. Here shutdown() is indeed special because the last_error was emulated. For real -EIO errors it'll always be set when e.g. sendmsg() error triggers so we won't miss those ones, only shutdown() is a bit tricky here.
--- migration/qemu-file.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
migration/qemu-file.c +24 -3
@@ -79,6 +79,30 @@ int qemu_file_shutdown(QEMUFile *f)int ret =0 ; f->shutdown = true; + +/* +* We must set qemufile error before the real shutdown(), otherwise +* there can be a race window where we thought IO all went though +* (because last_error==NULL) but actually IO has already stopped. +* +* If without correct ordering, the race can happen like this: +* +* page receiver other thread +* ------------- ------------ +* qemu_get_buffer() +* do shutdown() +* returns 0 (buffer all zero) +* (we didn't check this retcode) +* try to detect IO error +* last_error==NULL, IO okay +* install ALL-ZERO page +* set last_error +* --> guest crash! +*/ +if (!f->last_error) { +qemu_file_set_error (f, -EIO ); + } +if (!qio_channel_has_feature (f->ioc,QIO_CHANNEL_FEATURE_SHUTDOWN )) {return -ENOSYS ;
@@ -88,9 +112,6 @@ int qemu_file_shutdown(QEMUFile *f) ret = -EIO ; } -if (!f->last_error) { -qemu_file_set_error (f, -EIO ); - }return ret; }
Signature · 1 line
2.37.3
Re: [PATCH v2 2/5] migration: Fix race on qemu_file_shutdown()
Quoted 31 lines · Peter Xu
In
qemu_file_shutdown(), there's a possible race if with current order of operation. There're two major things to do:(1) Do real
shutdown()(e.g.shutdown()syscall on socket)
(2) Update qemufile'slast_errorWe must do (2) before (1) otherwise there can be a race condition like:
page receiver other thread
------------- ------------
qemu_get_buffer()
doshutdown()
returns 0 (buffer all zero) (meanwhile we didn't check this retcode)
try to detect IO error
last_error==NULL, IO okay
install ALL-ZERO page
setlast_error
--> guest crash!To fix this, we can also check retval of
qemu_get_buffer(), but not all APIs can be properly checked and ultimately we still need to go back toqemu_file_get_error(). E.g.qemu_get_byte()doesn't return error.Maybe some day a rework of qemufile API is really needed, but for now keep using
qemu_file_get_error()and fix it by not allowing that race condition to happen. Hereshutdown()is indeed special because thelast_errorwas emulated. For real-EIOerrors it'll always be set when e.g.sendmsg()error triggers so we won't miss those ones, onlyshutdown()is a bit tricky here.
Quoted 3 lines
Cc Daniel P. Berrange <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by Peter Xu <[email protected]>
PATCH v2 3/5 migration: Disallow postcopy preempt to be used with compress
In reply to Peter XuThe preempt mode requires the capability to assign channel for each of the page, while the compression logic will currently assign pages to different compress thread/local-channel so potentially they're incompatible.
--- migration/migration.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
migration/migration.c +11 -0
@@ -1336,6 +1336,17 @@ static bool migrate_caps_check(bool *cap_list,error_setg (errp,"Postcopy preempt requires postcopy-ram" );return false; } + +/* +* Preempt mode requires urgent pages to be sent in separate +* channel, OTOH compression logic will disorder all pages into +* different compression channels, which is not compatible with the +* preempt assumptions on channel assignments. +*/ +if (cap_list[MIGRATION_CAPABILITY_COMPRESS ]) { +error_setg (errp,"Postcopy preempt not compatible with compress" ); +return false; + } }return true;
Signature · 1 line
2.37.3
Re: [PATCH v2 3/5] migration: Disallow postcopy preempt to be used with compress
Quoted 6 lines · Peter Xu
The preempt mode requires the capability to assign channel for each of the page, while the compression logic will currently assign pages to different compress thread/local-channel so potentially they're incompatible.
Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by Peter Xu <[email protected]>
PATCH v2 4/5 migration: Use non-atomic ops for clear log bitmap
In reply to Peter XuSince we already have bitmap_mutex to protect either the dirty bitmap or the clear log bitmap, we don't need atomic operations to set/clear/test on the clear log bitmap. Switching all ops from atomic to non-atomic versions, meanwhile touch up the comments to show which lock is in charge.
Introduced non-atomic version of bitmap_test_and_clear_atomic(), mostly the same as the atomic version but simplified a few places, e.g. dropped the "old_bits" variable, and also the explicit memory barriers.
--- include/exec/ram_addr.h | 11 +++++----- include/exec/ramblock.h | 3 +++ include/qemu/bitmap.h | 1 + util/bitmap.c | 45 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-)
include/exec/ram_addr.h +6 -5
@@ -42,7 +42,8 @@ static inline long clear_bmap_size(uint64_t pages, uint8_t shift) }/** -* clear_bmap_set: set clear bitmap for the page range +* clear_bmap_set: set clear bitmap for the page range. Must be with +* bitmap_mutex held. * * @rb: the ramblock to operate on * @start: the start page number
@@ -55,12 +56,12 @@ static inline void clear_bmap_set(RAMBlock *rb, uint64_t start, {uint8_t shift = rb->clear_bmap_shift; -bitmap_set_atomic (rb->clear_bmap, start >> shift, -clear_bmap_size (npages, shift)); +bitmap_set (rb->clear_bmap, start >> shift,clear_bmap_size (npages, shift)); }/** -* clear_bmap_test_and_clear: test clear bitmap for the page, clear if set +* clear_bmap_test_and_clear: test clear bitmap for the page, clear if set. +* Must be with bitmap_mutex held. * * @rb: the ramblock to operate on * @page: the page number to check
@@ -71,7 +72,7 @@ static inline bool clear_bmap_test_and_clear(RAMBlock *rb, uint64_t page) {uint8_t shift = rb->clear_bmap_shift; -return bitmap_test_and_clear_atomic (rb->clear_bmap, page >> shift,1 ); +return bitmap_test_and_clear (rb->clear_bmap, page >> shift,1 ); }static inline bool offset_in_ramblock (RAMBlock *b,ram_addr_t offset)
include/exec/ramblock.h +3 -0
@@ -53,6 +53,9 @@ struct RAMBlock {* and split clearing of dirty bitmap on the remote node (e.g., * KVM). The bitmap will be set only when doing global sync. * +* It is only used during src side of ram migration, and it is +* protected by the global ram_state.bitmap_mutex. +* * NOTE: this bitmap is different comparing to the other bitmaps * in that one bit can represent multiple guest pages (which is * decided by the `clear_bmap_shift' variable below). On
include/qemu/bitmap.h +1 -0
@@ -253,6 +253,7 @@ 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);bool bitmap_test_and_clear_atomic (unsigned long *map,long start,long nr); +bool bitmap_test_and_clear (unsigned long *map,long start,long nr);void bitmap_copy_and_clear_atomic (unsigned long *dst,unsigned long *src,long nr); unsigned long bitmap_find_next_zero_area(unsigned long *map,
util/bitmap.c +45 -0
51 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH v2 4/5] migration: Use non-atomic ops for clear log bitmap
Quoted 11 lines · Peter Xu
Since we already have
bitmap_mutexto protect either the dirty bitmap or the clear log bitmap, we don't need atomic operations to set/clear/test on the clear log bitmap. Switching all ops from atomic to non-atomic versions, meanwhile touch up the comments to show which lock is in charge.Introduced non-atomic version of
bitmap_test_and_clear_atomic(), mostly the same as the atomic version but simplified a few places, e.g. dropped the "old_bits" variable, and also the explicit memory barriers.Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by Peter Xu <[email protected]>
PATCH v2 5/5 migration: Disable multifd explicitly with compression
In reply to Peter XuMultifd thread model does not work for compression, explicitly disable it.
Note that previuosly even we can enable both of them, nothing will go wrong, because the compression code has higher priority so multifd feature will just be ignored. Now we'll fail even earlier at config time so the user should be aware of the consequence better.
Note that there can be a slight chance of breaking existing users, but let's assume they're not majority and not serious users, or they should have found that multifd is not working already.
With that, we can safely drop the check in ram_save_target_page() for using multifd, because when multifd=on then compression=off, then the removed check on save_page_use_compression() will also always return false too.
--- migration/migration.c | 7 +++++++ migration/ram.c | 11 +++++------ 2 files changed, 12 insertions(+), 6 deletions(-)
migration/migration.c +7 -0
@@ -1349,6 +1349,13 @@ static bool migrate_caps_check(bool *cap_list, } } +if (cap_list[MIGRATION_CAPABILITY_MULTIFD ]) { +if (cap_list[MIGRATION_CAPABILITY_COMPRESS ]) { +error_setg (errp,"Multifd is not compatible with compress" ); +return false; + } + } +return true; }
migration/ram.c +5 -6
@@ -2305,13 +2305,12 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss) }/* -* Do not use multifd for: -* 1. Compression as the first page in the new block should be posted out -* before sending the compressed page -* 2. In postcopy as one whole host page should be placed +* Do not use multifd in postcopy as one whole host page should be +* placed. Meanwhile postcopy requires atomic update of pages, so even +* if host page size == guest page size the dest guest during run may +* still see partially copied pages which is data corruption. */ -if (!save_page_use_compression (rs) &&migrate_use_multifd () - && !migration_in_postcopy ()) { +if (migrate_use_multifd () && !migration_in_postcopy ()) {return ram_save_multifd_page (rs, block, offset); }
Signature · 1 line
2.37.3
Re: [PATCH v2 5/5] migration: Disable multifd explicitly with compression
Quoted 11 lines · * Peter Xu ([email protected]) wrote:
Multifd thread model does not work for compression, explicitly disable it.
Note that previuosly even we can enable both of them, nothing will go wrong, because the compression code has higher priority so multifd feature will just be ignored. Now we'll fail even earlier at config time so the user should be aware of the consequence better.
Note that there can be a slight chance of breaking existing users, but let's assume they're not majority and not serious users, or they should have found that multifd is not working already.
Quoted 5 lines
With that, we can safely drop the check in
ram_save_target_page()for using multifd, because when multifd=on then compression=off, then the removed check onsave_page_use_compression()will also always return false too.Signed-off-by Peter Xu <[email protected]>
Yes, and of course if they were trying to use it, they should have been trying to use multifd-compression parameter instead, which is different code.
Quoted 49 lines
--- migration/migration.c | 7 +++++++ migration/ram.c | 11 +++++------ 2 files changed, 12 insertions(+), 6 deletions(-)migration/migration.c +7
-0@@ -1349,6 +1349,13 @@ static bool migrate_caps_check(bool *cap_list, } } +if (cap_list[MIGRATION_CAPABILITY_MULTIFD ]) { +if (cap_list[MIGRATION_CAPABILITY_COMPRESS ]) { +error_setg (errp,"Multifd is not compatible with compress" ); +return false; + } + } +return true; }migration/ram.c +5
-6@@ -2305,13 +2305,12 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss) }/* -* Do not use multifd for: -* 1. Compression as the first page in the new block should be posted out -* before sending the compressed page -* 2. In postcopy as one whole host page should be placed +* Do not use multifd in postcopy as one whole host page should be +* placed. Meanwhile postcopy requires atomic update of pages, so even +* if host page size == guest page size the dest guest during run may +* still see partially copied pages which is data corruption. */ -if (!save_page_use_compression (rs) &&migrate_use_multifd () - && !migration_in_postcopy ()) { +if (migrate_use_multifd () && !migration_in_postcopy ()) {return ram_save_multifd_page (rs, block, offset); }Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v2 5/5] migration: Disable multifd explicitly with compression
In reply to Peter XuQuoted 11 lines · Peter Xu
Multifd thread model does not work for compression, explicitly disable it.
Note that previuosly even we can enable both of them, nothing will go wrong, because the compression code has higher priority so multifd feature will just be ignored. Now we'll fail even earlier at config time so the user should be aware of the consequence better.
Note that there can be a slight chance of breaking existing users, but let's assume they're not majority and not serious users, or they should have found that multifd is not working already.
Quoted 5 lines
With that, we can safely drop the check in
ram_save_target_page()for using multifd, because when multifd=on then compression=off, then the removed check onsave_page_use_compression()will also always return false too.Signed-off-by Peter Xu <[email protected]>