From: Vladimir Sementsov-Ogievskiy Date: Wed, 4 Mar 2026 21:22:47 +0000 (+0300) Subject: migration: make .post_save() a void function X-Git-Tag: v11.1.0-rc0~157^2~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ef2045832ea414faa719d75a64bb20583c88353f;p=thirdparty%2Fqemu.git migration: make .post_save() a void function All other handlers now have _errp() variants. Should we go this way for .post_save()? Actually it's rather strange, when the vmstate do successful preparations in .pre_save(), then successfully save all sections and subsections, end then fail when all the state is successfully transferred to the target. Happily, we have only three .post_save() realizations, all always successful. Let's make this a rule. Also note, that we call .post_save() in two places, and handle its (theoretical) failure inconsistently. Fix that too. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Peter Xu Reviewed-by: Zhao Liu #rust Link: https://lore.kernel.org/qemu-devel/20260304212303.667141-4-vsementsov@yandex-team.ru Signed-off-by: Fabiano Rosas --- diff --git a/docs/devel/migration/main.rst b/docs/devel/migration/main.rst index 234d280249a..2de70507640 100644 --- a/docs/devel/migration/main.rst +++ b/docs/devel/migration/main.rst @@ -439,7 +439,7 @@ The functions to do that are inside a vmstate definition, and are called: This function is called before we save the state of one device. -- ``int (*post_save)(void *opaque);`` +- ``void (*post_save)(void *opaque);`` This function is called after we save the state of one device (even upon failure, unless the call to pre_save returned an error). diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index ea998bdff15..1dc3b02659f 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -2093,14 +2093,13 @@ static int spapr_pci_pre_save(void *opaque) return 0; } -static int spapr_pci_post_save(void *opaque) +static void spapr_pci_post_save(void *opaque) { SpaprPhbState *sphb = opaque; g_free(sphb->msi_devs); sphb->msi_devs = NULL; sphb->msi_devs_num = 0; - return 0; } static int spapr_pci_post_load(void *opaque, int version_id) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 78d97151bd5..9d42cf7a64d 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -223,7 +223,13 @@ struct VMStateDescription { bool (*post_load_errp)(void *opaque, int version_id, Error **errp); int (*pre_save)(void *opaque); bool (*pre_save_errp)(void *opaque, Error **errp); - int (*post_save)(void *opaque); + + /* + * Unless .pre_save() fails, .post_save() is called after saving + * fields and subsections. It should not fail because at this + * point the state has potentially already been transferred. + */ + void (*post_save)(void *opaque); bool (*needed)(void *opaque); bool (*dev_unplug_pending)(void *opaque); diff --git a/migration/savevm.c b/migration/savevm.c index dd58f2a7051..699d2c9f8b9 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -321,14 +321,13 @@ static int configuration_pre_save(void *opaque) return 0; } -static int configuration_post_save(void *opaque) +static void configuration_post_save(void *opaque) { SaveState *state = opaque; g_free(state->capabilities); state->capabilities = NULL; state->caps_count = 0; - return 0; } static int configuration_pre_load(void *opaque) diff --git a/migration/vmstate.c b/migration/vmstate.c index 651c3fe0115..5111e7a141f 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -550,10 +550,7 @@ static int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd, if (ret) { error_prepend(errp, "Save of field %s/%s failed: ", vmsd->name, field->name); - if (vmsd->post_save) { - vmsd->post_save(opaque); - } - return ret; + goto out; } /* Compressed arrays only care about the first element */ @@ -578,12 +575,9 @@ static int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd, ret = vmstate_subsection_save(f, vmsd, opaque, vmdesc, errp); +out: if (vmsd->post_save) { - int ps_ret = vmsd->post_save(opaque); - if (!ret && ps_ret) { - ret = ps_ret; - error_setg(errp, "post-save failed: %s", vmsd->name); - } + vmsd->post_save(opaque); } return ret; } diff --git a/rust/migration/src/migratable.rs b/rust/migration/src/migratable.rs index 7748aac2f27..b9e5e1fc15d 100644 --- a/rust/migration/src/migratable.rs +++ b/rust/migration/src/migratable.rs @@ -406,10 +406,8 @@ impl Migratable { Ok(()) } - fn post_save(&self) -> Result<(), InvalidError> { - let state = unsafe { Box::from_raw(self.migration_state.replace(ptr::null_mut())) }; - drop(state); - Ok(()) + fn post_save(&self) { + let _ = unsafe { Box::from_raw(self.migration_state.replace(ptr::null_mut())) }; } fn pre_load(&self) -> Result<(), InvalidError> { diff --git a/rust/migration/src/vmstate.rs b/rust/migration/src/vmstate.rs index edc7c702656..f34a36f6809 100644 --- a/rust/migration/src/vmstate.rs +++ b/rust/migration/src/vmstate.rs @@ -492,6 +492,11 @@ unsafe extern "C" fn vmstate_no_version_cb< into_neg_errno(result) } +unsafe extern "C" fn vmstate_post_save_cb FnCall<(&'a T,), ()>>(opaque: *mut c_void) { + // SAFETY: the function is used in T's implementation of VMState. + F::call((unsafe { &*(opaque.cast::()) },)); +} + unsafe extern "C" fn vmstate_post_load_cb< T, F: for<'a> FnCall<(&'a T, u8), Result<(), impl Into>>, @@ -597,12 +602,9 @@ impl VMStateDescriptionBuilder { } #[must_use] - pub const fn post_save FnCall<(&'a T,), Result<(), impl Into>>>( - mut self, - _f: &F, - ) -> Self { + pub const fn post_save FnCall<(&'a T,), ()>>(mut self, _f: &F) -> Self { self.0.post_save = if F::IS_SOME { - Some(vmstate_no_version_cb::) + Some(vmstate_post_save_cb::) } else { None }; diff --git a/target/arm/machine.c b/target/arm/machine.c index b0e499515cf..50d80ffb68c 100644 --- a/target/arm/machine.c +++ b/target/arm/machine.c @@ -998,7 +998,7 @@ static int cpu_pre_save(void *opaque) return 0; } -static int cpu_post_save(void *opaque) +static void cpu_post_save(void *opaque) { ARMCPU *cpu = opaque; @@ -1008,8 +1008,6 @@ static int cpu_post_save(void *opaque) cpu->cpreg_vmstate_indexes = NULL; cpu->cpreg_vmstate_values = NULL; - - return 0; } static int cpu_pre_load(void *opaque)