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. That said, this is a nice cleanup on its own. 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. Patch 1 (re)adds a dedicated lock for kvm_usage_count. Hopefully it's a temporary lock, as we can likely fix the cpus_read_lock() vs. kvm_lock mess for good by switching vm_list to an (S)RCU-protected list. v4: - Collect more reviews. - Fix documentation for kvm_usage_lock. [Kai] - Add a blurb in locking.rst to call out that cpus_read_lock() vs kvm_lock is a general problem. - Add a paragraph in the locking patch to suggest switching vm_list to an (S)RCU-protected list, so that walkers don't need to take kvm_lock. - Add an example call chain for the other cpus_read_lock() vs kvm_lock issues that are lurking. - Enable the module param by default from the get-go. [Paolo] - Rename even more APIs/symbols (all of 'em I could find). [Paolo] - Clarify that it's the 0=>1 VM creation that's problematic, not simply the creation of the very first VM. [Paolo] - Document enable_virt_at_load, and call out the (dis)advantages of enabling virtualization when KVM is loaded. - Drop the WARN on kvm_usage_count being elevated at the end of kvm_uninit_virtualization(), as it's annoyingly difficult to keep the WARN and still allow arch code (i.e. TDX) to enable virtualization during setup. v3: - https://lore.kernel.org/all/20240608000639.3295768-1-seanjc@xxxxxxxxxx - 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 (10): KVM: Use dedicated mutex to protect kvm_usage_count to avoid deadlock KVM: Register cpuhp and syscore callbacks when enabling hardware KVM: Rename symbols related to enabling virtualization hardware KVM: Rename arch hooks related to per-CPU virtualization enabling KVM: MIPS: Rename virtualization {en,dis}abling APIs to match common KVM KVM: x86: Rename virtualization {en,dis}abling APIs to match common KVM 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 .../admin-guide/kernel-parameters.txt | 17 ++ Documentation/virt/kvm/locking.rst | 31 +- arch/arm64/kvm/arm.c | 6 +- arch/loongarch/kvm/main.c | 4 +- arch/mips/include/asm/kvm_host.h | 4 +- arch/mips/kvm/mips.c | 8 +- arch/mips/kvm/vz.c | 8 +- arch/riscv/kvm/main.c | 4 +- arch/x86/include/asm/kvm-x86-ops.h | 4 +- arch/x86/include/asm/kvm_host.h | 7 +- arch/x86/include/asm/reboot.h | 2 +- arch/x86/kvm/svm/svm.c | 19 +- arch/x86/kvm/vmx/main.c | 6 +- arch/x86/kvm/vmx/vmx.c | 10 +- arch/x86/kvm/vmx/x86_ops.h | 5 +- arch/x86/kvm/x86.c | 26 +- include/linux/kvm_host.h | 18 +- virt/kvm/kvm_main.c | 270 +++++++++--------- 18 files changed, 251 insertions(+), 198 deletions(-) base-commit: 15e1c3d65975524c5c792fcd59f7d89f00402261 -- 2.46.0.469.g59c65b2a67-goog