Re: [PATCH v8 09/46] KVM: guest_memfd: Introduce function to check GFN private/shared status
From: Fuad Tabba <hidden>
Date: 2026-06-19 08:25:49
Also in:
kvm, linux-coco, linux-doc, linux-kselftest, linux-mm, lkml
On Fri, 19 Jun 2026 at 01:31, Ackerley Tng via B4 Relay [off-list ref] wrote:
From: Ackerley Tng <redacted> Introduce function for KVM to check the private/shared status of guest memory at a given GFN. This will be used in a later patch. Signed-off-by: Ackerley Tng <redacted> Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Fuad Tabba <redacted> Cheers, /fuad
quoted hunk ↗ jump to hunk
--- include/linux/kvm_host.h | 2 ++ virt/kvm/guest_memfd.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+)diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 3915da2a61778..27687fb9d5201 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h@@ -2575,6 +2575,8 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */ #ifdef CONFIG_KVM_GUEST_MEMFD +bool kvm_gmem_is_private(struct kvm *kvm, gfn_t gfn); + int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, struct page **page, int *max_order);diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 8101f64e0366f..bca912db5be6e 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c@@ -510,6 +510,37 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) return 0; } +bool kvm_gmem_is_private(struct kvm *kvm, gfn_t gfn) +{ + struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); + struct inode *inode; + + /* + * If this gfn has no associated memslot, there's no chance of the gfn + * being backed by private memory, since guest_memfd must be used for + * private memory, and guest_memfd must be associated with some memslot. + */ + if (!slot) + return 0; + + CLASS(gmem_get_file, file)(slot); + if (!file) + return 0; + + inode = file_inode(file); + + /* + * Rely on the maple tree's internal RCU lock to ensure a + * stable result. This result can become stale as soon as the + * lock is dropped, so the caller _must_ still protect + * consumption of private vs. shared by checking + * mmu_invalidate_retry_gfn() under mmu_lock to serialize + * against ongoing attribute updates. + */ + return kvm_gmem_is_private_mem(inode, kvm_gmem_get_index(slot, gfn)); +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_is_private); + static struct file_operations kvm_gmem_fops = { .mmap = kvm_gmem_mmap, .open = generic_file_open, --2.55.0.rc0.738.g0c8ab3ebcc-goog