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

From: Ackerley Tng

Date: Tue Oct 07 2025 - 12:09:02 EST


Sean Christopherson <seanjc@xxxxxxxxxx> writes:

> On Mon, Oct 06, 2025, Ackerley Tng wrote:
>> Sean Christopherson <seanjc@xxxxxxxxxx> 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);
>

Had the same thing in mind, I guess having a precedent (and seeing it in
code) makes it seem less awkward. Thanks!

> 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.
>

I meant "conditionally valid" as in if GUEST_MEMFD_FLAG_BAR was valid
only when GUEST_MEMFD_FLAG_FOO is set, then with this model, when
KVM_CAP_GUEST_MEMFD_FLAGS is queried, would KVM return
GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_FOO | GUEST_MEMFD_FLAG_BAR,
where GUEST_MEMFD_FLAG_BAR is the conditionally valid flag?

>> 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.

Got it.