Skip to content

Commit d2042d8

Browse files
committed
KVM: Rework KVM_CAP_GUEST_MEMFD_MMAP into KVM_CAP_GUEST_MEMFD_FLAGS
Rework the not-yet-released KVM_CAP_GUEST_MEMFD_MMAP into a more generic KVM_CAP_GUEST_MEMFD_FLAGS capability so that adding new flags doesn't require a new capability, and so that developers aren't tempted to bundle multiple flags into a single capability. Note, kvm_vm_ioctl_check_extension_generic() can only return a 32-bit value, but that limitation can be easily circumvented by adding e.g. KVM_CAP_GUEST_MEMFD_FLAGS2 in the unlikely event guest_memfd supports more than 32 flags. 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-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 034417c commit d2042d8

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6432,9 +6432,13 @@ most one mapping per page, i.e. binding multiple memory regions to a single
64326432
guest_memfd range is not allowed (any number of memory regions can be bound to
64336433
a single guest_memfd file, but the bound ranges must not overlap).
64346434

6435-
When the capability KVM_CAP_GUEST_MEMFD_MMAP is supported, the 'flags' field
6436-
supports GUEST_MEMFD_FLAG_MMAP. Setting this flag on guest_memfd creation
6437-
enables mmap() and faulting of guest_memfd memory to host userspace.
6435+
The capability KVM_CAP_GUEST_MEMFD_FLAGS enumerates the `flags` that can be
6436+
specified via KVM_CREATE_GUEST_MEMFD. Currently defined flags:
6437+
6438+
============================ ================================================
6439+
GUEST_MEMFD_FLAG_MMAP Enable using mmap() on the guest_memfd file
6440+
descriptor.
6441+
============================ ================================================
64386442

64396443
When the KVM MMU performs a PFN lookup to service a guest fault and the backing
64406444
guest_memfd has the GUEST_MEMFD_FLAG_MMAP set, then the fault will always be

include/uapi/linux/kvm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ struct kvm_enable_cap {
962962
#define KVM_CAP_ARM_EL2_E2H0 241
963963
#define KVM_CAP_RISCV_MP_STATE_RESET 242
964964
#define KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED 243
965-
#define KVM_CAP_GUEST_MEMFD_MMAP 244
965+
#define KVM_CAP_GUEST_MEMFD_FLAGS 244
966966

967967
struct kvm_irq_routing_irqchip {
968968
__u32 irqchip;

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,17 @@ static void test_guest_memfd_flags(struct kvm_vm *vm, uint64_t valid_flags)
262262

263263
static void test_guest_memfd(unsigned long vm_type)
264264
{
265-
uint64_t flags = 0;
266265
struct kvm_vm *vm;
267266
size_t total_size;
268267
size_t page_size;
268+
uint64_t flags;
269269
int fd;
270270

271271
page_size = getpagesize();
272272
total_size = page_size * 4;
273273

274274
vm = vm_create_barebones_type(vm_type);
275-
276-
if (vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP))
277-
flags |= GUEST_MEMFD_FLAG_MMAP;
275+
flags = vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS);
278276

279277
test_create_guest_memfd_multiple(vm);
280278
test_create_guest_memfd_invalid_sizes(vm, flags, page_size);
@@ -328,13 +326,14 @@ static void test_guest_memfd_guest(void)
328326
size_t size;
329327
int fd, i;
330328

331-
if (!kvm_has_cap(KVM_CAP_GUEST_MEMFD_MMAP))
329+
if (!kvm_check_cap(KVM_CAP_GUEST_MEMFD_FLAGS))
332330
return;
333331

334332
vm = __vm_create_shape_with_one_vcpu(VM_SHAPE_DEFAULT, &vcpu, 1, guest_code);
335333

336-
TEST_ASSERT(vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP),
337-
"Default VM type should always support guest_memfd mmap()");
334+
TEST_ASSERT(vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS) & GUEST_MEMFD_FLAG_MMAP,
335+
"Default VM type should support MMAP, supported flags = 0x%x",
336+
vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS));
338337

339338
size = vm->page_size;
340339
fd = vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP);

virt/kvm/kvm_main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4928,8 +4928,11 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
49284928
#ifdef CONFIG_KVM_GUEST_MEMFD
49294929
case KVM_CAP_GUEST_MEMFD:
49304930
return 1;
4931-
case KVM_CAP_GUEST_MEMFD_MMAP:
4932-
return !kvm || kvm_arch_supports_gmem_mmap(kvm);
4931+
case KVM_CAP_GUEST_MEMFD_FLAGS:
4932+
if (!kvm || kvm_arch_supports_gmem_mmap(kvm))
4933+
return GUEST_MEMFD_FLAG_MMAP;
4934+
4935+
return 0;
49334936
#endif
49344937
default:
49354938
break;

0 commit comments

Comments
 (0)