public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Chao Gao <chao.gao@intel.com>, Kai Huang <kai.huang@intel.com>
Subject: [PATCH v2 3/6] KVM: Add a module param to allow enabling virtualization when KVM is loaded
Date: Tue, 21 May 2024 19:28:24 -0700	[thread overview]
Message-ID: <20240522022827.1690416-4-seanjc@google.com> (raw)
In-Reply-To: <20240522022827.1690416-1-seanjc@google.com>

Add an off-by-default module param, enable_virt_at_load, to let userspace
force virtualization to be enabled in hardware when KVM is initialized,
i.e. just before /dev/kvm is exposed to userspace.  Enabling virtualization
during KVM initialization allows userspace to avoid the additional latency
when creating/destroying the first/last VM.  Now that KVM uses the cpuhp
framework to do per-CPU enabling, the latency could be non-trivial as the
cpuhup bringup/teardown is serialized across CPUs, e.g. the latency could
be problematic for use case that need to spin up VMs quickly.

Enabling virtualizaton during initialization will also allow KVM to setup
the Intel TDX Module, which requires VMX to be fully enabled, without
needing additional APIs to temporarily enable virtualization.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 virt/kvm/kvm_main.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 8ba2861e7788..8d331ab3aef7 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -5495,6 +5495,9 @@ static struct miscdevice kvm_dev = {
 };
 
 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
+static bool enable_virt_at_load;
+module_param(enable_virt_at_load, bool, 0444);
+
 __visible bool kvm_rebooting;
 EXPORT_SYMBOL_GPL(kvm_rebooting);
 
@@ -5645,15 +5648,41 @@ static void kvm_disable_virtualization(void)
 	unregister_syscore_ops(&kvm_syscore_ops);
 	cpuhp_remove_state(CPUHP_AP_KVM_ONLINE);
 }
+
+static int kvm_init_virtualization(void)
+{
+	if (enable_virt_at_load)
+		return kvm_enable_virtualization();
+
+	return 0;
+}
+
+static void kvm_uninit_virtualization(void)
+{
+	if (enable_virt_at_load)
+		kvm_disable_virtualization();
+
+	WARN_ON(kvm_usage_count);
+}
 #else /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */
 static int kvm_enable_virtualization(void)
 {
 	return 0;
 }
 
+static int kvm_init_virtualization(void)
+{
+	return 0;
+}
+
 static void kvm_disable_virtualization(void)
 {
 
+}
+
+static void kvm_uninit_virtualization(void)
+{
+
 }
 #endif /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */
 
@@ -6395,6 +6424,10 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 
 	kvm_gmem_init(module);
 
+	r = kvm_init_virtualization();
+	if (r)
+		goto err_virt;
+
 	/*
 	 * Registration _must_ be the very last thing done, as this exposes
 	 * /dev/kvm to userspace, i.e. all infrastructure must be setup!
@@ -6408,6 +6441,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 	return 0;
 
 err_register:
+	kvm_uninit_virtualization();
+err_virt:
 	kvm_vfio_ops_exit();
 err_vfio:
 	kvm_async_pf_deinit();
@@ -6433,6 +6468,8 @@ void kvm_exit(void)
 	 */
 	misc_deregister(&kvm_dev);
 
+	kvm_uninit_virtualization();
+
 	debugfs_remove_recursive(kvm_debugfs_dir);
 	for_each_possible_cpu(cpu)
 		free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
-- 
2.45.0.215.g3402c0e53f-goog


  parent reply	other threads:[~2024-05-22  2:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-22  2:28 [PATCH v2 0/6] KVM: Register cpuhp/syscore callbacks when enabling virt Sean Christopherson
2024-05-22  2:28 ` [PATCH v2 1/6] KVM: Register cpuhp and syscore callbacks when enabling hardware Sean Christopherson
2024-05-22  6:10   ` Chao Gao
2024-05-29 14:29     ` Sean Christopherson
2024-05-22  2:28 ` [PATCH v2 2/6] KVM: Rename functions related to enabling virtualization hardware Sean Christopherson
2024-05-22  7:10   ` Chao Gao
2024-05-22 22:34   ` Huang, Kai
2024-05-22  2:28 ` Sean Christopherson [this message]
2024-05-22 22:27   ` [PATCH v2 3/6] KVM: Add a module param to allow enabling virtualization when KVM is loaded Huang, Kai
2024-05-23  4:23     ` Chao Gao
2024-05-23 23:11       ` Huang, Kai
2024-05-24  2:39         ` Chao Gao
2024-05-27 22:36           ` Huang, Kai
2024-05-29 15:01             ` Sean Christopherson
2024-05-29 22:45               ` Huang, Kai
2024-05-29 23:07                 ` Sean Christopherson
2024-05-30  0:06                   ` Huang, Kai
2024-05-22  2:28 ` [PATCH v2 4/6] KVM: Add arch hooks for enabling/disabling virtualization Sean Christopherson
2024-05-22 22:33   ` Huang, Kai
2024-05-28 22:50     ` Sean Christopherson
2024-05-23  5:31   ` Chao Gao
2024-05-22  2:28 ` [PATCH v2 5/6] x86/reboot: Unconditionally define cpu_emergency_virt_cb typedef Sean Christopherson
2024-05-22 22:35   ` Huang, Kai
2024-05-23  5:41   ` Chao Gao
2024-05-22  2:28 ` [PATCH v2 6/6] KVM: x86: Register "emergency disable" callbacks when virt is enabled Sean Christopherson
2024-05-22 22:37   ` Huang, Kai
2024-05-23  5:59   ` Chao Gao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240522022827.1690416-4-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=chao.gao@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox