From 61712afcb876caaeca004cbf1f5eacab59df1ffe Mon Sep 17 00:00:00 2001
From: Juergen Gross <jgross@suse.com>
Date: May 26 2025 17:52:27 +0000
Subject: KVM: x86: Remove the unreachable case for 0x80000022 leaf in

__do_cpuid_func() (git-fixes).

---

diff --git a/patches.suse/KVM-x86-Remove-the-unreachable-case-for-0x80000022-l.patch b/patches.suse/KVM-x86-Remove-the-unreachable-case-for-0x80000022-l.patch
new file mode 100644
index 0000000..baabe22
--- /dev/null
+++ b/patches.suse/KVM-x86-Remove-the-unreachable-case-for-0x80000022-l.patch
@@ -0,0 +1,58 @@
+Patch-mainline: v6.15
+Git-commit: e6c8728a8e2d20b262209c70a8ca67719a628833
+References: git-fixes
+From: Xiaoyao Li <xiaoyao.li@intel.com>
+Date: Tue, 4 Mar 2025 03:23:13 -0500
+Subject: [PATCH] KVM: x86: Remove the unreachable case for 0x80000022 leaf in
+ __do_cpuid_func()
+
+Remove dead/unreachable (and misguided) code in KVM's processing of
+0x80000022.  The case statement breaks early if PERFMON_V2 isnt supported,
+i.e. kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2) must be true when KVM reaches
+the code code to setup EBX.
+
+Note, early versions of the patch that became commit 94cdeebd8211 ("KVM:
+x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022") didn't break
+early on lack of PERFMON_V2 support, and instead enumerated the effective
+number of counters KVM could emulate.  All of that code was flawed, e.g.
+the APM explicitly states EBX is valid only for v2.
+
+  Performance Monitoring Version 2 supported. When set,
+  CPUID_Fn8000_0022_EBX reports the number of available performance counters.
+
+When the flaw of not respecting v2 support was addressed, the misguided
+stuffing of the number of counters got left behind.
+
+Link: https://lore.kernel.org/all/20220919093453.71737-4-likexu@tencent.com
+Fixes: 94cdeebd8211 ("KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022")
+Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
+Link: https://lore.kernel.org/r/20250304082314.472202-2-xiaoyao.li@intel.com
+[sean: elaborate on the situation a bit more, add Fixes]
+Signed-off-by: Sean Christopherson <seanjc@google.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+---
+ arch/x86/kvm/cpuid.c | 8 +-------
+ 1 file changed, 1 insertion(+), 7 deletions(-)
+
+diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
+index 97a90689a9dc..668e8fac1f6e 100644
+--- a/arch/x86/kvm/cpuid.c
++++ b/arch/x86/kvm/cpuid.c
+@@ -1773,13 +1773,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
+ 
+ 		cpuid_entry_override(entry, CPUID_8000_0022_EAX);
+ 
+-		if (kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
+-			ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+-		else if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
+-			ebx.split.num_core_pmc = AMD64_NUM_COUNTERS_CORE;
+-		else
+-			ebx.split.num_core_pmc = AMD64_NUM_COUNTERS;
+-
++		ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+ 		entry->ebx = ebx.full;
+ 		break;
+ 	}
+-- 
+2.43.0
+
diff --git a/series.conf b/series.conf
index 5f6a43a..3a741cd 100644
--- a/series.conf
+++ b/series.conf
@@ -31066,6 +31066,7 @@
 	patches.suse/KVM-nVMX-Check-PAUSE_EXITING-not-BUS_LOCK_DETECTION-.patch
 	patches.suse/KVM-nSVM-Pass-next-RIP-not-current-RIP-for-nested-VM.patch
 	patches.suse/KVM-nVMX-Allow-emulating-RDPID-on-behalf-of-L2.patch
+	patches.suse/KVM-x86-Remove-the-unreachable-case-for-0x80000022-l.patch
 	patches.suse/KVM-arm64-vgic-v4-Only-attempt-vLPI-mapping-for-actu.patch
 	patches.suse/KVM-arm64-vgic-v4-Fall-back-to-software-irqbypass-if.patch
 	patches.suse/KVM-arm64-Tear-down-vGIC-on-failed-vCPU-creation.patch