migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST
cover migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST
CI: https://gitlab.com/peterx/qemu/-/pipelines/3091862038
This is for QEMU 12.0 material only. I could have marked this RFC, but I found that I want / see this more than "request for comments", so I send it as formal patches.
To PeterM: I know you may still have some concern on dropping VALIDATE, but until now I still think this should be the right thing to do. Let's move the discussion here and we can continue discussions here.
===8<===
Peter Maydell reported an unwanted assertion on source QEMU due to a failed check in VMSTATE_VALIDATE():
https://lore.kernel.org/r/[email protected]
I decided to look at this in a slightly broader range, because I always thought hijacking field_exists() is very error prone and hard to follow, and VMSTATE_VALIDATE() is the only user of it (VMS_MUST_EXIST). Meanwhile it is the only VMSTATE_*() macro that does not carry a real entity of data stream, so it's not really a FIELD, but some checker we added adhoc inline into the VMSD fields. It might be confusing to anyone new reading VMSD code, including myself when reading it first time.
Instead of updating the source behavior, let's directly wipe that code out. I think it's not making a lot of sense doing device state check in save(), because those sanity check should be done in emulation code, not after seding all RAM, which is extremely awkward. post_load() OTOH still makes some sense to sanity check fields, so I moved all VALIDATE there.
Any comment welcomed, thanks.
Peter Xu (6):
pcie/aer: Remove the usage of VMSTATE_VALIDATE() hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp()
hpet: Move VMSTATE_VALIDATE() check into post_load arm/machine: Move all VMSTATE_VALIDATE() usages into post_load
rust: Replace vmstate_validate with post_load
migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST
include/migration/vmstate.h | 11 ----- hw/gpio/zaurus.c | 29 ++++++------ hw/pci/pcie_aer.c | 40 ++++++++++++++-- hw/timer/hpet.c | 18 ++++---- migration/savevm.c | 5 -- migration/vmstate.c | 12 ----- target/arm/machine.c | 45 ++++++++++++++++-- rust/hw/timer/hpet/src/device.rs | 9 ++-- rust/migration/src/prelude.rs | 1 - rust/migration/src/vmstate.rs | 17 ------- rust/tests/tests/vmstate_tests.rs | 77 ------------------------------- 11 files changed, 106 insertions(+), 158 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/6 pcie/aer: Remove the usage of VMSTATE_VALIDATE()
Paving way removing it. We can replace it with a post_load_errp() instead. Tested with migration with "-device ioh3420,id=rp0" against pre-patch, which means migration compatibility is unaffected.
--- hw/pci/pcie_aer.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-)
hw/pci/pcie_aer.c +35 -5
@@ -807,21 +807,51 @@ static const VMStateDescription vmstate_pcie_aer_err = { } }; -static bool pcie_aer_state_log_num_valid (void *opaque,int version_id) -{ -PCIEAERLog *s = opaque; +typedef struct { +uint16_t v; +}tmp_u16 ; +static bool pcie_aer_state_log_num_valid (PCIEAERLog *s) +{return s->log_num <= s->log_max; } +static bool pcie_aer_state_log_num_post_load (void *opaque,int version, +Error **errp) +{ +PCIEAERLog *s =container_of (opaque, PCIEAERLog, log_num); + +if (!pcie_aer_state_log_num_valid (s)) { +error_setg (errp,"PCIe AER log num too big: log_num > log_max" ); +return false; + } + +return true; +} + +static const VMStateDescription vmstate_pcie_aer_log_num = { + .name ="PCIE_AER_ERROR_LOG/NUM" , + .version_id =1 , + .minimum_version_id =1 , + .post_load_errp = pcie_aer_state_log_num_post_load, + .fields = (const VMStateField []) { +VMSTATE_UINT16 (v, tmp_u16), +VMSTATE_END_OF_LIST () + } +}; +const VMStateDescription vmstate_pcie_aer_log = { .name ="PCIE_AER_ERROR_LOG" , .version_id =1 , .minimum_version_id =1 , .fields = (const VMStateField []) { -VMSTATE_UINT16 (log_num, PCIEAERLog), +/* +* Only to use STRUCT for a post_load_errp(). New code should try +* to use something look better, e.g. subsections in order. +*/ +VMSTATE_STRUCT (log_num, PCIEAERLog,1 , + vmstate_pcie_aer_log_num, uint16_t),VMSTATE_UINT16_EQUAL (log_max, PCIEAERLog), -VMSTATE_VALIDATE ("log_num <= log_max" , pcie_aer_state_log_num_valid),VMSTATE_STRUCT_VARRAY_POINTER_UINT16 (log, PCIEAERLog, log_num, vmstate_pcie_aer_err, PCIEAERErr),VMSTATE_END_OF_LIST ()
Signature · 1 line
2.56.0
PATCH 2/6 hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp()
In reply to Peter XuTo pave way for removing VMSTATE_VALIDATE(), remove the use in this file by moving it into the post_load_errp().
--- hw/gpio/zaurus.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
hw/gpio/zaurus.c +16 -13
@@ -23,6 +23,7 @@#include "qemu/module.h" #include "qemu/log.h" #include "qom/object.h" +#include "qapi/error.h" /* SCOOP devices */
@@ -188,12 +189,24 @@ static void scoop_init(Object *obj)sysbus_init_mmio (sbd, &s->iomem); } -static int scoop_post_load (void *opaque,int version_id) +static bool vmstate_scoop_validate (ScoopInfo *s) +{ +return !(s->prev_level &0xffff0000 ) && + !(s->gpio_level &0xffff0000 ) && + !(s->gpio_dir &0xffff0000 ); +} + +static bool scoop_post_load (void *opaque,int version_id,Error **errp) {ScoopInfo *s = (ScoopInfo *) opaque;int i;uint32_t level; +if (!vmstate_scoop_validate (s)) { +error_setg (errp,"irq levels are not 16-bits" ); +return false; + } + level = s->gpio_level & s->gpio_dir;for (i =0 ; i <16 ; i++) {
@@ -202,7 +215,7 @@ static int scoop_post_load(void *opaque, int version_id) s->prev_level = level; -return 0 ; +return true; }static bool is_version_0 (void *opaque,int version_id)
@@ -210,27 +223,17 @@ static bool is_version_0(void *opaque, int version_id)return version_id ==0 ; } -static bool vmstate_scoop_validate (void *opaque,int version_id) -{ -ScoopInfo *s = opaque; - -return !(s->prev_level &0xffff0000 ) && - !(s->gpio_level &0xffff0000 ) && - !(s->gpio_dir &0xffff0000 ); -} -static const VMStateDescription vmstate_scoop_regs = { .name ="scoop" , .version_id =1 , .minimum_version_id =0 , - .post_load = scoop_post_load, + .post_load_errp = scoop_post_load, .fields = (const VMStateField []) {VMSTATE_UINT16 (status, ScoopInfo),VMSTATE_UINT16 (power, ScoopInfo),VMSTATE_UINT32 (gpio_level, ScoopInfo),VMSTATE_UINT32 (gpio_dir, ScoopInfo),VMSTATE_UINT32 (prev_level, ScoopInfo), -VMSTATE_VALIDATE ("irq levels are 16 bit" , vmstate_scoop_validate),VMSTATE_UINT16 (mcr, ScoopInfo),VMSTATE_UINT16 (cdr, ScoopInfo),VMSTATE_UINT16 (ccr, ScoopInfo),
Signature · 1 line
2.56.0
Re: [PATCH for-12.0 2/6] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp()
Quoted 35 lines · Peter Xu
To pave way for removing
VMSTATE_VALIDATE(), remove the use in this file by moving it into thepost_load_errp().--- hw/gpio/zaurus.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)hw/gpio/zaurus.c +8
-1@@ -23,6 +23,7 @@#include "qemu/module.h" #include "qemu/log.h" #include "qom/object.h" +#include "qapi/error.h" /* SCOOP devices */ @@ -188,12 +189,24 @@ static void scoop_init(Object *obj)sysbus_init_mmio (sbd, &s->iomem); } -static int scoop_post_load (void *opaque,int version_id) +static bool vmstate_scoop_validate (ScoopInfo *s) +{ +return !(s->prev_level &0xffff0000 ) && + !(s->gpio_level &0xffff0000 ) && + !(s->gpio_dir &0xffff0000 ); +} +
hw/gpio/zaurus.c static void scoop_init(Object *obj) · line 208
Quoted 10 lines
hw/gpio/zaurus.c +5
-0+static bool scoop_post_load (void *opaque,int version_id,Error **errp) {ScoopInfo *s = (ScoopInfo *) opaque;int i;uint32_t level; +if (!vmstate_scoop_validate (s)) { +error_setg (errp,"irq levels are not 16-bits" ); +return false; + }
I would put the condition inline rather than into a function (especially since the function name is not very informative). Otherwise this looks good to me.
thanks
-- PMM
PATCH 3/6 hpet: Move VMSTATE_VALIDATE() check into post_load
In reply to Peter XuTo pave way for removing 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.
--- hw/timer/hpet.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
hw/timer/hpet.c +10 -8
@@ -254,25 +254,28 @@ static int hpet_pre_save(void *opaque)return 0 ; } -static bool hpet_validate_num_timers (void *opaque,int version_id) +static bool hpet_validate_num_timers (HPETState *s) { -HPETState *s = opaque; -return s->num_timers == s->num_timers_save; } -static int hpet_post_load (void *opaque,int version_id) +static bool hpet_post_load (void *opaque,int version_id,Error **errp) {HPETState *s = opaque;int i; +if (!hpet_validate_num_timers (s)) { +error_setg (errp,"num_timers must match" ); +return false; + } +for (i =0 ; i < s->num_timers; i++) {HPETTimer *t = &s->timer[i]; t->cmp64 =hpet_calculate_cmp64 (t, s->hpet_counter, t->cmp); t->last =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) -NANOSECONDS_PER_SECOND ; } -return 0 ; +return true; }static bool hpet_offset_needed (void *opaque)
@@ -332,14 +335,13 @@ static const VMStateDescription vmstate_hpet = { .version_id = 2, .minimum_version_id = 2, .pre_save = hpet_pre_save, - .post_load = hpet_post_load, + .post_load_errp = hpet_post_load, .fields = (const VMStateField []) {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState),VMSTATE_UINT8 (num_timers_save, HPETState), -VMSTATE_VALIDATE ("num_timers must match" , hpet_validate_num_timers), -VMSTATE_STRUCT_VARRAY_UINT8 (timer, HPETState, num_timers_save,0 , +VMSTATE_STRUCT_VARRAY_UINT8 (timer, HPETState, num_timers,0 , vmstate_hpet_timer, HPETTimer),VMSTATE_END_OF_LIST () },
Signature · 1 line
2.56.0
Re: [PATCH for-12.0 3/6] hpet: Move VMSTATE_VALIDATE() check into post_load
Quoted 55 lines · Peter Xu
To pave way for removing
VMSTATE_VALIDATE(), move the usage of hpet intopost_load. To make it safer, loading the timers with thenum_timers, then only check against the wire number at last.Cc "Michael S. Tsirkin" <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Peter Xu <[email protected]>--- hw/timer/hpet.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)hw/timer/hpet.c +8
-6@@ -254,25 +254,28 @@ static int hpet_pre_save(void *opaque)return 0 ; } -static bool hpet_validate_num_timers (void *opaque,int version_id) +static bool hpet_validate_num_timers (HPETState *s) { -HPETState *s = opaque; -return s->num_timers == s->num_timers_save; } -static int hpet_post_load (void *opaque,int version_id) +static bool hpet_post_load (void *opaque,int version_id,Error **errp) {HPETState *s = opaque;int i; +if (!hpet_validate_num_timers (s)) { +error_setg (errp,"num_timers must match" ); +return false; + } +for (i =0 ; i < s->num_timers; i++) {HPETTimer *t = &s->timer[i]; t->cmp64 =hpet_calculate_cmp64 (t, s->hpet_counter, t->cmp); t->last =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) -NANOSECONDS_PER_SECOND ; } -return 0 ; +return true; }static bool hpet_offset_needed (void *opaque)@@ -332,14 +335,13 @@ static const VMStateDescription vmstate_hpet = { .version_id = 2, .minimum_version_id = 2, .pre_save = hpet_pre_save, - .post_load = hpet_post_load,
hw/timer/hpet.c static const VMStateDescription vmstate_hpet = { · line 345
Quoted 10 lines
hw/timer/hpet.c +2
-2+ .post_load_errp = hpet_post_load, .fields = (const VMStateField []) {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState),VMSTATE_UINT8 (num_timers_save, HPETState), -VMSTATE_VALIDATE ("num_timers must match" , hpet_validate_num_timers), -VMSTATE_STRUCT_VARRAY_UINT8 (timer, HPETState, num_timers_save,0 , +VMSTATE_STRUCT_VARRAY_UINT8 (timer, HPETState, num_timers,0 , vmstate_hpet_timer, HPETTimer),
What's the failure mode of VMSTATE_STRUCT_VARRAY if the destination tries to send us the wrong size of data for what we're expecting? Hopefully it gives a useful clear error message.
hw/timer/hpet.c static const VMStateDescription vmstate_hpet = { · line 347
Quoted 2 lines
hw/timer/hpet.c +0
-0VMSTATE_END_OF_LIST () },
We need to keep sending out the num_timers_save value on the wire so we can send to a destination QEMU that honours the inbound value, but it's tempting to suggest that on input we should just ignore the inbound value, as we would do for any other QOM property or command line parameter that needs to match on source and dest for a successful migration. If they don't match then it's very likely that the migration already failed on the STRUCT_VARRAY field before it even got to the post_load hook.
thanks
-- PMM
PATCH 4/6 arm/machine: Move all VMSTATE_VALIDATE() usages into post_load
In reply to Peter XuTo pave way removing VMSTATE_VALIDATE(), move all four such uses into a post_load hook instead.
--- target/arm/machine.c | 45 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-)
target/arm/machine.c +40 -5
@@ -11,6 +11,7 @@#include "migration/qemu-file-types.h" #include "migration/vmstate.h" #include "target/arm/gtimer.h" +#include "qapi/error.h" static bool vfp_needed (void *opaque) {
@@ -421,14 +422,24 @@ static bool m_csselr_needed(void *opaque)return !arm_v7m_csselr_razwi (cpu); } +static bool csselr_post_load (void *opaque,int version_id,Error **errp) +{ +if (!csselr_vmstate_validate (opaque, version_id)) { +error_setg (errp,"CSSELR is invalid" ); +return false; + } + +return true; +} +static const VMStateDescription vmstate_m_csselr = { .name ="cpu/m/csselr" , .version_id =1 , .minimum_version_id =1 , .needed = m_csselr_needed, + .post_load_errp = csselr_post_load, .fields = (const VMStateField []) {VMSTATE_UINT32_ARRAY (env.v7m.csselr,ARMCPU ,M_REG_NUM_BANKS ), -VMSTATE_VALIDATE ("CSSELR is valid" , csselr_vmstate_validate),VMSTATE_END_OF_LIST () } };
@@ -596,11 +607,22 @@ static bool pmsav7_rgnr_vmstate_validate(void *opaque, int version_id)return cpu->env.pmsav7.rnr[M_REG_NS ] < cpu->pmsav7_dregion; } +static bool pmsav7_rgnr_post_load (void *opaque,int version_id,Error **errp) +{ +if (!pmsav7_rgnr_vmstate_validate (opaque, version_id)) { +error_setg (errp,"rgnr is invalid" ); +return false; + } + +return true; +} +static const VMStateDescription vmstate_pmsav7 = { .name ="cpu/pmsav7" , .version_id =1 , .minimum_version_id =1 , .needed = pmsav7_needed, + .post_load_errp = pmsav7_rgnr_post_load, .fields = (const VMStateField []) {VMSTATE_VARRAY_UINT32 (env.pmsav7.drbar,ARMCPU , pmsav7_dregion,0 , vmstate_info_uint32, uint32_t),
@@ -608,7 +630,6 @@ static const VMStateDescription vmstate_pmsav7 = { vmstate_info_uint32, uint32_t), VMSTATE_VARRAY_UINT32(env.pmsav7.dracr, ARMCPU, pmsav7_dregion, 0, vmstate_info_uint32, uint32_t), -VMSTATE_VALIDATE ("rgnr is valid" , pmsav7_rgnr_vmstate_validate), VMSTATE_END_OF_LIST() } };
@@ -711,11 +732,27 @@ static bool m_security_needed(void *opaque)return arm_feature (env,ARM_FEATURE_M_SECURITY ); } +static bool m_security_post_load (void *opaque,int version_id,Error **errp) +{ +if (!s_rnr_vmstate_validate (opaque, version_id)) { +error_setg (errp,"secure MPU_RNR is invalid" ); +return false; + } + +if (!sau_rnr_vmstate_validate (opaque, version_id)) { +error_setg (errp,"SAU_RNR is invalid" ); +return false; + } + +return true; +} +static const VMStateDescription vmstate_m_security = { .name ="cpu/m-security" , .version_id =1 , .minimum_version_id =1 , .needed = m_security_needed, + .post_load_errp = m_security_post_load, .fields = (const VMStateField []) {VMSTATE_UINT32 (env.v7m.secure,ARMCPU ),VMSTATE_UINT32 (env.v7m.other_ss_msp,ARMCPU ),
@@ -732,7 +769,6 @@ static const VMStateDescription vmstate_m_security = {VMSTATE_VARRAY_UINT32 (env.pmsav8.rlar[M_REG_S ],ARMCPU , pmsav7_dregion,0 , vmstate_info_uint32, uint32_t),VMSTATE_UINT32 (env.pmsav7.rnr[M_REG_S ],ARMCPU ), -VMSTATE_VALIDATE ("secure MPU_RNR is valid" , s_rnr_vmstate_validate),VMSTATE_UINT32 (env.v7m.mpu_ctrl[M_REG_S ],ARMCPU ),VMSTATE_UINT32 (env.v7m.ccr[M_REG_S ],ARMCPU ), VMSTATE_UINT32(env.v7m.mmfar[M_REG_S], ARMCPU),
@@ -744,7 +780,6 @@ static const VMStateDescription vmstate_m_security = {VMSTATE_VARRAY_UINT32 (env.sau.rlar,ARMCPU , sau_sregion,0 , vmstate_info_uint32, uint32_t),VMSTATE_UINT32 (env.sau.rnr,ARMCPU ), -VMSTATE_VALIDATE ("SAU_RNR is valid" , sau_rnr_vmstate_validate),VMSTATE_UINT32 (env.sau.ctrl,ARMCPU ), VMSTATE_UINT32(env.v7m.scr[M_REG_S], ARMCPU),/* AIRCR is not secure-only, but our implementation is R/O if the
@@ -1405,7 +1440,7 @@ const VMStateDescription vmstate_arm_cpu = { &vmstate_m, &vmstate_thumb2ee,/* pmsav7_rnr must come before pmsav7 so that we have the -* region number before we test it in the VMSTATE_VALIDATE +* region number before we test it in the post_load_errp() * in vmstate_pmsav7. */ &vmstate_pmsav7_rnr,
Signature · 1 line
2.56.0
Re: [PATCH for-12.0 4/6] arm/machine: Move all VMSTATE_VALIDATE() usages into post_load
Quoted 10 lines · Peter Xu
To pave way removing
VMSTATE_VALIDATE(), move all four such uses into apost_loadhook instead.--- target/arm/machine.c | 45 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-)
I think I'd put all of these validity checks into the top level cpu_post_load hook, rather than spreading them out across the vmstate structs (and expand out the _validate functions rather than retaining them). That also lets you drop the comment about there being an ordering constraint between subsections.
thanks
-- PMM
PATCH 5/6 rust: Replace vmstate_validate with post_load
In reply to Peter XuWe dropped all of that in C, drop that in Rust too. The goal is to remove the VMSTATE_VALIDATE() together with VMS_MUST_EXIST.
Use post_load instead for the check.
--- rust/hw/timer/hpet/src/device.rs | 9 ++-- rust/migration/src/prelude.rs | 1 - rust/migration/src/vmstate.rs | 17 ------- rust/tests/tests/vmstate_tests.rs | 77 ------------------------------- 4 files changed, 5 insertions(+), 99 deletions(-)
rust/hw/timer/hpet/src/device.rs +5 -4
@@ -934,10 +934,14 @@ 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; +if !self.validate_num_timers (version_id) { +return Err (migration::InvalidError ); + } +for tn_regsin regs.tn_regs.iter_mut ().take (self.num_timers) { tn_regs.update_cmp64 (cnt); tn_regs.last =CLOCK_VIRTUAL .get_ns () -NANOSECONDS_PER_SECOND ;
@@ -1082,8 +1086,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 ,
@@ -1109,7 +1111,6 @@ fn restore_migrated_state( .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!(
rust/migration/src/prelude.rs +0 -1
@@ -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
@@ -372,23 +372,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 +0 -77
80 lines not on this page · raw message
Signature · 1 line
2.56.0
Re: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
Hi Peter,
Quoted 6 lines · Peter Xu
Date: Fri, 20 Nov 2026 07:49:22 -0500
From: Peter Xu <[email protected]>
Subject: [PATCH for-12.0 5/6] rust: Replacevmstate_validatewithpost_load
X-Mailer: git-send-email 2.56.0We dropped all of that in C, drop that in Rust too.
but rust is using the different way to address this case :)
Quoted 4 lines
The goal is to remove
theVMSTATE_VALIDATE()together withVMS_MUST_EXIST.Use
post_loadinstead for the check.
...
rust/hw/timer/hpet/src/device.rs fn restore_migrated_state( · line 1114
Quoted 9 lines
// HPETRegistersMigration is generated by ToMigrationState macro. impl_vmstate_struct!(
HPETRegistersMigration,rust/hw/timer/hpet/src/device.rs +0
-1@@ -1109,7 +1111,6 @@ fn restore_migrated_state( .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 ),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Before this patch, the test method didn't work since vmstate_validate! has already done the check. But, IIUC, when vmstate_validate! goes away, this test seems necessary for rust side since it can help avoid overflow caused by num_timers_save (maybe it can be greater than HPET_MAX_TIMERS? before the commit 7be175e65f07).
Review fn restore_migrated_state( · line 1115
Quoted 1 line
(quoted diff) +0
-0})
So LGTM for the change in rust hpet,
And I find C side (in patch 3) handles this differently via using the local "num_timers".
In this case, I think we still have the option to keep the logic consistent between C and Rust :). We can add VMSTATE_STRUCT_VARRAY_TEST for C side:
#define VMSTATE_STRUCT_VARRAY_TEST(_field, _state, _field_num, _test,\
_version, _vmsd, _type) {\
.name = (stringify(_field)),\
.num_indirect = vmstate_field_offset(_state, _field_num),\
.version_id = (_version),\
.field_exists = (_test),\
.vmsd = &(_vmsd),\
.size = sizeof(_type),\
.flags = VMS_STRUCT | VMS_VARRAY,\
.offset = vmstate_offset_varray(_state, _field, _type),\
}and make C hpet use this:
VMSTATE_STRUCT_VARRAY_TEST(timer, HPETState, num_timers_save,
hpet_validate_num_timers, 0, vmstate_hpet_timer, HPETTimer),
Alternatively, we can also have Rust use num_timers (as u8) for timers array VMState, but this will bring back the conversion issue between u8 and usize, which is the commit b3bf86b8936d & 6e85cfe44c0e want to fix...
Thanks,
Zhao
Re: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
Quoted 1 line · Zhao Liu
Hi Peter,
Hi, Zhao,
Quoted 10 lines · Peter Xu
Quoted 6 lines
Date: Fri, 20 Nov 2026 07:49:22 -0500
From: Peter Xu <[email protected]>
Subject: [PATCH for-12.0 5/6] rust: Replacevmstate_validatewithpost_load
X-Mailer: git-send-email 2.56.0We dropped all of that in C, drop that in Rust too.
but rust is using the different way to address this case :)
It's good that someone really knows Rust chimed in. :)
Quoted 18 lines
Quoted 5 lines
The goal is to remove
theVMSTATE_VALIDATE()together withVMS_MUST_EXIST.Use
post_loadinstead for the check....
rust/hw/timer/hpet/src/device.rs fn restore_migrated_state( · line 1114
Quoted 9 lines
// HPETRegistersMigration is generated by ToMigrationState macro. impl_vmstate_struct!(
HPETRegistersMigration,rust/hw/timer/hpet/src/device.rs +0
-1@@ -1109,7 +1111,6 @@ fn restore_migrated_state( .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 ),^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 5 lines
Before this patch, the test method didn't work since
vmstate_validate! has already done the check. But, IIUC, whenvmstate_validate! goes away, this test seems necessary for rust side since it can help avoid overflow caused bynum_timers_save(maybe it can be greater thanHPET_MAX_TIMERS? before the commit7be175e65f07).
True, actually for the HPET C change I did flip to use the num_timers rather than the on wire integer to do the loading, assuming safer.
Quoted 27 lines
Review fn restore_migrated_state( · line 1115
Quoted 2 lines
(quoted diff) +0
-0})So LGTM for the change in rust hpet,
Reviewed-by Zhao Liu <[email protected]>And I find C side (in patch 3) handles this differently via using the local "
num_timers".In this case, I think we still have the option to keep the logic consistent between C and Rust :). We can add
VMSTATE_STRUCT_VARRAY_TESTfor C side:#define VMSTATE_STRUCT_VARRAY_TEST(_field, _state, _field_num, _test,\ _version, _vmsd, _type) {\ .name = (stringify(_field)),\ .num_indirect = vmstate_field_offset(_state, _field_num),\ .version_id = (_version),\ .field_exists = (_test),\ .vmsd = &(_vmsd),\ .size = sizeof(_type),\ .flags = VMS_STRUCT | VMS_VARRAY,\ .offset = vmstate_offset_varray(_state, _field, _type),\ }
Quoted 5 lines
and make C hpet use this:
VMSTATE_STRUCT_VARRAY_TEST(timer, HPETState,
num_timers_save,
hpet_validate_num_timers, 0,vmstate_hpet_timer, HPETTimer),
One thing to mention is, here the test() function is not for validation, but for checking if the field exists.
It means, assuming dest QEMU has num_timers=8, source QEMU has num_timers=4, when migration happens with your VARRAY_TEST(), it won't sanity check it, but since it saw 4!=8 it will assume the whole VARRAY doesn't exist.
That's one reason why I want to get rid of VALIDATE(), because it's abusing field_exist() IMO, and we don't necessarily need such abuse when we have post_load.
Quoted 4 lines
Alternatively, we can also have Rust use
num_timers(as u8) for timers array VMState, but this will bring back the conversion issue between u8 and usize, which is the commitb3bf86b8936d&6e85cfe44c0ewant to fix...
Ohhh, I decided to not dig into this part. May I make a request instead? :)
Could you help me to come up with a Rust patch that does what's needed for this series to drop VALIDATE and VMS_MUST_EXIST and replace this one (drop my Author), meanwhile matches the best you'd like to? It'll be much easier to me then because my Rust is rust (this is not strict; it never shined), so I can just pick it up when repost.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
Quoted 28 lines
Quoted 19 lines
And I find C side (in patch 3) handles this differently via using the local "
num_timers".In this case, I think we still have the option to keep the logic consistent between C and Rust :). We can add
VMSTATE_STRUCT_VARRAY_TESTfor C side:#define VMSTATE_STRUCT_VARRAY_TEST(_field, _state, _field_num, _test,\ _version, _vmsd, _type) {\ .name = (stringify(_field)),\ .num_indirect = vmstate_field_offset(_state, _field_num),\ .version_id = (_version),\ .field_exists = (_test),\ .vmsd = &(_vmsd),\ .size = sizeof(_type),\ .flags = VMS_STRUCT | VMS_VARRAY,\ .offset = vmstate_offset_varray(_state, _field, _type),\ }Quoted 5 lines
and make C hpet use this:
VMSTATE_STRUCT_VARRAY_TEST(timer, HPETState,
num_timers_save,
hpet_validate_num_timers, 0,vmstate_hpet_timer, HPETTimer),One thing to mention is, here the
test()function is not for validation, but for checking if the field exists.
Quoted 8 lines
It means, assuming dest QEMU has num_timers=8, source QEMU has num_timers=4, when migration happens with your
VARRAY_TEST(), it won't sanity check it, but since it saw 4!=8 it will assume the whole VARRAY doesn't exist.That's one reason why I want to get rid of
VALIDATE(), because it's abusingfield_exist()IMO, and we don't necessarily need such abuse when we havepost_load.
I see, this makes sense.
Quoted 6 lines
Quoted 4 lines
Alternatively, we can also have Rust use
num_timers(as u8) for timers array VMState, but this will bring back the conversion issue between u8 and usize, which is the commitb3bf86b8936d&6e85cfe44c0ewant to fix...Ohhh, I decided to not dig into this part. May I make a request instead? :)
Happy to help :-)
Quoted 5 lines
Could you help me to come up with a Rust patch that does what's needed for this series to drop VALIDATE and
VMS_MUST_EXISTand replace this one (drop my Author), meanwhile matches the best you'd like to? It'll be much easier to me then because my Rust is rust (this is not strict; it never shined), so I can just pick it up when repost.
I find it's possible to use num_timers directly without going back to u8 type. Since the commit 1f0b40dccfe5 ("migration: Remove redundant flags"), C side has already dropped VMS_VARRAY_UINT8/16/32, so Rust side can use usize as the array length type (without migrating it), like:
unsafe impl VMState for usize {
const BASE: VMStateField = {
panic!("usize cannot be migrated, use a fixed-size integer type");
};
const VARRAY_FLAG: VMStateFlags = VMStateFlags::VMS_VARRAY;
}I'll think more/cook a patch based on this approach.
Thanks,
Zhao
Re: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
Quoted 59 lines · Zhao Liu
Quoted 28 lines
Quoted 19 lines
And I find C side (in patch 3) handles this differently via using the local "
num_timers".In this case, I think we still have the option to keep the logic consistent between C and Rust :). We can add
VMSTATE_STRUCT_VARRAY_TESTfor C side:#define VMSTATE_STRUCT_VARRAY_TEST(_field, _state, _field_num, _test,\ _version, _vmsd, _type) {\ .name = (stringify(_field)),\ .num_indirect = vmstate_field_offset(_state, _field_num),\ .version_id = (_version),\ .field_exists = (_test),\ .vmsd = &(_vmsd),\ .size = sizeof(_type),\ .flags = VMS_STRUCT | VMS_VARRAY,\ .offset = vmstate_offset_varray(_state, _field, _type),\ }Quoted 5 lines
and make C hpet use this:
VMSTATE_STRUCT_VARRAY_TEST(timer, HPETState,
num_timers_save,
hpet_validate_num_timers, 0,vmstate_hpet_timer, HPETTimer),One thing to mention is, here the
test()function is not for validation, but for checking if the field exists.Quoted 8 lines
It means, assuming dest QEMU has num_timers=8, source QEMU has num_timers=4, when migration happens with your
VARRAY_TEST(), it won't sanity check it, but since it saw 4!=8 it will assume the whole VARRAY doesn't exist.That's one reason why I want to get rid of
VALIDATE(), because it's abusingfield_exist()IMO, and we don't necessarily need such abuse when we havepost_load.I see, this makes sense.
Quoted 6 lines
Quoted 4 lines
Alternatively, we can also have Rust use
num_timers(as u8) for timers array VMState, but this will bring back the conversion issue between u8 and usize, which is the commitb3bf86b8936d&6e85cfe44c0ewant to fix...Ohhh, I decided to not dig into this part. May I make a request instead? :)
Happy to help :-)
Quoted 5 lines
Could you help me to come up with a Rust patch that does what's needed for this series to drop VALIDATE and
VMS_MUST_EXISTand replace this one (drop my Author), meanwhile matches the best you'd like to? It'll be much easier to me then because my Rust is rust (this is not strict; it never shined), so I can just pick it up when repost.I find it's possible to use
num_timersdirectly without going back to u8 type. Since the commit1f0b40dccfe5("migration: Remove redundant flags"), C side has already dropped VMS_VARRAY_UINT8/16/32, so Rust side can use usize as the array length type (without migrating it), like:
Quoted 8 lines
unsafe impl VMState for usize {
const BASE: VMStateField = { panic!("usize cannot be migrated, use a fixed-size integer type"); }; const VARRAY_FLAG: VMStateFlags = VMStateFlags::VMS_VARRAY; }I'll think more/cook a patch based on this approach.
Thank you! For me I don't need everything, a minimum patch to make the next patch pass would work. But anyway I'll wait for that. If your series will be separate and stand on its own, I'll just rebase to it.
Signature · 1 line
Peter Xu
PATCH 6/6 migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST
In reply to Peter XuWe have moved all users to other API, now they're never used.
--- include/migration/vmstate.h | 11 ----------- migration/savevm.c | 5 ----- migration/vmstate.c | 12 ------------ 3 files changed, 28 deletions(-)
include/migration/vmstate.h +0 -11
@@ -130,10 +130,6 @@ enum VMStateFlags {* allocated. Only valid in combination with VMS_VBUFFER. */ VMS_MULTIPLY = 0x200, -/* Fail loading the serialised VM state if this field is missing -* from the input. */ - VMS_MUST_EXIST = 0x1000, -/* When loading serialised VM state, allocate memory for the * (entire) field. Only valid in combination with * VMS_POINTER. Note: Not all combinations with other flags are
@@ -458,13 +454,6 @@ extern const VMStateInfo vmstate_info_g_byte_array; .offset = vmstate_offset_value(_state, _field, _type), \ } -/* Validate state using a boolean predicate. */ -#define VMSTATE_VALIDATE (_name, _test) { \ - .name = (_name), \ - .field_exists = (_test), \ - .flags = VMS_MUST_EXIST | VMS_NO_STATE, \ -} -#define VMSTATE_POINTER (_field, _state, _version, _info, _type) { \ .name = (stringify(_field)), \ .version_id = (_version), \
migration/savevm.c +0 -5
@@ -622,11 +622,6 @@ static void dump_vmstate_vmsd(FILE *out_file,fprintf (out_file,",\n%*s\"Fields\": [\n" , indent,"" ); first = true;while (field->name !=NULL ) { -if (field->flags &VMS_MUST_EXIST ) { -/* Ignore VMSTATE_VALIDATE bits; these don't get migrated */ - field++; -continue ; - }if (!first) {fprintf (out_file,",\n" ); }
migration/vmstate.c +0 -12
@@ -116,8 +116,6 @@ static uint64_t vmstate_n_elems(void *opaque, const VMStateField *field) n_elems = field->num; }else if (field->flags &VMS_VARRAY ) { n_elems =vmstate_read_from_offset (&field->num_indirect, opaque); - }else if (field->flags &VMS_MUST_EXIST && field->flags &VMS_NO_STATE ) { - n_elems =0 ; }else { n_elems =1 ; }
@@ -430,10 +428,6 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,return false; } } - }else if (field->flags &VMS_MUST_EXIST ) { -error_setg (errp,"Input validation failed: %s/%s version_id: %d" , - vmsd->name, field->name, vmsd->version_id); -return false; } field++; }
@@ -810,12 +804,6 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd, vmdesc_loop =NULL ; } } - }else { -if (field->flags &VMS_MUST_EXIST ) { -error_report ("Output state validation failed: %s/%s" , - vmsd->name, field->name); -assert (!(field->flags &VMS_MUST_EXIST )); - } } field++; }
Signature · 1 line
2.56.0