Skip to content

Commit 576d035

Browse files
Fuad Tabbabonzini
authored andcommitted
KVM: guest_memfd: Track guest_memfd mmap support in memslot
Add a new internal flag, KVM_MEMSLOT_GMEM_ONLY, to the top half of memslot->flags (which makes it strictly for KVM's internal use). This flag tracks when a guest_memfd-backed memory slot supports host userspace mmap operations, which implies that all memory, not just private memory for CoCo VMs, is consumed through guest_memfd: "gmem only". This optimization avoids repeatedly checking the underlying guest_memfd file for mmap support, which would otherwise require taking and releasing a reference on the file for each check. By caching this information directly in the memslot, we reduce overhead and simplify the logic involved in handling guest_memfd-backed pages for host mappings. Reviewed-by: Gavin Shan <gshan@redhat.com> Reviewed-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Acked-by: David Hildenbrand <david@redhat.com> Suggested-by: David Hildenbrand <david@redhat.com> Signed-off-by: Fuad Tabba <tabba@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20250729225455.670324-12-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent a12578e commit 576d035

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

include/linux/kvm_host.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
* used in kvm, other bits are visible for userspace which are defined in
5555
* include/uapi/linux/kvm.h.
5656
*/
57-
#define KVM_MEMSLOT_INVALID (1UL << 16)
57+
#define KVM_MEMSLOT_INVALID (1UL << 16)
58+
#define KVM_MEMSLOT_GMEM_ONLY (1UL << 17)
5859

5960
/*
6061
* Bit 63 of the memslot generation number is an "update in-progress flag",
@@ -2490,6 +2491,14 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
24902491
vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE;
24912492
}
24922493

2494+
static inline bool kvm_memslot_is_gmem_only(const struct kvm_memory_slot *slot)
2495+
{
2496+
if (!IS_ENABLED(CONFIG_KVM_GUEST_MEMFD))
2497+
return false;
2498+
2499+
return slot->flags & KVM_MEMSLOT_GMEM_ONLY;
2500+
}
2501+
24932502
#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
24942503
static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn)
24952504
{

virt/kvm/guest_memfd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
578578
*/
579579
WRITE_ONCE(slot->gmem.file, file);
580580
slot->gmem.pgoff = start;
581+
if (kvm_gmem_supports_mmap(inode))
582+
slot->flags |= KVM_MEMSLOT_GMEM_ONLY;
581583

582584
xa_store_range(&gmem->bindings, start, end - 1, slot, GFP_KERNEL);
583585
filemap_invalidate_unlock(inode->i_mapping);

0 commit comments

Comments
 (0)