From nobody Sat Feb 7 15:29:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1531929278702158.42723332666094; Wed, 18 Jul 2018 08:54:38 -0700 (PDT) Received: from localhost ([::1]:37316 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ffomj-0005LD-C0 for importer@patchew.org; Wed, 18 Jul 2018 11:54:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52973) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ffoal-0003Hm-IY for qemu-devel@nongnu.org; Wed, 18 Jul 2018 11:42:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ffoag-0000H6-Db for qemu-devel@nongnu.org; Wed, 18 Jul 2018 11:42:15 -0400 Received: from relay.sw.ru ([185.231.240.75]:36398) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ffoag-0000Fn-3K for qemu-devel@nongnu.org; Wed, 18 Jul 2018 11:42:10 -0400 Received: from vz-out.virtuozzo.com ([185.231.240.5] helo=dptest2.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1ffoae-0006bu-Hb; Wed, 18 Jul 2018 18:42:08 +0300 From: Denis Plotnikov To: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com Date: Wed, 18 Jul 2018 18:41:54 +0300 Message-Id: <20180718154200.26777-12-dplotnikov@virtuozzo.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180718154200.26777-1-dplotnikov@virtuozzo.com> References: <20180718154200.26777-1-dplotnikov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v1 11/17] background snapshot: add a memory page copying function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It's the only function making a memory page copy. It supports multithreading semantics ensuring that the page is copied by one thread only and releasing the copied page from write protection. Signed-off-by: Denis Plotnikov --- migration/ram.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ migration/ram.h | 1 + 2 files changed, 57 insertions(+) diff --git a/migration/ram.c b/migration/ram.c index 3c4ccd85b4..10b6fdf23e 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1706,6 +1706,62 @@ RAMBlock *ram_bgs_block_find(uint8_t *address, ram_a= ddr_t *page_offset) return NULL; } =20 +/** + * ram_copy_page: make a page copy + * + * Used in the background snapshot to make a copy of a memeory page. + * Ensures that the memeory page is copied only once. + * When a page copy is done, restores read/write access to the memory + * page. + * If a page is being copied by another thread, wait until the copying + * ends and exit. + * + * Returns: + * -1 - on error + * 0 - the page wasn't copied by the function call + * 1 - the page has been copied + * + * @block: RAM block to use + * @page_nr: the page number to copy + * @page_copy: the pointer to return a page copy + * + */ +int ram_copy_page(RAMBlock *block, unsigned long page_nr, + void **page_copy) +{ + void *host_page; + + if (test_and_set_bit_atomic(page_nr, block->touched_map)) { + while (!test_bit_atomic(page_nr, block->copied_map)) { + /* the page is being copied -- wait for the end of the copying + * and check once again + */ + qemu_event_wait(&ram_state->page_copying_done); + } + return 0; + } + + *page_copy =3D ram_page_buffer_get(); + if (!*page_copy) { + return -1; + } + + host_page =3D block->host + (page_nr << TARGET_PAGE_BITS); + memcpy(*page_copy, host_page, TARGET_PAGE_SIZE); + + if (ram_set_rw(host_page, TARGET_PAGE_SIZE)) { + ram_page_buffer_free(*page_copy); + *page_copy =3D NULL; + return -1; + } + + set_bit_atomic(page_nr, block->copied_map); + qemu_event_set(&ram_state->page_copying_done); + qemu_event_reset(&ram_state->page_copying_done); + + return 1; +} + /** * ram_find_and_save_block: finds a dirty page and sends it to f * diff --git a/migration/ram.h b/migration/ram.h index c3679ba65e..76ab0a3377 100644 --- a/migration/ram.h +++ b/migration/ram.h @@ -75,4 +75,5 @@ int ram_page_buffer_free(void *buffer); int ram_block_list_set_readonly(void); int ram_block_list_set_writable(void); =20 +int ram_copy_page(RAMBlock *block, unsigned long page_nr, void **page_copy= ); #endif --=20 2.17.0