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 DAC6DC83F34 for ; Thu, 17 Jul 2025 16:56:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79AE58D000A; Thu, 17 Jul 2025 12:56:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74B008D0006; Thu, 17 Jul 2025 12:56:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EC618D000A; Thu, 17 Jul 2025 12:56:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4B1EA8D0006 for ; Thu, 17 Jul 2025 12:56:06 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CDA8C5643C for ; Thu, 17 Jul 2025 16:56:05 +0000 (UTC) X-FDA: 83674359090.03.707D10C Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf15.hostedemail.com (Postfix) with ESMTP id 010DBA0004 for ; Thu, 17 Jul 2025 16:56:03 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="v1QGbJ/0"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3Iit5aAsKCKcHJRLYSLfaUNNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3Iit5aAsKCKcHJRLYSLfaUNNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752771364; a=rsa-sha256; cv=none; b=CuxwJLwLgjzexkSXnOTwQNggnNRIY925E55IaCYdUSidyjDW5NxmoICGif+Y+g9gVpSJqH YTqgx4kwmi7IfHIIXoWMfcYw/QhDTO0oaxx9G2RaS0U2BTD8c0Lb/FrO3r8+Xko8etaPpV hRvvWMWoNJ5b1aemarRWpCEWQxIXZBM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="v1QGbJ/0"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3Iit5aAsKCKcHJRLYSLfaUNNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3Iit5aAsKCKcHJRLYSLfaUNNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752771364; 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=YtKaXiis7X8OUZ8IxU2O9gUN20EGW8+A38M9nr3POFE=; b=RIi7umj47bwfMcz/X2VxMYNHuy2wBj5zZ0rCAz7nlFOB1HO0CLx6DRCKHve+RYtCMU8aSP 8jgTGJrH0buQI8U15p5ydHcAruOZCbY2D4lxogfGhMOVmC6MH8FOFoXnIcGpJJPwmo7kBR x6CG2Q834zbM/dGT8GSS4lfI0I0DJZE= Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2c36951518so1235027a12.2 for ; Thu, 17 Jul 2025 09:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752771363; x=1753376163; darn=kvack.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=YtKaXiis7X8OUZ8IxU2O9gUN20EGW8+A38M9nr3POFE=; b=v1QGbJ/02PoC7RrRHlDjJm/mJ8dXGG7SxKY93wvgPUqfos5hm3M+gTV94emlGd1aFB 95kIIo5dQ87gWbu+ToFUoxbhIoqJZW9FQY4e4CD3cpVjmkyxVqR0qdeQYBSDdiNoYMzx WceSV1lvNyVtjg4xwDcB1OQ2dWNFpAZyfmvefK/xL6iJ4lrmWhzq5cbtYH3szBTmSeK4 WPBezzGkZXh4Q2I9ttLfs4uhg95x0+Jcq8B8O0lin21AD/U/OV31mqGBeiycQCSeSS/B 2+FGB4H8HbcEqXhkWHuXGY+ZCOAB5ko2X3Pk6Om8qVj3sXymFrXWxik1dQpfUTTOljUh Vfzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752771363; x=1753376163; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=YtKaXiis7X8OUZ8IxU2O9gUN20EGW8+A38M9nr3POFE=; b=czjSLkHs9hpnH+1qn+roSHxnU4vdXwLFZpqAATuKtjQam5+7tKgre8FceAr7XYR3dV PUrlhJcMno07J6Q8UE1reyL6fNb0ztAtVGLZ8iKMyKxUISJsG17HlnLViHvCF5/RvmuH Bf2dJS3nLYls3dNiDCuwVjC5v1q52YVLOkC65WRVu8BvBpdreB5j980vMjGzbQAckb++ 4DC2EP2i9Hk/nkxeTWjgmHmINnTY2HEO18JErDCOCXQmDqXkOR9myWeoTzGV92gSMZ9o emzTjSwhnUZNuKIbqKgc8iknSUclKFVVKyyj2Zpyamq7NehqYo4rYBeZOvcAuUH2Xjfq 3T0w== X-Forwarded-Encrypted: i=1; AJvYcCUERXgavyQofnbJTCxsyEIlZ7Y0BvhoQn2i8IVPxDZhJbljjeaqe64pJXR3zsb5vvGZRuT5qzm27w==@kvack.org X-Gm-Message-State: AOJu0YziRfSg7PhAnSaBX2skqGkoeex/q1IU83A5DyfqtPqeN+WcdPHw vRzaAYGBlhkAsswSK2bh18aenDhWbTbkeig85mxISr7TTPRVnJEVsXeQ+bIYO6U4MYqK9qggfNF Z5ZkZGkRrcqDYkVnmWRYqICAtQw== X-Google-Smtp-Source: AGHT+IGT+Ip7MN3kC7WfmhPlW1Zbfs3wNc+DIuzQ2qHyU4q2gnwNyjohySS08EGy0S5iPmKjYIt5Af/xFOvpCcEiDQ== X-Received: from pjg12.prod.google.com ([2002:a17:90b:3f4c:b0:312:df0e:5f09]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2584:b0:311:df4b:4b81 with SMTP id 98e67ed59e1d1-31caf8ef446mr4773759a91.25.1752771362614; Thu, 17 Jul 2025 09:56:02 -0700 (PDT) Date: Thu, 17 Jul 2025 09:56:01 -0700 In-Reply-To: Mime-Version: 1.0 References: <9502503f-e0c2-489e-99b0-94146f9b6f85@amd.com> <20250624130811.GB72557@ziepe.ca> Message-ID: Subject: Re: [RFC PATCH v2 04/51] KVM: guest_memfd: Introduce KVM_GMEM_CONVERT_SHARED/PRIVATE ioctls From: Ackerley Tng To: Xu Yilun Cc: Yan Zhao , Vishal Annapurve , Jason Gunthorpe , Alexey Kardashevskiy , Fuad Tabba , kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org, ajones@ventanamicro.com, akpm@linux-foundation.org, amoorthy@google.com, anthony.yznaga@oracle.com, anup@brainfault.org, aou@eecs.berkeley.edu, bfoster@redhat.com, binbin.wu@linux.intel.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, 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, thomas.lendacky@amd.com, usama.arif@bytedance.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, yilun.xu@intel.com, yuzenghui@huawei.com, zhiquan1.li@intel.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 010DBA0004 X-Stat-Signature: 7psya53u45eb3yqfhqcii6gd31aprewa X-HE-Tag: 1752771363-73954 X-HE-Meta: U2FsdGVkX1+05tfnvqPrmlPbyXfIt/JlosAkR+u3tuM9xydLTJohPhggFXe2mKKA5jjuxU2NKnOxPP/P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0r7B+O6ZqNR6ORAb4Cs2mEzHligOPGKEVOVLHKmTwJp826Bm003Uw5zZi4wdQG4/W1SiRwN/EPJCUuGfu/yZ1p6j/EB2prMiTFhJdsr8DoTGVt8y+NZbsy/8dxF5grg6ctQSd19/MuG5F0H4bFiJV54BBJNyId65g44njScWS8G+S9I3n0E3KRGHLjDnK6ja7DQs9vRo+6erA4os9wHOkq4YWSjBH3VQnDC887QYB9EKbqec8ryhA4o0M9FIKflqxUnzrYun9G0xB4qq4sZT3i8wXrdReQjRP/JUhevhei8k5MLZSRUVWBx+Lu+d91Lh20DFR3xqRMMPnLY95ANxJQavrsD/bQiCPgMdRxe2IGy763aLqJtAV8IYe8zWfKkoFsadguECQtjiNYaDZfEBQFf0PnmJAGkFSdASdhVVbGKsiltn+yuT3G1U6h01Akf0jCOUXIUppb1d5rtTXKWfDflMatjWPjAmsQp8KqlEujh7RijNeIMfWoQAv0xgpSeiV6PM5GlK8TTtJ7vgt3aCdmdz6bxtyISTXgzYQK/jMhqMqrML17QRShZI3W3YHcyqoQeFJkVY= 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: Xu Yilun writes: > On Wed, Jul 16, 2025 at 03:22:06PM -0700, Ackerley Tng wrote: >> Yan Zhao writes: >>=20 >> > On Tue, Jun 24, 2025 at 07:10:38AM -0700, Vishal Annapurve wrote: >> >> On Tue, Jun 24, 2025 at 6:08=E2=80=AFAM Jason Gunthorpe wrote: >> >> > >> >> > On Tue, Jun 24, 2025 at 06:23:54PM +1000, Alexey Kardashevskiy wrot= e: >> >> > >> >> > > Now, I am rebasing my RFC on top of this patchset and it fails in >> >> > > kvm_gmem_has_safe_refcount() as IOMMU holds references to all the= se >> >> > > folios in my RFC. >> >> > > >> >> > > So what is the expected sequence here? The userspace unmaps a DMA >> >> > > page and maps it back right away, all from the userspace? The end >> >> > > result will be the exactly same which seems useless. And IOMMU TL= B >> >>=20 >> >> As Jason described, ideally IOMMU just like KVM, should just: >> >> 1) Directly rely on guest_memfd for pinning -> no page refcounts take= n >> >> by IOMMU stack >> > In TDX connect, TDX module and TDs do not trust VMM. So, it's the TDs = to inform >> > TDX module about which pages are used by it for DMAs purposes. >> > So, if a page is regarded as pinned by TDs for DMA, the TDX module wil= l fail the >> > unmap of the pages from S-EPT. >> > >> > If IOMMU side does not increase refcount, IMHO, some way to indicate t= hat >> > certain PFNs are used by TDs for DMA is still required, so guest_memfd= can >> > reject the request before attempting the actual unmap. >> > Otherwise, the unmap of TD-DMA-pinned pages will fail. >> > >> > Upon this kind of unmapping failure, it also doesn't help for host to = retry >> > unmapping without unpinning from TD. >> > >> > >>=20 >> Yan, Yilun, would it work if, on conversion, >>=20 >> 1. guest_memfd notifies IOMMU that a conversion is about to happen for a >> PFN range > > It is the Guest fw call to release the pinning. I see, thanks for explaining. > By the time VMM get the > conversion requirement, the page is already physically unpinned. So I > agree with Jason the pinning doesn't have to reach to iommu from SW POV. > If by the time KVM gets the conversion request, the page is unpinned, then we're all good, right? When guest_memfd gets the conversion request, as part of conversion handling it will request to zap the page from stage-2 page tables. TDX module would see that the page is unpinned and the unmapping will proceed fine. Is that understanding correct? >> 2. IOMMU forwards the notification to TDX code in the kernel >> 3. TDX code in kernel tells TDX module to stop thinking of any PFNs in >> the range as pinned for DMA? > > TDX host can't stop the pinning. Actually this mechanism is to prevent > host from unpin/unmap the DMA out of Guest expectation. > On this note, I'd also like to check something else. Putting TDX connect and IOMMUs aside, if the host unmaps a guest private page today without the guest requesting it, the unmapping will work and the guest will be broken, right? > Thanks, > Yilun > >>=20 >> If the above is possible then by the time we get to unmapping from >> S-EPTs, TDX module would already consider the PFNs in the range "not >> pinned for DMA".