From nobody Thu Apr 10 07:07:56 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=1741594913; cv=none; d=zohomail.com; s=zohoarc; b=Gz+T0GlUb9tIWAgCfOAFFn8ybS9qlF2uub7MhGn0aKhoGtDTYEg6YP51wFkgIwx9SFokY3TQUaBQfqjSnG9eNZY89LkTYCrhwuTx/2MJF4t3EDYTF8BZMwMIFXLTkKVxC8yxdZ+5dCWug9aS0VBGTrXG2kiOF1m4DUpPC1X5gwg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741594913; 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=JjWRn46aVPfTYQ4jG9nsUXeKj4f0xNThzSmx2+/UT+g=; b=PvfOuxxNQCvstH/O/WbdAlmn8ChphYM69elthdWFBYFf5kOL+LXmXUox0WDmH61oXZ0OYzOI0adPtWhfudsJrWXtqdOBnXh5HbZejBY2lPSdYO+/vGnlV7JYS26Jh3J8GFNSDX93XSao1Et95QQbOqnngoE1I85X5uOzu5+I6j4= 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 1741594913853320.5960989700387; Mon, 10 Mar 2025 01:21:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1trYNS-0006ZS-Il; Mon, 10 Mar 2025 04:20:46 -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 1trYNO-0006XU-W2 for qemu-devel@nongnu.org; Mon, 10 Mar 2025 04:20:43 -0400 Received: from mgamail.intel.com ([198.175.65.16]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1trYNM-0004U2-6r for qemu-devel@nongnu.org; Mon, 10 Mar 2025 04:20:42 -0400 Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2025 01:20:38 -0700 Received: from emr-bkc.sh.intel.com ([10.112.230.82]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2025 01:20:33 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1741594840; x=1773130840; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mUz3KRZzpMdb9UUDv1l5scZ1u6EFNRr/6avaaV0mfRQ=; b=PDmmafLzps/7y+fAeTR0RzTsuTLWzsIXoMmWT2uUpDQOIr7CflSjgTgs OMc6RDeJNyP3Fzo98oQQPPtHRDhGUObqMCz1lATDBgdn8/5MnCD02Ko0Q mgLOwny5wbAYkZqpL1O0vWlcYTKLbKHHvxk54235Q0yGLl7DSjJSWq58y kXD4OePdI0fMtkBI2lpYsBKzPgZEuGqVsCZN2iC7kIJ9k8OlEE/yhVSTA KVkF3eMpnU2Sp599lz92MuWF6uaFhvMj4YUK1MlAe9hBneJkMAia/pV9u wfSvQH2WLpq+9dzV3teV5zTkPBeUWM5U4scHYw1R/yl4U51xMlht5RQ1S g==; X-CSE-ConnectionGUID: 2NskhrB/Q7y2E1HWXY7SsA== X-CSE-MsgGUID: E/jGX4xUSKKw/g91Cv3ZYA== X-IronPort-AV: E=McAfee;i="6700,10204,11368"; a="42688519" X-IronPort-AV: E=Sophos;i="6.14,235,1736841600"; d="scan'208";a="42688519" X-CSE-ConnectionGUID: oa6bsM18TPihU1XUuN3n/A== X-CSE-MsgGUID: xVbdbMIeR62RqMthfT4pbw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,235,1736841600"; d="scan'208";a="150862852" From: Chenyi Qiang To: David Hildenbrand , Alexey Kardashevskiy , Peter Xu , 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 , Peng Chao P , Gao Chao , Xu Yilun , Li Xiaoyao Subject: [PATCH v3 6/7] memory: Attach MemoryAttributeManager to guest_memfd-backed RAMBlocks Date: Mon, 10 Mar 2025 16:18:34 +0800 Message-ID: <20250310081837.13123-7-chenyi.qiang@intel.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250310081837.13123-1-chenyi.qiang@intel.com> References: <20250310081837.13123-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.16; envelope-from=chenyi.qiang@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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: 1741594916180019000 Content-Type: text/plain; charset="utf-8" Introduce a new field, memory_attribute_manager, in RAMBlock to link to an MemoryAttributeManager object. This change centralizes all guest_memfd state information (like fd and shared_bitmap) within a RAMBlock, making it easier to manage. Use the realize()/unrealize() helpers to initialize/uninitialize the MemoryAttributeManager object. Register/unregister the object in the target RAMBlock's MemoryRegion when creating guest_memfd. In the kvm_convert_memory() function, manage memory state changes by using the shared_bitmap to call set_attribute() only on the specific memory range. Additionally, use the memory_attribute_manager_state_change() helper to notify the reigstered RamDiscardListener of these changes. Signed-off-by: Chenyi Qiang --- Changes in v3: - Use ram_discard_manager_reply_populated/discarded() to set the memory attribute and add the undo support if state_change() failed. - Didn't add Reviewed-by from Alexey due to the new changes in this commit. Changes in v2: - Introduce a new field memory_attribute_manager in RAMBlock. - Move the state_change() handling during page conversion in this patch. - Undo what we did if it fails to set. - Change the order of close(guest_memfd) and memory_attribute_manager c= leanup. --- accel/kvm/kvm-all.c | 50 +++++++++++++++++++++++++++++++++++++++-- include/exec/ramblock.h | 2 ++ system/physmem.c | 13 +++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index c1fea69d58..a89c5655e8 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -48,6 +48,7 @@ #include "kvm-cpus.h" #include "system/dirtylimit.h" #include "qemu/range.h" +#include "system/memory-attribute-manager.h" =20 #include "hw/boards.h" #include "system/stats.h" @@ -3018,6 +3019,25 @@ static void kvm_eat_signals(CPUState *cpu) } while (sigismember(&chkset, SIG_IPI)); } =20 +typedef struct SetMemoryAttribute { + bool to_private; +} SetMemoryAttribute; + +static int kvm_set_memory_attributes_cb(MemoryRegionSection *section, + void *opaque) +{ + hwaddr start =3D section->offset_within_address_space; + hwaddr size =3D section->size; + SetMemoryAttribute *args =3D opaque; + bool to_private =3D args->to_private; + + if (to_private) { + return kvm_set_memory_attributes_private(start, size); + } else { + return kvm_set_memory_attributes_shared(start, size); + } +} + int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) { MemoryRegionSection section; @@ -3026,6 +3046,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, boo= l to_private) RAMBlock *rb; void *addr; int ret =3D -EINVAL; + SetMemoryAttribute args =3D { .to_private =3D to_private }; =20 trace_kvm_convert_memory(start, size, to_private ? "shared_to_private"= : "private_to_shared"); =20 @@ -3077,9 +3098,13 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bo= ol to_private) } =20 if (to_private) { - ret =3D kvm_set_memory_attributes_private(start, size); + ret =3D ram_discard_manager_replay_populated(mr->rdm, §ion, + kvm_set_memory_attribut= es_cb, + &args); } else { - ret =3D kvm_set_memory_attributes_shared(start, size); + ret =3D ram_discard_manager_replay_discarded(mr->rdm, §ion, + kvm_set_memory_attribut= es_cb, + &args); } if (ret) { goto out_unref; @@ -3088,6 +3113,27 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bo= ol to_private) addr =3D memory_region_get_ram_ptr(mr) + section.offset_within_region; rb =3D qemu_ram_block_from_host(addr, false, &offset); =20 + ret =3D memory_attribute_manager_state_change(MEMORY_ATTRIBUTE_MANAGER= (mr->rdm), + offset, size, to_private); + if (ret) { + warn_report("Failed to notify the listener the state change of " + "(0x%"HWADDR_PRIx" + 0x%"HWADDR_PRIx") to %s", + start, size, to_private ? "private" : "shared"); + args.to_private =3D !to_private; + if (to_private) { + ret =3D ram_discard_manager_replay_populated(mr->rdm, §ion, + kvm_set_memory_attr= ibutes_cb, + &args); + } else { + ret =3D ram_discard_manager_replay_discarded(mr->rdm, §ion, + kvm_set_memory_attr= ibutes_cb, + &args); + } + if (ret) { + goto out_unref; + } + } + if (to_private) { if (rb->page_size !=3D qemu_real_host_page_size()) { /* diff --git a/include/exec/ramblock.h b/include/exec/ramblock.h index 0babd105c0..06fd365326 100644 --- a/include/exec/ramblock.h +++ b/include/exec/ramblock.h @@ -23,6 +23,7 @@ #include "cpu-common.h" #include "qemu/rcu.h" #include "exec/ramlist.h" +#include "system/memory-attribute-manager.h" =20 struct RAMBlock { struct rcu_head rcu; @@ -42,6 +43,7 @@ struct RAMBlock { int fd; uint64_t fd_offset; int guest_memfd; + MemoryAttributeManager *memory_attribute_manager; size_t page_size; /* dirty bitmap used during migration */ unsigned long *bmap; diff --git a/system/physmem.c b/system/physmem.c index c76503aea8..0ed394c5d2 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -54,6 +54,7 @@ #include "system/hostmem.h" #include "system/hw_accel.h" #include "system/xen-mapcache.h" +#include "system/memory-attribute-manager.h" #include "trace.h" =20 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE @@ -1885,6 +1886,16 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp) qemu_mutex_unlock_ramlist(); goto out_free; } + + new_block->memory_attribute_manager =3D MEMORY_ATTRIBUTE_MANAGER(o= bject_new(TYPE_MEMORY_ATTRIBUTE_MANAGER)); + if (memory_attribute_manager_realize(new_block->memory_attribute_m= anager, new_block->mr)) { + error_setg(errp, "Failed to realize memory attribute manager"); + object_unref(OBJECT(new_block->memory_attribute_manager)); + close(new_block->guest_memfd); + ram_block_discard_require(false); + qemu_mutex_unlock_ramlist(); + goto out_free; + } } =20 ram_size =3D (new_block->offset + new_block->max_length) >> TARGET_PAG= E_BITS; @@ -2138,6 +2149,8 @@ static void reclaim_ramblock(RAMBlock *block) } =20 if (block->guest_memfd >=3D 0) { + memory_attribute_manager_unrealize(block->memory_attribute_manager= ); + object_unref(OBJECT(block->memory_attribute_manager)); close(block->guest_memfd); ram_block_discard_require(false); } --=20 2.43.5