KVM: Convert the kvm->vcpus array to a xarray
authorMarc Zyngier <maz@kernel.org>
Tue, 16 Nov 2021 16:04:01 +0000 (16:04 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 8 Dec 2021 09:24:14 +0000 (04:24 -0500)
At least on arm64 and x86, the vcpus array is pretty huge (up to
1024 entries on x86) and is mostly empty in the majority of the cases
(running 1k vcpu VMs is not that common).

This mean that we end-up with a 4kB block of unused memory in the
middle of the kvm structure.

Instead of wasting away this memory, let's use an xarray instead,
which gives us almost the same flexibility as a normal array, but
with a reduced memory usage with smaller VMs.

Signed-off-by: Marc Zyngier <maz@kernel.org>
Message-Id: <20211116160403.4074052-6-maz@kernel.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index e2f9f8f67c58763af82f4c307662633c57672361..2201dc07126a318f643cea57c07c6d264eee63c7 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/refcount.h>
 #include <linux/nospec.h>
 #include <linux/notifier.h>
+#include <linux/xarray.h>
 #include <asm/signal.h>
 
 #include <linux/kvm.h>
@@ -552,7 +553,7 @@ struct kvm {
        struct mutex slots_arch_lock;
        struct mm_struct *mm; /* userspace tied to this vm */
        struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM];
-       struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
+       struct xarray vcpu_array;
 
        /* Used to wait for completion of MMU notifiers.  */
        spinlock_t mn_invalidate_lock;
@@ -701,7 +702,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
 
        /* Pairs with smp_wmb() in kvm_vm_ioctl_create_vcpu.  */
        smp_rmb();
-       return kvm->vcpus[i];
+       return xa_load(&kvm->vcpu_array, i);
 }
 
 #define kvm_for_each_vcpu(idx, vcpup, kvm) \
index 0a504c7988dcb56cd14e9a9d3449fa2c1edbad0e..594f90307b20e1edd6e69c242383d8d8b918887d 100644 (file)
@@ -458,7 +458,7 @@ void kvm_destroy_vcpus(struct kvm *kvm)
 
        kvm_for_each_vcpu(i, vcpu, kvm) {
                kvm_vcpu_destroy(vcpu);
-               kvm->vcpus[i] = NULL;
+               xa_erase(&kvm->vcpu_array, i);
        }
 
        atomic_set(&kvm->online_vcpus, 0);
@@ -1063,6 +1063,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
        mutex_init(&kvm->slots_arch_lock);
        spin_lock_init(&kvm->mn_invalidate_lock);
        rcuwait_init(&kvm->mn_memslots_update_rcuwait);
+       xa_init(&kvm->vcpu_array);
 
        INIT_LIST_HEAD(&kvm->devices);
 
@@ -3598,7 +3599,10 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
        }
 
        vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
-       BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
+       r = xa_insert(&kvm->vcpu_array, vcpu->vcpu_idx, vcpu, GFP_KERNEL_ACCOUNT);
+       BUG_ON(r == -EBUSY);
+       if (r)
+               goto unlock_vcpu_destroy;
 
        /* Fill the stats id string for the vcpu */
        snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d",
@@ -3608,15 +3612,14 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
        kvm_get_kvm(kvm);
        r = create_vcpu_fd(vcpu);
        if (r < 0) {
+               xa_erase(&kvm->vcpu_array, vcpu->vcpu_idx);
                kvm_put_kvm_no_destroy(kvm);
                goto unlock_vcpu_destroy;
        }
 
-       kvm->vcpus[vcpu->vcpu_idx] = vcpu;
-
        /*
-        * Pairs with smp_rmb() in kvm_get_vcpu.  Write kvm->vcpus
-        * before kvm->online_vcpu's incremented value.
+        * Pairs with smp_rmb() in kvm_get_vcpu.  Store the vcpu
+        * pointer before kvm->online_vcpu's incremented value.
         */
        smp_wmb();
        atomic_inc(&kvm->online_vcpus);