All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST
@ 2026-11-20 12:49 Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 1/6] pcie/aer: Remove the usage of VMSTATE_VALIDATE() Peter Xu
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Fabiano Rosas, peterx

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/20261119165546.3050013-1-peter.maydell@linaro.org

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

-- 
2.56.0



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH for-12.0 1/6] pcie/aer: Remove the usage of VMSTATE_VALIDATE()
  2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
@ 2026-11-20 12:49 ` Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 2/6] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp() Peter Xu
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Fabiano Rosas, peterx, Michael S. Tsirkin

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.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/pci/pcie_aer.c | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/hw/pci/pcie_aer.c b/hw/pci/pcie_aer.c
index 43b14c1f22..87248556d4 100644
--- a/hw/pci/pcie_aer.c
+++ b/hw/pci/pcie_aer.c
@@ -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()
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH for-12.0 2/6] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp()
  2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 1/6] pcie/aer: Remove the usage of VMSTATE_VALIDATE() Peter Xu
@ 2026-11-20 12:49 ` Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 3/6] hpet: Move VMSTATE_VALIDATE() check into post_load Peter Xu
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Fabiano Rosas, peterx, qemu-arm

To pave way for removing VMSTATE_VALIDATE(), remove the use in this file by
moving it into the post_load_errp().

Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-arm@nongnu.org
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/gpio/zaurus.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/hw/gpio/zaurus.c b/hw/gpio/zaurus.c
index 3cf626a550..a60f4d4393 100644
--- a/hw/gpio/zaurus.c
+++ b/hw/gpio/zaurus.c
@@ -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),
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH for-12.0 3/6] hpet: Move VMSTATE_VALIDATE() check into post_load
  2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 1/6] pcie/aer: Remove the usage of VMSTATE_VALIDATE() Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 2/6] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp() Peter Xu
@ 2026-11-20 12:49 ` Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 4/6] arm/machine: Move all VMSTATE_VALIDATE() usages " Peter Xu
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Fabiano Rosas, peterx, Michael S. Tsirkin,
	Paolo Bonzini

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

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/timer/hpet.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/hw/timer/hpet.c b/hw/timer/hpet.c
index 42285cff76..85cd26a454 100644
--- a/hw/timer/hpet.c
+++ b/hw/timer/hpet.c
@@ -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()
     },
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH for-12.0 4/6] arm/machine: Move all VMSTATE_VALIDATE() usages into post_load
  2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
                   ` (2 preceding siblings ...)
  2026-11-20 12:49 ` [PATCH for-12.0 3/6] hpet: Move VMSTATE_VALIDATE() check into post_load Peter Xu
@ 2026-11-20 12:49 ` Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load Peter Xu
  2026-11-20 12:49 ` [PATCH for-12.0 6/6] migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
  5 siblings, 0 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Fabiano Rosas, peterx, qemu-arm

To pave way removing VMSTATE_VALIDATE(), move all four such uses into a
post_load hook instead.

Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-arm@nongnu.org
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 target/arm/machine.c | 45 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/target/arm/machine.c b/target/arm/machine.c
index c3aef3f330..ec6daf1ab9 100644
--- a/target/arm/machine.c
+++ b/target/arm/machine.c
@@ -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,
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
  2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
                   ` (3 preceding siblings ...)
  2026-11-20 12:49 ` [PATCH for-12.0 4/6] arm/machine: Move all VMSTATE_VALIDATE() usages " Peter Xu
@ 2026-11-20 12:49 ` Peter Xu
  2026-11-20 14:25   ` Zhao Liu
  2026-11-20 12:49 ` [PATCH for-12.0 6/6] migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
  5 siblings, 1 reply; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Fabiano Rosas, peterx, Manos Pitsidianakis,
	Zhao Liu, Paolo Bonzini, qemu-rust

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

Cc: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Cc: Zhao Liu <zhao1.liu@intel.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-rust@nongnu.org
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 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(-)

diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 89ba7a5c4d..da0540f528 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -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_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;
@@ -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!(
diff --git a/rust/migration/src/prelude.rs b/rust/migration/src/prelude.rs
index 797e7ec59d..e2cbbda43e 100644
--- a/rust/migration/src/prelude.rs
+++ b/rust/migration/src/prelude.rs
@@ -16,4 +16,3 @@
 pub use crate::vmstate_of;
 pub use crate::vmstate_subsections;
 pub use crate::vmstate_unused;
-pub use crate::vmstate_validate;
diff --git a/rust/migration/src/vmstate.rs b/rust/migration/src/vmstate.rs
index 5ed061f2ab..0c8b92f9d6 100644
--- a/rust/migration/src/vmstate.rs
+++ b/rust/migration/src/vmstate.rs
@@ -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
diff --git a/rust/tests/tests/vmstate_tests.rs b/rust/tests/tests/vmstate_tests.rs
index 6578d54b37..f4db02abac 100644
--- a/rust/tests/tests/vmstate_tests.rs
+++ b/rust/tests/tests/vmstate_tests.rs
@@ -406,80 +406,3 @@ fn test_vmstate_macro_array_of_pointer_wrapped() {
     // The last VMStateField in VMSTATE_FOOC.
     assert_eq!(foo_fields[4].flags, VMStateFlags::VMS_END);
 }
-
-// =========================== Test VMSTATE_FOOD ===========================
-// Test the use cases of the vmstate macro, corresponding to the following C
-// macro variants:
-//   * VMSTATE_FOOD:
-//     - VMSTATE_VALIDATE
-
-// Add more member fields when vmstate_of support "test" parameter.
-struct FooD;
-
-impl FooD {
-    fn validate_food_0(&self, _version_id: u8) -> bool {
-        true
-    }
-
-    fn validate_food_1(_state: &FooD, _version_id: u8) -> bool {
-        false
-    }
-}
-
-fn validate_food_2(_state: &FooD, _version_id: u8) -> bool {
-    true
-}
-
-static VMSTATE_FOOD: VMStateDescription<FooD> = VMStateDescriptionBuilder::<FooD>::new()
-    .name(c"foo_d")
-    .version_id(3)
-    .minimum_version_id(1)
-    .fields(vmstate_fields! {
-        vmstate_validate!(FooD, c"foo_d_0", FooD::validate_food_0),
-        vmstate_validate!(FooD, c"foo_d_1", FooD::validate_food_1),
-        vmstate_validate!(FooD, c"foo_d_2", validate_food_2),
-    })
-    .build();
-
-#[test]
-fn test_vmstate_validate() {
-    let foo_fields: &[VMStateField] =
-        unsafe { slice::from_raw_parts(VMSTATE_FOOD.as_ref().fields, 4) };
-    let mut foo_d = FooD;
-    let foo_d_p = std::ptr::addr_of_mut!(foo_d).cast::<c_void>();
-
-    // 1st VMStateField in VMSTATE_FOOD
-    assert_eq!(
-        unsafe { CStr::from_ptr(foo_fields[0].name) }.to_bytes_with_nul(),
-        b"foo_d_0\0"
-    );
-    assert_eq!(foo_fields[0].offset, 0);
-    assert_eq!(foo_fields[0].num_indirect.size, 0);
-    assert!(foo_fields[0].info.is_null());
-    assert_eq!(foo_fields[0].version_id, 0);
-    assert_eq!(foo_fields[0].size, 0);
-    assert_eq!(foo_fields[0].num, 0);
-    assert_eq!(
-        foo_fields[0].flags.0,
-        VMStateFlags::VMS_NO_STATE.0 | VMStateFlags::VMS_MUST_EXIST.0
-    );
-    assert!(foo_fields[0].vmsd.is_null());
-    assert!(unsafe { foo_fields[0].field_exists.unwrap()(foo_d_p, 0) });
-
-    // 2nd VMStateField in VMSTATE_FOOD
-    assert_eq!(
-        unsafe { CStr::from_ptr(foo_fields[1].name) }.to_bytes_with_nul(),
-        b"foo_d_1\0"
-    );
-    assert!(!unsafe { foo_fields[1].field_exists.unwrap()(foo_d_p, 1) });
-
-    // 3rd VMStateField in VMSTATE_FOOD
-    assert_eq!(
-        unsafe { CStr::from_ptr(foo_fields[2].name) }.to_bytes_with_nul(),
-        b"foo_d_2\0"
-    );
-    assert!(unsafe { foo_fields[2].field_exists.unwrap()(foo_d_p, 2) });
-
-    // The last VMStateField in VMSTATE_FOOD.
-    assert_eq!(foo_fields[3].flags, VMStateFlags::VMS_END);
-}
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH for-12.0 6/6] migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST
  2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
                   ` (4 preceding siblings ...)
  2026-11-20 12:49 ` [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load Peter Xu
@ 2026-11-20 12:49 ` Peter Xu
  5 siblings, 0 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 12:49 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Fabiano Rosas, peterx

We have moved all users to other API, now they're never used.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/migration/vmstate.h | 11 -----------
 migration/savevm.c          |  5 -----
 migration/vmstate.c         | 12 ------------
 3 files changed, 28 deletions(-)

diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h
index 2901877308..de7a957140 100644
--- a/include/migration/vmstate.h
+++ b/include/migration/vmstate.h
@@ -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),                                        \
diff --git a/migration/savevm.c b/migration/savevm.c
index fe94ba96e4..e4f614cc86 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -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");
             }
diff --git a/migration/vmstate.c b/migration/vmstate.c
index 24004565bf..7734caab06 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -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++;
     }
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
  2026-11-20 12:49 ` [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load Peter Xu
@ 2026-11-20 14:25   ` Zhao Liu
  2026-11-20 15:43     ` Peter Xu
  0 siblings, 1 reply; 9+ messages in thread
From: Zhao Liu @ 2026-11-20 14:25 UTC (permalink / raw)
  To: Peter Xu
  Cc: qemu-devel, Peter Maydell, Fabiano Rosas, Manos Pitsidianakis,
	Paolo Bonzini, qemu-rust, Zhao Liu

Hi Peter,

On Fri, Nov 20, 2026 at 07:49:22AM -0500, Peter Xu wrote:
> Date: Fri, 20 Nov 2026 07:49:22 -0500
> From: Peter Xu <peterx@redhat.com>
> Subject: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
> X-Mailer: git-send-email 2.56.0
> 
> We dropped all of that in C, drop that in Rust too.

but rust is using the different way to address this case :)

> The goal is to remove
> the VMSTATE_VALIDATE() together with VMS_MUST_EXIST.
> 
> Use post_load instead for the check.

...

>  // 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),
                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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).

>          })

So LGTM for the change in rust hpet,

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>


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



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
  2026-11-20 14:25   ` Zhao Liu
@ 2026-11-20 15:43     ` Peter Xu
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Xu @ 2026-11-20 15:43 UTC (permalink / raw)
  To: Zhao Liu
  Cc: qemu-devel, Peter Maydell, Fabiano Rosas, Manos Pitsidianakis,
	Paolo Bonzini, qemu-rust

On Fri, Nov 20, 2026 at 10:25:23PM +0800, Zhao Liu wrote:
> Hi Peter,

Hi, Zhao,

> 
> On Fri, Nov 20, 2026 at 07:49:22AM -0500, Peter Xu wrote:
> > Date: Fri, 20 Nov 2026 07:49:22 -0500
> > From: Peter Xu <peterx@redhat.com>
> > Subject: [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load
> > X-Mailer: git-send-email 2.56.0
> > 
> > We 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. :)

> 
> > The goal is to remove
> > the VMSTATE_VALIDATE() together with VMS_MUST_EXIST.
> > 
> > Use post_load instead for the check.
> 
> ...
> 
> >  // 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),
>                                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 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).

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.

> 
> >          })
> 
> So LGTM for the change in rust hpet,
> 
> Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
> 
> 
> 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),

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.

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

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,

-- 
Peter Xu



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-20 15:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 12:49 [PATCH for-12.0 0/6] migration: Remove VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 1/6] pcie/aer: Remove the usage of VMSTATE_VALIDATE() Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 2/6] hw/gpio/zaurus: Move VMSTATE_VALIDATE() check into post_load_errp() Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 3/6] hpet: Move VMSTATE_VALIDATE() check into post_load Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 4/6] arm/machine: Move all VMSTATE_VALIDATE() usages " Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 5/6] rust: Replace vmstate_validate with post_load Peter Xu
2026-11-20 14:25   ` Zhao Liu
2026-11-20 15:43     ` Peter Xu
2026-11-20 12:49 ` [PATCH for-12.0 6/6] migration: Drop VMSTATE_VALIDATE() and VMS_MUST_EXIST Peter Xu

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.