Skip to content

Commit fd21732

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Fold guts of kvm_arch_sync_events() into kvm_arch_pre_destroy_vm()
Fold the guts of kvm_arch_sync_events() into kvm_arch_pre_destroy_vm(), as the kvmclock and PIT background workers only need to be stopped before destroying vCPUs (to avoid accessing vCPUs as they are being freed); it's a-ok for them to be running while the VM is visible on the global vm_list. Note, the PIT also needs to be stopped before IRQ routing is freed (because KVM's IRQ routing is garbage and assumes there is always non-NULL routing). Opportunistically add comments to explain why KVM stops/frees certain assets early. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20250224235542.2562848-7-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent e447212 commit fd21732

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

arch/x86/kvm/x86.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12759,9 +12759,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1275912759

1276012760
void kvm_arch_sync_events(struct kvm *kvm)
1276112761
{
12762-
cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
12763-
cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
12764-
kvm_free_pit(kvm);
12762+
1276512763
}
1276612764

1276712765
/**
@@ -12842,6 +12840,17 @@ EXPORT_SYMBOL_GPL(__x86_set_memory_region);
1284212840

1284312841
void kvm_arch_pre_destroy_vm(struct kvm *kvm)
1284412842
{
12843+
/*
12844+
* Stop all background workers and kthreads before destroying vCPUs, as
12845+
* iterating over vCPUs in a different task while vCPUs are being freed
12846+
* is unsafe, i.e. will lead to use-after-free. The PIT also needs to
12847+
* be stopped before IRQ routing is freed.
12848+
*/
12849+
cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
12850+
cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
12851+
12852+
kvm_free_pit(kvm);
12853+
1284512854
kvm_mmu_pre_destroy_vm(kvm);
1284612855
}
1284712856

0 commit comments

Comments
 (0)