From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33B904E4306 for ; Wed, 30 Sep 2026 15:10:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790781016; cv=none; b=f5N1+HqnRZPPpJdkmjLqd+iedWRrTUsINKT8nU+VMyXJxD3Jylt53Rf/GMSJJ6GnV9hYmlthiwUkGR5D6u0bbN5O+2vxLz/hixokcuarIxtF1XHQuAV5XSl1BX1XM6TkXN9c2bVYPSBcqgV6CSmo7Stsr7eevCw3SjhG2xypUn8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790781016; c=relaxed/simple; bh=mSLphW/MdBAnnFKav9K2WduZOteiCVKG92yy+cfKPx8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MNjERgVUxQj4l407IIHO/yRnpAFTC6dyeth+BRoHLD29h4Rt6CDnyprTP8LasEGgU9Hj9QPbBMuLMRYp0A/UvMW9OjA5QqMS1zDwQJbhhobePnz1cIaCZNqaatwxp+uWS0N/+qH1txsmsH9tO3TkNGAoB7xWzx/3TOsG5zmW6iQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Xk1fHf8B; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Xk1fHf8B" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E4C61F00898; Wed, 30 Sep 2026 15:10:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790781000; bh=5BQq4+N/a1vXSMfHOoVVJ7ZLsv1i5EzlyqM4N+2MkyI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Xk1fHf8Bc0Hxt1FkYJy6a9BY5UytABCsgcUEWI8AaoAvWJfD5KAr4ix+75W6g0Zzo oViyRc7THY4gdmXY0pPHMounhJoYtGUt4m5pBSX2T0Rt7hITNhMkeXvWdaBmDwLf9w mNJyD3u9WNPQNYKWuXEy64z2UX28pSXjK+dF5zlHPu7ffoJYi0koERUFyVrR8mDSQ+ VXqVAP6R+qwCChXQfJ5YVK2rpfek3v5okT/7wHuQFikpIJTPCbIJjs+Dy6TWByp4nS GuSkCil7VuHxHK23WEIoOQKeLH61gwnQeV++e2X/2lcT0DDQsn4GK1ejYE1E6bgvb2 TAJfCuDz4+GjQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Sean Christopherson , Vitaly Kuznetsov , Sasha Levin Subject: [PATCH 6.18.y 3/5] KVM: x86: Reject nested CAP enablement if nested virtualization is disabled Date: Wed, 30 Sep 2026 11:09:55 -0400 Message-ID: <20260930150957.2359214-3-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260930150957.2359214-1-sashal@kernel.org> References: <2026092945-breath-candle-8e2c@gregkh> <20260930150957.2359214-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson [ Upstream commit b65699be2c606d2687593516d93e66d16a713b61 ] Add a flag to explicitly track if nested virtualization is enabled, and use it enumerate that various nested CAPs are unsupported, and to reject enablement of said CAPs. When the nested ops hooks were moved to their own structure, KVM's NULL-by-default behavior was deliberately dropped, with the changelog asserting that all was well. That wasn't quite true; there is no danger to KVM, but now KVM is over-reporting support for KVM_CAP_NESTED_STATE and KVM_CAP_HYPERV_ENLIGHTENED_VMCS. Fixes: 33b22172452f ("KVM: x86: move nested-related kvm_x86_ops to a separate struct") Reviewed-by: Vitaly Kuznetsov Link: https://patch.msgid.link/20260630202828.440724-2-seanjc@google.com Signed-off-by: Sean Christopherson Stable-dep-of: 10180a277549 ("KVM: x86: Re-pend GET_NESTED_STATE_PAGES if getting said pages fails") Signed-off-by: Sasha Levin --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/hyperv.c | 3 ++- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 12 +++++++----- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 1f2dabd7dbc92..20df39a5318f2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1954,6 +1954,8 @@ struct kvm_x86_ops { }; struct kvm_x86_nested_ops { + bool enabled; + void (*leave_nested)(struct kvm_vcpu *vcpu); bool (*is_exception_vmexit)(struct kvm_vcpu *vcpu, u8 vector, u32 error_code); diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 380a211f56f00..4753d084e2567 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2799,7 +2799,8 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, }; int i, nent = ARRAY_SIZE(cpuid_entries); - if (kvm_x86_ops.nested_ops->get_evmcs_version) + if (kvm_x86_ops.nested_ops->enabled && + kvm_x86_ops.nested_ops->get_evmcs_version) evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu); if (cpuid->nent < nent) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index a24a6871b6936..e3daa6e57192d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5461,6 +5461,7 @@ static __init int svm_hardware_setup(void) if (r) return r; } + svm_nested_ops.enabled = nested; /* * KVM's MMU doesn't support using 2-level paging for itself, and thus diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6b51f352d2826..5bae3d5ce6eec 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8642,6 +8642,7 @@ __init int vmx_hardware_setup(void) if (r) return r; } + vmx_nested_ops.enabled = nested; r = alloc_kvm_area(); if (r && nested) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f08a34d3ff3d9..91d38d709d8a7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4604,7 +4604,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r &= ~KVM_X2APIC_ENABLE_SUPPRESS_EOI_BROADCAST; break; case KVM_CAP_NESTED_STATE: - r = kvm_x86_ops.nested_ops->get_state ? + r = kvm_x86_ops.nested_ops->enabled ? kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; break; #ifdef CONFIG_KVM_HYPERV @@ -4612,7 +4612,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r = kvm_x86_ops.enable_l2_tlb_flush != NULL; break; case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: - r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; + r = kvm_x86_ops.nested_ops->enabled && + kvm_x86_ops.nested_ops->enable_evmcs != NULL; break; #endif case KVM_CAP_SMALLER_MAXPHYADDR: @@ -5650,7 +5651,8 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, uint16_t vmcs_version; void __user *user_ptr; - if (!kvm_x86_ops.nested_ops->enable_evmcs) + if (!kvm_x86_ops.nested_ops->enabled || + !kvm_x86_ops.nested_ops->enable_evmcs) return -ENOTTY; r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); if (!r) { @@ -6150,7 +6152,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, u32 user_data_size; r = -EINVAL; - if (!kvm_x86_ops.nested_ops->get_state) + if (!kvm_x86_ops.nested_ops->enabled) break; BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); @@ -6180,7 +6182,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, int idx; r = -EINVAL; - if (!kvm_x86_ops.nested_ops->set_state) + if (!kvm_x86_ops.nested_ops->enabled) break; r = -EFAULT; -- 2.53.0