Skip to contentSkip to conversation

KVM: Turn the vcpu array into an xarray

PATCH Superseded Marc Zyngier 6 participants 24 messages Nov 5, 2021 – Nov 16, 2021

Subsystems (13)
x86 KVM PowerPC MIPS +9 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

cover KVM: Turn the vcpu array into an xarray

The kvm structure is pretty large. A large portion of it is the vcpu array, which is 4kB on x86_64 and arm64 as they deal with 512 vcpu VMs. Of course, hardly anyone runs VMs this big, so this is often a net waste of memory and cache locality.

A possible approach is to turn the fixed-size array into an xarray, which results in a net code deletion after a bit of cleanup.

This series is on top of the current linux/master as it touches the RISC-V implementation. Only tested on arm64.

Marc Zyngier (5):
KVM: Move wiping of the kvm->vcpus array to common code
KVM: mips: Use kvm_get_vcpu() instead of open-coded access
KVM: s390: Use kvm_get_vcpu() instead of open-coded access
KVM: x86: Use kvm_get_vcpu() instead of open-coded access
KVM: Convert the kvm->vcpus array to a xarray

 arch/arm64/kvm/arm.c           | 10 +---------
 arch/mips/kvm/loongson_ipi.c   |  4 ++--
 arch/mips/kvm/mips.c           | 23 ++---------------------
 arch/powerpc/kvm/powerpc.c     | 10 +---------
 arch/riscv/kvm/vm.c            | 10 +---------
 arch/s390/kvm/kvm-s390.c       | 26 ++++++--------------------
 arch/x86/kvm/vmx/posted_intr.c |  2 +-
 arch/x86/kvm/x86.c             |  9 +--------
 include/linux/kvm_host.h       |  7 ++++---
 virt/kvm/kvm_main.c            | 33 ++++++++++++++++++++++++++-------
 10 files changed, 45 insertions(+), 89 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/5 KVM: Move wiping of the kvm->vcpus array to common code

All architectures have similar loops iterating over the vcpus, freeing one vcpu at a time, and eventually wiping the reference off the vcpus array. They are also inconsistently taking the kvm->lock mutex when wiping the references from the array.

Make this code common, which will simplify further changes.

---
 arch/arm64/kvm/arm.c       | 10 +---------
 arch/mips/kvm/mips.c       | 21 +--------------------
 arch/powerpc/kvm/powerpc.c | 10 +---------
 arch/riscv/kvm/vm.c        | 10 +---------
 arch/s390/kvm/kvm-s390.c   | 18 +-----------------
 arch/x86/kvm/x86.c         |  9 +--------
 include/linux/kvm_host.h   |  2 +-
 virt/kvm/kvm_main.c        | 20 ++++++++++++++++++--
 8 files changed, 25 insertions(+), 75 deletions(-)
arch/arm64/kvm/arm.c +1 -9
index f5490afe1ebf..75bb7215da03 100644
@@ -175,19 +175,11 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  */
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	int i;
-
 	bitmap_free(kvm->arch.pmu_filter);
 
 	kvm_vgic_destroy(kvm);
 
-	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-		if (kvm->vcpus[i]) {
-			kvm_vcpu_destroy(kvm->vcpus[i]);
-			kvm->vcpus[i] = NULL;
-		}
-	}
-	atomic_set(&kvm->online_vcpus, 0);
+	kvm_destroy_vcpus(kvm);
 }
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
arch/mips/kvm/mips.c +1 -20
index 562aa878b266..ceacca74f808 100644
@@ -171,25 +171,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	return 0;
 }
 
-void kvm_mips_free_vcpus(struct kvm *kvm)
-{
-	unsigned int i;
-	struct kvm_vcpu *vcpu;
-
-	kvm_for_each_vcpu(i, vcpu, kvm) {
-		kvm_vcpu_destroy(vcpu);
-	}
-
-	mutex_lock(&kvm->lock);
-
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
-
-	atomic_set(&kvm->online_vcpus, 0);
-
-	mutex_unlock(&kvm->lock);
-}
-
 static void kvm_mips_free_gpa_pt(struct kvm *kvm)
 {
 	/* It should always be safe to remove after flushing the whole range */
@@ -199,7 +180,7 @@ static void kvm_mips_free_gpa_pt(struct kvm *kvm)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	kvm_mips_free_vcpus(kvm);
+	kvm_destroy_vcpus(kvm);
 	kvm_mips_free_gpa_pt(kvm);
 }
 
arch/powerpc/kvm/powerpc.c +1 -9
index 35e9cccdeef9..492e4a4121cb 100644
@@ -463,9 +463,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	unsigned int i;
-	struct kvm_vcpu *vcpu;
-
 #ifdef CONFIG_KVM_XICS
 	/*
 	 * We call kick_all_cpus_sync() to ensure that all
@@ -476,14 +473,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 		kick_all_cpus_sync();
 #endif
 
-	kvm_for_each_vcpu(i, vcpu, kvm)
-		kvm_vcpu_destroy(vcpu);
+	kvm_destroy_vcpus(kvm);
 
 	mutex_lock(&kvm->lock);
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
-
-	atomic_set(&kvm->online_vcpus, 0);
 
 	kvmppc_core_destroy_vm(kvm);
 
arch/riscv/kvm/vm.c +1 -9
index 26399df15b63..6af6cde295eb 100644
@@ -46,15 +46,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	int i;
-
-	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-		if (kvm->vcpus[i]) {
-			kvm_vcpu_destroy(kvm->vcpus[i]);
-			kvm->vcpus[i] = NULL;
-		}
-	}
-	atomic_set(&kvm->online_vcpus, 0);
+	kvm_destroy_vcpus(kvm);
 }
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
arch/s390/kvm/kvm-s390.c +1 -17
index c6257f625929..7af53b8788fa 100644
@@ -2819,27 +2819,11 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)(vcpu->arch.sie_block));
 }
 
-static void kvm_free_vcpus(struct kvm *kvm)
-{
-	unsigned int i;
-	struct kvm_vcpu *vcpu;
-
-	kvm_for_each_vcpu(i, vcpu, kvm)
-		kvm_vcpu_destroy(vcpu);
-
-	mutex_lock(&kvm->lock);
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
-
-	atomic_set(&kvm->online_vcpus, 0);
-	mutex_unlock(&kvm->lock);
-}
-
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
 	u16 rc, rrc;
 
-	kvm_free_vcpus(kvm);
+	kvm_destroy_vcpus(kvm);
 	sca_dispose(kvm);
 	kvm_s390_gisa_destroy(kvm);
 	/*
arch/x86/kvm/x86.c +1 -8
index c1c4e2b05a63..498a43126615 100644
@@ -11302,15 +11302,8 @@ static void kvm_free_vcpus(struct kvm *kvm)
 		kvm_clear_async_pf_completion_queue(vcpu);
 		kvm_unload_vcpu_mmu(vcpu);
 	}
-	kvm_for_each_vcpu(i, vcpu, kvm)
-		kvm_vcpu_destroy(vcpu);
-
-	mutex_lock(&kvm->lock);
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
 
-	atomic_set(&kvm->online_vcpus, 0);
-	mutex_unlock(&kvm->lock);
+	kvm_destroy_vcpus(kvm);
 }
 
 void kvm_arch_sync_events(struct kvm *kvm)
include/linux/kvm_host.h +1 -1
index 60a35d9fe259..36967291b8c6 100644
@@ -725,7 +725,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
 		if (WARN_ON_ONCE(!memslot->npages)) {			\
 		} else
 
-void kvm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_destroy_vcpus(struct kvm *kvm);
 
 void vcpu_load(struct kvm_vcpu *vcpu);
 void vcpu_put(struct kvm_vcpu *vcpu);
virt/kvm/kvm_main.c +18 -2
index 3f6d450355f0..d83553eeea21 100644
@@ -435,7 +435,7 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	vcpu->last_used_slot = 0;
 }
 
-void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
+static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
 	kvm_dirty_ring_free(&vcpu->dirty_ring);
 	kvm_arch_vcpu_destroy(vcpu);
@@ -450,7 +450,23 @@ void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->run);
 	kmem_cache_free(kvm_vcpu_cache, vcpu);
 }
-EXPORT_SYMBOL_GPL(kvm_vcpu_destroy);
+
+void kvm_destroy_vcpus(struct kvm *kvm)
+{
+	unsigned int i;
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_destroy(vcpu);
+
+	mutex_lock(&kvm->lock);
+	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
+		kvm->vcpus[i] = NULL;
+
+	atomic_set(&kvm->online_vcpus, 0);
+	mutex_unlock(&kvm->lock);
+}
+EXPORT_SYMBOL_GPL(kvm_destroy_vcpus);
 
 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
Signature · 1 line
2.30.2
author maintainer
#6 lore raw

PATCH 2/5 KVM: mips: Use kvm_get_vcpu() instead of open-coded access

In reply to Marc Zyngier

As we are about to change the way vcpus are allocated, mandate the use of kvm_get_vcpu() instead of open-coding the access.

---
 arch/mips/kvm/loongson_ipi.c | 4 ++--
 arch/mips/kvm/mips.c         | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/mips/kvm/loongson_ipi.c +2 -2
index 3681fc8fba38..5d53f32d837c 100644
@@ -120,7 +120,7 @@ static int loongson_vipi_write(struct loongson_kvm_ipi *ipi,
 		s->status |= data;
 		irq.cpu = id;
 		irq.irq = 6;
-		kvm_vcpu_ioctl_interrupt(kvm->vcpus[id], &irq);
+		kvm_vcpu_ioctl_interrupt(kvm_get_vcpu(kvm, id), &irq);
 		break;
 
 	case CORE0_CLEAR_OFF:
@@ -128,7 +128,7 @@ static int loongson_vipi_write(struct loongson_kvm_ipi *ipi,
 		if (!s->status) {
 			irq.cpu = id;
 			irq.irq = -6;
-			kvm_vcpu_ioctl_interrupt(kvm->vcpus[id], &irq);
+			kvm_vcpu_ioctl_interrupt(kvm_get_vcpu(kvm, id), &irq);
 		}
 		break;
 
arch/mips/kvm/mips.c +1 -1
index ceacca74f808..6228bf396d63 100644
@@ -479,7 +479,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
 	if (irq->cpu == -1)
 		dvcpu = vcpu;
 	else
-		dvcpu = vcpu->kvm->vcpus[irq->cpu];
+		dvcpu = kvm_get_vcpu(vcpu->kvm, irq->cpu);
 
 	if (intr == 2 || intr == 3 || intr == 4 || intr == 6) {
 		kvm_mips_callbacks->queue_io_int(dvcpu, irq);
Signature · 1 line
2.30.2
author maintainer
#8 lore raw

PATCH 3/5 KVM: s390: Use kvm_get_vcpu() instead of open-coded access

In reply to Marc Zyngier

As we are about to change the way vcpus are allocated, mandate the use of kvm_get_vcpu() instead of open-coding the access.

---
 arch/s390/kvm/kvm-s390.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kvm/kvm-s390.c +5 -3
index 7af53b8788fa..4a0f62b03964 100644
@@ -4572,7 +4572,7 @@ int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
 	}
 
 	for (i = 0; i < online_vcpus; i++) {
-		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
+		if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
 			started_vcpus++;
 	}
 
@@ -4634,9 +4634,11 @@ int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
 	__disable_ibs_on_vcpu(vcpu);
 
 	for (i = 0; i < online_vcpus; i++) {
-		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
+		struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
+
+		if (!is_vcpu_stopped(tmp)) {
 			started_vcpus++;
-			started_vcpu = vcpu->kvm->vcpus[i];
+			started_vcpu = tmp;
 		}
 	}
 
Signature · 1 line
2.30.2
author maintainer
#10 lore raw

PATCH 4/5 KVM: x86: Use kvm_get_vcpu() instead of open-coded access

In reply to Marc Zyngier

As we are about to change the way vcpus are allocated, mandate the use of kvm_get_vcpu() instead of open-coding the access.

---
 arch/x86/kvm/vmx/posted_intr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/posted_intr.c +1 -1
index 5f81ef092bd4..82a49720727d 100644
@@ -272,7 +272,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
 
 	if (!kvm_arch_has_assigned_device(kvm) ||
 	    !irq_remapping_cap(IRQ_POSTING_CAP) ||
-	    !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+	    !kvm_vcpu_apicv_active(kvm_get_vcpu(kvm, 0)))
 		return 0;
 
 	idx = srcu_read_lock(&kvm->irq_srcu);
Signature · 1 line
2.30.2
author maintainer
#15 lore raw

PATCH 5/5 KVM: Convert the kvm->vcpus array to a xarray

In reply to Marc Zyngier

At least on arm64 and x86, the vcpus array is pretty huge (512 entries), and is mostly empty in most cases (running 512 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.

---
 include/linux/kvm_host.h |  5 +++--
 virt/kvm/kvm_main.c      | 15 +++++++++------
 2 files changed, 12 insertions(+), 8 deletions(-)
include/linux/kvm_host.h +3 -2
index 36967291b8c6..3933d825e28b 100644
@@ -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;
@@ -693,7 +694,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) \
virt/kvm/kvm_main.c +9 -6
index d83553eeea21..4c18d7911fa5 100644
@@ -461,7 +461,7 @@ void kvm_destroy_vcpus(struct kvm *kvm)
 
 	mutex_lock(&kvm->lock);
 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
+		xa_erase(&kvm->vcpu_array, i);
 
 	atomic_set(&kvm->online_vcpus, 0);
 	mutex_unlock(&kvm->lock);
@@ -1066,6 +1066,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);
 
@@ -3661,7 +3662,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",
@@ -3671,15 +3675,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);
Signature · 1 line
2.30.2
#11 lore raw

Re: [PATCH 4/5] KVM: x86: Use kvm_get_vcpu() instead of open-coded access

In reply to Marc Zyngier
Quoted 13 lines · Marc Zyngier

As we are about to change the way vcpus are allocated, mandate the use of kvm_get_vcpu() instead of open-coding the access.

---
 arch/x86/kvm/vmx/posted_intr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/posted_intr.c +0 -0
index 5f81ef092bd4..82a49720727d 100644

arch/x86/kvm/vmx/posted_intr.c int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, · line 275

Quoted 5 lines
arch/x86/kvm/vmx/posted_intr.c +1 -1
@@ -272,7 +272,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
 
 	if (!kvm_arch_has_assigned_device(kvm) ||
 	    !irq_remapping_cap(IRQ_POSTING_CAP) ||
-	    !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+	    !kvm_vcpu_apicv_active(kvm_get_vcpu(kvm, 0)))

Huh. The existing code is decidedly odd. I think it might even be broken, as it's not obvious that vCPU0 _must_ be created when e.g. kvm_arch_irq_bypass_add_producer() is called.

An equivalent, safe check would be:

arch/x86/kvm/vmx/posted_intr.c +1 -1
index 5f81ef092bd4..a3100591a9ca 100644
@@ -272,7 +272,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
 
        if (!kvm_arch_has_assigned_device(kvm) ||
            !irq_remapping_cap(IRQ_POSTING_CAP) ||
-           !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+           !kvm_apicv_activated(kvm))
                return 0;
 
        idx = srcu_read_lock(&kvm->irq_srcu);

But I think even that is flawed, as APICv can be dynamically deactivated and re-activated while the VM is running, and I don't see a path that re-updates the IRTE when APICv is re-activated. So I think a more conservative check is needed, e.g.

arch/x86/kvm/vmx/posted_intr.c +1 -1
index 5f81ef092bd4..6cf5b2e86118 100644
@@ -272,7 +272,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
 
        if (!kvm_arch_has_assigned_device(kvm) ||
            !irq_remapping_cap(IRQ_POSTING_CAP) ||
-           !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+           !irqchip_in_kernel(kvm) || !enable_apicv)
                return 0;
 
        idx = srcu_read_lock(&kvm->irq_srcu);

Paolo, am I missing something?

#2 lore raw

Re: [PATCH 1/5] KVM: Move wiping of the kvm->vcpus array to common code

In reply to Marc Zyngier
Quoted 4 lines · Marc Zyngier

All architectures have similar loops iterating over the vcpus, freeing one vcpu at a time, and eventually wiping the reference off the vcpus array. They are also inconsistently taking the kvm->lock mutex when wiping the references from the array.

...

virt/kvm/kvm_main.c void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) · line 462

Quoted 9 lines
virt/kvm/kvm_main.c +9 -0
+void kvm_destroy_vcpus(struct kvm *kvm)
+{
+	unsigned int i;
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_destroy(vcpu);
+
+	mutex_lock(&kvm->lock);

But why is kvm->lock taken here? Unless I'm overlooking an arch, everyone calls this from kvm_arch_destroy_vm(), in which case this is the only remaining reference to @kvm. And if there's some magic path for which that's not true, I don't see how it can possibly be safe to call kvm_vcpu_destroy() without holding kvm->lock, or how this would guarantee that all vCPUs have actually been destroyed before nullifying the array.

Quoted 7 lines
virt/kvm/kvm_main.c +7 -0
+	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
+		kvm->vcpus[i] = NULL;
+
+	atomic_set(&kvm->online_vcpus, 0);
+	mutex_unlock(&kvm->lock);
+}
+EXPORT_SYMBOL_GPL(kvm_destroy_vcpus);
#16 lore raw

Re: [PATCH 5/5] KVM: Convert the kvm->vcpus array to a xarray

In reply to Marc Zyngier
Quoted 4 lines · Marc Zyngier

At least on arm64 and x86, the vcpus array is pretty huge (512 entries), and is mostly empty in most cases (running 512 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.

Heh, x86 is now up to 1024 entries.

include/linux/kvm_host.h static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) · line 698

Quoted 13 lines

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.

---

include/linux/kvm_host.h +1 -1
@@ -693,7 +694,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);
 }

It'd be nice for this series to convert kvm_for_each_vcpu() to use xa_for_each() as well. Maybe as a patch on top so that potential explosions from that are isolated from the initiali conversion?

Or maybe even use xa_for_each_range() to cap at online_vcpus? That's technically a functional change, but IMO it's easier to reason about iterating over a snapshot of vCPUs as opposed to being able to iterate over vCPUs as their being added. In practice I doubt it matters.

#define kvm_for_each_vcpu(idx, vcpup, kvm) \
	xa_for_each_range(&kvm->vcpu_array, idx, vcpup, 0, atomic_read(&kvm->online_vcpus))
author maintainer
#3 lore raw

Re: [PATCH 1/5] KVM: Move wiping of the kvm->vcpus array to common code

In reply to Sean Christopherson
Quoted 19 lines · Sean Christopherson
Quoted 4 lines · Marc Zyngier

All architectures have similar loops iterating over the vcpus, freeing one vcpu at a time, and eventually wiping the reference off the vcpus array. They are also inconsistently taking the kvm->lock mutex when wiping the references from the array.

...

virt/kvm/kvm_main.c void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) · line 462

Quoted 9 lines
virt/kvm/kvm_main.c +9 -0
+void kvm_destroy_vcpus(struct kvm *kvm)
+{
+	unsigned int i;
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_destroy(vcpu);
+
+	mutex_lock(&kvm->lock);
Quoted 7 lines

But why is kvm->lock taken here? Unless I'm overlooking an arch, everyone calls this from kvm_arch_destroy_vm(), in which case this is the only remaining reference to @kvm. And if there's some magic path for which that's not true, I don't see how it can possibly be safe to call kvm_vcpu_destroy() without holding kvm->lock, or how this would guarantee that all vCPUs have actually been destroyed before nullifying the array.

I asked myself the same question two years ago, and couldn't really understand the requirement. However, x86 does just that, so I preserved the behaviour.

If you too believe that this is just wrong, I'm happy to drop the locking altogether. If that breaks someone's flow, they'll shout soon enough.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer
#17 lore raw

Re: [PATCH 5/5] KVM: Convert the kvm->vcpus array to a xarray

In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson
Quoted 4 lines · Marc Zyngier

At least on arm64 and x86, the vcpus array is pretty huge (512 entries), and is mostly empty in most cases (running 512 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.

Heh, x86 is now up to 1024 entries.

Humph. I don't want to know whether people are actually using that in practice. The only time I create VMs with 512 vcpus is to check whether it still works...

Quoted 19 lines
Quoted 8 lines

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.

---

include/linux/kvm_host.h static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) · line 698

Quoted 6 lines
include/linux/kvm_host.h +1 -1
@@ -693,7 +694,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);
 }

It'd be nice for this series to convert kvm_for_each_vcpu() to use xa_for_each() as well. Maybe as a patch on top so that potential explosions from that are isolated from the initiali conversion?

Quoted 9 lines

Or maybe even use xa_for_each_range() to cap at online_vcpus? That's technically a functional change, but IMO it's easier to reason about iterating over a snapshot of vCPUs as opposed to being able to iterate over vCPUs as their being added. In practice I doubt it matters.

#define kvm_for_each_vcpu(idx, vcpup, kvm) \
	xa_for_each_range(&kvm->vcpu_array, idx, vcpup, 0, atomic_read(&kvm->online_vcpus))

I think that's already the behaviour of this iterator (we stop at the first empty slot capped to online_vcpus. The only change in behaviour is that vcpup currently holds a pointer to the last vcpu in no empty slot has been encountered. xa_for_each{,_range}() would set the pointer to NULL at all times.

I doubt anyone relies on that, but it is probably worth eyeballing some of the use cases...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Reviewed-by on v1 2/5
#7 lore raw

Re: [PATCH 2/5] KVM: mips: Use kvm_get_vcpu() instead of open-coded access

In reply to Marc Zyngier
Quoted 9 lines · Marc Zyngier

As we are about to change the way vcpus are allocated, mandate the use of kvm_get_vcpu() instead of open-coding the access.

---
 arch/mips/kvm/loongson_ipi.c | 4 ++--
 arch/mips/kvm/mips.c         | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
author maintainer
#18 lore raw

Re: [PATCH 5/5] KVM: Convert the kvm->vcpus array to a xarray

In reply to Marc Zyngier
Quoted 45 lines · Marc Zyngier
Quoted 8 lines · Sean Christopherson
Quoted 4 lines · Marc Zyngier

At least on arm64 and x86, the vcpus array is pretty huge (512 entries), and is mostly empty in most cases (running 512 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.

Heh, x86 is now up to 1024 entries.

Humph. I don't want to know whether people are actually using that in practice. The only time I create VMs with 512 vcpus is to check whether it still works...

Quoted 25 lines
Quoted 8 lines

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.

---

include/linux/kvm_host.h static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) · line 698

Quoted 6 lines
include/linux/kvm_host.h +1 -1
@@ -693,7 +694,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);
 }

It'd be nice for this series to convert kvm_for_each_vcpu() to use xa_for_each() as well. Maybe as a patch on top so that potential explosions from that are isolated from the initiali conversion?

Or maybe even use xa_for_each_range() to cap at online_vcpus? That's technically a functional change, but IMO it's easier to reason about iterating over a snapshot of vCPUs as opposed to being able to iterate over vCPUs as their being added. In practice I doubt it matters.

Quoted 4 lines
#define kvm_for_each_vcpu(idx, vcpup, kvm) \
	xa_for_each_range(&kvm->vcpu_array, idx, vcpup, 0, 

atomic_read(&kvm->online_vcpus))

Quoted 8 lines

I think that's already the behaviour of this iterator (we stop at the first empty slot capped to online_vcpus. The only change in behaviour is that vcpup currently holds a pointer to the last vcpu in no empty slot has been encountered. xa_for_each{,_range}() would set the pointer to NULL at all times.

I doubt anyone relies on that, but it is probably worth eyeballing some of the use cases...

This turned out to be an interesting exercise, as we always use an int for the index, and the xarray iterators insist on an unsigned long (and even on a pointer to it). On the other hand, I couldn't spot any case where we'd rely on the last value of the vcpu pointer.

I'll repost the series once we have a solution for patch #4, and we can then decide whether we want the iterator churn.

Signature · 1 line
Jazz is not dead. It just smells funny...
maintainer Reviewed-by on v1 1/5
#5 lore raw

Re: [PATCH 1/5] KVM: Move wiping of the kvm->vcpus array to common code

In reply to Marc Zyngier
Quoted 8 lines · On Fri, 5 Nov 2021 19:20:57 +0000 Marc Zyngier <[email protected]> wrote:

All architectures have similar loops iterating over the vcpus, freeing one vcpu at a time, and eventually wiping the reference off the vcpus array. They are also inconsistently taking the kvm->lock mutex when wiping the references from the array.

Make this code common, which will simplify further changes.

no objections

Quoted 231 lines
---
 arch/arm64/kvm/arm.c       | 10 +---------
 arch/mips/kvm/mips.c       | 21 +--------------------
 arch/powerpc/kvm/powerpc.c | 10 +---------
 arch/riscv/kvm/vm.c        | 10 +---------
 arch/s390/kvm/kvm-s390.c   | 18 +-----------------
 arch/x86/kvm/x86.c         |  9 +--------
 include/linux/kvm_host.h   |  2 +-
 virt/kvm/kvm_main.c        | 20 ++++++++++++++++++--
 8 files changed, 25 insertions(+), 75 deletions(-)
arch/arm64/kvm/arm.c +1 -9
index f5490afe1ebf..75bb7215da03 100644
@@ -175,19 +175,11 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  */
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	int i;
-
 	bitmap_free(kvm->arch.pmu_filter);
 
 	kvm_vgic_destroy(kvm);
 
-	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-		if (kvm->vcpus[i]) {
-			kvm_vcpu_destroy(kvm->vcpus[i]);
-			kvm->vcpus[i] = NULL;
-		}
-	}
-	atomic_set(&kvm->online_vcpus, 0);
+	kvm_destroy_vcpus(kvm);
 }
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
arch/mips/kvm/mips.c +1 -20
index 562aa878b266..ceacca74f808 100644
@@ -171,25 +171,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	return 0;
 }
 
-void kvm_mips_free_vcpus(struct kvm *kvm)
-{
-	unsigned int i;
-	struct kvm_vcpu *vcpu;
-
-	kvm_for_each_vcpu(i, vcpu, kvm) {
-		kvm_vcpu_destroy(vcpu);
-	}
-
-	mutex_lock(&kvm->lock);
-
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
-
-	atomic_set(&kvm->online_vcpus, 0);
-
-	mutex_unlock(&kvm->lock);
-}
-
 static void kvm_mips_free_gpa_pt(struct kvm *kvm)
 {
 	/* It should always be safe to remove after flushing the whole range */
@@ -199,7 +180,7 @@ static void kvm_mips_free_gpa_pt(struct kvm *kvm)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	kvm_mips_free_vcpus(kvm);
+	kvm_destroy_vcpus(kvm);
 	kvm_mips_free_gpa_pt(kvm);
 }
 
arch/powerpc/kvm/powerpc.c +1 -9
index 35e9cccdeef9..492e4a4121cb 100644
@@ -463,9 +463,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	unsigned int i;
-	struct kvm_vcpu *vcpu;
-
 #ifdef CONFIG_KVM_XICS
 	/*
 	 * We call kick_all_cpus_sync() to ensure that all
@@ -476,14 +473,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 		kick_all_cpus_sync();
 #endif
 
-	kvm_for_each_vcpu(i, vcpu, kvm)
-		kvm_vcpu_destroy(vcpu);
+	kvm_destroy_vcpus(kvm);
 
 	mutex_lock(&kvm->lock);
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
-
-	atomic_set(&kvm->online_vcpus, 0);
 
 	kvmppc_core_destroy_vm(kvm);
 
arch/riscv/kvm/vm.c +1 -9
index 26399df15b63..6af6cde295eb 100644
@@ -46,15 +46,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
-	int i;
-
-	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-		if (kvm->vcpus[i]) {
-			kvm_vcpu_destroy(kvm->vcpus[i]);
-			kvm->vcpus[i] = NULL;
-		}
-	}
-	atomic_set(&kvm->online_vcpus, 0);
+	kvm_destroy_vcpus(kvm);
 }
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
arch/s390/kvm/kvm-s390.c +1 -17
index c6257f625929..7af53b8788fa 100644
@@ -2819,27 +2819,11 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)(vcpu->arch.sie_block));
 }
 
-static void kvm_free_vcpus(struct kvm *kvm)
-{
-	unsigned int i;
-	struct kvm_vcpu *vcpu;
-
-	kvm_for_each_vcpu(i, vcpu, kvm)
-		kvm_vcpu_destroy(vcpu);
-
-	mutex_lock(&kvm->lock);
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
-
-	atomic_set(&kvm->online_vcpus, 0);
-	mutex_unlock(&kvm->lock);
-}
-
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
 	u16 rc, rrc;
 
-	kvm_free_vcpus(kvm);
+	kvm_destroy_vcpus(kvm);
 	sca_dispose(kvm);
 	kvm_s390_gisa_destroy(kvm);
 	/*
arch/x86/kvm/x86.c +1 -8
index c1c4e2b05a63..498a43126615 100644
@@ -11302,15 +11302,8 @@ static void kvm_free_vcpus(struct kvm *kvm)
 		kvm_clear_async_pf_completion_queue(vcpu);
 		kvm_unload_vcpu_mmu(vcpu);
 	}
-	kvm_for_each_vcpu(i, vcpu, kvm)
-		kvm_vcpu_destroy(vcpu);
-
-	mutex_lock(&kvm->lock);
-	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
-		kvm->vcpus[i] = NULL;
 
-	atomic_set(&kvm->online_vcpus, 0);
-	mutex_unlock(&kvm->lock);
+	kvm_destroy_vcpus(kvm);
 }
 
 void kvm_arch_sync_events(struct kvm *kvm)
include/linux/kvm_host.h +1 -1
index 60a35d9fe259..36967291b8c6 100644
@@ -725,7 +725,7 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
 		if (WARN_ON_ONCE(!memslot->npages)) {			\
 		} else
 
-void kvm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_destroy_vcpus(struct kvm *kvm);
 
 void vcpu_load(struct kvm_vcpu *vcpu);
 void vcpu_put(struct kvm_vcpu *vcpu);
virt/kvm/kvm_main.c +18 -2
index 3f6d450355f0..d83553eeea21 100644
@@ -435,7 +435,7 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	vcpu->last_used_slot = 0;
 }
 
-void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
+static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
 	kvm_dirty_ring_free(&vcpu->dirty_ring);
 	kvm_arch_vcpu_destroy(vcpu);
@@ -450,7 +450,23 @@ void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->run);
 	kmem_cache_free(kvm_vcpu_cache, vcpu);
 }
-EXPORT_SYMBOL_GPL(kvm_vcpu_destroy);
+
+void kvm_destroy_vcpus(struct kvm *kvm)
+{
+	unsigned int i;
+	struct kvm_vcpu *vcpu;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_destroy(vcpu);
+
+	mutex_lock(&kvm->lock);
+	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
+		kvm->vcpus[i] = NULL;
+
+	atomic_set(&kvm->online_vcpus, 0);
+	mutex_unlock(&kvm->lock);
+}
+EXPORT_SYMBOL_GPL(kvm_destroy_vcpus);
 
 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
maintainer Reviewed-by on v1 3/5
#9 lore raw

Re: [PATCH 3/5] KVM: s390: Use kvm_get_vcpu() instead of open-coded access

In reply to Marc Zyngier
Quoted 4 lines · On Fri, 5 Nov 2021 19:20:59 +0000 Marc Zyngier <[email protected]> wrote:

As we are about to change the way vcpus are allocated, mandate the use of kvm_get_vcpu() instead of open-coding the access.

makes sense

Quoted 31 lines
---
 arch/s390/kvm/kvm-s390.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kvm/kvm-s390.c +5 -3
index 7af53b8788fa..4a0f62b03964 100644
@@ -4572,7 +4572,7 @@ int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
 	}
 
 	for (i = 0; i < online_vcpus; i++) {
-		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
+		if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
 			started_vcpus++;
 	}
 
@@ -4634,9 +4634,11 @@ int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
 	__disable_ibs_on_vcpu(vcpu);
 
 	for (i = 0; i < online_vcpus; i++) {
-		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
+		struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
+
+		if (!is_vcpu_stopped(tmp)) {
 			started_vcpus++;
-			started_vcpu = vcpu->kvm->vcpus[i];
+			started_vcpu = tmp;
 		}
 	}
 
maintainer
#4 lore raw

Re: [PATCH 1/5] KVM: Move wiping of the kvm->vcpus array to common code

In reply to Marc Zyngier
Quoted 4 lines · Marc Zyngier

If you too believe that this is just wrong, I'm happy to drop the locking altogether. If that breaks someone's flow, they'll shout soon enough.

Yes, it's not necessary. It was added in 2009 (commit 988a2cae6a3c, "KVM: Use macro to iterate over vcpus.") and it was unnecessary back then too.

Paolo

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#12 lore raw

Re: [PATCH 4/5] KVM: x86: Use kvm_get_vcpu() instead of open-coded access

In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

But I think even that is flawed, as APICv can be dynamically deactivated and re-activated while the VM is running, and I don't see a path that re-updates the IRTE when APICv is re-activated. So I think a more conservative check is needed, e.g.

arch/x86/kvm/vmx/posted_intr.c +0 -0
index 5f81ef092bd4..6cf5b2e86118 100644

arch/x86/kvm/vmx/posted_intr.c int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, · line 278

Quoted 8 lines
arch/x86/kvm/vmx/posted_intr.c +1 -1
@@ -272,7 +272,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
 
         if (!kvm_arch_has_assigned_device(kvm) ||
             !irq_remapping_cap(IRQ_POSTING_CAP) ||
-           !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+           !irqchip_in_kernel(kvm) || !enable_apicv)
                 return 0;
 
         idx = srcu_read_lock(&kvm->irq_srcu);

What happens then if pi_pre_block is called and the IRTE denotes a posted interrupt?

I might be wrong, but it seems to me that you have to change all of the occurrences this way. As soon as enable_apicv is set, you need to go through the POSTED_INTR_WAKEUP_VECTOR just in case.

Paolo

Tested-by on the v1 series
#19 lore raw
In reply to Marc Zyngier
Quoted 18 lines · Marc Zyngier

The kvm structure is pretty large. A large portion of it is the vcpu array, which is 4kB on x86_64 and arm64 as they deal with 512 vcpu VMs. Of course, hardly anyone runs VMs this big, so this is often a net waste of memory and cache locality.

A possible approach is to turn the fixed-size array into an xarray, which results in a net code deletion after a bit of cleanup.

This series is on top of the current linux/master as it touches the RISC-V implementation. Only tested on arm64.

Marc Zyngier (5):
KVM: Move wiping of the kvm->vcpus array to common code
KVM: mips: Use kvm_get_vcpu() instead of open-coded access
KVM: s390: Use kvm_get_vcpu() instead of open-coded access
KVM: x86: Use kvm_get_vcpu() instead of open-coded access
KVM: Convert the kvm->vcpus array to a xarray

Quoted 12 lines
  arch/arm64/kvm/arm.c           | 10 +---------
  arch/mips/kvm/loongson_ipi.c   |  4 ++--
  arch/mips/kvm/mips.c           | 23 ++---------------------
  arch/powerpc/kvm/powerpc.c     | 10 +---------
  arch/riscv/kvm/vm.c            | 10 +---------
  arch/s390/kvm/kvm-s390.c       | 26 ++++++--------------------
  arch/x86/kvm/vmx/posted_intr.c |  2 +-
  arch/x86/kvm/x86.c             |  9 +--------
  include/linux/kvm_host.h       |  7 ++++---
  virt/kvm/kvm_main.c            | 33 ++++++++++++++++++++++++++-------
  10 files changed, 45 insertions(+), 89 deletions(-)

For x86 you can add my:

Juergen

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#20 lore raw
Quoted 32 lines · Juergen Gross
Quoted 28 lines · Marc Zyngier

The kvm structure is pretty large. A large portion of it is the vcpu array, which is 4kB on x86_64 and arm64 as they deal with 512 vcpu VMs. Of course, hardly anyone runs VMs this big, so this is often a net waste of memory and cache locality.

A possible approach is to turn the fixed-size array into an xarray, which results in a net code deletion after a bit of cleanup.

This series is on top of the current linux/master as it touches the RISC-V implementation. Only tested on arm64.

Marc Zyngier (5):
   KVM: Move wiping of the kvm->vcpus array to common code
   KVM: mips: Use kvm_get_vcpu() instead of open-coded access
   KVM: s390: Use kvm_get_vcpu() instead of open-coded access
   KVM: x86: Use kvm_get_vcpu() instead of open-coded access
   KVM: Convert the kvm->vcpus array to a xarray

  arch/arm64/kvm/arm.c           | 10 +--------- arch/mips/kvm/loongson_ipi.c   |  4 ++-- arch/mips/kvm/mips.c           | 23 ++--------------------- arch/powerpc/kvm/powerpc.c     | 10 +--------- arch/riscv/kvm/vm.c            | 10 +--------- arch/s390/kvm/kvm-s390.c       | 26 ++++++-------------------- arch/x86/kvm/vmx/posted_intr.c |  2 +-
  arch/x86/kvm/x86.c             |  9 +-------- include/linux/kvm_host.h       |  7 ++++--- virt/kvm/kvm_main.c            | 33 ++++++++++++++++++++++++++-------

Quoted 2 lines
  10 files changed, 45 insertions(+), 89 deletions(-)
Quoted 3 lines

For x86 you can add my:

Heh, unfortunately x86 is the only one that needs a change in patch 4. I'll Cc you on my version.

Paolo

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#21 lore raw
Quoted 33 lines · Paolo Bonzini
Quoted 32 lines · Juergen Gross
Quoted 30 lines · Marc Zyngier

The kvm structure is pretty large. A large portion of it is the vcpu array, which is 4kB on x86_64 and arm64 as they deal with 512 vcpu VMs. Of course, hardly anyone runs VMs this big, so this is often a net waste of memory and cache locality.

A possible approach is to turn the fixed-size array into an xarray, which results in a net code deletion after a bit of cleanup.

This series is on top of the current linux/master as it touches the RISC-V implementation. Only tested on arm64.

Marc Zyngier (5):
   KVM: Move wiping of the kvm->vcpus array to common code
   KVM: mips: Use kvm_get_vcpu() instead of open-coded access
   KVM: s390: Use kvm_get_vcpu() instead of open-coded access
   KVM: x86: Use kvm_get_vcpu() instead of open-coded access
   KVM: Convert the kvm->vcpus array to a xarray

  arch/arm64/kvm/arm.c           | 10 +--------- arch/mips/kvm/loongson_ipi.c   |  4 ++-- arch/mips/kvm/mips.c           | 23 ++--------------------- arch/powerpc/kvm/powerpc.c     | 10 +--------- arch/riscv/kvm/vm.c            | 10 +--------- arch/s390/kvm/kvm-s390.c       | 26 ++++++-------------------- arch/x86/kvm/vmx/posted_intr.c |  2 +-
  arch/x86/kvm/x86.c             |  9 +-------- include/linux/kvm_host.h       |  7 ++++--- virt/kvm/kvm_main.c            | 33 ++++++++++++++++++++++++++-------

  10 files changed, 45 insertions(+), 89 deletions(-)
Quoted 5 lines
Quoted 3 lines

For x86 you can add my:

Heh, unfortunately x86 is the only one that needs a change in patch 4. I'll Cc you on my version.

I guess the changes in kvm_main.c are more important for my series. :-)

I've replaced patch 4 with your variant and everything is still working. Not sure how relevant that is, though.

Juergen

maintainer
#22 lore raw
In reply to Marc Zyngier
Quoted 10 lines · Marc Zyngier

The kvm structure is pretty large. A large portion of it is the vcpu array, which is 4kB on x86_64 and arm64 as they deal with 512 vcpu VMs. Of course, hardly anyone runs VMs this big, so this is often a net waste of memory and cache locality.

A possible approach is to turn the fixed-size array into an xarray, which results in a net code deletion after a bit of cleanup.

This series is on top of the current linux/master as it touches the RISC-V implementation. Only tested on arm64.

Queued, only locally until I get a review for my replacement of patch 4 (see https://lore.kernel.org/kvm/[email protected]/T/).

Paolo

Quoted 7 lines

Marc Zyngier (5):
KVM: Move wiping of the kvm->vcpus array to common code
KVM: mips: Use kvm_get_vcpu() instead of open-coded access
KVM: s390: Use kvm_get_vcpu() instead of open-coded access
KVM: x86: Use kvm_get_vcpu() instead of open-coded access
KVM: Convert the kvm->vcpus array to a xarray

Quoted 12 lines
  arch/arm64/kvm/arm.c           | 10 +---------
  arch/mips/kvm/loongson_ipi.c   |  4 ++--
  arch/mips/kvm/mips.c           | 23 ++---------------------
  arch/powerpc/kvm/powerpc.c     | 10 +---------
  arch/riscv/kvm/vm.c            | 10 +---------
  arch/s390/kvm/kvm-s390.c       | 26 ++++++--------------------
  arch/x86/kvm/vmx/posted_intr.c |  2 +-
  arch/x86/kvm/x86.c             |  9 +--------
  include/linux/kvm_host.h       |  7 ++++---
  virt/kvm/kvm_main.c            | 33 ++++++++++++++++++++++++++-------
  10 files changed, 45 insertions(+), 89 deletions(-)

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

author maintainer
#23 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Marc Zyngier

The kvm structure is pretty large. A large portion of it is the vcpu array, which is 4kB on x86_64 and arm64 as they deal with 512 vcpu VMs. Of course, hardly anyone runs VMs this big, so this is often a net waste of memory and cache locality.

A possible approach is to turn the fixed-size array into an xarray, which results in a net code deletion after a bit of cleanup.

Quoted 6 lines
Quoted 2 lines

This series is on top of the current linux/master as it touches the RISC-V implementation. Only tested on arm64.

Queued, only locally until I get a review for my replacement of patch 4 (see
https://lore.kernel.org/kvm/[email protected]/T/).

In which case, let me send a v2 with the changes that we discussed with Sean. It will still have my version of patch 4, but that's nothing you can't fix.

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#13 lore raw

Re: [PATCH 4/5] KVM: x86: Use kvm_get_vcpu() instead of open-coded access

In reply to Paolo Bonzini
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson

But I think even that is flawed, as APICv can be dynamically deactivated and re-activated while the VM is running, and I don't see a path that re-updates the IRTE when APICv is re-activated. So I think a more conservative check is needed, e.g.

arch/x86/kvm/vmx/posted_intr.c +1 -1
index 5f81ef092bd4..6cf5b2e86118 100644
@@ -272,7 +272,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
 
         if (!kvm_arch_has_assigned_device(kvm) ||
             !irq_remapping_cap(IRQ_POSTING_CAP) ||
-           !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+           !irqchip_in_kernel(kvm) || !enable_apicv)
                 return 0;
 
Quoted 8 lines

arch/x86/kvm/vmx/posted_intr.c int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, · line 278

Quoted 1 line
arch/x86/kvm/vmx/posted_intr.c +0 -0
         idx = srcu_read_lock(&kvm->irq_srcu);

What happens then if pi_pre_block is called and the IRTE denotes a posted interrupt?

I might be wrong, but it seems to me that you have to change all of the occurrences this way. As soon as enable_apicv is set, you need to go through the POSTED_INTR_WAKEUP_VECTOR just in case.

Sorry, I didn't grok that at all. All occurences of what?
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#14 lore raw

Re: [PATCH 4/5] KVM: x86: Use kvm_get_vcpu() instead of open-coded access

Quoted 6 lines · Sean Christopherson
Quoted 6 lines
Quoted 6 lines
          if (!kvm_arch_has_assigned_device(kvm) ||
              !irq_remapping_cap(IRQ_POSTING_CAP) ||
arch/x86/kvm/vmx/posted_intr.c +1 -1
-           !kvm_vcpu_apicv_active(kvm->vcpus[0]))
+           !irqchip_in_kernel(kvm) || !enable_apicv)
                  return 0;
Quoted 8 lines
Quoted 7 lines
Quoted 1 line
          idx = srcu_read_lock(&kvm->irq_srcu);

What happens then if pi_pre_block is called and the IRTE denotes a posted interrupt?

I might be wrong, but it seems to me that you have to change all of the occurrences this way. As soon as enable_apicv is set, you need to go through the POSTED_INTR_WAKEUP_VECTOR just in case.

Sorry, I didn't grok that at all. All occurences of what?

Of the !assigned-device || !VTd-PI || !kvm_vcpu_apicv_active(vcpu) checks. This way, CPUs are woken up correctly even if you have !kvm_vcpu_apicv_active(vcpu) but the IRTE is a posted-interrupt one.

Paolo

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm