public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: Sean Christopherson <seanjc@google.com>,
	Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org, tglx@linutronix.de,
	bp@alien8.de, rafael@kernel.org, lenb@kernel.org,
	dave.jiang@intel.com, irenic.rajneesh@gmail.com,
	david.e.box@intel.com
Subject: Re: [PATCH 10/11] x86/fpu: Remove unnecessary CPUID level check
Date: Fri, 22 Nov 2024 09:46:41 -0800	[thread overview]
Message-ID: <bb937b3f-e595-4aa8-a6e5-08bdbd4702bf@intel.com> (raw)
In-Reply-To: <Zz9VjVKbzMehRTjA@google.com>

[-- Attachment #1: Type: text/plain, Size: 789 bytes --]

On 11/21/24 07:45, Sean Christopherson wrote:
> On Wed, Nov 20, 2024, Dave Hansen wrote:
>> The CPUID level dependency table will entirely zap X86_FEATURE_XSAVE
> 
> What table is that?  XSAVE depends on FXSR, but I can't find anything that clears
> X86_FEATURE_XSAVE if cpuid_level < XSTATE_CPUID.  Even if it did, dropping a
> sanity check in a one-time path adds risk for almost no reward.

arch/x86/kernel/cpu/common.c::cpuid_dependent_features[]

It's hard to find because it hard-codes the leaf number:

        { X86_FEATURE_XSAVE,            0x0000000d },

Fixing that was my initial motivation for this series.

As for removing the checks, I'd much rather have a super generic check
in the CPUID helpers that have all the callers code something.
Something like the attached patch?

[-- Attachment #2: cpuid_count-warn.patch --]
[-- Type: text/x-patch, Size: 2247 bytes --]



---

 b/arch/x86/include/asm/cpuid.h |    6 ++++++
 b/arch/x86/kernel/cpu/common.c |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff -puN arch/x86/include/asm/cpuid.h~cpuid_count-warn arch/x86/include/asm/cpuid.h
--- a/arch/x86/include/asm/cpuid.h~cpuid_count-warn	2024-11-22 08:50:12.618186610 -0800
+++ b/arch/x86/include/asm/cpuid.h	2024-11-22 09:10:12.112217942 -0800
@@ -64,6 +64,8 @@ native_cpuid_reg(edx)
 #define __cpuid			native_cpuid
 #endif
 
+extern void check_cpuid_level(unsigned int level);
+
 /*
  * Generic CPUID function
  * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
@@ -73,6 +75,8 @@ static inline void cpuid(unsigned int op
 			 unsigned int *eax, unsigned int *ebx,
 			 unsigned int *ecx, unsigned int *edx)
 {
+	check_cpuid_level(op);
+
 	*eax = op;
 	*ecx = 0;
 	__cpuid(eax, ebx, ecx, edx);
@@ -83,6 +87,8 @@ static inline void cpuid_count(unsigned
 			       unsigned int *eax, unsigned int *ebx,
 			       unsigned int *ecx, unsigned int *edx)
 {
+	check_cpuid_level(op);
+
 	*eax = op;
 	*ecx = count;
 	__cpuid(eax, ebx, ecx, edx);
diff -puN arch/x86/kernel/cpu/common.c~cpuid_count-warn arch/x86/kernel/cpu/common.c
--- a/arch/x86/kernel/cpu/common.c~cpuid_count-warn	2024-11-22 09:07:43.922591720 -0800
+++ b/arch/x86/kernel/cpu/common.c	2024-11-22 09:42:48.950952538 -0800
@@ -2425,3 +2425,41 @@ void __init arch_cpu_finalize_init(void)
 	 */
 	mem_encrypt_init();
 }
+
+void check_cpuid_level(unsigned int leaf)
+{
+	unsigned int region = leaf >> 4;
+	int max_leaf;
+
+	/*
+	 * The max leaf in a region is discovered from the first
+	 * leaf. Allow this kind of discovery without checks:
+	 */
+	if (!(leaf & GENMASK(3, 0)))
+		return;
+
+	switch (region) {
+		case 0x0000:
+			max_leaf = boot_cpu_data.cpuid_level;
+			break;
+		case 0x8000:
+			max_leaf = boot_cpu_data.extended_cpuid_level;
+			break;
+		default:
+			/* Only check the basic and extended regions: */
+			return;
+	}
+
+	/*
+	 * Skip checks before ->cpuid_level is populated
+	 * and on CPUs without CPUID support:
+	 */
+	if (!max_leaf)
+		return;
+
+	if (leaf <= max_leaf)
+		return;
+
+	WARN_ONCE(1, "CPUID read leaf 0x%x above max supported leaf: 0x%x",
+		leaf, max_leaf);
+}
_

  reply	other threads:[~2024-11-22 17:46 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-20 19:53 [PATCH 00/11] x86/cpu: Centralize and standardize CPUID leaf naming Dave Hansen
2024-11-20 19:53 ` [PATCH 01/11] x86/cpu: Move MWAIT leaf definition to common header Dave Hansen
2024-11-26  3:20   ` Zhao Liu
2024-11-20 19:53 ` [PATCH 02/11] x86/cpu: Use MWAIT leaf definition Dave Hansen
2024-11-26  3:24   ` Zhao Liu
2024-11-20 19:53 ` [PATCH 03/11] x86/cpu: Remove unnecessary MwAIT leaf checks Dave Hansen
2024-11-20 19:53 ` [PATCH 04/11] x86/acpi: Check MWAIT feature instead of CPUID level Dave Hansen
2024-11-21 15:33   ` Sean Christopherson
2024-11-21 21:46     ` Dave Hansen
2024-11-20 19:53 ` [PATCH 05/11] x86/cpu: Refresh DCA leaf reading code Dave Hansen
2024-11-26  4:11   ` Zhao Liu
2024-11-26  3:55     ` Dave Hansen
2024-11-20 19:53 ` [PATCH 06/11] x86/cpu: Move TSC CPUID leaf definition Dave Hansen
2024-11-26  4:23   ` Zhao Liu
2024-11-20 19:53 ` [PATCH 07/11] x86/tsc: Move away from TSC leaf magic numbers Dave Hansen
2024-11-20 19:53 ` [PATCH 08/11] x86/tsc: Remove CPUID "frequency" " Dave Hansen
2024-11-26  4:37   ` Zhao Liu
2024-11-20 19:53 ` [PATCH 09/11] x86/fpu: Move CPUID leaf definitions to common code Dave Hansen
2024-11-26  4:43   ` Zhao Liu
2024-11-20 19:53 ` [PATCH 10/11] x86/fpu: Remove unnecessary CPUID level check Dave Hansen
2024-11-21 15:45   ` Sean Christopherson
2024-11-22 17:46     ` Dave Hansen [this message]
2024-11-20 19:53 ` [PATCH 11/11] x86/cpu: Make all all CPUID leaf names consistent Dave Hansen
2024-11-20 20:23   ` Dave Jiang
  -- strict thread matches above, loose matches on Subject: below --
2024-10-30 21:33 [PATCH 00/11] x86/cpu: Centralize and standardize CPUID leaf naming Dave Hansen
2024-10-30 21:33 ` [PATCH 10/11] x86/fpu: Remove unnecessary CPUID level check Dave Hansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bb937b3f-e595-4aa8-a6e5-08bdbd4702bf@intel.com \
    --to=dave.hansen@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dave.jiang@intel.com \
    --cc=david.e.box@intel.com \
    --cc=irenic.rajneesh@gmail.com \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox