From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5AE48C54ED1 for ; Tue, 27 May 2025 08:44:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8E8A6B0095; Tue, 27 May 2025 04:44:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D3E6A6B0098; Tue, 27 May 2025 04:44:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2DB06B0099; Tue, 27 May 2025 04:44:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 99BDA6B0095 for ; Tue, 27 May 2025 04:44:32 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5C8F158B96 for ; Tue, 27 May 2025 08:44:32 +0000 (UTC) X-FDA: 83488051584.01.293FE8D Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by imf11.hostedemail.com (Postfix) with ESMTP id B1B4740008 for ; Tue, 27 May 2025 08:44:29 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jhsbScrG; spf=none (imf11.hostedemail.com: domain of binbin.wu@linux.intel.com has no SPF policy when checking 198.175.65.10) smtp.mailfrom=binbin.wu@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1748335470; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LDUja/soH89gEGH4sh7r6gwl6Kbwv9oCc2U3NgLy3do=; b=sqpzjLIJn1TrFkc+ZrnMM7KpEvOCwYJBiawQLA6FCJR2DzzBwNDZ8xRpHqn5SDicZ6QOoZ aQkN1Gqc80+Uyo2KWGPfPoIK3upD0nIF2xjdE8wLPCujG353SQJZcPUoHbr9KvCS1bQbG+ P6jdg0LNa7VoufRBeWavmU8TZfR9lZM= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jhsbScrG; spf=none (imf11.hostedemail.com: domain of binbin.wu@linux.intel.com has no SPF policy when checking 198.175.65.10) smtp.mailfrom=binbin.wu@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748335470; a=rsa-sha256; cv=none; b=iy8/LvHRLMV6xaWcCf6wCK7aj0WaM8z/GZ9gCIKFUrjtVG5M46IipTKRnp8KU+9Uu9ZbM6 aZ6O/wv2wy1N5SLYbk5lJGAnqvnyvLrzfhOd4T4NJ91GD1WLdfOqs5zJueDHqoqi+umdg1 Xz6fVRb/BeQYPJwQZBGR/BmpbTDcDZU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1748335470; x=1779871470; h=message-id:date:mime-version:subject:from:to:cc: references:in-reply-to:content-transfer-encoding; bh=LDUja/soH89gEGH4sh7r6gwl6Kbwv9oCc2U3NgLy3do=; b=jhsbScrGTEcoulm7Yi6lwJCuSG1V14hQnwMza4AShf7VBfFiAlSjszLV LQ0V2byjq30WEdzSSRe0bW33+GiWCozSTo5oxzN+0A4B4TyjbgA0Tr4Lx ES8Hx5ARzpeU9go9lTXmoyk5/8FFAjMGR8f77CzzXUbi4RLzRswkb8zcf jxdJFkgZzhpRT+K2HcZglZoY9NmevMYKYUB2RePXGYof39StBsqp+3o8L tTNSiLWkhx32Wn0qhQ8mz8Wo6M+eRc0VwRkJJO3JZ3oKuZFycUxafhR1b +QNvVDTSHTW1y8obBfF19rHsHl9+jbs25NYIZTIlvNPqv5yLCjWrtIbOw A==; X-CSE-ConnectionGUID: 3n6USzg8RnOsrPOwD2c4ow== X-CSE-MsgGUID: MAB0Yk00S42d4Idm3FwsuQ== X-IronPort-AV: E=McAfee;i="6700,10204,11445"; a="67732398" X-IronPort-AV: E=Sophos;i="6.15,317,1739865600"; d="scan'208";a="67732398" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2025 01:44:28 -0700 X-CSE-ConnectionGUID: JMWUB66JSwiPLT/caKW45g== X-CSE-MsgGUID: 6r760WWXS2u+N35uVyKcmQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,317,1739865600"; d="scan'208";a="173670439" Received: from unknown (HELO [10.238.11.3]) ([10.238.11.3]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2025 01:44:01 -0700 Message-ID: <9157f2d4-62f0-41c2-a755-80233b534b42@linux.intel.com> Date: Tue, 27 May 2025 16:43:57 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v2 02/51] KVM: guest_memfd: Introduce and use shareability to guard faulting From: Binbin Wu To: Ackerley Tng Cc: kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org, aik@amd.com, ajones@ventanamicro.com, akpm@linux-foundation.org, amoorthy@google.com, anthony.yznaga@oracle.com, anup@brainfault.org, aou@eecs.berkeley.edu, bfoster@redhat.com, brauner@kernel.org, catalin.marinas@arm.com, chao.p.peng@intel.com, chenhuacai@kernel.org, dave.hansen@intel.com, david@redhat.com, dmatlack@google.com, dwmw@amazon.co.uk, erdemaktas@google.com, fan.du@intel.com, fvdl@google.com, graf@amazon.com, haibo1.xu@intel.com, hch@infradead.org, hughd@google.com, ira.weiny@intel.com, isaku.yamahata@intel.com, jack@suse.cz, james.morse@arm.com, jarkko@kernel.org, jgg@ziepe.ca, jgowans@amazon.com, jhubbard@nvidia.com, jroedel@suse.de, jthoughton@google.com, jun.miao@intel.com, kai.huang@intel.com, keirf@google.com, kent.overstreet@linux.dev, kirill.shutemov@intel.com, liam.merwick@oracle.com, maciej.wieczor-retman@intel.com, mail@maciej.szmigiero.name, maz@kernel.org, mic@digikod.net, michael.roth@amd.com, mpe@ellerman.id.au, muchun.song@linux.dev, nikunj@amd.com, nsaenz@amazon.es, oliver.upton@linux.dev, palmer@dabbelt.com, pankaj.gupta@amd.com, paul.walmsley@sifive.com, pbonzini@redhat.com, pdurrant@amazon.co.uk, peterx@redhat.com, pgonda@google.com, pvorel@suse.cz, qperret@google.com, quic_cvanscha@quicinc.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, quic_svaddagi@quicinc.com, quic_tsoni@quicinc.com, richard.weiyang@gmail.com, rick.p.edgecombe@intel.com, rientjes@google.com, roypat@amazon.co.uk, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, steven.sistare@oracle.com, suzuki.poulose@arm.com, tabba@google.com, thomas.lendacky@amd.com, usama.arif@bytedance.com, vannapurve@google.com, vbabka@suse.cz, viro@zeniv.linux.org.uk, vkuznets@redhat.com, wei.w.wang@intel.com, will@kernel.org, willy@infradead.org, xiaoyao.li@intel.com, yan.y.zhao@intel.com, yilun.xu@intel.com, yuzenghui@huawei.com, zhiquan1.li@intel.com References: <9483e9e3-9b29-49c6-adcc-04fe45ac28fd@linux.intel.com> Content-Language: en-US In-Reply-To: <9483e9e3-9b29-49c6-adcc-04fe45ac28fd@linux.intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: B1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nAuy qJuxAu1l qiCBgDyhyNdEx1kA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 5/27/2025 4:25 PM, Binbin Wu wrote: > > > On 5/15/2025 7:41 AM, Ackerley Tng wrote: >> Track guest_memfd memory's shareability status within the inode as >> opposed to the file, since it is property of the guest_memfd's memory >> contents. >> >> Shareability is a property of the memory and is indexed using the >> page's index in the inode. Because shareability is the memory's >> property, it is stored within guest_memfd instead of within KVM, like >> in kvm->mem_attr_array. >> >> KVM_MEMORY_ATTRIBUTE_PRIVATE in kvm->mem_attr_array must still be >> retained to allow VMs to only use guest_memfd for private memory and >> some other memory for shared memory. >> >> Not all use cases require guest_memfd() to be shared with the host >> when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_PRIVATE, >> which when set on KVM_CREATE_GUEST_MEMFD, initializes the memory as >> private to the guest, and therefore not mappable by the >> host. Otherwise, memory is shared until explicitly converted to >> private. >> >> Signed-off-by: Ackerley Tng >> Co-developed-by: Vishal Annapurve >> Signed-off-by: Vishal Annapurve >> Co-developed-by: Fuad Tabba >> Signed-off-by: Fuad Tabba >> Change-Id: If03609cbab3ad1564685c85bdba6dcbb6b240c0f >> --- >>   Documentation/virt/kvm/api.rst |   5 ++ >>   include/uapi/linux/kvm.h       |   2 + >>   virt/kvm/guest_memfd.c         | 124 ++++++++++++++++++++++++++++++++- >>   3 files changed, 129 insertions(+), 2 deletions(-) >> >> diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst >> index 86f74ce7f12a..f609337ae1c2 100644 >> --- a/Documentation/virt/kvm/api.rst >> +++ b/Documentation/virt/kvm/api.rst >> @@ -6408,6 +6408,11 @@ belonging to the slot via its userspace_addr. >>   The use of GUEST_MEMFD_FLAG_SUPPORT_SHARED will not be allowed for CoCo VMs. >>   This is validated when the guest_memfd instance is bound to the VM. >>   +If the capability KVM_CAP_GMEM_CONVERSIONS is supported, then the 'flags' field >> +supports GUEST_MEMFD_FLAG_INIT_PRIVATE. > > It seems that the sentence is stale? > Didn't find the definition of KVM_CAP_GMEM_CONVERSIONS. Aha! It's a typo, should be KVM_CAP_GMEM_CONVERSION. > >> Setting GUEST_MEMFD_FLAG_INIT_PRIVATE >> +will initialize the memory for the guest_memfd as guest-only and not faultable >> +by the host. >> + > [...] >>     static int kvm_gmem_init_fs_context(struct fs_context *fc) >> @@ -549,12 +645,26 @@ static const struct inode_operations kvm_gmem_iops = { >>   static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, >>                                 loff_t size, u64 flags) >>   { >> +    struct kvm_gmem_inode_private *private; >>       struct inode *inode; >> +    int err; >>         inode = alloc_anon_secure_inode(kvm_gmem_mnt->mnt_sb, name); >>       if (IS_ERR(inode)) >>           return inode; >>   +    err = -ENOMEM; >> +    private = kzalloc(sizeof(*private), GFP_KERNEL); >> +    if (!private) >> +        goto out; >> + >> +    mt_init(&private->shareability); > > shareability is defined only when CONFIG_KVM_GMEM_SHARED_MEM enabled, should be done within CONFIG_KVM_GMEM_SHARED_MEM . > > >> + inode->i_mapping->i_private_data = private; >> + >> +    err = kvm_gmem_shareability_setup(private, size, flags); >> +    if (err) >> +        goto out; >> + >>       inode->i_private = (void *)(unsigned long)flags; >>       inode->i_op = &kvm_gmem_iops; >>       inode->i_mapping->a_ops = &kvm_gmem_aops; >> @@ -566,6 +676,11 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, >>       WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); >>         return inode; >> + >> +out: >> +    iput(inode); >> + >> +    return ERR_PTR(err); >>   } >> > [...] >