Skip to contentSkip to conversation

rust/migration: Remove vmstate_validate!() and VMS_MUST_EXIST

PATCH In review Zhao Liu 3 participants 7 messages Nov 24 – Nov 25 Rust

lore
Share & tools
lore

Zhao Liu

author
#0 lore raw

cover rust/migration: Remove vmstate_validate!() and VMS_MUST_EXIST

Hi,

This simple series is based on Peter Xu's series [*] and mainly refines patch 5 — removing vmstate_validate!() from Rust HPET (while maintaining consistency logic with C HPET as much as possible).

I originally planned to use a single patch, but breaking it up this way makes it easier to review. I suppose these three small patches could also replace Peter's patch 5 :-).

There is also some cleanup work left (e.g., support post_load_errp() in Rust); I will address this in some separate patch later (without blocking the current effort to remove vmstate_validate!()).

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

Thanks and Best Regards,
Zhao
---
Zhao Liu (3):
rust/migration: Allow usize as the length of a varray rust/hpet: Move vmstate_validate!() check into post_load rust/migration: Remove vmstate_validate!() macro and test

 rust/hw/timer/hpet/src/device.rs  | 45 +++++++++----------
 rust/migration/src/prelude.rs     |  1 -
 rust/migration/src/vmstate.rs     | 33 ++++++--------
 rust/tests/tests/vmstate_tests.rs | 72 ++++++++++---------------------
 4 files changed, 59 insertions(+), 92 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 rust/migration: Allow usize as the length of a varray

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.

---
 rust/migration/src/vmstate.rs     | 16 ++++++++--
 rust/tests/tests/vmstate_tests.rs | 49 +++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)
rust/migration/src/vmstate.rs +14 -2
index 5ed061f2abb6..0b05db4e7049 100644
@@ -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) => {
rust/tests/tests/vmstate_tests.rs +49 -0

52 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/3 rust/hpet: Move vmstate_validate!() check into post_load

In reply to Zhao Liu

To pave way for removing VMSTATE_VALIDATE() (and vmstate_validate!()), move the usage of hpet into post_load. To make it safer, loading the timers with the num_timers, then only check against the wire number at last.

In addition, drop the test_fn() in the VMStateField of HPETState.timers
- this was duplicated, but now the check in post_load is enough (and test_fn should not be used for validation).

---
Copy Peter's commit message from his C HPET patch because now this patch has the (almost) same logic.

---
 rust/hw/timer/hpet/src/device.rs | 45 ++++++++++++++++----------------
 1 file changed, 23 insertions(+), 22 deletions(-)
rust/hw/timer/hpet/src/device.rs +23 -22
index 89ba7a5c4d21..c7a2b7f6c2ec 100644
@@ -934,10 +934,15 @@ 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;
 
+        /* FIXME: use post_load_errp() to print error message. */
+        if !self.validate_num_timers() {
+            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;
@@ -956,7 +961,7 @@ fn is_offset_needed(&self) -> bool {
         self.regs.borrow().is_hpet_enabled()
     }
 
-    fn validate_num_timers(&self, _version_id: u8) -> bool {
+    fn validate_num_timers(&self) -> bool {
         self.num_timers == self.num_timers_save.get().into()
     }
 }
@@ -1082,8 +1087,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,
@@ -1099,24 +1102,22 @@ fn restore_migrated_state(
         .build()
 );
 
-const VMSTATE_HPET: VMStateDescription<HPETState> =
-    VMStateDescriptionBuilder::<HPETState>::new()
-        .name(c"hpet")
-        .version_id(2)
-        .minimum_version_id(2)
-        .pre_save(&HPETState::pre_save)
-        .post_load(&HPETState::post_load)
-        .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!(
-            VMSTATE_HPET_RTC_IRQ_LEVEL,
-            VMSTATE_HPET_OFFSET,
-        ))
-        .build();
+const VMSTATE_HPET: VMStateDescription<HPETState> = VMStateDescriptionBuilder::<HPETState>::new()
+    .name(c"hpet")
+    .version_id(2)
+    .minimum_version_id(2)
+    .pre_save(&HPETState::pre_save)
+    .post_load(&HPETState::post_load)
+    .fields(vmstate_fields! {
+        vmstate_of!(HPETState, regs),
+        vmstate_of!(HPETState, num_timers_save),
+        vmstate_of!(HPETState, timers[0 .. num_timers]).with_version_id(0),
+    })
+    .subsections(vmstate_subsections!(
+        VMSTATE_HPET_RTC_IRQ_LEVEL,
+        VMSTATE_HPET_OFFSET,
+    ))
+    .build();
 
 impl DeviceImpl for HPETState {
     const VMSTATE: Option<VMStateDescription<Self>> = Some(VMSTATE_HPET);
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH 2/3] rust/hpet: Move vmstate_validate!() check into post_load

Quoted 43 lines · Zhao Liu

To pave way for removing VMSTATE_VALIDATE() (and vmstate_validate!()), move the usage of hpet into post_load. To make it safer, loading the timers with the num_timers, then only check against the wire number at last.

In addition, drop the test_fn() in the VMStateField of HPETState.timers
- this was duplicated, but now the check in post_load is enough (and test_fn should not be used for validation).

---
Copy Peter's commit message from his C HPET patch because now this patch has the (almost) same logic.

---
  rust/hw/timer/hpet/src/device.rs | 45 ++++++++++++++++----------------
  1 file changed, 23 insertions(+), 22 deletions(-)
rust/hw/timer/hpet/src/device.rs +6 -18
-const VMSTATE_HPET: VMStateDescription<HPETState> =
-    VMStateDescriptionBuilder::<HPETState>::new()
-        .name(c"hpet")
-        .version_id(2)
-        .minimum_version_id(2)
-        .pre_save(&HPETState::pre_save)
-        .post_load(&HPETState::post_load)
-        .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!(
-            VMSTATE_HPET_RTC_IRQ_LEVEL,
-            VMSTATE_HPET_OFFSET,
-        ))
-        .build();
+const VMSTATE_HPET: VMStateDescription<HPETState> = VMStateDescriptionBuilder::<HPETState>::new()
+    .name(c"hpet")
+    .version_id(2)
+    .minimum_version_id(2)
+    .pre_save(&HPETState::pre_save)
+    .post_load(&HPETState::post_load)

rust/hw/timer/hpet/src/device.rs fn restore_migrated_state( · line 1120

Quoted 10 lines
rust/hw/timer/hpet/src/device.rs +10 -0
+    .fields(vmstate_fields! {
+        vmstate_of!(HPETState, regs),
+        vmstate_of!(HPETState, num_timers_save),
+        vmstate_of!(HPETState, timers[0 .. num_timers]).with_version_id(0),
+    })
+    .subsections(vmstate_subsections!(
+        VMSTATE_HPET_RTC_IRQ_LEVEL,
+        VMSTATE_HPET_OFFSET,
+    ))
+    .build();

Doing the indentation change in a preliminary patch would ease review.

author
#4 lore raw

Re: [PATCH 2/3] rust/hpet: Move vmstate_validate!() check into post_load

Quoted 28 lines
Quoted 28 lines
rust/hw/timer/hpet/src/device.rs +10 -18
-const VMSTATE_HPET: VMStateDescription<HPETState> =
-    VMStateDescriptionBuilder::<HPETState>::new()
-        .name(c"hpet")
-        .version_id(2)
-        .minimum_version_id(2)
-        .pre_save(&HPETState::pre_save)
-        .post_load(&HPETState::post_load)
-        .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!(
-            VMSTATE_HPET_RTC_IRQ_LEVEL,
-            VMSTATE_HPET_OFFSET,
-        ))
-        .build();
+const VMSTATE_HPET: VMStateDescription<HPETState> = VMStateDescriptionBuilder::<HPETState>::new()
+    .name(c"hpet")
+    .version_id(2)
+    .minimum_version_id(2)
+    .pre_save(&HPETState::pre_save)
+    .post_load(&HPETState::post_load)
+    .fields(vmstate_fields! {
+        vmstate_of!(HPETState, regs),
+        vmstate_of!(HPETState, num_timers_save),
+        vmstate_of!(HPETState, timers[0 .. num_timers]).with_version_id(0),
Quoted 8 lines

rust/hw/timer/hpet/src/device.rs fn restore_migrated_state( · line 1120

Quoted 6 lines
rust/hw/timer/hpet/src/device.rs +6 -0
+    })
+    .subsections(vmstate_subsections!(
+        VMSTATE_HPET_RTC_IRQ_LEVEL,
+        VMSTATE_HPET_OFFSET,
+    ))
+    .build();

Doing the indentation change in a preliminary patch would ease review.

Yeah, but this style change was from rustfmt's complaints about this patch, so I've merged rustfmt's changes into this patch as well... this ensures this individual patch could pass all checks.

Thanks,
Zhao

author
#5 lore raw

PATCH 3/3 rust/migration: Remove vmstate_validate!() macro and test

In reply to Zhao Liu

We have moved all users (in Rust side) to other API, now they're never used.

After the related test (originally FOOD) was removed, rename FOOE to new FOOD test.

---
 rust/migration/src/prelude.rs     |  1 -
 rust/migration/src/vmstate.rs     | 17 ------
 rust/tests/tests/vmstate_tests.rs | 93 +++----------------------------
 3 files changed, 8 insertions(+), 103 deletions(-)
rust/migration/src/prelude.rs +0 -1
index 797e7ec59d1b..e2cbbda43e0a 100644
@@ -16,4 +16,3 @@
 pub use crate::vmstate_of;
 pub use crate::vmstate_subsections;
 pub use crate::vmstate_unused;
-pub use crate::vmstate_validate;
rust/migration/src/vmstate.rs +0 -17
index 0b05db4e7049..ec8b712cbf86 100644
@@ -384,23 +384,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
rust/tests/tests/vmstate_tests.rs +8 -85

127 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw
In reply to Zhao Liu
Quoted 10 lines · Zhao Liu

Hi,

This simple series is based on Peter Xu's series [*] and mainly refines patch 5 — removing vmstate_validate!() from Rust HPET (while maintaining consistency logic with C HPET as much as possible).

I originally planned to use a single patch, but breaking it up this way makes it easier to review. I suppose these three small patches could also replace Peter's patch 5 :-).

Quoted 5 lines

There is also some cleanup work left (e.g., support post_load_errp() in Rust); I will address this in some separate patch later (without blocking the current effort to remove vmstate_validate!()).

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

I'll carry this series too while reposting mine just to make it complete; feel free to repost / address comments concurrently from other Rust developers, we'll figure out how it lands.

Thanks again for sending!

Signature · 1 line
Peter Xu