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 CC9CFC83F03 for ; Wed, 9 Jul 2025 14:29:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B67B8D0001; Wed, 9 Jul 2025 10:29:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 65FD86B00A5; Wed, 9 Jul 2025 10:29:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 527968D0001; Wed, 9 Jul 2025 10:29:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3BA956B0096 for ; Wed, 9 Jul 2025 10:29:05 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E02E9B74C3 for ; Wed, 9 Jul 2025 14:29:04 +0000 (UTC) X-FDA: 83644958208.28.DA715A0 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf08.hostedemail.com (Postfix) with ESMTP id E386B16000C for ; Wed, 9 Jul 2025 14:29:02 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nWwnhqHi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of vannapurve@google.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=vannapurve@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752071343; 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=otqsPDt5osEXqjc5euk3u1KgCdwLcQ8YUxZOr7B7LAY=; b=dLcZK1CaqKlDEMMbhsGry6N89H1D1QHubBCUENm9GGUsbkduFed9i3iI7gFB0rLvVslzR2 rRUVzuayTVZcMoJcfKn6H7F49Abs7XONTMPLeUBH8MOtDNDd4O1j3SHLgY4cI7etF/gOyj nzOniYQDE+SzIozFqPjsJ68hNqLkMkY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752071343; a=rsa-sha256; cv=none; b=n2QI83ti+/uw0uDfWdnMdFoDr62fTCyMTQpPYkg/X8jCq28DDZ2tM6e2A2yFtg93wXXNuX m8rvizTRmSzJ+QPqoX7Ok6IJ2ltbJxIWgaY0kL42iBlWUC4JYMSxen4coyzXy5PAJ08Tn7 txIvV/ckygJJ2j+coCeT6Vyh2Sg7eHc= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nWwnhqHi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of vannapurve@google.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=vannapurve@google.com Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2357c61cda7so155975ad.1 for ; Wed, 09 Jul 2025 07:29:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752071341; x=1752676141; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=otqsPDt5osEXqjc5euk3u1KgCdwLcQ8YUxZOr7B7LAY=; b=nWwnhqHiWX/FVNQqPJ0++1PaHH6Z+1XNB+xjnf9i+l2yHIXwZ1buUYOAjeyZgWw8Xu uDYzEmT1p9Q449n5Adn1mmNGoDl+bLUOiDBnywACkV0TNIPDpE/BLlmDW7LfoD8BTgVQ yxLu0Dr4lnUvUXTvkeoCrv6JSLyFzwEMpblFRNOODoV/U1lHCCXf6ZiQDo6Sw5JIxDwc 5cNw2cxTTDHLeEOUq8f6VgtMNb7nlxNIlu18E8ldgCykx7vr30MmYe/nLPH+UQSywBF5 eo+XaoX6B0BKwpAY0PxD2HkjsA7bylakmXj8q12d0IsRLwr6sjIxIX7OMJz3nmt8YmGW ZKpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752071342; x=1752676142; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=otqsPDt5osEXqjc5euk3u1KgCdwLcQ8YUxZOr7B7LAY=; b=c1buV5fbuTnFZrckuZ2tFN/njIK0D0igBVWYJsoJUeKa+0/ZhHU0hd86hi5wa8O9kU E59iXpjVHiggNzvzQKahRWymb680gOqzrsAHDMXDOXQi3EjGCKQQnMmOc+9B8fq9VyBV vD17BStnFus1ubp/FRXio3CbmXdjUaSl7WMNVXvDjPE519HAhArT8thj33rp4QbStW1h TsicpIMhY+niYd7Rqf/0RdeI0S8MAicqV3KfUY9CdnMGEbT30HXepD/cNqwr8X+R+U6p Fr0/R+z+LRwuj2DcqJkGzorldx4wN4vQsKiYM3p4Lg9qC6KWZ8ofxJLHSkwxMSxDDIWu K0ug== X-Forwarded-Encrypted: i=1; AJvYcCW8OLwOTB3oUVdfIbScPO3xyRmZjarS+r+V6JaXLCRRiIYlbBQRsHYvLofMSmZC11SsCbG81y1OoA==@kvack.org X-Gm-Message-State: AOJu0Yz75v758yhZ4/7pr1oUKTuWMBKde+BUZlNJXtDqpA5RRYveDfLH 3yjVACmPKFFqESAv9ffHUYakugfB1xA+cBaWbZYp5oE5nXkN+r8uiKh/nzopAE4fTMwfKMhNA0Z gzn3CaHbh9oJkPtQwwscsS2Rm1Me/YREnwjaOHMrf X-Gm-Gg: ASbGncsy1kuo3EQrVx8bsZzUnzGP6SDAv6IkYf8ZbK3ss60M24mTv2lDPmFxOYG95j/ C0OfcSgxEForb7DOegYCIFjV+4f+rxXK3n99Oz6ui6vsq8wA86L3Afz+3DjanTrfM6Edi9C3FrT olpYFw3BvbZjcnF4Il8iGTbxN6mePaBaujBEyzqK/Q28sDHX/w+wk5FYRVYB3cU5VQhMwgYfbfL fsX X-Google-Smtp-Source: AGHT+IHZnJT8IPYNYYlq8IZWbrFrKSERazCCUgk/PYCXPT2eafZj+EWruKSZR2KdLOBmcVX/Dr9qFLZcvU+hjVe4mck= X-Received: by 2002:a17:903:234a:b0:22e:4509:cb86 with SMTP id d9443c01a7336-23ddae070d9mr2349035ad.19.1752071340735; Wed, 09 Jul 2025 07:29:00 -0700 (PDT) MIME-Version: 1.0 References: <006899ccedf93f45082390460620753090c01914.camel@intel.com> <5decd42b3239d665d5e6c5c23e58c16c86488ca8.camel@intel.com> In-Reply-To: From: Vishal Annapurve Date: Wed, 9 Jul 2025 07:28:48 -0700 X-Gm-Features: Ac12FXzvLP7atWaiCB-cuaA1xwFEC8dxkhwp1kDPwptSswG_7FLCPsdSJrQ6QBQ Message-ID: Subject: Re: [RFC PATCH v2 00/51] 1G page support for guest_memfd To: Sean Christopherson Cc: Rick P Edgecombe , "pvorel@suse.cz" , "kvm@vger.kernel.org" , "catalin.marinas@arm.com" , Jun Miao , "palmer@dabbelt.com" , "pdurrant@amazon.co.uk" , "vbabka@suse.cz" , "peterx@redhat.com" , "x86@kernel.org" , "amoorthy@google.com" , "tabba@google.com" , "quic_svaddagi@quicinc.com" , "maz@kernel.org" , "vkuznets@redhat.com" , "anthony.yznaga@oracle.com" , "mail@maciej.szmigiero.name" , "quic_eberman@quicinc.com" , Wei W Wang , Fan Du , "Wieczor-Retman, Maciej" , Yan Y Zhao , "ajones@ventanamicro.com" , Dave Hansen , "paul.walmsley@sifive.com" , "quic_mnalajal@quicinc.com" , "aik@amd.com" , "usama.arif@bytedance.com" , "fvdl@google.com" , "jack@suse.cz" , "quic_cvanscha@quicinc.com" , Kirill Shutemov , "willy@infradead.org" , "steven.price@arm.com" , "anup@brainfault.org" , "thomas.lendacky@amd.com" , "keirf@google.com" , "mic@digikod.net" , "linux-kernel@vger.kernel.org" , "nsaenz@amazon.es" , "akpm@linux-foundation.org" , "oliver.upton@linux.dev" , "binbin.wu@linux.intel.com" , "muchun.song@linux.dev" , Zhiquan1 Li , "rientjes@google.com" , Erdem Aktas , "mpe@ellerman.id.au" , "david@redhat.com" , "jgg@ziepe.ca" , "hughd@google.com" , "jhubbard@nvidia.com" , Haibo1 Xu , Isaku Yamahata , "jthoughton@google.com" , "rppt@kernel.org" , "steven.sistare@oracle.com" , "jarkko@kernel.org" , "quic_pheragu@quicinc.com" , "chenhuacai@kernel.org" , Kai Huang , "shuah@kernel.org" , "bfoster@redhat.com" , "dwmw@amazon.co.uk" , Chao P Peng , "pankaj.gupta@amd.com" , Alexander Graf , "nikunj@amd.com" , "viro@zeniv.linux.org.uk" , "pbonzini@redhat.com" , "yuzenghui@huawei.com" , "jroedel@suse.de" , "suzuki.poulose@arm.com" , "jgowans@amazon.com" , Yilun Xu , "liam.merwick@oracle.com" , "michael.roth@amd.com" , "quic_tsoni@quicinc.com" , Xiaoyao Li , "aou@eecs.berkeley.edu" , Ira Weiny , "richard.weiyang@gmail.com" , "kent.overstreet@linux.dev" , "qperret@google.com" , "dmatlack@google.com" , "james.morse@arm.com" , "brauner@kernel.org" , "linux-fsdevel@vger.kernel.org" , "ackerleytng@google.com" , "pgonda@google.com" , "quic_pderrin@quicinc.com" , "roypat@amazon.co.uk" , "hch@infradead.org" , "will@kernel.org" , "linux-mm@kvack.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: E386B16000C X-Rspamd-Server: rspam03 X-Stat-Signature: qj7xnhzxjnz9mhodaxzxs8tsmzhou8ic X-HE-Tag: 1752071342-257281 X-HE-Meta: U2FsdGVkX188+MrNDK9VuAk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2txCoddtpHPEjQylhYrNaCThlDAvr3Zq2+vqyAw7yE0/4XTSwkF4tjqAQ8bHYwP78MCK/nv4SzmXmazxnV7boEHc4YlyXfP1MDHDWQM+NiJgUUUQeHqEUDa4jI3V5IFjEw/Z1KBpjkGHMGhh9gKVi/gDqVp2QMxAIYxF5+faQtUOXG+/1ymrVGHe52UD2EuaF+wNBkYnZt0VvR3es7HLbRcGTvegDUtPiWOaY0MG52MEdiwjlMZwvOW8FE1aSwgssxAWjSwdqIWuh34qGXoRQQ== 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 Tue, Jul 8, 2025 at 11:55=E2=80=AFAM Sean Christopherson wrote: > > On Tue, Jul 08, 2025, Rick P Edgecombe wrote: > > On Tue, 2025-07-08 at 11:03 -0700, Sean Christopherson wrote: > > > > I think there is interest in de-coupling it? > > > > > > No? > > > > I'm talking about the intra-host migration/reboot optimization stuff. A= nd not > > doing a good job, sorry. > > > > > Even if we get to a point where multiple distinct VMs can bind to a= single > > > guest_memfd, e.g. for inter-VM shared memory, there will still need t= o be a > > > sole > > > owner of the memory. AFAICT, fully decoupling guest_memfd from a VM = would add > > > non-trivial complexity for zero practical benefit. > > > > I'm talking about moving a gmem fd between different VMs or something u= sing > > KVM_LINK_GUEST_MEMFD [0]. Not advocating to try to support it. But tryi= ng to > > feel out where the concepts are headed. It kind of allows gmem fds (or = just > > their source memory?) to live beyond a VM lifecycle. > > I think the answer is that we want to let guest_memfd live beyond the "st= ruct kvm" > instance, but not beyond the Virtual Machine. From a past discussion on = this topic[*]. > > : No go. Because again, the inode (physical memory) is coupled to the v= irtual machine > : as a thing, not to a "struct kvm". Or more concretely, the inode is c= oupled to an > : ASID or an HKID, and there can be multiple "struct kvm" objects associ= ated with a > : single ASID. And at some point in the future, I suspect we'll have mu= ltiple KVM > : objects per HKID too. > : > : The current SEV use case is for the migration helper, where two KVM ob= jects share > : a single ASID (the "real" VM and the helper). I suspect TDX will end = up with > : similar behavior where helper "VMs" can use the HKID of the "real" VM.= For KVM, > : that means multiple struct kvm objects being associated with a single = HKID. > : > : To prevent use-after-free, KVM "just" needs to ensure the helper insta= nces can't > : outlive the real instance, i.e. can't use the HKID/ASID after the owni= ng virtual > : machine has been destroyed. > : > : To put it differently, "struct kvm" is a KVM software construct that _= usually_, > : but not always, is associated 1:1 with a virtual machine. > : > : And FWIW, stashing the pointer without holding a reference would not b= e a complete > : solution, because it couldn't guard against KVM reusing a pointer. E.= g. if a > : struct kvm was unbound and then freed, KVM could reuse the same memory= for a new > : struct kvm, with a different ASID/HKID, and get a false negative on th= e rebinding > : check. > > Exactly what that will look like in code is TBD, but the concept/logic ho= lds up. I think we can simplify the role of guest_memfd in line with discussion [1]= : 1) guest_memfd is a memory provider for userspace, KVM, IOMMU. - It allows fallocate to populate/deallocate memory 2) guest_memfd supports the notion of private/shared faults. 3) guest_memfd supports memory access control: - It allows shared faults from userspace, KVM, IOMMU - It allows private faults from KVM, IOMMU 4) guest_memfd supports changing access control on its ranges between shared/private. - It notifies the users to invalidate their mappings for the ranges getting converted/truncated. Responsibilities that ideally should not be taken up by guest_memfd: 1) guest_memfd can not initiate pre-faulting on behalf of it's users. 2) guest_memfd should not be directly communicating with the underlying architecture layers. - All communication should go via KVM/IOMMU. 3) KVM should ideally associate the lifetime of backing pagetables/protection tables/RMP tables with the lifetime of the binding of memslots with guest_memfd. - Today KVM SNP logic ties RMP table entry lifetimes with how long the folios are mapped in guest_memfd, which I think should be revisited. Some very early thoughts on how guest_memfd could be laid out for the long = term: 1) guest_memfd code ideally should be built-in to the kernel. 2) guest_memfd instances should still be created using KVM IOCTLs that carry specific capabilities/restrictions for its users based on the backing VM/arch. 3) Any outgoing communication from guest_memfd to it's users like userspace/KVM/IOMMU should be via notifiers to invalidate similar to how MMU notifiers work. 4) KVM and IOMMU can implement intermediate layers to handle interaction with guest_memfd. - e.g. there could be a layer within kvm that handles: - creating guest_memfd files and associating a kvm_gmem_context with those files. - memslot binding - kvm_gmem_context will be used to bind kvm memslots with the context ranges. - invalidate notifier handling - kvm_gmem_context will be used to intercept guest_memfd callbacks and translate them to the right GPA ranges. - linking - kvm_gmem_context can be linked to different KVM instances. This line of thinking can allow cleaner separation between guest_memfd/KVM/IOMMU [2]. [1] https://lore.kernel.org/lkml/CAGtprH-+gPN8J_RaEit=3DM_ErHWTmFHeCipC6viT= 6PHhG3ELg6A@mail.gmail.com/#t [2] https://lore.kernel.org/lkml/31beeed3-b1be-439b-8a5b-db8c06dadc30@amd.c= om/ > > [*] https://lore.kernel.org/all/ZOO782YGRY0YMuPu@google.com > > > [0] https://lore.kernel.org/all/cover.1747368092.git.afranji@google.com= / > > https://lore.kernel.org/kvm/cover.1749672978.git.afranji@google.com/