Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Fix emulated CPUID features being applied to wrong sub-leaf
@ 2026-06-09  7:57 Binbin Wu
  2026-06-09  9:21 ` Xiaoyao Li
  0 siblings, 1 reply; 2+ messages in thread
From: Binbin Wu @ 2026-06-09  7:57 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: seanjc, pbonzini, rick.p.edgecombe, xiaoyao.li, chao.gao,
	kai.huang, binbin.wu

Pass the CPUID index into cpuid_func_emulated() and return no emulated
features for indexed CPUID leaves with a non-zero index.

KVM currently emulates CPUID features only for index 0, but
kvm_vcpu_after_set_cpuid() looks up emulated features by function alone.
As a result, reverse_cpuid[] entries that share a function but use a
non-zero index, e.g. CPUID.7.1:ECX, can inherit emulated features that
belong to index 0.  For example, RDPID, which is CPUID.7.0:ECX[22], can
be incorrectly OR'd into CPUID.7.1:ECX.

This is benign today because the affected bits do not correspond to
features KVM cares about, but it can become a real bug as new CPUID
features are defined.  Make the helper index-aware so emulated features
are applied only to the CPUID entry they actually describe.

Fixes: e592ec657d84 ("KVM: x86: Initialize guest cpu_caps based on KVM support")
Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>
---
 arch/x86/kvm/cpuid.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 591d2294acd7..d92ce1e02cd3 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -369,7 +369,7 @@ static u32 cpuid_get_reg_unsafe(struct kvm_cpuid_entry2 *entry, u32 reg)
 	}
 }
 
-static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func,
+static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, u32 index,
 			       bool include_partially_emulated);
 
 void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
@@ -399,7 +399,7 @@ void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 		if (!entry)
 			continue;
 
-		cpuid_func_emulated(&emulated, cpuid.function, true);
+		cpuid_func_emulated(&emulated, cpuid.function, cpuid.index, true);
 
 		/*
 		 * A vCPU has a feature if it's supported by KVM and is enabled
@@ -1368,11 +1368,15 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
 	return entry;
 }
 
-static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func,
+static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, u32 index,
 			       bool include_partially_emulated)
 {
 	memset(entry, 0, sizeof(*entry));
 
+	/* KVM doesn't currently emulate any non-zero indices. */
+	if (cpuid_function_is_indexed(func) && index)
+		return 0;
+
 	entry->function = func;
 	entry->index = 0;
 	entry->flags = 0;
@@ -1410,7 +1414,7 @@ static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
 	if (array->nent >= array->maxnent)
 		return -E2BIG;
 
-	array->nent += cpuid_func_emulated(&array->entries[array->nent], func, false);
+	array->nent += cpuid_func_emulated(&array->entries[array->nent], func, 0, false);
 	return 0;
 }
 

base-commit: de3a35be92d2391ece4bf3143ef2887192625fd0
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] KVM: x86: Fix emulated CPUID features being applied to wrong sub-leaf
  2026-06-09  7:57 [PATCH] KVM: x86: Fix emulated CPUID features being applied to wrong sub-leaf Binbin Wu
@ 2026-06-09  9:21 ` Xiaoyao Li
  0 siblings, 0 replies; 2+ messages in thread
From: Xiaoyao Li @ 2026-06-09  9:21 UTC (permalink / raw)
  To: Binbin Wu, kvm, linux-kernel
  Cc: seanjc, pbonzini, rick.p.edgecombe, chao.gao, kai.huang

On 6/9/2026 3:57 PM, Binbin Wu wrote:
> Pass the CPUID index into cpuid_func_emulated() and return no emulated
> features for indexed CPUID leaves with a non-zero index.
> 
> KVM currently emulates CPUID features only for index 0, but
> kvm_vcpu_after_set_cpuid() looks up emulated features by function alone.
> As a result, reverse_cpuid[] entries that share a function but use a
> non-zero index, e.g. CPUID.7.1:ECX, can inherit emulated features that
> belong to index 0.  For example, RDPID, which is CPUID.7.0:ECX[22], can
> be incorrectly OR'd into CPUID.7.1:ECX.
> 
> This is benign today because the affected bits do not correspond to
> features KVM cares about, but it can become a real bug as new CPUID
> features are defined.  Make the helper index-aware so emulated features
> are applied only to the CPUID entry they actually describe.
> 
> Fixes: e592ec657d84 ("KVM: x86: Initialize guest cpu_caps based on KVM support")
> Suggested-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>

Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-06-09  9:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-09  7:57 [PATCH] KVM: x86: Fix emulated CPUID features being applied to wrong sub-leaf Binbin Wu
2026-06-09  9:21 ` Xiaoyao Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox