Skip to contentSkip to conversation

migration: Remove extra type-checking from vmstate macros

PATCH Superseded Fabiano Rosas 4 participants 27 messages Jul 29 – Aug 11 Rust

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover migration: Remove extra type-checking from vmstate macros

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

 include/migration/vmstate.h        | 255 ++++++++++-------------------
 migration/savevm.c                 |  10 +-
 migration/vmstate.c                |  50 ++++--
 rust/bindings/migration-sys/lib.rs |  15 +-
 rust/migration/src/vmstate.rs      |  30 +++-
 rust/tests/tests/vmstate_tests.rs  |  30 ++--
 6 files changed, 177 insertions(+), 213 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 migration: Remove VMSTATE_ARRAY_INT32_UNSAFE

The ARRAY_INT32_UNSAFE vmstate macro is not used anywhere. Remove it.

---
 include/migration/vmstate.h | 9 ---------
 1 file changed, 9 deletions(-)
include/migration/vmstate.h +0 -9
index 1b7f295417e..92a1a9fe98e 100644
@@ -451,15 +451,6 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
 }
 
-#define VMSTATE_ARRAY_INT32_UNSAFE(_field, _state, _field_num, _info, _type) {\
-    .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
-    .info       = &(_info),                                          \
-    .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_INT32,                                  \
-    .offset     = vmstate_offset_varray(_state, _field, _type),      \
-}
-
 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
Signature · 1 line
2.53.0
#3 lore raw

PATCH 2/4 migration: Introduce VMStateOffset

In reply to Fabiano Rosas

When migrating a buffer or array, the vmstate code needs to know the size of the buffer and the number of elements of the array. Today a vmstate writer can choose from a number of macros that take as last input the name of a struct member from where the size/num will be read.

At load time, the code will access those value via an opaque pointer to the migrated data and therefore it needs to also know the size of the struct member at that offset.

Currently that information is communicated by means of the VMS_VARRAY_* and VMS_VBUFFER_* flags, where each possible type is represented by a flag.

So far, that's all fine, but since the vmstate code makes heavy use of macros, handling several types individually (i.e. by name: int, int32_t, etc) requires several versions of a same macro, one for each type. E.g: VMSTATE_VBUFFER_ALLOC_UINT32

                                 ^

This creates a pattern where the vmstate writer has to match the macro name to the data type and has resulted in the code having a tendency of having one macro version for each type, for each type of vmstate.

There is also some cognitive load to deal with, e.g. VMSTATE_VARRAY_INT32 doesn't hold an array of int32, it holds an array of something else and the number of elements for the array is stored in a variable of type int32.

We're now dealing with the scenario where the code has been expecting int32_t at some places, but a uint64_t macro variant has been added without the code being updated.

To address all these situations, introduce a new struct that will hold the offset of the struct members, but also their size, so the various extra macros can all be removed and the person writing the vmstate doesn't need to care about type-checking. Still, keep a minimum check that those fields are at least integers and fit into 64 bits.

What changes:

1) type checking changes from individual types to a single check for all integers;

2) there are new ways to access the offsets;

num_offset -> num_offset.off
size_offset -> size_offset.off
[new] num_offset.size
[new] size_offset.size

2) reading the offsets goes from checking the VMS_VARRAY_* flags in an if/elseif block to comparing offset.size against the hardcoded sizes in bytes;

3) the VMS_VARRAY_* and VMS_VBUFFER_* flags become obsolete. Removed in the next patch;

4) memory usage increases +1 byte per vmstate;

---
 include/migration/vmstate.h | 66 +++++++++++++++++++++++--------------
 migration/savevm.c          |  4 +--
 migration/vmstate.c         | 51 ++++++++++++++++++----------
 3 files changed, 76 insertions(+), 45 deletions(-)
include/migration/vmstate.h +41 -25
index 92a1a9fe98e..2ad3cc01371 100644
@@ -31,6 +31,7 @@
 
 typedef struct VMStateInfo VMStateInfo;
 typedef struct VMStateField VMStateField;
+typedef struct VMStateOffset VMStateOffset;
 
 /*
  * VMStateInfo allows customized migration of objects that don't fit in
@@ -187,6 +188,11 @@ typedef enum {
     MIG_PRI_MAX,
 } MigrationPriority;
 
+struct VMStateOffset {
+    uint32_t off;
+    uint8_t size;
+};
+
 struct VMStateField {
     const char *name;
     size_t offset;
@@ -205,11 +211,11 @@ struct VMStateField {
      * pointer point to.
      */
     size_t size;
-    size_t size_offset;
+    VMStateOffset size_offset;
 
     size_t start;
     int num;
-    size_t num_offset;
+    VMStateOffset num_offset;
     const VMStateInfo *info;
     enum VMStateFlags flags;
     const VMStateDescription *vmsd;
@@ -328,6 +334,16 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     (type_check(t1, typeof_elt_of_field(t2, f))                      \
      + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
 
+#define type_check_int64(t)                                             \
+    (((ptrdiff_t)0 * (ptrdiff_t)(~((t)0))) +                            \
+     (0 * sizeof(char[(sizeof(t) <= sizeof(uint64_t)) ? 1 : -1])))
+
+#define vmstate_field_offset(_state, _field) {                  \
+    .off = (offsetof(_state, _field) +                          \
+            type_check_int64(typeof_field(_state, _field))),    \
+    .size = sizeof(typeof_field(_state, _field)),               \
+}
+
 #define vmstate_offset_value(_state, _field, _type)                  \
     (offsetof(_state, _field) +                                      \
      type_check(_type, typeof_field(_state, _field)))
@@ -454,7 +470,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
@@ -464,7 +480,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
@@ -474,7 +490,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
@@ -484,7 +500,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
@@ -494,7 +510,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
@@ -504,7 +520,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT16,                                 \
@@ -583,7 +599,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     _field, _state, _field_num, _version, _vmsd, _type) {            \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
@@ -596,7 +612,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     _field, _state, _field_num, _version, _vmsd, _type) {             \
     .name       = (stringify(_field)),                                \
     .version_id = (_version),                                         \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),           \
     .vmsd       = &(_vmsd),                                           \
     .size       = sizeof(_type),                                      \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
@@ -608,7 +624,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
     .name       = (stringify(_field)),                                    \
     .version_id = (_version),                                             \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
+    .num_offset = vmstate_field_offset(_state, _field_num),               \
     .info       = &(_info),                                               \
     .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
@@ -650,7 +666,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -674,7 +690,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -684,7 +700,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -694,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
@@ -703,7 +719,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -713,7 +729,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -725,7 +741,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
@@ -746,7 +762,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
@@ -757,7 +773,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
@@ -767,7 +783,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
@@ -777,7 +793,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint64_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER | VMS_POINTER,                       \
     .offset       = offsetof(_state, _field),                        \
@@ -788,7 +804,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
     .offset       = offsetof(_state, _field),                        \
@@ -848,7 +864,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
     .name         = "unused",                                        \
     .field_exists = (_test),                                         \
-    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset   = vmstate_field_offset(_state, _field_num),        \
     .version_id   = (_version),                                      \
     .size         = (_size),                                         \
     .info         = &vmstate_info_unused_buffer,                     \
@@ -862,7 +878,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .field_exists = (_test),                                         \
     .version_id   = (_version),                                      \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
migration/savevm.c +2 -2
index 34dd06f9f73..a272bcfd0b4 100644
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                      * Size must be provided because dest QEMU needs that
                      * info to know what to allocate
                      */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);
                 } else {
                     /*
                      * Otherwise size info isn't useful (because it's
                      * always the size of host pointer), detect accidental
                      * setup of sizes in this case.
                      */
-                    assert(field->size == 0 && field->size_offset == 0);
+                    assert(field->size == 0 && field->size_offset.size == 0);
                 }
                 /*
                  * VMS_ARRAY_OF_POINTER must be used only together with one
migration/vmstate.c +33 -18

88 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

Quoted 80 lines · Fabiano Rosas

When migrating a buffer or array, the vmstate code needs to know the size of the buffer and the number of elements of the array. Today a vmstate writer can choose from a number of macros that take as last input the name of a struct member from where the size/num will be read.

At load time, the code will access those value via an opaque pointer to the migrated data and therefore it needs to also know the size of the struct member at that offset.

Currently that information is communicated by means of the VMS_VARRAY_* and VMS_VBUFFER_* flags, where each possible type is represented by a flag.

So far, that's all fine, but since the vmstate code makes heavy use of macros, handling several types individually (i.e. by name: int, int32_t, etc) requires several versions of a same macro, one for each type. E.g: VMSTATE_VBUFFER_ALLOC_UINT32

                                  ^

This creates a pattern where the vmstate writer has to match the macro name to the data type and has resulted in the code having a tendency of having one macro version for each type, for each type of vmstate.

There is also some cognitive load to deal with, e.g. VMSTATE_VARRAY_INT32 doesn't hold an array of int32, it holds an array of something else and the number of elements for the array is stored in a variable of type int32.

We're now dealing with the scenario where the code has been expecting int32_t at some places, but a uint64_t macro variant has been added without the code being updated.

To address all these situations, introduce a new struct that will hold the offset of the struct members, but also their size, so the various extra macros can all be removed and the person writing the vmstate doesn't need to care about type-checking. Still, keep a minimum check that those fields are at least integers and fit into 64 bits.

What changes:

1) type checking changes from individual types to a single check for

    all integers;

2) there are new ways to access the offsets;

num_offset -> num_offset.off
size_offset -> size_offset.off
[new] num_offset.size
[new] size_offset.size

2) reading the offsets goes from checking the VMS_VARRAY_* flags in an if/elseif block to comparing offset.size against the hardcoded sizes in bytes;

3) the VMS_VARRAY_* and VMS_VBUFFER_* flags become obsolete. Removed

    in the next patch;

4) memory usage increases +1 byte per vmstate;

---
  include/migration/vmstate.h | 66 +++++++++++++++++++++++--------------
  migration/savevm.c          |  4 +--
  migration/vmstate.c         | 51 ++++++++++++++++++----------
  3 files changed, 76 insertions(+), 45 deletions(-)
include/migration/vmstate.h +1 -0
index 92a1a9fe98e..2ad3cc01371 100644
@@ -31,6 +31,7 @@
 
  typedef struct VMStateInfo VMStateInfo;
  typedef struct VMStateField VMStateField;
+typedef struct VMStateOffset VMStateOffset;
 
  /*
   * VMStateInfo allows customized migration of objects that don't fit in

include/migration/vmstate.h typedef enum { · line 194

Quoted 7 lines
include/migration/vmstate.h +4 -0
@@ -187,6 +188,11 @@ typedef enum {
      MIG_PRI_MAX,
  } MigrationPriority;
 
+struct VMStateOffset {
+    uint32_t off;
+    uint8_t size;
+};

A bit strange to have offset field (off) inside "Offset" structure.

VMStatePosition may be?

include/migration/vmstate.h struct VMStateField { · line 214

Quoted 10 lines
include/migration/vmstate.h +2 -1
+
  struct VMStateField {
      const char *name;
      size_t offset;
@@ -205,11 +211,11 @@ struct VMStateField {
       * pointer point to.
       */
      size_t size;
-    size_t size_offset;
+    VMStateOffset size_offset;

size_offset variable of type Offset, which has offset and size fields inside. And different meaning for each "size" and "offset" in this context.. Partly preexisting, but we make it even more complicated by this patch. Maybe:

VMStatePosition size_pos;

or even

VMStatePosition bufsize_pos;

to also distinguish "buffer size" consept of "size of field containing buffer size".

Side questions: is there any difference between VARRAY of bytes and BUFFER? We could probably share same @num and @num_offset fields for buffers, an drop size/size_offset at all.

Quoted 9 lines
include/migration/vmstate.h +1 -1
      size_t start;
      int num;
-    size_t num_offset;
+    VMStateOffset num_offset;
      const VMStateInfo *info;
      enum VMStateFlags flags;
      const VMStateDescription *vmsd;

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 338

Quoted 5 lines
include/migration/vmstate.h +2 -0
@@ -328,6 +334,16 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      (type_check(t1, typeof_elt_of_field(t2, f))                      \
       + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
 
+#define type_check_int64(t)                                             \
+    (((ptrdiff_t)0 * (ptrdiff_t)(~((t)0))) +                            \

why use ptrdiff_t, if want to compatibility with int64?

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 339

Quoted 1 line
include/migration/vmstate.h +1 -0
+     (0 * sizeof(char[(sizeof(t) <= sizeof(uint64_t)) ? 1 : -1])))

could QEMU_BUILD_BUG_ON() be used to make more readable check?

Quoted 334 lines
include/migration/vmstate.h +30 -23
+
+#define vmstate_field_offset(_state, _field) {                  \
+    .off = (offsetof(_state, _field) +                          \
+            type_check_int64(typeof_field(_state, _field))),    \
+    .size = sizeof(typeof_field(_state, _field)),               \
+}
+
  #define vmstate_offset_value(_state, _field, _type)                  \
      (offsetof(_state, _field) +                                      \
       type_check(_type, typeof_field(_state, _field)))
@@ -454,7 +470,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
@@ -464,7 +480,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
@@ -474,7 +490,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
@@ -484,7 +500,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
@@ -494,7 +510,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
@@ -504,7 +520,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT16,                                 \
@@ -583,7 +599,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      _field, _state, _field_num, _version, _vmsd, _type) {            \
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
@@ -596,7 +612,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      _field, _state, _field_num, _version, _vmsd, _type) {             \
      .name       = (stringify(_field)),                                \
      .version_id = (_version),                                         \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),           \
      .vmsd       = &(_vmsd),                                           \
      .size       = sizeof(_type),                                      \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
@@ -608,7 +624,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
      .name       = (stringify(_field)),                                    \
      .version_id = (_version),                                             \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
+    .num_offset = vmstate_field_offset(_state, _field_num),               \
      .info       = &(_info),                                               \
      .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
      .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
@@ -650,7 +666,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -674,7 +690,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -684,7 +700,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -694,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
@@ -703,7 +719,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -713,7 +729,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -725,7 +741,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
      .offset     = vmstate_offset_pointer(_state, _field, _type),     \
@@ -746,7 +762,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .size         = (_multiply),                                      \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
@@ -757,7 +773,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
@@ -767,7 +783,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
@@ -777,7 +793,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint64_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER | VMS_POINTER,                       \
      .offset       = offsetof(_state, _field),                        \
@@ -788,7 +804,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
      .offset       = offsetof(_state, _field),                        \
@@ -848,7 +864,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
      .name         = "unused",                                        \
      .field_exists = (_test),                                         \
-    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset   = vmstate_field_offset(_state, _field_num),        \
      .version_id   = (_version),                                      \
      .size         = (_size),                                         \
      .info         = &vmstate_info_unused_buffer,                     \
@@ -862,7 +878,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .field_exists = (_test),                                         \
      .version_id   = (_version),                                      \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_bitmap,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
migration/savevm.c +2 -2
index 34dd06f9f73..a272bcfd0b4 100644
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                       * Size must be provided because dest QEMU needs that
                       * info to know what to allocate
                       */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);
                  } else {
                      /*
                       * Otherwise size info isn't useful (because it's
                       * always the size of host pointer), detect accidental
                       * setup of sizes in this case.
                       */
-                    assert(field->size == 0 && field->size_offset == 0);
+                    assert(field->size == 0 && field->size_offset.size == 0);
                  }
                  /*
                   * VMS_ARRAY_OF_POINTER must be used only together with one
migration/vmstate.c +33 -18
index 50ebe378452..0a0b9faa20e 100644
@@ -78,32 +78,45 @@ vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
      };
  }
 
-static int vmstate_n_elems(void *opaque, const VMStateField *field)
+static uint64_t vmstate_read_from_offset(void *opaque,
+                                         const VMStateOffset *offset)
  {
-    int n_elems = 1;
+    uint8_t *ptr = (uint8_t *)opaque + offset->off;
+
+    switch (offset->size) {
+    case 1:
+        return *(uint8_t *)ptr;
+    case 2:
+        return *(uint16_t *)ptr;
+    case 4:
+        return *(uint32_t *)ptr;
+    case 8:
+        return *(uint64_t *)ptr;
+    }
+    g_assert_not_reached();
+}
+
+static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
+{
+    uint64_t n_elems = 1;
 
      if (field->flags & VMS_ARRAY) {
          n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY_INT32) {
-        n_elems = *(int32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT32) {
-        n_elems = *(uint32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT16) {
-        n_elems = *(uint16_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT8) {
-        n_elems = *(uint8_t *)(opaque + field->num_offset);
+    } else if (field->flags & (VMS_VARRAY_INT32 | VMS_VARRAY_UINT32
+                               | VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8)) {
+        n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
      }
 
      trace_vmstate_n_elems(field->name, n_elems);
      return n_elems;
  }
 
-static int vmstate_size(void *opaque, const VMStateField *field)
+static uint64_t vmstate_size(void *opaque, const VMStateField *field)
  {
-    int size;
+    uint64_t size;
 
      if (field->flags & VMS_VBUFFER) {
-        size = *(int32_t *)(opaque + field->size_offset);
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
          if (field->flags & VMS_MULTIPLY) {
              size *= field->size;
          }
@@ -124,7 +137,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
                                   void *opaque)
  {
      if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
-        gsize size = vmstate_size(opaque, field);
+        uint64_t size = vmstate_size(opaque, field);
          size *= vmstate_n_elems(opaque, field);
          if (size) {
              *(void **)ptr = g_malloc(size);
@@ -335,8 +348,9 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
 
          if (exists) {
              void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
 
              vmstate_handle_alloc(first_elem, field, opaque);
              if (field->flags & VMS_POINTER) {
@@ -650,8 +664,9 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
      while (field->name) {
          if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
              void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
              JSONWriter *vmdesc_loop = vmdesc;
              bool is_prev_null = false;
              /*
Signature · 2 lines
Best regards,
Vladimir
#5 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

Quoted 84 lines · Vladimir Sementsov-Ogievskiy
Quoted 83 lines · Fabiano Rosas

When migrating a buffer or array, the vmstate code needs to know the size of the buffer and the number of elements of the array. Today a vmstate writer can choose from a number of macros that take as last input the name of a struct member from where the size/num will be read.

At load time, the code will access those value via an opaque pointer to the migrated data and therefore it needs to also know the size of the struct member at that offset.

Currently that information is communicated by means of the VMS_VARRAY_* and VMS_VBUFFER_* flags, where each possible type is represented by a flag.

So far, that's all fine, but since the vmstate code makes heavy use of macros, handling several types individually (i.e. by name: int, int32_t, etc) requires several versions of a same macro, one for each type. E.g: VMSTATE_VBUFFER_ALLOC_UINT32

                                  ^

This creates a pattern where the vmstate writer has to match the macro name to the data type and has resulted in the code having a tendency of having one macro version for each type, for each type of vmstate.

There is also some cognitive load to deal with, e.g. VMSTATE_VARRAY_INT32 doesn't hold an array of int32, it holds an array of something else and the number of elements for the array is stored in a variable of type int32.

We're now dealing with the scenario where the code has been expecting int32_t at some places, but a uint64_t macro variant has been added without the code being updated.

To address all these situations, introduce a new struct that will hold the offset of the struct members, but also their size, so the various extra macros can all be removed and the person writing the vmstate doesn't need to care about type-checking. Still, keep a minimum check that those fields are at least integers and fit into 64 bits.

What changes:

1) type checking changes from individual types to a single check for

    all integers;

2) there are new ways to access the offsets;

num_offset -> num_offset.off
size_offset -> size_offset.off
[new] num_offset.size
[new] size_offset.size

2) reading the offsets goes from checking the VMS_VARRAY_* flags in an if/elseif block to comparing offset.size against the hardcoded sizes in bytes;

3) the VMS_VARRAY_* and VMS_VBUFFER_* flags become obsolete. Removed

    in the next patch;

4) memory usage increases +1 byte per vmstate;

---
  include/migration/vmstate.h | 66 +++++++++++++++++++++++--------------
  migration/savevm.c          |  4 +--
  migration/vmstate.c         | 51 ++++++++++++++++++----------
  3 files changed, 76 insertions(+), 45 deletions(-)
include/migration/vmstate.h +1 -0
index 92a1a9fe98e..2ad3cc01371 100644
@@ -31,6 +31,7 @@
 
  typedef struct VMStateInfo VMStateInfo;
  typedef struct VMStateField VMStateField;
+typedef struct VMStateOffset VMStateOffset;
 
  /*
   * VMStateInfo allows customized migration of objects that don't fit in
@@ -187,6 +188,11 @@ typedef enum {
      MIG_PRI_MAX,
  } MigrationPriority;
 
Quoted 7 lines

include/migration/vmstate.h typedef enum { · line 194

Quoted 4 lines
include/migration/vmstate.h +4 -0
+struct VMStateOffset {
+    uint32_t off;
+    uint8_t size;
+};

A bit strange to have offset field (off) inside "Offset" structure.

Yes! All the naming is this series is a bit off (hehe), I didn't want to start changing too much and diverting from the core of the series.

Quoted 2 lines

VMStatePosition may be?

I want to try to use that struct for the main offset also and for storing the maximum size of any "len" and "size" fields. Maybe:

VMStateIndirectField // as in, not the main field being migrated
VMStateMetaField // as in, it brings extra data about the main field

If we have Field in there, then we can treat this as the main VMStateField, but with less stuff in it. I think it makes sense because then "offset" and "size" map naturally.

Quoted 6 lines
Quoted 6 lines
include/migration/vmstate.h +1 -0
+
  struct VMStateField {
      const char *name;
      size_t offset;
@@ -205,11 +211,11 @@ struct VMStateField {
       * pointer point to.
Quoted 9 lines

include/migration/vmstate.h struct VMStateField { · line 214

Quoted 4 lines
include/migration/vmstate.h +1 -1
       */
      size_t size;
-    size_t size_offset;
+    VMStateOffset size_offset;

size_offset variable of type Offset, which has offset and size fields inside. And different meaning for each "size" and "offset" in this context.. Partly preexisting, but we make it even more complicated by this patch. Maybe:

Yes, even writing the commit message is hard, we don't have good terms yet. Let's see what others think.

Quoted 4 lines
    VMStatePosition size_pos;

or even

Quoted 8 lines
    VMStatePosition bufsize_pos;

to also distinguish "buffer size" consept of "size of field containing buffer size".

Side questions: is there any difference between VARRAY of bytes and BUFFER? We could probably share same @num and @num_offset fields for buffers, an drop size/size_offset at all.

Hm, maybe, that would be nice indeed. Let me check.

Quoted 12 lines
Quoted 12 lines
include/migration/vmstate.h +1 -1
      size_t start;
      int num;
-    size_t num_offset;
+    VMStateOffset num_offset;
      const VMStateInfo *info;
      enum VMStateFlags flags;
      const VMStateDescription *vmsd;
@@ -328,6 +334,16 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      (type_check(t1, typeof_elt_of_field(t2, f))                      \
       + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
 
Quoted 5 lines

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 338

Quoted 2 lines
include/migration/vmstate.h +2 -0
+#define type_check_int64(t)                                             \
+    (((ptrdiff_t)0 * (ptrdiff_t)(~((t)0))) +                            \

why use ptrdiff_t, if want to compatibility with int64?

Yeah, I think that cast is useless here. I changed this macro a lot because it was having issues with the gcc-11 from the CI. I'll fix it.

Quoted 4 lines

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 339

Quoted 1 line
include/migration/vmstate.h +1 -0
+     (0 * sizeof(char[(sizeof(t) <= sizeof(uint64_t)) ? 1 : -1])))

could QEMU_BUILD_BUG_ON() be used to make more readable check?

Good idea. Some trickery is still needed to satisfy the compiler, but it seems more readable and the error message now shows the type name:

#define type_check_int64(t)                                     \
    (~((t)0) * sizeof(struct {                                  \
            QEMU_BUILD_BUG_ON(sizeof(t) > sizeof(uint64_t));    \
    }))
Quoted 334 lines
Quoted 334 lines
include/migration/vmstate.h +30 -23
+
+#define vmstate_field_offset(_state, _field) {                  \
+    .off = (offsetof(_state, _field) +                          \
+            type_check_int64(typeof_field(_state, _field))),    \
+    .size = sizeof(typeof_field(_state, _field)),               \
+}
+
  #define vmstate_offset_value(_state, _field, _type)                  \
      (offsetof(_state, _field) +                                      \
       type_check(_type, typeof_field(_state, _field)))
@@ -454,7 +470,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
@@ -464,7 +480,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
@@ -474,7 +490,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
@@ -484,7 +500,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
@@ -494,7 +510,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
@@ -504,7 +520,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT16,                                 \
@@ -583,7 +599,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      _field, _state, _field_num, _version, _vmsd, _type) {            \
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
@@ -596,7 +612,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      _field, _state, _field_num, _version, _vmsd, _type) {             \
      .name       = (stringify(_field)),                                \
      .version_id = (_version),                                         \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),           \
      .vmsd       = &(_vmsd),                                           \
      .size       = sizeof(_type),                                      \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
@@ -608,7 +624,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
      .name       = (stringify(_field)),                                    \
      .version_id = (_version),                                             \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
+    .num_offset = vmstate_field_offset(_state, _field_num),               \
      .info       = &(_info),                                               \
      .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
      .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
@@ -650,7 +666,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -674,7 +690,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -684,7 +700,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -694,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
@@ -703,7 +719,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -713,7 +729,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -725,7 +741,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
      .offset     = vmstate_offset_pointer(_state, _field, _type),     \
@@ -746,7 +762,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .size         = (_multiply),                                      \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
@@ -757,7 +773,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
@@ -767,7 +783,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
@@ -777,7 +793,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint64_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER | VMS_POINTER,                       \
      .offset       = offsetof(_state, _field),                        \
@@ -788,7 +804,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
      .offset       = offsetof(_state, _field),                        \
@@ -848,7 +864,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
      .name         = "unused",                                        \
      .field_exists = (_test),                                         \
-    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset   = vmstate_field_offset(_state, _field_num),        \
      .version_id   = (_version),                                      \
      .size         = (_size),                                         \
      .info         = &vmstate_info_unused_buffer,                     \
@@ -862,7 +878,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .field_exists = (_test),                                         \
      .version_id   = (_version),                                      \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_bitmap,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
migration/savevm.c +2 -2
index 34dd06f9f73..a272bcfd0b4 100644
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                       * Size must be provided because dest QEMU needs that
                       * info to know what to allocate
                       */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);
                  } else {
                      /*
                       * Otherwise size info isn't useful (because it's
                       * always the size of host pointer), detect accidental
                       * setup of sizes in this case.
                       */
-                    assert(field->size == 0 && field->size_offset == 0);
+                    assert(field->size == 0 && field->size_offset.size == 0);
                  }
                  /*
                   * VMS_ARRAY_OF_POINTER must be used only together with one
migration/vmstate.c +33 -18
index 50ebe378452..0a0b9faa20e 100644
@@ -78,32 +78,45 @@ vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
      };
  }
 
-static int vmstate_n_elems(void *opaque, const VMStateField *field)
+static uint64_t vmstate_read_from_offset(void *opaque,
+                                         const VMStateOffset *offset)
  {
-    int n_elems = 1;
+    uint8_t *ptr = (uint8_t *)opaque + offset->off;
+
+    switch (offset->size) {
+    case 1:
+        return *(uint8_t *)ptr;
+    case 2:
+        return *(uint16_t *)ptr;
+    case 4:
+        return *(uint32_t *)ptr;
+    case 8:
+        return *(uint64_t *)ptr;
+    }
+    g_assert_not_reached();
+}
+
+static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
+{
+    uint64_t n_elems = 1;
 
      if (field->flags & VMS_ARRAY) {
          n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY_INT32) {
-        n_elems = *(int32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT32) {
-        n_elems = *(uint32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT16) {
-        n_elems = *(uint16_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT8) {
-        n_elems = *(uint8_t *)(opaque + field->num_offset);
+    } else if (field->flags & (VMS_VARRAY_INT32 | VMS_VARRAY_UINT32
+                               | VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8)) {
+        n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
      }
 
      trace_vmstate_n_elems(field->name, n_elems);
      return n_elems;
  }
 
-static int vmstate_size(void *opaque, const VMStateField *field)
+static uint64_t vmstate_size(void *opaque, const VMStateField *field)
  {
-    int size;
+    uint64_t size;
 
      if (field->flags & VMS_VBUFFER) {
-        size = *(int32_t *)(opaque + field->size_offset);
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
          if (field->flags & VMS_MULTIPLY) {
              size *= field->size;
          }
@@ -124,7 +137,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
                                   void *opaque)
  {
      if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
-        gsize size = vmstate_size(opaque, field);
+        uint64_t size = vmstate_size(opaque, field);
          size *= vmstate_n_elems(opaque, field);
          if (size) {
              *(void **)ptr = g_malloc(size);
@@ -335,8 +348,9 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
 
          if (exists) {
              void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
 
              vmstate_handle_alloc(first_elem, field, opaque);
              if (field->flags & VMS_POINTER) {
@@ -650,8 +664,9 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
      while (field->name) {
          if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
              void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
              JSONWriter *vmdesc_loop = vmdesc;
              bool is_prev_null = false;
              /*
#6 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

In reply to Vladimir Sementsov-Ogievskiy
Quoted 112 lines · Vladimir Sementsov-Ogievskiy
Quoted 80 lines · Fabiano Rosas

When migrating a buffer or array, the vmstate code needs to know the size of the buffer and the number of elements of the array. Today a vmstate writer can choose from a number of macros that take as last input the name of a struct member from where the size/num will be read.

At load time, the code will access those value via an opaque pointer to the migrated data and therefore it needs to also know the size of the struct member at that offset.

Currently that information is communicated by means of the VMS_VARRAY_* and VMS_VBUFFER_* flags, where each possible type is represented by a flag.

So far, that's all fine, but since the vmstate code makes heavy use of macros, handling several types individually (i.e. by name: int, int32_t, etc) requires several versions of a same macro, one for each type. E.g: VMSTATE_VBUFFER_ALLOC_UINT32

                                  ^

This creates a pattern where the vmstate writer has to match the macro name to the data type and has resulted in the code having a tendency of having one macro version for each type, for each type of vmstate.

There is also some cognitive load to deal with, e.g. VMSTATE_VARRAY_INT32 doesn't hold an array of int32, it holds an array of something else and the number of elements for the array is stored in a variable of type int32.

We're now dealing with the scenario where the code has been expecting int32_t at some places, but a uint64_t macro variant has been added without the code being updated.

To address all these situations, introduce a new struct that will hold the offset of the struct members, but also their size, so the various extra macros can all be removed and the person writing the vmstate doesn't need to care about type-checking. Still, keep a minimum check that those fields are at least integers and fit into 64 bits.

What changes:

1) type checking changes from individual types to a single check for

    all integers;

2) there are new ways to access the offsets;

num_offset -> num_offset.off
size_offset -> size_offset.off
[new] num_offset.size
[new] size_offset.size

2) reading the offsets goes from checking the VMS_VARRAY_* flags in an if/elseif block to comparing offset.size against the hardcoded sizes in bytes;

3) the VMS_VARRAY_* and VMS_VBUFFER_* flags become obsolete. Removed

    in the next patch;

4) memory usage increases +1 byte per vmstate;

---
  include/migration/vmstate.h | 66 +++++++++++++++++++++++--------------
  migration/savevm.c          |  4 +--
  migration/vmstate.c         | 51 ++++++++++++++++++----------
  3 files changed, 76 insertions(+), 45 deletions(-)
include/migration/vmstate.h +1 -0
index 92a1a9fe98e..2ad3cc01371 100644
@@ -31,6 +31,7 @@
 
  typedef struct VMStateInfo VMStateInfo;
  typedef struct VMStateField VMStateField;
+typedef struct VMStateOffset VMStateOffset;
 
  /*
   * VMStateInfo allows customized migration of objects that don't fit in

include/migration/vmstate.h typedef enum { · line 194

Quoted 7 lines
include/migration/vmstate.h +4 -0
@@ -187,6 +188,11 @@ typedef enum {
      MIG_PRI_MAX,
  } MigrationPriority;
 
+struct VMStateOffset {
+    uint32_t off;
+    uint8_t size;
+};

A bit strange to have offset field (off) inside "Offset" structure.

VMStatePosition may be?

include/migration/vmstate.h struct VMStateField { · line 214

Quoted 10 lines
include/migration/vmstate.h +2 -1
+
  struct VMStateField {
      const char *name;
      size_t offset;
@@ -205,11 +211,11 @@ struct VMStateField {
       * pointer point to.
       */
      size_t size;
-    size_t size_offset;
+    VMStateOffset size_offset;

size_offset variable of type Offset, which has offset and size fields inside. And different meaning for each "size" and "offset" in this context.. Partly preexisting, but we make it even more complicated by this patch. Maybe:

    VMStatePosition size_pos;

or even

Quoted 8 lines
    VMStatePosition bufsize_pos;

to also distinguish "buffer size" consept of "size of field containing buffer size".

Side questions: is there any difference between VARRAY of bytes and BUFFER? We could probably share same @num and @num_offset fields for buffers, an drop size/size_offset at all.

Hi Vladimir, I've been looking at this, could you clarify which vmstates do you think we could merge? I don't see it, either VARRAY vs. VBUFFER or VARRAY vs. BUFFER, also ARRAY vs. BUFFER doesn't seem to work.

One main point of difference is the size_offset/num_offset variants are only known at load-time, so we can't convert them between each other at build time because the either the total size or num will not be know.

Quoted 355 lines
Quoted 9 lines
include/migration/vmstate.h +1 -1
      size_t start;
      int num;
-    size_t num_offset;
+    VMStateOffset num_offset;
      const VMStateInfo *info;
      enum VMStateFlags flags;
      const VMStateDescription *vmsd;

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 338

Quoted 5 lines
include/migration/vmstate.h +2 -0
@@ -328,6 +334,16 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      (type_check(t1, typeof_elt_of_field(t2, f))                      \
       + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
 
+#define type_check_int64(t)                                             \
+    (((ptrdiff_t)0 * (ptrdiff_t)(~((t)0))) +                            \

why use ptrdiff_t, if want to compatibility with int64?

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 339

Quoted 1 line
include/migration/vmstate.h +1 -0
+     (0 * sizeof(char[(sizeof(t) <= sizeof(uint64_t)) ? 1 : -1])))

could QEMU_BUILD_BUG_ON() be used to make more readable check?

Quoted 334 lines
include/migration/vmstate.h +30 -23
+
+#define vmstate_field_offset(_state, _field) {                  \
+    .off = (offsetof(_state, _field) +                          \
+            type_check_int64(typeof_field(_state, _field))),    \
+    .size = sizeof(typeof_field(_state, _field)),               \
+}
+
  #define vmstate_offset_value(_state, _field, _type)                  \
      (offsetof(_state, _field) +                                      \
       type_check(_type, typeof_field(_state, _field)))
@@ -454,7 +470,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
@@ -464,7 +480,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
@@ -474,7 +490,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
@@ -484,7 +500,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
@@ -494,7 +510,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
@@ -504,7 +520,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .info       = &(_info),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_VARRAY_UINT16,                                 \
@@ -583,7 +599,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      _field, _state, _field_num, _version, _vmsd, _type) {            \
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
@@ -596,7 +612,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      _field, _state, _field_num, _version, _vmsd, _type) {             \
      .name       = (stringify(_field)),                                \
      .version_id = (_version),                                         \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),           \
      .vmsd       = &(_vmsd),                                           \
      .size       = sizeof(_type),                                      \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
@@ -608,7 +624,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
      .name       = (stringify(_field)),                                    \
      .version_id = (_version),                                             \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
+    .num_offset = vmstate_field_offset(_state, _field_num),               \
      .info       = &(_info),                                               \
      .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
      .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
@@ -650,7 +666,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -674,7 +690,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -684,7 +700,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -694,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
      .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .vmsd       = &(_vmsd),                                          \
      .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
@@ -703,7 +719,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -713,7 +729,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
  #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
      .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
      .size       = sizeof(_type),                                     \
@@ -725,7 +741,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name       = (stringify(_field)),                               \
      .version_id = (_version),                                        \
      .vmsd       = &(_vmsd),                                          \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
      .size       = sizeof(_type),                                     \
      .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
      .offset     = vmstate_offset_pointer(_state, _field, _type),     \
@@ -746,7 +762,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .size         = (_multiply),                                      \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
@@ -757,7 +773,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
@@ -767,7 +783,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
@@ -777,7 +793,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint64_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER | VMS_POINTER,                       \
      .offset       = offsetof(_state, _field),                        \
@@ -788,7 +804,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .version_id   = (_version),                                      \
      .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_buffer,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
      .offset       = offsetof(_state, _field),                        \
@@ -848,7 +864,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
      .name         = "unused",                                        \
      .field_exists = (_test),                                         \
-    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset   = vmstate_field_offset(_state, _field_num),        \
      .version_id   = (_version),                                      \
      .size         = (_size),                                         \
      .info         = &vmstate_info_unused_buffer,                     \
@@ -862,7 +878,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
      .name         = (stringify(_field)),                             \
      .field_exists = (_test),                                         \
      .version_id   = (_version),                                      \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
      .info         = &vmstate_info_bitmap,                            \
      .flags        = VMS_VBUFFER|VMS_POINTER,                         \
      .offset       = offsetof(_state, _field),                        \
migration/savevm.c +2 -2
index 34dd06f9f73..a272bcfd0b4 100644
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                       * Size must be provided because dest QEMU needs that
                       * info to know what to allocate
                       */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);
                  } else {
                      /*
                       * Otherwise size info isn't useful (because it's
                       * always the size of host pointer), detect accidental
                       * setup of sizes in this case.
                       */
-                    assert(field->size == 0 && field->size_offset == 0);
+                    assert(field->size == 0 && field->size_offset.size == 0);
                  }
                  /*
                   * VMS_ARRAY_OF_POINTER must be used only together with one
migration/vmstate.c +33 -18
index 50ebe378452..0a0b9faa20e 100644
@@ -78,32 +78,45 @@ vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
      };
  }
 
-static int vmstate_n_elems(void *opaque, const VMStateField *field)
+static uint64_t vmstate_read_from_offset(void *opaque,
+                                         const VMStateOffset *offset)
  {
-    int n_elems = 1;
+    uint8_t *ptr = (uint8_t *)opaque + offset->off;
+
+    switch (offset->size) {
+    case 1:
+        return *(uint8_t *)ptr;
+    case 2:
+        return *(uint16_t *)ptr;
+    case 4:
+        return *(uint32_t *)ptr;
+    case 8:
+        return *(uint64_t *)ptr;
+    }
+    g_assert_not_reached();
+}
+
+static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
+{
+    uint64_t n_elems = 1;
 
      if (field->flags & VMS_ARRAY) {
          n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY_INT32) {
-        n_elems = *(int32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT32) {
-        n_elems = *(uint32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT16) {
-        n_elems = *(uint16_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT8) {
-        n_elems = *(uint8_t *)(opaque + field->num_offset);
+    } else if (field->flags & (VMS_VARRAY_INT32 | VMS_VARRAY_UINT32
+                               | VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8)) {
+        n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
      }
 
      trace_vmstate_n_elems(field->name, n_elems);
      return n_elems;
  }
 
-static int vmstate_size(void *opaque, const VMStateField *field)
+static uint64_t vmstate_size(void *opaque, const VMStateField *field)
  {
-    int size;
+    uint64_t size;
 
      if (field->flags & VMS_VBUFFER) {
-        size = *(int32_t *)(opaque + field->size_offset);
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
          if (field->flags & VMS_MULTIPLY) {
              size *= field->size;
          }
@@ -124,7 +137,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
                                   void *opaque)
  {
      if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
-        gsize size = vmstate_size(opaque, field);
+        uint64_t size = vmstate_size(opaque, field);
          size *= vmstate_n_elems(opaque, field);
          if (size) {
              *(void **)ptr = g_malloc(size);
@@ -335,8 +348,9 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
 
          if (exists) {
              void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
 
              vmstate_handle_alloc(first_elem, field, opaque);
              if (field->flags & VMS_POINTER) {
@@ -650,8 +664,9 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
      while (field->name) {
          if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
              void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
              JSONWriter *vmdesc_loop = vmdesc;
              bool is_prev_null = false;
              /*
#7 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

Quoted 7 lines · Fabiano Rosas

Hi Vladimir, I've been looking at this, could you clarify which vmstates do you think we could merge? I don't see it, either VARRAY vs. VBUFFER or VARRAY vs. BUFFER, also ARRAY vs. BUFFER doesn't seem to work.

One main point of difference is the size_offset/num_offset variants are only known at load-time, so we can't convert them between each other at build time because the either the total size or num will not be know.

Something like this?

NOTE: I hid a fix to a comment that is irrelevant.. which I mentioned @size and @size_offset can't co-exist, but it can when VMS_MULTIPLY is set. I had a vague feeling we can further clean vmstate flags.

include/migration/vmstate.h +12 -25
index 24631fd678..3a02709650 100644
@@ -69,17 +69,15 @@ enum VMStateFlags {
      * }). Dereference the pointer before using it as basis for
      * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
      * affect the meaning of VMStateField.num_offset or
-     * VMStateField.size_offset; see VMS_VARRAY and VMS_VBUFFER for
-     * those.
+     * VMStateField.size_offset; see VMS_VARRAY for those.
      */
     VMS_POINTER          = 0x002,
 
     /* The field is an array of fixed size. VMStateField.num contains
      * the number of entries in the array. The size of each entry is
      * given by VMStateField.size and / or opaque +
-     * VMStateField.size_offset; see VMS_VBUFFER and
-     * VMS_MULTIPLY. Each array entry will be processed individually
-     * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
+     * VMStateField.size_offset. Each array entry will be processed
+     * individually (VMStateField.info.get()/put() if VMS_STRUCT is not set,
      * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
      * be combined with VMS_VARRAY.
      */
@@ -108,18 +106,10 @@ enum VMStateFlags {
      */
     VMS_ARRAY_OF_POINTER = 0x040,
 
-    /* The size of the individual entries (a single array entry if
-     * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
-     * neither is set) is variable (i.e. not known at compile-time),
-     * but the same for all entries. Use the int32_t at opaque +
-     * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
-     * the size of each (and every) entry. */
-    VMS_VBUFFER          = 0x100,
-
     /* Multiply the entry size given by the int32_t at opaque +
-     * VMStateField.size_offset (see VMS_VBUFFER description) with
-     * VMStateField.size to determine the number of bytes to be
-     * allocated. Only valid in combination with VMS_VBUFFER. */
+     * VMStateField.size_offset with VMStateField.size to determine the
+     * number of bytes to be allocated.
+     */
     VMS_MULTIPLY         = 0x200,
 
     /* Fail loading the serialised VM state if this field is missing
@@ -181,8 +171,8 @@ struct VMStateField {
 
     /*
      * @size or @size_offset specifies the size of the element embeded in
-     * the field.  Only one of them should be present never both.  When
-     * @size_offset is used together with VMS_VBUFFER, it means the size is
+     * the field.  Only one of them should be present never both, except
+     * VMS_MULTIPLY.  When @size_offset is used, it means the size is
      * dynamic calculated instead of a constant.
      *
      * When the field is an array of any type, this stores the size of one
@@ -696,7 +686,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_MULTIPLY,              \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -706,7 +696,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -720,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_ALLOC,                \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -798,7 +788,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id   = (_version),                                      \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -1200,9 +1190,6 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
 
-#define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
-    VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
-
 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
 
migration/vmstate.c +7 -7
index bc8eb3d3ca..f7363a8776 100644
@@ -102,7 +102,7 @@ static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
 
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY) {
+    } else if (field->num_offset.size) {
         n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
     }
 
@@ -114,17 +114,17 @@ static uint64_t vmstate_size(void *opaque, const VMStateField *field)
 {
     uint64_t size;
 
-    if (field->flags & VMS_VBUFFER) {
-        size = vmstate_read_from_offset(opaque, &field->size_offset);
-        if (field->flags & VMS_MULTIPLY) {
-            size *= field->size;
-        }
-    } else if (field->flags & VMS_ARRAY_OF_POINTER) {
+    if (field->flags & VMS_ARRAY_OF_POINTER) {
         /*
          * For an array of pointer, the each element is always size of a
          * host pointer.
          */
         size = sizeof(void *);
+    } else if (field->size_offset.size) {
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
+        if (field->flags & VMS_MULTIPLY) {
+            size *= field->size;
+        }
     } else {
         size = field->size;
     }
Signature · 4 lines
2.54.0

-- 
Peter Xu
#8 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

Quoted 9 lines · Peter Xu
Quoted 7 lines · Fabiano Rosas

Hi Vladimir, I've been looking at this, could you clarify which vmstates do you think we could merge? I don't see it, either VARRAY vs. VBUFFER or VARRAY vs. BUFFER, also ARRAY vs. BUFFER doesn't seem to work.

One main point of difference is the size_offset/num_offset variants are only known at load-time, so we can't convert them between each other at build time because the either the total size or num will not be know.

Quoted 5 lines

Something like this?

NOTE: I hid a fix to a comment that is irrelevant.. which I mentioned @size and @size_offset can't co-exist, but it can when VMS_MULTIPLY is set. I had a vague feeling we can further clean vmstate flags.

Is there a reason we cannot use .num for MULTIPLY instead of .size?

Quoted 110 lines
include/migration/vmstate.h +12 -25
index 24631fd678..3a02709650 100644
@@ -69,17 +69,15 @@ enum VMStateFlags {
      * }). Dereference the pointer before using it as basis for
      * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
      * affect the meaning of VMStateField.num_offset or
-     * VMStateField.size_offset; see VMS_VARRAY and VMS_VBUFFER for
-     * those.
+     * VMStateField.size_offset; see VMS_VARRAY for those.
      */
     VMS_POINTER          = 0x002,
 
     /* The field is an array of fixed size. VMStateField.num contains
      * the number of entries in the array. The size of each entry is
      * given by VMStateField.size and / or opaque +
-     * VMStateField.size_offset; see VMS_VBUFFER and
-     * VMS_MULTIPLY. Each array entry will be processed individually
-     * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
+     * VMStateField.size_offset. Each array entry will be processed
+     * individually (VMStateField.info.get()/put() if VMS_STRUCT is not set,
      * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
      * be combined with VMS_VARRAY.
      */
@@ -108,18 +106,10 @@ enum VMStateFlags {
      */
     VMS_ARRAY_OF_POINTER = 0x040,
 
-    /* The size of the individual entries (a single array entry if
-     * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
-     * neither is set) is variable (i.e. not known at compile-time),
-     * but the same for all entries. Use the int32_t at opaque +
-     * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
-     * the size of each (and every) entry. */
-    VMS_VBUFFER          = 0x100,
-
     /* Multiply the entry size given by the int32_t at opaque +
-     * VMStateField.size_offset (see VMS_VBUFFER description) with
-     * VMStateField.size to determine the number of bytes to be
-     * allocated. Only valid in combination with VMS_VBUFFER. */
+     * VMStateField.size_offset with VMStateField.size to determine the
+     * number of bytes to be allocated.
+     */
     VMS_MULTIPLY         = 0x200,
 
     /* Fail loading the serialised VM state if this field is missing
@@ -181,8 +171,8 @@ struct VMStateField {
 
     /*
      * @size or @size_offset specifies the size of the element embeded in
-     * the field.  Only one of them should be present never both.  When
-     * @size_offset is used together with VMS_VBUFFER, it means the size is
+     * the field.  Only one of them should be present never both, except
+     * VMS_MULTIPLY.  When @size_offset is used, it means the size is
      * dynamic calculated instead of a constant.
      *
      * When the field is an array of any type, this stores the size of one
@@ -696,7 +686,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_MULTIPLY,              \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -706,7 +696,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -720,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_ALLOC,                \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -798,7 +788,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id   = (_version),                                      \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -1200,9 +1190,6 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
 
-#define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
-    VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
-
 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
 
migration/vmstate.c +0 -0
index bc8eb3d3ca..f7363a8776 100644

migration/vmstate.c static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field) · line 105

Quoted 5 lines
migration/vmstate.c +1 -1
@@ -102,7 +102,7 @@ static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
 
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY) {
+    } else if (field->num_offset.size) {

Hmm, is it better to infer from the data rather than having the explicit flag? And if so, can't we remove VMS_VARRAY flag altogether?

From Vladimir's message I though the intent was not only to remove the flag, but to remove the definition of VMSTATE_VBUFFER entirely and somehow fit that case into VMSTATE_VARRAY, removing size_offset along with it.

Quoted 16 lines
migration/vmstate.c +1 -6
         n_elems = vmstate_read_from_offset(opaque,
     &field->num_offset); }
 
@@ -114,17 +114,17 @@ static uint64_t vmstate_size(void *opaque, const VMStateField *field)
 {
     uint64_t size;
 
-    if (field->flags & VMS_VBUFFER) {
-        size = vmstate_read_from_offset(opaque, &field->size_offset);
-        if (field->flags & VMS_MULTIPLY) {
-            size *= field->size;
-        }
-    } else if (field->flags & VMS_ARRAY_OF_POINTER) {
+    if (field->flags & VMS_ARRAY_OF_POINTER) {
         /*
          * For an array of pointer, the each element is always size of a

migration/vmstate.c static uint64_t vmstate_size(void *opaque, const VMStateField *field) · line 129

Quoted 10 lines
migration/vmstate.c +5 -0
          * host pointer.
          */
         size = sizeof(void *);
+    } else if (field->size_offset.size) {
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
+        if (field->flags & VMS_MULTIPLY) {
+            size *= field->size;
+        }
     } else {
         size = field->size;

Since we're talking about flags, I'd like to be able to assert(size) at this point, but there is some weirness with vmstate_msix and vmstate_scsi_device that have no state at all. I'm thinking of introducing a new flag to identify those cases:

-- >8 --
From: Fabiano Rosas <[email protected]>
Subject: [PATCH] migration: Add VMS_NO_STATE flag

There are a few special cases of vmstate usage:

The vmstate_msix and vmstate_scsi_device have fields that contain no data, only a vmstate_info structure.

The VMSTATE_VALIDATE macro serves only to invoke the .field_exists routine for validation.

Regardless whether these scenarios are valid, add a separate flag to identify them so we can enforce common constraints for the normal vmstates such as having a size greater than zero.

---
 hw/pci/msix.c               | 6 +-----
 hw/scsi/scsi-bus.c          | 6 +-----
 include/migration/vmstate.h | 9 +++++++--
 migration/savevm.c          | 3 +--
 4 files changed, 10 insertions(+), 14 deletions(-)
hw/pci/msix.c +1 -5
index 1b23eaf1007..adf76b5bccc 100644
@@ -711,12 +711,8 @@ const VMStateDescription vmstate_msix = {
     .fields = (const VMStateField[]) {
         {
             .name         = "msix",
-            .version_id   = 0,
-            .field_exists = NULL,
-            .size         = 0,   /* ouch */
             .info         = &vmstate_info_msix,
-            .flags        = VMS_SINGLE,
-            .offset       = 0,
+            .flags        = VMS_SINGLE | VMS_NO_STATE,
         },
         VMSTATE_END_OF_LIST()
     }
hw/scsi/scsi-bus.c +1 -5
index deb43d5560e..aa02ff631b7 100644
@@ -1980,12 +1980,8 @@ const VMStateDescription vmstate_scsi_device = {
         VMSTATE_UINT32(sense_len, SCSIDevice),
         {
             .name         = "requests",
-            .version_id   = 0,
-            .field_exists = NULL,
-            .size         = 0,   /* ouch */
             .info         = &vmstate_info_scsi_requests,
-            .flags        = VMS_SINGLE,
-            .offset       = 0,
+            .flags        = VMS_SINGLE | VMS_NO_STATE,
         },
         VMSTATE_END_OF_LIST()
     },
include/migration/vmstate.h +7 -2
index 09b2e535645..ada1625f5b6 100644
@@ -108,6 +108,12 @@ enum VMStateFlags {
      */
     VMS_ARRAY_OF_POINTER = 0x040,
 
+    /*
+     * The field contains no data. Used for special cases such as
+     * invoking a custom VMStateInfo.
+     */
+    VMS_NO_STATE = 0x080,
+
     /* The size of the individual entries (a single array entry if
      * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
      * neither is set) is variable (i.e. not known at compile-time),
@@ -451,8 +457,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VALIDATE(_name, _test) { \
     .name         = (_name),                                         \
     .field_exists = (_test),                                         \
-    .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
-    .num          = 0, /* 0 elements: no data, only run _test */     \
+    .flags        = VMS_ARRAY | VMS_MUST_EXIST | VMS_NO_STATE,       \
 }
 
 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
migration/savevm.c +1 -2
index 3e5cce6520d..160c0f2d97d 100644
@@ -617,8 +617,7 @@ static void dump_vmstate_vmsd(FILE *out_file,
         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
         first = true;
         while (field->name != NULL) {
-            if (field->flags & VMS_MUST_EXIST) {
-                /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
+            if (field->flags & VMS_NO_STATE) {
                 field++;
                 continue;
             }
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

Quoted 164 lines · Fabiano Rosas
Quoted 9 lines · Peter Xu
Quoted 7 lines · Fabiano Rosas

Hi Vladimir, I've been looking at this, could you clarify which vmstates do you think we could merge? I don't see it, either VARRAY vs. VBUFFER or VARRAY vs. BUFFER, also ARRAY vs. BUFFER doesn't seem to work.

One main point of difference is the size_offset/num_offset variants are only known at load-time, so we can't convert them between each other at build time because the either the total size or num will not be know.

Quoted 5 lines

Something like this?

NOTE: I hid a fix to a comment that is irrelevant.. which I mentioned @size and @size_offset can't co-exist, but it can when VMS_MULTIPLY is set. I had a vague feeling we can further clean vmstate flags.

Is there a reason we cannot use .num for MULTIPLY instead of .size?

Quoted 110 lines
include/migration/vmstate.h +12 -25
index 24631fd678..3a02709650 100644
@@ -69,17 +69,15 @@ enum VMStateFlags {
      * }). Dereference the pointer before using it as basis for
      * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
      * affect the meaning of VMStateField.num_offset or
-     * VMStateField.size_offset; see VMS_VARRAY and VMS_VBUFFER for
-     * those.
+     * VMStateField.size_offset; see VMS_VARRAY for those.
      */
     VMS_POINTER          = 0x002,
 
     /* The field is an array of fixed size. VMStateField.num contains
      * the number of entries in the array. The size of each entry is
      * given by VMStateField.size and / or opaque +
-     * VMStateField.size_offset; see VMS_VBUFFER and
-     * VMS_MULTIPLY. Each array entry will be processed individually
-     * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
+     * VMStateField.size_offset. Each array entry will be processed
+     * individually (VMStateField.info.get()/put() if VMS_STRUCT is not set,
      * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
      * be combined with VMS_VARRAY.
      */
@@ -108,18 +106,10 @@ enum VMStateFlags {
      */
     VMS_ARRAY_OF_POINTER = 0x040,
 
-    /* The size of the individual entries (a single array entry if
-     * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
-     * neither is set) is variable (i.e. not known at compile-time),
-     * but the same for all entries. Use the int32_t at opaque +
-     * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
-     * the size of each (and every) entry. */
-    VMS_VBUFFER          = 0x100,
-
     /* Multiply the entry size given by the int32_t at opaque +
-     * VMStateField.size_offset (see VMS_VBUFFER description) with
-     * VMStateField.size to determine the number of bytes to be
-     * allocated. Only valid in combination with VMS_VBUFFER. */
+     * VMStateField.size_offset with VMStateField.size to determine the
+     * number of bytes to be allocated.
+     */
     VMS_MULTIPLY         = 0x200,
 
     /* Fail loading the serialised VM state if this field is missing
@@ -181,8 +171,8 @@ struct VMStateField {
 
     /*
      * @size or @size_offset specifies the size of the element embeded in
-     * the field.  Only one of them should be present never both.  When
-     * @size_offset is used together with VMS_VBUFFER, it means the size is
+     * the field.  Only one of them should be present never both, except
+     * VMS_MULTIPLY.  When @size_offset is used, it means the size is
      * dynamic calculated instead of a constant.
      *
      * When the field is an array of any type, this stores the size of one
@@ -696,7 +686,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_MULTIPLY,              \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -706,7 +696,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -720,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_ALLOC,                \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -798,7 +788,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id   = (_version),                                      \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -1200,9 +1190,6 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
 
-#define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
-    VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
-
 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
 
migration/vmstate.c +0 -0
index bc8eb3d3ca..f7363a8776 100644

migration/vmstate.c static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field) · line 105

Quoted 5 lines
migration/vmstate.c +1 -1
@@ -102,7 +102,7 @@ static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
 
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY) {
+    } else if (field->num_offset.size) {

Hmm, is it better to infer from the data rather than having the explicit flag? And if so, can't we remove VMS_VARRAY flag altogether?

From Vladimir's message I though the intent was not only to remove the flag, but to remove the definition of VMSTATE_VBUFFER entirely and somehow fit that case into VMSTATE_VARRAY, removing size_offset along with it.

Quoted 21 lines
migration/vmstate.c +3 -6
         n_elems = vmstate_read_from_offset(opaque,
     &field->num_offset); }
 
@@ -114,17 +114,17 @@ static uint64_t vmstate_size(void *opaque, const VMStateField *field)
 {
     uint64_t size;
 
-    if (field->flags & VMS_VBUFFER) {
-        size = vmstate_read_from_offset(opaque, &field->size_offset);
-        if (field->flags & VMS_MULTIPLY) {
-            size *= field->size;
-        }
-    } else if (field->flags & VMS_ARRAY_OF_POINTER) {
+    if (field->flags & VMS_ARRAY_OF_POINTER) {
         /*
          * For an array of pointer, the each element is always size of a
          * host pointer.
          */
         size = sizeof(void *);
+    } else if (field->size_offset.size) {
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
Quoted 8 lines

migration/vmstate.c static uint64_t vmstate_size(void *opaque, const VMStateField *field) · line 129

Quoted 5 lines
migration/vmstate.c +3 -0
+        if (field->flags & VMS_MULTIPLY) {
+            size *= field->size;
+        }
     } else {
         size = field->size;

Since we're talking about flags, I'd like to be able to assert(size) at this point

But size can legitimately be 0 I guess. Nevermind.

#10 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

In reply to Fabiano Rosas
Quoted 11 lines · Fabiano Rosas
Quoted 9 lines · Peter Xu
Quoted 7 lines · Fabiano Rosas

Hi Vladimir, I've been looking at this, could you clarify which vmstates do you think we could merge? I don't see it, either VARRAY vs. VBUFFER or VARRAY vs. BUFFER, also ARRAY vs. BUFFER doesn't seem to work.

One main point of difference is the size_offset/num_offset variants are only known at load-time, so we can't convert them between each other at build time because the either the total size or num will not be know.

Quoted 7 lines
Quoted 5 lines

Something like this?

NOTE: I hid a fix to a comment that is irrelevant.. which I mentioned @size and @size_offset can't co-exist, but it can when VMS_MULTIPLY is set. I had a vague feeling we can further clean vmstate flags.

Is there a reason we cannot use .num for MULTIPLY instead of .size?

.num decides not only the size for allocation when needed, but also how many times we'll invoke the vmstate_save|load_field(), so maybe not directly replaceable.

Quoted 112 lines
Quoted 112 lines
include/migration/vmstate.h +12 -25
index 24631fd678..3a02709650 100644
@@ -69,17 +69,15 @@ enum VMStateFlags {
      * }). Dereference the pointer before using it as basis for
      * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
      * affect the meaning of VMStateField.num_offset or
-     * VMStateField.size_offset; see VMS_VARRAY and VMS_VBUFFER for
-     * those.
+     * VMStateField.size_offset; see VMS_VARRAY for those.
      */
     VMS_POINTER          = 0x002,
 
     /* The field is an array of fixed size. VMStateField.num contains
      * the number of entries in the array. The size of each entry is
      * given by VMStateField.size and / or opaque +
-     * VMStateField.size_offset; see VMS_VBUFFER and
-     * VMS_MULTIPLY. Each array entry will be processed individually
-     * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
+     * VMStateField.size_offset. Each array entry will be processed
+     * individually (VMStateField.info.get()/put() if VMS_STRUCT is not set,
      * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
      * be combined with VMS_VARRAY.
      */
@@ -108,18 +106,10 @@ enum VMStateFlags {
      */
     VMS_ARRAY_OF_POINTER = 0x040,
 
-    /* The size of the individual entries (a single array entry if
-     * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
-     * neither is set) is variable (i.e. not known at compile-time),
-     * but the same for all entries. Use the int32_t at opaque +
-     * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
-     * the size of each (and every) entry. */
-    VMS_VBUFFER          = 0x100,
-
     /* Multiply the entry size given by the int32_t at opaque +
-     * VMStateField.size_offset (see VMS_VBUFFER description) with
-     * VMStateField.size to determine the number of bytes to be
-     * allocated. Only valid in combination with VMS_VBUFFER. */
+     * VMStateField.size_offset with VMStateField.size to determine the
+     * number of bytes to be allocated.
+     */
     VMS_MULTIPLY         = 0x200,
 
     /* Fail loading the serialised VM state if this field is missing
@@ -181,8 +171,8 @@ struct VMStateField {
 
     /*
      * @size or @size_offset specifies the size of the element embeded in
-     * the field.  Only one of them should be present never both.  When
-     * @size_offset is used together with VMS_VBUFFER, it means the size is
+     * the field.  Only one of them should be present never both, except
+     * VMS_MULTIPLY.  When @size_offset is used, it means the size is
      * dynamic calculated instead of a constant.
      *
      * When the field is an array of any type, this stores the size of one
@@ -696,7 +686,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_MULTIPLY,              \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -706,7 +696,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -720,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .field_exists = (_test),                                         \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
+    .flags        = VMS_VARRAY|VMS_POINTER|VMS_ALLOC,                \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -798,7 +788,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id   = (_version),                                      \
     .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
+    .flags        = VMS_VARRAY|VMS_POINTER,                          \
     .offset       = offsetof(_state, _field),                        \
 }
 
@@ -1200,9 +1190,6 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
 
-#define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
-    VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
-
 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
 
migration/vmstate.c +0 -0
index bc8eb3d3ca..f7363a8776 100644
@@ -102,7 +102,7 @@ static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
 
Quoted 7 lines

migration/vmstate.c static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field) · line 105

Quoted 4 lines
migration/vmstate.c +1 -1
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY) {
+    } else if (field->num_offset.size) {

Hmm, is it better to infer from the data rather than having the explicit flag? And if so, can't we remove VMS_VARRAY flag altogether?

Yes, looks like we can drop it too.

Quoted 5 lines

From Vladimir's message I though the intent was not only to remove the flag, but to remove the definition of VMSTATE_VBUFFER entirely and somehow fit that case into VMSTATE_VARRAY, removing size_offset along with it.

I may have messed up one thing in my prior quick patch, that I replaced VMS_VBUFFER with VMS_VARRAY, but in reality I don't think we need .num for VBUFFER case.. so maybe VMS_VARRAY isn't needed either.

Or say, we should have two ways to get rid of VBUFFER: either treat it as one buffer (.size non-zero, .num not needed), or multiple chars (.size = 1 which is a byte, .num to be size of buffer).

Quoted 24 lines
Quoted 24 lines
migration/vmstate.c +5 -6
         n_elems = vmstate_read_from_offset(opaque,
     &field->num_offset); }
 
@@ -114,17 +114,17 @@ static uint64_t vmstate_size(void *opaque, const VMStateField *field)
 {
     uint64_t size;
 
-    if (field->flags & VMS_VBUFFER) {
-        size = vmstate_read_from_offset(opaque, &field->size_offset);
-        if (field->flags & VMS_MULTIPLY) {
-            size *= field->size;
-        }
-    } else if (field->flags & VMS_ARRAY_OF_POINTER) {
+    if (field->flags & VMS_ARRAY_OF_POINTER) {
         /*
          * For an array of pointer, the each element is always size of a
          * host pointer.
          */
         size = sizeof(void *);
+    } else if (field->size_offset.size) {
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
+        if (field->flags & VMS_MULTIPLY) {
+            size *= field->size;
Quoted 8 lines

migration/vmstate.c static uint64_t vmstate_size(void *opaque, const VMStateField *field) · line 129

Quoted 3 lines
migration/vmstate.c +1 -0
+        }
     } else {
         size = field->size;

Since we're talking about flags, I'd like to be able to assert(size) at this point, but there is some weirness with vmstate_msix and vmstate_scsi_device that have no state at all. I'm thinking of introducing a new flag to identify those cases:

Your patch below looks ok to me, I'll wait for you to repost with all the things. There seems to be quite something we can explore in vmstate code to clean it up..

Just to say, IIUC there're better way to impl both. It looks like both of them came from 10+ years ago so vmstate code was rough. Now we have more facilities and I think we can do better on them, I suspect,

- vmstate_msix can be replaced with two VBUFFER plus some post_load hooks,

- vmstate_scsi_device might be replaced with VMSTATE_QTAILQ_V, but this one is trickier to make sure ABI compatible.

These can be future items to explore even if applicable.. Let's fix the security / hardening issues first that your series addresses.

Thanks,

Quoted 105 lines

-- >8 --
From: Fabiano Rosas <[email protected]>
Subject: [PATCH] migration: Add VMS_NO_STATE flag

There are a few special cases of vmstate usage:

The vmstate_msix and vmstate_scsi_device have fields that contain no data, only a vmstate_info structure.

The VMSTATE_VALIDATE macro serves only to invoke the .field_exists routine for validation.

Regardless whether these scenarios are valid, add a separate flag to identify them so we can enforce common constraints for the normal vmstates such as having a size greater than zero.

---
 hw/pci/msix.c               | 6 +-----
 hw/scsi/scsi-bus.c          | 6 +-----
 include/migration/vmstate.h | 9 +++++++--
 migration/savevm.c          | 3 +--
 4 files changed, 10 insertions(+), 14 deletions(-)
hw/pci/msix.c +1 -5
index 1b23eaf1007..adf76b5bccc 100644
@@ -711,12 +711,8 @@ const VMStateDescription vmstate_msix = {
     .fields = (const VMStateField[]) {
         {
             .name         = "msix",
-            .version_id   = 0,
-            .field_exists = NULL,
-            .size         = 0,   /* ouch */
             .info         = &vmstate_info_msix,
-            .flags        = VMS_SINGLE,
-            .offset       = 0,
+            .flags        = VMS_SINGLE | VMS_NO_STATE,
         },
         VMSTATE_END_OF_LIST()
     }
hw/scsi/scsi-bus.c +1 -5
index deb43d5560e..aa02ff631b7 100644
@@ -1980,12 +1980,8 @@ const VMStateDescription vmstate_scsi_device = {
         VMSTATE_UINT32(sense_len, SCSIDevice),
         {
             .name         = "requests",
-            .version_id   = 0,
-            .field_exists = NULL,
-            .size         = 0,   /* ouch */
             .info         = &vmstate_info_scsi_requests,
-            .flags        = VMS_SINGLE,
-            .offset       = 0,
+            .flags        = VMS_SINGLE | VMS_NO_STATE,
         },
         VMSTATE_END_OF_LIST()
     },
include/migration/vmstate.h +7 -2
index 09b2e535645..ada1625f5b6 100644
@@ -108,6 +108,12 @@ enum VMStateFlags {
      */
     VMS_ARRAY_OF_POINTER = 0x040,
 
+    /*
+     * The field contains no data. Used for special cases such as
+     * invoking a custom VMStateInfo.
+     */
+    VMS_NO_STATE = 0x080,
+
     /* The size of the individual entries (a single array entry if
      * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
      * neither is set) is variable (i.e. not known at compile-time),
@@ -451,8 +457,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VALIDATE(_name, _test) { \
     .name         = (_name),                                         \
     .field_exists = (_test),                                         \
-    .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
-    .num          = 0, /* 0 elements: no data, only run _test */     \
+    .flags        = VMS_ARRAY | VMS_MUST_EXIST | VMS_NO_STATE,       \
 }
 
 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
migration/savevm.c +1 -2
index 3e5cce6520d..160c0f2d97d 100644
@@ -617,8 +617,7 @@ static void dump_vmstate_vmsd(FILE *out_file,
         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
         first = true;
         while (field->name != NULL) {
-            if (field->flags & VMS_MUST_EXIST) {
-                /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
+            if (field->flags & VMS_NO_STATE) {
                 field++;
                 continue;
             }
Signature · 1 line
2.53.0
Signature · 1 line
Peter Xu
#11 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

In reply to Fabiano Rosas
Quoted 55 lines · Fabiano Rosas

When migrating a buffer or array, the vmstate code needs to know the size of the buffer and the number of elements of the array. Today a vmstate writer can choose from a number of macros that take as last input the name of a struct member from where the size/num will be read.

At load time, the code will access those value via an opaque pointer to the migrated data and therefore it needs to also know the size of the struct member at that offset.

Currently that information is communicated by means of the VMS_VARRAY_* and VMS_VBUFFER_* flags, where each possible type is represented by a flag.

So far, that's all fine, but since the vmstate code makes heavy use of macros, handling several types individually (i.e. by name: int, int32_t, etc) requires several versions of a same macro, one for each type. E.g: VMSTATE_VBUFFER_ALLOC_UINT32

                                 ^

This creates a pattern where the vmstate writer has to match the macro name to the data type and has resulted in the code having a tendency of having one macro version for each type, for each type of vmstate.

There is also some cognitive load to deal with, e.g. VMSTATE_VARRAY_INT32 doesn't hold an array of int32, it holds an array of something else and the number of elements for the array is stored in a variable of type int32.

We're now dealing with the scenario where the code has been expecting int32_t at some places, but a uint64_t macro variant has been added without the code being updated.

To address all these situations, introduce a new struct that will hold the offset of the struct members, but also their size, so the various extra macros can all be removed and the person writing the vmstate doesn't need to care about type-checking. Still, keep a minimum check that those fields are at least integers and fit into 64 bits.

What changes:

1) type checking changes from individual types to a single check for all integers;

2) there are new ways to access the offsets;

num_offset -> num_offset.off
size_offset -> size_offset.off
[new] num_offset.size
[new] size_offset.size

2) reading the offsets goes from checking the VMS_VARRAY_* flags in an if/elseif block to comparing offset.size against the hardcoded sizes in bytes;

Quoted 6 lines

3) the VMS_VARRAY_* and VMS_VBUFFER_* flags become obsolete. Removed in the next patch;

4) memory usage increases +1 byte per vmstate;

Nice work! Just some minor comments below.

Quoted 19 lines
---
 include/migration/vmstate.h | 66 +++++++++++++++++++++++--------------
 migration/savevm.c          |  4 +--
 migration/vmstate.c         | 51 ++++++++++++++++++----------
 3 files changed, 76 insertions(+), 45 deletions(-)
include/migration/vmstate.h +1 -0
index 92a1a9fe98e..2ad3cc01371 100644
@@ -31,6 +31,7 @@
 
 typedef struct VMStateInfo VMStateInfo;
 typedef struct VMStateField VMStateField;
+typedef struct VMStateOffset VMStateOffset;
 
 /*
  * VMStateInfo allows customized migration of objects that don't fit in

include/migration/vmstate.h typedef enum { · line 195

Quoted 8 lines
include/migration/vmstate.h +5 -0
@@ -187,6 +188,11 @@ typedef enum {
     MIG_PRI_MAX,
 } MigrationPriority;
 
+struct VMStateOffset {
+    uint32_t off;
+    uint8_t size;
+};
+

I think it would be great to add some comments here.

What does the struct describe? size and offset of what where? And IIUC size 0 has a special meaning?
And neither offset nor size ever come from the migration stream, right?

Quoted 246 lines
include/migration/vmstate.h +35 -25
 struct VMStateField {
     const char *name;
     size_t offset;
@@ -205,11 +211,11 @@ struct VMStateField {
      * pointer point to.
      */
     size_t size;
-    size_t size_offset;
+    VMStateOffset size_offset;
 
     size_t start;
     int num;
-    size_t num_offset;
+    VMStateOffset num_offset;
     const VMStateInfo *info;
     enum VMStateFlags flags;
     const VMStateDescription *vmsd;
@@ -328,6 +334,16 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     (type_check(t1, typeof_elt_of_field(t2, f))                      \
      + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
 
+#define type_check_int64(t)                                             \
+    (((ptrdiff_t)0 * (ptrdiff_t)(~((t)0))) +                            \
+     (0 * sizeof(char[(sizeof(t) <= sizeof(uint64_t)) ? 1 : -1])))
+
+#define vmstate_field_offset(_state, _field) {                  \
+    .off = (offsetof(_state, _field) +                          \
+            type_check_int64(typeof_field(_state, _field))),    \
+    .size = sizeof(typeof_field(_state, _field)),               \
+}
+
 #define vmstate_offset_value(_state, _field, _type)                  \
     (offsetof(_state, _field) +                                      \
      type_check(_type, typeof_field(_state, _field)))
@@ -454,7 +470,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
@@ -464,7 +480,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
@@ -474,7 +490,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
@@ -484,7 +500,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
@@ -494,7 +510,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
@@ -504,7 +520,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT16,                                 \
@@ -583,7 +599,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     _field, _state, _field_num, _version, _vmsd, _type) {            \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
@@ -596,7 +612,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     _field, _state, _field_num, _version, _vmsd, _type) {             \
     .name       = (stringify(_field)),                                \
     .version_id = (_version),                                         \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),           \
     .vmsd       = &(_vmsd),                                           \
     .size       = sizeof(_type),                                      \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
@@ -608,7 +624,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
     .name       = (stringify(_field)),                                    \
     .version_id = (_version),                                             \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
+    .num_offset = vmstate_field_offset(_state, _field_num),               \
     .info       = &(_info),                                               \
     .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
@@ -650,7 +666,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -674,7 +690,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -684,7 +700,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -694,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
@@ -703,7 +719,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -713,7 +729,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -725,7 +741,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
@@ -746,7 +762,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
@@ -757,7 +773,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
@@ -767,7 +783,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
@@ -777,7 +793,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint64_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER | VMS_POINTER,                       \
     .offset       = offsetof(_state, _field),                        \
@@ -788,7 +804,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
     .offset       = offsetof(_state, _field),                        \
@@ -848,7 +864,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
     .name         = "unused",                                        \
     .field_exists = (_test),                                         \
-    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset   = vmstate_field_offset(_state, _field_num),        \
     .version_id   = (_version),                                      \
     .size         = (_size),                                         \
     .info         = &vmstate_info_unused_buffer,                     \
@@ -862,7 +878,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .field_exists = (_test),                                         \
     .version_id   = (_version),                                      \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
migration/savevm.c +0 -0
index 34dd06f9f73..a272bcfd0b4 100644

migration/savevm.c static void vmstate_check(const VMStateDescription *vmsd) · line 876

Quoted 5 lines
migration/savevm.c +1 -1
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                      * Size must be provided because dest QEMU needs that
                      * info to know what to allocate
                      */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);

Why is field->size checked by coersing it to bool, but field->size_offset.size - by comparison to 0? field->size != 0 for consistency?

Quoted 16 lines
migration/savevm.c +1 -1
                 } else {
                     /*
                      * Otherwise size info isn't useful (because it's
                      * always the size of host pointer), detect accidental
                      * setup of sizes in this case.
                      */
-                    assert(field->size == 0 && field->size_offset == 0);
+                    assert(field->size == 0 && field->size_offset.size == 0);
                 }
                 /*
                  * VMS_ARRAY_OF_POINTER must be used only together with one
migration/vmstate.c +0 -0
index 50ebe378452..0a0b9faa20e 100644

migration/vmstate.c vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field) · line 85

Quoted 10 lines
migration/vmstate.c +4 -2
@@ -78,32 +78,45 @@ vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
     };
 }
 
-static int vmstate_n_elems(void *opaque, const VMStateField *field)
+static uint64_t vmstate_read_from_offset(void *opaque,
+                                         const VMStateOffset *offset)
 {
-    int n_elems = 1;
+    uint8_t *ptr = (uint8_t *)opaque + offset->off;
+

The below can easily produce at least UB if offset is not size aligned.
I *think* offset and size both come from macros that guarantee this never happens, but maybe better be safe.

Quoted 83 lines
migration/vmstate.c +29 -16
+    switch (offset->size) {
+    case 1:
+        return *(uint8_t *)ptr;
+    case 2:
+        return *(uint16_t *)ptr;
+    case 4:
+        return *(uint32_t *)ptr;
+    case 8:
+        return *(uint64_t *)ptr;
+    }
+    g_assert_not_reached();
+}
+
+static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
+{
+    uint64_t n_elems = 1;
 
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY_INT32) {
-        n_elems = *(int32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT32) {
-        n_elems = *(uint32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT16) {
-        n_elems = *(uint16_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT8) {
-        n_elems = *(uint8_t *)(opaque + field->num_offset);
+    } else if (field->flags & (VMS_VARRAY_INT32 | VMS_VARRAY_UINT32
+                               | VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8)) {
+        n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
     }
 
     trace_vmstate_n_elems(field->name, n_elems);
     return n_elems;
 }
 
-static int vmstate_size(void *opaque, const VMStateField *field)
+static uint64_t vmstate_size(void *opaque, const VMStateField *field)
 {
-    int size;
+    uint64_t size;
 
     if (field->flags & VMS_VBUFFER) {
-        size = *(int32_t *)(opaque + field->size_offset);
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
         if (field->flags & VMS_MULTIPLY) {
             size *= field->size;
         }
@@ -124,7 +137,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
                                  void *opaque)
 {
     if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
-        gsize size = vmstate_size(opaque, field);
+        uint64_t size = vmstate_size(opaque, field);
         size *= vmstate_n_elems(opaque, field);
         if (size) {
             *(void **)ptr = g_malloc(size);
@@ -335,8 +348,9 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
 
         if (exists) {
             void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
 
             vmstate_handle_alloc(first_elem, field, opaque);
             if (field->flags & VMS_POINTER) {
@@ -650,8 +664,9 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
     while (field->name) {
         if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
             void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
             JSONWriter *vmdesc_loop = vmdesc;
             bool is_prev_null = false;
             /*
Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

Quoted 87 lines · Michael S. Tsirkin
Quoted 55 lines · Fabiano Rosas

When migrating a buffer or array, the vmstate code needs to know the size of the buffer and the number of elements of the array. Today a vmstate writer can choose from a number of macros that take as last input the name of a struct member from where the size/num will be read.

At load time, the code will access those value via an opaque pointer to the migrated data and therefore it needs to also know the size of the struct member at that offset.

Currently that information is communicated by means of the VMS_VARRAY_* and VMS_VBUFFER_* flags, where each possible type is represented by a flag.

So far, that's all fine, but since the vmstate code makes heavy use of macros, handling several types individually (i.e. by name: int, int32_t, etc) requires several versions of a same macro, one for each type. E.g: VMSTATE_VBUFFER_ALLOC_UINT32

                                 ^

This creates a pattern where the vmstate writer has to match the macro name to the data type and has resulted in the code having a tendency of having one macro version for each type, for each type of vmstate.

There is also some cognitive load to deal with, e.g. VMSTATE_VARRAY_INT32 doesn't hold an array of int32, it holds an array of something else and the number of elements for the array is stored in a variable of type int32.

We're now dealing with the scenario where the code has been expecting int32_t at some places, but a uint64_t macro variant has been added without the code being updated.

To address all these situations, introduce a new struct that will hold the offset of the struct members, but also their size, so the various extra macros can all be removed and the person writing the vmstate doesn't need to care about type-checking. Still, keep a minimum check that those fields are at least integers and fit into 64 bits.

What changes:

1) type checking changes from individual types to a single check for all integers;

2) there are new ways to access the offsets;

num_offset -> num_offset.off
size_offset -> size_offset.off
[new] num_offset.size
[new] size_offset.size

2) reading the offsets goes from checking the VMS_VARRAY_* flags in an if/elseif block to comparing offset.size against the hardcoded sizes in bytes;

Quoted 6 lines

3) the VMS_VARRAY_* and VMS_VBUFFER_* flags become obsolete. Removed in the next patch;

4) memory usage increases +1 byte per vmstate;

Nice work! Just some minor comments below.

Quoted 22 lines
---
 include/migration/vmstate.h | 66 +++++++++++++++++++++++--------------
 migration/savevm.c          |  4 +--
 migration/vmstate.c         | 51 ++++++++++++++++++----------
 3 files changed, 76 insertions(+), 45 deletions(-)
include/migration/vmstate.h +1 -0
index 92a1a9fe98e..2ad3cc01371 100644
@@ -31,6 +31,7 @@
 
 typedef struct VMStateInfo VMStateInfo;
 typedef struct VMStateField VMStateField;
+typedef struct VMStateOffset VMStateOffset;
 
 /*
  * VMStateInfo allows customized migration of objects that don't fit in
@@ -187,6 +188,11 @@ typedef enum {
     MIG_PRI_MAX,
 } MigrationPriority;
 
Quoted 10 lines

include/migration/vmstate.h typedef enum { · line 195

Quoted 5 lines
include/migration/vmstate.h +5 -0
+struct VMStateOffset {
+    uint32_t off;
+    uint8_t size;
+};
+

I think it would be great to add some comments here.

Indeed, hopefully also some better naming for everything.

Quoted 1 line

What does the struct describe? size and offset of what where?

size and offset of a field inside the struct being migrated that represent either the size of a buffer or the numer of elements in an array. Super simple =D

Quoted 1 line

And IIUC size 0 has a special meaning?

See below.

Quoted 1 line

And neither offset nor size ever come from the migration stream, right?

Right.

Quoted 248 lines
Quoted 248 lines
include/migration/vmstate.h +35 -25
 struct VMStateField {
     const char *name;
     size_t offset;
@@ -205,11 +211,11 @@ struct VMStateField {
      * pointer point to.
      */
     size_t size;
-    size_t size_offset;
+    VMStateOffset size_offset;
 
     size_t start;
     int num;
-    size_t num_offset;
+    VMStateOffset num_offset;
     const VMStateInfo *info;
     enum VMStateFlags flags;
     const VMStateDescription *vmsd;
@@ -328,6 +334,16 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     (type_check(t1, typeof_elt_of_field(t2, f))                      \
      + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
 
+#define type_check_int64(t)                                             \
+    (((ptrdiff_t)0 * (ptrdiff_t)(~((t)0))) +                            \
+     (0 * sizeof(char[(sizeof(t) <= sizeof(uint64_t)) ? 1 : -1])))
+
+#define vmstate_field_offset(_state, _field) {                  \
+    .off = (offsetof(_state, _field) +                          \
+            type_check_int64(typeof_field(_state, _field))),    \
+    .size = sizeof(typeof_field(_state, _field)),               \
+}
+
 #define vmstate_offset_value(_state, _field, _type)                  \
     (offsetof(_state, _field) +                                      \
      type_check(_type, typeof_field(_state, _field)))
@@ -454,7 +470,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
@@ -464,7 +480,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
@@ -474,7 +490,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
@@ -484,7 +500,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
@@ -494,7 +510,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
@@ -504,7 +520,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_VARRAY_UINT16,                                 \
@@ -583,7 +599,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     _field, _state, _field_num, _version, _vmsd, _type) {            \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
@@ -596,7 +612,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     _field, _state, _field_num, _version, _vmsd, _type) {             \
     .name       = (stringify(_field)),                                \
     .version_id = (_version),                                         \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),           \
     .vmsd       = &(_vmsd),                                           \
     .size       = sizeof(_type),                                      \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
@@ -608,7 +624,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
     .name       = (stringify(_field)),                                    \
     .version_id = (_version),                                             \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
+    .num_offset = vmstate_field_offset(_state, _field_num),               \
     .info       = &(_info),                                               \
     .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
@@ -650,7 +666,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -674,7 +690,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -684,7 +700,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
@@ -694,7 +710,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
@@ -703,7 +719,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -713,7 +729,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 
 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
@@ -725,7 +741,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
-    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
+    .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
@@ -746,7 +762,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .size         = (_multiply),                                      \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
@@ -757,7 +773,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
@@ -767,7 +783,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
@@ -777,7 +793,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint64_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER | VMS_POINTER,                       \
     .offset       = offsetof(_state, _field),                        \
@@ -788,7 +804,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
-    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_buffer,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
     .offset       = offsetof(_state, _field),                        \
@@ -848,7 +864,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
     .name         = "unused",                                        \
     .field_exists = (_test),                                         \
-    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
+    .num_offset   = vmstate_field_offset(_state, _field_num),        \
     .version_id   = (_version),                                      \
     .size         = (_size),                                         \
     .info         = &vmstate_info_unused_buffer,                     \
@@ -862,7 +878,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .name         = (stringify(_field)),                             \
     .field_exists = (_test),                                         \
     .version_id   = (_version),                                      \
-    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
+    .size_offset  = vmstate_field_offset(_state, _field_size),       \
     .info         = &vmstate_info_bitmap,                            \
     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
     .offset       = offsetof(_state, _field),                        \
migration/savevm.c +0 -0
index 34dd06f9f73..a272bcfd0b4 100644
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                      * Size must be provided because dest QEMU needs that
Quoted 9 lines

migration/savevm.c static void vmstate_check(const VMStateDescription *vmsd) · line 876

Quoted 4 lines
migration/savevm.c +1 -1
                      * info to know what to allocate
                      */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);

Why is field->size checked by coersing it to bool, but field->size_offset.size - by comparison to 0? field->size != 0 for consistency?

Ah, I initially had field->size_offset.off != 0 but 0 is a valid offset, so I changed to size.

This just wants to say "there's no size_offset". So 0 is not special, it just means that the size_offset was never set. I'll make both sides uniform.

Quoted 20 lines
Quoted 20 lines
migration/savevm.c +1 -1
                 } else {
                     /*
                      * Otherwise size info isn't useful (because it's
                      * always the size of host pointer), detect accidental
                      * setup of sizes in this case.
                      */
-                    assert(field->size == 0 && field->size_offset == 0);
+                    assert(field->size == 0 && field->size_offset.size == 0);
                 }
                 /*
                  * VMS_ARRAY_OF_POINTER must be used only together with one
migration/vmstate.c +0 -0
index 50ebe378452..0a0b9faa20e 100644
@@ -78,32 +78,45 @@ vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field)
     };
 }
 
Quoted 13 lines

migration/vmstate.c vmsd_init_ptr_marker_field(VMStateField *fake, const VMStateField *field) · line 85

Quoted 7 lines
migration/vmstate.c +4 -2
-static int vmstate_n_elems(void *opaque, const VMStateField *field)
+static uint64_t vmstate_read_from_offset(void *opaque,
+                                         const VMStateOffset *offset)
 {
-    int n_elems = 1;
+    uint8_t *ptr = (uint8_t *)opaque + offset->off;
+

The below can easily produce at least UB if offset is not size aligned.
I *think* offset and size both come from macros that guarantee this never happens, but maybe better be safe.

Right, I'll put a memcpy inside the switch then.

Quoted 83 lines
Quoted 83 lines
migration/vmstate.c +29 -16
+    switch (offset->size) {
+    case 1:
+        return *(uint8_t *)ptr;
+    case 2:
+        return *(uint16_t *)ptr;
+    case 4:
+        return *(uint32_t *)ptr;
+    case 8:
+        return *(uint64_t *)ptr;
+    }
+    g_assert_not_reached();
+}
+
+static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
+{
+    uint64_t n_elems = 1;
 
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & VMS_VARRAY_INT32) {
-        n_elems = *(int32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT32) {
-        n_elems = *(uint32_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT16) {
-        n_elems = *(uint16_t *)(opaque + field->num_offset);
-    } else if (field->flags & VMS_VARRAY_UINT8) {
-        n_elems = *(uint8_t *)(opaque + field->num_offset);
+    } else if (field->flags & (VMS_VARRAY_INT32 | VMS_VARRAY_UINT32
+                               | VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8)) {
+        n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
     }
 
     trace_vmstate_n_elems(field->name, n_elems);
     return n_elems;
 }
 
-static int vmstate_size(void *opaque, const VMStateField *field)
+static uint64_t vmstate_size(void *opaque, const VMStateField *field)
 {
-    int size;
+    uint64_t size;
 
     if (field->flags & VMS_VBUFFER) {
-        size = *(int32_t *)(opaque + field->size_offset);
+        size = vmstate_read_from_offset(opaque, &field->size_offset);
         if (field->flags & VMS_MULTIPLY) {
             size *= field->size;
         }
@@ -124,7 +137,7 @@ static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
                                  void *opaque)
 {
     if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
-        gsize size = vmstate_size(opaque, field);
+        uint64_t size = vmstate_size(opaque, field);
         size *= vmstate_n_elems(opaque, field);
         if (size) {
             *(void **)ptr = g_malloc(size);
@@ -335,8 +348,9 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
 
         if (exists) {
             void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
 
             vmstate_handle_alloc(first_elem, field, opaque);
             if (field->flags & VMS_POINTER) {
@@ -650,8 +664,9 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
     while (field->name) {
         if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
             void *first_elem = opaque + field->offset;
-            int i, n_elems = vmstate_n_elems(opaque, field);
-            int size = vmstate_size(opaque, field);
+            int i;
+            uint64_t n_elems = vmstate_n_elems(opaque, field);
+            uint64_t size = vmstate_size(opaque, field);
             JSONWriter *vmdesc_loop = vmdesc;
             bool is_prev_null = false;
             /*
Signature · 1 line
2.53.0
#13 lore raw

Re: [PATCH 2/4] migration: Introduce VMStateOffset

In reply to Michael S. Tsirkin
Quoted 6 lines · Michael S. Tsirkin
Quoted 6 lines
migration/savevm.c +0 -0
index 34dd06f9f73..a272bcfd0b4 100644
@@ -873,14 +873,14 @@ static void vmstate_check(const VMStateDescription *vmsd)
                      * Size must be provided because dest QEMU needs that
Quoted 8 lines

migration/savevm.c static void vmstate_check(const VMStateDescription *vmsd) · line 876

Quoted 4 lines
migration/savevm.c +1 -1
                      * info to know what to allocate
                      */
-                    assert(field->size || field->size_offset);
+                    assert(field->size || field->size_offset.size != 0);

Why is field->size checked by coersing it to bool, but field->size_offset.size - by comparison to 0? field->size != 0 for consistency?

Indeed it might be better to add a comment, but for another reason: IIUC old code was wrong.. size_offset can be 0 if the size field is the 1st element.. so this may have fixed a bug.

Signature · 1 line
Peter Xu
#14 lore raw

PATCH 3/4 migration: Remove redundant flags

In reply to Fabiano Rosas

Remove the VMS_VARRAY and VMS_VBUFFER flags that became redundant due to the previous commit which stores the size of the vmstate offset variables along with the offset itself.

---
 include/migration/vmstate.h        | 76 ++++++++++++------------------
 migration/savevm.c                 |  6 +--
 migration/vmstate.c                |  3 +-
 rust/bindings/migration-sys/lib.rs | 15 ++++--
 rust/migration/src/vmstate.rs      | 30 ++++++++++--
 rust/tests/tests/vmstate_tests.rs  | 30 ++++++------
 6 files changed, 83 insertions(+), 77 deletions(-)
include/migration/vmstate.h +29 -47
index 2ad3cc01371..c4fd036344d 100644
@@ -69,8 +69,9 @@ enum VMStateFlags {
      * }). Dereference the pointer before using it as basis for
      * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
      * affect the meaning of VMStateField.num_offset or
-     * VMStateField.size_offset; see VMS_VARRAY* and VMS_VBUFFER for
-     * those. */
+     * VMStateField.size_offset; see VMS_VARRAY and VMS_VBUFFER for
+     * those.
+     */
     VMS_POINTER          = 0x002,
 
     /* The field is an array of fixed size. VMStateField.num contains
@@ -80,21 +81,22 @@ enum VMStateFlags {
      * VMS_MULTIPLY. Each array entry will be processed individually
      * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
      * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
-     * be combined with VMS_VARRAY*. */
+     * be combined with VMS_VARRAY.
+     */
     VMS_ARRAY            = 0x004,
 
     /* The field is itself a struct, containing one or more
      * fields. Recurse into VMStateField.vmsd. Most useful in
-     * combination with VMS_ARRAY / VMS_VARRAY*, recursing into each
+     * combination with VMS_ARRAY / VMS_VARRAY, recursing into each
      * array entry. */
     VMS_STRUCT           = 0x008,
 
     /* The field is an array of variable size. The int32_t at opaque +
      * VMStateField.num_offset contains the number of entries in the
      * array. See the VMS_ARRAY description regarding array handling
-     * in general. May not be combined with VMS_ARRAY or any other
-     * VMS_VARRAY*. */
-    VMS_VARRAY_INT32     = 0x010,
+     * in general. May not be combined with VMS_ARRAY.
+     */
+    VMS_VARRAY     = 0x010,
 
     /* Ignored */
     VMS_BUFFER           = 0x020,
@@ -102,18 +104,12 @@ enum VMStateFlags {
     /* The field is a (fixed-size or variable-size) array of pointers
      * (e.g. struct a { uint8_t *b[]; }). Dereference each array entry
      * before using it. Note: Does not imply any one of VMS_ARRAY /
-     * VMS_VARRAY*; these need to be set explicitly. */
+     * VMS_VARRAY; these need to be set explicitly.
+     */
     VMS_ARRAY_OF_POINTER = 0x040,
 
-    /* The field is an array of variable size. The uint16_t at opaque
-     * + VMStateField.num_offset
-     * contains the number of entries in the array. See the VMS_ARRAY
-     * description regarding array handling in general. May not be
-     * combined with VMS_ARRAY or any other VMS_VARRAY*. */
-    VMS_VARRAY_UINT16    = 0x080,
-
     /* The size of the individual entries (a single array entry if
-     * VMS_ARRAY or any of VMS_VARRAY* are set, or the field itself if
+     * VMS_ARRAY or VMS_VARRAY are set, or the field itself if
      * neither is set) is variable (i.e. not known at compile-time),
      * but the same for all entries. Use the int32_t at opaque +
      * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
@@ -126,20 +122,6 @@ enum VMStateFlags {
      * allocated. Only valid in combination with VMS_VBUFFER. */
     VMS_MULTIPLY         = 0x200,
 
-    /* The field is an array of variable size. The uint8_t at opaque +
-     * VMStateField.num_offset
-     * contains the number of entries in the array. See the VMS_ARRAY
-     * description regarding array handling in general. May not be
-     * combined with VMS_ARRAY or any other VMS_VARRAY*. */
-    VMS_VARRAY_UINT8     = 0x400,
-
-    /* The field is an array of variable size. The uint32_t at opaque
-     * + VMStateField.num_offset
-     * contains the number of entries in the array. See the VMS_ARRAY
-     * description regarding array handling in general. May not be
-     * combined with VMS_ARRAY or any other VMS_VARRAY*. */
-    VMS_VARRAY_UINT32    = 0x800,
-
     /* Fail loading the serialised VM state if this field is missing
      * from the input. */
     VMS_MUST_EXIST       = 0x1000,
@@ -473,7 +455,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
+    .flags      = VMS_VARRAY | VMS_POINTER,                          \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -483,7 +465,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
+    .flags      = VMS_VARRAY | VMS_POINTER,                          \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -493,7 +475,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_INT32 | VMS_POINTER | VMS_ALLOC,        \
+    .flags      = VMS_VARRAY | VMS_POINTER | VMS_ALLOC,              \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -503,7 +485,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
+    .flags      = VMS_VARRAY | VMS_POINTER | VMS_ALLOC,              \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -513,7 +495,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
+    .flags      = VMS_VARRAY | VMS_POINTER | VMS_ALLOC,              \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -523,7 +505,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .info       = &(_info),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY_UINT16,                                 \
+    .flags      = VMS_VARRAY,                                        \
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
 
@@ -602,7 +584,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_POINTER | VMS_VARRAY_UINT8 |                   \
+    .flags      = VMS_POINTER | VMS_VARRAY |                         \
                   VMS_ARRAY_OF_POINTER | VMS_STRUCT |                \
                   VMS_ARRAY_OF_POINTER_AUTO_ALLOC,                   \
     .offset     = vmstate_offset_pointer(_state, _field, _type *),   \
@@ -615,7 +597,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),           \
     .vmsd       = &(_vmsd),                                           \
     .size       = sizeof(_type),                                      \
-    .flags      = VMS_POINTER | VMS_VARRAY_UINT32 |                   \
+    .flags      = VMS_POINTER | VMS_VARRAY |                          \
                   VMS_ARRAY_OF_POINTER | VMS_STRUCT |                 \
                   VMS_ARRAY_OF_POINTER_AUTO_ALLOC,                    \
     .offset     = vmstate_offset_pointer(_state, _field, _type *),    \
@@ -626,7 +608,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id = (_version),                                             \
     .num_offset = vmstate_field_offset(_state, _field_num),               \
     .info       = &(_info),                                               \
-    .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
+    .flags      = VMS_VARRAY | VMS_ARRAY_OF_POINTER | VMS_POINTER,        \
     .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
 }
 
@@ -670,7 +652,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
+    .flags      = VMS_STRUCT | VMS_VARRAY,                           \
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
 
@@ -693,7 +675,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
-    .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
+    .flags      = VMS_POINTER | VMS_VARRAY | VMS_STRUCT,             \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -703,7 +685,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
-    .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
+    .flags      = VMS_POINTER | VMS_VARRAY | VMS_STRUCT,             \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -713,7 +695,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
     .vmsd       = &(_vmsd),                                          \
-    .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
+    .flags      = VMS_POINTER | VMS_VARRAY | VMS_STRUCT,             \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -723,7 +705,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
+    .flags      = VMS_STRUCT | VMS_VARRAY,                           \
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
 
@@ -733,7 +715,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id = (_version),                                        \
     .vmsd       = &(_vmsd),                                          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
+    .flags      = VMS_STRUCT | VMS_VARRAY,                           \
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
 
@@ -743,7 +725,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .vmsd       = &(_vmsd),                                          \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .size       = sizeof(_type),                                     \
-    .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
+    .flags      = VMS_STRUCT | VMS_VARRAY | VMS_ALLOC | VMS_POINTER, \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
@@ -868,7 +850,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .version_id   = (_version),                                      \
     .size         = (_size),                                         \
     .info         = &vmstate_info_unused_buffer,                     \
-    .flags        = VMS_VARRAY_UINT32 | VMS_BUFFER,                  \
+    .flags        = VMS_VARRAY | VMS_BUFFER,                         \
 }
 
 /* _field_size should be a int32_t field in the _state struct giving the
migration/savevm.c +2 -4
index a272bcfd0b4..0b4fbfc72bb 100644
@@ -884,11 +884,9 @@ static void vmstate_check(const VMStateDescription *vmsd)
                 }
                 /*
                  * VMS_ARRAY_OF_POINTER must be used only together with one
-                 * of VMS_(V)ARRAY* flags.
+                 * of VMS_(V)ARRAY flags.
                  */
-                assert(field->flags & (VMS_ARRAY | VMS_VARRAY_INT32 |
-                                       VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8 |
-                                       VMS_VARRAY_UINT32));
+                assert(field->flags & (VMS_ARRAY | VMS_VARRAY));
             }
 
             if (field->flags & VMS_ARRAY_OF_POINTER_AUTO_ALLOC) {
migration/vmstate.c +1 -2
index 0a0b9faa20e..bc8eb3d3ca6 100644
@@ -102,8 +102,7 @@ static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field)
 
     if (field->flags & VMS_ARRAY) {
         n_elems = field->num;
-    } else if (field->flags & (VMS_VARRAY_INT32 | VMS_VARRAY_UINT32
-                               | VMS_VARRAY_UINT16 | VMS_VARRAY_UINT8)) {
+    } else if (field->flags & VMS_VARRAY) {
         n_elems = vmstate_read_from_offset(opaque, &field->num_offset);
     }
 
rust/bindings/migration-sys/lib.rs +11 -4
index 9581481e421..75b8c1387c2 100644
@@ -47,6 +47,7 @@ fn default() -> Self {
 unsafe impl Zeroable for VMStateFlags {}
 unsafe impl Zeroable for VMStateField {}
 unsafe impl Zeroable for VMStateDescription {}
+unsafe impl Zeroable for VMStateOffset {}
 
 // The following higher-level helpers could be in "migration"
 // crate when Rust has const trait impl.
@@ -57,10 +58,7 @@ pub trait VMStateFlagsExt {
 
 impl VMStateFlagsExt for VMStateFlags {
     const VMS_VARRAY_FLAGS: VMStateFlags = VMStateFlags(
-        VMStateFlags::VMS_VARRAY_INT32.0
-            | VMStateFlags::VMS_VARRAY_UINT8.0
-            | VMStateFlags::VMS_VARRAY_UINT16.0
-            | VMStateFlags::VMS_VARRAY_UINT32.0,
+        VMStateFlags::VMS_VARRAY.0
     );
 }
 
@@ -115,3 +113,12 @@ pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> Self {
         self.with_varray_flag_unchecked(flag)
     }
 }
+
+impl VMStateOffset {
+    pub const fn new(off: usize, size: usize) -> Self {
+        Self {
+            off: off as u32,
+            size: size as u8,
+        }
+    }
+}
rust/migration/src/vmstate.rs +25 -5
index 63d78b4f275..7023ebd2c22 100644
@@ -42,7 +42,7 @@
 };
 
 use crate::bindings::{self, VMStateFlags};
-pub use crate::bindings::{MigrationPriority, VMStateField};
+pub use crate::bindings::{MigrationPriority, VMStateField, VMStateOffset};
 
 /// This macro is used to call a function with a generic argument bound
 /// to the type of a field.  The function must take a
@@ -120,6 +120,23 @@ pub const fn vmstate_varray_flag<T: VMState>(_: PhantomData<T>) -> VMStateFlags
     T::VARRAY_FLAG
 }
 
+pub const OPAQUE: &[u8; 1048576] = &[0; 1048576];
+
+pub const fn size_of_ptr_type<T>(_: *const T) -> usize {
+    ::core::mem::size_of::<T>()
+}
+
+#[macro_export]
+macro_rules! size_of_field_type {
+    ($struct_name:ty, $($field_name:ident).+) => {
+        $crate::vmstate::size_of_ptr_type(unsafe {
+            ::core::ptr::addr_of!(
+                (*$crate::vmstate::OPAQUE.as_ptr().cast::<$struct_name>()).$($field_name).+
+            )
+        })
+    };
+}
+
 /// Return the `VMStateField` for a field of a struct.  The field must be
 /// visible in the current scope.
 ///
@@ -148,7 +165,10 @@ macro_rules! vmstate_of {
                 .as_bytes()
                 .as_ptr().cast::<::std::os::raw::c_char>(),
             offset: ::std::mem::offset_of!($struct_name, $($field_name).+),
-            $(num_offset: ::std::mem::offset_of!($struct_name, $($num).+),)?
+            $(num_offset: $crate::vmstate::VMStateOffset {
+                off: ::std::mem::offset_of!($struct_name, $($num).+) as u32,
+		size: $crate::size_of_field_type!($struct_name, $($num).+) as u8,
+            },)?
             $(field_exists: $crate::vmstate_exist_fn!($struct_name, $test_fn),)?
             // The calls to `call_func_with_field!` are the magic that
             // computes most of the VMStateField from the type of the field.
@@ -267,9 +287,9 @@ unsafe impl $crate::vmstate::VMState for $type {
 impl_vmstate_scalar!(vmstate_info_int16, i16);
 impl_vmstate_scalar!(vmstate_info_int32, i32);
 impl_vmstate_scalar!(vmstate_info_int64, i64);
-impl_vmstate_scalar!(vmstate_info_uint8, u8, VMS_VARRAY_UINT8);
-impl_vmstate_scalar!(vmstate_info_uint16, u16, VMS_VARRAY_UINT16);
-impl_vmstate_scalar!(vmstate_info_uint32, u32, VMS_VARRAY_UINT32);
+impl_vmstate_scalar!(vmstate_info_uint8, u8, VMS_VARRAY);
+impl_vmstate_scalar!(vmstate_info_uint16, u16, VMS_VARRAY);
+impl_vmstate_scalar!(vmstate_info_uint32, u32, VMS_VARRAY);
 impl_vmstate_scalar!(vmstate_info_uint64, u64);
 impl_vmstate_scalar!(vmstate_info_timer, util::timer::Timer);
 
rust/tests/tests/vmstate_tests.rs +15 -15
index c2c12cfab52..c5baed4d40b 100644
@@ -65,7 +65,7 @@ fn test_vmstate_uint16() {
         b"elem\0"
     );
     assert_eq!(foo_fields[0].offset, 16);
-    assert_eq!(foo_fields[0].num_offset, 0);
+    assert_eq!(foo_fields[0].num_offset.size, 0);
     assert_eq!(foo_fields[0].info, unsafe { &vmstate_info_int8 });
     assert_eq!(foo_fields[0].version_id, 0);
     assert_eq!(foo_fields[0].size, 1);
@@ -86,7 +86,7 @@ fn test_vmstate_unused() {
         b"unused\0"
     );
     assert_eq!(foo_fields[1].offset, 0);
-    assert_eq!(foo_fields[1].num_offset, 0);
+    assert_eq!(foo_fields[1].num_offset.size, 0);
     assert_eq!(foo_fields[1].info, unsafe { &vmstate_info_unused_buffer });
     assert_eq!(foo_fields[1].version_id, 0);
     assert_eq!(foo_fields[1].size, 8);
@@ -108,12 +108,12 @@ fn test_vmstate_varray_uint16_unsafe() {
         b"arr\0"
     );
     assert_eq!(foo_fields[2].offset, 0);
-    assert_eq!(foo_fields[2].num_offset, 4);
+    assert_eq!(foo_fields[2].num_offset.off, 4);
     assert_eq!(foo_fields[2].info, unsafe { &vmstate_info_uint8 });
     assert_eq!(foo_fields[2].version_id, 0);
     assert_eq!(foo_fields[2].size, 1);
     assert_eq!(foo_fields[2].num, 0);
-    assert_eq!(foo_fields[2].flags, VMStateFlags::VMS_VARRAY_UINT16);
+    assert_eq!(foo_fields[2].flags, VMStateFlags::VMS_VARRAY);
     assert!(foo_fields[2].vmsd.is_null());
     assert!(foo_fields[2].field_exists.is_none());
 }
@@ -172,7 +172,7 @@ fn test_vmstate_bool_v() {
         b"val\0"
     );
     assert_eq!(foo_fields[0].offset, 136);
-    assert_eq!(foo_fields[0].num_offset, 0);
+    assert_eq!(foo_fields[0].num_offset.size, 0);
     assert_eq!(foo_fields[0].info, unsafe { &vmstate_info_bool });
     assert_eq!(foo_fields[0].version_id, 2);
     assert_eq!(foo_fields[0].size, 1);
@@ -193,7 +193,7 @@ fn test_vmstate_uint64() {
         b"wrap\0"
     );
     assert_eq!(foo_fields[1].offset, 128);
-    assert_eq!(foo_fields[1].num_offset, 0);
+    assert_eq!(foo_fields[1].num_offset.size, 0);
     assert_eq!(foo_fields[1].info, unsafe { &vmstate_info_uint64 });
     assert_eq!(foo_fields[1].version_id, 0);
     assert_eq!(foo_fields[1].size, 8);
@@ -215,14 +215,14 @@ fn test_vmstate_struct_varray_uint8() {
         b"arr_a\0"
     );
     assert_eq!(foo_fields[2].offset, 0);
-    assert_eq!(foo_fields[2].num_offset, 60);
+    assert_eq!(foo_fields[2].num_offset.off, 60);
     assert!(foo_fields[2].info.is_null()); // VMSTATE_STRUCT_VARRAY_UINT8 doesn't set info field.
     assert_eq!(foo_fields[2].version_id, 1);
     assert_eq!(foo_fields[2].size, 20);
     assert_eq!(foo_fields[2].num, 0);
     assert_eq!(
         foo_fields[2].flags.0,
-        VMStateFlags::VMS_STRUCT.0 | VMStateFlags::VMS_VARRAY_UINT8.0
+        VMStateFlags::VMS_STRUCT.0 | VMStateFlags::VMS_VARRAY.0
     );
     assert_eq!(foo_fields[2].vmsd, VMSTATE_FOOA.as_ref());
     assert!(foo_fields[2].field_exists.is_none());
@@ -240,7 +240,7 @@ fn test_vmstate_macro_array() {
         b"arr_i64\0"
     );
     assert_eq!(foo_fields[4].offset, 144);
-    assert_eq!(foo_fields[4].num_offset, 0);
+    assert_eq!(foo_fields[4].num_offset.size, 0);
     assert_eq!(foo_fields[4].info, unsafe { &vmstate_info_int64 });
     assert_eq!(foo_fields[4].version_id, 0);
     assert_eq!(foo_fields[4].size, 8);
@@ -264,7 +264,7 @@ fn test_vmstate_struct_varray_uint8_wrapper() {
         unsafe { CStr::from_ptr(foo_fields[5].name) }.to_bytes_with_nul(),
         b"arr_a_wrap\0"
     );
-    assert_eq!(foo_fields[5].num_offset, 228);
+    assert_eq!(foo_fields[5].num_offset.off, 228);
     assert!(unsafe { foo_fields[5].field_exists.unwrap()(foo_b_p, 0) });
 
     // The last VMStateField in VMSTATE_FOOB.
@@ -316,7 +316,7 @@ fn test_vmstate_pointer() {
         b"ptr\0"
     );
     assert_eq!(foo_fields[0].offset, 0);
-    assert_eq!(foo_fields[0].num_offset, 0);
+    assert_eq!(foo_fields[0].num_offset.size, 0);
     assert_eq!(foo_fields[0].info, unsafe { &vmstate_info_int32 });
     assert_eq!(foo_fields[0].version_id, 2);
     assert_eq!(foo_fields[0].size, 4);
@@ -341,7 +341,7 @@ fn test_vmstate_struct_pointer() {
         b"ptr_a\0"
     );
     assert_eq!(foo_fields[1].offset, PTR_SIZE);
-    assert_eq!(foo_fields[1].num_offset, 0);
+    assert_eq!(foo_fields[1].num_offset.size, 0);
     assert_eq!(foo_fields[1].vmsd, VMSTATE_FOOA.as_ref());
     assert_eq!(foo_fields[1].version_id, 0);
     assert_eq!(foo_fields[1].size, size_of::<FooA>());
@@ -366,7 +366,7 @@ fn test_vmstate_macro_array_of_pointer() {
         b"arr_ptr\0"
     );
     assert_eq!(foo_fields[2].offset, 2 * PTR_SIZE);
-    assert_eq!(foo_fields[2].num_offset, 0);
+    assert_eq!(foo_fields[2].num_offset.size, 0);
     assert_eq!(foo_fields[2].info, unsafe { &vmstate_info_uint8 });
     assert_eq!(foo_fields[2].version_id, 0);
     assert_eq!(foo_fields[2].size, PTR_SIZE);
@@ -391,7 +391,7 @@ fn test_vmstate_macro_array_of_pointer_wrapped() {
         b"arr_ptr_wrap\0"
     );
     assert_eq!(foo_fields[3].offset, (FOO_ARRAY_MAX + 2) * PTR_SIZE);
-    assert_eq!(foo_fields[3].num_offset, 0);
+    assert_eq!(foo_fields[3].num_offset.size, 0);
     assert_eq!(foo_fields[3].info, unsafe { &vmstate_info_uint8 });
     assert_eq!(foo_fields[3].version_id, 0);
     assert_eq!(foo_fields[3].size, PTR_SIZE);
@@ -454,7 +454,7 @@ fn test_vmstate_validate() {
         b"foo_d_0\0"
     );
     assert_eq!(foo_fields[0].offset, 0);
-    assert_eq!(foo_fields[0].num_offset, 0);
+    assert_eq!(foo_fields[0].num_offset.size, 0);
     assert!(foo_fields[0].info.is_null());
     assert_eq!(foo_fields[0].version_id, 0);
     assert_eq!(foo_fields[0].size, 0);
Signature · 1 line
2.53.0
#15 lore raw

Re: [PATCH 3/4] migration: Remove redundant flags

Quoted 6 lines · Fabiano Rosas

Remove the VMS_VARRAY and VMS_VBUFFER flags that became redundant due to the previous commit which stores the size of the vmstate offset variables along with the offset itself.

---

[..]

Quoted 23 lines
rust/bindings/migration-sys/lib.rs +2 -4
@@ -47,6 +47,7 @@ fn default() -> Self {
  unsafe impl Zeroable for VMStateFlags {}
  unsafe impl Zeroable for VMStateField {}
  unsafe impl Zeroable for VMStateDescription {}
+unsafe impl Zeroable for VMStateOffset {}
 
  // The following higher-level helpers could be in "migration"
  // crate when Rust has const trait impl.
@@ -57,10 +58,7 @@ pub trait VMStateFlagsExt {
 
  impl VMStateFlagsExt for VMStateFlags {
      const VMS_VARRAY_FLAGS: VMStateFlags = VMStateFlags(
-        VMStateFlags::VMS_VARRAY_INT32.0
-            | VMStateFlags::VMS_VARRAY_UINT8.0
-            | VMStateFlags::VMS_VARRAY_UINT16.0
-            | VMStateFlags::VMS_VARRAY_UINT32.0,
+        VMStateFlags::VMS_VARRAY.0
      );
  }
 

rust/bindings/migration-sys/lib.rs pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> Self { · line 117

Quoted 5 lines
rust/bindings/migration-sys/lib.rs +2 -0
@@ -115,3 +113,12 @@ pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> Self {
          self.with_varray_flag_unchecked(flag)
      }
  }
+
+impl VMStateOffset {

This looks like part of patch 01? And may be some other changes in rust.

Quoted 7 lines
rust/bindings/migration-sys/lib.rs +7 -0
+    pub const fn new(off: usize, size: usize) -> Self {
+        Self {
+            off: off as u32,
+            size: size as u8,
+        }
+    }
+}

[..]

rust/tests/tests/vmstate_tests.rs fn test_vmstate_uint16() { · line 68

Quoted 10 lines
rust/tests/tests/vmstate_tests.rs +1 -1
index c2c12cfab52..c5baed4d40b 100644
@@ -65,7 +65,7 @@ fn test_vmstate_uint16() {
          b"elem\0"
      );
      assert_eq!(foo_fields[0].offset, 16);
-    assert_eq!(foo_fields[0].num_offset, 0);
+    assert_eq!(foo_fields[0].num_offset.size, 0);

This too.

Quoted 4 lines
rust/tests/tests/vmstate_tests.rs +0 -0
      assert_eq!(foo_fields[0].info, unsafe { &vmstate_info_int8 });
      assert_eq!(foo_fields[0].version_id, 0);
      assert_eq!(foo_fields[0].size, 1);
@@ -86,7 +86,7 @@ fn test_vmstate_unused() {
Signature · 2 lines
Best regards,
Vladimir
#16 lore raw

Re: [PATCH 3/4] migration: Remove redundant flags

Quoted 4 lines · Vladimir Sementsov-Ogievskiy

rust/bindings/migration-sys/lib.rs pub const fn with_varray_flag(mut self, flag: VMStateFlags) -> Self { · line 117

Quoted 2 lines
rust/bindings/migration-sys/lib.rs +2 -0
+
+impl VMStateOffset {

This looks like part of patch 01? And may be some other changes in rust.

02 I wanted to say

Signature · 2 lines
Best regards,
Vladimir
#17 lore raw

PATCH 4/4 migration: Remove duplicate vmstate macros

In reply to Fabiano Rosas

Now that type-checking is not being done per integer size anymore, various macros have duplicated bodies. Remove them.

The original names are left the same so the device code can be altered per-subsystem in the next patches.

---
 include/migration/vmstate.h | 136 ++++++++++--------------------------
 1 file changed, 36 insertions(+), 100 deletions(-)
include/migration/vmstate.h +36 -100
index c4fd036344d..24631fd6782 100644
@@ -449,7 +449,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
 }
 
-#define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
+#define VMSTATE_VARRAY(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
@@ -459,17 +459,10 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
-#define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
-    .name       = (stringify(_field)),                               \
-    .version_id = (_version),                                        \
-    .num_offset = vmstate_field_offset(_state, _field_num),          \
-    .info       = &(_info),                                          \
-    .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY | VMS_POINTER,                          \
-    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
-}
+#define VMSTATE_VARRAY_INT32 VMSTATE_VARRAY
+#define VMSTATE_VARRAY_UINT32 VMSTATE_VARRAY
 
-#define VMSTATE_VARRAY_INT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
+#define VMSTATE_VARRAY_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
@@ -479,27 +472,11 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
 
-#define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
-    .name       = (stringify(_field)),                               \
-    .version_id = (_version),                                        \
-    .num_offset = vmstate_field_offset(_state, _field_num),          \
-    .info       = &(_info),                                          \
-    .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY | VMS_POINTER | VMS_ALLOC,              \
-    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
-}
-
-#define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
-    .name       = (stringify(_field)),                               \
-    .version_id = (_version),                                        \
-    .num_offset = vmstate_field_offset(_state, _field_num),          \
-    .info       = &(_info),                                          \
-    .size       = sizeof(_type),                                     \
-    .flags      = VMS_VARRAY | VMS_POINTER | VMS_ALLOC,              \
-    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
-}
+#define VMSTATE_VARRAY_INT32_ALLOC VMSTATE_VARRAY_ALLOC
+#define VMSTATE_VARRAY_UINT32_ALLOC VMSTATE_VARRAY_ALLOC
+#define VMSTATE_VARRAY_UINT16_ALLOC VMSTATE_VARRAY_ALLOC
 
-#define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
+#define VMSTATE_VARRAY_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
@@ -509,6 +486,8 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
 
+#define VMSTATE_VARRAY_UINT16_UNSAFE VMSTATE_VARRAY_UNSAFE
+
 #define VMSTATE_VSTRUCT_TEST(_field, _state, _test, _version, _vmsd, _type, _struct_version) { \
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
@@ -577,7 +556,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
  * _info: VMStateInfo for _type (when VMS_STRUCT is not set)
  * start: size of (_type) pointed to (for auto memory allocation)
  */
-#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT8_ALLOC(\
+#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_ALLOC( \
     _field, _state, _field_num, _version, _vmsd, _type) {            \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
@@ -590,20 +569,12 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_pointer(_state, _field, _type *),   \
 }
 
-#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT32_ALLOC(\
-    _field, _state, _field_num, _version, _vmsd, _type) {             \
-    .name       = (stringify(_field)),                                \
-    .version_id = (_version),                                         \
-    .num_offset = vmstate_field_offset(_state, _field_num),           \
-    .vmsd       = &(_vmsd),                                           \
-    .size       = sizeof(_type),                                      \
-    .flags      = VMS_POINTER | VMS_VARRAY |                          \
-                  VMS_ARRAY_OF_POINTER | VMS_STRUCT |                 \
-                  VMS_ARRAY_OF_POINTER_AUTO_ALLOC,                    \
-    .offset     = vmstate_offset_pointer(_state, _field, _type *),    \
-}
+#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT8_ALLOC \
+    VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_ALLOC
+#define VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_UINT32_ALLOC \
+    VMSTATE_VARRAY_OF_POINTER_TO_STRUCT_ALLOC
 
-#define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
+#define VMSTATE_VARRAY_OF_POINTER(_field, _state, _field_num, _version, _info, _type) { \
     .name       = (stringify(_field)),                                    \
     .version_id = (_version),                                             \
     .num_offset = vmstate_field_offset(_state, _field_num),               \
@@ -612,6 +583,8 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = vmstate_offset_pointer(_state, _field, _type *),          \
 }
 
+#define VMSTATE_VARRAY_OF_POINTER_UINT32 VMSTATE_VARRAY_OF_POINTER
+
 #define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                                     \
     .version_id = (_version),                                              \
@@ -646,7 +619,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
                                            _n1, _n2),                \
 }
 
-#define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
+#define VMSTATE_STRUCT_VARRAY(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
@@ -655,6 +628,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .flags      = VMS_STRUCT | VMS_VARRAY,                           \
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
+#define VMSTATE_STRUCT_VARRAY_UINT8 VMSTATE_STRUCT_VARRAY
 
 /* a variable length array (i.e. _type *_field) but we know the
  * length
@@ -669,7 +643,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset     = offsetof(_state, _field),                          \
 }
 
-#define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
+#define VMSTATE_STRUCT_VARRAY_POINTER(_field, _state, _field_num, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .version_id = 0,                                                 \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
@@ -678,38 +652,11 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .flags      = VMS_POINTER | VMS_VARRAY | VMS_STRUCT,             \
     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 }
+#define VMSTATE_STRUCT_VARRAY_POINTER_INT32 VMSTATE_STRUCT_VARRAY_POINTER
+#define VMSTATE_STRUCT_VARRAY_POINTER_UINT32 VMSTATE_STRUCT_VARRAY_POINTER
+#define VMSTATE_STRUCT_VARRAY_POINTER_UINT16 VMSTATE_STRUCT_VARRAY_POINTER
 
-#define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
-    .name       = (stringify(_field)),                               \
-    .version_id = 0,                                                 \
-    .num_offset = vmstate_field_offset(_state, _field_num),          \
-    .size       = sizeof(_type),                                     \
-    .vmsd       = &(_vmsd),                                          \
-    .flags      = VMS_POINTER | VMS_VARRAY | VMS_STRUCT,             \
-    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
-}
-
-#define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
-    .name       = (stringify(_field)),                               \
-    .version_id = 0,                                                 \
-    .num_offset = vmstate_field_offset(_state, _field_num),          \
-    .size       = sizeof(_type),                                     \
-    .vmsd       = &(_vmsd),                                          \
-    .flags      = VMS_POINTER | VMS_VARRAY | VMS_STRUCT,             \
-    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
-}
-
-#define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
-    .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \
-    .version_id = (_version),                                        \
-    .vmsd       = &(_vmsd),                                          \
-    .size       = sizeof(_type),                                     \
-    .flags      = VMS_STRUCT | VMS_VARRAY,                           \
-    .offset     = vmstate_offset_varray(_state, _field, _type),      \
-}
-
-#define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
+#define VMSTATE_STRUCT_VARRAY(_field, _state, _field_num, _version, _vmsd, _type) { \
     .name       = (stringify(_field)),                               \
     .num_offset = vmstate_field_offset(_state, _field_num),          \
     .version_id = (_version),                                        \
@@ -718,6 +665,8 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .flags      = VMS_STRUCT | VMS_VARRAY,                           \
     .offset     = vmstate_offset_varray(_state, _field, _type),      \
 }
+#define VMSTATE_STRUCT_VARRAY_INT32 VMSTATE_STRUCT_VARRAY
+#define VMSTATE_STRUCT_VARRAY_UINT32 VMSTATE_STRUCT_VARRAY
 
 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
     .name       = (stringify(_field)),                               \
@@ -761,28 +710,11 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset       = offsetof(_state, _field),                        \
 }
 
-#define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
-    .name         = (stringify(_field)),                             \
-    .version_id   = (_version),                                      \
-    .field_exists = (_test),                                         \
-    .size_offset  = vmstate_field_offset(_state, _field_size),       \
-    .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
-    .offset       = offsetof(_state, _field),                        \
-}
+#define VMSTATE_VBUFFER_UINT32 VMSTATE_VBUFFER
+#define VMSTATE_VBUFFER_UINT64 VMSTATE_VBUFFER
 
-#define VMSTATE_VBUFFER_UINT64(_field, _state, _version, _test, _field_size) { \
-    .name         = (stringify(_field)),                             \
-    .version_id   = (_version),                                      \
-    .field_exists = (_test),                                         \
-    .size_offset  = vmstate_field_offset(_state, _field_size),       \
-    .info         = &vmstate_info_buffer,                            \
-    .flags        = VMS_VBUFFER | VMS_POINTER,                       \
-    .offset       = offsetof(_state, _field),                        \
-}
-
-#define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version,       \
-                                     _test, _field_size) {           \
+#define VMSTATE_VBUFFER_ALLOC(_field, _state, _version,              \
+                              _test, _field_size) {                  \
     .name         = (stringify(_field)),                             \
     .version_id   = (_version),                                      \
     .field_exists = (_test),                                         \
@@ -792,6 +724,8 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .offset       = offsetof(_state, _field),                        \
 }
 
+#define VMSTATE_VBUFFER_ALLOC_UINT32 VMSTATE_VBUFFER_ALLOC
+
 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
@@ -843,7 +777,7 @@ extern const VMStateInfo vmstate_info_g_byte_array;
 }
 
 /* Discard size * field_num bytes, where field_num is a uint32 member */
-#define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
+#define VMSTATE_UNUSED_VARRAY(_state, _test, _version, _field_num, _size) {\
     .name         = "unused",                                        \
     .field_exists = (_test),                                         \
     .num_offset   = vmstate_field_offset(_state, _field_num),        \
@@ -853,6 +787,8 @@ extern const VMStateInfo vmstate_info_g_byte_array;
     .flags        = VMS_VARRAY | VMS_BUFFER,                         \
 }
 
+#define VMSTATE_UNUSED_VARRAY_UINT32 VMSTATE_UNUSED_VARRAY
+
 /* _field_size should be a int32_t field in the _state struct giving the
  * size of the bitmap _field in bits.
  */
Signature · 1 line
2.53.0
#18 lore raw

Re: [PATCH 4/4] migration: Remove duplicate vmstate macros

include/migration/vmstate.h extern const VMStateInfo vmstate_info_g_byte_array; · line 704

Quoted 3 lines · Fabiano Rosas
include/migration/vmstate.h +0 -3
-#define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
-    .name       = (stringify(_field)),                               \
-    .num_offset = vmstate_field_offset(_state, _field_num, int32_t), \

Hmm, this int32_t should be dropped in earlier commit.. It doesn't break compilation because VMSTATE_STRUCT_VARRAY_INT32. Suggest to drop it completely in patch 01, together with VMSTATE_ARRAY_INT32_UNSAFE.

(interesting, do we have more unused macros?)

Quoted 7 lines
include/migration/vmstate.h +0 -7
-    .version_id = (_version),                                        \
-    .vmsd       = &(_vmsd),                                          \
-    .size       = sizeof(_type),                                     \
-    .flags      = VMS_STRUCT | VMS_VARRAY,                           \
-    .offset     = vmstate_offset_varray(_state, _field, _type),      \
-}
-
Signature · 2 lines
Best regards,
Vladimir
#19 lore raw
In reply to Fabiano Rosas
Quoted 11 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

Quoted 8 lines

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Thanks for doing it, all these _TYPE_ were pain!

Signature · 2 lines
Best regards,
Vladimir
#20 lore raw
In reply to Fabiano Rosas
Quoted 19 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Quoted 5 lines

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

Nice work!

I think I was only looking at VBUFFER side and I thought it was fine sticking with 32bit even signed or not, not a huge deal. But cleaning up VARRAY whole thing together looks definitely an improvement. I definitely like your version here.

I assume with your series I can drop both of my patches here, right?

[PATCH v2 1/5] migration: Fix possible overflow in vmstate_handle_alloc()
https://lore.kernel.org/r/[email protected]
(I'll still respin with the rest)

[PATCH] vhost/migration: Fix incorrect size used in inflight->addr in VMSD
https://lore.kernel.org/r/[email protected]

The only missing piece would be an multiply overflow check in vmstate_handle_alloc(), if you could add that check too while rewritting that in patch 1 then I think it'll cover all.

Vladimir's ask in the separate email makes sense: I wonder if we can also do one step further and merge VBUFFER into VARRAY.

The other trivial thing is, while looking, I found one trivial macro VMSTATE_PARTIAL_VBUFFER not used; can drop it altogether.

Signature · 1 line
Peter Xu
#21 lore raw
Quoted 46 lines · Peter Xu
Quoted 19 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Quoted 5 lines

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

Nice work!

I think I was only looking at VBUFFER side and I thought it was fine sticking with 32bit even signed or not, not a huge deal. But cleaning up VARRAY whole thing together looks definitely an improvement. I definitely like your version here.

I assume with your series I can drop both of my patches here, right?

[PATCH v2 1/5] migration: Fix possible overflow in vmstate_handle_alloc()
https://lore.kernel.org/r/[email protected]
(I'll still respin with the rest)

[PATCH] vhost/migration: Fix incorrect size used in inflight->addr in VMSD
https://lore.kernel.org/r/[email protected]

The only missing piece would be an multiply overflow check in vmstate_handle_alloc(), if you could add that check too while rewritting that in patch 1 then I think it'll cover all.

Quoted 5 lines

Vladimir's ask in the separate email makes sense: I wonder if we can also do one step further and merge VBUFFER into VARRAY.

The other trivial thing is, while looking, I found one trivial macro VMSTATE_PARTIAL_VBUFFER not used; can drop it altogether.

Now officially declare support for u64 on all these offsets, we also need to double check on our alignment with security issues.

Similar reports will not be a bug anymore but results will be the same I
assume: it's anything the attacker can feed a u64 directly (instead of an int32_t negative overflow), result is still failing a malloc() with enormously large numbers, legally this time.

Do you still plan to work on finding per-user upper limit or whatever of that kind? I'd say time spent working on series like this worths more than that, but I still want to check with you while looking at this solution.

I suppose with this, one option is we can close all tickets reporting security issues while allocating with all u64 fields.

Thanks,

Signature · 1 line
Peter Xu
#22 lore raw
Quoted 60 lines · Peter Xu
Quoted 46 lines · Peter Xu
Quoted 19 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Quoted 5 lines

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

Nice work!

I think I was only looking at VBUFFER side and I thought it was fine sticking with 32bit even signed or not, not a huge deal. But cleaning up VARRAY whole thing together looks definitely an improvement. I definitely like your version here.

I assume with your series I can drop both of my patches here, right?

[PATCH v2 1/5] migration: Fix possible overflow in vmstate_handle_alloc()
https://lore.kernel.org/r/[email protected]
(I'll still respin with the rest)

[PATCH] vhost/migration: Fix incorrect size used in inflight->addr in VMSD
https://lore.kernel.org/r/[email protected]

The only missing piece would be an multiply overflow check in vmstate_handle_alloc(), if you could add that check too while rewritting that in patch 1 then I think it'll cover all.

Quoted 5 lines

Vladimir's ask in the separate email makes sense: I wonder if we can also do one step further and merge VBUFFER into VARRAY.

The other trivial thing is, while looking, I found one trivial macro VMSTATE_PARTIAL_VBUFFER not used; can drop it altogether.

Now officially declare support for u64 on all these offsets, we also need to double check on our alignment with security issues. Similar reports will not be a bug anymore but results will be the same I
assume: it's anything the attacker can feed a u64 directly (instead of an int32_t negative overflow), result is still failing a malloc() with enormously large numbers, legally this time.

Quoted 6 lines

Do you still plan to work on finding per-user upper limit or whatever of that kind? I'd say time spent working on series like this worths more than that, but I still want to check with you while looking at this solution.

I suppose with this, one option is we can close all tickets reporting security issues while allocating with all u64 fields.

All issues where migration stream is malformed aren't security issues according to the current policy.

Whether to close these or use them as a starting point in research, is up to you guys.

Quoted 5 lines

Thanks,

Signature · 1 line
Peter Xu
#23 lore raw
In reply to Peter Xu
Quoted 53 lines · Peter Xu
Quoted 46 lines · Peter Xu
Quoted 19 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Quoted 5 lines

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

Nice work!

I think I was only looking at VBUFFER side and I thought it was fine sticking with 32bit even signed or not, not a huge deal. But cleaning up VARRAY whole thing together looks definitely an improvement. I definitely like your version here.

I assume with your series I can drop both of my patches here, right?

[PATCH v2 1/5] migration: Fix possible overflow in vmstate_handle_alloc()
https://lore.kernel.org/r/[email protected]
(I'll still respin with the rest)

[PATCH] vhost/migration: Fix incorrect size used in inflight->addr in VMSD
https://lore.kernel.org/r/[email protected]

The only missing piece would be an multiply overflow check in vmstate_handle_alloc(), if you could add that check too while rewritting that in patch 1 then I think it'll cover all.

Quoted 5 lines

Vladimir's ask in the separate email makes sense: I wonder if we can also do one step further and merge VBUFFER into VARRAY.

The other trivial thing is, while looking, I found one trivial macro VMSTATE_PARTIAL_VBUFFER not used; can drop it altogether.

Quoted 8 lines

Now officially declare support for u64 on all these offsets, we also need to double check on our alignment with security issues.

Similar reports will not be a bug anymore but results will be the same I
assume: it's anything the attacker can feed a u64 directly (instead of an int32_t negative overflow), result is still failing a malloc() with enormously large numbers, legally this time.

Sprinkle some try_alloc maybe? Shouldn't be just a few from the migration code itself.

Quoted 4 lines

Do you still plan to work on finding per-user upper limit or whatever of that kind? I'd say time spent working on series like this worths more than that, but I still want to check with you while looking at this solution.

I thought of some things for the future, but I don't have anything concrete, only experiments:

1) For all vmstate macros that take an unbounded size, length, etc, add a new variant:

VMSTATE_FOO_BAR_V()
+VMSTATE_FOO_BAR_V_BOUNDED()

And allow the vmstate writer to provide a "Bounds" object (format to be defined) and the vmstate core would just use that if it's there and that's it. I still think that's a functionality the migration code should have in its API.

2) Allowing a transfer limit in bytes to be set in QEMUFile, which can be adjusted at will, maybe using some context-manager-like construct:

WITH_RX_CAP(f, 1024) {
    qemu_get_byte();
    qemu_get_byte();
    vmstate_load_vmsd()
    qemu_get_buf();
    ...
}

I'm still not sure how useful these would be.

Quoted 4 lines

I suppose with this, one option is we can close all tickets reporting security issues while allocating with all u64 fields.

Thanks,

#24 lore raw
Quoted 58 lines · Fabiano Rosas
Quoted 56 lines · Peter Xu
Quoted 46 lines · Peter Xu
Quoted 19 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Quoted 5 lines

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

Nice work!

I think I was only looking at VBUFFER side and I thought it was fine sticking with 32bit even signed or not, not a huge deal. But cleaning up VARRAY whole thing together looks definitely an improvement. I definitely like your version here.

I assume with your series I can drop both of my patches here, right?

[PATCH v2 1/5] migration: Fix possible overflow in vmstate_handle_alloc()
https://lore.kernel.org/r/[email protected]
(I'll still respin with the rest)

[PATCH] vhost/migration: Fix incorrect size used in inflight->addr in VMSD
https://lore.kernel.org/r/[email protected]

The only missing piece would be an multiply overflow check in vmstate_handle_alloc(), if you could add that check too while rewritting that in patch 1 then I think it'll cover all.

Quoted 5 lines

Vladimir's ask in the separate email makes sense: I wonder if we can also do one step further and merge VBUFFER into VARRAY.

The other trivial thing is, while looking, I found one trivial macro VMSTATE_PARTIAL_VBUFFER not used; can drop it altogether.

Now officially declare support for u64 on all these offsets, we also need to double check on our alignment with security issues.

Quoted 8 lines
Quoted 5 lines

Similar reports will not be a bug anymore but results will be the same I
assume: it's anything the attacker can feed a u64 directly (instead of an int32_t negative overflow), result is still failing a malloc() with enormously large numbers, legally this time.

Sprinkle some try_alloc maybe? Shouldn't be just a few from the migration code itself.

It's a matter of how helpful is it to fail slightly more gracefully, v.s. malloc()'s error message.

Quoted 12 lines
Quoted 5 lines

Do you still plan to work on finding per-user upper limit or whatever of that kind? I'd say time spent working on series like this worths more than that, but I still want to check with you while looking at this solution.

I thought of some things for the future, but I don't have anything concrete, only experiments:

1) For all vmstate macros that take an unbounded size, length, etc, add a new variant:

Quoted 7 lines

VMSTATE_FOO_BAR_V()
+VMSTATE_FOO_BAR_V_BOUNDED()

And allow the vmstate writer to provide a "Bounds" object (format to be defined) and the vmstate core would just use that if it's there and that's it. I still think that's a functionality the migration code should have in its API.

Something relevant I just sent:

https://lore.kernel.org/r/[email protected]

Based on that, IMHO what we need in a more useful way is, again taking gtree example, a wrapper to g_tree_new_full() but allow specifying a size.. then gracefully allow failure to happen.

Maybe it's not easy to wrap it to make it graceful and generic, the whole point is for the existing unlimited resources problem with migration we should limit it from the user before migration even starts.

Quoted 19 lines

2) Allowing a transfer limit in bytes to be set in QEMUFile, which can be adjusted at will, maybe using some context-manager-like construct:

WITH_RX_CAP(f, 1024) {
    qemu_get_byte();
    qemu_get_byte();
    vmstate_load_vmsd()
    qemu_get_buf();
    ...
}

I'm still not sure how useful these would be.

Quoted 4 lines

I suppose with this, one option is we can close all tickets reporting security issues while allocating with all u64 fields.

Thanks,

Signature · 1 line
Peter Xu
#25 lore raw
In reply to Peter Xu
Quoted 46 lines · Peter Xu
Quoted 19 lines · Fabiano Rosas

Hi, this is basically what I ranted about in:
https://lore.kernel.org/r/[email protected]

I'm replacing the per-integer-size type checks with a single "int that fits in 32bit" check. This allows several lines of duplicated code to be removed.

I haven't changed the macro names in the device code yet. If this series gets positive feedback then I'll send per-subsystem patches doing that.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/2716814081
Also tested:
- migration-test --full --thorough
- x86_64 compat run forwards and backwards for previous 3 QEMU releases
- s390x compat run forwards and backwards for previous 2 QEMU releases
- ppc64 compat run forwards and backwards for previous QEMU release
- migration-test smoke ASAN/UBSAN run

Quoted 5 lines

Fabiano Rosas (4):
migration: Remove VMSTATE_ARRAY_INT32_UNSAFE
migration: Introduce VMStateOffset
migration: Remove redundant flags
migration: Remove duplicate vmstate macros

Nice work!

I think I was only looking at VBUFFER side and I thought it was fine sticking with 32bit even signed or not, not a huge deal. But cleaning up VARRAY whole thing together looks definitely an improvement. I definitely like your version here.

I assume with your series I can drop both of my patches here, right?

[PATCH v2 1/5] migration: Fix possible overflow in vmstate_handle_alloc()
https://lore.kernel.org/r/[email protected]
(I'll still respin with the rest)

[PATCH] vhost/migration: Fix incorrect size used in inflight->addr in VMSD
https://lore.kernel.org/r/[email protected]

The only missing piece would be an multiply overflow check in vmstate_handle_alloc(), if you could add that check too while rewritting that in patch 1 then I think it'll cover all.

Quoted 5 lines

Vladimir's ask in the separate email makes sense: I wonder if we can also do one step further and merge VBUFFER into VARRAY.

The other trivial thing is, while looking, I found one trivial macro VMSTATE_PARTIAL_VBUFFER not used; can drop it altogether.

I can look at it all. Just a heads-up, I'll be off for a couple of weeks starting tomorrow.

#26 lore raw
Quoted 2 lines · Fabiano Rosas

I can look at it all. Just a heads-up, I'll be off for a couple of weeks starting tomorrow.

Thanks. I think it's fine we resolve this after your back, as long as Michael is ok with it from vhost side. It's not real CVE, so I hope it's OK.

Signature · 1 line
Peter Xu