From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605790930; cv=none; d=zohomail.com; s=zohoarc; b=DmCZBF68gswSPiUxGGgJCqhCtEsIewIdqjzyTPI7/RywBQoxVBF9siVGz+a8076jFv9Ttm6VfZr8FfJQSRgpZTFLX9V9tz3nTbTDUkGwe2f+peBIPHidttXKlXvxi386EF6EcENb56K87nKePGiuomn+rYT00POr1k6p0+ahwR0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605790930; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=tGBv1Ick1ABzLINDSf4WOfrZT/bgMF+j4jp6hmBYwWQ=; b=DsizIz77mH2pccAlop+rh0d6IifXyYxk3+6y/6S366OAl4Yo4fnYuYfAp/YcOE25H61dGBYZ1Vf76QhJMZfISGHL3zSr9gzrq7ljAuKLIjpSfAZnvSRFKa64nnv0BTHuXlnkb95cvVyglGScw1vR5vaSgXaIdYjGC+BRpa3FLbU= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605790930796468.93348134727034; Thu, 19 Nov 2020 05:02:10 -0800 (PST) Received: from localhost ([::1]:56640 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfjZg-0006mm-Vu for importer@patchew.org; Thu, 19 Nov 2020 08:02:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54622) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjXj-0005BZ-PU for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:07 -0500 Received: from relay.sw.ru ([185.231.240.75]:49262 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjXh-0008MF-Eo for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:07 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjXT-009NTF-2d; Thu, 19 Nov 2020 15:59:51 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 1/7] introduce 'track-writes-ram' migration capability Date: Thu, 19 Nov 2020 15:59:34 +0300 Message-Id: <20201119125940.20017-2-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" Signed-off-by: Andrey Gruzdev --- migration/migration.c | 96 +++++++++++++++++++++++++++++++++++++++++++ migration/migration.h | 1 + qapi/migration.json | 7 +++- 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/migration/migration.c b/migration/migration.c index 87a9b59f83..ff0364dde0 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -56,6 +56,7 @@ #include "net/announce.h" #include "qemu/queue.h" #include "multifd.h" +#include "sysemu/cpus.h" =20 #ifdef CONFIG_VFIO #include "hw/vfio/vfio-common.h" @@ -1165,6 +1166,91 @@ static bool migrate_caps_check(bool *cap_list, } } =20 + if (cap_list[MIGRATION_CAPABILITY_TRACK_WRITES_RAM]) { + if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) { + error_setg(errp, + "Track-writes is not compatible with postcopy-ram"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_DIRTY_BITMAPS]) { + error_setg(errp, + "Track-writes is not compatible with dirty-bitmaps"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME]) { + error_setg(errp, + "Track-writes is not compatible with postcopy-blocktim= e"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE]) { + error_setg(errp, + "Track-writes is not compatible with late-block-activa= te"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_RETURN_PATH]) { + error_setg(errp, + "Track-writes is not compatible with return-path"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_MULTIFD]) { + error_setg(errp, "Track-writes is not compatible with multifd"= ); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER]) { + error_setg(errp, + "Track-writes is not compatible with pause-before-swit= chover"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_AUTO_CONVERGE]) { + error_setg(errp, + "Track-writes is not compatible with auto-converge"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_RELEASE_RAM]) { + error_setg(errp, + "Track-writes is not compatible with release-ram"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_RDMA_PIN_ALL]) { + error_setg(errp, + "Track-writes is not compatible with rdma-pin-all"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) { + error_setg(errp, + "Track-writes is not compatible with compression"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_XBZRLE]) { + error_setg(errp, + "Track-writes is not compatible with XBZLRE"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_X_COLO]) { + error_setg(errp, + "Track-writes is not compatible with x-colo"); + return false; + } + + if (cap_list[MIGRATION_CAPABILITY_VALIDATE_UUID]) { + error_setg(errp, + "Track-writes is not compatible with validate-uuid"); + return false; + } + } + return true; } =20 @@ -2490,6 +2576,15 @@ bool migrate_use_block_incremental(void) return s->parameters.block_incremental; } =20 +bool migrate_track_writes_ram(void) +{ + MigrationState *s; + + s =3D migrate_get_current(); + + return s->enabled_capabilities[MIGRATION_CAPABILITY_TRACK_WRITES_RAM]; +} + /* migration thread support */ /* * Something bad happened to the RP stream, mark an error @@ -3783,6 +3878,7 @@ static Property migration_properties[] =3D { 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-track-writes-ram", MIGRATION_CAPABILITY_TRACK_W= RITES_RAM), =20 DEFINE_PROP_END_OF_LIST(), }; diff --git a/migration/migration.h b/migration/migration.h index d096b77f74..339ae720e0 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -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_track_writes_ram(void); =20 /* Sending on the return path - generic and then for each message type */ void migrate_send_rp_shut(MigrationIncomingState *mis, diff --git a/qapi/migration.json b/qapi/migration.json index 3c75820527..a28d8b7ee8 100644 --- a/qapi/migration.json +++ b/qapi/migration.json @@ -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) # +# @track-writes-ram: 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', 'track-writes-ram'] } =20 ## # @MigrationCapabilityStatus: --=20 2.25.1 From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605791069; cv=none; d=zohomail.com; s=zohoarc; b=e19Vn68ahYnTke8XcQQWw0hOWqPWH+BaxO8niALd94fuv3ICDwfsi85FJD8YI767MJ0mLaFakaiGvDDqFcU3VtUJMfKiV4R7KKT1lWF6Qr/jKxpxMW+VjC/Oc4b59g6yd96O1ozK7fxA2zM4KH5A3sZgZt1lKyXQaERbshTYDMM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605791069; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=6Pm/zSTFZL2T+sWXnz2jD1SwSftlBM+OQKrcZevy3/8=; b=gdKYdqBF38hTIXUYY3VrZWvv9C8lsKeKcmYXgMBxW8ygbIRS9gDQu0kvksKVjoItDODxdW3aKmvEMgBLnVQ4KH1YDyPN5Zk8tAQeK0zQYLcsyiv1v+rCW2Uo+6WABCwMiUtjwX3+0NMxuCwwgzHKFIdFNqFgpItFGlJ54ICovAs= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605791069327221.42636306549946; Thu, 19 Nov 2020 05:04:29 -0800 (PST) Received: from localhost ([::1]:37084 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfjbv-0001rI-Uj for importer@patchew.org; Thu, 19 Nov 2020 08:04:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54902) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjXw-0005SO-FQ for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:20 -0500 Received: from relay.sw.ru ([185.231.240.75]:49340 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjXt-0008Qf-Bd for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:20 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjXe-009NTF-SC; Thu, 19 Nov 2020 16:00:02 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 2/7] introduce UFFD-WP low-level interface helpers Date: Thu, 19 Nov 2020 15:59:35 +0300 Message-Id: <20201119125940.20017-3-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" Implemented support for the whole RAM block memory protection/un-protection. Introduced higher level ram_write_tracking_start() and ram_write_tracking_stop() to start/stop tracking guest memory writes. Signed-off-by: Andrey Gruzdev --- include/exec/memory.h | 7 ++ migration/ram.c | 267 ++++++++++++++++++++++++++++++++++++++++++ migration/ram.h | 4 + 3 files changed, 278 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 0f3e6bcd5e..3d798fce16 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -139,6 +139,13 @@ typedef struct IOMMUNotifier IOMMUNotifier; /* RAM is a persistent kind memory */ #define RAM_PMEM (1 << 5) =20 +/* + * 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, diff --git a/migration/ram.c b/migration/ram.c index 7811cde643..7f273c9996 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -56,6 +56,12 @@ #include "savevm.h" #include "qemu/iov.h" #include "multifd.h" +#include +#include +#include +#include +#include +#include "sysemu/runstate.h" =20 /***********************************************************/ /* ram save/restore */ @@ -298,6 +304,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 */ @@ -453,6 +461,181 @@ static QemuThread *decompress_threads; static QemuMutex decomp_done_lock; static QemuCond decomp_done_cond; =20 +/** + * uffd_create_fd: create UFFD file descriptor + * + * Returns non-negative file descriptor or negative value in case of an er= ror + */ +static int uffd_create_fd(void) +{ + int uffd; + struct uffdio_api api_struct; + uint64_t ioctl_mask =3D BIT(_UFFDIO_REGISTER) | BIT(_UFFDIO_UNREGISTER= ); + + uffd =3D syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); + if (uffd < 0) { + error_report("uffd_create_fd() failed: UFFD not supported"); + return -1; + } + + api_struct.api =3D UFFD_API; + api_struct.features =3D UFFD_FEATURE_PAGEFAULT_FLAG_WP; + if (ioctl(uffd, UFFDIO_API, &api_struct)) { + error_report("uffd_create_fd() failed: " + "API version not supported version=3D%llx errno=3D%i", + api_struct.api, errno); + goto fail; + } + + if ((api_struct.ioctls & ioctl_mask) !=3D ioctl_mask) { + error_report("uffd_create_fd() failed: " + "PAGEFAULT_FLAG_WP feature missing"); + goto fail; + } + + return uffd; + +fail: + close(uffd); + return -1; +} + +/** + * uffd_close_fd: close UFFD file descriptor + * + * @uffd: UFFD file descriptor + */ +static void uffd_close_fd(int uffd) +{ + assert(uffd >=3D 0); + close(uffd); +} + +/** + * uffd_register_memory: register memory range with UFFD + * + * Returns 0 in case of success, negative value on error + * + * @uffd: UFFD file descriptor + * @start: starting virtual address of memory range + * @length: length of memory range + * @track_missing: generate events on missing-page faults + * @track_wp: generate events on write-protected-page faults + */ +static int uffd_register_memory(int uffd, hwaddr start, hwaddr length, + bool track_missing, bool track_wp) +{ + struct uffdio_register uffd_register; + + uffd_register.range.start =3D start; + uffd_register.range.len =3D length; + uffd_register.mode =3D (track_missing ? UFFDIO_REGISTER_MODE_MISSING := 0) | + (track_wp ? UFFDIO_REGISTER_MODE_WP : 0); + + if (ioctl(uffd, UFFDIO_REGISTER, &uffd_register)) { + error_report("uffd_register_memory() failed: " + "start=3D%0"PRIx64" len=3D%"PRIu64" mode=3D%llu errno=3D%i= ", + start, length, uffd_register.mode, errno); + return -1; + } + + return 0; +} + +/** + * uffd_protect_memory: protect/unprotect memory range for writes with UFFD + * + * Returns 0 on success or negative value in case of error + * + * @uffd: UFFD file descriptor + * @start: starting virtual address of memory range + * @length: length of memory range + * @wp: write-protect/unprotect + */ +static int uffd_protect_memory(int uffd, hwaddr start, hwaddr length, bool= wp) +{ + struct uffdio_writeprotect uffd_writeprotect; + int res; + + uffd_writeprotect.range.start =3D start; + uffd_writeprotect.range.len =3D length; + uffd_writeprotect.mode =3D (wp ? UFFDIO_WRITEPROTECT_MODE_WP : 0); + + do { + res =3D ioctl(uffd, UFFDIO_WRITEPROTECT, &uffd_writeprotect); + } while (res < 0 && errno =3D=3D EINTR); + if (res < 0) { + error_report("uffd_protect_memory() failed: " + "start=3D%0"PRIx64" len=3D%"PRIu64" mode=3D%llu errno=3D%i= ", + start, length, uffd_writeprotect.mode, errno); + return -1; + } + + return 0; +} + +__attribute__ ((unused)) +static int uffd_read_events(int uffd, struct uffd_msg *msgs, int count); +__attribute__ ((unused)) +static bool uffd_poll_events(int uffd, int tmo); + +/** + * uffd_read_events: read pending UFFD events + * + * Returns number of fetched messages, 0 if non is available or + * negative value in case of an error + * + * @uffd: UFFD file descriptor + * @msgs: pointer to message buffer + * @count: number of messages that can fit in the buffer + */ +static int uffd_read_events(int uffd, struct uffd_msg *msgs, int count) +{ + ssize_t res; + do { + res =3D read(uffd, msgs, count * sizeof(struct uffd_msg)); + } while (res < 0 && errno =3D=3D EINTR); + + if ((res < 0 && errno =3D=3D EAGAIN)) { + return 0; + } + if (res < 0) { + error_report("uffd_read_events() failed: errno=3D%i", errno); + return -1; + } + + return (int) (res / sizeof(struct uffd_msg)); +} + +/** + * uffd_poll_events: poll UFFD file descriptor for read + * + * Returns true if events are available for read, false otherwise + * + * @uffd: UFFD file descriptor + * @tmo: timeout in milliseconds, 0 for non-blocking operation, + * negative value for infinite wait + */ +static bool uffd_poll_events(int uffd, int tmo) +{ + int res; + struct pollfd poll_fd =3D { .fd =3D uffd, .events =3D POLLIN, .revents= =3D 0 }; + + do { + res =3D poll(&poll_fd, 1, tmo); + } while (res < 0 && errno =3D=3D EINTR); + + if (res =3D=3D 0) { + return false; + } + if (res < 0) { + error_report("uffd_poll_events() failed: errno=3D%i", errno); + return false; + } + + return (poll_fd.revents & POLLIN) !=3D 0; +} + static bool do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *= block, ram_addr_t offset, uint8_t *source_buf); =20 @@ -3788,6 +3971,90 @@ static int ram_resume_prepare(MigrationState *s, voi= d *opaque) return 0; } =20 +/** + * 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) +{ + int uffd; + RAMState *rs =3D ram_state; + RAMBlock *bs; + + /* Open UFFD file descriptor */ + uffd =3D uffd_create_fd(); + if (uffd < 0) { + return uffd; + } + rs->uffdio_fd =3D uffd; + + 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, (hwaddr) bs->host, + bs->max_length, false, true)) { + goto fail; + } + /* Apply UFFD write protection to the block memory range */ + if (uffd_protect_memory(rs->uffdio_fd, (hwaddr) bs->host, + bs->max_length, true)) { + goto fail; + } + bs->flags |=3D RAM_UF_WRITEPROTECT; + + info_report("UFFD-WP write-tracking enabled: " + "block_id=3D%s page_size=3D%zu start=3D%p length=3D%lu " + "romd_mode=3D%i ram=3D%i readonly=3D%i nonvolatile=3D%i ro= m_device=3D%i", + bs->idstr, bs->page_size, bs->host, bs->max_length, + bs->mr->romd_mode, bs->mr->ram, bs->mr->readonly, + bs->mr->nonvolatile, bs->mr->rom_device); + } + + return 0; + +fail: + uffd_close_fd(uffd); + rs->uffdio_fd =3D -1; + return -1; +} + +/** + * ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protec= tion + */ +void ram_write_tracking_stop(void) +{ + RAMState *rs =3D ram_state; + RAMBlock *bs; + assert(rs->uffdio_fd >=3D 0); + + RAMBLOCK_FOREACH_NOT_IGNORED(bs) { + if ((bs->flags & RAM_UF_WRITEPROTECT) =3D=3D 0) { + continue; + } + info_report("UFFD-WP write-tracking disabled: " + "block_id=3D%s page_size=3D%zu start=3D%p length=3D%lu " + "romd_mode=3D%i ram=3D%i readonly=3D%i nonvolatile=3D%i ro= m_device=3D%i", + bs->idstr, bs->page_size, bs->host, bs->max_length, + bs->mr->romd_mode, bs->mr->ram, bs->mr->readonly, + bs->mr->nonvolatile, bs->mr->rom_device); + /* Cleanup flags */ + bs->flags &=3D ~RAM_UF_WRITEPROTECT; + } + + /* + * Close UFFD file descriptor to remove protection, + * release registered memory regions and flush wait queues + */ + uffd_close_fd(rs->uffdio_fd); + rs->uffdio_fd =3D -1; +} + static SaveVMHandlers savevm_ram_handlers =3D { .save_setup =3D ram_save_setup, .save_live_iterate =3D ram_save_iterate, diff --git a/migration/ram.h b/migration/ram.h index 011e85414e..3611cb51de 100644 --- a/migration/ram.h +++ b/migration/ram.h @@ -79,4 +79,8 @@ void colo_flush_ram_cache(void); void colo_release_ram_cache(void); void colo_incoming_start_dirty_log(void); =20 +/* Live snapshots */ +int ram_write_tracking_start(void); +void ram_write_tracking_stop(void); + #endif --=20 2.25.1 From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605790949; cv=none; d=zohomail.com; s=zohoarc; b=RG2Rjlmra2SRbEXapaqTOnkpU8ozGxo8hXFRYZ82EAWacx+FVoycQda/Nm9P3kYf8JQQ5QxjtRYu0/Tr8zA9uDt6P+B8xv4cicejOPhNV8G5dC5H48ZfJ+a2ZuMFOefSI3/hLVQ/m/qZmnCblQgvPkXl8k0+iGPCU0NBM42YOXk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605790949; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=d853B4XUjCahlTXjpCvO1NTvtmJU66YzE8N9+IrubF0=; b=eiVpArl6PROM/uy5F2cwdEGujjxxZ7y6Hqe9lz87NDF5/BDrM29N9fwOhH+SI1c6+UIhd8fFHRcNEQJo5tcLBiECJITrGAnwEsAnzkJsHq/lyXfQ2tKe+ck8/RdATggbKK+CMN+1gnP9LWYz0+lvcMOiDdKssT+uZwv9XQ8zib4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605790949186759.4741274453631; Thu, 19 Nov 2020 05:02:29 -0800 (PST) Received: from localhost ([::1]:58172 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfjZz-0007PS-T7 for importer@patchew.org; Thu, 19 Nov 2020 08:02:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55074) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjY9-0005eo-EK for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:34 -0500 Received: from relay.sw.ru ([185.231.240.75]:49450 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjY5-00008q-KE for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:33 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjXq-009NTF-Mz; Thu, 19 Nov 2020 16:00:14 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 3/7] support UFFD write fault processing in ram_save_iterate() Date: Thu, 19 Nov 2020 15:59:36 +0300 Message-Id: <20201119125940.20017-4-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" 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. 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 --- migration/ram.c | 124 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 115 insertions(+), 9 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index 7f273c9996..08a1d7a252 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -314,6 +314,8 @@ struct RAMState { ram_addr_t last_page; /* last ram version we have seen */ uint32_t last_version; + /* 'write-tracking' migration is enabled */ + bool ram_wt_enabled; /* We are in the first round */ bool ram_bulk_stage; /* The free page optimization is enabled */ @@ -574,8 +576,6 @@ static int uffd_protect_memory(int uffd, hwaddr start, = hwaddr length, bool wp) return 0; } =20 -__attribute__ ((unused)) -static int uffd_read_events(int uffd, struct uffd_msg *msgs, int count); __attribute__ ((unused)) static bool uffd_poll_events(int uffd, int tmo); =20 @@ -1929,6 +1929,86 @@ static int ram_save_host_page(RAMState *rs, PageSear= chStatus *pss, return pages; } =20 +/** + * ram_find_block_by_host_address: find RAM block containing host page + * + * Returns true if RAM block is found and pss->block/page are + * pointing to the given host page, false in case of an error + * + * @rs: current RAM state + * @pss: page-search-status structure + */ +static bool ram_find_block_by_host_address(RAMState *rs, PageSearchStatus = *pss, + hwaddr page_address) +{ + bool found =3D false; + + pss->block =3D rs->last_seen_block; + do { + if (page_address >=3D (hwaddr) pss->block->host && + (page_address + TARGET_PAGE_SIZE) <=3D + ((hwaddr) pss->block->host + pss->block->used_length))= { + pss->page =3D (unsigned long) + ((page_address - (hwaddr) pss->block->host) >> TARGET_= PAGE_BITS); + found =3D true; + break; + } + + pss->block =3D QLIST_NEXT_RCU(pss->block, next); + if (!pss->block) { + /* Hit the end of the list */ + pss->block =3D QLIST_FIRST_RCU(&ram_list.blocks); + } + } while (pss->block !=3D rs->last_seen_block); + + rs->last_seen_block =3D pss->block; + /* + * Since we are in the same loop with ram_find_and_save_block(), + * need to reset pss->complete_round after switching to + * other block/page in pss. + */ + pss->complete_round =3D false; + + return found; +} + +/** + * get_fault_page: try to get next UFFD write fault page and, if pending f= ault + * is found, put info about RAM block and block page into pss structure + * + * Returns true in case of UFFD write fault detected, false otherwise + * + * @rs: current RAM state + * @pss: page-search-status structure + * + */ +static bool get_fault_page(RAMState *rs, PageSearchStatus *pss) +{ + struct uffd_msg uffd_msg; + hwaddr page_address; + int res; + + if (!rs->ram_wt_enabled) { + return false; + } + + res =3D uffd_read_events(rs->uffdio_fd, &uffd_msg, 1); + if (res <=3D 0) { + return false; + } + + page_address =3D uffd_msg.arg.pagefault.address; + if (!ram_find_block_by_host_address(rs, pss, page_address)) { + /* In case we couldn't find respective block, just unprotect fault= ing page */ + uffd_protect_memory(rs->uffdio_fd, page_address, TARGET_PAGE_SIZE,= false); + error_report("ram_find_block_by_host_address() failed: address=3D0= x%0lx", + page_address); + return false; + } + + return true; +} + /** * ram_find_and_save_block: finds a dirty page and sends it to f * @@ -1955,25 +2035,50 @@ static int ram_find_and_save_block(RAMState *rs, bo= ol last_stage) return pages; } =20 + if (!rs->last_seen_block) { + rs->last_seen_block =3D QLIST_FIRST_RCU(&ram_list.blocks); + } pss.block =3D rs->last_seen_block; pss.page =3D rs->last_page; pss.complete_round =3D false; =20 - if (!pss.block) { - pss.block =3D QLIST_FIRST_RCU(&ram_list.blocks); - } - do { + ram_addr_t page; + ram_addr_t page_to; + again =3D true; - found =3D get_queued_page(rs, &pss); - + /* In case of 'write-tracking' migration we first try + * to poll UFFD and get write page fault event */ + found =3D get_fault_page(rs, &pss); + if (!found) { + /* No trying to fetch something from the priority queue */ + found =3D get_queued_page(rs, &pss); + } if (!found) { /* priority queue empty, so just search for something dirty */ found =3D find_dirty_block(rs, &pss, &again); } =20 if (found) { + page =3D pss.page; pages =3D ram_save_host_page(rs, &pss, last_stage); + page_to =3D pss.page; + + /* Check if page is from UFFD-managed region */ + if (pss.block->flags & RAM_UF_WRITEPROTECT) { + hwaddr page_address =3D (hwaddr) pss.block->host + + ((hwaddr) page << TARGET_PAGE_BITS); + hwaddr run_length =3D (hwaddr) (page_to - page + 1) << TAR= GET_PAGE_BITS; + int res; + + /* Flush async buffers before un-protect */ + qemu_fflush(rs->f); + /* Un-protect memory range */ + res =3D uffd_protect_memory(rs->uffdio_fd, page_address, r= un_length, false); + if (res < 0) { + break; + } + } } } while (!pages && again); =20 @@ -2086,7 +2191,8 @@ static void ram_state_reset(RAMState *rs) rs->last_sent_block =3D NULL; rs->last_page =3D 0; rs->last_version =3D ram_list.version; - rs->ram_bulk_stage =3D true; + rs->ram_wt_enabled =3D migrate_track_writes_ram(); + rs->ram_bulk_stage =3D !rs->ram_wt_enabled; rs->fpo_enabled =3D false; } =20 --=20 2.25.1 From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605790949; cv=none; d=zohomail.com; s=zohoarc; b=Xt7TfN/MN0CBu0KXlAweosik7zvqIldW01x/OG4YgzQ8gZx82N2x5ZfW9GnaVWL/1wNVoR8agyjooURMDNOKlG+SS1271ZRZS9L9s8gAHQQeEXcrEba/63HA4Fh2FJRBMXVjEK4qQbzwB/b1bsf0esoURQIiMdxyrhuGyuYESNU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605790949; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=lqfcOTCbeMhmzQ88SxaWtjnPMrfvD+ujf+vkRw+jHTA=; b=WLbacLEX01Hx7/Jxy5fbA0j6vKiELZMCmzRDwe+GQE7f4xj/4/0Of+LQDR8nCowtJMwKXKsJ1fkDOSYeLxvn1FkLwFsycrEwfAWMuzvaJjti1atBKps8bpc2n089pAZc+QcKk/jFsr1mM0tN8RVg4kbIRzXepkf9U+gpHJYOoak= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605790949805540.5353517031557; Thu, 19 Nov 2020 05:02:29 -0800 (PST) Received: from localhost ([::1]:58248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfja0-0007R4-FG for importer@patchew.org; Thu, 19 Nov 2020 08:02:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55182) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYL-0005j9-KO for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:45 -0500 Received: from relay.sw.ru ([185.231.240.75]:49536 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYI-0000Fe-VC for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:44 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjY2-009NTF-Gh; Thu, 19 Nov 2020 16:00:26 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 4/7] implementation of write-tracking migration thread Date: Thu, 19 Nov 2020 15:59:37 +0300 Message-Id: <20201119125940.20017-5-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URG_BIZ=0.573 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" Signed-off-by: Andrey Gruzdev --- migration/migration.c | 168 +++++++++++++++++++++++++++++++++++++++++- migration/migration.h | 3 + migration/savevm.c | 1 - migration/savevm.h | 2 + 4 files changed, 171 insertions(+), 3 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index ff0364dde0..158e5441ec 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -2013,6 +2013,7 @@ void migrate_init(MigrationState *s) * locks. */ s->cleanup_bh =3D 0; + s->wt_vm_start_bh =3D 0; s->to_dst_file =3D NULL; s->state =3D MIGRATION_STATUS_NONE; s->rp_state.from_dst_file =3D NULL; @@ -3551,6 +3552,21 @@ static void migration_iteration_finish(MigrationStat= e *s) qemu_mutex_unlock_iothread(); } =20 +static void wt_migration_iteration_finish(MigrationState *s) +{ + /* TODO: implement */ +} + +/* + * Return true if continue to the next iteration directly, false + * otherwise. + */ +static MigIterateState wt_migration_iteration_run(MigrationState *s) +{ + /* TODO: implement */ + return MIG_ITERATE_RESUME; +} + void migration_make_urgent_request(void) { qemu_sem_post(&migrate_get_current()->rate_limit_sem); @@ -3698,6 +3714,148 @@ static void *migration_thread(void *opaque) return NULL; } =20 +static void wt_migration_vm_start_bh(void *opaque) +{ + /* TODO: implement */ +} + +/* + * Master migration thread on the source VM. + * This is an alternative implementation of live migration + * which uses userfault_fd write protection mechanism introduced in + * 5.7 kernel. Compared to existing dirty page logging migration + * it produces much lesser traffic and smaller snapshot images since + * no page duplicates can get into the stream. Another the key point + * is that generated vmstate stream reflects machine state 'frozen' + * at the beginning of migration compared to dirty page logging + * mechanism, which effectively results in that saved snapshot is the + * state at the end of migration process. + */ +static void *wt_migration_thread(void *opaque) +{ + MigrationState *s =3D opaque; + int64_t setup_start; + MigThrError thr_error; + QEMUFile *fb; + bool early_fail =3D true; + + rcu_register_thread(); + object_ref(OBJECT(s)); + + qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); + + setup_start =3D 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 =3D qio_channel_buffer_new(128 * 1024); + qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer"); + fb =3D 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 =3D=3D 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); + } + s->setup_time =3D qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; + + migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, + MIGRATION_STATUS_ACTIVE); + trace_migration_thread_setup_complete(); + s->downtime_start =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); + + qemu_mutex_lock_iothread(); + + 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 =3D 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->wt_vm_start_bh =3D qemu_bh_new(wt_migration_vm_start_bh, s); + qemu_bh_schedule(s->wt_vm_start_bh); + + qemu_mutex_unlock_iothread(); + + while (migration_is_active(s)) { + MigIterateState iter_state =3D wt_migration_iteration_run(s); + if (iter_state =3D=3D MIG_ITERATE_SKIP) { + continue; + } else if (iter_state =3D=3D MIG_ITERATE_BREAK) { + break; + } + + /* + * Try to detect any kind of failures, and see whether we + * should stop the migration now. + */ + thr_error =3D migration_detect_error(s); + if (thr_error =3D=3D 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(); + } + + wt_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 =3D NULL; @@ -3761,8 +3919,14 @@ void migrate_fd_connect(MigrationState *s, Error *er= ror_in) migrate_fd_cleanup(s); return; } - qemu_thread_create(&s->thread, "live_migration", migration_thread, s, - QEMU_THREAD_JOINABLE); + + if (migrate_track_writes_ram()) { + qemu_thread_create(&s->thread, "wt_live_migration", + wt_migration_thread, s, QEMU_THREAD_JOINABLE); + } else { + qemu_thread_create(&s->thread, "live_migration", + migration_thread, s, QEMU_THREAD_JOINABLE); + } s->migration_thread_running =3D true; } =20 diff --git a/migration/migration.h b/migration/migration.h index 339ae720e0..c3b4c7f2fd 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -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" =20 @@ -147,8 +148,10 @@ struct MigrationState { =20 /*< public >*/ QemuThread thread; + QEMUBH *wt_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 diff --git a/migration/savevm.c b/migration/savevm.c index 5f937a2762..62d5f8a869 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -1352,7 +1352,6 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUF= ile *f, bool in_postcopy) return 0; } =20 -static int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, bool in_postcopy, bool inactivate_disks) diff --git a/migration/savevm.h b/migration/savevm.h index ba64a7e271..aaee2528ed 100644 --- a/migration/savevm.h +++ b/migration/savevm.h @@ -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); =20 #endif --=20 2.25.1 From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605791163; cv=none; d=zohomail.com; s=zohoarc; b=iDwrB+GmlsZ06XUoh9SFRAwaA0UMClyvzUzU5oqFuvOgJ8Ga0gAklSV+03HULG71YGEWMiFTVj/5GGYbflETCGrDfXQjb7LA/hmp404dI/FXiaHSot7N9+kD77R8Vhhc+0M5dPm1EhPoheaNNpYvrpmfBIeIR1/4jICHI/VUBYs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605791163; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=jwuhogBOVG2YPzgFXL4k5rOvzTWOsKXrmebvhxlROYo=; b=dXBfTNgpVWT3PS5RjLVoBuiptPfpYHmaE9zRryXwxXKQSK/R9JTWbyGofTCIB33aywW2r5yLKk2E6H+xXunuXsf6pv3Fmoe16Qe9q2+43TbMfLIZEEnSv+qZKfd5LfrUbbHmvtLqIZgH3Gp9z6yZJF9JmwgQfboUKoKCcFvAtic= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605791163308147.46987751899667; Thu, 19 Nov 2020 05:06:03 -0800 (PST) Received: from localhost ([::1]:42002 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfjdS-0003zZ-6w for importer@patchew.org; Thu, 19 Nov 2020 08:06:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55204) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYY-0005wt-3W for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:58 -0500 Received: from relay.sw.ru ([185.231.240.75]:49606 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYS-0000LL-Mw for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:00:57 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjYE-009NTF-9v; Thu, 19 Nov 2020 16:00:38 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 5/7] implementation of vm_start() BH Date: Thu, 19 Nov 2020 15:59:38 +0300 Message-Id: <20201119125940.20017-6-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" To avoid saving updated versions of memory pages we need to start tracking RAM writes before we resume operation of vCPUs. This sequence is especially critical for virtio device backends whos VQs are mapped to main memory and accessed directly not using MMIO callbacks. One problem is that vm_start() routine makes calls state change notifier callbacks directly from itself. Virtio drivers do some stuff with syncing/flusing VQs in its notifier routines. Since we poll UFFD and process faults on the same thread, that leads to the situation when the thread locks in vm_start() if we try to call it from the migration thread. The solution is to call ram_write_tracking_start() directly from migration thread and then schedule BH for vm_start. Signed-off-by: Andrey Gruzdev --- migration/migration.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/migration/migration.c b/migration/migration.c index 158e5441ec..dba388f8bd 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -3716,7 +3716,13 @@ static void *migration_thread(void *opaque) =20 static void wt_migration_vm_start_bh(void *opaque) { - /* TODO: implement */ + MigrationState *s =3D opaque; + + qemu_bh_delete(s->wt_vm_start_bh); + s->wt_vm_start_bh =3D NULL; + + vm_start(); + s->downtime =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - s->downtime_s= tart; } =20 /* --=20 2.25.1 From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605791082; cv=none; d=zohomail.com; s=zohoarc; b=SuGh/YWYmx1FWqNFKw8T9xKwRWj19hBHqfExAZsm3z06SNi6zsibiB/CVDZk79FGE8S/9H3XSpogl0PNC+qoI2AtF1Po8cgWfffyfO1doR7FtQINqhITNHuM45IxynfCa8gyuHNgebMaA4yMLSUczj5IXFr1CW/BdODBJIBmWRk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605791082; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=VHyTcNPfXYQlelX4/Aue0EZ90Xbn8sCg7BLVFX/64KE=; b=DPTQzweMq53H+w4Ts/AFHFbac+q/OmXAi8Eu/PF8ZwGZ+VVjmUkzgYLARtKA6oGCbTnNQRLqfrYeaiAwzB1QlGPtg979YLsYCl1Ajxj1VLM5sPc5G//NM1rSeC194nYo+B+Qe0QirshCa90yoFS2Ds7RNra7BLNuSb0DABTdARw= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605791082529791.6283631206675; Thu, 19 Nov 2020 05:04:42 -0800 (PST) Received: from localhost ([::1]:38038 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfjc9-0002GJ-AC for importer@patchew.org; Thu, 19 Nov 2020 08:04:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55228) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYg-0006Cy-Ky for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:01:06 -0500 Received: from relay.sw.ru ([185.231.240.75]:49698 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYe-0000SV-JT for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:01:06 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjYQ-009NTF-3Y; Thu, 19 Nov 2020 16:00:50 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 6/7] the rest of write tracking migration code Date: Thu, 19 Nov 2020 15:59:39 +0300 Message-Id: <20201119125940.20017-7-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" Signed-off-by: Andrey Gruzdev --- migration/migration.c | 72 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index dba388f8bd..ccb451b238 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -3212,6 +3212,48 @@ fail: MIGRATION_STATUS_FAILED); } =20 +/** + * wt_migration_completion: Used by wt_migration_thread when after all the + * RAM has been saved. The caller 'breaks' the loop when this returns. + * + * @s: Current migration state + */ +static void wt_migration_completion(MigrationState *s) +{ + int current_active_state =3D s->state; + + /* Stop tracking RAM writes - un-protect memory, un-register UFFD memo= ry ranges, + * flush kernel wait queues and wake up threads waiting for write faul= t to be + * resolved. All of this is essentially done by closing UFFD file desc= riptor */ + ram_write_tracking_stop(); + + if (s->state =3D=3D MIGRATION_STATUS_ACTIVE) { + /* + * By this moment we have RAM content saved into the migration str= eam. + * The next step is to flush the non-RAM content (device state) + * right after the ram content. The device state has been stored i= nto + * 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 =3D=3D 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 =3D migrate_get_current(); @@ -3554,7 +3596,26 @@ static void migration_iteration_finish(MigrationStat= e *s) =20 static void wt_migration_iteration_finish(MigrationState *s) { - /* TODO: implement */ + 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(); } =20 /* @@ -3563,7 +3624,14 @@ static void wt_migration_iteration_finish(MigrationS= tate *s) */ static MigIterateState wt_migration_iteration_run(MigrationState *s) { - /* TODO: implement */ + int res; + + res =3D qemu_savevm_state_iterate(s->to_dst_file, false); + if (res) { + wt_migration_completion(s); + return MIG_ITERATE_BREAK; + } + return MIG_ITERATE_RESUME; } =20 --=20 2.25.1 From nobody Thu Mar 28 11:12:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1605791065; cv=none; d=zohomail.com; s=zohoarc; b=MfwPkh79mTOsr3qvQN8089HWnzYxuXHFat6/u9DxSDeigqTsgE53SLdBC3amo8uVUVkdM9Dh4m8JH41iR4bdytZ0wTcovIsECk80osJ8R8U13Tq51UKk7nDWwBNMRlLqR3Bvf3+tcbPSFpN6rnuLQ7fC+/Mcz/oXpeWz+nvbCoc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605791065; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=kru5WCFOievLHpFxMgfVqNFWj8UtYZVuGp0vCUAyO1o=; b=Fd/vXenDDVwVBYi9UlCNWst8W0evppbb0CtztSg9wT/h6xKOuptAmJuVKNU/EZoHvv4nLbEZVS3ZBNG44Ey11iOgX3eK0YVBIvw0LJr+oxHXUVp41NXzi33p3vhfpZs2eTqJQuRUwq2qPOT0AQWSwFaIxMDLXRLPZVHkL9eSVOU= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1605791065541647.6396097327881; Thu, 19 Nov 2020 05:04:25 -0800 (PST) Received: from localhost ([::1]:36698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfjbs-0001i4-Gd for importer@patchew.org; Thu, 19 Nov 2020 08:04:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55300) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYs-0006XH-NF for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:01:18 -0500 Received: from relay.sw.ru ([185.231.240.75]:49772 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfjYq-0000VI-AF for qemu-devel@nongnu.org; Thu, 19 Nov 2020 08:01:18 -0500 Received: from [192.168.15.221] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfjYb-009NTF-TE; Thu, 19 Nov 2020 16:01:01 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Peter Xu , Andrey Gruzdev Subject: [PATCH v3 7/7] introduce simple linear scan rate limiting mechanism Date: Thu, 19 Nov 2020 15:59:40 +0300 Message-Id: <20201119125940.20017-8-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> References: <20201119125940.20017-1-andrey.gruzdev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.gruzdev@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/19 07:59:53 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Gruzdev From: Andrey Gruzdev via Content-Type: text/plain; charset="utf-8" Since reading UFFD events and saving paged data are performed from the same thread, write fault latencies are sensitive to migration stream stalls. Limiting total page saving rate is a method to reduce amount of noticiable fault resolution latencies. Migration bandwidth limiting is achieved via noticing cases of out-of-threshold write fault latencies and temporarily disabling (strictly speaking, severely throttling) saving non-faulting pages. Signed-off-by: Andrey Gruzdev --- migration/ram.c | 58 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index 08a1d7a252..89fe106585 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -325,6 +325,10 @@ struct RAMState { /* these variables are used for bitmap sync */ /* last time we did a full bitmap_sync */ int64_t time_last_bitmap_sync; + /* last time UFFD fault occured */ + int64_t last_fault_ns; + /* linear scan throttling counter */ + int throttle_skip_counter; /* bytes transferred at start_time */ uint64_t bytes_xfer_prev; /* number of dirty pages since start_time */ @@ -576,9 +580,6 @@ static int uffd_protect_memory(int uffd, hwaddr start, = hwaddr length, bool wp) return 0; } =20 -__attribute__ ((unused)) -static bool uffd_poll_events(int uffd, int tmo); - /** * uffd_read_events: read pending UFFD events * @@ -2006,9 +2007,51 @@ static bool get_fault_page(RAMState *rs, PageSearchS= tatus *pss) return false; } =20 + rs->last_fault_ns =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); return true; } =20 +#define FAULT_HIGH_LATENCY_NS 5000000 /* 5 ms */ +#define SLOW_FAULT_POLL_TMO 5 /* 5 ms */ +#define SLOW_FAULT_SKIP_PAGES 200 + +/** + * limit_scan_rate: limit RAM linear scan rate in case of growing write fa= ult + * latencies, used in write-tracking migration implementation + * + * @rs: current RAM state + * + */ +static void limit_scan_rate(RAMState *rs) +{ + int64_t last_fault_latency_ns =3D 0; + + if (!rs->ram_wt_enabled) { + return; + } + + /* Check if last write fault time is available */ + if (rs->last_fault_ns) { + last_fault_latency_ns =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - + rs->last_fault_ns; + rs->last_fault_ns =3D 0; + } + + /* In case last fault time was available and we have + * latency value, check if it's not too high */ + if (last_fault_latency_ns > FAULT_HIGH_LATENCY_NS) { + /* Reset counter after each slow write fault */ + rs->throttle_skip_counter =3D SLOW_FAULT_SKIP_PAGES; + } + /* Delay thread execution till next write fault occures or timeout exp= ires. + * Next SLOW_FAULT_SKIP_PAGES can be write fault pages only, not from = pages going from + * linear scan logic. Thus we moderate migration stream rate to reduce= latencies */ + if (rs->throttle_skip_counter > 0) { + uffd_poll_events(rs->uffdio_fd, SLOW_FAULT_POLL_TMO); + rs->throttle_skip_counter--; + } +} + /** * ram_find_and_save_block: finds a dirty page and sends it to f * @@ -2078,6 +2121,9 @@ static int ram_find_and_save_block(RAMState *rs, bool= last_stage) if (res < 0) { break; } + + /* Linear scan rate limiting */ + limit_scan_rate(rs); } } } while (!pages && again); @@ -2191,12 +2237,15 @@ static void ram_state_reset(RAMState *rs) rs->last_sent_block =3D NULL; rs->last_page =3D 0; rs->last_version =3D ram_list.version; + rs->last_fault_ns =3D 0; + rs->throttle_skip_counter =3D 0; rs->ram_wt_enabled =3D migrate_track_writes_ram(); rs->ram_bulk_stage =3D !rs->ram_wt_enabled; rs->fpo_enabled =3D false; } =20 #define MAX_WAIT 50 /* ms, half buffered_file limit */ +#define WT_MAX_WAIT 1000 /* 1000 ms, need bigger limit for 'write-tracking= ' migration */ =20 /* * 'expected' is the value you expect the bitmap mostly to be full @@ -2872,7 +2921,8 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) if ((i & 63) =3D=3D 0) { uint64_t t1 =3D (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - = t0) / 1000000; - if (t1 > MAX_WAIT) { + uint64_t max_wait =3D rs->ram_wt_enabled ? WT_MAX_WAIT : M= AX_WAIT; + if (t1 > max_wait) { trace_ram_save_iterate_big_wait(t1, i); break; } --=20 2.25.1