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

Sean Christopherson
2025-01-21T16:05:59Z

On Mon, Jan 20, 2025, Christoph Schlameuss wrote:
> 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@linux.intel.com>
> > Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
> > Cc: Christian Borntraeger <borntraeger@de.ibm.com>
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> > 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);

It's not missing. As of this patch, x86 is the only user of KVM-internal memslots,
and x86 acquires slots_lock outside of kvm_set_internal_memslot() because x86 can
have multiple address spaces (regular vs SMM) and KVM's internal memslots need to
be created for both, i.e. it's desirable to holds slots_lock in the caller.

If it's annoying for s390 to acquire slots_lock, we could add a wrapper, i.e. turn
this into __kvm_set_internal_memslot() and then re-add kvm_set_internal_memslot()
as a version that acquires and releases slots_lock.