On Mon, Dec 15, 2025 at 03:51:53PM -0500, Peter Xu wrote:
> Detect supported guest-memfd flags by the current kernel, and reject
> creations of guest-memfd using invalid flags.  When the cap isn't
> available, then no flag is supported.
> 
> Reviewed-by: Xiaoyao Li <[email protected]>
> Signed-off-by: Peter Xu <[email protected]>
> ---
>  accel/kvm/kvm-all.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 59836ebdff..68d57c1af0 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -108,6 +108,7 @@ static int kvm_sstep_flags;
>  static bool kvm_immediate_exit;
>  static uint64_t kvm_supported_memory_attributes;
>  static bool kvm_guest_memfd_supported;
> +static uint64_t kvm_guest_memfd_flags_supported;
>  static hwaddr kvm_max_slot_size = ~0;
>  
>  static const KVMCapabilityInfo kvm_required_capabilites[] = {
> @@ -2787,6 +2788,10 @@ static int kvm_init(AccelState *as, MachineState *ms)
>      kvm_guest_memfd_supported =
>          kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
>          kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
> +
> +    ret = kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD_FLAGS);
> +    kvm_guest_memfd_flags_supported = ret > 0 ? ret : 0;

kvm_vm_check_extension() zeroes out negative return values already so
should be able to use the same format as below.

But either way:

Reviewed-by: Michael Roth <[email protected]>

> +
>      kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, 
> KVM_CAP_PRE_FAULT_MEMORY);
>  
>      if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {
> @@ -4492,6 +4497,13 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t 
> flags, Error **errp)
>          return -1;
>      }
>  
> +    if (flags & ~kvm_guest_memfd_flags_supported) {
> +        error_setg(errp, "Current KVM instance does not support "
> +                   "guest-memfd flag: 0x%"PRIx64,
> +                   flags & ~kvm_guest_memfd_flags_supported);
> +        return -1;
> +    }
> +
>      fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
>      if (fd < 0) {
>          error_setg_errno(errp, errno, "Error creating KVM guest_memfd");
> -- 
> 2.50.1
> 
> 

Reply via email to