From nobody Mon Feb 9 11:30:35 2026 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=1605699155; cv=none; d=zohomail.com; s=zohoarc; b=YTxPMcN3/Qlk4cDBLDO+LsvdePoXF5ZfctJ4YTVennobATFyalI4rFVqku9Xk0FxiaJ1PemnAp1PIpO3OuEUQvvupLtToURX72GxiJLOT2Jb8tzsSxX4CMqVjbcWp4Jlt/HD7nX7LwcPKSeaRSUA2gvwukcwrEWNu2566FKV2JQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605699155; 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=54kXq/IkzQuJWTA3y42yfVQ5JquLzV8PjQiHKQBeGBU=; b=eFnBOYtv+G/6rN9ry7T0At3SBEXSwSlpLce+W/dotqo0D9SkOFj7Dcfl8UoxyjeqJUGHMiYLXdzUl1FK2xsv0w/eFcYEnXF57TfmdsouaTGyKaFHzb8Vn1Cf/Z2YXBDdJA7gqkRx0+xaTPY6x/HW34Zq9/ONX1/fJMqtKzHf7rI= 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 1605699155055991.5730514699816; Wed, 18 Nov 2020 03:32:35 -0800 (PST) Received: from localhost ([::1]:43134 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfLhR-0007U6-Tl for importer@patchew.org; Wed, 18 Nov 2020 06:32:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36648) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfLY1-000567-H5 for qemu-devel@nongnu.org; Wed, 18 Nov 2020 06:22:51 -0500 Received: from relay.sw.ru ([185.231.240.75]:41860 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 1kfLXq-0003Ok-L3 for qemu-devel@nongnu.org; Wed, 18 Nov 2020 06:22:49 -0500 Received: from [192.168.15.76] (helo=andrey-MS-7B54.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kfLXb-009A89-1X; Wed, 18 Nov 2020 14:22:23 +0300 To: qemu-devel@nongnu.org Cc: Den Lunev , Eric Blake , Paolo Bonzini , Juan Quintela , "Dr . David Alan Gilbert" , Markus Armbruster , Andrey Gruzdev Subject: [PATCH 2/7] Introduced UFFD-WP low-level interface helpers. Implemented support for the whole RAM block memory protection/un-protection. Higher level ram_write_tracking_start() and ram_write_tracking_stop() to start/stop tracking memory writes on the whole VM memory. Date: Wed, 18 Nov 2020 14:22:28 +0300 Message-Id: <20201118112233.264530-3-andrey.gruzdev@virtuozzo.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201118112233.264530-1-andrey.gruzdev@virtuozzo.com> References: <20201118112233.264530-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/18 06:22:34 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 --- 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