[PATCH v3 0/8] KVM: Register cpuhp/syscore callbacks when enabling virt

From: Sean Christopherson
Date: Fri Jun 07 2024 - 20:07:06 EST


Register KVM's cpuhp and syscore callbacks when enabling virtualization in
hardware, as the sole purpose of said callbacks is to disable and re-enable
virtualization as needed.

The primary motivation for this series is to simplify dealing with enabling
virtualization for Intel's TDX, which needs to enable virtualization
when kvm-intel.ko is loaded, i.e. long before the first VM is created. TDX
doesn't _need_ to keep virtualization enabled, but doing so is much simpler
for KVM (see patch 3).

That said, this is a nice cleanup on its own, assuming I haven't broken
something. By registering the callbacks on-demand, the callbacks themselves
don't need to check kvm_usage_count, because their very existence implies a
non-zero count.

The meat is in patch 1. Patches 2 renames the helpers so that patch 3 is
less awkward. Patch 3 adds a module param to enable virtualization when KVM
is loaded. Patches 4-6 are tangentially related x86 cleanups to registers
KVM's "emergency disable" callback on-demand, same as the syscore callbacks.

The suspend/resume and cphup paths still need to be fully tested, as do
non-x86 architectures.

v3:
- Collect reviews/acks.
- Switch to kvm_usage_lock in a dedicated patch, Cc'd for stable@. [Chao]
- Enable virt at load by default. [Chao]
- Add comments to document how kvm_arch_{en,dis}able_virtualization() fit
into the overall flow. [Kai]

v2:
- https://lore.kernel.org/all/20240522022827.1690416-1-seanjc@xxxxxxxxxx
- Use a dedicated mutex to avoid lock inversion issues between kvm_lock and
the cpuhp lock.
- Register emergency disable callbacks on-demand. [Kai]
- Drop an unintended s/junk/ign rename. [Kai]
- Decrement kvm_usage_count on failure. [Chao]

v1: https://lore.kernel.org/all/20240425233951.3344485-1-seanjc@xxxxxxxxxx

Sean Christopherson (8):
KVM: Use dedicated mutex to protect kvm_usage_count to avoid deadlock
KVM: Register cpuhp and syscore callbacks when enabling hardware
KVM: Rename functions related to enabling virtualization hardware
KVM: Add a module param to allow enabling virtualization when KVM is
loaded
KVM: Add arch hooks for enabling/disabling virtualization
x86/reboot: Unconditionally define cpu_emergency_virt_cb typedef
KVM: x86: Register "emergency disable" callbacks when virt is enabled
KVM: Enable virtualization at load/initialization by default

Documentation/virt/kvm/locking.rst | 19 ++-
arch/x86/include/asm/kvm_host.h | 3 +
arch/x86/include/asm/reboot.h | 2 +-
arch/x86/kvm/svm/svm.c | 5 +-
arch/x86/kvm/vmx/main.c | 2 +
arch/x86/kvm/vmx/vmx.c | 6 +-
arch/x86/kvm/vmx/x86_ops.h | 1 +
arch/x86/kvm/x86.c | 10 ++
include/linux/kvm_host.h | 14 ++
virt/kvm/kvm_main.c | 258 ++++++++++++++---------------
10 files changed, 175 insertions(+), 145 deletions(-)


base-commit: af0903ab52ee6d6f0f63af67fa73d5eb00f79b9a
--
2.45.2.505.gda0bf45e8d-goog