virtio-mem: Handle preallocation with migration
v5 cover virtio-mem: Handle preallocation with migration
While playing with migration of virtio-mem with an ordinary file backing, I realized that migration and prealloc doesn't currently work as expected for virtio-mem. Further, Jing Qi reported that setup issues (insufficient huge pages on the destination) result in QEMU getting killed with SIGBUS instead of failing gracefully.
In contrast to ordinary memory backend preallocation, virtio-mem preallocates memory before plugging blocks to the guest. Consequently, when migrating we are not actually preallocating on the destination but "only" migrate pages. Fix that be migrating the bitmap early, before any RAM content, and use that information to preallocate memory early, before migrating any RAM.
Postcopy needs some extra care, and I realized that prealloc+postcopy is shaky in general. Let's at least try to mimic what ordinary prealloc+postcopy does: temporarily allocate the memory, discard it, and cross fingers that we'll still have sufficient memory when postcopy actually tries placing pages.
v4 -> v5:
- "migration/savevm: Move more savevm handling into vmstate_save()" -- Extended patch description regarding tracing
- "migration/savevm: Prepare vmdesc json writer in qemu_savevm_state_setup()"
-- Move freeing to migrate_fd_cleanup()
- "migration/savevm: Allow immutable device state to be migrated early (i.e., before RAM)"
-- "immutable" -> "early_setup"
-- Extend comment
- Added some RBs (thanks!)
v3 -> v4:
- First 3 patches:
-- Minimze code changes and simplify
-- Save immutable device state during qemu_savevm_state_setup() -- Don't use vmsd priorities, use a new flag
-- Split it logically up
- "migration/ram: Factor out check for advised postcopy" -- Don't factor out postcopy_is_running()
- "virtio-mem: Migrate immutable properties early"
-- Adjust to changed vmsd interface
- "virtio-mem: Proper support for preallocation with migration" -- Drop sanity check in virtio_mem_post_load_early()
v2 -> v3:
- New approach/rewrite, drop RB and TB of last patch
v1 -> v2:
- Added RBs and Tested-bys
- "virtio-mem: Fail if a memory backend with "prealloc=on" is specified" -- Fail instead of warn
-- Adjust subject/description
David Hildenbrand (8):
migration/savevm: Move more savevm handling into vmstate_save() migration/savevm: Prepare vmdesc json writer in
qemu_savevm_state_setup()
migration/savevm: Allow immutable device state to be migrated early
(i.e., before RAM)
migration/vmstate: Introduce VMSTATE_WITH_TMP_TEST() and
VMSTATE_BITMAP_TEST()
migration/ram: Factor out check for advised postcopy
virtio-mem: Fail if a memory backend with "prealloc=on" is specified
virtio-mem: Migrate immutable properties early
virtio-mem: Proper support for preallocation with migration
hw/core/machine.c | 4 +- hw/virtio/virtio-mem.c | 144 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-mem.h | 8 ++ include/migration/misc.h | 4 +- include/migration/vmstate.h | 28 ++++++- migration/migration.c | 9 +++ migration/migration.h | 4 + migration/ram.c | 8 +- migration/savevm.c | 105 +++++++++++++----------- 9 files changed, 255 insertions(+), 59 deletions(-)
Signature · 1 line
2.39.0
PATCH v5 1/8 migration/savevm: Move more savevm handling into vmstate_save()
Let's move more code into vmstate_save(), reducing code duplication and preparing for reuse of vmstate_save() in qemu_savevm_state_setup(). We have to move vmstate_save() to make the compiler happy.
We'll now also trace from qemu_save_device_state(), triggering the same tracepoints as previously called from qemu_savevm_state_complete_precopy_non_iterable() only. Note that qemu_save_device_state() ignores iterable device state, such as RAM, and consequently doesn't trigger some other trace points (e.g., trace_savevm_state_setup()).
--- migration/savevm.c | 79 ++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 42 deletions(-)
migration/savevm.c +37 -42
@@ -897,17 +897,6 @@ static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, } } -static int vmstate_save (QEMUFile *f,SaveStateEntry *se, -JSONWriter *vmdesc) -{ -trace_vmstate_save (se->idstr, se->vmsd ? se->vmsd->name :"(old)" ); -if (!se->vmsd) { -vmstate_save_old_style (f, se, vmdesc); -return 0 ; - } -return vmstate_save_state (f, se->vmsd, se->opaque, vmdesc); -} -/* * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) */
@@ -941,6 +930,43 @@ static void save_section_footer(QEMUFile *f, SaveStateEntry *se) } } +static int vmstate_save (QEMUFile *f,SaveStateEntry *se,JSONWriter *vmdesc) +{ +int ret; + +if ((!se->ops || !se->ops->save_state) && !se->vmsd) { +return 0 ; + } +if (se->vmsd && !vmstate_save_needed (se->vmsd, se->opaque)) { +trace_savevm_section_skip (se->idstr, se->section_id); +return 0 ; + } + +trace_savevm_section_start (se->idstr, se->section_id); +save_section_header (f, se,QEMU_VM_SECTION_FULL ); +if (vmdesc) { +json_writer_start_object (vmdesc,NULL ); +json_writer_str (vmdesc,"name" , se->idstr); +json_writer_int64 (vmdesc,"instance_id" , se->instance_id); + } + +trace_vmstate_save (se->idstr, se->vmsd ? se->vmsd->name :"(old)" ); +if (!se->vmsd) { +vmstate_save_old_style (f, se, vmdesc); + }else { + ret =vmstate_save_state (f, se->vmsd, se->opaque, vmdesc); +if (ret) { +return ret; + } + } + +trace_savevm_section_end (se->idstr, se->section_id,0 ); +save_section_footer (f, se); +if (vmdesc) { +json_writer_end_object (vmdesc); + } +return 0 ; +}/** * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the * command and associated data.
@@ -1374,31 +1400,11 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,json_writer_int64 (vmdesc,"page_size" ,qemu_target_page_size ());json_writer_start_array (vmdesc,"devices" ); QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { - -if ((!se->ops || !se->ops->save_state) && !se->vmsd) { -continue ; - } -if (se->vmsd && !vmstate_save_needed (se->vmsd, se->opaque)) { -trace_savevm_section_skip (se->idstr, se->section_id); -continue ; - } - -trace_savevm_section_start (se->idstr, se->section_id); - -json_writer_start_object (vmdesc,NULL ); -json_writer_str (vmdesc,"name" , se->idstr); -json_writer_int64 (vmdesc,"instance_id" , se->instance_id); - -save_section_header (f, se,QEMU_VM_SECTION_FULL ); ret =vmstate_save (f, se, vmdesc);if (ret) {qemu_file_set_error (f, ret);return ret; } -trace_savevm_section_end (se->idstr, se->section_id,0 ); -save_section_footer (f, se); - -json_writer_end_object (vmdesc); }if (inactivate_disks) {
@@ -1594,21 +1600,10 @@ int qemu_save_device_state(QEMUFile *f)if (se->is_ram) {continue ; } -if ((!se->ops || !se->ops->save_state) && !se->vmsd) { -continue ; - } -if (se->vmsd && !vmstate_save_needed (se->vmsd, se->opaque)) { -continue ; - } - -save_section_header (f, se,QEMU_VM_SECTION_FULL ); - ret =vmstate_save (f, se,NULL );if (ret) {return ret; } - -save_section_footer (f, se); }qemu_put_byte (f,QEMU_VM_EOF );
Signature · 1 line
2.39.0
Re: [PATCH v5 1/8] migration/savevm: Move more savevm handling into vmstate_save()
Quoted 4 lines · David Hildenbrand
Let's move more code into
vmstate_save(), reducing code duplication and preparing for reuse ofvmstate_save()inqemu_savevm_state_setup(). We have to movevmstate_save()to make the compiler happy.
Quoted 8 lines
We'll now also trace from
qemu_save_device_state(), triggering the same tracepoints as previously called fromqemu_savevm_state_complete_precopy_non_iterable()only. Note thatqemu_save_device_state()ignores iterable device state, such as RAM, and consequently doesn't trigger some other trace points (e.g.,trace_savevm_state_setup()).Signed-off-by David Hildenbrand <[email protected]>
PATCH v5 2/8 migration/savevm: Prepare vmdesc json writer in qemu_savevm_state_setup()
In reply to David Hildenbrand... and store it in the migration state. This is a preparation for storing selected vmds's already in qemu_savevm_state_setup().
--- migration/migration.c | 2 ++ migration/migration.h | 4 ++++ migration/savevm.c | 18 ++++++++++++------ 3 files changed, 18 insertions(+), 6 deletions(-)
migration/migration.c +2 -0
@@ -1903,6 +1903,8 @@ static void migrate_fd_cleanup(MigrationState *s)g_free (s->hostname); s->hostname =NULL ; +json_writer_free (s->vmdesc); + s->vmdesc =NULL ;qemu_savevm_state_cleanup ();
migration/migration.h +4 -0
@@ -17,6 +17,7 @@#include "exec/cpu-common.h" #include "hw/qdev-core.h" #include "qapi/qapi-types-migration.h" +#include "qapi/qmp/json-writer.h" #include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h"
@@ -366,6 +367,9 @@ struct MigrationState {* This save hostname when out-going migration starts */ char *hostname; + +/* QEMU_VM_VMDESCRIPTION content filled for all non-iterable devices. */ +JSONWriter *vmdesc; };void migrate_set_state (int *state,int old_state,int new_state);
migration/savevm.c +12 -6
@@ -42,7 +42,6 @@#include "postcopy-ram.h" #include "qapi/error.h" #include "qapi/qapi-commands-migration.h" -#include "qapi/qmp/json-writer.h" #include "qapi/clone-visitor.h" #include "qapi/qapi-builtin-visit.h" #include "qapi/qmp/qerror.h"
@@ -1189,10 +1188,16 @@ bool qemu_savevm_state_guest_unplug_pending(void)void qemu_savevm_state_setup (QEMUFile *f) { +MigrationState *ms =migrate_get_current ();SaveStateEntry *se;Error *local_err =NULL ;int ret; + ms->vmdesc =json_writer_new (false); +json_writer_start_object (ms->vmdesc,NULL ); +json_writer_int64 (ms->vmdesc,"page_size" ,qemu_target_page_size ()); +json_writer_start_array (ms->vmdesc,"devices" ); +trace_savevm_state_setup (); QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {if (!se->ops || !se->ops->save_setup) {
@@ -1390,15 +1395,12 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, bool in_postcopy, bool inactivate_disks) { -g_autoptr (JSONWriter ) vmdesc =NULL ; +MigrationState *ms =migrate_get_current (); +JSONWriter *vmdesc = ms->vmdesc;int vmdesc_len;SaveStateEntry *se;int ret; - vmdesc =json_writer_new (false); -json_writer_start_object (vmdesc,NULL ); -json_writer_int64 (vmdesc,"page_size" ,qemu_target_page_size ()); -json_writer_start_array (vmdesc,"devices" ); QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { ret =vmstate_save (f, se, vmdesc);if (ret) {
@@ -1433,6 +1435,10 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,qemu_put_buffer (f, (uint8_t *)json_writer_get (vmdesc), vmdesc_len); } +/* Free it now to detect any inconsistencies. */ +json_writer_free (vmdesc); + ms->vmdesc =NULL ; +return 0 ; }
Signature · 1 line
2.39.0
Re: [PATCH v5 2/8] migration/savevm: Prepare vmdesc json writer in qemu_savevm_state_setup()
Quoted 5 lines · David Hildenbrand
... and store it in the migration state. This is a preparation for storing selected vmds's already in
qemu_savevm_state_setup().Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
PATCH v5 3/8 migration/savevm: Allow immutable device state to be migrated early (i.e., before RAM)
In reply to David HildenbrandFor virtio-mem, we want to have the plugged/unplugged state of memory blocks available before migrating any actual RAM content, and perform sanity checks before touching anything on the destination. This information is immutable on the migration source while migration is active,
We want to use this information for proper preallocation support with
migration: currently, we don't preallocate memory on the migration target, and especially with hugetlb, we can easily run out of hugetlb pages during RAM migration and will crash (SIGBUS) instead of catching this gracefully via preallocation.
Migrating device state via a VMSD before we start iterating is currently
impossible: the only approach that would be possible is avoiding a VMSD and migrating state manually during save_setup(), to be restored during load_state().
Let's allow for migrating device state via a VMSD early, during the setup phase in qemu_savevm_state_setup(). To keep it simple, we indicate applicable VMSD's using an "early_setup" flag.
Note that only very selected devices (i.e., ones seriously messing with RAM setup) are supposed to make use of such early state migration.
While at it, also use a bool for the "unmigratable" member.
--- include/migration/vmstate.h | 16 +++++++++++++++- migration/savevm.c | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-)
include/migration/vmstate.h +15 -1
@@ -178,7 +178,21 @@ struct VMStateField {struct VMStateDescription {const char *name; -int unmigratable; +bool unmigratable; +/* +* This VMSD describes something that should be sent during setup phase +* of migration. It plays similar role as save_setup() for explicitly +* registered vmstate entries, so it can be seen as a way to describe +* save_setup() in VMSD structures. +* +* Note that for now, a SaveStateEntry cannot have a VMSD and +* operations (e.g., save_setup()) set at the same time. Consequently, +* save_setup() and a VMSD with early_setup set to true are mutually +* exclusive. For this reason, also early_setup VMSDs are migrated in a +* QEMU_VM_SECTION_FULL section, while save_setup() data is migrated in +* a QEMU_VM_SECTION_START section. +*/ +bool early_setup;int version_id;int minimum_version_id;MigrationPriority priority;
migration/savevm.c +14 -0
@@ -1200,6 +1200,15 @@ void qemu_savevm_state_setup(QEMUFile *f)trace_savevm_state_setup (); QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { +if (se->vmsd && se->vmsd->early_setup) { + ret =vmstate_save (f, se, ms->vmdesc); +if (ret) { +qemu_file_set_error (f, ret); +break ; + } +continue ; + } +if (!se->ops || !se->ops->save_setup) {continue ; }
@@ -1402,6 +1411,11 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,int ret; QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { +if (se->vmsd && se->vmsd->early_setup) { +/* Already saved during qemu_savevm_state_setup(). */ +continue ; + } + ret =vmstate_save (f, se, vmdesc);if (ret) {qemu_file_set_error (f, ret);
Signature · 1 line
2.39.0
Re: [PATCH v5 3/8] migration/savevm: Allow immutable device state to be migrated early (i.e., before RAM)
Quoted 20 lines · David Hildenbrand
For virtio-mem, we want to have the plugged/unplugged state of memory blocks available before migrating any actual RAM content, and perform sanity checks before touching anything on the destination. This information is immutable on the migration source while migration is active,
We want to use this information for proper preallocation support with
migration: currently, we don't preallocate memory on the migration target, and especially with hugetlb, we can easily run out of hugetlb pages during RAM migration and will crash (SIGBUS) instead of catching this gracefully via preallocation.Migrating device state via a VMSD before we start iterating is currently
impossible: the only approach that would be possible is avoiding a VMSD and migrating state manually duringsave_setup(), to be restored duringload_state().Let's allow for migrating device state via a VMSD early, during the setup phase in
qemu_savevm_state_setup(). To keep it simple, we indicate applicable VMSD's using an "early_setup" flag.
Quoted 6 lines
Note that only very selected devices (i.e., ones seriously messing with RAM setup) are supposed to make use of such early state migration.
While at it, also use a bool for the "unmigratable" member.
Signed-off-by David Hildenbrand <[email protected]>
PATCH v5 4/8 migration/vmstate: Introduce VMSTATE_WITH_TMP_TEST() and VMSTATE_BITMAP_TEST()
In reply to David HildenbrandWe'll make use of both next in the context of virtio-mem.
--- include/migration/vmstate.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
include/migration/vmstate.h +10 -2
@@ -719,8 +719,9 @@ extern const VMStateInfo vmstate_info_qlist;* '_state' type * That the pointer is right at the start of _tmp_type. */ -#define VMSTATE_WITH_TMP (_state, _tmp_type, _vmsd) { \ +#define VMSTATE_WITH_TMP_TEST (_state, _test, _tmp_type, _vmsd) { \ .name = "tmp", \ + .field_exists = (_test), \ .size = sizeof(_tmp_type) + \ QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \ type_check_pointer(_state, \
@@ -729,6 +730,9 @@ extern const VMStateInfo vmstate_info_qlist; .info = &vmstate_info_tmp, \ } +#define VMSTATE_WITH_TMP (_state, _tmp_type, _vmsd) \ + VMSTATE_WITH_TMP_TEST(_state, NULL, _tmp_type, _vmsd) +#define VMSTATE_UNUSED_BUFFER (_test, _version, _size) { \ .name = "unused", \ .field_exists = (_test), \
@@ -752,8 +756,9 @@ extern const VMStateInfo vmstate_info_qlist;/* _field_size should be a int32_t field in the _state struct giving the * size of the bitmap _field in bits. */ -#define VMSTATE_BITMAP (_field, _state, _version, _field_size) { \ +#define VMSTATE_BITMAP_TEST (_field, _state, _test, _version, _field_size) { \ .name = (stringify(_field)), \ + .field_exists = (_test), \ .version_id = (_version), \ .size_offset = vmstate_offset_value(_state, _field_size, int32_t),\ .info = &vmstate_info_bitmap, \
@@ -761,6 +766,9 @@ extern const VMStateInfo vmstate_info_qlist; .offset = offsetof(_state, _field), \ } +#define VMSTATE_BITMAP (_field, _state, _version, _field_size) \ + VMSTATE_BITMAP_TEST(_field, _state, NULL, _version, _field_size) +/* For migrating a QTAILQ. * Target QTAILQ needs be properly initialized. * _type: type of QTAILQ element
Signature · 1 line
2.39.0
Re: [PATCH v5 4/8] migration/vmstate: Introduce VMSTATE_WITH_TMP_TEST() and VMSTATE_BITMAP_TEST()
Quoted 4 lines · David Hildenbrand
We'll make use of both next in the context of virtio-mem.
Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
PATCH v5 5/8 migration/ram: Factor out check for advised postcopy
In reply to David HildenbrandLet's factor out this check, to be used in virtio-mem context next.
While at it, fix a spelling error in a related comment.
--- include/migration/misc.h | 4 +++- migration/migration.c | 7 +++++++ migration/ram.c | 8 +------- 3 files changed, 11 insertions(+), 8 deletions(-)
include/migration/misc.h +3 -1
@@ -67,8 +67,10 @@ bool migration_has_failed(MigrationState *);/* ...and after the device transmission */ bool migration_in_postcopy_after_devices (MigrationState *);void migration_global_dump (Monitor *mon); -/* True if incomming migration entered POSTCOPY_INCOMING_DISCARD */ +/* True if incoming migration entered POSTCOPY_INCOMING_DISCARD */ bool migration_in_incoming_postcopy (void ); +/* True if incoming migration entered POSTCOPY_INCOMING_ADVISE */ +bool migration_incoming_postcopy_advised (void );/* True if background snapshot is active */ bool migration_in_bg_snapshot (void );
migration/migration.c +7 -0
@@ -2096,6 +2096,13 @@ bool migration_in_incoming_postcopy(void)return ps >=POSTCOPY_INCOMING_DISCARD && ps <POSTCOPY_INCOMING_END ; } +bool migration_incoming_postcopy_advised (void ) +{ +PostcopyState ps =postcopy_state_get (); + +return ps >=POSTCOPY_INCOMING_ADVISE && ps <POSTCOPY_INCOMING_END ; +} +bool migration_in_bg_snapshot (void ) {MigrationState *s =migrate_get_current ();
migration/ram.c +1 -7
@@ -4091,12 +4091,6 @@ int ram_load_postcopy(QEMUFile *f, int channel)return ret; } -static bool postcopy_is_advised (void ) -{ -PostcopyState ps =postcopy_state_get (); -return ps >=POSTCOPY_INCOMING_ADVISE && ps <POSTCOPY_INCOMING_END ; -} -static bool postcopy_is_running (void ) {PostcopyState ps =postcopy_state_get ();
@@ -4167,7 +4161,7 @@ static int ram_load_precopy(QEMUFile *f)MigrationIncomingState *mis =migration_incoming_get_current ();int flags =0 , ret =0 , invalid_flags =0 , len =0 , i =0 ;/* ADVISE is earlier, it shows the source has the postcopy capability on */ -bool postcopy_advised =postcopy_is_advised (); +bool postcopy_advised =migration_incoming_postcopy_advised ();if (!migrate_use_compression ()) { invalid_flags |=RAM_SAVE_FLAG_COMPRESS_PAGE ; }
Signature · 1 line
2.39.0
Re: [PATCH v5 5/8] migration/ram: Factor out check for advised postcopy
Quoted 5 lines · David Hildenbrand
Let's factor out this check, to be used in virtio-mem context next.
While at it, fix a spelling error in a related comment.
Signed-off-by David Hildenbrand <[email protected]>
PATCH v5 6/8 virtio-mem: Fail if a memory backend with "prealloc=on" is specified
In reply to David Hildenbrand"prealloc=on" for the memory backend does not work as expected, as virtio-mem will simply discard all preallocated memory immediately again. In the best case, it's an expensive NOP. In the worst case, it's an unexpected allocation error.
Instead, "prealloc=on" should be specified for the virtio-mem device only, such that virtio-mem will try preallocating memory before plugging memory dynamically to the guest. Fail if such a memory backend is provided.
--- hw/virtio/virtio-mem.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/virtio/virtio-mem.c +6 -0
@@ -772,6 +772,12 @@ static void virtio_mem_device_realize(DeviceState *dev, Error **errp)error_setg (errp,"'%s' property specifies an unsupported memdev" ,VIRTIO_MEM_MEMDEV_PROP );return ; + }else if (vmem->memdev->prealloc) { +error_setg (errp,"'%s' property specifies a memdev with preallocation" +" enabled: %s. Instead, specify 'prealloc=on' for the" +" virtio-mem device. " ,VIRTIO_MEM_MEMDEV_PROP , +object_get_canonical_path_component (OBJECT (vmem->memdev))); +return ; } if ((nb_numa_nodes && vmem->node >= nb_numa_nodes) ||
Signature · 1 line
2.39.0
Re: [PATCH v5 6/8] virtio-mem: Fail if a memory backend with "prealloc=on" is specified
Quoted 5 lines · David Hildenbrand
"prealloc=on" for the memory backend does not work as expected, as virtio-mem will simply discard all preallocated memory immediately again. In the best case, it's an expensive NOP. In the worst case, it's an unexpected allocation error.
Quoted 8 lines
Instead, "prealloc=on" should be specified for the virtio-mem device only, such that virtio-mem will try preallocating memory before plugging memory dynamically to the guest. Fail if such a memory backend is provided.
Tested-by Michal Privoznik <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
PATCH v5 7/8 virtio-mem: Migrate immutable properties early
In reply to David HildenbrandThe bitmap and the size are immutable while migration is active: see virtio_mem_is_busy(). We can migrate this information early, before migrating any actual RAM content. Further, all information we need for sanity checks is immutable as well.
Having this information in place early will, for example, allow for properly preallocating memory before touching these memory locations during RAM migration: this way, we can make sure that all memory was actually preallocated and that any user errors (e.g., insufficient hugetlb pages) can be handled gracefully.
In contrast, usable_region_size and requested_size can theoretically still be modified on the source while the VM is running. Keep migrating these properties the usual, late, way.
Use a new device property to keep behavior of compat machines unmodified.
--- hw/core/machine.c | 4 ++- hw/virtio/virtio-mem.c | 51 ++++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-mem.h | 8 ++++++ 3 files changed, 60 insertions(+), 3 deletions(-)
hw/core/machine.c +3 -1
@@ -41,7 +41,9 @@#include "hw/virtio/virtio-pci.h" #include "qom/object_interfaces.h" -GlobalProperty hw_compat_7_2[] = {}; +GlobalProperty hw_compat_7_2[] = { + {"virtio-mem" ,"x-early-migration" ,"false" }, +};const size_t hw_compat_7_2_len =G_N_ELEMENTS (hw_compat_7_2);GlobalProperty hw_compat_7_1[] = {
hw/virtio/virtio-mem.c +49 -2
91 lines not on this page · raw message
include/hw/virtio/virtio-mem.h +8 -0
@@ -31,6 +31,7 @@ OBJECT_DECLARE_TYPE(VirtIOMEM, VirtIOMEMClass,#define VIRTIO_MEM_BLOCK_SIZE_PROP "block-size"#define VIRTIO_MEM_ADDR_PROP "memaddr"#define VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP "unplugged-inaccessible" +#define VIRTIO_MEM_EARLY_MIGRATION_PROP "x-early-migration"#define VIRTIO_MEM_PREALLOC_PROP "prealloc"struct VirtIOMEM {
@@ -74,6 +75,13 @@ struct VirtIOMEM {/* whether to prealloc memory when plugging new blocks */ bool prealloc; +/* +* Whether we migrate properties that are immutable while migration is +* active early, before state of other devices and especially, before +* migrating any RAM content. +*/ +bool early_migration; +/* notifiers to notify when "size" changes */ NotifierList size_change_notifiers;
Signature · 1 line
2.39.0
Re: [PATCH v5 7/8] virtio-mem: Migrate immutable properties early
Quoted 11 lines · David Hildenbrand
The bitmap and the size are immutable while migration is active: see
virtio_mem_is_busy(). We can migrate this information early, before migrating any actual RAM content. Further, all information we need for sanity checks is immutable as well.Having this information in place early will, for example, allow for properly preallocating memory before touching these memory locations during RAM migration: this way, we can make sure that all memory was actually preallocated and that any user errors (e.g., insufficient hugetlb pages) can be handled gracefully.
Quoted 8 lines
In contrast,
usable_region_sizeandrequested_sizecan theoretically still be modified on the source while the VM is running. Keep migrating these properties the usual, late, way.Use a new device property to keep behavior of compat machines unmodified.
Signed-off-by David Hildenbrand <[email protected]>
hw/core/machine.c line 46
Quoted 12 lines
hw/core/machine.c +3
-1@@ -41,7 +41,9 @@#include "hw/virtio/virtio-pci.h" #include "qom/object_interfaces.h" -GlobalProperty hw_compat_7_2[] = {}; +GlobalProperty hw_compat_7_2[] = { + {"virtio-mem" ,"x-early-migration" ,"false" }, +};
We can stop prettending that we don't support this kind of properties O:-)
PATCH v5 8/8 virtio-mem: Proper support for preallocation with migration
In reply to David HildenbrandOrdinary memory preallocation runs when QEMU starts up and creates the memory backends, before processing the incoming migration stream. With virtio-mem, we don't know which memory blocks to preallocate before migration started. Now that we migrate the virtio-mem bitmap early, before migrating any RAM content, we can safely preallocate memory for all plugged memory blocks before migrating any RAM content.
This is especially relevant for the following cases:
(1) User errors
With hugetlb/files, if we don't have sufficient backend memory available on the migration destination, we'll crash QEMU (SIGBUS) during RAM migration when running out of backend memory. Preallocating memory before actual RAM migration allows for failing gracefully and informing the user about the setup problem.
(2) Excluded memory ranges during migration
For example, virtio-balloon free page hinting will exclude some pages from getting migrated. In that case, we won't crash during RAM migration, but later, when running the VM on the destination, which is bad.
To fix this for new QEMU machines that migrate the bitmap early, preallocate the memory early, before any RAM migration. Warn with old QEMU machines.
Getting postcopy right is a bit tricky, but we essentially now implement the same (problematic) preallocation logic as ordinary preallocation:
preallocate memory early and discard it again before precopy starts. During ordinary preallocation, discarding of RAM happens when postcopy is advised. As the state (bitmap) is loaded after postcopy was advised but before postcopy starts listening, we have to discard memory we preallocated immediately again ourselves.
Note that nothing (not even hugetlb reservations) guarantees for postcopy that backend memory (especially, hugetlb pages) are still free after they were freed ones while discarding RAM. Still, allocating that memory at least once helps catching some basic setup problems.
Before this change, trying to restore a VM when insufficient hugetlb pages are around results in the process crashing to to a "Bus error" (SIGBUS). With this change, QEMU fails gracefully:
qemu-system-x86_64: qemu_prealloc_mem: preallocating memory failed: Bad address
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-mem-device-early'
qemu-system-x86_64: load of migration failed: Cannot allocate memory
And we can even introspect the early migration data, including the bitmap:
$ ./scripts/analyze-migration.py -f STATEFILE
{
"ram (2)": {
"section sizes": {
"0000:00:03.0/mem0": "0x0000000780000000",
"0000:00:04.0/mem1": "0x0000000780000000",
"pc.ram": "0x0000000100000000",
"/rom@etc/acpi/tables": "0x0000000000020000",
"pc.bios": "0x0000000000040000",
"0000:00:02.0/e1000.rom": "0x0000000000040000",
"pc.rom": "0x0000000000020000",
"/rom@etc/table-loader": "0x0000000000001000",
"/rom@etc/acpi/rsdp": "0x0000000000001000"
}
},
"0000:00:03.0/virtio-mem-device-early (51)": {
"tmp": "00 00 00 01 40 00 00 00 00 00 00 07 80 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00",
"size": "0x0000000040000000",
"bitmap": "ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [...]
},
"0000:00:04.0/virtio-mem-device-early (53)": {
"tmp": "00 00 00 08 c0 00 00 00 00 00 00 07 80 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00",
"size": "0x00000001fa400000",
"bitmap": "ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [...]
},
[...]--- hw/virtio/virtio-mem.c | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+)
hw/virtio/virtio-mem.c +87 -0
@@ -204,6 +204,30 @@ static int virtio_mem_for_each_unplugged_range(const VirtIOMEM *vmem, void *arg,return ret; } +static int virtio_mem_for_each_plugged_range (const VirtIOMEM *vmem,void *arg, +virtio_mem_range_cb cb) +{ +unsigned long first_bit, last_bit; +uint64_t offset, size; +int ret =0 ; + + first_bit =find_first_bit (vmem->bitmap, vmem->bitmap_size); +while (first_bit < vmem->bitmap_size) { + offset = first_bit * vmem->block_size; + last_bit =find_next_zero_bit (vmem->bitmap, vmem->bitmap_size, + first_bit +1 ) -1 ; + size = (last_bit - first_bit +1 ) * vmem->block_size; + + ret =cb (vmem, arg, offset, size); +if (ret) { +break ; + } + first_bit =find_next_bit (vmem->bitmap, vmem->bitmap_size, + last_bit +2 ); + } +return ret; +} +/* * Adjust the memory section to cover the intersection with the given range. *
@@ -938,6 +962,10 @@ static int virtio_mem_post_load(void *opaque, int version_id)RamDiscardListener *rdl;int ret; +if (vmem->prealloc && !vmem->early_migration) { +warn_report ("Proper preallocation with migration requires a newer QEMU machine" ); + } +/* * We started out with all memory discarded and our memory region is mapped * into an address space. Replay, now that we updated the bitmap.
@@ -957,6 +985,64 @@ static int virtio_mem_post_load(void *opaque, int version_id)return virtio_mem_restore_unplugged (vmem); } +static int virtio_mem_prealloc_range_cb (const VirtIOMEM *vmem,void *arg, +uint64_t offset,uint64_t size) +{ +void *area =memory_region_get_ram_ptr (&vmem->memdev->mr) + offset; +int fd =memory_region_get_fd (&vmem->memdev->mr); +Error *local_err =NULL ; + +qemu_prealloc_mem (fd, area, size,1 ,NULL , &local_err); +if (local_err) { +error_report_err (local_err); +return -ENOMEM ; + } +return 0 ; +} + +static int virtio_mem_post_load_early (void *opaque,int version_id) +{ +VirtIOMEM *vmem =VIRTIO_MEM (opaque); +RAMBlock *rb = vmem->memdev->mr.ram_block; +int ret; + +if (!vmem->prealloc) { +return 0 ; + } + +/* +* We restored the bitmap and verified that the basic properties +* match on source and destination, so we can go ahead and preallocate +* memory for all plugged memory blocks, before actual RAM migration starts +* touching this memory. +*/ + ret =virtio_mem_for_each_plugged_range (vmem,NULL , + virtio_mem_prealloc_range_cb); +if (ret) { +return ret; + } + +/* +* This is tricky: postcopy wants to start with a clean slate. On +* POSTCOPY_INCOMING_ADVISE, postcopy code discards all (ordinarily +* preallocated) RAM such that postcopy will work as expected later. +* +* However, we run after POSTCOPY_INCOMING_ADVISE -- but before actual +* RAM migration. So let's discard all memory again. This looks like an +* expensive NOP, but actually serves a purpose: we made sure that we +* were able to allocate all required backend memory once. We cannot +* guarantee that the backend memory we will free will remain free +* until we need it during postcopy, but at least we can catch the +* obvious setup issues this way. +*/ +if (migration_incoming_postcopy_advised ()) { +if (ram_block_discard_range (rb,0 ,qemu_ram_get_used_length (rb))) { +return -EBUSY ; + } + } +return 0 ; +} + typedef struct VirtIOMEMMigSanityChecks { VirtIOMEM *parent; uint64_t addr;
@@ -1068,6 +1154,7 @@ static const VMStateDescription vmstate_virtio_mem_device_early = { .minimum_version_id = 1, .version_id = 1, .early_setup = true, + .post_load = virtio_mem_post_load_early, .fields = (VMStateField []) {VMSTATE_WITH_TMP (VirtIOMEM, VirtIOMEMMigSanityChecks, vmstate_virtio_mem_sanity_checks),
Signature · 1 line
2.39.0
Re: [PATCH v5 8/8] virtio-mem: Proper support for preallocation with migration
Quoted 78 lines · David Hildenbrand
Ordinary memory preallocation runs when QEMU starts up and creates the memory backends, before processing the incoming migration stream. With virtio-mem, we don't know which memory blocks to preallocate before migration started. Now that we migrate the virtio-mem bitmap early, before migrating any RAM content, we can safely preallocate memory for all plugged memory blocks before migrating any RAM content.
This is especially relevant for the following cases:
(1) User errors
With hugetlb/files, if we don't have sufficient backend memory available on the migration destination, we'll crash QEMU (SIGBUS) during RAM migration when running out of backend memory. Preallocating memory before actual RAM migration allows for failing gracefully and informing the user about the setup problem.
(2) Excluded memory ranges during migration
For example, virtio-balloon free page hinting will exclude some pages from getting migrated. In that case, we won't crash during RAM migration, but later, when running the VM on the destination, which is bad.
To fix this for new QEMU machines that migrate the bitmap early, preallocate the memory early, before any RAM migration. Warn with old QEMU machines.
Getting postcopy right is a bit tricky, but we essentially now implement the same (problematic) preallocation logic as ordinary preallocation:
preallocate memory early and discard it again before precopy starts. During ordinary preallocation, discarding of RAM happens when postcopy is advised. As the state (bitmap) is loaded after postcopy was advised but before postcopy starts listening, we have to discard memory we preallocated immediately again ourselves.Note that nothing (not even hugetlb reservations) guarantees for postcopy that backend memory (especially, hugetlb pages) are still free after they were freed ones while discarding RAM. Still, allocating that memory at least once helps catching some basic setup problems.
Before this change, trying to restore a VM when insufficient hugetlb pages are around results in the process crashing to to a "Bus error" (SIGBUS). With this change, QEMU fails gracefully:
qemu-system-x86_64:
qemu_prealloc_mem: preallocating memory failed: Bad address
qemu-system-x86_64: error while loading state for instance0x0of device '0000:00:03.0/virtio-mem-device-early'
qemu-system-x86_64: load of migration failed: Cannot allocate memoryAnd we can even introspect the early migration data, including the bitmap:
$ ./scripts/analyze-migration.py -f STATEFILE { "ram (2)": { "section sizes": { "0000:00:03.0/mem0": "0x0000000780000000", "0000:00:04.0/mem1": "0x0000000780000000", "pc.ram": "0x0000000100000000", "/rom@etc/acpi/tables": "0x0000000000020000", "pc.bios": "0x0000000000040000", "0000:00:02.0/e1000.rom": "0x0000000000040000", "pc.rom": "0x0000000000020000", "/rom@etc/table-loader": "0x0000000000001000", "/rom@etc/acpi/rsdp": "0x0000000000001000" } }, "0000:00:03.0/virtio-mem-device-early (51)": { "tmp": "00 00 00 01 40 00 00 00 00 00 00 07 80 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00", "size": "0x0000000040000000", "bitmap": "ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [...] }, "0000:00:04.0/virtio-mem-device-early (53)": { "tmp": "00 00 00 08 c0 00 00 00 00 00 00 07 80 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00", "size": "0x00000001fa400000", "bitmap": "ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [...] }, [...]
Quoted 3 lines
Reported-by Jing Qi <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 19 lines · David Hildenbrand
While playing with migration of virtio-mem with an ordinary file backing, I realized that migration and prealloc doesn't currently work as expected for virtio-mem. Further, Jing Qi reported that setup issues (insufficient huge pages on the destination) result in QEMU getting killed with SIGBUS instead of failing gracefully.
In contrast to ordinary memory backend preallocation, virtio-mem preallocates memory before plugging blocks to the guest. Consequently, when migrating we are not actually preallocating on the destination but "only" migrate pages. Fix that be migrating the bitmap early, before any RAM content, and use that information to preallocate memory early, before migrating any RAM.
Postcopy needs some extra care, and I realized that prealloc+postcopy is shaky in general. Let's at least try to mimic what ordinary prealloc+postcopy does: temporarily allocate the memory, discard it, and cross fingers that we'll still have sufficient memory when postcopy actually tries placing pages.
Quoted 5 lines
Cc Dr. David Alan Gilbert <[email protected]>Cc Juan Quintela <[email protected]>Cc Peter Xu <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Michal Privoznik <[email protected]>
For patch 1-5:
Signature · 1 line
Peter Xu
Quoted 19 lines · David Hildenbrand
While playing with migration of virtio-mem with an ordinary file backing, I realized that migration and prealloc doesn't currently work as expected for virtio-mem. Further, Jing Qi reported that setup issues (insufficient huge pages on the destination) result in QEMU getting killed with SIGBUS instead of failing gracefully.
In contrast to ordinary memory backend preallocation, virtio-mem preallocates memory before plugging blocks to the guest. Consequently, when migrating we are not actually preallocating on the destination but "only" migrate pages. Fix that be migrating the bitmap early, before any RAM content, and use that information to preallocate memory early, before migrating any RAM.
Postcopy needs some extra care, and I realized that prealloc+postcopy is shaky in general. Let's at least try to mimic what ordinary prealloc+postcopy does: temporarily allocate the memory, discard it, and cross fingers that we'll still have sufficient memory when postcopy actually tries placing pages.
Quoted 5 lines
Cc Dr. David Alan Gilbert <[email protected]>Cc Juan Quintela <[email protected]>Cc Peter Xu <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Michal Privoznik <[email protected]>
@Juan, David: I can similarly take this via my tree as well.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 24 lines · David Hildenbrand
Quoted 14 lines · David Hildenbrand
While playing with migration of virtio-mem with an ordinary file backing, I realized that migration and prealloc doesn't currently work as expected for virtio-mem. Further, Jing Qi reported that setup issues (insufficient huge pages on the destination) result in QEMU getting killed with SIGBUS instead of failing gracefully.
In contrast to ordinary memory backend preallocation, virtio-mem preallocates memory before plugging blocks to the guest. Consequently, when migrating we are not actually preallocating on the destination but "only" migrate pages. Fix that be migrating the bitmap early, before any RAM content, and use that information to preallocate memory early, before migrating any RAM.
Postcopy needs some extra care, and I realized that prealloc+postcopy is
shaky in general. Let's at least try to mimic what ordinaryQuoted 8 lines
prealloc+postcopy does: temporarily allocate the memory, discard it, and cross fingers that we'll still have sufficient memory when postcopy actually tries placing pages.
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Juan Quintela <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>
Cc: Michal Privoznik <[email protected]>
Quoted 1 line
@Juan, David: I can similarly take this via my tree as well.
Reviewing it.
I can get it through migration tree, thanks.
Later, Juan.
Quoted 25 lines · Juan Quintela
Quoted 24 lines · David Hildenbrand
Quoted 22 lines · David Hildenbrand
While playing with migration of virtio-mem with an ordinary file backing, I realized that migration and prealloc doesn't currently work as expected for virtio-mem. Further, Jing Qi reported that setup issues (insufficient huge pages on the destination) result in QEMU getting killed with SIGBUS instead of failing gracefully.
In contrast to ordinary memory backend preallocation, virtio-mem preallocates memory before plugging blocks to the guest. Consequently, when migrating we are not actually preallocating on the destination but "only" migrate pages. Fix that be migrating the bitmap early, before any RAM content, and use that information to preallocate memory early, before migrating any RAM.
Postcopy needs some extra care, and I realized that prealloc+postcopy is
shaky in general. Let's at least try to mimic what ordinary prealloc+postcopy does: temporarily allocate the memory, discard it, and cross fingers that we'll still have sufficient memory when postcopy actually tries placing pages.
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Juan Quintela <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>
Cc: Michal Privoznik <[email protected]>
Quoted 7 lines
Quoted 1 line
@Juan, David: I can similarly take this via my tree as well.
Reviewing it.
I can get it through migration tree, thanks.
Later, Juan.
Thanks Juan and Michael!
Signature · 3 lines
Thanks, David / dhildenb
Quoted 19 lines · David Hildenbrand
While playing with migration of virtio-mem with an ordinary file backing, I realized that migration and prealloc doesn't currently work as expected for virtio-mem. Further, Jing Qi reported that setup issues (insufficient huge pages on the destination) result in QEMU getting killed with SIGBUS instead of failing gracefully.
In contrast to ordinary memory backend preallocation, virtio-mem preallocates memory before plugging blocks to the guest. Consequently, when migrating we are not actually preallocating on the destination but "only" migrate pages. Fix that be migrating the bitmap early, before any RAM content, and use that information to preallocate memory early, before migrating any RAM.
Postcopy needs some extra care, and I realized that prealloc+postcopy is shaky in general. Let's at least try to mimic what ordinary prealloc+postcopy does: temporarily allocate the memory, discard it, and cross fingers that we'll still have sufficient memory when postcopy actually tries placing pages.
Quoted 5 lines
Cc Dr. David Alan Gilbert <[email protected]>Cc Juan Quintela <[email protected]>Cc Peter Xu <[email protected]>Cc Michael S. Tsirkin <[email protected]>Cc Michal Privoznik <[email protected]>
Quoted 61 lines
v4 -> v5:
- "migration/savevm: Move more savevm handling intovmstate_save()" -- Extended patch description regarding tracing
- "migration/savevm: Prepare vmdesc json writer inqemu_savevm_state_setup()"
-- Move freeing tomigrate_fd_cleanup()
- "migration/savevm: Allow immutable device state to be migrated early (i.e., before RAM)"
-- "immutable" -> "early_setup"
-- Extend comment
- Added some RBs (thanks!)v3 -> v4:
- First 3 patches:
-- Minimze code changes and simplify
-- Save immutable device state duringqemu_savevm_state_setup()-- Don't use vmsd priorities, use a new flag
-- Split it logically up
- "migration/ram: Factor out check for advised postcopy" -- Don't factor outpostcopy_is_running()
- "virtio-mem: Migrate immutable properties early"
-- Adjust to changed vmsd interface
- "virtio-mem: Proper support for preallocation with migration" -- Drop sanity check invirtio_mem_post_load_early()v2 -> v3:
- New approach/rewrite, drop RB and TB of last patchv1 -> v2:
- Added RBs and Tested-bys
- "virtio-mem: Fail if a memory backend with "prealloc=on" is specified" -- Fail instead of warn
-- Adjust subject/descriptionDavid Hildenbrand (8):
migration/savevm: Move more savevm handling intovmstate_save()migration/savevm: Prepare vmdesc json writer in
qemu_savevm_state_setup()
migration/savevm: Allow immutable device state to be migrated early(i.e., before RAM)migration/vmstate: Introduce
VMSTATE_WITH_TMP_TEST()and
VMSTATE_BITMAP_TEST()
migration/ram: Factor out check for advised postcopy
virtio-mem: Fail if a memory backend with "prealloc=on" is specified
virtio-mem: Migrate immutable properties early
virtio-mem: Proper support for preallocation with migrationhw/core/machine.c | 4 +- hw/virtio/virtio-mem.c | 144 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-mem.h | 8 ++ include/migration/misc.h | 4 +- include/migration/vmstate.h | 28 ++++++- migration/migration.c | 9 +++ migration/migration.h | 4 + migration/ram.c | 8 +- migration/savevm.c | 105 +++++++++++++----------- 9 files changed, 255 insertions(+), 59 deletions(-)Signature · 1 line
2.39.0