migration/multifd: Change multifd_load_cleanup() signature and usage
PATCH 1/4 migration/multifd: Change multifd_load_cleanup() signature and usage
Since it's introduction in commit f986c3d256 ("migration: Create multifd migration threads"), multifd_load_cleanup() never returned any value different than 0, neither set up any error on errp.
Even though, on process_incoming_migration_bh() an if clause uses it's return value to decide on setting autostart = false, which will never happen.
In order to simplify the codebase, change multifd_load_cleanup() signature to 'void multifd_load_cleanup(void)', and for every usage remove error handling or decision made based on return value != 0.
--- migration/multifd.h | 2 +- migration/migration.c | 14 ++++---------- migration/multifd.c | 6 ++---- 3 files changed, 7 insertions(+), 15 deletions(-)
migration/multifd.h +1 -1
@@ -16,7 +16,7 @@int multifd_save_setup (Error **errp);void multifd_save_cleanup (void );int multifd_load_setup (Error **errp); -int multifd_load_cleanup (Error **errp); +void multifd_load_cleanup (void );bool multifd_recv_all_channels_created (void );void multifd_recv_new_channel (QIOChannel *ioc,Error **errp);void multifd_recv_sync_main (void );
migration/migration.c +4 -10
@@ -543,13 +543,7 @@ static void process_incoming_migration_bh(void *opaque)*/ qemu_announce_self (&mis->announce_timer,migrate_announce_params ()); -if (multifd_load_cleanup (&local_err) !=0 ) { -error_report_err (local_err); - autostart = false; - } -/* If global state section was not received or we are in running -state, we need to obey autostart. Any other state is set with -runstate_set. */ +multifd_load_cleanup ();dirty_bitmap_mig_before_vm_start ();
@@ -649,9 +643,9 @@ fail:migrate_set_state (&mis->state,MIGRATION_STATUS_ACTIVE ,MIGRATION_STATUS_FAILED );qemu_fclose (mis->from_src_file); -if (multifd_load_cleanup (&local_err) !=0 ) { -error_report_err (local_err); - } + +multifd_load_cleanup (); +exit (EXIT_FAILURE ); }
migration/multifd.c +2 -4
@@ -1022,12 +1022,12 @@ static void multifd_recv_terminate_threads(Error *err) } } -int multifd_load_cleanup (Error **errp) +void multifd_load_cleanup (void ) {int i;if (!migrate_use_multifd () || !migrate_multi_channels_is_allowed ()) { -return 0 ; +return ; }multifd_recv_terminate_threads (NULL );for (i =0 ; i <migrate_multifd_channels (); i++) {
@@ -1067,8 +1067,6 @@ int multifd_load_cleanup(Error **errp) multifd_recv_state->params =NULL ;g_free (multifd_recv_state); multifd_recv_state =NULL ; - -return 0 ; }void multifd_recv_sync_main (void )
Signature · 1 line
2.39.1
PATCH 2/4 migration/multifd: Remove unnecessary assignment on multifd_load_cleanup()
Before assigning "p->quit = true" for every multifd channel, multifd_load_cleanup() will call multifd_recv_terminate_threads() which already does the same assignment, while protected by a mutex.
So there is no point doing the same assignment again.
--- migration/multifd.c | 1 - 1 file changed, 1 deletion(-)
migration/multifd.c +0 -1
@@ -1034,7 +1034,6 @@ void multifd_load_cleanup(void)MultiFDRecvParams *p = &multifd_recv_state->params[i];if (p->running) { - p->quit = true;/* * multifd_recv_thread may hung at MULTIFD_FLAG_SYNC handle code, * however try to wakeup it without harm in cleanup phase.
Signature · 1 line
2.39.1
PATCH 3/4 migration/multifd: Join all multifd threads in order to avoid leaks
In reply to Leonardo BrasCurrent approach will only join threads that are still running.
For the threads not joined, resources or private memory are always kept in the process space and never reclaimed before process end, and this risks serious memory leaks.
This should usually not represent a big problem, since multifd migration is usually just ran at most a few times, and after it succeeds there is not much to be done before exiting the process.
Yet still, it should not hurt performance to join all of them.
--- migration/multifd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
migration/multifd.c +2 -1
@@ -1039,8 +1039,9 @@ void multifd_load_cleanup(void)* however try to wakeup it without harm in cleanup phase. */ qemu_sem_post (&p->sem_sync); -qemu_thread_join (&p->thread); } + +qemu_thread_join (&p->thread); }for (i =0 ; i <migrate_multifd_channels (); i++) {MultiFDRecvParams *p = &multifd_recv_state->params[i];
Signature · 1 line
2.39.1
PATCH 4/4 migration/multifd: Move load_cleanup inside incoming_state_destroy
In reply to Leonardo BrasCurrently running migration_incoming_state_destroy() without first running multifd_load_cleanup() will cause a yank error:
qemu-system-x86_64: ../util/yank.c:107: yank_unregister_instance:
Assertion `QLIST_EMPTY(&entry->yankfns)' failed.
(core dumped)
The above error happens in the target host, when multifd is being used for precopy, and then postcopy is triggered and the migration finishes. This will crash the VM in the target host.
To avoid that, move multifd_load_cleanup() inside migration_incoming_state_destroy(), so that the load cleanup becomes part of the incoming state destroying process.
Running multifd_load_cleanup() twice can become an issue, though, but the only scenario it could be ran twice is on process_incoming_migration_bh(). So removing this extra call is necessary.
On the other hand, this multifd_load_cleanup() call happens way before the migration_incoming_state_destroy() and having this happening before dirty_bitmap_mig_before_vm_start() and vm_start() may be a need.
So introduce a new function multifd_load_shutdown() that will mainly stop all multifd threads and close their QIOChannels. Then use this function instead of multifd_load_cleanup() to make sure nothing else is received before dirty_bitmap_mig_before_vm_start().
--- migration/multifd.h | 1 + migration/migration.c | 4 +++- migration/multifd.c | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-)
migration/multifd.h +1 -0
@@ -17,6 +17,7 @@ int multifd_save_setup(Error **errp);void multifd_save_cleanup (void );int multifd_load_setup (Error **errp);void multifd_load_cleanup (void ); +void multifd_load_shutdown (void );bool multifd_recv_all_channels_created (void );void multifd_recv_new_channel (QIOChannel *ioc,Error **errp);void multifd_recv_sync_main (void );
migration/migration.c +3 -1
@@ -302,6 +302,8 @@ void migration_incoming_state_destroy(void) {struct MigrationIncomingState *mis =migration_incoming_get_current (); +multifd_load_cleanup (); +if (mis->to_src_file) {/* Tell source that we are done */ migrate_send_rp_shut (mis,qemu_file_get_error (mis->from_src_file) !=0 );
@@ -543,7 +545,7 @@ static void process_incoming_migration_bh(void *opaque)*/ qemu_announce_self (&mis->announce_timer,migrate_announce_params ()); -multifd_load_cleanup (); +multifd_load_shutdown ();dirty_bitmap_mig_before_vm_start ();
migration/multifd.c +7 -0
@@ -1022,6 +1022,13 @@ static void multifd_recv_terminate_threads(Error *err) } } +void multifd_load_shutdown (void ) +{ +if (migrate_use_multifd () &&migrate_multi_channels_is_allowed ()) { +multifd_recv_terminate_threads (NULL ); + } +} +void multifd_load_cleanup (void ) {int i;
Signature · 1 line
2.39.1
Sent with the incorrect credentials. Sorry about the noise. Will resend them correctly.
Quoted 92 lines · Leonardo Bras
Since it's introduction in commit
f986c3d256("migration: Create multifd migration threads"),multifd_load_cleanup()never returned any value different than 0, neither set up any error on errp.Even though, on
process_incoming_migration_bh()an if clause uses it's return value to decide on setting autostart = false, which will never happen.In order to simplify the codebase, change
multifd_load_cleanup()signature to 'voidmultifd_load_cleanup(void)', and for every usage remove error handling or decision made based on return value != 0.Signed-off-by Leonardo Bras <[email protected]>--- migration/multifd.h | 2 +- migration/migration.c | 14 ++++---------- migration/multifd.c | 6 ++---- 3 files changed, 7 insertions(+), 15 deletions(-)migration/multifd.h +1
-1@@ -16,7 +16,7 @@int multifd_save_setup (Error **errp);void multifd_save_cleanup (void );int multifd_load_setup (Error **errp); -int multifd_load_cleanup (Error **errp); +void multifd_load_cleanup (void );bool multifd_recv_all_channels_created (void );void multifd_recv_new_channel (QIOChannel *ioc,Error **errp);void multifd_recv_sync_main (void );migration/migration.c +4
-10@@ -543,13 +543,7 @@ static void process_incoming_migration_bh(void *opaque)*/ qemu_announce_self (&mis->announce_timer,migrate_announce_params ()); -if (multifd_load_cleanup (&local_err) !=0 ) { -error_report_err (local_err); - autostart = false; - } -/* If global state section was not received or we are in running -state, we need to obey autostart. Any other state is set with -runstate_set. */ +multifd_load_cleanup ();dirty_bitmap_mig_before_vm_start ();@@ -649,9 +643,9 @@ fail:migrate_set_state (&mis->state,MIGRATION_STATUS_ACTIVE ,MIGRATION_STATUS_FAILED );qemu_fclose (mis->from_src_file); -if (multifd_load_cleanup (&local_err) !=0 ) { -error_report_err (local_err); - } + +multifd_load_cleanup (); +exit (EXIT_FAILURE ); }migration/multifd.c +2
-4@@ -1022,12 +1022,12 @@ static void multifd_recv_terminate_threads(Error *err) } } -int multifd_load_cleanup (Error **errp) +void multifd_load_cleanup (void ) {int i;if (!migrate_use_multifd () || !migrate_multi_channels_is_allowed ()) { -return 0 ; +return ; }multifd_recv_terminate_threads (NULL );for (i =0 ; i <migrate_multifd_channels (); i++) {@@ -1067,8 +1067,6 @@ int multifd_load_cleanup(Error **errp) multifd_recv_state->params =NULL ;g_free (multifd_recv_state); multifd_recv_state =NULL ; - -return 0 ; }void multifd_recv_sync_main (void )