From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: Juraj Marcin <jmarcin@redhat.com>,
Peter Maydell <peter.maydell@linaro.org>,
peterx@redhat.com, Fabiano Rosas <farosas@suse.de>,
Gavin Shan <gshan@redhat.com>, Zhao Liu <zhao1.liu@intel.com>
Subject: [PATCH for-12.0 v2 1/9] rust/migration: Allow usize as the length of a varray
Date: Thu, 26 Nov 2026 18:16:23 -0500 [thread overview]
Message-ID: <20261126231631.3392756-2-peterx@redhat.com> (raw)
In-Reply-To: <20261126231631.3392756-1-peterx@redhat.com>
From: Zhao Liu <zhao1.liu@intel.com>
Since commit f9f70b50e3eb ("migration: Introduce VMStateStructMember")
and commit 1f0b40dccfe5 ("Rmigration: Remove redundant flags"), the
length of a varray is no longer tied to a specific integer type.
This makes it possible to use `usize` type for varray length. It's safe
since it is used only to resolve varray lengths in the local vmstate
code and is not used for migration.
Thus, implement a "dummy" VMState for `usize` so that it can provide
VARRAY_FLAG, and panic when anyone tries to define VMStateField with
`usize` type (including integers, pointers, [usize; N], Box<usize>,
BqlCell<usize>, etc).
In addition, add a test case for such case.
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20261124095814.437738-2-zhao1.liu@intel.com
Signed-off-by: Peter Xu <peterx@redhat.com>
---
rust/migration/src/vmstate.rs | 16 ++++++++--
rust/tests/tests/vmstate_tests.rs | 49 +++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 2 deletions(-)
diff --git a/rust/migration/src/vmstate.rs b/rust/migration/src/vmstate.rs
index 5ed061f2ab..0b05db4e70 100644
--- a/rust/migration/src/vmstate.rs
+++ b/rust/migration/src/vmstate.rs
@@ -97,8 +97,8 @@ pub unsafe trait VMState {
/// A flag that is added to another field's `VMStateField` to specify the
/// length's type in a variable-sized array. If this is not a supported
- /// type for the length (i.e. if it is not `u8`, `u16`, `u32`), using it
- /// in a call to [`vmstate_of!`](crate::vmstate_of) will cause a
+ /// type for the length (i.e. if it is not `u8`, `u16`, `u32`, `usize`),
+ /// using it in a call to [`vmstate_of!`](crate::vmstate_of) will cause a
/// compile-time error.
#[doc(hidden)] // https://github.com/rust-lang/rust/issues/149635
const VARRAY_FLAG: VMStateFlags = {
@@ -256,6 +256,18 @@ unsafe impl $crate::vmstate::VMState for $type {
impl_vmstate_scalar!(vmstate_info_uint64, u64);
impl_vmstate_scalar!(vmstate_info_timer, util::timer::Timer);
+// SAFETY: BASE is never produced (any use of a usize as migrated data will
+// always result in a compile-time error). VARRAY_FLAG only tells the C code to
+// read `size_of::<usize>()` bytes (see `size_of_field_type!`) at the length's
+// offset.
+unsafe impl VMState for usize {
+ #[doc(hidden)] // https://github.com/rust-lang/rust/issues/149635
+ const BASE: VMStateField = {
+ panic!("usize cannot be migrated, use a fixed-size integer type");
+ };
+ const VARRAY_FLAG: VMStateFlags = VMStateFlags::VMS_VARRAY;
+}
+
#[macro_export]
macro_rules! impl_vmstate_c_struct {
($type:ty, $vmsd:expr) => {
diff --git a/rust/tests/tests/vmstate_tests.rs b/rust/tests/tests/vmstate_tests.rs
index 6578d54b37..b680cfd3c7 100644
--- a/rust/tests/tests/vmstate_tests.rs
+++ b/rust/tests/tests/vmstate_tests.rs
@@ -483,3 +483,52 @@ fn test_vmstate_validate() {
// The last VMStateField in VMSTATE_FOOD.
assert_eq!(foo_fields[3].flags, VMStateFlags::VMS_END);
}
+
+// =========================== Test VMSTATE_FOOE ===========================
+// Test the use cases of the vmstate macro, corresponding to the following C
+// macro variants:
+// * VMSTATE_FOOE:
+// - VMSTATE_VARRAY with a `usize` length
+#[repr(C)]
+#[derive(Default)]
+struct FooE {
+ arr: [u8; FOO_ARRAY_MAX],
+ num: usize,
+}
+
+static VMSTATE_FOOE: VMStateDescription<FooE> = VMStateDescriptionBuilder::<FooE>::new()
+ .name(c"foo_e")
+ .version_id(1)
+ .minimum_version_id(1)
+ .fields(vmstate_fields! {
+ vmstate_of!(FooE, arr[0 .. num]),
+ })
+ .build();
+
+#[test]
+fn test_vmstate_varray_usize() {
+ let foo_fields: &[VMStateField] =
+ unsafe { slice::from_raw_parts(VMSTATE_FOOE.as_ref().fields, 2) };
+
+ // 1st VMStateField ("arr") in VMSTATE_FOOE
+ assert_eq!(
+ unsafe { CStr::from_ptr(foo_fields[0].name) }.to_bytes_with_nul(),
+ b"arr\0"
+ );
+ assert_eq!(foo_fields[0].offset, 0);
+ assert_eq!(
+ foo_fields[0].num_indirect.offset,
+ std::mem::offset_of!(FooE, num) as u32
+ );
+ assert_eq!(foo_fields[0].num_indirect.size, size_of::<usize>() as u8);
+ assert_eq!(foo_fields[0].info, unsafe { &vmstate_info_uint8 });
+ assert_eq!(foo_fields[0].version_id, 0);
+ assert_eq!(foo_fields[0].size, 1);
+ assert_eq!(foo_fields[0].num, 0);
+ assert_eq!(foo_fields[0].flags, VMStateFlags::VMS_VARRAY);
+ assert!(foo_fields[0].vmsd.is_null());
+ assert!(foo_fields[0].field_exists.is_none());
+
+ // The last VMStateField in VMSTATE_FOOE.
+ assert_eq!(foo_fields[1].flags, VMStateFlags::VMS_END);
+}
--
2.56.0
next prev parent reply other threads:[~2026-11-26 23:18 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 23:16 [PATCH for-12.0 v2 0/9] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
2026-11-26 23:16 ` Peter Xu [this message]
2026-11-26 23:16 ` [PATCH for-12.0 v2 2/9] rust/hpet: Move vmstate_validate!() check into post_load Peter Xu
2026-11-26 23:16 ` [PATCH for-12.0 v2 3/9] rust/migration: Remove vmstate_validate!() macro and test Peter Xu
2026-11-26 23:16 ` [PATCH for-12.0 v2 4/9] pcie/aer: Remove the usage of VMSTATE_VALIDATE() Peter Xu
2026-11-26 23:16 ` [PATCH for-12.0 v2 5/9] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp() Peter Xu
2026-11-26 23:16 ` [PATCH for-12.0 v2 6/9] hpet: Move VMSTATE_VALIDATE() check into post_load Peter Xu
2026-11-26 23:16 ` [PATCH for-12.0 v2 7/9] arm/machine: Convert cpu_post_load() to take Error** Peter Xu
2026-11-27 13:22 ` Fabiano Rosas
2026-11-27 14:23 ` Peter Xu
2026-11-27 14:44 ` Peter Maydell
2026-11-27 17:00 ` Eric Auger
2026-11-26 23:16 ` [PATCH for-12.0 v2 8/9] arm/machine: Move all VMSTATE_VALIDATE() usages into post_load Peter Xu
2026-11-27 13:41 ` Fabiano Rosas
2026-11-27 13:50 ` Peter Maydell
2026-11-27 14:29 ` Peter Xu
2026-11-26 23:16 ` [PATCH for-12.0 v2 9/9] migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
2026-11-27 14:16 ` Fabiano Rosas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126231631.3392756-2-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=farosas@suse.de \
--cc=gshan@redhat.com \
--cc=jmarcin@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.