[PATCH 3/3] KVM: x86: Only allow enabling KVM_BUS_LOCK_DETECTION_EXIT before creating any vCPU

Xiaoyao Li <[email protected]>
Newsgroups org.kernel.vger.kvm,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
When userspace enables KVM_BUS_LOCK_DETECTION_EXIT after some vCPUs have
been created, KVM may fail to enable the feature for created vCPUs. It
is possible for KVM to later enable the feature for created vCPUs in some
cases, but it's not guaranteed. Instead of introducing complexity to support
this use case, just disallow enabling KVM_BUS_LOCK_DETECTION_EXIT when
vCPUs have been created for simplicity.

Signed-off-by: Xiaoyao Li <[email protected]>
---
 arch/x86/kvm/x86.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3d8422d1cd04..df8ee03cd9ad 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4061,9 +4061,15 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		if (!kvm_caps.has_bus_lock_exit)
 			break;
 
-		if (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)
-			kvm->arch.bus_lock_detection_enabled = true;
 		r = 0;
+		if (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) {
+			mutex_lock(&kvm->lock);
+			if (!kvm->created_vcpus)
+				kvm->arch.bus_lock_detection_enabled = true;
+			else
+				r = -EINVAL;
+			mutex_unlock(&kvm->lock);
+		}
 		break;
 #ifdef CONFIG_X86_SGX_KVM
 	case KVM_CAP_SGX_ATTRIBUTE: {
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.