Skip to contentSkip to conversation

KVM: simplify hardware initialization

PATCH In review [email protected] 2 participants 6 messages Nov 2, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover KVM: simplify hardware initialization

From: Isaku Yamahata <[email protected]>

This patch series include random simplifications of KVM hardware enable/disable. Although the past attempt [1] was turned out to be a bad idea, it has still useful patches. This is small subset of [1].

[1] https://lore.kernel.org/kvm/[email protected]/

Isaku Yamahata (3):
KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit()
KVM: Make cpus_hardware_enabled cpumask_t instead of cpumask_var_t
KVM: kvm_main.c: Remove a global variable, hardware_enable_failed

Marc Zyngier (1):
KVM: arm64: Simplify the CPUHP logic

 arch/arm64/kvm/arch_timer.c     | 27 +++++++------------
 arch/arm64/kvm/arm.c            | 13 +++++++++
 arch/arm64/kvm/vgic/vgic-init.c | 19 ++-----------
 include/kvm/arm_arch_timer.h    |  4 +++
 include/kvm/arm_vgic.h          |  4 +++
 include/linux/cpuhotplug.h      |  3 ---
 virt/kvm/kvm_main.c             | 48 ++++++++++++++++-----------------
 7 files changed, 56 insertions(+), 62 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit()

From: Isaku Yamahata <[email protected]>

hardware_enable/disable_nolock() check if the hardware is already enabled/disabled and work as nop when they are called multiple times. Also kvm_usage_count tracks the number of the existing VMs.

When VM is created/destroyed by kvm_create/destroy_vm(), hardware_enable/disable_all() and module_get/put() are called. It means when kvm module is removed, it's guaranteed that there is no vm (kvm_usage_count = 0) and that hardware_disable_nolock() was called on each cpus.

Although the module exit function, kvm_exit(), calls on_each_cpu(hardware_disable_nolock), it's essentially nop. Eliminate nop call in kvm_exit() and add WARN_ON(kvm_usage_count) to prove that there is no remaining VMs.

---
 virt/kvm/kvm_main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +2 -1
index 1376a47fedee..a8c4e62b29ca 100644
@@ -5945,6 +5945,8 @@ void kvm_exit(void)
 {
 	int cpu;
 
+	WARN_ON(kvm_usage_count);
+
 	debugfs_remove_recursive(kvm_debugfs_dir);
 	misc_deregister(&kvm_dev);
 	for_each_possible_cpu(cpu)
@@ -5954,7 +5956,6 @@ void kvm_exit(void)
 	unregister_syscore_ops(&kvm_syscore_ops);
 	unregister_reboot_notifier(&kvm_reboot_notifier);
 	cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
-	on_each_cpu(hardware_disable_nolock, NULL, 1);
 	kvm_arch_hardware_unsetup();
 	kvm_arch_exit();
 	kvm_irqfd_exit();
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/4 KVM: Make cpus_hardware_enabled cpumask_t instead of cpumask_var_t

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

On kvm module initialization, it dynamically allocates cpumask_var_t. Remove dynamic allocation by making it cpumask_t as static allocation. It simplifies module init and exit.

---
 virt/kvm/kvm_main.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
virt/kvm/kvm_main.c +6 -14
index a8c4e62b29ca..1a21b21ba326 100644
@@ -103,7 +103,7 @@ DEFINE_MUTEX(kvm_lock);
 static DEFINE_RAW_SPINLOCK(kvm_count_lock);
 LIST_HEAD(vm_list);
 
-static cpumask_var_t cpus_hardware_enabled;
+static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE;
 static int kvm_usage_count;
 static atomic_t hardware_enable_failed;
 
@@ -5012,15 +5012,15 @@ static void hardware_enable_nolock(void *junk)
 	int cpu = raw_smp_processor_id();
 	int r;
 
-	if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
+	if (cpumask_test_cpu(cpu, &cpus_hardware_enabled))
 		return;
 
-	cpumask_set_cpu(cpu, cpus_hardware_enabled);
+	cpumask_set_cpu(cpu, &cpus_hardware_enabled);
 
 	r = kvm_arch_hardware_enable();
 
 	if (r) {
-		cpumask_clear_cpu(cpu, cpus_hardware_enabled);
+		cpumask_clear_cpu(cpu, &cpus_hardware_enabled);
 		atomic_inc(&hardware_enable_failed);
 		pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
 	}
@@ -5039,9 +5039,9 @@ static void hardware_disable_nolock(void *junk)
 {
 	int cpu = raw_smp_processor_id();
 
-	if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
+	if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled))
 		return;
-	cpumask_clear_cpu(cpu, cpus_hardware_enabled);
+	cpumask_clear_cpu(cpu, &cpus_hardware_enabled);
 	kvm_arch_hardware_disable();
 }
 
@@ -5849,11 +5849,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 	if (r)
 		goto out_irqfd;
 
-	if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
-		r = -ENOMEM;
-		goto out_free_0;
-	}
-
 	r = kvm_arch_hardware_setup(opaque);
 	if (r < 0)
 		goto out_free_1;
@@ -5931,8 +5926,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 out_free_2:
 	kvm_arch_hardware_unsetup();
 out_free_1:
-	free_cpumask_var(cpus_hardware_enabled);
-out_free_0:
 	kvm_irqfd_exit();
 out_irqfd:
 	kvm_arch_exit();
@@ -5959,7 +5952,6 @@ void kvm_exit(void)
 	kvm_arch_hardware_unsetup();
 	kvm_arch_exit();
 	kvm_irqfd_exit();
-	free_cpumask_var(cpus_hardware_enabled);
 	kvm_vfio_ops_exit();
 }
 EXPORT_SYMBOL_GPL(kvm_exit);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/4 KVM: kvm_main.c: Remove a global variable, hardware_enable_failed

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

A global variable hardware_enable_failed in kvm_main.c is used only by hardware_enable_all() and hardware_enable_nolock(). It doesn't have to be a global variable. Make it function local.

---
 virt/kvm/kvm_main.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
virt/kvm/kvm_main.c +17 -12
index 1a21b21ba326..ac24fef2c818 100644
@@ -105,7 +105,6 @@ LIST_HEAD(vm_list);
 
 static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE;
 static int kvm_usage_count;
-static atomic_t hardware_enable_failed;
 
 static struct kmem_cache *kvm_vcpu_cache;
 
@@ -5007,30 +5006,36 @@ static struct miscdevice kvm_dev = {
 	&kvm_chardev_ops,
 };
 
-static void hardware_enable_nolock(void *junk)
+static int __hardware_enable_nolock(void)
 {
 	int cpu = raw_smp_processor_id();
 	int r;
 
-	if (cpumask_test_cpu(cpu, &cpus_hardware_enabled))
-		return;
-
-	cpumask_set_cpu(cpu, &cpus_hardware_enabled);
+	if (cpumask_test_and_set_cpu(cpu, &cpus_hardware_enabled))
+		return 0;
 
 	r = kvm_arch_hardware_enable();
 
 	if (r) {
 		cpumask_clear_cpu(cpu, &cpus_hardware_enabled);
-		atomic_inc(&hardware_enable_failed);
 		pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
 	}
+	return r;
+}
+
+static void hardware_enable_nolock(void *arg)
+{
+	atomic_t *failed = arg;
+
+	if (__hardware_enable_nolock())
+		atomic_inc(failed);
 }
 
 static int kvm_starting_cpu(unsigned int cpu)
 {
 	raw_spin_lock(&kvm_count_lock);
 	if (kvm_usage_count)
-		hardware_enable_nolock(NULL);
+		(void)__hardware_enable_nolock();
 	raw_spin_unlock(&kvm_count_lock);
 	return 0;
 }
@@ -5072,16 +5077,16 @@ static void hardware_disable_all(void)
 
 static int hardware_enable_all(void)
 {
+	atomic_t failed = ATOMIC_INIT(0);
 	int r = 0;
 
 	raw_spin_lock(&kvm_count_lock);
 
 	kvm_usage_count++;
 	if (kvm_usage_count == 1) {
-		atomic_set(&hardware_enable_failed, 0);
-		on_each_cpu(hardware_enable_nolock, NULL, 1);
+		on_each_cpu(hardware_enable_nolock, &failed, 1);
 
-		if (atomic_read(&hardware_enable_failed)) {
+		if (atomic_read(&failed)) {
 			hardware_disable_all_nolock();
 			r = -EBUSY;
 		}
@@ -5702,7 +5707,7 @@ static void kvm_resume(void)
 {
 	if (kvm_usage_count) {
 		lockdep_assert_not_held(&kvm_count_lock);
-		hardware_enable_nolock(NULL);
+		(void)__hardware_enable_nolock();
 	}
 }
 
Signature · 1 line
2.25.1
#4 lore raw

PATCH 4/4 KVM: arm64: Simplify the CPUHP logic

In reply to [email protected]

From: Marc Zyngier <[email protected]>

For a number of historical reasons, the KVM/arm64 hotplug setup is pretty complicated, and we have two extra CPUHP notifiers for vGIC and timers.

It looks pretty pointless, and gets in the way of further changes. So let's just expose some helpers that can be called from the core CPUHP callback, and get rid of everything else.

This gives us the opportunity to drop a useless notifier entry, as well as tidy-up the timer enable/disable, which was a bit odd.

---
 arch/arm64/kvm/arch_timer.c     | 27 ++++++++++-----------------
 arch/arm64/kvm/arm.c            | 13 +++++++++++++
 arch/arm64/kvm/vgic/vgic-init.c | 19 ++-----------------
 include/kvm/arm_arch_timer.h    |  4 ++++
 include/kvm/arm_vgic.h          |  4 ++++
 include/linux/cpuhotplug.h      |  3 ---
 6 files changed, 33 insertions(+), 37 deletions(-)
arch/arm64/kvm/arch_timer.c +10 -17
index bb24a76b4224..33fca1a691a5 100644
@@ -811,10 +811,18 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
 	ptimer->host_timer_irq_flags = host_ptimer_irq_flags;
 }
 
-static void kvm_timer_init_interrupt(void *info)
+void kvm_timer_cpu_up(void)
 {
 	enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
-	enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags);
+	if (host_ptimer_irq)
+		enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags);
+}
+
+void kvm_timer_cpu_down(void)
+{
+	disable_percpu_irq(host_vtimer_irq);
+	if (host_ptimer_irq)
+		disable_percpu_irq(host_ptimer_irq);
 }
 
 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
@@ -976,18 +984,6 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
 	preempt_enable();
 }
 
-static int kvm_timer_starting_cpu(unsigned int cpu)
-{
-	kvm_timer_init_interrupt(NULL);
-	return 0;
-}
-
-static int kvm_timer_dying_cpu(unsigned int cpu)
-{
-	disable_percpu_irq(host_vtimer_irq);
-	return 0;
-}
-
 static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
 {
 	if (vcpu)
@@ -1185,9 +1181,6 @@ int kvm_timer_hyp_init(bool has_gic)
 		goto out_free_irq;
 	}
 
-	cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
-			  "kvm/arm/timer:starting", kvm_timer_starting_cpu,
-			  kvm_timer_dying_cpu);
 	return 0;
 out_free_irq:
 	free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
arch/arm64/kvm/arm.c +13 -0
index 94d33e296e10..a15d9933edbd 100644
@@ -1675,7 +1675,15 @@ static void _kvm_arch_hardware_enable(void *discard)
 
 int kvm_arch_hardware_enable(void)
 {
+	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
+
 	_kvm_arch_hardware_enable(NULL);
+
+	if (!was_enabled) {
+		kvm_vgic_cpu_up();
+		kvm_timer_cpu_up();
+	}
+
 	return 0;
 }
 
@@ -1689,6 +1697,11 @@ static void _kvm_arch_hardware_disable(void *discard)
 
 void kvm_arch_hardware_disable(void)
 {
+	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
+		kvm_timer_cpu_down();
+		kvm_vgic_cpu_down();
+	}
+
 	if (!is_protected_kvm_enabled())
 		_kvm_arch_hardware_disable(NULL);
 }
arch/arm64/kvm/vgic/vgic-init.c +2 -17
index f6d4f4052555..6c7f6ae21ec0 100644
@@ -465,17 +465,15 @@ int kvm_vgic_map_resources(struct kvm *kvm)
 
 /* GENERIC PROBE */
 
-static int vgic_init_cpu_starting(unsigned int cpu)
+void kvm_vgic_cpu_up(void)
 {
 	enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
-	return 0;
 }
 
 
-static int vgic_init_cpu_dying(unsigned int cpu)
+void kvm_vgic_cpu_down(void)
 {
 	disable_percpu_irq(kvm_vgic_global_state.maint_irq);
-	return 0;
 }
 
 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
@@ -584,19 +582,6 @@ int kvm_vgic_hyp_init(void)
 		return ret;
 	}
 
-	ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
-				"kvm/arm/vgic:starting",
-				vgic_init_cpu_starting, vgic_init_cpu_dying);
-	if (ret) {
-		kvm_err("Cannot register vgic CPU notifier\n");
-		goto out_free_irq;
-	}
-
 	kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
 	return 0;
-
-out_free_irq:
-	free_percpu_irq(kvm_vgic_global_state.maint_irq,
-			kvm_get_running_vcpus());
-	return ret;
 }
include/kvm/arm_arch_timer.h +4 -0
index cd6d8f260eab..1638418f72dd 100644
@@ -104,4 +104,8 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
 u32 timer_get_ctl(struct arch_timer_context *ctxt);
 u64 timer_get_cval(struct arch_timer_context *ctxt);
 
+/* CPU HP callbacks */
+void kvm_timer_cpu_up(void);
+void kvm_timer_cpu_down(void);
+
 #endif
include/kvm/arm_vgic.h +4 -0
index 4df9e73a8bb5..fc4acc91ba06 100644
@@ -431,4 +431,8 @@ int vgic_v4_load(struct kvm_vcpu *vcpu);
 void vgic_v4_commit(struct kvm_vcpu *vcpu);
 int vgic_v4_put(struct kvm_vcpu *vcpu, bool need_db);
 
+/* CPU HP callbacks */
+void kvm_vgic_cpu_up(void);
+void kvm_vgic_cpu_down(void);
+
 #endif /* __KVM_ARM_VGIC_H */
include/linux/cpuhotplug.h +0 -3
index f61447913db9..7337414e4947 100644
@@ -186,9 +186,6 @@ enum cpuhp_state {
 	CPUHP_AP_TI_GP_TIMER_STARTING,
 	CPUHP_AP_HYPERV_TIMER_STARTING,
 	CPUHP_AP_KVM_STARTING,
-	CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
-	CPUHP_AP_KVM_ARM_VGIC_STARTING,
-	CPUHP_AP_KVM_ARM_TIMER_STARTING,
 	/* Must be the last timer callback */
 	CPUHP_AP_DUMMY_TIMER_STARTING,
 	CPUHP_AP_ARM_XEN_STARTING,
Signature · 1 line
2.25.1
#5 lore raw
In reply to [email protected]
Quoted 5 lines · On Tue, Nov 01, 2022, [email protected] wrote:

From: Isaku Yamahata <[email protected]>

This patch series include random simplifications of KVM hardware enable/disable. Although the past attempt [1] was turned out to be a bad idea, it has still useful patches.

I have a larger cleanup that includes all of these patches except "Remove a global variable, hardware_enable_failed". I was planning on posting the series last weeks, but I've dealing with a comedy of errors. With luck, I'll get it posted today.

I'll fold in the aforementioned patch as well, there are quite a few conflicts as my series has a variety of bug fixes before it gets to these cleanups.