Skip to contentSkip to conversation

KVM: arm64: Disable preemption in kvm_arch_hardware_enable()

PATCH Applied Marc Zyngier 4 participants 9 messages Jul 3, 2023 – Jul 11, 2023

Subsystems (3)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 KVM: arm64: Disable preemption in kvm_arch_hardware_enable()

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

Since this affects released versions, let's do the easy fix first, disabling preemption in kvm_arch_hardware_enable(). We can always revisit this with a more invasive fix in the future.

Fixes 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock")
Cc [email protected] # v6.3, v6.4
---
 arch/arm64/kvm/arm.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/arm64/kvm/arm.c +12 -1
index aaeae1145359..a28c4ffe4932 100644
@@ -1894,8 +1894,17 @@ static void _kvm_arch_hardware_enable(void *discard)
 
 int kvm_arch_hardware_enable(void)
 {
-	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
+	int was_enabled;
 
+	/*
+	 * Most calls to this function are made with migration
+	 * disabled, but not with preemption disabled. The former is
+	 * enough to ensure correctness, but most of the helpers
+	 * expect the later and will throw a tantrum otherwise.
+	 */
+	preempt_disable();
+
+	was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
 	_kvm_arch_hardware_enable(NULL);
 
 	if (!was_enabled) {
@@ -1903,6 +1912,8 @@ int kvm_arch_hardware_enable(void)
 		kvm_timer_cpu_up();
 	}
 
+	preempt_enable();
+
 	return 0;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · Marc Zyngier

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

Since this affects released versions, let's do the easy fix first, disabling preemption in kvm_arch_hardware_enable(). We can always revisit this with a more invasive fix in the future.

Quoted 6 lines
Fixes 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock")
Cc [email protected] # v6.3, v6.4

Typo here, didn't make it to the stable list (kernek.org -> kernel.org)

Kristina

#2 lore raw
Quoted 15 lines · Kristina Martsenko
Quoted 14 lines · Marc Zyngier

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

Since this affects released versions, let's do the easy fix first, disabling preemption in kvm_arch_hardware_enable(). We can always revisit this with a more invasive fix in the future.

Quoted 8 lines
Quoted 6 lines
Fixes 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock")
Cc [email protected] # v6.3, v6.4

Typo here, didn't make it to the stable list (kernek.org -> kernel.org)

Thanks for catching this Kristina, I'll fix it up when I apply the patch. So long as the patch appears in Linus' tree with the correct Cc it'll find its way to the stable trees.

--
Thanks,
Oliver

author maintainer
#3 lore raw
In reply to Kristina Martsenko
Quoted 16 lines · Kristina Martsenko
Quoted 14 lines · Marc Zyngier

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

Since this affects released versions, let's do the easy fix first, disabling preemption in kvm_arch_hardware_enable(). We can always revisit this with a more invasive fix in the future.

Quoted 8 lines
Quoted 6 lines
Fixes 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock")
Cc [email protected] # v6.3, v6.4

Typo here, didn't make it to the stable list (kernek.org -> kernel.org)

Yeah, I had a bad day (I also typoed Catalin's address in a separate email...).

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#4 lore raw
In reply to Marc Zyngier
Quoted 34 lines · Marc Zyngier

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

Since this affects released versions, let's do the easy fix first, disabling preemption in kvm_arch_hardware_enable(). We can always revisit this with a more invasive fix in the future.

Fixes 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock")
Cc [email protected] # v6.3, v6.4
---
 arch/arm64/kvm/arm.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/arm64/kvm/arm.c +1 -1
index aaeae1145359..a28c4ffe4932 100644
@@ -1894,8 +1894,17 @@ static void _kvm_arch_hardware_enable(void *discard)
 
 int kvm_arch_hardware_enable(void)
 {
-	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
+	int was_enabled;

arch/arm64/kvm/arm.c static void _kvm_arch_hardware_enable(void *discard) · line 1907

Quoted 10 lines
arch/arm64/kvm/arm.c +9 -0
 
+	/*
+	 * Most calls to this function are made with migration
+	 * disabled, but not with preemption disabled. The former is
+	 * enough to ensure correctness, but most of the helpers
+	 * expect the later and will throw a tantrum otherwise.
+	 */
+	preempt_disable();
+
+	was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);

IMO, this_cpu_has_cap() is at fault. E.g. why not do this?

arch/arm64/kernel/cpufeature.c +3 -1
index 7d7128c65161..b862477de2ce 100644
@@ -3193,7 +3193,9 @@ static void __init setup_boot_cpu_capabilities(void)
 
 bool this_cpu_has_cap(unsigned int n)
 {
-       if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
+       __this_cpu_preempt_check("has_cap");
+
+       if (n < ARM64_NCAPS) {
                const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
 
                if (cap)
author maintainer
#5 lore raw
Quoted 55 lines · Sean Christopherson
Quoted 34 lines · Marc Zyngier

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

Since this affects released versions, let's do the easy fix first, disabling preemption in kvm_arch_hardware_enable(). We can always revisit this with a more invasive fix in the future.

Fixes 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock")
Cc [email protected] # v6.3, v6.4
---
 arch/arm64/kvm/arm.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/arm64/kvm/arm.c +1 -1
index aaeae1145359..a28c4ffe4932 100644
@@ -1894,8 +1894,17 @@ static void _kvm_arch_hardware_enable(void *discard)
 
 int kvm_arch_hardware_enable(void)
 {
-	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
+	int was_enabled;

arch/arm64/kvm/arm.c static void _kvm_arch_hardware_enable(void *discard) · line 1907

Quoted 10 lines
arch/arm64/kvm/arm.c +9 -0
 
+	/*
+	 * Most calls to this function are made with migration
+	 * disabled, but not with preemption disabled. The former is
+	 * enough to ensure correctness, but most of the helpers
+	 * expect the later and will throw a tantrum otherwise.
+	 */
+	preempt_disable();
+
+	was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);

IMO, this_cpu_has_cap() is at fault. E.g. why not do this?

arch/arm64/kernel/cpufeature.c +0 -0
index 7d7128c65161..b862477de2ce 100644

arch/arm64/kernel/cpufeature.c static void __init setup_boot_cpu_capabilities(void) · line 3201

Quoted 10 lines
arch/arm64/kernel/cpufeature.c +3 -1
@@ -3193,7 +3193,9 @@ static void __init setup_boot_cpu_capabilities(void)
 
 bool this_cpu_has_cap(unsigned int n)
 {
-       if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
+       __this_cpu_preempt_check("has_cap");
+
+       if (n < ARM64_NCAPS) {
                const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
 
                if (cap)

Because this check is not on at all times (it relies on DEBUG_PREEMPT), and we really want it to be there.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw
In reply to Sean Christopherson
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Marc Zyngier
arch/arm64/kvm/arm.c +1 -1
index aaeae1145359..a28c4ffe4932 100644
@@ -1894,8 +1894,17 @@ static void _kvm_arch_hardware_enable(void *discard)
 
 int kvm_arch_hardware_enable(void)
 {
-	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
+	int was_enabled;
 
Quoted 11 lines

arch/arm64/kvm/arm.c static void _kvm_arch_hardware_enable(void *discard) · line 1907

Quoted 9 lines
arch/arm64/kvm/arm.c +9 -0
+	/*
+	 * Most calls to this function are made with migration
+	 * disabled, but not with preemption disabled. The former is
+	 * enough to ensure correctness, but most of the helpers
+	 * expect the later and will throw a tantrum otherwise.
+	 */
+	preempt_disable();
+
+	was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);

IMO, this_cpu_has_cap() is at fault.

Who ever said otherwise?

arch/arm64/kernel/cpufeature.c static void __init setup_boot_cpu_capabilities(void) · line 3198

Quoted 12 lines
arch/arm64/kernel/cpufeature.c +3 -1
index 7d7128c65161..b862477de2ce 100644
@@ -3193,7 +3193,9 @@ static void __init setup_boot_cpu_capabilities(void)
 
 bool this_cpu_has_cap(unsigned int n)
 {
-       if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
+       __this_cpu_preempt_check("has_cap");
+
+       if (n < ARM64_NCAPS) {

This is likely sufficient, but to Marc's point we have !preemptible() checks littered about, it just so happens that this_cpu_has_cap() is the first to get called. We need to make sure there aren't any other checks that'd break under hotplug.

While I'd normally like to see the 'right' fix fully fleshed out for something like this, the bug is ugly enough where I'd rather take a hack for the time being.

Signature · 2 lines
Thanks,
Oliver
#7 lore raw
Quoted 25 lines · Oliver Upton
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Marc Zyngier
arch/arm64/kvm/arm.c +1 -1
index aaeae1145359..a28c4ffe4932 100644
@@ -1894,8 +1894,17 @@ static void _kvm_arch_hardware_enable(void *discard)
 
 int kvm_arch_hardware_enable(void)
 {
-	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
+	int was_enabled;
 
Quoted 11 lines

arch/arm64/kvm/arm.c static void _kvm_arch_hardware_enable(void *discard) · line 1907

Quoted 9 lines
arch/arm64/kvm/arm.c +9 -0
+	/*
+	 * Most calls to this function are made with migration
+	 * disabled, but not with preemption disabled. The former is
+	 * enough to ensure correctness, but most of the helpers
+	 * expect the later and will throw a tantrum otherwise.
+	 */
+	preempt_disable();
+
+	was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);

IMO, this_cpu_has_cap() is at fault.

Quoted 1 line

Who ever said otherwise?

Sorry, should have phrased that as "should be fixed".

Quoted 6 lines
Quoted 6 lines
arch/arm64/kernel/cpufeature.c +0 -0
index 7d7128c65161..b862477de2ce 100644
@@ -3193,7 +3193,9 @@ static void __init setup_boot_cpu_capabilities(void)
 
Quoted 11 lines

arch/arm64/kernel/cpufeature.c static void __init setup_boot_cpu_capabilities(void) · line 3198

Quoted 6 lines
arch/arm64/kernel/cpufeature.c +3 -1
 bool this_cpu_has_cap(unsigned int n)
 {
-       if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
+       __this_cpu_preempt_check("has_cap");
+
+       if (n < ARM64_NCAPS) {

This is likely sufficient, but to Marc's point we have !preemptible() checks littered about, it just so happens that this_cpu_has_cap() is the first to get called. We need to make sure there aren't any other checks that'd break under hotplug.

Ah, "we" is all of arm64. E.g. this pattern in arch/arm64/kernel/cpu_errata.c is splattered all over the place.

  WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());

AFAICT, the only issue in KVM proper is the BUG_ON() in kvm_vgic_init_cpu_hardware().

Sadly, a hack-a-fix for stable probably does make sense :-(

Applied
#8 lore raw
In reply to Marc Zyngier
Quoted 11 lines · Marc Zyngier

Since 0bf50497f03b ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock"), hotplugging back a CPU whilst a guest is running results in a number of ugly splats as most of this code expects to run with preemption disabled, which isn't the case anymore.

While the context is preemptable, it isn't migratable, which should be enough. But we have plenty of preemptible() checks all over the place, and our per-CPU accessors also disable preemption.

[...]

Applied to kvmarm/fixes, thanks!

[1/1] KVM: arm64: Disable preemption in kvm_arch_hardware_enable()

      https://git.kernel.org/kvmarm/kvmarm/c/970dee09b230

--
Best,
Oliver