From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com
Subject: [PATCH 4/5] rust: migration: implement ToMigrationState for Timer
Date: Mon, 17 Nov 2025 09:47:51 +0100 [thread overview]
Message-ID: <20251117084752.203219-5-pbonzini@redhat.com> (raw)
In-Reply-To: <20251117084752.203219-1-pbonzini@redhat.com>
Timer is a complex struct, allow adding it to a struct that
uses #[derive(ToMigrationState)]; similar to vmstate_timer, only
the expiration time has to be preserved.
In fact, because it is thread-safe, ToMigrationStateShared can
also be implemented without needing a cell or mutex that wraps
the timer.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/timer/hpet/src/device.rs | 1 -
rust/migration/src/migratable.rs | 31 +++++++++++++++++++++++++++++++
rust/util/src/timer.rs | 10 +++++++++-
3 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 5bcf151a680..373ec37bbd3 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -250,7 +250,6 @@ const fn get_individual_route(&self) -> usize {
}
/// HPET Timer Abstraction
-#[repr(C)]
#[derive(Debug)]
pub struct HPETTimer {
/// timer N index within the timer block (`HPETState`)
diff --git a/rust/migration/src/migratable.rs b/rust/migration/src/migratable.rs
index 02efe31d72c..c82a6b9a7cf 100644
--- a/rust/migration/src/migratable.rs
+++ b/rust/migration/src/migratable.rs
@@ -140,6 +140,26 @@ fn restore_migrated_state_mut(
impl_for_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, bool);
+impl ToMigrationState for util::timer::Timer {
+ type Migrated = i64;
+
+ fn snapshot_migration_state(&self, target: &mut i64) -> Result<(), InvalidError> {
+ // SAFETY: as_ptr() is unsafe to ensure that the caller reasons about
+ // the pinning of the data inside the Opaque<>. Here all we do is
+ // access a field.
+ *target = self.expire_time_ns().unwrap_or(-1);
+ Ok(())
+ }
+
+ fn restore_migrated_state_mut(
+ &mut self,
+ source: Self::Migrated,
+ version_id: u8,
+ ) -> Result<(), InvalidError> {
+ self.restore_migrated_state(source, version_id)
+ }
+}
+
impl<T: ToMigrationState, const N: usize> ToMigrationState for [T; N]
where
[T::Migrated; N]: Default,
@@ -237,6 +257,17 @@ fn restore_migrated_state(
) -> Result<(), InvalidError>;
}
+impl ToMigrationStateShared for util::timer::Timer {
+ fn restore_migrated_state(&self, source: i64, _version_id: u8) -> Result<(), InvalidError> {
+ if source >= 0 {
+ self.modify(source as u64);
+ } else {
+ self.delete();
+ }
+ Ok(())
+ }
+}
+
impl<T: ToMigrationStateShared, const N: usize> ToMigrationStateShared for [T; N]
where
[T::Migrated; N]: Default,
diff --git a/rust/util/src/timer.rs b/rust/util/src/timer.rs
index 829f52d111e..4109d84c398 100644
--- a/rust/util/src/timer.rs
+++ b/rust/util/src/timer.rs
@@ -10,7 +10,8 @@
use common::{callbacks::FnCall, Opaque};
use crate::bindings::{
- self, qemu_clock_get_ns, timer_del, timer_init_full, timer_mod, QEMUClockType,
+ self, qemu_clock_get_ns, timer_del, timer_expire_time_ns, timer_init_full, timer_mod,
+ QEMUClockType,
};
/// A safe wrapper around [`bindings::QEMUTimer`].
@@ -88,6 +89,13 @@ pub fn init_full<T, F>(
}
}
+ pub fn expire_time_ns(&self) -> Option<i64> {
+ // SAFETY: the only way to obtain a Timer safely is via methods that
+ // take a Pin<&mut Self>, therefore the timer is pinned
+ let ret = unsafe { timer_expire_time_ns(self.as_ptr()) };
+ i64::try_from(ret).ok()
+ }
+
pub fn modify(&self, expire_time: u64) {
// SAFETY: the only way to obtain a Timer safely is via methods that
// take a Pin<&mut Self>, therefore the timer is pinned
--
2.51.1
next prev parent reply other threads:[~2025-11-17 8:48 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 8:47 [PATCH 0/5] rust/hpet: complete moving state out of HPETTimer Paolo Bonzini
2025-11-17 8:47 ` [PATCH 1/5] rust/hpet: move hidden registers to HPETTimerRegisters Paolo Bonzini
2025-11-18 8:35 ` Zhao Liu
2025-11-17 8:47 ` [PATCH 2/5] rust/hpet: move hpet_offset to HPETRegisters Paolo Bonzini
2025-11-18 13:54 ` Zhao Liu
2025-11-17 8:47 ` [PATCH 3/5] rust/hpet: remove BqlRefCell around HPETTimer Paolo Bonzini
2025-11-19 15:17 ` Zhao Liu
2025-11-19 22:28 ` Paolo Bonzini
2025-11-17 8:47 ` Paolo Bonzini [this message]
2025-11-20 14:31 ` [PATCH 4/5] rust: migration: implement ToMigrationState for Timer Zhao Liu
2025-11-17 8:47 ` [PATCH 5/5] rust/hpet: Apply Migratable<> wrapper and ToMigrationState Paolo Bonzini
2025-11-19 15:31 ` Zhao Liu
2025-11-19 15:59 ` [PATCH 0/5] rust/hpet: complete moving state out of HPETTimer Zhao Liu
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=20251117084752.203219-5-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).