diff mbox series

[V1,06/26] migration: precreate vmstate for exec

Message ID 1714406135-451286-7-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New
Headers show
Series Live update: cpr-exec | expand

Commit Message

Steven Sistare April 29, 2024, 3:55 p.m. UTC
Provide migration_precreate_save for saving precreate vmstate across exec.
Create a memfd, save its value in the environment, and serialize state
to it.  Reverse the process in migration_precreate_load.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 include/migration/misc.h |   5 ++
 migration/meson.build    |   1 +
 migration/precreate.c    | 139 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+)
 create mode 100644 migration/precreate.c

Comments

Fabiano Rosas May 6, 2024, 11:34 p.m. UTC | #1
Steve Sistare <steven.sistare@oracle.com> writes:

> Provide migration_precreate_save for saving precreate vmstate across exec.
> Create a memfd, save its value in the environment, and serialize state
> to it.  Reverse the process in migration_precreate_load.
>
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> ---
>  include/migration/misc.h |   5 ++
>  migration/meson.build    |   1 +
>  migration/precreate.c    | 139 +++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 145 insertions(+)
>  create mode 100644 migration/precreate.c
>
> diff --git a/include/migration/misc.h b/include/migration/misc.h
> index c9e200f..cf30351 100644
> --- a/include/migration/misc.h
> +++ b/include/migration/misc.h
> @@ -56,6 +56,11 @@ AnnounceParameters *migrate_announce_params(void);
>  
>  void dump_vmstate_json_to_file(FILE *out_fp);
>  
> +/* migration/precreate.c */
> +int migration_precreate_save(Error **errp);
> +void migration_precreate_unsave(void);
> +int migration_precreate_load(Error **errp);
> +
>  /* migration/migration.c */
>  void migration_object_init(void);
>  void migration_shutdown(void);
> diff --git a/migration/meson.build b/migration/meson.build
> index f76b1ba..50e7cb2 100644
> --- a/migration/meson.build
> +++ b/migration/meson.build
> @@ -26,6 +26,7 @@ system_ss.add(files(
>    'ram-compress.c',
>    'options.c',
>    'postcopy-ram.c',
> +  'precreate.c',
>    'savevm.c',
>    'socket.c',
>    'tls.c',
> diff --git a/migration/precreate.c b/migration/precreate.c
> new file mode 100644
> index 0000000..0bf5e1f
> --- /dev/null
> +++ b/migration/precreate.c
> @@ -0,0 +1,139 @@
> +/*
> + * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qemu/cutils.h"
> +#include "qemu/memfd.h"
> +#include "qapi/error.h"
> +#include "io/channel-file.h"
> +#include "migration/misc.h"
> +#include "migration/qemu-file.h"
> +#include "migration/savevm.h"
> +
> +#define PRECREATE_STATE_NAME "QEMU_PRECREATE_STATE"
> +
> +static QEMUFile *qemu_file_new_fd_input(int fd, const char *name)
> +{
> +    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
> +    QIOChannel *ioc = QIO_CHANNEL(fioc);
> +    qio_channel_set_name(ioc, name);
> +    return qemu_file_new_input(ioc);
> +}
> +
> +static QEMUFile *qemu_file_new_fd_output(int fd, const char *name)
> +{
> +    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
> +    QIOChannel *ioc = QIO_CHANNEL(fioc);
> +    qio_channel_set_name(ioc, name);
> +    return qemu_file_new_output(ioc);
> +}
> +
> +static int memfd_create_named(const char *name, Error **errp)
> +{
> +    int mfd;
> +    char val[16];
> +
> +    mfd = memfd_create(name, 0);
> +    if (mfd < 0) {
> +        error_setg_errno(errp, errno, "memfd_create failed");
> +        return -1;
> +    }
> +
> +    /* Remember mfd in environment for post-exec load */
> +    qemu_clear_cloexec(mfd);
> +    snprintf(val, sizeof(val), "%d", mfd);
> +    g_setenv(name, val, 1);
> +
> +    return mfd;
> +}
> +
> +static int memfd_find_named(const char *name, int *mfd_p, Error **errp)
> +{
> +    const char *val = g_getenv(name);
> +
> +    if (!val) {
> +        *mfd_p = -1;
> +        return 0;       /* No memfd was created, not an error */
> +    }
> +    g_unsetenv(name);
> +    if (qemu_strtoi(val, NULL, 10, mfd_p)) {
> +        error_setg(errp, "Bad %s env value %s", PRECREATE_STATE_NAME, val);
> +        return -1;
> +    }
> +    lseek(*mfd_p, 0, SEEK_SET);
> +    return 0;
> +}
> +
> +static void memfd_delete_named(const char *name)
> +{
> +    int mfd;
> +    const char *val = g_getenv(name);
> +
> +    if (val) {
> +        g_unsetenv(name);
> +        if (!qemu_strtoi(val, NULL, 10, &mfd)) {
> +            close(mfd);
> +        }
> +    }
> +}
> +
> +static QEMUFile *qemu_file_new_memfd_output(const char *name, Error **errp)
> +{
> +    int mfd = memfd_create_named(name, errp);
> +
> +    if (mfd < 0) {
> +        return NULL;
> +    }
> +
> +    return qemu_file_new_fd_output(mfd, name);
> +}
> +
> +static QEMUFile *qemu_file_new_memfd_input(const char *name, Error **errp)
> +{
> +    int ret, mfd;
> +
> +    ret = memfd_find_named(name, &mfd, errp);
> +    if (ret || mfd < 0) {
> +        return NULL;
> +    }
> +
> +    return qemu_file_new_fd_input(mfd, name);
> +}
> +
> +int migration_precreate_save(Error **errp)
> +{
> +    QEMUFile *f = qemu_file_new_memfd_output(PRECREATE_STATE_NAME, errp);
> +
> +    if (!f) {
> +        return -1;
> +    } else if (qemu_savevm_precreate_save(f, errp)) {
> +        memfd_delete_named(PRECREATE_STATE_NAME);
> +        return -1;
> +    } else {
> +        /* Do not close f, as mfd must remain open. */
> +        return 0;
> +    }
> +}
> +
> +void migration_precreate_unsave(void)
> +{
> +    memfd_delete_named(PRECREATE_STATE_NAME);
> +}
> +
> +int migration_precreate_load(Error **errp)
> +{
> +    int ret;
> +    QEMUFile *f = qemu_file_new_memfd_input(PRECREATE_STATE_NAME, errp);

Can we avoid the QEMUFile? I don't see it being exported from this file.

> +
> +    if (!f) {
> +        return -1;
> +    }
> +    ret = qemu_savevm_precreate_load(f, errp);
> +    qemu_fclose(f);
> +    g_unsetenv(PRECREATE_STATE_NAME);
> +    return ret;
> +}
Steven Sistare May 13, 2024, 7:28 p.m. UTC | #2
On 5/6/2024 7:34 PM, Fabiano Rosas wrote:
> Steve Sistare <steven.sistare@oracle.com> writes:
> 
>> Provide migration_precreate_save for saving precreate vmstate across exec.
>> Create a memfd, save its value in the environment, and serialize state
>> to it.  Reverse the process in migration_precreate_load.
>>
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>> ---
>>   include/migration/misc.h |   5 ++
>>   migration/meson.build    |   1 +
>>   migration/precreate.c    | 139 +++++++++++++++++++++++++++++++++++++++++++++++
>>   3 files changed, 145 insertions(+)
>>   create mode 100644 migration/precreate.c
>>
>> diff --git a/include/migration/misc.h b/include/migration/misc.h
>> index c9e200f..cf30351 100644
>> --- a/include/migration/misc.h
>> +++ b/include/migration/misc.h
>> @@ -56,6 +56,11 @@ AnnounceParameters *migrate_announce_params(void);
>>   
>>   void dump_vmstate_json_to_file(FILE *out_fp);
>>   
>> +/* migration/precreate.c */
>> +int migration_precreate_save(Error **errp);
>> +void migration_precreate_unsave(void);
>> +int migration_precreate_load(Error **errp);
>> +
>>   /* migration/migration.c */
>>   void migration_object_init(void);
>>   void migration_shutdown(void);
>> diff --git a/migration/meson.build b/migration/meson.build
>> index f76b1ba..50e7cb2 100644
>> --- a/migration/meson.build
>> +++ b/migration/meson.build
>> @@ -26,6 +26,7 @@ system_ss.add(files(
>>     'ram-compress.c',
>>     'options.c',
>>     'postcopy-ram.c',
>> +  'precreate.c',
>>     'savevm.c',
>>     'socket.c',
>>     'tls.c',
>> diff --git a/migration/precreate.c b/migration/precreate.c
>> new file mode 100644
>> index 0000000..0bf5e1f
>> --- /dev/null
>> +++ b/migration/precreate.c
>> @@ -0,0 +1,139 @@
>> +/*
>> + * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
>> + *
>> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
>> + * See the COPYING file in the top-level directory.
>> + */
>> +
>> +#include "qemu/osdep.h"
>> +#include "qemu/cutils.h"
>> +#include "qemu/memfd.h"
>> +#include "qapi/error.h"
>> +#include "io/channel-file.h"
>> +#include "migration/misc.h"
>> +#include "migration/qemu-file.h"
>> +#include "migration/savevm.h"
>> +
>> +#define PRECREATE_STATE_NAME "QEMU_PRECREATE_STATE"
>> +
>> +static QEMUFile *qemu_file_new_fd_input(int fd, const char *name)
>> +{
>> +    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
>> +    QIOChannel *ioc = QIO_CHANNEL(fioc);
>> +    qio_channel_set_name(ioc, name);
>> +    return qemu_file_new_input(ioc);
>> +}
>> +
>> +static QEMUFile *qemu_file_new_fd_output(int fd, const char *name)
>> +{
>> +    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
>> +    QIOChannel *ioc = QIO_CHANNEL(fioc);
>> +    qio_channel_set_name(ioc, name);
>> +    return qemu_file_new_output(ioc);
>> +}
>> +
>> +static int memfd_create_named(const char *name, Error **errp)
>> +{
>> +    int mfd;
>> +    char val[16];
>> +
>> +    mfd = memfd_create(name, 0);
>> +    if (mfd < 0) {
>> +        error_setg_errno(errp, errno, "memfd_create failed");
>> +        return -1;
>> +    }
>> +
>> +    /* Remember mfd in environment for post-exec load */
>> +    qemu_clear_cloexec(mfd);
>> +    snprintf(val, sizeof(val), "%d", mfd);
>> +    g_setenv(name, val, 1);
>> +
>> +    return mfd;
>> +}
>> +
>> +static int memfd_find_named(const char *name, int *mfd_p, Error **errp)
>> +{
>> +    const char *val = g_getenv(name);
>> +
>> +    if (!val) {
>> +        *mfd_p = -1;
>> +        return 0;       /* No memfd was created, not an error */
>> +    }
>> +    g_unsetenv(name);
>> +    if (qemu_strtoi(val, NULL, 10, mfd_p)) {
>> +        error_setg(errp, "Bad %s env value %s", PRECREATE_STATE_NAME, val);
>> +        return -1;
>> +    }
>> +    lseek(*mfd_p, 0, SEEK_SET);
>> +    return 0;
>> +}
>> +
>> +static void memfd_delete_named(const char *name)
>> +{
>> +    int mfd;
>> +    const char *val = g_getenv(name);
>> +
>> +    if (val) {
>> +        g_unsetenv(name);
>> +        if (!qemu_strtoi(val, NULL, 10, &mfd)) {
>> +            close(mfd);
>> +        }
>> +    }
>> +}
>> +
>> +static QEMUFile *qemu_file_new_memfd_output(const char *name, Error **errp)
>> +{
>> +    int mfd = memfd_create_named(name, errp);
>> +
>> +    if (mfd < 0) {
>> +        return NULL;
>> +    }
>> +
>> +    return qemu_file_new_fd_output(mfd, name);
>> +}
>> +
>> +static QEMUFile *qemu_file_new_memfd_input(const char *name, Error **errp)
>> +{
>> +    int ret, mfd;
>> +
>> +    ret = memfd_find_named(name, &mfd, errp);
>> +    if (ret || mfd < 0) {
>> +        return NULL;
>> +    }
>> +
>> +    return qemu_file_new_fd_input(mfd, name);
>> +}
>> +
>> +int migration_precreate_save(Error **errp)
>> +{
>> +    QEMUFile *f = qemu_file_new_memfd_output(PRECREATE_STATE_NAME, errp);
>> +
>> +    if (!f) {
>> +        return -1;
>> +    } else if (qemu_savevm_precreate_save(f, errp)) {
>> +        memfd_delete_named(PRECREATE_STATE_NAME);
>> +        return -1;
>> +    } else {
>> +        /* Do not close f, as mfd must remain open. */
>> +        return 0;
>> +    }
>> +}
>> +
>> +void migration_precreate_unsave(void)
>> +{
>> +    memfd_delete_named(PRECREATE_STATE_NAME);
>> +}
>> +
>> +int migration_precreate_load(Error **errp)
>> +{
>> +    int ret;
>> +    QEMUFile *f = qemu_file_new_memfd_input(PRECREATE_STATE_NAME, errp);
> 
> Can we avoid the QEMUFile? I don't see it being exported from this file.

It is not exported, but within this file, it is the basis for all read and
write operations, via the existing functions qemu_file_new_input() and 
qemu_file_new_output()

- Steve

>> +
>> +    if (!f) {
>> +        return -1;
>> +    }
>> +    ret = qemu_savevm_precreate_load(f, errp);
>> +    qemu_fclose(f);
>> +    g_unsetenv(PRECREATE_STATE_NAME);
>> +    return ret;
>> +}
Fabiano Rosas May 13, 2024, 9:21 p.m. UTC | #3
Steven Sistare <steven.sistare@oracle.com> writes:

> On 5/6/2024 7:34 PM, Fabiano Rosas wrote:
>> Steve Sistare <steven.sistare@oracle.com> writes:
>> 
>>> Provide migration_precreate_save for saving precreate vmstate across exec.
>>> Create a memfd, save its value in the environment, and serialize state
>>> to it.  Reverse the process in migration_precreate_load.
>>>
>>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>>> ---
>>>   include/migration/misc.h |   5 ++
>>>   migration/meson.build    |   1 +
>>>   migration/precreate.c    | 139 +++++++++++++++++++++++++++++++++++++++++++++++
>>>   3 files changed, 145 insertions(+)
>>>   create mode 100644 migration/precreate.c
>>>
>>> diff --git a/include/migration/misc.h b/include/migration/misc.h
>>> index c9e200f..cf30351 100644
>>> --- a/include/migration/misc.h
>>> +++ b/include/migration/misc.h
>>> @@ -56,6 +56,11 @@ AnnounceParameters *migrate_announce_params(void);
>>>   
>>>   void dump_vmstate_json_to_file(FILE *out_fp);
>>>   
>>> +/* migration/precreate.c */
>>> +int migration_precreate_save(Error **errp);
>>> +void migration_precreate_unsave(void);
>>> +int migration_precreate_load(Error **errp);
>>> +
>>>   /* migration/migration.c */
>>>   void migration_object_init(void);
>>>   void migration_shutdown(void);
>>> diff --git a/migration/meson.build b/migration/meson.build
>>> index f76b1ba..50e7cb2 100644
>>> --- a/migration/meson.build
>>> +++ b/migration/meson.build
>>> @@ -26,6 +26,7 @@ system_ss.add(files(
>>>     'ram-compress.c',
>>>     'options.c',
>>>     'postcopy-ram.c',
>>> +  'precreate.c',
>>>     'savevm.c',
>>>     'socket.c',
>>>     'tls.c',
>>> diff --git a/migration/precreate.c b/migration/precreate.c
>>> new file mode 100644
>>> index 0000000..0bf5e1f
>>> --- /dev/null
>>> +++ b/migration/precreate.c
>>> @@ -0,0 +1,139 @@
>>> +/*
>>> + * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
>>> + *
>>> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
>>> + * See the COPYING file in the top-level directory.
>>> + */
>>> +
>>> +#include "qemu/osdep.h"
>>> +#include "qemu/cutils.h"
>>> +#include "qemu/memfd.h"
>>> +#include "qapi/error.h"
>>> +#include "io/channel-file.h"
>>> +#include "migration/misc.h"
>>> +#include "migration/qemu-file.h"
>>> +#include "migration/savevm.h"
>>> +
>>> +#define PRECREATE_STATE_NAME "QEMU_PRECREATE_STATE"
>>> +
>>> +static QEMUFile *qemu_file_new_fd_input(int fd, const char *name)
>>> +{
>>> +    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
>>> +    QIOChannel *ioc = QIO_CHANNEL(fioc);
>>> +    qio_channel_set_name(ioc, name);
>>> +    return qemu_file_new_input(ioc);
>>> +}
>>> +
>>> +static QEMUFile *qemu_file_new_fd_output(int fd, const char *name)
>>> +{
>>> +    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
>>> +    QIOChannel *ioc = QIO_CHANNEL(fioc);
>>> +    qio_channel_set_name(ioc, name);
>>> +    return qemu_file_new_output(ioc);
>>> +}
>>> +
>>> +static int memfd_create_named(const char *name, Error **errp)
>>> +{
>>> +    int mfd;
>>> +    char val[16];
>>> +
>>> +    mfd = memfd_create(name, 0);
>>> +    if (mfd < 0) {
>>> +        error_setg_errno(errp, errno, "memfd_create failed");
>>> +        return -1;
>>> +    }
>>> +
>>> +    /* Remember mfd in environment for post-exec load */
>>> +    qemu_clear_cloexec(mfd);
>>> +    snprintf(val, sizeof(val), "%d", mfd);
>>> +    g_setenv(name, val, 1);
>>> +
>>> +    return mfd;
>>> +}
>>> +
>>> +static int memfd_find_named(const char *name, int *mfd_p, Error **errp)
>>> +{
>>> +    const char *val = g_getenv(name);
>>> +
>>> +    if (!val) {
>>> +        *mfd_p = -1;
>>> +        return 0;       /* No memfd was created, not an error */
>>> +    }
>>> +    g_unsetenv(name);
>>> +    if (qemu_strtoi(val, NULL, 10, mfd_p)) {
>>> +        error_setg(errp, "Bad %s env value %s", PRECREATE_STATE_NAME, val);
>>> +        return -1;
>>> +    }
>>> +    lseek(*mfd_p, 0, SEEK_SET);
>>> +    return 0;
>>> +}
>>> +
>>> +static void memfd_delete_named(const char *name)
>>> +{
>>> +    int mfd;
>>> +    const char *val = g_getenv(name);
>>> +
>>> +    if (val) {
>>> +        g_unsetenv(name);
>>> +        if (!qemu_strtoi(val, NULL, 10, &mfd)) {
>>> +            close(mfd);
>>> +        }
>>> +    }
>>> +}
>>> +
>>> +static QEMUFile *qemu_file_new_memfd_output(const char *name, Error **errp)
>>> +{
>>> +    int mfd = memfd_create_named(name, errp);
>>> +
>>> +    if (mfd < 0) {
>>> +        return NULL;
>>> +    }
>>> +
>>> +    return qemu_file_new_fd_output(mfd, name);
>>> +}
>>> +
>>> +static QEMUFile *qemu_file_new_memfd_input(const char *name, Error **errp)
>>> +{
>>> +    int ret, mfd;
>>> +
>>> +    ret = memfd_find_named(name, &mfd, errp);
>>> +    if (ret || mfd < 0) {
>>> +        return NULL;
>>> +    }
>>> +
>>> +    return qemu_file_new_fd_input(mfd, name);
>>> +}
>>> +
>>> +int migration_precreate_save(Error **errp)
>>> +{
>>> +    QEMUFile *f = qemu_file_new_memfd_output(PRECREATE_STATE_NAME, errp);
>>> +
>>> +    if (!f) {
>>> +        return -1;
>>> +    } else if (qemu_savevm_precreate_save(f, errp)) {
>>> +        memfd_delete_named(PRECREATE_STATE_NAME);
>>> +        return -1;
>>> +    } else {
>>> +        /* Do not close f, as mfd must remain open. */
>>> +        return 0;
>>> +    }
>>> +}
>>> +
>>> +void migration_precreate_unsave(void)
>>> +{
>>> +    memfd_delete_named(PRECREATE_STATE_NAME);
>>> +}
>>> +
>>> +int migration_precreate_load(Error **errp)
>>> +{
>>> +    int ret;
>>> +    QEMUFile *f = qemu_file_new_memfd_input(PRECREATE_STATE_NAME, errp);
>> 
>> Can we avoid the QEMUFile? I don't see it being exported from this file.
>
> It is not exported, but within this file, it is the basis for all read and
> write operations, via the existing functions qemu_file_new_input() and 
> qemu_file_new_output()

Right, that's the easy part, we could just use the QIOChannel
directly. But I missed the fact that you also need to interop with the
existing code that needs the QEMUFile.

Reviewed-by: Fabiano Rosas <farosas@suse.de>
diff mbox series

Patch

diff --git a/include/migration/misc.h b/include/migration/misc.h
index c9e200f..cf30351 100644
--- a/include/migration/misc.h
+++ b/include/migration/misc.h
@@ -56,6 +56,11 @@  AnnounceParameters *migrate_announce_params(void);
 
 void dump_vmstate_json_to_file(FILE *out_fp);
 
+/* migration/precreate.c */
+int migration_precreate_save(Error **errp);
+void migration_precreate_unsave(void);
+int migration_precreate_load(Error **errp);
+
 /* migration/migration.c */
 void migration_object_init(void);
 void migration_shutdown(void);
diff --git a/migration/meson.build b/migration/meson.build
index f76b1ba..50e7cb2 100644
--- a/migration/meson.build
+++ b/migration/meson.build
@@ -26,6 +26,7 @@  system_ss.add(files(
   'ram-compress.c',
   'options.c',
   'postcopy-ram.c',
+  'precreate.c',
   'savevm.c',
   'socket.c',
   'tls.c',
diff --git a/migration/precreate.c b/migration/precreate.c
new file mode 100644
index 0000000..0bf5e1f
--- /dev/null
+++ b/migration/precreate.c
@@ -0,0 +1,139 @@ 
+/*
+ * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "qemu/memfd.h"
+#include "qapi/error.h"
+#include "io/channel-file.h"
+#include "migration/misc.h"
+#include "migration/qemu-file.h"
+#include "migration/savevm.h"
+
+#define PRECREATE_STATE_NAME "QEMU_PRECREATE_STATE"
+
+static QEMUFile *qemu_file_new_fd_input(int fd, const char *name)
+{
+    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
+    QIOChannel *ioc = QIO_CHANNEL(fioc);
+    qio_channel_set_name(ioc, name);
+    return qemu_file_new_input(ioc);
+}
+
+static QEMUFile *qemu_file_new_fd_output(int fd, const char *name)
+{
+    g_autoptr(QIOChannelFile) fioc = qio_channel_file_new_fd(fd);
+    QIOChannel *ioc = QIO_CHANNEL(fioc);
+    qio_channel_set_name(ioc, name);
+    return qemu_file_new_output(ioc);
+}
+
+static int memfd_create_named(const char *name, Error **errp)
+{
+    int mfd;
+    char val[16];
+
+    mfd = memfd_create(name, 0);
+    if (mfd < 0) {
+        error_setg_errno(errp, errno, "memfd_create failed");
+        return -1;
+    }
+
+    /* Remember mfd in environment for post-exec load */
+    qemu_clear_cloexec(mfd);
+    snprintf(val, sizeof(val), "%d", mfd);
+    g_setenv(name, val, 1);
+
+    return mfd;
+}
+
+static int memfd_find_named(const char *name, int *mfd_p, Error **errp)
+{
+    const char *val = g_getenv(name);
+
+    if (!val) {
+        *mfd_p = -1;
+        return 0;       /* No memfd was created, not an error */
+    }
+    g_unsetenv(name);
+    if (qemu_strtoi(val, NULL, 10, mfd_p)) {
+        error_setg(errp, "Bad %s env value %s", PRECREATE_STATE_NAME, val);
+        return -1;
+    }
+    lseek(*mfd_p, 0, SEEK_SET);
+    return 0;
+}
+
+static void memfd_delete_named(const char *name)
+{
+    int mfd;
+    const char *val = g_getenv(name);
+
+    if (val) {
+        g_unsetenv(name);
+        if (!qemu_strtoi(val, NULL, 10, &mfd)) {
+            close(mfd);
+        }
+    }
+}
+
+static QEMUFile *qemu_file_new_memfd_output(const char *name, Error **errp)
+{
+    int mfd = memfd_create_named(name, errp);
+
+    if (mfd < 0) {
+        return NULL;
+    }
+
+    return qemu_file_new_fd_output(mfd, name);
+}
+
+static QEMUFile *qemu_file_new_memfd_input(const char *name, Error **errp)
+{
+    int ret, mfd;
+
+    ret = memfd_find_named(name, &mfd, errp);
+    if (ret || mfd < 0) {
+        return NULL;
+    }
+
+    return qemu_file_new_fd_input(mfd, name);
+}
+
+int migration_precreate_save(Error **errp)
+{
+    QEMUFile *f = qemu_file_new_memfd_output(PRECREATE_STATE_NAME, errp);
+
+    if (!f) {
+        return -1;
+    } else if (qemu_savevm_precreate_save(f, errp)) {
+        memfd_delete_named(PRECREATE_STATE_NAME);
+        return -1;
+    } else {
+        /* Do not close f, as mfd must remain open. */
+        return 0;
+    }
+}
+
+void migration_precreate_unsave(void)
+{
+    memfd_delete_named(PRECREATE_STATE_NAME);
+}
+
+int migration_precreate_load(Error **errp)
+{
+    int ret;
+    QEMUFile *f = qemu_file_new_memfd_input(PRECREATE_STATE_NAME, errp);
+
+    if (!f) {
+        return -1;
+    }
+    ret = qemu_savevm_precreate_load(f, errp);
+    qemu_fclose(f);
+    g_unsetenv(PRECREATE_STATE_NAME);
+    return ret;
+}