Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev
Cc: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>,
	Ackerley Tng <ackerleytng@google.com>,
	David Woodhouse <dwmw2@infradead.org>,
	David Hildenbrand <david@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	Alexey Kardashevskiy <aik@amd.com>,
	Xu Yilun <yilun.xu@linux.intel.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Steven Price <steven.price@arm.com>,
	Fred Griffoul <griffoul@gmail.com>,
	iommu@lists.linux.dev, kvm@vger.kernel.org,
	patches@lists.linux.dev
Subject: [RFC PATCH v5 06/16] KVM: guest_memfd: Share memory through provider range lookup
Date: Thu, 12 Nov 2026 13:11:10 +0530	[thread overview]
Message-ID: <20261112074120.1570072-7-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261112074120.1570072-1-aneesh.kumar@kernel.org>

Use provider range lookup for both folios and PFNs when converting memory
to shared. Skip holes without allocating pages and return lookup errors.

Invalidate mappings before conversion and publish shared attributes only
after conversion succeeds.

Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
 virt/kvm/guest_memfd.c | 126 ++++++++++++++++++++---------------------
 1 file changed, 62 insertions(+), 64 deletions(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index c69a8b7d046c..ffe0a8e0f2dd 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -1110,87 +1110,79 @@ static int gmem_folio_prepare_attributes(struct inode *inode, void *provider,
 	return 0;
 }
 
-#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT
-static void kvm_gmem_make_shared(struct inode *inode, pgoff_t start, pgoff_t end)
+static int gmem_provider_make_shared(struct inode *inode, pgoff_t start,
+		pgoff_t end)
 {
+#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT
 	struct gmem_inode *gi = GMEM_I(inode);
-	struct folio_batch fbatch;
-	pgoff_t next = start;
-	int i;
+	void *entry;
 
-	if (kvm_gmem_is_pfn_mode(gi)) {
-		pgoff_t index;
+	MA_STATE(mas, &gi->attributes, start, start);
 
-		for (index = start; index < end; ) {
-			size_t step = 1;
+	if (!kvm_arch_has_gmem_convert())
+		return 0;
+
+	mas_for_each(&mas, entry, end - 1) {
+		pgoff_t first = max(start, mas.index);
+		pgoff_t last = min(end, mas.last + 1);
+
+		if (!(kvm_gmem_get_attributes(inode, entry) & KVM_MEMORY_ATTRIBUTE_PRIVATE))
+			continue;
+
+		while (first < last) {
 			size_t nr_pages;
-			kvm_pfn_t pfn;
 			u64 pfn_attrs;
-
-			if (!gmem_provider_lookup_pfn_range(gi, index, &pfn, &nr_pages,
-							    &pfn_attrs)) {
-				step = min_t(size_t, nr_pages, end - index);
-				kvm_arch_gmem_make_shared(pfn, step);
+			kvm_pfn_t pfn;
+			int ret;
+
+			ret = gmem_provider_lookup_pfn_range(gi, first, &pfn, &nr_pages,
+							     &pfn_attrs);
+			if (ret == -ENOENT) {
+				/*
+				 * Skip the reported hole, advancing at least one page
+				 * even if nr_pages is zero, but stop at the end of this
+				 * attribute range.
+				 */
+				first += min_t(size_t, max_t(size_t, nr_pages, 1),
+					       last - first);
+				cond_resched();
+				continue;
 			}
+			if (ret)
+				return ret;
 
-			index += step;
-			cond_resched();
-		}
-		return;
-	}
+			nr_pages = min_t(size_t, nr_pages, last - first);
+			kvm_arch_gmem_make_shared(pfn, nr_pages);
+			ret = 0;
+			if (ret)
+				return ret;
 
-	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);
+			first += nr_pages;
+			cond_resched();
 		}
-
-		folio_batch_release(&fbatch);
-		cond_resched();
 	}
-}
-#else
-static void kvm_gmem_make_shared(struct inode *inode, pgoff_t start, pgoff_t end) {}
 #endif
+	return 0;
+}
 
 static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,
-				     size_t nr_pages, uint64_t attrs,
-				     pgoff_t *err_index)
+		size_t nr_pages, u64 attrs, pgoff_t *err_index)
 {
 	bool to_private = attrs & KVM_MEMORY_ATTRIBUTE_PRIVATE;
 	struct address_space *mapping = inode->i_mapping;
 	struct gmem_inode *gi = GMEM_I(inode);
 	enum kvm_gfn_range_filter filter;
 	pgoff_t end = start + nr_pages;
-	struct maple_tree *mt;
 	struct ma_state mas;
 	int r = 0;
 
-	mt = &gi->attributes;
-
 	*err_index = start;
 	filemap_invalidate_lock(mapping);
 
 	if (kvm_gmem_range_has_attributes(inode, start, nr_pages, attrs))
 		goto out;
 
-	mas_init(&mas, mt, start);
+	mas_init(&mas, &gi->attributes, start);
 	r = kvm_gmem_mas_preallocate(&mas, attrs, start, nr_pages);
 	if (r)
 		goto out;
@@ -1207,29 +1199,35 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,
 	if (gi->provider_ops->prepare_attributes) {
 		r = gi->provider_ops->prepare_attributes(inode, gi->provider, start,
 							nr_pages, attrs, err_index);
-		if (r) {
-			mas_destroy(&mas);
-			goto out;
-		}
+		if (r)
+			goto destroy;
 	}
 
-	/*
-	 * From this point on guest_memfd has performed necessary
-	 * checks and can proceed to do guest-breaking changes.
-	 */
-
 	gmem_provider_lock_updates(gi);
 
 	filter = to_private ? KVM_FILTER_SHARED : KVM_FILTER_PRIVATE;
 	kvm_gmem_invalidate_start(inode, start, end, filter);
 
-	if (!to_private && kvm_arch_has_gmem_convert())
-		kvm_gmem_make_shared(inode, start, end);
+	/*
+	 * Make existing pages host-accessible before publishing shared attributes.
+	 * Host faults map shared pages without performing architecture conversion.
+	 * Private conversion revokes host mappings here, but can defer making
+	 * pages private until a guest fault or prefault.
+	 */
+	if (!to_private)
+		r = gmem_provider_make_shared(inode, start, end);
 
-	mas_store_prealloc(&mas, xa_mk_value(attrs));
+	if (!r)
+		mas_store_prealloc(&mas, xa_mk_value(attrs));
 
+	if (r)
+		mas_destroy(&mas);
 	kvm_gmem_invalidate_end(inode, start, end);
 	gmem_provider_unlock_updates(gi);
+	goto out;
+
+destroy:
+	mas_destroy(&mas);
 out:
 	filemap_invalidate_unlock(mapping);
 	return r;
-- 
2.43.0


  parent reply	other threads:[~2026-11-12  7:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  7:41 [RFC PATCH v5 00/16] KVM: guest_memfd: Add a common framework for device memory providers Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 01/16] KVM: guest_memfd: Add a handle for provider PFNs Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 02/16] KVM: guest_memfd: Get PFNs through common provider operations Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 03/16] KVM: guest_memfd: Let providers coordinate mapping changes Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 04/16] KVM: guest_memfd: Add provider PFN range lookup Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 05/16] KVM: guest_memfd: Let providers validate attribute changes Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` Aneesh Kumar K.V (Arm) [this message]
2026-11-12  7:41 ` [RFC PATCH v5 07/16] KVM: guest_memfd: Release and revoke memory through provider operations Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 08/16] KVM: guest_memfd: Use provider operations for fallocate allocation Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 09/16] KVM: guest_memfd: Use provider operations for host mappings and faults Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 10/16] KVM: guest_memfd: Let providers prepare memory regions Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 11/16] KVM: guest_memfd: Pass KVM explicitly to provider attach Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 12/16] KVM: guest_memfd: Support devices through common provider operations Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 13/16] KVM: guest_memfd: Notify providers after private attribute updates Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 14/16] KVM: guest_memfd: Enable shared host mappings for device providers Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 15/16] KVM: guest_memfd: Notify providers during file release Aneesh Kumar K.V (Arm)
2026-11-12  7:41 ` [RFC PATCH v5 16/16] KVM: guest_memfd: Invalidate mappings before memslot unbind Aneesh Kumar K.V (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112074120.1570072-7-aneesh.kumar@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=ackerleytng@google.com \
    --cc=aik@amd.com \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=griffoul@gmail.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=patches@lists.linux.dev \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=steven.price@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=yilun.xu@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox