Live Update
v2 cover Live Update
Provide the cprsave and cprload commands for live update. These save and restore VM state, with minimal guest pause time, so that qemu may be updated to a new version in between.
cprsave stops the VM and saves vmstate to an ordinary file. It supports two
modes: restart and reboot. For restart, cprsave exec's the qemu binary (or /usr/bin/qemu-exec if it exists) with the same argv. qemu restarts in a paused state and waits for the cprload command.
To use the restart mode, qemu must be started with the memfd-alloc option, which allocates guest ram using memfd_create. The memfd's are saved to the environment and kept open across exec, after which they are found from the environment and re-mmap'd. Hence guest ram is preserved in place, albeit with new virtual addresses in the qemu process. The caller resumes the guest by calling cprload, which loads state from the file. If the VM was running at cprsave time, then VM execution resumes. cprsave supports any type of guest image and block device, but the caller must not modify guest block devices between cprsave and cprload.
The restart mode supports vfio devices by preserving the vfio container, group, device, and event descriptors across the qemu re-exec, and by updating DMA mapping virtual addresses using VFIO_DMA_UNMAP_FLAG_SUSPEND and VFIO_DMA_MAP_FLAG_RESUME as proposed in
https://lore.kernel.org/kvm/[email protected]
For the reboot mode, cprsave saves state and exits qemu, and the caller is
allowed to update the host kernel and system software and reboot. The
caller resumes the guest by running qemu with the same arguments as the
original process and calling cprload. To use this mode, guest ram must be
mapped to a persistent shared memory file such as /dev/dax0.0, or /dev/shm
PKRAM as proposed in https://lore.kernel.org/lkml/[email protected]/
The reboot mode supports vfio devices if the caller suspends the guest instead of stopping the VM, such as by issuing guest-suspend-ram to the qemu guest agent. The guest drivers' suspend methods flush outstanding requests and re-initialize the devices, and thus there is no device state to save and restore.
The first patches add helper functions:
- as_flat_walk
- qemu_ram_volatile
- oslib: qemu_clr_cloexec
- util: env var helpers
- vl: memfd-alloc option
- vl: add helper to request re-exec
The next patches implement cprsave and cprload:
- cpr
- cpr: QMP interfaces
- cpr: HMP interfaces
The next patches add vfio support for the restart mode:
- pci: export functions for cpr
- vfio-pci: refactor for cpr
- vfio-pci: cpr
The next patches preserve various descriptor-based backend devices across a cprsave restart:
- vhost: reset vhost devices upon cprsave
- chardev: cpr framework
- chardev: cpr for simple devices
- chardev: cpr for pty
- chardev: socket accept subroutine
- chardev: cpr for sockets
- monitor: cpr support
- cpr: only-cpr-capable option
- cpr: maintainers
- simplify savevm
Here is an example of updating qemu from v4.2.0 to v4.2.1 using "cprload restart". The software update is performed while the guest is running to minimize downtime.
window 1 | window 2
| # qemu-system-x86_64 ... | QEMU 4.2.0 monitor - type 'help' ... | (qemu) info status | VM status: running | | # yum update qemu (qemu) cprsave /tmp/qemu.sav restart | QEMU 4.2.1 monitor - type 'help' ... | (qemu) info status | VM status: paused (prelaunch) | (qemu) cprload /tmp/qemu.sav | (qemu) info status | VM status: running |
Here is an example of updating the host kernel using "cprload reboot"
window 1 | window 2
| # qemu-system-x86_64 ...mem-path=/dev/dax0.0 ...| QEMU 4.2.1 monitor - type 'help' ... | (qemu) info status | VM status: running | | # yum update kernel-uek (qemu) cprsave /tmp/qemu.sav restart | | # systemctl kexec | kexec_core: Starting new kernel | ... | | # qemu-system-x86_64 ...mem-path=/dev/dax0.0 ...| QEMU 4.2.1 monitor - type 'help' ... | (qemu) info status | VM status: paused (prelaunch) | (qemu) cprload /tmp/qemu.sav | (qemu) info status | VM status: running |
Changes from V1 to V2:
- revert vmstate infrastructure changes
- refactor cpr functions into new files
- delete MADV_DOEXEC and use memfd + VFIO_DMA_UNMAP_FLAG_SUSPEND to preserve memory.
- add framework to filter chardev's that support cpr
- save and restore vfio eventfd's
- modify cprinfo QMP interface
- incorporate misc review feedback
- remove unrelated and unneeded patches
- refactor all patches into a shorter and easier to review series
Steve Sistare (17):
as_flat_walk
qemu_ram_volatile
oslib: qemu_clr_cloexec
util: env var helpers
vl: memfd-alloc option
vl: add helper to request re-exec
cpr
pci: export functions for cpr
vfio-pci: refactor for cpr
vfio-pci: cpr
chardev: cpr framework
chardev: cpr for simple devices
chardev: cpr for pty
chardev: socket accept subroutine
cpr: only-cpr-capable option
cpr: maintainers
simplify savevm
Mark Kanda (5):
cpr: QMP interfaces
cpr: HMP interfaces
vhost: reset vhost devices upon cprsave
chardev: cpr for sockets
monitor: cpr support
MAINTAINERS | 11 +++ chardev/char-mux.c | 1 + chardev/char-null.c | 1 + chardev/char-pty.c | 16 +++- chardev/char-serial.c | 1 + chardev/char-socket.c | 31 +++++++ chardev/char-stdio.c | 8 ++ chardev/char.c | 41 ++++++++- exec.c | 75 +++++++++++++-- gdbstub.c | 1 + hmp-commands.hx | 44 +++++++++ hw/pci/msix.c | 20 ++-- hw/pci/pci.c | 7 +- hw/vfio/Makefile.objs | 2 +- hw/vfio/common.c | 63 ++++++++++++- hw/vfio/cpr.c | 117 +++++++++++++++++++++++ hw/vfio/pci.c | 209 ++++++++++++++++++++++++++++++++++++++---- hw/vfio/trace-events | 1 + hw/virtio/vhost.c | 11 +++ include/chardev/char.h | 6 ++ include/exec/memory.h | 11 +++ include/hw/pci/msix.h | 5 + include/hw/pci/pci.h | 2 + include/hw/vfio/vfio-common.h | 7 ++ include/hw/virtio/vhost.h | 1 + include/io/channel-socket.h | 12 +++ include/migration/cpr.h | 17 ++++ include/monitor/hmp.h | 3 + include/monitor/monitor.h | 2 + include/qemu/env.h | 27 ++++++ include/qemu/osdep.h | 1 + include/sysemu/sysemu.h | 4 + io/channel-socket.c | 52 +++++++---- linux-headers/linux/vfio.h | 5 + migration/Makefile.objs | 2 +- migration/cpr.c | 198 +++++++++++++++++++++++++++++++++++++++ migration/migration.c | 6 ++ migration/savevm.c | 19 ++-- migration/savevm.h | 2 + monitor/hmp-cmds.c | 48 ++++++++++ monitor/monitor.c | 5 + monitor/qmp-cmds.c | 31 +++++++ monitor/qmp.c | 43 +++++++++ qapi/Makefile.objs | 3 +- qapi/char.json | 5 +- qapi/cpr.json | 68 ++++++++++++++ qapi/qapi-schema.json | 1 + qemu-options.hx | 45 ++++++++- slirp | 2 +- softmmu/memory.c | 17 ++++ softmmu/vl.c | 68 +++++++++++++- stubs/Makefile.objs | 1 + stubs/cpr.c | 3 + trace-events | 1 + util/Makefile.objs | 2 +- util/env.c | 119 ++++++++++++++++++++++++ util/oslib-posix.c | 9 ++ util/oslib-win32.c | 4 + 58 files changed, 1433 insertions(+), 84 deletions(-) create mode 100644 hw/vfio/cpr.c create mode 100644 include/migration/cpr.h create mode 100644 include/qemu/env.h create mode 100644 migration/cpr.c create mode 100644 qapi/cpr.json create mode 100644 stubs/cpr.c create mode 100644 util/env.c
Signature · 1 line
1.8.3.1
PATCH v2 1/22 as_flat_walk
Add an iterator over the sections of a flattened address space.
--- include/exec/memory.h | 3 +++ softmmu/memory.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+)
include/exec/memory.h +3 -0
@@ -1894,6 +1894,9 @@ bool memory_region_present(MemoryRegion *container, hwaddr addr);*/ bool memory_region_is_mapped (MemoryRegion *mr); +typedef int (*qemu_flat_walk_cb )(MemoryRegionSection *s,void *handle); +int as_flat_walk (AddressSpace *as,qemu_flat_walk_cb func,void *handle); +/** * memory_region_find: translate an address/size relative to a * MemoryRegion into a #MemoryRegionSection.
softmmu/memory.c +17 -0
@@ -2513,6 +2513,23 @@ bool memory_region_is_mapped(MemoryRegion *mr)return mr->container ? true : false; } +int as_flat_walk (AddressSpace *as,qemu_flat_walk_cb func,void *handle) +{ +FlatView *view =address_space_get_flatview (as); +FlatRange *fr; +int ret; + + FOR_EACH_FLAT_RANGE(fr, view) { +MemoryRegionSection section =section_from_flat_range (fr, view); + ret =func (§ion, handle); +if (ret) { +return ret; + } + } + +return 0 ; +} +/* Same as memory_region_find, but it does not add a reference to the * returned region. It must be called from an RCU critical section. */
Signature · 1 line
1.8.3.1
PATCH v2 2/22 qemu_ram_volatile
In reply to Steve SistareAdd a function that returns true if any ram_list block represents volatile memory.
--- exec.c | 30 ++++++++++++++++++++++++++++++ include/exec/memory.h | 8 ++++++++ slirp | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-)
exec.c +30 -0
@@ -2726,6 +2726,36 @@ ram_addr_t qemu_ram_addr_from_host(void *ptr)return block->offset + offset; } +/* +* Return true if any memory regions are writable and not backed by shared +* memory. +*/ +bool qemu_ram_volatile (Error **errp) +{ +RAMBlock *block; +MemoryRegion *mr; +bool ret = false; + +rcu_read_lock (); + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { + mr = block->mr; +if (mr && +memory_region_is_ram (mr) && + !memory_region_is_ram_device (mr) && + !memory_region_is_rom (mr) && + (block->fd ==-1 || !qemu_ram_is_shared (block))) { + +error_setg (errp,"Memory region %s is volatile" , +memory_region_name (mr)); + ret = true; +break ; + } + } + +rcu_read_unlock (); +return ret; +} +/* Generate a debug exception if a watchpoint has been hit. */ void cpu_check_watchpoint (CPUState *cpu,vaddr addr,vaddr len,MemTxAttrs attrs,int flags,uintptr_t ra)
include/exec/memory.h +8 -0
@@ -2522,6 +2522,14 @@ bool ram_block_discard_is_disabled(void);*/ bool ram_block_discard_is_required (void ); +/** +* qemu_ram_volatile: return true if any memory regions are writable and not +* backed by shared memory. +* +* @errp: returned error message identifying the bad region. +*/ +bool qemu_ram_volatile (Error **errp); +#endif #endif
slirp +1 -1
@@ -1 +1 @@ -Subproject commit ce94eba2042d52a0ba3d9e252ebce86715e94275 +Subproject commit a62d36734ffe9828d0f70df1b3898a3b4fbda755
Signature · 1 line
1.8.3.1
PATCH v2 3/22 oslib: qemu_clr_cloexec
In reply to Steve SistareDefine qemu_clr_cloexec, analogous to qemu_set_cloexec.
--- include/qemu/osdep.h | 1 + util/oslib-posix.c | 9 +++++++++ util/oslib-win32.c | 4 ++++ 3 files changed, 14 insertions(+)
include/qemu/osdep.h +1 -0
@@ -554,6 +554,7 @@ static inline void qemu_timersub(const struct timeval *val1,#endif void qemu_set_cloexec (int fd); +void qemu_clr_cloexec (int fd);/* Starting on QEMU 2.5, qemu_hw_version() returns "2.5+" by default * instead of QEMU_VERSION, so setting hw_version on MachineClass
util/oslib-posix.c +9 -0
@@ -314,6 +314,15 @@ void qemu_set_cloexec(int fd)assert (f !=-1 ); } +void qemu_clr_cloexec (int fd) +{ +int f; + f =fcntl (fd,F_GETFD ); +assert (f !=-1 ); + f =fcntl (fd,F_SETFD , f & ~FD_CLOEXEC ); +assert (f !=-1 ); +} +/* * Creates a pipe with FD_CLOEXEC set on both file descriptors */
util/oslib-win32.c +4 -0
@@ -254,6 +254,10 @@ void qemu_set_cloexec(int fd) { } +void qemu_clr_cloexec (int fd) +{ +} +/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ #define _W32_FT_OFFSET (116444736000000000ULL)
Signature · 1 line
1.8.3.1
PATCH v2 4/22 util: env var helpers
In reply to Steve SistareAdd functions for saving fd's and other values in the environment via setenv, and for reading them back via getenv.
--- include/qemu/env.h | 27 ++++++++++++ util/Makefile.objs | 2 +- util/env.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 include/qemu/env.h create mode 100644 util/env.c
include/qemu/env.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Copyright (c) 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QEMU_ENV_H +#define QEMU_ENV_H + +#define FD_PREFIX "QEMU_FD_" +#define BOOL_PREFIX "QEMU_BOOL_" + +typedef int (*walkenv_cb )(const char *name,const char *val,void *handle); + +int getenv_fd (const char *name); +void setenv_fd (const char *name,int fd); +void unsetenv_fd (const char *name); +void unsetenv_fdv (const char *fmt, ...); +bool getenv_bool (const char *name); +void setenv_bool (const char *name,bool val); +void unsetenv_bool (const char *name); +int walkenv (const char *prefix,walkenv_cb cb,void *handle); +void printenv (void ); + +#endif
util/Makefile.objs +1 -1
@@ -1,4 +1,4 @@ -util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o +util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o env.o util-obj-$(call lnot,$(CONFIG_ATOMIC64)) += atomic64.o util-obj-$(CONFIG_POSIX) += aio-posix.o util-obj-$(CONFIG_POSIX) += fdmon-poll.o
util/env.c new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 5/22 vl: memfd-alloc option
In reply to Steve SistareAllocate anonymous memory using memfd_create if the memfd-alloc option is set.
--- exec.c | 38 ++++++++++++++++++++++++++++++-------- include/sysemu/sysemu.h | 1 + qemu-options.hx | 11 +++++++++++ softmmu/vl.c | 4 ++++ trace-events | 1 + 5 files changed, 47 insertions(+), 8 deletions(-)
exec.c +30 -8
@@ -67,6 +67,7 @@#include "exec/log.h" #include "qemu/pmem.h" +#include "qemu/memfd.h" #include "migration/vmstate.h"
@@ -2231,34 +2232,55 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) {RAMBlock *block;RAMBlock *last_block =NULL ; +struct MemoryRegion *mr = new_block->mr;ram_addr_t old_ram_size, new_ram_size;Error *err =NULL ; +const char *name; +void *addr =0 ; +size_t maxlen; old_ram_size =last_ram_page ();qemu_mutex_lock_ramlist (); - new_block->offset =find_ram_offset (new_block->max_length); + maxlen = new_block->max_length; + new_block->offset =find_ram_offset (maxlen);if (!new_block->host) {if (xen_enabled ()) { -xen_ram_alloc (new_block->offset, new_block->max_length, - new_block->mr, &err); +xen_ram_alloc (new_block->offset, maxlen, new_block->mr, &err);if (err) {error_propagate (errp, err);qemu_mutex_unlock_ramlist ();return ; } }else { - new_block->host =phys_mem_alloc (new_block->max_length, - &new_block->mr->align, shared); -if (!new_block->host) { + name =memory_region_name (new_block->mr); +if (memfd_alloc) { +int mfd =-1 ;/* placeholder until next patch */ + mr->align =QEMU_VMALLOC_ALIGN ; +if (mfd <0 ) { + mfd =qemu_memfd_create (name, maxlen + mr->align, +0 ,0 ,0 , &err); +if (mfd <0 ) { +return ; + } + } + new_block->flags |=RAM_SHARED ; + addr =file_ram_alloc (new_block, maxlen, mfd, false, errp); +trace_anon_memfd_alloc (name, maxlen, addr, mfd); + }else { + addr =phys_mem_alloc (maxlen, &mr->align, shared); + } + +if (!addr) {error_setg_errno (errp, errno,"cannot set up guest memory '%s'" , -memory_region_name (new_block->mr)); + name);qemu_mutex_unlock_ramlist ();return ; } -memory_try_enable_merging (new_block->host, new_block->max_length); +memory_try_enable_merging (addr, maxlen); + new_block->host = addr; } }
include/sysemu/sysemu.h +1 -0
@@ -13,6 +13,7 @@ extern int only_migratable;extern const char *qemu_name;extern QemuUUID qemu_uuid;extern bool qemu_uuid_set; +extern bool memfd_alloc;void qemu_add_data_dir (const char *path);
qemu-options.hx +11 -0
@@ -4094,6 +4094,17 @@ SRST an unmigratable state. ERST +#ifdef __linux__ +DEF("memfd-alloc", 0, QEMU_OPTION_memfd_alloc, \ + "-memfd-alloc allocate anonymous memory using memfd_create\n", + QEMU_ARCH_ALL) +#endif + +SRST +``-memfd-alloc`` + Allocate anonymous memory using memfd_create (Linux only). +ERST + DEF("nodefaults", 0, QEMU_OPTION_nodefaults, \ "-nodefaults don't create default devices\n", QEMU_ARCH_ALL) SRST
softmmu/vl.c +4 -0
@@ -164,6 +164,7 @@ bool boot_strict;uint8_t *boot_splash_filedata;int only_migratable;/* turn it off unless user states otherwise */ bool wakeup_suspend_enabled; +bool memfd_alloc;int icount_align_option;
@@ -3635,6 +3636,9 @@ void qemu_init(int argc, char **argv, char **envp)case QEMU_OPTION_only_migratable: only_migratable =1 ;break ; +case QEMU_OPTION_memfd_alloc: + memfd_alloc = true; +break ;case QEMU_OPTION_nodefaults: has_defaults =0 ;break ;
trace-events +1 -0
@@ -54,6 +54,7 @@ find_ram_offset_loop(uint64_t size, uint64_t candidate, uint64_t offset, uint64_ ram_block_discard_range(const char *rbname, void *hva, size_t length, bool need_madvise, bool need_fallocate, int ret) "%s@%p + 0x%zx: madvise: %d fallocate: %d ret: %d" memory_notdirty_write_access(uint64_t vaddr, uint64_t ram_addr, unsigned size) "0x%" PRIx64 " ram_addr 0x%" PRIx64 " size %u" memory_notdirty_set_dirty(uint64_t vaddr) "0x%" PRIx64 +anon_memfd_alloc(const char *name, size_t size, void *ptr, int fd) "%s size %zu ptr %p fd %d" # memory.c memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
Signature · 1 line
1.8.3.1
Re: [PATCH V2 05/22] vl: memfd-alloc option
Quoted 20 lines · Steve Sistare
Allocate anonymous memory using
memfd_createif the memfd-alloc option is set.Signed-off-by Steve Sistare <[email protected]>--- exec.c | 38 ++++++++++++++++++++++++++++++-------- include/sysemu/sysemu.h | 1 + qemu-options.hx | 11 +++++++++++ softmmu/vl.c | 4 ++++ trace-events | 1 + 5 files changed, 47 insertions(+), 8 deletions(-)qemu-options.hx +0
-0@@ -4094,6 +4094,17 @@ SRST an unmigratable state. ERST
qemu-options.hx SRST · line 4106
Quoted 10 lines
qemu-options.hx +10
-0+#ifdef __linux__ +DEF("memfd-alloc", 0, QEMU_OPTION_memfd_alloc, \ + "-memfd-alloc allocate anonymous memory using memfd_create\n", + QEMU_ARCH_ALL) +#endif + +SRST +``-memfd-alloc`` + Allocate anonymous memory using memfd_create (Linux only). +ERST
Do we really need a new arg for this ? It is already possible to request use of memfd for the guest RAM using
-object memory-backend-memfd,id=ram-node0,size=NNNN
this memory backend object framework was intended to remove the need to add new ad-hoc CLI args for controlling memory allocation.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH V2 05/22] vl: memfd-alloc option
Quoted 35 lines · Daniel P. Berrangé
Quoted 20 lines · Steve Sistare
Allocate anonymous memory using
memfd_createif the memfd-alloc option is set.Signed-off-by Steve Sistare <[email protected]>--- exec.c | 38 ++++++++++++++++++++++++++++++-------- include/sysemu/sysemu.h | 1 + qemu-options.hx | 11 +++++++++++ softmmu/vl.c | 4 ++++ trace-events | 1 + 5 files changed, 47 insertions(+), 8 deletions(-)qemu-options.hx +0
-0@@ -4094,6 +4094,17 @@ SRST an unmigratable state. ERSTqemu-options.hx SRST · line 4106
Quoted 10 lines
qemu-options.hx +10
-0+#ifdef __linux__ +DEF("memfd-alloc", 0, QEMU_OPTION_memfd_alloc, \ + "-memfd-alloc allocate anonymous memory using memfd_create\n", + QEMU_ARCH_ALL) +#endif + +SRST +``-memfd-alloc`` + Allocate anonymous memory using memfd_create (Linux only). +ERSTDo we really need a new arg for this ? It is already possible to request use of memfd for the guest RAM using
Quoted 4 lines
-object memory-backend-memfd,id=ram-node0,size=NNNN
this memory backend object framework was intended to remove the need to add new ad-hoc CLI args for controlling memory allocation.
Yes, I considered that, but there are other memory regions that cannot be controlled by the command line but which must be preserved, such as vram, bios, and rom. If vram is not preserved, parts of the screen will be blank until the user performs some action which refreshes the display. bios and rom should be preserved rather than re-recreated with potentially different contents from the firmware images in the updated qemu package.
However, your comment reminds me that I must add a few lines of code to preserve the memory-backend-memfd.
- Steve
Re: [PATCH V2 05/22] vl: memfd-alloc option
Il mer 6 gen 2021, 17:37 Steven Sistare <[email protected]> ha scritto:
Quoted 11 lines
Yes, I considered that, but there are other memory regions that cannot be controlled
by the command line but which must be preserved, such as vram, bios, and rom. If vram
is not preserved, parts of the screen will be blank until the user performs some action
which refreshes the display. bios and rom should be preserved rather than re-recreated
with potentially different contents from the firmware images in the updated qemu package.
Quoted 4 lines
However, your comment reminds me that I must add a few lines of code to preserve the
memory-backend-memfd.
A new option specific to memory is the wrong way to do this. If a special mode must be specified when starting QEMU, you can make it a -machine option and block the QMP commands unless it's specified. Otherwise you can use "normal" migration to marshal and unmarshal across the update those memory regions that aren't backed by shared memory or memfd.
Also, because of the mess that vl.c had grown into, adding new "simple" options is going to be very very hard. In fact I am working on turning many options like -smp or -m into syntactic sugar for -machine; at some point I would like to (almost) forbid adding _any_ new option.
Paolo
Quoted 3 lines
- Steve
Re: [PATCH V2 05/22] vl: memfd-alloc option
Quoted 8 lines · Paolo Bonzini
Il mer 6 gen 2021, 17:37 Steven Sistare <[email protected] <mailto:[email protected]>> ha scritto:
Yes, I considered that, but there are other memory regions that cannot be controlled by the command line but which must be preserved, such as vram, bios, and rom. If vram is not preserved, parts of the screen will be blank until the user performs some action which refreshes the display. bios and rom should be preserved rather than re-recreated with potentially different contents from the firmware images in the updated qemu package.
Quoted 7 lines
However, your comment reminds me that I must add a few lines of code to preserve the memory-backend-memfd.
A new option specific to memory is the wrong way to do this. If a special mode must be specified when starting QEMU, you can make it a -machine option and block the QMP commands unless it's specified. Otherwise you can use "normal" migration to marshal and unmarshal across the update those memory regions that aren't backed by shared memory or memfd.
Also, because of the mess that
vl.chad grown into, adding new "simple" options is going to be very very hard. In fact I am working on turning many options like -smp or -m into syntactic sugar for -machine; at some point I would like to (almost) forbid adding _any_ new option.
Will do. Thanks for the heads up on the future of vl.c.
I defined the option independently of cpr for generality. Do you think this could be useful? If yes, I will name and define the -machine option to use memfd;
if no, I will name and define it to enable cpr, and implicitly enable memfd without saying so.
- Steve
PATCH v2 6/22 vl: add helper to request re-exec
In reply to Steve SistareAdd a qemu_exec_requested() hook that causes the main loop to exit and re-exec qemu using the same initial arguments. If /usr/bin/qemu-exec exists, exec that instead. This is an optional site-specific trampoline that may alter the environment before exec'ing the qemu binary.
--- include/sysemu/sysemu.h | 1 + softmmu/vl.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/sysemu/sysemu.h +1 -0
@@ -17,6 +17,7 @@ extern bool memfd_alloc;void qemu_add_data_dir (const char *path); +void qemu_system_exec_request (void );void qemu_add_exit_notifier (Notifier *notify);void qemu_remove_exit_notifier (Notifier *notify);
softmmu/vl.c +30 -0
@@ -165,6 +165,7 @@ uint8_t *boot_splash_filedata;int only_migratable;/* turn it off unless user states otherwise */ bool wakeup_suspend_enabled;bool memfd_alloc; +static char **argv_main;int icount_align_option;
@@ -1294,6 +1295,7 @@ static ShutdownCause reset_requested;static ShutdownCause shutdown_requested;static int shutdown_signal;static pid_t shutdown_pid; +static int exec_requested;static int powerdown_requested;static int debug_requested;static int suspend_requested;
@@ -1324,6 +1326,11 @@ static int qemu_shutdown_requested(void)return atomic_xchg (&shutdown_requested,SHUTDOWN_CAUSE_NONE ); } +static int qemu_exec_requested (void ) +{ +return atomic_xchg (&exec_requested,0 ); +} +static void qemu_kill_report (void ) {if (!qtest_driver () && shutdown_signal) {
@@ -1570,6 +1577,13 @@ void qemu_system_shutdown_request(ShutdownCause reason)qemu_notify_event (); } +void qemu_system_exec_request (void ) +{ + shutdown_requested =1 ; + exec_requested =1 ; +qemu_notify_event (); +} +static void qemu_system_powerdown (void ) {qapi_event_send_powerdown ();
@@ -1605,6 +1619,16 @@ void qemu_system_debug_request(void)qemu_notify_event (); } +static void qemu_exec (void ) +{ +const char *helper ="/usr/bin/qemu-exec" ; +const char *bin = !access (helper,X_OK ) ? helper : argv_main[0 ]; + +execvp (bin, argv_main); +error_report ("execvp failed, errno %d." , errno); +exit (1 ); +} +static bool main_loop_should_exit (void ) {RunState r;
@@ -1625,6 +1649,11 @@ static bool main_loop_should_exit(void) } request =qemu_shutdown_requested ();if (request) { + +if (qemu_exec_requested ()) { +qemu_exec (); +/* not reached */ + }qemu_kill_report ();qemu_system_shutdown (request);if (no_shutdown) {
@@ -2872,6 +2901,7 @@ void qemu_init(int argc, char **argv, char **envp)os_set_line_buffering (); + argv_main = argv;error_init (argv[0 ]);module_call_init (MODULE_INIT_TRACE );
Signature · 1 line
1.8.3.1
PATCH v2 7/22 cpr
In reply to Steve SistareProvide the cprsave and cprload functions for live update. These save and restore VM state, with minimal guest pause time, so that qemu may be updated to a new version in between.
cprsave stops the VM and saves vmstate to an ordinary file. It supports two
modes: restart and reboot. For restart, cprsave exec's the qemu binary (or /usr/bin/qemu-exec if it exists) with the same argv. qemu restarts in a paused state and waits for the cprload command.
To use the restart mode, qemu must be started with the memfd-alloc option. The memfd's are saved to the environment and kept open across exec, after which they are found from the environment and re-mmap'd. Hence guest ram is preserved in place, albeit with new virtual addresses in the qemu process. The caller resumes the guest by calling cprload, which loads state from the file. If the VM was running at cprsave time, then VM execution resumes. cprsave supports any type of guest image and block device, but the caller must not modify guest block devices between cprsave and cprload.
For the reboot mode, cprsave saves state and exits qemu, and the caller is allowed to update the host kernel and system software and reboot. The caller resumes the guest by running qemu with the same arguments as the original process and calling cprload. To use this mode, guest ram must be mapped to a persistent shared memory file such as /dev/dax0.0 or /dev/shm PKRAM.
The reboot mode supports vfio devices if the caller suspends the guest instead of stopping the VM, such as by issuing guest-suspend-ram to the qemu guest agent. The guest drivers' suspend methods flush outstanding requests and re-initialize the devices, and thus there is no device state to save and restore.
The restart mode supports vfio devices in a subsequent patch.
--- exec.c | 6 +- include/migration/cpr.h | 17 +++++ include/sysemu/sysemu.h | 1 + migration/Makefile.objs | 2 +- migration/cpr.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ migration/savevm.h | 2 + softmmu/vl.c | 21 +++++- 7 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 include/migration/cpr.h create mode 100644 migration/cpr.c
exec.c +5 -1
@@ -68,6 +68,7 @@#include "qemu/pmem.h" #include "qemu/memfd.h" +#include "qemu/env.h" #include "migration/vmstate.h"
@@ -2256,7 +2257,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) }else { name =memory_region_name (new_block->mr);if (memfd_alloc) { -int mfd =-1 ;/* placeholder until next patch */ +int mfd =getenv_fd (name); mr->align =QEMU_VMALLOC_ALIGN ;if (mfd <0 ) { mfd = qemu_memfd_create(name, maxlen + mr->align,
@@ -2264,7 +2265,9 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)if (mfd <0 ) {return ; } +setenv_fd (name, mfd); } +qemu_clr_cloexec (mfd); new_block->flags |=RAM_SHARED ; addr =file_ram_alloc (new_block, maxlen, mfd, false, errp);trace_anon_memfd_alloc (name, maxlen, addr, mfd);
@@ -2521,6 +2524,7 @@ void qemu_ram_free(RAMBlock *block) }qemu_mutex_lock_ramlist (); +unsetenv_fd (memory_region_name (block->mr));QLIST_REMOVE_RCU (block, next); ram_list.mru_block =NULL ;/* Write list before version */
include/migration/cpr.h new file +17 -0
@@ -0,0 +1,17 @@ +/* +* Copyright (c) 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +#ifndef MIGRATION_CPR_H +#define MIGRATION_CPR_H + +#include "qapi/qapi-types-cpr.h" + +bool cpr_active (void ); +void cprsave (const char *file,CprMode mode,Error **errp); +void cprload (const char *file,Error **errp); + +#endif
include/sysemu/sysemu.h +1 -0
@@ -14,6 +14,7 @@ extern const char *qemu_name;extern QemuUUID qemu_uuid;extern bool qemu_uuid_set;extern bool memfd_alloc; +extern int start_on_wake;void qemu_add_data_dir (const char *path);
migration/Makefile.objs +1 -1
@@ -1,5 +1,5 @@ common-obj-y += migration.o socket.o fd.o exec.o -common-obj-y += tls.o channel.o savevm.o +common-obj-y += tls.o channel.o savevm.o cpr.o common-obj-y += colo.o colo-failover.o common-obj-y += vmstate.o vmstate-types.o page_cache.o common-obj-y += qemu-file.o global_state.o
migration/cpr.c new file +187 -0
187 lines not on this page · raw message
migration/savevm.h +2 -0
@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +QEMUFile *qf_file_open (const char *path,int flags,int mode, +const char *name,Error **errp);#endif
softmmu/vl.c +20 -1
@@ -165,6 +165,7 @@ uint8_t *boot_splash_filedata;int only_migratable;/* turn it off unless user states otherwise */ bool wakeup_suspend_enabled;bool memfd_alloc; +int start_on_wake;static char **argv_main;int icount_align_option;
@@ -604,6 +605,8 @@ static const RunStateTransition runstate_transitions_def[] = { { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING }, { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE }, { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE }, + { RUN_STATE_PRELAUNCH, RUN_STATE_SUSPENDED }, + { RUN_STATE_PRELAUNCH, RUN_STATE_PAUSED }, { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING }, { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
@@ -1527,7 +1530,17 @@ void qemu_system_wakeup_request(WakeupReason reason, Error **errp)if (!(wakeup_reason_mask & (1 << reason))) {return ; } -runstate_set (RUN_STATE_RUNNING ); + +/* +* Must call vm_start if it has never been called, to invoke the state +* change callbacks for the first time. +*/ +if (start_on_wake) { + start_on_wake =0 ; +vm_start (); + }else { +runstate_set (RUN_STATE_RUNNING ); + } wakeup_reason = reason;qemu_notify_event (); }
@@ -4510,6 +4523,12 @@ void qemu_init(int argc, char **argv, char **envp)exit (0 ); } +/* Equivalent to -S, but no need for parent to modify argv. */ +if (getenv ("QEMU_START_FREEZE" )) { +unsetenv ("QEMU_START_FREEZE" ); + autostart =0 ; + } +if (incoming) {Error *local_err =NULL ;qemu_start_incoming_migration (incoming, &local_err);
Signature · 1 line
1.8.3.1
PATCH v2 8/22 cpr: QMP interfaces
In reply to Steve Sistarecprsave calls cprsave(). Syntax:
{ 'enum': 'CprMode', 'data': [ 'reboot', 'restart' ] }
{ 'command': 'cprsave', 'data': { 'file': 'str', 'mode': 'CprMode' } }
cprload calls cprload(). Syntax:
{ 'command': 'cprload', 'data': { 'file': 'str' } }
cprinfo returns a list of supported modes. Syntax:
{ 'struct': 'CprInfo', 'data': { 'modes': [ 'CprMode' ] } } { 'command': 'cprinfo', 'returns': 'CprInfo' }
--- monitor/qmp-cmds.c | 31 +++++++++++++++++++++++ qapi/Makefile.objs | 3 ++- qapi/cpr.json | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ qapi/qapi-schema.json | 1 + 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 qapi/cpr.json
monitor/qmp-cmds.c +31 -0
@@ -35,9 +35,11 @@#include "qapi/qapi-commands-machine.h" #include "qapi/qapi-commands-misc.h" #include "qapi/qapi-commands-ui.h" +#include "qapi/qapi-commands-cpr.h" #include "qapi/qmp/qerror.h" #include "hw/mem/memory-device.h" #include "hw/acpi/acpi_dev_interface.h" +#include "migration/cpr.h" NameInfo *qmp_query_name (Error **errp) {
@@ -161,6 +163,35 @@ void qmp_cont(Error **errp) } } +CprInfo *qmp_cprinfo (Error **errp) +{ +CprInfo *cprinfo; +CprModeList *mode, *mode_list =NULL ; +CprMode i; + + cprinfo =g_malloc0 (sizeof (*cprinfo)); + +for (i =0 ; i <CPR_MODE__MAX ; i++) { + mode =g_malloc0 (sizeof (*mode)); + mode->value = i; + mode->next = mode_list; + mode_list = mode; + } + + cprinfo->modes = mode_list; +return cprinfo; +} + +void qmp_cprsave (const char *file,CprMode mode,Error **errp) +{ +cprsave (file, mode, errp); +} + +void qmp_cprload (const char *file,Error **errp) +{ +cprload (file, errp); +} +void qmp_system_wakeup (Error **errp) {if (!qemu_wakeup_suspend_enabled ()) {
qapi/Makefile.objs +2 -1
@@ -5,7 +5,8 @@ util-obj-y += opts-visitor.o qapi-clone-visitor.o util-obj-y += qmp-event.o util-obj-y += qapi-util.o -QAPI_COMMON_MODULES = audio authz block-core block char common control crypto +QAPI_COMMON_MODULES = audio authz block-core block char +QAPI_COMMON_MODULES += common cpr control crypto QAPI_COMMON_MODULES += dump error introspect job machine migration misc QAPI_COMMON_MODULES += net pragma qdev qom rdma rocker run-state sockets tpm QAPI_COMMON_MODULES += trace transaction ui
qapi/cpr.json new file +68 -0
68 lines not on this page · raw message
qapi/qapi-schema.json +1 -0
@@ -74,6 +74,7 @@ { 'include': 'ui.json' } { 'include': 'authz.json' } { 'include': 'migration.json' } +{ 'include': 'cpr.json' } { 'include': 'transaction.json' } { 'include': 'trace.json' } { 'include': 'control.json' }
Signature · 1 line
1.8.3.1
PATCH v2 9/22 cpr: HMP interfaces
In reply to Steve Sistarecprsave <file> <mode>
Call cprsave().
Arguments:
file : save vmstate to this file name
mode: "reboot" or "restart"
cprload <file>
Call cprload().
Arguments:
file : load vmstate from this file name
cprinfo
Print to stdout a space-delimited list of modes supported by cprsave.
Arguments: none
--- hmp-commands.hx | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/monitor/hmp.h | 3 +++ monitor/hmp-cmds.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+)
hmp-commands.hx +44 -0
@@ -354,6 +354,50 @@ SRST ERST { + .name = "cprinfo", + .args_type = "", + .params = "", + .help = "return list of modes supported by cprsave", + .cmd = hmp_cprinfo, + }, + +SRST +``cprinfo`` + Return a space-delimited list of modes supported by cprsave. +ERST + + { + .name = "cprsave", + .args_type = "file:s,mode:s", + .params = "file 'restart'|'reboot'", + .help = "create a checkpoint of the VM in file", + .cmd = hmp_cprsave, + }, + +SRST +``cprsave`` *file* *mode* + Create a checkpoint of the whole virtual machine and save it in *file*. + If *mode* is 'reboot', the checkpoint remains valid after a host kexec + reboot. Guest ram must be backed by persistant shared memory. + If *mode* is 'restart', pause the VCPUs, exec /usr/bin/qemu-exec if it + exists, else exec argv[0], passing all the original command line arguments. + Guest ram must be allocated with the memfd-anon option. +ERST + + { + .name = "cprload", + .args_type = "file:s", + .params = "file", + .help = "load VM checkpoint from file", + .cmd = hmp_cprload, + }, + +SRST +``cprload`` *file* +Load a virtual machine from checkpoint file *file* and continue VCPUs. +ERST + + { .name = "delvm", .args_type = "name:s", .params = "tag",
include/monitor/hmp.h +3 -0
@@ -59,6 +59,9 @@ void hmp_balloon(Monitor *mon, const QDict *qdict);void hmp_loadvm (Monitor *mon,const QDict *qdict);void hmp_savevm (Monitor *mon,const QDict *qdict);void hmp_delvm (Monitor *mon,const QDict *qdict); +void hmp_cprinfo (Monitor *mon,const QDict *qdict); +void hmp_cprsave (Monitor *mon,const QDict *qdict); +void hmp_cprload (Monitor *mon,const QDict *qdict);void hmp_migrate_cancel (Monitor *mon,const QDict *qdict);void hmp_migrate_continue (Monitor *mon,const QDict *qdict);void hmp_migrate_incoming (Monitor *mon,const QDict *qdict);
monitor/hmp-cmds.c +48 -0
60 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 10/22 pci: export functions for cpr
In reply to Steve SistareExport msix_is_pending and msix_init_vector_notifiers for use by cpr. No functional change.
--- hw/pci/msix.c | 20 ++++++++++++++------ hw/pci/pci.c | 3 +-- include/hw/pci/msix.h | 5 +++++ include/hw/pci/pci.h | 1 + 4 files changed, 21 insertions(+), 8 deletions(-)
hw/pci/msix.c +14 -6
@@ -64,7 +64,7 @@ static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)return dev->msix_pba + vector /8 ; } -static int msix_is_pending (PCIDevice *dev,int vector) +int msix_is_pending (PCIDevice *dev,unsigned int vector) {return *msix_pending_byte (dev, vector) &msix_pending_mask (vector); }
@@ -576,6 +576,17 @@ static void msix_unset_notifier_for_vector(PCIDevice *dev, unsigned int vector) dev->msix_vector_release_notifier (dev, vector); } +void msix_init_vector_notifiers (PCIDevice *dev, +MSIVectorUseNotifier use_notifier, +MSIVectorReleaseNotifier release_notifier, +MSIVectorPollNotifier poll_notifier) +{ +assert (use_notifier && release_notifier); + dev->msix_vector_use_notifier = use_notifier; + dev->msix_vector_release_notifier = release_notifier; + dev->msix_vector_poll_notifier = poll_notifier; +} + int msix_set_vector_notifiers(PCIDevice *dev, MSIVectorUseNotifier use_notifier, MSIVectorReleaseNotifier release_notifier,
@@ -583,11 +594,8 @@ int msix_set_vector_notifiers(PCIDevice *dev, {int vector, ret; -assert (use_notifier && release_notifier); - - dev->msix_vector_use_notifier = use_notifier; - dev->msix_vector_release_notifier = release_notifier; - dev->msix_vector_poll_notifier = poll_notifier; +msix_init_vector_notifiers (dev, use_notifier, release_notifier, + poll_notifier);if ((dev->config[dev->msix_cap +MSIX_CONTROL_OFFSET ] & (MSIX_ENABLE_MASK |MSIX_MASKALL_MASK )) ==MSIX_ENABLE_MASK ) {
hw/pci/pci.c +1 -2
@@ -216,7 +216,6 @@ static const TypeInfo pcie_bus_info = { };static PCIBus *pci_find_bus_nr (PCIBus *bus,int bus_num); -static void pci_update_mappings (PCIDevice *d);static void pci_irq_handler (void *opaque,int irq_num,int level);static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom,Error **);static void pci_del_option_rom (PCIDevice *pdev);
@@ -1316,7 +1315,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,return new_addr; } -static void pci_update_mappings (PCIDevice *d) +void pci_update_mappings (PCIDevice *d) {PCIIORegion *r;int i;
include/hw/pci/msix.h +5 -0
@@ -32,6 +32,7 @@ int msix_present(PCIDevice *dev);bool msix_is_masked (PCIDevice *dev,unsigned vector);void msix_set_pending (PCIDevice *dev,unsigned vector);void msix_clr_pending (PCIDevice *dev,int vector); +int msix_is_pending (PCIDevice *dev,unsigned vector);int msix_vector_use (PCIDevice *dev,unsigned vector);void msix_vector_unuse (PCIDevice *dev,unsigned vector);
@@ -41,6 +42,10 @@ void msix_notify(PCIDevice *dev, unsigned vector);void msix_reset (PCIDevice *dev); +void msix_init_vector_notifiers (PCIDevice *dev, +MSIVectorUseNotifier use_notifier, +MSIVectorReleaseNotifier release_notifier, +MSIVectorPollNotifier poll_notifier); int msix_set_vector_notifiers(PCIDevice *dev, MSIVectorUseNotifier use_notifier, MSIVectorReleaseNotifier release_notifier,
include/hw/pci/pci.h +1 -0
@@ -865,5 +865,6 @@ extern const VMStateDescription vmstate_pci_device; }MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +void pci_update_mappings (PCIDevice *d);#endif
Signature · 1 line
1.8.3.1
PATCH v2 11/22 vfio-pci: refactor for cpr
In reply to Steve SistareExport vfio_address_spaces and vfio_listener_skipped_section. Add optional eventfd arg to vfio_add_kvm_msi_virq. Refactor vector use into a helper vfio_vector_init. All for use by cpr in a subsequent patch. No functional change.
--- hw/vfio/common.c | 4 ++-- hw/vfio/pci.c | 36 +++++++++++++++++++++++++----------- include/hw/vfio/vfio-common.h | 3 +++ 3 files changed, 30 insertions(+), 13 deletions(-)
hw/vfio/common.c +2 -2
@@ -40,7 +40,7 @@VFIOGroupList vfio_group_list =QLIST_HEAD_INITIALIZER (vfio_group_list); -static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces = +VFIOAddressSpaceList vfio_address_spaces =QLIST_HEAD_INITIALIZER (vfio_address_spaces);#ifdef CONFIG_KVM
@@ -393,7 +393,7 @@ static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova,return -1 ; } -static bool vfio_listener_skipped_section (MemoryRegionSection *section) +bool vfio_listener_skipped_section (MemoryRegionSection *section) {return (!memory_region_is_ram (section->mr) && !memory_region_is_iommu(section->mr)) ||
hw/vfio/pci.c +25 -11
@@ -412,7 +412,7 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) }static void vfio_add_kvm_msi_virq (VFIOPCIDevice *vdev,VFIOMSIVector *vector, -int vector_n,bool msix) +int vector_n,bool msix,int eventfd) {int virq;
@@ -420,7 +420,9 @@ static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vector,return ; } -if (event_notifier_init (&vector->kvm_interrupt,0 )) { +if (eventfd >=0 ) { +event_notifier_init_fd (&vector->kvm_interrupt, eventfd); + }else if (event_notifier_init (&vector->kvm_interrupt,0 )) {return ; }
@@ -456,6 +458,22 @@ static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg,kvm_irqchip_commit_routes (kvm_state); } +static void vfio_vector_init (VFIOPCIDevice *vdev,int nr,int eventfd) +{ +VFIOMSIVector *vector = &vdev->msi_vectors[nr]; +PCIDevice *pdev = &vdev->pdev; + + vector->vdev = vdev; + vector->virq =-1 ; +if (eventfd >=0 ) { +event_notifier_init_fd (&vector->interrupt, eventfd); + }else if (event_notifier_init (&vector->interrupt,0 )) { +error_report ("vfio: Error: event_notifier_init failed" ); + } + vector->use = true; +msix_vector_use (pdev, nr); +} +static int vfio_msix_vector_do_use (PCIDevice *pdev,unsigned int nr,MSIMessage *msg,IOHandler *handler) {
@@ -467,14 +485,10 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, vector = &vdev->msi_vectors[nr]; +vfio_vector_init (vdev, nr,-1 ); +if (!vector->use) { - vector->vdev = vdev; - vector->virq =-1 ; -if (event_notifier_init (&vector->interrupt,0 )) { -error_report ("vfio: Error: event_notifier_init failed" ); - } - vector->use = true; -msix_vector_use (pdev, nr); +vfio_vector_init (vdev, nr,-1 ); } qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
@@ -492,7 +506,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, } }else {if (msg) { -vfio_add_kvm_msi_virq (vdev, vector, nr, true); +vfio_add_kvm_msi_virq (vdev, vector, nr, true,-1 ); } }
@@ -628,7 +642,7 @@ retry:* Attempt to enable route through KVM irqchip, * default to userspace handling if unavailable. */ -vfio_add_kvm_msi_virq (vdev, vector, i, false); +vfio_add_kvm_msi_virq (vdev, vector, i, false,-1 ); }/* Set interrupt type prior to possible interrupts */
include/hw/vfio/vfio-common.h +3 -0
@@ -182,6 +182,8 @@ int vfio_get_device(VFIOGroup *group, const char *name,extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;extern VFIOGroupList vfio_group_list; +typedef QLIST_HEAD(, VFIOAddressSpace) VFIOAddressSpaceList; +extern VFIOAddressSpaceList vfio_address_spaces;#ifdef CONFIG_LINUX int vfio_get_region_info(VFIODevice *vbasedev, int index,
@@ -193,6 +195,7 @@ struct vfio_info_cap_header * vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);#endif extern const MemoryListener vfio_prereg_listener; +bool vfio_listener_skipped_section (MemoryRegionSection *section); int vfio_spapr_create_window(VFIOContainer *container, MemoryRegionSection *section,
Signature · 1 line
1.8.3.1
PATCH v2 12/22 vfio-pci: cpr
In reply to Steve SistareEnable vfio-pci devices to be saved and restored across an exec restart of qemu.
At vfio creation time, save the value of vfio container, group, and device descriptors in the environment.
In cprsave, suspend the use of virtual addresses in DMA mappings with VFIO_DMA_UNMAP_FLAG_SUSPEND, because guest ram will be remapped at a different VA after exec. DMA to already-mapped pages continues. Save the msi message area as part of vfio-pci vmstate, save the interrupt and notifier eventfd's in the environment, and clear the close-on-exec flag for the vfio descriptors. The flag is not cleared earlier because the descriptors should not persist across miscellaneous fork and exec calls that may be performed during normal operation.
On qemu restart, vfio_realize() finds the descriptor env vars, uses the descriptors, and notes that the device is being reused. Device and iommu state is already configured, so operations in vfio_realize that would modify the configuration are skipped for a reused device, including vfio ioctl's and writes to PCI configuration space. The result is that vfio_realize constructs qemu data structures that reflect the current state of the device. However, the reconstruction is not complete until cprload is called. cprload loads the msi data and finds eventfds in the environment. It rebuilds vector data structures and attaches the interrupts to the new KVM instance. cprload then walks the flattened ranges of the vfio_address_spaces and calls VFIO_DMA_MAP_FLAG_RESUME to inform the kernel of the new VA's. Lastly, it starts the VM and suppresses vfio device reset.
--- hw/pci/pci.c | 4 + hw/vfio/Makefile.objs | 2 +- hw/vfio/common.c | 59 +++++++++++++- hw/vfio/cpr.c | 117 ++++++++++++++++++++++++++++ hw/vfio/pci.c | 173 ++++++++++++++++++++++++++++++++++++++++-- hw/vfio/trace-events | 1 + include/hw/pci/pci.h | 1 + include/hw/vfio/vfio-common.h | 4 + linux-headers/linux/vfio.h | 5 ++ migration/cpr.c | 4 + 10 files changed, 360 insertions(+), 10 deletions(-) create mode 100644 hw/vfio/cpr.c
hw/pci/pci.c +4 -0
@@ -291,6 +291,10 @@ static void pci_do_device_reset(PCIDevice *dev) {int r; +if (dev->reused) { +return ; + } +pci_device_deassert_intx (dev);assert (dev->irq_state ==0 );
hw/vfio/Makefile.objs +1 -1
@@ -1,4 +1,4 @@ -obj-y += common.o spapr.o +obj-y += common.o spapr.o cpr.o obj-$(CONFIG_VFIO_PCI) += pci.o pci-quirks.o display.o obj-$(CONFIG_VFIO_CCW) += ccw.o obj-$(CONFIG_VFIO_PLATFORM) += platform.o
hw/vfio/common.c +56 -3
165 lines not on this page · raw message
hw/vfio/cpr.c new file +117 -0
117 lines not on this page · raw message
hw/vfio/pci.c +167 -6
251 lines not on this page · raw message
hw/vfio/trace-events +1 -0
@@ -115,6 +115,7 @@ vfio_region_sparse_mmap_header(const char *name, int index, int nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%0x8" vfio_dma_unmap_overflow_workaround(void) "" +vfio_region_resume(const char *name, int fd, uint64_t iova_start, uint64_t iova_end, void *vaddr) "%s fd %d 0x%"PRIx64" - 0x%"PRIx64" [%p]" # platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d"
include/hw/pci/pci.h +1 -0
@@ -358,6 +358,7 @@ struct PCIDevice {/* ID of standby device in net_failover pair */ char *failover_pair_id; +bool reused; }; void pci_register_bar(PCIDevice *pci_dev, int region_num,
include/hw/vfio/vfio-common.h +4 -0
@@ -73,6 +73,7 @@ typedef struct VFIOContainer {unsigned iommu_type;Error *error;bool initialized; +bool reused;unsigned long pgsizes; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
@@ -109,6 +110,7 @@ typedef struct VFIODevice {bool needs_reset;bool no_mmap;bool ram_block_discard_allowed; +bool reused;VFIODeviceOps *ops;unsigned int num_irqs;unsigned int num_regions;
@@ -178,6 +180,8 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);void vfio_put_group (VFIOGroup *group);int vfio_get_device (VFIOGroup *group,const char *name,VFIODevice *vbasedev,Error **errp); +int vfio_cprsave (void ); +int vfio_cprload (void );extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
linux-headers/linux/vfio.h +5 -0
@@ -46,6 +46,9 @@*/ #define VFIO_NOIOMMU_IOMMU 8 +/* Supports VFIO DMA suspend and resume */ +#define VFIO_SUSPEND 9 +/* * The IOCTL interface is designed for extensibility by embedding the * structure length (argsz) and flags into structures passed between
@@ -1052,6 +1055,7 @@ struct vfio_iommu_type1_dma_map {__u32 flags;#define VFIO_DMA_MAP_FLAG_READ (1 << 0)/* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)/* writable from device */ +#define VFIO_DMA_MAP_FLAG_RESUME (1 << 2)__u64 vaddr;/* Process virtual address */ __u64 iova;/* IO virtual address */ __u64 size;/* Size of mapping (bytes) */
@@ -1088,6 +1092,7 @@ struct vfio_iommu_type1_dma_unmap {__u32 argsz;__u32 flags;#define VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP (1 << 0) +#define VFIO_DMA_UNMAP_FLAG_SUSPEND (1 << 1)__u64 iova;/* IO virtual address */ __u64 size;/* Size of mapping (bytes) */ __u8 data[];
migration/cpr.c +4 -0
@@ -131,6 +131,9 @@ void cprsave(const char *file, CprMode mode, Error **errp) no_shutdown =0 ;qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); }else if (restart) { +if (vfio_cprsave ()) { + goto err; + }walkenv (FD_PREFIX , preserve_fd,0 );setenv ("QEMU_START_FREEZE" ,"" ,1 );qemu_system_exec_request ();
@@ -174,6 +177,7 @@ void cprload(const char *file, Error **errp)error_setg (errp,"Error %d while loading VM state" , ret);return ; } +vfio_cprload (); state =global_state_get_runstate ();if (state ==RUN_STATE_RUNNING ) {
Signature · 1 line
1.8.3.1
PATCH v2 13/22 vhost: reset vhost devices upon cprsave
In reply to Steve SistareA vhost device is implicitly preserved across re-exec because its fd is not closed, and the value of the fd is specified on the command line for the new qemu to find. However, new qemu issues an VHOST_RESET_OWNER ioctl, which fails because the device already has an owner. To fix, reset the owner prior to exec.
--- hw/virtio/vhost.c | 11 +++++++++++ include/hw/virtio/vhost.h | 1 + migration/cpr.c | 1 + 3 files changed, 13 insertions(+)
hw/virtio/vhost.c +11 -0
@@ -1764,6 +1764,17 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev) hdev->vdev =NULL ; } +void vhost_dev_reset_all (void ) +{ +struct vhost_dev *dev; + + QLIST_FOREACH(dev, &vhost_devices, entry) { +if (dev->vhost_ops->vhost_reset_device (dev) <0 ) { +VHOST_OPS_DEBUG ("vhost_reset_device failed" ); + } + } +} +int vhost_net_set_backend (struct vhost_dev *hdev,struct vhost_vring_file *file) {
include/hw/virtio/vhost.h +1 -0
@@ -105,6 +105,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,void vhost_dev_cleanup (struct vhost_dev *hdev);int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev);void vhost_dev_stop (struct vhost_dev *hdev,VirtIODevice *vdev); +void vhost_dev_reset_all (void );int vhost_dev_enable_notifiers (struct vhost_dev *hdev,VirtIODevice *vdev);void vhost_dev_disable_notifiers (struct vhost_dev *hdev,VirtIODevice *vdev);
migration/cpr.c +1 -0
@@ -135,6 +135,7 @@ void cprsave(const char *file, CprMode mode, Error **errp) goto err; }walkenv (FD_PREFIX , preserve_fd,0 ); +vhost_dev_reset_all ();setenv ("QEMU_START_FREEZE" ,"" ,1 );qemu_system_exec_request (); }
Signature · 1 line
1.8.3.1
PATCH v2 14/22 chardev: cpr framework
In reply to Steve SistareAdd QEMU_CHAR_FEATURE_CPR for devices that support cpr.
Add the chardev close_on_cpr option for devices that can be closed on cpr and reopened after exec.
cpr is allowed only if either QEMU_CHAR_FEATURE_CPR or close_on_cpr is set for all chardevs in the configuration.
--- chardev/char.c | 41 ++++++++++++++++++++++++++++++++++++++--- include/chardev/char.h | 5 +++++ migration/cpr.c | 3 +++ qapi/char.json | 5 ++++- qemu-options.hx | 26 ++++++++++++++++++++++++-- 5 files changed, 74 insertions(+), 6 deletions(-)
chardev/char.c +38 -3
@@ -37,6 +37,7 @@#include "qemu/help_option.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/env.h" #include "qemu/id.h" #include "qemu/coroutine.h"
@@ -226,6 +227,9 @@ static void qemu_char_open(Chardev *chr, ChardevBackend *backend,ChardevClass *cc =CHARDEV_GET_CLASS (chr);/* Any ChardevCommon member would work */ ChardevCommon *common = backend ? backend->u.null.data :NULL ; +char fdname[40 ]; + + chr->close_on_cpr = (common && common->close_on_cpr);if (common && common->has_logfile) {int flags =O_WRONLY |O_CREAT ;
@@ -235,7 +239,14 @@ static void qemu_char_open(Chardev *chr, ChardevBackend *backend, }else { flags |=O_TRUNC ; } - chr->logfd =qemu_open (common->logfile, flags,0666 ); +snprintf (fdname,sizeof (fdname),"%s_log" , chr->label); + chr->logfd =getenv_fd (fdname); +if (chr->logfd <0 ) { + chr->logfd =qemu_open (common->logfile, flags,0666 ); +if (!chr->close_on_cpr) { +setenv_fd (fdname, chr->logfd); + } + }if (chr->logfd <0 ) { error_setg_errno(errp, errno, "Unable to open logfile %s",
@@ -286,11 +297,12 @@ static void char_finalize(Object *obj)if (chr->be) { chr->be->chr =NULL ; } -g_free (chr->filename); -g_free (chr->label);if (chr->logfd !=-1 ) {close (chr->logfd); +unsetenv_fdv ("%s_log" , chr->label); } +g_free (chr->filename); +g_free (chr->label);qemu_mutex_destroy (&chr->chr_write_lock); }
@@ -490,6 +502,8 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) backend->has_logappend = true; backend->logappend =qemu_opt_get_bool (opts,"logappend" , false); + + backend->close_on_cpr =qemu_opt_get_bool (opts,"close-on-cpr" , false); }static const ChardevClass *char_get_class (const char *driver,Error **errp)
@@ -922,6 +936,9 @@ QemuOptsList qemu_chardev_opts = { },{ .name = "abstract", .type = QEMU_OPT_BOOL, + },{ + .name = "close-on-cpr", + .type = QEMU_OPT_BOOL, }, {/* end of list */ } },
@@ -1163,6 +1180,24 @@ GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,return source; } +static int chr_cpr_capable (Object *obj,void *opaque) +{ +Chardev *chr = (Chardev *)obj; +Error **errp = opaque; + +if (qemu_chr_has_feature (chr,QEMU_CHAR_FEATURE_CPR ) || chr->close_on_cpr) { +return 0 ; + } +error_setg (errp,"error: chardev %s -> %s is not capable of cpr" , + chr->label, chr->filename); +return 1 ; +} + +bool qemu_chr_cpr_capable (Error **errp) +{ +return !object_child_foreach (get_chardevs_root (), chr_cpr_capable, errp); +} +void qemu_chr_cleanup (void ) {object_unparent (get_chardevs_root ());
include/chardev/char.h +5 -0
@@ -50,6 +50,8 @@ typedef enum {/* Whether the gcontext can be changed after calling * qemu_chr_be_update_read_handlers() */ QEMU_CHAR_FEATURE_GCONTEXT , +/* Whether the device supports cpr */ +QEMU_CHAR_FEATURE_CPR ,QEMU_CHAR_FEATURE_LAST , } ChardevFeature;
@@ -65,6 +67,7 @@ struct Chardev {char *filename;int logfd;int be_open; +bool close_on_cpr;GSource *gsource;GMainContext *gcontext;DECLARE_BITMAP (features,QEMU_CHAR_FEATURE_LAST );
@@ -290,4 +293,6 @@ GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,/* console.c */ void qemu_chr_parse_vc (QemuOpts *opts,ChardevBackend *backend,Error **errp); +bool qemu_chr_cpr_capable (Error **errp); +#endif
migration/cpr.c +3 -0
@@ -131,6 +131,9 @@ void cprsave(const char *file, CprMode mode, Error **errp) no_shutdown =0 ;qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); }else if (restart) { +if (!qemu_chr_cpr_capable (errp)) { + goto err; + }if (vfio_cprsave ()) { goto err; }
qapi/char.json +4 -1
@@ -204,12 +204,15 @@ # @logfile: The name of a logfile to save output # @logappend: true to append instead of truncate # (default to false to truncate) +# @close-on-cpr: if true, close device's fd on cprsave. defaults to false. +# since 5.3. # # Since: 2.6 ## { 'struct': 'ChardevCommon', 'data': { '*logfile': 'str', - '*logappend': 'bool' } } + '*logappend': 'bool', + '*close-on-cpr': 'bool' } } ## # @ChardevFile:
qemu-options.hx +24 -2
@@ -2956,42 +2956,60 @@ DEFHEADING(Character device options:) DEF("chardev", HAS_ARG, QEMU_OPTION_chardev, "-chardev help\n" "-chardev null,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev socket,id=id[,host=host],port=port[,to=to][,ipv4][,ipv6][,nodelay][,reconnect=seconds]\n" " [,server][,nowait][,telnet][,websocket][,reconnect=seconds][,mux=on|off]\n" " [,logfile=PATH][,logappend=on|off][,tls-creds=ID][,tls-authz=ID] (tcp)\n" + " [,close-on-cpr=on|off]\n" "-chardev socket,id=id,path=path[,server][,nowait][,telnet][,websocket][,reconnect=seconds]\n" - " [,mux=on|off][,logfile=PATH][,logappend=on|off][,abstract=on|off][,tight=on|off] (unix)\n" + " [,mux=on|off][,logfile=PATH][,logappend=on|off][,abstract=on|off][,tight=on|off][,close-on-cpr=on|off] (unix)\n" "-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr]\n" " [,localport=localport][,ipv4][,ipv6][,mux=on|off]\n" - " [,logfile=PATH][,logappend=on|off]\n" + " [,logfile=PATH][,logappend=on|off][,close-on-cpr=on|off]\n" "-chardev msmouse,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev vc,id=id[[,width=width][,height=height]][[,cols=cols][,rows=rows]]\n" " [,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev ringbuf,id=id[,size=size][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev file,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev pipe,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #ifdef _WIN32 "-chardev console,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev serial,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #else "-chardev pty,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev stdio,id=id[,mux=on|off][,signal=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #endif #ifdef CONFIG_BRLAPI "-chardev braille,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #endif #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) "-chardev serial,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev tty,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #endif #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) "-chardev parallel,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev parport,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #endif #if defined(CONFIG_SPICE) "-chardev spicevmc,id=id,name=name[,debug=debug][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" "-chardev spiceport,id=id,name=name[,debug=debug][,logfile=PATH][,logappend=on|off]\n" + " [,close-on-cpr=on|off]\n" #endif , QEMU_ARCH_ALL )
@@ -3064,6 +3082,10 @@ The general form of a character device option is: ``logappend`` option controls whether the log file will be truncated or appended to when opened. + Every backend supports the ``close-on-cpr`` option. If on, the devices's + descriptor is closed during cprsave, and reopened after exec. This is + useful for devices that do not support cpr. + The available backends are: ``-chardev null,id=id``
Signature · 1 line
1.8.3.1
PATCH v2 15/22 chardev: cpr for simple devices
In reply to Steve SistareSet QEMU_CHAR_FEATURE_CPR for devices that trivially support cpr. char-stdio is slightly less trivial. Allow the gdb server by closing it on exec.
--- chardev/char-mux.c | 1 + chardev/char-null.c | 1 + chardev/char-serial.c | 1 + chardev/char-stdio.c | 8 ++++++++ gdbstub.c | 1 + include/chardev/char.h | 1 + migration/cpr.c | 1 + 7 files changed, 14 insertions(+)
chardev/char-mux.c +1 -0
@@ -330,6 +330,7 @@ static void qemu_chr_open_mux(Chardev *chr,*/ *be_opened = machine_init_done;qemu_chr_fe_init (&d->chr, drv, errp); +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_CPR ); } static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
chardev/char-null.c +1 -0
@@ -32,6 +32,7 @@ static void null_chr_open(Chardev *chr, Error **errp) { *be_opened = false; +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_CPR ); }static void char_null_class_init (ObjectClass *oc,void *data)
chardev/char-serial.c +1 -0
@@ -274,6 +274,7 @@ static void qmp_chardev_open_serial(Chardev *chr,qemu_set_nonblock (fd);tty_serial_init (fd,115200 ,'N' ,8 ,1 ); +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_CPR ); qemu_chr_open_fd(chr, fd, fd); }#endif /* __linux__ || __sun__ */
chardev/char-stdio.c +8 -0
@@ -116,9 +116,17 @@ static void qemu_chr_open_stdio(Chardev *chr, stdio_allow_signal = opts->signal; }qemu_chr_set_echo_stdio (chr, false); +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_CPR ); }#endif +void qemu_term_exit (void ) +{ +#ifndef _WIN32 +term_exit (); +#endif +} +static void qemu_chr_parse_stdio (QemuOpts *opts,ChardevBackend *backend,Error **errp) {
gdbstub.c +1 -0
@@ -3417,6 +3417,7 @@ int gdbserver_start(const char *device) mon_chr = gdbserver_state.mon_chr;reset_gdbserver_state (); } + mon_chr->close_on_cpr = true;create_processes (&gdbserver_state);
include/chardev/char.h +1 -0
@@ -294,5 +294,6 @@ GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms,void qemu_chr_parse_vc (QemuOpts *opts,ChardevBackend *backend,Error **errp);bool qemu_chr_cpr_capable (Error **errp); +void qemu_term_exit (void );#endif
migration/cpr.c +1 -0
@@ -139,6 +139,7 @@ void cprsave(const char *file, CprMode mode, Error **errp) }walkenv (FD_PREFIX , preserve_fd,0 );vhost_dev_reset_all (); +qemu_term_exit ();setenv ("QEMU_START_FREEZE" ,"" ,1 );qemu_system_exec_request (); }
Signature · 1 line
1.8.3.1
PATCH v2 16/22 chardev: cpr for pty
In reply to Steve SistareSave and restore pty descriptors across cprsave and cprload.
--- chardev/char-pty.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
chardev/char-pty.c +14 -2
@@ -30,6 +30,7 @@#include "qemu/sockets.h" #include "qemu/error-report.h" #include "qemu/module.h" +#include "qemu/env.h" #include "qemu/qemu-print.h" #include "chardev/char-io.h"
@@ -188,12 +189,14 @@ static void char_pty_finalize(Object *obj)Chardev *chr =CHARDEV (obj);PtyChardev *s =PTY_CHARDEV (obj); +unsetenv_fd (chr->label);pty_chr_state (chr,0 );object_unref (OBJECT (s->ioc));pty_chr_timer_cancel (s);qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); } + static void char_pty_open(Chardev *chr, ChardevBackend *backend, bool *be_opened,
@@ -204,19 +207,28 @@ static void char_pty_open(Chardev *chr,char pty_name[PATH_MAX ];char *name; + master_fd =getenv_fd (chr->label); +if (master_fd >=0 ) { + chr->filename =g_strdup_printf ("pty:unknown" ); + goto have_fd; + } + master_fd =qemu_openpty_raw (&slave_fd, pty_name);if (master_fd <0 ) {error_setg_errno (errp, errno,"Failed to create PTY" );return ; } - +if (!chr->close_on_cpr) { +setenv_fd (chr->label, master_fd); + }close (slave_fd);qemu_set_nonblock (master_fd); - chr->filename =g_strdup_printf ("pty:%s" , pty_name);qemu_printf ("char device redirected to %s (label %s)\n" , pty_name, chr->label); +have_fd: +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_CPR ); s =PTY_CHARDEV (chr); s->ioc =QIO_CHANNEL (qio_channel_file_new_fd (master_fd)); name =g_strdup_printf ("chardev-pty-%s" , chr->label);
Signature · 1 line
1.8.3.1
PATCH v2 17/22 chardev: socket accept subroutine
In reply to Steve SistareFactor out the post-accept actions into a subroutine that can be used in a subsequent patch. No functional change.
--- io/channel-socket.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-)
io/channel-socket.c +24 -19
@@ -349,16 +349,34 @@ void qio_channel_socket_dgram_async(QIOChannelSocket *ioc, context); } +static int qio_channel_socket_post_accept (QIOChannelSocket *cioc, +Error **errp) +{ + cioc->localAddrLen =sizeof (cioc->localAddr); +if (getsockname (cioc->fd, (struct sockaddr *)&cioc->localAddr, + &cioc->localAddrLen) <0 ) { +error_setg_errno (errp, errno, +"Unable to query local socket address" ); +return 1 ; + } + +#ifndef WIN32 +if (cioc->localAddr.ss_family ==AF_UNIX ) { +QIOChannel *ioc_local =QIO_CHANNEL (cioc); +qio_channel_set_feature (ioc_local,QIO_CHANNEL_FEATURE_FD_PASS ); + } +#endif /* WIN32 */ + +return 0 ; +}QIOChannelSocket *qio_channel_socket_accept (QIOChannelSocket *ioc,Error **errp) { -QIOChannelSocket *cioc; +QIOChannelSocket *cioc =qio_channel_socket_new (); - cioc =qio_channel_socket_new (); cioc->remoteAddrLen =sizeof (ioc->remoteAddr); - cioc->localAddrLen =sizeof (ioc->localAddr); retry:trace_qio_channel_socket_accept (ioc);
@@ -372,24 +390,11 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,trace_qio_channel_socket_accept_fail (ioc); goto error; } - -if (getsockname (cioc->fd, (struct sockaddr *)&cioc->localAddr, - &cioc->localAddrLen) <0 ) { -error_setg_errno (errp, errno, -"Unable to query local socket address" ); - goto error; +if (!qio_channel_socket_post_accept (cioc, errp)) { +trace_qio_channel_socket_accept_complete (ioc, cioc, cioc->fd); +return cioc; } -#ifndef WIN32 -if (cioc->localAddr.ss_family ==AF_UNIX ) { -QIOChannel *ioc_local =QIO_CHANNEL (cioc); -qio_channel_set_feature (ioc_local,QIO_CHANNEL_FEATURE_FD_PASS ); - } -#endif /* WIN32 */ - -trace_qio_channel_socket_accept_complete (ioc, cioc, cioc->fd); -return cioc; - error:object_unref (OBJECT (cioc));return NULL ;
Signature · 1 line
1.8.3.1
PATCH v2 18/22 chardev: cpr for sockets
In reply to Steve SistareDefine qio_channel_socket_reuse to initialize a channel based on an existing socket fd. Save accepted socket fds in the environment before cprsave, and look for fds in the environment after cprload. Reject cprsave if a socket enables the TLS or websocket option.
--- chardev/char-socket.c | 30 ++++++++++++++++++++++++++++++ include/io/channel-socket.h | 12 ++++++++++++ io/channel-socket.c | 9 +++++++++ stubs/Makefile.objs | 1 + stubs/cpr.c | 3 +++ 5 files changed, 55 insertions(+) create mode 100644 stubs/cpr.c
chardev/char-socket.c +30 -0
@@ -36,6 +36,7 @@#include "qapi/qapi-visit-sockets.h" #include "chardev/char-io.h" +#include "qemu/env.h" /***********************************************************/ /* TCP Net console */
@@ -400,6 +401,7 @@ static void tcp_chr_free_connection(Chardev *chr)SocketChardev *s =SOCKET_CHARDEV (chr);int i; +unsetenv_fd (chr->label);if (s->read_msgfds_num) {for (i =0 ; i < s->read_msgfds_num; i++) {close (s->read_msgfds[i]);
@@ -1157,6 +1159,25 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } +static void load_char_socket_fd (Chardev *chr) +{ +SocketChardev *sockchar =SOCKET_CHARDEV (chr); +QIOChannelSocket *sioc; +int fd =getenv_fd (chr->label); + +if (fd !=-1 ) { + sockchar =SOCKET_CHARDEV (chr); + sioc =qio_channel_socket_reuse (fd,NULL ); +if (sioc) { +tcp_chr_accept (sockchar->listener, sioc, chr); + }else { +error_printf ("error: could not restore socket for %s\n" , + chr->label); + } + }else if (sockchar->sioc && !chr->close_on_cpr) { +setenv_fd (chr->label, sockchar->sioc->fd); + } +} static int qmp_chardev_open_socket_server(Chardev *chr, bool is_telnet,
@@ -1360,6 +1381,13 @@ static void qmp_chardev_open_socket(Chardev *chr,qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_FD_PASS ); } +if (!s->tls_creds && !s->is_websock) { +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_CPR ); + }else if (only_cpr_capable) { +error_setg (errp,"error: socket %s is not cpr capable due to %s option" , + chr->label, (s->tls_creds ?"TLS" :"websocket" )); + } +/* be isn't opened until we get a connection */ *be_opened = false;
@@ -1375,6 +1403,8 @@ static void qmp_chardev_open_socket(Chardev *chr,return ; } } + +load_char_socket_fd (chr); } static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
include/io/channel-socket.h +12 -0
@@ -260,5 +260,17 @@ QIOChannelSocket *qio_channel_socket_accept (QIOChannelSocket *ioc, Error **errp); +/** +* qio_channel_socket_reuse: +* @fd: existing client socket descriptor +* @errp: pointer to a NULL-initialized error object +* +* Construct a client channel using @fd. +* +* Returns: the new client channel, or NULL on error +*/ +QIOChannelSocket * +qio_channel_socket_reuse (int fd, +Error **errp);#endif /* QIO_CHANNEL_SOCKET_H */
io/channel-socket.c +9 -0
@@ -400,6 +400,15 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,return NULL ; } +QIOChannelSocket * +qio_channel_socket_reuse (int fd, +Error **errp) +{ +QIOChannelSocket *cioc =qio_channel_socket_new (); + cioc->fd = fd; +return qio_channel_socket_post_accept (cioc, errp) ?0 : cioc; +} +static void qio_channel_socket_init (Object *obj) {QIOChannelSocket *ioc =QIO_CHANNEL_SOCKET (obj);
stubs/Makefile.objs +1 -0
@@ -1,5 +1,6 @@ stub-obj-y += blk-commit-all.o stub-obj-y += cmos.o +stub-obj-y += cpr.o stub-obj-y += cpu-get-clock.o stub-obj-y += cpu-get-icount.o stub-obj-y += dump.o
stubs/cpr.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "qemu/osdep.h" + +bool only_cpr_capable;
Signature · 1 line
1.8.3.1
Re: [PATCH V2 18/22] chardev: cpr for sockets
Quoted 45 lines · Steve Sistare
Define
qio_channel_socket_reuseto initialize a channel based on an existing socket fd. Save accepted socket fds in the environment before cprsave, and look for fds in the environment after cprload. Reject cprsave if a socket enables the TLS or websocket option.Signed-off-by Mark Kanda <[email protected]>Signed-off-by Steve Sistare <[email protected]>--- chardev/char-socket.c | 30 ++++++++++++++++++++++++++++++ include/io/channel-socket.h | 12 ++++++++++++ io/channel-socket.c | 9 +++++++++ stubs/Makefile.objs | 1 + stubs/cpr.c | 3 +++ 5 files changed, 55 insertions(+) create mode 100644 stubs/cpr.cinclude/io/channel-socket.h +12
-0@@ -260,5 +260,17 @@ QIOChannelSocket *qio_channel_socket_accept (QIOChannelSocket *ioc, Error **errp); +/** +* qio_channel_socket_reuse: +* @fd: existing client socket descriptor +* @errp: pointer to a NULL-initialized error object +* +* Construct a client channel using @fd. +* +* Returns: the new client channel, or NULL on error +*/ +QIOChannelSocket * +qio_channel_socket_reuse (int fd, +Error **errp);#endif /* QIO_CHANNEL_SOCKET_H */ io/channel-socket.c +0
-0@@ -400,6 +400,15 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,return NULL ;
io/channel-socket.c qio_channel_socket_accept(QIOChannelSocket *ioc, · line 410
Quoted 10 lines
io/channel-socket.c +8
-0} +QIOChannelSocket * +qio_channel_socket_reuse (int fd, +Error **errp) +{ +QIOChannelSocket *cioc =qio_channel_socket_new (); + cioc->fd = fd; +return qio_channel_socket_post_accept (cioc, errp) ?0 : cioc; +}
Why do we need to add this new API when we already have
qio_channel_socket_new_fd(int fd, Error **errp)
which accepts a pre-opened socket FD ?
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH V2 18/22] chardev: cpr for sockets
Quoted 59 lines · Daniel P. Berrangé
Quoted 45 lines · Steve Sistare
Define
qio_channel_socket_reuseto initialize a channel based on an existing socket fd. Save accepted socket fds in the environment before cprsave, and look for fds in the environment after cprload. Reject cprsave if a socket enables the TLS or websocket option.Signed-off-by Mark Kanda <[email protected]>Signed-off-by Steve Sistare <[email protected]>--- chardev/char-socket.c | 30 ++++++++++++++++++++++++++++++ include/io/channel-socket.h | 12 ++++++++++++ io/channel-socket.c | 9 +++++++++ stubs/Makefile.objs | 1 + stubs/cpr.c | 3 +++ 5 files changed, 55 insertions(+) create mode 100644 stubs/cpr.cinclude/io/channel-socket.h +12
-0@@ -260,5 +260,17 @@ QIOChannelSocket *qio_channel_socket_accept (QIOChannelSocket *ioc, Error **errp); +/** +* qio_channel_socket_reuse: +* @fd: existing client socket descriptor +* @errp: pointer to a NULL-initialized error object +* +* Construct a client channel using @fd. +* +* Returns: the new client channel, or NULL on error +*/ +QIOChannelSocket * +qio_channel_socket_reuse (int fd, +Error **errp);#endif /* QIO_CHANNEL_SOCKET_H */ io/channel-socket.c +0
-0@@ -400,6 +400,15 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,return NULL ;io/channel-socket.c qio_channel_socket_accept(QIOChannelSocket *ioc, · line 410
Quoted 10 lines
io/channel-socket.c +8
-0} +QIOChannelSocket * +qio_channel_socket_reuse (int fd, +Error **errp) +{ +QIOChannelSocket *cioc =qio_channel_socket_new (); + cioc->fd = fd; +return qio_channel_socket_post_accept (cioc, errp) ?0 : cioc; +}Why do we need to add this new API when we already have
Quoted 3 lines
qio_channel_socket_new_fd(int fd, Error **errp)which accepts a pre-opened socket FD ?
That was fast!
Good call, thanks. I missed that qio_channel_socket_new_fd calls qio_channel_socket_set_fd and the latter performs the necessary post-accept actions. I will also delete patch 17.
- Steve
PATCH v2 19/22 monitor: cpr support
In reply to Steve SistareA monitor socket requires special treatment. Save and restore the
qmp negotiation status. Stop the monitor's iothread in cpsave. Otherwise,
the thread will detect the close of the monitor socket and call unsetenv_fd,which modifies environ and races with execv which uses environ.
--- include/monitor/monitor.h | 2 ++ migration/cpr.c | 2 ++ monitor/monitor.c | 5 +++++ monitor/qmp.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+)
include/monitor/monitor.h +2 -0
@@ -22,6 +22,8 @@ void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp);int monitor_init (MonitorOptions *opts,bool allow_hmp,Error **errp);int monitor_init_opts (QemuOpts *opts,Error **errp);void monitor_cleanup (void ); +void monitor_iothread_stop (void ); +void monitor_cprsave (void );int monitor_suspend (Monitor *mon);void monitor_resume (Monitor *mon);
migration/cpr.c +2 -0
@@ -137,9 +137,11 @@ void cprsave(const char *file, CprMode mode, Error **errp)if (vfio_cprsave ()) { goto err; } +monitor_iothread_stop ();walkenv (FD_PREFIX , preserve_fd,0 );vhost_dev_reset_all ();qemu_term_exit (); +monitor_cprsave ();setenv ("QEMU_START_FREEZE" ,"" ,1 );qemu_system_exec_request (); }
monitor/monitor.c +5 -0
@@ -591,6 +591,11 @@ void monitor_cleanup(void) } } +void monitor_iothread_stop (void ) +{ +iothread_stop (mon_iothread); +} +static void monitor_qapi_event_init (void ) { monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
monitor/qmp.c +43 -0
59 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 20/22 cpr: only-cpr-capable option
In reply to Steve SistareAdd the only-cpr-capable option, which causes qemu to exit with an error if any devices that are not capable of cpr are added. This guarantees that a cprsave operation will not fail with an unsupported device error.
--- chardev/char-socket.c | 1 + exec.c | 3 +++ include/sysemu/sysemu.h | 1 + migration/migration.c | 6 ++++++ qemu-options.hx | 8 ++++++++ softmmu/vl.c | 13 +++++++++++++ 6 files changed, 32 insertions(+)
chardev/char-socket.c +1 -0
@@ -36,6 +36,7 @@#include "qapi/qapi-visit-sockets.h" #include "chardev/char-io.h" +#include "sysemu/sysemu.h" #include "qemu/env.h" /***********************************************************/
exec.c +3 -0
@@ -2271,6 +2271,9 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) new_block->flags |=RAM_SHARED ; addr =file_ram_alloc (new_block, maxlen, mfd, false, errp);trace_anon_memfd_alloc (name, maxlen, addr, mfd); + }else if (only_cpr_capable) { +error_report ("only-cpr-capable requires memfd-alloc" ); + errno =0 ; }else { addr =phys_mem_alloc (maxlen, &mr->align, shared); }
include/sysemu/sysemu.h +1 -0
@@ -14,6 +14,7 @@ extern const char *qemu_name;extern QemuUUID qemu_uuid;extern bool qemu_uuid_set;extern bool memfd_alloc; +extern bool only_cpr_capable;extern int start_on_wake;void qemu_add_data_dir (const char *path);
migration/migration.c +6 -0
@@ -1099,6 +1099,12 @@ static bool migrate_caps_check(bool *cap_list, } } +if (cap_list[MIGRATION_CAPABILITY_X_COLO ] && only_cpr_capable) { +error_setg (errp,"x-colo is not compatible with -only-cpr-capable" ); +return false; + } + +return true; }
qemu-options.hx +8 -0
@@ -4116,6 +4116,14 @@ SRST an unmigratable state. ERST +DEF("only-cpr-capable", 0, QEMU_OPTION_only_cpr_capable, \ + "-only-cpr-capable allow only cpr capable devices\n", QEMU_ARCH_ALL) +SRST +``-only-cpr-capable`` + Only allow cpr capable devices, which guarantees that cprsave will not fail + with an unsupported device error. +ERST + #ifdef __linux__ DEF("memfd-alloc", 0, QEMU_OPTION_memfd_alloc, \ "-memfd-alloc allocate anonymous memory using memfd_create\n",
softmmu/vl.c +13 -0
@@ -164,6 +164,7 @@ bool boot_strict;uint8_t *boot_splash_filedata;int only_migratable;/* turn it off unless user states otherwise */ bool wakeup_suspend_enabled; +bool only_cpr_capable;bool memfd_alloc;int start_on_wake;static char **argv_main;
@@ -3679,6 +3680,9 @@ void qemu_init(int argc, char **argv, char **envp)case QEMU_OPTION_only_migratable: only_migratable =1 ;break ; +case QEMU_OPTION_only_cpr_capable: + only_cpr_capable = true; +break ;case QEMU_OPTION_memfd_alloc: memfd_alloc = true;break ;
@@ -4433,6 +4437,15 @@ void qemu_init(int argc, char **argv, char **envp)cpu_synchronize_all_post_init (); +if (only_cpr_capable && replay_mode !=REPLAY_MODE_NONE ) { +error_report ("replay is not compatible with -only-cpr-capable" ); +exit (1 ); + } +if (only_cpr_capable && !qemu_chr_cpr_capable (&err)) { +error_report_err (err); +exit (1 ); + } +rom_reset_order_override ();/* Did we create any drives that we failed to create a device for? */
Signature · 1 line
1.8.3.1
PATCH v2 21/22 cpr: maintainers
In reply to Steve SistareAdd the maintainers for cpr related files.
--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -2631,6 +2631,17 @@ F: net/colo* F: net/filter-rewriter.c F: net/filter-mirror.c +CPR +M: Steve Sistare <[email protected]> +M: Mark Kanda <[email protected]> +S: Maintained +F: hw/vfio/cpr.c +F: include/migration/cpr.h +F: migration/cpr.c +F: qapi/cpr.json +F: include/qemu/env.h +F: util/env.c + Record/replay M: Pavel Dovgalyuk <[email protected]> R: Paolo Bonzini <[email protected]>
Signature · 1 line
1.8.3.1
PATCH v2 22/22 simplify savevm
In reply to Steve SistareUse qf_file_open to simplify a few functions in savevm.c. No functional change.
--- migration/savevm.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
migration/savevm.c +6 -13
@@ -2788,8 +2788,8 @@ int save_snapshot(const char *name, Error **errp)void qmp_xen_save_devices_state (const char *filename,bool has_live,bool live,Error **errp) { +const char *ioc_name ="migration-xen-save-state" ;QEMUFile *f; -QIOChannelFile *ioc;int saved_vm_running;int ret;
@@ -2803,13 +2803,10 @@ void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,vm_stop (RUN_STATE_SAVE_VM );global_state_store_running (); - ioc =qio_channel_file_new_path (filename,O_WRONLY |O_CREAT ,0660 , errp); -if (!ioc) { + f =qf_file_open (filename,O_WRONLY |O_CREAT ,0660 , ioc_name, errp); +if (!f) { goto the_end; } -qio_channel_set_name (QIO_CHANNEL (ioc),"migration-xen-save-state" ); - f =qemu_fopen_channel_output (QIO_CHANNEL (ioc)); -object_unref (OBJECT (ioc)); ret =qemu_save_device_state (f);if (ret <0 ||qemu_fclose (f) <0 ) {error_setg (errp,QERR_IO_ERROR );
@@ -2837,8 +2834,8 @@ void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,void qmp_xen_load_devices_state (const char *filename,Error **errp) { +const char *ioc_name ="migration-xen-load-state" ;QEMUFile *f; -QIOChannelFile *ioc;int ret;/* Guest must be paused before loading the device state; the RAM state
@@ -2850,14 +2847,10 @@ void qmp_xen_load_devices_state(const char *filename, Error **errp) }vm_stop (RUN_STATE_RESTORE_VM ); - ioc =qio_channel_file_new_path (filename,O_RDONLY |O_BINARY ,0 , errp); -if (!ioc) { + f =qf_file_open (filename,O_RDONLY |O_BINARY ,0 , ioc_name, errp); +if (!f) {return ; } -qio_channel_set_name (QIO_CHANNEL (ioc),"migration-xen-load-state" ); - f =qemu_fopen_channel_input (QIO_CHANNEL (ioc)); -object_unref (OBJECT (ioc)); - ret =qemu_loadvm_state (f);qemu_fclose (f);if (ret <0 ) {
Signature · 1 line
1.8.3.1