mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/cpufeatures: Add CPUID_LNX_5 to track recently added Linux-defined word
@ 2024-04-05  0:16 Sean Christopherson
  2024-04-05  0:33 ` Dave Hansen
  0 siblings, 1 reply; 2+ messages in thread
From: Sean Christopherson @ 2024-04-05  0:16 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Sean Christopherson, Paolo Bonzini
  Cc: linux-kernel, kvm, Sandipan Das

Add CPUID_LNX_5 to track cpufeatures' word 21, and add the appropriate
compile-time assert in KVM to prevent direct lookups on the features in
CPUID_LNX_5.  KVM uses X86_FEATURE_* flags to manage guest CPUID, and so
must translate features that are scattered by Linux from the Linux-defined
bit to the hardware-defined bit, i.e. should never try to directly access
scattered features in guest CPUID.

Opportunistically add NR_CPUID_WORDS to enum cpuid_leafs, along with a
compile-time assert in KVM's CPUID infrastructure to ensure that future
additions update cpuid_leafs along with NCAPINTS.

No functional change intended.

Fixes: 7f274e609f3d ("x86/cpufeatures: Add new word for scattered features")
Cc: Sandipan Das <sandipan.das@amd.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/include/asm/cpufeature.h | 2 ++
 arch/x86/kvm/reverse_cpuid.h      | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index 42157ddcc09d..686e92d2663e 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -33,6 +33,8 @@ enum cpuid_leafs
 	CPUID_7_EDX,
 	CPUID_8000_001F_EAX,
 	CPUID_8000_0021_EAX,
+	CPUID_LNX_5,
+	NR_CPUID_WORDS,
 };
 
 #define X86_CAP_FMT_NUM "%d:%d"
diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h
index aadefcaa9561..58ac8d69c94b 100644
--- a/arch/x86/kvm/reverse_cpuid.h
+++ b/arch/x86/kvm/reverse_cpuid.h
@@ -102,10 +102,12 @@ static const struct cpuid_reg reverse_cpuid[] = {
  */
 static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
 {
+	BUILD_BUG_ON(NR_CPUID_WORDS != NCAPINTS);
 	BUILD_BUG_ON(x86_leaf == CPUID_LNX_1);
 	BUILD_BUG_ON(x86_leaf == CPUID_LNX_2);
 	BUILD_BUG_ON(x86_leaf == CPUID_LNX_3);
 	BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
+	BUILD_BUG_ON(x86_leaf == CPUID_LNX_5);
 	BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
 	BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
 }

base-commit: c85af715cac0a951eea97393378e84bb49384734
-- 
2.44.0.478.gd926399ef9-goog


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

* Re: [PATCH] x86/cpufeatures: Add CPUID_LNX_5 to track recently added Linux-defined word
  2024-04-05  0:16 [PATCH] x86/cpufeatures: Add CPUID_LNX_5 to track recently added Linux-defined word Sean Christopherson
@ 2024-04-05  0:33 ` Dave Hansen
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Hansen @ 2024-04-05  0:33 UTC (permalink / raw)
  To: Sean Christopherson, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, Paolo Bonzini
  Cc: linux-kernel, kvm, Sandipan Das

On 4/4/24 17:16, Sean Christopherson wrote:
> Add CPUID_LNX_5 to track cpufeatures' word 21, and add the appropriate
> compile-time assert in KVM to prevent direct lookups on the features in
> CPUID_LNX_5.  KVM uses X86_FEATURE_* flags to manage guest CPUID, and so
> must translate features that are scattered by Linux from the Linux-defined
> bit to the hardware-defined bit, i.e. should never try to directly access
> scattered features in guest CPUID.
> 
> Opportunistically add NR_CPUID_WORDS to enum cpuid_leafs, along with a
> compile-time assert in KVM's CPUID infrastructure to ensure that future
> additions update cpuid_leafs along with NCAPINTS.
> 
> No functional change intended.
> 
> Fixes: 7f274e609f3d ("x86/cpufeatures: Add new word for scattered features")
> Cc: Sandipan Das <sandipan.das@amd.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>

I don't think I noticed this KVM code before, or at least how it
depended on the enum.  But the BUILD_BUG_ON() will surely make the
ignorant among us aware _now_.  Looks good:

Acked-by: Dave Hansen <dave.hansen@linux.intel.com>


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

end of thread, other threads:[~2024-04-05  0:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-05  0:16 [PATCH] x86/cpufeatures: Add CPUID_LNX_5 to track recently added Linux-defined word Sean Christopherson
2024-04-05  0:33 ` Dave Hansen

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

all inboxes | Powered by JetHome®