All of lore.kernel.org
 help / color / mirror / Atom feed
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 2/9] rust/hpet: Move vmstate_validate!() check into post_load
Date: Thu, 26 Nov 2026 18:16:24 -0500	[thread overview]
Message-ID: <20261126231631.3392756-3-peterx@redhat.com> (raw)
In-Reply-To: <20261126231631.3392756-1-peterx@redhat.com>

From: Zhao Liu <zhao1.liu@intel.com>

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).

Originally-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20261124095814.437738-3-zhao1.liu@intel.com
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 rust/hw/timer/hpet/src/device.rs | 45 ++++++++++++++++----------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 89ba7a5c4d..c7a2b7f6c2 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -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);
-- 
2.56.0



  parent reply	other threads:[~2026-11-26 23:17 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 ` [PATCH for-12.0 v2 1/9] rust/migration: Allow usize as the length of a varray Peter Xu
2026-11-26 23:16 ` Peter Xu [this message]
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-3-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.