Skip to contentSkip to conversation

Live update: cpr-exec

PATCH In review Steve Sistare 7 participants 77 messages Jun 30, 2024 – Sep 5, 2024 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover Live update: cpr-exec

What?

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)
* anon-alloc (command-line option for -machine)

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Why?

This mode has less impact on the guest than any other method of updating in place. The pause time is much lower, because devices need not be torn down and recreated, DMA does not need to be drained and quiesced, and minimal state is copied to new QEMU. Further, there are no constraints on the guest. By contrast, cpr-reboot mode requires the guest to support S3 suspend-to-ram, and suspending plus resuming vfio devices adds multiple seconds to the guest pause time. Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

These benefits all derive from the core design principle of this mode, which is preserving open descriptors. This approach is very general and can be used to support a wide variety of devices that do not have hardware support for live migration, including but not limited to: vfio, chardev, vhost, vdpa, and iommufd. Some devices need new kernel software interfaces to allow a descriptor to be used in a process that did not originally open it.

In a containerized QEMU environment, cpr-exec reuses an existing QEMU container and its assigned resources. By contrast, consider a design in which a new container is created on the same host as the target of the CPR operation. Resources must be reserved for the new container, while the old container still reserves resources until the operation completes. Avoiding over commitment requires extra work in the management layer. This is one reason why a cloud provider may prefer cpr-exec. A second reason is that the container may include agents with their own connections to the outside world, and such connections remain intact if the container is reused.

How?

All memory that is mapped by the guest is preserved in place. Indeed, it must be, because it may be the target of DMA requests, which are not quiesced during cpr-exec. All such memory must be mmap'able in new QEMU. This is easy for named memory-backend objects, as long as they are mapped shared, because they are visible in the file system in both old and new QEMU. Anonymous memory must be allocated using memfd_create rather than MAP_ANON, so the memfd's can be sent to new QEMU. Pages that were locked in memory for DMA in old QEMU remain locked in new QEMU, because the descriptor of the device that locked them remains open.

cpr-exec preserves descriptors across exec by clearing the CLOEXEC flag, and by sending the unique name and value of each descriptor to new QEMU via CPR state.

For device descriptors, new QEMU reuses the descriptor when creating the device, rather than opening it again. The same holds for chardevs. For memfd descriptors, new QEMU mmap's the preserved memfd when a ramblock is created.

CPR state cannot be sent over the normal migration channel, because devices and backends are created prior to reading the channel, so this mode sends CPR state over a second migration channel that is not visible to the user. New QEMU reads the second channel prior to creating devices or backends.

The exec itself is trivial. After writing to the migration channels, the migration code calls a new main-loop hook to perform the exec.

Example:

In this example, we simply restart the same version of QEMU, but in a real scenario one would use a new QEMU binary path in cpr-exec-command.

  # qemu-kvm -monitor stdio -object
  memory-backend-file,id=ram0,size=4G,mem-path=/dev/shm/ram0,share=on
  -m 4G -machine anon-alloc=memfd ...

QEMU 9.1.50 monitor - type 'help' for more information
(qemu) info status
VM status: running
(qemu) migrate_set_parameter mode cpr-exec
(qemu) migrate_set_parameter cpr-exec-command qemu-kvm ... -incoming file:vm.state (qemu) migrate -d file:vm.state
(qemu) QEMU 9.1.50 monitor - type 'help' for more information
(qemu) info status
VM status: running

This patch series implements a minimal version of cpr-exec. Additional series are ready to be posted to deliver the complete vision described above, including
* vfio
* chardev
* vhost
* blockers
* hostmem-memfd
* migration-test cases

Works in progress include:
* vdpa
* iommufd
* cpr-transfer mode

Changes since V1:
* Dropped precreate and factory patches. Added CPR state instead.
* Dropped patches that refactor ramblock allocation
* Dropped vmstate_info_void patch (peter)
* Dropped patch "seccomp: cpr-exec blocker" (Daniel)
* Redefined memfd-alloc option as anon-alloc
* No longer preserve ramblock fields, except for fd (peter)
* Added fd preservation functions in CPR state
* Hoisted cpr code out of migrate_fd_cleanup (fabiano)
* Revised migration.json docs (markus)
* Fixed qtest failures (fabiano)
* Renamed SAVEVM_FOREACH macros (fabiano)
* Renamed cpr-exec-args as cpr-exec-command (markus)

The first 6 patches below are foundational and are needed for both cpr-exec mode and cpr-transfer mode. The last 5 patches are specific to cpr-exec and implement the mechanisms for sharing state across exec.

Steve Sistare (11):
machine: alloc-anon option
migration: cpr-state
migration: save cpr mode
migration: stop vm earlier for cpr
physmem: preserve ram blocks for cpr
migration: fix mismatched GPAs during cpr
oslib: qemu_clear_cloexec
vl: helper to request exec
migration: cpr-exec-command parameter
migration: cpr-exec save and load
migration: cpr-exec mode

 hmp-commands.hx                |   2 +-
 hw/core/machine.c              |  24 +++++
 include/exec/memory.h          |  12 +++
 include/hw/boards.h            |   1 +
 include/migration/cpr.h        |  35 ++++++
 include/qemu/osdep.h           |   9 ++
 include/sysemu/runstate.h      |   3 +
 migration/cpr-exec.c           | 180 +++++++++++++++++++++++++++++++
 migration/cpr.c                | 238 +++++++++++++++++++++++++++++++++++++++++
 migration/meson.build          |   2 +
 migration/migration-hmp-cmds.c |  25 +++++
 migration/migration.c          |  43 ++++++--
 migration/options.c            |  23 +++-
 migration/ram.c                |  17 +--
 migration/trace-events         |   5 +
 qapi/machine.json              |  14 +++
 qapi/migration.json            |  45 +++++++-
 qemu-options.hx                |  13 +++
 system/memory.c                |  22 +++-
 system/physmem.c               |  61 ++++++++++-
 system/runstate.c              |  29 +++++
 system/trace-events            |   3 +
 system/vl.c                    |   3 +
 util/oslib-posix.c             |   9 ++
 util/oslib-win32.c             |   4 +
 25 files changed, 792 insertions(+), 30 deletions(-)
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr-exec.c
 create mode 100644 migration/cpr.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/11 machine: alloc-anon option

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option, and various memory regions such as ROMs that are allocated when devices are created. This option does not affect memory-backend-file, memory-backend-memfd, or memory-backend-epc objects.

The memfd option is intended to support new migration modes, in which the memory region can be transferred in place to a new QEMU process, by sending the memfd file descriptor to the process. Memory contents are preserved, and if the mode also transfers device descriptors, then pages that are locked in memory for DMA remain locked. This behavior is a pre-requisite for supporting vfio, vdpa, and iommufd devices with the new modes.

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

---
 hw/core/machine.c   | 24 ++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 qapi/machine.json   | 14 ++++++++++++++
 qemu-options.hx     | 13 +++++++++++++
 system/memory.c     | 12 +++++++++---
 system/physmem.c    | 38 +++++++++++++++++++++++++++++++++++++-
 system/trace-events |  3 +++
 7 files changed, 101 insertions(+), 4 deletions(-)
hw/core/machine.c +24 -0
index 655d75c..7ca2ad0 100644
@@ -454,6 +454,20 @@ static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
     ms->mem_merge = value;
 }
 
+static int machine_get_anon_alloc(Object *obj, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    return ms->anon_alloc;
+}
+
+static void machine_set_anon_alloc(Object *obj, int value, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    ms->anon_alloc = value;
+}
+
 static bool machine_get_usb(Object *obj, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -1066,6 +1080,11 @@ static void machine_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, "mem-merge",
         "Enable/disable memory merge support");
 
+    object_class_property_add_enum(oc, "anon-alloc", "AnonAllocOption",
+                                   &AnonAllocOption_lookup,
+                                   machine_get_anon_alloc,
+                                   machine_set_anon_alloc);
+
     object_class_property_add_bool(oc, "usb",
         machine_get_usb, machine_set_usb);
     object_class_property_set_description(oc, "usb",
@@ -1416,6 +1435,11 @@ static bool create_default_memdev(MachineState *ms, const char *path, Error **er
     if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
         goto out;
     }
+    if (!object_property_set_bool(obj, "share",
+                                  ms->anon_alloc == ANON_ALLOC_OPTION_MEMFD,
+                                  errp)) {
+        goto out;
+    }
     object_property_add_child(object_get_objects_root(), mc->default_ram_id,
                               obj);
     /* Ensure backend's memory region name is equal to mc->default_ram_id */
include/hw/boards.h +1 -0
index 73ad319..77f16ad 100644
@@ -383,6 +383,7 @@ struct MachineState {
     bool enable_graphics;
     ConfidentialGuestSupport *cgs;
     HostMemoryBackend *memdev;
+    AnonAllocOption anon_alloc;
     /*
      * convenience alias to ram_memdev_id backend memory region
      * or to numa container memory region
qapi/machine.json +14 -0
index 2fd3e9c..9173953 100644
@@ -1881,3 +1881,17 @@
 { 'command': 'x-query-interrupt-controllers',
   'returns': 'HumanReadableText',
   'features': [ 'unstable' ]}
+
+##
+# @AnonAllocOption:
+#
+# An enumeration of the options for allocating anonymous guest memory.
+#
+# @mmap: allocate using mmap MAP_ANON
+#
+# @memfd: allocate using memfd_create
+#
+# Since: 9.1
+##
+{ 'enum': 'AnonAllocOption',
+  'data': [ 'mmap', 'memfd' ] }
qemu-options.hx +13 -0
index 8ca7f34..595b693 100644
@@ -38,6 +38,7 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                nvdimm=on|off controls NVDIMM support (default=off)\n"
     "                memory-encryption=@var{} memory encryption object to use (default=none)\n"
     "                hmat=on|off controls ACPI HMAT support (default=off)\n"
+    "                anon-alloc=mmap|memfd allocate anonymous guest RAM using mmap MAP_ANON or memfd_create (default: mmap)\n"
     "                memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n"
     "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n",
     QEMU_ARCH_ALL)
@@ -101,6 +102,18 @@ SRST
         Enables or disables ACPI Heterogeneous Memory Attribute Table
         (HMAT) support. The default is off.
 
+    ``anon-alloc=mmap|memfd``
+        Allocate anonymous guest RAM using mmap MAP_ANON (the default)
+        or memfd_create.  This affects memory-backend-ram objects,
+        RAM created with the global -m option but without an
+        associated memory-backend object and without the -mem-path
+        option, and various memory regions such as ROMs that are
+        allocated when devices are created.  This option does not
+        affect memory-backend-file, memory-backend-memfd, or
+        memory-backend-epc objects.
+
+        Some migration modes require anon-alloc=memfd.
+
     ``memory-backend='id'``
         An alternative to legacy ``-mem-path`` and ``mem-prealloc`` options.
         Allows to use a memory backend as main RAM.
system/memory.c +9 -3
index 2d69521..28a837d 100644
@@ -1552,8 +1552,10 @@ bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
                                       uint64_t size,
                                       Error **errp)
 {
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
     return memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                  size, 0, errp);
+                                                  size, flags, errp);
 }
 
 bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
@@ -1713,8 +1715,10 @@ bool memory_region_init_rom_nomigrate(MemoryRegion *mr,
                                       uint64_t size,
                                       Error **errp)
 {
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
     if (!memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                size, 0, errp)) {
+                                                size, flags, errp)) {
          return false;
     }
     mr->readonly = true;
@@ -1731,6 +1735,8 @@ bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
                                              Error **errp)
 {
     Error *err = NULL;
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
     assert(ops);
     memory_region_init(mr, owner, name, size);
     mr->ops = ops;
@@ -1738,7 +1744,7 @@ bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
     mr->terminates = true;
     mr->rom_device = true;
     mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc(size, 0, mr, &err);
+    mr->ram_block = qemu_ram_alloc(size, flags, mr, &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
system/physmem.c +37 -1
index 33d09f7..efe95ff 100644
@@ -47,6 +47,7 @@
 #include "qemu/qemu-print.h"
 #include "qemu/log.h"
 #include "qemu/memalign.h"
+#include "qemu/memfd.h"
 #include "exec/memory.h"
 #include "exec/ioport.h"
 #include "sysemu/dma.h"
@@ -54,6 +55,7 @@
 #include "sysemu/hw_accel.h"
 #include "sysemu/xen-mapcache.h"
 #include "trace/trace-root.h"
+#include "trace.h"
 
 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
 #include <linux/falloc.h>
@@ -69,6 +71,8 @@
 
 #include "qemu/pmem.h"
 
+#include "qapi/qapi-types-migration.h"
+#include "migration/options.h"
 #include "migration/vmstate.h"
 
 #include "qemu/range.h"
@@ -1828,6 +1832,32 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
                 qemu_mutex_unlock_ramlist();
                 return;
             }
+
+        } else if (new_block->flags & RAM_SHARED) {
+            size_t max_length = new_block->max_length;
+            MemoryRegion *mr = new_block->mr;
+            const char *name = memory_region_name(mr);
+
+            new_block->mr->align = QEMU_VMALLOC_ALIGN;
+
+            if (new_block->fd == -1) {
+                new_block->fd = qemu_memfd_create(name, max_length + mr->align,
+                                                  0, 0, 0, errp);
+            }
+
+            if (new_block->fd >= 0) {
+                int mfd = new_block->fd;
+                qemu_set_cloexec(mfd);
+                new_block->host = file_ram_alloc(new_block, max_length, mfd,
+                                                 false, 0, errp);
+            }
+            if (!new_block->host) {
+                qemu_mutex_unlock_ramlist();
+                return;
+            }
+            memory_try_enable_merging(new_block->host, new_block->max_length);
+            free_on_error = true;
+
         } else {
             new_block->host = qemu_anon_ram_alloc(new_block->max_length,
                                                   &new_block->mr->align,
@@ -1911,6 +1941,9 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         ram_block_notify_add(new_block->host, new_block->used_length,
                              new_block->max_length);
     }
+    trace_ram_block_add(memory_region_name(new_block->mr), new_block->flags,
+                        new_block->fd, new_block->used_length,
+                        new_block->max_length);
     return;
 
 out_free:
@@ -2097,8 +2130,11 @@ RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
                                                      void *host),
                                      MemoryRegion *mr, Error **errp)
 {
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
+    flags |= RAM_RESIZEABLE;
     return qemu_ram_alloc_internal(size, maxsz, resized, NULL,
-                                   RAM_RESIZEABLE, mr, errp);
+                                   flags, mr, errp);
 }
 
 static void reclaim_ramblock(RAMBlock *block)
system/trace-events +3 -0
index 69c9044..f8ebf42 100644
@@ -38,3 +38,6 @@ dirtylimit_state_finalize(void)
 dirtylimit_throttle_pct(int cpu_index, uint64_t pct, int64_t time_us) "CPU[%d] throttle percent: %" PRIu64 ", throttle adjust time %"PRIi64 " us"
 dirtylimit_set_vcpu(int cpu_index, uint64_t quota) "CPU[%d] set dirty page rate limit %"PRIu64
 dirtylimit_vcpu_execute(int cpu_index, int64_t sleep_time_us) "CPU[%d] sleep %"PRIi64 " us"
+
+#physmem.c
+ram_block_add(const char *name, uint32_t flags, int fd, size_t used_length, size_t max_length) "%s, flags %u, fd %d, len %lu, maxlen %lu"
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/11
#2 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 15 lines · Steve Sistare

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option, and various memory regions such as ROMs that are allocated when devices are created. This option does not affect memory-backend-file, memory-backend-memfd, or memory-backend-epc objects.

The memfd option is intended to support new migration modes, in which the memory region can be transferred in place to a new QEMU process, by sending the memfd file descriptor to the process. Memory contents are preserved, and if the mode also transfers device descriptors, then pages that are locked in memory for DMA remain locked. This behavior is a pre-requisite for supporting vfio, vdpa, and iommufd devices with the new modes.

Quoted 8 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

The commit message is inconsistent with alloc-anon, anon-alloc.

#3 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

In reply to Steve Sistare
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 2 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

Quoted 267 lines
---
 hw/core/machine.c   | 24 ++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 qapi/machine.json   | 14 ++++++++++++++
 qemu-options.hx     | 13 +++++++++++++
 system/memory.c     | 12 +++++++++---
 system/physmem.c    | 38 +++++++++++++++++++++++++++++++++++++-
 system/trace-events |  3 +++
 7 files changed, 101 insertions(+), 4 deletions(-)
hw/core/machine.c +24 -0
index 655d75c..7ca2ad0 100644
@@ -454,6 +454,20 @@ static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
     ms->mem_merge = value;
 }
 
+static int machine_get_anon_alloc(Object *obj, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    return ms->anon_alloc;
+}
+
+static void machine_set_anon_alloc(Object *obj, int value, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    ms->anon_alloc = value;
+}
+
 static bool machine_get_usb(Object *obj, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -1066,6 +1080,11 @@ static void machine_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, "mem-merge",
         "Enable/disable memory merge support");
 
+    object_class_property_add_enum(oc, "anon-alloc", "AnonAllocOption",
+                                   &AnonAllocOption_lookup,
+                                   machine_get_anon_alloc,
+                                   machine_set_anon_alloc);
+
     object_class_property_add_bool(oc, "usb",
         machine_get_usb, machine_set_usb);
     object_class_property_set_description(oc, "usb",
@@ -1416,6 +1435,11 @@ static bool create_default_memdev(MachineState *ms, const char *path, Error **er
     if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
         goto out;
     }
+    if (!object_property_set_bool(obj, "share",
+                                  ms->anon_alloc == ANON_ALLOC_OPTION_MEMFD,
+                                  errp)) {
+        goto out;
+    }
     object_property_add_child(object_get_objects_root(), mc->default_ram_id,
                               obj);
     /* Ensure backend's memory region name is equal to mc->default_ram_id */
include/hw/boards.h +1 -0
index 73ad319..77f16ad 100644
@@ -383,6 +383,7 @@ struct MachineState {
     bool enable_graphics;
     ConfidentialGuestSupport *cgs;
     HostMemoryBackend *memdev;
+    AnonAllocOption anon_alloc;
     /*
      * convenience alias to ram_memdev_id backend memory region
      * or to numa container memory region
qapi/machine.json +14 -0
index 2fd3e9c..9173953 100644
@@ -1881,3 +1881,17 @@
 { 'command': 'x-query-interrupt-controllers',
   'returns': 'HumanReadableText',
   'features': [ 'unstable' ]}
+
+##
+# @AnonAllocOption:
+#
+# An enumeration of the options for allocating anonymous guest memory.
+#
+# @mmap: allocate using mmap MAP_ANON
+#
+# @memfd: allocate using memfd_create
+#
+# Since: 9.1
+##
+{ 'enum': 'AnonAllocOption',
+  'data': [ 'mmap', 'memfd' ] }
qemu-options.hx +13 -0
index 8ca7f34..595b693 100644
@@ -38,6 +38,7 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                nvdimm=on|off controls NVDIMM support (default=off)\n"
     "                memory-encryption=@var{} memory encryption object to use (default=none)\n"
     "                hmat=on|off controls ACPI HMAT support (default=off)\n"
+    "                anon-alloc=mmap|memfd allocate anonymous guest RAM using mmap MAP_ANON or memfd_create (default: mmap)\n"
     "                memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n"
     "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n",
     QEMU_ARCH_ALL)
@@ -101,6 +102,18 @@ SRST
         Enables or disables ACPI Heterogeneous Memory Attribute Table
         (HMAT) support. The default is off.
 
+    ``anon-alloc=mmap|memfd``
+        Allocate anonymous guest RAM using mmap MAP_ANON (the default)
+        or memfd_create.  This affects memory-backend-ram objects,
+        RAM created with the global -m option but without an
+        associated memory-backend object and without the -mem-path
+        option, and various memory regions such as ROMs that are
+        allocated when devices are created.  This option does not
+        affect memory-backend-file, memory-backend-memfd, or
+        memory-backend-epc objects.
+
+        Some migration modes require anon-alloc=memfd.
+
     ``memory-backend='id'``
         An alternative to legacy ``-mem-path`` and ``mem-prealloc`` options.
         Allows to use a memory backend as main RAM.
system/memory.c +9 -3
index 2d69521..28a837d 100644
@@ -1552,8 +1552,10 @@ bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
                                       uint64_t size,
                                       Error **errp)
 {
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
     return memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                  size, 0, errp);
+                                                  size, flags, errp);
 }
 
 bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
@@ -1713,8 +1715,10 @@ bool memory_region_init_rom_nomigrate(MemoryRegion *mr,
                                       uint64_t size,
                                       Error **errp)
 {
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
     if (!memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                size, 0, errp)) {
+                                                size, flags, errp)) {
          return false;
     }
     mr->readonly = true;
@@ -1731,6 +1735,8 @@ bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
                                              Error **errp)
 {
     Error *err = NULL;
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
     assert(ops);
     memory_region_init(mr, owner, name, size);
     mr->ops = ops;
@@ -1738,7 +1744,7 @@ bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
     mr->terminates = true;
     mr->rom_device = true;
     mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc(size, 0, mr, &err);
+    mr->ram_block = qemu_ram_alloc(size, flags, mr, &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
system/physmem.c +37 -1
index 33d09f7..efe95ff 100644
@@ -47,6 +47,7 @@
 #include "qemu/qemu-print.h"
 #include "qemu/log.h"
 #include "qemu/memalign.h"
+#include "qemu/memfd.h"
 #include "exec/memory.h"
 #include "exec/ioport.h"
 #include "sysemu/dma.h"
@@ -54,6 +55,7 @@
 #include "sysemu/hw_accel.h"
 #include "sysemu/xen-mapcache.h"
 #include "trace/trace-root.h"
+#include "trace.h"
 
 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
 #include <linux/falloc.h>
@@ -69,6 +71,8 @@
 
 #include "qemu/pmem.h"
 
+#include "qapi/qapi-types-migration.h"
+#include "migration/options.h"
 #include "migration/vmstate.h"
 
 #include "qemu/range.h"
@@ -1828,6 +1832,32 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
                 qemu_mutex_unlock_ramlist();
                 return;
             }
+
+        } else if (new_block->flags & RAM_SHARED) {
+            size_t max_length = new_block->max_length;
+            MemoryRegion *mr = new_block->mr;
+            const char *name = memory_region_name(mr);
+
+            new_block->mr->align = QEMU_VMALLOC_ALIGN;
+
+            if (new_block->fd == -1) {
+                new_block->fd = qemu_memfd_create(name, max_length + mr->align,
+                                                  0, 0, 0, errp);
+            }
+
+            if (new_block->fd >= 0) {
+                int mfd = new_block->fd;
+                qemu_set_cloexec(mfd);
+                new_block->host = file_ram_alloc(new_block, max_length, mfd,
+                                                 false, 0, errp);
+            }
+            if (!new_block->host) {
+                qemu_mutex_unlock_ramlist();
+                return;
+            }
+            memory_try_enable_merging(new_block->host, new_block->max_length);
+            free_on_error = true;
+
         } else {
             new_block->host = qemu_anon_ram_alloc(new_block->max_length,
                                                   &new_block->mr->align,
@@ -1911,6 +1941,9 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         ram_block_notify_add(new_block->host, new_block->used_length,
                              new_block->max_length);
     }
+    trace_ram_block_add(memory_region_name(new_block->mr), new_block->flags,
+                        new_block->fd, new_block->used_length,
+                        new_block->max_length);
     return;
 
 out_free:
@@ -2097,8 +2130,11 @@ RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
                                                      void *host),
                                      MemoryRegion *mr, Error **errp)
 {
+    uint32_t flags = (current_machine->anon_alloc == ANON_ALLOC_OPTION_MEMFD) ?
+                     RAM_SHARED : 0;
+    flags |= RAM_RESIZEABLE;
     return qemu_ram_alloc_internal(size, maxsz, resized, NULL,
-                                   RAM_RESIZEABLE, mr, errp);
+                                   flags, mr, errp);
 }
 
 static void reclaim_ramblock(RAMBlock *block)
system/trace-events +3 -0
index 69c9044..f8ebf42 100644
@@ -38,3 +38,6 @@ dirtylimit_state_finalize(void)
 dirtylimit_throttle_pct(int cpu_index, uint64_t pct, int64_t time_us) "CPU[%d] throttle percent: %" PRIu64 ", throttle adjust time %"PRIi64 " us"
 dirtylimit_set_vcpu(int cpu_index, uint64_t quota) "CPU[%d] set dirty page rate limit %"PRIu64
 dirtylimit_vcpu_execute(int cpu_index, int64_t sleep_time_us) "CPU[%d] sleep %"PRIi64 " us"
+
+#physmem.c
+ram_block_add(const char *name, uint32_t flags, int fd, size_t used_length, size_t max_length) "%s, flags %u, fd %d, len %lu, maxlen %lu"
#4 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 19 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

Quoted 8 lines

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 2 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

I shared the same concern when reviewing the previous version, and I keep having so.

Quoted 4 lines

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

ROMs should be fine IIUC, as they shouldn't be large, and they can be migrated normally (because they're not DMA target from VFIO assigned devices). IOW, per my understanding what must be shared via memfd is writable memories that can be DMAed from a VFIO device.

I raised such question on whether / why vram can be a DMA target, but I didn't get a response. So I would like to redo this comment: I think we should figure out what is missing when we switch all backends to use -object, rather than adding this flag easily. When added, we should be crystal clear on which RAM region will be applicable by this flag.

PS to Steve: and I think I left tons of other comments in previous version outside this patch too, but I don't think they're fully discussed when this series was sent. I can re-read the series again, but I don't think it'll work out if we keep skipping discussions..

Thanks,

Signature · 1 line
Peter Xu
#5 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

On 7/17/2024 3:24 PM, Peter Xu wrote:
[...]

Quoted 5 lines

PS to Steve: and I think I left tons of other comments in previous version outside this patch too, but I don't think they're fully discussed when this series was sent. I can re-read the series again, but I don't think it'll work out if we keep skipping discussions..

Hi Peter, let me address this part first, because I don't want you to think that I ignored your questions and concerns. This V2 series tries to address them. The change log was intended to be my response, rather than responding to each open question individually, but let me try again here with more detail. I apologize if I don't summarize your concerns correctly or completely.

issue: discomfort with exec. why is it needed?
response: exec is just a transport mechanism to send fd's to new qemu.
I refactored to separate core patches from exec-specific patches, submitted cpr-transfer patches to illustrate a non-exec method, and provided reasons why one vs the other would be desirable in the commit messages and cover letter.

issue: why do we need to preserve the ramblock fields and make them available
prior to object creation?
response. we don't need to preserve all of them, and we only need fd prior
to object creation, so I deleted the precreate, factory, and named object patches, and added CprState to preserve fd's. used_length arrives in the normal migration stream. max_length is recovered from the mfd using lseek.

issue: the series is too large, with too much change.
response: in addition to the deletions mentioned above, I simplified the
functionality and tossed out style patches and nice-to-haves, so we can focus on core functionality. V2 is much smaller.

issue: memfd_create option is oddly expressed and hard to understand.
response: I redefined the option, deleted all the stylistic ramblock patches
to lay its workings bare, and explicitly documented its affect on all types of memory in the commit messages and qapi documentation.

issue: no need to preserve blocks like ROM for DMA (with memfd_create).
Blocks that must be preserved should be surfaced to the user as objects.
response: I disagree, and will continue that conversation in this email thread.

issue: how will vfio be handled?
response: I submitted the vfio patches (non-trivial, because first I had to
rework them without using precreate vmstate).

issue: how will fd's be preserved for chardevs?
response: via cpr_save_fd, CprState, and cpr_load_fd at device creation time,
in each device's creation function, just like vfio. Those primitives are defined in this V2 series.

- Steve

#6 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 42 lines · Steven Sistare

On 7/17/2024 3:24 PM, Peter Xu wrote:
[...]

Quoted 5 lines

PS to Steve: and I think I left tons of other comments in previous version outside this patch too, but I don't think they're fully discussed when this series was sent. I can re-read the series again, but I don't think it'll work out if we keep skipping discussions..

Hi Peter, let me address this part first, because I don't want you to think that I ignored your questions and concerns. This V2 series tries to address them. The change log was intended to be my response, rather than responding to each open question individually, but let me try again here with more detail. I apologize if I don't summarize your concerns correctly or completely.

issue: discomfort with exec. why is it needed?
response: exec is just a transport mechanism to send fd's to new qemu.
I refactored to separate core patches from exec-specific patches, submitted cpr-transfer patches to illustrate a non-exec method, and provided reasons why one vs the other would be desirable in the commit messages and cover letter.

issue: why do we need to preserve the ramblock fields and make them available
prior to object creation?
response. we don't need to preserve all of them, and we only need fd prior
to object creation, so I deleted the precreate, factory, and named object patches, and added CprState to preserve fd's. used_length arrives in the normal migration stream. max_length is recovered from the mfd using lseek.

issue: the series is too large, with too much change.
response: in addition to the deletions mentioned above, I simplified the
functionality and tossed out style patches and nice-to-haves, so we can focus on core functionality. V2 is much smaller.

issue: memfd_create option is oddly expressed and hard to understand.
response: I redefined the option, deleted all the stylistic ramblock patches
to lay its workings bare, and explicitly documented its affect on all types of memory in the commit messages and qapi documentation.

issue: no need to preserve blocks like ROM for DMA (with memfd_create).
Blocks that must be preserved should be surfaced to the user as objects.
response: I disagree, and will continue that conversation in this email thread.

Quoted 8 lines

issue: how will vfio be handled?
response: I submitted the vfio patches (non-trivial, because first I had to
rework them without using precreate vmstate).

issue: how will fd's be preserved for chardevs?
response: via cpr_save_fd, CprState, and cpr_load_fd at device creation time,
in each device's creation function, just like vfio. Those primitives are defined in this V2 series.

Thanks for the answers. I think I'll need to read more into the patches in the next few days; it looks like I'll get more answers from there.

I just sent an email probably when you're drafting this one.. it may has some questions that may not be covered here.

I think a major issue with exec() is the (1-3) steps that I mentioned there that needs to run sequentially, and IIUC all these steps can be completely avoided in cpr-transfer, and it may matter a lot in huge VMs. But maybe I missed something.

Please have a look there.

Signature · 1 line
Peter Xu
#7 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

In reply to Peter Xu
Quoted 42 lines · Peter Xu
Quoted 19 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

Quoted 8 lines

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 2 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

I shared the same concern when reviewing the previous version, and I keep having so.

Quoted 4 lines

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

ROMs should be fine IIUC, as they shouldn't be large, and they can be migrated normally (because they're not DMA target from VFIO assigned devices). IOW, per my understanding what must be shared via memfd is writable memories that can be DMAed from a VFIO device.

Quoted 5 lines

I raised such question on whether / why vram can be a DMA target, but I didn't get a response. So I would like to redo this comment: I think we should figure out what is missing when we switch all backends to use -object, rather than adding this flag easily. When added, we should be crystal clear on which RAM region will be applicable by this flag.

All RAM regions that are mapped by the guest are registered for vfio DMA by a memory listener and could potentially be DMA'd, either read or written. That is defined by the architecture. We are not allowed to make value judgements and decide to not support the architecture for some segments such as ROM.

Alex Williamson, any comment here?

- Steve

#8 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 49 lines · Steven Sistare
Quoted 42 lines · Peter Xu
Quoted 19 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

Quoted 8 lines

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 2 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

I shared the same concern when reviewing the previous version, and I keep having so.

Quoted 4 lines

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

ROMs should be fine IIUC, as they shouldn't be large, and they can be migrated normally (because they're not DMA target from VFIO assigned devices). IOW, per my understanding what must be shared via memfd is writable memories that can be DMAed from a VFIO device.

Quoted 5 lines

I raised such question on whether / why vram can be a DMA target, but I didn't get a response. So I would like to redo this comment: I think we should figure out what is missing when we switch all backends to use -object, rather than adding this flag easily. When added, we should be crystal clear on which RAM region will be applicable by this flag.

Quoted 5 lines

All RAM regions that are mapped by the guest are registered for vfio DMA by a memory listener and could potentially be DMA'd, either read or written. That is defined by the architecture. We are not allowed to make value judgements and decide to not support the architecture for some segments such as ROM.

You're right. However the problem is we have pretty good grasp of the major DMA target here (guest mem), so what I feel like is some missing work in this area, that we're not sure what this new parameter is applying to.

It's not the case where we know "OK we have a million use case of RAM, and we're 100% sure we want to make them all fd-based, and we introduce this flag simply because adding this to each 1-million will take years and thousands LOC changes".

The new parameter is cheap to paper over the question being raised here, but it definitely adds not only ambiguity (when it conflicts with -object) and that we'll need to maintain its compatibility for all RAMs that we have totally no idea what can be implied underneath for whatever QEMU cmdline that can be specified.

IMHO that, OTOH, justifies that there may need some further study to justify this parameter alone. For example, if it's only the vRAM that is missing, we may at least want to have a parameter nailing down to vRAM behavior rather than affecting anything, so as to at least avoid collision on -object parameters.

Thanks,

Signature · 1 line
Peter Xu
#9 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

In reply to Igor Mammedov
Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

Quoted 4 lines

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes. I dropped an an important adjective, "implicit".

"guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 7 lines
Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

memory-backend-ram is affected by alloc-anon. But in addition, the user must explicitly add the "share" option. I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

Quoted 9 lines
Quoted 2 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd. The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object. The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

- Steve

#10 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 24 lines · Steven Sistare
Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

Quoted 4 lines

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes. I dropped an an important adjective, "implicit".

"guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 5 lines
Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

Quoted 7 lines
Quoted 2 lines

so statement at the top that memory-backend-ram is affected is not really valid?

memory-backend-ram is affected by alloc-anon. But in addition, the user must explicitly add the "share" option. I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

Note that memory-backend-memfd uses "shared=on" as default, as using "shared=off" is something that shouldn't have ever been allowed. It can (and will) result in a double memory consumption.

One reason I also don't quite like this approach :/

Signature · 3 lines
Cheers,

David / dhildenb
#11 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

In reply to Steven Sistare
Quoted 24 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

Quoted 4 lines

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes. I dropped an an important adjective, "implicit".

"guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 5 lines
Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

Quoted 7 lines
Quoted 2 lines

so statement at the top that memory-backend-ram is affected is not really valid?

memory-backend-ram is affected by alloc-anon. But in addition, the user must explicitly add the "share" option. I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 19 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd. The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object. The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

- Steve

#12 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 30 lines · Igor Mammedov
Quoted 27 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property. This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

Quoted 4 lines

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes. I dropped an an important adjective, "implicit".

"guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 7 lines
Quoted 5 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared. Therefore, the implementation always sets

RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option. In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 24 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon. But in addition, the user must explicitly add the "share" option. I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 16 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd. The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object. The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback. The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects. OK, accepted. I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd). CPR will be blocked otherwise. The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only). The alloc-anon option is deleted. The logic in ram_block_add becomes:

    if (!new_block->host) {
        if (xen_enabled()) {
            ...
        } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent,
                                        TYPE_MEMORY_BACKEND)) {
            qemu_memfd_create()
        } else {
            qemu_anon_ram_alloc()
        }

Is that acceptable to everyone? Igor, Peter, Daniel?

- Steve

#13 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 76 lines · Steven Sistare
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 11 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property.  This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes.  I dropped an an important adjective, "implicit".

    "guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 6 lines
Quoted 4 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared.  Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option.  In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 24 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon.  But in addition, the user must explicitly add the "share" option.  I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 16 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends  should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd.  The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object.  The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback.  The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects.  OK, accepted.  I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd).  CPR will be blocked otherwise.  The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

    if (!new_block->host) {
        if (xen_enabled()) {
            ...
        } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent, TYPE_MEMORY_BACKEND)) { qemu_memfd_create()
        } else {
            qemu_anon_ram_alloc()
        }

Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

In a simple test here are the NON-memory-backend-object ramblocks which are allocated with memfd_create in my new proposal:

memfd_create system.flash0 3653632 @ 0x7fffe1000000 2 rw memfd_create system.flash1 540672 @ 0x7fffe0c00000 2 rw memfd_create pc.rom 131072 @ 0x7fffe0800000 2 rw memfd_create vga.vram 16777216 @ 0x7fffcac00000 2 rw memfd_create vga.rom 65536 @ 0x7fffe0400000 2 rw memfd_create /rom@etc/acpi/tables 2097152 @ 0x7fffca400000 6 rw memfd_create /rom@etc/table-loader 65536 @ 0x7fffca000000 6 rw memfd_create /rom@etc/acpi/rsdp 4096 @ 0x7fffc9c00000 6 rw

Of those, only a subset are mapped for DMA, per the existing QEMU logic, no changes from me:

dma_map: pc.rom 131072 @ 0x7fffe0800000 ro
dma_map: vga.vram 16777216 @ 0x7fffcac00000 rw
dma_map: vga.rom 65536 @ 0x7fffe0400000 ro
dma_map: 0000:3a:10.0 BAR 0 mmaps[0] 16384 @ 0x7ffff7fef000 rw
dma_map: 0000:3a:10.0 BAR 3 mmaps[0] 12288 @ 0x7ffff7fec000 rw

system.flash0 is excluded by the vfio listener because it is a rom_device. The rom@etc blocks are excluded because their MemoryRegions are not added to any container region, so the flatmem traversal of the AS used by the listener does not see them.

The BARs should not be mapped IMO, and I propose excluding them in the iommufd series:
https://lore.kernel.org/qemu-devel/[email protected]/

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration. Live migration copies the contents in the stream. Live update preserves the contents in place by preserving the memfd. Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

- Steve

#14 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 77 lines · Steven Sistare
Quoted 76 lines · Steven Sistare
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 11 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property.  This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes.  I dropped an an important adjective, "implicit".

    "guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 6 lines
Quoted 4 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared.  Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option.  In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 24 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon.  But in addition, the user must explicitly add the "share" option.  I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 16 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends  should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd.  The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object.  The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback.  The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects.  OK, accepted.  I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd).  CPR will be blocked otherwise.  The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

    if (!new_block->host) {
        if (xen_enabled()) {
            ...
        } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent, TYPE_MEMORY_BACKEND)) { qemu_memfd_create()
        } else {
            qemu_anon_ram_alloc()
        }

Quoted 1 line
Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

Sorry for a late reply.

I think this may not work as David pointed out? Where AFAIU it will switch many old anon use cases to use memfd, aka, shmem, and it might be problematic when share=off: we have double memory consumption issue with shmem with private mapping.

I assume that includes things like "-m", "memory-backend-ram", and maybe more. IIUC memory consumption of the VM will double with them.

Quoted 13 lines

In a simple test here are the NON-memory-backend-object ramblocks which are allocated with memfd_create in my new proposal:

memfd_create system.flash0 3653632 @ 0x7fffe1000000 2 rw memfd_create system.flash1 540672 @ 0x7fffe0c00000 2 rw memfd_create pc.rom 131072 @ 0x7fffe0800000 2 rw memfd_create vga.vram 16777216 @ 0x7fffcac00000 2 rw memfd_create vga.rom 65536 @ 0x7fffe0400000 2 rw memfd_create /rom@etc/acpi/tables 2097152 @ 0x7fffca400000 6 rw memfd_create /rom@etc/table-loader 65536 @ 0x7fffca000000 6 rw memfd_create /rom@etc/acpi/rsdp 4096 @ 0x7fffc9c00000 6 rw

Quoted 6 lines

Of those, only a subset are mapped for DMA, per the existing QEMU logic, no changes from me:

dma_map: pc.rom 131072 @ 0x7fffe0800000 ro
dma_map: vga.vram 16777216 @ 0x7fffcac00000 rw
dma_map: vga.rom 65536 @ 0x7fffe0400000 ro

I wonder whether there's any case that the "rom"s can be DMA target at all.. I understand it's logically possible to be READ from as ROMs, but I am curious what happens if we don't map them at all when they're ROMs, or whether there's any device that can (in real life) DMA from device ROMs, and for what use.

Quoted 11 lines

dma_map: 0000:3a:10.0 BAR 0 mmaps[0] 16384 @ 0x7ffff7fef000 rw
dma_map: 0000:3a:10.0 BAR 3 mmaps[0] 12288 @ 0x7ffff7fec000 rw

system.flash0 is excluded by the vfio listener because it is a rom_device. The rom@etc blocks are excluded because their MemoryRegions are not added to any container region, so the flatmem traversal of the AS used by the listener does not see them.

The BARs should not be mapped IMO, and I propose excluding them in the iommufd series:
https://lore.kernel.org/qemu-devel/[email protected]/

Looks like this is clear now that they should be there.

Quoted 5 lines

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration. Live migration copies the contents in the stream. Live update preserves the contents in place by preserving the memfd. Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

IMHO the 1st purpose is a fake one. IOW:

- Preserving content will be important on large RAM/ROM regions. When it's small, it shouldn't matter a huge deal, IMHO, because this is about "how fast we can migrate / live upgrade'. IOW, this is not a functional requirement.

- DMA mapped pinned pages: instead this is a hard requirement that we must make sure these pages are fd-based, because only a fd-based mapping can persist the pages (via page cache).

IMHO we shouldn't mangle them, and we should start with sticking with the 2nd goal here. To be explicit, if we can find a good replacement for -alloc-anon, IMHO we could still migrate the ramblocks only fall into the 1st purpose category, e.g. device ROMs, hopefully even if they're pinned, they should never be DMAed to/from.

Thanks,

Signature · 1 line
Peter Xu
#15 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 136 lines · On Tue, 13 Aug 2024 11:35:15 -0400 Peter Xu <[email protected]> wrote:
Quoted 77 lines · Steven Sistare
Quoted 76 lines · Steven Sistare
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 11 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property.  This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes.  I dropped an an important adjective, "implicit".

    "guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 6 lines
Quoted 4 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared.  Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option.  In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 24 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon.  But in addition, the user must explicitly add the "share" option.  I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 16 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends  should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd.  The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object.  The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback.  The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects.  OK, accepted.  I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd).  CPR will be blocked otherwise.  The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

    if (!new_block->host) {
        if (xen_enabled()) {
            ...
        } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent, TYPE_MEMORY_BACKEND)) { qemu_memfd_create()
        } else {
            qemu_anon_ram_alloc()
        }

Quoted 1 line
Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

Sorry for a late reply.

I think this may not work as David pointed out? Where AFAIU it will switch many old anon use cases to use memfd, aka, shmem, and it might be problematic when share=off: we have double memory consumption issue with shmem with private mapping.

I assume that includes things like "-m", "memory-backend-ram", and maybe more. IIUC memory consumption of the VM will double with them.

Quoted 13 lines

In a simple test here are the NON-memory-backend-object ramblocks which are allocated with memfd_create in my new proposal:

memfd_create system.flash0 3653632 @ 0x7fffe1000000 2 rw memfd_create system.flash1 540672 @ 0x7fffe0c00000 2 rw memfd_create pc.rom 131072 @ 0x7fffe0800000 2 rw memfd_create vga.vram 16777216 @ 0x7fffcac00000 2 rw memfd_create vga.rom 65536 @ 0x7fffe0400000 2 rw memfd_create /rom@etc/acpi/tables 2097152 @ 0x7fffca400000 6 rw memfd_create /rom@etc/table-loader 65536 @ 0x7fffca000000 6 rw memfd_create /rom@etc/acpi/rsdp 4096 @ 0x7fffc9c00000 6 rw

Quoted 6 lines

Of those, only a subset are mapped for DMA, per the existing QEMU logic, no changes from me:

dma_map: pc.rom 131072 @ 0x7fffe0800000 ro
dma_map: vga.vram 16777216 @ 0x7fffcac00000 rw
dma_map: vga.rom 65536 @ 0x7fffe0400000 ro

I wonder whether there's any case that the "rom"s can be DMA target at all.. I understand it's logically possible to be READ from as ROMs, but I am curious what happens if we don't map them at all when they're ROMs, or whether there's any device that can (in real life) DMA from device ROMs, and for what use.

Quoted 11 lines

dma_map: 0000:3a:10.0 BAR 0 mmaps[0] 16384 @ 0x7ffff7fef000 rw
dma_map: 0000:3a:10.0 BAR 3 mmaps[0] 12288 @ 0x7ffff7fec000 rw

system.flash0 is excluded by the vfio listener because it is a rom_device. The rom@etc blocks are excluded because their MemoryRegions are not added to any container region, so the flatmem traversal of the AS used by the listener does not see them.

The BARs should not be mapped IMO, and I propose excluding them in the iommufd series:
https://lore.kernel.org/qemu-devel/[email protected]/

Looks like this is clear now that they should be there.

Quoted 5 lines

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration. Live migration copies the contents in the stream. Live update preserves the contents in place by preserving the memfd. Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

Quoted 6 lines

IMHO the 1st purpose is a fake one. IOW:

- Preserving content will be important on large RAM/ROM regions. When it's small, it shouldn't matter a huge deal, IMHO, because this is about "how fast we can migrate / live upgrade'. IOW, this is not a functional requirement.

Regardless of the size of a ROM region, how would it ever be faster to migrate ROMs rather that reload them from stable media on the target? Furthermore, what mechanism other than migrating the ROM do we have to guarantee the contents of the ROM are identical?

I have a hard time accepting that ROMs are only migrated for performance and there isn't some aspect of migrating them to ensure the contents remain identical, and by that token CPR would also need to preserve the contents to provide the same guarantee. Thanks,

Alex

Quoted 12 lines

- DMA mapped pinned pages: instead this is a hard requirement that we must make sure these pages are fd-based, because only a fd-based mapping can persist the pages (via page cache).

IMHO we shouldn't mangle them, and we should start with sticking with the 2nd goal here. To be explicit, if we can find a good replacement for -alloc-anon, IMHO we could still migrate the ramblocks only fall into the 1st purpose category, e.g. device ROMs, hopefully even if they're pinned, they should never be DMAed to/from.

Thanks,

#16 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 151 lines · Alex Williamson
Quoted 136 lines · On Tue, 13 Aug 2024 11:35:15 -0400 Peter Xu <[email protected]> wrote:
Quoted 77 lines · Steven Sistare
Quoted 76 lines · Steven Sistare
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 11 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property.  This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes.  I dropped an an important adjective, "implicit".

    "guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 6 lines
Quoted 4 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared.  Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option.  In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 24 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon.  But in addition, the user must explicitly add the "share" option.  I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 16 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends  should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd.  The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object.  The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback.  The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects.  OK, accepted.  I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd).  CPR will be blocked otherwise.  The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

      if (!new_block->host) {
          if (xen_enabled()) {
              ...
          } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent,
                                          TYPE_MEMORY_BACKEND)) {
              qemu_memfd_create()
          } else {
              qemu_anon_ram_alloc()
          }
Quoted 1 line
Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

Sorry for a late reply.

I think this may not work as David pointed out? Where AFAIU it will switch many old anon use cases to use memfd, aka, shmem, and it might be problematic when share=off: we have double memory consumption issue with shmem with private mapping.

I assume that includes things like "-m", "memory-backend-ram", and maybe more. IIUC memory consumption of the VM will double with them.

Quoted 13 lines

In a simple test here are the NON-memory-backend-object ramblocks which are allocated with memfd_create in my new proposal:

   memfd_create system.flash0 3653632 @ 0x7fffe1000000 2 rw
   memfd_create system.flash1 540672 @ 0x7fffe0c00000 2 rw
   memfd_create pc.rom 131072 @ 0x7fffe0800000 2 rw
   memfd_create vga.vram 16777216 @ 0x7fffcac00000 2 rw
   memfd_create vga.rom 65536 @ 0x7fffe0400000 2 rw
   memfd_create /rom@etc/acpi/tables 2097152 @ 0x7fffca400000 6 rw
   memfd_create /rom@etc/table-loader 65536 @ 0x7fffca000000 6 rw
   memfd_create /rom@etc/acpi/rsdp 4096 @ 0x7fffc9c00000 6 rw
Quoted 6 lines

Of those, only a subset are mapped for DMA, per the existing QEMU logic, no changes from me:

   dma_map: pc.rom 131072 @ 0x7fffe0800000 ro
   dma_map: vga.vram 16777216 @ 0x7fffcac00000 rw
   dma_map: vga.rom 65536 @ 0x7fffe0400000 ro

I wonder whether there's any case that the "rom"s can be DMA target at all.. I understand it's logically possible to be READ from as ROMs, but I am curious what happens if we don't map them at all when they're ROMs, or whether there's any device that can (in real life) DMA from device ROMs, and for what use.

Quoted 11 lines
   dma_map: 0000:3a:10.0 BAR 0 mmaps[0] 16384 @ 0x7ffff7fef000 rw
   dma_map: 0000:3a:10.0 BAR 3 mmaps[0] 12288 @ 0x7ffff7fec000 rw

system.flash0 is excluded by the vfio listener because it is a rom_device. The rom@etc blocks are excluded because their MemoryRegions are not added to any container region, so the flatmem traversal of the AS used by the listener does not see them.

The BARs should not be mapped IMO, and I propose excluding them in the iommufd series:

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

Looks like this is clear now that they should be there.

Quoted 5 lines

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration. Live migration copies the contents in the stream. Live update preserves the contents in place by preserving the memfd. Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

Quoted 6 lines

IMHO the 1st purpose is a fake one. IOW:

- Preserving content will be important on large RAM/ROM regions. When it's small, it shouldn't matter a huge deal, IMHO, because this is about "how fast we can migrate / live upgrade'. IOW, this is not a functional requirement.

Regardless of the size of a ROM region, how would it ever be faster to migrate ROMs rather that reload them from stable media on the target? Furthermore, what mechanism other than migrating the ROM do we have to guarantee the contents of the ROM are identical?

Quoted 4 lines

I have a hard time accepting that ROMs are only migrated for performance and there isn't some aspect of migrating them to ensure the contents remain identical, and by that token CPR would also need to preserve the contents to provide the same guarantee. Thanks,

I agree. Any ramblock may change if the contents are read from a file in the QEMU distribution, or if the contents are composed by QEMU code. Live migration guards against this by sending the old ramblock contents in the migration stream.

- Steve

Quoted 13 lines
Quoted 12 lines

- DMA mapped pinned pages: instead this is a hard requirement that we must make sure these pages are fd-based, because only a fd-based mapping can persist the pages (via page cache).

IMHO we shouldn't mangle them, and we should start with sticking with the 2nd goal here. To be explicit, if we can find a good replacement for -alloc-anon, IMHO we could still migrate the ramblocks only fall into the 1st purpose category, e.g. device ROMs, hopefully even if they're pinned, they should never be DMAed to/from.

Thanks,

#17 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 153 lines · Steven Sistare
Quoted 147 lines · Alex Williamson
Quoted 132 lines · On Tue, 13 Aug 2024 11:35:15 -0400 Peter Xu <[email protected]> wrote:
Quoted 73 lines · Steven Sistare
Quoted 72 lines · Steven Sistare
Quoted 26 lines · Igor Mammedov
Quoted 24 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 11 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property.  This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes.  I dropped an an important adjective, "implicit".

     "guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 6 lines
Quoted 4 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared.  Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option.  In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 22 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon.  But in addition, the user must explicitly add the "share" option.  I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 15 lines
Quoted 9 lines
Quoted 2 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends  should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd.  The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object.  The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback.  The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects.  OK, accepted.  I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd).  CPR will be blocked otherwise.  The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

      if (!new_block->host) {
          if (xen_enabled()) {
              ...
          } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent, TYPE_MEMORY_BACKEND)) { qemu_memfd_create()
          } else {
              qemu_anon_ram_alloc()
          }

Quoted 1 line
Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

Sorry for a late reply.

I think this may not work as David pointed out? Where AFAIU it will switch many old anon use cases to use memfd, aka, shmem, and it might be problematic when share=off: we have double memory consumption issue with shmem with private mapping.

I assume that includes things like "-m", "memory-backend-ram", and maybe more.  IIUC memory consumption of the VM will double with them.

Quoted 13 lines

In a simple test here are the NON-memory-backend-object ramblocks which are allocated with memfd_create in my new proposal:

   memfd_create system.flash0 3653632 @ 0x7fffe1000000 2 rw memfd_create system.flash1 540672 @ 0x7fffe0c00000 2 rw memfd_create pc.rom 131072 @ 0x7fffe0800000 2 rw memfd_create vga.vram 16777216 @ 0x7fffcac00000 2 rw memfd_create vga.rom 65536 @ 0x7fffe0400000 2 rw memfd_create /rom@etc/acpi/tables 2097152 @ 0x7fffca400000 6 rw memfd_create /rom@etc/table-loader 65536 @ 0x7fffca000000 6 rw memfd_create /rom@etc/acpi/rsdp 4096 @ 0x7fffc9c00000 6 rw

Quoted 6 lines

Of those, only a subset are mapped for DMA, per the existing QEMU logic, no changes from me:

   dma_map: pc.rom 131072 @ 0x7fffe0800000 ro
   dma_map: vga.vram 16777216 @ 0x7fffcac00000 rw
   dma_map: vga.rom 65536 @ 0x7fffe0400000 ro

I wonder whether there's any case that the "rom"s can be DMA target at all..  I understand it's logically possible to be READ from as ROMs, but I am curious what happens if we don't map them at all when they're ROMs, or whether there's any device that can (in real life) DMA from device ROMs, and for what use.

Quoted 11 lines

   dma_map: 0000:3a:10.0 BAR 0 mmaps[0] 16384 @ 0x7ffff7fef000 rw
   dma_map: 0000:3a:10.0 BAR 3 mmaps[0] 12288 @ 0x7ffff7fec000 rw

system.flash0 is excluded by the vfio listener because it is a rom_device. The rom@etc blocks are excluded because their MemoryRegions are not added to any container region, so the flatmem traversal of the AS used by the listener does not see them.

The BARs should not be mapped IMO, and I propose excluding them in the iommufd series:
   https://lore.kernel.org/qemu-devel/[email protected]/

Looks like this is clear now that they should be there.

Quoted 5 lines

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration.  Live migration copies the contents in the stream.  Live update preserves the contents in place by preserving the memfd.  Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

Quoted 6 lines

IMHO the 1st purpose is a fake one.  IOW:

   - Preserving content will be important on large RAM/ROM regions.  When it's small, it shouldn't matter a huge deal, IMHO, because this is about "how fast we can migrate / live upgrade'.  IOW, this is not a functional requirement.

Regardless of the size of a ROM region, how would it ever be faster to migrate ROMs rather that reload them from stable media on the target? Furthermore, what mechanism other than migrating the ROM do we have to guarantee the contents of the ROM are identical?

Quoted 4 lines

I have a hard time accepting that ROMs are only migrated for performance and there isn't some aspect of migrating them to ensure the contents remain identical, and by that token CPR would also need to preserve the contents to provide the same guarantee.  Thanks,

Quoted 4 lines

I agree.  Any ramblock may change if the contents are read from a file in the QEMU distribution, or if the contents are composed by QEMU code.  Live migration guards against this by sending the old ramblock contents in the migration stream.

Our emails just crossed. I will repost this to your new reply so keep a single conversation thread.

- Steve

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

   - DMA mapped pinned pages: instead this is a hard requirement that we must make sure these pages are fd-based, because only a fd-based mapping can persist the pages (via page cache).

IMHO we shouldn't mangle them, and we should start with sticking with the 2nd goal here.  To be explicit, if we can find a good replacement for -alloc-anon, IMHO we could still migrate the ramblocks only fall into the 1st purpose category, e.g. device ROMs, hopefully even if they're pinned, they should never be DMAed to/from.

Thanks,

#18 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

In reply to Alex Williamson
Quoted 12 lines · Alex Williamson
Quoted 11 lines
Quoted 4 lines

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration. Live migration copies the contents in the stream. Live update preserves the contents in place by preserving the memfd. Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

IMHO the 1st purpose is a fake one. IOW:

- Preserving content will be important on large RAM/ROM regions. When it's small, it shouldn't matter a huge deal, IMHO, because this is about "how fast we can migrate / live upgrade'. IOW, this is not a functional requirement.

Quoted 4 lines

Regardless of the size of a ROM region, how would it ever be faster to migrate ROMs rather that reload them from stable media on the target? Furthermore, what mechanism other than migrating the ROM do we have to guarantee the contents of the ROM are identical?

IIRC we need to migrate ROMs in some form because they can be different on src/dst, e.g., ROM files can upgrade after QEMU upgrades. Here either putting them onto migration stream, or making that fd-based should work.

Frankly I don't solidly know the details on why they can't be different. My current understanding was that if one device boots with one version of ROM/firmware, then it's possible the device keep interacting with the ROM region in some way (in the form of referring addresses in this specific version of ROM?), so that it may stop working if the ROM content changed.

IOW, if my understanding is correct, new ROM files won't get used after migration automatically, but it requires one system reset. When a system reset triggered after VM migrated to destination host, it'll reload device ROMs with the files specified (which will start to point to the upgraded version of ROM files), and IIUC that's where the devices will boostrap with the new ROM / BIOS files.

Quoted 3 lines

I have a hard time accepting that ROMs are only migrated for performance

AFAICT, it's never about performance or making it faster when putting ROM data on wire. Even in this context where Steve wanted to use fd backing the ROMs, then putting that on wire is still slower than sharing fds.

Here my previous comment / point was that this should be a small region, so it shouldn't matter a huge deal for ROMs to migrate either through the wire or via the fd page cache. I wanted to remove one more dependency that we may even need the new -alloc-anon parameter as it doesn't sound required for ROM migrations.

Quoted 3 lines

and there isn't some aspect of migrating them to ensure the contents remain identical, and by that token CPR would also need to preserve the contents to provide the same guarantee. Thanks,

Thanks,

Signature · 1 line
Peter Xu
#19 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 13 lines · Peter Xu
Quoted 12 lines · Alex Williamson
Quoted 11 lines
Quoted 4 lines

Note that the old-QEMU contents of all ramblocks must be preserved, just like in live migration. Live migration copies the contents in the stream. Live update preserves the contents in place by preserving the memfd. Thus memfd serves two purposes: preserving old contents, and preserving DMA mapped pinned pages.

IMHO the 1st purpose is a fake one. IOW:

- Preserving content will be important on large RAM/ROM regions. When it's small, it shouldn't matter a huge deal, IMHO, because this is about "how fast we can migrate / live upgrade'. IOW, this is not a functional requirement.

Quoted 8 lines
Quoted 4 lines

Regardless of the size of a ROM region, how would it ever be faster to migrate ROMs rather that reload them from stable media on the target? Furthermore, what mechanism other than migrating the ROM do we have to guarantee the contents of the ROM are identical?

IIRC we need to migrate ROMs in some form because they can be different on src/dst, e.g., ROM files can upgrade after QEMU upgrades. Here either putting them onto migration stream, or making that fd-based should work.

Agreed.

Quoted 1 line

Frankly I don't solidly know the details on why they can't be different.

Any ramblock may change if the contents are read from a file in the QEMU distribution, or if the contents are composed by QEMU code.

Quoted 4 lines

My current understanding was that if one device boots with one version of ROM/firmware, then it's possible the device keep interacting with the ROM region in some way (in the form of referring addresses in this specific version of ROM?), so that it may stop working if the ROM content changed.

Yes, the guest may continue to read data or execute code from the block.

Quoted 6 lines

IOW, if my understanding is correct, new ROM files won't get used after migration automatically, but it requires one system reset. When a system reset triggered after VM migrated to destination host, it'll reload device ROMs with the files specified (which will start to point to the upgraded version of ROM files), and IIUC that's where the devices will boostrap with the new ROM / BIOS files.

Agreed.

Quoted 7 lines
Quoted 2 lines

I have a hard time accepting that ROMs are only migrated for performance

AFAICT, it's never about performance or making it faster when putting ROM data on wire. Even in this context where Steve wanted to use fd backing the ROMs, then putting that on wire is still slower than sharing fds.

Quoted 5 lines

Here my previous comment / point was that this should be a small region, so it shouldn't matter a huge deal for ROMs to migrate either through the wire or via the fd page cache. I wanted to remove one more dependency that we may even need the new -alloc-anon parameter as it doesn't sound required for ROM migrations.

Agreed that performance is not the issue. I use memfd for correctness.

Also note that my new proposal deletes the alloc-anon parameter. I allocate non-command-line ramblocks unconditionally using memfd_create.

- Steve

Quoted 6 lines
Quoted 3 lines

and there isn't some aspect of migrating them to ensure the contents remain identical, and by that token CPR would also need to preserve the contents to provide the same guarantee. Thanks,

Thanks,

#20 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

In reply to Peter Xu
Quoted 82 lines · Peter Xu
Quoted 77 lines · Steven Sistare
Quoted 76 lines · Steven Sistare
Quoted 28 lines · Igor Mammedov
Quoted 25 lines · On Sat, 20 Jul 2024 16:28:25 -0400 Steven Sistare <[email protected]>…
Quoted 11 lines · Igor Mammedov
Quoted 5 lines · On Sun, 30 Jun 2024 12:40:24 -0700 Steve Sistare <[email protected]>…

Allocate anonymous memory using mmap MAP_ANON or memfd_create depending on the value of the anon-alloc machine property.  This affects memory-backend-ram objects, guest RAM created with the global -m option but without an associated memory-backend object and without the -mem-path option

nowadays, all machines were converted to use memory backend for VM RAM. so -m option implicitly creates memory-backend object,
which will be either MEMORY_BACKEND_FILE if -mem-path present or MEMORY_BACKEND_RAM otherwise.

Yes.  I dropped an an important adjective, "implicit".

    "guest RAM created with the global -m option but without an explicit associated memory-backend object and without the -mem-path option"

Quoted 6 lines
Quoted 4 lines

To access the same memory in the old and new QEMU processes, the memory must be mapped shared.  Therefore, the implementation always sets RAM_SHARED if alloc-anon=memfd, except for memory-backend-ram, where the user must explicitly specify the share option.  In lieu of defining a new

so statement at the top that memory-backend-ram is affected is not really valid?

Quoted 24 lines
Quoted 4 lines

memory-backend-ram is affected by alloc-anon.  But in addition, the user must explicitly add the "share" option.  I don't implicitly set share in this case, because I would be overriding the user's specification of the memory object's property, which would be private if omitted.

instead of touching implicit RAM (-m), it would be better to error out and ask user to provide properly configured memory-backend explicitly.

Quoted 16 lines
Quoted 10 lines
Quoted 3 lines

RAM flag, at the lowest level the implementation uses RAM_SHARED with fd=-1 as the condition for calling memfd_create.

In general I do dislike adding yet another option that will affect guest RAM allocation (memory-backends  should be sufficient).

However I do see that you need memfd for device memory (vram, roms, ...). Can we just use memfd/shared unconditionally for those and avoid introducing a new confusing option?

The Linux kernel has different tunables for backing memfd's with huge pages, so we could hurt performance if we unconditionally change to memfd.  The user should have a choice for any segment that is large enough for huge pages to improve performance, which potentially is any memory-backend-object.  The non memory-backend objects are small, and it would be OK to use memfd unconditionally for them.

Thanks everyone for your feedback.  The common theme is that you dislike that the new option modifies the allocation of memory-backend-objects.  OK, accepted.  I propose to remove that interaction, and document in the QAPI which backends work for CPR. Specifically, memory-backend-memfd or memory-backend-file object is required, with share=on (which is the default for memory-backend-memfd).  CPR will be blocked otherwise.  The legacy -m option without an explicit memory-backend-object will not support CPR.

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

      if (!new_block->host) {
          if (xen_enabled()) {
              ...
          } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent,
                                          TYPE_MEMORY_BACKEND)) {
              qemu_memfd_create()
          } else {
              qemu_anon_ram_alloc()
          }
Quoted 1 line
Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

Sorry for a late reply.

Quoted 7 lines

I think this may not work as David pointed out? Where AFAIU it will switch many old anon use cases to use memfd, aka, shmem, and it might be problematic when share=off: we have double memory consumption issue with shmem with private mapping.

I assume that includes things like "-m", "memory-backend-ram", and maybe more. IIUC memory consumption of the VM will double with them.

The new proposal only affects anon allocations that are not described on the command line, and their memfd will be shared. There is no command line option which would set share=off for these blocks.

"-m" and "memory-backend-ram" are not affected.
They will not work with CPR.

I will respond to your other comments separately.

- Steve

#21 lore raw

Re: [PATCH V2 01/11] machine: alloc-anon option

Quoted 26 lines · Steven Sistare
Quoted 18 lines
Quoted 14 lines
Quoted 14 lines

Non memory-backend-objects (ramblocks not described on the qemu command line) will always be allocated using memfd_create (on Linux only).  The alloc-anon option is deleted. The logic in ram_block_add becomes:

      if (!new_block->host) {
          if (xen_enabled()) {
              ...
          } else if (!object_dynamic_cast(new_block->mr->parent_obj.parent,
                                          TYPE_MEMORY_BACKEND)) {
              qemu_memfd_create()
          } else {
              qemu_anon_ram_alloc()
          }
Quoted 1 line
Quoted 1 line

Is that acceptable to everyone?  Igor, Peter, Daniel?

Sorry for a late reply.

Quoted 7 lines

I think this may not work as David pointed out? Where AFAIU it will switch many old anon use cases to use memfd, aka, shmem, and it might be problematic when share=off: we have double memory consumption issue with shmem with private mapping.

I assume that includes things like "-m", "memory-backend-ram", and maybe more. IIUC memory consumption of the VM will double with them.

Quoted 6 lines

The new proposal only affects anon allocations that are not described on the command line, and their memfd will be shared. There is no command line option which would set share=off for these blocks.

"-m" and "memory-backend-ram" are not affected.
They will not work with CPR.

Hmm yeah memory-backend-ram should be TYPE_MEMORY_BACKEND for sure.. and I just noticed "-m" looks like the same.

Though this change still gives me the feeling that we don't yet know who's the target of this change at all, and what purpose it services.

I'll see how others see this. For me, at least in this case I think it'll be nice the "else if" will not be used unless cpr is enabled in the first place. But that's still a bit hacky to me.

Thanks,

Signature · 1 line
Peter Xu
#22 lore raw

PATCH v2 2/11 migration: cpr-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, as objects must already exist before that stream can be loaded. Instead, define auxilliary state structures and vmstate descriptions, not associated with any registered object, and serialize the aux state to a cpr-specific stream in cpr_state_save. Deserialize in cpr_state_load after QEMU restarts, before devices are realized.

Provide accessors for clients to register file descriptors for saving. The mechanism for passing the fd's to the new process will be specific to each migration mode, and added in subsequent patches.

---
 include/migration/cpr.h |  21 ++++++
 migration/cpr.c         | 188 ++++++++++++++++++++++++++++++++++++++++++++++++
 migration/meson.build   |   1 +
 migration/migration.c   |   6 ++
 migration/trace-events  |   5 ++
 system/vl.c             |   3 +
 6 files changed, 224 insertions(+)
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr.c
include/migration/cpr.h new file +21 -0
new file mode 100644
index 0000000..8e7e705
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2021, 2024 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef MIGRATION_CPR_H
+#define MIGRATION_CPR_H
+
+typedef int (*cpr_walk_fd_cb)(int fd);
+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 cpr_resave_fd(const char *name, int id, int fd);
+
+int cpr_state_save(Error **errp);
+int cpr_state_load(Error **errp);
+
+#endif
migration/cpr.c new file +188 -0

188 lines not on this page · raw message

migration/meson.build +1 -0
index 5ce2acb4..87feb4c 100644
@@ -13,6 +13,7 @@ system_ss.add(files(
   'block-dirty-bitmap.c',
   'channel.c',
   'channel-block.c',
+  'cpr.c',
   'dirtyrate.c',
   'exec.c',
   'fd.c',
migration/migration.c +6 -0
index 3dea06d..e394ad7 100644
@@ -27,6 +27,7 @@
 #include "sysemu/cpu-throttle.h"
 #include "rdma.h"
 #include "ram.h"
+#include "migration/cpr.h"
 #include "migration/global_state.h"
 #include "migration/misc.h"
 #include "migration.h"
@@ -2118,6 +2119,10 @@ void qmp_migrate(const char *uri, bool has_channels,
         }
     }
 
+    if (cpr_state_save(&local_err)) {
+        goto out;
+    }
+
     if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
         SocketAddress *saddr = &addr->u.socket;
         if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
@@ -2142,6 +2147,7 @@ void qmp_migrate(const char *uri, bool has_channels,
                           MIGRATION_STATUS_FAILED);
     }
 
+out:
     if (local_err) {
         if (!resume_requested) {
             yank_unregister_instance(MIGRATION_YANK_INSTANCE);
migration/trace-events +5 -0
index 0b7c332..173f2c0 100644
@@ -340,6 +340,11 @@ colo_receive_message(const char *msg) "Receive '%s' message"
 # colo-failover.c
 colo_failover_set_state(const char *new_state) "new state %s"
 
+# cpr.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"
+
 # 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
system/vl.c +3 -0
index 03951be..6521ee3 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"
@@ -3713,6 +3714,8 @@ void qemu_init(int argc, char **argv)
 
     qemu_create_machine(machine_opts_dict);
 
+    cpr_state_load(&error_fatal);
+
     suspend_mux_open();
 
     qemu_disable_default_devices();
Signature · 1 line
1.8.3.1
Reviewed-by on v2 2/11
#23 lore raw

Re: [PATCH V2 02/11] migration: cpr-state

Quoted 8 lines · 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, as objects must already exist before that stream can be loaded. Instead, define auxilliary state structures and vmstate descriptions, not associated with any registered object, and serialize the aux state to a cpr-specific stream in cpr_state_save. Deserialize in cpr_state_load after QEMU restarts, before devices are realized.

Quoted 5 lines

Provide accessors for clients to register file descriptors for saving. The mechanism for passing the fd's to the new process will be specific to each migration mode, and added in subsequent patches.

#24 lore raw

Re: [PATCH V2 02/11] migration: cpr-state

In reply to Steve Sistare
Quoted 82 lines · 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, as objects must already exist before that stream can be loaded. Instead, define auxilliary state structures and vmstate descriptions, not associated with any registered object, and serialize the aux state to a cpr-specific stream in cpr_state_save. Deserialize in cpr_state_load after QEMU restarts, before devices are realized.

Provide accessors for clients to register file descriptors for saving. The mechanism for passing the fd's to the new process will be specific to each migration mode, and added in subsequent patches.

---
 include/migration/cpr.h |  21 ++++++
 migration/cpr.c         | 188 ++++++++++++++++++++++++++++++++++++++++++++++++
 migration/meson.build   |   1 +
 migration/migration.c   |   6 ++
 migration/trace-events  |   5 ++
 system/vl.c             |   3 +
 6 files changed, 224 insertions(+)
 create mode 100644 include/migration/cpr.h
 create mode 100644 migration/cpr.c
include/migration/cpr.h new file +21 -0
new file mode 100644
index 0000000..8e7e705
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2021, 2024 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef MIGRATION_CPR_H
+#define MIGRATION_CPR_H
+
+typedef int (*cpr_walk_fd_cb)(int fd);
+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 cpr_resave_fd(const char *name, int id, int fd);
+
+int cpr_state_save(Error **errp);
+int cpr_state_load(Error **errp);
+
+#endif
migration/cpr.c new file +25 -0
new file mode 100644
index 0000000..313e74e
@@ -0,0 +1,188 @@
+/*
+ * Copyright (c) 2021-2024 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "migration/cpr.h"
+#include "migration/misc.h"
+#include "migration/qemu-file.h"
+#include "migration/savevm.h"
+#include "migration/vmstate.h"
+#include "sysemu/runstate.h"
+#include "trace.h"
+
+/*************************************************************************/
+/* cpr state container for all information to be saved. */
+
+typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
+
+typedef struct CprState {
+    CprFdList fds;
+} CprState;

migration/cpr.c line 35

Quoted 10 lines
migration/cpr.c new file +10 -0
+
+static CprState cpr_state;
+
+/****************************************************************************/
+
+typedef struct CprFd {
+    char *name;
+    unsigned int namelen;
+    int id;
+    int fd;

[1]

Quoted 71 lines
migration/cpr.c new file +71 -0
+    QLIST_ENTRY(CprFd) next;
+} CprFd;
+
+static const VMStateDescription vmstate_cpr_fd = {
+    .name = "cpr fd",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(namelen, CprFd),
+        VMSTATE_VBUFFER_ALLOC_UINT32(name, CprFd, 0, NULL, namelen),
+        VMSTATE_INT32(id, CprFd),
+        VMSTATE_INT32(fd, CprFd),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpr_save_fd(const char *name, int id, int fd)
+{
+    CprFd *elem = g_new0(CprFd, 1);
+
+    trace_cpr_save_fd(name, id, fd);
+    elem->name = g_strdup(name);
+    elem->namelen = strlen(name) + 1;
+    elem->id = id;
+    elem->fd = fd;
+    QLIST_INSERT_HEAD(&cpr_state.fds, elem, next);
+}
+
+static CprFd *find_fd(CprFdList *head, const char *name, int id)
+{
+    CprFd *elem;
+
+    QLIST_FOREACH(elem, head, next) {
+        if (!strcmp(elem->name, name) && elem->id == id) {
+            return elem;
+        }
+    }
+    return NULL;
+}
+
+void cpr_delete_fd(const char *name, int id)
+{
+    CprFd *elem = find_fd(&cpr_state.fds, name, id);
+
+    if (elem) {
+        QLIST_REMOVE(elem, next);
+        g_free(elem->name);
+        g_free(elem);
+    }
+
+    trace_cpr_delete_fd(name, id);
+}
+
+int cpr_find_fd(const char *name, int id)
+{
+    CprFd *elem = find_fd(&cpr_state.fds, name, id);
+    int fd = elem ? elem->fd : -1;
+
+    trace_cpr_find_fd(name, id, fd);
+    return fd;
+}
+
+int cpr_walk_fd(cpr_walk_fd_cb cb)
+{
+    CprFd *elem;
+
+    QLIST_FOREACH(elem, &cpr_state.fds, next) {
+        if (elem->fd >= 0 && cb(elem->fd)) {
+            return 1;
+        }
+    }

migration/cpr.c line 116

Quoted 10 lines
migration/cpr.c new file +10 -0
+    return 0;
+}
+
+void cpr_resave_fd(const char *name, int id, int fd)
+{
+    CprFd *elem = find_fd(&cpr_state.fds, name, id);
+    int old_fd = elem ? elem->fd : -1;
+
+    if (old_fd < 0) {
+        cpr_save_fd(name, id, fd);

I don't think I know well on when old_fd<0 would happen yet, as this series doesn't look like to use this function at all. From that POV, maybe nice to add a comment above [1] for "fd" field.

Meanwhile, do we need to remove the old_fd<0 element here, or is it intended to keep that and the new CprFD?

Quoted 21 lines
migration/cpr.c new file +21 -0
+    } else if (old_fd != fd) {
+        error_setg(&error_fatal,
+                   "internal error: cpr fd '%s' id %d value %d "
+                   "already saved with a different value %d",
+                   name, id, fd, old_fd);
+    }
+}
+/*************************************************************************/
+#define CPR_STATE "CprState"
+
+static const VMStateDescription vmstate_cpr_state = {
+    .name = CPR_STATE,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
+        VMSTATE_END_OF_LIST()
+    }
+};
+/*************************************************************************/
+

migration/cpr.c line 147

Quoted 10 lines
migration/cpr.c new file +10 -0
+int cpr_state_save(Error **errp)
+{
+    int ret;
+    QEMUFile *f;
+
+    /* set f based on mode in a later patch in this series */
+    return 0;
+
+    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+    qemu_put_be32(f, QEMU_VM_FILE_VERSION);

Having magic/version makes sense to me, though I'd suggest we use CPR new magic/versions, so that if we see an binary dump we know what it is, and we don't mixup a CPR image against a migration stream image.

migration/cpr.c line 149

Quoted 2 lines
migration/cpr.c new file +2 -0
+
+    ret = vmstate_save_state(f, &vmstate_cpr_state, &cpr_state, 0);

s/0/NULL/

migration/cpr.c line 152

Quoted 3 lines
migration/cpr.c new file +3 -0
+    if (ret) {
+        error_setg(errp, "vmstate_save_state error %d", ret);
+    }

Can consider using vmstate_save_state_with_err().

Quoted 21 lines
migration/cpr.c new file +21 -0
+
+    qemu_fclose(f);
+    return ret;
+}
+
+int cpr_state_load(Error **errp)
+{
+    int ret;
+    uint32_t v;
+    QEMUFile *f;
+
+    /* set f based on mode in a later patch in this series */
+    return 0;
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        error_setg(errp, "Not a migration stream (bad magic %x)", v);
+        qemu_fclose(f);
+        return -EINVAL;
+    }
+    v = qemu_get_be32(f);

migration/cpr.c line 183

Quoted 10 lines
migration/cpr.c new file +10 -0
+    if (v != QEMU_VM_FILE_VERSION) {
+        error_setg(errp, "Unsupported migration stream version %d", v);
+        qemu_fclose(f);
+        return -ENOTSUP;
+    }
+
+    ret = vmstate_load_state(f, &vmstate_cpr_state, &cpr_state, 1);
+    if (ret) {
+        error_setg(errp, "vmstate_load_state error %d", ret);
+    }

Simiarly, can use vmstate_save_state_with_err().

Quoted 77 lines
migration/cpr.c new file +5 -0
+
+    qemu_fclose(f);
+    return ret;
+}
+
migration/meson.build +1 -0
index 5ce2acb4..87feb4c 100644
@@ -13,6 +13,7 @@ system_ss.add(files(
   'block-dirty-bitmap.c',
   'channel.c',
   'channel-block.c',
+  'cpr.c',
   'dirtyrate.c',
   'exec.c',
   'fd.c',
migration/migration.c +6 -0
index 3dea06d..e394ad7 100644
@@ -27,6 +27,7 @@
 #include "sysemu/cpu-throttle.h"
 #include "rdma.h"
 #include "ram.h"
+#include "migration/cpr.h"
 #include "migration/global_state.h"
 #include "migration/misc.h"
 #include "migration.h"
@@ -2118,6 +2119,10 @@ void qmp_migrate(const char *uri, bool has_channels,
         }
     }
 
+    if (cpr_state_save(&local_err)) {
+        goto out;
+    }
+
     if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
         SocketAddress *saddr = &addr->u.socket;
         if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
@@ -2142,6 +2147,7 @@ void qmp_migrate(const char *uri, bool has_channels,
                           MIGRATION_STATUS_FAILED);
     }
 
+out:
     if (local_err) {
         if (!resume_requested) {
             yank_unregister_instance(MIGRATION_YANK_INSTANCE);
migration/trace-events +5 -0
index 0b7c332..173f2c0 100644
@@ -340,6 +340,11 @@ colo_receive_message(const char *msg) "Receive '%s' message"
 # colo-failover.c
 colo_failover_set_state(const char *new_state) "new state %s"
 
+# cpr.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"
+
 # 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
system/vl.c +1 -0
index 03951be..6521ee3 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"

system/vl.c void qemu_init(int argc, char **argv) · line 3717

Quoted 4 lines
system/vl.c +1 -0
@@ -3713,6 +3714,8 @@ void qemu_init(int argc, char **argv)
 
     qemu_create_machine(machine_opts_dict);
 
+    cpr_state_load(&error_fatal);

Might be good to add a rich comment here explaining the decision on why loading here; I think most of the tricks lie here. E.g., it needs to be before XXX and it needs to be after YYY.

Thanks,

Quoted 7 lines
system/vl.c +1 -0
+
     suspend_mux_open();
 
     qemu_disable_default_devices();
Signature · 1 line
1.8.3.1
Signature · 1 line
Peter Xu
#25 lore raw

Re: [PATCH V2 02/11] migration: cpr-state

Quoted 173 lines · Peter Xu
Quoted 82 lines · 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, as objects must already exist before that stream can be loaded. Instead, define auxilliary state structures and vmstate descriptions, not associated with any registered object, and serialize the aux state to a cpr-specific stream in cpr_state_save. Deserialize in cpr_state_load after QEMU restarts, before devices are realized.

Provide accessors for clients to register file descriptors for saving. The mechanism for passing the fd's to the new process will be specific to each migration mode, and added in subsequent patches.

---
  include/migration/cpr.h |  21 ++++++
  migration/cpr.c         | 188 ++++++++++++++++++++++++++++++++++++++++++++++++
  migration/meson.build   |   1 +
  migration/migration.c   |   6 ++
  migration/trace-events  |   5 ++
  system/vl.c             |   3 +
  6 files changed, 224 insertions(+)

create mode 100644 include/migration/cpr.h
create mode 100644 migration/cpr.c

include/migration/cpr.h new file +21 -0
new file mode 100644
index 0000000..8e7e705
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2021, 2024 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef MIGRATION_CPR_H
+#define MIGRATION_CPR_H
+
+typedef int (*cpr_walk_fd_cb)(int fd);
+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 cpr_resave_fd(const char *name, int id, int fd);
+
+int cpr_state_save(Error **errp);
+int cpr_state_load(Error **errp);
+
+#endif
migration/cpr.c new file +25 -0
new file mode 100644
index 0000000..313e74e
@@ -0,0 +1,188 @@
+/*
+ * Copyright (c) 2021-2024 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "migration/cpr.h"
+#include "migration/misc.h"
+#include "migration/qemu-file.h"
+#include "migration/savevm.h"
+#include "migration/vmstate.h"
+#include "sysemu/runstate.h"
+#include "trace.h"
+
+/*************************************************************************/
+/* cpr state container for all information to be saved. */
+
+typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
+
+typedef struct CprState {
+    CprFdList fds;
+} CprState;

migration/cpr.c line 35

Quoted 10 lines
migration/cpr.c new file +10 -0
+
+static CprState cpr_state;
+
+/****************************************************************************/
+
+typedef struct CprFd {
+    char *name;
+    unsigned int namelen;
+    int id;
+    int fd;

[1]

Quoted 77 lines
migration/cpr.c new file +77 -0
+    QLIST_ENTRY(CprFd) next;
+} CprFd;
+
+static const VMStateDescription vmstate_cpr_fd = {
+    .name = "cpr fd",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(namelen, CprFd),
+        VMSTATE_VBUFFER_ALLOC_UINT32(name, CprFd, 0, NULL, namelen),
+        VMSTATE_INT32(id, CprFd),
+        VMSTATE_INT32(fd, CprFd),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpr_save_fd(const char *name, int id, int fd)
+{
+    CprFd *elem = g_new0(CprFd, 1);
+
+    trace_cpr_save_fd(name, id, fd);
+    elem->name = g_strdup(name);
+    elem->namelen = strlen(name) + 1;
+    elem->id = id;
+    elem->fd = fd;
+    QLIST_INSERT_HEAD(&cpr_state.fds, elem, next);
+}
+
+static CprFd *find_fd(CprFdList *head, const char *name, int id)
+{
+    CprFd *elem;
+
+    QLIST_FOREACH(elem, head, next) {
+        if (!strcmp(elem->name, name) && elem->id == id) {
+            return elem;
+        }
+    }
+    return NULL;
+}
+
+void cpr_delete_fd(const char *name, int id)
+{
+    CprFd *elem = find_fd(&cpr_state.fds, name, id);
+
+    if (elem) {
+        QLIST_REMOVE(elem, next);
+        g_free(elem->name);
+        g_free(elem);
+    }
+
+    trace_cpr_delete_fd(name, id);
+}
+
+int cpr_find_fd(const char *name, int id)
+{
+    CprFd *elem = find_fd(&cpr_state.fds, name, id);
+    int fd = elem ? elem->fd : -1;
+
+    trace_cpr_find_fd(name, id, fd);
+    return fd;
+}
+
+int cpr_walk_fd(cpr_walk_fd_cb cb)
+{
+    CprFd *elem;
+
+    QLIST_FOREACH(elem, &cpr_state.fds, next) {
+        if (elem->fd >= 0 && cb(elem->fd)) {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+void cpr_resave_fd(const char *name, int id, int fd)
+{
+    CprFd *elem = find_fd(&cpr_state.fds, name, id);
Quoted 8 lines

migration/cpr.c line 116

Quoted 4 lines
migration/cpr.c new file +4 -0
+    int old_fd = elem ? elem->fd : -1;
+
+    if (old_fd < 0) {
+        cpr_save_fd(name, id, fd);

I don't think I know well on when old_fd<0 would happen yet, as this series doesn't look like to use this function at all. From that POV, maybe nice to add a comment above [1] for "fd" field.

cpr_resave_fd can simplify client logic. It allows the same name,fd,id triplet to be (re) saved without creating a duplicate entry. The vfio series uses it. Yes, I should add some brief API docs in the header file.

Quoted 2 lines

Meanwhile, do we need to remove the old_fd<0 element here, or is it intended to keep that and the new CprFD?

old_fd < 0 is not an entry, it means no entry was found.

Quoted 27 lines
Quoted 27 lines
migration/cpr.c new file +27 -0
+    } else if (old_fd != fd) {
+        error_setg(&error_fatal,
+                   "internal error: cpr fd '%s' id %d value %d "
+                   "already saved with a different value %d",
+                   name, id, fd, old_fd);
+    }
+}
+/*************************************************************************/
+#define CPR_STATE "CprState"
+
+static const VMStateDescription vmstate_cpr_state = {
+    .name = CPR_STATE,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
+        VMSTATE_END_OF_LIST()
+    }
+};
+/*************************************************************************/
+
+int cpr_state_save(Error **errp)
+{
+    int ret;
+    QEMUFile *f;
+
+    /* set f based on mode in a later patch in this series */
Quoted 8 lines

migration/cpr.c line 147

Quoted 4 lines
migration/cpr.c new file +4 -0
+    return 0;
+
+    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+    qemu_put_be32(f, QEMU_VM_FILE_VERSION);

Having magic/version makes sense to me, though I'd suggest we use CPR new magic/versions, so that if we see an binary dump we know what it is, and we don't mixup a CPR image against a migration stream image.

Will do.

Quoted 4 lines

migration/cpr.c line 149

Quoted 2 lines
migration/cpr.c new file +2 -0
+
+    ret = vmstate_save_state(f, &vmstate_cpr_state, &cpr_state, 0);

s/0/NULL/

Will do.

Quoted 5 lines

migration/cpr.c line 152

Quoted 3 lines
migration/cpr.c new file +3 -0
+    if (ret) {
+        error_setg(errp, "vmstate_save_state error %d", ret);
+    }

Can consider using vmstate_save_state_with_err().

Cool, will do.

Quoted 25 lines
Quoted 25 lines
migration/cpr.c new file +25 -0
+
+    qemu_fclose(f);
+    return ret;
+}
+
+int cpr_state_load(Error **errp)
+{
+    int ret;
+    uint32_t v;
+    QEMUFile *f;
+
+    /* set f based on mode in a later patch in this series */
+    return 0;
+
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_MAGIC) {
+        error_setg(errp, "Not a migration stream (bad magic %x)", v);
+        qemu_fclose(f);
+        return -EINVAL;
+    }
+    v = qemu_get_be32(f);
+    if (v != QEMU_VM_FILE_VERSION) {
+        error_setg(errp, "Unsupported migration stream version %d", v);
+        qemu_fclose(f);
+        return -ENOTSUP;
Quoted 8 lines

migration/cpr.c line 183

Quoted 6 lines
migration/cpr.c new file +6 -0
+    }
+
+    ret = vmstate_load_state(f, &vmstate_cpr_state, &cpr_state, 1);
+    if (ret) {
+        error_setg(errp, "vmstate_load_state error %d", ret);
+    }

Simiarly, can use vmstate_save_state_with_err().

Hmm, vmstate_load_state_with_err does not exist.

Quoted 78 lines
Quoted 78 lines
migration/cpr.c new file +5 -0
+
+    qemu_fclose(f);
+    return ret;
+}
+
migration/meson.build +1 -0
index 5ce2acb4..87feb4c 100644
@@ -13,6 +13,7 @@ system_ss.add(files(
    'block-dirty-bitmap.c',
    'channel.c',
    'channel-block.c',
+  'cpr.c',
    'dirtyrate.c',
    'exec.c',
    'fd.c',
migration/migration.c +6 -0
index 3dea06d..e394ad7 100644
@@ -27,6 +27,7 @@
  #include "sysemu/cpu-throttle.h"
  #include "rdma.h"
  #include "ram.h"
+#include "migration/cpr.h"
  #include "migration/global_state.h"
  #include "migration/misc.h"
  #include "migration.h"
@@ -2118,6 +2119,10 @@ void qmp_migrate(const char *uri, bool has_channels,
          }
      }
 
+    if (cpr_state_save(&local_err)) {
+        goto out;
+    }
+
      if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
          SocketAddress *saddr = &addr->u.socket;
          if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
@@ -2142,6 +2147,7 @@ void qmp_migrate(const char *uri, bool has_channels,
                            MIGRATION_STATUS_FAILED);
      }
 
+out:
      if (local_err) {
          if (!resume_requested) {
              yank_unregister_instance(MIGRATION_YANK_INSTANCE);
migration/trace-events +5 -0
index 0b7c332..173f2c0 100644
@@ -340,6 +340,11 @@ colo_receive_message(const char *msg) "Receive '%s' message"
  # colo-failover.c
  colo_failover_set_state(const char *new_state) "new state %s"
 
+# cpr.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"
+
  # 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
system/vl.c +1 -0
index 03951be..6521ee3 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"
@@ -3713,6 +3714,8 @@ void qemu_init(int argc, char **argv)
 
Quoted 7 lines

system/vl.c void qemu_init(int argc, char **argv) · line 3717

Quoted 3 lines
system/vl.c +1 -0
      qemu_create_machine(machine_opts_dict);
 
+    cpr_state_load(&error_fatal);

Might be good to add a rich comment here explaining the decision on why loading here; I think most of the tricks lie here. E.g., it needs to be before XXX and it needs to be after YYY.

Will do.

- Steve

#26 lore raw

PATCH v2 3/11 migration: save cpr mode

In reply to Steve Sistare

Save the mode in CPR state, so the user does not need to explicitly specify it for the target. Modify migrate_mode() so it returns the incoming mode on the target.

---
 include/migration/cpr.h |  7 +++++++
 migration/cpr.c         | 23 ++++++++++++++++++++++-
 migration/migration.c   |  1 +
 migration/options.c     |  9 +++++++--
 4 files changed, 37 insertions(+), 3 deletions(-)
include/migration/cpr.h +7 -0
index 8e7e705..42b4019 100644
@@ -8,6 +8,13 @@
 #ifndef MIGRATION_CPR_H
 #define MIGRATION_CPR_H
 
+#include "qapi/qapi-types-migration.h"
+
+#define MIG_MODE_NONE MIG_MODE__MAX
+
+MigMode cpr_get_incoming_mode(void);
+void cpr_set_incoming_mode(MigMode mode);
+
 typedef int (*cpr_walk_fd_cb)(int fd);
 void cpr_save_fd(const char *name, int id, int fd);
 void cpr_delete_fd(const char *name, int id);
migration/cpr.c +22 -1
index 313e74e..1c296c6 100644
@@ -21,10 +21,23 @@
 typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
 
 typedef struct CprState {
+    MigMode mode;
     CprFdList fds;
 } CprState;
 
-static CprState cpr_state;
+static CprState cpr_state = {
+    .mode = MIG_MODE_NONE,
+};
+
+MigMode cpr_get_incoming_mode(void)
+{
+    return cpr_state.mode;
+}
+
+void cpr_set_incoming_mode(MigMode mode)
+{
+    cpr_state.mode = mode;
+}
 
 /****************************************************************************/
 
@@ -124,11 +137,19 @@ void cpr_resave_fd(const char *name, int id, int fd)
 /*************************************************************************/
 #define CPR_STATE "CprState"
 
+static int cpr_state_presave(void *opaque)
+{
+    cpr_state.mode = migrate_mode();
+    return 0;
+}
+
 static const VMStateDescription vmstate_cpr_state = {
     .name = CPR_STATE,
     .version_id = 1,
     .minimum_version_id = 1,
+    .pre_save = cpr_state_presave,
     .fields = (VMStateField[]) {
+        VMSTATE_UINT32(mode, CprState),
         VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
         VMSTATE_END_OF_LIST()
     }
migration/migration.c +1 -0
index e394ad7..0f47765 100644
@@ -411,6 +411,7 @@ void migration_incoming_state_destroy(void)
         mis->postcopy_qemufile_dst = NULL;
     }
 
+    cpr_set_incoming_mode(MIG_MODE_NONE);
     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
 }
 
migration/options.c +7 -2
index 645f550..305397a 100644
@@ -22,6 +22,7 @@
 #include "qapi/qmp/qnull.h"
 #include "sysemu/runstate.h"
 #include "migration/colo.h"
+#include "migration/cpr.h"
 #include "migration/misc.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -758,8 +759,12 @@ uint64_t migrate_max_postcopy_bandwidth(void)
 
 MigMode migrate_mode(void)
 {
-    MigrationState *s = migrate_get_current();
-    MigMode mode = s->parameters.mode;
+    MigMode mode = cpr_get_incoming_mode();
+
+    if (mode == MIG_MODE_NONE) {
+        MigrationState *s = migrate_get_current();
+        mode = s->parameters.mode;
+    }
 
     assert(mode >= 0 && mode < MIG_MODE__MAX);
     return mode;
Signature · 1 line
1.8.3.1
#27 lore raw

Re: [PATCH V2 03/11] migration: save cpr mode

Quoted 17 lines · Steve Sistare

Save the mode in CPR state, so the user does not need to explicitly specify it for the target. Modify migrate_mode() so it returns the incoming mode on the target.

---
 include/migration/cpr.h |  7 +++++++
 migration/cpr.c         | 23 ++++++++++++++++++++++-
 migration/migration.c   |  1 +
 migration/options.c     |  9 +++++++--
 4 files changed, 37 insertions(+), 3 deletions(-)
include/migration/cpr.h +0 -0
index 8e7e705..42b4019 100644

include/migration/cpr.h line 13

Quoted 6 lines
include/migration/cpr.h +3 -0
@@ -8,6 +8,13 @@
 #ifndef MIGRATION_CPR_H
 #define MIGRATION_CPR_H
 
+#include "qapi/qapi-types-migration.h"
+
+#define MIG_MODE_NONE MIG_MODE__MAX

What happens when a QEMU that knows about a new mode migrates into a QEMU that doesn't know that mode, i.e. sees it as MIG_MODE__MAX?

I'd just use -1.

Quoted 95 lines
include/migration/cpr.h +4 -0
+
+MigMode cpr_get_incoming_mode(void);
+void cpr_set_incoming_mode(MigMode mode);
+
 typedef int (*cpr_walk_fd_cb)(int fd);
 void cpr_save_fd(const char *name, int id, int fd);
 void cpr_delete_fd(const char *name, int id);
migration/cpr.c +22 -1
index 313e74e..1c296c6 100644
@@ -21,10 +21,23 @@
 typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
 
 typedef struct CprState {
+    MigMode mode;
     CprFdList fds;
 } CprState;
 
-static CprState cpr_state;
+static CprState cpr_state = {
+    .mode = MIG_MODE_NONE,
+};
+
+MigMode cpr_get_incoming_mode(void)
+{
+    return cpr_state.mode;
+}
+
+void cpr_set_incoming_mode(MigMode mode)
+{
+    cpr_state.mode = mode;
+}
 
 /****************************************************************************/
 
@@ -124,11 +137,19 @@ void cpr_resave_fd(const char *name, int id, int fd)
 /*************************************************************************/
 #define CPR_STATE "CprState"
 
+static int cpr_state_presave(void *opaque)
+{
+    cpr_state.mode = migrate_mode();
+    return 0;
+}
+
 static const VMStateDescription vmstate_cpr_state = {
     .name = CPR_STATE,
     .version_id = 1,
     .minimum_version_id = 1,
+    .pre_save = cpr_state_presave,
     .fields = (VMStateField[]) {
+        VMSTATE_UINT32(mode, CprState),
         VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
         VMSTATE_END_OF_LIST()
     }
migration/migration.c +1 -0
index e394ad7..0f47765 100644
@@ -411,6 +411,7 @@ void migration_incoming_state_destroy(void)
         mis->postcopy_qemufile_dst = NULL;
     }
 
+    cpr_set_incoming_mode(MIG_MODE_NONE);
     yank_unregister_instance(MIGRATION_YANK_INSTANCE);
 }
 
migration/options.c +7 -2
index 645f550..305397a 100644
@@ -22,6 +22,7 @@
 #include "qapi/qmp/qnull.h"
 #include "sysemu/runstate.h"
 #include "migration/colo.h"
+#include "migration/cpr.h"
 #include "migration/misc.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -758,8 +759,12 @@ uint64_t migrate_max_postcopy_bandwidth(void)
 
 MigMode migrate_mode(void)
 {
-    MigrationState *s = migrate_get_current();
-    MigMode mode = s->parameters.mode;
+    MigMode mode = cpr_get_incoming_mode();
+
+    if (mode == MIG_MODE_NONE) {
+        MigrationState *s = migrate_get_current();
+        mode = s->parameters.mode;
+    }
 
     assert(mode >= 0 && mode < MIG_MODE__MAX);
     return mode;
#28 lore raw

Re: [PATCH V2 03/11] migration: save cpr mode

Quoted 22 lines · Fabiano Rosas
Quoted 20 lines · Steve Sistare

Save the mode in CPR state, so the user does not need to explicitly specify it for the target. Modify migrate_mode() so it returns the incoming mode on the target.

---
  include/migration/cpr.h |  7 +++++++
  migration/cpr.c         | 23 ++++++++++++++++++++++-
  migration/migration.c   |  1 +
  migration/options.c     |  9 +++++++--
  4 files changed, 37 insertions(+), 3 deletions(-)
include/migration/cpr.h +0 -0
index 8e7e705..42b4019 100644
@@ -8,6 +8,13 @@
  #ifndef MIGRATION_CPR_H
  #define MIGRATION_CPR_H
 
Quoted 8 lines

include/migration/cpr.h line 13

Quoted 3 lines
include/migration/cpr.h +3 -0
+#include "qapi/qapi-types-migration.h"
+
+#define MIG_MODE_NONE MIG_MODE__MAX

What happens when a QEMU that knows about a new mode migrates into a QEMU that doesn't know that mode, i.e. sees it as MIG_MODE__MAX?

I'd just use -1.

Good idea, thanks - steve

Quoted 95 lines
Quoted 95 lines
include/migration/cpr.h +4 -0
+
+MigMode cpr_get_incoming_mode(void);
+void cpr_set_incoming_mode(MigMode mode);
+
  typedef int (*cpr_walk_fd_cb)(int fd);
  void cpr_save_fd(const char *name, int id, int fd);
  void cpr_delete_fd(const char *name, int id);
migration/cpr.c +22 -1
index 313e74e..1c296c6 100644
@@ -21,10 +21,23 @@
  typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
 
  typedef struct CprState {
+    MigMode mode;
      CprFdList fds;
  } CprState;
 
-static CprState cpr_state;
+static CprState cpr_state = {
+    .mode = MIG_MODE_NONE,
+};
+
+MigMode cpr_get_incoming_mode(void)
+{
+    return cpr_state.mode;
+}
+
+void cpr_set_incoming_mode(MigMode mode)
+{
+    cpr_state.mode = mode;
+}
 
  /****************************************************************************/
 
@@ -124,11 +137,19 @@ void cpr_resave_fd(const char *name, int id, int fd)
  /*************************************************************************/
  #define CPR_STATE "CprState"
 
+static int cpr_state_presave(void *opaque)
+{
+    cpr_state.mode = migrate_mode();
+    return 0;
+}
+
  static const VMStateDescription vmstate_cpr_state = {
      .name = CPR_STATE,
      .version_id = 1,
      .minimum_version_id = 1,
+    .pre_save = cpr_state_presave,
      .fields = (VMStateField[]) {
+        VMSTATE_UINT32(mode, CprState),
          VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
          VMSTATE_END_OF_LIST()
      }
migration/migration.c +1 -0
index e394ad7..0f47765 100644
@@ -411,6 +411,7 @@ void migration_incoming_state_destroy(void)
          mis->postcopy_qemufile_dst = NULL;
      }
 
+    cpr_set_incoming_mode(MIG_MODE_NONE);
      yank_unregister_instance(MIGRATION_YANK_INSTANCE);
  }
 
migration/options.c +7 -2
index 645f550..305397a 100644
@@ -22,6 +22,7 @@
  #include "qapi/qmp/qnull.h"
  #include "sysemu/runstate.h"
  #include "migration/colo.h"
+#include "migration/cpr.h"
  #include "migration/misc.h"
  #include "migration.h"
  #include "migration-stats.h"
@@ -758,8 +759,12 @@ uint64_t migrate_max_postcopy_bandwidth(void)
 
  MigMode migrate_mode(void)
  {
-    MigrationState *s = migrate_get_current();
-    MigMode mode = s->parameters.mode;
+    MigMode mode = cpr_get_incoming_mode();
+
+    if (mode == MIG_MODE_NONE) {
+        MigrationState *s = migrate_get_current();
+        mode = s->parameters.mode;
+    }
 
      assert(mode >= 0 && mode < MIG_MODE__MAX);
      return mode;
#29 lore raw

PATCH v2 4/11 migration: stop vm earlier for cpr

In reply to Steve Sistare

Stop the vm earlier for cpr, to guarantee consistent device state when CPR state is saved.

---
 migration/migration.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
migration/migration.c +13 -9
index 0f47765..8a8e927 100644
@@ -2077,6 +2077,7 @@ void qmp_migrate(const char *uri, bool has_channels,
     MigrationState *s = migrate_get_current();
     g_autoptr(MigrationChannel) channel = NULL;
     MigrationAddress *addr = NULL;
+    bool stopped = false;
 
     /*
      * Having preliminary checks for uri and channel
@@ -2120,6 +2121,15 @@ void qmp_migrate(const char *uri, bool has_channels,
         }
     }
 
+    if (migrate_mode_is_cpr(s)) {
+        int ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
+        if (ret < 0) {
+            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
+            goto out;
+        }
+        stopped = true;
+    }
+
     if (cpr_state_save(&local_err)) {
         goto out;
     }
@@ -2155,6 +2165,9 @@ out:
         }
         migrate_fd_error(s, local_err);
         error_propagate(errp, local_err);
+        if (stopped && runstate_is_live(s->vm_old_state)) {
+            vm_start();
+        }
         return;
     }
 }
@@ -3738,7 +3751,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
     Error *local_err = NULL;
     uint64_t rate_limit;
     bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
-    int ret;
 
     /*
      * If there's a previous error, free it and prepare for another one.
@@ -3810,14 +3822,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
         return;
     }
 
-    if (migrate_mode_is_cpr(s)) {
-        ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
-        if (ret < 0) {
-            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
-            goto fail;
-        }
-    }
-
     if (migrate_background_snapshot()) {
         qemu_thread_create(&s->thread, "mig/snapshot",
                 bg_migration_thread, s, QEMU_THREAD_JOINABLE);
Signature · 1 line
1.8.3.1
#30 lore raw

Re: [PATCH V2 04/11] migration: stop vm earlier for cpr

Quoted 37 lines · Steve Sistare

Stop the vm earlier for cpr, to guarantee consistent device state when CPR state is saved.

---
 migration/migration.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
migration/migration.c +10 -0
index 0f47765..8a8e927 100644
@@ -2077,6 +2077,7 @@ void qmp_migrate(const char *uri, bool has_channels,
     MigrationState *s = migrate_get_current();
     g_autoptr(MigrationChannel) channel = NULL;
     MigrationAddress *addr = NULL;
+    bool stopped = false;
 
     /*
      * Having preliminary checks for uri and channel
@@ -2120,6 +2121,15 @@ void qmp_migrate(const char *uri, bool has_channels,
         }
     }
 
+    if (migrate_mode_is_cpr(s)) {
+        int ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
+        if (ret < 0) {
+            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
+            goto out;
+        }
+        stopped = true;
+    }
+
     if (cpr_state_save(&local_err)) {
         goto out;
     }

migration/migration.c out: · line 2170

Quoted 6 lines
migration/migration.c +3 -0
@@ -2155,6 +2165,9 @@ out:
         }
         migrate_fd_error(s, local_err);
         error_propagate(errp, local_err);
+        if (stopped && runstate_is_live(s->vm_old_state)) {
+            vm_start();
+        }

What about non-live states? Shouldn't this be:

if (stopped) {
vm_resume();
}

Quoted 26 lines
migration/migration.c +0 -9
         return;
     }
 }
@@ -3738,7 +3751,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
     Error *local_err = NULL;
     uint64_t rate_limit;
     bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
-    int ret;
 
     /*
      * If there's a previous error, free it and prepare for another one.
@@ -3810,14 +3822,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
         return;
     }
 
-    if (migrate_mode_is_cpr(s)) {
-        ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
-        if (ret < 0) {
-            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
-            goto fail;
-        }
-    }
-
     if (migrate_background_snapshot()) {
         qemu_thread_create(&s->thread, "mig/snapshot",
                 bg_migration_thread, s, QEMU_THREAD_JOINABLE);
#31 lore raw

Re: [PATCH V2 04/11] migration: stop vm earlier for cpr

Quoted 48 lines · Fabiano Rosas
Quoted 37 lines · Steve Sistare

Stop the vm earlier for cpr, to guarantee consistent device state when CPR state is saved.

---
  migration/migration.c | 22 +++++++++++++---------
  1 file changed, 13 insertions(+), 9 deletions(-)
migration/migration.c +10 -0
index 0f47765..8a8e927 100644
@@ -2077,6 +2077,7 @@ void qmp_migrate(const char *uri, bool has_channels,
      MigrationState *s = migrate_get_current();
      g_autoptr(MigrationChannel) channel = NULL;
      MigrationAddress *addr = NULL;
+    bool stopped = false;
 
      /*
       * Having preliminary checks for uri and channel
@@ -2120,6 +2121,15 @@ void qmp_migrate(const char *uri, bool has_channels,
          }
      }
 
+    if (migrate_mode_is_cpr(s)) {
+        int ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
+        if (ret < 0) {
+            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
+            goto out;
+        }
+        stopped = true;
+    }
+
      if (cpr_state_save(&local_err)) {
          goto out;
      }

migration/migration.c out: · line 2170

Quoted 6 lines
migration/migration.c +3 -0
@@ -2155,6 +2165,9 @@ out:
          }
          migrate_fd_error(s, local_err);
          error_propagate(errp, local_err);
+        if (stopped && runstate_is_live(s->vm_old_state)) {
+            vm_start();
+        }

What about non-live states? Shouldn't this be:

Quoted 3 lines
if (stopped) {
    vm_resume();
}

Not quite. vm_old_state may be a stopped state, so we don't want to resume. However, I should probably restore the old stopped state here. I'll try some more error recovery scenarios.

- Steve

Quoted 27 lines
Quoted 27 lines
migration/migration.c +0 -9
          return;
      }
  }
@@ -3738,7 +3751,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
      Error *local_err = NULL;
      uint64_t rate_limit;
      bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
-    int ret;
 
      /*
       * If there's a previous error, free it and prepare for another one.
@@ -3810,14 +3822,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
          return;
      }
 
-    if (migrate_mode_is_cpr(s)) {
-        ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
-        if (ret < 0) {
-            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
-            goto fail;
-        }
-    }
-
      if (migrate_background_snapshot()) {
          qemu_thread_create(&s->thread, "mig/snapshot",
                  bg_migration_thread, s, QEMU_THREAD_JOINABLE);
#32 lore raw

Re: [PATCH V2 04/11] migration: stop vm earlier for cpr

Quoted 49 lines · Steven Sistare
Quoted 48 lines · Fabiano Rosas
Quoted 37 lines · Steve Sistare

Stop the vm earlier for cpr, to guarantee consistent device state when CPR state is saved.

---
  migration/migration.c | 22 +++++++++++++---------
  1 file changed, 13 insertions(+), 9 deletions(-)
migration/migration.c +10 -0
index 0f47765..8a8e927 100644
@@ -2077,6 +2077,7 @@ void qmp_migrate(const char *uri, bool has_channels,
      MigrationState *s = migrate_get_current();
      g_autoptr(MigrationChannel) channel = NULL;
      MigrationAddress *addr = NULL;
+    bool stopped = false;
 
      /*
       * Having preliminary checks for uri and channel
@@ -2120,6 +2121,15 @@ void qmp_migrate(const char *uri, bool has_channels,
          }
      }
 
+    if (migrate_mode_is_cpr(s)) {
+        int ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
+        if (ret < 0) {
+            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
+            goto out;
+        }
+        stopped = true;
+    }
+
      if (cpr_state_save(&local_err)) {
          goto out;
      }

migration/migration.c out: · line 2170

Quoted 6 lines
migration/migration.c +3 -0
@@ -2155,6 +2165,9 @@ out:
          }
          migrate_fd_error(s, local_err);
          error_propagate(errp, local_err);
+        if (stopped && runstate_is_live(s->vm_old_state)) {
+            vm_start();
+        }

What about non-live states? Shouldn't this be:

Quoted 7 lines
Quoted 3 lines
if (stopped) {
    vm_resume();
}

Not quite. vm_old_state may be a stopped state, so we don't want to resume. However, I should probably restore the old stopped state here. I'll try some more error recovery scenarios.

AIUI vm_resume() does the right thing already:

void vm_resume(RunState state)

{
    if (runstate_is_live(state)) {
        vm_start();
    } else {
        runstate_set(state);
    }
}
Quoted 30 lines

- Steve

Quoted 27 lines
Quoted 27 lines
migration/migration.c +0 -9
          return;
      }
  }
@@ -3738,7 +3751,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
      Error *local_err = NULL;
      uint64_t rate_limit;
      bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
-    int ret;
 
      /*
       * If there's a previous error, free it and prepare for another one.
@@ -3810,14 +3822,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
          return;
      }
 
-    if (migrate_mode_is_cpr(s)) {
-        ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
-        if (ret < 0) {
-            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
-            goto fail;
-        }
-    }
-
      if (migrate_background_snapshot()) {
          qemu_thread_create(&s->thread, "mig/snapshot",
                  bg_migration_thread, s, QEMU_THREAD_JOINABLE);
#33 lore raw

Re: [PATCH V2 04/11] migration: stop vm earlier for cpr

Quoted 61 lines · Fabiano Rosas
Quoted 49 lines · Steven Sistare
Quoted 48 lines · Fabiano Rosas
Quoted 37 lines · Steve Sistare

Stop the vm earlier for cpr, to guarantee consistent device state when CPR state is saved.

---
   migration/migration.c | 22 +++++++++++++---------
   1 file changed, 13 insertions(+), 9 deletions(-)
migration/migration.c +10 -0
index 0f47765..8a8e927 100644
@@ -2077,6 +2077,7 @@ void qmp_migrate(const char *uri, bool has_channels,
       MigrationState *s = migrate_get_current();
       g_autoptr(MigrationChannel) channel = NULL;
       MigrationAddress *addr = NULL;
+    bool stopped = false;
 
       /*
        * Having preliminary checks for uri and channel
@@ -2120,6 +2121,15 @@ void qmp_migrate(const char *uri, bool has_channels,
           }
       }
 
+    if (migrate_mode_is_cpr(s)) {
+        int ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
+        if (ret < 0) {
+            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
+            goto out;
+        }
+        stopped = true;
+    }
+
       if (cpr_state_save(&local_err)) {
           goto out;
       }

migration/migration.c out: · line 2170

Quoted 6 lines
migration/migration.c +3 -0
@@ -2155,6 +2165,9 @@ out:
           }
           migrate_fd_error(s, local_err);
           error_propagate(errp, local_err);
+        if (stopped && runstate_is_live(s->vm_old_state)) {
+            vm_start();
+        }

What about non-live states? Shouldn't this be:

Quoted 7 lines
Quoted 3 lines
if (stopped) {
     vm_resume();
}

Not quite. vm_old_state may be a stopped state, so we don't want to resume. However, I should probably restore the old stopped state here. I'll try some more error recovery scenarios.

AIUI vm_resume() does the right thing already:

Quoted 8 lines

void vm_resume(RunState state)

{
     if (runstate_is_live(state)) {
         vm_start();
     } else {
         runstate_set(state);
     }
}

Yes, thanks, I do need to set vm_old_state if not live. It should be:

out:

    ...
    if (stopped) {
        vm_resume(s->vm_old_state);
    }

- Steve

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
migration/migration.c +0 -9
           return;
       }
   }
@@ -3738,7 +3751,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
       Error *local_err = NULL;
       uint64_t rate_limit;
       bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
-    int ret;
 
       /*
        * If there's a previous error, free it and prepare for another one.
@@ -3810,14 +3822,6 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
           return;
       }
 
-    if (migrate_mode_is_cpr(s)) {
-        ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE);
-        if (ret < 0) {
-            error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
-            goto fail;
-        }
-    }
-
       if (migrate_background_snapshot()) {
           qemu_thread_create(&s->thread, "mig/snapshot",
                   bg_migration_thread, s, QEMU_THREAD_JOINABLE);
#34 lore raw

PATCH v2 5/11 physmem: preserve ram blocks for cpr

In reply to Steve Sistare

Save the memfd for anonymous ramblocks in CPR state, along with a name that uniquely identifies it. The block's idstr is not yet set, so it cannot be used for this purpose. Find the saved memfd in new QEMU when creating a block. QEMU hard-codes the length of some internally-created blocks, so to guard against that length changing, use lseek to get the actual length of an incoming memfd.

---
 system/physmem.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
system/physmem.c +24 -1
index efe95ff..e37352e 100644
@@ -73,6 +73,7 @@
 
 #include "qapi/qapi-types-migration.h"
 #include "migration/options.h"
+#include "migration/cpr.h"
 #include "migration/vmstate.h"
 
 #include "qemu/range.h"
@@ -1641,6 +1642,19 @@ void qemu_ram_unset_idstr(RAMBlock *block)
     }
 }
 
+static char *cpr_name(RAMBlock *block)
+{
+    MemoryRegion *mr = block->mr;
+    const char *mr_name = memory_region_name(mr);
+    g_autofree char *id = mr->dev ? qdev_get_dev_path(mr->dev) : NULL;
+
+    if (id) {
+        return g_strdup_printf("%s/%s", id, mr_name);
+    } else {
+        return g_strdup(mr_name);
+    }
+}
+
 size_t qemu_ram_pagesize(RAMBlock *rb)
 {
     return rb->page_size;
@@ -1836,13 +1850,17 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         } else if (new_block->flags & RAM_SHARED) {
             size_t max_length = new_block->max_length;
             MemoryRegion *mr = new_block->mr;
-            const char *name = memory_region_name(mr);
+            g_autofree char *name = cpr_name(new_block);
 
             new_block->mr->align = QEMU_VMALLOC_ALIGN;
+            new_block->fd = cpr_find_fd(name, 0);
 
             if (new_block->fd == -1) {
                 new_block->fd = qemu_memfd_create(name, max_length + mr->align,
                                                   0, 0, 0, errp);
+                cpr_save_fd(name, 0, new_block->fd);
+            } else {
+                new_block->max_length = lseek(new_block->fd, 0, SEEK_END);
             }
 
             if (new_block->fd >= 0) {
@@ -1852,6 +1870,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
                                                  false, 0, errp);
             }
             if (!new_block->host) {
+                cpr_delete_fd(name, 0);
                 qemu_mutex_unlock_ramlist();
                 return;
             }
@@ -2162,6 +2181,8 @@ static void reclaim_ramblock(RAMBlock *block)
 
 void qemu_ram_free(RAMBlock *block)
 {
+    g_autofree char *name = NULL;
+
     if (!block) {
         return;
     }
@@ -2172,6 +2193,8 @@ void qemu_ram_free(RAMBlock *block)
     }
 
     qemu_mutex_lock_ramlist();
+    name = cpr_name(block);
+    cpr_delete_fd(name, 0);
     QLIST_REMOVE_RCU(block, next);
     ram_list.mru_block = NULL;
     /* Write list before version */
Signature · 1 line
1.8.3.1
#35 lore raw

PATCH v2 6/11 migration: fix mismatched GPAs during cpr

In reply to Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

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

---
 include/exec/memory.h | 12 ++++++++++++
 migration/ram.c       | 15 +++++++++------
 system/memory.c       | 10 ++++++++--
 3 files changed, 29 insertions(+), 8 deletions(-)
include/exec/memory.h +12 -0
index c26ede3..227169e 100644
@@ -811,6 +811,7 @@ struct MemoryRegion {
     bool ram_device;
     bool enabled;
     bool warning_printed; /* For reservations */
+    bool has_addr;
     uint8_t vga_logging_count;
     MemoryRegion *alias;
     hwaddr alias_offset;
@@ -2408,6 +2409,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 +9 -6
index edec1a2..eaf3151 100644
@@ -4059,12 +4059,15 @@ static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length)
     }
     if (migrate_ignore_shared()) {
         hwaddr addr = qemu_get_be64(f);
-        if (migrate_ram_is_ignored(block) &&
-            block->mr->addr != addr) {
-            error_report("Mismatched GPAs for block %s "
-                         "%" PRId64 "!= %" PRId64, block->idstr,
-                         (uint64_t)addr, (uint64_t)block->mr->addr);
-            return -EINVAL;
+        if (migrate_ram_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, block->idstr,
+                             (uint64_t)addr, (uint64_t)block->mr->addr);
+                return -EINVAL;
+            }
         }
     }
     ret = rdma_block_notification_handle(f, block->idstr);
system/memory.c +8 -2
index 28a837d..b7548bf 100644
@@ -2655,7 +2655,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);
 }
 
@@ -2735,10 +2735,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
#36 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

Thanks,

Signature · 1 line
Peter Xu
#37 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 10 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It does not occur during normal migration.

Quoted 9 lines

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

I will continue this thread later and explain more fully.

- Steve

#38 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

In reply to Peter Xu
Quoted 17 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

Quoted 3 lines

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

The error_report does not bite for normal migration because migrate_ram_is_ignored() is false for the problematic blocks, so the block->mr->addr check is not performed. However, mr->addr is never fixed up in this case, which is a quiet potential bug, and this patch fixes that with the "has_addr" check.

For cpr-exec, migrate_ram_is_ignored() is true for all blocks, because we do not copy the contents over the migration stream, we preserve the memory in place. So we fall into the block->mr->addr sanity check and fail with the original code.

I will add this to the commit message.

- Steve

#39 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 27 lines · Steven Sistare
Quoted 17 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

Quoted 3 lines

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

The error_report does not bite for normal migration because migrate_ram_is_ignored() is false for the problematic blocks, so the block->mr->addr check is not performed. However, mr->addr is never fixed up in this case, which is a quiet potential bug, and this patch fixes that with the "has_addr" check.

Quoted 4 lines

For cpr-exec, migrate_ram_is_ignored() is true for all blocks, because we do not copy the contents over the migration stream, we preserve the memory in place. So we fall into the block->mr->addr sanity check and fail with the original code.

OK I get your point now. However this doesn't look right, instead I start to question why we need to send mr->addr at all..

As I said previously, AFAIU mr->addr should only be updated when there's some PCI config space updates so that it moves the MR around in the address space based on how guest drivers / BIOS (?) set things up. Now after these days not looking, and just started to look at this again, I think the only sane place to do this update is during a post_load().

And if we start to check some of the memory_region_set_address() users, that's exactly what happened..

- ich9_pm_iospace_update(), update addr for ICH9LPCPMRegs.io, where ich9_pm_post_load() also invokes it.

- pm_io_space_update(), updates PIIX4PMState.io, where vmstate_acpi_post_load() also invokes it.

I stopped here just looking at the initial two users, it looks all sane to me that it only got updated there, because the update requires pci config space being migrated first.

IOW, I don't think having mismatched mr->addr is wrong at this stage. Instead, I don't see why we should send mr->addr at all in this case during as early as SETUP, and I don't see anything justifies the mr->addr needs to be verified in parse_ramblock() since ignore-shared introduced by Yury in commit fbd162e629aaf8 in 2019.

We can't drop mr->addr now when it's on-wire, but I think we should drop the error report and addr check, instead of this patch.

Thanks,

Signature · 1 line
Peter Xu
#40 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 55 lines · Peter Xu
Quoted 27 lines · Steven Sistare
Quoted 17 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

Quoted 3 lines

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

The error_report does not bite for normal migration because migrate_ram_is_ignored() is false for the problematic blocks, so the block->mr->addr check is not performed. However, mr->addr is never fixed up in this case, which is a quiet potential bug, and this patch fixes that with the "has_addr" check.

Quoted 4 lines

For cpr-exec, migrate_ram_is_ignored() is true for all blocks, because we do not copy the contents over the migration stream, we preserve the memory in place. So we fall into the block->mr->addr sanity check and fail with the original code.

OK I get your point now. However this doesn't look right, instead I start to question why we need to send mr->addr at all..

As I said previously, AFAIU mr->addr should only be updated when there's some PCI config space updates so that it moves the MR around in the address space based on how guest drivers / BIOS (?) set things up. Now after these days not looking, and just started to look at this again, I think the only sane place to do this update is during a post_load().

And if we start to check some of the memory_region_set_address() users, that's exactly what happened..

- ich9_pm_iospace_update(), update addr for ICH9LPCPMRegs.io, where ich9_pm_post_load() also invokes it.

- pm_io_space_update(), updates PIIX4PMState.io, where vmstate_acpi_post_load() also invokes it.

I stopped here just looking at the initial two users, it looks all sane to me that it only got updated there, because the update requires pci config space being migrated first.

Quoted 8 lines

IOW, I don't think having mismatched mr->addr is wrong at this stage. Instead, I don't see why we should send mr->addr at all in this case during as early as SETUP, and I don't see anything justifies the mr->addr needs to be verified in parse_ramblock() since ignore-shared introduced by Yury in commit fbd162e629aaf8 in 2019.

We can't drop mr->addr now when it's on-wire, but I think we should drop the error report and addr check, instead of this patch.

As it turns out, my test case triggers this bug because it sets x-ignore-shared, but x-ignore-shared is not needed for cpr-exec, because migrate_ram_is_ignored is true for all blocks when mode==cpr-exec. So, the best fix for the GPAs bug for me is to stop setting x-ignore-shared. I will drop this patch.

I agree that post_load is the right place to restore mr->addr, and I don't understand why commit fbd162e629aaf8 added the error report, but I am going to leave it as is.

Thanks for reviewing this.

- Steve

#41 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 65 lines · Steven Sistare
Quoted 55 lines · Peter Xu
Quoted 27 lines · Steven Sistare
Quoted 17 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

Quoted 3 lines

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

The error_report does not bite for normal migration because migrate_ram_is_ignored() is false for the problematic blocks, so the block->mr->addr check is not performed. However, mr->addr is never fixed up in this case, which is a quiet potential bug, and this patch fixes that with the "has_addr" check.

Quoted 4 lines

For cpr-exec, migrate_ram_is_ignored() is true for all blocks, because we do not copy the contents over the migration stream, we preserve the memory in place. So we fall into the block->mr->addr sanity check and fail with the original code.

OK I get your point now. However this doesn't look right, instead I start to question why we need to send mr->addr at all..

As I said previously, AFAIU mr->addr should only be updated when there's some PCI config space updates so that it moves the MR around in the address space based on how guest drivers / BIOS (?) set things up. Now after these days not looking, and just started to look at this again, I think the only sane place to do this update is during a post_load().

And if we start to check some of the memory_region_set_address() users, that's exactly what happened..

- ich9_pm_iospace_update(), update addr for ICH9LPCPMRegs.io, where ich9_pm_post_load() also invokes it.

- pm_io_space_update(), updates PIIX4PMState.io, where vmstate_acpi_post_load() also invokes it.

I stopped here just looking at the initial two users, it looks all sane to me that it only got updated there, because the update requires pci config space being migrated first.

Quoted 8 lines

IOW, I don't think having mismatched mr->addr is wrong at this stage. Instead, I don't see why we should send mr->addr at all in this case during as early as SETUP, and I don't see anything justifies the mr->addr needs to be verified in parse_ramblock() since ignore-shared introduced by Yury in commit fbd162e629aaf8 in 2019.

We can't drop mr->addr now when it's on-wire, but I think we should drop the error report and addr check, instead of this patch.

Quoted 8 lines

As it turns out, my test case triggers this bug because it sets x-ignore-shared, but x-ignore-shared is not needed for cpr-exec, because migrate_ram_is_ignored is true for all blocks when mode==cpr-exec. So, the best fix for the GPAs bug for me is to stop setting x-ignore-shared. I will drop this patch.

I agree that post_load is the right place to restore mr->addr, and I don't understand why commit fbd162e629aaf8 added the error report, but I am going to leave it as is.

Ah, I didn't notice that cpr special cased migrate_ram_is_ignored()..

Shall we stick with the old check, but always require cpr to rely on ignore-shared?

Then we replace this patch with removing the error_report, probably together with not caring about whatever is received at all.. would that be cleaner?

Thanks,

Signature · 1 line
Peter Xu
#42 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 75 lines · Peter Xu
Quoted 65 lines · Steven Sistare
Quoted 55 lines · Peter Xu
Quoted 27 lines · Steven Sistare
Quoted 17 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

Quoted 3 lines

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

The error_report does not bite for normal migration because migrate_ram_is_ignored() is false for the problematic blocks, so the block->mr->addr check is not performed. However, mr->addr is never fixed up in this case, which is a quiet potential bug, and this patch fixes that with the "has_addr" check.

Quoted 4 lines

For cpr-exec, migrate_ram_is_ignored() is true for all blocks, because we do not copy the contents over the migration stream, we preserve the memory in place. So we fall into the block->mr->addr sanity check and fail with the original code.

OK I get your point now. However this doesn't look right, instead I start to question why we need to send mr->addr at all..

As I said previously, AFAIU mr->addr should only be updated when there's some PCI config space updates so that it moves the MR around in the address space based on how guest drivers / BIOS (?) set things up. Now after these days not looking, and just started to look at this again, I think the only sane place to do this update is during a post_load().

And if we start to check some of the memory_region_set_address() users, that's exactly what happened..

- ich9_pm_iospace_update(), update addr for ICH9LPCPMRegs.io, where ich9_pm_post_load() also invokes it.

- pm_io_space_update(), updates PIIX4PMState.io, where vmstate_acpi_post_load() also invokes it.

I stopped here just looking at the initial two users, it looks all sane to me that it only got updated there, because the update requires pci config space being migrated first.

Quoted 8 lines

IOW, I don't think having mismatched mr->addr is wrong at this stage. Instead, I don't see why we should send mr->addr at all in this case during as early as SETUP, and I don't see anything justifies the mr->addr needs to be verified in parse_ramblock() since ignore-shared introduced by Yury in commit fbd162e629aaf8 in 2019.

We can't drop mr->addr now when it's on-wire, but I think we should drop the error report and addr check, instead of this patch.

Quoted 8 lines

As it turns out, my test case triggers this bug because it sets x-ignore-shared, but x-ignore-shared is not needed for cpr-exec, because migrate_ram_is_ignored is true for all blocks when mode==cpr-exec. So, the best fix for the GPAs bug for me is to stop setting x-ignore-shared. I will drop this patch.

I agree that post_load is the right place to restore mr->addr, and I don't understand why commit fbd162e629aaf8 added the error report, but I am going to leave it as is.

Quoted 8 lines

Ah, I didn't notice that cpr special cased migrate_ram_is_ignored()..

Shall we stick with the old check, but always require cpr to rely on ignore-shared?

Then we replace this patch with removing the error_report, probably together with not caring about whatever is received at all.. would that be cleaner?

migrate_ram_is_ignored() is called in many places and must return true for cpr-exec/cpr-transfer, independently of migrate_ignore_shared. That logic must remain as is.

The cleanest change is no change, just dropping this patch. I was just confused when I set x-ignore-shared for the test.

However, if an unsuspecting user sets x-ignore-shared, it will trigger this error, so perhaps I should delete the error_report.

- Steve

#43 lore raw

Re: [PATCH V2 06/11] migration: fix mismatched GPAs during cpr

Quoted 81 lines · Steven Sistare
Quoted 80 lines · Peter Xu
Quoted 65 lines · Steven Sistare
Quoted 55 lines · Peter Xu
Quoted 27 lines · Steven Sistare
Quoted 17 lines · Peter Xu
Quoted 7 lines · Steve Sistare

For new cpr modes, ramblock_is_ignored will always be true, because the memory is preserved in place rather than copied. However, for an ignored block, parse_ramblock currently requires that the received address of the block must match the address of the statically initialized region on the target. This fails for a PCI rom block, because the memory 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 migration.

Is this a common fix with/without cpr mode?

It looks to me mr->addr (for these ROMs) should only be set in PCI config region updates as you mentioned. But then I didn't figure out when they're updated on dest in live migration: the ramblock info was sent at the beginning of migration, so it doesn't even have PCI config space migrated; I thought the real mr->addr should be in there.

Quoted 3 lines

I also failed to understand yet on why the mr->addr check needs to be done by ignore-shared only. Some explanation would be greatly helpful around this area..

The error_report does not bite for normal migration because migrate_ram_is_ignored() is false for the problematic blocks, so the block->mr->addr check is not performed. However, mr->addr is never fixed up in this case, which is a quiet potential bug, and this patch fixes that with the "has_addr" check.

Quoted 4 lines

For cpr-exec, migrate_ram_is_ignored() is true for all blocks, because we do not copy the contents over the migration stream, we preserve the memory in place. So we fall into the block->mr->addr sanity check and fail with the original code.

OK I get your point now. However this doesn't look right, instead I start to question why we need to send mr->addr at all..

As I said previously, AFAIU mr->addr should only be updated when there's some PCI config space updates so that it moves the MR around in the address space based on how guest drivers / BIOS (?) set things up. Now after these days not looking, and just started to look at this again, I think the only sane place to do this update is during a post_load().

And if we start to check some of the memory_region_set_address() users, that's exactly what happened..

- ich9_pm_iospace_update(), update addr for ICH9LPCPMRegs.io, where ich9_pm_post_load() also invokes it.

- pm_io_space_update(), updates PIIX4PMState.io, where vmstate_acpi_post_load() also invokes it.

I stopped here just looking at the initial two users, it looks all sane to me that it only got updated there, because the update requires pci config space being migrated first.

Quoted 8 lines

IOW, I don't think having mismatched mr->addr is wrong at this stage. Instead, I don't see why we should send mr->addr at all in this case during as early as SETUP, and I don't see anything justifies the mr->addr needs to be verified in parse_ramblock() since ignore-shared introduced by Yury in commit fbd162e629aaf8 in 2019.

We can't drop mr->addr now when it's on-wire, but I think we should drop the error report and addr check, instead of this patch.

Quoted 8 lines

As it turns out, my test case triggers this bug because it sets x-ignore-shared, but x-ignore-shared is not needed for cpr-exec, because migrate_ram_is_ignored is true for all blocks when mode==cpr-exec. So, the best fix for the GPAs bug for me is to stop setting x-ignore-shared. I will drop this patch.

I agree that post_load is the right place to restore mr->addr, and I don't understand why commit fbd162e629aaf8 added the error report, but I am going to leave it as is.

Ah, I didn't notice that cpr special cased migrate_ram_is_ignored()..

Shall we stick with the old check, but always require cpr to rely on ignore-shared?

Quoted 7 lines
Quoted 3 lines

Then we replace this patch with removing the error_report, probably together with not caring about whatever is received at all.. would that be cleaner?

migrate_ram_is_ignored() is called in many places and must return true for cpr-exec/cpr-transfer, independently of migrate_ignore_shared. That logic must remain as is.

Is this because cpr can fail some ramblock in qemu_ram_is_named_file()?

It's not obvious in this case, maybe some re-strcture would be nice. Would something like this look nicer and easier to understand?

===8<===

migration/ram.c +20 -5
index 1e1e05e859..ace635b167 100644
@@ -214,14 +214,29 @@ static bool postcopy_preempt_active(void)
     return migrate_postcopy_preempt() && migration_in_postcopy();
 }
 
-bool migrate_ram_is_ignored(RAMBlock *block)
+/* Whether the destination QEMU can share the access on this ramblock? */
+bool migrate_ram_is_shared(RAMBlock *block)
 {
     MigMode mode = migrate_mode();
+
+    /* Private ram is never share-able */
+    if (!qemu_ram_is_shared(block)) {
+        return false;
+    }
+
+    /* Named file ram is always assumed to be share-able */
+    if (qemu_ram_is_named_file(block)) {
+        return true;
+    }
+
+    /* It's a private fd, only cpr mode can share it (by sharing fd) */
+    return (mode == MIG_MODE_CPR_EXEC) || (mode == MIG_MODE_CPR_TRANSFER);
+}
+
+bool migrate_ram_is_ignored(RAMBlock *block)
+{
     return !qemu_ram_is_migratable(block) ||
-           mode == MIG_MODE_CPR_EXEC ||
-           mode == MIG_MODE_CPR_TRANSFER ||
-           (migrate_ignore_shared() && qemu_ram_is_shared(block)
-                                    && qemu_ram_is_named_file(block));
+           (migrate_ignore_shared() && migrate_ram_is_shared(block));
 }

===8<===

Please feel free to squash this to your patch in whatever way if it looks reasonable to you.

Quoted 6 lines

The cleanest change is no change, just dropping this patch. I was just confused when I set x-ignore-shared for the test.

However, if an unsuspecting user sets x-ignore-shared, it will trigger this error, so perhaps I should delete the error_report.

Yes, feel free to send that as a separate patch if you want, since we digged it this far it'll be nice we fix it even if it's not relevant now.

Thanks,

Signature · 1 line
Peter Xu
#44 lore raw

PATCH v2 7/11 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 191916f..6ebf192 100644
@@ -662,6 +662,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 e764416..614c3e5 100644
@@ -272,6 +272,15 @@ int qemu_socketpair(int domain, int type, int protocol, int sv[2])
     return ret;
 }
 
+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 b623830..c3e969a 100644
@@ -222,6 +222,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
#45 lore raw

PATCH v2 8/11 vl: helper to request exec

In reply to Steve Sistare

Add a qemu_system_exec_request() hook that causes the main loop to exit and exec a command using the specified arguments. This will be used during CPR to exec a new version of QEMU.

---
 include/sysemu/runstate.h |  3 +++
 system/runstate.c         | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
include/sysemu/runstate.h +3 -0
index 0117d24..cb669cf 100644
@@ -80,6 +80,8 @@ typedef enum WakeupReason {
     QEMU_WAKEUP_REASON_OTHER,
 } WakeupReason;
 
+typedef void (*qemu_exec_func)(char **exec_argv);
+
 void qemu_system_reset_request(ShutdownCause reason);
 void qemu_system_suspend_request(void);
 void qemu_register_suspend_notifier(Notifier *notifier);
@@ -91,6 +93,7 @@ void qemu_register_wakeup_support(void);
 void qemu_system_shutdown_request_with_code(ShutdownCause reason,
                                             int exit_code);
 void qemu_system_shutdown_request(ShutdownCause reason);
+void qemu_system_exec_request(qemu_exec_func func, const strList *args);
 void qemu_system_powerdown_request(void);
 void qemu_register_powerdown_notifier(Notifier *notifier);
 void qemu_register_shutdown_notifier(Notifier *notifier);
system/runstate.c +29 -0
index ec32e27..afc56e4 100644
@@ -40,6 +40,7 @@
 #include "qapi/error.h"
 #include "qapi/qapi-commands-run-state.h"
 #include "qapi/qapi-events-run-state.h"
+#include "qapi/type-helpers.h"
 #include "qemu/accel.h"
 #include "qemu/error-report.h"
 #include "qemu/job.h"
@@ -400,6 +401,8 @@ static NotifierList wakeup_notifiers =
 static NotifierList shutdown_notifiers =
     NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
+qemu_exec_func exec_func;
+static char **exec_argv;
 
 ShutdownCause qemu_shutdown_requested_get(void)
 {
@@ -416,6 +419,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) {
@@ -693,6 +701,23 @@ void qemu_system_shutdown_request(ShutdownCause reason)
     qemu_notify_event();
 }
 
+static void qemu_system_exec(void)
+{
+    exec_func(exec_argv);
+
+    /* exec failed */
+    g_strfreev(exec_argv);
+    exec_argv = NULL;
+    exec_func = NULL;
+}
+
+void qemu_system_exec_request(qemu_exec_func func, const strList *args)
+{
+    exec_func = func;
+    exec_argv = strv_from_str_list(args);
+    qemu_notify_event();
+}
+
 static void qemu_system_powerdown(void)
 {
     qapi_event_send_powerdown();
@@ -739,6 +764,10 @@ static bool main_loop_should_exit(int *status)
     if (qemu_suspend_requested()) {
         qemu_system_suspend();
     }
+    if (qemu_exec_requested()) {
+        qemu_system_exec();
+        return false;
+    }
     request = qemu_shutdown_requested();
     if (request) {
         qemu_kill_report();
Signature · 1 line
1.8.3.1
#46 lore raw

PATCH v2 9/11 migration: cpr-exec-command parameter

In reply to Steve Sistare

Create the cpr-exec-command migration parameter, defined as a list of strings. It will be used for cpr-exec migration mode in a subsequent patch, and contains forward references to cpr-exec mode in the qapi doc.

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

---
 hmp-commands.hx                |  2 +-
 migration/migration-hmp-cmds.c | 25 +++++++++++++++++++++++++
 migration/options.c            | 14 ++++++++++++++
 qapi/migration.json            | 21 ++++++++++++++++++---
 4 files changed, 58 insertions(+), 4 deletions(-)
hmp-commands.hx +1 -1
index 06746f0..0e04eac 100644
@@ -1009,7 +1009,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-hmp-cmds.c +25 -0
index 7d608d2..16a4b00 100644
@@ -229,6 +229,18 @@ void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
     qapi_free_MigrationCapabilityStatusList(caps);
 }
 
+static void monitor_print_cpr_exec_command(Monitor *mon, strList *args)
+{
+    monitor_printf(mon, "%s:",
+        MigrationParameter_str(MIGRATION_PARAMETER_CPR_EXEC_COMMAND));
+
+    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;
@@ -358,6 +370,9 @@ void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
                                MIGRATION_PARAMETER_DIRECT_IO),
                            params->direct_io ? "on" : "off");
         }
+
+        assert(params->has_cpr_exec_command);
+        monitor_print_cpr_exec_command(mon, params->cpr_exec_command);
     }
 
     qapi_free_MigrationParameters(params);
@@ -635,6 +650,16 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
         p->has_direct_io = true;
         visit_type_bool(v, param, &p->direct_io, &err);
         break;
+    case MIGRATION_PARAMETER_CPR_EXEC_COMMAND: {
+        g_autofree char **strv = g_strsplit(valuestr ?: "", " ", -1);
+        strList **tail = &p->cpr_exec_command;
+
+        for (int i = 0; strv[i]; i++) {
+            QAPI_LIST_APPEND(tail, strv[i]);
+        }
+        p->has_cpr_exec_command = true;
+        break;
+    }
     default:
         assert(0);
     }
migration/options.c +14 -0
index 305397a..b8d5f72 100644
@@ -931,6 +931,9 @@ MigrationParameters *qmp_query_migrate_parameters(Error **errp)
     params->zero_page_detection = s->parameters.zero_page_detection;
     params->has_direct_io = true;
     params->direct_io = s->parameters.direct_io;
+    params->has_cpr_exec_command = true;
+    params->cpr_exec_command = QAPI_CLONE(strList,
+                                          s->parameters.cpr_exec_command);
 
     return params;
 }
@@ -964,6 +967,7 @@ void migrate_params_init(MigrationParameters *params)
     params->has_mode = true;
     params->has_zero_page_detection = true;
     params->has_direct_io = true;
+    params->has_cpr_exec_command = true;
 }
 
 /*
@@ -1252,6 +1256,10 @@ static void migrate_params_test_apply(MigrateSetParameters *params,
     if (params->has_direct_io) {
         dest->direct_io = params->direct_io;
     }
+
+    if (params->has_cpr_exec_command) {
+        dest->cpr_exec_command = params->cpr_exec_command;
+    }
 }
 
 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
@@ -1381,6 +1389,12 @@ static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
     if (params->has_direct_io) {
         s->parameters.direct_io = params->direct_io;
     }
+
+    if (params->has_cpr_exec_command) {
+        qapi_free_strList(s->parameters.cpr_exec_command);
+        s->parameters.cpr_exec_command =
+            QAPI_CLONE(strList, params->cpr_exec_command);
+    }
 }
 
 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
qapi/migration.json +18 -3
index 0f24206..20092d2 100644
@@ -829,6 +829,10 @@
 #     only has effect if the @mapped-ram capability is enabled.
 #     (Since 9.1)
 #
+# @cpr-exec-command: Command to start the new QEMU process when @mode
+#     is @cpr-exec.  The first list element is the program's filename,
+#     the remainder its arguments. (Since 9.1)
+#
 # Features:
 #
 # @unstable: Members @x-checkpoint-delay and
@@ -854,7 +858,8 @@
            'vcpu-dirty-limit',
            'mode',
            'zero-page-detection',
-           'direct-io'] }
+           'direct-io',
+           'cpr-exec-command'] }
 
 ##
 # @MigrateSetParameters:
@@ -1004,6 +1009,10 @@
 #     only has effect if the @mapped-ram capability is enabled.
 #     (Since 9.1)
 #
+# @cpr-exec-command: Command to start the new QEMU process when @mode
+#     is @cpr-exec.  The first list element is the program's filename,
+#     the remainder its arguments. (Since 9.1)
+#
 # Features:
 #
 # @unstable: Members @x-checkpoint-delay and
@@ -1044,7 +1053,8 @@
             '*vcpu-dirty-limit': 'uint64',
             '*mode': 'MigMode',
             '*zero-page-detection': 'ZeroPageDetection',
-            '*direct-io': 'bool' } }
+            '*direct-io': 'bool',
+            '*cpr-exec-command': [ 'str' ]} }
 
 ##
 # @migrate-set-parameters:
@@ -1208,6 +1218,10 @@
 #     only has effect if the @mapped-ram capability is enabled.
 #     (Since 9.1)
 #
+# @cpr-exec-command: Command to start the new QEMU process when @mode
+#     is @cpr-exec.  The first list element is the program's filename,
+#     the remainder its arguments. (Since 9.1)
+#
 # Features:
 #
 # @unstable: Members @x-checkpoint-delay and
@@ -1245,7 +1259,8 @@
             '*vcpu-dirty-limit': 'uint64',
             '*mode': 'MigMode',
             '*zero-page-detection': 'ZeroPageDetection',
-            '*direct-io': 'bool' } }
+            '*direct-io': 'bool',
+            '*cpr-exec-command': [ 'str' ]} }
 
 ##
 # @query-migrate-parameters:
Signature · 1 line
1.8.3.1
#47 lore raw

PATCH v2 10/11 migration: cpr-exec save and load

In reply to Steve Sistare

To preserve CPR state across exec, create a QEMUFile based on a memfd, and keep the memfd open across exec. Save the value of the memfd in an environment variable so post-exec QEMU can find it.

These new functions are called in a subsequent patch.

---
 include/migration/cpr.h |  5 +++
 migration/cpr-exec.c    | 95 +++++++++++++++++++++++++++++++++++++++++++++++++
 migration/meson.build   |  1 +
 3 files changed, 101 insertions(+)
 create mode 100644 migration/cpr-exec.c
include/migration/cpr.h +5 -0
index 42b4019..76d6ccb 100644
@@ -25,4 +25,9 @@ void cpr_resave_fd(const char *name, int id, int fd);
 int cpr_state_save(Error **errp);
 int cpr_state_load(Error **errp);
 
+QEMUFile *cpr_exec_output(Error **errp);
+QEMUFile *cpr_exec_input(Error **errp);
+void cpr_exec_persist_state(QEMUFile *f);
+bool cpr_exec_has_state(void);
+void cpr_exec_unpersist_state(void);
 #endif
migration/cpr-exec.c new file +95 -0

95 lines not on this page · raw message

migration/meson.build +1 -0
index 87feb4c..dd1d315 100644
@@ -14,6 +14,7 @@ system_ss.add(files(
   'channel.c',
   'channel-block.c',
   'cpr.c',
+  'cpr-exec.c',
   'dirtyrate.c',
   'exec.c',
   'fd.c',
Signature · 1 line
1.8.3.1
#48 lore raw

PATCH v2 11/11 migration: cpr-exec mode

In reply to Steve Sistare

Add the cpr-exec migration mode. Usage:
qemu-system-$arch -machine anon-alloc=memfd ...
migrate_set_parameter mode cpr-exec

  migrate_set_parameter cpr-exec-command \
    <arg1> <arg2> ... -incoming <uri-1> \

migrate -d <uri-1>

The migrate command stops the VM, saves state to uri-1, directly exec's a new version of QEMU on the same host, replacing the original process while retaining its PID, and loads state from uri-1. Guest RAM is preserved in place, albeit with new virtual addresses.

The new QEMU process is started by exec'ing the command specified by the @cpr-exec-command parameter. The first word of the command is the binary, and the remaining words are its arguments. The command may be a direct invocation of new QEMU, or may be a non-QEMU command that exec's the new QEMU binary.

This mode creates a second migration channel that is not visible to the user. At the start of migration, old QEMU saves CPR state to the second channel, and at the end of migration, it tells the main loop to call cpr_exec. New QEMU loads CPR state early, before objects are created.

Because old QEMU terminates when new QEMU starts, one cannot stream data between the two, so uri-1 must be a type, such as a file, that accepts all data before old QEMU exits. Otherwise, old QEMU may quietly block writing to the channel.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process. The memfds are kept open across exec by clearing the close-on-exec flag, their values are saved in CPR state, and they are mmap'd in new qemu.

Note that the anon-alloc option is not related to memory-backend-memfd. Later patches add support for memory-backend-memfd.

---
 include/migration/cpr.h |  2 ++
 migration/cpr-exec.c    | 85 +++++++++++++++++++++++++++++++++++++++++++++++++
 migration/cpr.c         | 37 ++++++++++++++++++---
 migration/migration.c   | 14 ++++++--
 migration/ram.c         |  2 ++
 qapi/migration.json     | 24 +++++++++++++-
 6 files changed, 157 insertions(+), 7 deletions(-)
include/migration/cpr.h +2 -0
index 76d6ccb..c6c60f8 100644
@@ -30,4 +30,6 @@ QEMUFile *cpr_exec_input(Error **errp);
 void cpr_exec_persist_state(QEMUFile *f);
 bool cpr_exec_has_state(void);
 void cpr_exec_unpersist_state(void);
+void cpr_mig_init(void);
+void cpr_unpreserve_fds(void);
 #endif
migration/cpr-exec.c +85 -0

96 lines not on this page · raw message

migration/cpr.c +33 -4
index 1c296c6..f756c15 100644
@@ -9,6 +9,7 @@
 #include "qapi/error.h"
 #include "migration/cpr.h"
 #include "migration/misc.h"
+#include "migration/options.h"
 #include "migration/qemu-file.h"
 #include "migration/savevm.h"
 #include "migration/vmstate.h"
@@ -160,9 +161,16 @@ int cpr_state_save(Error **errp)
 {
     int ret;
     QEMUFile *f;
+    MigMode mode = migrate_mode();
 
-    /* set f based on mode in a later patch in this series */
-    return 0;
+    if (mode == MIG_MODE_CPR_EXEC) {
+        f = cpr_exec_output(errp);
+    } else {
+        return 0;
+    }
+    if (!f) {
+        return -1;
+    }
 
     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
@@ -170,8 +178,14 @@ int cpr_state_save(Error **errp)
     ret = vmstate_save_state(f, &vmstate_cpr_state, &cpr_state, 0);
     if (ret) {
         error_setg(errp, "vmstate_save_state error %d", ret);
+        goto out;
     }
 
+    if (mode == MIG_MODE_CPR_EXEC) {
+        cpr_exec_persist_state(f);
+    }
+
+out:
     qemu_fclose(f);
     return ret;
 }
@@ -182,8 +196,18 @@ int cpr_state_load(Error **errp)
     uint32_t v;
     QEMUFile *f;
 
-    /* set f based on mode in a later patch in this series */
-    return 0;
+    /*
+     * Mode will be loaded in CPR state, so cannot use it to decide which
+     * form of state to load.
+     */
+    if (cpr_exec_has_state()) {
+        f = cpr_exec_input(errp);
+    } else {
+        return 0;
+    }
+    if (!f) {
+        return -1;
+    }
 
     v = qemu_get_be32(f);
     if (v != QEMU_VM_FILE_MAGIC) {
@@ -203,6 +227,11 @@ int cpr_state_load(Error **errp)
         error_setg(errp, "vmstate_load_state error %d", ret);
     }
 
+    if (migrate_mode() == MIG_MODE_CPR_EXEC) {
+        /* Set cloexec to prevent fd leaks from fork until the next cpr-exec */
+        cpr_unpreserve_fds();
+    }
+
     qemu_fclose(f);
     return ret;
 }
migration/migration.c +12 -2
index 8a8e927..a4a020e 100644
@@ -73,9 +73,10 @@
 
 #define INMIGRATE_DEFAULT_EXIT_ON_ERROR true
 
-static NotifierWithReturnList migration_state_notifiers[] = {
+static NotifierWithReturnList migration_state_notifiers[MIG_MODE__MAX] = {
     NOTIFIER_ELEM_INIT(migration_state_notifiers, MIG_MODE_NORMAL),
     NOTIFIER_ELEM_INIT(migration_state_notifiers, MIG_MODE_CPR_REBOOT),
+    NOTIFIER_ELEM_INIT(migration_state_notifiers, MIG_MODE_CPR_EXEC),
 };
 
 /* Messages sent on the return path from destination to source */
@@ -264,6 +265,7 @@ void migration_object_init(void)
 
     ram_mig_init();
     dirty_bitmap_mig_init();
+    cpr_mig_init();
 }
 
 typedef struct {
@@ -1693,7 +1695,9 @@ bool migration_thread_is_self(void)
 
 bool migrate_mode_is_cpr(MigrationState *s)
 {
-    return s->parameters.mode == MIG_MODE_CPR_REBOOT;
+    MigMode mode = s->parameters.mode;
+    return mode == MIG_MODE_CPR_REBOOT ||
+           mode == MIG_MODE_CPR_EXEC;
 }
 
 int migrate_init(MigrationState *s, Error **errp)
@@ -2028,6 +2032,12 @@ static bool migrate_prepare(MigrationState *s, bool resume, Error **errp)
         return false;
     }
 
+    if (migrate_mode() == MIG_MODE_CPR_EXEC &&
+        !s->parameters.has_cpr_exec_command) {
+        error_setg(errp, "cpr-exec mode requires setting cpr-exec-command");
+        return false;
+    }
+
     if (migration_is_blocked(errp)) {
         return false;
     }
migration/ram.c +2 -0
index eaf3151..45b8f00 100644
@@ -216,7 +216,9 @@ static bool postcopy_preempt_active(void)
 
 bool migrate_ram_is_ignored(RAMBlock *block)
 {
+    MigMode mode = migrate_mode();
     return !qemu_ram_is_migratable(block) ||
+           mode == MIG_MODE_CPR_EXEC ||
            (migrate_ignore_shared() && qemu_ram_is_shared(block)
                                     && qemu_ram_is_named_file(block));
 }
qapi/migration.json +23 -1
index 20092d2..4e626df 100644
@@ -604,9 +604,31 @@
 #     or COLO.
 #
 #     (since 8.2)
+#
+# @cpr-exec: The migrate command stops the VM, saves state to the
+#     migration channel, directly exec's a new version of QEMU on the
+#     same host, replacing the original process while retaining its
+#     PID, and loads state from the channel.  Guest RAM is preserved
+#     in place.
+#
+#     Old QEMU starts new QEMU by exec'ing the command specified by
+#     the @cpr-exec-command parameter.  The command may be a direct
+#     invocation of new QEMU, or may be a non-QEMU command that exec's
+#     the new QEMU binary.
+#
+#     Because old QEMU terminates when new QEMU starts, one cannot
+#     stream data between the two, so the channel must be a type,
+#     such as a file, that accepts all data before old QEMU exits.
+#     Otherwise, old QEMU may quietly block writing to the channel.
+#
+#     Memory-backend objects must have the share=on attribute, but
+#     memory-backend-epc is not supported.  The VM must be started
+#     with the '-machine anon-alloc=memfd' option.
+#
+#     (since 9.1)
 ##
 { 'enum': 'MigMode',
-  'data': [ 'normal', 'cpr-reboot' ] }
+  'data': [ 'normal', 'cpr-reboot', 'cpr-exec' ] }
 
 ##
 # @ZeroPageDetection:
Signature · 1 line
1.8.3.1
#49 lore raw
In reply to Steve Sistare

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

I really, really hope we can avoid this..

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

Quoted 1 line

* anon-alloc (command-line option for -machine)

Igor questioned this, and I second his opinion.. We can leave the discussion there for this one.

Quoted 14 lines

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Quoted 4 lines

Why?

This mode has less impact on the guest than any other method of updating in place.

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

In this whole process, I doubt (2) could be relatively fast, (3) I donno, maybe it could be slow but I never measured; Paolo may have good idea as I know he used to work on qboot.

For (1), I also doubt in your test cases it's fast, but it may not always be fast. Consider the guest has a huge TBs of shared mem, even if the memory will be completely shared between src/dst QEMUs, the pgtable won't! It means if the TBs are mapped in PAGE_SIZE tearing down the src QEMU pgtable alone can even take time, and that will be accounted in step (1) and further in exec() request.

All these fuss will be avoided if you use a generic live migration model like cpr-transfer you proposed. That's also cleaner.

Quoted 7 lines

The pause time is much lower, because devices need not be torn down and recreated, DMA does not need to be drained and quiesced, and minimal state is copied to new QEMU. Further, there are no constraints on the guest. By contrast, cpr-reboot mode requires the guest to support S3 suspend-to-ram, and suspending plus resuming vfio devices adds multiple seconds to the guest pause time. Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

Again, I raised the question on why this would matter, as after all mgmt app will need to coop with reconnections due to the fact they'll need to support a generic live migration, in which case reconnection is a must.

So far it doesn't sound like a performance critical path, for example, to do the mgmt reconnects on the ports. So this might be an optimization that most mgmt apps may not care much?

Quoted 7 lines

These benefits all derive from the core design principle of this mode, which is preserving open descriptors. This approach is very general and can be used to support a wide variety of devices that do not have hardware support for live migration, including but not limited to: vfio, chardev, vhost, vdpa, and iommufd. Some devices need new kernel software interfaces to allow a descriptor to be used in a process that did not originally open it.

Yes, I still think this is a great idea. It just can also be built on top of something else than exec().

Quoted 6 lines

In a containerized QEMU environment, cpr-exec reuses an existing QEMU container and its assigned resources. By contrast, consider a design in which a new container is created on the same host as the target of the CPR operation. Resources must be reserved for the new container, while the old container still reserves resources until the operation completes.

Note that if we need to share RAM anyway, the resources consumption should be minimal, as mem should IMHO be the major concern (except CPU, but CPU isn't a concern in this scenario) in container world and here the shared guest mem shouldn't be accounted to the dest container. So IMHO it's about the metadata QEMU/KVM needs to do the hypervisor work, it seems to me, and that should be relatively small.

In that case I don't yet see it a huge improvement, if the dest container is cheap to initiate.

Quoted 1 line

Avoiding over commitment requires extra work in the management layer.

So it would be nice to know what needs to be overcommitted here. I confess I don't know much on containerized VMs, so maybe the page cache can be a problem even if shared. But I hope we can spell that out. Logically IIUC memcg shouldn't account those page cache if preallocated, because memcg accounting should be done at folio allocations, at least, where the page cache should miss first (so not this case..).

Quoted 25 lines

This is one reason why a cloud provider may prefer cpr-exec. A second reason is that the container may include agents with their own connections to the outside world, and such connections remain intact if the container is reused.

How?

All memory that is mapped by the guest is preserved in place. Indeed, it must be, because it may be the target of DMA requests, which are not quiesced during cpr-exec. All such memory must be mmap'able in new QEMU. This is easy for named memory-backend objects, as long as they are mapped shared, because they are visible in the file system in both old and new QEMU. Anonymous memory must be allocated using memfd_create rather than MAP_ANON, so the memfd's can be sent to new QEMU. Pages that were locked in memory for DMA in old QEMU remain locked in new QEMU, because the descriptor of the device that locked them remains open.

cpr-exec preserves descriptors across exec by clearing the CLOEXEC flag, and by sending the unique name and value of each descriptor to new QEMU via CPR state.

For device descriptors, new QEMU reuses the descriptor when creating the device, rather than opening it again. The same holds for chardevs. For memfd descriptors, new QEMU mmap's the preserved memfd when a ramblock is created.

Quoted 4 lines

CPR state cannot be sent over the normal migration channel, because devices and backends are created prior to reading the channel, so this mode sends CPR state over a second migration channel that is not visible to the user. New QEMU reads the second channel prior to creating devices or backends.

Oh, maybe this is the reason that cpr-transfer will need a separate uri..

Thanks,

Signature · 1 line
Peter Xu
#50 lore raw
Quoted 23 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

Quoted 5 lines

I really, really hope we can avoid this..

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

This is no different than live migration, requiring a management agent to launch target qemu with all the arguments use to start source QEMU. Now that same agent will send the arguments via cpr-exec-command.

Quoted 4 lines
Quoted 1 line

* anon-alloc (command-line option for -machine)

Igor questioned this, and I second his opinion.. We can leave the discussion there for this one.

Continued on the other thread.

Quoted 13 lines
Quoted 13 lines

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Quoted 7 lines
Quoted 4 lines

Why?

This mode has less impact on the guest than any other method of updating in place.

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

Not yet, but I will measure it.

Quoted 11 lines

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

In this whole process, I doubt (2) could be relatively fast, (3) I donno, maybe it could be slow but I never measured; Paolo may have good idea as I know he used to work on qboot.

We'll see, but in any case these take < 100 msec, which is a wonderfully short pause time unless your customer is doing high speed stock trading. If cpr-transfer is faster still, that's gravy, but cpr-exec is still great.

Quoted 6 lines

For (1), I also doubt in your test cases it's fast, but it may not always be fast. Consider the guest has a huge TBs of shared mem, even if the memory will be completely shared between src/dst QEMUs, the pgtable won't! It means if the TBs are mapped in PAGE_SIZE tearing down the src QEMU pgtable alone can even take time, and that will be accounted in step (1) and further in exec() request.

Yes, there is an O(n) effect here, but it is a fast O(n) when the memory is backed by huge pages. In UEK, we make it faster still by unmapping in parallel with multiple threads. I don't have the data handy but can share after running some experiments. Regardless, this time is negligible for small and medium size guests, which form the majority of instances in a cloud.

Quoted 11 lines

All these fuss will be avoided if you use a generic live migration model like cpr-transfer you proposed. That's also cleaner.

Quoted 7 lines

The pause time is much lower, because devices need not be torn down and recreated, DMA does not need to be drained and quiesced, and minimal state is copied to new QEMU. Further, there are no constraints on the guest. By contrast, cpr-reboot mode requires the guest to support S3 suspend-to-ram, and suspending plus resuming vfio devices adds multiple seconds to the guest pause time. Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

Quoted 7 lines

Again, I raised the question on why this would matter, as after all mgmt app will need to coop with reconnections due to the fact they'll need to support a generic live migration, in which case reconnection is a must.

So far it doesn't sound like a performance critical path, for example, to do the mgmt reconnects on the ports. So this might be an optimization that most mgmt apps may not care much?

Perhaps. I view the chardev preservation as nice to have, but not essential. It does not appear in this series, other than in docs. It's easy to implement given the CPR foundation. I suggest we continue this discussion when I post the chardev series, so we can focus on the core functionality.

Quoted 24 lines
Quoted 6 lines

These benefits all derive from the core design principle of this mode, which is preserving open descriptors. This approach is very general and can be used to support a wide variety of devices that do not have hardware support for live migration, including but not limited to: vfio, chardev, vhost, vdpa, and iommufd. Some devices need new kernel software interfaces to allow a descriptor to be used in a process that did not originally open it.

Yes, I still think this is a great idea. It just can also be built on top of something else than exec().

Quoted 6 lines

In a containerized QEMU environment, cpr-exec reuses an existing QEMU container and its assigned resources. By contrast, consider a design in which a new container is created on the same host as the target of the CPR operation. Resources must be reserved for the new container, while the old container still reserves resources until the operation completes.

Note that if we need to share RAM anyway, the resources consumption should be minimal, as mem should IMHO be the major concern (except CPU, but CPU isn't a concern in this scenario) in container world and here the shared guest mem shouldn't be accounted to the dest container. So IMHO it's about the metadata QEMU/KVM needs to do the hypervisor work, it seems to me, and that should be relatively small.

Quoted 2 lines

In that case I don't yet see it a huge improvement, if the dest container is cheap to initiate.

It's about reserving memory and CPUs, and transferring those reservations from the old instance to the new, and fiddling with the OS mechanisms that enforce reservations and limits. The devil is in the details, and with the exec model, the management agent can ignore all of that.

You don't see it as a huge improvement because you don't need to write the management code. I do!

Both modes are valid and useful - exec in container, or launch a new container. I have volunteered to implement the cpr-transfer mode for the latter, a mode I do not use. Please don't reward me by dropping the mode I care about :) Both modes can co-exist. The presence of the cpr-exec specific code in qemu will not hinder future live migration development.

Quoted 14 lines
Quoted 1 line

Avoiding over commitment requires extra work in the management layer.

So it would be nice to know what needs to be overcommitted here. I confess I don't know much on containerized VMs, so maybe the page cache can be a problem even if shared. But I hope we can spell that out. Logically IIUC memcg shouldn't account those page cache if preallocated, because memcg accounting should be done at folio allocations, at least, where the page cache should miss first (so not this case..).

Quoted 5 lines

This is one reason why a cloud provider may prefer cpr-exec. A second reason is that the container may include agents with their own connections to the outside world, and such connections remain intact if the container is reused.

How?

chardev preservation. The qemu socket chardevs to these agents are preserved, and the agent connections to the outside world do not change, so no one sees any interruption of traffic.

Quoted 19 lines
Quoted 19 lines

All memory that is mapped by the guest is preserved in place. Indeed, it must be, because it may be the target of DMA requests, which are not quiesced during cpr-exec. All such memory must be mmap'able in new QEMU. This is easy for named memory-backend objects, as long as they are mapped shared, because they are visible in the file system in both old and new QEMU. Anonymous memory must be allocated using memfd_create rather than MAP_ANON, so the memfd's can be sent to new QEMU. Pages that were locked in memory for DMA in old QEMU remain locked in new QEMU, because the descriptor of the device that locked them remains open.

cpr-exec preserves descriptors across exec by clearing the CLOEXEC flag, and by sending the unique name and value of each descriptor to new QEMU via CPR state.

For device descriptors, new QEMU reuses the descriptor when creating the device, rather than opening it again. The same holds for chardevs. For memfd descriptors, new QEMU mmap's the preserved memfd when a ramblock is created.

Quoted 6 lines
Quoted 4 lines

CPR state cannot be sent over the normal migration channel, because devices and backends are created prior to reading the channel, so this mode sends CPR state over a second migration channel that is not visible to the user. New QEMU reads the second channel prior to creating devices or backends.

Oh, maybe this is the reason that cpr-transfer will need a separate uri..

Indeed.

- Steve

#51 lore raw
Quoted 26 lines · Steven Sistare
Quoted 25 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

I really, really hope we can avoid this..

Quoted 7 lines
Quoted 3 lines

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

This is no different than live migration, requiring a management agent to launch target qemu with all the arguments use to start source QEMU. Now that same agent will send the arguments via cpr-exec-command.

It's still a bit different.

There we append "-incoming defer" only, which makes sense because we're instructing a QEMU to take an incoming stream to load. Now we append the complete qemu cmdline within the QEMU itself, that was booted with exactly the same cmdline.. :-( I would at least start to ask why we need to pass the same thing twice..

Not saying that this is no-go, but really looks unpretty to me from this part.. especially if a cleaner solution seems possible.

Quoted 23 lines
Quoted 5 lines
Quoted 2 lines

* anon-alloc (command-line option for -machine)

Igor questioned this, and I second his opinion.. We can leave the discussion there for this one.

Continued on the other thread.

Quoted 15 lines
Quoted 15 lines

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Why?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This mode has less impact on the guest than any other method of updating in place.

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

Not yet, but I will measure it.

Thanks.

Quoted 6 lines
Quoted 6 lines

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

Quoted 8 lines
Quoted 6 lines

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

In this whole process, I doubt (2) could be relatively fast, (3) I donno, maybe it could be slow but I never measured; Paolo may have good idea as I know he used to work on qboot.

We'll see, but in any case these take < 100 msec, which is a wonderfully short

I doubt whether it keeps <100ms when the VM is large. Note that I think we should cover the case where the user does 4k mapping for a large guest.

So I agree that 4k mapping over e.g. 1T without hugetlb may not be the ideal case, but the question is I suspect there're indeed serious users using QEMU like that, and if we have most exactly a parallel solution that does cover this case, it is definitely preferrable to consider the other from this POV, simply because there's nothing to lose there..

Quoted 10 lines

pause time unless your customer is doing high speed stock trading. If cpr-transfer is faster still, that's gravy, but cpr-exec is still great.

Quoted 6 lines

For (1), I also doubt in your test cases it's fast, but it may not always be fast. Consider the guest has a huge TBs of shared mem, even if the memory will be completely shared between src/dst QEMUs, the pgtable won't! It means if the TBs are mapped in PAGE_SIZE tearing down the src QEMU pgtable alone can even take time, and that will be accounted in step (1) and further in exec() request.

Quoted 5 lines

Yes, there is an O(n) effect here, but it is a fast O(n) when the memory is backed by huge pages. In UEK, we make it faster still by unmapping in parallel with multiple threads. I don't have the data handy but can share after running some experiments. Regardless, this time is negligible for small and medium size guests, which form the majority of instances in a cloud.

Possible. It's just that it sounds like a good idea to avoid having the downtime taking any pgtable tearing down into account here for the old mm, irrelevant of how much time it'll take. It's just that I suspect some use case can take fair amount of time.

So I think this is "one point less" for exec() solution, while the issue can be big or small on its own. What matters is IMHO where exec() is superior so that we'd like to pay for this. I'll try to stop saying "let's try to avoid using exec() as it sounds risky", but we still need to compare with solid pros and cons.

Quoted 16 lines
Quoted 16 lines

All these fuss will be avoided if you use a generic live migration model like cpr-transfer you proposed. That's also cleaner.

Quoted 7 lines

The pause time is much lower, because devices need not be torn down and recreated, DMA does not need to be drained and quiesced, and minimal state is copied to new QEMU. Further, there are no constraints on the guest. By contrast, cpr-reboot mode requires the guest to support S3 suspend-to-ram, and suspending plus resuming vfio devices adds multiple seconds to the guest pause time. Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

Again, I raised the question on why this would matter, as after all mgmt app will need to coop with reconnections due to the fact they'll need to support a generic live migration, in which case reconnection is a must.

Quoted 8 lines
Quoted 3 lines

So far it doesn't sound like a performance critical path, for example, to do the mgmt reconnects on the ports. So this might be an optimization that most mgmt apps may not care much?

Perhaps. I view the chardev preservation as nice to have, but not essential. It does not appear in this series, other than in docs. It's easy to implement given the CPR foundation. I suggest we continue this discussion when I post the chardev series, so we can focus on the core functionality.

It's just that it can affect our decision on choosing the way to go.

For example, do we have someone from Libvirt or any mgmt layer can help justify this point?

As I said, I thought most facilities for reconnection should be ready, but I could miss important facts in mgmt layers..

Quoted 28 lines
Quoted 25 lines
Quoted 7 lines

These benefits all derive from the core design principle of this mode, which is preserving open descriptors. This approach is very general and can be used to support a wide variety of devices that do not have hardware support for live migration, including but not limited to: vfio, chardev, vhost, vdpa, and iommufd. Some devices need new kernel software interfaces to allow a descriptor to be used in a process that did not originally open it.

Yes, I still think this is a great idea. It just can also be built on top of something else than exec().

Quoted 6 lines

In a containerized QEMU environment, cpr-exec reuses an existing QEMU container and its assigned resources. By contrast, consider a design in which a new container is created on the same host as the target of the CPR operation. Resources must be reserved for the new container, while the old container still reserves resources until the operation completes.

Note that if we need to share RAM anyway, the resources consumption should be minimal, as mem should IMHO be the major concern (except CPU, but CPU isn't a concern in this scenario) in container world and here the shared guest mem shouldn't be accounted to the dest container. So IMHO it's about the metadata QEMU/KVM needs to do the hypervisor work, it seems to me, and that should be relatively small.

Quoted 2 lines

In that case I don't yet see it a huge improvement, if the dest container is cheap to initiate.

Quoted 7 lines

It's about reserving memory and CPUs, and transferring those reservations from the old instance to the new, and fiddling with the OS mechanisms that enforce reservations and limits. The devil is in the details, and with the exec model, the management agent can ignore all of that.

You don't see it as a huge improvement because you don't need to write the management code. I do!

Heh, possibly true.

Could I ask what management code you're working on? Why that management code doesn't need to already work out these problems with reconnections (like pre-CPR ways of live upgrade)?

Quoted 6 lines

Both modes are valid and useful - exec in container, or launch a new container. I have volunteered to implement the cpr-transfer mode for the latter, a mode I do not use. Please don't reward me by dropping the mode I care about :) Both modes can co-exist. The presence of the cpr-exec specific code in qemu will not hinder future live migration development.

I'm trying to remove some of my "prejudices" on exec() :). Hopefully that proved more or less that I simply wanted to be fair on making a design decision. I don't think I have a strong opinion, but it looks to me not ideal to merge two solutions if both modes share the use case.

Or if you think both modes should service different purpose, we might consider both, but that needs to be justified - IOW, we shouldn't merge anything that will never be used.

Thanks,

Quoted 50 lines
Quoted 15 lines
Quoted 2 lines

Avoiding over commitment requires extra work in the management layer.

So it would be nice to know what needs to be overcommitted here. I confess I don't know much on containerized VMs, so maybe the page cache can be a problem even if shared. But I hope we can spell that out. Logically IIUC memcg shouldn't account those page cache if preallocated, because memcg accounting should be done at folio allocations, at least, where the page cache should miss first (so not this case..).

Quoted 5 lines

This is one reason why a cloud provider may prefer cpr-exec. A second reason is that the container may include agents with their own connections to the outside world, and such connections remain intact if the container is reused.

How?

chardev preservation. The qemu socket chardevs to these agents are preserved, and the agent connections to the outside world do not change, so no one sees any interruption of traffic.

Quoted 19 lines
Quoted 19 lines

All memory that is mapped by the guest is preserved in place. Indeed, it must be, because it may be the target of DMA requests, which are not quiesced during cpr-exec. All such memory must be mmap'able in new QEMU. This is easy for named memory-backend objects, as long as they are mapped shared, because they are visible in the file system in both old and new QEMU. Anonymous memory must be allocated using memfd_create rather than MAP_ANON, so the memfd's can be sent to new QEMU. Pages that were locked in memory for DMA in old QEMU remain locked in new QEMU, because the descriptor of the device that locked them remains open.

cpr-exec preserves descriptors across exec by clearing the CLOEXEC flag, and by sending the unique name and value of each descriptor to new QEMU via CPR state.

For device descriptors, new QEMU reuses the descriptor when creating the device, rather than opening it again. The same holds for chardevs. For memfd descriptors, new QEMU mmap's the preserved memfd when a ramblock is created.

Quoted 6 lines
Quoted 4 lines

CPR state cannot be sent over the normal migration channel, because devices and backends are created prior to reading the channel, so this mode sends CPR state over a second migration channel that is not visible to the user. New QEMU reads the second channel prior to creating devices or backends.

Oh, maybe this is the reason that cpr-transfer will need a separate uri..

Indeed.

- Steve

Signature · 1 line
Peter Xu
#52 lore raw
Quoted 35 lines · Peter Xu
Quoted 26 lines · Steven Sistare
Quoted 25 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

I really, really hope we can avoid this..

Quoted 7 lines
Quoted 3 lines

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

This is no different than live migration, requiring a management agent to launch target qemu with all the arguments use to start source QEMU. Now that same agent will send the arguments via cpr-exec-command.

Quoted 7 lines

It's still a bit different.

There we append "-incoming defer" only, which makes sense because we're instructing a QEMU to take an incoming stream to load. Now we append the complete qemu cmdline within the QEMU itself, that was booted with exactly the same cmdline.. :-( I would at least start to ask why we need to pass the same thing twice..

Sometimes one must modify the command line arguments passed to new QEMU. This interface allows for that possibility.

In an earlier patch series, I proposed a cpr-exec command that took no arguments, and reused the old QEMU argv, which was remembered in main. A reviewer pointed out how inflexible that was. See my response to Daniel yesterday for more on the value of this flexibility.

This is not a burden for the mgmt agent. It already knows the arguments because it can launch new qemu with the arguments for live migration. Passing the arguments to cpr-exec-command is trivial.

Quoted 76 lines

Not saying that this is no-go, but really looks unpretty to me from this part.. especially if a cleaner solution seems possible.

Quoted 23 lines
Quoted 5 lines
Quoted 2 lines

* anon-alloc (command-line option for -machine)

Igor questioned this, and I second his opinion.. We can leave the discussion there for this one.

Continued on the other thread.

Quoted 15 lines
Quoted 15 lines

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Why?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This mode has less impact on the guest than any other method of updating in place.

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

Not yet, but I will measure it.

Thanks.

Quoted 6 lines
Quoted 6 lines

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

Quoted 8 lines
Quoted 6 lines

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

In this whole process, I doubt (2) could be relatively fast, (3) I donno, maybe it could be slow but I never measured; Paolo may have good idea as I know he used to work on qboot.

We'll see, but in any case these take < 100 msec, which is a wonderfully short

I doubt whether it keeps <100ms when the VM is large. Note that I think we should cover the case where the user does 4k mapping for a large guest.

So I agree that 4k mapping over e.g. 1T without hugetlb may not be the ideal case, but the question is I suspect there're indeed serious users using QEMU like that, and if we have most exactly a parallel solution that does cover this case, it is definitely preferrable to consider the other from this POV, simply because there's nothing to lose there..

Quoted 10 lines

pause time unless your customer is doing high speed stock trading. If cpr-transfer is faster still, that's gravy, but cpr-exec is still great.

Quoted 6 lines

For (1), I also doubt in your test cases it's fast, but it may not always be fast. Consider the guest has a huge TBs of shared mem, even if the memory will be completely shared between src/dst QEMUs, the pgtable won't! It means if the TBs are mapped in PAGE_SIZE tearing down the src QEMU pgtable alone can even take time, and that will be accounted in step (1) and further in exec() request.

Quoted 5 lines

Yes, there is an O(n) effect here, but it is a fast O(n) when the memory is backed by huge pages. In UEK, we make it faster still by unmapping in parallel with multiple threads. I don't have the data handy but can share after running some experiments. Regardless, this time is negligible for small and medium size guests, which form the majority of instances in a cloud.

Quoted 4 lines

Possible. It's just that it sounds like a good idea to avoid having the downtime taking any pgtable tearing down into account here for the old mm, irrelevant of how much time it'll take. It's just that I suspect some use case can take fair amount of time.

Here is the guest pause time, measured as the interval from the start of the migrate command to the new QEMU guest reaching the running state. The average over 10 runs is shown, in msecs.
Huge pages are enabled.
Guest memory is memfd.
The kernel is 6.9.0 (not UEK, so no parallel unmap)
The system is old and slow: Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz

        cpr-exec    cpr-transfer
256M    180         148
16G     190         150
128G    250         159
1T      300 ?       159 ?                // extrapolated

At these scales, the difference between exec and transfer is not significant. A provider would choose one vs the other based on ease of implementation in their mgmt agent and container environment.

For small pages and large memory, cpr-exec can take multiple seconds, and the UEK parallel unmap reduces that further. But, that is the exception, not the rule. Providers strive to back huge memories with huge pages. It makes no sense to use such a valuable resource in the crappiest way possible (ie with small pages).

Quoted 31 lines

So I think this is "one point less" for exec() solution, while the issue can be big or small on its own. What matters is IMHO where exec() is superior so that we'd like to pay for this. I'll try to stop saying "let's try to avoid using exec() as it sounds risky", but we still need to compare with solid pros and cons.

Quoted 16 lines
Quoted 16 lines

All these fuss will be avoided if you use a generic live migration model like cpr-transfer you proposed. That's also cleaner.

Quoted 7 lines

The pause time is much lower, because devices need not be torn down and recreated, DMA does not need to be drained and quiesced, and minimal state is copied to new QEMU. Further, there are no constraints on the guest. By contrast, cpr-reboot mode requires the guest to support S3 suspend-to-ram, and suspending plus resuming vfio devices adds multiple seconds to the guest pause time. Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

Again, I raised the question on why this would matter, as after all mgmt app will need to coop with reconnections due to the fact they'll need to support a generic live migration, in which case reconnection is a must.

Quoted 8 lines
Quoted 3 lines

So far it doesn't sound like a performance critical path, for example, to do the mgmt reconnects on the ports. So this might be an optimization that most mgmt apps may not care much?

Perhaps. I view the chardev preservation as nice to have, but not essential. It does not appear in this series, other than in docs. It's easy to implement given the CPR foundation. I suggest we continue this discussion when I post the chardev series, so we can focus on the core functionality.

Quoted 7 lines

It's just that it can affect our decision on choosing the way to go.

For example, do we have someone from Libvirt or any mgmt layer can help justify this point?

As I said, I thought most facilities for reconnection should be ready, but I could miss important facts in mgmt layers..

I will more deeply study reconnects in the mgmt layer, run some experiments to see if it is seamless for the end user, and get back to you, but it will take some time.

Quoted 32 lines
Quoted 32 lines
Quoted 24 lines
Quoted 6 lines

These benefits all derive from the core design principle of this mode, which is preserving open descriptors. This approach is very general and can be used to support a wide variety of devices that do not have hardware support for live migration, including but not limited to: vfio, chardev, vhost, vdpa, and iommufd. Some devices need new kernel software interfaces to allow a descriptor to be used in a process that did not originally open it.

Yes, I still think this is a great idea. It just can also be built on top of something else than exec().

Quoted 6 lines

In a containerized QEMU environment, cpr-exec reuses an existing QEMU container and its assigned resources. By contrast, consider a design in which a new container is created on the same host as the target of the CPR operation. Resources must be reserved for the new container, while the old container still reserves resources until the operation completes.

Note that if we need to share RAM anyway, the resources consumption should be minimal, as mem should IMHO be the major concern (except CPU, but CPU isn't a concern in this scenario) in container world and here the shared guest mem shouldn't be accounted to the dest container. So IMHO it's about the metadata QEMU/KVM needs to do the hypervisor work, it seems to me, and that should be relatively small.

Quoted 2 lines

In that case I don't yet see it a huge improvement, if the dest container is cheap to initiate.

It's about reserving memory and CPUs, and transferring those reservations from the old instance to the new, and fiddling with the OS mechanisms that enforce reservations and limits. The devil is in the details, and with the exec model, the management agent can ignore all of that.

Quoted 8 lines
Quoted 2 lines

You don't see it as a huge improvement because you don't need to write the management code. I do!

Heh, possibly true.

Could I ask what management code you're working on? Why that management code doesn't need to already work out these problems with reconnections (like pre-CPR ways of live upgrade)?

OCI - Oracle Cloud Infrastructure.
Mgmt needs to manage reconnections for live migration, and perhaps I could leverage that code for live update, but happily I did not need to. Regardless, reconnection is the lesser issue. The bigger issue is resource management and the container environment. But I cannot justify that statement in detail without actually trying to implement cpr-transfer in OCI.

Quoted 6 lines
Quoted 5 lines

Both modes are valid and useful - exec in container, or launch a new container. I have volunteered to implement the cpr-transfer mode for the latter, a mode I do not use. Please don't reward me by dropping the mode I care about :) Both modes can co-exist. The presence of the cpr-exec specific code in qemu will not hinder future live migration development.

Quoted 8 lines

I'm trying to remove some of my "prejudices" on exec() :). Hopefully that proved more or less that I simply wanted to be fair on making a design decision. I don't think I have a strong opinion, but it looks to me not ideal to merge two solutions if both modes share the use case.

Or if you think both modes should service different purpose, we might consider both, but that needs to be justified - IOW, we shouldn't merge anything that will never be used.

The use case is the same for both modes, but they are simply different transport methods for moving descriptors from old QEMU to new. The developer of the mgmt agent should be allowed to choose.

- Steve

Quoted 50 lines
Quoted 49 lines
Quoted 14 lines
Quoted 1 line

Avoiding over commitment requires extra work in the management layer.

So it would be nice to know what needs to be overcommitted here. I confess I don't know much on containerized VMs, so maybe the page cache can be a problem even if shared. But I hope we can spell that out. Logically IIUC memcg shouldn't account those page cache if preallocated, because memcg accounting should be done at folio allocations, at least, where the page cache should miss first (so not this case..).

Quoted 5 lines

This is one reason why a cloud provider may prefer cpr-exec. A second reason is that the container may include agents with their own connections to the outside world, and such connections remain intact if the container is reused.

How?

chardev preservation. The qemu socket chardevs to these agents are preserved, and the agent connections to the outside world do not change, so no one sees any interruption of traffic.

Quoted 19 lines
Quoted 19 lines

All memory that is mapped by the guest is preserved in place. Indeed, it must be, because it may be the target of DMA requests, which are not quiesced during cpr-exec. All such memory must be mmap'able in new QEMU. This is easy for named memory-backend objects, as long as they are mapped shared, because they are visible in the file system in both old and new QEMU. Anonymous memory must be allocated using memfd_create rather than MAP_ANON, so the memfd's can be sent to new QEMU. Pages that were locked in memory for DMA in old QEMU remain locked in new QEMU, because the descriptor of the device that locked them remains open.

cpr-exec preserves descriptors across exec by clearing the CLOEXEC flag, and by sending the unique name and value of each descriptor to new QEMU via CPR state.

For device descriptors, new QEMU reuses the descriptor when creating the device, rather than opening it again. The same holds for chardevs. For memfd descriptors, new QEMU mmap's the preserved memfd when a ramblock is created.

Quoted 6 lines
Quoted 4 lines

CPR state cannot be sent over the normal migration channel, because devices and backends are created prior to reading the channel, so this mode sends CPR state over a second migration channel that is not visible to the user. New QEMU reads the second channel prior to creating devices or backends.

Oh, maybe this is the reason that cpr-transfer will need a separate uri..

Indeed.

- Steve

#53 lore raw
Quoted 47 lines · Steven Sistare
Quoted 35 lines · Peter Xu
Quoted 26 lines · Steven Sistare
Quoted 25 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

I really, really hope we can avoid this..

Quoted 7 lines
Quoted 3 lines

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

This is no different than live migration, requiring a management agent to launch target qemu with all the arguments use to start source QEMU. Now that same agent will send the arguments via cpr-exec-command.

Quoted 7 lines

It's still a bit different.

There we append "-incoming defer" only, which makes sense because we're instructing a QEMU to take an incoming stream to load. Now we append the complete qemu cmdline within the QEMU itself, that was booted with exactly the same cmdline.. :-( I would at least start to ask why we need to pass the same thing twice..

Sometimes one must modify the command line arguments passed to new QEMU. This interface allows for that possibility.

Quoted 8 lines

In an earlier patch series, I proposed a cpr-exec command that took no arguments, and reused the old QEMU argv, which was remembered in main. A reviewer pointed out how inflexible that was. See my response to Daniel yesterday for more on the value of this flexibility.

This is not a burden for the mgmt agent. It already knows the arguments because it can launch new qemu with the arguments for live migration. Passing the arguments to cpr-exec-command is trivial.

Right, trivial as-is. To me it's not a major blocker yet so far, but it's still about being hackish, and I have this unpleasant feeling that we're digging holes for our future.

Quoted 96 lines
Quoted 77 lines

Not saying that this is no-go, but really looks unpretty to me from this part.. especially if a cleaner solution seems possible.

Quoted 23 lines
Quoted 5 lines
Quoted 2 lines

* anon-alloc (command-line option for -machine)

Igor questioned this, and I second his opinion.. We can leave the discussion there for this one.

Continued on the other thread.

Quoted 15 lines
Quoted 15 lines

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Why?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This mode has less impact on the guest than any other method of updating in place.

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

Not yet, but I will measure it.

Thanks.

Quoted 6 lines
Quoted 6 lines

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

Quoted 8 lines
Quoted 6 lines

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

In this whole process, I doubt (2) could be relatively fast, (3) I donno, maybe it could be slow but I never measured; Paolo may have good idea as I know he used to work on qboot.

We'll see, but in any case these take < 100 msec, which is a wonderfully short

I doubt whether it keeps <100ms when the VM is large. Note that I think we should cover the case where the user does 4k mapping for a large guest.

So I agree that 4k mapping over e.g. 1T without hugetlb may not be the ideal case, but the question is I suspect there're indeed serious users using QEMU like that, and if we have most exactly a parallel solution that does cover this case, it is definitely preferrable to consider the other from this POV, simply because there's nothing to lose there..

Quoted 10 lines

pause time unless your customer is doing high speed stock trading. If cpr-transfer is faster still, that's gravy, but cpr-exec is still great.

Quoted 6 lines

For (1), I also doubt in your test cases it's fast, but it may not always be fast. Consider the guest has a huge TBs of shared mem, even if the memory will be completely shared between src/dst QEMUs, the pgtable won't! It means if the TBs are mapped in PAGE_SIZE tearing down the src QEMU pgtable alone can even take time, and that will be accounted in step (1) and further in exec() request.

Quoted 5 lines

Yes, there is an O(n) effect here, but it is a fast O(n) when the memory is backed by huge pages. In UEK, we make it faster still by unmapping in parallel with multiple threads. I don't have the data handy but can share after running some experiments. Regardless, this time is negligible for small and medium size guests, which form the majority of instances in a cloud.

Quoted 4 lines

Possible. It's just that it sounds like a good idea to avoid having the downtime taking any pgtable tearing down into account here for the old mm, irrelevant of how much time it'll take. It's just that I suspect some use case can take fair amount of time.

Here is the guest pause time, measured as the interval from the start of the migrate command to the new QEMU guest reaching the running state. The average over 10 runs is shown, in msecs.
Huge pages are enabled.
Guest memory is memfd.
The kernel is 6.9.0 (not UEK, so no parallel unmap)
The system is old and slow: Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz

        cpr-exec    cpr-transfer
256M    180         148
16G     190         150
128G    250         159
1T      300 ?       159 ?                // extrapolated
Quoted 3 lines

At these scales, the difference between exec and transfer is not significant. A provider would choose one vs the other based on ease of implementation in their mgmt agent and container environment.

Thanks. Are these maximum huge page the memsize can take (either 2M or 1G), or is this only about 2M?

Quoted 6 lines

For small pages and large memory, cpr-exec can take multiple seconds, and the UEK parallel unmap reduces that further. But, that is the exception, not the rule. Providers strive to back huge memories with huge pages. It makes no sense to use such a valuable resource in the crappiest way possible (ie with small pages).

I can't say nobody uses small pages when memory is large. The thing is hugetlb loses features comparing to small pages; or say, merely all the memory features Linux provides. So I won't be surprised if someone tells me there's TB level VMs that is using small page on purpose for any of those features (swap, ksm, etc.). I had vague memory there was customers using such setup in the past, without remembering the reasons.

I do wish a design that will perform well even there, so it works for all cases we can think of so far. Not to mention cpr-transfer seems to outperforms everywhere too. Unless the "management layer benefits" are so strong.. it seems to me we have a clear choice. I understand it may affect your plan, but let's be fair, or.. is it not the case?

Quoted 84 lines
Quoted 32 lines

So I think this is "one point less" for exec() solution, while the issue can be big or small on its own. What matters is IMHO where exec() is superior so that we'd like to pay for this. I'll try to stop saying "let's try to avoid using exec() as it sounds risky", but we still need to compare with solid pros and cons.

Quoted 16 lines
Quoted 16 lines

All these fuss will be avoided if you use a generic live migration model like cpr-transfer you proposed. That's also cleaner.

Quoted 7 lines

The pause time is much lower, because devices need not be torn down and recreated, DMA does not need to be drained and quiesced, and minimal state is copied to new QEMU. Further, there are no constraints on the guest. By contrast, cpr-reboot mode requires the guest to support S3 suspend-to-ram, and suspending plus resuming vfio devices adds multiple seconds to the guest pause time. Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

Again, I raised the question on why this would matter, as after all mgmt app will need to coop with reconnections due to the fact they'll need to support a generic live migration, in which case reconnection is a must.

Quoted 8 lines
Quoted 3 lines

So far it doesn't sound like a performance critical path, for example, to do the mgmt reconnects on the ports. So this might be an optimization that most mgmt apps may not care much?

Perhaps. I view the chardev preservation as nice to have, but not essential. It does not appear in this series, other than in docs. It's easy to implement given the CPR foundation. I suggest we continue this discussion when I post the chardev series, so we can focus on the core functionality.

Quoted 7 lines

It's just that it can affect our decision on choosing the way to go.

For example, do we have someone from Libvirt or any mgmt layer can help justify this point?

As I said, I thought most facilities for reconnection should be ready, but I could miss important facts in mgmt layers..

I will more deeply study reconnects in the mgmt layer, run some experiments to see if it is seamless for the end user, and get back to you, but it will take some time.

Quoted 32 lines
Quoted 32 lines
Quoted 24 lines
Quoted 6 lines

These benefits all derive from the core design principle of this mode, which is preserving open descriptors. This approach is very general and can be used to support a wide variety of devices that do not have hardware support for live migration, including but not limited to: vfio, chardev, vhost, vdpa, and iommufd. Some devices need new kernel software interfaces to allow a descriptor to be used in a process that did not originally open it.

Yes, I still think this is a great idea. It just can also be built on top of something else than exec().

Quoted 6 lines

In a containerized QEMU environment, cpr-exec reuses an existing QEMU container and its assigned resources. By contrast, consider a design in which a new container is created on the same host as the target of the CPR operation. Resources must be reserved for the new container, while the old container still reserves resources until the operation completes.

Note that if we need to share RAM anyway, the resources consumption should be minimal, as mem should IMHO be the major concern (except CPU, but CPU isn't a concern in this scenario) in container world and here the shared guest mem shouldn't be accounted to the dest container. So IMHO it's about the metadata QEMU/KVM needs to do the hypervisor work, it seems to me, and that should be relatively small.

Quoted 2 lines

In that case I don't yet see it a huge improvement, if the dest container is cheap to initiate.

It's about reserving memory and CPUs, and transferring those reservations from the old instance to the new, and fiddling with the OS mechanisms that enforce reservations and limits. The devil is in the details, and with the exec model, the management agent can ignore all of that.

Quoted 8 lines
Quoted 2 lines

You don't see it as a huge improvement because you don't need to write the management code. I do!

Heh, possibly true.

Could I ask what management code you're working on? Why that management code doesn't need to already work out these problems with reconnections (like pre-CPR ways of live upgrade)?

Quoted 6 lines

OCI - Oracle Cloud Infrastructure.
Mgmt needs to manage reconnections for live migration, and perhaps I could leverage that code for live update, but happily I did not need to. Regardless, reconnection is the lesser issue. The bigger issue is resource management and the container environment. But I cannot justify that statement in detail without actually trying to implement cpr-transfer in OCI.

I see. Is OCI open source somewhere?

If it's close-sourced, maybe it'll be helpful to see how the exec() design could benefit other open source mgmt applications.

Quoted 12 lines
Quoted 12 lines
Quoted 6 lines

Both modes are valid and useful - exec in container, or launch a new container. I have volunteered to implement the cpr-transfer mode for the latter, a mode I do not use. Please don't reward me by dropping the mode I care about :) Both modes can co-exist. The presence of the cpr-exec specific code in qemu will not hinder future live migration development.

I'm trying to remove some of my "prejudices" on exec() :). Hopefully that proved more or less that I simply wanted to be fair on making a design decision. I don't think I have a strong opinion, but it looks to me not ideal to merge two solutions if both modes share the use case.

Quoted 7 lines
Quoted 3 lines

Or if you think both modes should service different purpose, we might consider both, but that needs to be justified - IOW, we shouldn't merge anything that will never be used.

The use case is the same for both modes, but they are simply different transport methods for moving descriptors from old QEMU to new. The developer of the mgmt agent should be allowed to choose.

It's out of my capability to review the mgmt impact on this one. This is all based on the idea that I think most mgmt apps supports reconnections pretty well. If that's the case, I'd definitely go for the transfer mode. I'm not sure whether there's anyone from mgmt layer would like to share some opinion; Dan could be the most suitable in the loop already.

Thanks,

Signature · 1 line
Peter Xu
#54 lore raw

Re: [PATCH V2 00/11] Live update: cpr-exec (reconnections)

Quoted 4 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 2 lines · Peter Xu
Quoted 1 line · Steven Sistare

On 7/18/2024 11:56 AM, Peter Xu wrote:

[...]

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Lastly, there is no loss of connectivity to the guest, because chardev descriptors remain open and connected.

Again, I raised the question on why this would matter, as after all mgmt app will need to coop with reconnections due to the fact they'll need to support a generic live migration, in which case reconnection is a must.

Quoted 8 lines
Quoted 3 lines

So far it doesn't sound like a performance critical path, for example, to do the mgmt reconnects on the ports. So this might be an optimization that most mgmt apps may not care much?

Perhaps. I view the chardev preservation as nice to have, but not essential. It does not appear in this series, other than in docs. It's easy to implement given the CPR foundation. I suggest we continue this discussion when I post the chardev series, so we can focus on the core functionality.

It's just that it can affect our decision on choosing the way to go.

For example, do we have someone from Libvirt or any mgmt layer can help justify this point?

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

As I said, I thought most facilities for reconnection should be ready, but I could miss important facts in mgmt layers..

I will more deeply study reconnects in the mgmt layer, run some experiments to see if it is seamless for the end user, and get back to you, but it will take some time.

See below.

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 3 lines

Could I ask what management code you're working on? Why that management code doesn't need to already work out these problems with reconnections (like pre-CPR ways of live upgrade)?

OCI - Oracle Cloud Infrastructure.
Mgmt needs to manage reconnections for live migration, and perhaps I could leverage that code for live update, but happily I did not need to. Regardless, reconnection is the lesser issue. The bigger issue is resource management and the container environment. But I cannot justify that statement in detail without actually trying to implement cpr-transfer in OCI.

[...]

Quoted 7 lines
Quoted 3 lines

The use case is the same for both modes, but they are simply different transport methods for moving descriptors from old QEMU to new. The developer of the mgmt agent should be allowed to choose.

It's out of my capability to review the mgmt impact on this one. This is all based on the idea that I think most mgmt apps supports reconnections pretty well. If that's the case, I'd definitely go for the transfer mode.

Closing the loop here on reconnections --

The managers I studied do not reconnect QEMU chardevs such as the guest console after live migration. In all cases, the old console goes dark and the user must manually reconnect to the console on the target.

OCI does not auto reconnect. libvirt does not, one must reconnect through libvirtd on the target. kubevirt does not AFAICT; one must reconnect on the target using virtctl console.

Thus chardev preservation does offer an improved user experience in this regard. chardevs can be preserved using either cpr-exec or cpr-transfer. But, if QEMU runs in a containerized environment that has agents that proxy connections between QEMU chardevs and the outside world, then only cpr-exec (which preserves the existing container) preserves connections end-to-end. OCI has such agents. I believe kubevirt does also.

- Steve

#55 lore raw
In reply to Peter Xu
Quoted 56 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

I really, really hope we can avoid this..

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

Quoted 1 line

* anon-alloc (command-line option for -machine)

Igor questioned this, and I second his opinion.. We can leave the discussion there for this one.

Quoted 14 lines

The user sets the mode parameter before invoking the migrate command. In this mode, the user issues the migrate command to old QEMU, which stops the VM and saves state to the migration channels. Old QEMU then exec's new QEMU, replacing the original process while retaining its PID. The user specifies the command to exec new QEMU in the migration parameter cpr-exec-command. The command must pass all old QEMU arguments to new QEMU, plus the -incoming option. Execution resumes in new QEMU.

Memory-backend objects must have the share=on attribute, but memory-backend-epc is not supported. The VM must be started with the '-machine anon-alloc=memfd' option, which allows anonymous memory to be transferred in place to the new process.

Quoted 4 lines

Why?

This mode has less impact on the guest than any other method of updating in place.

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

Quoted 7 lines

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

I'll note (not sure if already discussed) that with the "async-teardown" option we have a way to move the MM teardown to a separate process, such that it will happen asynchronously.

Signature · 3 lines
Cheers,

David / dhildenb
#56 lore raw
Quoted 8 lines · David Hildenbrand
Quoted 8 lines

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

Quoted 6 lines
Quoted 2 lines

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

I'll note (not sure if already discussed) that with the "async-teardown" option we have a way to move the MM teardown to a separate process, such that it will happen asynchronously.

I just had a look, maybe it won't trivially work, as it relies on QEMU process to quit first..

async_teardown_fn():

    if (the_ppid == getppid()) {
        pause();
    }

While if we stick with exec(), then PID shouldn't change, so the teardown process can hold the mm and pause until the VM is destroyed..

Thanks,

Signature · 1 line
Peter Xu
#57 lore raw
Quoted 19 lines · Peter Xu
Quoted 8 lines · David Hildenbrand
Quoted 8 lines

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

Quoted 6 lines
Quoted 2 lines

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

I'll note (not sure if already discussed) that with the "async-teardown" option we have a way to move the MM teardown to a separate process, such that it will happen asynchronously.

I just had a look, maybe it won't trivially work, as it relies on QEMU process to quit first..

Quoted 7 lines

async_teardown_fn():

     if (the_ppid == getppid()) {
         pause();
     }

While if we stick with exec(), then PID shouldn't change, so the teardown process can hold the mm and pause until the VM is destroyed..

Right, the mechanism would have to be extended to realize that exec() happened. Notifying the child before exec() would be undesired, so it would have to happen after exec() from the changed parent.

Sounds doable, but certainly doesn't come for free!

Signature · 3 lines
Cheers,

David / dhildenb
#58 lore raw
Quoted 25 lines · David Hildenbrand
Quoted 24 lines · Peter Xu
Quoted 8 lines · David Hildenbrand
Quoted 8 lines

So I wonder whether there's comparison between exec() and transfer mode that you recently proposed.

I'm asking because exec() (besides all the rest of things that I dislike on it in this approach..) should be simply slower, logically, due to the serialized operation to (1) tearing down the old mm, (2) reload the new ELF, then (3) runs through the QEMU init process.

Quoted 6 lines
Quoted 2 lines

If with a generic migration solution, the dest QEMU can start running (2+3) concurrently without even need to run (1).

I'll note (not sure if already discussed) that with the "async-teardown" option we have a way to move the MM teardown to a separate process, such that it will happen asynchronously.

I just had a look, maybe it won't trivially work, as it relies on QEMU process to quit first..

async_teardown_fn():

      if (the_ppid == getppid()) {
          pause();
      }
Quoted 8 lines
Quoted 2 lines

While if we stick with exec(), then PID shouldn't change, so the teardown process can hold the mm and pause until the VM is destroyed..

Right, the mechanism would have to be extended to realize that exec() happened. Notifying the child before exec() would be undesired, so it would have to happen after exec() from the changed parent.

Sounds doable, but certainly doesn't come for free!

I did not look deeply into this, but possibly using a pipe between both processes created with O_CLOEXEC might do. Anyhow, something to look into if really required :)

Signature · 3 lines
Cheers,

David / dhildenb
#59 lore raw
In reply to Peter Xu
Quoted 23 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

Quoted 5 lines

I really, really hope we can avoid this..

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

A further issue I have is that it presumes the QEMU configuration is fully captured by the command line. We have a long term design goal in QEMU to get away from specifying configuration on the command line, and move entirely to configuring QEMU via a series of QMP commands.

This proposed command is introducing the concept of command line argv as a formal part of the QEMU API and IMHO that is undesirable. Even today we have backend configuration steps only done via QMP, and I'm wondering how it would fit in with how mgmt apps currently doing live migration.

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#60 lore raw
Quoted 30 lines · Daniel P. Berrangé
Quoted 23 lines · Peter Xu

Steve,

Quoted 1 line · Steve Sistare

What?

Thanks for trying out with the cpr-transfer series. I saw that that series missed most of the cc list here, so I'm attaching the link here:

https://lore.kernel.org/r/[email protected]

I think most of my previous questions for exec() solution still are there, I'll try to summarize them all in this reply as much as I can.

Quoted 9 lines

This patch series adds the live migration cpr-exec mode, which allows the user to update QEMU with minimal guest pause time, by preserving guest RAM in place, albeit with new virtual addresses in new QEMU, and by preserving device file descriptors.

The new user-visible interfaces are:
* cpr-exec (MigMode migration parameter)
* cpr-exec-command (migration parameter)

Quoted 5 lines

I really, really hope we can avoid this..

It's super cumbersome to pass in a qemu cmdline in a qemu migration parameter.. if we can do that with generic live migration ways, I hope we stick with the clean approach.

Quoted 8 lines

A further issue I have is that it presumes the QEMU configuration is fully captured by the command line. We have a long term design goal in QEMU to get away from specifying configuration on the command line, and move entirely to configuring QEMU via a series of QMP commands.

This proposed command is introducing the concept of command line argv as a formal part of the QEMU API and IMHO that is undesirable.

Actually cpr-exec-command does not presume anything; it is an arbitrary command with arbitrary arguments. If in the future QEMU takes no command-line arguments, then mgmt will pass a simple launcher command as cpr-exec-command, and the launcher will issue QMP commands. Or the launcher will send a message to another mgmt agent to do so. It is very flexible. Regardless, the API definition of cpr-exec-command will not change.

As another example, in our cloud environment, when the mgmt agent starts QEMU, it saves the QEMU args in a file. My cpr-exec-command is just "/bin/qemu-exec" with a few simple arguments. That command reads QEMU args from the file and exec's new QEMU.

Quoted 4 lines

Even
today we have backend configuration steps only done via QMP, and I'm wondering how it would fit in with how mgmt apps currently doing live migration.

Sure, and that still works. For live migration, mgmt starts new QEMU with its static arguments plus -S plus -incoming, then mgmt detects QEMU has reached the prelaunch state, then it issues QMP commands. For live update, cpr-exec-command has the static arguments plus -S, then mgmt detects QEMU has reached the prelaunch state, then it issues QMP commands.

Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

Indeed, and "files" includes unix domain sockets. Network ports also conflict. cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

- Steve

#61 lore raw
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

Quoted 4 lines

Indeed, and "files" includes unix domain sockets. Network ports also conflict. cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Thanks,

Signature · 1 line
Peter Xu
#62 lore raw
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;
Quoted 1 line
Quoted 1 line

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

- Steve

#63 lore raw
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 21 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Quoted 5 lines

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

I suppose that's also why I'm slightly confused on how cpr-exec can provide benefit for mgmt layers yet so far with these open projects. It might affect Oracle's mgmt layers, but again I'm curious why Oracle does not support these, because if that should support normal live migration, I thought it should be needed to support changed ports on host etc..

Signature · 1 line
Peter Xu
#64 lore raw
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#65 lore raw
Quoted 54 lines · Daniel P. Berrangé
Quoted 53 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

   blk_attach_dev()
     if (runstate_check(RUN_STATE_INMIGRATE))
       blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 26 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Quoted 8 lines
Quoted 3 lines

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Thank you! This is what I suspected but could not prove due to my lack of experience with libvirt.

- Steve

#66 lore raw
In reply to Daniel P. Berrangé
Quoted 54 lines · Daniel P. Berrangé
Quoted 53 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 26 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Quoted 8 lines
Quoted 3 lines

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

Signature · 1 line
Peter Xu
#67 lore raw
Quoted 59 lines · Peter Xu
Quoted 58 lines · Daniel P. Berrangé
Quoted 56 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 21 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Quoted 5 lines

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Quoted 7 lines
Quoted 4 lines

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

Quoted 4 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#68 lore raw
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 9 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

Quoted 8 lines

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

Thanks,

Signature · 1 line
Peter Xu
#69 lore raw
Quoted 95 lines · Peter Xu
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

  blk_attach_dev()
    if (runstate_check(RUN_STATE_INMIGRATE))
      blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 15 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Quoted 8 lines
Quoted 2 lines

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

Since nothing is merged, I'd consider whatever might have been presented wrt TDX migration to all be open for potential re-design. With SNP there is the SVSM paravisor which runs inside the guest to provide services, and IIUC its intended to address migration service needs.

There's a push to support SVSM with TDX too, in order to enable vTPM support. With that it might make sense to explore whether SVSM can service migration for TDX too, instead of having a separate parallel VM on the host. IMHO its highly desirable to have a common architecture for CVM migration from QEMU's POV, and from a libvirt POV I'd like to avoid having extra host VMs too.

Quoted 7 lines

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

Or we have cpr-transfer, but libvirt has to more games to solve the clashing resources problem, by making much more use of FD passing, and/or by changnig path conventions, or a mix of both.

What might make this viable is that IIUC, CPR only permits a subset of backends to be used, so libvirt doesn't have to solve clashing resources for /everything/, just parts that are supported by CPR.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#70 lore raw
In reply to Peter Xu
Quoted 104 lines · Peter Xu
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

   blk_attach_dev()
     if (runstate_check(RUN_STATE_INMIGRATE))
       blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 9 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

Quoted 8 lines

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

Quoted 7 lines

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

libvirt starts qemu with the -sandbox spawn=deny option which blocks fork, exec, and change namespace operations. I have a patch in my workspace to be submitted later called "seccomp: fine-grained control of fork, exec, and namespace" that allows libvirt to block fork and namespace but allow exec.

- Steve

#71 lore raw
Quoted 113 lines · Steven Sistare
Quoted 104 lines · Peter Xu
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

   blk_attach_dev()
     if (runstate_check(RUN_STATE_INMIGRATE))
       blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 9 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

Quoted 8 lines

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

Quoted 7 lines

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

Quoted 4 lines

libvirt starts qemu with the -sandbox spawn=deny option which blocks fork, exec, and change namespace operations. I have a patch in my workspace to be submitted later called "seccomp: fine-grained control of fork, exec, and namespace" that allows libvirt to block fork and namespace but allow exec.

The question is whether that would be accepted, and it also gives me the feeling that even if it's accepted, it might limit the use cases that cpr can apply to.

What I read so far from Dan is that cpr-transfer seems to be also preferred from Libvirt POV:

https://lore.kernel.org/r/[email protected]

Did I read it right?

Thanks,

Signature · 1 line
Peter Xu
#72 lore raw
Quoted 114 lines · Peter Xu
Quoted 113 lines · Steven Sistare
Quoted 111 lines · Peter Xu
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

    blk_attach_dev()
      if (runstate_check(RUN_STATE_INMIGRATE))
        blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 9 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

Quoted 8 lines

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

Quoted 8 lines
Quoted 4 lines

libvirt starts qemu with the -sandbox spawn=deny option which blocks fork, exec, and change namespace operations. I have a patch in my workspace to be submitted later called "seccomp: fine-grained control of fork, exec, and namespace" that allows libvirt to block fork and namespace but allow exec.

The question is whether that would be accepted, and it also gives me the feeling that even if it's accepted, it might limit the use cases that cpr can apply to.

This is more acceptable for libvirt running in a container (such as under kubevirt) with a limited set of binaries in /bin that could be exec'd. In that case allowing exec is more reasonable.

Quoted 6 lines

What I read so far from Dan is that cpr-transfer seems to be also preferred from Libvirt POV:

https://lore.kernel.org/r/[email protected]

Did I read it right?

I read that as: cpr-transfer is a viable option for libvirt. I don't hear him excluding the possibility of cpr-exec.

I agree that "Dan the libvirt expert prefers cpr-transfer" is a good reason to provide cpr-transfer. Which I will do.

So does "Steve the OCI expert prefers cpr-exec" carry equal weight, for also providing cpr-exec?

We are at an impasse on this series. To make forward progress, I am willing to reorder the patches, and re-submit cpr-transfer as the first mode, so we can review and pull that. I will submit cpr-exec as a follow on and we can resume our arguments then.

- Steve

#73 lore raw
Quoted 131 lines · Steven Sistare
Quoted 114 lines · Peter Xu
Quoted 113 lines · Steven Sistare
Quoted 111 lines · Peter Xu
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

    blk_attach_dev()
      if (runstate_check(RUN_STATE_INMIGRATE))
        blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 9 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

Quoted 8 lines

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

Quoted 8 lines
Quoted 4 lines

libvirt starts qemu with the -sandbox spawn=deny option which blocks fork, exec, and change namespace operations. I have a patch in my workspace to be submitted later called "seccomp: fine-grained control of fork, exec, and namespace" that allows libvirt to block fork and namespace but allow exec.

The question is whether that would be accepted, and it also gives me the feeling that even if it's accepted, it might limit the use cases that cpr can apply to.

This is more acceptable for libvirt running in a container (such as under kubevirt) with a limited set of binaries in /bin that could be exec'd. In that case allowing exec is more reasonable.

Quoted 3 lines

What I read so far from Dan is that cpr-transfer seems to be also preferred from Libvirt POV:

Quoted 6 lines
Quoted 3 lines

https://lore.kernel.org/r/[email protected]

Did I read it right?

I read that as: cpr-transfer is a viable option for libvirt. I don't hear him excluding the possibility of cpr-exec.

I preferred not having two solution because if they work the same problem out, then it potentially means one of them might be leftover at some point, unless they suite different needs. But I don't feel strongly, especially if cpr-exec is light if cpr-transfer is there.

Quoted 6 lines

I agree that "Dan the libvirt expert prefers cpr-transfer" is a good reason to provide cpr-transfer. Which I will do.

So does "Steve the OCI expert prefers cpr-exec" carry equal weight, for also providing cpr-exec?

As an open source project, Libvirt using it means the feature can be actively used and tested. When e.g. there's a new feature replacing CPR we know when we can obsolete the old CPR, no matter -exec or -transfer.

Close sourced projects can also be great itself but naturally are less important in open source communities IMHO due to not accessible to anyone in the community. E.g., we never know when an close sourced project abandoned a feature, then QEMU can carry over that feature forever without knowing who's using it.

It's the same as when Linux doesn't maintain kabi on out-of-tree drivers to me. It's just that here the open source virt stack is a huge project and QEMU plays its role within.

Quoted 5 lines

We are at an impasse on this series. To make forward progress, I am willing to reorder the patches, and re-submit cpr-transfer as the first mode, so we can review and pull that. I will submit cpr-exec as a follow on and we can resume our arguments then.

Yes this could be better to justify how small change cpr-exec would need on top of cpr-transfer, but I'd still wait for some comments from Dan or others in case they'll chime in, just to avoid sinking your time with rebases.

Thanks,

Signature · 1 line
Peter Xu
#74 lore raw
Quoted 30 lines · Peter Xu
Quoted 6 lines · Steven Sistare
Quoted 5 lines · Peter Xu
Quoted 1 line · Steven Sistare

On 8/16/2024 12:17 PM, Peter Xu wrote:

What I read so far from Dan is that cpr-transfer seems to be also preferred from Libvirt POV:

Quoted 6 lines
Quoted 3 lines

https://lore.kernel.org/r/[email protected]

Did I read it right?

I read that as: cpr-transfer is a viable option for libvirt. I don't hear him excluding the possibility of cpr-exec.

I preferred not having two solution because if they work the same problem out, then it potentially means one of them might be leftover at some point, unless they suite different needs. But I don't feel strongly, especially if cpr-exec is light if cpr-transfer is there.

Quoted 6 lines

I agree that "Dan the libvirt expert prefers cpr-transfer" is a good reason to provide cpr-transfer. Which I will do.

So does "Steve the OCI expert prefers cpr-exec" carry equal weight, for also providing cpr-exec?

As an open source project, Libvirt using it means the feature can be actively used and tested. When e.g. there's a new feature replacing CPR we know when we can obsolete the old CPR, no matter -exec or -transfer.

Quoted 5 lines

Close sourced projects can also be great itself but naturally are less important in open source communities IMHO due to not accessible to anyone in the community. E.g., we never know when an close sourced project abandoned a feature, then QEMU can carry over that feature forever without knowing who's using it.

In terms of closed source projects, effectively they don't exist from a QEMU maintainer's POV. Our deprecation & removal policy is designed so that we don't need to think about who is using stuff.

When QEMU deprecates something, any users (whether open source or closed source) have 2 releases in which to notice this, and make a request that we cancel the deprecation, or change their code.

Libvirt is special in the sense that we'll CC libvirt mailing list on changes to the deprecated.rst file, and we'll often not propose deprecations in the first place if we know libvirt is using it, since we can ask libvirt quite easily & libvirt people pay attention to QEMU.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#75 lore raw
In reply to Steven Sistare
Quoted 8 lines · Steven Sistare
Quoted 7 lines · Peter Xu
Quoted 5 lines · Steven Sistare

libvirt starts qemu with the -sandbox spawn=deny option which blocks fork, exec, and change namespace operations. I have a patch in my workspace to be submitted later called "seccomp: fine-grained control of fork, exec, and namespace" that allows libvirt to block fork and namespace but allow exec.

Quoted 7 lines
Quoted 3 lines

The question is whether that would be accepted, and it also gives me the feeling that even if it's accepted, it might limit the use cases that cpr can apply to.

This is more acceptable for libvirt running in a container (such as under kubevirt) with a limited set of binaries in /bin that could be exec'd. In that case allowing exec is more reasonable.

Running inside a container does protect the host to a significant degree. I'd say it is still important, however, to protect the control plane (libvirt's daemons & kubevirt's agent) from the QEMU process being managed, and in that case it still looks pretty compelling to deny exec.

Quoted 5 lines
Quoted 5 lines

What I read so far from Dan is that cpr-transfer seems to be also preferred from Libvirt POV:

https://lore.kernel.org/r/[email protected]

Quoted 7 lines
Quoted 1 line

Did I read it right?

I read that as: cpr-transfer is a viable option for libvirt. I don't hear him excluding the possibility of cpr-exec.

I agree that "Dan the libvirt expert prefers cpr-transfer" is a good reason to provide cpr-transfer. Which I will do.

Both approaches have significant challenges for integration, but my general preference is towards a solution that doesn't require undermining our security protections.

When starting a VM we have no knowledge of whether a user may want to use CPR at a later date. We're not going to disable the seccomp sandbox by default, so that means cpr-exec would not be viable in a default VM deployment.

Admins could choose to modify /etc/libvirt/qemu.conf to turn off seccomp, but I'm very much not in favour of introducing a feature that requires them todo this. It would be a first in libvirt, as everything else we support is possible to use with seccomp enabled. The seccomp opt-out is essentially just there as an emergency escape hatch, not as something we want used in production.

Quoted 4 lines

We are at an impasse on this series. To make forward progress, I am willing to reorder the patches, and re-submit cpr-transfer as the first mode, so we can review and pull that. I will submit cpr-exec as a follow on and we can resume our arguments then.

Considering the end result, are there CPR usage scenarios that are possible with cpr-exec, that can't be achieved with cpr-transfer ?

Supporting two ways to doing the same thing is increasing the maint burden for QEMU maintainers, as well as downstream testing engineers who have to validate this functionality. So unless there's compelling need to support both cpr-transfer and cpr-exec, it'd be nice to standardize on just one of them.

cpr-transfer does look like its probably more viable, even with its own challenges wrt resources being opened twice.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#76 lore raw
In reply to Steven Sistare
Quoted 113 lines · Steven Sistare
Quoted 104 lines · Peter Xu
Quoted 65 lines · Daniel P. Berrangé
Quoted 64 lines · Peter Xu
Quoted 62 lines · Daniel P. Berrangé
Quoted 50 lines · Peter Xu
Quoted 15 lines · Steven Sistare
Quoted 11 lines · Peter Xu
Quoted 3 lines · Steven Sistare
Quoted 3 lines

The flipside, however, is that localhost migration via 2 separate QEMU processes has issues where both QEMUs want to be opening the very same file, and only 1 of them can ever have them open.

I thought we used to have similar issue with block devices, but I assume it's solved for years (and whoever owns it will take proper file lock, IIRC, and QEMU migration should properly serialize the time window on who's going to take the file lock).

Maybe this is about something else?

I don't have an example where this fails.

Quoted 7 lines

I can cause "Failed to get "write" lock" errors if two qemu instances open the same block device, but the error is suppressed if you add the -incoming argument, due to this code:

   blk_attach_dev()
     if (runstate_check(RUN_STATE_INMIGRATE))
       blk->disable_perm = true;

Yep, this one is pretty much expected.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

Indeed, and "files" includes unix domain sockets.

More on this -- the second qemu to bind a unix domain socket for listening wins, and the first qemu loses it (because second qemu unlinks and recreates the socket path before binding on the assumption that it is stale).

One must use a different name for the socket for second qemu, and clients that wish to connect must be aware of the new port.

Quoted 10 lines
Quoted 3 lines

Network ports also conflict.
cpr-exec avoids such problems, and is one of the advantages of the method that I forgot to promote.

I was thinking that's fine, as the host ports should be the backend of the VM ports only anyway so they don't need to be identical on both sides?

IOW, my understanding is it's the guest IP/ports/... which should still be stable across migrations, where the host ports can be different as long as the host ports can forward guest port messages correctly?

Yes, one must use a different host port number for the second qemu, and clients that wish to connect must be aware of the new port.

Quoted 6 lines
Quoted 2 lines

That is my point -- cpr-transfer requires fiddling with such things. cpr-exec does not.

Right, and my understanding is all these facilities are already there, so no new code should be needed on reconnect issues if to support cpr-transfer in Libvirt or similar management layers that supports migrations.

Note Libvirt explicitly blocks localhost migration today because solving all these clashing resource problems is a huge can of worms and it can't be made invisible to the user of libvirt in any practical way.

Quoted 8 lines
Quoted 2 lines

Ahhh, OK. I'm pretty surprised by this, as I thought at least kubevirt supported local migration somehow on top of libvirt.

Since kubevirt runs inside a container, "localhost" migration is effectively migrating between 2 completely separate OS installs (containers), that happen to be on the same physical host. IOW, it is a cross-host migration from Libvirt & QEMU's POV, and there are no clashing resources to worry about.

OK, makes sense.

Then do you think it's possible to support cpr-transfer in that scenario from Libvirt POV?

Quoted 9 lines
Quoted 5 lines

Does it mean that cpr-transfer is a no-go in this case at least for Libvirt to consume it (as cpr-* is only for local host migrations so far)? Even if all the rest issues we're discussing with cpr-exec, is that the only way to go for Libvirt, then?

cpr-exec is certainly appealing from the POV of avoiding the clashing resources problem in libvirt.

Quoted 8 lines

It has own issues though, because libvirt runs all QEMU processes with seccomp filters that block 'execve', as we consider QEMU to be untrustworthy and thus don't want to allow it to exec anything !

I don't know which is the lesser evil from libvirt's POV.

Personally I see security controls as an overriding requirement for everything.

One thing I am aware of is cpr-exec is not the only one who might start to use exec() in QEMU. TDX fundamentally will need to create another key VM to deliver the keys and the plan seems to be using exec() too. However in that case per my understanding the exec() is optional - the key VM can also be created by Libvirt.

Quoted 7 lines

IOW, it looks like we can still stick with execve() being blocked yet so far except cpr-exec().

Hmm, this makes the decision harder to make. We need to figure out a way on knowing how to consume this feature for at least open source virt stack.. So far it looks like it's only possible (if we take seccomp high priority) we use cpr-transfer but only in a container.

Quoted 4 lines

libvirt starts qemu with the -sandbox spawn=deny option which blocks fork, exec, and change namespace operations. I have a patch in my workspace to be submitted later called "seccomp: fine-grained control of fork, exec, and namespace" that allows libvirt to block fork and namespace but allow exec.

IMHO this significantly undermines the protection offered. fork(), without execve() is relatively benign from a security POV, mostly a slightly greater resource consumption issue, compared to spawning threads, which is always allowed. Blocking execve() is the key security benefit, as that is a way to pick up new privileges (through setuid), or bring new binary code into memory (via the new ELF images loaded), or pick up new MAC policy through transition rules, etc.

IOW, if you're going to allow 'exec', there's little point in blocking fork IMHO, and as such this doesn't sound very appealing as something to add to libvirt.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|