From nobody Mon Dec 15 22:58:28 2025 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=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1765195887; cv=none; d=zohomail.com; s=zohoarc; b=ZqxFP040dcTQ4/V462RwlaTTDs05XTHVLEdqHy1usar8/ZG9iVGhrQmkvMJaA81IezSXXSgu/GLEw+CryWLaaJH1uX/MfxVrWi4CDEftqlltiunFFxUsf5q/szZJAzGRGlb/DJGCxIiOw2IGZF0Xy0Lr6qLmvnZ4xtmv9XifOHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765195887; h=Content-Type: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=rHg3xnaKRqLE1X+9qqpvj71mFOVEcBAGM3Q//3nmbWY=; b=fsgo7hoovOr+CbtjakBuydm3YIj3xYgrwufwE68UfXoYzTgSjlGRcIbCDYOadFoB/7lnoEQZei6DB+h3bTbOIiV33MOLyNHQx8keV5RRGip9EZEZc/8uvSFq6ip3+2ua/q5qOA4gkCsznhHQ2o9AS/xbOBaRn43YcNE4Wsv3VY0= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1765195887525697.818554662791; Mon, 8 Dec 2025 04:11:27 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vSa4r-00072c-5O; Mon, 08 Dec 2025 07:10:53 -0500 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 1vSa4h-00071g-Pk for qemu-devel@nongnu.org; Mon, 08 Dec 2025 07:10:45 -0500 Received: from sg-1-104.ptr.blmpb.com ([118.26.132.104]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vSa4d-0003RD-Vo for qemu-devel@nongnu.org; Mon, 08 Dec 2025 07:10:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1765195832; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=rHg3xnaKRqLE1X+9qqpvj71mFOVEcBAGM3Q//3nmbWY=; b=QT/Xgpx6gj4AqGCxhEI1GAfJu7tgMiLSO68JXnD+7yiEsghRW59rrXzgv2fnnHKWX520nJ qhf21l885Lu81JCe2s0VZLQNBTOg9aQMH2zJq9WvSBIwPuuBZjfwMdkStTHQYxm8Ve0qNC 9cOQIagj6TcLK83uRz+elsPtryTheznhNPx3Yy/IkuFmDJ1JELwQZVNkuKeD5UXVFQYlzL iRrVnohISc3UgoEI+q2awNISs+ceXNCQ2I3/KNWTI8jOTk8IUtdyRa0yt4I5iIN0Z5hap8 5XlWWUHZZd0/Dj12ay/Kx6HyBM2Qp9rLlcBh53iwytbrcXavw9EjW+uIamipXg== To: X-Mailer: git-send-email 2.39.3 (Apple Git-146) X-Original-From: Chuang Xu Content-Transfer-Encoding: quoted-printable From: "Chuang Xu" Message-Id: <20251208120952.37563-3-xuchuangxclwt@bytedance.com> Mime-Version: 1.0 Cc: , , , , , , , , "xuchuangxclwt" Subject: [RFC v1 2/2] migration: merge fragmented clear_dirty ioctls In-Reply-To: <20251208120952.37563-1-xuchuangxclwt@bytedance.com> Date: Mon, 8 Dec 2025 20:09:52 +0800 X-Lms-Return-Path: References: <20251208120952.37563-1-xuchuangxclwt@bytedance.com> 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=118.26.132.104; envelope-from=xuchuangxclwt@bytedance.com; helo=sg-1-104.ptr.blmpb.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 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, FROM_LOCAL_NOVOWEL=0.5, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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.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 @bytedance.com) X-ZM-MESSAGEID: 1765195890390158500 Content-Type: text/plain; charset="utf-8" From: xuchuangxclwt When the addresses processed are not aligned, a large number of clear_dirty ioctl occur (e.g. a 16MB misaligned memory can generate 4096 clear_dirty ioctls), which increases the time required for bitmap_sync and makes it more difficult for dirty pages to converge. Attempt to merge those fragmented clear_dirty ioctls. Signed-off-by: Chuang Xu --- accel/tcg/cputlb.c | 5 +++-- include/system/physmem.h | 3 ++- migration/ram.c | 17 ++++++++++++++++- system/memory.c | 2 +- system/physmem.c | 19 +++++++++++-------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index fd1606c856..8a054cb545 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -857,8 +857,9 @@ void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUS= tate *src_cpu, void tlb_protect_code(ram_addr_t ram_addr) { physical_memory_test_and_clear_dirty(ram_addr & TARGET_PAGE_MASK, - TARGET_PAGE_SIZE, - DIRTY_MEMORY_CODE); + TARGET_PAGE_SIZE, + DIRTY_MEMORY_CODE, + true); } =20 /* update the TLB so that writes in physical page 'phys_addr' are no longer diff --git a/include/system/physmem.h b/include/system/physmem.h index 879f6eae38..8529f0510a 100644 --- a/include/system/physmem.h +++ b/include/system/physmem.h @@ -41,7 +41,8 @@ void physical_memory_dirty_bits_cleared(ram_addr_t start,= ram_addr_t length); =20 bool physical_memory_test_and_clear_dirty(ram_addr_t start, ram_addr_t length, - unsigned client); + unsigned client, + bool clear_dirty); =20 DirtyBitmapSnapshot * physical_memory_snapshot_and_clear_dirty(MemoryRegion *mr, hwaddr offset, diff --git a/migration/ram.c b/migration/ram.c index 29f016cb25..329168d081 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -995,18 +995,33 @@ static uint64_t physical_memory_sync_dirty_bitmap(RAM= Block *rb, } } else { ram_addr_t offset =3D rb->offset; + unsigned long end, start_page; + uint64_t mr_offset, mr_size; =20 for (addr =3D 0; addr < length; addr +=3D TARGET_PAGE_SIZE) { + /* + * Here we don't expect so many clear_dirty, so we call + * physical_memory_test_and_clear_dirty with clear_dirty + * set to false. Later we'll do make an overall clear_dirty + * outside the loop. + */ if (physical_memory_test_and_clear_dirty( start + addr + offset, TARGET_PAGE_SIZE, - DIRTY_MEMORY_MIGRATION)) { + DIRTY_MEMORY_MIGRATION, + false)) { long k =3D (start + addr) >> TARGET_PAGE_BITS; if (!test_and_set_bit(k, dest)) { num_dirty++; } } } + end =3D TARGET_PAGE_ALIGN(start + addr + offset + TARGET_PAGE_SIZE) + >> TARGET_PAGE_BITS; + start_page =3D (start + offset) >> TARGET_PAGE_BITS; + mr_offset =3D (ram_addr_t)(start_page << TARGET_PAGE_BITS) - offse= t; + mr_size =3D (end - start_page) << TARGET_PAGE_BITS; + memory_region_clear_dirty_bitmap(rb->mr, mr_offset, mr_size); } =20 return num_dirty; diff --git a/system/memory.c b/system/memory.c index 8b84661ae3..7b81b84f30 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2424,7 +2424,7 @@ void memory_region_reset_dirty(MemoryRegion *mr, hwad= dr addr, { assert(mr->ram_block); physical_memory_test_and_clear_dirty( - memory_region_get_ram_addr(mr) + addr, size, client); + memory_region_get_ram_addr(mr) + addr, size, client, true); } =20 int memory_region_get_fd(MemoryRegion *mr) diff --git a/system/physmem.c b/system/physmem.c index c9869e4049..21b2db3884 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -1091,8 +1091,9 @@ void physical_memory_set_dirty_range(ram_addr_t start= , ram_addr_t length, =20 /* Note: start and end must be within the same ram block. */ bool physical_memory_test_and_clear_dirty(ram_addr_t start, - ram_addr_t length, - unsigned client) + ram_addr_t length, + unsigned client, + bool clear_dirty) { DirtyMemoryBlocks *blocks; unsigned long end, page, start_page; @@ -1126,9 +1127,11 @@ bool physical_memory_test_and_clear_dirty(ram_addr_t= start, page +=3D num; } =20 - mr_offset =3D (ram_addr_t)(start_page << TARGET_PAGE_BITS) - rambl= ock->offset; - mr_size =3D (end - start_page) << TARGET_PAGE_BITS; - memory_region_clear_dirty_bitmap(ramblock->mr, mr_offset, mr_size); + if (clear_dirty) { + mr_offset =3D (ram_addr_t)(start_page << TARGET_PAGE_BITS) - r= amblock->offset; + mr_size =3D (end - start_page) << TARGET_PAGE_BITS; + memory_region_clear_dirty_bitmap(ramblock->mr, mr_offset, mr_s= ize); + } } =20 if (dirty) { @@ -1140,9 +1143,9 @@ bool physical_memory_test_and_clear_dirty(ram_addr_t = start, =20 static void physical_memory_clear_dirty_range(ram_addr_t addr, ram_addr_t = length) { - physical_memory_test_and_clear_dirty(addr, length, DIRTY_MEMORY_MIGRAT= ION); - physical_memory_test_and_clear_dirty(addr, length, DIRTY_MEMORY_VGA); - physical_memory_test_and_clear_dirty(addr, length, DIRTY_MEMORY_CODE); + physical_memory_test_and_clear_dirty(addr, length, DIRTY_MEMORY_MIGRAT= ION, true); + physical_memory_test_and_clear_dirty(addr, length, DIRTY_MEMORY_VGA, t= rue); + physical_memory_test_and_clear_dirty(addr, length, DIRTY_MEMORY_CODE, = true); } =20 DirtyBitmapSnapshot *physical_memory_snapshot_and_clear_dirty --=20 2.20.1