Re: [PATCH v2 01/13] KVM: Rework KVM_CAP_GUEST_MEMFD_MMAP into KVM_CAP_GUEST_MEMFD_FLAGS

Sean Christopherson
2025-10-06T20:19:55Z

On Mon, Oct 06, 2025, Ackerley Tng wrote:
> Sean Christopherson <seanjc@google.com> writes:
>
> > 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.
>
> I know you suggested that guest_memfd's HugeTLB sizes shouldn't be
> squashed into the flags. Just using that as an example, would those
> kinds of flags (since they're using the upper bits, above the lower 32
> bits) be awkward to represent in this new model?

Are you asking specifically about flags that use bits 63:32? If so, no, I don't
see those as being awkward to deal with. Hopefully we kill of 32-bit KVM and it's
a complete non-issue, but even if we have to add KVM_CAP_GUEST_MEMFD_FLAGS2, I
don't see it being all that awkward for userspace to do:

uint64_t supported_gmem_flags = kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS) |
(kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS2) << 32);

We could even mimic what Intel did with 64-bit VMCS fields to handle 32-bit mode,
and explicitly name the second one KVM_CAP_GUEST_MEMFD_FLAGS_HI:

uint64_t supported_gmem_flags = kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS) |
(kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS_HI) << 32);

so that if KVM_CAP_GUEST_MEMFD_FLAGS_HI precedes 64-bit-only KVM, it could become
fully redundant, i.e. where someday this would hold true:

kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS) ==
kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS) | kvm_check_extension(KVM_CAP_GUEST_MEMFD_FLAGS_HI) << 32

> In this model, conditionally valid flags are always set,

I followed everything except this snippet.

> but userspace won't be able to do a flags check against the returned 32-bit
> value. Or do you think when this issue comes up, we'd put the flags in the
> upper bits in KVM_CAP_GUEST_MEMFD_FLAGS2 and userspace would then check
> against the OR-ed set of flags instead?

As above, enumerate support for flags 63:32 in a separate capability.