Skip to contentSkip to conversation

migration: cleanups, fixes and micro-optimizations

PATCH Applied Bin Guo 3 participants 18 messages May 18 – May 20

lore
Share & tools
lore
author
#3 lore raw

PATCH 2/8 migration/global_state: replace strcpy("") with explicit NUL termination

In reply to Bin Guo

Drop the unnecessary strcpy of an empty literal (and its spurious (char *)& cast) in favor of a direct NUL store, which avoids the libc call and hides no bugs behind a cast.

---
 migration/global_state.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/global_state.c +1 -1
index c1f90fce0f..91fefdf525 100644
@@ -148,7 +148,7 @@ static const VMStateDescription vmstate_globalstate = {
 void register_global_state(void)
 {
     /* We would use it independently that we receive it */
-    strcpy((char *)&global_state.runstate, "");
+    global_state.runstate[0] = '\0';
     global_state.received = false;
     vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
 }
Signature · 1 line
2.50.1 (Apple Git-155)

Bin Guo

author
#0 lore raw

cover migration: cleanups, fixes and micro-optimizations

This series collects several small improvements to the migration subsystem:

- Bug fix: off-by-one in multifd recv channel ID validation that could allow an out-of-bounds write (patch 5).
- Micro-optimizations: cache migrate_multifd_channels() in hot paths (patches 7-8), use stack-allocated bitmap instead of heap (patch 4), avoid per-element heap churn in vmstate ptr marker field (patch 3).
- Cleanups: collapse migration_fd_valid into a single boolean expression (patch 1), replace strcpy("") with explicit NUL termination (patch 2), merge thread-join and cleanup loops in multifd_recv_cleanup (patch 6).

No functional change intended except for the off-by-one fix in patch 5.

Bin Guo (8):
migration/fd: collapse migration_fd_valid into single boolean
expression
migration/global_state: replace strcpy("") with explicit NUL
termination
migration/vmstate: avoid per-element heap churn in vmsd ptr marker
field
migration/savevm: use stack-allocated bitmap in
configuration_validate_capabilities
migration/multifd: fix off-by-one in recv channel ID validation migration/multifd: merge thread-join and cleanup loops in
multifd_recv_cleanup
migration/multifd: cache migrate_multifd_channels() in send/recv hot
paths
migration/multifd: cache channel count in multifd_send_sync_main

 migration/fd.c           | 10 +---------
 migration/global_state.c |  2 +-
 migration/multifd.c      | 32 +++++++++++++++++--------------
 migration/savevm.c       |  5 ++---
 migration/vmstate.c      | 41 ++++++++++++++++------------------------
 5 files changed, 38 insertions(+), 52 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
author
#15 lore raw

PATCH 8/8 migration/multifd: cache channel count in multifd_send_sync_main

multifd_send_sync_main() is called once per RAM synchronization round during live migration. It iterates over all multifd channels twice (signal loop + wait loop), calling migrate_multifd_channels() independently in each loop header.

Cache migrate_multifd_channels() in a local thread_count variable at function entry, matching the pattern already used in multifd_send_setup() and multifd_recv_setup(). This eliminates 2 redundant config lookups per sync call.

---
 migration/multifd.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
migration/multifd.c +4 -2
index cc2fa90204..bc8aacd660 100644
@@ -611,13 +611,15 @@ static int multifd_zero_copy_flush(QIOChannel *c)
 int multifd_send_sync_main(MultiFDSyncReq req)
 {
     int i;
+    int thread_count;
     bool flush_zero_copy;
 
     assert(req != MULTIFD_SYNC_NONE);
 
+    thread_count = migrate_multifd_channels();
     flush_zero_copy = migrate_zero_copy_send();
 
-    for (i = 0; i < migrate_multifd_channels(); i++) {
+    for (i = 0; i < thread_count; i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
 
         if (multifd_send_should_exit()) {
@@ -634,7 +636,7 @@ int multifd_send_sync_main(MultiFDSyncReq req)
         qatomic_set(&p->pending_sync, req);
         qemu_sem_post(&p->sem);
     }
-    for (i = 0; i < migrate_multifd_channels(); i++) {
+    for (i = 0; i < thread_count; i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
 
         if (multifd_send_should_exit()) {
Signature · 1 line
2.50.1 (Apple Git-155)
author
#5 lore raw

PATCH 3/8 migration/vmstate: avoid per-element heap churn in vmsd ptr marker field

In reply to Bin Guo

For every NULL slot in a VMS_ARRAY_OF_POINTER (or every entry of a dynamic array), the saver allocates a 1-element fake VMStateField via g_new0 and frees it again right after the save. For arrays of thousands of entries this is thousands of malloc/free pairs on the hot save path.

Replace the heap-allocated marker with a stack-resident field populated by an init helper. The caller passes a pointer to a local VMStateField, the helper fills it in (still asserting the precondition), and no g_free is needed.

---
 migration/vmstate.c | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)
migration/vmstate.c +16 -25
index 2f13b48a37..9ced78532f 100644
@@ -59,29 +59,23 @@ vmstate_field_exists(const VMStateDescription *vmsd, const VMStateField *field,
  * array of a VMS_ARRAY_OF_POINTER VMSD field.  It's needed because we
  * can't dereference the NULL pointer.
  */
-static const VMStateField *
-vmsd_create_ptr_marker_field(const VMStateField *field)
+static void
+vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
 {
-    VMStateField *fake = g_new0(VMStateField, 1);
-
     /* It can only happen on an array of pointers! */
     assert(field->flags & VMS_ARRAY_OF_POINTER);
 
-    /* Some of fake's properties should match the original's */
-    fake->name = field->name;
-    fake->version_id = field->version_id;
-
-    /* Do not need "field_exists" check as it always exists */
-    fake->field_exists = NULL;
-
-    /* See vmstate_info_ptr_marker - use 1 byte to represent ptr status */
-    fake->size = 1;
-    fake->info = &vmstate_info_ptr_marker;
-    fake->flags = VMS_SINGLE;
-
-    /* All the rest fields shouldn't matter.. */
-
-    return (const VMStateField *)fake;
+    /* See vmstate_info_ptr_marker - 1 byte represents ptr status */
+    *fake = (VMStateField) {
+        .name = field->name,
+        .version_id = field->version_id,
+        /* Marker always exists, no field_exists callback needed */
+        .field_exists = NULL,
+        .size = 1,
+        .info = &vmstate_info_ptr_marker,
+        .flags = VMS_SINGLE,
+        /* All other fields stay zero-initialised */
+    };
 }
 
 static int vmstate_n_elems(void *opaque, const VMStateField *field)
@@ -680,6 +674,7 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
             for (i = 0; i < n_elems; i++) {
                 void *curr_elem = first_elem + size * i;
                 const VMStateField *inner_field;
+                VMStateField marker_field;
                 /* maximum number of elements to compress in the JSON blob */
                 int max_elems = vmsd_can_compress(field) ? (n_elems - i) : 1;
                 bool use_marker_field, is_null = false;
@@ -693,7 +688,8 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
                 use_marker_field = use_dynamic_array || is_null;
 
                 if (use_marker_field) {
-                    inner_field = vmsd_create_ptr_marker_field(field);
+                    vmsd_init_ptr_marker_field(&marker_field, field);
+                    inner_field = &marker_field;
                 } else {
                     inner_field = field;
                 }
@@ -730,11 +726,6 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
                                                     inner_field, vmdesc_loop,
                                                     i, max_elems, errp);
 
-                /* If we used a fake temp field.. free it now */
-                if (use_marker_field) {
-                    g_clear_pointer((gpointer *)&inner_field, g_free);
-                }
-
                 if (!ok) {
                     goto out;
                 }
Signature · 1 line
2.50.1 (Apple Git-155)
author
#9 lore raw

PATCH 5/8 migration/multifd: fix off-by-one in recv channel ID validation

In reply to Bin Guo

multifd_recv_initial_packet() validates the channel ID received from the source against the configured number of channels. The current check uses '>' which allows msg.id == N to pass through. This ID is then used to index multifd_recv_state->params[msg.id], which was allocated with g_new0(MultiFDRecvParams, N) -- an out-of-bounds access.

A malicious or buggy source could send id == N and cause heap corruption on the destination.

Fix by changing '>' to '>='. Also fix the error message to say "exceeds channel count" for accuracy.

---
 migration/multifd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
migration/multifd.c +3 -3
index 035cb70f7b..b3eef875cc 100644
@@ -210,9 +210,9 @@ static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
         return -1;
     }
 
-    if (msg.id > migrate_multifd_channels()) {
-        error_setg(errp, "multifd: received channel id %u is greater than "
-                   "number of channels %u", msg.id, migrate_multifd_channels());
+    if (msg.id >= migrate_multifd_channels()) {
+        error_setg(errp, "multifd: received channel id %u exceeds "
+                   "channel count %u", msg.id, migrate_multifd_channels());
         return -1;
     }
 
Signature · 1 line
2.50.1 (Apple Git-155)
author
#7 lore raw

PATCH 4/8 migration/savevm: use stack-allocated bitmap in configuration_validate_capabilities

In reply to Bin Guo

configuration_validate_capabilities() allocates a bitmap on the heap to track source capabilities via bitmap_new()/g_free(). Since MIGRATION_CAPABILITY__MAX is a small compile-time constant (< 64), a heap allocation for a bitmap this small is wasteful: it adds malloc/free overhead and a potential cache miss for a transient 8-byte allocation.

Replace with DECLARE_BITMAP() on the stack and bitmap_zero() to initialize. This eliminates the heap round-trip entirely.

---
 migration/savevm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
migration/savevm.c +2 -3
index d1dd696c17..23adaf9dd9 100644
@@ -347,10 +347,10 @@ static bool configuration_validate_capabilities(SaveState *state)
 {
     bool ret = true;
     MigrationState *s = migrate_get_current();
-    unsigned long *source_caps_bm;
+    DECLARE_BITMAP(source_caps_bm, MIGRATION_CAPABILITY__MAX);
     int i;
 
-    source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
+    bitmap_zero(source_caps_bm, MIGRATION_CAPABILITY__MAX);
     for (i = 0; i < state->caps_count; i++) {
         MigrationCapability capability = state->capabilities[i];
         set_bit(capability, source_caps_bm);
@@ -373,7 +373,6 @@ static bool configuration_validate_capabilities(SaveState *state)
         }
     }
 
-    g_free(source_caps_bm);
     return ret;
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)
author
#11 lore raw

PATCH 6/8 migration/multifd: merge thread-join and cleanup loops in multifd_recv_cleanup

In reply to Bin Guo

multifd_recv_cleanup() iterates over all receive channels twice:
one loop to join threads and another to clean up each channel. Unlike the send side where all threads must be signalled before any is joined (to avoid deadlock), on the receive side the threads are already terminated by multifd_recv_terminate_threads(). Each join simply waits for an already-terminated thread, so the join and cleanup for channel i are independent of channel j, and the two loops can safely be merged into one.

This cuts the iteration count in half and improves locality: the thread's resources are freed immediately after its join.

---
 migration/multifd.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
migration/multifd.c +2 -3
index b3eef875cc..67ee9bdf5e 100644
@@ -1153,15 +1153,14 @@ void multifd_recv_cleanup(void)
         return;
     }
     multifd_recv_terminate_threads(NULL);
+
     for (i = 0; i < migrate_multifd_channels(); i++) {
         MultiFDRecvParams *p = &multifd_recv_state->params[i];
 
         if (p->thread_created) {
             qemu_thread_join(&p->thread);
         }
-    }
-    for (i = 0; i < migrate_multifd_channels(); i++) {
-        multifd_recv_cleanup_channel(&multifd_recv_state->params[i]);
+        multifd_recv_cleanup_channel(p);
     }
     multifd_recv_cleanup_state();
 }
Signature · 1 line
2.50.1 (Apple Git-155)
author
#13 lore raw

PATCH 7/8 migration/multifd: cache migrate_multifd_channels() in send/recv hot paths

In reply to Bin Guo

multifd_send() and multifd_recv() are on the per-page-batch hot path of live migration. Both functions call migrate_multifd_channels() multiple times (3-4 calls each) for modulo arithmetic in the round-robin channel selection loop.

Each call goes through migrate_get_current() -> dereference MigrationState -> read parameters.multifd_channels. While each individual call is cheap, these functions execute for every page batch during the entire migration, easily millions of times.

Cache the return value in a local variable at function entry. The channel count is fixed for the duration of a migration and cannot change mid-flight.

For multifd_send(): 3 calls reduced to 1.
For multifd_recv(): 4 calls reduced to 1.

---
 migration/multifd.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
migration/multifd.c +9 -6
index 67ee9bdf5e..cc2fa90204 100644
@@ -362,13 +362,15 @@ bool multifd_send(MultiFDSendData **send_data)
     /* We wait here, until at least one channel is ready */
     qemu_sem_wait(&multifd_send_state->channels_ready);
 
+    int thread_count = migrate_multifd_channels();
+
     /*
      * next_channel can remain from a previous migration that was
      * using more channels, so ensure it doesn't overflow if the
      * limit is lower now.
      */
-    next_channel %= migrate_multifd_channels();
-    for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
+    next_channel %= thread_count;
+    for (i = next_channel;; i = (i + 1) % thread_count) {
         if (multifd_send_should_exit()) {
             return false;
         }
@@ -378,7 +380,7 @@ bool multifd_send(MultiFDSendData **send_data)
          * sender thread can clear it.
          */
         if (qatomic_read(&p->pending_job) == false) {
-            next_channel = (i + 1) % migrate_multifd_channels();
+            next_channel = (i + 1) % thread_count;
             break;
         }
     }
@@ -998,6 +1000,7 @@ bool multifd_recv(void)
     int i;
     static int next_recv_channel;
     MultiFDRecvParams *p = NULL;
+    int thread_count = migrate_multifd_channels();
     MultiFDRecvData *data = multifd_recv_state->data;
 
     /*
@@ -1005,8 +1008,8 @@ bool multifd_recv(void)
      * using more channels, so ensure it doesn't overflow if the
      * limit is lower now.
      */
-    next_recv_channel %= migrate_multifd_channels();
-    for (i = next_recv_channel;; i = (i + 1) % migrate_multifd_channels()) {
+    next_recv_channel %= thread_count;
+    for (i = next_recv_channel;; i = (i + 1) % thread_count) {
         if (multifd_recv_should_exit()) {
             return false;
         }
@@ -1014,7 +1017,7 @@ bool multifd_recv(void)
         p = &multifd_recv_state->params[i];
 
         if (qatomic_read(&p->pending_job) == false) {
-            next_recv_channel = (i + 1) % migrate_multifd_channels();
+            next_recv_channel = (i + 1) % thread_count;
             break;
         }
     }
Signature · 1 line
2.50.1 (Apple Git-155)
author
#1 lore raw

PATCH 1/8 migration/fd: collapse migration_fd_valid into single boolean expression

In reply to Bin Guo

Two if/return-true blocks followed by a final return false are equivalent to a single OR-expression and easier to scan.

---
 migration/fd.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
migration/fd.c +1 -9
index f05f95992f..939cdda4e1 100644
@@ -39,15 +39,7 @@ static bool fd_is_pipe(int fd)
 
 static bool migration_fd_valid(int fd)
 {
-    if (fd_is_socket(fd)) {
-        return true;
-    }
-
-    if (fd_is_pipe(fd)) {
-        return true;
-    }
-
-    return false;
+    return fd_is_socket(fd) || fd_is_pipe(fd);
 }
 
 QIOChannel *fd_connect_outgoing(MigrationState *s, const char *fdname,
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 5/8
#10 lore raw

Re: [PATCH 5/8] migration/multifd: fix off-by-one in recv channel ID validation

In reply to Bin Guo
Quoted 24 lines · Bin Guo

multifd_recv_initial_packet() validates the channel ID received from the source against the configured number of channels. The current check uses '>' which allows msg.id == N to pass through. This ID is then used to index multifd_recv_state->params[msg.id], which was allocated with g_new0(MultiFDRecvParams, N) -- an out-of-bounds access.

A malicious or buggy source could send id == N and cause heap corruption on the destination.

Fix by changing '>' to '>='. Also fix the error message to say "exceeds channel count" for accuracy.

---
 migration/multifd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
migration/multifd.c +0 -0
index 035cb70f7b..b3eef875cc 100644
@@ -210,9 +210,9 @@ static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
         return -1;

migration/multifd.c static int multifd_recv_initial_packet(QIOChannel *c, Error **errp) · line 217

Quoted 10 lines
migration/multifd.c +3 -3
     }
 
-    if (msg.id > migrate_multifd_channels()) {
-        error_setg(errp, "multifd: received channel id %u is greater than "
-                   "number of channels %u", msg.id, migrate_multifd_channels());
+    if (msg.id >= migrate_multifd_channels()) {
+        error_setg(errp, "multifd: received channel id %u exceeds "
+                   "channel count %u", msg.id, migrate_multifd_channels());
         return -1;
     }
#12 lore raw

Re: [PATCH 6/8] migration/multifd: merge thread-join and cleanup loops in multifd_recv_cleanup

In reply to Bin Guo
Quoted 2 lines · Bin Guo

multifd_recv_cleanup() iterates over all receive channels twice:
one loop to join threads and another to clean up each channel.

ok

Quoted 2 lines

Unlike the send side where all threads must be signalled before any is joined (to avoid deadlock),

Not super relevant what the send side does. It's just a similar piece of code, that's all.

Quoted 6 lines

on the receive side the threads are already
terminated by multifd_recv_terminate_threads(). Each join simply waits for an already-terminated thread, so the join and cleanup for channel i are independent of channel j, and the two loops can safely be merged into one.

We do really need to join all threads before proceeding. There's no guarantee on the the order in which they'll terminate.

Quoted 30 lines

This cuts the iteration count in half and improves locality: the thread's resources are freed immediately after its join.

---
 migration/multifd.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
migration/multifd.c +2 -3
index b3eef875cc..67ee9bdf5e 100644
@@ -1153,15 +1153,14 @@ void multifd_recv_cleanup(void)
         return;
     }
     multifd_recv_terminate_threads(NULL);
+
     for (i = 0; i < migrate_multifd_channels(); i++) {
         MultiFDRecvParams *p = &multifd_recv_state->params[i];
 
         if (p->thread_created) {
             qemu_thread_join(&p->thread);
         }
-    }
-    for (i = 0; i < migrate_multifd_channels(); i++) {
-        multifd_recv_cleanup_channel(&multifd_recv_state->params[i]);
+        multifd_recv_cleanup_channel(p);
     }
     multifd_recv_cleanup_state();
 }
#2 lore raw

Re: [PATCH 1/8] migration/fd: collapse migration_fd_valid into single boolean expression

In reply to Bin Guo
Quoted 19 lines · Bin Guo

Two if/return-true blocks followed by a final return false are equivalent to a single OR-expression and easier to scan.

---
 migration/fd.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
migration/fd.c +0 -3
index f05f95992f..939cdda4e1 100644
@@ -39,15 +39,7 @@ static bool fd_is_pipe(int fd)
 
 static bool migration_fd_valid(int fd)
 {
-    if (fd_is_socket(fd)) {
-        return true;
-    }

migration/fd.c static bool fd_is_pipe(int fd) · line 45

Quoted 10 lines
migration/fd.c +1 -6
-
-    if (fd_is_pipe(fd)) {
-        return true;
-    }
-
-    return false;
+    return fd_is_socket(fd) || fd_is_pipe(fd);
 }
 
 QIOChannel *fd_connect_outgoing(MigrationState *s, const char *fdname,

Let's not invest effort in replacing perfectly good code.

Reviewed-by on v1 2/8
#4 lore raw

Re: [PATCH 2/8] migration/global_state: replace strcpy("") with explicit NUL termination

In reply to Bin Guo
Quoted 14 lines · Bin Guo

Drop the unnecessary strcpy of an empty literal (and its spurious (char *)& cast) in favor of a direct NUL store, which avoids the libc call and hides no bugs behind a cast.

---
 migration/global_state.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/global_state.c +0 -0
index c1f90fce0f..91fefdf525 100644

migration/global_state.c static const VMStateDescription vmstate_globalstate = { · line 154

Quoted 8 lines
migration/global_state.c +1 -1
@@ -148,7 +148,7 @@ static const VMStateDescription vmstate_globalstate = {
 void register_global_state(void)
 {
     /* We would use it independently that we receive it */
-    strcpy((char *)&global_state.runstate, "");
+    global_state.runstate[0] = '\0';
     global_state.received = false;
     vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
 }
Reviewed-by on v1 4/8
#8 lore raw

Re: [PATCH 4/8] migration/savevm: use stack-allocated bitmap in configuration_validate_capabilities

In reply to Bin Guo
Quoted 33 lines · Bin Guo

configuration_validate_capabilities() allocates a bitmap on the heap to track source capabilities via bitmap_new()/g_free(). Since MIGRATION_CAPABILITY__MAX is a small compile-time constant (< 64), a heap allocation for a bitmap this small is wasteful: it adds malloc/free overhead and a potential cache miss for a transient 8-byte allocation.

Replace with DECLARE_BITMAP() on the stack and bitmap_zero() to initialize. This eliminates the heap round-trip entirely.

---
 migration/savevm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
migration/savevm.c +2 -2
index d1dd696c17..23adaf9dd9 100644
@@ -347,10 +347,10 @@ static bool configuration_validate_capabilities(SaveState *state)
 {
     bool ret = true;
     MigrationState *s = migrate_get_current();
-    unsigned long *source_caps_bm;
+    DECLARE_BITMAP(source_caps_bm, MIGRATION_CAPABILITY__MAX);
     int i;
 
-    source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
+    bitmap_zero(source_caps_bm, MIGRATION_CAPABILITY__MAX);
     for (i = 0; i < state->caps_count; i++) {
         MigrationCapability capability = state->capabilities[i];
         set_bit(capability, source_caps_bm);

migration/savevm.c static bool configuration_validate_capabilities(SaveState *state) · line 377

Quoted 6 lines
migration/savevm.c +0 -1
@@ -373,7 +373,6 @@ static bool configuration_validate_capabilities(SaveState *state)
         }
     }
 
-    g_free(source_caps_bm);
     return ret;
 }
Reviewed-by on v1 7/8
#14 lore raw

Re: [PATCH 7/8] migration/multifd: cache migrate_multifd_channels() in send/recv hot paths

In reply to Bin Guo
Quoted 73 lines · Bin Guo

multifd_send() and multifd_recv() are on the per-page-batch hot path of live migration. Both functions call migrate_multifd_channels() multiple times (3-4 calls each) for modulo arithmetic in the round-robin channel selection loop.

Each call goes through migrate_get_current() -> dereference MigrationState -> read parameters.multifd_channels. While each individual call is cheap, these functions execute for every page batch during the entire migration, easily millions of times.

Cache the return value in a local variable at function entry. The channel count is fixed for the duration of a migration and cannot change mid-flight.

For multifd_send(): 3 calls reduced to 1.
For multifd_recv(): 4 calls reduced to 1.

---
 migration/multifd.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
migration/multifd.c +8 -5
index 67ee9bdf5e..cc2fa90204 100644
@@ -362,13 +362,15 @@ bool multifd_send(MultiFDSendData **send_data)
     /* We wait here, until at least one channel is ready */
     qemu_sem_wait(&multifd_send_state->channels_ready);
 
+    int thread_count = migrate_multifd_channels();
+
     /*
      * next_channel can remain from a previous migration that was
      * using more channels, so ensure it doesn't overflow if the
      * limit is lower now.
      */
-    next_channel %= migrate_multifd_channels();
-    for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) {
+    next_channel %= thread_count;
+    for (i = next_channel;; i = (i + 1) % thread_count) {
         if (multifd_send_should_exit()) {
             return false;
         }
@@ -378,7 +380,7 @@ bool multifd_send(MultiFDSendData **send_data)
          * sender thread can clear it.
          */
         if (qatomic_read(&p->pending_job) == false) {
-            next_channel = (i + 1) % migrate_multifd_channels();
+            next_channel = (i + 1) % thread_count;
             break;
         }
     }
@@ -998,6 +1000,7 @@ bool multifd_recv(void)
     int i;
     static int next_recv_channel;
     MultiFDRecvParams *p = NULL;
+    int thread_count = migrate_multifd_channels();
     MultiFDRecvData *data = multifd_recv_state->data;
 
     /*
@@ -1005,8 +1008,8 @@ bool multifd_recv(void)
      * using more channels, so ensure it doesn't overflow if the
      * limit is lower now.
      */
-    next_recv_channel %= migrate_multifd_channels();
-    for (i = next_recv_channel;; i = (i + 1) % migrate_multifd_channels()) {
+    next_recv_channel %= thread_count;
+    for (i = next_recv_channel;; i = (i + 1) % thread_count) {
         if (multifd_recv_should_exit()) {
             return false;
         }

migration/multifd.c bool multifd_recv(void) · line 1023

Quoted 8 lines
migration/multifd.c +1 -1
@@ -1014,7 +1017,7 @@ bool multifd_recv(void)
         p = &multifd_recv_state->params[i];
 
         if (qatomic_read(&p->pending_job) == false) {
-            next_recv_channel = (i + 1) % migrate_multifd_channels();
+            next_recv_channel = (i + 1) % thread_count;
             break;
         }
     }
Reviewed-by on v1 8/8
#16 lore raw

Re: [PATCH 8/8] migration/multifd: cache channel count in multifd_send_sync_main

In reply to Bin Guo
Quoted 37 lines · Bin Guo

multifd_send_sync_main() is called once per RAM synchronization round during live migration. It iterates over all multifd channels twice (signal loop + wait loop), calling migrate_multifd_channels() independently in each loop header.

Cache migrate_multifd_channels() in a local thread_count variable at function entry, matching the pattern already used in multifd_send_setup() and multifd_recv_setup(). This eliminates 2 redundant config lookups per sync call.

---
 migration/multifd.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
migration/multifd.c +3 -1
index cc2fa90204..bc8aacd660 100644
@@ -611,13 +611,15 @@ static int multifd_zero_copy_flush(QIOChannel *c)
 int multifd_send_sync_main(MultiFDSyncReq req)
 {
     int i;
+    int thread_count;
     bool flush_zero_copy;
 
     assert(req != MULTIFD_SYNC_NONE);
 
+    thread_count = migrate_multifd_channels();
     flush_zero_copy = migrate_zero_copy_send();
 
-    for (i = 0; i < migrate_multifd_channels(); i++) {
+    for (i = 0; i < thread_count; i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
 
         if (multifd_send_should_exit()) {

migration/multifd.c int multifd_send_sync_main(MultiFDSyncReq req) · line 642

Quoted 8 lines
migration/multifd.c +1 -1
@@ -634,7 +636,7 @@ int multifd_send_sync_main(MultiFDSyncReq req)
         qatomic_set(&p->pending_sync, req);
         qemu_sem_post(&p->sem);
     }
-    for (i = 0; i < migrate_multifd_channels(); i++) {
+    for (i = 0; i < thread_count; i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
 
         if (multifd_send_should_exit()) {
Reviewed-by on v1 3/8
#6 lore raw

Re: [PATCH 3/8] migration/vmstate: avoid per-element heap churn in vmsd ptr marker field

In reply to Bin Guo
Quoted 83 lines · Bin Guo

For every NULL slot in a VMS_ARRAY_OF_POINTER (or every entry of a dynamic array), the saver allocates a 1-element fake VMStateField via g_new0 and frees it again right after the save. For arrays of thousands of entries this is thousands of malloc/free pairs on the hot save path.

Replace the heap-allocated marker with a stack-resident field populated by an init helper. The caller passes a pointer to a local VMStateField, the helper fills it in (still asserting the precondition), and no g_free is needed.

---
 migration/vmstate.c | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)
migration/vmstate.c +16 -20
index 2f13b48a37..9ced78532f 100644
@@ -59,29 +59,23 @@ vmstate_field_exists(const VMStateDescription *vmsd, const VMStateField *field,
  * array of a VMS_ARRAY_OF_POINTER VMSD field.  It's needed because we
  * can't dereference the NULL pointer.
  */
-static const VMStateField *
-vmsd_create_ptr_marker_field(const VMStateField *field)
+static void
+vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
 {
-    VMStateField *fake = g_new0(VMStateField, 1);
-
     /* It can only happen on an array of pointers! */
     assert(field->flags & VMS_ARRAY_OF_POINTER);
 
-    /* Some of fake's properties should match the original's */
-    fake->name = field->name;
-    fake->version_id = field->version_id;
-
-    /* Do not need "field_exists" check as it always exists */
-    fake->field_exists = NULL;
-
-    /* See vmstate_info_ptr_marker - use 1 byte to represent ptr status */
-    fake->size = 1;
-    fake->info = &vmstate_info_ptr_marker;
-    fake->flags = VMS_SINGLE;
-
-    /* All the rest fields shouldn't matter.. */
-
-    return (const VMStateField *)fake;
+    /* See vmstate_info_ptr_marker - 1 byte represents ptr status */
+    *fake = (VMStateField) {
+        .name = field->name,
+        .version_id = field->version_id,
+        /* Marker always exists, no field_exists callback needed */
+        .field_exists = NULL,
+        .size = 1,
+        .info = &vmstate_info_ptr_marker,
+        .flags = VMS_SINGLE,
+        /* All other fields stay zero-initialised */
+    };
 }
 
 static int vmstate_n_elems(void *opaque, const VMStateField *field)
@@ -680,6 +674,7 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
             for (i = 0; i < n_elems; i++) {
                 void *curr_elem = first_elem + size * i;
                 const VMStateField *inner_field;
+                VMStateField marker_field;
                 /* maximum number of elements to compress in the JSON blob */
                 int max_elems = vmsd_can_compress(field) ? (n_elems - i) : 1;
                 bool use_marker_field, is_null = false;
@@ -693,7 +688,8 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
                 use_marker_field = use_dynamic_array || is_null;
 
                 if (use_marker_field) {
-                    inner_field = vmsd_create_ptr_marker_field(field);
+                    vmsd_init_ptr_marker_field(&marker_field, field);
+                    inner_field = &marker_field;
                 } else {
                     inner_field = field;
                 }
@@ -730,11 +726,6 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
                                                     inner_field, vmdesc_loop,

migration/vmstate.c static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd, · line 731

Quoted 10 lines
migration/vmstate.c +0 -5
                                                     i, max_elems, errp);
 
-                /* If we used a fake temp field.. free it now */
-                if (use_marker_field) {
-                    g_clear_pointer((gpointer *)&inner_field, g_free);
-                }
-
                 if (!ok) {
                     goto out;
                 }
Applied
#17 lore raw
In reply to Bin Guo
Quoted 17 lines · Bin Guo

This series collects several small improvements to the migration subsystem:

- Bug fix: off-by-one in multifd recv channel ID validation that could allow an out-of-bounds write (patch 5).
- Micro-optimizations: cache migrate_multifd_channels() in hot paths (patches 7-8), use stack-allocated bitmap instead of heap (patch 4), avoid per-element heap churn in vmstate ptr marker field (patch 3).
- Cleanups: collapse migration_fd_valid into a single boolean expression (patch 1), replace strcpy("") with explicit NUL termination (patch 2), merge thread-join and cleanup loops in multifd_recv_cleanup (patch 6).

No functional change intended except for the off-by-one fix in patch 5.

Quoted 15 lines

Bin Guo (8):
migration/fd: collapse migration_fd_valid into single boolean
expression
migration/global_state: replace strcpy("") with explicit NUL
termination
migration/vmstate: avoid per-element heap churn in vmsd ptr marker
field
migration/savevm: use stack-allocated bitmap in
configuration_validate_capabilities
migration/multifd: fix off-by-one in recv channel ID validation migration/multifd: merge thread-join and cleanup loops in
multifd_recv_cleanup
migration/multifd: cache migrate_multifd_channels() in send/recv hot
paths
migration/multifd: cache channel count in multifd_send_sync_main

For patch 1, I tend to agree with Fabiano; I don't clearly see why the oneliner is always better. Oneliners can be harder to read to some (for most of the cases, myself included..).

For patch 6, I confess I can't see an issue with the current patch, but since there's a discussion, let it happen.

I queued the rest, thanks.

Signature · 1 line
Peter Xu