Skip to contentSkip to conversation

rust: vmstate: improvements to varray handling

PATCH New Paolo Bonzini 1 participant 3 messages May 2, 2025 Rust

lore
Share & tools
lore
#0 lore raw

cover rust: vmstate: improvements to varray handling

Small improvements to vmstate. The second patch is a simpler replacement for https://lists.nongnu.org/archive/html/qemu-rust/2025-04/msg00018.html,
and is needed for HPET migration support.

I will include the remaining HPET migration code in the next pull request. I have done some more experiments on Builder, but vmstate_fields!{} requires the const_refs_static feature (which should not be a surprise...) and hence Rust 1.83.0. I will maintain the patches in my tree though.

Paolo Bonzini (2):
rust: vmstate: support varray for vmstate_clock!
rust: assertions: Support index field wrapped in BqlCell

 rust/qemu-api/src/assertions.rs | 25 +++++++++----------------
 rust/qemu-api/src/vmstate.rs    | 13 ++++++++++---
 2 files changed, 19 insertions(+), 19 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 rust: vmstate: support varray for vmstate_clock!

Make vmstate_struct and vmstate_clock more similar; they are basically the same thing, except for the clock case having a built-in VMStateDescription.

---
 rust/qemu-api/src/vmstate.rs | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
rust/qemu-api/src/vmstate.rs +10 -3
index 7d9f3a2ca6f..98152e5964d 100644
@@ -506,7 +506,7 @@ macro_rules! vmstate_struct {
 #[doc(alias = "VMSTATE_CLOCK")]
 #[macro_export]
 macro_rules! vmstate_clock {
-    ($struct_name:ty, $field_name:ident) => {{
+    ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(* $factor:expr)?])?) => {{
         $crate::bindings::VMStateField {
             name: ::core::concat!(::core::stringify!($field_name), "\0")
                 .as_bytes()
@@ -515,7 +515,7 @@ macro_rules! vmstate_clock {
                 $crate::assert_field_type!(
                     $struct_name,
                     $field_name,
-                    $crate::qom::Owned<$crate::qdev::Clock>
+                    $crate::qom::Owned<$crate::qdev::Clock> $(, num = $num)?
                 );
                 $crate::offset_of!($struct_name, $field_name)
             },
@@ -526,7 +526,14 @@ macro_rules! vmstate_clock {
             ),
             vmsd: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_clock) },
             ..$crate::zeroable::Zeroable::ZERO
-        }
+         } $(.with_varray_flag_unchecked(
+                  $crate::call_func_with_field!(
+                      $crate::vmstate::vmstate_varray_flag,
+                      $struct_name,
+                      $num
+                  )
+              )
+           $(.with_varray_multiply($factor))?)?
     }};
 }
 
Signature · 1 line
2.49.0
#2 lore raw

PATCH 2/2 rust: assertions: Support index field wrapped in BqlCell

In reply to Paolo Bonzini

Currently, if the num field of a varray is not a numeric type, such as being placed in a wrapper, the array variant of assert_field_type will fail the check.

HPET currently wraps num_timers in BqlCell<>. Although BqlCell<> is not necessary from strictly speaking, it makes sense for vmstate to respect BqlCell.

The failure of assert_field_type is because it cannot convert BqlCell<T> into usize for use as the index. Use a constant 0 instead for the index, by avoiding $(...)? and extracting the common parts of assert_field_type! into an internal case.

Commit message based on a patch by Zhao Liu <[email protected]>.

---
 rust/qemu-api/src/assertions.rs | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
rust/qemu-api/src/assertions.rs +9 -16
index eb12e9499a7..a2d38c877df 100644
@@ -78,33 +78,26 @@ fn types_must_be_equal<T, U>(_: T)
 /// ```
 #[macro_export]
 macro_rules! assert_field_type {
-    ($t:ty, $i:tt, $ti:ty) => {
+    (@internal $param_name:ident, $ti:ty, $t:ty, $($field:tt)*) => {
         const _: () = {
             #[allow(unused)]
-            fn assert_field_type(v: $t) {
-                fn types_must_be_equal<T, U>(_: T)
+            fn assert_field_type($param_name: &$t) {
+                fn types_must_be_equal<T, U>(_: &T)
                 where
                     T: $crate::assertions::EqType<Itself = U>,
                 {
                 }
-                types_must_be_equal::<_, $ti>(v.$i);
+                types_must_be_equal::<_, $ti>(&$($field)*);
             }
         };
     };
 
+    ($t:ty, $i:tt, $ti:ty) => {
+        $crate::assert_field_type!(@internal v, $ti, $t, v.$i);
+    };
+
     ($t:ty, $i:tt, $ti:ty, num = $num:ident) => {
-        const _: () = {
-            #[allow(unused)]
-            fn assert_field_type(v: $t) {
-                fn types_must_be_equal<T, U>(_: T)
-                where
-                    T: $crate::assertions::EqType<Itself = U>,
-                {
-                }
-                let index: usize = v.$num.try_into().unwrap();
-                types_must_be_equal::<_, &$ti>(&v.$i[index]);
-            }
-        };
+        $crate::assert_field_type!(@internal v, $ti, $t, v.$i[0]);
     };
 }
 
Signature · 1 line
2.49.0