From nobody Fri Dec 19 17:12:00 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1734073879; cv=none; d=zohomail.com; s=zohoarc; b=FOhGI89OuJDs1IrghhjLXAAH5ALcGNclYW2ghhrutEz05ZPpkYHSLYbkeORQ6QNCZR5H4Ds89McBhT4SX7EInI2WIyLxHCgc+451omwqU2zMEGMJI1EXw7nGNZEdSV+e6MCsHTAjniyzd8pFhmKtYQ7LOfYDkKS97T85+LHQZPE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1734073879; 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=8Za47OM5ap+iFZ43Od81xOBIb7djFi7d3k8NzmIi5M8=; b=hBH1KD9TL8HX1rMplGeHMdHL5prfrUkMdtihcVxDoDTiqjXrA7Z73itXtds2jReUyhTVnwN/llLB051qaNBxCuZZGKxksBajGw/2kDvh7Qy4uPWwzOghNkpMYnBSznolzdRecOx84kPj44AfI5jddZ9CAJrz7gStSL78WblOIHM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.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) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1734073879762856.3638263492116; Thu, 12 Dec 2024 23:11:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tLznn-0007Wt-4B; Fri, 13 Dec 2024 02:09:31 -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 1tLznl-0007WJ-H5 for qemu-devel@nongnu.org; Fri, 13 Dec 2024 02:09:29 -0500 Received: from mgamail.intel.com ([198.175.65.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tLzne-00071G-HX for qemu-devel@nongnu.org; Fri, 13 Dec 2024 02:09:29 -0500 Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Dec 2024 23:09:19 -0800 Received: from emr-bkc.sh.intel.com ([10.112.230.82]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Dec 2024 23:09:16 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1734073763; x=1765609763; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+gGDRKc6A8E0xNRDSzs8+ysZ9ibQoXdsqeDZYD2FgLQ=; b=cgjCaRTpSKt3edYn07ahIjG9qbd7gELfLiN5Wcitefu9L0x8gwT883Hy Cz5SDWMeHiolHPhS3DsokGhcGu2r3enO3WY4U0oA08aPRvBls3C4rbECy 0JUeiWHvmjJ1L54+zmi5KfbTVH13sRrGVw0vKtV2hoxRkubUXvjzaeB7c 6mqQDOQE/bv7WlPxSGIj5gj5SXG9hhDMJ0U28R9844PcL3sX+Le4Mcl99 8yoX77EJt4gJosBTfj9ZDNb213SSaqF3OI/BgqV3qwGaScjXIGSNsQWSz OYhNM/ktvPvZuUaP05i8NtT42wlBtUtYVWCBgosKxSeuUGKtI5KgABX9Z A==; X-CSE-ConnectionGUID: fV49oYbDTcCdjKf+1un+3A== X-CSE-MsgGUID: sIfMSqFVQZKt05IMQBg7EQ== X-IronPort-AV: E=McAfee;i="6700,10204,11284"; a="51937069" X-IronPort-AV: E=Sophos;i="6.12,230,1728975600"; d="scan'208";a="51937069" X-CSE-ConnectionGUID: dr84zZlCQqSEHaVcDIz0oQ== X-CSE-MsgGUID: swkuzg1CTXaftgdaBFhv4g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,230,1728975600"; d="scan'208";a="96365541" From: Chenyi Qiang To: David Hildenbrand , Paolo Bonzini , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Michael Roth Cc: Chenyi Qiang , qemu-devel@nongnu.org, kvm@vger.kernel.org, Williams Dan J , Peng Chao P , Gao Chao , Xu Yilun Subject: [PATCH 1/7] memory: Export a helper to get intersection of a MemoryRegionSection with a given range Date: Fri, 13 Dec 2024 15:08:43 +0800 Message-ID: <20241213070852.106092-2-chenyi.qiang@intel.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241213070852.106092-1-chenyi.qiang@intel.com> References: <20241213070852.106092-1-chenyi.qiang@intel.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=198.175.65.10; envelope-from=chenyi.qiang@intel.com; helo=mgamail.intel.com X-Spam_score_int: -48 X-Spam_score: -4.9 X-Spam_bar: ---- X-Spam_report: (-4.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.496, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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 @intel.com) X-ZM-MESSAGEID: 1734073881095116600 Content-Type: text/plain; charset="utf-8" Rename the helper to memory_region_section_intersect_range() to make it more generic. Signed-off-by: Chenyi Qiang Reviewed-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 32 +++++--------------------------- include/exec/memory.h | 13 +++++++++++++ system/memory.c | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 80ada89551..e3d1ccaeeb 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -242,28 +242,6 @@ static int virtio_mem_for_each_plugged_range(VirtIOMEM= *vmem, void *arg, return ret; } =20 -/* - * Adjust the memory section to cover the intersection with the given rang= e. - * - * Returns false if the intersection is empty, otherwise returns true. - */ -static bool virtio_mem_intersect_memory_section(MemoryRegionSection *s, - uint64_t offset, uint64_t = size) -{ - uint64_t start =3D MAX(s->offset_within_region, offset); - uint64_t end =3D MIN(s->offset_within_region + int128_get64(s->size), - offset + size); - - if (end <=3D start) { - return false; - } - - s->offset_within_address_space +=3D start - s->offset_within_region; - s->offset_within_region =3D start; - s->size =3D int128_make64(end - start); - return true; -} - typedef int (*virtio_mem_section_cb)(MemoryRegionSection *s, void *arg); =20 static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem, @@ -285,7 +263,7 @@ static int virtio_mem_for_each_plugged_section(const Vi= rtIOMEM *vmem, first_bit + 1) - 1; size =3D (last_bit - first_bit + 1) * vmem->block_size; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { break; } ret =3D cb(&tmp, arg); @@ -317,7 +295,7 @@ static int virtio_mem_for_each_unplugged_section(const = VirtIOMEM *vmem, first_bit + 1) - 1; size =3D (last_bit - first_bit + 1) * vmem->block_size; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { break; } ret =3D cb(&tmp, arg); @@ -353,7 +331,7 @@ static void virtio_mem_notify_unplug(VirtIOMEM *vmem, u= int64_t offset, QLIST_FOREACH(rdl, &vmem->rdl_list, next) { MemoryRegionSection tmp =3D *rdl->section; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { continue; } rdl->notify_discard(rdl, &tmp); @@ -369,7 +347,7 @@ static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint= 64_t offset, QLIST_FOREACH(rdl, &vmem->rdl_list, next) { MemoryRegionSection tmp =3D *rdl->section; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { continue; } ret =3D rdl->notify_populate(rdl, &tmp); @@ -386,7 +364,7 @@ static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint= 64_t offset, if (rdl2 =3D=3D rdl) { break; } - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)= ) { continue; } rdl2->notify_discard(rdl2, &tmp); diff --git a/include/exec/memory.h b/include/exec/memory.h index e5e865d1a9..ec7bc641e8 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1196,6 +1196,19 @@ MemoryRegionSection *memory_region_section_new_copy(= MemoryRegionSection *s); */ void memory_region_section_free_copy(MemoryRegionSection *s); =20 +/** + * memory_region_section_intersect_range: Adjust the memory section to cov= er + * the intersection with the given range. + * + * @s: the #MemoryRegionSection to be adjusted + * @offset: the offset of the given range in the memory region + * @size: the size of the given range + * + * Returns false if the intersection is empty, otherwise returns true. + */ +bool memory_region_section_intersect_range(MemoryRegionSection *s, + uint64_t offset, uint64_t size); + /** * memory_region_init: Initialize a memory region * diff --git a/system/memory.c b/system/memory.c index 85f6834cb3..ddcec90f5e 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2898,6 +2898,23 @@ void memory_region_section_free_copy(MemoryRegionSec= tion *s) g_free(s); } =20 +bool memory_region_section_intersect_range(MemoryRegionSection *s, + uint64_t offset, uint64_t size) +{ + uint64_t start =3D MAX(s->offset_within_region, offset); + uint64_t end =3D MIN(s->offset_within_region + int128_get64(s->size), + offset + size); + + if (end <=3D start) { + return false; + } + + s->offset_within_address_space +=3D start - s->offset_within_region; + s->offset_within_region =3D start; + s->size =3D int128_make64(end - start); + return true; +} + bool memory_region_present(MemoryRegion *container, hwaddr addr) { MemoryRegion *mr; --=20 2.43.5