]> git.ipfire.org Git - thirdparty/qemu.git/commit
migration: Take migration object refcount earlier for threads
authorPeter Xu <peterx@redhat.com>
Thu, 24 Oct 2024 21:30:49 +0000 (17:30 -0400)
committerPeter Xu <peterx@redhat.com>
Thu, 31 Oct 2024 19:48:18 +0000 (15:48 -0400)
commit7fc8beb16ee05399837efb41d0924fa1897da864
tree1e83c4f1fb33ebb2e1545d2b25129e5cd32a995c
parent228529d1fec305c05a7c7e173258b1c939ef3b1b
migration: Take migration object refcount earlier for threads

Both migration thread or background snapshot thread will take a refcount of
the migration object at the entrace of the thread function.

That makes sense, because it protects the object from being freed by the
main thread in migration_shutdown() later, but it might still race with it
if the thread is scheduled too late.  Consider the case right after
pthread_create() happened, VM shuts down with the object released, but
right after that the migration thread finally got created, referencing
MigrationState* in the opaque pointer which is already freed.

The only 100% safe way to make sure it won't get freed is taking the
refcount right before the thread is created, meanwhile when BQL is held.

Reviewed-by: Cédric Le Goater <clg@redhat.com>
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Link: https://lore.kernel.org/r/20241024213056.1395400-2-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
migration/migration.c