From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1922642F702; Thu, 13 Aug 2026 06:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786604191; cv=none; b=UPfU/keSsThGkaShUdtA294MKFnxuhjvECinhjk/FZCVNm3Be1n22zFl819MWKAefMNzq073MgXBJQ3FP3k7VXMhfeD3NEUHMwnLxZWJml3m0wt+HlSR+LZ5VEVzZFgqtqGVyvGN4Pmx9CrGr5aMcFgNK6DPino9m13WsSv602w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786604191; c=relaxed/simple; bh=EiRUT7eibLcdH0tOQiOYhPqjIQxyxShzLvniKf9KyN0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=lrErwSUz+inmbBN7aQytfeOC/+3GPPHFB4vW7spnYexe3qCCZNp9P8sWI4CmYXU0XX4eS/w3htRCyXuqLMMUn4bDgyeSxccMO9Gq7F8x5wPkIi+vPpT5VqC2EQQDzCfJoQ75g1WjmOnP7pu2dPUo57NtN/bsbIkrAPYMOJ0Ia8g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DcUSMufw; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DcUSMufw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786604190; x=1818140190; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=EiRUT7eibLcdH0tOQiOYhPqjIQxyxShzLvniKf9KyN0=; b=DcUSMufwNz+PqSYYqon6wSY5/VjfeChHGQXmwYoKYBh8Gq/gJ7yjtmBu Rv4sdZFiAz4zJSUdftQx8GwX1aqM/GDbvtgp8x4f+vf4pMLn2MQoGj6ng WzQtELWbFYyhH23sAENDUh8rbBMidu3fV61vuFCmqKsdG2OLrOKZ0EUSy R2HYLu3xF01GugRrqv/sW17Xg23h5lrNiJLY0L4WdeQx8rK2twPXv5WFg eKDFQXY0EnLBL3TrIFKQiZiX8Yl1xhVzLjMrtyLrapazUXwo2TXx5moZy R9U/KyitPKRoWuXYZevkGtx9g1TF0/zsF3GPmrz/FvD7Fwwa6vLnxyLaz A==; X-CSE-ConnectionGUID: GhMaEBwOTG+85KdnOVhNkw== X-CSE-MsgGUID: YMbghMO9ShyDNeycten64g== X-IronPort-AV: E=McAfee;i="6800,10657,11873"; a="86290007" X-IronPort-AV: E=Sophos;i="6.25,220,1779174000"; d="scan'208";a="86290007" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Aug 2026 23:56:28 -0700 X-CSE-ConnectionGUID: G3H/IDqNR/ShOWEVvKkhtw== X-CSE-MsgGUID: hHyHwXdWQJSEvzpnZYeIQg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,220,1779174000"; d="scan'208";a="265841672" Received: from unknown (HELO [10.238.2.33]) ([10.238.2.33]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Aug 2026 23:56:14 -0700 Message-ID: <1ec08cd8-3072-4753-ad5e-cd34956647f8@linux.intel.com> Date: Thu, 13 Aug 2026 14:56:12 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 12/41] KVM: guest_memfd: Call arch make_shared callback for to-shared conversion To: ackerleytng@google.com Cc: aik@amd.com, andrew.jones@linux.dev, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, jmattson@google.com, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, liam@infradead.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Baoquan He , Jason Gunthorpe , John Hubbard , Peter Xu , tarunsahu@google.com, Vlastimil Babka , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, Fuad Tabba References: <20260807-gmem-inplace-conversion-v10-0-2fc18ee6d3ba@google.com> <20260807-gmem-inplace-conversion-v10-12-2fc18ee6d3ba@google.com> Content-Language: en-US From: Binbin Wu In-Reply-To: <20260807-gmem-inplace-conversion-v10-12-2fc18ee6d3ba@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 8/8/2026 5:52 AM, Ackerley Tng via B4 Relay wrote: > From: Ackerley Tng > > When memory in guest_memfd is converted from private to shared, the > platform-specific state associated with the guest-private pages must be > invalidated or cleaned up. > > Iterate over the folios in the affected range and call the > kvm_arch_gmem_make_shared() hook for each PFN range. This allows > architectures to update hardware metadata or encryption states to > transition pages to the shared state. > > Invoke this helper after indicating to KVM's mmu code that an invalidation > is in progress to stop in-flight page faults from succeeding. > > Omit support for calling the arch hook to make private, since SNP, the only > implementer of the arch make-private hook today, would actually prefer > making private only just before faulting memory into the NPTs. > > Calling the make-private arch hook would require iterating both bindings > and the filemap to find the intersection of bindings and allocated > folios. On top of that, SNP would need to figure out whether to actually > make private based on whether the memory is about the be faulted, or ^ the -> to? > whether it is a conversion. > [...] > > +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT > +static void kvm_gmem_make_shared(struct inode *inode, pgoff_t start, pgoff_t end) > +{ > + struct folio_batch fbatch; > + pgoff_t next = start; > + int i; > + > + folio_batch_init(&fbatch); > + while (filemap_get_folios(inode->i_mapping, &next, end - 1, &fbatch)) { > + for (i = 0; i < folio_batch_count(&fbatch); ++i) { > + struct folio *folio = fbatch.folios[i]; > + pgoff_t start_index, end_index; > + kvm_pfn_t start_pfn; > + kvm_pfn_t nr_pages; > + > + start_index = max(start, folio->index); > + end_index = min(end, folio_next_index(folio)); > + /* > + * end_index is either in folio or points to > + * the first page of the next folio. Hence, > + * all pages in range [start_index, end_index) > + * are contiguous. > + */ > + start_pfn = folio_file_pfn(folio, start_index); > + nr_pages = end_index - start_index; > + > + kvm_arch_gmem_make_shared(start_pfn, nr_pages); > + } > + > + folio_batch_release(&fbatch); > + cond_resched(); > + } > +} > +#else > +static void kvm_gmem_make_shared(struct inode *inode, pgoff_t start, pgoff_t end) {} > +#endif > + > static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, > size_t nr_pages, uint64_t attrs, > pgoff_t *err_index) > @@ -599,7 +636,12 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, > > filter = to_private ? KVM_FILTER_SHARED : KVM_FILTER_PRIVATE; > kvm_gmem_invalidate_start(inode, start, end, filter); > + > + if (!to_private) > + kvm_gmem_make_shared(inode, start, end); If both KVM_AMD_SEV and KVM_INTEL_TDX are enabled, HAVE_KVM_ARCH_GMEM_CONVERT will be enabled and the logic in kvm_gmem_make_shared() introduces unnecessary overhead for TDX. Not sure about CSPs, but in a standard distribution kernel, it's very likely that both are enabled, right? Should kvm_gmem_make_shared() do some optimization or the overhead is relative small in the conversion to shared path so that the optimization is not worth it? > + > mas_store_prealloc(&mas, xa_mk_value(attrs)); > + > kvm_gmem_invalidate_end(inode, start, end); > out: > filemap_invalidate_unlock(mapping); >