From nobody Mon Feb 9 17:25:24 2026 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=1690821383; cv=none; d=zohomail.com; s=zohoarc; b=X/MbKtud6yWfIxu/9CI2AnpZPz5XF4656StNm1u5ROw8VlFyhm59iYIufrhGe/D3KO7y3pL0Ry41ezndtH0Hm4j3yeg621J98KUOqDp6DhOq5KgHlv46s+ahKmDcd/xlVRr+yXOZaVoVnFR/2TbtfRbC8Q/MnbS0lO7lTD5TM9k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690821383; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=EUgPsknTD+9jGXoSnB3u5/0AVPfg23ZJ6KH7if8rGLg=; b=Zur8wKOt9kQNbObvX71AQcD5CmXuH00R9ZgAcyFkUnEqV4p/GMna4xgU4tP4xvJfeSc8Y2WYxwTn8Dfo/F/EKS7KcUk0o8VcMTY77kby2HpiyUU/oiRiDCiEdhGWUBdk5h37vwSSzyIRDnRj0rod1eHtlKb4OnCp3BoY/Jrp8c0= 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 1690821383403385.3968255377863; Mon, 31 Jul 2023 09:36:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQVpe-00030g-IT; Mon, 31 Jul 2023 12:33:18 -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 1qQVpb-0002YI-PR for qemu-devel@nongnu.org; Mon, 31 Jul 2023 12:33:15 -0400 Received: from [134.134.136.126] (helo=mgamail.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQVpa-0001LE-32 for qemu-devel@nongnu.org; Mon, 31 Jul 2023 12:33:15 -0400 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jul 2023 09:25:38 -0700 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.46]) by orsmga008.jf.intel.com with ESMTP; 31 Jul 2023 09:25:33 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690821194; x=1722357194; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vc/AA0VTFSVygJctuEuxWBeG2HxDcS51m4hly05CELA=; b=h50jh97bx2RJk5kW4UfUItrvDnzC4YD2R31qL8zy2nnIDKbE2YHYi7yd mvJdAqTCQMIOAEhQ0MvnJT1oBfwj+dbfhWK5qKB87HszoKs9R/XhdwKGn YiVqeVR3i7pWFVB9H1EUGqBwJDWNHSe+kbxk5JZPw0+RMOO2jfKSXYNDs zWwyJ769ReFqGaFj9EpYRIu7JTHSau4Q6sSI/YvrHXX77ROyLpao2O/eA UovDt+xTpn5KF9OD1D5KaixxyCd19Yf0VbWT0dtLeXsnB1UeDoyPFNLEc jLE79PmIOkvyVqSC/Ycd8xjt+AzxwgyfclKMmf6NWLsS+xim5jNAm2TM3 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="353993495" X-IronPort-AV: E=Sophos;i="6.01,244,1684825200"; d="scan'208";a="353993495" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="757984252" X-IronPort-AV: E=Sophos;i="6.01,244,1684825200"; d="scan'208";a="757984252" From: Xiaoyao Li To: Paolo Bonzini , Sean Christopherson , David Hildenbrand , Igor Mammedov , "Michael S. Tsirkin" , Marcel Apfelbaum , Richard Henderson , Marcelo Tosatti Cc: Markus Armbruster , Eric Blake , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peter Xu , Chao Peng , Michael Roth , isaku.yamahata@gmail.com, xiaoyao.li@intel.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Subject: [RFC PATCH 10/19] kvm: Introduce support for memory_attributes Date: Mon, 31 Jul 2023 12:21:52 -0400 Message-Id: <20230731162201.271114-11-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230731162201.271114-1-xiaoyao.li@intel.com> References: <20230731162201.271114-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 134.134.136.126 (failed) 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=134.134.136.126; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: 1690821384888100003 Content-Type: text/plain; charset="utf-8" Introcude the helper functions to set the attributes of a range of memory to private and shared. Signed-off-by: Xiaoyao Li --- accel/kvm/kvm-all.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/sysemu/kvm.h | 3 +++ 2 files changed, 46 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 7b1818334ba7..6dd22fa4fd6f 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -105,6 +105,7 @@ bool kvm_msi_use_devid; bool kvm_has_guest_debug; static int kvm_sstep_flags; static bool kvm_immediate_exit; +static uint64_t kvm_supported_memory_attributes; static hwaddr kvm_max_slot_size =3D ~0; =20 static const KVMCapabilityInfo kvm_required_capabilites[] =3D { @@ -1343,6 +1344,44 @@ void kvm_set_max_memslot_size(hwaddr max_slot_size) kvm_max_slot_size =3D max_slot_size; } =20 +static int kvm_set_memory_attributes(hwaddr start, hwaddr size, uint64_t a= ttr) +{ + struct kvm_memory_attributes attrs; + int r; + + attrs.attributes =3D attr; + attrs.address =3D start; + attrs.size =3D size; + attrs.flags =3D 0; + + r =3D kvm_vm_ioctl(kvm_state, KVM_SET_MEMORY_ATTRIBUTES, &attrs); + if (r) { + warn_report("%s: failed to set memory (0x%lx+%#zx) with attr 0x%lx= error '%s'", + __func__, start, size, attr, strerror(errno)); + } + return r; +} + +int kvm_set_memory_attributes_private(hwaddr start, hwaddr size) +{ + if (!(kvm_supported_memory_attributes & KVM_MEMORY_ATTRIBUTE_PRIVATE))= { + error_report("KVM doesn't support PRIVATE memory attribute\n"); + return -EINVAL; + } + + return kvm_set_memory_attributes(start, size, KVM_MEMORY_ATTRIBUTE_PRI= VATE); +} + +int kvm_set_memory_attributes_shared(hwaddr start, hwaddr size) +{ + if (!(kvm_supported_memory_attributes & KVM_MEMORY_ATTRIBUTE_PRIVATE))= { + error_report("KVM doesn't support PRIVATE memory attribute\n"); + return -EINVAL; + } + + return kvm_set_memory_attributes(start, size, 0); +} + /* Called with KVMMemoryListener.slots_lock held */ static void kvm_set_phys_mem(KVMMemoryListener *kml, MemoryRegionSection *section, bool add) @@ -2556,6 +2595,10 @@ static int kvm_init(MachineState *ms) } s->as =3D g_new0(struct KVMAs, s->nr_as); =20 + if (kvm_check_extension(s, KVM_CAP_MEMORY_ATTRIBUTES)) { + kvm_supported_memory_attributes =3D kvm_ioctl(s, KVM_GET_SUPPORTED= _MEMORY_ATTRIBUTES, 0); + } + if (object_property_find(OBJECT(current_machine), "kvm-type")) { g_autofree char *kvm_type =3D object_property_get_str(OBJECT(curre= nt_machine), "kvm-type", diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index 115f0cca79d1..49c896d8a512 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -580,4 +580,7 @@ bool kvm_arch_cpu_check_are_resettable(void); bool kvm_dirty_ring_enabled(void); =20 uint32_t kvm_dirty_ring_size(void); + +int kvm_set_memory_attributes_private(hwaddr start, hwaddr size); +int kvm_set_memory_attributes_shared(hwaddr start, hwaddr size); #endif --=20 2.34.1