[RFC PATCH 01/14] KVM: guest_memfd: Clamp the order of PFN mode blocks to what KVM can map
From: Fred Griffoul
Date: Tue Oct 27 2026 - 12:07:04 EST
From: Fred Griffoul <fgriffo@xxxxxxxxxxxx>
KVM maps guest memory in 4K, 2M or 1G blocks, and uses guest_memfd's
max_order to choose. A folio of order N is physically aligned and never
mixes shared and private pages, so the order needs no check. A PFN mode
provider's frames need not be aligned and the provider does not know
which pages are private, yet kvm_gmem_get_pfn() passes its order to KVM
unchanged. KVM can then map a block over the wrong frames, or with one
attribute over shared and private pages.
Define the provider's order as that of the naturally aligned block
around the index, preset to 0. Clamp it to a block size KVM supports,
in which the gfn, the index and the pfn are aligned alike and every
page has the same attribute.
A fault that sized its block just before a revoke or conversion of
another page in the block would not retry, since KVM only retries
faults inside the invalidated range. Make every invalidation of a PFN
mode file cover the 1G-aligned blocks around its range.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Fred Griffoul <fgriffo@xxxxxxxxxxxx>
---
include/linux/guest_memfd.h | 5 +--
virt/kvm/guest_memfd.c | 63 +++++++++++++++++++++++++++++++++++--
2 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h
index 3e2c0714ca33..a38a81693422 100644
--- a/include/linux/guest_memfd.h
+++ b/include/linux/guest_memfd.h
@@ -21,8 +21,9 @@ struct mempolicy;
* @get_pfn: Obtain a PFN for a given index. The provider must ensure backing
* memory is present and is responsible for its contents, such as
* zeroing if necessary. On success, return 0 with the physical frame
- * number in @pfn and, if @max_order is non-NULL, the maximum page
- * order of contiguous backing memory containing @pfn. Returns a
+ * number in @pfn and, if @max_order is non-NULL, the largest order
+ * for which the naturally aligned block of 2^order pages that
+ * contains @index is backed by contiguous frames. Returns a
* negative error code on failure. The provider must define
* @lookup_pfn_range when @get_pfn is defined.
* @lookup_pfn_range: Look up the PFN for a given index. Returns 0 on success
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 3c4bedceccfc..be26bd16d420 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -147,6 +147,8 @@ static int gmem_provider_get_pfn(struct gmem_inode *gi, pgoff_t index,
unsigned long raw_pfn;
int r;
+ if (max_order)
+ *max_order = 0;
r = gi->provider_ops->get_pfn(gi->provider, index, &raw_pfn, max_order);
if (r)
return r;
@@ -326,10 +328,13 @@ static enum kvm_gfn_range_filter kvm_gmem_get_all_gfns_filter(struct inode *inod
return KVM_FILTER_PRIVATE;
}
-static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,
+static void __kvm_gmem_invalidate_start(struct inode *inode,
+ struct gmem_file *f, pgoff_t start,
pgoff_t end,
enum kvm_gfn_range_filter attr_filter)
{
+ const gfn_t blk = kvm_gmem_is_pfn_mode(GMEM_I(inode)) ?
+ 1UL << PUD_ORDER : 1;
bool flush = false, found_memslot = false;
struct kvm_memory_slot *slot;
struct kvm *kvm = f->kvm;
@@ -355,6 +360,16 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,
flush |= kvm_mmu_unmap_gfn_range(kvm, &gfn_range);
+ /*
+ * In PFN mode, a fault may map a block of up to PUD_ORDER
+ * pages, sized before this invalidation (see
+ * kvm_gmem_pfn_map_order()): make faults anywhere in the
+ * blocks around the range retry.
+ */
+ kvm_mmu_invalidate_range_add(kvm,
+ round_down(gfn_range.start, blk),
+ round_up(gfn_range.end, blk));
+
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
kvm_arch_gmem_invalidate_range(kvm, &gfn_range);
#endif
@@ -374,7 +389,7 @@ static void kvm_gmem_invalidate_start(struct inode *inode, pgoff_t start,
struct gmem_file *f;
kvm_gmem_for_each_file(f, inode)
- __kvm_gmem_invalidate_start(f, start, end, filter);
+ __kvm_gmem_invalidate_start(inode, f, start, end, filter);
}
static void __kvm_gmem_invalidate_end(struct gmem_file *f, pgoff_t start,
@@ -670,7 +685,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
* Zap all SPTEs pointed at by this file. Do not free the backing
* memory, as its lifetime is associated with the inode, not the file.
*/
- __kvm_gmem_invalidate_start(f, 0, -1ul,
+ __kvm_gmem_invalidate_start(inode, f, 0, -1ul,
kvm_gmem_get_all_gfns_filter(inode));
__kvm_gmem_invalidate_end(f, 0, -1ul);
@@ -1535,6 +1550,44 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
return folio;
}
+/*
+ * The order a PFN mode block can be mapped at: the provider's order, no
+ * larger than the alignment of @gfn and @pfn to @index allows, rounded down to
+ * a page-table size, and never over pages that are not all shared or all
+ * private, since KVM maps a block with a single attribute. Called with the
+ * invalidate lock held; an invalidation of any page of the block makes the
+ * fault retry (see __kvm_gmem_invalidate_start()).
+ */
+static int kvm_gmem_pfn_map_order(struct inode *inode, pgoff_t index,
+ gfn_t gfn, kvm_pfn_t pfn, int order)
+{
+ u64 misalign = (gfn ^ index) | (pfn ^ index);
+ u64 attrs;
+
+ if (WARN_ON_ONCE(order < 0))
+ return 0;
+
+ order = min(order, PUD_ORDER);
+ if (misalign)
+ order = min_t(int, order, __ffs64(misalign));
+
+ if (order >= PUD_ORDER)
+ order = PUD_ORDER;
+ else if (order >= PMD_ORDER)
+ order = PMD_ORDER;
+ else
+ return 0;
+
+ attrs = kvm_gmem_get_attributes(inode,
+ mtree_load(&GMEM_I(inode)->attributes, index));
+ while (order &&
+ !kvm_gmem_range_has_attributes(inode,
+ ALIGN_DOWN(index, 1UL << order),
+ 1UL << order, attrs))
+ order = order == PUD_ORDER ? PMD_ORDER : 0;
+ return order;
+}
+
int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
gfn_t gfn, kvm_pfn_t *pfn, int *max_order)
{
@@ -1556,6 +1609,10 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
goto out;
}
+ if (kvm_gmem_is_pfn_mode(GMEM_I(file_inode(file))))
+ *max_order = kvm_gmem_pfn_map_order(file_inode(file), index, gfn,
+ *pfn, *max_order);
+
if (kvm_arch_has_gmem_convert() &&
kvm_gmem_is_private_mem(file_inode(file), index))
r = kvm_arch_gmem_make_private(kvm, gfn, *pfn,
--
2.47.3