From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Fabiano Rosas <farosas@suse.de>,
peterx@redhat.com,
Manos Pitsidianakis <manos.pitsidianakis@linaro.org>,
Zhao Liu <zhao1.liu@intel.com>,
Paolo Bonzini <pbonzini@redhat.com>,
qemu-rust@nongnu.org
Subject: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
Date: Fri, 20 Nov 2026 07:49:22 -0500 [thread overview]
Message-ID: <20261120124923.1656220-6-peterx@redhat.com> (raw)
In-Reply-To: <20261120124923.1656220-1-peterx@redhat.com>
We dropped all of that in C, drop that in Rust too. The goal is to remove
the VMSTATE_VALIDATE() together with VMS_MUST_EXIST.
Use post_load instead for the check.
Cc: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Cc: Zhao Liu <zhao1.liu@intel.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-rust@nongnu.org
Signed-off-by: Peter Xu <peterx@redhat.com>
---
rust/hw/timer/hpet/src/device.rs | 9 ++--
rust/migration/src/prelude.rs | 1 -
rust/migration/src/vmstate.rs | 17 -------
rust/tests/tests/vmstate_tests.rs | 77 -------------------------------
4 files changed, 5 insertions(+), 99 deletions(-)
diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 89ba7a5c4d..da0540f528 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -934,10 +934,14 @@ fn pre_save(&self) -> Result<(), migration::Infallible> {
Ok(())
}
- fn post_load(&self, _version_id: u8) -> Result<(), migration::Infallible> {
+ fn post_load(&self, version_id: u8) -> Result<(), migration::InvalidError> {
let mut regs = self.regs.borrow_mut();
let cnt = regs.counter;
+ if !self.validate_num_timers(version_id) {
+ return Err(migration::InvalidError);
+ }
+
for tn_regs in regs.tn_regs.iter_mut().take(self.num_timers) {
tn_regs.update_cmp64(cnt);
tn_regs.last = CLOCK_VIRTUAL.get_ns() - NANOSECONDS_PER_SECOND;
@@ -1082,8 +1086,6 @@ fn restore_migrated_state(
impl_vmstate_struct!(HPETTimerMigration, VMSTATE_HPET_TIMER);
-const VALIDATE_TIMERS_NAME: &CStr = c"num_timers must match";
-
// HPETRegistersMigration is generated by ToMigrationState macro.
impl_vmstate_struct!(
HPETRegistersMigration,
@@ -1109,7 +1111,6 @@ fn restore_migrated_state(
.fields(vmstate_fields! {
vmstate_of!(HPETState, regs),
vmstate_of!(HPETState, num_timers_save),
- vmstate_validate!(HPETState, VALIDATE_TIMERS_NAME, HPETState::validate_num_timers),
vmstate_of!(HPETState, timers[0 .. num_timers_save], HPETState::validate_num_timers).with_version_id(0),
})
.subsections(vmstate_subsections!(
diff --git a/rust/migration/src/prelude.rs b/rust/migration/src/prelude.rs
index 797e7ec59d..e2cbbda43e 100644
--- a/rust/migration/src/prelude.rs
+++ b/rust/migration/src/prelude.rs
@@ -16,4 +16,3 @@
pub use crate::vmstate_of;
pub use crate::vmstate_subsections;
pub use crate::vmstate_unused;
-pub use crate::vmstate_validate;
diff --git a/rust/migration/src/vmstate.rs b/rust/migration/src/vmstate.rs
index 5ed061f2ab..0c8b92f9d6 100644
--- a/rust/migration/src/vmstate.rs
+++ b/rust/migration/src/vmstate.rs
@@ -372,23 +372,6 @@ macro_rules! vmstate_fields {
}}
}
-#[doc(alias = "VMSTATE_VALIDATE")]
-#[macro_export]
-macro_rules! vmstate_validate {
- ($struct_name:ty, $test_name:expr, $test_fn:expr $(,)?) => {
- $crate::bindings::VMStateField {
- name: ::std::ffi::CStr::as_ptr($test_name),
- field_exists: $crate::vmstate_exist_fn!($struct_name, $test_fn),
- flags: $crate::bindings::VMStateFlags(
- $crate::bindings::VMStateFlags::VMS_MUST_EXIST.0
- | $crate::bindings::VMStateFlags::VMS_NO_STATE.0,
- ),
- num: 0, // 0 elements: no data, only run test_fn callback
- ..::common::zeroable::Zeroable::ZERO
- }
- };
-}
-
/// Helper macro to allow using a struct in [`vmstate_of!`]
///
/// # Safety
diff --git a/rust/tests/tests/vmstate_tests.rs b/rust/tests/tests/vmstate_tests.rs
index 6578d54b37..f4db02abac 100644
--- a/rust/tests/tests/vmstate_tests.rs
+++ b/rust/tests/tests/vmstate_tests.rs
@@ -406,80 +406,3 @@ fn test_vmstate_macro_array_of_pointer_wrapped() {
// The last VMStateField in VMSTATE_FOOC.
assert_eq!(foo_fields[4].flags, VMStateFlags::VMS_END);
}
-
-// =========================== Test VMSTATE_FOOD ===========================
-// Test the use cases of the vmstate macro, corresponding to the following C
-// macro variants:
-// * VMSTATE_FOOD:
-// - VMSTATE_VALIDATE
-
-// Add more member fields when vmstate_of support "test" parameter.
-struct FooD;
-
-impl FooD {
- fn validate_food_0(&self, _version_id: u8) -> bool {
- true
- }
-
- fn validate_food_1(_state: &FooD, _version_id: u8) -> bool {
- false
- }
-}
-
-fn validate_food_2(_state: &FooD, _version_id: u8) -> bool {
- true
-}
-
-static VMSTATE_FOOD: VMStateDescription<FooD> = VMStateDescriptionBuilder::<FooD>::new()
- .name(c"foo_d")
- .version_id(3)
- .minimum_version_id(1)
- .fields(vmstate_fields! {
- vmstate_validate!(FooD, c"foo_d_0", FooD::validate_food_0),
- vmstate_validate!(FooD, c"foo_d_1", FooD::validate_food_1),
- vmstate_validate!(FooD, c"foo_d_2", validate_food_2),
- })
- .build();
-
-#[test]
-fn test_vmstate_validate() {
- let foo_fields: &[VMStateField] =
- unsafe { slice::from_raw_parts(VMSTATE_FOOD.as_ref().fields, 4) };
- let mut foo_d = FooD;
- let foo_d_p = std::ptr::addr_of_mut!(foo_d).cast::<c_void>();
-
- // 1st VMStateField in VMSTATE_FOOD
- assert_eq!(
- unsafe { CStr::from_ptr(foo_fields[0].name) }.to_bytes_with_nul(),
- b"foo_d_0\0"
- );
- assert_eq!(foo_fields[0].offset, 0);
- assert_eq!(foo_fields[0].num_indirect.size, 0);
- assert!(foo_fields[0].info.is_null());
- assert_eq!(foo_fields[0].version_id, 0);
- assert_eq!(foo_fields[0].size, 0);
- assert_eq!(foo_fields[0].num, 0);
- assert_eq!(
- foo_fields[0].flags.0,
- VMStateFlags::VMS_NO_STATE.0 | VMStateFlags::VMS_MUST_EXIST.0
- );
- assert!(foo_fields[0].vmsd.is_null());
- assert!(unsafe { foo_fields[0].field_exists.unwrap()(foo_d_p, 0) });
-
- // 2nd VMStateField in VMSTATE_FOOD
- assert_eq!(
- unsafe { CStr::from_ptr(foo_fields[1].name) }.to_bytes_with_nul(),
- b"foo_d_1\0"
- );
- assert!(!unsafe { foo_fields[1].field_exists.unwrap()(foo_d_p, 1) });
-
- // 3rd VMStateField in VMSTATE_FOOD
- assert_eq!(
- unsafe { CStr::from_ptr(foo_fields[2].name) }.to_bytes_with_nul(),
- b"foo_d_2\0"
- );
- assert!(unsafe { foo_fields[2].field_exists.unwrap()(foo_d_p, 2) });
-
- // The last VMStateField in VMSTATE_FOOD.
- assert_eq!(foo_fields[3].flags, VMStateFlags::VMS_END);
-}
--
2.56.0
next prev parent reply other threads:[~2026-11-20 12:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 1/6] pcie/aer: Remove the usage of VMSTATE_VALIDATE() Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 2/6] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp() Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 3/6] hpet: Move VMSTATE_VALIDATE() check into post_load Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 4/6] arm/machine: Move all VMSTATE_VALIDATE() usages " Peter Xu
2026-11-20 12:49 ` Peter Xu [this message]
2026-11-20 14:25 ` [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load Zhao Liu
2026-11-20 15:43 ` Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 6/6] migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
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=20261120124923.1656220-6-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=farosas@suse.de \
--cc=manos.pitsidianakis@linaro.org \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@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.