Re: [PATCH v2 3/5] KVM: Add a dedicated API for setting KVM-internal memslots

From: Christoph Schlameuss
Date: Mon Jan 20 2025 - 09:45:20 EST


On Sat Jan 11, 2025 at 1:20 AM CET, Sean Christopherson wrote:
> Add a dedicated API for setting internal memslots, and have it explicitly
> disallow setting userspace memslots. Setting a userspace memslots without
> a direct command from userspace would result in all manner of issues.
>
> No functional change intended.
>
> Cc: Tao Su <tao1.su@xxxxxxxxxxxxxxx>
> Cc: Claudio Imbrenda <imbrenda@xxxxxxxxxxxxx>
> Cc: Christian Borntraeger <borntraeger@xxxxxxxxxx>
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> ---
> arch/x86/kvm/x86.c | 2 +-
> include/linux/kvm_host.h | 4 ++--
> virt/kvm/kvm_main.c | 15 ++++++++++++---
> 3 files changed, 15 insertions(+), 6 deletions(-)

[...]

> +int kvm_set_internal_memslot(struct kvm *kvm,
> + const struct kvm_userspace_memory_region2 *mem)
> +{
> + if (WARN_ON_ONCE(mem->slot < KVM_USER_MEM_SLOTS))
> + return -EINVAL;
> +

Looking at Claudios changes I found that this is missing to acquire the
slots_lock here.

guard(mutex)(&kvm->slots_lock);

> + return __kvm_set_memory_region(kvm, mem);
> +}
> +EXPORT_SYMBOL_GPL(kvm_set_internal_memslot);
>
> static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
> struct kvm_userspace_memory_region2 *mem)