Skip to content

Commit e4aa7f8

Browse files
gaochaointelbonzini
authored andcommitted
KVM: Disable CPU hotplug during hardware enabling/disabling
Disable CPU hotplug when enabling/disabling hardware to prevent the corner case where if the following sequence occurs: 1. A hotplugged CPU marks itself online in cpu_online_mask 2. The hotplugged CPU enables interrupt before invoking KVM's ONLINE callback 3 hardware_{en,dis}able_all() is invoked on another CPU the hotplugged CPU will be included in on_each_cpu() and thus get sent through hardware_{en,dis}able_nolock() before kvm_online_cpu() is called. start_secondary { ... set_cpu_online(smp_processor_id(), true); <- 1 ... local_irq_enable(); <- 2 ... cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); <- 3 } KVM currently fudges around this race by keeping track of which CPUs have done hardware enabling (see commit 1b6c016 "KVM: Keep track of which cpus have virtualization enabled"), but that's an inefficient, convoluted, and hacky solution. Signed-off-by: Chao Gao <[email protected]> [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent aaf12a7 commit e4aa7f8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

arch/x86/kvm/x86.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9296,7 +9296,16 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops)
92969296

92979297
static int kvm_x86_check_processor_compatibility(void)
92989298
{
9299-
struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
9299+
int cpu = smp_processor_id();
9300+
struct cpuinfo_x86 *c = &cpu_data(cpu);
9301+
9302+
/*
9303+
* Compatibility checks are done when loading KVM and when enabling
9304+
* hardware, e.g. during CPU hotplug, to ensure all online CPUs are
9305+
* compatible, i.e. KVM should never perform a compatibility check on
9306+
* an offline CPU.
9307+
*/
9308+
WARN_ON(!cpu_online(cpu));
93009309

93019310
if (__cr4_reserved_bits(cpu_has, c) !=
93029311
__cr4_reserved_bits(cpu_has, &boot_cpu_data))

virt/kvm/kvm_main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5167,15 +5167,26 @@ static void hardware_disable_all_nolock(void)
51675167

51685168
static void hardware_disable_all(void)
51695169
{
5170+
cpus_read_lock();
51705171
raw_spin_lock(&kvm_count_lock);
51715172
hardware_disable_all_nolock();
51725173
raw_spin_unlock(&kvm_count_lock);
5174+
cpus_read_unlock();
51735175
}
51745176

51755177
static int hardware_enable_all(void)
51765178
{
51775179
int r = 0;
51785180

5181+
/*
5182+
* When onlining a CPU, cpu_online_mask is set before kvm_online_cpu()
5183+
* is called, and so on_each_cpu() between them includes the CPU that
5184+
* is being onlined. As a result, hardware_enable_nolock() may get
5185+
* invoked before kvm_online_cpu(), which also enables hardware if the
5186+
* usage count is non-zero. Disable CPU hotplug to avoid attempting to
5187+
* enable hardware multiple times.
5188+
*/
5189+
cpus_read_lock();
51795190
raw_spin_lock(&kvm_count_lock);
51805191

51815192
kvm_usage_count++;
@@ -5190,6 +5201,7 @@ static int hardware_enable_all(void)
51905201
}
51915202

51925203
raw_spin_unlock(&kvm_count_lock);
5204+
cpus_read_unlock();
51935205

51945206
return r;
51955207
}

0 commit comments

Comments
 (0)