From nobody Sat Nov 15 22:31:41 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=1747736968; cv=none; d=zohomail.com; s=zohoarc; b=dhWGHLuPwvQfJ6lwR+PjMNZe2kYdIeW2+Ymzyly9yDE8EV30ooFxL7mGY/voFY+U8g4joMCUmw0o+psjEuxeQ9fk9qTz2kQPwzVOSfgN/UjeIsF7A/gIhKrnqXebvO9wuq0P1pcdOicRZTeb2AtKeJvNsRDg4FbDXNUcZ/Eu95I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747736968; 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=Vyrw+adadFxsdj7/q3ZGWnk7UkK5X8nJpruQISgDnUg=; b=Nn8XH6P3icBk9W3d9vZiZ81zKm63WFKm7UEjnK2eb56Y0gjlTI3g3Z1QJC1lx+N6RsqThX2tPayWqBM7BNujOFrsakoBja8TcQvanzfVDtgXnLZfcsViC9Sp23Yyfg99lOKtdcv5penbfvFtxNVv5Z6KvPRce4ECEYBLFFrYQtg= 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 1747736968373197.08169090934643; Tue, 20 May 2025 03:29:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHKDh-0004bf-Ng; Tue, 20 May 2025 06:29:13 -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 1uHKDg-0004bG-2f for qemu-devel@nongnu.org; Tue, 20 May 2025 06:29:12 -0400 Received: from mgamail.intel.com ([198.175.65.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHKDd-0004f3-Q6 for qemu-devel@nongnu.org; Tue, 20 May 2025 06:29:11 -0400 Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2025 03:29:08 -0700 Received: from emr-bkc.sh.intel.com ([10.112.230.82]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2025 03:29:04 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1747736950; x=1779272950; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pvakodypSo80D6jOTbKAlrpB0vU0hAgMhd1K2+L0NeY=; b=mfsA31kWQGcLPmE4kbSSFH2ZQsVa7aUWxmNcFTBs31DU1j2THWsxfDu/ yHv6JOP7r9aG72DqCU/hYfusSOWbtoJbxfzg+ntEw8z206nIrqsKrInDB 4IR5W+RVX2UrQPoGNX9vQeNgKOkarA2MQY/KQdaR+gBH5TeTw3b0UjrSK D0TGNI9Hle6/MpuyspFh4qHekO6A4jV1rPIhmHFRy/VVxZUKfuQq+LUUX LtOFNluSpSAdCF13kj05VR6zQkD3lQeeI6AX7NpdHCqQfcf9LAU1PdR9j zG1F+EDeHnlX0R4HU8680Bnqt+G1s5gXXO6o5zqCgEHcuLGnHttPUNUTr w==; X-CSE-ConnectionGUID: /mOufPj2QQqJR8c5z0rgEg== X-CSE-MsgGUID: GziRokHSS5utPlqMTdqubA== X-IronPort-AV: E=McAfee;i="6700,10204,11438"; a="49566621" X-IronPort-AV: E=Sophos;i="6.15,302,1739865600"; d="scan'208";a="49566621" X-CSE-ConnectionGUID: mVaTGQJsQqajrbrL1g2z1Q== X-CSE-MsgGUID: FWYrQxcpTkqyaLVKg5nkCA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,302,1739865600"; d="scan'208";a="144905221" From: Chenyi Qiang To: David Hildenbrand , Alexey Kardashevskiy , Peter Xu , Gupta Pankaj , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Michael Roth Cc: Chenyi Qiang , qemu-devel@nongnu.org, kvm@vger.kernel.org, Williams Dan J , Zhao Liu , Baolu Lu , Gao Chao , Xu Yilun , Li Xiaoyao Subject: [PATCH v5 01/10] memory: Export a helper to get intersection of a MemoryRegionSection with a given range Date: Tue, 20 May 2025 18:28:41 +0800 Message-ID: <20250520102856.132417-2-chenyi.qiang@intel.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250520102856.132417-1-chenyi.qiang@intel.com> References: <20250520102856.132417-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.21; envelope-from=chenyi.qiang@intel.com; helo=mgamail.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.13, 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_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 @intel.com) X-ZM-MESSAGEID: 1747736969777116600 Content-Type: text/plain; charset="utf-8" Rename the helper to memory_region_section_intersect_range() to make it more generic. Meanwhile, define the @end as Int128 and replace the related operations with Int128_* format since the helper is exported as a wider API. Suggested-by: Alexey Kardashevskiy Reviewed-by: Alexey Kardashevskiy Reviewed-by: David Hildenbrand Reviewed-by: Zhao Liu Signed-off-by: Chenyi Qiang --- Changes in v5: - Indent change for int128 ops to avoid the line over 80 - Add two Review-by from Alexey and Zhao Changes in v4: - No change. Changes in v3: - No change Changes in v2: - Make memory_region_section_intersect_range() an inline function. - Add Reviewed-by from David - Define the @end as Int128 and use the related Int128_* ops as a wilder API (Alexey) --- hw/virtio/virtio-mem.c | 32 +++++--------------------------- include/system/memory.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index a3d1a676e7..b3c126ea1e 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -244,28 +244,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, @@ -287,7 +265,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); @@ -319,7 +297,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); @@ -355,7 +333,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); @@ -371,7 +349,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); @@ -388,7 +366,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/system/memory.h b/include/system/memory.h index fbbf4cf911..b961c4076a 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -1211,6 +1211,36 @@ 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. + */ +static inline bool memory_region_section_intersect_range(MemoryRegionSecti= on *s, + uint64_t offset, + uint64_t size) +{ + uint64_t start =3D MAX(s->offset_within_region, offset); + Int128 end =3D int128_min(int128_add(int128_make64(s->offset_within_re= gion), + s->size), + int128_add(int128_make64(offset), + int128_make64(size))); + + if (int128_le(end, int128_make64(start))) { + return false; + } + + s->offset_within_address_space +=3D start - s->offset_within_region; + s->offset_within_region =3D start; + s->size =3D int128_sub(end, int128_make64(start)); + return true; +} + /** * memory_region_init: Initialize a memory region * --=20 2.43.5