From nobody Sun Sep 28 16:33:57 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1758378660; cv=none; d=zohomail.com; s=zohoarc; b=GjjRxbfLEflwQUTOueI+jK1ethOi1RxyGEwpuXRMDYIR/BZFK04pM72afkE88dXW5VZf1Sqcy3WpOM6SAC8ShZE1LucArHWO+YL8lLrsJWrQksf+Ve8g7lYkBRMrsAHUmq81Gx6OfIBn5W/8RKkPYt5dCwxlmPOEwDgubtf/LKs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1758378660; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fIs8xQFHDs7W4qQkqDiO7oV8jErY0TH0WVxlppzb/1s=; b=AScOH+K7JcYef95EuPdP6/5bi/kqeACvBIX6CSP08n9pw80sdxhRyPa1pCTlutecMrHDennDz6nIfoCpJSacB+il1WMqHYTw3qK0AMo2Rd6jGQ83FttVPKkXsmOtXOdGNJonloPQk2J50ZpmrCd/K+/OJz2I5eh3gswD5ZzADb0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1758378660586340.3050041483723; Sat, 20 Sep 2025 07:31:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uzybU-0007Jr-SB; Sat, 20 Sep 2025 10:30:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uzybR-0007J0-SE for qemu-devel@nongnu.org; Sat, 20 Sep 2025 10:30:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uzybP-0005Sn-Db for qemu-devel@nongnu.org; Sat, 20 Sep 2025 10:30:17 -0400 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-131-wp8ER5C3MPOMpzWDW1Bm-w-1; Sat, 20 Sep 2025 10:30:12 -0400 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-45f2a1660fcso28088725e9.1 for ; Sat, 20 Sep 2025 07:30:12 -0700 (PDT) Received: from [192.168.10.48] ([151.95.47.123]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-467fc818e00sm84311545e9.0.2025.09.20.07.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Sep 2025 07:30:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758378614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fIs8xQFHDs7W4qQkqDiO7oV8jErY0TH0WVxlppzb/1s=; b=CyHu+uyBpD4YArI8lxO1NqAZ2EKEyYPAQf22BaZ5hOg+cuaVnPLwkA+zd2wW9A1eHKSZGR 4FcDz7i9/ONXPBhWDwsrNfLbBasSIUcim7yUArBNNQghK1KOGPv4eh0q+sIU8SVbKJhqn3 deOBMEBRMs6gYHsYlKApfWqVmS1H7R8= X-MC-Unique: wp8ER5C3MPOMpzWDW1Bm-w-1 X-Mimecast-MFC-AGG-ID: wp8ER5C3MPOMpzWDW1Bm-w_1758378612 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758378611; x=1758983411; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fIs8xQFHDs7W4qQkqDiO7oV8jErY0TH0WVxlppzb/1s=; b=atORjQ5TqFQRbbzWEaVOCOeVUTFE31NNg2mzzlYDcGQKGpmivP2n62FQXtHCRBFHfF 3LWXHcJzOwlVtPAVR1IYWE4le4bBi+y1gX8CzR7zx48gwbtmOFywajJV3+SeTJ3h2V4B jVytb4m56ow4hVY16zgNx6ZioN00mdmmdPN1Sg+A8IlVymYSrc/tE0D7WLXM+dLcd+id Mm4+PLRq7iQ8SiAQQIp/Xr+rBNt+ETbH4VN4DRB51/+rOYlEMA+25VNonMCXmtytPoYp eGtHU8YEpoKL6JHflgp7Hu3w4Vs+E0MxC6Hph5Kat+dDqU/8Ucv4kdIQw6PMNA2V98Md Or4Q== X-Gm-Message-State: AOJu0YxgEylchIzCfrXn3AH5ni6W51fbLq3tUbXRQRW6wCKg6mg8fXlU 5RCR43cUx4nvcWkUa7cF0T69zjBgaO+mKcj594uHyheRQQBbsO3u12/yziacDtCtdX8XZi7DOKV xo0eKHmK6jMMFbikrfK4GEA8XZg/MkXEbUk2UE/YhIsUpvjgEc1rcesdhUi1hEHsoopVf8XVxoo vWEHGq6eVD4JGpXUfz5UItd6TLyhkjoJ/FHmS9YmgH X-Gm-Gg: ASbGncvB5lGcZSZyRUFo8Pb2QtHY91mLl/vFqRjrC6ZwrcvTwIZ/b19wkF1+K/4SHIF 1ogO3aJi9wN/qEUaY0729Qz4pZ396VHZgQnsXeisiI9A9G15RahrvMBQOoQIHoEfJw/h8sj92J4 WDWPL5Uy5rhtEgPM6pfoqRSl6akgTbEYRPpMfzqIEIDy6S9IvgRjaM/qqMCgucLX4SDFNkKPobD z1Kds7L8mp+V48/zMApBgbzvsFzBYO7u/ryHEIA3HHkGI2UuXta1McS1PJruNrlo9/WR3xffQGy +on/sSOLolsbDIwTwwENCxKd5JIOXXnZsQhpf1jRzojvBjkflhav9hS8fG0ypwPy/DOu42JIBYu i08nDLnn4V73XqzOpJmQZNx+OOn1QJosSK0QzSQReH34= X-Received: by 2002:a05:600c:45d2:b0:45b:7d77:b592 with SMTP id 5b1f17b1804b1-467e6f36218mr68881545e9.12.1758378610612; Sat, 20 Sep 2025 07:30:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF6yAihBDDQWq6K/o0tKGfwu/bfeX3ARyU00Vs5xEdNClC2xOeX5XKAVt22ovDYOp0oxPHHJA== X-Received: by 2002:a05:600c:45d2:b0:45b:7d77:b592 with SMTP id 5b1f17b1804b1-467e6f36218mr68881255e9.12.1758378609964; Sat, 20 Sep 2025 07:30:09 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, manos.pitsidianakis@linaro.org Subject: [PATCH 4/7] rust: migration: add high-level migration wrappers Date: Sat, 20 Sep 2025 16:29:55 +0200 Message-ID: <20250920142958.181910-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250920142958.181910-1-pbonzini@redhat.com> References: <20250920142958.181910-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.045, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1758378663213116600 Content-Type: text/plain; charset="utf-8" Instead of dealing with pre/post callbacks, allow devices to implement a snapshot/restore mechanism; this has two main advantages: - it can be easily implemented via procedural macros - there can be generic implementations to deal with various kinds of interior-mutable containers, from BqlRefCell to Mutex, so that C code does not see Rust concepts such as Mutex<>. Using it is easy; you can implement the snapshot/restore trait ToMigrationState and declare your state like: regs: Migratable> Migratable<> allows dereferencing to the underlying object with no run-time cost. Note that Migratable<> actually does not accept ToMigrationState, only the similar ToMigrationStateShared trait that the user will mostly not care about. This is required by the fact that pre/post callbacks take a &self, and ensures that the argument is a Mutex or BqlRefCell (including an array or Arc<> thereof). Signed-off-by: Paolo Bonzini Reviewed-by: Zhao Liu --- rust/Cargo.lock | 1 + rust/migration/Cargo.toml | 1 + rust/migration/meson.build | 1 + rust/migration/src/lib.rs | 3 + rust/migration/src/migratable.rs | 430 +++++++++++++++++++++++++++++++ 5 files changed, 436 insertions(+) create mode 100644 rust/migration/src/migratable.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 960f603cedb..32db90066f1 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -140,6 +140,7 @@ version =3D "0.1.0" dependencies =3D [ "bql", "common", + "qemu_macros", "util", ] =20 diff --git a/rust/migration/Cargo.toml b/rust/migration/Cargo.toml index f4a86275152..8efce7a72cb 100644 --- a/rust/migration/Cargo.toml +++ b/rust/migration/Cargo.toml @@ -15,6 +15,7 @@ rust-version.workspace =3D true [dependencies] bql =3D { path =3D "../bql" } common =3D { path =3D "../common" } +qemu_macros =3D { path =3D "../qemu-macros" } util =3D { path =3D "../util" } =20 [lints] diff --git a/rust/migration/meson.build b/rust/migration/meson.build index 2f38da9220f..c258881790d 100644 --- a/rust/migration/meson.build +++ b/rust/migration/meson.build @@ -30,6 +30,7 @@ _migration_rs =3D static_library( [ 'src/lib.rs', 'src/bindings.rs', + 'src/migratable.rs', 'src/vmstate.rs', ], {'.' : _migration_bindings_inc_rs}, diff --git a/rust/migration/src/lib.rs b/rust/migration/src/lib.rs index 5f51dde4406..efe9896b619 100644 --- a/rust/migration/src/lib.rs +++ b/rust/migration/src/lib.rs @@ -2,5 +2,8 @@ =20 pub mod bindings; =20 +pub mod migratable; +pub use migratable::*; + pub mod vmstate; pub use vmstate::*; diff --git a/rust/migration/src/migratable.rs b/rust/migration/src/migratab= le.rs new file mode 100644 index 00000000000..d09eeb35f11 --- /dev/null +++ b/rust/migration/src/migratable.rs @@ -0,0 +1,430 @@ +// Copyright 2025 Red Hat, Inc. +// Author(s): Paolo Bonzini +// SPDX-License-Identifier: GPL-2.0-or-later + +use std::{ + fmt, + mem::size_of, + ptr::{self, addr_of, NonNull}, + sync::{Arc, Mutex}, +}; + +use bql::{BqlCell, BqlRefCell}; +use common::Zeroable; + +use crate::{ + bindings, vmstate_fields_ref, vmstate_of, InvalidError, VMState, VMSta= teDescriptionBuilder, +}; + +/// Enables QEMU migration support even when a type is wrapped with +/// synchronization primitives (like `Mutex`) that the C migration +/// code cannot directly handle. The trait provides methods to +/// extract essential state for migration and restore it after +/// migration completes. +/// +/// On top of extracting data from synchronization wrappers during save +/// and restoring it during load, it's also possible to use `ToMigrationSt= ate` +/// to convert runtime representations to migration-safe formats. +/// +/// # Examples +/// +/// ``` +/// use bql::BqlCell; +/// use migration::{InvalidError, ToMigrationState, VMState}; +/// # use migration::VMStateField; +/// +/// # #[derive(Debug, PartialEq, Eq)] +/// struct DeviceState { +/// counter: BqlCell, +/// enabled: bool, +/// } +/// +/// # #[derive(Debug)] +/// #[derive(Default)] +/// struct DeviceMigrationState { +/// counter: u32, +/// enabled: bool, +/// } +/// +/// # unsafe impl VMState for DeviceMigrationState { +/// # const BASE: VMStateField =3D ::common::Zeroable::ZERO; +/// # } +/// impl ToMigrationState for DeviceState { +/// type Migrated =3D DeviceMigrationState; +/// +/// fn snapshot_migration_state( +/// &self, +/// target: &mut Self::Migrated, +/// ) -> Result<(), InvalidError> { +/// target.counter =3D self.counter.get(); +/// target.enabled =3D self.enabled; +/// Ok(()) +/// } +/// +/// fn restore_migrated_state_mut( +/// &mut self, +/// source: Self::Migrated, +/// _version_id: u8, +/// ) -> Result<(), InvalidError> { +/// self.counter.set(source.counter); +/// self.enabled =3D source.enabled; +/// Ok(()) +/// } +/// } +/// # bql::start_test(); +/// # let dev =3D DeviceState { counter: 10.into(), enabled: true }; +/// # let mig =3D dev.to_migration_state().unwrap(); +/// # assert!(matches!(*mig, DeviceMigrationState { counter: 10, enabled: = true })); +/// # let mut dev2 =3D DeviceState { counter: 42.into(), enabled: false }; +/// # dev2.restore_migrated_state_mut(*mig, 1).unwrap(); +/// # assert_eq!(dev2, dev); +/// ``` +pub trait ToMigrationState { + /// The type used to represent the migrated state. + type Migrated: Default + VMState; + + /// Capture the current state into a migration-safe format, failing + /// if the state cannot be migrated. + fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Res= ult<(), InvalidError>; + + /// Restores state from a migrated representation, failing if the + /// state cannot be restored. + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError>; + + /// Convenience method to combine allocation and state capture + /// into a single operation. + fn to_migration_state(&self) -> Result, InvalidErr= or> { + let mut migrated =3D Box::::default(); + self.snapshot_migration_state(&mut migrated)?; + Ok(migrated) + } +} + +// Implementations for primitive types. Do not use a blanket implementati= on +// for all Copy types, because [T; N] is Copy if T is Copy; that would con= flict +// with the below implementation for arrays. +macro_rules! impl_for_primitive { + ($($t:ty),*) =3D> { + $( + impl ToMigrationState for $t { + type Migrated =3D Self; + + fn snapshot_migration_state( + &self, + target: &mut Self::Migrated, + ) -> Result<(), InvalidError> { + *target =3D *self; + Ok(()) + } + + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + _version_id: u8, + ) -> Result<(), InvalidError> { + *self =3D source; + Ok(()) + } + } + )* + }; +} + +impl_for_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, bool); + +impl ToMigrationState for [T; N] +where + [T::Migrated; N]: Default, +{ + type Migrated =3D [T::Migrated; N]; + + fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Res= ult<(), InvalidError> { + for (item, target_item) in self.iter().zip(target.iter_mut()) { + item.snapshot_migration_state(target_item)?; + } + Ok(()) + } + + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + for (item, source_item) in self.iter_mut().zip(source) { + item.restore_migrated_state_mut(source_item, version_id)?; + } + Ok(()) + } +} + +/// Extension trait for types that support migration state restoration +/// through interior mutability. +/// +/// This trait extends [`ToMigrationState`] for types that can restore +/// their state without requiring mutable access. While user structs +/// will generally use `ToMigrationState`, the device will have multiple +/// references and therefore the device struct has to employ an interior +/// mutability wrapper like [`Mutex`] or [`BqlRefCell`]. +/// +/// Anything that implements this trait can in turn be used within +/// [`Migratable`], which makes no assumptions on how to achieve mutable +/// access to the runtime state. +/// +/// # Examples +/// +/// ``` +/// use std::sync::Mutex; +/// +/// use migration::ToMigrationStateShared; +/// +/// let device_state =3D Mutex::new(42); +/// // Can restore without &mut access +/// device_state.restore_migrated_state(100, 1).unwrap(); +/// assert_eq!(*device_state.lock().unwrap(), 100); +/// ``` +pub trait ToMigrationStateShared: ToMigrationState { + /// Restores state from a migrated representation to an interior-mutab= le + /// object. Similar to `restore_migrated_state_mut`, but requires a + /// shared reference; therefore it can be used to restore a device's + /// state even though devices have multiple references to them. + fn restore_migrated_state( + &self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError>; +} + +impl ToMigrationStateShared for= [T; N] +where + [T::Migrated; N]: Default, +{ + fn restore_migrated_state( + &self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + for (item, source_item) in self.iter().zip(source) { + item.restore_migrated_state(source_item, version_id)?; + } + Ok(()) + } +} + +impl ToMigrationState for Arc { + type Migrated =3D T::Migrated; + + fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Res= ult<(), InvalidError> { + (**self).snapshot_migration_state(target) + } + + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + (**self).restore_migrated_state(source, version_id) + } +} + +impl ToMigrationStateShared for Arc { + fn restore_migrated_state( + &self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + (**self).restore_migrated_state(source, version_id) + } +} + +// Interior-mutable types only require ToMigrationState for the inner type! + +impl ToMigrationState for Mutex { + type Migrated =3D T::Migrated; + + fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Res= ult<(), InvalidError> { + self.lock().unwrap().snapshot_migration_state(target) + } + + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + self.restore_migrated_state(source, version_id) + } +} + +impl ToMigrationStateShared for Mutex { + fn restore_migrated_state( + &self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + self.lock() + .unwrap() + .restore_migrated_state_mut(source, version_id) + } +} + +impl ToMigrationState for BqlRefCell { + type Migrated =3D T::Migrated; + + fn snapshot_migration_state(&self, target: &mut Self::Migrated) -> Res= ult<(), InvalidError> { + self.borrow().snapshot_migration_state(target) + } + + fn restore_migrated_state_mut( + &mut self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + self.get_mut() + .restore_migrated_state_mut(source, version_id) + } +} + +impl ToMigrationStateShared for BqlRefCell { + fn restore_migrated_state( + &self, + source: Self::Migrated, + version_id: u8, + ) -> Result<(), InvalidError> { + self.borrow_mut() + .restore_migrated_state_mut(source, version_id) + } +} + +/// A wrapper that enables QEMU migration for types with shared state. +/// +/// `Migratable` provides a bridge between Rust types that use interior +/// mutability (like `Mutex`) and QEMU's C-based migration infrastructu= re. +/// It manages the lifecycle of migration state and provides automatic +/// conversion between runtime and migration representations. +/// +/// ```ignore +/// # use std::sync::Mutex; +/// # use migration::Migratable; +/// +/// pub struct DeviceRegs { +/// status: u32, +/// } +/// +/// pub struct SomeDevice { +/// // ... +/// registers: Migratable>, +/// } +/// ``` +#[repr(C)] +pub struct Migratable { + /// Pointer to migration state, valid only during migration operations. + /// C vmstate does not support NULL pointers, so no `Option>`. + migration_state: BqlCell<*mut T::Migrated>, + + /// The runtime state that can be accessed during normal operation + runtime_state: T, +} + +impl std::ops::Deref for Migratable { + type Target =3D T; + + fn deref(&self) -> &Self::Target { + &self.runtime_state + } +} + +impl std::ops::DerefMut for Migratable { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.runtime_state + } +} + +impl Migratable { + /// Creates a new `Migratable` wrapper around the given runtime state. + /// + /// # Returns + /// A new `Migratable` instance ready for use and migration + pub fn new(runtime_state: T) -> Self { + Self { + migration_state: BqlCell::new(ptr::null_mut()), + runtime_state, + } + } + + fn pre_save(&self) -> Result<(), InvalidError> { + let state =3D self.runtime_state.to_migration_state()?; + self.migration_state.set(Box::into_raw(state)); + Ok(()) + } + + fn post_save(&self) -> Result<(), InvalidError> { + let state =3D unsafe { Box::from_raw(self.migration_state.take()) = }; + drop(state); + Ok(()) + } + + fn pre_load(&self) -> Result<(), InvalidError> { + self.migration_state + .set(Box::into_raw(Box::::default())); + Ok(()) + } + + fn post_load(&self, version_id: u8) -> Result<(), InvalidError> { + let state =3D unsafe { Box::from_raw(self.migration_state.take()) = }; + self.runtime_state + .restore_migrated_state(*state, version_id) + } +} + +impl fmt::Debug for Migratable +where + T::Migrated: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut struct_f =3D f.debug_struct("Migratable"); + struct_f.field("runtime_state", &self.runtime_state); + + let state =3D NonNull::new(self.migration_state.get()).map(|x| uns= afe { x.as_ref() }); + struct_f.field("migration_state", &state); + struct_f.finish() + } +} + +impl Default for Migratable { + fn default() -> Self { + Self::new(T::default()) + } +} + +impl Migratable { + const FIELD: bindings::VMStateField =3D vmstate_of!(Self, migration_st= ate); + + const FIELDS: &[bindings::VMStateField] =3D vmstate_fields_ref! { + Migratable::::FIELD + }; + + const VMSD: &'static bindings::VMStateDescription =3D VMStateDescripti= onBuilder::::new() + .version_id(1) + .minimum_version_id(1) + .pre_save(&Self::pre_save) + .pre_load(&Self::pre_load) + .post_save(&Self::post_save) + .post_load(&Self::post_load) + .fields(Self::FIELDS.as_ptr()) + .build() + .as_ref(); +} + +unsafe impl VMState for Migratable= { + const BASE: bindings::VMStateField =3D { + bindings::VMStateField { + vmsd: addr_of!(*Self::VMSD), + size: size_of::(), + flags: bindings::VMStateFlags::VMS_STRUCT, + ..Zeroable::ZERO + } + }; +} --=20 2.51.0