Skip to contentSkip to conversation

Live Update

PATCH In review Steve Sistare 6 participants 69 messages Jul 26, 2022 – Dec 1, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v9 cover Live Update

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in
https://lore.kernel.org/lkml/[email protected]
but this is not enforced. The restarted qemu arguments must match those used to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in
https://lore.kernel.org/kvm/[email protected]
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1                                        | window 2
                                                |
# qemu-system-$arch ...                         |
  -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec      |
(qemu) migrate_set_parameter cpr-exec-args      |
  qemu-system-$arch ... -incoming defer         |
(qemu) migrate -d file:/tmp/qemu.sav            |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1                                        | window 2
                                                |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
  -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -d file:/tmp/qemu.sav            |
(qemu) quit                                     |
                                                |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
  -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

 MAINTAINERS                         |  14 ++
 accel/xen/xen-all.c                 |   3 +
 backends/hostmem-epc.c              |  18 +-
 backends/hostmem-file.c             |   1 +
 backends/hostmem-memfd.c            |  22 ++-
 backends/tpm/tpm_emulator.c         |  11 +-
 block/parallels.c                   |   7 +-
 block/qcow.c                        |   7 +-
 block/vdi.c                         |   7 +-
 block/vhdx.c                        |   7 +-
 block/vmdk.c                        |   7 +-
 block/vpc.c                         |   7 +-
 block/vvfat.c                       |   7 +-
 chardev/char-mux.c                  |   1 +
 chardev/char-null.c                 |   1 +
 chardev/char-pty.c                  |  16 +-
 chardev/char-serial.c               |   1 +
 chardev/char-socket.c               |  48 +++++
 chardev/char-stdio.c                |  31 +++
 chardev/char.c                      |  49 ++++-
 dump/dump.c                         |   4 +-
 gdbstub.c                           |   1 +
 hmp-commands.hx                     |   2 +-
 hw/9pfs/9p.c                        |  11 +-
 hw/core/qdev-properties-system.c    |  12 ++
 hw/core/qdev-properties.c           |  44 +++++
 hw/display/virtio-gpu-base.c        |   8 +-
 hw/intc/arm_gic_kvm.c               |   3 +-
 hw/intc/arm_gicv3_its_kvm.c         |   3 +-
 hw/intc/arm_gicv3_kvm.c             |   3 +-
 hw/misc/ivshmem.c                   |   8 +-
 hw/net/virtio-net.c                 |  10 +-
 hw/pci/msix.c                       |   2 +-
 hw/pci/pci.c                        |  12 ++
 hw/ppc/pef.c                        |   2 +-
 hw/ppc/spapr.c                      |   2 +-
 hw/ppc/spapr_events.c               |   2 +-
 hw/ppc/spapr_rtas.c                 |   2 +-
 hw/remote/proxy.c                   |   7 +-
 hw/s390x/s390-virtio-ccw.c          |   9 +-
 hw/scsi/vhost-scsi.c                |   9 +-
 hw/vfio/common.c                    | 235 +++++++++++++++++++----
 hw/vfio/cpr.c                       | 177 ++++++++++++++++++
 hw/vfio/meson.build                 |   1 +
 hw/vfio/migration.c                 |  23 +--
 hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
 hw/vfio/trace-events                |   1 +
 hw/virtio/vhost-vdpa.c              |   6 +-
 hw/virtio/vhost.c                   |  32 +++-
 include/chardev/char-socket.h       |   1 +
 include/chardev/char.h              |   5 +
 include/exec/memory.h               |  48 +++++
 include/exec/ram_addr.h             |   1 +
 include/exec/ramblock.h             |   1 +
 include/hw/pci/msix.h               |   1 +
 include/hw/qdev-properties-system.h |   4 +
 include/hw/qdev-properties.h        |   3 +
 include/hw/vfio/vfio-common.h       |  12 ++
 include/hw/virtio/vhost.h           |   1 +
 include/migration/blocker.h         |  69 ++++++-
 include/migration/cpr-state.h       |  30 +++
 include/migration/cpr.h             |  20 ++
 include/migration/misc.h            |  13 +-
 include/migration/vmstate.h         |   2 +
 include/qapi/util.h                 |  28 +++
 include/qemu/osdep.h                |   9 +
 include/sysemu/runstate.h           |   2 +
 migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
 migration/cpr.c                     |  85 +++++++++
 migration/file.c                    |  62 ++++++
 migration/file.h                    |  14 ++
 migration/meson.build               |   3 +
 migration/migration.c               | 268 +++++++++++++++++++++++---
 migration/ram.c                     |  24 ++-
 migration/target.c                  |   1 +
 migration/trace-events              |  12 ++
 monitor/hmp-cmds.c                  |  59 +++---
 monitor/hmp.c                       |   3 +
 monitor/qmp.c                       |   4 +
 python/qemu/machine/machine.py      |  14 ++
 qapi/char.json                      |   7 +-
 qapi/migration.json                 |  68 ++++++-
 qapi/qapi-util.c                    |  37 ++++
 qemu-options.hx                     |  50 ++++-
 replay/replay.c                     |   4 +
 softmmu/memory.c                    |  31 ++-
 softmmu/physmem.c                   | 100 +++++++++-
 softmmu/runstate.c                  |  42 ++++-
 softmmu/vl.c                        |  10 +
 stubs/cpr-state.c                   |  26 +++
 stubs/meson.build                   |   2 +
 stubs/migr-blocker.c                |   9 +-
 stubs/migration.c                   |  33 ++++
 target/i386/kvm/kvm.c               |   8 +-
 target/i386/nvmm/nvmm-all.c         |   4 +-
 target/i386/sev.c                   |   2 +-
 target/i386/whpx/whpx-all.c         |   3 +-
 tests/avocado/cpr.py                | 176 ++++++++++++++++++
 tests/unit/meson.build              |   1 +
 tests/unit/test-strlist.c           |  81 ++++++++
 trace-events                        |   1 +
 ui/spice-core.c                     |   5 +-
 ui/vdagent.c                        |   5 +-
 util/oslib-posix.c                  |   9 +
 util/oslib-win32.c                  |   4 +
 105 files changed, 2781 insertions(+), 330 deletions(-)
 create mode 100644 hw/vfio/cpr.c
 create mode 100644 include/migration/cpr-state.h
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr-state.c
 create mode 100644 migration/cpr.c
 create mode 100644 migration/file.c
 create mode 100644 migration/file.h
 create mode 100644 stubs/cpr-state.c
 create mode 100644 stubs/migration.c
 create mode 100644 tests/avocado/cpr.py
 create mode 100644 tests/unit/test-strlist.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v9 1/46 migration: fix populate_vfio_info

Include CONFIG_DEVICES so that populate_vfio_info is instantiated for CONFIG_VFIO.

Fixes 43bd0bf30f ("migration: Move populate_vfio_info() into a separate file")
---
 migration/target.c | 1 +
 1 file changed, 1 insertion(+)
migration/target.c +1 -0
index 907ebf0..a0991bc 100644
@@ -8,6 +8,7 @@
 #include "qemu/osdep.h"
 #include "qapi/qapi-types-migration.h"
 #include "migration.h"
+#include CONFIG_DEVICES
 
 #ifdef CONFIG_VFIO
 #include "hw/vfio/vfio-common.h"
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v9 2/46 memory: RAM_NAMED_FILE flag

In reply to Steve Sistare

A memory-backend-ram or a memory-backend-memfd block with the RAM_SHARED flag set is not migrated when migrate_ignore_shared() is true, but this is wrong, because it has no named backing store, and its contents will be lost. Define a new flag RAM_NAMED_FILE to distinguish this case. Cpr will also test this flag, for similar reasons.

---
 backends/hostmem-file.c | 1 +
 include/exec/memory.h   | 3 +++
 include/exec/ram_addr.h | 1 +
 migration/ram.c         | 3 ++-
 softmmu/physmem.c       | 7 ++++++-
 5 files changed, 13 insertions(+), 2 deletions(-)
backends/hostmem-file.c +1 -0
index 2514128..486d77d 100644
@@ -56,6 +56,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     ram_flags = backend->share ? RAM_SHARED : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
+    ram_flags |= RAM_NAMED_FILE;
     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
                                      backend->size, fb->align, ram_flags,
                                      fb->mem_path, fb->readonly, errp);
include/exec/memory.h +3 -0
index bfb1de8..7513d04 100644
@@ -206,6 +206,9 @@ typedef struct IOMMUTLBEvent {
 /* RAM that isn't accessible through normal means. */
 #define RAM_PROTECTED (1 << 8)
 
+/* RAM is an mmap-ed named file */
+#define RAM_NAMED_FILE (1 << 9)
+
 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
                                        IOMMUNotifierFlag flags,
                                        hwaddr start, hwaddr end,
include/exec/ram_addr.h +1 -0
index f3e0c78..4405e2e 100644
@@ -94,6 +94,7 @@ static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
 }
 
 bool ramblock_is_pmem(RAMBlock *rb);
+bool ramblock_is_named_file(RAMBlock *rb);
 
 long qemu_minrampagesize(void);
 long qemu_maxrampagesize(void);
migration/ram.c +2 -1
index b94669b..3ea3b41 100644
@@ -165,7 +165,8 @@ out:
 bool ramblock_is_ignored(RAMBlock *block)
 {
     return !qemu_ram_is_migratable(block) ||
-           (migrate_ignore_shared() && qemu_ram_is_shared(block));
+           (migrate_ignore_shared() && qemu_ram_is_shared(block) &&
+            ramblock_is_named_file(block));
 }
 
 #undef RAMBLOCK_FOREACH
softmmu/physmem.c +6 -1
index dc3c3e5..cce721a 100644
@@ -2070,7 +2070,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
 
     /* Just support these ram flags by now. */
     assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
-                          RAM_PROTECTED)) == 0);
+                          RAM_PROTECTED | RAM_NAMED_FILE)) == 0);
 
     if (xen_enabled()) {
         error_setg(errp, "-mem-path not supported with Xen");
@@ -3675,6 +3675,11 @@ bool ramblock_is_pmem(RAMBlock *rb)
     return rb->flags & RAM_PMEM;
 }
 
+bool ramblock_is_named_file(RAMBlock *rb)
+{
+    return rb->flags & RAM_NAMED_FILE;
+}
+
 static void mtree_print_phys_entries(int start, int end, int skip, int ptr)
 {
     if (start == end - 1) {
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v9 3/46 migration: file URI

In reply to Steve Sistare

Extend the migration URI to support file:<filename>. This can be used for any migration scenario that does not require a reverse path. It is easier to use than the fd:<fdname> URI. It can be use in HMP commands, and as a qemu command-line parameter.

---
 migration/file.c       | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 migration/file.h       | 14 ++++++++++++
 migration/meson.build  |  1 +
 migration/migration.c  |  5 ++++
 migration/trace-events |  4 ++++
 qemu-options.hx        |  6 ++++-
 6 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 migration/file.c
 create mode 100644 migration/file.h
migration/file.c new file +62 -0
new file mode 100644
index 0000000..233bcda
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2021, 2022 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.
+ */
+
+#include "qemu/osdep.h"
+#include "channel.h"
+#include "file.h"
+#include "migration.h"
+#include "io/channel-file.h"
+#include "io/channel-util.h"
+#include "trace.h"
+
+void file_start_outgoing_migration(MigrationState *s, const char *filename,
+                                   Error **errp)
+{
+    g_autoptr(QIOChannelFile) fioc = NULL;
+    QIOChannel *ioc;
+
+    trace_migration_file_outgoing(filename);
+
+    fioc = qio_channel_file_new_path(filename, O_CREAT | O_WRONLY | O_TRUNC,
+                                     0600, errp);
+    if (!fioc) {
+        return;
+    }
+
+    ioc = QIO_CHANNEL(fioc);
+    qio_channel_set_name(ioc, "migration-file-outgoing");
+    migration_channel_connect(s, ioc, NULL, NULL);
+}
+
+static gboolean file_accept_incoming_migration(QIOChannel *ioc,
+                                               GIOCondition condition,
+                                               gpointer opaque)
+{
+    migration_channel_process_incoming(ioc);
+    object_unref(OBJECT(ioc));
+    return G_SOURCE_REMOVE;
+}
+
+void file_start_incoming_migration(const char *filename, Error **errp)
+{
+    QIOChannelFile *fioc = NULL;
+    QIOChannel *ioc;
+
+    trace_migration_file_incoming(filename);
+
+    fioc = qio_channel_file_new_path(filename, O_RDONLY, 0, errp);
+    if (!fioc) {
+        return;
+    }
+
+    ioc = QIO_CHANNEL(fioc);
+    qio_channel_set_name(QIO_CHANNEL(ioc), "migration-file-incoming");
+    qio_channel_add_watch_full(ioc, G_IO_IN,
+                               file_accept_incoming_migration,
+                               NULL, NULL,
+                               g_main_context_get_thread_default());
+}
migration/file.h new file +14 -0
new file mode 100644
index 0000000..aa697df
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2021, 2022 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_MIGRATION_FILE_H
+#define QEMU_MIGRATION_FILE_H
+void file_start_incoming_migration(const char *filename, Error **errp);
+
+void file_start_outgoing_migration(MigrationState *s, const char *filename,
+                                   Error **errp);
+#endif
migration/meson.build +1 -0
index 690487c..30a8392 100644
@@ -17,6 +17,7 @@ softmmu_ss.add(files(
   'colo.c',
   'exec.c',
   'fd.c',
+  'file.c',
   'global_state.c',
   'migration.c',
   'multifd.c',
migration/migration.c +5 -0
index e03f698..d1cfca1 100644
@@ -20,6 +20,7 @@
 #include "migration/blocker.h"
 #include "exec.h"
 #include "fd.h"
+#include "file.h"
 #include "socket.h"
 #include "sysemu/runstate.h"
 #include "sysemu/sysemu.h"
@@ -505,6 +506,8 @@ static void qemu_start_incoming_migration(const char *uri, Error **errp)
         exec_start_incoming_migration(p, errp);
     } else if (strstart(uri, "fd:", &p)) {
         fd_start_incoming_migration(p, errp);
+    } else if (strstart(uri, "file:", &p)) {
+        file_start_incoming_migration(p, errp);
     } else {
         error_setg(errp, "unknown migration protocol: %s", uri);
     }
@@ -2414,6 +2417,8 @@ void qmp_migrate(const char *uri, bool has_blk, bool blk,
         exec_start_outgoing_migration(s, p, &local_err);
     } else if (strstart(uri, "fd:", &p)) {
         fd_start_outgoing_migration(s, p, &local_err);
+    } else if (strstart(uri, "file:", &p)) {
+        file_start_outgoing_migration(s, p, &local_err);
     } else {
         if (!(has_resume && resume)) {
             yank_unregister_instance(MIGRATION_YANK_INSTANCE);
migration/trace-events +4 -0
index a34afe7..d72dd85 100644
@@ -302,6 +302,10 @@ migration_exec_incoming(const char *cmd) "cmd=%s"
 migration_fd_outgoing(int fd) "fd=%d"
 migration_fd_incoming(int fd) "fd=%d"
 
+# file.c
+migration_file_outgoing(const char *filename) "filename=%s"
+migration_file_incoming(const char *filename) "filename=%s"
+
 # socket.c
 migration_socket_incoming_accepted(void) ""
 migration_socket_outgoing_connected(const char *hostname) "hostname=%s"
qemu-options.hx +5 -1
index 79e0091..5ff47f4 100644
@@ -4451,6 +4451,7 @@ DEF("incoming", HAS_ARG, QEMU_OPTION_incoming, \
     "                prepare for incoming migration, listen on\n" \
     "                specified protocol and socket address\n" \
     "-incoming fd:fd\n" \
+    "-incoming file:filename\n" \
     "-incoming exec:cmdline\n" \
     "                accept incoming migration on given file descriptor\n" \
     "                or from given external command\n" \
@@ -4467,7 +4468,10 @@ SRST
     Prepare for incoming migration, listen on a given unix socket.
 
 ``-incoming fd:fd``
-    Accept incoming migration from a given filedescriptor.
+    Accept incoming migration from a given file descriptor.
+
+``-incoming file:filename``
+    Accept incoming migration from a given file.
 
 ``-incoming exec:cmdline``
     Accept incoming migration as an output from specified external
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v9 4/46 migration: mode parameter

In reply to Steve Sistare

Create a mode migration parameter that can be used to select alternate migration algorithms. The default mode is normal, representing the current migration algorithm, and does not need to be explicitly set.

No functional change until a new mode is added, except that the mode is shown by the 'info migrate' command.

---
 hw/core/qdev-properties-system.c    | 12 ++++++++++++
 include/hw/qdev-properties-system.h |  4 ++++
 include/migration/misc.h            |  4 ++++
 migration/migration.c               | 28 ++++++++++++++++++++++++++++
 monitor/hmp-cmds.c                  |  8 ++++++++
 qapi/migration.json                 | 28 +++++++++++++++++++++++++---
 6 files changed, 81 insertions(+), 3 deletions(-)
hw/core/qdev-properties-system.c +12 -0
index a91f605..0a7b25b 100644
@@ -650,6 +650,18 @@ const PropertyInfo qdev_prop_multifd_compression = {
     .set_default_value = qdev_propinfo_set_default_value_enum,
 };
 
+/* --- MigMode --- */
+
+const PropertyInfo qdev_prop_mig_mode = {
+    .name = "MigMode",
+    .description = "mig_mode values, "
+                   "normal/exec",
+    .enum_table = &MigMode_lookup,
+    .get = qdev_propinfo_get_enum,
+    .set = qdev_propinfo_set_enum,
+    .set_default_value = qdev_propinfo_set_default_value_enum,
+};
+
 /* --- Reserved Region --- */
 
 /*
include/hw/qdev-properties-system.h +4 -0
index 0ac327a..1418801 100644
@@ -7,6 +7,7 @@ extern const PropertyInfo qdev_prop_chr;
 extern const PropertyInfo qdev_prop_macaddr;
 extern const PropertyInfo qdev_prop_reserved_region;
 extern const PropertyInfo qdev_prop_multifd_compression;
+extern const PropertyInfo qdev_prop_mig_mode;
 extern const PropertyInfo qdev_prop_losttickpolicy;
 extern const PropertyInfo qdev_prop_blockdev_on_error;
 extern const PropertyInfo qdev_prop_bios_chs_trans;
@@ -41,6 +42,9 @@ extern const PropertyInfo qdev_prop_pcie_link_width;
 #define DEFINE_PROP_MULTIFD_COMPRESSION(_n, _s, _f, _d) \
     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_multifd_compression, \
                        MultiFDCompression)
+#define DEFINE_PROP_MIG_MODE(_n, _s, _f, _d) \
+    DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_mig_mode, \
+                       MigMode)
 #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
                         LostTickPolicy)
include/migration/misc.h +4 -0
index 4659067..1e01134 100644
@@ -15,6 +15,7 @@
 #define MIGRATION_MISC_H
 
 #include "qemu/notify.h"
+#include "qapi/qapi-types-migration.h"
 #include "qapi/qapi-types-net.h"
 
 /* migration/ram.c */
@@ -75,4 +76,7 @@ bool migration_in_bg_snapshot(void);
 /* migration/block-dirty-bitmap.c */
 void dirty_bitmap_mig_init(void);
 
+MigMode migrate_mode(void);
+MigMode migrate_mode_of(MigrationState *s);
+
 #endif
migration/migration.c +28 -0
index d1cfca1..38584db 100644
@@ -904,6 +904,8 @@ MigrationParameters *qmp_query_migrate_parameters(Error **errp)
 
     /* TODO use QAPI_CLONE() instead of duplicating it inline */
     params = g_malloc0(sizeof(*params));
+    params->has_mode = true;
+    params->mode = s->parameters.mode;
     params->has_compress_level = true;
     params->compress_level = s->parameters.compress_level;
     params->has_compress_threads = true;
@@ -1576,6 +1578,10 @@ static void migrate_params_test_apply(MigrateSetParameters *params,
 
     /* TODO use QAPI_CLONE() instead of duplicating it inline */
 
+    if (params->has_mode) {
+        dest->mode = params->mode;
+    }
+
     if (params->has_compress_level) {
         dest->compress_level = params->compress_level;
     }
@@ -1673,6 +1679,10 @@ static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
 
     /* TODO use QAPI_CLONE() instead of duplicating it inline */
 
+    if (params->has_mode) {
+        s->parameters.mode = params->mode;
+    }
+
     if (params->has_compress_level) {
         s->parameters.compress_level = params->compress_level;
     }
@@ -2663,6 +2673,20 @@ int migrate_use_tls(void)
     return s->parameters.tls_creds && *s->parameters.tls_creds;
 }
 
+MigMode migrate_mode(void)
+{
+    MigrationState *s;
+
+    s = migrate_get_current();
+
+    return s->parameters.mode;
+}
+
+MigMode migrate_mode_of(MigrationState *s)
+{
+    return s->parameters.mode;
+}
+
 int migrate_use_xbzrle(void)
 {
     MigrationState *s;
@@ -4321,6 +4345,9 @@ static Property migration_properties[] = {
                       clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
 
     /* Migration parameters */
+    DEFINE_PROP_MIG_MODE("mode", MigrationState,
+                      parameters.mode,
+                      MIG_MODE_NORMAL),
     DEFINE_PROP_UINT8("x-compress-level", MigrationState,
                       parameters.compress_level,
                       DEFAULT_MIGRATE_COMPRESS_LEVEL),
@@ -4454,6 +4481,7 @@ static void migration_instance_init(Object *obj)
     params->tls_creds = g_strdup("");
 
     /* Set has_* up only for parameter checks */
+    params->has_mode = true;
     params->has_compress_level = true;
     params->has_compress_threads = true;
     params->has_decompress_threads = true;
monitor/hmp-cmds.c +8 -0
index c6cd6f9..6632ada 100644
@@ -431,6 +431,10 @@ void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
             params->announce_step);
+        assert(params->has_mode);
+        monitor_printf(mon, "%s: %s\n",
+            MigrationParameter_str(MIGRATION_PARAMETER_MODE),
+            qapi_enum_lookup(&MigMode_lookup, params->mode));
         assert(params->has_compress_level);
         monitor_printf(mon, "%s: %u\n",
             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
@@ -1219,6 +1223,10 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
     }
 
     switch (val) {
+    case MIGRATION_PARAMETER_MODE:
+        p->has_mode = true;
+        visit_type_MigMode(v, param, &p->mode, &err);
+        break;
     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
         p->has_compress_level = true;
         visit_type_uint8(v, param, &p->compress_level, &err);
qapi/migration.json +25 -3
index 81185d4..931c492 100644
@@ -568,6 +568,16 @@
             { 'name': 'zstd', 'if': 'CONFIG_ZSTD' } ] }
 
 ##
+# @MigMode:
+#
+# @normal: the original form of migration.
+#
+# Since: 7.1
+##
+{ 'enum': 'MigMode',
+  'data': [ 'normal' ] }
+
+##
 # @BitmapMigrationBitmapAliasTransform:
 #
 # @persistent: If present, the bitmap will be made persistent
@@ -627,6 +637,9 @@
 #
 # Migration parameters enumeration
 #
+# @mode: Migration mode. See description in @MigMode. Default is 'normal'.
+#        (Since 7.1)
+#
 # @announce-initial: Initial delay (in milliseconds) before sending the first
 #                    announce (Since 4.0)
 #
@@ -782,7 +795,8 @@
 # Since: 2.4
 ##
 { 'enum': 'MigrationParameter',
-  'data': ['announce-initial', 'announce-max',
+  'data': ['mode',
+           'announce-initial', 'announce-max',
            'announce-rounds', 'announce-step',
            'compress-level', 'compress-threads', 'decompress-threads',
            'compress-wait-thread', 'throttle-trigger-threshold',
@@ -801,6 +815,9 @@
 ##
 # @MigrateSetParameters:
 #
+# @mode: Migration mode. See description in @MigMode. Default is 'normal'.
+#        (Since 7.1)
+#
 # @announce-initial: Initial delay (in milliseconds) before sending the first
 #                    announce (Since 4.0)
 #
@@ -949,7 +966,8 @@
 # TODO either fuse back into MigrationParameters, or make
 # MigrationParameters members mandatory
 { 'struct': 'MigrateSetParameters',
-  'data': { '*announce-initial': 'size',
+  'data': { '*mode': 'MigMode',
+            '*announce-initial': 'size',
             '*announce-max': 'size',
             '*announce-rounds': 'size',
             '*announce-step': 'size',
@@ -999,6 +1017,9 @@
 #
 # The optional members aren't actually optional.
 #
+# @mode: Migration mode. See description in @MigMode. Default is 'normal'.
+#        (Since 7.1)
+#
 # @announce-initial: Initial delay (in milliseconds) before sending the
 #                    first announce (Since 4.0)
 #
@@ -1147,7 +1168,8 @@
 # Since: 2.4
 ##
 { 'struct': 'MigrationParameters',
-  'data': { '*announce-initial': 'size',
+  'data': { '*mode': 'MigMode',
+            '*announce-initial': 'size',
             '*announce-max': 'size',
             '*announce-rounds': 'size',
             '*announce-step': 'size',
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH v9 5/46 migration: migrate-enable-mode option

In reply to Steve Sistare

Add the '-migrate-enable-mode <mode>' command-line option as a pre-requisite for migration using the mode. Multiple -migrate-enable-mode options may be specified, one per mode.

Requiring -migrate-enable-mode allows qemu to initialize objects differently, if necessary, so that migration for a mode is not blocked.

---
 include/migration/misc.h |  2 ++
 migration/migration.c    | 31 +++++++++++++++++++++++++++++++
 qemu-options.hx          | 10 ++++++++++
 softmmu/vl.c             |  4 ++++
 4 files changed, 47 insertions(+)
include/migration/misc.h +2 -0
index 1e01134..71b6285 100644
@@ -78,5 +78,7 @@ void dirty_bitmap_mig_init(void);
 
 MigMode migrate_mode(void);
 MigMode migrate_mode_of(MigrationState *s);
+void migrate_enable_mode(MigMode mode);
+bool migrate_mode_enabled(MigMode mode);
 
 #endif
migration/migration.c +31 -0
index 38584db..5f704a9 100644
@@ -173,6 +173,7 @@ INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
 
 static MigrationState *current_migration;
 static MigrationIncomingState *current_incoming;
+static int migrate_enabled_modes = BIT(MIG_MODE_NORMAL);
 
 static GSList *migration_blockers;
 
@@ -2140,6 +2141,29 @@ bool migration_is_active(MigrationState *s)
             s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
 }
 
+void migrate_enable_mode(MigMode mode)
+{
+    migrate_enabled_modes |= BIT(mode);
+}
+
+bool migrate_mode_enabled(MigMode mode)
+{
+    return !!(migrate_enabled_modes & BIT(mode));
+}
+
+static int migrate_check_enabled(Error **errp)
+{
+    MigMode mode = migrate_mode();
+
+    if (!migrate_mode_enabled(mode)) {
+        error_setg(errp, "migrate mode is not enabled.  "
+                         "Use '-migrate-mode-enable %s'.",
+                   MigMode_str(mode));
+        return -1;
+    }
+    return 0;
+}
+
 void migrate_init(MigrationState *s)
 {
     /*
@@ -2210,6 +2234,9 @@ void qmp_migrate_incoming(const char *uri, Error **errp)
     Error *local_err = NULL;
     static bool once = true;
 
+    if (migrate_check_enabled(errp)) {
+        return;
+    }
     if (!once) {
         error_setg(errp, "The incoming migration has already been started");
         return;
@@ -2356,6 +2383,10 @@ static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
         return false;
     }
 
+    if (migrate_check_enabled(errp)) {
+        return false;
+    }
+
     if (migration_is_blocked(errp)) {
         return false;
     }
qemu-options.hx +10 -0
index 5ff47f4..7797d3d 100644
@@ -4491,6 +4491,16 @@ SRST
     an unmigratable state.
 ERST
 
+DEF("migrate-mode-enable", HAS_ARG, QEMU_OPTION_migrate_mode_enable, \
+    "-migrate-mode-enable <mode>    enable the migration mode.\n",
+    QEMU_ARCH_ALL)
+SRST
+``-migrate-mode-enable <mode>``
+    Enable the specified migrate mode.  May be supplied
+    multiple times, once per mode.  This is a pre-requisite for performing a
+    migration using any mode except 'normal'.
+ERST
+
 DEF("nodefaults", 0, QEMU_OPTION_nodefaults, \
     "-nodefaults     don't create default devices\n", QEMU_ARCH_ALL)
 SRST
softmmu/vl.c +4 -0
index 7189eb6..83f3be0 100644
@@ -3364,6 +3364,10 @@ void qemu_init(int argc, char **argv, char **envp)
             case QEMU_OPTION_only_migratable:
                 only_migratable = 1;
                 break;
+            case QEMU_OPTION_migrate_mode_enable:
+                migrate_enable_mode(qapi_enum_parse(&MigMode_lookup, optarg, -1,
+                                                    &error_fatal));
+                break;
             case QEMU_OPTION_nodefaults:
                 has_defaults = 0;
                 break;
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH v9 6/46 migration: simplify blockers

In reply to Steve Sistare

Modify migrate_add_blocker and migrate_del_blocker to take an Error ** reason. This allows migration to own the Error object, so that if an error occurs, migration code can free the Error and clear the client handle, simplifying client code. This is a pre-requisite for a subsequent patch that will allow one Error blocker to be registered for multiple migration modes.

No functional change.

---
 backends/tpm/tpm_emulator.c  | 10 ++--------
 block/parallels.c            |  6 ++----
 block/qcow.c                 |  6 ++----
 block/vdi.c                  |  6 ++----
 block/vhdx.c                 |  6 ++----
 block/vmdk.c                 |  6 ++----
 block/vpc.c                  |  6 ++----
 block/vvfat.c                |  6 ++----
 dump/dump.c                  |  4 ++--
 hw/9pfs/9p.c                 | 10 ++--------
 hw/display/virtio-gpu-base.c |  8 ++------
 hw/intc/arm_gic_kvm.c        |  3 +--
 hw/intc/arm_gicv3_its_kvm.c  |  3 +--
 hw/intc/arm_gicv3_kvm.c      |  3 +--
 hw/misc/ivshmem.c            |  8 ++------
 hw/ppc/pef.c                 |  2 +-
 hw/ppc/spapr.c               |  2 +-
 hw/ppc/spapr_events.c        |  2 +-
 hw/ppc/spapr_rtas.c          |  2 +-
 hw/remote/proxy.c            |  7 ++-----
 hw/s390x/s390-virtio-ccw.c   |  9 +++------
 hw/scsi/vhost-scsi.c         |  8 +++-----
 hw/vfio/migration.c          | 13 ++-----------
 hw/virtio/vhost-vdpa.c       |  6 +++---
 hw/virtio/vhost.c            |  8 ++------
 include/migration/blocker.h  | 28 ++++++++++++++++++++++------
 migration/migration.c        | 29 +++++++++++++++++++++--------
 stubs/migr-blocker.c         |  4 ++--
 target/i386/kvm/kvm.c        |  8 ++++----
 target/i386/nvmm/nvmm-all.c  |  3 +--
 target/i386/sev.c            |  2 +-
 target/i386/whpx/whpx-all.c  |  3 +--
 ui/vdagent.c                 |  5 ++---
 33 files changed, 100 insertions(+), 132 deletions(-)
backends/tpm/tpm_emulator.c +2 -8
index 87d061e..ed67633 100644
@@ -492,11 +492,8 @@ static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
         error_setg(&tpm_emu->migration_blocker,
                    "Migration disabled: TPM emulator does not support "
                    "migration");
-        if (migrate_add_blocker(tpm_emu->migration_blocker, &err) < 0) {
+        if (migrate_add_blocker(&tpm_emu->migration_blocker, &err) < 0) {
             error_report_err(err);
-            error_free(tpm_emu->migration_blocker);
-            tpm_emu->migration_blocker = NULL;
-
             return -1;
         }
     }
@@ -950,10 +947,7 @@ static void tpm_emulator_inst_finalize(Object *obj)
 
     qapi_free_TPMEmulatorOptions(tpm_emu->options);
 
-    if (tpm_emu->migration_blocker) {
-        migrate_del_blocker(tpm_emu->migration_blocker);
-        error_free(tpm_emu->migration_blocker);
-    }
+    migrate_del_blocker(&tpm_emu->migration_blocker);
 
     tpm_sized_buffer_reset(&state_blobs->volatil);
     tpm_sized_buffer_reset(&state_blobs->permanent);
block/parallels.c +2 -4
index 8b235b9..25d24d6 100644
@@ -878,9 +878,8 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The Parallels format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(s->migration_blocker, errp);
+    ret = migrate_add_blocker(&s->migration_blocker, errp);
     if (ret < 0) {
-        error_free(s->migration_blocker);
         goto fail;
     }
     qemu_co_mutex_init(&s->lock);
@@ -912,8 +911,7 @@ static void parallels_close(BlockDriverState *bs)
     g_free(s->bat_dirty_bmap);
     qemu_vfree(s->header);
 
-    migrate_del_blocker(s->migration_blocker);
-    error_free(s->migration_blocker);
+    migrate_del_blocker(&s->migration_blocker);
 }
 
 static BlockDriver bdrv_parallels = {
block/qcow.c +2 -4
index 311aaa8..3b4e6a4 100644
@@ -305,9 +305,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(s->migration_blocker, errp);
+    ret = migrate_add_blocker(&s->migration_blocker, errp);
     if (ret < 0) {
-        error_free(s->migration_blocker);
         goto fail;
     }
 
@@ -798,8 +797,7 @@ static void qcow_close(BlockDriverState *bs)
     g_free(s->cluster_cache);
     g_free(s->cluster_data);
 
-    migrate_del_blocker(s->migration_blocker);
-    error_free(s->migration_blocker);
+    migrate_del_blocker(&s->migration_blocker);
 }
 
 static int coroutine_fn qcow_co_create(BlockdevCreateOptions *opts,
block/vdi.c +2 -4
index e942325..cead854 100644
@@ -495,9 +495,8 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(s->migration_blocker, errp);
+    ret = migrate_add_blocker(&s->migration_blocker, errp);
     if (ret < 0) {
-        error_free(s->migration_blocker);
         goto fail_free_bmap;
     }
 
@@ -985,8 +984,7 @@ static void vdi_close(BlockDriverState *bs)
 
     qemu_vfree(s->bmap);
 
-    migrate_del_blocker(s->migration_blocker);
-    error_free(s->migration_blocker);
+    migrate_del_blocker(&s->migration_blocker);
 }
 
 static int vdi_has_zero_init(BlockDriverState *bs)
block/vhdx.c +2 -4
index e10e78e..07b6077 100644
@@ -985,8 +985,7 @@ static void vhdx_close(BlockDriverState *bs)
     s->bat = NULL;
     qemu_vfree(s->parent_entries);
     s->parent_entries = NULL;
-    migrate_del_blocker(s->migration_blocker);
-    error_free(s->migration_blocker);
+    migrate_del_blocker(&s->migration_blocker);
     qemu_vfree(s->log.hdr);
     s->log.hdr = NULL;
     vhdx_region_unregister_all(s);
@@ -1094,9 +1093,8 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(s->migration_blocker, errp);
+    ret = migrate_add_blocker(&s->migration_blocker, errp);
     if (ret < 0) {
-        error_free(s->migration_blocker);
         goto fail;
     }
 
block/vmdk.c +2 -4
index fe07a54..7a7cb57 100644
@@ -1360,9 +1360,8 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(s->migration_blocker, errp);
+    ret = migrate_add_blocker(&s->migration_blocker, errp);
     if (ret < 0) {
-        error_free(s->migration_blocker);
         goto fail;
     }
 
@@ -2855,8 +2854,7 @@ static void vmdk_close(BlockDriverState *bs)
     vmdk_free_extents(bs);
     g_free(s->create_type);
 
-    migrate_del_blocker(s->migration_blocker);
-    error_free(s->migration_blocker);
+    migrate_del_blocker(&s->migration_blocker);
 }
 
 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
block/vpc.c +2 -4
index 4f49ef2..a465784 100644
@@ -450,9 +450,8 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(s->migration_blocker, errp);
+    ret = migrate_add_blocker(&s->migration_blocker, errp);
     if (ret < 0) {
-        error_free(s->migration_blocker);
         goto fail;
     }
 
@@ -1188,8 +1187,7 @@ static void vpc_close(BlockDriverState *bs)
     g_free(s->pageentry_u8);
 #endif
 
-    migrate_del_blocker(s->migration_blocker);
-    error_free(s->migration_blocker);
+    migrate_del_blocker(&s->migration_blocker);
 }
 
 static QemuOptsList vpc_create_opts = {
block/vvfat.c +2 -4
index d6dd919..bc96876 100644
@@ -1265,9 +1265,8 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
                    "The vvfat (rw) format used by node '%s' "
                    "does not support live migration",
                    bdrv_get_device_or_node_name(bs));
-        ret = migrate_add_blocker(s->migration_blocker, errp);
+        ret = migrate_add_blocker(&s->migration_blocker, errp);
         if (ret < 0) {
-            error_free(s->migration_blocker);
             goto fail;
         }
     }
@@ -3216,8 +3215,7 @@ static void vvfat_close(BlockDriverState *bs)
     g_free(s->cluster_buffer);
 
     if (s->qcow) {
-        migrate_del_blocker(s->migration_blocker);
-        error_free(s->migration_blocker);
+        migrate_del_blocker(&s->migration_blocker);
     }
 }
 
dump/dump.c +2 -2
index 4d9658f..9e4ab73 100644
@@ -108,7 +108,7 @@ static int dump_cleanup(DumpState *s)
             qemu_mutex_unlock_iothread();
         }
     }
-    migrate_del_blocker(dump_migration_blocker);
+    migrate_del_blocker(&dump_migration_blocker);
 
     return 0;
 }
@@ -1970,7 +1970,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
      * Allows even for -only-migratable, but forbid migration during the
      * process of dump guest memory.
      */
-    if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
+    if (migrate_add_blocker_internal(&dump_migration_blocker, errp)) {
         /* Remember to release the fd before passing it over to dump state */
         close(fd);
         return;
hw/9pfs/9p.c +2 -8
index aebadea..d5ac01f 100644
@@ -409,11 +409,7 @@ static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
              * delete the migration blocker. Ideally, this
              * should be hooked to transport close notification
              */
-            if (pdu->s->migration_blocker) {
-                migrate_del_blocker(pdu->s->migration_blocker);
-                error_free(pdu->s->migration_blocker);
-                pdu->s->migration_blocker = NULL;
-            }
+            migrate_del_blocker(&pdu->s->migration_blocker);
         }
         return free_fid(pdu, fidp);
     }
@@ -1491,10 +1487,8 @@ static void coroutine_fn v9fs_attach(void *opaque)
         error_setg(&s->migration_blocker,
                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
-        err = migrate_add_blocker(s->migration_blocker, NULL);
+        err = migrate_add_blocker(&s->migration_blocker, NULL);
         if (err < 0) {
-            error_free(s->migration_blocker);
-            s->migration_blocker = NULL;
             clunk_fid(s, fid);
             goto out;
         }
hw/display/virtio-gpu-base.c +2 -6
index a29f191..995c9a9 100644
@@ -167,8 +167,7 @@ virtio_gpu_base_device_realize(DeviceState *qdev,
 
     if (virtio_gpu_virgl_enabled(g->conf)) {
         error_setg(&g->migration_blocker, "virgl is not yet migratable");
-        if (migrate_add_blocker(g->migration_blocker, errp) < 0) {
-            error_free(g->migration_blocker);
+        if (migrate_add_blocker(&g->migration_blocker, errp) < 0) {
             return false;
         }
     }
@@ -232,10 +231,7 @@ virtio_gpu_base_device_unrealize(DeviceState *qdev)
 {
     VirtIOGPUBase *g = VIRTIO_GPU_BASE(qdev);
 
-    if (g->migration_blocker) {
-        migrate_del_blocker(g->migration_blocker);
-        error_free(g->migration_blocker);
-    }
+    migrate_del_blocker(&g->migration_blocker);
 }
 
 static void
hw/intc/arm_gic_kvm.c +1 -2
index 7d2a132..c9fac45 100644
@@ -514,8 +514,7 @@ static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
     if (!kvm_arm_gic_can_save_restore(s)) {
         error_setg(&s->migration_blocker, "This operating system kernel does "
                                           "not support vGICv2 migration");
-        if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
-            error_free(s->migration_blocker);
+        if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
             return;
         }
     }
hw/intc/arm_gicv3_its_kvm.c +1 -2
index 529c7bd..b80d2a6 100644
@@ -114,8 +114,7 @@ static void kvm_arm_its_realize(DeviceState *dev, Error **errp)
         GITS_CTLR)) {
         error_setg(&s->migration_blocker, "This operating system kernel "
                    "does not support vITS migration");
-        if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
-            error_free(s->migration_blocker);
+        if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
             return;
         }
     } else {
hw/intc/arm_gicv3_kvm.c +1 -2
index 3ca643e..edd058e 100644
@@ -876,8 +876,7 @@ static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
                                GICD_CTLR)) {
         error_setg(&s->migration_blocker, "This operating system kernel does "
                                           "not support vGICv3 migration");
-        if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
-            error_free(s->migration_blocker);
+        if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
             return;
         }
     }
hw/misc/ivshmem.c +2 -6
index 8270db5..29fd249 100644
@@ -903,8 +903,7 @@ static void ivshmem_common_realize(PCIDevice *dev, Error **errp)
     if (!ivshmem_is_master(s)) {
         error_setg(&s->migration_blocker,
                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
-        if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
-            error_free(s->migration_blocker);
+        if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
             return;
         }
     }
@@ -922,10 +921,7 @@ static void ivshmem_exit(PCIDevice *dev)
     IVShmemState *s = IVSHMEM_COMMON(dev);
     int i;
 
-    if (s->migration_blocker) {
-        migrate_del_blocker(s->migration_blocker);
-        error_free(s->migration_blocker);
-    }
+    migrate_del_blocker(&s->migration_blocker);
 
     if (memory_region_is_mapped(s->ivshmem_bar2)) {
         if (!s->hostmem) {
hw/ppc/pef.c +1 -1
index cc44d5e..d28ed3b 100644
@@ -63,7 +63,7 @@ static int kvmppc_svm_init(ConfidentialGuestSupport *cgs, Error **errp)
     /* add migration blocker */
     error_setg(&pef_mig_blocker, "PEF: Migration is not implemented");
     /* NB: This can fail if --only-migratable is used */
-    migrate_add_blocker(pef_mig_blocker, &error_fatal);
+    migrate_add_blocker(&pef_mig_blocker, &error_fatal);
 
     cgs->ready = true;
 
hw/ppc/spapr.c +1 -1
index bc9ba6e..386deff 100644
@@ -1723,7 +1723,7 @@ static void spapr_machine_reset(MachineState *machine)
     /* Signal all vCPUs waiting on this condition */
     qemu_cond_broadcast(&spapr->fwnmi_machine_check_interlock_cond);
 
-    migrate_del_blocker(spapr->fwnmi_migration_blocker);
+    migrate_del_blocker(&spapr->fwnmi_migration_blocker);
 }
 
 static void spapr_create_nvram(SpaprMachineState *spapr)
hw/ppc/spapr_events.c +1 -1
index 4508e40..c1b7bb9 100644
@@ -920,7 +920,7 @@ void spapr_mce_req_event(PowerPCCPU *cpu, bool recovered)
      * fails when running with -only-migrate.  A proper interface to
      * delay migration completion for a bit could avoid that.
      */
-    ret = migrate_add_blocker(spapr->fwnmi_migration_blocker, NULL);
+    ret = migrate_add_blocker(&spapr->fwnmi_migration_blocker, NULL);
     if (ret == -EBUSY) {
         warn_report("Received a fwnmi while migration was in progress");
     }
hw/ppc/spapr_rtas.c +1 -1
index d58b65e..69cdf81 100644
@@ -495,7 +495,7 @@ static void rtas_ibm_nmi_interlock(PowerPCCPU *cpu,
     spapr->fwnmi_machine_check_interlock = -1;
     qemu_cond_signal(&spapr->fwnmi_machine_check_interlock_cond);
     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
-    migrate_del_blocker(spapr->fwnmi_migration_blocker);
+    migrate_del_blocker(&spapr->fwnmi_migration_blocker);
 }
 
 static struct rtas_call {
hw/remote/proxy.c +2 -5
index 1c7786b..d8797ac 100644
@@ -108,8 +108,7 @@ static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)
 
     error_setg(&dev->migration_blocker, "%s does not support migration",
                TYPE_PCI_PROXY_DEV);
-    if (migrate_add_blocker(dev->migration_blocker, errp) < 0) {
-        error_free(dev->migration_blocker);
+    if (migrate_add_blocker(&dev->migration_blocker, errp) < 0) {
         object_unref(dev->ioc);
         return;
     }
@@ -135,9 +134,7 @@ static void pci_proxy_dev_exit(PCIDevice *pdev)
         qio_channel_close(dev->ioc, NULL);
     }
 
-    migrate_del_blocker(dev->migration_blocker);
-
-    error_free(dev->migration_blocker);
+    migrate_del_blocker(&dev->migration_blocker);
 
     proxy_memory_listener_deconfigure(&dev->proxy_listener);
 
hw/s390x/s390-virtio-ccw.c +3 -6
index cc3097b..7edf6da 100644
@@ -323,8 +323,7 @@ static void s390_machine_unprotect(S390CcwMachineState *ms)
 {
     s390_pv_vm_disable();
     ms->pv = false;
-    migrate_del_blocker(pv_mig_blocker);
-    error_free_or_abort(&pv_mig_blocker);
+    migrate_del_blocker(&pv_mig_blocker);
     ram_block_discard_disable(false);
 }
 
@@ -347,11 +346,10 @@ static int s390_machine_protect(S390CcwMachineState *ms)
 
     error_setg(&pv_mig_blocker,
                "protected VMs are currently not migrateable.");
-    rc = migrate_add_blocker(pv_mig_blocker, &local_err);
+    rc = migrate_add_blocker(&pv_mig_blocker, &local_err);
     if (rc) {
         ram_block_discard_disable(false);
         error_report_err(local_err);
-        error_free_or_abort(&pv_mig_blocker);
         return rc;
     }
 
@@ -359,8 +357,7 @@ static int s390_machine_protect(S390CcwMachineState *ms)
     rc = s390_pv_vm_enable();
     if (rc) {
         ram_block_discard_disable(false);
-        migrate_del_blocker(pv_mig_blocker);
-        error_free_or_abort(&pv_mig_blocker);
+        migrate_del_blocker(&pv_mig_blocker);
         return rc;
     }
 
hw/scsi/vhost-scsi.c +3 -5
index 3059068..b5e34f2 100644
@@ -208,7 +208,7 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
                 "When external environment supports it (Orchestrator migrates "
                 "target SCSI device state or use shared storage over network), "
                 "set 'migratable' property to true to enable migration.");
-        if (migrate_add_blocker(vsc->migration_blocker, errp) < 0) {
+        if (migrate_add_blocker(&vsc->migration_blocker, errp) < 0) {
             goto free_virtio;
         }
     }
@@ -241,10 +241,9 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
  free_vqs:
     g_free(vqs);
     if (!vsc->migratable) {
-        migrate_del_blocker(vsc->migration_blocker);
+        migrate_del_blocker(&vsc->migration_blocker);
     }
  free_virtio:
-    error_free(vsc->migration_blocker);
     virtio_scsi_common_unrealize(dev);
  close_fd:
     if (vhostfd >= 0) {
@@ -260,8 +259,7 @@ static void vhost_scsi_unrealize(DeviceState *dev)
     struct vhost_virtqueue *vqs = vsc->dev.vqs;
 
     if (!vsc->migratable) {
-        migrate_del_blocker(vsc->migration_blocker);
-        error_free(vsc->migration_blocker);
+        migrate_del_blocker(&vsc->migration_blocker);
     }
 
     /* This will stop vhost backend. */
hw/vfio/migration.c +2 -11
index a6ad1f8..d6f9e29 100644
@@ -887,12 +887,7 @@ add_blocker:
                "VFIO device doesn't support migration");
     g_free(info);
 
-    ret = migrate_add_blocker(vbasedev->migration_blocker, errp);
-    if (ret < 0) {
-        error_free(vbasedev->migration_blocker);
-        vbasedev->migration_blocker = NULL;
-    }
-    return ret;
+    return migrate_add_blocker(&vbasedev->migration_blocker, errp);
 }
 
 void vfio_migration_finalize(VFIODevice *vbasedev)
@@ -906,9 +901,5 @@ void vfio_migration_finalize(VFIODevice *vbasedev)
         vfio_migration_exit(vbasedev);
     }
 
-    if (vbasedev->migration_blocker) {
-        migrate_del_blocker(vbasedev->migration_blocker);
-        error_free(vbasedev->migration_blocker);
-        vbasedev->migration_blocker = NULL;
-    }
+    migrate_del_blocker(&vbasedev->migration_blocker);
 }
hw/virtio/vhost-vdpa.c +3 -3
index 291cd19..19ea2ee 100644
@@ -1024,7 +1024,7 @@ static bool vhost_vdpa_svqs_start(struct vhost_dev *dev)
     }
 
     if (v->migration_blocker) {
-        int r = migrate_add_blocker(v->migration_blocker, &err);
+        int r = migrate_add_blocker(&v->migration_blocker, &err);
         if (unlikely(r < 0)) {
             return false;
         }
@@ -1073,7 +1073,7 @@ err:
     }
 
     if (v->migration_blocker) {
-        migrate_del_blocker(v->migration_blocker);
+        migrate_remove_blocker(v->migration_blocker);
     }
 
     return false;
@@ -1096,7 +1096,7 @@ static bool vhost_vdpa_svqs_stop(struct vhost_dev *dev)
     }
 
     if (v->migration_blocker) {
-        migrate_del_blocker(v->migration_blocker);
+        migrate_remove_blocker(v->migration_blocker);
     }
     return true;
 }
hw/virtio/vhost.c +2 -6
index 0827d63..ae52b62 100644
@@ -1431,9 +1431,8 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     }
 
     if (hdev->migration_blocker != NULL) {
-        r = migrate_add_blocker(hdev->migration_blocker, errp);
+        r = migrate_add_blocker(&hdev->migration_blocker, errp);
         if (r < 0) {
-            error_free(hdev->migration_blocker);
             goto fail_busyloop;
         }
     }
@@ -1481,10 +1480,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
         memory_listener_unregister(&hdev->memory_listener);
         QLIST_REMOVE(hdev, entry);
     }
-    if (hdev->migration_blocker) {
-        migrate_del_blocker(hdev->migration_blocker);
-        error_free(hdev->migration_blocker);
-    }
+    migrate_del_blocker(&hdev->migration_blocker);
     g_free(hdev->mem);
     g_free(hdev->mem_sections);
     if (hdev->vhost_ops) {
include/migration/blocker.h +22 -6
index 9cebe2b..1483f5d 100644
@@ -17,19 +17,22 @@
 /**
  * @migrate_add_blocker - prevent migration from proceeding
  *
- * @reason - an error to be returned whenever migration is attempted
+ * @reasonp - address of an error to be returned whenever migration is attempted
  *
  * @errp - [out] The reason (if any) we cannot block migration right now.
  *
  * @returns - 0 on success, -EBUSY/-EACCES on failure, with errp set.
+ *
+ * *@reasonp is freed and set to NULL if failure is returned.
+ * On success, the caller must not free *@reasonp before the blocker is removed.
  */
-int migrate_add_blocker(Error *reason, Error **errp);
+int migrate_add_blocker(Error **reasonp, Error **errp);
 
 /**
  * @migrate_add_blocker_internal - prevent migration from proceeding without
  *                                 only-migrate implications
  *
- * @reason - an error to be returned whenever migration is attempted
+ * @reasonp - address of an error to be returned whenever migration is attempted
  *
  * @errp - [out] The reason (if any) we cannot block migration right now.
  *
@@ -38,14 +41,27 @@ int migrate_add_blocker(Error *reason, Error **errp);
  * Some of the migration blockers can be temporary (e.g., for a few seconds),
  * so it shouldn't need to conflict with "-only-migratable".  For those cases,
  * we can call this function rather than @migrate_add_blocker().
+ *
+ * *@reasonp is freed and set to NULL if failure is returned.
+ * On success, the caller must not free *@reasonp before the blocker is removed.
+ */
+int migrate_add_blocker_internal(Error **reasonp, Error **errp);
+
+/**
+ * @migrate_del_blocker - remove a blocking error from migration and free it.
+ *
+ * @reasonp - address of the error blocking migration
+ *
+ * This function frees *@reasonp and sets it to NULL.
  */
-int migrate_add_blocker_internal(Error *reason, Error **errp);
+void migrate_del_blocker(Error **reasonp);
 
 /**
- * @migrate_del_blocker - remove a blocking error from migration
+ * @migrate_remove_blocker - remove a migration blocker.
  *
  * @reason - the error blocking migration
+ *
  */
-void migrate_del_blocker(Error *reason);
+void migrate_remove_blocker(Error *reason);
 
 #endif
migration/migration.c +21 -8
index 5f704a9..0d3bed5 100644
@@ -2198,35 +2198,48 @@ void migrate_init(MigrationState *s)
     s->threshold_size = 0;
 }
 
-int migrate_add_blocker_internal(Error *reason, Error **errp)
+int migrate_add_blocker_internal(Error **reasonp, Error **errp)
 {
     /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */
     if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) {
-        error_propagate_prepend(errp, error_copy(reason),
+        error_propagate_prepend(errp, *reasonp,
                                 "disallowing migration blocker "
                                 "(migration/snapshot in progress) for: ");
+        *reasonp = NULL;
         return -EBUSY;
     }
 
-    migration_blockers = g_slist_prepend(migration_blockers, reason);
+    migration_blockers = g_slist_prepend(migration_blockers, *reasonp);
     return 0;
 }
 
-int migrate_add_blocker(Error *reason, Error **errp)
+int migrate_add_blocker(Error **reasonp, Error **errp)
 {
     if (only_migratable) {
-        error_propagate_prepend(errp, error_copy(reason),
+        error_propagate_prepend(errp, *reasonp,
                                 "disallowing migration blocker "
                                 "(--only-migratable) for: ");
+        *reasonp = NULL;
         return -EACCES;
     }
 
-    return migrate_add_blocker_internal(reason, errp);
+    return migrate_add_blocker_internal(reasonp, errp);
 }
 
-void migrate_del_blocker(Error *reason)
+void migrate_del_blocker(Error **reasonp)
 {
-    migration_blockers = g_slist_remove(migration_blockers, reason);
+    if (*reasonp) {
+        migrate_remove_blocker(*reasonp);
+        error_free(*reasonp);
+        *reasonp = NULL;
+    }
+}
+
+void migrate_remove_blocker(Error *reason)
+{
+    if (reason) {
+        migration_blockers = g_slist_remove(migration_blockers, reason);
+    }
 }
 
 void qmp_migrate_incoming(const char *uri, Error **errp)
stubs/migr-blocker.c +2 -2
index 5676a2f..17a5dbf 100644
@@ -1,11 +1,11 @@
 #include "qemu/osdep.h"
 #include "migration/blocker.h"
 
-int migrate_add_blocker(Error *reason, Error **errp)
+int migrate_add_blocker(Error **reasonp, Error **errp)
 {
     return 0;
 }
 
-void migrate_del_blocker(Error *reason)
+void migrate_del_blocker(Error **reasonp)
 {
 }
target/i386/kvm/kvm.c +4 -4
index f148a6d..a4aba94 100644
@@ -1571,7 +1571,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
         error_setg(&hv_passthrough_mig_blocker,
                    "'hv-passthrough' CPU flag prevents migration, use explicit"
                    " set of hv-* flags instead");
-        ret = migrate_add_blocker(hv_passthrough_mig_blocker, &local_err);
+        ret = migrate_add_blocker(&hv_passthrough_mig_blocker, &local_err);
         if (ret < 0) {
             error_report_err(local_err);
             return ret;
@@ -1585,7 +1585,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
                    " use explicit 'hv-no-nonarch-coresharing=on' instead (but"
                    " make sure SMT is disabled and/or that vCPUs are properly"
                    " pinned)");
-        ret = migrate_add_blocker(hv_no_nonarch_cs_mig_blocker, &local_err);
+        ret = migrate_add_blocker(&hv_no_nonarch_cs_mig_blocker, &local_err);
         if (ret < 0) {
             error_report_err(local_err);
             return ret;
@@ -2082,7 +2082,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
             error_setg(&invtsc_mig_blocker,
                        "State blocked by non-migratable CPU device"
                        " (invtsc flag)");
-            r = migrate_add_blocker(invtsc_mig_blocker, &local_err);
+            r = migrate_add_blocker(&invtsc_mig_blocker, &local_err);
             if (r < 0) {
                 error_report_err(local_err);
                 return r;
@@ -2149,7 +2149,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     return 0;
 
  fail:
-    migrate_del_blocker(invtsc_mig_blocker);
+    migrate_del_blocker(&invtsc_mig_blocker);
 
     return r;
 }
target/i386/nvmm/nvmm-all.c +1 -2
index b75738e..f66b140 100644
@@ -935,9 +935,8 @@ nvmm_init_vcpu(CPUState *cpu)
         error_setg(&nvmm_migration_blocker,
             "NVMM: Migration not supported");
 
-        if (migrate_add_blocker(nvmm_migration_blocker, &local_error) < 0) {
+        if (migrate_add_blocker(&nvmm_migration_blocker, &local_error) < 0) {
             error_report_err(local_error);
-            error_free(nvmm_migration_blocker);
             return -EINVAL;
         }
     }
target/i386/sev.c +1 -1
index 32f7dba..927bd75 100644
@@ -891,7 +891,7 @@ sev_launch_finish(SevGuestState *sev)
     /* add migration blocker */
     error_setg(&sev_mig_blocker,
                "SEV: Migration is not implemented");
-    migrate_add_blocker(sev_mig_blocker, &error_fatal);
+    migrate_add_blocker(&sev_mig_blocker, &error_fatal);
 }
 
 static void
target/i386/whpx/whpx-all.c +1 -2
index b22a331..004a80e 100644
@@ -2172,9 +2172,8 @@ int whpx_init_vcpu(CPUState *cpu)
                "State blocked due to non-migratable CPUID feature support,"
                "dirty memory tracking support, and XSAVE/XRSTOR support");
 
-        if (migrate_add_blocker(whpx_migration_blocker, &local_error) < 0) {
+        if (migrate_add_blocker(&whpx_migration_blocker, &local_error) < 0) {
             error_report_err(local_error);
-            error_free(whpx_migration_blocker);
             ret = -EINVAL;
             goto error;
         }
ui/vdagent.c +2 -3
index aa6167f..e6d1cfd 100644
@@ -672,7 +672,7 @@ static void vdagent_chr_open(Chardev *chr,
     return;
 #endif
 
-    if (migrate_add_blocker(vd->migration_blocker, errp) != 0) {
+    if (migrate_add_blocker(&vd->migration_blocker, errp) != 0) {
         return;
     }
 
@@ -915,10 +915,9 @@ static void vdagent_chr_fini(Object *obj)
 {
     VDAgentChardev *vd = QEMU_VDAGENT_CHARDEV(obj);
 
-    migrate_del_blocker(vd->migration_blocker);
+    migrate_del_blocker(&vd->migration_blocker);
     vdagent_disconnect(vd);
     buffer_free(&vd->outbuf);
-    error_free(vd->migration_blocker);
 }
 
 static const TypeInfo vdagent_chr_type_info = {
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH v9 7/46 migration: per-mode blockers

In reply to Steve Sistare

Extend the blocker interface so that a blocker can be registered for one or more migration modes. The existing interfaces register a blocker for all modes, and the new interfaces take a varargs list of modes.

Internally, maintain a separate blocker list per mode. The same Error object may be added to multiple lists. When a block is deleted, it is removed from every list, and the Error is freed.

No functional change until a new mode is added.

---
 include/migration/blocker.h | 45 ++++++++++++++++++++---
 migration/migration.c       | 87 +++++++++++++++++++++++++++++++++++++++------
 stubs/migr-blocker.c        |  5 +++
 3 files changed, 123 insertions(+), 14 deletions(-)
include/migration/blocker.h +41 -4
index 1483f5d..1a21937 100644
@@ -14,8 +14,12 @@
 #ifndef MIGRATION_BLOCKER_H
 #define MIGRATION_BLOCKER_H
 
+#include "qapi/qapi-types-migration.h"
+
+#define MIG_MODE_ALL MIG_MODE__MAX
+
 /**
- * @migrate_add_blocker - prevent migration from proceeding
+ * @migrate_add_blocker - prevent all modes of migration from proceeding
  *
  * @reasonp - address of an error to be returned whenever migration is attempted
  *
@@ -29,8 +33,41 @@
 int migrate_add_blocker(Error **reasonp, Error **errp);
 
 /**
+ * @migrate_add_blockers - prevent migration for specified modes from proceeding
+ *
+ * @reasonp - address of an error to be returned whenever migration is attempted
+ *
+ * @errp - [out] The reason (if any) we cannot block migration right now.
+ *
+ * @mode - one or more migration modes to be blocked.  The list is terminated
+ *         by -1 or MIG_MODE_ALL.  For the latter, all modes are blocked.
+ *
+ * @returns - 0 on success, -EBUSY/-EACCES on failure, with errp set.
+ *
+ * *@reasonp is freed and set to NULL if failure is returned.
+ * On success, the caller must not free *@reasonp before the blocker is removed.
+ */
+int migrate_add_blockers(Error **reasonp, Error **errp, MigMode mode, ...);
+
+/**
+ * @migrate_add_blocker_always - permanently prevent migration for specified
+ *  modes from proceeding.  The blocker cannot be deleted.
+ *
+ * @msg - text of error to be returned whenever migration is attempted
+ *
+ * @errp - [out] The reason (if any) we cannot block migration right now.
+ *
+ * @mode - one or more migration modes to be blocked.  The list is terminated
+ *         by -1 or MIG_MODE_ALL.  For the latter, all modes are blocked.
+ *
+ * @returns - 0 on success, -EBUSY/-EACCES on failure, with errp set.
+ */
+int
+migrate_add_blocker_always(const char *msg, Error **errp, MigMode mode, ...);
+
+/**
  * @migrate_add_blocker_internal - prevent migration from proceeding without
- *                                 only-migrate implications
+ *                                 only-migrate implications, for all modes
  *
  * @reasonp - address of an error to be returned whenever migration is attempted
  *
@@ -48,7 +85,7 @@ int migrate_add_blocker(Error **reasonp, Error **errp);
 int migrate_add_blocker_internal(Error **reasonp, Error **errp);
 
 /**
- * @migrate_del_blocker - remove a blocking error from migration and free it.
+ * @migrate_del_blocker - remove a migration blocker for all modes and free it.
  *
  * @reasonp - address of the error blocking migration
  *
@@ -57,7 +94,7 @@ int migrate_add_blocker_internal(Error **reasonp, Error **errp);
 void migrate_del_blocker(Error **reasonp);
 
 /**
- * @migrate_remove_blocker - remove a migration blocker.
+ * @migrate_remove_blocker - remove a migration blocker for all modes.
  *
  * @reason - the error blocking migration
  *
migration/migration.c +77 -10

138 lines not on this page · raw message

stubs/migr-blocker.c +5 -0
index 17a5dbf..60769d8 100644
@@ -6,6 +6,11 @@ int migrate_add_blocker(Error **reasonp, Error **errp)
     return 0;
 }
 
+int migrate_add_blockers(Error **reasonp, Error **errp, MigMode mode, ...)
+{
+    return 0;
+}
+
 void migrate_del_blocker(Error **reasonp)
 {
 }
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH v9 8/46 cpr: relax some blockers

In reply to Steve Sistare

Some devices block migration because they rely on local state that is not migrated to the target host, such as for local filesystems. These need not block cpr, which will restart qemu on the same host. Narrow the scope of these blockers so they only apply to normal mode. They will not block cpr modes when they are added in subsequent patches.

No functional change until a new mode is added.

---
 backends/tpm/tpm_emulator.c | 3 ++-
 block/parallels.c           | 3 ++-
 block/qcow.c                | 3 ++-
 block/vdi.c                 | 3 ++-
 block/vhdx.c                | 3 ++-
 block/vmdk.c                | 3 ++-
 block/vpc.c                 | 3 ++-
 block/vvfat.c               | 3 ++-
 hw/9pfs/9p.c                | 3 ++-
 hw/scsi/vhost-scsi.c        | 3 ++-
 hw/virtio/vhost.c           | 3 ++-
 target/i386/nvmm/nvmm-all.c | 3 ++-
 12 files changed, 24 insertions(+), 12 deletions(-)
backends/tpm/tpm_emulator.c +2 -1
index ed67633..18cac1b 100644
@@ -492,7 +492,8 @@ static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
         error_setg(&tpm_emu->migration_blocker,
                    "Migration disabled: TPM emulator does not support "
                    "migration");
-        if (migrate_add_blocker(&tpm_emu->migration_blocker, &err) < 0) {
+        if (migrate_add_blockers(&tpm_emu->migration_blocker, &err,
+                                 MIG_MODE_NORMAL, -1) < 0) {
             error_report_err(err);
             return -1;
         }
block/parallels.c +2 -1
index 25d24d6..0837c32 100644
@@ -878,7 +878,8 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The Parallels format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(&s->migration_blocker, errp);
+    ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                               -1);
     if (ret < 0) {
         goto fail;
     }
block/qcow.c +2 -1
index 3b4e6a4..4057438 100644
@@ -305,7 +305,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(&s->migration_blocker, errp);
+    ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                               -1);
     if (ret < 0) {
         goto fail;
     }
block/vdi.c +2 -1
index cead854..022dab5 100644
@@ -495,7 +495,8 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(&s->migration_blocker, errp);
+    ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                               -1);
     if (ret < 0) {
         goto fail_free_bmap;
     }
block/vhdx.c +2 -1
index 07b6077..8564777 100644
@@ -1093,7 +1093,8 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(&s->migration_blocker, errp);
+    ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                              -1);
     if (ret < 0) {
         goto fail;
     }
block/vmdk.c +2 -1
index 7a7cb57..043b895 100644
@@ -1360,7 +1360,8 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(&s->migration_blocker, errp);
+    ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                               -1);
     if (ret < 0) {
         goto fail;
     }
block/vpc.c +2 -1
index a465784..6a25e2d 100644
@@ -450,7 +450,8 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
     error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
                "does not support live migration",
                bdrv_get_device_or_node_name(bs));
-    ret = migrate_add_blocker(&s->migration_blocker, errp);
+    ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                               -1);
     if (ret < 0) {
         goto fail;
     }
block/vvfat.c +2 -1
index bc96876..45f3280 100644
@@ -1265,7 +1265,8 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
                    "The vvfat (rw) format used by node '%s' "
                    "does not support live migration",
                    bdrv_get_device_or_node_name(bs));
-        ret = migrate_add_blocker(&s->migration_blocker, errp);
+        ret = migrate_add_blockers(&s->migration_blocker, errp, MIG_MODE_NORMAL,
+                                   -1);
         if (ret < 0) {
             goto fail;
         }
hw/9pfs/9p.c +2 -1
index d5ac01f..3a48f32 100644
@@ -1487,7 +1487,8 @@ static void coroutine_fn v9fs_attach(void *opaque)
         error_setg(&s->migration_blocker,
                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
-        err = migrate_add_blocker(&s->migration_blocker, NULL);
+        err = migrate_add_blockers(&s->migration_blocker, NULL, MIG_MODE_NORMAL,
+                                   -1);
         if (err < 0) {
             clunk_fid(s, fid);
             goto out;
hw/scsi/vhost-scsi.c +2 -1
index b5e34f2..4a9d711 100644
@@ -208,7 +208,8 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
                 "When external environment supports it (Orchestrator migrates "
                 "target SCSI device state or use shared storage over network), "
                 "set 'migratable' property to true to enable migration.");
-        if (migrate_add_blocker(&vsc->migration_blocker, errp) < 0) {
+        if (migrate_add_blockers(&vsc->migration_blocker, errp, MIG_MODE_NORMAL,
+                                 -1) < 0) {
             goto free_virtio;
         }
     }
hw/virtio/vhost.c +2 -1
index ae52b62..8d2a136 100644
@@ -1431,7 +1431,8 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     }
 
     if (hdev->migration_blocker != NULL) {
-        r = migrate_add_blocker(&hdev->migration_blocker, errp);
+        r = migrate_add_blockers(&hdev->migration_blocker, errp,
+                                 MIG_MODE_NORMAL, -1);
         if (r < 0) {
             goto fail_busyloop;
         }
target/i386/nvmm/nvmm-all.c +2 -1
index f66b140..e102f64 100644
@@ -935,7 +935,8 @@ nvmm_init_vcpu(CPUState *cpu)
         error_setg(&nvmm_migration_blocker,
             "NVMM: Migration not supported");
 
-        if (migrate_add_blocker(&nvmm_migration_blocker, &local_error) < 0) {
+        if (migrate_add_blockers(&nvmm_migration_blocker, &local_error,
+                                 MIG_MODE_NORMAL, -1) < 0) {
             error_report_err(local_error);
             return -EINVAL;
         }
Signature · 1 line
1.8.3.1
#9 lore raw

PATCH v9 9/46 cpr: reboot mode

In reply to Steve Sistare

Add the cpr-reboot migration mode. Usage:
qemu-system-$arch -migrate-mode-enable cpr-reboot ... migrate_set_parameter mode cpr-reboot migrate -d file:<filename>
... poll for runstate postmigrate ...
quit

qemu-system-$arch -migrate-mode-enable cpr-reboot ... -incoming defer migrate_set_parameter mode cpr-reboot
migrate_incoming file:<filename>

In this mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, and restart an updated version of qemu. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, such as by backing it with a dax device, but this is not enforced. The restarted qemu arguments must match those used to initially start qemu, plus the -incoming option.

---
 qapi/migration.json | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
qapi/migration.json +15 -1
index 931c492..55f0479 100644
@@ -572,10 +572,24 @@
 #
 # @normal: the original form of migration.
 #
+# @cpr-reboot: The migrate command saves state to a file, allowing one to
+#              quit qemu, reboot to an updated kernel, and restart an updated
+#              version of qemu.  The caller must specify a migration URI
+#              that writes to and reads from a file.  Unlike normal mode,
+#              the use of certain local storage options does not block the
+#              migration, but the caller must not modify guest block devices
+#              between the quit and restart.  The guest RAM memory-backend
+#              must be shared, and the @x-ignore-shared migration capability
+#              must be set, to avoid saving it to the file.  Guest RAM must
+#              be non-volatile across reboot, such as by backing it with
+#              a dax device, but this is not enforced.  The restarted qemu
+#              arguments must match those used to initially start qemu, plus
+#              the -incoming option.
+#
 # Since: 7.1
 ##
 { 'enum': 'MigMode',
-  'data': [ 'normal' ] }
+  'data': [ 'normal', 'cpr-reboot' ] }
 
 ##
 # @BitmapMigrationBitmapAliasTransform:
Signature · 1 line
1.8.3.1
#10 lore raw

PATCH v9 10/46 qdev-properties: strList

In reply to Steve Sistare

Define a list-of-strings property, to be used for the cpr-exec-args migration property in a subsequent patch.

---
 hw/core/qdev-properties.c    | 44 ++++++++++++++++++++++++++++++++++++++++++++
 include/hw/qdev-properties.h |  3 +++
 2 files changed, 47 insertions(+)
hw/core/qdev-properties.c +44 -0
index 357b876..851f490 100644
@@ -9,6 +9,7 @@
 #include "qemu/units.h"
 #include "qemu/cutils.h"
 #include "qdev-prop-internal.h"
+#include "qapi/qapi-builtin-visit.h"
 
 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
                                   Error **errp)
@@ -490,6 +491,49 @@ const PropertyInfo qdev_prop_string = {
     .set   = set_string,
 };
 
+/* --- strList --- */
+
+static void release_strList(Object *obj, const char *name, void *opaque)
+{
+    Property *prop = opaque;
+    g_free(*(char **)object_field_prop_ptr(obj, prop));
+}
+
+static void get_strList(Object *obj, Visitor *v, const char *name,
+                       void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    strList **ptr = object_field_prop_ptr(obj, prop);
+
+    if (!*ptr) {
+        strList *str = NULL;
+        visit_type_strList(v, name, &str, errp);
+    } else {
+        visit_type_strList(v, name, ptr, errp);
+    }
+}
+
+static void set_strList(Object *obj, Visitor *v, const char *name,
+                       void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    strList **ptr = object_field_prop_ptr(obj, prop);
+    strList *str;
+
+    if (!visit_type_strList(v, name, &str, errp)) {
+        return;
+    }
+    g_free(*ptr);
+    *ptr = str;
+}
+
+const PropertyInfo qdev_prop_strlist = {
+    .name  = "strList",
+    .release = release_strList,
+    .get   = get_strList,
+    .set   = set_strList,
+};
+
 /* --- on/off/auto --- */
 
 const PropertyInfo qdev_prop_on_off_auto = {
include/hw/qdev-properties.h +3 -0
index e1df088..df1b869 100644
@@ -59,6 +59,7 @@ extern const PropertyInfo qdev_prop_uint64_checkmask;
 extern const PropertyInfo qdev_prop_int64;
 extern const PropertyInfo qdev_prop_size;
 extern const PropertyInfo qdev_prop_string;
+extern const PropertyInfo qdev_prop_strlist;
 extern const PropertyInfo qdev_prop_on_off_auto;
 extern const PropertyInfo qdev_prop_size32;
 extern const PropertyInfo qdev_prop_arraylen;
@@ -171,6 +172,8 @@ extern const PropertyInfo qdev_prop_link;
     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
 #define DEFINE_PROP_STRING(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
+#define DEFINE_PROP_STRLIST(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_strlist, strList*)
 #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
 #define DEFINE_PROP_SIZE32(_n, _s, _f, _d)                       \
Signature · 1 line
1.8.3.1
#11 lore raw

Re: [PATCH V9 10/46] qdev-properties: strList

Hi Paolo, Daniel, Eduardo,
Could one of you review this patch which is in your area? It defines
DEFINE_PROP_STRLIST, which is added to migration_properties[] in patch 15 of this series:
https://lore.kernel.org/qemu-devel/[email protected]/

For cpr, the user passes a strlist of args for exec'ing new qemu, as a new 'cpr-exec-args' parameter of the migrate-set-parameters command.

Thanks!

- Steve

Quoted 92 lines · Steve Sistare

Define a list-of-strings property, to be used for the cpr-exec-args migration property in a subsequent patch.

---
 hw/core/qdev-properties.c    | 44 ++++++++++++++++++++++++++++++++++++++++++++
 include/hw/qdev-properties.h |  3 +++
 2 files changed, 47 insertions(+)
hw/core/qdev-properties.c +44 -0
index 357b876..851f490 100644
@@ -9,6 +9,7 @@
 #include "qemu/units.h"
 #include "qemu/cutils.h"
 #include "qdev-prop-internal.h"
+#include "qapi/qapi-builtin-visit.h"
 
 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
                                   Error **errp)
@@ -490,6 +491,49 @@ const PropertyInfo qdev_prop_string = {
     .set   = set_string,
 };
 
+/* --- strList --- */
+
+static void release_strList(Object *obj, const char *name, void *opaque)
+{
+    Property *prop = opaque;
+    g_free(*(char **)object_field_prop_ptr(obj, prop));
+}
+
+static void get_strList(Object *obj, Visitor *v, const char *name,
+                       void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    strList **ptr = object_field_prop_ptr(obj, prop);
+
+    if (!*ptr) {
+        strList *str = NULL;
+        visit_type_strList(v, name, &str, errp);
+    } else {
+        visit_type_strList(v, name, ptr, errp);
+    }
+}
+
+static void set_strList(Object *obj, Visitor *v, const char *name,
+                       void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    strList **ptr = object_field_prop_ptr(obj, prop);
+    strList *str;
+
+    if (!visit_type_strList(v, name, &str, errp)) {
+        return;
+    }
+    g_free(*ptr);
+    *ptr = str;
+}
+
+const PropertyInfo qdev_prop_strlist = {
+    .name  = "strList",
+    .release = release_strList,
+    .get   = get_strList,
+    .set   = set_strList,
+};
+
 /* --- on/off/auto --- */
 
 const PropertyInfo qdev_prop_on_off_auto = {
include/hw/qdev-properties.h +3 -0
index e1df088..df1b869 100644
@@ -59,6 +59,7 @@ extern const PropertyInfo qdev_prop_uint64_checkmask;
 extern const PropertyInfo qdev_prop_int64;
 extern const PropertyInfo qdev_prop_size;
 extern const PropertyInfo qdev_prop_string;
+extern const PropertyInfo qdev_prop_strlist;
 extern const PropertyInfo qdev_prop_on_off_auto;
 extern const PropertyInfo qdev_prop_size32;
 extern const PropertyInfo qdev_prop_arraylen;
@@ -171,6 +172,8 @@ extern const PropertyInfo qdev_prop_link;
     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
 #define DEFINE_PROP_STRING(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
+#define DEFINE_PROP_STRLIST(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_strlist, strList*)
 #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
 #define DEFINE_PROP_SIZE32(_n, _s, _f, _d)                       \
#12 lore raw

PATCH v9 11/46 qapi: strList_from_string

In reply to Steve Sistare

Generalize strList_from_comma_list() to take any delimiter character, rename as strList_from_string(), and move it to qapi/util.c.

No functional change.

---
 include/qapi/util.h |  9 +++++++++
 monitor/hmp-cmds.c  | 31 +++----------------------------
 qapi/qapi-util.c    | 23 +++++++++++++++++++++++
 3 files changed, 35 insertions(+), 28 deletions(-)
include/qapi/util.h +9 -0
index 81a2b13..7d88b09 100644
@@ -22,6 +22,8 @@ typedef struct QEnumLookup {
     const int size;
 } QEnumLookup;
 
+struct strList;
+
 const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
 int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
                     int def, Error **errp);
@@ -31,6 +33,13 @@ bool qapi_bool_parse(const char *name, const char *value, bool *obj,
 int parse_qapi_name(const char *name, bool complete);
 
 /*
+ * Produce a strList from the character delimited string @in.
+ * All strings are g_strdup'd.
+ * A NULL or empty input string returns NULL.
+ */
+struct strList *strList_from_string(const char *in, char delim);
+
+/*
  * For any GenericList @list, insert @element at the front.
  *
  * Note that this macro evaluates @element exactly once, so it is safe
monitor/hmp-cmds.c +3 -28
index 6632ada..a394526 100644
@@ -43,6 +43,7 @@
 #include "qapi/qapi-commands-stats.h"
 #include "qapi/qapi-commands-tpm.h"
 #include "qapi/qapi-commands-ui.h"
+#include "qapi/util.h"
 #include "qapi/qapi-visit-net.h"
 #include "qapi/qapi-visit-migration.h"
 #include "qapi/qmp/qdict.h"
@@ -71,32 +72,6 @@ bool hmp_handle_error(Monitor *mon, Error *err)
     return false;
 }
 
-/*
- * Produce a strList from a comma separated list.
- * A NULL or empty input string return NULL.
- */
-static strList *strList_from_comma_list(const char *in)
-{
-    strList *res = NULL;
-    strList **tail = &res;
-
-    while (in && in[0]) {
-        char *comma = strchr(in, ',');
-        char *value;
-
-        if (comma) {
-            value = g_strndup(in, comma - in);
-            in = comma + 1; /* skip the , */
-        } else {
-            value = g_strdup(in);
-            in = NULL;
-        }
-        QAPI_LIST_APPEND(tail, value);
-    }
-
-    return res;
-}
-
 void hmp_info_name(Monitor *mon, const QDict *qdict)
 {
     NameInfo *info;
@@ -1125,7 +1100,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict)
                                             migrate_announce_params());
 
     qapi_free_strList(params->interfaces);
-    params->interfaces = strList_from_comma_list(interfaces_str);
+    params->interfaces = strList_from_string(interfaces_str, ',');
     params->has_interfaces = params->interfaces != NULL;
     params->id = g_strdup(id);
     params->has_id = !!params->id;
@@ -2409,7 +2384,7 @@ static StatsFilter *stats_filter(StatsTarget target, const char *names,
             request->provider = provider_idx;
             if (names && !g_str_equal(names, "*")) {
                 request->has_names = true;
-                request->names = strList_from_comma_list(names);
+                request->names = strList_from_string(names, ',');
             }
             QAPI_LIST_PREPEND(request_list, request);
         }
qapi/qapi-util.c +23 -0
index 63596e1..b61c73c 100644
@@ -15,6 +15,7 @@
 #include "qapi/error.h"
 #include "qemu/ctype.h"
 #include "qapi/qmp/qerror.h"
+#include "qapi/qapi-builtin-types.h"
 
 CompatPolicy compat_policy;
 
@@ -152,3 +153,25 @@ int parse_qapi_name(const char *str, bool complete)
     }
     return p - str;
 }
+
+strList *strList_from_string(const char *in, char delim)
+{
+    strList *res = NULL;
+    strList **tail = &res;
+
+    while (in && in[0]) {
+        char *next = strchr(in, delim);
+        char *value;
+
+        if (next) {
+            value = g_strndup(in, next - in);
+            in = next + 1; /* skip the delim */
+        } else {
+            value = g_strdup(in);
+            in = NULL;
+        }
+        QAPI_LIST_APPEND(tail, value);
+    }
+
+    return res;
+}
Signature · 1 line
1.8.3.1
#13 lore raw

PATCH v9 12/46 qapi: QAPI_LIST_LENGTH

In reply to Steve Sistare
---
 include/qapi/util.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/qapi/util.h +13 -0
index 7d88b09..75dddca 100644
@@ -65,4 +65,17 @@ struct strList *strList_from_string(const char *in, char delim);
     (tail) = &(*(tail))->next; \
 } while (0)
 
+/*
+ * For any GenericList @list, return its length.
+ */
+#define QAPI_LIST_LENGTH(list) \
+    ({ \
+        int len = 0; \
+        typeof(list) elem; \
+        for (elem = list; elem != NULL; elem = elem->next) { \
+            len++; \
+        } \
+        len; \
+    })
+
 #endif
Signature · 1 line
1.8.3.1
#14 lore raw

PATCH v9 13/46 qapi: strv_from_strList

In reply to Steve Sistare
---
 include/qapi/util.h |  6 ++++++
 qapi/qapi-util.c    | 14 ++++++++++++++
 2 files changed, 20 insertions(+)
include/qapi/util.h +6 -0
index 75dddca..51ff64e 100644
@@ -33,6 +33,12 @@ bool qapi_bool_parse(const char *name, const char *value, bool *obj,
 int parse_qapi_name(const char *name, bool complete);
 
 /*
+ * Produce and return a NULL-terminated array of strings from @args.
+ * All strings are g_strdup'd.
+ */
+GStrv strv_from_strList(const struct strList *args);
+
+/*
  * Produce a strList from the character delimited string @in.
  * All strings are g_strdup'd.
  * A NULL or empty input string returns NULL.
qapi/qapi-util.c +14 -0
index b61c73c..fe6bda2 100644
@@ -154,6 +154,20 @@ int parse_qapi_name(const char *str, bool complete)
     return p - str;
 }
 
+GStrv strv_from_strList(const strList *args)
+{
+    const strList *arg;
+    int i = 0;
+    GStrv argv = g_new(char *, QAPI_LIST_LENGTH(args) + 1);
+
+    for (arg = args; arg != NULL; arg = arg->next) {
+        argv[i++] = g_strdup(arg->value);
+    }
+    argv[i] = NULL;
+
+    return argv;
+}
+
 strList *strList_from_string(const char *in, char delim)
 {
     strList *res = NULL;
Signature · 1 line
1.8.3.1
#15 lore raw

PATCH v9 14/46 qapi: strList unit tests

In reply to Steve Sistare
---
 MAINTAINERS               |  6 ++++
 tests/unit/meson.build    |  1 +
 tests/unit/test-strlist.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 tests/unit/test-strlist.c
MAINTAINERS +6 -0
index 6af9cd9..362cfde 100644
@@ -3160,6 +3160,12 @@ F: net/filter-rewriter.c
 F: net/filter-mirror.c
 F: tests/qtest/test-filter*
 
+CPR
+M: Steve Sistare <[email protected]>
+R: Mark Kanda <[email protected]>
+S: Maintained
+F: tests/unit/test-strlist.c
+
 Record/replay
 M: Pavel Dovgalyuk <[email protected]>
 R: Paolo Bonzini <[email protected]>
tests/unit/meson.build +1 -0
index b497a41..1030e61 100644
@@ -17,6 +17,7 @@ tests = {
   'test-forward-visitor': [testqapi],
   'test-string-input-visitor': [testqapi],
   'test-string-output-visitor': [testqapi],
+  'test-strlist': [testqapi],
   'test-opts-visitor': [testqapi],
   'test-visitor-serialization': [testqapi],
   'test-bitmap': [],
tests/unit/test-strlist.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#16 lore raw

PATCH v9 15/46 migration: cpr-exec-args parameter

In reply to Steve Sistare

Create the cpr-exec-args migration parameter, defined as a list of strings. It will be used for cpr-exec migration mode in a subsequent patch.

No functional change, except that cpr-exec-args is shown by the 'info migrate' command.

---
 hmp-commands.hx       |  2 +-
 migration/migration.c | 15 +++++++++++++++
 monitor/hmp-cmds.c    | 20 ++++++++++++++++++++
 qapi/migration.json   |  9 +++++++++
 4 files changed, 45 insertions(+), 1 deletion(-)
hmp-commands.hx +1 -1
index 182e639..6f5154b 100644
@@ -1006,7 +1006,7 @@ ERST
 
     {
         .name       = "migrate_set_parameter",
-        .args_type  = "parameter:s,value:s",
+        .args_type  = "parameter:s,value:S",
         .params     = "parameter value",
         .help       = "Set the parameter for migration",
         .cmd        = hmp_migrate_set_parameter,
migration/migration.c +15 -0
index 0e62227..80b7d09 100644
@@ -923,6 +923,8 @@ MigrationParameters *qmp_query_migrate_parameters(Error **errp)
     params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
     params->has_cpu_throttle_tailslow = true;
     params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
+    params->has_cpr_exec_args = true;
+    params->cpr_exec_args = QAPI_CLONE(strList, s->parameters.cpr_exec_args);
     params->has_tls_creds = true;
     params->tls_creds = g_strdup(s->parameters.tls_creds);
     params->has_tls_hostname = true;
@@ -1615,6 +1617,10 @@ static void migrate_params_test_apply(MigrateSetParameters *params,
         dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
     }
 
+    if (params->has_cpr_exec_args) {
+        dest->cpr_exec_args = params->cpr_exec_args;
+    }
+
     if (params->has_tls_creds) {
         assert(params->tls_creds->type == QTYPE_QSTRING);
         dest->tls_creds = params->tls_creds->u.s;
@@ -1716,6 +1722,12 @@ static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
         s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
     }
 
+    if (params->has_cpr_exec_args) {
+        qapi_free_strList(s->parameters.cpr_exec_args);
+        s->parameters.cpr_exec_args =
+            QAPI_CLONE(strList, params->cpr_exec_args);
+    }
+
     if (params->has_tls_creds) {
         g_free(s->parameters.tls_creds);
         assert(params->tls_creds->type == QTYPE_QSTRING);
@@ -4481,6 +4493,8 @@ static Property migration_properties[] = {
                       DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
     DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
                       parameters.cpu_throttle_tailslow, false),
+    DEFINE_PROP_STRLIST("cpr-exec-args", MigrationState,
+                      parameters.cpr_exec_args),
     DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
                       parameters.max_bandwidth, MAX_THROTTLE),
     DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
@@ -4597,6 +4611,7 @@ static void migration_instance_init(Object *obj)
     params->has_compress_threads = true;
     params->has_decompress_threads = true;
     params->has_throttle_trigger_threshold = true;
+    params->has_cpr_exec_args = true;
     params->has_cpu_throttle_initial = true;
     params->has_cpu_throttle_increment = true;
     params->has_cpu_throttle_tailslow = true;
monitor/hmp-cmds.c +20 -0
index a394526..8794459 100644
@@ -387,6 +387,18 @@ void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
     qapi_free_MigrationCapabilityStatusList(caps);
 }
 
+static void monitor_print_cpr_exec_args(Monitor *mon, strList *args)
+{
+    monitor_printf(mon, "%s:",
+        MigrationParameter_str(MIGRATION_PARAMETER_CPR_EXEC_ARGS));
+
+    while (args) {
+        monitor_printf(mon, " %s", args->value);
+        args = args->next;
+    }
+    monitor_printf(mon, "\n");
+}
+
 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
 {
     MigrationParameters *params;
@@ -446,6 +458,8 @@ void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "%s: %u\n",
             MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
             params->max_cpu_throttle);
+        assert(params->has_cpr_exec_args);
+        monitor_print_cpr_exec_args(mon, params->cpr_exec_args);
         assert(params->has_tls_creds);
         monitor_printf(mon, "%s: '%s'\n",
             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
@@ -1190,6 +1204,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
     uint64_t valuebw = 0;
     uint64_t cache_size;
     Error *err = NULL;
+    g_autofree char *str = NULL;
     int val, ret;
 
     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
@@ -1238,6 +1253,11 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
         p->has_max_cpu_throttle = true;
         visit_type_uint8(v, param, &p->max_cpu_throttle, &err);
         break;
+    case MIGRATION_PARAMETER_CPR_EXEC_ARGS:
+        p->has_cpr_exec_args = true;
+        visit_type_str(v, param, &str, &err);
+        p->cpr_exec_args = strList_from_string(str, ' ');
+        break;
     case MIGRATION_PARAMETER_TLS_CREDS:
         p->has_tls_creds = true;
         p->tls_creds = g_new0(StrOrNull, 1);
qapi/migration.json +9 -0
index 55f0479..839fcd4 100644
@@ -712,6 +712,8 @@
 #                         at tail stage.
 #                         The default value is false. (Since 5.1)
 #
+# @cpr-exec-args: defined in a subsequent patch.
+#
 # @tls-creds: ID of the 'tls-creds' object that provides credentials for
 #             establishing a TLS connection over the migration data channel.
 #             On the outgoing side of the migration, the credentials must
@@ -816,6 +818,7 @@
            'compress-wait-thread', 'throttle-trigger-threshold',
            'cpu-throttle-initial', 'cpu-throttle-increment',
            'cpu-throttle-tailslow',
+           'cpr-exec-args',
            'tls-creds', 'tls-hostname', 'tls-authz', 'max-bandwidth',
            'downtime-limit',
            { 'name': 'x-checkpoint-delay', 'features': [ 'unstable' ] },
@@ -882,6 +885,8 @@
 #                         at tail stage.
 #                         The default value is false. (Since 5.1)
 #
+# @cpr-exec-args: defined in a subsequent patch.
+#
 # @tls-creds: ID of the 'tls-creds' object that provides credentials
 #             for establishing a TLS connection over the migration data
 #             channel. On the outgoing side of the migration, the credentials
@@ -993,6 +998,7 @@
             '*cpu-throttle-initial': 'uint8',
             '*cpu-throttle-increment': 'uint8',
             '*cpu-throttle-tailslow': 'bool',
+            '*cpr-exec-args': [ 'str' ],
             '*tls-creds': 'StrOrNull',
             '*tls-hostname': 'StrOrNull',
             '*tls-authz': 'StrOrNull',
@@ -1084,6 +1090,8 @@
 #                         at tail stage.
 #                         The default value is false. (Since 5.1)
 #
+# @cpr-exec-args: defined in a subsequent patch.
+#
 # @tls-creds: ID of the 'tls-creds' object that provides credentials
 #             for establishing a TLS connection over the migration data
 #             channel. On the outgoing side of the migration, the credentials
@@ -1195,6 +1203,7 @@
             '*cpu-throttle-initial': 'uint8',
             '*cpu-throttle-increment': 'uint8',
             '*cpu-throttle-tailslow': 'bool',
+            '*cpr-exec-args': [ 'str' ],
             '*tls-creds': 'str',
             '*tls-hostname': 'str',
             '*tls-authz': 'str',
Signature · 1 line
1.8.3.1
#17 lore raw

PATCH v9 16/46 migration: simplify notifiers

In reply to Steve Sistare

Pass the callback function to add_migration_state_change_notifier so that migration can initialize the notifier on add and clear it on delete, which simplifies the call sites. Also shorten the function names.

No functional change.

---
 hw/net/virtio-net.c      |  6 +++---
 hw/vfio/migration.c      |  6 +++---
 include/migration/misc.h |  6 ++++--
 migration/migration.c    | 22 ++++++++++++++++------
 ui/spice-core.c          |  3 +--
 5 files changed, 27 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +3 -3
index dd0d056..29af1a5 100644
@@ -3443,8 +3443,8 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
         n->primary_listener.hide_device = failover_hide_primary_device;
         qatomic_set(&n->failover_primary_hidden, true);
         device_listener_register(&n->primary_listener);
-        n->migration_state.notify = virtio_net_migration_state_notifier;
-        add_migration_state_change_notifier(&n->migration_state);
+        migration_add_notifier(&n->migration_state,
+                               virtio_net_migration_state_notifier);
         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
     }
 
@@ -3607,7 +3607,7 @@ static void virtio_net_device_unrealize(DeviceState *dev)
     if (n->failover) {
         qobject_unref(n->primary_opts);
         device_listener_unregister(&n->primary_listener);
-        remove_migration_state_change_notifier(&n->migration_state);
+        migration_remove_notifier(&n->migration_state);
     } else {
         assert(n->primary_opts == NULL);
     }
hw/vfio/migration.c +3 -3
index d6f9e29..5109124 100644
@@ -839,8 +839,8 @@ static int vfio_migration_init(VFIODevice *vbasedev,
     migration->vm_state = qdev_add_vm_change_state_handler(vbasedev->dev,
                                                            vfio_vmstate_change,
                                                            vbasedev);
-    migration->migration_state.notify = vfio_migration_state_notifier;
-    add_migration_state_change_notifier(&migration->migration_state);
+    migration_add_notifier(&migration->migration_state,
+                           vfio_migration_state_notifier);
     return 0;
 
 err:
@@ -895,7 +895,7 @@ void vfio_migration_finalize(VFIODevice *vbasedev)
     if (vbasedev->migration) {
         VFIOMigration *migration = vbasedev->migration;
 
-        remove_migration_state_change_notifier(&migration->migration_state);
+        migration_remove_notifier(&migration->migration_state);
         qemu_del_vm_change_state_handler(migration->vm_state);
         unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
         vfio_migration_exit(vbasedev);
include/migration/misc.h +4 -2
index 71b6285..d4c2d7d 100644
@@ -60,8 +60,10 @@ void migration_object_init(void);
 void migration_shutdown(void);
 bool migration_is_idle(void);
 bool migration_is_active(MigrationState *);
-void add_migration_state_change_notifier(Notifier *notify);
-void remove_migration_state_change_notifier(Notifier *notify);
+void migration_add_notifier(Notifier *notify,
+                            void (*func)(Notifier *notifier, void *data));
+void migration_remove_notifier(Notifier *notify);
+void migration_call_notifiers(MigrationState *s);
 bool migration_in_setup(MigrationState *);
 bool migration_has_finished(MigrationState *);
 bool migration_has_failed(MigrationState *);
migration/migration.c +16 -6
index 80b7d09..d7c6902 100644
@@ -1962,7 +1962,7 @@ static void migrate_fd_cleanup(MigrationState *s)
         /* It is used on info migrate.  We can't free it */
         error_report_err(error_copy(s->error));
     }
-    notifier_list_notify(&migration_state_notifiers, s);
+    migration_call_notifiers(s);
     block_cleanup_parameters(s);
     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
 }
@@ -2057,14 +2057,24 @@ static void migrate_fd_cancel(MigrationState *s)
     }
 }
 
-void add_migration_state_change_notifier(Notifier *notify)
+void migration_add_notifier(Notifier *notify,
+                            void (*func)(Notifier *notifier, void *data))
 {
+    notify->notify = func;
     notifier_list_add(&migration_state_notifiers, notify);
 }
 
-void remove_migration_state_change_notifier(Notifier *notify)
+void migration_remove_notifier(Notifier *notify)
+{
+    if (notify->notify) {
+        notifier_remove(notify);
+        notify->notify = NULL;
+    }
+}
+
+void migration_call_notifiers(MigrationState *s)
 {
-    notifier_remove(notify);
+    notifier_list_notify(&migration_state_notifiers, s);
 }
 
 bool migration_in_setup(MigrationState *s)
@@ -3341,7 +3351,7 @@ static int postcopy_start(MigrationState *ms)
      * spice needs to trigger a transition now
      */
     ms->postcopy_after_devices = true;
-    notifier_list_notify(&migration_state_notifiers, ms);
+    migration_call_notifiers(ms);
 
     ms->downtime =  qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
 
@@ -4378,7 +4388,7 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
         rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
 
         /* Notify before starting migration thread */
-        notifier_list_notify(&migration_state_notifiers, s);
+        migration_call_notifiers(s);
     }
 
     qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
ui/spice-core.c +1 -2
index c3ac20a..62b7893 100644
@@ -819,8 +819,7 @@ static void qemu_spice_init(void)
     };
     using_spice = 1;
 
-    migration_state.notify = migration_state_notifier;
-    add_migration_state_change_notifier(&migration_state);
+    migration_add_notifier(&migration_state, migration_state_notifier);
     spice_migrate.base.sif = &migrate_interface.base;
     qemu_spice.add_interface(&spice_migrate.base);
 
Signature · 1 line
1.8.3.1
#18 lore raw

PATCH v9 17/46 migration: check mode in notifiers

In reply to Steve Sistare

The existing notifiers should only apply to normal mode.

No functional change.

---
 hw/net/virtio-net.c | 4 ++++
 hw/vfio/migration.c | 3 +++
 ui/spice-core.c     | 2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +4 -0
index 29af1a5..06f0dbe 100644
@@ -3351,6 +3351,10 @@ static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s)
 static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
 {
     MigrationState *s = data;
+
+    if (migrate_mode_of(s) != MIG_MODE_NORMAL) {
+        return;
+    }
     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
     virtio_net_handle_migration_primary(n, s);
 }
hw/vfio/migration.c +3 -0
index 5109124..6b0d401 100644
@@ -759,6 +759,9 @@ static void vfio_migration_state_notifier(Notifier *notifier, void *data)
     VFIODevice *vbasedev = migration->vbasedev;
     int ret;
 
+    if (migrate_mode_of(s) != MIG_MODE_NORMAL) {
+        return;
+    }
     trace_vfio_migration_state_notifier(vbasedev->name,
                                         MigrationStatus_str(s->state));
 
ui/spice-core.c +1 -1
index 62b7893..413c392 100644
@@ -558,7 +558,7 @@ static void migration_state_notifier(Notifier *notifier, void *data)
 {
     MigrationState *s = data;
 
-    if (!spice_have_target_host) {
+    if (!spice_have_target_host || migrate_mode_of(s) != MIG_MODE_NORMAL) {
         return;
     }
 
Signature · 1 line
1.8.3.1
#19 lore raw

PATCH v9 18/46 memory: flat section iterator

In reply to Steve Sistare

Add an iterator over the sections of a flattened address space.

---
 include/exec/memory.h | 31 +++++++++++++++++++++++++++++++
 softmmu/memory.c      | 21 +++++++++++++++++++++
 2 files changed, 52 insertions(+)
include/exec/memory.h +31 -0
index 7513d04..9629289 100644
@@ -2346,6 +2346,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
                                            RamDiscardManager *rdm);
 
 /**
+ * memory_region_section_cb: callback for address_space_flat_for_each_section()
+ *
+ * @mrs: MemoryRegionSection of the range
+ * @opaque: data pointer passed to address_space_flat_for_each_section()
+ * @errp: error message, returned to the address_space_flat_for_each_section
+ *        caller.
+ *
+ * Returns: non-zero to stop the iteration, and 0 to continue.  The same
+ * non-zero value is returned to the address_space_flat_for_each_section caller.
+ */
+
+typedef int (*memory_region_section_cb)(MemoryRegionSection *mrs,
+                                        void *opaque,
+                                        Error **errp);
+
+/**
+ * address_space_flat_for_each_section: walk the ranges in the address space
+ * flat view and call @func for each.  Return 0 on success, else return non-zero
+ * with a message in @errp.
+ *
+ * @as: target address space
+ * @func: callback function
+ * @opaque: passed to @func
+ * @errp: passed to @func
+ */
+int address_space_flat_for_each_section(AddressSpace *as,
+                                        memory_region_section_cb func,
+                                        void *opaque,
+                                        Error **errp);
+
+/**
  * memory_region_find: translate an address/size relative to a
  * MemoryRegion into a #MemoryRegionSection.
  *
softmmu/memory.c +21 -0
index 7ba2048..6aa3a2f 100644
@@ -2683,6 +2683,27 @@ bool memory_region_is_mapped(MemoryRegion *mr)
     return !!mr->container || mr->mapped_via_alias;
 }
 
+int address_space_flat_for_each_section(AddressSpace *as,
+                                        memory_region_section_cb func,
+                                        void *opaque,
+                                        Error **errp)
+{
+    FlatView *view = address_space_get_flatview(as);
+    FlatRange *fr;
+    int ret;
+
+    FOR_EACH_FLAT_RANGE(fr, view) {
+        MemoryRegionSection mrs = section_from_flat_range(fr, view);
+        ret = func(&mrs, opaque, errp);
+        if (ret) {
+            return ret;
+        }
+    }
+
+    flatview_unref(view);
+    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
#20 lore raw

PATCH v9 19/46 oslib: qemu_clear_cloexec

In reply to Steve Sistare

Define qemu_clear_cloexec, analogous to qemu_set_cloexec.

---
 include/qemu/osdep.h | 9 +++++++++
 util/oslib-posix.c   | 9 +++++++++
 util/oslib-win32.c   | 4 ++++
 3 files changed, 22 insertions(+)
include/qemu/osdep.h +9 -0
index b1c161c..015c764 100644
@@ -549,6 +549,15 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
 
 void qemu_set_cloexec(int fd);
 
+/*
+ * Clear FD_CLOEXEC for a descriptor.
+ *
+ * The caller must guarantee that no other fork+exec's occur before the
+ * exec that is intended to inherit this descriptor, eg by suspending CPUs
+ * and blocking monitor commands.
+ */
+void qemu_clear_cloexec(int fd);
+
 /* Return a dynamically allocated directory path that is appropriate for storing
  * local state.
  *
util/oslib-posix.c +9 -0
index bffec18..7990aab 100644
@@ -257,6 +257,15 @@ void qemu_set_cloexec(int fd)
     assert(f != -1);
 }
 
+void qemu_clear_cloexec(int fd)
+{
+    int f;
+    f = fcntl(fd, F_GETFD);
+    assert(f != -1);
+    f = fcntl(fd, F_SETFD, f & ~FD_CLOEXEC);
+    assert(f != -1);
+}
+
 char *
 qemu_get_local_state_dir(void)
 {
util/oslib-win32.c +4 -0
index 5723d3e..5bed148 100644
@@ -226,6 +226,10 @@ void qemu_set_cloexec(int fd)
 {
 }
 
+void qemu_clear_cloexec(int fd)
+{
+}
+
 int qemu_get_thread_id(void)
 {
     return GetCurrentThreadId();
Signature · 1 line
1.8.3.1
#21 lore raw

PATCH v9 20/46 vl: helper to request re-exec

In reply to Steve Sistare

Add a qemu_system_exec_request() hook that causes the main loop to exit and re-exec qemu using the specified arguments.

---
 include/sysemu/runstate.h |  1 +
 softmmu/runstate.c        | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)
include/sysemu/runstate.h +1 -0
index f3ed525..a5f0738 100644
@@ -62,6 +62,7 @@ void qemu_system_wakeup_enable(WakeupReason reason, bool enabled);
 void qemu_register_wakeup_notifier(Notifier *notifier);
 void qemu_register_wakeup_support(void);
 void qemu_system_shutdown_request(ShutdownCause reason);
+void qemu_system_exec_request(const strList *args);
 void qemu_system_powerdown_request(void);
 void qemu_register_powerdown_notifier(Notifier *notifier);
 void qemu_register_shutdown_notifier(Notifier *notifier);
softmmu/runstate.c +24 -0
index 168e1b7..14b43df 100644
@@ -37,6 +37,7 @@
 #include "monitor/monitor.h"
 #include "net/net.h"
 #include "net/vhost_net.h"
+#include "qapi/util.h"
 #include "qapi/error.h"
 #include "qapi/qapi-commands-run-state.h"
 #include "qapi/qapi-events-run-state.h"
@@ -353,6 +354,7 @@ static NotifierList wakeup_notifiers =
 static NotifierList shutdown_notifiers =
     NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
+static GStrv exec_argv;
 
 ShutdownCause qemu_shutdown_requested_get(void)
 {
@@ -369,6 +371,11 @@ static int qemu_shutdown_requested(void)
     return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
 }
 
+static int qemu_exec_requested(void)
+{
+    return exec_argv != NULL;
+}
+
 static void qemu_kill_report(void)
 {
     if (!qtest_driver() && shutdown_signal) {
@@ -627,6 +634,13 @@ void qemu_system_shutdown_request(ShutdownCause reason)
     qemu_notify_event();
 }
 
+void qemu_system_exec_request(const strList *args)
+{
+    exec_argv = strv_from_strList(args);
+    shutdown_requested = 1;
+    qemu_notify_event();
+}
+
 static void qemu_system_powerdown(void)
 {
     qapi_event_send_powerdown();
@@ -675,6 +689,16 @@ static bool main_loop_should_exit(void)
     }
     request = qemu_shutdown_requested();
     if (request) {
+
+        if (qemu_exec_requested()) {
+            Error *err = NULL;
+            execvp(exec_argv[0], exec_argv);
+            error_setg_errno(&err, errno, "execvp %s failed", exec_argv[0]);
+            error_report_err(err);
+            g_strfreev(exec_argv);
+            exec_argv = NULL;
+            return false;
+        }
         qemu_kill_report();
         qemu_system_shutdown(request);
         if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
Signature · 1 line
1.8.3.1
#22 lore raw

PATCH v9 21/46 cpr: preserve extra state

In reply to Steve Sistare

cpr must save state that is needed after qemu is restarted, when devices are realized. Thus the extra state cannot be saved in the migration stream file, as objects must already exist before that file can be loaded. Instead, define auxilliary state structures and vmstate descriptions, not associated with any registered object, and serialize the aux state to a memfd file. Deserialize after qemu restarts, before devices are realized.

The following state is saved:
* cpr mode
* file descriptor names and values
* memfd values and properties for ram blocks

---
 MAINTAINERS                   |   3 +
 include/migration/cpr-state.h |  30 ++++
 migration/cpr-state.c         | 362 ++++++++++++++++++++++++++++++++++++++++++
 migration/meson.build         |   1 +
 migration/trace-events        |   8 +
 stubs/cpr-state.c             |  26 +++
 stubs/meson.build             |   1 +
 7 files changed, 431 insertions(+)
 create mode 100644 include/migration/cpr-state.h
 create mode 100644 migration/cpr-state.c
 create mode 100644 stubs/cpr-state.c
MAINTAINERS +3 -0
index 362cfde..122af30 100644
@@ -3165,6 +3165,9 @@ M: Steve Sistare <[email protected]>
 R: Mark Kanda <[email protected]>
 S: Maintained
 F: tests/unit/test-strlist.c
+F: include/migration/cpr-state.h
+F: migration/cpr-state.c
+F: stubs/cpr-state.c
 
 Record/replay
 M: Pavel Dovgalyuk <[email protected]>
include/migration/cpr-state.h new file +30 -0
new file mode 100644
index 0000000..a9ae6ae
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2021, 2022 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_STATE_H
+#define MIGRATION_CPR_STATE_H
+
+#include "qapi/qapi-types-migration.h"
+
+typedef int (*cpr_walk_fd_cb)(const char *name, int id, int fd, void *opaque);
+
+void cpr_save_fd(const char *name, int id, int fd);
+void cpr_delete_fd(const char *name, int id);
+int cpr_find_fd(const char *name, int id);
+int cpr_walk_fd(cpr_walk_fd_cb cb, void *handle);
+void cpr_save_memfd(const char *name, int fd, size_t len, size_t maxlen,
+                    uint64_t align);
+int cpr_find_memfd(const char *name, size_t *lenp, size_t *maxlenp,
+                   uint64_t *alignp);
+void cpr_delete_memfd(const char *name);
+void cpr_resave_fd(const char *name, int id, int fd);
+int cpr_state_save(Error **errp);
+void cpr_state_unsave(void);
+int cpr_state_load(Error **errp);
+void cpr_state_print(void);
+
+#endif
migration/cpr-state.c new file +362 -0

362 lines not on this page · raw message

migration/meson.build +1 -0
index 30a8392..f7d130d 100644
@@ -15,6 +15,7 @@ softmmu_ss.add(files(
   'channel-block.c',
   'colo-failover.c',
   'colo.c',
+  'cpr-state.c',
   'exec.c',
   'fd.c',
   'file.c',
migration/trace-events +8 -0
index d72dd85..ed15a22 100644
@@ -327,6 +327,14 @@ colo_receive_message(const char *msg) "Receive '%s' message"
 # colo-failover.c
 colo_failover_set_state(const char *new_state) "new state %s"
 
+# cpr-state.c
+cpr_save_fd(const char *name, int id, int fd) "%s, id %d, fd %d"
+cpr_delete_fd(const char *name, int id) "%s, id %d"
+cpr_find_fd(const char *name, int id, int fd) "%s, id %d returns %d"
+cpr_save_memfd(const char *name, size_t len, size_t maxlen, uint64_t align) "%s, len %lu, maxlen %lu, align %lu"
+cpr_delete_memfd(const char *name) "%s"
+cpr_find_memfd(const char *name, size_t len, size_t maxlen, uint64_t align) "%s, len %lu, maxlen %lu, align %lu"
+
 # block-dirty-bitmap.c
 send_bitmap_header_enter(void) ""
 send_bitmap_bits(uint32_t flags, uint64_t start_sector, uint32_t nr_sectors, uint64_t data_size) "flags: 0x%x, start_sector: %" PRIu64 ", nr_sectors: %" PRIu32 ", data_size: %" PRIu64
stubs/cpr-state.c new file +26 -0
new file mode 100644
index 0000000..1c9dc78
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2022 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.
+ */
+
+#include "qemu/osdep.h"
+#include "migration/cpr-state.h"
+
+void cpr_save_fd(const char *name, int id, int fd)
+{
+}
+
+void cpr_delete_fd(const char *name, int id)
+{
+}
+
+int cpr_find_fd(const char *name, int id)
+{
+    return -1;
+}
+
+void cpr_resave_fd(const char *name, int id, int fd)
+{
+}
stubs/meson.build +1 -0
index d8f3fd5..392b1b5 100644
@@ -4,6 +4,7 @@ stub_ss.add(files('blk-exp-close-all.c'))
 stub_ss.add(files('blockdev-close-all-bdrv-states.c'))
 stub_ss.add(files('change-state-handler.c'))
 stub_ss.add(files('cmos.c'))
+stub_ss.add(files('cpr-state.c'))
 stub_ss.add(files('cpu-get-clock.c'))
 stub_ss.add(files('cpus-get-virtual-clock.c'))
 stub_ss.add(files('qemu-timer-notify-cb.c'))
Signature · 1 line
1.8.3.1
#23 lore raw

PATCH v9 22/46 cpr: exec mode

In reply to Steve Sistare

Add the cpr-exec migration mode. Usage:
qemu-system-$arch -migrate-mode-enable cpr-exec ... migrate_set_parameter mode cpr-exec
migrate_set_parameter cpr-exec-args <arg1> <arg2> ... -incoming defer migrate -d file:<filename>
... poll for runstate inmigrate ...
migrate_incoming file:<filename>

In this mode, the migrate command saves state to a file, directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID, and loads the file via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Arguments for the new qemu process are taken from the @cpr-exec-args parameter. The first argument should be the path of a new qemu binary, or a prefix command that exec's the new qemu binary. The arguments must match those used to initially start qemu, plus the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram, and the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Hence guest RAM is preserved in place, albeit with new virtual addresses in the qemu process. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy, and the implementation ignores all related parameters.

Cpr-exec mode supports memory-backend-memfd, memory-backend-epc, and vfio devices in subsequent patches.

---
 MAINTAINERS             |  2 ++
 include/migration/cpr.h | 18 +++++++++++
 migration/cpr.c         | 85 +++++++++++++++++++++++++++++++++++++++++++++++++
 migration/meson.build   |  1 +
 migration/migration.c   |  9 ++++++
 migration/ram.c         |  4 ++-
 qapi/migration.json     | 25 ++++++++++++---
 softmmu/physmem.c       | 48 +++++++++++++++++++++++++++-
 softmmu/runstate.c      |  4 ++-
 trace-events            |  1 +
 10 files changed, 190 insertions(+), 7 deletions(-)
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr.c
MAINTAINERS +2 -0
index 122af30..42f6f4a 100644
@@ -3168,6 +3168,8 @@ F: tests/unit/test-strlist.c
 F: include/migration/cpr-state.h
 F: migration/cpr-state.c
 F: stubs/cpr-state.c
+F: include/migration/cpr.h
+F: migration/cpr.c
 
 Record/replay
 M: Pavel Dovgalyuk <[email protected]>
include/migration/cpr.h new file +18 -0
new file mode 100644
index 0000000..c48be2d
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021, 2022 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
+
+extern bool only_cpr_capable;
+
+void cpr_init(void);
+void cpr_exec(void);
+void cpr_exec_failed(Error *err);
+void cpr_preserve_fds(void);
+
+#endif
migration/cpr.c new file +85 -0

85 lines not on this page · raw message

migration/meson.build +1 -0
index f7d130d..5b65561 100644
@@ -15,6 +15,7 @@ softmmu_ss.add(files(
   'channel-block.c',
   'colo-failover.c',
   'colo.c',
+  'cpr.c',
   'cpr-state.c',
   'exec.c',
   'fd.c',
migration/migration.c +9 -0
index d7c6902..1a8a6ff 100644
@@ -32,6 +32,7 @@
 #include "migration.h"
 #include "savevm.h"
 #include "qemu-file.h"
+#include "migration/cpr.h"
 #include "migration/vmstate.h"
 #include "block/block.h"
 #include "qapi/error.h"
@@ -231,6 +232,7 @@ void migration_object_init(void)
     blk_mig_init();
     ram_mig_init();
     dirty_bitmap_mig_init();
+    cpr_init();
 }
 
 void migration_cancel(const Error *error)
@@ -1964,6 +1966,7 @@ static void migrate_fd_cleanup(MigrationState *s)
     }
     migration_call_notifiers(s);
     block_cleanup_parameters(s);
+    cpr_exec();
     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
 }
 
@@ -2489,6 +2492,12 @@ static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
         return false;
     }
 
+    if (migrate_mode_of(s) == MIG_MODE_CPR_EXEC &&
+        !s->parameters.has_cpr_exec_args) {
+        error_setg(errp, "cpr-exec mode requires setting cpr-exec-args");
+        return false;
+    }
+
     if (migration_is_blocked(errp)) {
         return false;
     }
migration/ram.c +3 -1
index 3ea3b41..4c868d2 100644
@@ -165,6 +165,7 @@ out:
 bool ramblock_is_ignored(RAMBlock *block)
 {
     return !qemu_ram_is_migratable(block) ||
+           migrate_mode() == MIG_MODE_CPR_EXEC ||
            (migrate_ignore_shared() && qemu_ram_is_shared(block) &&
             ramblock_is_named_file(block));
 }
@@ -3058,7 +3059,8 @@ static void ram_init_bitmaps(RAMState *rs)
     WITH_RCU_READ_LOCK_GUARD() {
         ram_list_init_bitmaps();
         /* We don't use dirty log with background snapshots */
-        if (!migrate_background_snapshot()) {
+        if (!migrate_background_snapshot() &&
+            migrate_mode() == MIG_MODE_NORMAL) {
             memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION);
             migration_bitmap_sync_precopy(rs);
         }
qapi/migration.json +21 -4
index 839fcd4..a068d4c 100644
@@ -586,10 +586,21 @@
 #              arguments must match those used to initially start qemu, plus
 #              the -incoming option.
 #
+# @cpr-exec: The migrate command saves state to a file, directly exec's a
+#            new version of qemu on the same host, replacing the original
+#            process while retaining its PID, and loads the file via the
+#            migrate-incoming command.  The caller must specify a migration URI
+#            that writes to and reads from a file.  Guest RAM must be backed by
+#            a memory backend with share=on, and cannot be memory-backend-ram.
+#            Guest RAM is not copied, and storage blocks are not migrated, so
+#            all capabilities related to page and block copy must be disabled,
+#            and all related parameters are ignored.  Arguments for the new
+#            qemu process are taken from the @cpr-exec-args parameter.
+#
 # Since: 7.1
 ##
 { 'enum': 'MigMode',
-  'data': [ 'normal', 'cpr-reboot' ] }
+  'data': [ 'normal', 'cpr-reboot', 'cpr-exec' ] }
 
 ##
 # @BitmapMigrationBitmapAliasTransform:
@@ -712,7 +723,11 @@
 #                         at tail stage.
 #                         The default value is false. (Since 5.1)
 #
-# @cpr-exec-args: defined in a subsequent patch.
+# @cpr-exec-args: arguments passed to new qemu for cpr-exec mode.  The first
+#                 argument should be the path of a new qemu binary, or a prefix
+#                 command that exec's the new qemu binary.  The arguments must
+#                 match those used to initially start qemu, plus the -incoming
+#                 option. (Since 7.1)
 #
 # @tls-creds: ID of the 'tls-creds' object that provides credentials for
 #             establishing a TLS connection over the migration data channel.
@@ -885,7 +900,8 @@
 #                         at tail stage.
 #                         The default value is false. (Since 5.1)
 #
-# @cpr-exec-args: defined in a subsequent patch.
+# @cpr-exec-args: Arguments passed to new qemu for cpr-exec mode.
+#                 See description in @MigrationParameter. (Since 7.1)
 #
 # @tls-creds: ID of the 'tls-creds' object that provides credentials
 #             for establishing a TLS connection over the migration data
@@ -1090,7 +1106,8 @@
 #                         at tail stage.
 #                         The default value is false. (Since 5.1)
 #
-# @cpr-exec-args: defined in a subsequent patch.
+# @cpr-exec-args: Arguments passed to new qemu for cpr-exec mode.
+#                 See description in @MigrationParameter. (Since 7.1)
 #
 # @tls-creds: ID of the 'tls-creds' object that provides credentials
 #             for establishing a TLS connection over the migration data
softmmu/physmem.c +47 -1

85 lines not on this page · raw message

softmmu/runstate.c +3 -1
index 14b43df..fb86740 100644
@@ -32,6 +32,7 @@
 #include "exec/cpu-common.h"
 #include "exec/gdbstub.h"
 #include "hw/boards.h"
+#include "migration/cpr.h"
 #include "migration/misc.h"
 #include "migration/postcopy-ram.h"
 #include "monitor/monitor.h"
@@ -692,9 +693,10 @@ static bool main_loop_should_exit(void)
 
         if (qemu_exec_requested()) {
             Error *err = NULL;
+            cpr_preserve_fds();
             execvp(exec_argv[0], exec_argv);
             error_setg_errno(&err, errno, "execvp %s failed", exec_argv[0]);
-            error_report_err(err);
+            cpr_exec_failed(err);
             g_strfreev(exec_argv);
             exec_argv = NULL;
             return false;
trace-events +1 -0
index bc71006..07369bb 100644
@@ -45,6 +45,7 @@ ram_block_discard_range(const char *rbname, void *hva, size_t length, bool need_
 # accel/tcg/cputlb.c
 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"
 
 # gdbstub.c
 gdbstub_op_start(const char *device) "Starting gdbstub using device %s"
Signature · 1 line
1.8.3.1
#24 lore raw

PATCH v9 23/46 cpr: add exec-mode blockers

In reply to Steve Sistare

Add blockers for cpr-exec migration mode for devices and options that do not support it.

---
 accel/xen/xen-all.c   | 3 +++
 migration/migration.c | 5 +++++
 replay/replay.c       | 4 ++++
 3 files changed, 12 insertions(+)
accel/xen/xen-all.c +3 -0
index 69aa7d0..2896044 100644
@@ -181,6 +181,9 @@ static int xen_init(MachineState *ms)
      * opt out of system RAM being allocated by generic code
      */
     mc->default_ram_id = NULL;
+
+    migrate_add_blocker_always("xen does not support cpr exec",
+                               &error_fatal, MIG_MODE_CPR_EXEC, -1);
     return 0;
 }
 
migration/migration.c +5 -0
index 1a8a6ff..7b46213 100644
@@ -1348,6 +1348,11 @@ static bool migrate_caps_check(bool *cap_list,
         }
     }
 
+    if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
+        return migrate_add_blocker_always("x-colo is not compatible with cpr",
+                                          errp, MIG_MODE_CPR_EXEC, -1);
+    }
+
     return true;
 }
 
replay/replay.c +4 -0
index 4c396bb..b0194dd 100644
@@ -19,6 +19,7 @@
 #include "qemu/option.h"
 #include "sysemu/cpus.h"
 #include "qemu/error-report.h"
+#include "migration/blocker.h"
 
 /* Current version of the replay mechanism.
    Increase it when file format changes. */
@@ -232,6 +233,9 @@ static void replay_enable(const char *fname, int mode)
     const char *fmode = NULL;
     assert(!replay_file);
 
+    migrate_add_blocker_always("replay is not compatible with cpr",
+                                &error_fatal, MIG_MODE_CPR_EXEC, -1);
+
     switch (mode) {
     case REPLAY_MODE_RECORD:
         fmode = "wb";
Signature · 1 line
1.8.3.1
#25 lore raw

PATCH v9 24/46 cpr: ram block blockers

In reply to Steve Sistare

Unlike cpr-reboot mode, cpr-exec mode cannot save volatile ram blocks in the migration stream file and recreate them later, because the physical memory for the blocks is pinned and registered for vfio. Add an exec-mode blocker for volatile ram blocks.

---
 include/exec/memory.h   |  2 ++
 include/exec/ramblock.h |  1 +
 softmmu/physmem.c       | 45 +++++++++++++++++++++++++++++++++++++++++++++
 softmmu/vl.c            |  2 ++
 4 files changed, 50 insertions(+)
include/exec/memory.h +2 -0
index 9629289..128bf3e 100644
@@ -3039,6 +3039,8 @@ bool ram_block_discard_is_disabled(void);
  */
 bool ram_block_discard_is_required(void);
 
+void ram_block_add_cpr_blockers(Error **errp);
+
 #endif
 
 #endif
include/exec/ramblock.h +1 -0
index 6cbedf9..a5cbd9e 100644
@@ -39,6 +39,7 @@ struct RAMBlock {
     /* RCU-enabled, writes protected by the ramlist lock */
     QLIST_ENTRY(RAMBlock) next;
     QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
+    Error *cpr_blocker;
     int fd;
     size_t page_size;
     /* dirty bitmap used during migration */
softmmu/physmem.c +45 -0

63 lines not on this page · raw message

softmmu/vl.c +2 -0
index 83f3be0..5f6cd8c 100644
@@ -28,6 +28,7 @@
 #include "qemu/units.h"
 #include "exec/cpu-common.h"
 #include "exec/page-vary.h"
+#include "exec/memory.h"
 #include "hw/qdev-properties.h"
 #include "qapi/compat-policy.h"
 #include "qapi/error.h"
@@ -2589,6 +2590,7 @@ void qmp_x_exit_preconfig(Error **errp)
     qemu_init_board();
     qemu_create_cli_devices();
     qemu_machine_creation_done();
+    ram_block_add_cpr_blockers(&error_fatal);
 
     if (loadvm) {
         load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
Signature · 1 line
1.8.3.1
#26 lore raw

PATCH v9 25/46 cpr: only-cpr-capable

In reply to Steve Sistare

Add 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 the migrate commanmd will not fail due to a blocker.

---
 include/migration/cpr.h |  2 ++
 migration/migration.c   | 13 +++++++++++++
 qemu-options.hx         |  8 ++++++++
 softmmu/vl.c            |  4 ++++
 4 files changed, 27 insertions(+)
include/migration/cpr.h +2 -0
index c48be2d..f2dfb1b 100644
@@ -8,6 +8,8 @@
 #ifndef MIGRATION_CPR_H
 #define MIGRATION_CPR_H
 
+#define CPR_MODES     (BIT(MIG_MODE_CPR_REBOOT) | BIT(MIG_MODE_CPR_EXEC))
+
 extern bool only_cpr_capable;
 
 void cpr_init(void);
migration/migration.c +13 -0
index 7b46213..e0fc2b8 100644
@@ -2181,6 +2181,11 @@ bool migrate_mode_enabled(MigMode mode)
     return !!(migrate_enabled_modes & BIT(mode));
 }
 
+static bool migrate_modes_enabled(int modes)
+{
+    return (modes & migrate_enabled_modes) == modes;
+}
+
 static int migrate_check_enabled(Error **errp)
 {
     MigMode mode = migrate_mode();
@@ -2262,6 +2267,14 @@ static int check_blockers(Error **reasonp, Error **errp, int modes)
         return -EACCES;
     }
 
+    if (only_cpr_capable && (modes & CPR_MODES) &&
+        migrate_modes_enabled(modes & CPR_MODES)) {
+        error_propagate_prepend(errp, *reasonp,
+                                "-only-cpr-capable specified, but: ");
+        *reasonp = NULL;
+        return -EACCES;
+    }
+
     return add_blockers(reasonp, errp, modes);
 }
 
qemu-options.hx +8 -0
index 7797d3d..3ed9270 100644
@@ -4501,6 +4501,14 @@ SRST
     migration using any mode except 'normal'.
 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 cpr will not
+    fail due to a cpr blocker.
+ERST
+
 DEF("nodefaults", 0, QEMU_OPTION_nodefaults, \
     "-nodefaults     don't create default devices\n", QEMU_ARCH_ALL)
 SRST
softmmu/vl.c +4 -0
index 5f6cd8c..bed147c 100644
@@ -77,6 +77,7 @@
 #include "hw/block/block.h"
 #include "hw/i386/x86.h"
 #include "hw/i386/pc.h"
+#include "migration/cpr.h"
 #include "migration/misc.h"
 #include "migration/snapshot.h"
 #include "sysemu/tpm.h"
@@ -3366,6 +3367,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_migrate_mode_enable:
                 migrate_enable_mode(qapi_enum_parse(&MigMode_lookup, optarg, -1,
                                                     &error_fatal));
Signature · 1 line
1.8.3.1
#27 lore raw

PATCH v9 26/46 cpr: Mismatched GPAs fix

In reply to Steve Sistare

For cpr-exec mode, ramblock_is_ignored is always true, and the address of each migrated memory region must match the address of the statically initialized region on the target. However, for a PCI rom block, the region address is set when the guest writes to a BAR on the source, which does not occur on the target, causing a "Mismatched GPAs" error during cpr-exec migration.

To fix, unconditionally set the target's address to the source's address if the region does not have an address yet.

---
 include/exec/memory.h | 12 ++++++++++++
 migration/ram.c       | 17 ++++++++++-------
 softmmu/memory.c      | 10 ++++++++--
 3 files changed, 30 insertions(+), 9 deletions(-)
include/exec/memory.h +12 -0
index 128bf3e..9b3bad5 100644
@@ -738,6 +738,7 @@ struct MemoryRegion {
     bool flush_coalesced_mmio;
     uint8_t dirty_log_mask;
     bool is_iommu;
+    bool has_addr;
     RAMBlock *ram_block;
     Object *owner;
 
@@ -2268,6 +2269,17 @@ void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
 
 /*
+ * memory_region_set_address_only: set the address of a region.
+ *
+ * Same as memory_region_set_address, but without causing transaction side
+ * effects.
+ *
+ * @mr: the region to be updated
+ * @addr: new address, relative to container region
+ */
+void memory_region_set_address_only(MemoryRegion *mr, hwaddr addr);
+
+/*
  * memory_region_set_size: dynamically update the size of a region.
  *
  * Dynamically updates the size of a region.
migration/ram.c +10 -7
index 4c868d2..6005c53 100644
@@ -4314,13 +4314,16 @@ static int ram_load_precopy(QEMUFile *f)
                     }
                     if (migrate_ignore_shared()) {
                         hwaddr addr = qemu_get_be64(f);
-                        if (ramblock_is_ignored(block) &&
-                            block->mr->addr != addr) {
-                            error_report("Mismatched GPAs for block %s "
-                                         "%" PRId64 "!= %" PRId64,
-                                         id, (uint64_t)addr,
-                                         (uint64_t)block->mr->addr);
-                            ret = -EINVAL;
+                        if (ramblock_is_ignored(block)) {
+                            if (!block->mr->has_addr) {
+                                memory_region_set_address_only(block->mr, addr);
+                            } else if (block->mr->addr != addr) {
+                                error_report("Mismatched GPAs for block %s "
+                                             "%" PRId64 "!= %" PRId64,
+                                             id, (uint64_t)addr,
+                                             (uint64_t)block->mr->addr);
+                                ret = -EINVAL;
+                            }
                         }
                     }
                     ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG,
softmmu/memory.c +8 -2
index 6aa3a2f..8825608 100644
@@ -2552,7 +2552,7 @@ static void memory_region_add_subregion_common(MemoryRegion *mr,
     for (alias = subregion->alias; alias; alias = alias->alias) {
         alias->mapped_via_alias++;
     }
-    subregion->addr = offset;
+    memory_region_set_address_only(subregion, offset);
     memory_region_update_container_subregions(subregion);
 }
 
@@ -2632,10 +2632,16 @@ static void memory_region_readd_subregion(MemoryRegion *mr)
     }
 }
 
+void memory_region_set_address_only(MemoryRegion *mr, hwaddr addr)
+{
+    mr->addr = addr;
+    mr->has_addr = true;
+}
+
 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
 {
     if (addr != mr->addr) {
-        mr->addr = addr;
+        memory_region_set_address_only(mr, addr);
         memory_region_readd_subregion(mr);
     }
 }
Signature · 1 line
1.8.3.1
#28 lore raw

PATCH v9 27/46 hostmem-memfd: cpr support

In reply to Steve Sistare

Preserve memory-backend-memfd memory objects during cpr.

---
 backends/hostmem-memfd.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
backends/hostmem-memfd.c +12 -10
index 3fc85c3..d421a4f 100644
@@ -14,6 +14,7 @@
 #include "sysemu/hostmem.h"
 #include "qom/object_interfaces.h"
 #include "qemu/memfd.h"
+#include "migration/cpr-state.h"
 #include "qemu/module.h"
 #include "qapi/error.h"
 #include "qom/object.h"
@@ -36,28 +37,29 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
     HostMemoryBackendMemfd *m = MEMORY_BACKEND_MEMFD(backend);
     uint32_t ram_flags;
-    char *name;
-    int fd;
+    g_autofree char *name = host_memory_backend_get_name(backend);
+    int fd = cpr_find_fd(name, 0);
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
         return;
     }
 
-    fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
-                           m->hugetlb, m->hugetlbsize, m->seal ?
-                           F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
-                           errp);
-    if (fd == -1) {
-        return;
+    if (fd < 0) {
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
+                               m->hugetlb, m->hugetlbsize, m->seal ?
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
+                               errp);
+        if (fd == -1) {
+            return;
+        }
+        cpr_save_fd(name, 0, fd);
     }
 
-    name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
     memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
                                    backend->size, ram_flags, fd, 0, errp);
-    g_free(name);
 }
 
 static bool
Signature · 1 line
1.8.3.1
#29 lore raw

PATCH v9 28/46 hostmem-epc: cpr support

In reply to Steve Sistare

Preserve memory-backend-epc memory objects during cpr.

---
 backends/hostmem-epc.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
backends/hostmem-epc.c +10 -8
index 037292d..c6b1fe4 100644
@@ -16,32 +16,34 @@
 #include "qapi/error.h"
 #include "sysemu/hostmem.h"
 #include "hw/i386/hostmem-epc.h"
+#include "migration/cpr-state.h"
 
 static void
 sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
     uint32_t ram_flags;
-    char *name;
-    int fd;
+    g_autofree char *name = object_get_canonical_path(OBJECT(backend));
+    int fd = cpr_find_fd(name, 0);
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
         return;
     }
 
-    fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
     if (fd < 0) {
-        error_setg_errno(errp, errno,
-                         "failed to open /dev/sgx_vepc to alloc SGX EPC");
-        return;
+        fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
+        if (fd < 0) {
+            error_setg_errno(errp, errno,
+                            "failed to open /dev/sgx_vepc to alloc SGX EPC");
+            return;
+        }
+        cpr_save_fd(name, 0, fd);
     }
 
-    name = object_get_canonical_path(OBJECT(backend));
     ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED;
     memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
                                    name, backend->size, ram_flags,
                                    fd, 0, errp);
-    g_free(name);
 }
 
 static void sgx_epc_backend_instance_init(Object *obj)
Signature · 1 line
1.8.3.1
#30 lore raw

PATCH v9 29/46 pci: export msix_is_pending

In reply to Steve Sistare

Export msix_is_pending for use by cpr. No functional change.

---
 hw/pci/msix.c         | 2 +-
 include/hw/pci/msix.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
hw/pci/msix.c +1 -1
index 1e381a9..f642a43 100644
@@ -69,7 +69,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);
 }
include/hw/pci/msix.h +1 -0
index 4f1cda0..f2f2d85 100644
@@ -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);
Signature · 1 line
1.8.3.1
#31 lore raw

PATCH v9 30/46 vfio-pci: refactor for cpr

In reply to Steve Sistare

Refactor vector use into a helper vfio_vector_init.
Add vfio_notifier_init and vfio_notifier_cleanup for named notifiers, and pass additional arguments to vfio_remove_kvm_msi_virq.

All for use by cpr in a subsequent patch. No functional change.

---
 hw/vfio/pci.c | 99 +++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 66 insertions(+), 33 deletions(-)
hw/vfio/pci.c +66 -33
index 939dcc3..0143c9a 100644
@@ -52,6 +52,27 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
 static void vfio_msi_disable_common(VFIOPCIDevice *vdev);
 
+/* Create new or reuse existing eventfd */
+static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
+                              const char *name, int nr)
+{
+    int fd = -1;   /* placeholder until a subsequent patch */
+    int ret = 0;
+
+    if (fd >= 0) {
+        event_notifier_init_fd(e, fd);
+    } else {
+        ret = event_notifier_init(e, 0);
+    }
+    return ret;
+}
+
+static void vfio_notifier_cleanup(VFIOPCIDevice *vdev, EventNotifier *e,
+                                  const char *name, int nr)
+{
+    event_notifier_cleanup(e);
+}
+
 /*
  * Disabling BAR mmaping can be slow, but toggling it around INTx can
  * also be a huge overhead.  We try to get the best of both worlds by
@@ -132,8 +153,8 @@ static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
     pci_irq_deassert(&vdev->pdev);
 
     /* Get an eventfd for resample/unmask */
-    if (event_notifier_init(&vdev->intx.unmask, 0)) {
-        error_setg(errp, "event_notifier_init failed eoi");
+    if (vfio_notifier_init(vdev, &vdev->intx.unmask, "intx-unmask", 0)) {
+        error_setg(errp, "vfio_notifier_init intx-unmask failed");
         goto fail;
     }
 
@@ -165,7 +186,7 @@ fail_vfio:
     kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vdev->intx.interrupt,
                                           vdev->intx.route.irq);
 fail_irqfd:
-    event_notifier_cleanup(&vdev->intx.unmask);
+    vfio_notifier_cleanup(vdev, &vdev->intx.unmask, "intx-unmask", 0);
 fail:
     qemu_set_fd_handler(irq_fd, vfio_intx_interrupt, NULL, vdev);
     vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
@@ -194,7 +215,7 @@ static void vfio_intx_disable_kvm(VFIOPCIDevice *vdev)
     }
 
     /* We only need to close the eventfd for VFIO to cleanup the kernel side */
-    event_notifier_cleanup(&vdev->intx.unmask);
+    vfio_notifier_cleanup(vdev, &vdev->intx.unmask, "intx-unmask", 0);
 
     /* QEMU starts listening for interrupt events. */
     qemu_set_fd_handler(event_notifier_get_fd(&vdev->intx.interrupt),
@@ -285,9 +306,10 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
     }
 #endif
 
-    ret = event_notifier_init(&vdev->intx.interrupt, 0);
+    ret = vfio_notifier_init(vdev, &vdev->intx.interrupt, "intx-interrupt", 0);
     if (ret) {
-        error_setg_errno(errp, -ret, "event_notifier_init failed");
+        error_setg_errno(errp, -ret,
+                         "vfio_notifier_init intx-interrupt failed");
         return ret;
     }
     fd = event_notifier_get_fd(&vdev->intx.interrupt);
@@ -296,7 +318,7 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
     if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
                                VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) {
         qemu_set_fd_handler(fd, NULL, NULL, vdev);
-        event_notifier_cleanup(&vdev->intx.interrupt);
+        vfio_notifier_cleanup(vdev, &vdev->intx.interrupt, "intx-interrupt", 0);
         return -errno;
     }
 
@@ -324,7 +346,7 @@ static void vfio_intx_disable(VFIOPCIDevice *vdev)
 
     fd = event_notifier_get_fd(&vdev->intx.interrupt);
     qemu_set_fd_handler(fd, NULL, NULL, vdev);
-    event_notifier_cleanup(&vdev->intx.interrupt);
+    vfio_notifier_cleanup(vdev, &vdev->intx.interrupt, "intx-interrupt", 0);
 
     vdev->interrupt = VFIO_INT_NONE;
 
@@ -424,13 +446,15 @@ static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vector,
                                              vector_n, &vdev->pdev);
 }
 
-static void vfio_connect_kvm_msi_virq(VFIOMSIVector *vector)
+static void vfio_connect_kvm_msi_virq(VFIOMSIVector *vector, int nr)
 {
+    const char *name = "kvm_interrupt";
+
     if (vector->virq < 0) {
         return;
     }
 
-    if (event_notifier_init(&vector->kvm_interrupt, 0)) {
+    if (vfio_notifier_init(vector->vdev, &vector->kvm_interrupt, name, nr)) {
         goto fail_notifier;
     }
 
@@ -442,19 +466,20 @@ static void vfio_connect_kvm_msi_virq(VFIOMSIVector *vector)
     return;
 
 fail_kvm:
-    event_notifier_cleanup(&vector->kvm_interrupt);
+    vfio_notifier_cleanup(vector->vdev, &vector->kvm_interrupt, name, nr);
 fail_notifier:
     kvm_irqchip_release_virq(kvm_state, vector->virq);
     vector->virq = -1;
 }
 
-static void vfio_remove_kvm_msi_virq(VFIOMSIVector *vector)
+static void vfio_remove_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vector,
+                                     int nr)
 {
     kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vector->kvm_interrupt,
                                           vector->virq);
     kvm_irqchip_release_virq(kvm_state, vector->virq);
     vector->virq = -1;
-    event_notifier_cleanup(&vector->kvm_interrupt);
+    vfio_notifier_cleanup(vdev, &vector->kvm_interrupt, "kvm_interrupt", nr);
 }
 
 static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg,
@@ -464,6 +489,20 @@ 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)
+{
+    VFIOMSIVector *vector = &vdev->msi_vectors[nr];
+    PCIDevice *pdev = &vdev->pdev;
+
+    vector->vdev = vdev;
+    vector->virq = -1;
+    if (vfio_notifier_init(vdev, &vector->interrupt, "interrupt", nr)) {
+        error_report("vfio: vfio_notifier_init interrupt 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)
 {
@@ -476,13 +515,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
     vector = &vdev->msi_vectors[nr];
 
     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);
     }
 
     qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
@@ -494,7 +527,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
      */
     if (vector->virq >= 0) {
         if (!msg) {
-            vfio_remove_kvm_msi_virq(vector);
+            vfio_remove_kvm_msi_virq(vdev, vector, nr);
         } else {
             vfio_update_kvm_msi_virq(vector, *msg, pdev);
         }
@@ -506,7 +539,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                 vfio_route_change = kvm_irqchip_begin_route_changes(kvm_state);
                 vfio_add_kvm_msi_virq(vdev, vector, nr, true);
                 kvm_irqchip_commit_route_changes(&vfio_route_change);
-                vfio_connect_kvm_msi_virq(vector);
+                vfio_connect_kvm_msi_virq(vector, nr);
             }
         }
     }
@@ -602,7 +635,7 @@ static void vfio_commit_kvm_msi_virq_batch(VFIOPCIDevice *vdev)
     kvm_irqchip_commit_route_changes(&vfio_route_change);
 
     for (i = 0; i < vdev->nr_vectors; i++) {
-        vfio_connect_kvm_msi_virq(&vdev->msi_vectors[i]);
+        vfio_connect_kvm_msi_virq(&vdev->msi_vectors[i], i);
     }
 }
 
@@ -681,8 +714,8 @@ retry:
         vector->virq = -1;
         vector->use = true;
 
-        if (event_notifier_init(&vector->interrupt, 0)) {
-            error_report("vfio: Error: event_notifier_init failed");
+        if (vfio_notifier_init(vdev, &vector->interrupt, "interrupt", i)) {
+            error_report("vfio: Error: vfio_notifier_init failed");
         }
 
         qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
@@ -737,11 +770,11 @@ static void vfio_msi_disable_common(VFIOPCIDevice *vdev)
         VFIOMSIVector *vector = &vdev->msi_vectors[i];
         if (vdev->msi_vectors[i].use) {
             if (vector->virq >= 0) {
-                vfio_remove_kvm_msi_virq(vector);
+                vfio_remove_kvm_msi_virq(vdev, vector, i);
             }
             qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
                                 NULL, NULL, NULL);
-            event_notifier_cleanup(&vector->interrupt);
+            vfio_notifier_cleanup(vdev, &vector->interrupt, "interrupt", i);
         }
     }
 
@@ -2740,7 +2773,7 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
         return;
     }
 
-    if (event_notifier_init(&vdev->err_notifier, 0)) {
+    if (vfio_notifier_init(vdev, &vdev->err_notifier, "err_notifier", 0)) {
         error_report("vfio: Unable to init event notifier for error detection");
         vdev->pci_aer = false;
         return;
@@ -2753,7 +2786,7 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
                                VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
         qemu_set_fd_handler(fd, NULL, NULL, vdev);
-        event_notifier_cleanup(&vdev->err_notifier);
+        vfio_notifier_cleanup(vdev, &vdev->err_notifier, "err_notifier", 0);
         vdev->pci_aer = false;
     }
 }
@@ -2772,7 +2805,7 @@ static void vfio_unregister_err_notifier(VFIOPCIDevice *vdev)
     }
     qemu_set_fd_handler(event_notifier_get_fd(&vdev->err_notifier),
                         NULL, NULL, vdev);
-    event_notifier_cleanup(&vdev->err_notifier);
+    vfio_notifier_cleanup(vdev, &vdev->err_notifier, "err_notifier", 0);
 }
 
 static void vfio_req_notifier_handler(void *opaque)
@@ -2806,7 +2839,7 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
         return;
     }
 
-    if (event_notifier_init(&vdev->req_notifier, 0)) {
+    if (vfio_notifier_init(vdev, &vdev->req_notifier, "req_notifier", 0)) {
         error_report("vfio: Unable to init event notifier for device request");
         return;
     }
@@ -2818,7 +2851,7 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
                            VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
         qemu_set_fd_handler(fd, NULL, NULL, vdev);
-        event_notifier_cleanup(&vdev->req_notifier);
+        vfio_notifier_cleanup(vdev, &vdev->req_notifier, "req_notifier", 0);
     } else {
         vdev->req_enabled = true;
     }
@@ -2838,7 +2871,7 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     }
     qemu_set_fd_handler(event_notifier_get_fd(&vdev->req_notifier),
                         NULL, NULL, vdev);
-    event_notifier_cleanup(&vdev->req_notifier);
+    vfio_notifier_cleanup(vdev, &vdev->req_notifier, "req_notifier", 0);
 
     vdev->req_enabled = false;
 }
Signature · 1 line
1.8.3.1
#32 lore raw

PATCH v9 31/46 vfio-pci: cpr part 1 (fd and dma)

In reply to Steve Sistare

Enable vfio-pci devices to be saved and restored across a cpr-exec of qemu.

At vfio creation time, save the value of vfio container, group, and device descriptors in cpr state.

In the container pre_save handler, suspend the use of virtual addresses in DMA mappings with VFIO_DMA_UNMAP_FLAG_VADDR, 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 cpr state, 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 saved descriptors, 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. Vfio PCI device reset is also suppressed. 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 migrate_incoming is called. migrate_incoming loads the msi data, the vfio post_load handler finds eventfds in cpr state, rebuilds vector data structures, and attaches the interrupts to the new KVM instance. The container post_load handler then invokes the main vfio listener callback, which walks the flattened ranges of the vfio address space and calls VFIO_DMA_MAP_FLAG_VADDR to inform the kernel of the new VA's. Lastly, migration resumes the VM.

This functionality is delivered by 3 patches for clarity. Part 1 handles device file descriptors and DMA. Part 2 adds eventfd and MSI/MSI-X vector support. Part 3 adds INTX support.

---
 MAINTAINERS                   |   1 +
 hw/pci/pci.c                  |  12 ++++
 hw/vfio/common.c              | 152 +++++++++++++++++++++++++++++++++++-------
 hw/vfio/cpr.c                 | 120 +++++++++++++++++++++++++++++++++
 hw/vfio/meson.build           |   1 +
 hw/vfio/pci.c                 |  43 ++++++++++++
 hw/vfio/trace-events          |   1 +
 include/hw/vfio/vfio-common.h |  11 +++
 include/migration/vmstate.h   |   2 +
 9 files changed, 319 insertions(+), 24 deletions(-)
 create mode 100644 hw/vfio/cpr.c
MAINTAINERS +1 -0
index 42f6f4a..3af099a 100644
@@ -3170,6 +3170,7 @@ F: migration/cpr-state.c
 F: stubs/cpr-state.c
 F: include/migration/cpr.h
 F: migration/cpr.c
+F: hw/vfio/cpr.c
 
 Record/replay
 M: Pavel Dovgalyuk <[email protected]>
hw/pci/pci.c +12 -0
index 2f450f6..d7f945f 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/pci_host.h"
 #include "hw/qdev-properties.h"
 #include "hw/qdev-properties-system.h"
+#include "migration/misc.h"
 #include "migration/qemu-file-types.h"
 #include "migration/vmstate.h"
 #include "monitor/monitor.h"
@@ -350,6 +351,17 @@ static void pci_reset_regions(PCIDevice *dev)
 
 static void pci_do_device_reset(PCIDevice *dev)
 {
+    /*
+     * A PCI device that is resuming for cpr is already configured, so do
+     * not reset it here when we are called from qemu_system_reset prior to
+     * cpr load, else interrupts may be lost for vfio-pci devices.  It is
+     * safe to skip this reset for all PCI devices, because cpr load will set
+     * all fields that would have been set here.
+     */
+    if (migrate_mode() == MIG_MODE_CPR_EXEC) {
+        return;
+    }
+
     pci_device_deassert_intx(dev);
     assert(dev->irq_state == 0);
 
hw/vfio/common.c +128 -24

313 lines not on this page · raw message

hw/vfio/cpr.c new file +120 -0

120 lines not on this page · raw message

hw/vfio/meson.build +1 -0
index da9af29..e247b2b 100644
@@ -5,6 +5,7 @@ vfio_ss.add(files(
   'migration.c',
 ))
 vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
+  'cpr.c',
   'display.c',
   'pci-quirks.c',
   'pci.c',
hw/vfio/pci.c +43 -0

67 lines not on this page · raw message

hw/vfio/trace-events +1 -0
index 73dffe9..a6d0034 100644
@@ -119,6 +119,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_remap(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/vfio/vfio-common.h +11 -0
index e573f5a..17ad9ba 100644
@@ -81,10 +81,14 @@ typedef struct VFIOContainer {
     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
     MemoryListener listener;
     MemoryListener prereg_listener;
+    Notifier cpr_notifier;
+    Error *cpr_blocker;
     unsigned iommu_type;
     Error *error;
     bool initialized;
     bool dirty_pages_supported;
+    bool reused;
+    bool vaddr_unmapped;
     uint64_t dirty_pgsizes;
     uint64_t max_dirty_bitmap_size;
     unsigned long pgsizes;
@@ -136,6 +140,7 @@ typedef struct VFIODevice {
     bool no_mmap;
     bool ram_block_discard_allowed;
     bool enable_migration;
+    bool reused;
     VFIODeviceOps *ops;
     unsigned int num_irqs;
     unsigned int num_regions;
@@ -213,6 +218,9 @@ void vfio_put_group(VFIOGroup *group);
 int vfio_get_device(VFIOGroup *group, const char *name,
                     VFIODevice *vbasedev, Error **errp);
 
+int vfio_cpr_register_container(VFIOContainer *container, Error **errp);
+void vfio_cpr_unregister_container(VFIOContainer *container);
+
 extern const MemoryRegionOps vfio_region_ops;
 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
 extern VFIOGroupList vfio_group_list;
@@ -234,6 +242,9 @@ struct vfio_info_cap_header *
 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
 #endif
 extern const MemoryListener vfio_prereg_listener;
+void vfio_listener_register(VFIOContainer *container);
+void vfio_container_region_add(VFIOContainer *container,
+                               MemoryRegionSection *section);
 
 int vfio_spapr_create_window(VFIOContainer *container,
                              MemoryRegionSection *section,
include/migration/vmstate.h +2 -0
index ad24aa1..d76f7f1 100644
@@ -157,6 +157,8 @@ typedef enum {
     MIG_PRI_GICV3_ITS,          /* Must happen before PCI devices */
     MIG_PRI_GICV3,              /* Must happen before the ITS */
     MIG_PRI_MAX,
+    MIG_PRI_VFIO_PCI =
+        MIG_PRI_DEFAULT + 1,   /* Must happen before vfio containers */
 } MigrationPriority;
 
 struct VMStateField {
Signature · 1 line
1.8.3.1
#33 lore raw

PATCH v9 32/46 vfio-pci: cpr part 2 (msi)

In reply to Steve Sistare

Finish cpr for vfio-pci MSI/MSI-X devices by preserving eventfd's and vector state.

---
 hw/vfio/pci.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.c +118 -1
index b5fd2ec..1d0e8db 100644
@@ -54,17 +54,47 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
 static void vfio_msi_disable_common(VFIOPCIDevice *vdev);
 
+#define EVENT_FD_NAME(vdev, name)   \
+    g_strdup_printf("%s_%s", (vdev)->vbasedev.name, (name))
+
+static void save_event_fd(VFIOPCIDevice *vdev, const char *name, int nr,
+                          EventNotifier *ev)
+{
+    int fd = event_notifier_get_fd(ev);
+
+    if (fd >= 0) {
+        g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+
+        cpr_resave_fd(fdname, nr, fd);
+    }
+}
+
+static int load_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    return cpr_find_fd(fdname, nr);
+}
+
+static void delete_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    cpr_delete_fd(fdname, nr);
+}
+
 /* Create new or reuse existing eventfd */
 static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
                               const char *name, int nr)
 {
-    int fd = -1;   /* placeholder until a subsequent patch */
     int ret = 0;
+    int fd = load_event_fd(vdev, name, nr);
 
     if (fd >= 0) {
         event_notifier_init_fd(e, fd);
     } else {
         ret = event_notifier_init(e, 0);
+        if (!ret) {
+            save_event_fd(vdev, name, nr, e);
+        }
     }
     return ret;
 }
@@ -72,6 +102,7 @@ static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
 static void vfio_notifier_cleanup(VFIOPCIDevice *vdev, EventNotifier *e,
                                   const char *name, int nr)
 {
+    delete_event_fd(vdev, name, nr);
     event_notifier_cleanup(e);
 }
 
@@ -512,6 +543,15 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
     VFIOMSIVector *vector;
     int ret;
 
+    /*
+     * Ignore the callback from msix_set_vector_notifiers during resume.
+     * The necessary subset of these actions is called from vfio_claim_vectors
+     * during post load.
+     */
+    if (vdev->vbasedev.reused) {
+        return 0;
+    }
+
     trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
 
     vector = &vdev->msi_vectors[nr];
@@ -2784,6 +2824,11 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
     fd = event_notifier_get_fd(&vdev->err_notifier);
     qemu_set_fd_handler(fd, vfio_err_notifier_handler, NULL, vdev);
 
+    /* Do not alter irq_signaling during vfio_realize for cpr */
+    if (vdev->vbasedev.reused) {
+        return;
+    }
+
     if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0,
                                VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
@@ -2849,6 +2894,12 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
     fd = event_notifier_get_fd(&vdev->req_notifier);
     qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);
 
+    /* Do not alter irq_signaling during vfio_realize for cpr */
+    if (vdev->vbasedev.reused) {
+        vdev->req_enabled = true;
+        return;
+    }
+
     if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
                            VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
@@ -3357,6 +3408,46 @@ static Property vfio_pci_dev_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void vfio_claim_vectors(VFIOPCIDevice *vdev, int nr_vectors, bool msix)
+{
+    int i, fd;
+    bool pending = false;
+    PCIDevice *pdev = &vdev->pdev;
+
+    vdev->nr_vectors = nr_vectors;
+    vdev->msi_vectors = g_new0(VFIOMSIVector, nr_vectors);
+    vdev->interrupt = msix ? VFIO_INT_MSIX : VFIO_INT_MSI;
+
+    vfio_prepare_kvm_msi_virq_batch(vdev);
+
+    for (i = 0; i < nr_vectors; i++) {
+        VFIOMSIVector *vector = &vdev->msi_vectors[i];
+
+        fd = load_event_fd(vdev, "interrupt", i);
+        if (fd >= 0) {
+            vfio_vector_init(vdev, i);
+            qemu_set_fd_handler(fd, vfio_msi_interrupt, NULL, vector);
+        }
+
+        if (load_event_fd(vdev, "kvm_interrupt", i) >= 0) {
+            vfio_add_kvm_msi_virq(vdev, vector, i, msix);
+        } else {
+            vdev->msi_vectors[i].virq = -1;
+        }
+
+        if (msix && msix_is_pending(pdev, i) && msix_is_masked(pdev, i)) {
+            set_bit(i, vdev->msix->pending);
+            pending = true;
+        }
+    }
+
+    vfio_commit_kvm_msi_virq_batch(vdev);
+
+    if (msix) {
+        memory_region_set_enabled(&pdev->msix_pba_mmio, pending);
+    }
+}
+
 /*
  * The kernel may change non-emulated config bits.  Exclude them from the
  * changed-bits check in get_pci_config_device.
@@ -3375,6 +3466,29 @@ static int vfio_pci_pre_load(void *opaque)
     return 0;
 }
 
+static int vfio_pci_post_load(void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int nr_vectors;
+
+    if (msix_enabled(pdev)) {
+        msix_set_vector_notifiers(pdev, vfio_msix_vector_use,
+                                   vfio_msix_vector_release, NULL);
+        nr_vectors = vdev->msix->entries;
+        vfio_claim_vectors(vdev, nr_vectors, true);
+
+    } else if (msi_enabled(pdev)) {
+        nr_vectors = msi_nr_vectors_allocated(pdev);
+        vfio_claim_vectors(vdev, nr_vectors, false);
+
+    } else if (vfio_pci_read_config(pdev, PCI_INTERRUPT_PIN, 1)) {
+        assert(0);      /* completed in a subsequent patch */
+    }
+
+    return 0;
+}
+
 static bool vfio_pci_needed(void *opaque)
 {
     return migrate_mode() == MIG_MODE_CPR_EXEC;
@@ -3386,8 +3500,11 @@ static const VMStateDescription vfio_pci_vmstate = {
     .minimum_version_id = 0,
     .priority = MIG_PRI_VFIO_PCI,       /* must load before container */
     .pre_load = vfio_pci_pre_load,
+    .post_load = vfio_pci_post_load,
     .needed = vfio_pci_needed,
     .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
+        VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, vfio_msix_present),
         VMSTATE_END_OF_LIST()
     }
 };
Signature · 1 line
1.8.3.1
#34 lore raw

Re: [PATCH V9 32/46] vfio-pci: cpr part 2 (msi)

Hi Steve,

I have a few questions about the msi part of the vfio device. In the reboot mode, you mentioned "The guest drivers' suspend methods flush outstanding requests and re-initialize the devices". This means, during the downtime,the vfio device dose not generate interrupts. So no special processing is required for the msi interrupt of the vfio device. Am I right?

In the cpr-exec mode, will the vfio device be "stopped"? If the vfio device is running all the time,it is possible to generate interrrupts during the downtime. How to deal with these interrupts?

In addition, ARM GICv4 provides support for the direct injection of vLPIs. Interrupts are more difficult to handle. In this case, what should be done?

Look forward to your reply.

Kunkun Jiang

Quoted 199 lines · Steve Sistare

Finish cpr for vfio-pci MSI/MSI-X devices by preserving eventfd's and vector state.

---
  hw/vfio/pci.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.c +118 -1
index b5fd2ec..1d0e8db 100644
@@ -54,17 +54,47 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
  static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
  static void vfio_msi_disable_common(VFIOPCIDevice *vdev);
 
+#define EVENT_FD_NAME(vdev, name)   \
+    g_strdup_printf("%s_%s", (vdev)->vbasedev.name, (name))
+
+static void save_event_fd(VFIOPCIDevice *vdev, const char *name, int nr,
+                          EventNotifier *ev)
+{
+    int fd = event_notifier_get_fd(ev);
+
+    if (fd >= 0) {
+        g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+
+        cpr_resave_fd(fdname, nr, fd);
+    }
+}
+
+static int load_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    return cpr_find_fd(fdname, nr);
+}
+
+static void delete_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    cpr_delete_fd(fdname, nr);
+}
+
  /* Create new or reuse existing eventfd */
  static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
                                const char *name, int nr)
  {
-    int fd = -1;   /* placeholder until a subsequent patch */
      int ret = 0;
+    int fd = load_event_fd(vdev, name, nr);
 
      if (fd >= 0) {
          event_notifier_init_fd(e, fd);
      } else {
          ret = event_notifier_init(e, 0);
+        if (!ret) {
+            save_event_fd(vdev, name, nr, e);
+        }
      }
      return ret;
  }
@@ -72,6 +102,7 @@ static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
  static void vfio_notifier_cleanup(VFIOPCIDevice *vdev, EventNotifier *e,
                                    const char *name, int nr)
  {
+    delete_event_fd(vdev, name, nr);
      event_notifier_cleanup(e);
  }
 
@@ -512,6 +543,15 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
      VFIOMSIVector *vector;
      int ret;
 
+    /*
+     * Ignore the callback from msix_set_vector_notifiers during resume.
+     * The necessary subset of these actions is called from vfio_claim_vectors
+     * during post load.
+     */
+    if (vdev->vbasedev.reused) {
+        return 0;
+    }
+
      trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
 
      vector = &vdev->msi_vectors[nr];
@@ -2784,6 +2824,11 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
      fd = event_notifier_get_fd(&vdev->err_notifier);
      qemu_set_fd_handler(fd, vfio_err_notifier_handler, NULL, vdev);
 
+    /* Do not alter irq_signaling during vfio_realize for cpr */
+    if (vdev->vbasedev.reused) {
+        return;
+    }
+
      if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0,
                                 VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
          error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
@@ -2849,6 +2894,12 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
      fd = event_notifier_get_fd(&vdev->req_notifier);
      qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);
 
+    /* Do not alter irq_signaling during vfio_realize for cpr */
+    if (vdev->vbasedev.reused) {
+        vdev->req_enabled = true;
+        return;
+    }
+
      if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
                             VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
          error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
@@ -3357,6 +3408,46 @@ static Property vfio_pci_dev_properties[] = {
      DEFINE_PROP_END_OF_LIST(),
  };
 
+static void vfio_claim_vectors(VFIOPCIDevice *vdev, int nr_vectors, bool msix)
+{
+    int i, fd;
+    bool pending = false;
+    PCIDevice *pdev = &vdev->pdev;
+
+    vdev->nr_vectors = nr_vectors;
+    vdev->msi_vectors = g_new0(VFIOMSIVector, nr_vectors);
+    vdev->interrupt = msix ? VFIO_INT_MSIX : VFIO_INT_MSI;
+
+    vfio_prepare_kvm_msi_virq_batch(vdev);
+
+    for (i = 0; i < nr_vectors; i++) {
+        VFIOMSIVector *vector = &vdev->msi_vectors[i];
+
+        fd = load_event_fd(vdev, "interrupt", i);
+        if (fd >= 0) {
+            vfio_vector_init(vdev, i);
+            qemu_set_fd_handler(fd, vfio_msi_interrupt, NULL, vector);
+        }
+
+        if (load_event_fd(vdev, "kvm_interrupt", i) >= 0) {
+            vfio_add_kvm_msi_virq(vdev, vector, i, msix);
+        } else {
+            vdev->msi_vectors[i].virq = -1;
+        }
+
+        if (msix && msix_is_pending(pdev, i) && msix_is_masked(pdev, i)) {
+            set_bit(i, vdev->msix->pending);
+            pending = true;
+        }
+    }
+
+    vfio_commit_kvm_msi_virq_batch(vdev);
+
+    if (msix) {
+        memory_region_set_enabled(&pdev->msix_pba_mmio, pending);
+    }
+}
+
  /*
   * The kernel may change non-emulated config bits.  Exclude them from the
   * changed-bits check in get_pci_config_device.
@@ -3375,6 +3466,29 @@ static int vfio_pci_pre_load(void *opaque)
      return 0;
  }
 
+static int vfio_pci_post_load(void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int nr_vectors;
+
+    if (msix_enabled(pdev)) {
+        msix_set_vector_notifiers(pdev, vfio_msix_vector_use,
+                                   vfio_msix_vector_release, NULL);
+        nr_vectors = vdev->msix->entries;
+        vfio_claim_vectors(vdev, nr_vectors, true);
+
+    } else if (msi_enabled(pdev)) {
+        nr_vectors = msi_nr_vectors_allocated(pdev);
+        vfio_claim_vectors(vdev, nr_vectors, false);
+
+    } else if (vfio_pci_read_config(pdev, PCI_INTERRUPT_PIN, 1)) {
+        assert(0);      /* completed in a subsequent patch */
+    }
+
+    return 0;
+}
+
  static bool vfio_pci_needed(void *opaque)
  {
      return migrate_mode() == MIG_MODE_CPR_EXEC;
@@ -3386,8 +3500,11 @@ static const VMStateDescription vfio_pci_vmstate = {
      .minimum_version_id = 0,
      .priority = MIG_PRI_VFIO_PCI,       /* must load before container */
      .pre_load = vfio_pci_pre_load,
+    .post_load = vfio_pci_post_load,
      .needed = vfio_pci_needed,
      .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
+        VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, vfio_msix_present),
          VMSTATE_END_OF_LIST()
      }
  };
#35 lore raw

Re: [PATCH V9 32/46] vfio-pci: cpr part 2 (msi)

Quoted 8 lines · Kunkun Jiang

Hi Steve,

I have a few questions about the msi part of the vfio device. In the reboot mode, you mentioned "The guest drivers' suspend methods flush outstanding requests and re-initialize the devices". This means, during the downtime,the vfio device dose not generate interrupts. So no special processing is required for the msi interrupt of the vfio device. Am I right?

Correct.

Quoted 3 lines

In the cpr-exec mode, will the vfio device be "stopped"? If the vfio device is running all the time,it is possible to generate interrrupts during the downtime. How to deal with these interrupts?

The vfio device is not stopped, but its connection to the kvm instance is severed. Interrupts are pended in the vfio kernel state, and that state is preserved across exec, by preserving the vfio descriptors. After exec, qemu creates a new kvm instance, attaches vfio to it, and the interrupts are delivered.

Quoted 2 lines

In addition, ARM GICv4 provides support for the direct injection of vLPIs. Interrupts are more difficult to handle. In this case, what should be done?

I have not studied this feature or tried it.

- Steve

Quoted 196 lines

Look forward to your reply.

Kunkun Jiang

Quoted 190 lines · Steve Sistare

Finish cpr for vfio-pci MSI/MSI-X devices by preserving eventfd's and vector state.

---
  hw/vfio/pci.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-

  1 file changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.c +31 -1
index b5fd2ec..1d0e8db 100644
@@ -54,17 +54,47 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
   static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
   static void vfio_msi_disable_common(VFIOPCIDevice *vdev);
   +#define EVENT_FD_NAME(vdev, name)   \
+    g_strdup_printf("%s_%s", (vdev)->vbasedev.name, (name))
+
+static void save_event_fd(VFIOPCIDevice *vdev, const char *name, int nr,
+                          EventNotifier *ev)
+{
+    int fd = event_notifier_get_fd(ev);
+
+    if (fd >= 0) {
+        g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+
+        cpr_resave_fd(fdname, nr, fd);
+    }
+}
+
+static int load_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    return cpr_find_fd(fdname, nr);
+}
+
+static void delete_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    cpr_delete_fd(fdname, nr);
+}
+
   /* Create new or reuse existing eventfd */
   static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
                                 const char *name, int nr)
   {
-    int fd = -1;   /* placeholder until a subsequent patch */
       int ret = 0;
+    int fd = load_event_fd(vdev, name, nr);
         if (fd >= 0) {
           event_notifier_init_fd(e, fd);
       } else {
           ret = event_notifier_init(e, 0);
+        if (!ret) {
+            save_event_fd(vdev, name, nr, e);
+        }
       }
       return ret;
   }
@@ -72,6 +102,7 @@ static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
   static void vfio_notifier_cleanup(VFIOPCIDevice *vdev, EventNotifier *e,
                                     const char *name, int nr)
   {
+    delete_event_fd(vdev, name, nr);
       event_notifier_cleanup(e);
   }
   @@ -512,6 +543,15 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,

      VFIOMSIVector *vector;
      int ret;
  +    /*
+     * Ignore the callback from msix_set_vector_notifiers during resume. +     * The necessary subset of these actions is called from vfio_claim_vectors +     * during post load.
+     */
+    if (vdev->vbasedev.reused) {
+        return 0;
+    }
+
      trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr); vector = &vdev->msi_vectors[nr];

hw/vfio/pci.c +0 -0
@@ -2784,6 +2824,11 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
       fd = event_notifier_get_fd(&vdev->err_notifier);
       qemu_set_fd_handler(fd, vfio_err_notifier_handler, NULL, vdev);

  +    /* Do not alter irq_signaling during vfio_realize for cpr */ +    if (vdev->vbasedev.reused) {
+        return;
+    }
+
      if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0, VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);

hw/vfio/pci.c +0 -0
@@ -2849,6 +2894,12 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
       fd = event_notifier_get_fd(&vdev->req_notifier);
       qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);

  +    /* Do not alter irq_signaling during vfio_realize for cpr */ +    if (vdev->vbasedev.reused) {
+        vdev->req_enabled = true;
+        return;
+    }
+
      if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0, VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);

hw/vfio/pci.c +61 -0
@@ -3357,6 +3408,46 @@ static Property vfio_pci_dev_properties[] = {
       DEFINE_PROP_END_OF_LIST(),
   };
   +static void vfio_claim_vectors(VFIOPCIDevice *vdev, int nr_vectors, bool msix)
+{
+    int i, fd;
+    bool pending = false;
+    PCIDevice *pdev = &vdev->pdev;
+
+    vdev->nr_vectors = nr_vectors;
+    vdev->msi_vectors = g_new0(VFIOMSIVector, nr_vectors);
+    vdev->interrupt = msix ? VFIO_INT_MSIX : VFIO_INT_MSI;
+
+    vfio_prepare_kvm_msi_virq_batch(vdev);
+
+    for (i = 0; i < nr_vectors; i++) {
+        VFIOMSIVector *vector = &vdev->msi_vectors[i];
+
+        fd = load_event_fd(vdev, "interrupt", i);
+        if (fd >= 0) {
+            vfio_vector_init(vdev, i);
+            qemu_set_fd_handler(fd, vfio_msi_interrupt, NULL, vector);
+        }
+
+        if (load_event_fd(vdev, "kvm_interrupt", i) >= 0) {
+            vfio_add_kvm_msi_virq(vdev, vector, i, msix);
+        } else {
+            vdev->msi_vectors[i].virq = -1;
+        }
+
+        if (msix && msix_is_pending(pdev, i) && msix_is_masked(pdev, i)) {
+            set_bit(i, vdev->msix->pending);
+            pending = true;
+        }
+    }
+
+    vfio_commit_kvm_msi_virq_batch(vdev);
+
+    if (msix) {
+        memory_region_set_enabled(&pdev->msix_pba_mmio, pending);
+    }
+}
+
   /*
    * The kernel may change non-emulated config bits.  Exclude them from the
    * changed-bits check in get_pci_config_device.
@@ -3375,6 +3466,29 @@ static int vfio_pci_pre_load(void *opaque)
       return 0;
   }
   +static int vfio_pci_post_load(void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int nr_vectors;
+
+    if (msix_enabled(pdev)) {
+        msix_set_vector_notifiers(pdev, vfio_msix_vector_use,
+                                   vfio_msix_vector_release, NULL);
+        nr_vectors = vdev->msix->entries;
+        vfio_claim_vectors(vdev, nr_vectors, true);
+
+    } else if (msi_enabled(pdev)) {
+        nr_vectors = msi_nr_vectors_allocated(pdev);
+        vfio_claim_vectors(vdev, nr_vectors, false);
+
+    } else if (vfio_pci_read_config(pdev, PCI_INTERRUPT_PIN, 1)) {
+        assert(0);      /* completed in a subsequent patch */
+    }
+
+    return 0;
+}
+
   static bool vfio_pci_needed(void *opaque)
   {
       return migrate_mode() == MIG_MODE_CPR_EXEC;
@@ -3386,8 +3500,11 @@ static const VMStateDescription vfio_pci_vmstate = {
       .minimum_version_id = 0,

      .priority = MIG_PRI_VFIO_PCI,       /* must load before container */ .pre_load = vfio_pci_pre_load,
+    .post_load = vfio_pci_post_load,
      .needed = vfio_pci_needed,
      .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
+        VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, vfio_msix_present), VMSTATE_END_OF_LIST()
      }
  };

#36 lore raw

Re: [PATCH V9 32/46] vfio-pci: cpr part 2 (msi)

Hi Steve,

Quoted 14 lines · Steven Sistare
Quoted 8 lines · Kunkun Jiang

Hi Steve,

I have a few questions about the msi part of the vfio device. In the reboot mode, you mentioned "The guest drivers' suspend methods flush outstanding requests and re-initialize the devices". This means, during the downtime,the vfio device dose not generate interrupts. So no special processing is required for the msi interrupt of the vfio device. Am I right?

Correct.

Quoted 3 lines

In the cpr-exec mode, will the vfio device be "stopped"? If the vfio device is running all the time,it is possible to generate interrrupts during the downtime. How to deal with these interrupts?

Quoted 8 lines

The vfio device is not stopped, but its connection to the kvm instance is severed. Interrupts are pended in the vfio kernel state, and that state is preserved across exec, by preserving the vfio descriptors. After exec, qemu creates a new kvm instance, attaches vfio to it, and the interrupts are delivered.

Quoted 2 lines

In addition, ARM GICv4 provides support for the direct injection of vLPIs. Interrupts are more difficult to handle. In this case, what should be done?

I have not studied this feature or tried it.

Have you analyzed the VT-D post-interrupt feature? It is similar to the direct injection of vLPI. They all implement the interrupt injecting channel by executing irq_bypass_register_consumer.

According to the current kernel code, it need to first cancel the connection between vfio producer and kvm consumer, then establishes the connection between vfio producer and the new kvm consumer. During the unbinding process, both ARM and x86 will execute the callback of *_set_vcpu_affinity to modify the interrupt reporting channel. For x86, in the process of stop posting interrupts, back to remapping mode, cmpxchg_double is used in the code. Does this guarantee that interrupts will not be lost?

For ARM, it will first send a DISCARD command to ITS and then establish the interrupt reporting channel for GICv3. The DISCARD will remove the pending interrupt. Interrupts that come before channel re-establishment are silently discarded.
Do you guys have any good ideas?

Look forward to your reply.
Kunkun Jiang

Quoted 199 lines

- Steve

Quoted 187 lines

Look forward to your reply.

Kunkun Jiang

Quoted 182 lines · Steve Sistare

Finish cpr for vfio-pci MSI/MSI-X devices by preserving eventfd's and vector state.

---
   hw/vfio/pci.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
   1 file changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.c +31 -1
index b5fd2ec..1d0e8db 100644
@@ -54,17 +54,47 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
   static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
   static void vfio_msi_disable_common(VFIOPCIDevice *vdev);
   +#define EVENT_FD_NAME(vdev, name)   \
+    g_strdup_printf("%s_%s", (vdev)->vbasedev.name, (name))
+
+static void save_event_fd(VFIOPCIDevice *vdev, const char *name, int nr,
+                          EventNotifier *ev)
+{
+    int fd = event_notifier_get_fd(ev);
+
+    if (fd >= 0) {
+        g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+
+        cpr_resave_fd(fdname, nr, fd);
+    }
+}
+
+static int load_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    return cpr_find_fd(fdname, nr);
+}
+
+static void delete_event_fd(VFIOPCIDevice *vdev, const char *name, int nr)
+{
+    g_autofree char *fdname = EVENT_FD_NAME(vdev, name);
+    cpr_delete_fd(fdname, nr);
+}
+
   /* Create new or reuse existing eventfd */
   static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
                                 const char *name, int nr)
   {
-    int fd = -1;   /* placeholder until a subsequent patch */
       int ret = 0;
+    int fd = load_event_fd(vdev, name, nr);
         if (fd >= 0) {
           event_notifier_init_fd(e, fd);
       } else {
           ret = event_notifier_init(e, 0);
+        if (!ret) {
+            save_event_fd(vdev, name, nr, e);
+        }
       }
       return ret;
   }
@@ -72,6 +102,7 @@ static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e,
   static void vfio_notifier_cleanup(VFIOPCIDevice *vdev, EventNotifier *e,
                                     const char *name, int nr)
   {
+    delete_event_fd(vdev, name, nr);
       event_notifier_cleanup(e);
   }
   @@ -512,6 +543,15 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,

      VFIOMSIVector *vector;
      int ret;
  +    /*
+     * Ignore the callback from msix_set_vector_notifiers during resume. +     * The necessary subset of these actions is called from vfio_claim_vectors +     * during post load.
+     */
+    if (vdev->vbasedev.reused) {
+        return 0;
+    }

hw/vfio/pci.c +71 -0
+
       trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
         vector = &vdev->msi_vectors[nr];
@@ -2784,6 +2824,11 @@ static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
       fd = event_notifier_get_fd(&vdev->err_notifier);
       qemu_set_fd_handler(fd, vfio_err_notifier_handler, NULL, vdev);
   +    /* Do not alter irq_signaling during vfio_realize for cpr */
+    if (vdev->vbasedev.reused) {
+        return;
+    }
+
       if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0,
                                  VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
           error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
@@ -2849,6 +2894,12 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
       fd = event_notifier_get_fd(&vdev->req_notifier);
       qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);
   +    /* Do not alter irq_signaling during vfio_realize for cpr */
+    if (vdev->vbasedev.reused) {
+        vdev->req_enabled = true;
+        return;
+    }
+
       if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
                              VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
           error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
@@ -3357,6 +3408,46 @@ static Property vfio_pci_dev_properties[] = {
       DEFINE_PROP_END_OF_LIST(),
   };
   +static void vfio_claim_vectors(VFIOPCIDevice *vdev, int nr_vectors, bool msix)
+{
+    int i, fd;
+    bool pending = false;
+    PCIDevice *pdev = &vdev->pdev;
+
+    vdev->nr_vectors = nr_vectors;
+    vdev->msi_vectors = g_new0(VFIOMSIVector, nr_vectors);
+    vdev->interrupt = msix ? VFIO_INT_MSIX : VFIO_INT_MSI;
+
+    vfio_prepare_kvm_msi_virq_batch(vdev);
+
+    for (i = 0; i < nr_vectors; i++) {
+        VFIOMSIVector *vector = &vdev->msi_vectors[i];
+
+        fd = load_event_fd(vdev, "interrupt", i);
+        if (fd >= 0) {
+            vfio_vector_init(vdev, i);
+            qemu_set_fd_handler(fd, vfio_msi_interrupt, NULL, vector);
+        }
+
+        if (load_event_fd(vdev, "kvm_interrupt", i) >= 0) {
+            vfio_add_kvm_msi_virq(vdev, vector, i, msix);
+        } else {
+            vdev->msi_vectors[i].virq = -1;
+        }
+
+        if (msix && msix_is_pending(pdev, i) && msix_is_masked(pdev, i)) {
+            set_bit(i, vdev->msix->pending);
+            pending = true;
+        }
+    }
+
+    vfio_commit_kvm_msi_virq_batch(vdev);
+
+    if (msix) {
+        memory_region_set_enabled(&pdev->msix_pba_mmio, pending);
+    }
+}
+
   /*
    * The kernel may change non-emulated config bits.  Exclude them from the
    * changed-bits check in get_pci_config_device.
@@ -3375,6 +3466,29 @@ static int vfio_pci_pre_load(void *opaque)
       return 0;
   }
   +static int vfio_pci_post_load(void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int nr_vectors;
+
+    if (msix_enabled(pdev)) {
+        msix_set_vector_notifiers(pdev, vfio_msix_vector_use,
+                                   vfio_msix_vector_release, NULL);
+        nr_vectors = vdev->msix->entries;
+        vfio_claim_vectors(vdev, nr_vectors, true);
+
+    } else if (msi_enabled(pdev)) {
+        nr_vectors = msi_nr_vectors_allocated(pdev);
+        vfio_claim_vectors(vdev, nr_vectors, false);
+
+    } else if (vfio_pci_read_config(pdev, PCI_INTERRUPT_PIN, 1)) {
+        assert(0);      /* completed in a subsequent patch */
+    }
+
+    return 0;
+}
+
   static bool vfio_pci_needed(void *opaque)
   {
       return migrate_mode() == MIG_MODE_CPR_EXEC;
@@ -3386,8 +3500,11 @@ static const VMStateDescription vfio_pci_vmstate = {
       .minimum_version_id = 0,
       .priority = MIG_PRI_VFIO_PCI,       /* must load before container */
       .pre_load = vfio_pci_pre_load,
Quoted 8 lines

hw/vfio/pci.c static const VMStateDescription vfio_pci_vmstate = { · line 3510

Quoted 8 lines
hw/vfio/pci.c +3 -0
+    .post_load = vfio_pci_post_load,
       .needed = vfio_pci_needed,
       .fields = (VMStateField[]) {
+        VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
+        VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, vfio_msix_present),
           VMSTATE_END_OF_LIST()
       }
   };

.

#37 lore raw

Re: [PATCH V9 32/46] vfio-pci: cpr part 2 (msi)

Quoted 6 lines · Kunkun Jiang

For ARM, it will first send a DISCARD command to ITS and then establish the interrupt reporting channel for GICv3. The DISCARD will remove the pending interrupt. Interrupts that come before channel re-establishment are silently discarded. Do you guys have any good ideas?

I'm missing the context, but if you're worried about interrupts that are lost between the DISCARD and the MAPTI commands, the only way to solve the problem is to inject a spurious interrupt after the MAPTI has taken place.

If it hurts, don't do that.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#38 lore raw

PATCH v9 33/46 vfio-pci: cpr part 3 (intx)

In reply to Steve Sistare

Preserve vfio INTX state across cpr restart. Preserve VFIOINTx fields as follows:
pin : Recover this from the vfio config in kernel space interrupt : Preserve its eventfd descriptor across exec. unmask : Ditto
route.irq : This could perhaps be recovered in vfio_pci_post_load by
calling pci_device_route_intx_to_irq(pin), whose implementation reads config space for a bridge device such as ich9. However, there is no guarantee that the bridge vmstate is read before vfio vmstate. Rather than fiddling with MigrationPriority for vmstate handlers, explicitly save route.irq in vfio vmstate.
pending : save in vfio vmstate.
mmap_timeout, mmap_timer : Re-initialize
bool kvm_accel : Re-initialize

In vfio_realize, defer calling vfio_intx_enable until the vmstate is available, in vfio_pci_post_load. Modify vfio_intx_enable and vfio_intx_kvm_enable to skip vfio initialization, but still perform kvm initialization.

---
 hw/vfio/pci.c | 81 ++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 61 insertions(+), 20 deletions(-)
hw/vfio/pci.c +61 -20
index 1d0e8db..906e3cb 100644
@@ -179,11 +179,13 @@ static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
         return;
     }
 
-    /* Get to a known interrupt state */
-    qemu_set_fd_handler(irq_fd, NULL, NULL, vdev);
-    vfio_mask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
-    vdev->intx.pending = false;
-    pci_irq_deassert(&vdev->pdev);
+    if (!vdev->vbasedev.reused) {
+        /* Get to a known interrupt state */
+        qemu_set_fd_handler(irq_fd, NULL, NULL, vdev);
+        vfio_mask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
+        vdev->intx.pending = false;
+        pci_irq_deassert(&vdev->pdev);
+    }
 
     /* Get an eventfd for resample/unmask */
     if (vfio_notifier_init(vdev, &vdev->intx.unmask, "intx-unmask", 0)) {
@@ -199,15 +201,17 @@ static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
         goto fail_irqfd;
     }
 
-    if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
-                               VFIO_IRQ_SET_ACTION_UNMASK,
-                               event_notifier_get_fd(&vdev->intx.unmask),
-                               errp)) {
-        goto fail_vfio;
-    }
+    if (!vdev->vbasedev.reused) {
+        if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
+                                   VFIO_IRQ_SET_ACTION_UNMASK,
+                                   event_notifier_get_fd(&vdev->intx.unmask),
+                                   errp)) {
+            goto fail_vfio;
+        }
 
-    /* Let'em rip */
-    vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
+        /* Let'em rip */
+        vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
+    }
 
     vdev->intx.kvm_accel = true;
 
@@ -323,7 +327,13 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
         return 0;
     }
 
-    vfio_disable_interrupts(vdev);
+    /*
+     * Do not alter interrupt state during vfio_realize and cpr load.  The
+     * reused flag is cleared thereafter.
+     */
+    if (!vdev->vbasedev.reused) {
+        vfio_disable_interrupts(vdev);
+    }
 
     vdev->intx.pin = pin - 1; /* Pin A (1) -> irq[0] */
     pci_config_set_interrupt_pin(vdev->pdev.config, pin);
@@ -348,7 +358,8 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
     fd = event_notifier_get_fd(&vdev->intx.interrupt);
     qemu_set_fd_handler(fd, vfio_intx_interrupt, NULL, vdev);
 
-    if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
+    if (!vdev->vbasedev.reused &&
+        vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
                                VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) {
         qemu_set_fd_handler(fd, NULL, NULL, vdev);
         vfio_notifier_cleanup(vdev, &vdev->intx.interrupt, "intx-interrupt", 0);
@@ -3189,9 +3200,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
                                              vfio_intx_routing_notifier);
         vdev->irqchip_change_notifier.notify = vfio_irqchip_change;
         kvm_irqchip_add_change_notifier(&vdev->irqchip_change_notifier);
-        ret = vfio_intx_enable(vdev, errp);
-        if (ret) {
-            goto out_deregister;
+
+        /* Wait until cpr load reads intx routing data to enable */
+        if (!vdev->vbasedev.reused) {
+            ret = vfio_intx_enable(vdev, errp);
+            if (ret) {
+                goto out_deregister;
+            }
         }
     }
 
@@ -3471,6 +3486,7 @@ static int vfio_pci_post_load(void *opaque, int version_id)
     VFIOPCIDevice *vdev = opaque;
     PCIDevice *pdev = &vdev->pdev;
     int nr_vectors;
+    int ret = 0;
 
     if (msix_enabled(pdev)) {
         msix_set_vector_notifiers(pdev, vfio_msix_vector_use,
@@ -3483,10 +3499,34 @@ static int vfio_pci_post_load(void *opaque, int version_id)
         vfio_claim_vectors(vdev, nr_vectors, false);
 
     } else if (vfio_pci_read_config(pdev, PCI_INTERRUPT_PIN, 1)) {
-        assert(0);      /* completed in a subsequent patch */
+        Error *err = 0;
+        ret = vfio_intx_enable(vdev, &err);
+        if (ret) {
+            error_report_err(err);
+        }
     }
 
-    return 0;
+    return ret;
+}
+
+static const VMStateDescription vfio_intx_vmstate = {
+    .name = "vfio-intx",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .fields = (VMStateField[]) {
+        VMSTATE_BOOL(pending, VFIOINTx),
+        VMSTATE_UINT32(route.mode, VFIOINTx),
+        VMSTATE_INT32(route.irq, VFIOINTx),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+#define VMSTATE_VFIO_INTX(_field, _state) {                         \
+    .name       = (stringify(_field)),                              \
+    .size       = sizeof(VFIOINTx),                                 \
+    .vmsd       = &vfio_intx_vmstate,                               \
+    .flags      = VMS_STRUCT,                                       \
+    .offset     = vmstate_offset_value(_state, _field, VFIOINTx),   \
 }
 
 static bool vfio_pci_needed(void *opaque)
@@ -3505,6 +3545,7 @@ static const VMStateDescription vfio_pci_vmstate = {
     .fields = (VMStateField[]) {
         VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
         VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, vfio_msix_present),
+        VMSTATE_VFIO_INTX(intx, VFIOPCIDevice),
         VMSTATE_END_OF_LIST()
     }
 };
Signature · 1 line
1.8.3.1
#39 lore raw

PATCH v9 34/46 vfio-pci: recover from unmap-all-vaddr failure

In reply to Steve Sistare

If there are multiple containers and unmap-all fails for some container, we need to remap vaddr for the other containers for which unmap-all succeeded. Recover by walking all flat sections of all containers to restore the vaddr for each. Do so by invoking the vfio listener callback, and passing a new "remap" flag that tells it to restore a mapping without re-allocating new userland data structures.

---
 hw/vfio/common.c              | 79 ++++++++++++++++++++++++++++++++++---------
 hw/vfio/cpr.c                 | 36 ++++++++++++++++++++
 include/hw/vfio/vfio-common.h |  2 +-
 3 files changed, 100 insertions(+), 17 deletions(-)
hw/vfio/common.c +63 -16
index b7b35d4..de821e1 100644
@@ -895,15 +895,35 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
     return true;
 }
 
+static VFIORamDiscardListener *vfio_find_ram_discard_listener(
+    VFIOContainer *container, MemoryRegionSection *section)
+{
+    VFIORamDiscardListener *vrdl;
+
+    QLIST_FOREACH(vrdl, &container->vrdl_list, next) {
+        if (vrdl->mr == section->mr &&
+            vrdl->offset_within_address_space ==
+            section->offset_within_address_space) {
+            break;
+        }
+    }
+
+    if (!vrdl) {
+        hw_error("vfio: Trying to sync missing RAM discard listener");
+        /* does not return */
+    }
+    return vrdl;
+}
+
 static void vfio_listener_region_add(MemoryListener *listener,
                                      MemoryRegionSection *section)
 {
     VFIOContainer *container = container_of(listener, VFIOContainer, listener);
-    vfio_container_region_add(container, section);
+    vfio_container_region_add(container, section, false);
 }
 
 void vfio_container_region_add(VFIOContainer *container,
-                               MemoryRegionSection *section)
+                               MemoryRegionSection *section, bool remap)
 {
     hwaddr iova, end;
     Int128 llend, llsize;
@@ -1033,6 +1053,30 @@ void vfio_container_region_add(VFIOContainer *container,
         int iommu_idx;
 
         trace_vfio_listener_region_add_iommu(iova, end);
+
+        /*
+         * If remap, then VFIO_DMA_UNMAP_FLAG_VADDR has been called, and we
+         * want to remap the vaddr.  vfio_container_region_add was already
+         * called in the past, so the giommu already exists.  Find it and
+         * replay it, which calls vfio_dma_map further down the stack.
+         */
+
+        if (remap) {
+            hwaddr as_offset = section->offset_within_address_space;
+            hwaddr iommu_offset = as_offset - section->offset_within_region;
+
+            QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
+                if (giommu->iommu_mr == iommu_mr &&
+                    giommu->iommu_offset == iommu_offset) {
+                    memory_region_iommu_replay(giommu->iommu_mr, &giommu->n);
+                    return;
+                }
+            }
+            error_report("Container cannot find iommu region %s offset %lx",
+                memory_region_name(section->mr), iommu_offset);
+            goto fail;
+        }
+
         /*
          * FIXME: For VFIO iommu types which have KVM acceleration to
          * avoid bouncing all map/unmaps through qemu this way, this
@@ -1083,7 +1127,21 @@ void vfio_container_region_add(VFIOContainer *container,
      * about changes.
      */
     if (memory_region_has_ram_discard_manager(section->mr)) {
-        vfio_register_ram_discard_listener(container, section);
+        /*
+         * If remap, then VFIO_DMA_UNMAP_FLAG_VADDR has been called, and we
+         * want to remap the vaddr.  vfio_container_region_add was already
+         * called in the past, so the ram discard listener already exists.
+         * Call its populate function directly, which calls vfio_dma_map.
+         */
+        if (remap)  {
+            VFIORamDiscardListener *vrdl =
+                vfio_find_ram_discard_listener(container, section);
+            if (vrdl->listener.notify_populate(&vrdl->listener, section)) {
+                error_report("listener.notify_populate failed");
+            }
+        } else {
+            vfio_register_ram_discard_listener(container, section);
+        }
         return;
     }
 
@@ -1417,19 +1475,8 @@ static int vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainer *container,
                                                    MemoryRegionSection *section)
 {
     RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
-    VFIORamDiscardListener *vrdl = NULL;
-
-    QLIST_FOREACH(vrdl, &container->vrdl_list, next) {
-        if (vrdl->mr == section->mr &&
-            vrdl->offset_within_address_space ==
-            section->offset_within_address_space) {
-            break;
-        }
-    }
-
-    if (!vrdl) {
-        hw_error("vfio: Trying to sync missing RAM discard listener");
-    }
+    VFIORamDiscardListener *vrdl =
+        vfio_find_ram_discard_listener(container, section);
 
     /*
      * We only want/can synchronize the bitmap for actually mapped parts -
hw/vfio/cpr.c +36 -0
index 83f7872..1f682cb 100644
@@ -34,6 +34,15 @@ vfio_dma_unmap_vaddr_all(VFIOContainer *container, Error **errp)
     return 0;
 }
 
+static int
+vfio_region_remap(MemoryRegionSection *section, void *handle, Error **errp)
+{
+    VFIOContainer *container = handle;
+    vfio_container_region_add(container, section, true);
+    container->vaddr_unmapped = false;
+    return 0;
+}
+
 static bool vfio_is_cpr_capable(VFIOContainer *container, Error **errp)
 {
     if (!ioctl(container->fd, VFIO_CHECK_EXTENSION, VFIO_UPDATE_VADDR) ||
@@ -99,6 +108,30 @@ static const VMStateDescription vfio_container_vmstate = {
     }
 };
 
+static void vfio_cpr_fail_notifier(Notifier *notifier, void *data)
+{
+    MigrationState *s = data;
+    VFIOContainer *container;
+    Error *err = NULL;
+
+    if (!migration_has_failed(s) || migrate_mode_of(s) != MIG_MODE_CPR_EXEC) {
+        return;
+    }
+
+    container = container_of(notifier, VFIOContainer, cpr_notifier);
+    if (container->vaddr_unmapped) {
+
+        /* Set reused so vfio_dma_map restores vaddr */
+        container->reused = true;
+        if (address_space_flat_for_each_section(container->space->as,
+                                                vfio_region_remap,
+                                                container, &err)) {
+            error_report_err(err);
+        }
+        container->reused = false;
+    }
+}
+
 int vfio_cpr_register_container(VFIOContainer *container, Error **errp)
 {
     container->cpr_blocker = NULL;
@@ -109,6 +142,7 @@ int vfio_cpr_register_container(VFIOContainer *container, Error **errp)
 
     vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 
+    migration_add_notifier(&container->cpr_notifier, vfio_cpr_fail_notifier);
     return 0;
 }
 
@@ -117,4 +151,6 @@ void vfio_cpr_unregister_container(VFIOContainer *container)
     migrate_del_blocker(&container->cpr_blocker);
 
     vmstate_unregister(NULL, &vfio_container_vmstate, container);
+
+    migration_remove_notifier(&container->cpr_notifier);
 }
include/hw/vfio/vfio-common.h +1 -1
index 17ad9ba..b682645 100644
@@ -244,7 +244,7 @@ vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
 extern const MemoryListener vfio_prereg_listener;
 void vfio_listener_register(VFIOContainer *container);
 void vfio_container_region_add(VFIOContainer *container,
-                               MemoryRegionSection *section);
+                               MemoryRegionSection *section, bool remap);
 
 int vfio_spapr_create_window(VFIOContainer *container,
                              MemoryRegionSection *section,
Signature · 1 line
1.8.3.1
#40 lore raw

PATCH v9 35/46 vhost: reset vhost devices for cpr

In reply to Steve Sistare

A 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         | 23 +++++++++++++++++++++++
 include/hw/virtio/vhost.h |  1 +
 2 files changed, 24 insertions(+)
hw/virtio/vhost.c +23 -0
index 8d2a136..bc8df55 100644
@@ -23,6 +23,7 @@
 #include "standard-headers/linux/vhost_types.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio-access.h"
+#include "migration/misc.h"
 #include "migration/blocker.h"
 #include "migration/qemu-file-types.h"
 #include "sysemu/dma.h"
@@ -1347,6 +1348,25 @@ static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
     }
 }
 
+static void vhost_cpr_exec_notifier(Notifier *notifier, void *data)
+{
+    MigrationState *s = data;
+    struct vhost_dev *dev;
+    int r;
+
+    if (migrate_mode_of(s) == MIG_MODE_CPR_EXEC) {
+        dev = container_of(notifier, struct vhost_dev, cpr_notifier);
+        if (migration_has_failed(s)) {
+            r = dev->vhost_ops->vhost_set_owner(dev);
+        } else {
+            r = dev->vhost_ops->vhost_reset_device(dev);
+        }
+        if (r < 0) {
+            VHOST_OPS_DEBUG(r, "vhost_reset_device failed");
+        }
+    }
+}
+
 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
                    VhostBackendType backend_type, uint32_t busyloop_timeout,
                    Error **errp)
@@ -1356,6 +1376,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 
     hdev->vdev = NULL;
     hdev->migration_blocker = NULL;
+    hdev->cpr_notifier.notify = NULL;
 
     r = vhost_set_backend_type(hdev, backend_type);
     assert(r >= 0);
@@ -1446,6 +1467,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     hdev->log_enabled = false;
     hdev->started = false;
     memory_listener_register(&hdev->memory_listener, &address_space_memory);
+    migration_add_notifier(&hdev->cpr_notifier, vhost_cpr_exec_notifier);
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
     if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
@@ -1482,6 +1504,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
         QLIST_REMOVE(hdev, entry);
     }
     migrate_del_blocker(&hdev->migration_blocker);
+    migration_remove_notifier(&hdev->cpr_notifier);
     g_free(hdev->mem);
     g_free(hdev->mem_sections);
     if (hdev->vhost_ops) {
include/hw/virtio/vhost.h +1 -0
index a346f23..a12c4e1 100644
@@ -101,6 +101,7 @@ struct vhost_dev {
     QLIST_ENTRY(vhost_dev) entry;
     QLIST_HEAD(, vhost_iommu) iommu_list;
     IOMMUNotifier n;
+    Notifier cpr_notifier;
     const VhostDevConfigOps *config_ops;
 };
 
Signature · 1 line
1.8.3.1
#41 lore raw

PATCH v9 36/46 chardev: cpr framework

In reply to Steve Sistare

Add QEMU_CHAR_FEATURE_CPR for devices that support cpr-exec by preserving an open descriptor across exec. Add the chardev reopen-on-cpr option for devices that should be closed on cpr and reopened after exec.

Enable cpr for a chardev if it has QEMU_CHAR_FEATURE_CPR and reopen-on-cpr is false. Allow cpr-save if either QEMU_CHAR_FEATURE_CPR or reopen-on-cpr is true for all chardevs in the configuration.

---
 chardev/char.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++----
 include/chardev/char.h |  5 +++++
 qapi/char.json         |  7 ++++++-
 qemu-options.hx        | 26 ++++++++++++++++++++++----
 4 files changed, 78 insertions(+), 9 deletions(-)
chardev/char.c +45 -4
index 0169d8d..01755d7 100644
@@ -36,6 +36,8 @@
 #include "qemu/help_option.h"
 #include "qemu/module.h"
 #include "qemu/option.h"
+#include "migration/cpr-state.h"
+#include "migration/blocker.h"
 #include "qemu/id.h"
 #include "qemu/coroutine.h"
 #include "qemu/yank.h"
@@ -236,26 +238,55 @@ int qemu_chr_add_client(Chardev *s, int fd)
 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
                            bool *be_opened, Error **errp)
 {
+    ERRP_GUARD();
+    g_autofree char *fdname = NULL;
+
     ChardevClass *cc = CHARDEV_GET_CLASS(chr);
     /* Any ChardevCommon member would work */
     ChardevCommon *common = backend ? backend->u.null.data : NULL;
+    bool has_logfile = (common && common->has_logfile);
+    bool has_feature_cpr;
 
-    if (common && common->has_logfile) {
+    if (has_logfile) {
         int flags = O_WRONLY;
+        fdname = g_strdup_printf("%s_log", chr->label);
         if (common->has_logappend &&
             common->logappend) {
             flags |= O_APPEND;
         } else {
             flags |= O_TRUNC;
         }
-        chr->logfd = qemu_create(common->logfile, flags, 0666, errp);
+        chr->logfd = cpr_find_fd(fdname, 0);
+        if (chr->logfd < 0) {
+            chr->logfd = qemu_create(common->logfile, flags, 0666, errp);
+        }
         if (chr->logfd < 0) {
             return;
         }
     }
 
+    chr->reopen_on_cpr = (common && common->reopen_on_cpr);
+
     if (cc->open) {
         cc->open(chr, backend, be_opened, errp);
+        if (*errp) {
+            return;
+        }
+    }
+
+    /* Evaluate this after the open method sets the feature */
+    has_feature_cpr = qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_CPR);
+    chr->cpr_enabled = !chr->reopen_on_cpr && has_feature_cpr;
+
+    if (!chr->reopen_on_cpr && !has_feature_cpr) {
+        chr->cpr_blocker = NULL;
+        error_setg(&chr->cpr_blocker,
+                "chardev %s -> %s does not allow cpr. See reopen-on-cpr.",
+                chr->label, chr->filename);
+        migrate_add_blockers(&chr->cpr_blocker, errp, MIG_MODE_CPR_EXEC, -1);
+
+    } else if (chr->cpr_enabled && has_logfile) {
+        cpr_resave_fd(fdname, 0, chr->logfd);
     }
 }
 
@@ -297,11 +328,16 @@ 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) {
+        g_autofree char *fdname = g_strdup_printf("%s_log", chr->label);
+        if (chr->cpr_enabled) {
+            cpr_delete_fd(fdname, 0);
+        }
         close(chr->logfd);
     }
+    migrate_del_blocker(&chr->cpr_blocker);
+    g_free(chr->filename);
+    g_free(chr->label);
     qemu_mutex_destroy(&chr->chr_write_lock);
 }
 
@@ -501,6 +537,8 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
 
     backend->has_logappend = true;
     backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
+
+    backend->reopen_on_cpr = qemu_opt_get_bool(opts, "reopen-on-cpr", false);
 }
 
 static const ChardevClass *char_get_class(const char *driver, Error **errp)
@@ -942,6 +980,9 @@ QemuOptsList qemu_chardev_opts = {
         },{
             .name = "abstract",
             .type = QEMU_OPT_BOOL,
+        },{
+            .name = "reopen-on-cpr",
+            .type = QEMU_OPT_BOOL,
 #endif
         },
         { /* end of list */ }
include/chardev/char.h +5 -0
index a319b5f..bbf2560 100644
@@ -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;
@@ -67,6 +69,9 @@ struct Chardev {
     int be_open;
     /* used to coordinate the chardev-change special-case: */
     bool handover_yank_instance;
+    bool reopen_on_cpr;
+    bool cpr_enabled;
+    Error *cpr_blocker;
     GSource *gsource;
     GMainContext *gcontext;
     DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
qapi/char.json +6 -1
index 923dc50..0c3558e 100644
@@ -204,12 +204,17 @@
 # @logfile: The name of a logfile to save output
 # @logappend: true to append instead of truncate
 #             (default to false to truncate)
+# @reopen-on-cpr: if true, close device's fd on cpr-save and reopen it after
+#                 cpr-exec. Set this to allow CPR on a device that does not
+#                 support QEMU_CHAR_FEATURE_CPR. defaults to false.
+#                 since 7.1.
 #
 # Since: 2.6
 ##
 { 'struct': 'ChardevCommon',
   'data': { '*logfile': 'str',
-            '*logappend': 'bool' } }
+            '*logappend': 'bool',
+            '*reopen-on-cpr': 'bool' } }
 
 ##
 # @ChardevFile:
qemu-options.hx +22 -4
index 3ed9270..ea79afa 100644
@@ -3295,43 +3295,57 @@ 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"
+    "-chardev null,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off][,reopen-on-cpr=on|off]\n"
     "-chardev socket,id=id[,host=host],port=port[,to=to][,ipv4=on|off][,ipv6=on|off][,nodelay=on|off]\n"
     "         [,server=on|off][,wait=on|off][,telnet=on|off][,websocket=on|off][,reconnect=seconds][,mux=on|off]\n"
-    "         [,logfile=PATH][,logappend=on|off][,tls-creds=ID][,tls-authz=ID] (tcp)\n"
+    "         [,logfile=PATH][,logappend=on|off][,tls-creds=ID][,tls-authz=ID][,reopen-on-cpr=on|off] (tcp)\n"
     "-chardev socket,id=id,path=path[,server=on|off][,wait=on|off][,telnet=on|off][,websocket=on|off][,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][,reopen-on-cpr=on|off] (unix)\n"
     "-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr]\n"
     "         [,localport=localport][,ipv4=on|off][,ipv6=on|off][,mux=on|off]\n"
-    "         [,logfile=PATH][,logappend=on|off]\n"
+    "         [,logfile=PATH][,logappend=on|off][,reopen-on-cpr=on|off]\n"
     "-chardev msmouse,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-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"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev ringbuf,id=id[,size=size][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev file,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev pipe,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
 #ifdef _WIN32
     "-chardev console,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
     "-chardev serial,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
 #else
     "-chardev pty,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev stdio,id=id[,mux=on|off][,signal=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
 #endif
 #ifdef CONFIG_BRLAPI
     "-chardev braille,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-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"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev tty,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-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"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev parport,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
 #endif
 #if defined(CONFIG_SPICE)
     "-chardev spicevmc,id=id,name=name[,debug=debug][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
     "-chardev spiceport,id=id,name=name[,debug=debug][,logfile=PATH][,logappend=on|off]\n"
+    "         [,reopen-on-cpr=on|off]\n"
 #endif
     , QEMU_ARCH_ALL
 )
@@ -3406,6 +3420,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 ``reopen-on-cpr`` option.  If on, the
+    devices's descriptor is closed during cpr save, 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
#42 lore raw

PATCH v9 37/46 chardev: cpr for simple devices

In reply to Steve Sistare

Set QEMU_CHAR_FEATURE_CPR for devices that trivially support cpr-exec. char-stdio is slightly less trivial. Allow the gdb server by closing it on exec.

---
 MAINTAINERS           |  1 +
 chardev/char-mux.c    |  1 +
 chardev/char-null.c   |  1 +
 chardev/char-serial.c |  1 +
 chardev/char-stdio.c  | 31 +++++++++++++++++++++++++++++++
 gdbstub.c             |  1 +
 stubs/meson.build     |  1 +
 stubs/migration.c     | 33 +++++++++++++++++++++++++++++++++
 8 files changed, 70 insertions(+)
 create mode 100644 stubs/migration.c
MAINTAINERS +1 -0
index 3af099a..b93b0bb 100644
@@ -3038,6 +3038,7 @@ F: tests/qtest/migration-test.c
 F: docs/devel/migration.rst
 F: qapi/migration.json
 F: tests/migration/
+F: stubs/migration.c
 
 D-Bus
 M: Marc-André Lureau <[email protected]>
chardev/char-mux.c +1 -0
index ee2d47b..d47fa31 100644
@@ -337,6 +337,7 @@ static void qemu_chr_open_mux(Chardev *chr,
      */
     *be_opened = muxes_opened;
     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
index 1c6a290..02acaff 100644
@@ -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
index 4b0b83d..7aa2042 100644
@@ -277,6 +277,7 @@ static void qmp_chardev_open_serial(Chardev *chr,
     }
     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 +31 -0
index 3c64867..7a96bdc 100644
@@ -27,6 +27,7 @@
 #include "qemu/option.h"
 #include "qemu/sockets.h"
 #include "qapi/error.h"
+#include "migration/misc.h"
 #include "chardev/char.h"
 
 #ifdef _WIN32
@@ -40,19 +41,46 @@
 #ifndef _WIN32
 /* init terminal so that we can grab keys */
 static struct termios oldtty;
+static struct termios newtty;
 static int old_fd0_flags;
+static int new_fd0_flags;
 static bool stdio_in_use;
 static bool stdio_allow_signal;
 static bool stdio_echo_state;
+static Notifier cpr_notifier;
 
 static void term_exit(void)
 {
     if (stdio_in_use) {
+        tcgetattr(0, &newtty);
+        new_fd0_flags = fcntl(0, F_GETFL);
+
         tcsetattr(0, TCSANOW, &oldtty);
         fcntl(0, F_SETFL, old_fd0_flags);
     }
 }
 
+static void term_reenter(void)
+{
+    if (stdio_in_use) {
+        tcsetattr(0, TCSANOW, &newtty);
+        fcntl(0, F_SETFL, new_fd0_flags);
+    }
+}
+
+static void term_cpr_exec_notifier(Notifier *notifier, void *data)
+{
+    MigrationState *s = data;
+
+    if (migrate_mode_of(s) == MIG_MODE_CPR_EXEC) {
+        if (migration_has_finished(s)) {
+            term_exit();
+        } else if (migration_has_failed(s)) {
+            term_reenter();
+        }
+    }
+}
+
 static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)
 {
     struct termios tty;
@@ -117,6 +145,8 @@ static void qemu_chr_open_stdio(Chardev *chr,
 
     stdio_allow_signal = !opts->has_signal || opts->signal;
     qemu_chr_set_echo_stdio(chr, false);
+    qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_CPR);
+    migration_add_notifier(&cpr_notifier, term_cpr_exec_notifier);
 }
 #endif
 
@@ -147,6 +177,7 @@ static void char_stdio_finalize(Object *obj)
 {
 #ifndef _WIN32
     term_exit();
+    migration_remove_notifier(&cpr_notifier);
 #endif
 }
 
gdbstub.c +1 -0
index cf869b1..08b3d80 100644
@@ -3616,6 +3616,7 @@ int gdbserver_start(const char *device)
         mon_chr = gdbserver_state.mon_chr;
         reset_gdbserver_state();
     }
+    mon_chr->reopen_on_cpr = true;
 
     create_processes(&gdbserver_state);
 
stubs/meson.build +1 -0
index 392b1b5..a1eda95 100644
@@ -27,6 +27,7 @@ if libaio.found()
   stub_ss.add(files('linux-aio.c'))
 endif
 stub_ss.add(files('migr-blocker.c'))
+stub_ss.add(files('migration.c'))
 stub_ss.add(files('module-opts.c'))
 stub_ss.add(files('monitor.c'))
 stub_ss.add(files('monitor-core.c'))
stubs/migration.c new file +33 -0
new file mode 100644
index 0000000..f2f79bd
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2021, 2022 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.
+ */
+
+#include "qemu/osdep.h"
+#include "migration/misc.h"
+
+void migration_add_notifier(Notifier *notify,
+                            void (*cb)(Notifier *notifier, void *data))
+{
+}
+
+void migration_remove_notifier(Notifier *notify)
+{
+}
+
+bool migration_has_finished(MigrationState *s)
+{
+    return false;
+}
+
+bool migration_has_failed(MigrationState *s)
+{
+    return false;
+}
+
+MigMode migrate_mode_of(MigrationState *s)
+{
+    return 0;
+}
Signature · 1 line
1.8.3.1
#43 lore raw

PATCH v9 38/46 chardev: cpr for pty

In reply to Steve Sistare

Save and restore pty descriptors across cpr-exec.

---
 chardev/char-pty.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
chardev/char-pty.c +15 -1
index 53f25c6..5707b13 100644
@@ -29,6 +29,7 @@
 #include "qemu/sockets.h"
 #include "qemu/error-report.h"
 #include "qemu/module.h"
+#include "migration/cpr-state.h"
 #include "qemu/qemu-print.h"
 
 #include "chardev/char-io.h"
@@ -190,6 +191,9 @@ static void char_pty_finalize(Object *obj)
     Chardev *chr = CHARDEV(obj);
     PtyChardev *s = PTY_CHARDEV(obj);
 
+    if (chr->cpr_enabled) {
+        cpr_delete_fd(chr->label, 0);
+    }
     pty_chr_state(chr, 0);
     object_unref(OBJECT(s->ioc));
     pty_chr_timer_cancel(s);
@@ -317,12 +321,20 @@ static void char_pty_open(Chardev *chr,
     char pty_name[PATH_MAX];
     char *name;
 
+    master_fd = cpr_find_fd(chr->label, 0);
+    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->cpr_enabled) {
+        cpr_save_fd(chr->label, 0, master_fd);
+    }
     close(slave_fd);
     if (!g_unix_set_fd_nonblocking(master_fd, true, NULL)) {
         error_setg_errno(errp, errno, "Failed to set FD nonblocking");
@@ -333,6 +345,8 @@ static void char_pty_open(Chardev *chr,
     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
#44 lore raw

PATCH v9 39/46 chardev: cpr for sockets

In reply to Steve Sistare

Save accepted socket fds before cpr-exec, and look for them after. Block cpr-exec if a socket enables the TLS or websocket option. Allow a monitor socket by closing it on exec.

---
 chardev/char-socket.c         | 48 +++++++++++++++++++++++++++++++++++++++++++
 include/chardev/char-socket.h |  1 +
 monitor/hmp.c                 |  3 +++
 monitor/qmp.c                 |  4 ++++
 4 files changed, 56 insertions(+)
chardev/char-socket.c +48 -0
index dc4e218..f93bae8 100644
@@ -26,6 +26,8 @@
 #include "chardev/char.h"
 #include "io/channel-socket.h"
 #include "io/channel-websock.h"
+#include "migration/blocker.h"
+#include "migration/cpr-state.h"
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "qemu/option.h"
@@ -33,6 +35,7 @@
 #include "qapi/clone-visitor.h"
 #include "qapi/qapi-visit-sockets.h"
 #include "qemu/yank.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 #include "chardev/char-socket.h"
@@ -358,6 +361,11 @@ static void tcp_chr_free_connection(Chardev *chr)
     SocketChardev *s = SOCKET_CHARDEV(chr);
     int i;
 
+    if (chr->cpr_enabled) {
+        cpr_delete_fd(chr->label, 0);
+    }
+    migrate_del_blocker(&s->cpr_blocker);
+
     if (s->read_msgfds_num) {
         for (i = 0; i < s->read_msgfds_num; i++) {
             close(s->read_msgfds[i]);
@@ -923,6 +931,10 @@ static void tcp_chr_accept(QIONetListener *listener,
                                QIO_CHANNEL(cioc));
     }
     tcp_chr_new_client(chr, cioc);
+
+    if (s->sioc && chr->cpr_enabled) {
+        cpr_resave_fd(chr->label, 0, s->sioc->fd);
+    }
 }
 
 
@@ -1178,6 +1190,27 @@ static gboolean socket_reconnect_timeout(gpointer opaque)
     return false;
 }
 
+static int load_char_socket_fd(Chardev *chr, Error **errp)
+{
+    ERRP_GUARD();
+    SocketChardev *sockchar = SOCKET_CHARDEV(chr);
+    QIOChannelSocket *sioc;
+    const char *label = chr->label;
+    int fd = cpr_find_fd(label, 0);
+
+    if (fd != -1) {
+        sockchar = SOCKET_CHARDEV(chr);
+        sioc = qio_channel_socket_new_fd(fd, errp);
+        if (sioc) {
+            tcp_chr_accept(sockchar->listener, sioc, chr);
+            object_unref(OBJECT(sioc));
+        } else {
+            error_prepend(errp, "could not restore socket for %s", label);
+            return -1;
+        }
+    }
+    return 0;
+}
 
 static int qmp_chardev_open_socket_server(Chardev *chr,
                                           bool is_telnet,
@@ -1388,6 +1421,19 @@ static void qmp_chardev_open_socket(Chardev *chr,
     }
     s->registered_yank = true;
 
+    if (!s->tls_creds && !s->is_websock) {
+        qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_CPR);
+    } else if (!chr->reopen_on_cpr) {
+        s->cpr_blocker = NULL;
+        error_setg(&s->cpr_blocker,
+                   "error: socket %s is not cpr capable due to %s option",
+                   chr->label, (s->tls_creds ? "TLS" : "websocket"));
+        if (migrate_add_blockers(&s->cpr_blocker, errp, MIG_MODE_CPR_EXEC,
+                                 -1)) {
+            return;
+        }
+    }
+
     /* be isn't opened until we get a connection */
     *be_opened = false;
 
@@ -1403,6 +1449,8 @@ static void qmp_chardev_open_socket(Chardev *chr,
             return;
         }
     }
+
+    load_char_socket_fd(chr, errp);
 }
 
 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
include/chardev/char-socket.h +1 -0
index 0708ca6..1c3abf7 100644
@@ -78,6 +78,7 @@ struct SocketChardev {
     bool connect_err_reported;
 
     QIOTask *connect_task;
+    Error *cpr_blocker;
 };
 typedef struct SocketChardev SocketChardev;
 
monitor/hmp.c +3 -0
index 15ca047..75e6739 100644
@@ -1501,4 +1501,7 @@ void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
     qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
                              monitor_event, NULL, &mon->common, NULL, true);
     monitor_list_append(&mon->common);
+
+    /* monitor cannot yet be preserved across cpr */
+    chr->reopen_on_cpr = true;
 }
monitor/qmp.c +4 -0
index 092c527..0b3aac6 100644
@@ -535,4 +535,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
                                  NULL, &mon->common, NULL, true);
         monitor_list_append(&mon->common);
     }
+
+    /* Monitor cannot yet be preserved across cpr */
+    chr->reopen_on_cpr = true;
+    chr->cpr_enabled = false;
 }
Signature · 1 line
1.8.3.1
#45 lore raw

PATCH v9 40/46 python/machine: QEMUMachine full_args

In reply to Steve Sistare

Provide full_args() to return all command-line arguments used to start a vm, some of which are not otherwise visible to QEMUMachine clients. This is needed by the cpr test, which must start a vm, then pass all qemu command-line arguments when setting the cpr-exec-args migration parameter.

---
 python/qemu/machine/machine.py | 5 +++++
 1 file changed, 5 insertions(+)
python/qemu/machine/machine.py +5 -0
index 37191f4..d05950e 100644
@@ -332,6 +332,11 @@ def args(self) -> List[str]:
         """Returns the list of arguments given to the QEMU binary."""
         return self._args
 
+    @property
+    def full_args(self) -> List[str]:
+        """Returns the full list of arguments used to launch QEMU."""
+        return list(self._qemu_full_args)
+
     def _pre_launch(self) -> None:
         if self._console_set:
             self._remove_files.append(self._console_address)
Signature · 1 line
1.8.3.1
Reviewed-by Acked-by on v9 40/46
#46 lore raw

Re: [PATCH V9 40/46] python/machine: QEMUMachine full_args

Quoted 28 lines · Steve Sistare

Provide full_args() to return all command-line arguments used to start a vm, some of which are not otherwise visible to QEMUMachine clients. This is needed by the cpr test, which must start a vm, then pass all qemu command-line arguments when setting the cpr-exec-args migration parameter.

---
 python/qemu/machine/machine.py | 5 +++++
 1 file changed, 5 insertions(+)
python/qemu/machine/machine.py +5 -1
index 37191f4..d05950e 100644
@@ -332,6 +332,11 @@ def args(self) -> List[str]:
         """Returns the list of arguments given to the QEMU binary."""
         return self._args
 
+    @property
+    def full_args(self) -> List[str]:
+        """Returns the full list of arguments used to launch QEMU."""
+        return list(self._qemu_full_args)
+
     def _pre_launch(self) -> None:
         if self._console_set:
             self._remove_files.append(self._console_address)
--
Quoted 2 lines

1.8.3.1

#47 lore raw

PATCH v9 41/46 python/machine: QEMUMachine reopen_qmp_connection

In reply to Steve Sistare

Provide reopen_qmp_connection() to reopen a closed monitor connection. This is needed by cpr, because qemu exec closes the monitor socket.

---
 python/qemu/machine/machine.py | 9 +++++++++
 1 file changed, 9 insertions(+)
python/qemu/machine/machine.py +9 -0
index d05950e..60b934d 100644
@@ -491,6 +491,15 @@ def _close_qmp_connection(self) -> None:
         finally:
             self._qmp_connection = None
 
+    def reopen_qmp_connection(self):
+        self._close_qmp_connection()
+        self._qmp_connection = QEMUMonitorProtocol(
+            self._monitor_address,
+            server=True,
+            nickname=self._name
+        )
+        self._qmp.accept(self._qmp_timer)
+
     def _early_cleanup(self) -> None:
         """
         Perform any cleanup that needs to happen before the VM exits.
Signature · 1 line
1.8.3.1
Reviewed-by on v9 41/46
#48 lore raw

Re: [PATCH V9 41/46] python/machine: QEMUMachine reopen_qmp_connection

Quoted 14 lines · Steve Sistare

Provide reopen_qmp_connection() to reopen a closed monitor connection. This is needed by cpr, because qemu exec closes the monitor socket.

---
 python/qemu/machine/machine.py | 9 +++++++++
 1 file changed, 9 insertions(+)
python/qemu/machine/machine.py +0 -0
index d05950e..60b934d 100644

python/qemu/machine/machine.py def _close_qmp_connection(self) -> None: · line 494

Quoted 4 lines
python/qemu/machine/machine.py +1 -0
@@ -491,6 +491,15 @@ def _close_qmp_connection(self) -> None:
         finally:
             self._qmp_connection = None
 
+    def reopen_qmp_connection(self):

def reopen_qmp_connection(self) -> None:
"""Close and re-open the QMP connection."""
...

Quoted 12 lines
python/qemu/machine/machine.py +8 -1
+        self._close_qmp_connection()
+        self._qmp_connection = QEMUMonitorProtocol(
+            self._monitor_address,
+            server=True,
+            nickname=self._name
+        )
+        self._qmp.accept(self._qmp_timer)
+
     def _early_cleanup(self) -> None:
         """
         Perform any cleanup that needs to happen before the VM exits.
--
Quoted 2 lines

1.8.3.1

With applied fixup:

#49 lore raw

Re: [PATCH V9 41/46] python/machine: QEMUMachine reopen_qmp_connection

Quoted 19 lines · John Snow
Quoted 17 lines · Steve Sistare

Provide reopen_qmp_connection() to reopen a closed monitor connection. This is needed by cpr, because qemu exec closes the monitor socket.

---
 python/qemu/machine/machine.py | 9 +++++++++
 1 file changed, 9 insertions(+)
python/qemu/machine/machine.py +0 -0
index d05950e..60b934d 100644
@@ -491,6 +491,15 @@ def _close_qmp_connection(self) -> None:
         finally:
             self._qmp_connection = None
 
Quoted 4 lines

python/qemu/machine/machine.py def _close_qmp_connection(self) -> None: · line 494

Quoted 1 line
python/qemu/machine/machine.py +1 -0
+    def reopen_qmp_connection(self):

def reopen_qmp_connection(self) -> None:
"""Close and re-open the QMP connection."""

Will do. Thanks very much John for the fast review.

- Steve

Quoted 21 lines
    ...
Quoted 12 lines
python/qemu/machine/machine.py +8 -1
+        self._close_qmp_connection()
+        self._qmp_connection = QEMUMonitorProtocol(
+            self._monitor_address,
+            server=True,
+            nickname=self._name
+        )
+        self._qmp.accept(self._qmp_timer)
+
     def _early_cleanup(self) -> None:
         """
         Perform any cleanup that needs to happen before the VM exits.
--
Quoted 2 lines

1.8.3.1

With applied fixup:

#50 lore raw

PATCH v9 42/46 tests/avocado: add cpr regression test

In reply to Steve Sistare
---
 MAINTAINERS          |   1 +
 tests/avocado/cpr.py | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 177 insertions(+)
 create mode 100644 tests/avocado/cpr.py
MAINTAINERS +1 -0
index b93b0bb..adc1218 100644
@@ -3172,6 +3172,7 @@ F: stubs/cpr-state.c
 F: include/migration/cpr.h
 F: migration/cpr.c
 F: hw/vfio/cpr.c
+F: tests/avocado/cpr.py
 
 Record/replay
 M: Pavel Dovgalyuk <[email protected]>
tests/avocado/cpr.py new file +176 -0

176 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#51 lore raw

Re: [PATCH V9 42/46] tests/avocado: add cpr regression test

Hi Steve,

Quoted 12 lines · Steve Sistare
---
  MAINTAINERS          |   1 +
  tests/avocado/cpr.py | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 177 insertions(+)

create mode 100644 tests/avocado/cpr.py

MAINTAINERS +0 -0
index b93b0bb..adc1218 100644

MAINTAINERS F: stubs/cpr-state.c · line 3175

Quoted 4 lines
MAINTAINERS +1 -0
@@ -3172,6 +3172,7 @@ F: stubs/cpr-state.c
  F: include/migration/cpr.h
  F: migration/cpr.c
  F: hw/vfio/cpr.c
+F: tests/avocado/cpr.py

Maybe rename migration_cpr.py.

tests/avocado/cpr.py line 1

Quoted 10 lines
MAINTAINERS +0 -0
  Record/replay
  M: Pavel Dovgalyuk <[email protected]>
tests/avocado/cpr.py new file +1 -0
new file mode 100644
index 0000000..11e1376
@@ -0,0 +1,176 @@
+# cpr test

"migration CPR test"

tests/avocado/cpr.py line 6

Quoted 5 lines
tests/avocado/cpr.py new file +5 -0
+
+# Copyright (c) 2021, 2022 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.

Preferably replace/complement with SPDX tag.

tests/avocado/cpr.py line 12

Quoted 6 lines
tests/avocado/cpr.py new file +6 -0
+
+import tempfile
+from avocado_qemu import QemuSystemTest
+from avocado.utils import wait
+
+class Cpr(QemuSystemTest):

MigrationCpr

tests/avocado/cpr.py line 14

Quoted 2 lines
tests/avocado/cpr.py new file +2 -0
+    """
+    :avocado: tags=cpr

Also:
:avocado: tags=migration

or even:

:avocado: tags=migration
:avocado: tags=migration:cpr

tests/avocado/cpr.py line 18

Quoted 4 lines
tests/avocado/cpr.py new file +4 -0
+    """
+
+    timeout = 5
+    fast_timeout = 1

These methods ...

tests/avocado/cpr.py line 22

Quoted 3 lines
tests/avocado/cpr.py new file +3 -0
+    @staticmethod
+    def has_status(vm, status, command):
+        return vm.command(command)['status'] in status

s/status/status_set/

tests/avocado/cpr.py line 28

Quoted 6 lines
tests/avocado/cpr.py new file +6 -0
+
+    def wait_for_status(self, vm, status, command):
+        wait.wait_for(self.has_status,
+                      timeout=self.timeout,
+                      step=0.1,
+                      args=(vm,status,command,))

s/status/status_set/

tests/avocado/cpr.py line 31

Quoted 3 lines
tests/avocado/cpr.py new file +3 -0
+
+    def wait_for_runstate(self, vm, status):
+        self.wait_for_status(vm, status, 'query-status')

s/status/status_set/

tests/avocado/cpr.py line 34

Quoted 3 lines
tests/avocado/cpr.py new file +3 -0
+
+    def wait_for_migration(self, vm, status):
+        self.wait_for_status(vm, status, 'query-migrate')

s/status/status_set/

tests/avocado/cpr.py line 41

Quoted 7 lines
tests/avocado/cpr.py new file +7 -0
+
+    def run_and_fail(self, vm, msg):
+        # Qemu will fail fast, so disable monitor to avoid timeout in accept
+        vm.set_qmp_monitor(False)
+        vm.launch()
+        vm.wait(self.timeout)
+        self.assertRegex(vm.get_log(), msg)

... are generic so could go to QemuSystemTest.

The rest LGTM!

Regards,

Phil.

#52 lore raw

PATCH v9 43/46 vl: start on wakeup request

In reply to Steve Sistare

If qemu starts and loads a VM in the suspended state, then a later wakeup request will set the state to running, which is not sufficient to initialize the vm, as vm_start was never called during this invocation of qemu. See qemu_system_wakeup_request().

Define the start_on_wakeup_requested() hook to cause vm_start() to be called when processing the wakeup request. This will be called in a subsequent migration patch.

---
 include/sysemu/runstate.h |  1 +
 softmmu/runstate.c        | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/sysemu/runstate.h +1 -0
index a5f0738..cfcdbf5 100644
@@ -57,6 +57,7 @@ void qemu_system_reset_request(ShutdownCause reason);
 void qemu_system_suspend_request(void);
 void qemu_register_suspend_notifier(Notifier *notifier);
 bool qemu_wakeup_suspend_enabled(void);
+void qemu_system_start_on_wakeup_request(void);
 void qemu_system_wakeup_request(WakeupReason reason, Error **errp);
 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled);
 void qemu_register_wakeup_notifier(Notifier *notifier);
softmmu/runstate.c +14 -1
index fb86740..c7db5ac 100644
@@ -338,6 +338,7 @@ void vm_state_notify(bool running, RunState state)
     }
 }
 
+static bool start_on_wakeup_requested;
 static ShutdownCause reset_requested;
 static ShutdownCause shutdown_requested;
 static int shutdown_signal;
@@ -571,6 +572,11 @@ void qemu_register_suspend_notifier(Notifier *notifier)
     notifier_list_add(&suspend_notifiers, notifier);
 }
 
+void qemu_system_start_on_wakeup_request(void)
+{
+    start_on_wakeup_requested = true;
+}
+
 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
 {
     trace_system_wakeup_request(reason);
@@ -583,7 +589,14 @@ void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
     if (!(wakeup_reason_mask & (1 << reason))) {
         return;
     }
-    runstate_set(RUN_STATE_RUNNING);
+
+    if (start_on_wakeup_requested) {
+        start_on_wakeup_requested = false;
+        vm_start();
+    } else {
+        runstate_set(RUN_STATE_RUNNING);
+    }
+
     wakeup_reason = reason;
     qemu_notify_event();
 }
Signature · 1 line
1.8.3.1
#53 lore raw

PATCH v9 44/46 migration: fix suspended runstate

In reply to Steve Sistare

Migration of a guest in the suspended state is broken. The incoming migration code automatically tries to wake the guest, which IMO is wrong -- the guest should end migration in the same state it started. Further, the wakeup attempt merely sets state to running but does not actually start the guest, as vm_start was never called during this invocation of qemu.

To fix, leave the guest in the suspended state, but call qemu_system_start_on_wakeup_request() so the guest is properly resumed when the client sends a system_wakeup command.

---
 migration/migration.c | 11 ++++-------
 softmmu/runstate.c    |  1 +
 2 files changed, 5 insertions(+), 7 deletions(-)
migration/migration.c +4 -7
index e0fc2b8..45eebea 100644
@@ -568,6 +568,10 @@ static void process_incoming_migration_bh(void *opaque)
         vm_start();
     } else {
         runstate_set(global_state_get_runstate());
+        if (runstate_check(RUN_STATE_SUSPENDED)) {
+            /* Force vm_start to be called later. */
+            qemu_system_start_on_wakeup_request();
+        }
     }
     /*
      * This must happen after any state changes since as soon as an external
@@ -3273,7 +3277,6 @@ static int postcopy_start(MigrationState *ms)
     qemu_mutex_lock_iothread();
     trace_postcopy_start_set_run();
 
-    qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
     global_state_store();
     ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
     if (ret < 0) {
@@ -3484,7 +3487,6 @@ static void migration_completion(MigrationState *s)
     if (s->state == MIGRATION_STATUS_ACTIVE) {
         qemu_mutex_lock_iothread();
         s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
-        qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
         s->vm_was_running = runstate_is_running();
         ret = global_state_store();
 
@@ -4285,11 +4287,6 @@ static void *bg_migration_thread(void *opaque)
 
     qemu_mutex_lock_iothread();
 
-    /*
-     * If VM is currently in suspended state, then, to make a valid runstate
-     * transition in vm_stop_force_state() we need to wakeup it up.
-     */
-    qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
     s->vm_was_running = runstate_is_running();
 
     if (global_state_store()) {
softmmu/runstate.c +1 -0
index c7db5ac..6ba0a4e 100644
@@ -154,6 +154,7 @@ static const RunStateTransition runstate_transitions_def[] = {
     { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
     { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
     { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
+    { RUN_STATE_SUSPENDED, RUN_STATE_PAUSED },
     { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
     { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
 
Signature · 1 line
1.8.3.1
#54 lore raw

PATCH v9 45/46 migration: notifier error reporting

In reply to Steve Sistare

Provide migration_notifier_set_error(), which allows migration notifiers to return an error message indicating they have failed, which halts the migration. The error message is visible in the 'info migrate' command.

No functional change until calls to migration_notifier_set_error are added.

---
 include/migration/misc.h |  3 ++-
 migration/migration.c    | 31 +++++++++++++++++++++++++++----
 2 files changed, 29 insertions(+), 5 deletions(-)
include/migration/misc.h +2 -1
index d4c2d7d..e5d8c81 100644
@@ -63,7 +63,8 @@ bool migration_is_active(MigrationState *);
 void migration_add_notifier(Notifier *notify,
                             void (*func)(Notifier *notifier, void *data));
 void migration_remove_notifier(Notifier *notify);
-void migration_call_notifiers(MigrationState *s);
+int migration_call_notifiers(MigrationState *s);
+void migration_notifier_set_error(MigrationState *s, Error *err);
 bool migration_in_setup(MigrationState *);
 bool migration_has_finished(MigrationState *);
 bool migration_has_failed(MigrationState *);
migration/migration.c +27 -4
index 45eebea..d477b58 100644
@@ -1924,6 +1924,8 @@ static void block_cleanup_parameters(MigrationState *s)
 
 static void migrate_fd_cleanup(MigrationState *s)
 {
+    bool already_failed;
+
     qemu_bh_delete(s->cleanup_bh);
     s->cleanup_bh = NULL;
 
@@ -1973,7 +1975,15 @@ static void migrate_fd_cleanup(MigrationState *s)
         /* It is used on info migrate.  We can't free it */
         error_report_err(error_copy(s->error));
     }
-    migration_call_notifiers(s);
+
+    already_failed = migration_has_failed(s);
+    if (migration_call_notifiers(s)) {
+        if (!already_failed) {
+            migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
+            /* Notify again to recover from this late failure. */
+            migration_call_notifiers(s);
+        }
+    }
     block_cleanup_parameters(s);
     cpr_exec();
     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
@@ -2084,9 +2094,16 @@ void migration_remove_notifier(Notifier *notify)
     }
 }
 
-void migration_call_notifiers(MigrationState *s)
+int migration_call_notifiers(MigrationState *s)
 {
     notifier_list_notify(&migration_state_notifiers, s);
+    return (s->error != NULL);
+}
+
+void migration_notifier_set_error(MigrationState *s, Error *err)
+{
+    migrate_set_error(s, err);
+    error_report_err(err);
 }
 
 bool migration_in_setup(MigrationState *s)
@@ -3381,7 +3398,9 @@ static int postcopy_start(MigrationState *ms)
      * spice needs to trigger a transition now
      */
     ms->postcopy_after_devices = true;
-    migration_call_notifiers(ms);
+    if (migration_call_notifiers(ms)) {
+        goto fail;
+    }
 
     ms->downtime =  qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
 
@@ -4412,7 +4431,11 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
         rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
 
         /* Notify before starting migration thread */
-        migration_call_notifiers(s);
+        if (migration_call_notifiers(s)) {
+            migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
+            migrate_fd_cleanup(s);
+            return;
+        }
     }
 
     qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
Signature · 1 line
1.8.3.1
#55 lore raw

PATCH v9 46/46 vfio: allow cpr-reboot migration if suspended

In reply to Steve Sistare

The cpr-reboot mode supports vfio devices if the caller first suspends the guest, 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.

Relax the vfio blocker, and add a notifier that verifies the guest is suspended. Skip dirty page tracking to avoid ioctl errors.

---
 hw/vfio/common.c              |  8 ++++++--
 hw/vfio/cpr.c                 | 21 +++++++++++++++++++++
 hw/vfio/migration.c           |  3 ++-
 include/hw/vfio/vfio-common.h |  1 +
 4 files changed, 30 insertions(+), 3 deletions(-)
hw/vfio/common.c +6 -2
index de821e1..5b54cbe 100644
@@ -1355,14 +1355,18 @@ static void vfio_listener_log_global_start(MemoryListener *listener)
 {
     VFIOContainer *container = container_of(listener, VFIOContainer, listener);
 
-    vfio_set_dirty_page_tracking(container, true);
+    if (!runstate_check(RUN_STATE_SUSPENDED)) {
+        vfio_set_dirty_page_tracking(container, true);
+    }
 }
 
 static void vfio_listener_log_global_stop(MemoryListener *listener)
 {
     VFIOContainer *container = container_of(listener, VFIOContainer, listener);
 
-    vfio_set_dirty_page_tracking(container, false);
+    if (!runstate_check(RUN_STATE_SUSPENDED)) {
+        vfio_set_dirty_page_tracking(container, false);
+    }
 }
 
 static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
hw/vfio/cpr.c +21 -0
index 1f682cb..ef8827c 100644
@@ -10,6 +10,7 @@
 #include <linux/vfio.h>
 #include "hw/vfio/vfio-common.h"
 #include "sysemu/kvm.h"
+#include "sysemu/runstate.h"
 #include "qapi/error.h"
 #include "migration/blocker.h"
 #include "migration/migration.h"
@@ -132,8 +133,26 @@ static void vfio_cpr_fail_notifier(Notifier *notifier, void *data)
     }
 }
 
+static void vfio_cpr_reboot_notifier(Notifier *notifier, void *data)
+{
+    MigrationState *s = data;
+
+    if (migrate_mode_of(s) == MIG_MODE_CPR_REBOOT &&
+        !migration_has_failed(s) &&
+        !runstate_check(RUN_STATE_SUSPENDED)) {
+
+        Error *err = NULL;
+        error_setg(&err, "VFIO device only supports cpr-reboot for "
+                         "runstate suspended");
+        migration_notifier_set_error(s, err);
+    }
+}
+
 int vfio_cpr_register_container(VFIOContainer *container, Error **errp)
 {
+    migration_add_notifier(&container->cpr_reboot_notifier,
+                           vfio_cpr_reboot_notifier);
+
     container->cpr_blocker = NULL;
     if (!vfio_is_cpr_capable(container, &container->cpr_blocker)) {
         return migrate_add_blockers(&container->cpr_blocker, errp,
@@ -148,6 +167,8 @@ int vfio_cpr_register_container(VFIOContainer *container, Error **errp)
 
 void vfio_cpr_unregister_container(VFIOContainer *container)
 {
+    migration_remove_notifier(&container->cpr_reboot_notifier);
+
     migrate_del_blocker(&container->cpr_blocker);
 
     vmstate_unregister(NULL, &vfio_container_vmstate, container);
hw/vfio/migration.c +2 -1
index 6b0d401..3919941 100644
@@ -890,7 +890,8 @@ add_blocker:
                "VFIO device doesn't support migration");
     g_free(info);
 
-    return migrate_add_blocker(&vbasedev->migration_blocker, errp);
+    return migrate_add_blockers(&vbasedev->migration_blocker, errp,
+                                MIG_MODE_NORMAL, -1);
 }
 
 void vfio_migration_finalize(VFIODevice *vbasedev)
include/hw/vfio/vfio-common.h +1 -0
index b682645..243376b 100644
@@ -83,6 +83,7 @@ typedef struct VFIOContainer {
     MemoryListener prereg_listener;
     Notifier cpr_notifier;
     Error *cpr_blocker;
+    Notifier cpr_reboot_notifier;
     unsigned iommu_type;
     Error *error;
     bool initialized;
Signature · 1 line
1.8.3.1
#56 lore raw
In reply to Steve Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://lore.kernel.org/qemu-devel/[email protected]/

- Steve

Quoted 303 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in
https://lore.kernel.org/lkml/[email protected]
but this is not enforced. The restarted qemu arguments must match those used to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in
https://lore.kernel.org/kvm/[email protected]
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1                                        | window 2
                                                |
# qemu-system-$arch ...                         |
  -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec      |
(qemu) migrate_set_parameter cpr-exec-args      |
  qemu-system-$arch ... -incoming defer         |
(qemu) migrate -d file:/tmp/qemu.sav            |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1                                        | window 2
                                                |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
  -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -d file:/tmp/qemu.sav            |
(qemu) quit                                     |
                                                |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
  -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

 MAINTAINERS                         |  14 ++
 accel/xen/xen-all.c                 |   3 +
 backends/hostmem-epc.c              |  18 +-
 backends/hostmem-file.c             |   1 +
 backends/hostmem-memfd.c            |  22 ++-
 backends/tpm/tpm_emulator.c         |  11 +-
 block/parallels.c                   |   7 +-
 block/qcow.c                        |   7 +-
 block/vdi.c                         |   7 +-
 block/vhdx.c                        |   7 +-
 block/vmdk.c                        |   7 +-
 block/vpc.c                         |   7 +-
 block/vvfat.c                       |   7 +-
 chardev/char-mux.c                  |   1 +
 chardev/char-null.c                 |   1 +
 chardev/char-pty.c                  |  16 +-
 chardev/char-serial.c               |   1 +
 chardev/char-socket.c               |  48 +++++
 chardev/char-stdio.c                |  31 +++
 chardev/char.c                      |  49 ++++-
 dump/dump.c                         |   4 +-
 gdbstub.c                           |   1 +
 hmp-commands.hx                     |   2 +-
 hw/9pfs/9p.c                        |  11 +-
 hw/core/qdev-properties-system.c    |  12 ++
 hw/core/qdev-properties.c           |  44 +++++
 hw/display/virtio-gpu-base.c        |   8 +-
 hw/intc/arm_gic_kvm.c               |   3 +-
 hw/intc/arm_gicv3_its_kvm.c         |   3 +-
 hw/intc/arm_gicv3_kvm.c             |   3 +-
 hw/misc/ivshmem.c                   |   8 +-
 hw/net/virtio-net.c                 |  10 +-
 hw/pci/msix.c                       |   2 +-
 hw/pci/pci.c                        |  12 ++
 hw/ppc/pef.c                        |   2 +-
 hw/ppc/spapr.c                      |   2 +-
 hw/ppc/spapr_events.c               |   2 +-
 hw/ppc/spapr_rtas.c                 |   2 +-
 hw/remote/proxy.c                   |   7 +-
 hw/s390x/s390-virtio-ccw.c          |   9 +-
 hw/scsi/vhost-scsi.c                |   9 +-
 hw/vfio/common.c                    | 235 +++++++++++++++++++----
 hw/vfio/cpr.c                       | 177 ++++++++++++++++++
 hw/vfio/meson.build                 |   1 +
 hw/vfio/migration.c                 |  23 +--
 hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
 hw/vfio/trace-events                |   1 +
 hw/virtio/vhost-vdpa.c              |   6 +-
 hw/virtio/vhost.c                   |  32 +++-
 include/chardev/char-socket.h       |   1 +
 include/chardev/char.h              |   5 +
 include/exec/memory.h               |  48 +++++
 include/exec/ram_addr.h             |   1 +
 include/exec/ramblock.h             |   1 +
 include/hw/pci/msix.h               |   1 +
 include/hw/qdev-properties-system.h |   4 +
 include/hw/qdev-properties.h        |   3 +
 include/hw/vfio/vfio-common.h       |  12 ++
 include/hw/virtio/vhost.h           |   1 +
 include/migration/blocker.h         |  69 ++++++-
 include/migration/cpr-state.h       |  30 +++
 include/migration/cpr.h             |  20 ++
 include/migration/misc.h            |  13 +-
 include/migration/vmstate.h         |   2 +
 include/qapi/util.h                 |  28 +++
 include/qemu/osdep.h                |   9 +
 include/sysemu/runstate.h           |   2 +
 migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
 migration/cpr.c                     |  85 +++++++++
 migration/file.c                    |  62 ++++++
 migration/file.h                    |  14 ++
 migration/meson.build               |   3 +
 migration/migration.c               | 268 +++++++++++++++++++++++---
 migration/ram.c                     |  24 ++-
 migration/target.c                  |   1 +
 migration/trace-events              |  12 ++
 monitor/hmp-cmds.c                  |  59 +++---
 monitor/hmp.c                       |   3 +
 monitor/qmp.c                       |   4 +
 python/qemu/machine/machine.py      |  14 ++
 qapi/char.json                      |   7 +-
 qapi/migration.json                 |  68 ++++++-
 qapi/qapi-util.c                    |  37 ++++
 qemu-options.hx                     |  50 ++++-
 replay/replay.c                     |   4 +
 softmmu/memory.c                    |  31 ++-
 softmmu/physmem.c                   | 100 +++++++++-
 softmmu/runstate.c                  |  42 ++++-
 softmmu/vl.c                        |  10 +
 stubs/cpr-state.c                   |  26 +++
 stubs/meson.build                   |   2 +
 stubs/migr-blocker.c                |   9 +-
 stubs/migration.c                   |  33 ++++
 target/i386/kvm/kvm.c               |   8 +-
 target/i386/nvmm/nvmm-all.c         |   4 +-
 target/i386/sev.c                   |   2 +-
 target/i386/whpx/whpx-all.c         |   3 +-
 tests/avocado/cpr.py                | 176 ++++++++++++++++++
 tests/unit/meson.build              |   1 +
 tests/unit/test-strlist.c           |  81 ++++++++
 trace-events                        |   1 +
 ui/spice-core.c                     |   5 +-
 ui/vdagent.c                        |   5 +-
 util/oslib-posix.c                  |   9 +
 util/oslib-win32.c                  |   4 +
 105 files changed, 2781 insertions(+), 330 deletions(-)
 create mode 100644 hw/vfio/cpr.c
 create mode 100644 include/migration/cpr-state.h
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr-state.c
 create mode 100644 migration/cpr.c
 create mode 100644 migration/file.c
 create mode 100644 migration/file.h
 create mode 100644 stubs/cpr-state.c
 create mode 100644 stubs/migration.c
 create mode 100644 tests/avocado/cpr.py
 create mode 100644 tests/unit/test-strlist.c
#57 lore raw

To make forward progress on this series and reduce its size, I will be posting those of its patches that can be independently integrated and have some value on their own, to a reduced distribution of reviewers for each. This is what I plan to break out:

migration: fix populate_vfio_info

memory: RAM_NAMED_FILE flag

memory: flat section iterator

oslib: qemu_clear_cloexec

migration: simplify blockers

migration: simplify notifiers

python/machine: QEMUMachine full_args

python/machine: QEMUMachine reopen_qmp_connection

qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests

- Steve

Quoted 320 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://lore.kernel.org/qemu-devel/[email protected]/

- Steve

Quoted 303 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in
https://lore.kernel.org/lkml/[email protected]
but this is not enforced. The restarted qemu arguments must match those used to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in
https://lore.kernel.org/kvm/[email protected]
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1                                        | window 2
                                                |
# qemu-system-$arch ...                         |
  -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec      |
(qemu) migrate_set_parameter cpr-exec-args      |
  qemu-system-$arch ... -incoming defer         |
(qemu) migrate -d file:/tmp/qemu.sav            |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1                                        | window 2
                                                |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
  -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -d file:/tmp/qemu.sav            |
(qemu) quit                                     |
                                                |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
  -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

 MAINTAINERS                         |  14 ++
 accel/xen/xen-all.c                 |   3 +
 backends/hostmem-epc.c              |  18 +-
 backends/hostmem-file.c             |   1 +
 backends/hostmem-memfd.c            |  22 ++-
 backends/tpm/tpm_emulator.c         |  11 +-
 block/parallels.c                   |   7 +-
 block/qcow.c                        |   7 +-
 block/vdi.c                         |   7 +-
 block/vhdx.c                        |   7 +-
 block/vmdk.c                        |   7 +-
 block/vpc.c                         |   7 +-
 block/vvfat.c                       |   7 +-
 chardev/char-mux.c                  |   1 +
 chardev/char-null.c                 |   1 +
 chardev/char-pty.c                  |  16 +-
 chardev/char-serial.c               |   1 +
 chardev/char-socket.c               |  48 +++++
 chardev/char-stdio.c                |  31 +++
 chardev/char.c                      |  49 ++++-
 dump/dump.c                         |   4 +-
 gdbstub.c                           |   1 +
 hmp-commands.hx                     |   2 +-
 hw/9pfs/9p.c                        |  11 +-
 hw/core/qdev-properties-system.c    |  12 ++
 hw/core/qdev-properties.c           |  44 +++++
 hw/display/virtio-gpu-base.c        |   8 +-
 hw/intc/arm_gic_kvm.c               |   3 +-
 hw/intc/arm_gicv3_its_kvm.c         |   3 +-
 hw/intc/arm_gicv3_kvm.c             |   3 +-
 hw/misc/ivshmem.c                   |   8 +-
 hw/net/virtio-net.c                 |  10 +-
 hw/pci/msix.c                       |   2 +-
 hw/pci/pci.c                        |  12 ++
 hw/ppc/pef.c                        |   2 +-
 hw/ppc/spapr.c                      |   2 +-
 hw/ppc/spapr_events.c               |   2 +-
 hw/ppc/spapr_rtas.c                 |   2 +-
 hw/remote/proxy.c                   |   7 +-
 hw/s390x/s390-virtio-ccw.c          |   9 +-
 hw/scsi/vhost-scsi.c                |   9 +-
 hw/vfio/common.c                    | 235 +++++++++++++++++++----
 hw/vfio/cpr.c                       | 177 ++++++++++++++++++
 hw/vfio/meson.build                 |   1 +
 hw/vfio/migration.c                 |  23 +--
 hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
 hw/vfio/trace-events                |   1 +
 hw/virtio/vhost-vdpa.c              |   6 +-
 hw/virtio/vhost.c                   |  32 +++-
 include/chardev/char-socket.h       |   1 +
 include/chardev/char.h              |   5 +
 include/exec/memory.h               |  48 +++++
 include/exec/ram_addr.h             |   1 +
 include/exec/ramblock.h             |   1 +
 include/hw/pci/msix.h               |   1 +
 include/hw/qdev-properties-system.h |   4 +
 include/hw/qdev-properties.h        |   3 +
 include/hw/vfio/vfio-common.h       |  12 ++
 include/hw/virtio/vhost.h           |   1 +
 include/migration/blocker.h         |  69 ++++++-
 include/migration/cpr-state.h       |  30 +++
 include/migration/cpr.h             |  20 ++
 include/migration/misc.h            |  13 +-
 include/migration/vmstate.h         |   2 +
 include/qapi/util.h                 |  28 +++
 include/qemu/osdep.h                |   9 +
 include/sysemu/runstate.h           |   2 +
 migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
 migration/cpr.c                     |  85 +++++++++
 migration/file.c                    |  62 ++++++
 migration/file.h                    |  14 ++
 migration/meson.build               |   3 +
 migration/migration.c               | 268 +++++++++++++++++++++++---
 migration/ram.c                     |  24 ++-
 migration/target.c                  |   1 +
 migration/trace-events              |  12 ++
 monitor/hmp-cmds.c                  |  59 +++---
 monitor/hmp.c                       |   3 +
 monitor/qmp.c                       |   4 +
 python/qemu/machine/machine.py      |  14 ++
 qapi/char.json                      |   7 +-
 qapi/migration.json                 |  68 ++++++-
 qapi/qapi-util.c                    |  37 ++++
 qemu-options.hx                     |  50 ++++-
 replay/replay.c                     |   4 +
 softmmu/memory.c                    |  31 ++-
 softmmu/physmem.c                   | 100 +++++++++-
 softmmu/runstate.c                  |  42 ++++-
 softmmu/vl.c                        |  10 +
 stubs/cpr-state.c                   |  26 +++
 stubs/meson.build                   |   2 +
 stubs/migr-blocker.c                |   9 +-
 stubs/migration.c                   |  33 ++++
 target/i386/kvm/kvm.c               |   8 +-
 target/i386/nvmm/nvmm-all.c         |   4 +-
 target/i386/sev.c                   |   2 +-
 target/i386/whpx/whpx-all.c         |   3 +-
 tests/avocado/cpr.py                | 176 ++++++++++++++++++
 tests/unit/meson.build              |   1 +
 tests/unit/test-strlist.c           |  81 ++++++++
 trace-events                        |   1 +
 ui/spice-core.c                     |   5 +-
 ui/vdagent.c                        |   5 +-
 util/oslib-posix.c                  |   9 +
 util/oslib-win32.c                  |   4 +
 105 files changed, 2781 insertions(+), 330 deletions(-)
 create mode 100644 hw/vfio/cpr.c
 create mode 100644 include/migration/cpr-state.h
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr-state.c
 create mode 100644 migration/cpr.c
 create mode 100644 migration/file.c
 create mode 100644 migration/file.h
 create mode 100644 stubs/cpr-state.c
 create mode 100644 stubs/migration.c
 create mode 100644 tests/avocado/cpr.py
 create mode 100644 tests/unit/test-strlist.c
#58 lore raw

and of course I immediately screwed up and forgot to delete series numbers from the email titles. I will resend.

- Steve

Quoted 349 lines · Steven Sistare

To make forward progress on this series and reduce its size, I will be posting those of its patches that can be independently integrated and have some value on their own, to a reduced distribution of reviewers for each. This is what I plan to break out:

migration: fix populate_vfio_info

memory: RAM_NAMED_FILE flag

memory: flat section iterator

oslib: qemu_clear_cloexec

migration: simplify blockers

migration: simplify notifiers

python/machine: QEMUMachine full_args

python/machine: QEMUMachine reopen_qmp_connection

qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests

- Steve

Quoted 320 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://lore.kernel.org/qemu-devel/[email protected]/

- Steve

Quoted 303 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in
https://lore.kernel.org/lkml/[email protected]
but this is not enforced. The restarted qemu arguments must match those used to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in
https://lore.kernel.org/kvm/[email protected]
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1                                        | window 2
                                                |
# qemu-system-$arch ...                         |
  -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec      |
(qemu) migrate_set_parameter cpr-exec-args      |
  qemu-system-$arch ... -incoming defer         |
(qemu) migrate -d file:/tmp/qemu.sav            |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1                                        | window 2
                                                |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
  -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -d file:/tmp/qemu.sav            |
(qemu) quit                                     |
                                                |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
  -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

 MAINTAINERS                         |  14 ++
 accel/xen/xen-all.c                 |   3 +
 backends/hostmem-epc.c              |  18 +-
 backends/hostmem-file.c             |   1 +
 backends/hostmem-memfd.c            |  22 ++-
 backends/tpm/tpm_emulator.c         |  11 +-
 block/parallels.c                   |   7 +-
 block/qcow.c                        |   7 +-
 block/vdi.c                         |   7 +-
 block/vhdx.c                        |   7 +-
 block/vmdk.c                        |   7 +-
 block/vpc.c                         |   7 +-
 block/vvfat.c                       |   7 +-
 chardev/char-mux.c                  |   1 +
 chardev/char-null.c                 |   1 +
 chardev/char-pty.c                  |  16 +-
 chardev/char-serial.c               |   1 +
 chardev/char-socket.c               |  48 +++++
 chardev/char-stdio.c                |  31 +++
 chardev/char.c                      |  49 ++++-
 dump/dump.c                         |   4 +-
 gdbstub.c                           |   1 +
 hmp-commands.hx                     |   2 +-
 hw/9pfs/9p.c                        |  11 +-
 hw/core/qdev-properties-system.c    |  12 ++
 hw/core/qdev-properties.c           |  44 +++++
 hw/display/virtio-gpu-base.c        |   8 +-
 hw/intc/arm_gic_kvm.c               |   3 +-
 hw/intc/arm_gicv3_its_kvm.c         |   3 +-
 hw/intc/arm_gicv3_kvm.c             |   3 +-
 hw/misc/ivshmem.c                   |   8 +-
 hw/net/virtio-net.c                 |  10 +-
 hw/pci/msix.c                       |   2 +-
 hw/pci/pci.c                        |  12 ++
 hw/ppc/pef.c                        |   2 +-
 hw/ppc/spapr.c                      |   2 +-
 hw/ppc/spapr_events.c               |   2 +-
 hw/ppc/spapr_rtas.c                 |   2 +-
 hw/remote/proxy.c                   |   7 +-
 hw/s390x/s390-virtio-ccw.c          |   9 +-
 hw/scsi/vhost-scsi.c                |   9 +-
 hw/vfio/common.c                    | 235 +++++++++++++++++++----
 hw/vfio/cpr.c                       | 177 ++++++++++++++++++
 hw/vfio/meson.build                 |   1 +
 hw/vfio/migration.c                 |  23 +--
 hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
 hw/vfio/trace-events                |   1 +
 hw/virtio/vhost-vdpa.c              |   6 +-
 hw/virtio/vhost.c                   |  32 +++-
 include/chardev/char-socket.h       |   1 +
 include/chardev/char.h              |   5 +
 include/exec/memory.h               |  48 +++++
 include/exec/ram_addr.h             |   1 +
 include/exec/ramblock.h             |   1 +
 include/hw/pci/msix.h               |   1 +
 include/hw/qdev-properties-system.h |   4 +
 include/hw/qdev-properties.h        |   3 +
 include/hw/vfio/vfio-common.h       |  12 ++
 include/hw/virtio/vhost.h           |   1 +
 include/migration/blocker.h         |  69 ++++++-
 include/migration/cpr-state.h       |  30 +++
 include/migration/cpr.h             |  20 ++
 include/migration/misc.h            |  13 +-
 include/migration/vmstate.h         |   2 +
 include/qapi/util.h                 |  28 +++
 include/qemu/osdep.h                |   9 +
 include/sysemu/runstate.h           |   2 +
 migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
 migration/cpr.c                     |  85 +++++++++
 migration/file.c                    |  62 ++++++
 migration/file.h                    |  14 ++
 migration/meson.build               |   3 +
 migration/migration.c               | 268 +++++++++++++++++++++++---
 migration/ram.c                     |  24 ++-
 migration/target.c                  |   1 +
 migration/trace-events              |  12 ++
 monitor/hmp-cmds.c                  |  59 +++---
 monitor/hmp.c                       |   3 +
 monitor/qmp.c                       |   4 +
 python/qemu/machine/machine.py      |  14 ++
 qapi/char.json                      |   7 +-
 qapi/migration.json                 |  68 ++++++-
 qapi/qapi-util.c                    |  37 ++++
 qemu-options.hx                     |  50 ++++-
 replay/replay.c                     |   4 +
 softmmu/memory.c                    |  31 ++-
 softmmu/physmem.c                   | 100 +++++++++-
 softmmu/runstate.c                  |  42 ++++-
 softmmu/vl.c                        |  10 +
 stubs/cpr-state.c                   |  26 +++
 stubs/meson.build                   |   2 +
 stubs/migr-blocker.c                |   9 +-
 stubs/migration.c                   |  33 ++++
 target/i386/kvm/kvm.c               |   8 +-
 target/i386/nvmm/nvmm-all.c         |   4 +-
 target/i386/sev.c                   |   2 +-
 target/i386/whpx/whpx-all.c         |   3 +-
 tests/avocado/cpr.py                | 176 ++++++++++++++++++
 tests/unit/meson.build              |   1 +
 tests/unit/test-strlist.c           |  81 ++++++++
 trace-events                        |   1 +
 ui/spice-core.c                     |   5 +-
 ui/vdagent.c                        |   5 +-
 util/oslib-posix.c                  |   9 +
 util/oslib-win32.c                  |   4 +
 105 files changed, 2781 insertions(+), 330 deletions(-)
 create mode 100644 hw/vfio/cpr.c
 create mode 100644 include/migration/cpr-state.h
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr-state.c
 create mode 100644 migration/cpr.c
 create mode 100644 migration/file.c
 create mode 100644 migration/file.h
 create mode 100644 stubs/cpr-state.c
 create mode 100644 stubs/migration.c
 create mode 100644 tests/avocado/cpr.py
 create mode 100644 tests/unit/test-strlist.c
#59 lore raw
In reply to Steven Sistare

Hi Steve,

Quoted 17 lines · Steven Sistare

To make forward progress on this series and reduce its size, I will be posting those of its patches that can be independently integrated and have some value on their own, to a reduced distribution of reviewers for each. This is what I plan to break out:

migration: fix populate_vfio_info

memory: RAM_NAMED_FILE flag

memory: flat section iterator

oslib: qemu_clear_cloexec

migration: simplify blockers

migration: simplify notifiers

Quoted 8 lines

python/machine: QEMUMachine full_args

python/machine: QEMUMachine reopen_qmp_connection

qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests

The break out looks good, but I guess it is a bit obsolete (as of today), you probably already re-posted some of these patches as separate series. Do you mind replying with a "ping" to the posted ones which still applies and need to be reviewed?

Thanks,

Phil.

Quoted 5 lines

- Steve

Quoted 2 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

#60 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé

Hi Steve,

Quoted 17 lines · Steven Sistare

To make forward progress on this series and reduce its size, I will be posting those of its patches that can be independently integrated and have some value on their own, to a reduced distribution of reviewers for each.  This is what I plan to break out:

migration: fix populate_vfio_info

memory: RAM_NAMED_FILE flag

memory: flat section iterator

oslib: qemu_clear_cloexec

migration: simplify blockers

migration: simplify notifiers

Quoted 8 lines

python/machine: QEMUMachine full_args

python/machine: QEMUMachine reopen_qmp_connection

qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests

Quoted 4 lines

The break out looks good, but I guess it is a bit obsolete (as of today), you probably already re-posted some of these patches as separate series. Do you mind replying with a "ping" to the posted ones which still applies and need to be reviewed?

Yes, I re-posted this list of patches, some of which have been accepted or are close. I will reply to the threads that are still open and cc you, but I will first verify that they still apply cleanly, and update them if not. Thanks!

- Steve

#61 lore raw
In reply to Steven Sistare

Hey Steven,

Have you done any "back-to-back" live update testing before?

I am still doing extensive testing on this myself. I am running into a bug that I have not yet diagnosed.

It involves the following:

1. Perform a live update (I'm using kexec + PMEM-based live updates). => VM comes back just fine. All is well.
2. Using the *same* QEMU instance: Try to perform another live update agaig,
this results in the following error:

monitor> migrate_incoming file:qemu.sav
qemu-system-x86_64: check_section_footer: Read section footer failed: -5
qemu-system-x86_64: load of migration failed: Invalid argument

I'm going to start diving into the code soon, but just thought I would ask first.

- Michael

Quoted 320 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://urldefense.com/v3/__https://lore.kernel.org/qemu-devel/[email protected]/__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3EAFRUVu$

- Steve

Quoted 303 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in
https://urldefense.com/v3/__https://lore.kernel.org/lkml/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3AKRJQux$
but this is not enforced. The restarted qemu arguments must match those used to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in
https://urldefense.com/v3/__https://lore.kernel.org/kvm/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3IK0BrTn$
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1 | window 2

                                                 |
# qemu-system-$arch ...                         |
   -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |

| # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec |
(qemu) migrate_set_parameter cpr-exec-args |
qemu-system-$arch ... -incoming defer |

(qemu) migrate -d file:/tmp/qemu.sav            |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1 | window 2

                                                 |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
   -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                 | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -d file:/tmp/qemu.sav            |
(qemu) quit                                     |
                                                 |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                 |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
   -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incoming file:/tmp/qemu.sav      |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

  MAINTAINERS                         |  14 ++
  accel/xen/xen-all.c                 |   3 +
  backends/hostmem-epc.c              |  18 +-
  backends/hostmem-file.c             |   1 +
  backends/hostmem-memfd.c            |  22 ++-
  backends/tpm/tpm_emulator.c         |  11 +-
  block/parallels.c                   |   7 +-
  block/qcow.c                        |   7 +-
  block/vdi.c                         |   7 +-
  block/vhdx.c                        |   7 +-
  block/vmdk.c                        |   7 +-
  block/vpc.c                         |   7 +-
  block/vvfat.c                       |   7 +-
  chardev/char-mux.c                  |   1 +
  chardev/char-null.c                 |   1 +
  chardev/char-pty.c                  |  16 +-
  chardev/char-serial.c               |   1 +
  chardev/char-socket.c               |  48 +++++
  chardev/char-stdio.c                |  31 +++
  chardev/char.c                      |  49 ++++-
  dump/dump.c                         |   4 +-
  gdbstub.c                           |   1 +
  hmp-commands.hx                     |   2 +-
  hw/9pfs/9p.c                        |  11 +-
  hw/core/qdev-properties-system.c    |  12 ++
  hw/core/qdev-properties.c           |  44 +++++
  hw/display/virtio-gpu-base.c        |   8 +-
  hw/intc/arm_gic_kvm.c               |   3 +-
  hw/intc/arm_gicv3_its_kvm.c         |   3 +-
  hw/intc/arm_gicv3_kvm.c             |   3 +-
  hw/misc/ivshmem.c                   |   8 +-
  hw/net/virtio-net.c                 |  10 +-
  hw/pci/msix.c                       |   2 +-
  hw/pci/pci.c                        |  12 ++
  hw/ppc/pef.c                        |   2 +-
  hw/ppc/spapr.c                      |   2 +-
  hw/ppc/spapr_events.c               |   2 +-
  hw/ppc/spapr_rtas.c                 |   2 +-
  hw/remote/proxy.c                   |   7 +-
  hw/s390x/s390-virtio-ccw.c          |   9 +-
  hw/scsi/vhost-scsi.c                |   9 +-
  hw/vfio/common.c                    | 235 +++++++++++++++++++----
  hw/vfio/cpr.c                       | 177 ++++++++++++++++++
  hw/vfio/meson.build                 |   1 +
  hw/vfio/migration.c                 |  23 +--
  hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
  hw/vfio/trace-events                |   1 +
  hw/virtio/vhost-vdpa.c              |   6 +-
  hw/virtio/vhost.c                   |  32 +++-
  include/chardev/char-socket.h       |   1 +
  include/chardev/char.h              |   5 +
  include/exec/memory.h               |  48 +++++
  include/exec/ram_addr.h             |   1 +
  include/exec/ramblock.h             |   1 +
  include/hw/pci/msix.h               |   1 +
  include/hw/qdev-properties-system.h |   4 +
  include/hw/qdev-properties.h        |   3 +
  include/hw/vfio/vfio-common.h       |  12 ++
  include/hw/virtio/vhost.h           |   1 +
  include/migration/blocker.h         |  69 ++++++-
  include/migration/cpr-state.h       |  30 +++
  include/migration/cpr.h             |  20 ++
  include/migration/misc.h            |  13 +-
  include/migration/vmstate.h         |   2 +
  include/qapi/util.h                 |  28 +++
  include/qemu/osdep.h                |   9 +
  include/sysemu/runstate.h           |   2 +
  migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
  migration/cpr.c                     |  85 +++++++++
  migration/file.c                    |  62 ++++++
  migration/file.h                    |  14 ++
  migration/meson.build               |   3 +
  migration/migration.c               | 268 +++++++++++++++++++++++---
  migration/ram.c                     |  24 ++-
  migration/target.c                  |   1 +
  migration/trace-events              |  12 ++
  monitor/hmp-cmds.c                  |  59 +++---
  monitor/hmp.c                       |   3 +
  monitor/qmp.c                       |   4 +
  python/qemu/machine/machine.py      |  14 ++
  qapi/char.json                      |   7 +-
  qapi/migration.json                 |  68 ++++++-
  qapi/qapi-util.c                    |  37 ++++
  qemu-options.hx                     |  50 ++++-
  replay/replay.c                     |   4 +
  softmmu/memory.c                    |  31 ++-
  softmmu/physmem.c                   | 100 +++++++++-
  softmmu/runstate.c                  |  42 ++++-
  softmmu/vl.c                        |  10 +
  stubs/cpr-state.c                   |  26 +++
  stubs/meson.build                   |   2 +
  stubs/migr-blocker.c                |   9 +-
  stubs/migration.c                   |  33 ++++
  target/i386/kvm/kvm.c               |   8 +-
  target/i386/nvmm/nvmm-all.c         |   4 +-
  target/i386/sev.c                   |   2 +-
  target/i386/whpx/whpx-all.c         |   3 +-
  tests/avocado/cpr.py                | 176 ++++++++++++++++++
  tests/unit/meson.build              |   1 +
  tests/unit/test-strlist.c           |  81 ++++++++
  trace-events                        |   1 +
  ui/spice-core.c                     |   5 +-
  ui/vdagent.c                        |   5 +-
  util/oslib-posix.c                  |   9 +
  util/oslib-win32.c                  |   4 +
  105 files changed, 2781 insertions(+), 330 deletions(-)

create mode 100644 hw/vfio/cpr.c
create mode 100644 include/migration/cpr-state.h create mode 100644 include/migration/cpr.h create mode 100644 migration/cpr-state.c create mode 100644 migration/cpr.c create mode 100644 migration/file.c create mode 100644 migration/file.h create mode 100644 stubs/cpr-state.c create mode 100644 stubs/migration.c create mode 100644 tests/avocado/cpr.py create mode 100644 tests/unit/test-strlist.c

Tested-by
#62 lore raw
In reply to Steven Sistare

Greetings,

For what its worth, our team has been aggressively testing this patch series and to date have not found any deficiencies or bottlenecks whatsoever.

Overall, with a very well-tuned system (and linux kernel), we are getting live update downtimes just above 15 seconds, and the vast majority of that downtime is kind of wasted time during kexec (particularly with PCI device probing, if anybody has any tips on that) and has nothing to do whatsoever with the QEMU-side of things. (We use this patchset in a PMEM-based configuration which has been working out extremely well so far).

We also tested "back-to-back" live updates as you would expect to do in production.

Overall we've done thousands of live updates over the past few months on many different types of hardware without failure.

Steven has been really responsive in answering some of our usability questions and we were able to fix those issues.

We will continue our testing throughout the year with more heavily-loaded workloads, but all in all we would very much be interested in seeing further reviews on this patch series from others. *
*---

Quoted 319 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://urldefense.com/v3/__https://lore.kernel.org/qemu-devel/[email protected]/__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3EAFRUVu$  

- Steve

Quoted 302 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in

https://urldefense.com/v3/__https://lore.kernel.org/lkml/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3AKRJQux$  but this is not enforced.  The restarted qemu arguments must match those used
to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in

   https://urldefense.com/v3/__https://lore.kernel.org/kvm/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3IK0BrTn$  
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1 | window 2

                                                 |
# qemu-system-$arch ...                         |
   -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |

| # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec |
(qemu) migrate_set_parameter cpr-exec-args |
qemu-system-$arch ... -incoming defer |

(qemu) migrate -dfile:/tmp/qemu.sav             |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incomingfile:/tmp/qemu.sav       |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1 | window 2

                                                 |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
   -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                 | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -dfile:/tmp/qemu.sav             |
(qemu) quit                                     |
                                                 |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                 |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
   -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incomingfile:/tmp/qemu.sav       |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

  MAINTAINERS                         |  14 ++
  accel/xen/xen-all.c                 |   3 +
  backends/hostmem-epc.c              |  18 +-
  backends/hostmem-file.c             |   1 +
  backends/hostmem-memfd.c            |  22 ++-
  backends/tpm/tpm_emulator.c         |  11 +-
  block/parallels.c                   |   7 +-
  block/qcow.c                        |   7 +-
  block/vdi.c                         |   7 +-
  block/vhdx.c                        |   7 +-
  block/vmdk.c                        |   7 +-
  block/vpc.c                         |   7 +-
  block/vvfat.c                       |   7 +-
  chardev/char-mux.c                  |   1 +
  chardev/char-null.c                 |   1 +
  chardev/char-pty.c                  |  16 +-
  chardev/char-serial.c               |   1 +
  chardev/char-socket.c               |  48 +++++
  chardev/char-stdio.c                |  31 +++
  chardev/char.c                      |  49 ++++-
  dump/dump.c                         |   4 +-
  gdbstub.c                           |   1 +
  hmp-commands.hx                     |   2 +-
  hw/9pfs/9p.c                        |  11 +-
  hw/core/qdev-properties-system.c    |  12 ++
  hw/core/qdev-properties.c           |  44 +++++
  hw/display/virtio-gpu-base.c        |   8 +-
  hw/intc/arm_gic_kvm.c               |   3 +-
  hw/intc/arm_gicv3_its_kvm.c         |   3 +-
  hw/intc/arm_gicv3_kvm.c             |   3 +-
  hw/misc/ivshmem.c                   |   8 +-
  hw/net/virtio-net.c                 |  10 +-
  hw/pci/msix.c                       |   2 +-
  hw/pci/pci.c                        |  12 ++
  hw/ppc/pef.c                        |   2 +-
  hw/ppc/spapr.c                      |   2 +-
  hw/ppc/spapr_events.c               |   2 +-
  hw/ppc/spapr_rtas.c                 |   2 +-
  hw/remote/proxy.c                   |   7 +-
  hw/s390x/s390-virtio-ccw.c          |   9 +-
  hw/scsi/vhost-scsi.c                |   9 +-
  hw/vfio/common.c                    | 235 +++++++++++++++++++----
  hw/vfio/cpr.c                       | 177 ++++++++++++++++++
  hw/vfio/meson.build                 |   1 +
  hw/vfio/migration.c                 |  23 +--
  hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
  hw/vfio/trace-events                |   1 +
  hw/virtio/vhost-vdpa.c              |   6 +-
  hw/virtio/vhost.c                   |  32 +++-
  include/chardev/char-socket.h       |   1 +
  include/chardev/char.h              |   5 +
  include/exec/memory.h               |  48 +++++
  include/exec/ram_addr.h             |   1 +
  include/exec/ramblock.h             |   1 +
  include/hw/pci/msix.h               |   1 +
  include/hw/qdev-properties-system.h |   4 +
  include/hw/qdev-properties.h        |   3 +
  include/hw/vfio/vfio-common.h       |  12 ++
  include/hw/virtio/vhost.h           |   1 +
  include/migration/blocker.h         |  69 ++++++-
  include/migration/cpr-state.h       |  30 +++
  include/migration/cpr.h             |  20 ++
  include/migration/misc.h            |  13 +-
  include/migration/vmstate.h         |   2 +
  include/qapi/util.h                 |  28 +++
  include/qemu/osdep.h                |   9 +
  include/sysemu/runstate.h           |   2 +
  migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
  migration/cpr.c                     |  85 +++++++++
  migration/file.c                    |  62 ++++++
  migration/file.h                    |  14 ++
  migration/meson.build               |   3 +
  migration/migration.c               | 268 +++++++++++++++++++++++---
  migration/ram.c                     |  24 ++-
  migration/target.c                  |   1 +
  migration/trace-events              |  12 ++
  monitor/hmp-cmds.c                  |  59 +++---
  monitor/hmp.c                       |   3 +
  monitor/qmp.c                       |   4 +
  python/qemu/machine/machine.py      |  14 ++
  qapi/char.json                      |   7 +-
  qapi/migration.json                 |  68 ++++++-
  qapi/qapi-util.c                    |  37 ++++
  qemu-options.hx                     |  50 ++++-
  replay/replay.c                     |   4 +
  softmmu/memory.c                    |  31 ++-
  softmmu/physmem.c                   | 100 +++++++++-
  softmmu/runstate.c                  |  42 ++++-
  softmmu/vl.c                        |  10 +
  stubs/cpr-state.c                   |  26 +++
  stubs/meson.build                   |   2 +
  stubs/migr-blocker.c                |   9 +-
  stubs/migration.c                   |  33 ++++
  target/i386/kvm/kvm.c               |   8 +-
  target/i386/nvmm/nvmm-all.c         |   4 +-
  target/i386/sev.c                   |   2 +-
  target/i386/whpx/whpx-all.c         |   3 +-
  tests/avocado/cpr.py                | 176 ++++++++++++++++++
  tests/unit/meson.build              |   1 +
  tests/unit/test-strlist.c           |  81 ++++++++
  trace-events                        |   1 +
  ui/spice-core.c                     |   5 +-
  ui/vdagent.c                        |   5 +-
  util/oslib-posix.c                  |   9 +
  util/oslib-win32.c                  |   4 +
  105 files changed, 2781 insertions(+), 330 deletions(-)

create mode 100644 hw/vfio/cpr.c
create mode 100644 include/migration/cpr-state.h create mode 100644 include/migration/cpr.h create mode 100644 migration/cpr-state.c create mode 100644 migration/cpr.c create mode 100644 migration/file.c create mode 100644 migration/file.h create mode 100644 stubs/cpr-state.c create mode 100644 stubs/migration.c create mode 100644 tests/avocado/cpr.py create mode 100644 tests/unit/test-strlist.c

#63 lore raw
In reply to Steven Sistare

Hi Steve,

In the current design you have, we have to specify both the command line parameter "-migrate-mode-enable cpr-reboot" *and* issue the monitor command "migrate_set_parameter mode cpr-${mode}".

Is it possible to opt-in to the CPR mode just once over the monitor instead of having to specify it twice on the command line? This would also match the live migration model: You do not need to necessarily "opt in" to live migration mode through a command line parameter, you simply request it when you need to. Can CPR behave the same way?

This would also make switching over to a CPR-capable version of QEMU much simpler and would even make it work for existing libvirt-managed guests as their command line parameters would no longer need to change. This would allow us to simply power-off and power-on existing VMs to make them CPR-capable and then work on a libvirt patch later when we're ready to do so.

Comments?

- Michael

Quoted 319 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://urldefense.com/v3/__https://lore.kernel.org/qemu-devel/[email protected]/__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3EAFRUVu$  

- Steve

Quoted 302 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in

https://urldefense.com/v3/__https://lore.kernel.org/lkml/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3AKRJQux$  but this is not enforced.  The restarted qemu arguments must match those used
to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in

   https://urldefense.com/v3/__https://lore.kernel.org/kvm/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3IK0BrTn$  
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1 | window 2

                                                 |
# qemu-system-$arch ...                         |
   -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |

| # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec |
(qemu) migrate_set_parameter cpr-exec-args |
qemu-system-$arch ... -incoming defer |

(qemu) migrate -dfile:/tmp/qemu.sav             |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incomingfile:/tmp/qemu.sav       |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1 | window 2

                                                 |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
   -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                 | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -dfile:/tmp/qemu.sav             |
(qemu) quit                                     |
                                                 |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                 |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
   -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incomingfile:/tmp/qemu.sav       |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

  MAINTAINERS                         |  14 ++
  accel/xen/xen-all.c                 |   3 +
  backends/hostmem-epc.c              |  18 +-
  backends/hostmem-file.c             |   1 +
  backends/hostmem-memfd.c            |  22 ++-
  backends/tpm/tpm_emulator.c         |  11 +-
  block/parallels.c                   |   7 +-
  block/qcow.c                        |   7 +-
  block/vdi.c                         |   7 +-
  block/vhdx.c                        |   7 +-
  block/vmdk.c                        |   7 +-
  block/vpc.c                         |   7 +-
  block/vvfat.c                       |   7 +-
  chardev/char-mux.c                  |   1 +
  chardev/char-null.c                 |   1 +
  chardev/char-pty.c                  |  16 +-
  chardev/char-serial.c               |   1 +
  chardev/char-socket.c               |  48 +++++
  chardev/char-stdio.c                |  31 +++
  chardev/char.c                      |  49 ++++-
  dump/dump.c                         |   4 +-
  gdbstub.c                           |   1 +
  hmp-commands.hx                     |   2 +-
  hw/9pfs/9p.c                        |  11 +-
  hw/core/qdev-properties-system.c    |  12 ++
  hw/core/qdev-properties.c           |  44 +++++
  hw/display/virtio-gpu-base.c        |   8 +-
  hw/intc/arm_gic_kvm.c               |   3 +-
  hw/intc/arm_gicv3_its_kvm.c         |   3 +-
  hw/intc/arm_gicv3_kvm.c             |   3 +-
  hw/misc/ivshmem.c                   |   8 +-
  hw/net/virtio-net.c                 |  10 +-
  hw/pci/msix.c                       |   2 +-
  hw/pci/pci.c                        |  12 ++
  hw/ppc/pef.c                        |   2 +-
  hw/ppc/spapr.c                      |   2 +-
  hw/ppc/spapr_events.c               |   2 +-
  hw/ppc/spapr_rtas.c                 |   2 +-
  hw/remote/proxy.c                   |   7 +-
  hw/s390x/s390-virtio-ccw.c          |   9 +-
  hw/scsi/vhost-scsi.c                |   9 +-
  hw/vfio/common.c                    | 235 +++++++++++++++++++----
  hw/vfio/cpr.c                       | 177 ++++++++++++++++++
  hw/vfio/meson.build                 |   1 +
  hw/vfio/migration.c                 |  23 +--
  hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
  hw/vfio/trace-events                |   1 +
  hw/virtio/vhost-vdpa.c              |   6 +-
  hw/virtio/vhost.c                   |  32 +++-
  include/chardev/char-socket.h       |   1 +
  include/chardev/char.h              |   5 +
  include/exec/memory.h               |  48 +++++
  include/exec/ram_addr.h             |   1 +
  include/exec/ramblock.h             |   1 +
  include/hw/pci/msix.h               |   1 +
  include/hw/qdev-properties-system.h |   4 +
  include/hw/qdev-properties.h        |   3 +
  include/hw/vfio/vfio-common.h       |  12 ++
  include/hw/virtio/vhost.h           |   1 +
  include/migration/blocker.h         |  69 ++++++-
  include/migration/cpr-state.h       |  30 +++
  include/migration/cpr.h             |  20 ++
  include/migration/misc.h            |  13 +-
  include/migration/vmstate.h         |   2 +
  include/qapi/util.h                 |  28 +++
  include/qemu/osdep.h                |   9 +
  include/sysemu/runstate.h           |   2 +
  migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
  migration/cpr.c                     |  85 +++++++++
  migration/file.c                    |  62 ++++++
  migration/file.h                    |  14 ++
  migration/meson.build               |   3 +
  migration/migration.c               | 268 +++++++++++++++++++++++---
  migration/ram.c                     |  24 ++-
  migration/target.c                  |   1 +
  migration/trace-events              |  12 ++
  monitor/hmp-cmds.c                  |  59 +++---
  monitor/hmp.c                       |   3 +
  monitor/qmp.c                       |   4 +
  python/qemu/machine/machine.py      |  14 ++
  qapi/char.json                      |   7 +-
  qapi/migration.json                 |  68 ++++++-
  qapi/qapi-util.c                    |  37 ++++
  qemu-options.hx                     |  50 ++++-
  replay/replay.c                     |   4 +
  softmmu/memory.c                    |  31 ++-
  softmmu/physmem.c                   | 100 +++++++++-
  softmmu/runstate.c                  |  42 ++++-
  softmmu/vl.c                        |  10 +
  stubs/cpr-state.c                   |  26 +++
  stubs/meson.build                   |   2 +
  stubs/migr-blocker.c                |   9 +-
  stubs/migration.c                   |  33 ++++
  target/i386/kvm/kvm.c               |   8 +-
  target/i386/nvmm/nvmm-all.c         |   4 +-
  target/i386/sev.c                   |   2 +-
  target/i386/whpx/whpx-all.c         |   3 +-
  tests/avocado/cpr.py                | 176 ++++++++++++++++++
  tests/unit/meson.build              |   1 +
  tests/unit/test-strlist.c           |  81 ++++++++
  trace-events                        |   1 +
  ui/spice-core.c                     |   5 +-
  ui/vdagent.c                        |   5 +-
  util/oslib-posix.c                  |   9 +
  util/oslib-win32.c                  |   4 +
  105 files changed, 2781 insertions(+), 330 deletions(-)

create mode 100644 hw/vfio/cpr.c
create mode 100644 include/migration/cpr-state.h create mode 100644 include/migration/cpr.h create mode 100644 migration/cpr-state.c create mode 100644 migration/cpr.c create mode 100644 migration/file.c create mode 100644 migration/file.h create mode 100644 stubs/cpr-state.c create mode 100644 stubs/migration.c create mode 100644 tests/avocado/cpr.py create mode 100644 tests/unit/test-strlist.c

#64 lore raw

Another option could be to expose "-migrate-mode-disable" (instead of enable) and just enable all 3 modes by default, since we are already required to switch from "normal" mode to a CPR-specific mode when it is time to do a live update, if the intention is to preserve the capability to completely prevent a running QEMU from using these modes before the VM starts up.

- Michael

Quoted 340 lines · Michael Galaxy

Hi Steve,

In the current design you have, we have to specify both the command line parameter "-migrate-mode-enable cpr-reboot" *and* issue the monitor command "migrate_set_parameter mode cpr-${mode}".

Is it possible to opt-in to the CPR mode just once over the monitor instead of having to specify it twice on the command line? This would also match the live migration model: You do not need to necessarily "opt in" to live migration mode through a command line parameter, you simply request it when you need to. Can CPR behave the same way?

This would also make switching over to a CPR-capable version of QEMU much simpler and would even make it work for existing libvirt-managed guests as their command line parameters would no longer need to change. This would allow us to simply power-off and power-on existing VMs to make them CPR-capable and then work on a libvirt patch later when we're ready to do so.

Comments?

- Michael

Quoted 319 lines · Steven Sistare

This series desperately needs review in its intersection with live migration. The code in other areas has been reviewed and revised multiple times -- thank you!

David, Juan, can you spare some time to review this? I have done my best to order the patches logically (see the labelled groups in this email), and to provide complete and clear cover letter and commit messages. Can I do anything to facilitate, like doing a code walk through via zoom?

And of course, I welcome anyone's feedback.

Here is the original posting.

https://urldefense.com/v3/__https://lore.kernel.org/qemu-devel/[email protected]/__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3EAFRUVu$  

- Steve

Quoted 302 lines · Steve Sistare

This version of the live update patch series integrates live update into the live migration framework. The new interfaces are:
* mode (migration parameter)
* cpr-exec-args (migration parameter)
* file (migration URI)
* migrate-mode-enable (command-line argument)
* only-cpr-capable (command-line argument)

Provide the cpr-exec and cpr-reboot migration modes 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. The caller sets the mode parameter before invoking the migrate or migrate-incoming commands.

In cpr-reboot mode, the migrate command saves state to a file, allowing one to quit qemu, reboot to an updated kernel, start an updated version of qemu, and resume via the migrate-incoming command. The caller must specify a migration URI that writes to and reads from a file. Unlike normal mode, the use of certain local storage options does not block the migration, but the caller must not modify guest block devices between the quit and restart. The guest RAM memory-backend must be shared, and the @x-ignore-shared migration capability must be set, to avoid saving it to the file. Guest RAM must be non-volatile across reboot, which can be achieved by backing it with a dax device, or /dev/shm PKRAM as proposed in

https://urldefense.com/v3/__https://lore.kernel.org/lkml/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3AKRJQux$  but this is not enforced.  The restarted qemu arguments must match those used
to initially start qemu, plus the -incoming option.

The reboot mode supports vfio devices if the caller first suspends the guest, 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. After issuing migrate-incoming, the caller must issue a system_wakeup command to resume.

In cpr-exec mode, the migrate command saves state to a file and directly exec's a new version of qemu on the same host, replacing the original process while retaining its PID. The caller must specify a migration URI that writes to and reads from a file, and resumes execution via the migrate-incoming command. Arguments for the new qemu process are taken from the cpr-exec-args migration parameter, and must include the -incoming option.

Guest RAM must be backed by a memory backend with share=on, but cannot be memory-backend-ram. The memory is re-mmap'd in the updated process, so guest ram is efficiently preserved in place, albeit with new virtual addresses. In addition, the '-migrate-mode-enable cpr-exec' option is required. This causes secondary guest ram blocks (those not specified on the command line) to be allocated by mmap'ing a memfd. The memfds are kept open across exec, their values are saved in special cpr state which is retrieved after exec, and they are re-mmap'd. Since guest RAM is not copied, and storage blocks are not migrated, the caller must disable all capabilities related to page and block copy. The implementation ignores all related parameters.

The exec 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_VADDR and VFIO_DMA_MAP_FLAG_VADDR as defined in

   https://urldefense.com/v3/__https://lore.kernel.org/kvm/[email protected]__;!!GjvTz_vk!U6U0yYHuO4GRyGQUNpn0TQdwlC2QjJfgYC0yE249AuONq8-5rs48pZ6l0K-LOSRHMA9cU2op2U8GC9hU3IK0BrTn$  
and integrated in Linux kernel 5.12.

Here is an example of updating qemu from v7.0.50 to v7.0.51 using exec mode. The software update is performed while the guest is running to minimize downtime.

window 1 | window 2

                                                 |
# qemu-system-$arch ...                         |
   -migrate-mode-enable cpr-exec                 |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |

| # yum update qemu
(qemu) migrate_set_parameter mode cpr-exec |
(qemu) migrate_set_parameter cpr-exec-args |
qemu-system-$arch ... -incoming defer |

(qemu) migrate -dfile:/tmp/qemu.sav             |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incomingfile:/tmp/qemu.sav       |
(qemu) info status                              |
VM status: running                              |

Here is an example of updating the host kernel using reboot mode.

window 1 | window 2

                                                 |
# qemu-system-$arch ... mem-path=/dev/dax0.0    |
   -migrate-mode-enable cpr-reboot               |
QEMU 7.0.50 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: running                              |
                                                 | # yum update kernel-uek
(qemu) migrate_set_parameter mode cpr-reboot    |
(qemu) migrate -dfile:/tmp/qemu.sav             |
(qemu) quit                                     |
                                                 |
# systemctl kexec                               |
kexec_core: Starting new kernel                 |
...                                             |
                                                 |
# qemu-system-$arch mem-path=/dev/dax0.0 ...    |
   -incoming defer                               |
QEMU 7.0.51 monitor - type 'help' ...           |
(qemu) info status                              |
VM status: paused (inmigrate)                   |
(qemu) migrate_incomingfile:/tmp/qemu.sav       |
(qemu) info status                              |
VM status: running                              |

Changes from V8 to V9:
vfio:
- free all cpr state during unwind in vfio_connect_container
- change cpr_resave_fd to return void, and avoid new unwind cases
- delete incorrect .unmigratable=1 in vmstate handlers
- add route batching in vfio_claim_vectors
- simplified vfio intx cpr code
- fix commit message for 'recover from unmap-all-vaddr failure'
- verify suspended runstate for cpr-reboot mode
Other:
- delete cpr-save, cpr-exec, cpr-load
- delete ram block vmstate handlers that were added in V8
- rename cpr-enable option to migrate-mode-enable
- add file URI for migration
- add mode and cpr-exec-args migration parameters
- add per-mode migration blockers
- add mode checks in migration notifiers
- fix suspended runstate during migration
- replace RAM_ANON flag with RAM_NAMED_FILE
- support memory-backend-epc

Steve Sistare (44):
migration: fix populate_vfio_info --- reboot mode ---
memory: RAM_NAMED_FILE flag
migration: file URI
migration: mode parameter
migration: migrate-enable-mode option
migration: simplify blockers
migration: per-mode blockers
cpr: relax some blockers
cpr: reboot mode

qdev-properties: strList --- exec mode ---
qapi: strList_from_string
qapi: QAPI_LIST_LENGTH
qapi: strv_from_strList
qapi: strList unit tests
migration: cpr-exec-args parameter
migration: simplify notifiers
migration: check mode in notifiers
memory: flat section iterator
oslib: qemu_clear_cloexec
vl: helper to request re-exec
cpr: preserve extra state
cpr: exec mode
cpr: add exec-mode blockers
cpr: ram block blockers
cpr: only-cpr-capable
cpr: Mismatched GPAs fix
hostmem-memfd: cpr support
hostmem-epc: cpr support

pci: export msix_is_pending --- vfio for exec ---
vfio-pci: refactor for cpr
vfio-pci: cpr part 1 (fd and dma)
vfio-pci: cpr part 2 (msi)
vfio-pci: cpr part 3 (intx)
vfio-pci: recover from unmap-all-vaddr failure

chardev: cpr framework --- misc for exec ---
chardev: cpr for simple devices
chardev: cpr for pty
python/machine: QEMUMachine full_args python/machine: QEMUMachine reopen_qmp_connection tests/avocado: add cpr regression test

vl: start on wakeup request --- vfio for reboot ---
migration: fix suspended runstate
migration: notifier error reporting
vfio: allow cpr-reboot migration if suspended

Mark Kanda, Steve Sistare (2):
vhost: reset vhost devices for cpr
chardev: cpr for sockets

  MAINTAINERS                         |  14 ++
  accel/xen/xen-all.c                 |   3 +
  backends/hostmem-epc.c              |  18 +-
  backends/hostmem-file.c             |   1 +
  backends/hostmem-memfd.c            |  22 ++-
  backends/tpm/tpm_emulator.c         |  11 +-
  block/parallels.c                   |   7 +-
  block/qcow.c                        |   7 +-
  block/vdi.c                         |   7 +-
  block/vhdx.c                        |   7 +-
  block/vmdk.c                        |   7 +-
  block/vpc.c                         |   7 +-
  block/vvfat.c                       |   7 +-
  chardev/char-mux.c                  |   1 +
  chardev/char-null.c                 |   1 +
  chardev/char-pty.c                  |  16 +-
  chardev/char-serial.c               |   1 +
  chardev/char-socket.c               |  48 +++++
  chardev/char-stdio.c                |  31 +++
  chardev/char.c                      |  49 ++++-
  dump/dump.c                         |   4 +-
  gdbstub.c                           |   1 +
  hmp-commands.hx                     |   2 +-
  hw/9pfs/9p.c                        |  11 +-
  hw/core/qdev-properties-system.c    |  12 ++
  hw/core/qdev-properties.c           |  44 +++++
  hw/display/virtio-gpu-base.c        |   8 +-
  hw/intc/arm_gic_kvm.c               |   3 +-
  hw/intc/arm_gicv3_its_kvm.c         |   3 +-
  hw/intc/arm_gicv3_kvm.c             |   3 +-
  hw/misc/ivshmem.c                   |   8 +-
  hw/net/virtio-net.c                 |  10 +-
  hw/pci/msix.c                       |   2 +-
  hw/pci/pci.c                        |  12 ++
  hw/ppc/pef.c                        |   2 +-
  hw/ppc/spapr.c                      |   2 +-
  hw/ppc/spapr_events.c               |   2 +-
  hw/ppc/spapr_rtas.c                 |   2 +-
  hw/remote/proxy.c                   |   7 +-
  hw/s390x/s390-virtio-ccw.c          |   9 +-
  hw/scsi/vhost-scsi.c                |   9 +-
  hw/vfio/common.c                    | 235 +++++++++++++++++++----
  hw/vfio/cpr.c                       | 177 ++++++++++++++++++
  hw/vfio/meson.build                 |   1 +
  hw/vfio/migration.c                 |  23 +--
  hw/vfio/pci.c                       | 336 ++++++++++++++++++++++++++++-----
  hw/vfio/trace-events                |   1 +
  hw/virtio/vhost-vdpa.c              |   6 +-
  hw/virtio/vhost.c                   |  32 +++-
  include/chardev/char-socket.h       |   1 +
  include/chardev/char.h              |   5 +
  include/exec/memory.h               |  48 +++++
  include/exec/ram_addr.h             |   1 +
  include/exec/ramblock.h             |   1 +
  include/hw/pci/msix.h               |   1 +
  include/hw/qdev-properties-system.h |   4 +
  include/hw/qdev-properties.h        |   3 +
  include/hw/vfio/vfio-common.h       |  12 ++
  include/hw/virtio/vhost.h           |   1 +
  include/migration/blocker.h         |  69 ++++++-
  include/migration/cpr-state.h       |  30 +++
  include/migration/cpr.h             |  20 ++
  include/migration/misc.h            |  13 +-
  include/migration/vmstate.h         |   2 +
  include/qapi/util.h                 |  28 +++
  include/qemu/osdep.h                |   9 +
  include/sysemu/runstate.h           |   2 +
  migration/cpr-state.c               | 362 ++++++++++++++++++++++++++++++++++++
  migration/cpr.c                     |  85 +++++++++
  migration/file.c                    |  62 ++++++
  migration/file.h                    |  14 ++
  migration/meson.build               |   3 +
  migration/migration.c               | 268 +++++++++++++++++++++++---
  migration/ram.c                     |  24 ++-
  migration/target.c                  |   1 +
  migration/trace-events              |  12 ++
  monitor/hmp-cmds.c                  |  59 +++---
  monitor/hmp.c                       |   3 +
  monitor/qmp.c                       |   4 +
  python/qemu/machine/machine.py      |  14 ++
  qapi/char.json                      |   7 +-
  qapi/migration.json                 |  68 ++++++-
  qapi/qapi-util.c                    |  37 ++++
  qemu-options.hx                     |  50 ++++-
  replay/replay.c                     |   4 +
  softmmu/memory.c                    |  31 ++-
  softmmu/physmem.c                   | 100 +++++++++-
  softmmu/runstate.c                  |  42 ++++-
  softmmu/vl.c                        |  10 +
  stubs/cpr-state.c                   |  26 +++
  stubs/meson.build                   |   2 +
  stubs/migr-blocker.c                |   9 +-
  stubs/migration.c                   |  33 ++++
  target/i386/kvm/kvm.c               |   8 +-
  target/i386/nvmm/nvmm-all.c         |   4 +-
  target/i386/sev.c                   |   2 +-
  target/i386/whpx/whpx-all.c         |   3 +-
  tests/avocado/cpr.py                | 176 ++++++++++++++++++
  tests/unit/meson.build              |   1 +
  tests/unit/test-strlist.c           |  81 ++++++++
  trace-events                        |   1 +
  ui/spice-core.c                     |   5 +-
  ui/vdagent.c                        |   5 +-
  util/oslib-posix.c                  |   9 +
  util/oslib-win32.c                  |   4 +
  105 files changed, 2781 insertions(+), 330 deletions(-)

create mode 100644 hw/vfio/cpr.c
create mode 100644 include/migration/cpr-state.h create mode 100644 include/migration/cpr.h create mode 100644 migration/cpr-state.c create mode 100644 migration/cpr.c create mode 100644 migration/file.c create mode 100644 migration/file.h create mode 100644 stubs/cpr-state.c create mode 100644 stubs/migration.c create mode 100644 tests/avocado/cpr.py create mode 100644 tests/unit/test-strlist.c

#65 lore raw
Quoted 5 lines · Michael Galaxy

Another option could be to expose "-migrate-mode-disable" (instead of enable) and just enable all 3 modes by default, since we are already required to switch from "normal" mode to a CPR-specific mode when it is time to do a live update, if the intention is to preserve the capability to completely prevent a running QEMU from using these modes before the VM starts up.

Quoted 19 lines

- Michael

Quoted 16 lines · Michael Galaxy

Hi Steve,

In the current design you have, we have to specify both the command line parameter "-migrate-mode-enable cpr-reboot" *and* issue the monitor command "migrate_set_parameter mode cpr-${mode}".

Is it possible to opt-in to the CPR mode just once over the monitor instead of having to specify it twice on the command line? This would also match the live migration model: You do not need to necessarily "opt in" to live migration mode through a command line parameter, you simply request it when you need to. Can CPR behave the same way?

This would also make switching over to a CPR-capable version of QEMU much simpler and would even make it work for existing libvirt-managed guests as their command line parameters would no longer need to change. This would allow us to simply power-off and power-on existing VMs to make them CPR-capable and then work on a libvirt patch later when we're ready to do so.

Comments?

Hi Michael,
Requiring -migrate-enable-mode allows qemu to initialize objects
differently, if necessary, so that migration for a mode is not blocked. See callers of migrate_mode_enabled. There is only one so far, in ram_block_add. If the mode is cpr-exec, then it creates anonymous ram blocks using memfd_create, else using MAP_ANON. In the V7 series, this was controlled by a '-machine memfd-alloc=on' option.

migrate-enable-mode is more future proof for the user. If something new must initialize differently to support cpr, then it adds a call to migrate_mode_enabled, and the command line remains the same. However, I could be persuaded to go either way.

A secondary reason for -migrate-enable-mode is to support the only-cpr-capable option. It needs to know which mode will be used, in order to check a mode-specific blocker list.

- Steve

#66 lore raw

Hi Steve,

Quoted 33 lines · Steven Sistare
Quoted 5 lines · Michael Galaxy

Another option could be to expose "-migrate-mode-disable" (instead of enable) and just enable all 3 modes by default, since we are already required to switch from "normal" mode to a CPR-specific mode when it is time to do a live update, if the intention is to preserve the capability to completely prevent a running QEMU from using these modes before the VM starts up.

Quoted 19 lines

- Michael

Quoted 16 lines · Michael Galaxy

Hi Steve,

In the current design you have, we have to specify both the command line parameter "-migrate-mode-enable cpr-reboot" *and* issue the monitor command "migrate_set_parameter mode cpr-${mode}".

Is it possible to opt-in to the CPR mode just once over the monitor instead of having to specify it twice on the command line? This would also match the live migration model: You do not need to necessarily "opt in" to live migration mode through a command line parameter, you simply request it when you need to. Can CPR behave the same way?

This would also make switching over to a CPR-capable version of QEMU much simpler and would even make it work for existing libvirt-managed guests as their command line parameters would no longer need to change. This would allow us to simply power-off and power-on existing VMs to make them CPR-capable and then work on a libvirt patch later when we're ready to do so.

Comments?

Hi Michael,
Requiring -migrate-enable-mode allows qemu to initialize objects
differently, if necessary, so that migration for a mode is not blocked. See callers of migrate_mode_enabled. There is only one so far, in ram_block_add. If the mode is cpr-exec, then it creates anonymous ram blocks using memfd_create, else using MAP_ANON. In the V7 series, this was controlled by a '-machine memfd-alloc=on' option.

Quoted 3 lines

migrate-enable-mode is more future proof for the user. If something new must initialize differently to support cpr, then it adds a call to migrate_mode_enabled, and the command line remains the same. However, I could be persuaded to go either way.

OK, so it is cpr-exec that needs this option (because of ram block allocation), not really cpr-reboot. Could the option then be made to only be required for cpr-exec and not cpr-reboot, then, since cpr-reboot doesn't require that consideration?

Quoted 3 lines

A secondary reason for -migrate-enable-mode is to support the only-cpr-capable option. It needs to know which mode will be used, in order to check a mode-specific blocker list.

Still, only-cpr-capable is also optional. If and only if one needs this option, the mode could be specified as part of the option itself, rather than requiring an extra command line parameter, no?

Further, in many clouds (including ours), our VM-management is generational with the development of the software versioning, so we *always* run tests and know whether or not a VM is CPR-capable.

If it is not CPR-capable, we would never run the VM in the first place, which means we would never really use that option at all.

I do see the appeal of the option, but could we simplify it, since it is opt-in?

- Michael

Quoted 2 lines

- Steve

#67 lore raw
Quoted 35 lines · Michael Galaxy

Hi Steve,

Quoted 33 lines · Steven Sistare
Quoted 5 lines · Michael Galaxy

Another option could be to expose "-migrate-mode-disable" (instead of enable) and just enable all 3 modes by default, since we are already required to switch from "normal" mode to a CPR-specific mode when it is time to do a live update, if the intention is to preserve the capability to completely prevent a running QEMU from using these modes before the VM starts up.

Quoted 19 lines

- Michael

Quoted 16 lines · Michael Galaxy

Hi Steve,

In the current design you have, we have to specify both the command line parameter "-migrate-mode-enable cpr-reboot" *and* issue the monitor command "migrate_set_parameter mode cpr-${mode}".

Is it possible to opt-in to the CPR mode just once over the monitor instead of having to specify it twice on the command line? This would also match the live migration model: You do not need to necessarily "opt in" to live migration mode through a command line parameter, you simply request it when you need to. Can CPR behave the same way?

This would also make switching over to a CPR-capable version of QEMU much simpler and would even make it work for existing libvirt-managed guests as their command line parameters would no longer need to change. This would allow us to simply power-off and power-on existing VMs to make them CPR-capable and then work on a libvirt patch later when we're ready to do so.

Comments?

Hi Michael,
   Requiring -migrate-enable-mode allows qemu to initialize objects differently, if necessary, so that migration for a mode is not blocked. See callers of migrate_mode_enabled.  There is only one so far, in ram_block_add.  If the mode is cpr-exec, then it creates anonymous ram blocks using memfd_create, else using MAP_ANON.  In the V7 series, this was controlled by a '-machine memfd-alloc=on' option.

Quoted 7 lines
Quoted 3 lines

migrate-enable-mode is more future proof for the user.  If something new must initialize differently to support cpr, then it adds a call to migrate_mode_enabled, and the command line remains the same.  However, I could be persuaded to go either way.

OK, so it is cpr-exec that needs this option (because of ram block allocation), not really cpr-reboot. Could the option then be made to only be required for cpr-exec and not cpr-reboot, then, since cpr-reboot doesn't require that consideration?

In a different forum Juan said this is a memory issue, so it should be expressed as a memory related option. So, I will delete -migrate-enable-mode and revert back to -machine memfd-alloc, as defined in the V7 patch series.

Quoted 6 lines
Quoted 3 lines

A secondary reason for -migrate-enable-mode is to support the only-cpr-capable option.  It needs to know which mode will be used, in order to check a mode-specific blocker list.

Still, only-cpr-capable is also optional. If and only if one needs this option, the mode could be specified as part of the option itself, rather than requiring an extra command line parameter, no?

Yes, I will make that change.

- Steve

Quoted 7 lines

Further, in many clouds (including ours), our VM-management is generational with the development of the software versioning, so we *always* run tests and know whether or not a VM is CPR-capable.

If it is not CPR-capable, we would never run the VM in the first place, which means we would never really use that option at all.

I do see the appeal of the option, but could we simplify it, since it is opt-in?

#68 lore raw

 Good morning,

Quoted 39 lines · Steven Sistare
Quoted 38 lines · Michael Galaxy

Hi Steve,

Quoted 36 lines · Steven Sistare
Quoted 5 lines · Michael Galaxy

Another option could be to expose "-migrate-mode-disable" (instead of enable) and just enable all 3 modes by default, since we are already required to switch from "normal" mode to a CPR-specific mode when it is time to do a live update, if the intention is to preserve the capability to completely prevent a running QEMU from using these modes before the VM starts up.

Quoted 19 lines

- Michael

Quoted 16 lines · Michael Galaxy

Hi Steve,

In the current design you have, we have to specify both the command line parameter "-migrate-mode-enable cpr-reboot" *and* issue the monitor command "migrate_set_parameter mode cpr-${mode}".

Is it possible to opt-in to the CPR mode just once over the monitor instead of having to specify it twice on the command line? This would also match the live migration model: You do not need to necessarily "opt in" to live migration mode through a command line parameter, you simply request it when you need to. Can CPR behave the same way?

This would also make switching over to a CPR-capable version of QEMU much simpler and would even make it work for existing libvirt-managed guests as their command line parameters would no longer need to change. This would allow us to simply power-off and power-on existing VMs to make them CPR-capable and then work on a libvirt patch later when we're ready to do so.

Comments?

Hi Michael,
   Requiring -migrate-enable-mode allows qemu to initialize objects
differently, if necessary, so that migration for a mode is not blocked. See callers of migrate_mode_enabled.  There is only one so far, in ram_block_add.  If the mode is cpr-exec, then it creates anonymous ram blocks using memfd_create, else using MAP_ANON.  In the V7 series, this was controlled by a '-machine memfd-alloc=on' option.

migrate-enable-mode is more future proof for the user.  If something new must initialize differently to support cpr, then it adds a call to migrate_mode_enabled, and the command line remains the same.  However, I could be persuaded to go either way.

Quoted 6 lines
Quoted 3 lines

OK, so it is cpr-exec that needs this option (because of ram block allocation), not really cpr-reboot. Could the option then be made to only be required for cpr-exec and not cpr-reboot, then, since cpr-reboot doesn't require that consideration?

In a different forum Juan said this is a memory issue, so it should be expressed as a memory related option. So, I will delete -migrate-enable-mode and revert back to -machine memfd-alloc, as defined in the V7 patch series.

Acknowledged. I'm going to try to get my reviewed-by's in soon. Sorry I haven't done it sooner.

We've finished testing these patches on our systems and are moving forward.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

A secondary reason for -migrate-enable-mode is to support the only-cpr-capable option.  It needs to know which mode will be used, in order to check a mode-specific blocker list.

Still, only-cpr-capable is also optional. If and only if one needs this option, the mode could be specified as part of the option itself, rather than requiring an extra command line parameter, no?

Yes, I will make that change.

- Steve

Acknowledged.