Skip to content
Snippets Groups Projects
  1. Sep 29, 2023
  2. Sep 27, 2023
    • Fabiano Rosas's avatar
      migration: Move return path cleanup to main migration thread · 36e9aab3
      Fabiano Rosas authored
      
      Now that the return path thread is allowed to finish during a paused
      migration, we can move the cleanup of the QEMUFiles to the main
      migration thread.
      
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Signed-off-by: default avatarFabiano Rosas <farosas@suse.de>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-ID: <20230918172822.19052-9-farosas@suse.de>
      36e9aab3
    • Fabiano Rosas's avatar
      migration: Replace the return path retry logic · ef796ee9
      Fabiano Rosas authored
      
      Replace the return path retry logic with finishing and restarting the
      thread. This fixes a race when resuming the migration that leads to a
      segfault.
      
      Currently when doing postcopy we consider that an IO error on the
      return path file could be due to a network intermittency. We then keep
      the thread alive but have it do cleanup of the 'from_dst_file' and
      wait on the 'postcopy_pause_rp' semaphore. When the user issues a
      migrate resume, a new return path is opened and the thread is allowed
      to continue.
      
      There's a race condition in the above mechanism. It is possible for
      the new return path file to be setup *before* the cleanup code in the
      return path thread has had a chance to run, leading to the *new* file
      being closed and the pointer set to NULL. When the thread is released
      after the resume, it tries to dereference 'from_dst_file' and crashes:
      
      Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
      [Switching to Thread 0x7fffd1dbf700 (LWP 9611)]
      0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
      154         return f->last_error;
      
      (gdb) bt
       #0  0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
       #1  0x00005555560e4983 in qemu_file_get_error (f=0x0) at ../migration/qemu-file.c:206
       #2  0x0000555555b9a1df in source_return_path_thread (opaque=0x555556e06000) at ../migration/migration.c:1876
       #3  0x000055555602e14f in qemu_thread_start (args=0x55555782e780) at ../util/qemu-thread-posix.c:541
       #4  0x00007ffff38d76ea in start_thread (arg=0x7fffd1dbf700) at pthread_create.c:477
       #5  0x00007ffff35efa6f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
      
      Here's the race (important bit is open_return_path happening before
      migration_release_dst_files):
      
      migration                 | qmp                         | return path
      --------------------------+-----------------------------+---------------------------------
      			    qmp_migrate_pause()
      			     shutdown(ms->to_dst_file)
      			      f->last_error = -EIO
      migrate_detect_error()
       postcopy_pause()
        set_state(PAUSED)
        wait(postcopy_pause_sem)
      			    qmp_migrate(resume)
      			    migrate_fd_connect()
      			     resume = state == PAUSED
      			     open_return_path <-- TOO SOON!
      			     set_state(RECOVER)
      			     post(postcopy_pause_sem)
      							(incoming closes to_src_file)
      							res = qemu_file_get_error(rp)
      							migration_release_dst_files()
      							ms->rp_state.from_dst_file = NULL
        post(postcopy_pause_rp_sem)
      							postcopy_pause_return_path_thread()
      							  wait(postcopy_pause_rp_sem)
      							rp = ms->rp_state.from_dst_file
      							goto retry
      							qemu_file_get_error(rp)
      							SIGSEGV
      -------------------------------------------------------------------------------------------
      
      We can keep the retry logic without having the thread alive and
      waiting. The only piece of data used by it is the 'from_dst_file' and
      it is only allowed to proceed after a migrate resume is issued and the
      semaphore released at migrate_fd_connect().
      
      Move the retry logic to outside the thread by waiting for the thread
      to finish before pausing the migration.
      
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Signed-off-by: default avatarFabiano Rosas <farosas@suse.de>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-ID: <20230918172822.19052-8-farosas@suse.de>
      ef796ee9
    • Fabiano Rosas's avatar
      migration: Consolidate return path closing code · d50f5dc0
      Fabiano Rosas authored
      
      We'll start calling the await_return_path_close_on_source() function
      from other parts of the code, so move all of the related checks and
      tracepoints into it.
      
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Signed-off-by: default avatarFabiano Rosas <farosas@suse.de>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-ID: <20230918172822.19052-7-farosas@suse.de>
      d50f5dc0
    • Fabiano Rosas's avatar
      migration: Remove redundant cleanup of postcopy_qemufile_src · b3b10115
      Fabiano Rosas authored
      
      This file is owned by the return path thread which is already doing
      cleanup.
      
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Signed-off-by: default avatarFabiano Rosas <farosas@suse.de>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-ID: <20230918172822.19052-6-farosas@suse.de>
      b3b10115
    • Fabiano Rosas's avatar
      migration: Fix possible race when shutting down to_dst_file · 7478fb0d
      Fabiano Rosas authored
      
      It's not safe to call qemu_file_shutdown() on the to_dst_file without
      first checking for the file's presence under the lock. The cleanup of
      this file happens at postcopy_pause() and migrate_fd_cleanup() which
      are not necessarily running in the same thread as migrate_fd_cancel().
      
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Signed-off-by: default avatarFabiano Rosas <farosas@suse.de>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-ID: <20230918172822.19052-5-farosas@suse.de>
      7478fb0d
    • Fabiano Rosas's avatar
      migration: Fix possible races when shutting down the return path · 639decf5
      Fabiano Rosas authored
      
      We cannot call qemu_file_shutdown() on the return path file without
      taking the file lock. The return path thread could be running it's
      cleanup code and have just cleared the from_dst_file pointer.
      
      Checking ms->to_dst_file for errors could also race with
      migrate_fd_cleanup() which clears the to_dst_file pointer.
      
      Protect both accesses by taking the file lock.
      
      This was caught by inspection, it should be rare, but the next patches
      will start calling this code from other places, so let's do the
      correct thing.
      
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Signed-off-by: default avatarFabiano Rosas <farosas@suse.de>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-ID: <20230918172822.19052-4-farosas@suse.de>
      639decf5
Loading