Skip to content

Commit 44c6cb9

Browse files
committed
KVM: guest_memfd: Allow mmap() on guest_memfd for x86 VMs with private memory
Allow mmap() on guest_memfd instances for x86 VMs with private memory as the need to track private vs. shared state in the guest_memfd instance is only pertinent to INIT_SHARED. Doing mmap() on private memory isn't terrible useful (yet!), but it's now possible, and will be desirable when guest_memfd gains support for other VMA-based syscalls, e.g. mbind() to set NUMA policy. Lift the restriction now, before MMAP support is officially released, so that KVM doesn't need to add another capability to enumerate support for mmap() on private memory. Fixes: 3d3a04f ("KVM: Allow and advertise support for host mmap() on guest_memfd files") Reviewed-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Ackerley Tng <ackerleytng@google.com> Reviewed-by: David Hildenbrand <david@redhat.com> Link: https://lore.kernel.org/r/20251003232606.4070510-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9aef71c commit 44c6cb9

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

arch/x86/kvm/x86.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13942,10 +13942,11 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
1394213942

1394313943
#ifdef CONFIG_KVM_GUEST_MEMFD
1394413944
/*
13945-
* KVM doesn't yet support mmap() on guest_memfd for VMs with private memory
13946-
* (the private vs. shared tracking needs to be moved into guest_memfd).
13945+
* KVM doesn't yet support initializing guest_memfd memory as shared for VMs
13946+
* with private memory (the private vs. shared tracking needs to be moved into
13947+
* guest_memfd).
1394713948
*/
13948-
bool kvm_arch_supports_gmem_mmap(struct kvm *kvm)
13949+
bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm)
1394913950
{
1395013951
return !kvm_arch_has_private_mem(kvm);
1395113952
}

include/linux/kvm_host.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
729729
#endif
730730

731731
#ifdef CONFIG_KVM_GUEST_MEMFD
732-
bool kvm_arch_supports_gmem_mmap(struct kvm *kvm);
732+
bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm);
733+
734+
static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)
735+
{
736+
u64 flags = GUEST_MEMFD_FLAG_MMAP;
737+
738+
if (!kvm || kvm_arch_supports_gmem_init_shared(kvm))
739+
flags |= GUEST_MEMFD_FLAG_INIT_SHARED;
740+
741+
return flags;
742+
}
733743
#endif
734744

735745
#ifndef kvm_arch_has_readonly_mem

virt/kvm/guest_memfd.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ static const struct inode_operations kvm_gmem_iops = {
485485
.setattr = kvm_gmem_setattr,
486486
};
487487

488-
bool __weak kvm_arch_supports_gmem_mmap(struct kvm *kvm)
488+
bool __weak kvm_arch_supports_gmem_init_shared(struct kvm *kvm)
489489
{
490490
return true;
491491
}
@@ -549,13 +549,8 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
549549
{
550550
loff_t size = args->size;
551551
u64 flags = args->flags;
552-
u64 valid_flags = 0;
553552

554-
if (kvm_arch_supports_gmem_mmap(kvm))
555-
valid_flags |= GUEST_MEMFD_FLAG_MMAP |
556-
GUEST_MEMFD_FLAG_INIT_SHARED;
557-
558-
if (flags & ~valid_flags)
553+
if (flags & ~kvm_gmem_get_supported_flags(kvm))
559554
return -EINVAL;
560555

561556
if (size <= 0 || !PAGE_ALIGNED(size))

virt/kvm/kvm_main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4929,11 +4929,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
49294929
case KVM_CAP_GUEST_MEMFD:
49304930
return 1;
49314931
case KVM_CAP_GUEST_MEMFD_FLAGS:
4932-
if (!kvm || kvm_arch_supports_gmem_mmap(kvm))
4933-
return GUEST_MEMFD_FLAG_MMAP |
4934-
GUEST_MEMFD_FLAG_INIT_SHARED;
4935-
4936-
return 0;
4932+
return kvm_gmem_get_supported_flags(kvm);
49374933
#endif
49384934
default:
49394935
break;

0 commit comments

Comments
 (0)