Skip to contentSkip to conversation

migration: NULL transport_data after freeing

PATCH In review Hanna Reitz 3 participants 4 messages Feb 17, 2022 – Mar 2, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: NULL transport_data after freeing

migration_incoming_state_destroy() NULLs all objects it frees after they are freed, presumably so that a subsequent call to the same function will not free them again, unless new objects have been created in the meantime.

transport_data is the exception, and it shows exactly this problem: When an incoming migration uses transport_cleanup() and transport_data, and a subsequent incoming migration (e.g. loadvm) occurs that does not, then when this second one is done, it will call transport_cleanup() on the old transport_data again -- which has already been freed. This is sometimes visible in the iotest 201, though for some reason I can only reproduce it with -m32.

To fix this, call transport_cleanup() only when transport_data is not NULL (otherwise there is nothing to clean up), and set transport_data to NULL when it has been cleaned up (i.e. freed).

(transport_cleanup() is used only by migration/socket.c, where socket_start_incoming_migration_internal() sets both it and transport_data to non-NULL values.)

---
 migration/migration.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
migration/migration.c +2 -1
index bcc385b94b..cdb2e76d02 100644
@@ -287,8 +287,9 @@ void migration_incoming_state_destroy(void)
         g_array_free(mis->postcopy_remote_fds, TRUE);
         mis->postcopy_remote_fds = NULL;
     }
-    if (mis->transport_cleanup) {
+    if (mis->transport_cleanup && mis->transport_data) {
         mis->transport_cleanup(mis->transport_data);
+        mis->transport_data = NULL;
     }
 
     qemu_event_reset(&mis->main_thread_load_event);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · * Hanna Reitz ([email protected]) wrote:

migration_incoming_state_destroy() NULLs all objects it frees after they are freed, presumably so that a subsequent call to the same function will not free them again, unless new objects have been created in the meantime.

transport_data is the exception, and it shows exactly this problem: When an incoming migration uses transport_cleanup() and transport_data, and a subsequent incoming migration (e.g. loadvm) occurs that does not, then when this second one is done, it will call transport_cleanup() on the old transport_data again -- which has already been freed. This is sometimes visible in the iotest 201, though for some reason I can only reproduce it with -m32.

To fix this, call transport_cleanup() only when transport_data is not NULL (otherwise there is nothing to clean up), and set transport_data to NULL when it has been cleaned up (i.e. freed).

Quoted 5 lines

(transport_cleanup() is used only by migration/socket.c, where socket_start_incoming_migration_internal() sets both it and transport_data to non-NULL values.)

That probably deserves a fixes: a59136f

Quoted 22 lines
---
 migration/migration.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
migration/migration.c +2 -1
index bcc385b94b..cdb2e76d02 100644
@@ -287,8 +287,9 @@ void migration_incoming_state_destroy(void)
         g_array_free(mis->postcopy_remote_fds, TRUE);
         mis->postcopy_remote_fds = NULL;
     }
-    if (mis->transport_cleanup) {
+    if (mis->transport_cleanup && mis->transport_data) {
         mis->transport_cleanup(mis->transport_data);
+        mis->transport_data = NULL;
     }
 
     qemu_event_reset(&mis->main_thread_load_event);
Signature · 1 line
2.34.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#2 lore raw
In reply to Hanna Reitz
Quoted 17 lines · Hanna Reitz

migration_incoming_state_destroy() NULLs all objects it frees after they are freed, presumably so that a subsequent call to the same function will not free them again, unless new objects have been created in the meantime.

transport_data is the exception, and it shows exactly this problem: When an incoming migration uses transport_cleanup() and transport_data, and a subsequent incoming migration (e.g. loadvm) occurs that does not, then when this second one is done, it will call transport_cleanup() on the old transport_data again -- which has already been freed. This is sometimes visible in the iotest 201, though for some reason I can only reproduce it with -m32.

To fix this, call transport_cleanup() only when transport_data is not NULL (otherwise there is nothing to clean up), and set transport_data to NULL when it has been cleaned up (i.e. freed).

Quoted 5 lines

(transport_cleanup() is used only by migration/socket.c, where socket_start_incoming_migration_internal() sets both it and transport_data to non-NULL values.)

I had a similar fix here:

https://lore.kernel.org/qemu-devel/[email protected]/

Though there it was because I need migration_incoming_transport_cleanup() for other purposes, so the fix came along.

My guess is this small fix will land earlier, if so I'll rebase. :)

Thanks,

Signature · 1 line
Peter Xu
#3 lore raw
Quoted 24 lines · * Peter Xu ([email protected]) wrote:
Quoted 17 lines · Hanna Reitz

migration_incoming_state_destroy() NULLs all objects it frees after they are freed, presumably so that a subsequent call to the same function will not free them again, unless new objects have been created in the meantime.

transport_data is the exception, and it shows exactly this problem: When an incoming migration uses transport_cleanup() and transport_data, and a subsequent incoming migration (e.g. loadvm) occurs that does not, then when this second one is done, it will call transport_cleanup() on the old transport_data again -- which has already been freed. This is sometimes visible in the iotest 201, though for some reason I can only reproduce it with -m32.

To fix this, call transport_cleanup() only when transport_data is not NULL (otherwise there is nothing to clean up), and set transport_data to NULL when it has been cleaned up (i.e. freed).

Quoted 5 lines

(transport_cleanup() is used only by migration/socket.c, where socket_start_incoming_migration_internal() sets both it and transport_data to non-NULL values.)

Quoted 8 lines

I had a similar fix here:

https://lore.kernel.org/qemu-devel/[email protected]/

Though there it was because I need migration_incoming_transport_cleanup() for other purposes, so the fix came along.

My guess is this small fix will land earlier, if so I'll rebase. :)

Actually it didn't; so since I've pulled a chunk of Peter's series in anyway I took the one from Peter's series.

Dave

Quoted 6 lines

Thanks,

Signature · 1 line
Peter Xu
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK