From nobody Mon Nov 25 03:49:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1718889803; cv=none; d=zohomail.com; s=zohoarc; b=Bb9IMTQjy/jZlH9eO6JjhCStZ/draSktoNNHT90u1aBixnM3Da6TepNQIXHSJw4JqYOYR+330qanCy8pJPFCedvrRe4WWCHebvHhXRE8UJyRzhw6Db9KRmwijB+nm9XWBboPKQRdVBnX3C3HM1VE+czREDcT8BafiYOg7Lt0R6k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1718889803; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PgQ2VWwSRJvrka/NwQyki/HUAVlNqzJSr4msKXGLKwc=; b=AS9ZKR62lqWbPT332Rw38WMK+U1iRszY2mpWdDmJaNkauea6ef3QDYNG6zrv3TGKhFJFabVldh2h/U/sRurXyyPWFVWWDDwRBsFmuv0pR/jM4o1gfCFQztT0eM3pVyeImZWHZ0a9ARrogyK+K40Ddg268MSUEx8+0vUrA8+JrYs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1718889803641588.0707287501493; Thu, 20 Jun 2024 06:23:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sKHkz-0004iA-N8; Thu, 20 Jun 2024 09:23:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sKEOE-0000rK-VK for qemu-devel@nongnu.org; Thu, 20 Jun 2024 05:47:34 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sKEOA-000424-J1 for qemu-devel@nongnu.org; Thu, 20 Jun 2024 05:47:34 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1f480624d0fso5173045ad.1 for ; Thu, 20 Jun 2024 02:47:29 -0700 (PDT) Received: from localhost.localdomain (KD175132079186.ppp-bb.dion.ne.jp. [175.132.79.186]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f993deb126sm49350585ad.219.2024.06.20.02.47.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 02:47:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718876848; x=1719481648; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PgQ2VWwSRJvrka/NwQyki/HUAVlNqzJSr4msKXGLKwc=; b=Vx2uLfmU6nDf4Z8BrqGKpn+1mF9YGG/XZZYaZ3ivR7Zv8TWpHALJWItFVIzeHge0og C34wpWO6Lw4oGMxAmoISXHoI6FHAcPbjK32B0qYuN9sYJ+TA4a9x4lvGHMDnHZmoGV4v ba9gza9z/TjFmg22NMQps6vPjtLzOAEBXc5gOq2NaIxW6B/LDzGzXlVMBL7pCylFjaF3 E3zOGK+Pcieqbdbw2/ETdkmjuNuxB5tsNyP7aQRrmqwq5zc9HfHlbxrGhzBkbuCla7xn U4xFhhc8O1RnhpMiFyFX4dN9Btdo2sFx85TQ337RlcqZhrOnMtrFbQa0TiCMsnU3kX5d yaBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718876848; x=1719481648; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PgQ2VWwSRJvrka/NwQyki/HUAVlNqzJSr4msKXGLKwc=; b=KWBIb37lw/TXLpPq4S5w9POFewcD6/8UbZRGwn9jelop6RlKVRN7VpYkIrnnxflwlV rQpop/g8qn8gjIoSxuuIbqVqHHVCTbSNXcvy+dXTAFn6WNgX7Wg2tZ5c5GxxI1skb2So wuU7aCgaglwgLabXhPT1PxXdTE1aFRYvy43ut8LernKwXXfDhNkhMDGfKjHExN+jXxqy Gy0yXfhYQ7Mnm7V3mvJGqWb7c4/MWmsZFvLXKdneyHoa4hDOdnkCtWlovjgJH/UWd7OG wET8fhRMPdbTR07mxeKN6+L17v5PZDwbuwvOIdMxw+zQBuad5yTJqO/uAbcQn5KdwCUO xLVw== X-Gm-Message-State: AOJu0Yweu9IVthGA0apXiejF7HLqjFbaq3K34KO/GXPzISo4psv7+zei Mb0G+je0/tQAIZvJg1YPomX3tUaaZAOTgA2nQU1uwG14hTAOZY96I8U9eXwWfUc= X-Google-Smtp-Source: AGHT+IHd28ZN6La1xBKTY8V5v1l/hzPQXsuSIg5jdcNTOMv+LGnSBUR0ECLIJuYXF7W5GRoA/6UpPg== X-Received: by 2002:a17:902:f68f:b0:1f6:f82d:a8cb with SMTP id d9443c01a7336-1f9aa473dd4mr44952935ad.52.1718876847933; Thu, 20 Jun 2024 02:47:27 -0700 (PDT) From: Shota Imamura To: qemu-devel@nongnu.org Cc: Shota Imamura , Paolo Bonzini , Peter Xu , David Hildenbrand , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Fabiano Rosas , kvm@vger.kernel.org (open list:Overall KVM CPUs) Subject: [PATCH 1/2] migration: Implement dirty ring Date: Thu, 20 Jun 2024 18:47:13 +0900 Message-Id: <20240620094714.871727-2-cosocaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240620094714.871727-1-cosocaf@gmail.com> References: <20240620094714.871727-1-cosocaf@gmail.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=2607:f8b0:4864:20::631; envelope-from=cosocaf@gmail.com; helo=mail-pl1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 20 Jun 2024 09:23:08 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1718889805828100003 Content-Type: text/plain; charset="utf-8" This commit implements the dirty ring as an alternative dirty tracking method to the dirty bitmap. While the dirty ring has already been implemented in accel/kvm using KVM's dirty ring, it was designed to set bits in the ramlist and ramblock bitmap. This commit introduces a new dirty ring to replace the bitmap, allowing the use of the dirty ring even without KVM. When using KVM's dirty ring, this implementation maximizes its effectiveness. To enable the dirty ring, specify the startup option "-migration dirty-logging=3Dring,dirty-ring-size=3DN". To use the bitmap, either specify nothing or "-migration dirty-logging=3Dbitmap". If the dirty ring becomes full, it falls back to the bitmap for that round. Signed-off-by: Shota Imamura --- accel/kvm/kvm-all.c | 36 ++++++++- include/exec/ram_addr.h | 131 +++++++++++++++++++++++++++++++-- include/exec/ramlist.h | 48 ++++++++++++ include/migration/misc.h | 4 +- include/qemu/bitops.h | 23 ++++++ migration/migration-hmp-cmds.c | 2 + migration/migration.c | 27 ++++++- migration/migration.h | 6 ++ migration/ram.c | 127 ++++++++++++++++++++++++++++---- qemu-options.hx | 29 ++++++++ system/physmem.c | 128 +++++++++++++++++++++++++++++++- system/vl.c | 63 +++++++++++++++- 12 files changed, 597 insertions(+), 27 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 854cb86b22..91410d682f 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -667,7 +667,13 @@ static void kvm_dirty_ring_mark_page(KVMState *s, uint= 32_t as_id, return; } =20 - set_bit(offset, mem->dirty_bmap); + if (!test_and_set_bit(offset, mem->dirty_bmap) && + mem->flags & KVM_MEM_LOG_DIRTY_PAGES && + migration_has_dirty_ring()) { + unsigned long pfn =3D + (mem->ram_start_offset >> TARGET_PAGE_BITS) + offset; + ram_list_enqueue_dirty(pfn); + } } =20 static bool dirty_gfn_is_dirtied(struct kvm_dirty_gfn *gfn) @@ -1675,6 +1681,34 @@ static void kvm_log_sync_global(MemoryListener *l, b= ool last_stage) /* Flush all kernel dirty addresses into KVMSlot dirty bitmap */ kvm_dirty_ring_flush(); =20 + if (!ram_list_enqueue_dirty_full()) { + cpu_physical_memory_set_dirty_ring(ram_list_get_enqueue_dirty()); + + if (s->kvm_dirty_ring_with_bitmap && last_stage) { + kvm_slots_lock(); + for (i =3D 0; i < s->nr_slots; i++) { + mem =3D &kml->slots[i]; + if (mem->memory_size && + mem->flags & KVM_MEM_LOG_DIRTY_PAGES && + kvm_slot_get_dirty_log(s, mem)) { + kvm_slot_sync_dirty_pages(mem); + } + } + kvm_slots_unlock(); + } + + kvm_slots_lock(); + for (i =3D 0; i < s->nr_slots; i++) { + mem =3D &kml->slots[i]; + if (mem->memory_size && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { + kvm_slot_reset_dirty_pages(mem); + } + } + kvm_slots_unlock(); + + return; + } + /* * TODO: make this faster when nr_slots is big while there are * only a few used slots (small VMs). diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 891c44cf2d..1eaebcf22f 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -27,6 +27,7 @@ #include "exec/ramblock.h" #include "exec/exec-all.h" #include "qemu/rcu.h" +#include "migration/misc.h" =20 extern uint64_t total_dirty_pages; =20 @@ -282,7 +283,11 @@ static inline void cpu_physical_memory_set_dirty_flag(= ram_addr_t addr, =20 blocks =3D qatomic_rcu_read(&ram_list.dirty_memory[client]); =20 - set_bit_atomic(offset, blocks->blocks[idx]); + if (!test_and_set_bit_atomic(offset, blocks->blocks[idx]) && + migration_has_dirty_ring() && + client =3D=3D DIRTY_MEMORY_MIGRATION) { + ram_list_enqueue_dirty(page); + } } =20 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start, @@ -313,8 +318,24 @@ static inline void cpu_physical_memory_set_dirty_range= (ram_addr_t start, unsigned long next =3D MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE= ); =20 if (likely(mask & (1 << DIRTY_MEMORY_MIGRATION))) { - bitmap_set_atomic(blocks[DIRTY_MEMORY_MIGRATION]->blocks[i= dx], - offset, next - page); + if (!migration_has_dirty_ring() || + ram_list_enqueue_dirty_full()) { + use_dirty_bmap: + bitmap_set_atomic( + blocks[DIRTY_MEMORY_MIGRATION]->blocks[idx], + offset, + next - page); + } else { + for (unsigned long p =3D page; p < next; p++) { + if (!test_and_set_bit_atomic( + p % DIRTY_MEMORY_BLOCK_SIZE, + blocks[DIRTY_MEMORY_MIGRATION]->blocks[idx= ])) { + if (unlikely(!ram_list_enqueue_dirty(p))) { + goto use_dirty_bmap; + } + } + } + } } if (unlikely(mask & (1 << DIRTY_MEMORY_VGA))) { bitmap_set_atomic(blocks[DIRTY_MEMORY_VGA]->blocks[idx], @@ -384,9 +405,29 @@ uint64_t cpu_physical_memory_set_dirty_lebitmap(unsign= ed long *bitmap, qatomic_or(&blocks[DIRTY_MEMORY_VGA][idx][offset], tem= p); =20 if (global_dirty_tracking) { - qatomic_or( + if (!migration_has_dirty_ring() || + ram_list_enqueue_dirty_full()) { + use_dirty_bmap: + qatomic_or( &blocks[DIRTY_MEMORY_MIGRATION][idx][offse= t], temp); + } else { + for (unsigned long p =3D 0; p < BITS_PER_LONG;= p++) { + if (temp & (1ul << p)) { + unsigned long pfn =3D + (k * BITS_PER_LONG + p) + + (start >> TARGET_PAGE_BITS); + if (!test_and_set_bit_atomic( + pfn % DIRTY_MEMORY_BLOCK_SIZE, + blocks[DIRTY_MEMORY_MIGRATION]= [idx])) { + if (unlikely(!ram_list_enqueue_dir= ty(pfn))) { + goto use_dirty_bmap; + } + } + } + } + } + if (unlikely( global_dirty_tracking & GLOBAL_DIRTY_DIRTY_RAT= E)) { total_dirty_pages +=3D nbits; @@ -443,6 +484,53 @@ uint64_t cpu_physical_memory_set_dirty_lebitmap(unsign= ed long *bitmap, =20 return num_dirty; } + +static inline +uint64_t cpu_physical_memory_set_dirty_ring(DirtyRing *ring) +{ + uint64_t num_dirty =3D 0; + unsigned long **blocks[DIRTY_MEMORY_NUM]; + + WITH_RCU_READ_LOCK_GUARD() { + for (int i =3D 0; i < DIRTY_MEMORY_NUM; i++) { + blocks[i] =3D + qatomic_rcu_read(&ram_list.dirty_memory[i])->blocks; + } + + for (unsigned long rpos =3D ring->rpos, wpos =3D ring->wpos; + rpos !=3D wpos; + rpos++) { + unsigned long page =3D ring->buffer[rpos & ring->mask]; + unsigned long idx =3D page / DIRTY_MEMORY_BLOCK_SIZE; + unsigned long offset =3D BIT_WORD(page % DIRTY_MEMORY_BLOCK_SI= ZE); + + qatomic_or(&blocks[DIRTY_MEMORY_VGA][idx][offset], BIT_MASK(pa= ge)); + + if (global_dirty_tracking) { + qatomic_or( + &blocks[DIRTY_MEMORY_MIGRATION][idx][offset], + BIT_MASK(page)); + + if (unlikely( + global_dirty_tracking & GLOBAL_DIRTY_DIRTY_RATE)) { + total_dirty_pages++; + } + } + + if (tcg_enabled()) { + qatomic_or(&blocks[DIRTY_MEMORY_CODE][idx][offset], + BIT_MASK(page)); + } + + num_dirty++; + + xen_hvm_modified_memory(page << TARGET_PAGE_BITS, + TARGET_PAGE_SIZE); + } + } + + return num_dirty; +} #endif /* not _WIN32 */ =20 static inline void cpu_physical_memory_dirty_bits_cleared(ram_addr_t start, @@ -484,8 +572,41 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBloc= k *rb, uint64_t num_dirty =3D 0; unsigned long *dest =3D rb->bmap; =20 + if (migration_has_dirty_ring() && !ram_list_dequeue_dirty_full()) { + unsigned long *const *src =3D qatomic_rcu_read( + &ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION])->blocks; + + DirtyRing *ring =3D ram_list_get_dequeue_dirty(); + for (unsigned long rpos =3D ring->rpos, wpos =3D ring->wpos; + rpos !=3D wpos; + rpos++) { + unsigned long page =3D ring->buffer[rpos & ring->mask]; + if (page >=3D ((start + rb->offset) >> TARGET_PAGE_BITS) && + page < ((start + rb->offset + length) >> TARGET_PAGE_BITS)= ) { + if (!test_and_set_bit(page - (rb->offset >> TARGET_PAGE_BI= TS), + dest)) { + num_dirty++; + } + + unsigned long idx =3D page / DIRTY_MEMORY_BLOCK_SIZE; + unsigned long offset =3D BIT_WORD(page % DIRTY_MEMORY_BLOC= K_SIZE); + src[idx][offset] &=3D ~BIT_MASK(page); + } + } + + if (num_dirty) { + cpu_physical_memory_dirty_bits_cleared(start, length); + } + + if (rb->clear_bmap) { + clear_bmap_set(rb, start >> TARGET_PAGE_BITS, + length >> TARGET_PAGE_BITS); + } else { + memory_region_clear_dirty_bitmap(rb->mr, start, length); + } + } /* start address and length is aligned at the start of a word? */ - if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) =3D=3D + else if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) =3D=3D (start + rb->offset) && !(length & ((BITS_PER_LONG << TARGET_PAGE_BITS) - 1))) { int k; diff --git a/include/exec/ramlist.h b/include/exec/ramlist.h index 2ad2a81acc..598b6d5a05 100644 --- a/include/exec/ramlist.h +++ b/include/exec/ramlist.h @@ -5,6 +5,7 @@ #include "qemu/thread.h" #include "qemu/rcu.h" #include "qemu/rcu_queue.h" +#include "exec/cpu-common.h" =20 typedef struct RAMBlockNotifier RAMBlockNotifier; =20 @@ -44,6 +45,37 @@ typedef struct { unsigned long *blocks[]; } DirtyMemoryBlocks; =20 +/* + * Ring buffer for dirty memory tracking. + * This ring buffer does not support deletion of intermediate elements. + * Therefore, the dirty bitmap must be checked to determine if a region has + * been cleared. + */ +typedef struct { + /* + * The starting address of the dirty-ring. It is NULL if the dirty-ring + * is not enabled. + */ + unsigned long *buffer; + /* + * The number of elements in the dirty-ring. + * Must be a power of 2. Note that the actual limit of elements that c= an + * be inserted is dirty_ring_size - 1 due to ring-buffer constraints. + */ + unsigned long size; + /* The mask for obtaining the index in the dirty-ring. */ + unsigned long mask; + /* + * The current read position in the dirty-ring. + * If dirty_ring_rpos =3D=3D dirty_ring_wpos, the dirty-ring is empty. + * If dirty_ring_wpos - dirty_ring_rpos =3D=3D dirty_ring_size, the di= rty-ring + * is full. + */ + unsigned long rpos; + /* The current write position in the dirty-ring. */ + unsigned long wpos; +} DirtyRing; + typedef struct RAMList { QemuMutex mutex; RAMBlock *mru_block; @@ -52,6 +84,9 @@ typedef struct RAMList { DirtyMemoryBlocks *dirty_memory[DIRTY_MEMORY_NUM]; uint32_t version; QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers; + /* Used only when dirty-ring is enabled */ + uint32_t dirty_ring_switch; + DirtyRing dirty_rings[2]; } RAMList; extern RAMList ram_list; =20 @@ -63,6 +98,8 @@ extern RAMList ram_list; =20 void qemu_mutex_lock_ramlist(void); void qemu_mutex_unlock_ramlist(void); +/* Called from RCU critical section */ +RAMBlock *qemu_get_ram_block(ram_addr_t addr); =20 struct RAMBlockNotifier { void (*ram_block_added)(RAMBlockNotifier *n, void *host, size_t size, @@ -80,6 +117,17 @@ void ram_block_notify_add(void *host, size_t size, size= _t max_size); void ram_block_notify_remove(void *host, size_t size, size_t max_size); void ram_block_notify_resize(void *host, size_t old_size, size_t new_size); =20 +DirtyRing *ram_list_get_enqueue_dirty(void); +DirtyRing *ram_list_get_dequeue_dirty(void); +bool ram_list_enqueue_dirty(unsigned long page); +bool ram_list_dequeue_dirty(unsigned long *page); +unsigned long ram_list_enqueue_dirty_capacity(void); +unsigned long ram_list_dequeue_dirty_capacity(void); +bool ram_list_enqueue_dirty_full(void); +bool ram_list_dequeue_dirty_full(void); +void ram_list_dequeue_dirty_reset(void); +void ram_list_dirty_ring_switch(void); + GString *ram_block_format(void); =20 #endif /* RAMLIST_H */ diff --git a/include/migration/misc.h b/include/migration/misc.h index bfadc5613b..1335e0958e 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -51,13 +51,15 @@ AnnounceParameters *migrate_announce_params(void); void dump_vmstate_json_to_file(FILE *out_fp); =20 /* migration/migration.c */ -void migration_object_init(void); +void migration_object_init(QDict *qdict); void migration_shutdown(void); bool migration_is_idle(void); bool migration_is_active(void); bool migration_is_device(void); bool migration_thread_is_self(void); bool migration_is_setup_or_active(void); +bool migration_has_dirty_ring(void); +unsigned long migration_get_dirty_ring_size(void); =20 typedef enum MigrationEventType { MIG_EVENT_PRECOPY_SETUP, diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h index 2c0a2fe751..ea5d02d0ec 100644 --- a/include/qemu/bitops.h +++ b/include/qemu/bitops.h @@ -108,6 +108,29 @@ static inline int test_and_set_bit(long nr, unsigned l= ong *addr) return (old & mask) !=3D 0; } =20 +/** + * test_and_set_bit_atomic - Set a bit and return its old value atomically + * @nr: Bit to set + * @addr: Address to count from + */ +static inline int test_and_set_bit_atomic(long nr, unsigned long *addr) +{ + unsigned long mask =3D BIT_MASK(nr); + unsigned long *p =3D addr + BIT_WORD(nr); + unsigned long old; + unsigned long desired; + + do { + old =3D qatomic_read(p); + if (old & mask) { + return false; + } + desired =3D old | mask; + } while (qatomic_cmpxchg(p, old, desired) !=3D desired); + + return true; +} + /** * test_and_clear_bit - Clear a bit and return its old value * @nr: Bit to clear diff --git a/migration/migration-hmp-cmds.c b/migration/migration-hmp-cmds.c index 9f0e8029e0..02b1fa7b29 100644 --- a/migration/migration-hmp-cmds.c +++ b/migration/migration-hmp-cmds.c @@ -48,6 +48,8 @@ static void migration_global_dump(Monitor *mon) ms->send_section_footer ? "on" : "off"); monitor_printf(mon, "clear-bitmap-shift: %u\n", ms->clear_bitmap_shift); + monitor_printf(mon, "dirty-logging-method: %s\n", + migration_has_dirty_ring() ? "ring" : "bitmap"); } =20 void hmp_info_migrate(Monitor *mon, const QDict *qdict) diff --git a/migration/migration.c b/migration/migration.c index e1b269624c..a059ac7626 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -44,6 +44,7 @@ #include "qapi/qapi-events-migration.h" #include "qapi/qmp/qerror.h" #include "qapi/qmp/qnull.h" +#include "qapi/qmp/qdict.h" #include "qemu/rcu.h" #include "postcopy-ram.h" #include "qemu/thread.h" @@ -207,12 +208,20 @@ static int migration_stop_vm(MigrationState *s, RunSt= ate state) return ret; } =20 -void migration_object_init(void) +void migration_object_init(QDict *qdict) { /* This can only be called once. */ assert(!current_migration); current_migration =3D MIGRATION_OBJ(object_new(TYPE_MIGRATION)); =20 + const char *logging_method =3D qdict_get_try_str(qdict, "dirty-logging= "); + if (logging_method =3D=3D NULL || strcmp(logging_method, "bitmap") =3D= =3D 0) { + current_migration->dirty_ring_size =3D 0; + } else if (strcmp(logging_method, "ring") =3D=3D 0) { + current_migration->dirty_ring_size =3D qdict_get_int(qdict, + "dirty-ring-siz= e"); + } + /* * Init the migrate incoming object as well no matter whether * we'll use it or not. @@ -1632,6 +1641,20 @@ bool migrate_mode_is_cpr(MigrationState *s) return s->parameters.mode =3D=3D MIG_MODE_CPR_REBOOT; } =20 +bool migration_has_dirty_ring(void) +{ + MigrationState *s =3D current_migration; + + return s->dirty_ring_size !=3D 0; +} + +unsigned long migration_get_dirty_ring_size(void) +{ + MigrationState *s =3D current_migration; + + return s->dirty_ring_size; +} + int migrate_init(MigrationState *s, Error **errp) { int ret; @@ -3806,6 +3829,8 @@ static void migration_instance_init(Object *obj) qemu_sem_init(&ms->wait_unplug_sem, 0); qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0); qemu_mutex_init(&ms->qemu_file_lock); + + ms->dirty_ring_size =3D 0; } =20 /* diff --git a/migration/migration.h b/migration/migration.h index 6af01362d4..4dfdf98acc 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -457,6 +457,12 @@ struct MigrationState { bool switchover_acked; /* Is this a rdma migration */ bool rdma_migration; + + /* + * The number of elements in the dirty-ring. + * It is 0 if the dirty-ring is not enabled. + */ + unsigned long dirty_ring_size; }; =20 void migrate_set_state(int *state, int old_state, int new_state); diff --git a/migration/ram.c b/migration/ram.c index ceea586b06..5bf45268ec 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -387,6 +387,7 @@ struct RAMState { bool xbzrle_started; /* Are we on the last stage of migration */ bool last_stage; + bool first_bitmap_scanning; =20 /* total handled target pages at the beginning of period */ uint64_t target_page_count_prev; @@ -1041,6 +1042,29 @@ static void migration_trigger_throttle(RAMState *rs) } } =20 +static uint64_t migration_sync_dirty_ring(RAMBlock *block) +{ + uint64_t dirty_page_count =3D 0; + + uint64_t base =3D block->offset >> TARGET_PAGE_BITS; + uint64_t size =3D block->used_length >> TARGET_PAGE_BITS; + uint64_t nr =3D BITS_TO_LONGS(size); + for (uint64_t i =3D 0; i < nr; i++) { + if (block->bmap[i]) { + uint64_t page =3D i * BITS_PER_LONG; + uint64_t end =3D MIN(page + BITS_PER_LONG, size); + for (; page < end; page++) { + if (test_bit(page, block->bmap)) { + ram_list_enqueue_dirty(base + page); + dirty_page_count++; + } + } + } + } + + return dirty_page_count; +} + static void migration_bitmap_sync(RAMState *rs, bool last_stage) { RAMBlock *block; @@ -1052,9 +1076,48 @@ static void migration_bitmap_sync(RAMState *rs, bool= last_stage) rs->time_last_bitmap_sync =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIM= E); } =20 + if (migration_has_dirty_ring()) { + if (unlikely(ram_list_dequeue_dirty_full())) { + ram_list_dequeue_dirty_reset(); + + if (ram_list_enqueue_dirty_capacity() >=3D + rs->migration_dirty_pages) { + uint64_t dirty_page_count =3D 0; + WITH_QEMU_LOCK_GUARD(&rs->bitmap_mutex) { + WITH_RCU_READ_LOCK_GUARD() { + RAMBLOCK_FOREACH_NOT_IGNORED(block) { + dirty_page_count +=3D + migration_sync_dirty_ring(block); + if (dirty_page_count >=3D + rs->migration_dirty_pages) { + break; + } + } + } + } + } else { + DirtyRing *ring =3D ram_list_get_enqueue_dirty(); + ring->wpos =3D ring->rpos + ring->size; + } + } else { + unsigned long page; + while (ram_list_dequeue_dirty(&page)) { + block =3D qemu_get_ram_block(page << TARGET_PAGE_BITS); + if (test_bit(page - (block->offset >> TARGET_PAGE_BITS), + block->bmap)) { + ram_list_enqueue_dirty(page); + } + } + } + } + trace_migration_bitmap_sync_start(); memory_global_dirty_log_sync(last_stage); =20 + if (migration_has_dirty_ring()) { + ram_list_dirty_ring_switch(); + } + WITH_QEMU_LOCK_GUARD(&rs->bitmap_mutex) { WITH_RCU_READ_LOCK_GUARD() { RAMBLOCK_FOREACH_NOT_IGNORED(block) { @@ -2214,7 +2277,11 @@ static int ram_save_host_page(RAMState *rs, PageSear= chStatus *pss) return tmppages; } =20 - pss_find_next_dirty(pss); + if (!migration_has_dirty_ring()) { + pss_find_next_dirty(pss); + } else { + break; + } } while (pss_within_range(pss)); =20 pss_host_page_finish(pss); @@ -2260,24 +2327,51 @@ static int ram_find_and_save_block(RAMState *rs) =20 pss_init(pss, rs->last_seen_block, rs->last_page); =20 - while (true){ - if (!get_queued_page(rs, pss)) { - /* priority queue empty, so just search for something dirty */ - int res =3D find_dirty_block(rs, pss); - if (res !=3D PAGE_DIRTY_FOUND) { - if (res =3D=3D PAGE_ALL_CLEAN) { - break; - } else if (res =3D=3D PAGE_TRY_AGAIN) { - continue; - } else if (res < 0) { - pages =3D res; - break; + if (rs->first_bitmap_scanning || + !migration_has_dirty_ring() || + ram_list_dequeue_dirty_full()) { + while (true) { + if (!get_queued_page(rs, pss)) { + /* priority queue empty, so just search for something dirt= y */ + int res =3D find_dirty_block(rs, pss); + if (pss->complete_round) { + rs->first_bitmap_scanning =3D false; } + if (res !=3D PAGE_DIRTY_FOUND) { + if (res =3D=3D PAGE_ALL_CLEAN) { + break; + } else if (res =3D=3D PAGE_TRY_AGAIN) { + continue; + } else if (res < 0) { + pages =3D res; + break; + } + } + } + pages =3D ram_save_host_page(rs, pss); + if (pages) { + break; } } - pages =3D ram_save_host_page(rs, pss); - if (pages) { - break; + } else { + while (get_queued_page(rs, pss)) { + pages =3D ram_save_host_page(rs, pss); + if (pages) { + break; + } + } + + if (!pages) { + unsigned long page; + while (ram_list_dequeue_dirty(&page)) { + pss->block =3D qemu_get_ram_block(page << TARGET_PAGE_BITS= ); + pss->page =3D page - (pss->block->offset >> TARGET_PAGE_BI= TS); + pss->complete_round =3D false; + pages =3D ram_save_host_page(rs, pss); + if (pages) { + break; + } + } } } =20 @@ -2404,6 +2498,7 @@ static void ram_state_reset(RAMState *rs) rs->last_page =3D 0; rs->last_version =3D ram_list.version; rs->xbzrle_started =3D false; + rs->first_bitmap_scanning =3D true; } =20 #define MAX_WAIT 50 /* ms, half buffered_file limit */ diff --git a/qemu-options.hx b/qemu-options.hx index 8ca7f34ef0..f1ec592ec0 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -556,6 +556,35 @@ SRST -numa hmat-cache,node-id=3D1,size=3D10K,level=3D1,associativity=3D= direct,policy=3Dwrite-back,line=3D8 ERST =20 +DEF("migration", HAS_ARG, QEMU_OPTION_migration, + "-migration [dirty-logging=3Dmethod][,dirty-ring-size=3Dn]\n" + " Configure migration settings.\n" + " dirty-logging: Specify the dirty logging method.\n" + " bitmap: Use bitmap-based dirty logging (default).= \n" + " ring: Use ring-based dirty logging.\n" + " dirty-ring-size: Specify the size of the dirty ring b= uffer.\n" + " This option is only applicable if dirty-logging i= s set to ring.\n" + " The size must be a power of 2.\n" + " Example: -migration dirty-logging=3Dring,dirty-ri= ng-size=3D1024\n", QEMU_ARCH_ALL) +SRST +``-migration [dirty-logging=3Dmethod,dirty-ring-size=3Dn]`` + Configure migration settings. + + ``dirty-logging=3Dbitmap`` + Use bitmap-based dirty logging. This is the default method for tra= cking changes to memory pages during migration. + + ``dirty-logging=3Dring`` + Use ring-based dirty logging. This method uses a ring buffer to tr= ack changes to memory pages, which can be more efficient in some scenarios. + + ``dirty-ring-size=3Dn`` + Specify the size of the dirty ring buffer when using ring-based di= rty logging. The size must be a power of 2 (e.g., 1024, 2048, 4096). This o= ption is only applicable if dirty-logging is set to ring. + + Example: + :: + + -migration dirty-logging=3Dring,dirty-ring-size=3D1024 +ERST + DEF("add-fd", HAS_ARG, QEMU_OPTION_add_fd, "-add-fd fd=3Dfd,set=3Dset[,opaque=3Dopaque]\n" " Add 'fd' to fd 'set'\n", QEMU_ARCH_ALL) diff --git a/system/physmem.c b/system/physmem.c index 33d09f7571..2c2ee54eae 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -783,7 +783,7 @@ AddressSpace *cpu_get_address_space(CPUState *cpu, int = asidx) } =20 /* Called from RCU critical section */ -static RAMBlock *qemu_get_ram_block(ram_addr_t addr) +RAMBlock *qemu_get_ram_block(ram_addr_t addr) { RAMBlock *block; =20 @@ -1804,6 +1804,37 @@ static void dirty_memory_extend(ram_addr_t old_ram_s= ize, } } =20 +/* Called with ram_list.mutex held */ +static bool dirty_ring_init(Error **errp) +{ + static bool initialized; + if (initialized) { + return true; + } + + unsigned long dirty_ring_size =3D migration_get_dirty_ring_size(); + + for (int i =3D 0; i < 2; i++) { + DirtyRing *ring =3D &ram_list.dirty_rings[i]; + ring->buffer =3D g_malloc(sizeof(ring->buffer[0]) * dirty_ring_siz= e); + + if (ring->buffer =3D=3D NULL) { + error_setg(errp, "Failed to allocate dirty ring buffer"); + return false; + } + + ring->size =3D dirty_ring_size; + ring->mask =3D dirty_ring_size - 1; + ring->rpos =3D 0; + ring->wpos =3D 0; + } + + ram_list.dirty_ring_switch =3D 0; + + initialized =3D true; + return true; +} + static void ram_block_add(RAMBlock *new_block, Error **errp) { const bool noreserve =3D qemu_ram_is_noreserve(new_block); @@ -1868,6 +1899,13 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp) if (new_ram_size > old_ram_size) { dirty_memory_extend(old_ram_size, new_ram_size); } + + if (migration_has_dirty_ring()) { + if (!dirty_ring_init(errp)) { + goto out_free; + } + } + /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ, * QLIST (which has an RCU-friendly variant) does not have insertion at * tail, so save the last element in last_block. @@ -3888,3 +3926,91 @@ bool ram_block_discard_is_required(void) return qatomic_read(&ram_block_discard_required_cnt) || qatomic_read(&ram_block_coordinated_discard_required_cnt); } + +DirtyRing *ram_list_get_enqueue_dirty(void) +{ + return &ram_list.dirty_rings[ram_list.dirty_ring_switch & 1]; +} + +DirtyRing *ram_list_get_dequeue_dirty(void) +{ + return &ram_list.dirty_rings[(ram_list.dirty_ring_switch + 1) & 1]; +} + +bool ram_list_enqueue_dirty(unsigned long page) +{ + DirtyRing *ring =3D &ram_list.dirty_rings[ram_list.dirty_ring_switch &= 1]; + + assert(ring->buffer); + + if (unlikely(ring->wpos - ring->rpos =3D=3D ring->size)) { + return false; + } + + ring->buffer[ring->wpos & ring->mask] =3D page; + ring->wpos++; + + return true; +} + +bool ram_list_dequeue_dirty(unsigned long *page) +{ + DirtyRing *ring =3D + &ram_list.dirty_rings[(ram_list.dirty_ring_switch + 1) & 1]; + + assert(ring->buffer); + + if (unlikely(ring->rpos =3D=3D ring->wpos)) { + return false; + } + + *page =3D ring->buffer[ring->rpos & ring->mask]; + ring->rpos++; + + return true; +} + +unsigned long ram_list_enqueue_dirty_capacity(void) +{ + DirtyRing *ring =3D &ram_list.dirty_rings[ram_list.dirty_ring_switch &= 1]; + + return ring->size - (ring->wpos - ring->rpos); + +} + +unsigned long ram_list_dequeue_dirty_capacity(void) +{ + DirtyRing *ring =3D + &ram_list.dirty_rings[(ram_list.dirty_ring_switch + 1) & 1]; + + return ring->wpos - ring->rpos; +} + +bool ram_list_enqueue_dirty_full(void) +{ + DirtyRing *ring =3D &ram_list.dirty_rings[ram_list.dirty_ring_switch &= 1]; + + return (ring->wpos - ring->rpos) =3D=3D ring->size; +} + +bool ram_list_dequeue_dirty_full(void) +{ + DirtyRing *ring =3D + &ram_list.dirty_rings[(ram_list.dirty_ring_switch + 1) & 1]; + + return (ring->wpos - ring->rpos) =3D=3D ring->size; +} + +void ram_list_dequeue_dirty_reset(void) +{ + DirtyRing *ring =3D + &ram_list.dirty_rings[(ram_list.dirty_ring_switch + 1) & 1]; + + ring->rpos =3D 0; + ring->wpos =3D 0; +} + +void ram_list_dirty_ring_switch(void) +{ + ram_list.dirty_ring_switch++; +} diff --git a/system/vl.c b/system/vl.c index a3eede5fa5..305a9386da 100644 --- a/system/vl.c +++ b/system/vl.c @@ -163,6 +163,7 @@ static const char *accelerators; static bool have_custom_ram_size; static const char *ram_memdev_id; static QDict *machine_opts_dict; +static QDict *migration_opts_dict; static QTAILQ_HEAD(, ObjectOption) object_opts =3D QTAILQ_HEAD_INITIALIZER= (object_opts); static QTAILQ_HEAD(, DeviceOption) device_opts =3D QTAILQ_HEAD_INITIALIZER= (device_opts); static int display_remote; @@ -758,6 +759,22 @@ static QemuOptsList qemu_smp_opts =3D { }, }; =20 +static QemuOptsList qemu_migration_opts =3D { + .name =3D "migration-opts", + .merge_lists =3D true, + .head =3D QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head), + .desc =3D { + { + .name =3D "dirty-logging", /* bitmap/ring */ + .type =3D QEMU_OPT_STRING, + }, { + .name =3D "dirty-ring-size", + .type =3D QEMU_OPT_SIZE, + }, + { /*End of list */ } + }, +}; + #if defined(CONFIG_POSIX) static QemuOptsList qemu_run_with_opts =3D { .name =3D "run-with", @@ -2742,7 +2759,7 @@ void qmp_x_exit_preconfig(Error **errp) void qemu_init(int argc, char **argv) { QemuOpts *opts; - QemuOpts *icount_opts =3D NULL, *accel_opts =3D NULL; + QemuOpts *icount_opts =3D NULL, *accel_opts =3D NULL, *migration_opts = =3D NULL; QemuOptsList *olist; int optind; const char *optarg; @@ -2781,6 +2798,7 @@ void qemu_init(int argc, char **argv) qemu_add_opts(&qemu_semihosting_config_opts); qemu_add_opts(&qemu_fw_cfg_opts); qemu_add_opts(&qemu_action_opts); + qemu_add_opts(&qemu_migration_opts); qemu_add_run_with_opts(); module_call_init(MODULE_INIT_OPTS); =20 @@ -2812,6 +2830,7 @@ void qemu_init(int argc, char **argv) } =20 machine_opts_dict =3D qdict_new(); + migration_opts_dict =3D qdict_new(); if (userconfig) { qemu_read_default_config_file(&error_fatal); } @@ -3369,6 +3388,46 @@ void qemu_init(int argc, char **argv) machine_parse_property_opt(qemu_find_opts("smp-opts"), "smp", optarg); break; + case QEMU_OPTION_migration: + migration_opts =3D + qemu_opts_parse_noisily(qemu_find_opts("migration-opts= "), + optarg, + false); + + optarg =3D qemu_opt_get(migration_opts, "dirty-logging"); + if (optarg =3D=3D NULL || strcmp(optarg, "bitmap") =3D=3D = 0) { + qdict_put_str(migration_opts_dict, + "dirty-logging", + "bitmap"); + + if (qemu_opt_find(migration_opts, "dirty-ring-size")) { + error_report("dirty-ring-size is only supported " + "with dirty-logging=3Dring"); + exit(1); + } + } else if (strcmp(optarg, "ring") =3D=3D 0) { + qdict_put_str(migration_opts_dict, "dirty-logging", "r= ing"); + + uint64_t dirty_ring_size =3D qemu_opt_get_size(migrati= on_opts, + "dirty-ri= ng-size", + 0); + if (ctpop64(dirty_ring_size) !=3D 1) { + error_report("dirty-ring-size must be a power of 2= "); + exit(1); + } else if (dirty_ring_size > (uint64_t)INT64_MAX) { + error_report("dirty-ring-size is too large"); + exit(1); + } + + qdict_put_int(migration_opts_dict, + "dirty-ring-size", + dirty_ring_size); + } else { + error_report("invalid dirty-logging option: %s", optar= g); + exit(1); + } + + break; #ifdef CONFIG_VNC case QEMU_OPTION_vnc: vnc_parse(optarg); @@ -3735,7 +3794,7 @@ void qemu_init(int argc, char **argv) * Note: creates a QOM object, must run only after global and * compat properties have been set up. */ - migration_object_init(); + migration_object_init(migration_opts_dict); =20 /* parse features once if machine provides default cpu_type */ current_machine->cpu_type =3D machine_class_default_cpu_type(machine_c= lass); --=20 2.34.1