From nobody Tue Oct 28 12:10:48 2025 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; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 151504602615257.083298595728365; Wed, 3 Jan 2018 22:07:06 -0800 (PST) Received: from localhost ([::1]:47473 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eWyg9-0007go-6o for importer@patchew.org; Thu, 04 Jan 2018 01:07:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36909) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eWybR-0003jf-R2 for qemu-devel@nongnu.org; Thu, 04 Jan 2018 01:02:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eWybO-0004BD-N4 for qemu-devel@nongnu.org; Thu, 04 Jan 2018 01:02:09 -0500 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:42367) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eWybO-0004AX-EU for qemu-devel@nongnu.org; Thu, 04 Jan 2018 01:02:06 -0500 Received: by mail-pf0-x243.google.com with SMTP id d23so369671pfe.9 for ; Wed, 03 Jan 2018 22:02:06 -0800 (PST) Received: from localhost.localdomain (120.236.201.35.bc.googleusercontent.com. [35.201.236.120]) by smtp.gmail.com with ESMTPSA id w5sm6214775pfi.74.2018.01.03.22.02.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 03 Jan 2018 22:02:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=VAikSzTF03wKyXAuXZOfmDeIsQ3AD0B+rsnNTHsnv2c=; b=BjdUDQygF8fb6+T6NVVUkmX5RaLNu+er8A4ppzdpBaRSuUUocOew90BWhbR0e4KSxL MLmM4Lfy2oJUlmv+WgQifj/rYNlB7zrYpPHWNKKXOp0HNoCmebcMt4Uk1oysXmPYlTfq Cxrs32PMbmvS7ZL4eXhdrGX5hGhSJFaoM/7vhGG+ocLP12x7b3SqPl72GRg9rjoJoz9o +0SJeZBRBF3PtujhRl5dnCruQS64fiXXfZT/qOKl5SgleAFG8LaLjU3ZZtAROG8C60ga B4P9AWNRlkK9SS1SzLAzEcB4Qv4hqZRu2Emmu9n8DGdCRuEdk4egj2SLT61Ms+zItAFs iJ6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=VAikSzTF03wKyXAuXZOfmDeIsQ3AD0B+rsnNTHsnv2c=; b=pvX6tHP38ZS55p/0sT93r4e1AAmt3hSbWkfryWGTX7mMvjgqoIccLnXc/Vj2N3dk/b Ynq2xpTIoVJXxGcyeqnryIX6ESPJpDpB1fq1oKiicL4+UjzB8EcIz+jUq/qm7ucgVil8 wACJCYnb805u2t+wIDF5+5o7Kwpuz0wD2RRHRjJE2Z/p1GV0P54wD4sYOqBYc5bz5CeO 7sO3yfVIq/XFItFlq9W7TqK2QkgDxWsLEtTFR3oCW090EInGOGjPIa9HPr8XLre6CwZ1 snz5zw8pnu+1pr8byT+neXCC/xzQuIMKoUXCKAh8Q0zvbwD+4O3T+yi9fHWLF3FLoGMu NMwQ== X-Gm-Message-State: AKGB3mIWXWnscCkJzqc0vsNgfLH3By4wfVCANtLAXHcPSRaqBVyJbbt4 u3VGQ5KK0F8e7XR7vUlF+n5EOo7U X-Google-Smtp-Source: ACJfBosUu21ql7TYT+7aXP+M8bzTF/yCuw9k2VL/3Jqg8zqvNI712arLZfPKBpqjYlqCEnb2V6Xuyw== X-Received: by 10.98.102.74 with SMTP id a71mr3659090pfc.184.1515045725098; Wed, 03 Jan 2018 22:02:05 -0800 (PST) From: Zhang Chen To: qemu devel Date: Thu, 4 Jan 2018 14:01:06 +0800 Message-Id: <1515045675-6993-8-git-send-email-zhangckid@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515045675-6993-1-git-send-email-zhangckid@gmail.com> References: <1515045675-6993-1-git-send-email-zhangckid@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH RESEND V3 07/16] COLO: Load dirty pages into SVM's RAM cache firstly 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: Zhang Chen , zhanghailiang , Li Zhijian , "Dr . David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: zhanghailiang We should not load PVM's state directly into SVM, because there maybe some errors happen when SVM is receving data, which will break SVM. We need to ensure receving all data before load the state into SVM. We use an extra memory to cache these data (PVM's ram). The ram cache in secondary= side is initially the same as SVM/PVM's memory. And in the process of checkpoint, we cache the dirty pages of PVM into this ram cache firstly, so this ram ca= che always the same as PVM's memory at every checkpoint, then we flush this cac= hed ram to SVM after we receive all PVM's state. Cc: Dr. David Alan Gilbert Signed-off-by: zhanghailiang Signed-off-by: Li Zhijian Signed-off-by: Zhang Chen --- include/exec/ram_addr.h | 1 + migration/migration.c | 2 + migration/ram.c | 97 +++++++++++++++++++++++++++++++++++++++++++++= ++-- migration/ram.h | 4 ++ migration/savevm.c | 2 +- 5 files changed, 102 insertions(+), 4 deletions(-) diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 6cbc02a..6b7b0dd 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -27,6 +27,7 @@ struct RAMBlock { struct rcu_head rcu; struct MemoryRegion *mr; uint8_t *host; + uint8_t *colo_cache; /* For colo, VM's ram cache */ ram_addr_t offset; ram_addr_t used_length; ram_addr_t max_length; diff --git a/migration/migration.c b/migration/migration.c index 8c16129..315b6d4 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -382,6 +382,8 @@ static void process_incoming_migration_co(void *opaque) =20 /* Wait checkpoint incoming thread exit before free resource */ qemu_thread_join(&mis->colo_incoming_thread); + /* We hold the global iothread lock, so it is safe here */ + colo_release_ram_cache(); } =20 if (ret < 0) { diff --git a/migration/ram.c b/migration/ram.c index 021d583..0fc0aee 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -2466,6 +2466,20 @@ static inline void *host_from_ram_block_offset(RAMBl= ock *block, return block->host + offset; } =20 +static inline void *colo_cache_from_block_offset(RAMBlock *block, + ram_addr_t offset) +{ + if (!offset_in_ramblock(block, offset)) { + return NULL; + } + if (!block->colo_cache) { + error_report("%s: colo_cache is NULL in block :%s", + __func__, block->idstr); + return NULL; + } + return block->colo_cache + offset; +} + /** * ram_handle_compressed: handle the zero page case * @@ -2619,6 +2633,55 @@ static void decompress_data_with_multi_threads(QEMUF= ile *f, qemu_mutex_unlock(&decomp_done_lock); } =20 +/* + * colo cache: this is for secondary VM, we cache the whole + * memory of the secondary VM, it is need to hold the global lock + * to call this helper. + */ +int colo_init_ram_cache(void) +{ + RAMBlock *block; + + rcu_read_lock(); + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { + block->colo_cache =3D qemu_anon_ram_alloc(block->used_length, NULL= ); + if (!block->colo_cache) { + error_report("%s: Can't alloc memory for COLO cache of block %= s," + "size 0x" RAM_ADDR_FMT, __func__, block->idstr, + block->used_length); + goto out_locked; + } + } + rcu_read_unlock(); + return 0; + +out_locked: + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { + if (block->colo_cache) { + qemu_anon_ram_free(block->colo_cache, block->used_length); + block->colo_cache =3D NULL; + } + } + + rcu_read_unlock(); + return -errno; +} + +/* It is need to hold the global lock to call this helper */ +void colo_release_ram_cache(void) +{ + RAMBlock *block; + + rcu_read_lock(); + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { + if (block->colo_cache) { + qemu_anon_ram_free(block->colo_cache, block->used_length); + block->colo_cache =3D NULL; + } + } + rcu_read_unlock(); +} + /** * ram_load_setup: Setup RAM for migration incoming side * @@ -2632,6 +2695,7 @@ static int ram_load_setup(QEMUFile *f, void *opaque) xbzrle_load_setup(); compress_threads_load_setup(); ramblock_recv_map_init(); + return 0; } =20 @@ -2645,6 +2709,7 @@ static int ram_load_cleanup(void *opaque) g_free(rb->receivedmap); rb->receivedmap =3D NULL; } + return 0; } =20 @@ -2845,7 +2910,7 @@ static int ram_load(QEMUFile *f, void *opaque, int ve= rsion_id) =20 while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) { ram_addr_t addr, total_ram_bytes; - void *host =3D NULL; + void *host =3D NULL, *host_bak =3D NULL; uint8_t ch; =20 addr =3D qemu_get_be64(f); @@ -2865,13 +2930,36 @@ static int ram_load(QEMUFile *f, void *opaque, int = version_id) RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { RAMBlock *block =3D ram_block_from_stream(f, flags); =20 - host =3D host_from_ram_block_offset(block, addr); + /* + * After going into COLO, we should load the Page into colo_ca= che + * NOTE: We need to keep a copy of SVM's ram in colo_cache. + * Privously, we copied all these memory in preparing stage of= COLO + * while we need to stop VM, which is a time-consuming process. + * Here we optimize it by a trick, back-up every page while in + * migration process while COLO is enabled, though it affects = the + * speed of the migration, but it obviously reduce the downtim= e of + * back-up all SVM'S memory in COLO preparing stage. + */ + if (migration_incoming_in_colo_state()) { + host =3D colo_cache_from_block_offset(block, addr); + /* After goes into COLO state, don't backup it any more */ + if (!migration_incoming_in_colo_state()) { + host_bak =3D host; + } + } + if (!migration_incoming_in_colo_state()) { + host =3D host_from_ram_block_offset(block, addr); + } if (!host) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret =3D -EINVAL; break; } - ramblock_recv_bitmap_set(block, host); + + if (!migration_incoming_in_colo_state()) { + ramblock_recv_bitmap_set(block, host); + } + trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host); } =20 @@ -2966,6 +3054,9 @@ static int ram_load(QEMUFile *f, void *opaque, int ve= rsion_id) if (!ret) { ret =3D qemu_file_get_error(f); } + if (!ret && host_bak && host) { + memcpy(host_bak, host, TARGET_PAGE_SIZE); + } } =20 wait_for_decompress_done(); diff --git a/migration/ram.h b/migration/ram.h index 64d81e9..07abf71 100644 --- a/migration/ram.h +++ b/migration/ram.h @@ -62,4 +62,8 @@ int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_ad= dr); void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr); void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr, size_t = nr); =20 +/* ram cache */ +int colo_init_ram_cache(void); +void colo_release_ram_cache(void); + #endif diff --git a/migration/savevm.c b/migration/savevm.c index cd753c4..c582716 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -1762,7 +1762,7 @@ static int loadvm_handle_cmd_packaged(MigrationIncomi= ngState *mis) static int loadvm_process_enable_colo(MigrationIncomingState *mis) { migration_incoming_enable_colo(); - return 0; + return colo_init_ram_cache(); } =20 /* --=20 2.7.4