UFFD write-tracking migration/snapshots
v11 cover UFFD write-tracking migration/snapshots
This patch series is a kind of 'rethinking' of Denis Plotnikov's ideas he's implemented in his series '[PATCH v0 0/4] migration: add background snapshot'.
Currently the only way to make (external) live VM snapshot is using existing dirty page logging migration mechanism. The main problem is that it tends to produce a lot of page duplicates while running VM goes on updating already saved pages. That leads to the fact that vmstate image size is commonly several times bigger then non-zero part of virtual machine's RSS. Time required to converge RAM migration and the size of snapshot image severely depend on the guest memory write rate, sometimes resulting in unacceptably long snapshot creation time and huge image size.
This series propose a way to solve the aforementioned problems. This is done by using different RAM migration mechanism based on UFFD write protection management introduced in v5.7 kernel. The migration strategy is to 'freeze' guest RAM content using write-protection and iteratively release protection for memory ranges that have already been saved to the migration stream. At the same time we read in pending UFFD write fault events and save those pages out-of-order with higher priority.
How to use:
1. Enable write-tracking migration capability
virsh qemu-monitor-command <domain> --hmp migrate_set_capability.
track-writes-ram on
2. Start the external migration to a file
virsh qemu-monitor-command <domain> --hmp migrate exec:'cat > ./vm_state'
3. Wait for the migration finish and check that the migration has completed. state.
Changes v10->v11:
* 1. Updated commit messages.
Changes v9->v10:
* 1. Fixed commit message for [PATCH v9 1/5].
Changes v8->v9:
* 1. Fixed wrong cover letter subject.
Changes v7->v8:
* 1. Fixed coding style problems to pass checkpatch.
Changes v6->v7:
* 1. Fixed background snapshot on suspended guest: call qemu_system_wakeup_request()
* before stopping VM to make runstate transition valid.
* 2. Disabled dirty page logging and log syn when 'background-snapshot' is enabled.
* 3. Introduced 'userfaultfd-wrlat.py' script to analyze UFFD write fault latencies.
Changes v5->v6:
* 1. Consider possible hot pluggin/unpluggin of memory device - don't use static
* for write-tracking support level in migrate_query_write_tracking(), check
* each time when one tries to enable 'background-snapshot' capability.
Changes v4->v5:
* 1. Refactored util/userfaultfd.c code to support features required by postcopy.
* 2. Introduced checks for host kernel and guest memory backend compatibility
* to 'background-snapshot' branch in migrate_caps_check().
* 3. Switched to using trace_xxx instead of info_report()/error_report() for
* cases when error message must be hidden (probing UFFD-IO) or info may be * really littering output if goes to stderr. * 4 Added RCU_READ_LOCK_GUARDs to the code dealing with RAM block list.
* 5. Added memory_region_ref() for each RAM block being wr-protected.
* 6. Reused qemu_ram_block_from_host() instead of custom RAM block lookup routine.
* 7. Refused from using specific hwaddr/ram_addr_t in favour of void */uint64_t.
* 8. Currently dropped 'linear-scan-rate-limiting' patch. The reason is that
* that choosen criteria for high-latency fault detection (i.e. timestamp of * UFFD event fetch) is not representative enough for this task. * At the moment it looks somehow like premature optimization effort.
* 8. Dropped some unnecessary/unused code.
Andrey Gruzdev (5):
migration: introduce 'background-snapshot' migration capability
migration: introduce UFFD-WP low-level interface helpers
migration: support UFFD write fault processing in ram_save_iterate()
migration: implementation of background snapshot thread
migration: introduce 'userfaultfd-wrlat.py' script
include/exec/memory.h | 8 + include/qemu/userfaultfd.h | 35 ++++ migration/migration.c | 365 ++++++++++++++++++++++++++++++++++- migration/migration.h | 4 + migration/ram.c | 288 ++++++++++++++++++++++++++- migration/ram.h | 6 + migration/savevm.c | 1 - migration/savevm.h | 2 + migration/trace-events | 2 + qapi/migration.json | 7 +- scripts/userfaultfd-wrlat.py | 148 ++++++++++++++ util/meson.build | 1 + util/trace-events | 9 + util/userfaultfd.c | 345 +++++++++++++++++++++++++++++++++ 14 files changed, 1211 insertions(+), 10 deletions(-) create mode 100644 include/qemu/userfaultfd.h create mode 100755 scripts/userfaultfd-wrlat.py create mode 100644 util/userfaultfd.c
Signature · 1 line
2.25.1
PATCH v11 1/5 migration: introduce 'background-snapshot' migration capability
Add new capability to 'qapi/migration.json' schema. Update migrate_caps_check() to validate enabled capability set against introduced one. Perform checks for required kernel features and compatibility with guest memory backends.
--- migration/migration.c | 110 ++++++++++++++++++++++++++++++++++++++++++ migration/migration.h | 1 + migration/ram.c | 21 ++++++++ migration/ram.h | 4 ++ qapi/migration.json | 7 ++- 5 files changed, 142 insertions(+), 1 deletion(-)
migration/migration.c +110 -0
@@ -57,6 +57,7 @@#include "net/announce.h" #include "qemu/queue.h" #include "multifd.h" +#include "sysemu/cpus.h" #ifdef CONFIG_VFIO #include "hw/vfio/vfio-common.h"
@@ -133,6 +134,38 @@ enum mig_rp_message_type {MIG_RP_MSG_MAX }; +/* Migration capabilities set */ +struct MigrateCapsSet { +int size;/* Capability set size */ +MigrationCapability caps[];/* Variadic array of capabilities */ +}; +typedef struct MigrateCapsSet MigrateCapsSet ; + +/* Define and initialize MigrateCapsSet */ +#define INITIALIZE_MIGRATE_CAPS_SET (_name, ...) \ + MigrateCapsSet _name = { \ + .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \ + .caps = { __VA_ARGS__ } \ + } + +/* Background-snapshot compatibility check list */ +static const +INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot, +MIGRATION_CAPABILITY_POSTCOPY_RAM , +MIGRATION_CAPABILITY_DIRTY_BITMAPS , +MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME , +MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE , +MIGRATION_CAPABILITY_RETURN_PATH , +MIGRATION_CAPABILITY_MULTIFD , +MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER , +MIGRATION_CAPABILITY_AUTO_CONVERGE , +MIGRATION_CAPABILITY_RELEASE_RAM , +MIGRATION_CAPABILITY_RDMA_PIN_ALL , +MIGRATION_CAPABILITY_COMPRESS , +MIGRATION_CAPABILITY_XBZRLE , +MIGRATION_CAPABILITY_X_COLO , + MIGRATION_CAPABILITY_VALIDATE_UUID); +/* When we add fault tolerance, we could have several migrations at once. For now we don't need to add dynamic creation of migration */
@@ -1089,6 +1122,33 @@ static void fill_source_migration_info(MigrationInfo *info) info->status = s->state; } +#ifdef CONFIG_LINUX +typedef enum WriteTrackingSupport { +WT_SUPPORT_UNKNOWN =0 , +WT_SUPPORT_ABSENT , +WT_SUPPORT_AVAILABLE , +WT_SUPPORT_COMPATIBLE +}WriteTrackingSupport ; + +static +WriteTrackingSupport migrate_query_write_tracking (void ) +{ +/* Check if kernel supports required UFFD features */ +if (!ram_write_tracking_available ()) { +return WT_SUPPORT_ABSENT ; + } +/* +* Check if current memory configuration is +* compatible with required UFFD features. +*/ +if (!ram_write_tracking_compatible ()) { +return WT_SUPPORT_AVAILABLE ; + } + +return WT_SUPPORT_COMPATIBLE ; +} +#endif /* CONFIG_LINUX */ +/** * @migration_caps_check - check capability validity *
@@ -1150,6 +1210,45 @@ static bool migrate_caps_check(bool *cap_list, } } +if (cap_list[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT ]) { +#ifdef CONFIG_LINUX +WriteTrackingSupport wt_support; +int idx; +/* +* Check if 'background-snapshot' capability is supported by +* host kernel and compatible with guest memory configuration. +*/ + wt_support =migrate_query_write_tracking (); +if (wt_support <WT_SUPPORT_AVAILABLE ) { +error_setg (errp,"Background-snapshot is not supported by host kernel" ); +return false; + } +if (wt_support <WT_SUPPORT_COMPATIBLE ) { +error_setg (errp,"Background-snapshot is not compatible " +"with guest memory configuration" ); +return false; + } + +/* +* Check if there are any migration capabilities +* incompatible with 'background-snapshot'. +*/ +for (idx =0 ; idx < check_caps_background_snapshot.size; idx++) { +int incomp_cap = check_caps_background_snapshot.caps[idx]; +if (cap_list[incomp_cap]) { +error_setg (errp, +"Background-snapshot is not compatible with %s" , +MigrationCapability_str (incomp_cap)); +return false; + } + } +#else +error_setg (errp, +"Background-snapshot is not supported on non-Linux hosts" ); +return false; +#endif + } +return true; }
@@ -2477,6 +2576,15 @@ bool migrate_use_block_incremental(void)return s->parameters.block_incremental; } +bool migrate_background_snapshot (void ) +{ +MigrationState *s; + + s =migrate_get_current (); + +return s->enabled_capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT ]; +} +/* migration thread support */ /* * Something bad happened to the RP stream, mark an error
@@ -3770,6 +3878,8 @@ static Property migration_properties[] = {DEFINE_PROP_MIG_CAP ("x-block" ,MIGRATION_CAPABILITY_BLOCK ),DEFINE_PROP_MIG_CAP ("x-return-path" ,MIGRATION_CAPABILITY_RETURN_PATH ),DEFINE_PROP_MIG_CAP ("x-multifd" ,MIGRATION_CAPABILITY_MULTIFD ), +DEFINE_PROP_MIG_CAP ("x-background-snapshot" , +MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT ), DEFINE_PROP_END_OF_LIST(), };
migration/migration.h +1 -0
@@ -341,6 +341,7 @@ int migrate_compress_wait_thread(void);int migrate_decompress_threads (void );bool migrate_use_events (void );bool migrate_postcopy_blocktime (void ); +bool migrate_background_snapshot (void );/* Sending on the return path - generic and then for each message type */ void migrate_send_rp_shut(MigrationIncomingState *mis,
migration/ram.c +21 -0
@@ -3788,6 +3788,27 @@ static int ram_resume_prepare(MigrationState *s, void *opaque)return 0 ; } +/* ram_write_tracking_available: check if kernel supports required UFFD features +* +* Returns true if supports, false otherwise +*/ +bool ram_write_tracking_available (void ) +{ +/* TODO: implement */ +return false; +} + +/* ram_write_tracking_compatible: check if guest configuration is +* compatible with 'write-tracking' +* +* Returns true if compatible, false otherwise +*/ +bool ram_write_tracking_compatible (void ) +{ +/* TODO: implement */ +return false; +} +static SaveVMHandlers savevm_ram_handlers = { .save_setup = ram_save_setup, .save_live_iterate = ram_save_iterate,
migration/ram.h +4 -0
@@ -79,4 +79,8 @@ void colo_flush_ram_cache(void);void colo_release_ram_cache (void );void colo_incoming_start_dirty_log (void ); +/* Background snapshot */ +bool ram_write_tracking_available (void ); +bool ram_write_tracking_compatible (void ); +#endif
qapi/migration.json +6 -1
@@ -442,6 +442,11 @@ # @validate-uuid: Send the UUID of the source to allow the destination # to ensure it is the same. (since 4.2) # +# @background-snapshot: If enabled, the migration stream will be a snapshot +# of the VM exactly at the point when the migration +# procedure starts. The VM RAM is saved with running VM. +# (since 6.0) +# # Since: 1.2 ## { 'enum': 'MigrationCapability',
@@ -449,7 +454,7 @@ 'compress', 'events', 'postcopy-ram', 'x-colo', 'release-ram', 'block', 'return-path', 'pause-before-switchover', 'multifd', 'dirty-bitmaps', 'postcopy-blocktime', 'late-block-activate', - 'x-ignore-shared', 'validate-uuid' ] } + 'x-ignore-shared', 'validate-uuid', 'background-snapshot'] } ## # @MigrationCapabilityStatus:
Signature · 1 line
2.25.1
PATCH v11 2/5 migration: introduce UFFD-WP low-level interface helpers
In reply to Andrey Gruzdev viaGlue code to the userfaultfd kernel implementation. Querying feature support, createing file descriptor, feature control, memory region registration, IOCTLs on registered registered regions.
--- include/exec/memory.h | 1 + include/qemu/userfaultfd.h | 35 ++++ util/meson.build | 1 + util/trace-events | 9 + util/userfaultfd.c | 345 +++++++++++++++++++++++++++++++++++++ 5 files changed, 391 insertions(+) create mode 100644 include/qemu/userfaultfd.h create mode 100644 util/userfaultfd.c
include/exec/memory.h +1 -0
@@ -149,6 +149,7 @@ typedef struct IOMMUTLBEvent {/* RAM is a persistent kind memory */ #define RAM_PMEM (1 << 5) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,
include/qemu/userfaultfd.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Linux UFFD-WP support +* +* Copyright Virtuozzo GmbH, 2020 +* +* Authors: +* Andrey Gruzdev <[email protected]> +* +* 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 USERFAULTFD_H +#define USERFAULTFD_H + +#include "qemu/osdep.h" +#include "exec/hwaddr.h" +#include <linux/userfaultfd.h> + +int uffd_query_features (uint64_t *features); +int uffd_create_fd (uint64_t features,bool non_blocking); +void uffd_close_fd (int uffd_fd); +int uffd_register_memory (int uffd_fd,void *addr,uint64_t length, +uint64_t mode,uint64_t *ioctls); +int uffd_unregister_memory (int uffd_fd,void *addr,uint64_t length); +int uffd_change_protection (int uffd_fd,void *addr,uint64_t length, +bool wp,bool dont_wake); +int uffd_copy_page (int uffd_fd,void *dst_addr,void *src_addr, +uint64_t length,bool dont_wake); +int uffd_zero_page (int uffd_fd,void *addr,uint64_t length,bool dont_wake); +int uffd_wakeup (int uffd_fd,void *addr,uint64_t length); +int uffd_read_events (int uffd_fd,struct uffd_msg *msgs,int count); +bool uffd_poll_events (int uffd_fd,int tmo); + +#endif /* USERFAULTFD_H */
util/meson.build +1 -0
@@ -50,6 +50,7 @@ endif if have_system util_ss.add(when: 'CONFIG_GIO', if_true: [files('dbus.c'), gio]) + util_ss.add(when: 'CONFIG_LINUX', if_true: files('userfaultfd.c')) endif if have_block
util/trace-events +9 -0
@@ -91,3 +91,12 @@ qemu_vfio_pci_read_config(void *buf, int ofs, int size, uint64_t region_ofs, uin qemu_vfio_pci_write_config(void *buf, int ofs, int size, uint64_t region_ofs, uint64_t region_size) "write cfg ptr %p ofs 0x%x size 0x%x (region addr 0x%"PRIx64" size 0x%"PRIx64")" qemu_vfio_region_info(const char *desc, uint64_t region_ofs, uint64_t region_size, uint32_t cap_offset) "region '%s' addr 0x%"PRIx64" size 0x%"PRIx64" cap_ofs 0x%"PRIx32 qemu_vfio_pci_map_bar(int index, uint64_t region_ofs, uint64_t region_size, int ofs, void *host) "map region bar#%d addr 0x%"PRIx64" size 0x%"PRIx64" ofs 0x%x host %p" + +#userfaultfd.c +uffd_query_features_nosys(int err) "errno: %i" +uffd_query_features_api_failed(int err) "errno: %i" +uffd_create_fd_nosys(int err) "errno: %i" +uffd_create_fd_api_failed(int err) "errno: %i" +uffd_create_fd_api_noioctl(uint64_t ioctl_req, uint64_t ioctl_supp) "ioctl_req: 0x%" PRIx64 "ioctl_supp: 0x%" PRIx64 +uffd_register_memory_failed(void *addr, uint64_t length, uint64_t mode, int err) "addr: %p length: %" PRIu64 " mode: 0x%" PRIx64 " errno: %i" +uffd_unregister_memory_failed(void *addr, uint64_t length, int err) "addr: %p length: %" PRIu64 " errno: %i"
util/userfaultfd.c new file +345 -0
345 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v11 3/5 migration: support UFFD write fault processing in ram_save_iterate()
In reply to Andrey Gruzdev viaIn this particular implementation the same single migration thread is responsible for both normal linear dirty page migration and procesing UFFD page fault events.
Processing write faults includes reading UFFD file descriptor, finding respective RAM block and saving faulting page to the migration stream. After page has been saved, write protection can be removed. Since asynchronous version of qemu_put_buffer() is expected to be used to save pages, we also have to flush migraion stream prior to un-protecting saved memory range.
Write protection is being removed for any previously protected memory chunk that has hit the migration stream. That's valid for pages from linear page scan along with write fault pages.
--- include/exec/memory.h | 7 ++ migration/ram.c | 269 +++++++++++++++++++++++++++++++++++++++-- migration/ram.h | 2 + migration/trace-events | 2 + 4 files changed, 272 insertions(+), 8 deletions(-)
include/exec/memory.h +7 -0
@@ -150,6 +150,13 @@ typedef struct IOMMUTLBEvent {#define RAM_PMEM (1 << 5) +/* +* UFFDIO_WRITEPROTECT is used on this RAMBlock to +* support 'write-tracking' migration type. +* Implies ram_state->ram_wt_enabled. +*/ +#define RAM_UF_WRITEPROTECT (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,
migration/ram.c +261 -8
333 lines not on this page · raw message
migration/ram.h +2 -0
@@ -82,5 +82,7 @@ void colo_incoming_start_dirty_log(void);/* Background snapshot */ bool ram_write_tracking_available (void );bool ram_write_tracking_compatible (void ); +int ram_write_tracking_start (void ); +void ram_write_tracking_stop (void );#endif
migration/trace-events +2 -0
@@ -111,6 +111,8 @@ save_xbzrle_page_skipping(void) "" save_xbzrle_page_overflow(void) "" ram_save_iterate_big_wait(uint64_t milliconds, int iterations) "big wait: %" PRIu64 " milliseconds, %d iterations" ram_load_complete(int ret, uint64_t seq_iter) "exit_code %d seq iteration %" PRIu64 +ram_write_tracking_ramblock_start(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" +ram_write_tracking_ramblock_stop(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" # multifd.c multifd_new_send_channel_async(uint8_t id) "channel %d"
Signature · 1 line
2.25.1
Re: [PATCH v11 3/5] migration: support UFFD write fault processing in ram_save_iterate()
Quoted 11 lines · * Andrey Gruzdev ([email protected]) wrote:
In this particular implementation the same single migration thread is responsible for both normal linear dirty page migration and procesing UFFD page fault events.
Processing write faults includes reading UFFD file descriptor, finding respective RAM block and saving faulting page to the migration stream. After page has been saved, write protection can be removed. Since asynchronous version of
qemu_put_buffer()is expected to be used to save pages, we also have to flush migraion stream prior to un-protecting saved memory range.
Quoted 6 lines
Write protection is being removed for any previously protected memory chunk that has hit the migration stream. That's valid for pages from linear page scan along with write fault pages.
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>
I think this is OK, so:
I think I'd have preferred to have kept the #ifdef LINUX's out of there, and I suspect using the opaque's for hte pre/post hook is overly complex; but other wise OK.
Dave
Quoted 400 lines
--- include/exec/memory.h | 7 ++ migration/ram.c | 269 +++++++++++++++++++++++++++++++++++++++-- migration/ram.h | 2 + migration/trace-events | 2 + 4 files changed, 272 insertions(+), 8 deletions(-)include/exec/memory.h +7
-0@@ -150,6 +150,13 @@ typedef struct IOMMUTLBEvent {#define RAM_PMEM (1 << 5) +/* +* UFFDIO_WRITEPROTECT is used on this RAMBlock to +* support 'write-tracking' migration type. +* Implies ram_state->ram_wt_enabled. +*/ +#define RAM_UF_WRITEPROTECT (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,migration/ram.c +261
-8@@ -56,6 +56,11 @@#include "savevm.h" #include "qemu/iov.h" #include "multifd.h" +#include "sysemu/runstate.h" + +#ifdef CONFIG_LINUX +#include "qemu/userfaultfd.h" +#endif /***********************************************************/ /* ram save/restore */ @@ -298,6 +303,8 @@ struct RAMSrcPageRequest {struct RAMState {/* QEMUFile used for this migration */ QEMUFile *f; +/* UFFD file descriptor, used in 'write-tracking' migration */ +int uffdio_fd;/* Last block that we have visited searching for dirty pages */ RAMBlock *last_seen_block;/* Last block from where we have sent data */ @@ -1434,6 +1441,40 @@ static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)return block; } +#ifdef CONFIG_LINUX +/** +* poll_fault_page: try to get next UFFD write fault page and, if pending fault +* is found, return RAM block pointer and page offset +* +* Returns pointer to the RAMBlock containing faulting page, +* NULL if no write faults are pending +* +* @rs: current RAM state +* @offset: page offset from the beginning of the block +*/ +static RAMBlock *poll_fault_page (RAMState *rs,ram_addr_t *offset) +{ +struct uffd_msg uffd_msg; +void *page_address; +RAMBlock *bs; +int res; + +if (!migrate_background_snapshot ()) { +return NULL ; + } + + res =uffd_read_events (rs->uffdio_fd, &uffd_msg,1 ); +if (res <=0 ) { +return NULL ; + } + + page_address = (void *) uffd_msg.arg.pagefault.address; + bs =qemu_ram_block_from_host (page_address, false, offset); +assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); +return bs; +} +#endif /* CONFIG_LINUX */ +/** * get_queued_page: unqueue a page from the postcopy requests * @@ -1473,6 +1514,16 @@ static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) }while (block && !dirty); +#ifdef CONFIG_LINUX +if (!block) { +/* +* Poll write faults too if background snapshot is enabled; that's +* when we have vcpus got blocked by the write protected pages. +*/ + block =poll_fault_page (rs, &offset); + } +#endif /* CONFIG_LINUX */ +if (block) {/* * As soon as we start servicing pages out of order, then we have @@ -1746,6 +1797,53 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,return pages; } +/** +* ram_save_host_page_pre: ram_save_host_page() pre-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: pointer to receive opaque context value +*/ +static inline +void ram_save_host_page_pre (RAMState *rs,PageSearchStatus *pss,void **opaque) +{ + *(unsigned long *) opaque = pss->page; +} + +/** +* ram_save_host_page_post: ram_save_host_page() post-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: opaque context value +* @res_override: pointer to the return value of ram_save_host_page(), +* overwritten in case of an error +*/ +static void ram_save_host_page_post (RAMState *rs,PageSearchStatus *pss, +void *opaque,int *res_override) +{ +#ifdef CONFIG_LINUX +/* Check if page is from UFFD-managed region. */ +if (pss->block->flags &RAM_UF_WRITEPROTECT ) { +unsigned long page_from = (unsigned long ) opaque; + +void *page_address = pss->block->host + (page_from <<TARGET_PAGE_BITS ); +uint64_t run_length = (pss->page - page_from +1 ) <<TARGET_PAGE_BITS ; +int res; + +/* Flush async buffers before un-protect. */ +qemu_fflush (rs->f); +/* Un-protect memory range. */ + res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, + false, false); +/* We don't want to override existing error from ram_save_host_page(). */ +if (res <0 && *res_override >=0 ) { + *res_override = res; + } + } +#endif /* CONFIG_LINUX */ +} +/** * ram_find_and_save_block: finds a dirty page and sends it to f * @@ -1790,7 +1888,11 @@ static int ram_find_and_save_block(RAMState *rs, bool last_stage) }if (found) { +void *opaque; + +ram_save_host_page_pre (rs, &pss, &opaque); pages =ram_save_host_page (rs, &pss, last_stage); +ram_save_host_page_post (rs, &pss, opaque, &pages); } }while (!pages && again);@@ -1880,10 +1982,13 @@ static void ram_save_cleanup(void *opaque)RAMState **rsp = opaque;RAMBlock *block; -/* caller have hold iothread lock or is in a bh, so there is -* no writing race against the migration bitmap -*/ -memory_global_dirty_log_stop (); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +/* caller have hold iothread lock or is in a bh, so there is +* no writing race against the migration bitmap +*/ +memory_global_dirty_log_stop (); + }RAMBLOCK_FOREACH_NOT_IGNORED (block) {g_free (block->clear_bmap);@@ -2343,8 +2448,11 @@ static void ram_init_bitmaps(RAMState *rs) WITH_RCU_READ_LOCK_GUARD() {ram_list_init_bitmaps (); -memory_global_dirty_log_start (); -migration_bitmap_sync_precopy (rs); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +memory_global_dirty_log_start (); +migration_bitmap_sync_precopy (rs); + } }qemu_mutex_unlock_ramlist ();qemu_mutex_unlock_iothread ();@@ -3794,7 +3902,14 @@ static int ram_resume_prepare(MigrationState *s, void *opaque)*/ bool ram_write_tracking_available (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +uint64_t uffd_features; +int res; + + res =uffd_query_features (&uffd_features); +return (res ==0 && + (uffd_features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ) !=0 ); +#endif return false; }@@ -3805,10 +3920,148 @@ bool ram_write_tracking_available(void)*/ bool ram_write_tracking_compatible (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +const uint64_t uffd_ioctls_mask =BIT (_UFFDIO_WRITEPROTECT); +int uffd_fd; +RAMBlock *bs; +bool ret = false; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , false); +if (uffd_fd <0 ) { +return false; + } + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +uint64_t uffd_ioctls; + +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } +/* Try to register block memory via UFFD-IO to track writes */ +if (uffd_register_memory (uffd_fd, bs->host, bs->max_length, +UFFDIO_REGISTER_MODE_WP , &uffd_ioctls)) { + goto out; + } +if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { + goto out; + } + } + ret = true; + +out: +uffd_close_fd (uffd_fd); +return ret; +#endif return false; } +/* +* ram_write_tracking_start: start UFFD-WP memory tracking +* +* Returns 0 for success or negative value in case of error +*/ +int ram_write_tracking_start (void ) +{ +#ifdef CONFIG_LINUX +int uffd_fd; +RAMState *rs = ram_state; +RAMBlock *bs; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , true); +if (uffd_fd <0 ) { +return uffd_fd; + } + rs->uffdio_fd = uffd_fd; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } + +/* Register block memory with UFFD to track writes */ +if (uffd_register_memory (rs->uffdio_fd, bs->host, + bs->max_length,UFFDIO_REGISTER_MODE_WP ,NULL )) { + goto fail; + } +/* Apply UFFD write protection to the block memory range */ +if (uffd_change_protection (rs->uffdio_fd, bs->host, + bs->max_length, true, false)) { + goto fail; + } + bs->flags |=RAM_UF_WRITEPROTECT ; +memory_region_ref (bs->mr); + +trace_ram_write_tracking_ramblock_start (bs->idstr, bs->page_size, + bs->host, bs->max_length); + } + +return 0 ; + +fail: +error_report ("ram_write_tracking_start() failed: restoring initial memory state" ); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* +* In case some memory block failed to be write-protected +* remove protection and unregister all succeeded RAM blocks +*/ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +uffd_close_fd (uffd_fd); +#endif /* CONFIG_LINUX */ + rs->uffdio_fd =-1 ; +return -1 ; +} + +/** +* ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection +*/ +void ram_write_tracking_stop (void ) +{ +#ifdef CONFIG_LINUX +RAMState *rs = ram_state; +RAMBlock *bs; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* Remove protection and unregister all affected RAM blocks */ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); + +trace_ram_write_tracking_ramblock_stop (bs->idstr, bs->page_size, + bs->host, bs->max_length); + +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +/* Finally close UFFD file descriptor */ +uffd_close_fd (rs->uffdio_fd); + rs->uffdio_fd =-1 ; +#endif /* CONFIG_LINUX */ +} +static SaveVMHandlers savevm_ram_handlers = { .save_setup = ram_save_setup, .save_live_iterate = ram_save_iterate,migration/ram.h +2
-0@@ -82,5 +82,7 @@ void colo_incoming_start_dirty_log(void);/* Background snapshot */ bool ram_write_tracking_available (void );bool ram_write_tracking_compatible (void ); +int ram_write_tracking_start (void ); +void ram_write_tracking_stop (void );#endif migration/trace-events +2
-0@@ -111,6 +111,8 @@ save_xbzrle_page_skipping(void) "" save_xbzrle_page_overflow(void) "" ram_save_iterate_big_wait(uint64_t milliconds, int iterations) "big wait: %" PRIu64 " milliseconds, %d iterations" ram_load_complete(int ret, uint64_t seq_iter) "exit_code %d seq iteration %" PRIu64 +ram_write_tracking_ramblock_start(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" +ram_write_tracking_ramblock_stop(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" # multifd.c multifd_new_send_channel_async(uint8_t id) "channel %d"Signature · 1 line
2.25.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v11 3/5] migration: support UFFD write fault processing in ram_save_iterate()
Quoted 20 lines · Dr. David Alan Gilbert
Quoted 11 lines · * Andrey Gruzdev ([email protected]) wrote:
In this particular implementation the same single migration thread is responsible for both normal linear dirty page migration and procesing UFFD page fault events.
Processing write faults includes reading UFFD file descriptor, finding respective RAM block and saving faulting page to the migration stream. After page has been saved, write protection can be removed. Since asynchronous version of
qemu_put_buffer()is expected to be used to save pages, we also have to flush migraion stream prior to un-protecting saved memory range.Quoted 6 lines
Write protection is being removed for any previously protected memory chunk that has hit the migration stream. That's valid for pages from linear page scan along with write fault pages.
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>I think this is OK, so:
Quoted 7 lines
Reviewed-by Dr. David Alan Gilbert <[email protected]>I think I'd have preferred to have kept the #ifdef LINUX's out of there, and I suspect using the opaque's for hte pre/post hook is overly complex; but other wise OK.
Dave
Mm, I think it's impossible to completely move #ifdef LINUX out of there, but I suspect all #ifdef LINUX code can be moved to a single place with stubs for the #else case.. Suppose it would be better then now. For pre/post hooks - not too complex, but for single #ifdef section it really looks better to move this stuff to ram_save_host_page().
Andrey
Quoted 400 lines
Quoted 400 lines
--- include/exec/memory.h | 7 ++ migration/ram.c | 269 +++++++++++++++++++++++++++++++++++++++-- migration/ram.h | 2 + migration/trace-events | 2 + 4 files changed, 272 insertions(+), 8 deletions(-)include/exec/memory.h +7
-0@@ -150,6 +150,13 @@ typedef struct IOMMUTLBEvent {#define RAM_PMEM (1 << 5) +/* +* UFFDIO_WRITEPROTECT is used on this RAMBlock to +* support 'write-tracking' migration type. +* Implies ram_state->ram_wt_enabled. +*/ +#define RAM_UF_WRITEPROTECT (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,migration/ram.c +261
-8@@ -56,6 +56,11 @@#include "savevm.h" #include "qemu/iov.h" #include "multifd.h" +#include "sysemu/runstate.h" + +#ifdef CONFIG_LINUX +#include "qemu/userfaultfd.h" +#endif /***********************************************************/ /* ram save/restore */ @@ -298,6 +303,8 @@ struct RAMSrcPageRequest {struct RAMState {/* QEMUFile used for this migration */ QEMUFile *f; +/* UFFD file descriptor, used in 'write-tracking' migration */ +int uffdio_fd;/* Last block that we have visited searching for dirty pages */ RAMBlock *last_seen_block;/* Last block from where we have sent data */ @@ -1434,6 +1441,40 @@ static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)return block; } +#ifdef CONFIG_LINUX +/** +* poll_fault_page: try to get next UFFD write fault page and, if pending fault +* is found, return RAM block pointer and page offset +* +* Returns pointer to the RAMBlock containing faulting page, +* NULL if no write faults are pending +* +* @rs: current RAM state +* @offset: page offset from the beginning of the block +*/ +static RAMBlock *poll_fault_page (RAMState *rs,ram_addr_t *offset) +{ +struct uffd_msg uffd_msg; +void *page_address; +RAMBlock *bs; +int res; + +if (!migrate_background_snapshot ()) { +return NULL ; + } + + res =uffd_read_events (rs->uffdio_fd, &uffd_msg,1 ); +if (res <=0 ) { +return NULL ; + } + + page_address = (void *) uffd_msg.arg.pagefault.address; + bs =qemu_ram_block_from_host (page_address, false, offset); +assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); +return bs; +} +#endif /* CONFIG_LINUX */ +/** * get_queued_page: unqueue a page from the postcopy requests * @@ -1473,6 +1514,16 @@ static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) }while (block && !dirty); +#ifdef CONFIG_LINUX +if (!block) { +/* +* Poll write faults too if background snapshot is enabled; that's +* when we have vcpus got blocked by the write protected pages. +*/ + block =poll_fault_page (rs, &offset); + } +#endif /* CONFIG_LINUX */ +if (block) {/* * As soon as we start servicing pages out of order, then we have @@ -1746,6 +1797,53 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,return pages; } +/** +* ram_save_host_page_pre: ram_save_host_page() pre-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: pointer to receive opaque context value +*/ +static inline +void ram_save_host_page_pre (RAMState *rs,PageSearchStatus *pss,void **opaque) +{ + *(unsigned long *) opaque = pss->page; +} + +/** +* ram_save_host_page_post: ram_save_host_page() post-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: opaque context value +* @res_override: pointer to the return value of ram_save_host_page(), +* overwritten in case of an error +*/ +static void ram_save_host_page_post (RAMState *rs,PageSearchStatus *pss, +void *opaque,int *res_override) +{ +#ifdef CONFIG_LINUX +/* Check if page is from UFFD-managed region. */ +if (pss->block->flags &RAM_UF_WRITEPROTECT ) { +unsigned long page_from = (unsigned long ) opaque; + +void *page_address = pss->block->host + (page_from <<TARGET_PAGE_BITS ); +uint64_t run_length = (pss->page - page_from +1 ) <<TARGET_PAGE_BITS ; +int res; + +/* Flush async buffers before un-protect. */ +qemu_fflush (rs->f); +/* Un-protect memory range. */ + res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, + false, false); +/* We don't want to override existing error from ram_save_host_page(). */ +if (res <0 && *res_override >=0 ) { + *res_override = res; + } + } +#endif /* CONFIG_LINUX */ +} +/** * ram_find_and_save_block: finds a dirty page and sends it to f * @@ -1790,7 +1888,11 @@ static int ram_find_and_save_block(RAMState *rs, bool last_stage) }if (found) { +void *opaque; + +ram_save_host_page_pre (rs, &pss, &opaque); pages =ram_save_host_page (rs, &pss, last_stage); +ram_save_host_page_post (rs, &pss, opaque, &pages); } }while (!pages && again);@@ -1880,10 +1982,13 @@ static void ram_save_cleanup(void *opaque)RAMState **rsp = opaque;RAMBlock *block; -/* caller have hold iothread lock or is in a bh, so there is -* no writing race against the migration bitmap -*/ -memory_global_dirty_log_stop (); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +/* caller have hold iothread lock or is in a bh, so there is +* no writing race against the migration bitmap +*/ +memory_global_dirty_log_stop (); + }RAMBLOCK_FOREACH_NOT_IGNORED (block) {g_free (block->clear_bmap);@@ -2343,8 +2448,11 @@ static void ram_init_bitmaps(RAMState *rs) WITH_RCU_READ_LOCK_GUARD() {ram_list_init_bitmaps (); -memory_global_dirty_log_start (); -migration_bitmap_sync_precopy (rs); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +memory_global_dirty_log_start (); +migration_bitmap_sync_precopy (rs); + } }qemu_mutex_unlock_ramlist ();qemu_mutex_unlock_iothread ();@@ -3794,7 +3902,14 @@ static int ram_resume_prepare(MigrationState *s, void *opaque)*/ bool ram_write_tracking_available (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +uint64_t uffd_features; +int res; + + res =uffd_query_features (&uffd_features); +return (res ==0 && + (uffd_features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ) !=0 ); +#endif return false; }@@ -3805,10 +3920,148 @@ bool ram_write_tracking_available(void)*/ bool ram_write_tracking_compatible (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +const uint64_t uffd_ioctls_mask =BIT (_UFFDIO_WRITEPROTECT); +int uffd_fd; +RAMBlock *bs; +bool ret = false; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , false); +if (uffd_fd <0 ) { +return false; + } + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +uint64_t uffd_ioctls; + +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } +/* Try to register block memory via UFFD-IO to track writes */ +if (uffd_register_memory (uffd_fd, bs->host, bs->max_length, +UFFDIO_REGISTER_MODE_WP , &uffd_ioctls)) { + goto out; + } +if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { + goto out; + } + } + ret = true; + +out: +uffd_close_fd (uffd_fd); +return ret; +#endif return false; } +/* +* ram_write_tracking_start: start UFFD-WP memory tracking +* +* Returns 0 for success or negative value in case of error +*/ +int ram_write_tracking_start (void ) +{ +#ifdef CONFIG_LINUX +int uffd_fd; +RAMState *rs = ram_state; +RAMBlock *bs; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , true); +if (uffd_fd <0 ) { +return uffd_fd; + } + rs->uffdio_fd = uffd_fd; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } + +/* Register block memory with UFFD to track writes */ +if (uffd_register_memory (rs->uffdio_fd, bs->host, + bs->max_length,UFFDIO_REGISTER_MODE_WP ,NULL )) { + goto fail; + } +/* Apply UFFD write protection to the block memory range */ +if (uffd_change_protection (rs->uffdio_fd, bs->host, + bs->max_length, true, false)) { + goto fail; + } + bs->flags |=RAM_UF_WRITEPROTECT ; +memory_region_ref (bs->mr); + +trace_ram_write_tracking_ramblock_start (bs->idstr, bs->page_size, + bs->host, bs->max_length); + } + +return 0 ; + +fail: +error_report ("ram_write_tracking_start() failed: restoring initial memory state" ); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* +* In case some memory block failed to be write-protected +* remove protection and unregister all succeeded RAM blocks +*/ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +uffd_close_fd (uffd_fd); +#endif /* CONFIG_LINUX */ + rs->uffdio_fd =-1 ; +return -1 ; +} + +/** +* ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection +*/ +void ram_write_tracking_stop (void ) +{ +#ifdef CONFIG_LINUX +RAMState *rs = ram_state; +RAMBlock *bs; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* Remove protection and unregister all affected RAM blocks */ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); + +trace_ram_write_tracking_ramblock_stop (bs->idstr, bs->page_size, + bs->host, bs->max_length); + +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +/* Finally close UFFD file descriptor */ +uffd_close_fd (rs->uffdio_fd); + rs->uffdio_fd =-1 ; +#endif /* CONFIG_LINUX */ +} +static SaveVMHandlers savevm_ram_handlers = { .save_setup = ram_save_setup, .save_live_iterate = ram_save_iterate,migration/ram.h +2
-0@@ -82,5 +82,7 @@ void colo_incoming_start_dirty_log(void);/* Background snapshot */ bool ram_write_tracking_available (void );bool ram_write_tracking_compatible (void ); +int ram_write_tracking_start (void ); +void ram_write_tracking_stop (void );#endif migration/trace-events +2
-0@@ -111,6 +111,8 @@ save_xbzrle_page_skipping(void) "" save_xbzrle_page_overflow(void) "" ram_save_iterate_big_wait(uint64_t milliconds, int iterations) "big wait: %" PRIu64 " milliseconds, %d iterations" ram_load_complete(int ret, uint64_t seq_iter) "exit_code %d seq iteration %" PRIu64 +ram_write_tracking_ramblock_start(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" +ram_write_tracking_ramblock_stop(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" # multifd.c multifd_new_send_channel_async(uint8_t id) "channel %d"Signature · 1 line
2.25.1
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comRe: [PATCH v11 3/5] migration: support UFFD write fault processing in ram_save_iterate()
Quoted 27 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 26 lines · Dr. David Alan Gilbert
Quoted 11 lines · * Andrey Gruzdev ([email protected]) wrote:
In this particular implementation the same single migration thread is responsible for both normal linear dirty page migration and procesing UFFD page fault events.
Processing write faults includes reading UFFD file descriptor, finding respective RAM block and saving faulting page to the migration stream. After page has been saved, write protection can be removed. Since asynchronous version of
qemu_put_buffer()is expected to be used to save pages, we also have to flush migraion stream prior to un-protecting saved memory range.Quoted 6 lines
Write protection is being removed for any previously protected memory chunk that has hit the migration stream. That's valid for pages from linear page scan along with write fault pages.
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>I think this is OK, so:
Reviewed-by Dr. David Alan Gilbert <[email protected]>I think I'd have preferred to have kept the #ifdef LINUX's out of there, and I suspect using the opaque's for hte pre/post hook is overly complex; but other wise OK.
Quoted 7 lines
Quoted 1 line
Dave
Mm, I think it's impossible to completely move #ifdef LINUX out of there, but I suspect all #ifdef LINUX code can be moved to a single place with stubs for the #else case.. Suppose it would be better then now. For pre/post hooks - not too complex, but for single #ifdef section it really looks better to move this stuff to
ram_save_host_page().
An add on patch to consolidate the ifdef'ing to one place would be interesting; I'll look at taking this series as is (other than the thread name fix up etc) when I get around to my next pull (although I need to review some other peoples patches first!)
Dave
Quoted 392 lines
Andrey
Quoted 384 lines
Quoted 384 lines
--- include/exec/memory.h | 7 ++ migration/ram.c | 269 +++++++++++++++++++++++++++++++++++++++-- migration/ram.h | 2 + migration/trace-events | 2 + 4 files changed, 272 insertions(+), 8 deletions(-)include/exec/memory.h +7
-0@@ -150,6 +150,13 @@ typedef struct IOMMUTLBEvent {#define RAM_PMEM (1 << 5) +/* +* UFFDIO_WRITEPROTECT is used on this RAMBlock to +* support 'write-tracking' migration type. +* Implies ram_state->ram_wt_enabled. +*/ +#define RAM_UF_WRITEPROTECT (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,migration/ram.c +261
-8@@ -56,6 +56,11 @@#include "savevm.h" #include "qemu/iov.h" #include "multifd.h" +#include "sysemu/runstate.h" + +#ifdef CONFIG_LINUX +#include "qemu/userfaultfd.h" +#endif /***********************************************************/ /* ram save/restore */ @@ -298,6 +303,8 @@ struct RAMSrcPageRequest {struct RAMState {/* QEMUFile used for this migration */ QEMUFile *f; +/* UFFD file descriptor, used in 'write-tracking' migration */ +int uffdio_fd;/* Last block that we have visited searching for dirty pages */ RAMBlock *last_seen_block;/* Last block from where we have sent data */ @@ -1434,6 +1441,40 @@ static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)return block; } +#ifdef CONFIG_LINUX +/** +* poll_fault_page: try to get next UFFD write fault page and, if pending fault +* is found, return RAM block pointer and page offset +* +* Returns pointer to the RAMBlock containing faulting page, +* NULL if no write faults are pending +* +* @rs: current RAM state +* @offset: page offset from the beginning of the block +*/ +static RAMBlock *poll_fault_page (RAMState *rs,ram_addr_t *offset) +{ +struct uffd_msg uffd_msg; +void *page_address; +RAMBlock *bs; +int res; + +if (!migrate_background_snapshot ()) { +return NULL ; + } + + res =uffd_read_events (rs->uffdio_fd, &uffd_msg,1 ); +if (res <=0 ) { +return NULL ; + } + + page_address = (void *) uffd_msg.arg.pagefault.address; + bs =qemu_ram_block_from_host (page_address, false, offset); +assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); +return bs; +} +#endif /* CONFIG_LINUX */ +/** * get_queued_page: unqueue a page from the postcopy requests * @@ -1473,6 +1514,16 @@ static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) }while (block && !dirty); +#ifdef CONFIG_LINUX +if (!block) { +/* +* Poll write faults too if background snapshot is enabled; that's +* when we have vcpus got blocked by the write protected pages. +*/ + block =poll_fault_page (rs, &offset); + } +#endif /* CONFIG_LINUX */ +if (block) {/* * As soon as we start servicing pages out of order, then we have @@ -1746,6 +1797,53 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,return pages; } +/** +* ram_save_host_page_pre: ram_save_host_page() pre-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: pointer to receive opaque context value +*/ +static inline +void ram_save_host_page_pre (RAMState *rs,PageSearchStatus *pss,void **opaque) +{ + *(unsigned long *) opaque = pss->page; +} + +/** +* ram_save_host_page_post: ram_save_host_page() post-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: opaque context value +* @res_override: pointer to the return value of ram_save_host_page(), +* overwritten in case of an error +*/ +static void ram_save_host_page_post (RAMState *rs,PageSearchStatus *pss, +void *opaque,int *res_override) +{ +#ifdef CONFIG_LINUX +/* Check if page is from UFFD-managed region. */ +if (pss->block->flags &RAM_UF_WRITEPROTECT ) { +unsigned long page_from = (unsigned long ) opaque; + +void *page_address = pss->block->host + (page_from <<TARGET_PAGE_BITS ); +uint64_t run_length = (pss->page - page_from +1 ) <<TARGET_PAGE_BITS ; +int res; + +/* Flush async buffers before un-protect. */ +qemu_fflush (rs->f); +/* Un-protect memory range. */ + res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, + false, false); +/* We don't want to override existing error from ram_save_host_page(). */ +if (res <0 && *res_override >=0 ) { + *res_override = res; + } + } +#endif /* CONFIG_LINUX */ +} +/** * ram_find_and_save_block: finds a dirty page and sends it to f * @@ -1790,7 +1888,11 @@ static int ram_find_and_save_block(RAMState *rs, bool last_stage) }if (found) { +void *opaque; + +ram_save_host_page_pre (rs, &pss, &opaque); pages =ram_save_host_page (rs, &pss, last_stage); +ram_save_host_page_post (rs, &pss, opaque, &pages); } }while (!pages && again);@@ -1880,10 +1982,13 @@ static void ram_save_cleanup(void *opaque)RAMState **rsp = opaque;RAMBlock *block; -/* caller have hold iothread lock or is in a bh, so there is -* no writing race against the migration bitmap -*/ -memory_global_dirty_log_stop (); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +/* caller have hold iothread lock or is in a bh, so there is +* no writing race against the migration bitmap +*/ +memory_global_dirty_log_stop (); + }RAMBLOCK_FOREACH_NOT_IGNORED (block) {g_free (block->clear_bmap);@@ -2343,8 +2448,11 @@ static void ram_init_bitmaps(RAMState *rs) WITH_RCU_READ_LOCK_GUARD() {ram_list_init_bitmaps (); -memory_global_dirty_log_start (); -migration_bitmap_sync_precopy (rs); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +memory_global_dirty_log_start (); +migration_bitmap_sync_precopy (rs); + } }qemu_mutex_unlock_ramlist ();qemu_mutex_unlock_iothread ();@@ -3794,7 +3902,14 @@ static int ram_resume_prepare(MigrationState *s, void *opaque)*/ bool ram_write_tracking_available (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +uint64_t uffd_features; +int res; + + res =uffd_query_features (&uffd_features); +return (res ==0 && + (uffd_features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ) !=0 ); +#endif return false; }@@ -3805,10 +3920,148 @@ bool ram_write_tracking_available(void)*/ bool ram_write_tracking_compatible (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +const uint64_t uffd_ioctls_mask =BIT (_UFFDIO_WRITEPROTECT); +int uffd_fd; +RAMBlock *bs; +bool ret = false; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , false); +if (uffd_fd <0 ) { +return false; + } + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +uint64_t uffd_ioctls; + +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } +/* Try to register block memory via UFFD-IO to track writes */ +if (uffd_register_memory (uffd_fd, bs->host, bs->max_length, +UFFDIO_REGISTER_MODE_WP , &uffd_ioctls)) { + goto out; + } +if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { + goto out; + } + } + ret = true; + +out: +uffd_close_fd (uffd_fd); +return ret; +#endif return false; } +/* +* ram_write_tracking_start: start UFFD-WP memory tracking +* +* Returns 0 for success or negative value in case of error +*/ +int ram_write_tracking_start (void ) +{ +#ifdef CONFIG_LINUX +int uffd_fd; +RAMState *rs = ram_state; +RAMBlock *bs; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , true); +if (uffd_fd <0 ) { +return uffd_fd; + } + rs->uffdio_fd = uffd_fd; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } + +/* Register block memory with UFFD to track writes */ +if (uffd_register_memory (rs->uffdio_fd, bs->host, + bs->max_length,UFFDIO_REGISTER_MODE_WP ,NULL )) { + goto fail; + } +/* Apply UFFD write protection to the block memory range */ +if (uffd_change_protection (rs->uffdio_fd, bs->host, + bs->max_length, true, false)) { + goto fail; + } + bs->flags |=RAM_UF_WRITEPROTECT ; +memory_region_ref (bs->mr); + +trace_ram_write_tracking_ramblock_start (bs->idstr, bs->page_size, + bs->host, bs->max_length); + } + +return 0 ; + +fail: +error_report ("ram_write_tracking_start() failed: restoring initial memory state" ); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* +* In case some memory block failed to be write-protected +* remove protection and unregister all succeeded RAM blocks +*/ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +uffd_close_fd (uffd_fd); +#endif /* CONFIG_LINUX */ + rs->uffdio_fd =-1 ; +return -1 ; +} + +/** +* ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection +*/ +void ram_write_tracking_stop (void ) +{ +#ifdef CONFIG_LINUX +RAMState *rs = ram_state; +RAMBlock *bs; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* Remove protection and unregister all affected RAM blocks */ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); + +trace_ram_write_tracking_ramblock_stop (bs->idstr, bs->page_size, + bs->host, bs->max_length); + +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +/* Finally close UFFD file descriptor */ +uffd_close_fd (rs->uffdio_fd); + rs->uffdio_fd =-1 ; +#endif /* CONFIG_LINUX */ +} +static SaveVMHandlers savevm_ram_handlers = { .save_setup = ram_save_setup, .save_live_iterate = ram_save_iterate,migration/ram.h +2
-0@@ -82,5 +82,7 @@ void colo_incoming_start_dirty_log(void);/* Background snapshot */ bool ram_write_tracking_available (void );bool ram_write_tracking_compatible (void ); +int ram_write_tracking_start (void ); +void ram_write_tracking_stop (void );#endif migration/trace-events +2
-0@@ -111,6 +111,8 @@ save_xbzrle_page_skipping(void) "" save_xbzrle_page_overflow(void) "" ram_save_iterate_big_wait(uint64_t milliconds, int iterations) "big wait: %" PRIu64 " milliseconds, %d iterations" ram_load_complete(int ret, uint64_t seq_iter) "exit_code %d seq iteration %" PRIu64 +ram_write_tracking_ramblock_start(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" +ram_write_tracking_ramblock_stop(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" # multifd.c multifd_new_send_channel_async(uint8_t id) "channel %d"Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v11 3/5] migration: support UFFD write fault processing in ram_save_iterate()
Quoted 34 lines · Dr. David Alan Gilbert
Quoted 27 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 26 lines · Dr. David Alan Gilbert
Quoted 11 lines · * Andrey Gruzdev ([email protected]) wrote:
In this particular implementation the same single migration thread is responsible for both normal linear dirty page migration and procesing UFFD page fault events.
Processing write faults includes reading UFFD file descriptor, finding respective RAM block and saving faulting page to the migration stream. After page has been saved, write protection can be removed. Since asynchronous version of
qemu_put_buffer()is expected to be used to save pages, we also have to flush migraion stream prior to un-protecting saved memory range.Quoted 6 lines
Write protection is being removed for any previously protected memory chunk that has hit the migration stream. That's valid for pages from linear page scan along with write fault pages.
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>I think this is OK, so:
Reviewed-by Dr. David Alan Gilbert <[email protected]>I think I'd have preferred to have kept the #ifdef LINUX's out of there, and I suspect using the opaque's for hte pre/post hook is overly complex; but other wise OK.
Quoted 6 lines
Quoted 1 line
Dave
Mm, I think it's impossible to completely move #ifdef LINUX out of there, but I suspect all #ifdef LINUX code can be moved to a single place with stubs for the #else case.. Suppose it would be better then now. For pre/post hooks - not too complex, but for single #ifdef section it really looks better to move this stuff to
ram_save_host_page().
Quoted 6 lines
An add on patch to consolidate the ifdef'ing to one place would be interesting; I'll look at taking this series as is (other than the thread name fix up etc) when I get around to my next pull (although I need to review some other peoples patches first!)
Dave
I'll make it and prepare next series, hope to finish in 1-2 days. Thanks!
Andrey
Quoted 391 lines
Quoted 391 lines
Andrey
Quoted 384 lines
Quoted 384 lines
--- include/exec/memory.h | 7 ++ migration/ram.c | 269 +++++++++++++++++++++++++++++++++++++++-- migration/ram.h | 2 + migration/trace-events | 2 + 4 files changed, 272 insertions(+), 8 deletions(-)include/exec/memory.h +7
-0@@ -150,6 +150,13 @@ typedef struct IOMMUTLBEvent {#define RAM_PMEM (1 << 5) +/* +* UFFDIO_WRITEPROTECT is used on this RAMBlock to +* support 'write-tracking' migration type. +* Implies ram_state->ram_wt_enabled. +*/ +#define RAM_UF_WRITEPROTECT (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,migration/ram.c +261
-8@@ -56,6 +56,11 @@#include "savevm.h" #include "qemu/iov.h" #include "multifd.h" +#include "sysemu/runstate.h" + +#ifdef CONFIG_LINUX +#include "qemu/userfaultfd.h" +#endif /***********************************************************/ /* ram save/restore */ @@ -298,6 +303,8 @@ struct RAMSrcPageRequest {struct RAMState {/* QEMUFile used for this migration */ QEMUFile *f; +/* UFFD file descriptor, used in 'write-tracking' migration */ +int uffdio_fd;/* Last block that we have visited searching for dirty pages */ RAMBlock *last_seen_block;/* Last block from where we have sent data */ @@ -1434,6 +1441,40 @@ static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)return block; } +#ifdef CONFIG_LINUX +/** +* poll_fault_page: try to get next UFFD write fault page and, if pending fault +* is found, return RAM block pointer and page offset +* +* Returns pointer to the RAMBlock containing faulting page, +* NULL if no write faults are pending +* +* @rs: current RAM state +* @offset: page offset from the beginning of the block +*/ +static RAMBlock *poll_fault_page (RAMState *rs,ram_addr_t *offset) +{ +struct uffd_msg uffd_msg; +void *page_address; +RAMBlock *bs; +int res; + +if (!migrate_background_snapshot ()) { +return NULL ; + } + + res =uffd_read_events (rs->uffdio_fd, &uffd_msg,1 ); +if (res <=0 ) { +return NULL ; + } + + page_address = (void *) uffd_msg.arg.pagefault.address; + bs =qemu_ram_block_from_host (page_address, false, offset); +assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); +return bs; +} +#endif /* CONFIG_LINUX */ +/** * get_queued_page: unqueue a page from the postcopy requests * @@ -1473,6 +1514,16 @@ static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) }while (block && !dirty); +#ifdef CONFIG_LINUX +if (!block) { +/* +* Poll write faults too if background snapshot is enabled; that's +* when we have vcpus got blocked by the write protected pages. +*/ + block =poll_fault_page (rs, &offset); + } +#endif /* CONFIG_LINUX */ +if (block) {/* * As soon as we start servicing pages out of order, then we have @@ -1746,6 +1797,53 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,return pages; } +/** +* ram_save_host_page_pre: ram_save_host_page() pre-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: pointer to receive opaque context value +*/ +static inline +void ram_save_host_page_pre (RAMState *rs,PageSearchStatus *pss,void **opaque) +{ + *(unsigned long *) opaque = pss->page; +} + +/** +* ram_save_host_page_post: ram_save_host_page() post-notifier +* +* @rs: current RAM state +* @pss: page-search-status structure +* @opaque: opaque context value +* @res_override: pointer to the return value of ram_save_host_page(), +* overwritten in case of an error +*/ +static void ram_save_host_page_post (RAMState *rs,PageSearchStatus *pss, +void *opaque,int *res_override) +{ +#ifdef CONFIG_LINUX +/* Check if page is from UFFD-managed region. */ +if (pss->block->flags &RAM_UF_WRITEPROTECT ) { +unsigned long page_from = (unsigned long ) opaque; + +void *page_address = pss->block->host + (page_from <<TARGET_PAGE_BITS ); +uint64_t run_length = (pss->page - page_from +1 ) <<TARGET_PAGE_BITS ; +int res; + +/* Flush async buffers before un-protect. */ +qemu_fflush (rs->f); +/* Un-protect memory range. */ + res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, + false, false); +/* We don't want to override existing error from ram_save_host_page(). */ +if (res <0 && *res_override >=0 ) { + *res_override = res; + } + } +#endif /* CONFIG_LINUX */ +} +/** * ram_find_and_save_block: finds a dirty page and sends it to f * @@ -1790,7 +1888,11 @@ static int ram_find_and_save_block(RAMState *rs, bool last_stage) }if (found) { +void *opaque; + +ram_save_host_page_pre (rs, &pss, &opaque); pages =ram_save_host_page (rs, &pss, last_stage); +ram_save_host_page_post (rs, &pss, opaque, &pages); } }while (!pages && again);@@ -1880,10 +1982,13 @@ static void ram_save_cleanup(void *opaque)RAMState **rsp = opaque;RAMBlock *block; -/* caller have hold iothread lock or is in a bh, so there is -* no writing race against the migration bitmap -*/ -memory_global_dirty_log_stop (); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +/* caller have hold iothread lock or is in a bh, so there is +* no writing race against the migration bitmap +*/ +memory_global_dirty_log_stop (); + }RAMBLOCK_FOREACH_NOT_IGNORED (block) {g_free (block->clear_bmap);@@ -2343,8 +2448,11 @@ static void ram_init_bitmaps(RAMState *rs) WITH_RCU_READ_LOCK_GUARD() {ram_list_init_bitmaps (); -memory_global_dirty_log_start (); -migration_bitmap_sync_precopy (rs); +/* We don't use dirty log with background snapshots */ +if (!migrate_background_snapshot ()) { +memory_global_dirty_log_start (); +migration_bitmap_sync_precopy (rs); + } }qemu_mutex_unlock_ramlist ();qemu_mutex_unlock_iothread ();@@ -3794,7 +3902,14 @@ static int ram_resume_prepare(MigrationState *s, void *opaque)*/ bool ram_write_tracking_available (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +uint64_t uffd_features; +int res; + + res =uffd_query_features (&uffd_features); +return (res ==0 && + (uffd_features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ) !=0 ); +#endif return false; }@@ -3805,10 +3920,148 @@ bool ram_write_tracking_available(void)*/ bool ram_write_tracking_compatible (void ) { -/* TODO: implement */ +#ifdef CONFIG_LINUX +const uint64_t uffd_ioctls_mask =BIT (_UFFDIO_WRITEPROTECT); +int uffd_fd; +RAMBlock *bs; +bool ret = false; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , false); +if (uffd_fd <0 ) { +return false; + } + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +uint64_t uffd_ioctls; + +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } +/* Try to register block memory via UFFD-IO to track writes */ +if (uffd_register_memory (uffd_fd, bs->host, bs->max_length, +UFFDIO_REGISTER_MODE_WP , &uffd_ioctls)) { + goto out; + } +if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { + goto out; + } + } + ret = true; + +out: +uffd_close_fd (uffd_fd); +return ret; +#endif return false; } +/* +* ram_write_tracking_start: start UFFD-WP memory tracking +* +* Returns 0 for success or negative value in case of error +*/ +int ram_write_tracking_start (void ) +{ +#ifdef CONFIG_LINUX +int uffd_fd; +RAMState *rs = ram_state; +RAMBlock *bs; + +/* Open UFFD file descriptor */ + uffd_fd =uffd_create_fd (UFFD_FEATURE_PAGEFAULT_FLAG_WP , true); +if (uffd_fd <0 ) { +return uffd_fd; + } + rs->uffdio_fd = uffd_fd; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +/* Nothing to do with read-only and MMIO-writable regions */ +if (bs->mr->readonly || bs->mr->rom_device) { +continue ; + } + +/* Register block memory with UFFD to track writes */ +if (uffd_register_memory (rs->uffdio_fd, bs->host, + bs->max_length,UFFDIO_REGISTER_MODE_WP ,NULL )) { + goto fail; + } +/* Apply UFFD write protection to the block memory range */ +if (uffd_change_protection (rs->uffdio_fd, bs->host, + bs->max_length, true, false)) { + goto fail; + } + bs->flags |=RAM_UF_WRITEPROTECT ; +memory_region_ref (bs->mr); + +trace_ram_write_tracking_ramblock_start (bs->idstr, bs->page_size, + bs->host, bs->max_length); + } + +return 0 ; + +fail: +error_report ("ram_write_tracking_start() failed: restoring initial memory state" ); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* +* In case some memory block failed to be write-protected +* remove protection and unregister all succeeded RAM blocks +*/ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +uffd_close_fd (uffd_fd); +#endif /* CONFIG_LINUX */ + rs->uffdio_fd =-1 ; +return -1 ; +} + +/** +* ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection +*/ +void ram_write_tracking_stop (void ) +{ +#ifdef CONFIG_LINUX +RAMState *rs = ram_state; +RAMBlock *bs; + +RCU_READ_LOCK_GUARD (); + +RAMBLOCK_FOREACH_NOT_IGNORED (bs) { +if ((bs->flags &RAM_UF_WRITEPROTECT ) ==0 ) { +continue ; + } +/* Remove protection and unregister all affected RAM blocks */ +uffd_change_protection (rs->uffdio_fd, bs->host, bs->max_length, false, false); +uffd_unregister_memory (rs->uffdio_fd, bs->host, bs->max_length); + +trace_ram_write_tracking_ramblock_stop (bs->idstr, bs->page_size, + bs->host, bs->max_length); + +/* Cleanup flags and remove reference */ + bs->flags &= ~RAM_UF_WRITEPROTECT ; +memory_region_unref (bs->mr); + } + +/* Finally close UFFD file descriptor */ +uffd_close_fd (rs->uffdio_fd); + rs->uffdio_fd =-1 ; +#endif /* CONFIG_LINUX */ +} +static SaveVMHandlers savevm_ram_handlers = { .save_setup = ram_save_setup, .save_live_iterate = ram_save_iterate,migration/ram.h +2
-0@@ -82,5 +82,7 @@ void colo_incoming_start_dirty_log(void);/* Background snapshot */ bool ram_write_tracking_available (void );bool ram_write_tracking_compatible (void ); +int ram_write_tracking_start (void ); +void ram_write_tracking_stop (void );#endif migration/trace-events +2
-0@@ -111,6 +111,8 @@ save_xbzrle_page_skipping(void) "" save_xbzrle_page_overflow(void) "" ram_save_iterate_big_wait(uint64_t milliconds, int iterations) "big wait: %" PRIu64 " milliseconds, %d iterations" ram_load_complete(int ret, uint64_t seq_iter) "exit_code %d seq iteration %" PRIu64 +ram_write_tracking_ramblock_start(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" +ram_write_tracking_ramblock_stop(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu" # multifd.c multifd_new_send_channel_async(uint8_t id) "channel %d"Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comPATCH v11 4/5 migration: implementation of background snapshot thread
In reply to Andrey Gruzdev viaIntroducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on userfault_fd with wr-protection support and is Linux-specific.
--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)
migration/migration.c +253 -2
@@ -2007,6 +2007,7 @@ void migrate_init(MigrationState *s)* locks. */ s->cleanup_bh =0 ; + s->vm_start_bh =0 ; s->to_dst_file =NULL ; s->state =MIGRATION_STATUS_NONE ; s->rp_state.from_dst_file =NULL ;
@@ -3211,6 +3212,50 @@ fail: MIGRATION_STATUS_FAILED); } +/** +* bg_migration_completion: Used by bg_migration_thread when after all the +* RAM has been saved. The caller 'breaks' the loop when this returns. +* +* @s: Current migration state +*/ +static void bg_migration_completion (MigrationState *s) +{ +int current_active_state = s->state; + +/* +* Stop tracking RAM writes - un-protect memory, un-register UFFD +* memory ranges, flush kernel wait queues and wake up threads +* waiting for write fault to be resolved. +*/ +ram_write_tracking_stop (); + +if (s->state ==MIGRATION_STATUS_ACTIVE ) { +/* +* By this moment we have RAM content saved into the migration stream. +* The next step is to flush the non-RAM content (device state) +* right after the ram content. The device state has been stored into +* the temporary buffer before RAM saving started. +*/ +qemu_put_buffer (s->to_dst_file, s->bioc->data, s->bioc->usage); +qemu_fflush (s->to_dst_file); + }else if (s->state ==MIGRATION_STATUS_CANCELLING ) { + goto fail; + } + +if (qemu_file_get_error (s->to_dst_file)) { +trace_migration_completion_file_err (); + goto fail; + } + +migrate_set_state (&s->state, current_active_state, +MIGRATION_STATUS_COMPLETED ); +return ; + +fail: +migrate_set_state (&s->state, current_active_state, +MIGRATION_STATUS_FAILED ); +} +bool migrate_colo_enabled (void ) {MigrationState *s =migrate_get_current ();
@@ -3551,6 +3596,47 @@ static void migration_iteration_finish(MigrationState *s)qemu_mutex_unlock_iothread (); } +static void bg_migration_iteration_finish (MigrationState *s) +{ +qemu_mutex_lock_iothread (); +switch (s->state) { +case MIGRATION_STATUS_COMPLETED : +migration_calculate_complete (s); +break ; + +case MIGRATION_STATUS_ACTIVE : +case MIGRATION_STATUS_FAILED : +case MIGRATION_STATUS_CANCELLED : +case MIGRATION_STATUS_CANCELLING : +break ; + +default : +/* Should not reach here, but if so, forgive the VM. */ +error_report ("%s: Unknown ending state %d" , __func__, s->state); +break ; + } + +migrate_fd_cleanup_schedule (s); +qemu_mutex_unlock_iothread (); +} + +/* +* Return true if continue to the next iteration directly, false +* otherwise. +*/ +static MigIterateState bg_migration_iteration_run (MigrationState *s) +{ +int res; + + res =qemu_savevm_state_iterate (s->to_dst_file, false); +if (res >0 ) { +bg_migration_completion (s); +return MIG_ITERATE_BREAK ; + } + +return MIG_ITERATE_RESUME ; +} +void migration_make_urgent_request (void ) {qemu_sem_post (&migrate_get_current ()->rate_limit_sem);
@@ -3698,6 +3784,165 @@ static void *migration_thread(void *opaque)return NULL ; } +static void bg_migration_vm_start_bh (void *opaque) +{ +MigrationState *s = opaque; + +qemu_bh_delete (s->vm_start_bh); + s->vm_start_bh =NULL ; + +vm_start (); + s->downtime =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ) - s->downtime_start; +} + +/** +* Background snapshot thread, based on live migration code. +* This is an alternative implementation of live migration mechanism +* introduced specifically to support background snapshots. +* +* It takes advantage of userfault_fd write protection mechanism introduced +* in v5.7 kernel. Compared to existing dirty page logging migration much +* lesser stream traffic is produced resulting in smaller snapshot images, +* simply cause of no page duplicates can get into the stream. +* +* Another key point is that generated vmstate stream reflects machine state +* 'frozen' at the beginning of snapshot creation compared to dirty page logging +* mechanism, which effectively results in that saved snapshot is the state of VM +* at the end of the process. +*/ +static void *bg_migration_thread (void *opaque) +{ +MigrationState *s = opaque; +int64_t setup_start; +MigThrError thr_error; +QEMUFile *fb; +bool early_fail = true; + +rcu_register_thread (); +object_ref (OBJECT (s)); + +qemu_file_set_rate_limit (s->to_dst_file,INT64_MAX ); + + setup_start =qemu_clock_get_ms (QEMU_CLOCK_HOST ); +/* +* We want to save vmstate for the moment when migration has been +* initiated but also we want to save RAM content while VM is running. +* The RAM content should appear first in the vmstate. So, we first +* stash the non-RAM part of the vmstate to the temporary buffer, +* then write RAM part of the vmstate to the migration stream +* with vCPUs running and, finally, write stashed non-RAM part of +* the vmstate from the buffer to the migration stream. +*/ + s->bioc =qio_channel_buffer_new (128 *1024 ); +qio_channel_set_name (QIO_CHANNEL (s->bioc),"vmstate-buffer" ); + fb =qemu_fopen_channel_output (QIO_CHANNEL (s->bioc)); +object_unref (OBJECT (s->bioc)); + +update_iteration_initial_status (s); + +qemu_savevm_state_header (s->to_dst_file); +qemu_savevm_state_setup (s->to_dst_file); + +if (qemu_savevm_state_guest_unplug_pending ()) { +migrate_set_state (&s->state,MIGRATION_STATUS_SETUP , +MIGRATION_STATUS_WAIT_UNPLUG ); + +while (s->state ==MIGRATION_STATUS_WAIT_UNPLUG && +qemu_savevm_state_guest_unplug_pending ()) { +qemu_sem_timedwait (&s->wait_unplug_sem,250 ); + } + +migrate_set_state (&s->state,MIGRATION_STATUS_WAIT_UNPLUG , +MIGRATION_STATUS_ACTIVE ); + }else { +migrate_set_state (&s->state,MIGRATION_STATUS_SETUP , +MIGRATION_STATUS_ACTIVE ); + } + s->setup_time =qemu_clock_get_ms (QEMU_CLOCK_HOST ) - setup_start; + +trace_migration_thread_setup_complete (); + s->downtime_start =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); + +qemu_mutex_lock_iothread (); + +/* +* If VM is currently in suspended state, then, to make a valid runstate +* transition in vm_stop_force_state() we need to wakeup it up. +*/ +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ,NULL ); + s->vm_was_running =runstate_is_running (); + +if (global_state_store ()) { + goto fail; + } +/* Forcibly stop VM before saving state of vCPUs and devices */ +if (vm_stop_force_state (RUN_STATE_PAUSED )) { + goto fail; + } +/* +* Put vCPUs in sync with shadow context structures, then +* save their state to channel-buffer along with devices. +*/ +cpu_synchronize_all_states (); +if (qemu_savevm_state_complete_precopy_non_iterable (fb, false, false)) { + goto fail; + } +/* Now initialize UFFD context and start tracking RAM writes */ +if (ram_write_tracking_start ()) { + goto fail; + } + early_fail = false; + +/* +* Start VM from BH handler to avoid write-fault lock here. +* UFFD-WP protection for the whole RAM is already enabled so +* calling VM state change notifiers from vm_start() would initiate +* writes to virtio VQs memory which is in write-protected region. +*/ + s->vm_start_bh =qemu_bh_new (bg_migration_vm_start_bh, s); +qemu_bh_schedule (s->vm_start_bh); + +qemu_mutex_unlock_iothread (); + +while (migration_is_active (s)) { +MigIterateState iter_state =bg_migration_iteration_run (s); +if (iter_state ==MIG_ITERATE_SKIP ) { +continue ; + }else if (iter_state ==MIG_ITERATE_BREAK ) { +break ; + } + +/* +* Try to detect any kind of failures, and see whether we +* should stop the migration now. +*/ + thr_error =migration_detect_error (s); +if (thr_error ==MIG_THR_ERR_FATAL ) { +/* Stop migration */ +break ; + } + +migration_update_counters (s,qemu_clock_get_ms (QEMU_CLOCK_REALTIME )); + } + +trace_migration_thread_after_loop (); + +fail: +if (early_fail) { +migrate_set_state (&s->state,MIGRATION_STATUS_ACTIVE , +MIGRATION_STATUS_FAILED ); +qemu_mutex_unlock_iothread (); + } + +bg_migration_iteration_finish (s); + +qemu_fclose (fb); +object_unref (OBJECT (s)); +rcu_unregister_thread (); + +return NULL ; +} +void migrate_fd_connect (MigrationState *s,Error *error_in) {Error *local_err =NULL ;
@@ -3761,8 +4006,14 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)migrate_fd_cleanup (s);return ; } -qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { +qemu_thread_create (&s->thread,"background_snapshot" , + bg_migration_thread, s,QEMU_THREAD_JOINABLE ); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }
migration/migration.h +3 -0
@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h"
@@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will
migration/ram.c +2 -0
@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */
@@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;
migration/savevm.c +0 -1
@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)
migration/savevm.h +2 -0
@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif
Signature · 1 line
2.25.1
Re: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 7 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.
I noticed there weren't any bdrv_ calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0
<snip>
migration/migration.c void migrate_fd_connect(MigrationState *s, Error *error_in) · line 4011
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",
Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to bg_snapshot
Other than that,
Quoted 78 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
Quoted 6 lines
Quoted 2 lines
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?
Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c void migrate_fd_connect(MigrationState *s, Error *error_in) · line 4011
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshot
Yep, missed that pthread_set_name_np() has a length limit)
Quoted 84 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 78 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comRe: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 14 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
Quoted 6 lines
Quoted 2 lines
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?
Quoted 4 lines
Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.
If you get a chance it would be great if you could put together an example of doing the combination RAM+block; that way we find out if there's anything silly missing.
Dave
Quoted 111 lines
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c void migrate_fd_connect(MigrationState *s, Error *error_in) · line 4011
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshotYep, missed that
pthread_set_name_np()has a length limit)Quoted 79 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 73 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.Quoted 8 lines
Quoted 4 lines
I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.
Quoted 5 lines
If you get a chance it would be great if you could put together an example of doing the combination RAM+block; that way we find out if there's anything silly missing.
Dave
Yep, I'll take a look at the QMP command sequences, how it should look like in our case and prepare an example, hope we are not missing something serious. At least we know that block setup data won't go to snapshot.
I've also checked starting background snapshot from the stopped VM state - looks OK, VM resumes operation, snapshot is saved, no apparent problems.
Maybe it will take some time, since now I'm on task to create tool to store snapshots with RAM indexable by GPFNs, together with the rest of VMSTATE. Based on QCOW2 format. Also it should support snapshot revert in postcopy mode.
Andrey
Quoted 109 lines
Quoted 109 lines
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c void migrate_fd_connect(MigrationState *s, Error *error_in) · line 4011
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshotYep, missed that
pthread_set_name_np()has a length limit)Quoted 79 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 73 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comRe: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 25 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.Quoted 8 lines
Quoted 4 lines
I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.Quoted 5 lines
If you get a chance it would be great if you could put together an example of doing the combination RAM+block; that way we find out if there's anything silly missing.
Dave
Quoted 8 lines
Yep, I'll take a look at the QMP command sequences, how it should look like in our case and prepare an example, hope we are not missing something serious. At least we know that block setup data won't go to snapshot.
I've also checked starting background snapshot from the stopped VM state - looks OK, VM resumes operation, snapshot is saved, no apparent problems.Maybe it will take some time, since now I'm on task to create tool to store snapshots with RAM indexable by GPFNs, together with the rest of VMSTATE.
If you want to make it indexable, why not just do a simple write(2) call for the whole of RAM rather than doing the thing like normal migration?
Dave
Quoted 118 lines
Based on QCOW2 format. Also it should support snapshot revert in postcopy mode.
Andrey
Quoted 109 lines
Quoted 109 lines
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c void migrate_fd_connect(MigrationState *s, Error *error_in) · line 4011
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshotYep, missed that
pthread_set_name_np()has a length limit)Quoted 79 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 73 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.comSignature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 31 lines · Dr. David Alan Gilbert
Quoted 30 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.Quoted 8 lines
Quoted 4 lines
I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.Quoted 5 lines
If you get a chance it would be great if you could put together an example of doing the combination RAM+block; that way we find out if there's anything silly missing.
Dave
Yep, I'll take a look at the QMP command sequences, how it should look like in our case and prepare an example, hope we are not missing something serious. At least we know that block setup data won't go to snapshot.
I've also checked starting background snapshot from the stopped VM state - looks OK, VM resumes operation, snapshot is saved, no apparent problems.
Quoted 6 lines
Quoted 2 lines
Maybe it will take some time, since now I'm on task to create tool to store snapshots with RAM indexable by GPFNs, together with the rest of VMSTATE.
If you want to make it indexable, why not just do a simple write(2) call for the whole of RAM rather than doing the thing like normal migration?
Dave
For me the main reason is apparent file size.. While we can get the same allocation size when saving via write(2) on Linux, in many cases the apparent file size will be much bigger then if use QCOW2.
Andrey
Quoted 118 lines
Quoted 118 lines
Based on QCOW2 format. Also it should support snapshot revert in postcopy mode.
Andrey
Quoted 109 lines
Quoted 109 lines
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c void migrate_fd_connect(MigrationState *s, Error *error_in) · line 4011
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshotYep, missed that
pthread_set_name_np()has a length limit)Quoted 79 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 73 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.comSignature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comRe: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 34 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 33 lines · Dr. David Alan Gilbert
Quoted 32 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.Quoted 8 lines
Quoted 4 lines
I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.Quoted 5 lines
If you get a chance it would be great if you could put together an example of doing the combination RAM+block; that way we find out if there's anything silly missing.
Dave
Yep, I'll take a look at the QMP command sequences, how it should look like in our case and prepare an example, hope we are not missing something serious. At least we know that block setup data won't go to snapshot.
I've also checked starting background snapshot from the stopped VM state - looks OK, VM resumes operation, snapshot is saved, no apparent problems.Maybe it will take some time, since now I'm on task to create tool to store snapshots with RAM indexable by GPFNs, together with the rest of VMSTATE.
Quoted 8 lines
Quoted 4 lines
If you want to make it indexable, why not just do a simple write(2) call for the whole of RAM rather than doing the thing like normal migration?
Dave
For me the main reason is apparent file size.. While we can get the same allocation size when saving via write(2) on Linux, in many cases the apparent file size will be much bigger then if use QCOW2.
Do you mean because of zero pages or for some other reason?
Dave
Quoted 126 lines
Andrey
Quoted 118 lines
Quoted 118 lines
Based on QCOW2 format. Also it should support snapshot revert in postcopy mode.
Andrey
Quoted 109 lines
Quoted 109 lines
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshotYep, missed that
pthread_set_name_np()has a length limit)Quoted 79 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 73 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.comSignature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.comSignature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v11 4/5] migration: implementation of background snapshot thread
Quoted 38 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 36 lines · Dr. David Alan Gilbert
Quoted 24 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Andrey Gruzdev ([email protected]) wrote:
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Andrey Gruzdev ([email protected]) wrote:
Introducing implementation of 'background' snapshot thread which in overall follows the logic of precopy migration while internally utilizes completely different mechanism to 'freeze' vmstate at the start of snapshot creation.
This mechanism is based on
userfault_fdwith wr-protection support and is Linux-specific.Quoted 8 lines
Quoted 4 lines
I noticed there weren't any
bdrv_calls in here; I guess with a snapshot you still have the source running so still have it accessing the disk; do you do anything to try and wire the ram snapshotting up to disk snapshotting?Block-related manipulations should be done externally, I think.
So create backing images for RW nodes, then stop VM, switch block graph and start background snapshot. Something like create 'virsh snapshot-create-as' does, but in other sequence.Quoted 5 lines
If you get a chance it would be great if you could put together an example of doing the combination RAM+block; that way we find out if there's anything silly missing.
Dave
Quoted 8 lines
Yep, I'll take a look at the QMP command sequences, how it should look like in our case and prepare an example, hope we are not missing something serious. At least we know that block setup data won't go to snapshot.
I've also checked starting background snapshot from the stopped VM state - looks OK, VM resumes operation, snapshot is saved, no apparent problems.Maybe it will take some time, since now I'm on task to create tool to store snapshots with RAM indexable by GPFNs, together with the rest of VMSTATE.
If you want to make it indexable, why not just do a simple write(2) call for the whole of RAM rather than doing the thing like normal migration?
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Dave
For me the main reason is apparent file size.. While we can get the same allocation size when saving via write(2) on Linux, in many cases the apparent file size will be much bigger then if use QCOW2.
Do you mean because of zero pages or for some other reason?
Dave
Yes. So plain sparse file on ext4 would grow to apparent size equal to highest non-zero GPA. While QCOW2 won't. It's important from the point of user experience, since desktop workload often show very small non-zero RSS. When I start Win10 on QEMU with a single Firefox tab with some Youtube HD video I have only 2-5GB of migration data on a 16GB VM.
Andrey
Quoted 125 lines
Quoted 125 lines
Andrey
Quoted 118 lines
Quoted 118 lines
Based on QCOW2 format. Also it should support snapshot revert in postcopy mode.
Andrey
Quoted 109 lines
Quoted 109 lines
//
Quoted 14 lines
Quoted 12 lines
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>--- migration/migration.c | 255 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/ram.c | 2 + migration/savevm.c | 1 - migration/savevm.h | 2 + 5 files changed, 260 insertions(+), 3 deletions(-)migration/migration.c +0
-0<snip>
Quoted 7 lines
migration/migration.c
Quoted 5 lines
migration/migration.c +3
-2-qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE ); + +if (migrate_background_snapshot ()) { + qemu_thread_create(&s->thread, "background_snapshot",Unfortunately that wont work - there's a 14 character limit on the thread name length; I guess we just shorten that to
bg_snapshotYep, missed that
pthread_set_name_np()has a length limit)Quoted 79 lines
Other than that,
Reviewed-by Dr. David Alan Gilbert <[email protected]>Quoted 73 lines
migration/migration.c +5
-0+ bg_migration_thread, s, QEMU_THREAD_JOINABLE); + }else { +qemu_thread_create (&s->thread,"live_migration" , + migration_thread, s,QEMU_THREAD_JOINABLE ); + } s->migration_thread_running = true; }migration/migration.h +3
-0@@ -20,6 +20,7 @@#include "qemu/thread.h" #include "qemu/coroutine_int.h" #include "io/channel.h" +#include "io/channel-buffer.h" #include "net/announce.h" #include "qom/object.h" @@ -147,8 +148,10 @@ struct MigrationState {/*< public >*/ QemuThread thread; +QEMUBH *vm_start_bh;QEMUBH *cleanup_bh;QEMUFile *to_dst_file; +QIOChannelBuffer *bioc;/* * Protects to_dst_file pointer. We need to make sure we won't * yield or hang during the critical section, since this lock will migration/ram.c +2
-0@@ -1471,6 +1471,7 @@ static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) page_address = (void *) uffd_msg.arg.pagefault.address; bs =qemu_ram_block_from_host (page_address, false, offset);assert (bs && (bs->flags &RAM_UF_WRITEPROTECT ) !=0 ); + return bs; }#endif /* CONFIG_LINUX */ @@ -1836,6 +1837,7 @@ static void ram_save_host_page_post(RAMState *rs, PageSearchStatus *pss,/* Un-protect memory range. */ res =uffd_change_protection (rs->uffdio_fd, page_address, run_length, false, false); +/* We don't want to override existing error from ram_save_host_page(). */ if (res <0 && *res_override >=0 ) { *res_override = res;migration/savevm.c +0
-1@@ -1354,7 +1354,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)return 0 ; } -static int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f,bool in_postcopy,bool inactivate_disks)migration/savevm.h +2
-0@@ -64,5 +64,7 @@ int qemu_loadvm_state(QEMUFile *f);void qemu_loadvm_state_cleanup (void );int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis);int qemu_load_device_state (QEMUFile *f); +int qemu_savevm_state_complete_precopy_non_iterable (QEMUFile *f, +bool in_postcopy,bool inactivate_disks);#endif Signature · 1 line
2.25.1Signature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.comSignature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.comSignature · 3 lines
Andrey Gruzdev, Principal Engineer Virtuozzo GmbH +7-903-247-6397 virtuzzo.com
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comPATCH v11 5/5 migration: introduce 'userfaultfd-wrlat.py' script
In reply to Andrey Gruzdev viaAdd BCC/eBPF script to analyze userfaultfd write fault latency distribution.
--- scripts/userfaultfd-wrlat.py | 148 +++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100755 scripts/userfaultfd-wrlat.py
scripts/userfaultfd-wrlat.py new file +148 -0
@@ -0,0 +1,148 @@ +#!/usr/bin/python3 +# +# userfaultfd-wrlat Summarize userfaultfd write fault latencies. +# Events are continuously accumulated for the +# run, while latency distribution histogram is +# dumped each 'interval' seconds. +# +# For Linux, uses BCC, eBPF. +# +# USAGE: userfaultfd-lat [interval [count]] +# +# Copyright Virtuozzo GmbH, 2020 +# +# Authors: +# Andrey Gruzdev <[email protected]> +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +from __future__ import print_function +from bcc import BPF +from ctypes import c_ushort, c_int, c_ulonglong +from time import sleep +from sys import argv + +def usage(): + print("USAGE: %s [interval [count]]" % argv[0]) + exit() + +# define BPF program +bpf_text = """ +#include <uapi/linux/ptrace.h> +#include <linux/mm.h> + +/* + * UFFD page fault event descriptor. + * Used as a key to BPF_HASH table. + */ +struct ev_desc { + u64 pid; + u64 addr; +}; + +BPF_HASH(ev_start, struct ev_desc, u64); +BPF_HASH(ctx_handle_userfault, u64, u64); +BPF_HISTOGRAM(ev_delta_hist, u64); + +/* Trace UFFD page fault start event. */ +static void do_event_start(u64 pid, u64 address) +{ + struct ev_desc desc = { .pid = pid, .addr = address }; + u64 ts = bpf_ktime_get_ns(); + + ev_start.insert(&desc, &ts); +} + +/* Trace UFFD page fault end event. */ +static void do_event_end(u64 pid, u64 address) +{ + struct ev_desc desc = { .pid = pid, .addr = address }; + u64 ts = bpf_ktime_get_ns(); + u64 *tsp; + + tsp = ev_start.lookup(&desc); + if (tsp) { + u64 delta = ts - (*tsp); + /* Transform time delta to milliseconds */ + ev_delta_hist.increment(bpf_log2l(delta / 1000000)); + ev_start.delete(&desc); + } +} + +/* KPROBE for handle_userfault(). */ +int probe_handle_userfault(struct pt_regs *ctx, struct vm_fault *vmf, + unsigned long reason) +{ + /* Trace only UFFD write faults. */ + if (reason & VM_UFFD_WP) { + u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 addr = vmf->address; + + do_event_start(pid, addr); + ctx_handle_userfault.update(&pid, &addr); + } + return 0; +} + +/* KRETPROBE for handle_userfault(). */ +int retprobe_handle_userfault(struct pt_regs *ctx) +{ + u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 *addr_p; + + /* + * Here we just ignore the return value. In case of spurious wakeup + * or pending signal we'll still get (at least for v5.8.0 kernel) + * VM_FAULT_RETRY or (VM_FAULT_RETRY | VM_FAULT_MAJOR) here. + * Anyhow, handle_userfault() would be re-entered if such case happens, + * keeping initial timestamp unchanged for the faulting thread. + */ + addr_p = ctx_handle_userfault.lookup(&pid); + if (addr_p) { + do_event_end(pid, *addr_p); + ctx_handle_userfault.delete(&pid); + } + return 0; +} +""" + +# arguments +interval = 10 +count = -1 +if len(argv) > 1: + try: + interval = int(argv[1]) + if interval == 0: + raise + if len(argv) > 2: + count = int(argv[2]) + except: # also catches -h, --help + usage() + +# load BPF program +b = BPF(text=bpf_text) +# attach KRPOBEs +b.attach_kprobe(event="handle_userfault", fn_name="probe_handle_userfault") +b.attach_kretprobe(event="handle_userfault", fn_name="retprobe_handle_userfault") + +# header +print("Tracing UFFD-WP write fault latency... Hit Ctrl-C to end.") + +# output +loop = 0 +do_exit = 0 +while (1): + if count > 0: + loop += 1 + if loop > count: + exit() + try: + sleep(interval) + except KeyboardInterrupt: + pass; do_exit = 1 + + print() + b["ev_delta_hist"].print_log2_hist("msecs") + if do_exit: + exit()
Signature · 1 line
2.25.1
Re: [PATCH v11 5/5] migration: introduce 'userfaultfd-wrlat.py' script
Quoted 4 lines · Andrey Gruzdev
Add BCC/eBPF script to analyze userfaultfd write fault latency distribution.
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>
(This seems to be the last patch that lacks a r-b ... Let's see whether I could
convert my a-b into an r-b... :)
scripts/userfaultfd-wrlat.py line 45
Quoted 2 lines
scripts/userfaultfd-wrlat.py new file +2
-0+BPF_HASH(ev_start, struct ev_desc, u64); +BPF_HASH(ctx_handle_userfault, u64, u64);
IMHO we only need one hash here instead of two:
BPF_HASH(ev_start, u32, u64);
Where we use the tid as the key (u32), and timestamp as the value (u64). The thing is we don't really need the address for current statistics, IMHO.
scripts/userfaultfd-wrlat.py line 78
Quoted 6 lines
scripts/userfaultfd-wrlat.py new file +6
-0+/* KPROBE for handle_userfault(). */ +int probe_handle_userfault(struct pt_regs *ctx, struct vm_fault *vmf, + unsigned long reason) +{ + /* Trace only UFFD write faults. */ + if (reason & VM_UFFD_WP) {
Better with comment:
/* Using "(u32)" to drop group ID which is upper 32 bits */
If even better, we'd want a get_current_tid() helper and call it here and below (bpf_get_current_pid_tgid() will return tid|gid<<32 I think, so I'm a bit confused why bcc people called it pid at the first place...).
Quoted 11 lines
scripts/userfaultfd-wrlat.py new file +11
-0+ u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 addr = vmf->address; + + do_event_start(pid, addr); + ctx_handle_userfault.update(&pid, &addr); + } + return 0; +} + +/* KRETPROBE for handle_userfault(). */ +int retprobe_handle_userfault(struct pt_regs *ctx)
scripts/userfaultfd-wrlat.py line 99
Quoted 10 lines
scripts/userfaultfd-wrlat.py new file +10
-0+{ + u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 *addr_p; + + /* + * Here we just ignore the return value. In case of spurious wakeup + * or pending signal we'll still get (at least for v5.8.0 kernel) + * VM_FAULT_RETRY or (VM_FAULT_RETRY | VM_FAULT_MAJOR) here. + * Anyhow, handle_userfault() would be re-entered if such case happens, + * keeping initial timestamp unchanged for the faulting thread.
AFAIU this comment is not matching what the code does. But I agree it's not a big problem because we won't miss any long delays (because the one long delayed sample will just be split into two or multiple delays, which will still be reflected in the histogram at last). Or am I wrong?
scripts/userfaultfd-wrlat.py line 108
Quoted 9 lines
scripts/userfaultfd-wrlat.py new file +9
-0+ */ + addr_p = ctx_handle_userfault.lookup(&pid); + if (addr_p) { + do_event_end(pid, *addr_p); + ctx_handle_userfault.delete(&pid); + } + return 0; +} +"""
Other than that, the rest looks good to me.
I'd think it's fine to even merge the current version since it actually works nicely. Andrey, if you agree with any of my above comments, feel free to repost this patch (since I see Dave provided the rest r-bs). Then I think I can r-b this one too. Thanks!
Signature · 1 line
Peter Xu
Re: [PATCH v11 5/5] migration: introduce 'userfaultfd-wrlat.py' script
Quoted 8 lines · Peter Xu
Quoted 4 lines · Andrey Gruzdev
Add BCC/eBPF script to analyze userfaultfd write fault latency distribution.
Signed-off-by Andrey Gruzdev <[email protected]>Acked-by Peter Xu <[email protected]>(This seems to be the last patch that lacks a r-b ... Let's see whether I could
convert my a-b into an r-b... :)
Quoted 8 lines
scripts/userfaultfd-wrlat.py line 45
Quoted 2 lines
scripts/userfaultfd-wrlat.py new file +2
-0+BPF_HASH(ev_start, struct ev_desc, u64); +BPF_HASH(ctx_handle_userfault, u64, u64);IMHO we only need one hash here instead of two:
BPF_HASH(ev_start, u32, u64);Where we use the tid as the key (u32), and timestamp as the value (u64). The thing is we don't really need the address for current statistics, IMHO.
Agree, that's a more appropriate way do that. Address here is not really needed. Thanks!
Quoted 9 lines
scripts/userfaultfd-wrlat.py line 78
Quoted 6 lines
scripts/userfaultfd-wrlat.py new file +6
-0+/* KPROBE for handle_userfault(). */ +int probe_handle_userfault(struct pt_regs *ctx, struct vm_fault *vmf, + unsigned long reason) +{ + /* Trace only UFFD write faults. */ + if (reason & VM_UFFD_WP) {Better with comment:
/* Using "(u32)" to drop group ID which is upper 32 bits */
Yep.
Quoted 22 lines
If even better, we'd want a
get_current_tid()helper and call it here and below (bpf_get_current_pid_tgid()will return tid|gid<<32 I think, so I'm a bit confused why bcc people called it pid at the first place...).Quoted 17 lines
scripts/userfaultfd-wrlat.py new file +17
-0+ u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 addr = vmf->address; + + do_event_start(pid, addr); + ctx_handle_userfault.update(&pid, &addr); + } + return 0; +} + +/* KRETPROBE for handle_userfault(). */ +int retprobe_handle_userfault(struct pt_regs *ctx) +{ + u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 *addr_p; + + /* + * Here we just ignore the return value. In case of spurious wakeup
Quoted 8 lines
scripts/userfaultfd-wrlat.py line 99
Quoted 4 lines
scripts/userfaultfd-wrlat.py new file +4
-0+ * or pending signal we'll still get (at least for v5.8.0 kernel) + * VM_FAULT_RETRY or (VM_FAULT_RETRY | VM_FAULT_MAJOR) here. + * Anyhow, handle_userfault() would be re-entered if such case happens, + * keeping initial timestamp unchanged for the faulting thread.AFAIU this comment is not matching what the code does. But I agree it's not a big problem because we won't miss any long delays (because the one long delayed sample will just be split into two or multiple delays, which will still be reflected in the histogram at last). Or am I wrong?
Mm, not really sure about comment.. I need to read kernel code again.
Quoted 9 lines
scripts/userfaultfd-wrlat.py line 108
Quoted 9 lines
scripts/userfaultfd-wrlat.py new file +9
-0+ */ + addr_p = ctx_handle_userfault.lookup(&pid); + if (addr_p) { + do_event_end(pid, *addr_p); + ctx_handle_userfault.delete(&pid); + } + return 0; +} +"""
Quoted 7 lines
Other than that, the rest looks good to me.
I'd think it's fine to even merge the current version since it actually works nicely. Andrey, if you agree with any of my above comments, feel free to repost this patch (since I see Dave provided the rest r-bs). Then I think I can r-b this one too. Thanks!
Thanks!
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comRe: [PATCH v11 5/5] migration: introduce 'userfaultfd-wrlat.py' script
Quoted 17 lines · Andrey Gruzdev
Quoted 8 lines
Quoted 8 lines
scripts/userfaultfd-wrlat.py new file +8
-0+/* KRETPROBE for handle_userfault(). */ +int retprobe_handle_userfault(struct pt_regs *ctx) +{ + u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 *addr_p; + + /* + * Here we just ignore the return value. In case of spurious wakeupQuoted 8 lines
scripts/userfaultfd-wrlat.py line 99
Quoted 4 lines
scripts/userfaultfd-wrlat.py new file +4
-0+ * or pending signal we'll still get (at least for v5.8.0 kernel) + * VM_FAULT_RETRY or (VM_FAULT_RETRY | VM_FAULT_MAJOR) here. + * Anyhow, handle_userfault() would be re-entered if such case happens, + * keeping initial timestamp unchanged for the faulting thread.AFAIU this comment is not matching what the code does. But I agree it's not a big problem because we won't miss any long delays (because the one long delayed sample will just be split into two or multiple delays, which will still be reflected in the histogram at last). Or am I wrong?
Quoted 1 line
Mm, not really sure about comment.. I need to read kernel code again.
Not relevant to kernel; I was only talking about the last sentence where we won't "keeping initial timestamp unchanged" but we'll do the statistic anyways. Because exactly as you said we'll get VM_FAULT_RETRY unconditionally while we won't be able to identify whether the page fault request is resolved or not.
Signature · 1 line
Peter Xu
Re: [PATCH v11 5/5] migration: introduce 'userfaultfd-wrlat.py' script
Quoted 17 lines · Peter Xu
Quoted 16 lines · Andrey Gruzdev
Quoted 16 lines
scripts/userfaultfd-wrlat.py line 99
Quoted 12 lines
scripts/userfaultfd-wrlat.py new file +12
-0+/* KRETPROBE for handle_userfault(). */ +int retprobe_handle_userfault(struct pt_regs *ctx) +{ + u64 pid = (u32) bpf_get_current_pid_tgid(); + u64 *addr_p; + + /* + * Here we just ignore the return value. In case of spurious wakeup + * or pending signal we'll still get (at least for v5.8.0 kernel) + * VM_FAULT_RETRY or (VM_FAULT_RETRY | VM_FAULT_MAJOR) here. + * Anyhow, handle_userfault() would be re-entered if such case happens, + * keeping initial timestamp unchanged for the faulting thread.AFAIU this comment is not matching what the code does. But I agree it's not a big problem because we won't miss any long delays (because the one long delayed sample will just be split into two or multiple delays, which will still be reflected in the histogram at last). Or am I wrong?
Quoted 6 lines
Quoted 1 line
Mm, not really sure about comment.. I need to read kernel code again.
Not relevant to kernel; I was only talking about the last sentence where we won't "keeping initial timestamp unchanged" but we'll do the statistic anyways. Because exactly as you said we'll get
VM_FAULT_RETRYunconditionally while we won't be able to identify whether the page fault request is resolved or not.
Yep, agree. My point is also that trying to get timestamp of real PF resolution is complicated and not very reasonable since a sequence of softbit and hardbit modifications to the in-memory paging structures occur, not immediately reflected in particular TLB entry. But for our statistics this level of accuracy is OK, I think.
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comPing
Quoted 105 lines · Andrey Gruzdev
This patch series is a kind of 'rethinking' of Denis Plotnikov's ideas he's implemented in his series '[PATCH v0 0/4] migration: add background snapshot'.
Currently the only way to make (external) live VM snapshot is using existing dirty page logging migration mechanism. The main problem is that it tends to produce a lot of page duplicates while running VM goes on updating already saved pages. That leads to the fact that vmstate image size is commonly several times bigger then non-zero part of virtual machine's RSS. Time required to converge RAM migration and the size of snapshot image severely depend on the guest memory write rate, sometimes resulting in unacceptably long snapshot creation time and huge image size.
This series propose a way to solve the aforementioned problems. This is done by using different RAM migration mechanism based on UFFD write protection management introduced in v5.7 kernel. The migration strategy is to 'freeze' guest RAM content using write-protection and iteratively release protection for memory ranges that have already been saved to the migration stream. At the same time we read in pending UFFD write fault events and save those pages out-of-order with higher priority.
How to use:
1. Enable write-tracking migration capability
virsh qemu-monitor-command <domain> --hmpmigrate_set_capability.
track-writes-ram on2. Start the external migration to a file
virsh qemu-monitor-command <domain> --hmp migrate exec:'cat > ./vm_state'3. Wait for the migration finish and check that the migration has completed. state.
Changes
v10->v11:* 1. Updated commit messages.
Changes
v9->v10:* 1. Fixed commit message for [PATCH v9 1/5].
Changes
v8->v9:* 1. Fixed wrong cover letter subject.
Changes
v7->v8:* 1. Fixed coding style problems to pass checkpatch.
Changes
v6->v7:* 1. Fixed background snapshot on suspended guest: call
qemu_system_wakeup_request()
* before stopping VM to make runstate transition valid.
* 2. Disabled dirty page logging and log syn when 'background-snapshot' is enabled.
* 3. Introduced 'userfaultfd-wrlat.py' script to analyze UFFD write fault latencies.Changes
v5->v6:* 1. Consider possible hot pluggin/unpluggin of memory device - don't use static
* for write-tracking support level inmigrate_query_write_tracking(), check
* each time when one tries to enable 'background-snapshot' capability.Changes
v4->v5:* 1. Refactored
util/userfaultfd.ccode to support features required by postcopy.
* 2. Introduced checks for host kernel and guest memory backend compatibility
* to 'background-snapshot' branch inmigrate_caps_check().
* 3. Switched to usingtrace_xxxinstead of info_report()/error_report() for* cases when error message must be hidden (probing UFFD-IO) or info may be * really littering output if goes to stderr. * 4 Added RCU_READ_LOCK_GUARDs to the code dealing with RAM block list.* 5. Added
memory_region_ref()for each RAM block being wr-protected.
* 6. Reusedqemu_ram_block_from_host()instead of custom RAM block lookup routine.
* 7. Refused from using specific hwaddr/ram_addr_t in favour of void */uint64_t.
* 8. Currently dropped 'linear-scan-rate-limiting' patch. The reason is that* that choosen criteria for high-latency fault detection (i.e. timestamp of * UFFD event fetch) is not representative enough for this task. * At the moment it looks somehow like premature optimization effort.* 8. Dropped some unnecessary/unused code.
Andrey Gruzdev (5):
migration: introduce 'background-snapshot' migration capability
migration: introduce UFFD-WP low-level interface helpers
migration: support UFFD write fault processing inram_save_iterate()
migration: implementation of background snapshot thread
migration: introduce 'userfaultfd-wrlat.py' scriptinclude/exec/memory.h | 8 + include/qemu/userfaultfd.h | 35 ++++ migration/migration.c | 365 ++++++++++++++++++++++++++++++++++- migration/migration.h | 4 + migration/ram.c | 288 ++++++++++++++++++++++++++- migration/ram.h | 6 + migration/savevm.c | 1 - migration/savevm.h | 2 + migration/trace-events | 2 + qapi/migration.json | 7 +- scripts/userfaultfd-wrlat.py | 148 ++++++++++++++ util/meson.build | 1 + util/trace-events | 9 + util/userfaultfd.c | 345 +++++++++++++++++++++++++++++++++ 14 files changed, 1211 insertions(+), 10 deletions(-)create mode 100644
include/qemu/userfaultfd.h
create mode 100755scripts/userfaultfd-wrlat.py
create mode 100644util/userfaultfd.c
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comQuoted 1 line · Andrey Gruzdev
Ping
Dave, Juan,
I really think this series needs some attention...
It'll be a great if QEMU can start to take live snapshot at least to me. Andrey (and also Denis with the older versions) did a great job working on it and moving it forward. IMHO it's something beneficial for the QEMU community and we should really encourage such behavior on working useful things, working in collaborative way, and being consistent.
Is there any concern about this series? Let me know if there's anything I can help with it.
Thanks!
Signature · 1 line
Peter Xu