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 E387DC83F22 for ; Fri, 18 Jul 2025 02:58:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E9288D0003; Thu, 17 Jul 2025 22:58:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 798B78D0001; Thu, 17 Jul 2025 22:58:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6616A8D0003; Thu, 17 Jul 2025 22:58:02 -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 52CA38D0001 for ; Thu, 17 Jul 2025 22:58:02 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 01CCE12DBB0 for ; Fri, 18 Jul 2025 02:58:01 +0000 (UTC) X-FDA: 83675876004.24.9C1F697 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf10.hostedemail.com (Postfix) with ESMTP id 6129EC0006 for ; Fri, 18 Jul 2025 02:57:59 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bC4rr6tF; spf=none (imf10.hostedemail.com: domain of yilun.xu@linux.intel.com has no SPF policy when checking 192.198.163.8) smtp.mailfrom=yilun.xu@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=1752807479; 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=EwdNjsz7gnGjerNfbaPf+QCeDEqFaFjU/nP16bpDj24=; b=3GikFxhf92UoUHb6g5cq4/5FSu0kcwuXmZK6PPBvZK5ZcdBvhrv/Nd5uLmMY5UutMCMXD4 rWEIRHCn0W4EF3kn7IG+3vUMZVZaFmWg08vJKDr0GYpwv4VuR0C397nIi3W2kiPE2CACz4 9zKR4nor0uW9JLe8/qpIrsPJamkFrVk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bC4rr6tF; spf=none (imf10.hostedemail.com: domain of yilun.xu@linux.intel.com has no SPF policy when checking 192.198.163.8) smtp.mailfrom=yilun.xu@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752807479; a=rsa-sha256; cv=none; b=07SQCLIwpDLm9ds4rZ2/Ft2oszfMxePokD6TkNGqBYI+bwJimpkCxvVtlZepsf+XiQ6WOg OzcnL43PdDXZIocfRtgO4gsoOyWsp3PIvkapsFPwLlrx6lf+cglCLj8B1umLRaHdCWAUfT xLOmyRZGZ2PYY8qoiAS4UWBFzMNx9aM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1752807479; x=1784343479; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=yQspi0IdSkvFDWpvj8rAaHUF0Fjkr1+s0cusMb2OIWk=; b=bC4rr6tFiNBKKFH9Q3A/Zg5d7C5f7H+ENSJ//Sx/L0FQ4ltIZD5zo32e uoSZ6KvdkUZfbQptVKDsrmjF4DBLLhzG9L4KQa8GOwgIQ6gxuED6cICc+ zSDp9HYtZVS6Lmgm6cfGbuTdg/2DT3+1AudKhpyIM9zjO47SAoudhjm73 3/lk9ZVij1R6GoeEMjQnI7NO1wG7daRz9m9dJGLDr+Hg6jPldHH1tFreL 0MYh9ej4v+zIt6GVZxy9Izo/JsqfLm+l1FP/cIb3Ay205mgjhjq4Dbkpl eGBTqsBY0eU0OSN3buHgD/nLmvuGxibS9Y+RLaG/oHOeyOGdhmH8IS8FK g==; X-CSE-ConnectionGUID: zTYHSa9NS6WYsutHFGSE8Q== X-CSE-MsgGUID: vHM3ldQfRUG1QdGx99aVRQ== X-IronPort-AV: E=McAfee;i="6800,10657,11495"; a="72666010" X-IronPort-AV: E=Sophos;i="6.16,320,1744095600"; d="scan'208";a="72666010" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jul 2025 19:57:57 -0700 X-CSE-ConnectionGUID: a1mpQy6NQsSBicOKr/urXQ== X-CSE-MsgGUID: 4L4luONoSeyCt8ddAZZX9A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,320,1744095600"; d="scan'208";a="158033113" Received: from yilunxu-optiplex-7050.sh.intel.com (HELO localhost) ([10.239.159.165]) by orviesa007.jf.intel.com with ESMTP; 17 Jul 2025 19:57:38 -0700 Date: Fri, 18 Jul 2025 10:48:55 +0800 From: Xu Yilun To: Ackerley Tng 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 Subject: Re: [RFC PATCH v2 04/51] KVM: guest_memfd: Introduce KVM_GMEM_CONVERT_SHARED/PRIVATE ioctls Message-ID: References: <9502503f-e0c2-489e-99b0-94146f9b6f85@amd.com> <20250624130811.GB72557@ziepe.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Rspamd-Queue-Id: 6129EC0006 X-Rspamd-Server: rspam06 X-Stat-Sign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L/ffYJf/FjwBjIch3uwBSnvNzV4x4Jbxb5Qsret65e33Ke/GZgDPX+ET+l2HmzVcylXRdYsfL182xk4RNRpc0QyOqBbzUb24VAEeDey+mq2EZTkfXpk9FM9eeN/M009oEV2vX6rSNSyXLUJMT3kjOFqfQfZub4WDHPzyu18xResbXXkVx2aept6OU3/IAbEuOLqisR2O1LhGmlf0as4TkttH0mW5k6A14QRIJALZiRku7EEVHthrgOr+/W9Pf5rgLR1/nUMSKYWO2N7wU4KbX0nwRjk+/y4vAg1bf4bBijHBCyJD+3R7HSA6ZQF5wA1nuMJQl5D8gswPFLqaacm6BSRKL3ZhRbE99wWapClZ/QitWiotV9Dv+yndryBR6pPsM7sTOeW0tatdli5MCCABdBYoeQX7PMWFex0ubln7/T98J 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 Thu, Jul 17, 2025 at 09:56:01AM -0700, Ackerley Tng wrote: > Xu Yilun writes: > > > On Wed, Jul 16, 2025 at 03:22:06PM -0700, Ackerley Tng wrote: > >> Yan Zhao writes: > >> > >> > On Tue, Jun 24, 2025 at 07:10:38AM -0700, Vishal Annapurve wrote: > >> >> On Tue, Jun 24, 2025 at 6:08 AM Jason Gunthorpe wrote: > >> >> > > >> >> > On Tue, Jun 24, 2025 at 06:23:54PM +1000, Alexey Kardashevskiy wrote: > >> >> > > >> >> > > 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 these > >> >> > > 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 TLB > >> >> > >> >> As Jason described, ideally IOMMU just like KVM, should just: > >> >> 1) Directly rely on guest_memfd for pinning -> no page refcounts taken > >> >> 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 will fail the > >> > unmap of the pages from S-EPT. > >> > > >> > If IOMMU side does not increase refcount, IMHO, some way to indicate that > >> > 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. > >> > > >> > > >> > >> Yan, Yilun, would it work if, on conversion, > >> > >> 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? Yes, unless guest doesn't unpin the page first by mistake. Guest would invoke a fw call tdg.mem.page.release to unpin the page before KVM_HC_MAP_GPA_RANGE. > > 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? Yes, again unless guess doesn't unpin. > > >> 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? Correct. The unmapping will work, the guest can't continue anymore. Thanks, Yilun