mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Maciej Wieczor-Retman <m.wieczorretman@pm.me>
To: bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com,
	xin@zytor.com, chang.seok.bae@intel.com, mingo@redhat.com,
	elena.reshetova@intel.com, maciej.wieczor-retman@intel.com,
	babu.moger@amd.com, sohil.mehta@intel.com,
	pawan.kumar.gupta@linux.intel.com, pmladek@suse.com,
	nik.borisov@suse.com, ptesarik@suse.com, darwi@linutronix.de,
	tglx@kernel.org, peterz@infradead.org, jpoimboe@kernel.org,
	ak@linux.intel.com
Cc: linux-kernel@vger.kernel.org, x86@kernel.org, m.wieczorretman@pm.me
Subject: [PATCH v12 2/4] x86/cpu: Check if feature string is non-zero
Date: Fri, 27 Mar 2026 15:10:52 +0000	[thread overview]
Message-ID: <ab741c4805c43c9bc8774dd935d62b61d03d945b.1774623092.git.m.wieczorretman@pm.me> (raw)
In-Reply-To: <cover.1774623092.git.m.wieczorretman@pm.me>

From: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>

In filter_cpuid_features(), x86_cap_flags[] is accessed based on the
features from cpuid_dependent_features[]. However it's not verified
whether the string in x86_cap_flags[] is non-zero which could lead to
unwanted output. While currently it's not a problem (all the features in
the checked list have non-zero strings) it may cause a failure if an
entry is added that doesn't have a feature string defined.

There are two other places that open code a similar operation of
printing out the feature string or if it's not present, the word:bit
feature format.

Reuse x86_feature_name() as a common helper to validate the presence of
feature strings in filter_cpuid_features() as well as unify similar open
coded cases.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
---
Changelog v12:
- Remove Reviewed-by tags.
- Redo the patch message so it points out that the problem is not here
  now but may happen.
- Rename
	- X86_CAP_BUF_SIZE -> X86_NAMELSS_FEAT_BUFLEN
	- x86_cap_name() -> x86_feature_name()
- Move exporting the header to arch/x86/kernel/cpu/cpu.h
- Remove x86_bug_flags[] handling from x86_feature_name() since it
  doesn't interact with the array anywhere.

Changelog v11:
- Remove one extra tab after X86_CAP_BUF_SIZE.
- Add Reviewed-by tags from Sohil and Pawan.

Changelog v10:
- Reword the patch message a bit.
- Move x86_cap_name() declaration and X86_CAP_BUF_SIZE define to
  asm/cpufeature.h.
- Don't include asm/cpu.h in cpuid-deps.c
- Extend the comment above x86_cap_name to include information on buffer
  size that needs to be prepared before calling the function.
- Remove the likely(), unlikely() calls since this is not a hot path.

Changelog v9:
- 16 -> X86_CAP_BUF_SIZE.
- Add comment to the x86_cap_name().

Changelog v8:
- Move x86_cap_name() declaration from linux/cpu.h to the arch/cpu.h.
  Include arch/cpu.h in the cpuid-deps.c file instead of linux/cpu.h.

Changelog v7:
- sizeof(buf) -> 16
- Rebase onto 7.01-rc1.

Changelog v6:
- Remove parts of the patch message that are redundant and just copy
  what's visible in the diff.
- Redo the helper to use an external char buffer instead of a local
  static string.

 arch/x86/kernel/cpu/common.c     | 30 +++++++++++++++++++++++++-----
 arch/x86/kernel/cpu/cpu.h        |  4 ++++
 arch/x86/kernel/cpu/cpuid-deps.c | 19 +++----------------
 3 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 76339e988304..7cfd124b3fbf 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -678,6 +678,7 @@ cpuid_dependent_features[] = {
 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
 {
 	const struct cpuid_dependent_feature *df;
+	char feature_buf[X86_NAMELESS_FEAT_BUFLEN];
 
 	for (df = cpuid_dependent_features; df->feature; df++) {
 
@@ -700,7 +701,7 @@ static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
 			continue;
 
 		pr_warn("CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
-			x86_cap_flags[df->feature], df->level);
+			x86_feature_name(df->feature, feature_buf), df->level);
 	}
 }
 
@@ -1637,6 +1638,7 @@ static inline bool parse_set_clear_cpuid(char *arg, bool set)
 
 	while (arg) {
 		bool found __maybe_unused = false;
+		char name_buf[X86_NAMELESS_FEAT_BUFLEN];
 		unsigned int bit;
 
 		opt = strsep(&arg, ",");
@@ -1657,10 +1659,7 @@ static inline bool parse_set_clear_cpuid(char *arg, bool set)
 					setup_clear_cpu_cap(bit);
 				}
 				/* empty-string, i.e., ""-defined feature flags */
-				if (!x86_cap_flags[bit])
-					pr_cont(" %d:%d\n", bit >> 5, bit & 31);
-				else
-					pr_cont(" %s\n", x86_cap_flags[bit]);
+				pr_cont(" %s\n", x86_feature_name(bit, name_buf));
 
 				taint++;
 			}
@@ -1983,6 +1982,27 @@ static void generic_identify(struct cpuinfo_x86 *c)
 #endif
 }
 
+/*
+ * Return the feature "name" if available, otherwise return the
+ * X86_FEATURE_* numerals to make it easier to identify the feature.
+ * Callers of this function need to pass a char * buffer of size
+ * X86_NAMELESS_FEAT_BUFLEN.
+ */
+const char *x86_feature_name(unsigned int bit, char *buf)
+{
+	unsigned int word = bit >> 5;
+	const char *name = NULL;
+
+	if (word < NCAPINTS)
+		name = x86_cap_flags[bit];
+
+	if (name)
+		return name;
+
+	snprintf(buf, X86_NAMELESS_FEAT_BUFLEN, "%u:%u", word, bit & 31);
+	return buf;
+}
+
 /*
  * This does the hard work of actually picking apart the CPU stuff...
  */
diff --git a/arch/x86/kernel/cpu/cpu.h b/arch/x86/kernel/cpu/cpu.h
index 5c7a3a71191a..0ce29ee56442 100644
--- a/arch/x86/kernel/cpu/cpu.h
+++ b/arch/x86/kernel/cpu/cpu.h
@@ -91,4 +91,8 @@ static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode)
 	       mode == SPECTRE_V2_EIBRS_LFENCE;
 }
 
+#define X86_NAMELESS_FEAT_BUFLEN	16
+
+const char *x86_feature_name(unsigned int bit, char *buf);
+
 #endif /* ARCH_X86_CPU_H */
diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index 146f6f8b0650..4354c0dd6d66 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -4,6 +4,8 @@
 #include <linux/module.h>
 #include <asm/cpufeature.h>
 
+#include "cpu.h"
+
 struct cpuid_dep {
 	unsigned int	feature;
 	unsigned int	depends;
@@ -156,24 +158,9 @@ void setup_clear_cpu_cap(unsigned int feature)
 	do_clear_cpu_cap(NULL, feature);
 }
 
-/*
- * Return the feature "name" if available, otherwise return
- * the X86_FEATURE_* numerals to make it easier to identify
- * the feature.
- */
-static const char *x86_feature_name(unsigned int feature, char *buf)
-{
-	if (x86_cap_flags[feature])
-		return x86_cap_flags[feature];
-
-	snprintf(buf, 16, "%d*32+%2d", feature / 32, feature % 32);
-
-	return buf;
-}
-
 void check_cpufeature_deps(struct cpuinfo_x86 *c)
 {
-	char feature_buf[16], depends_buf[16];
+	char feature_buf[X86_NAMELESS_FEAT_BUFLEN], depends_buf[X86_NAMELESS_FEAT_BUFLEN];
 	const struct cpuid_dep *d;
 
 	for (d = cpuid_deps; d->feature; d++) {
-- 
2.53.0



  parent reply	other threads:[~2026-03-27 15:11 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-27 15:10 [PATCH v12 0/4] x86: Capability bits fix and required bits sanity check Maciej Wieczor-Retman
2026-03-27 15:10 ` [PATCH v12 1/4] x86/cpu: Clear feature bits disabled at compile-time Maciej Wieczor-Retman
2026-03-27 15:10 ` Maciej Wieczor-Retman [this message]
2026-03-27 17:50   ` [PATCH v12 2/4] x86/cpu: Check if feature string is non-zero Sohil Mehta
2026-03-27 21:28     ` Maciej Wieczor-Retman
2026-03-30 20:00   ` Borislav Petkov
2026-03-30 20:42     ` Maciej Wieczor-Retman
2026-03-30 21:15       ` Borislav Petkov
2026-03-30 21:44         ` H. Peter Anvin
2026-03-30 22:59           ` Borislav Petkov
2026-03-30 23:42             ` H. Peter Anvin
2026-03-31  2:15               ` H. Peter Anvin
2026-03-31 13:27               ` Borislav Petkov
2026-03-27 15:10 ` [PATCH v12 3/4] x86/cpu: Do a sanity check on required feature bits Maciej Wieczor-Retman
2026-03-27 17:10   ` Pawan Gupta
2026-03-27 17:22     ` Maciej Wieczor-Retman
2026-03-27 15:11 ` [PATCH v12 4/4] x86/cpu: Clear feature bits whose dependencies were cleared Maciej Wieczor-Retman
2026-03-28  2:22   ` H. Peter Anvin
2026-03-30 10:40     ` Maciej Wieczor-Retman
2026-03-30 21:41     ` Ahmed S. Darwish
2026-03-31 11:41       ` Maciej Wieczor-Retman
2026-04-01 15:16       ` Maciej Wieczor-Retman
2026-04-13 14:42         ` Ahmed S. Darwish
2026-03-30  6:11 ` [PATCH v12 0/4] x86: Capability bits fix and required bits sanity check Andi Kleen
2026-03-30  8:52   ` Maciej Wieczor-Retman
2026-03-30 23:40   ` H. Peter Anvin
2026-03-31 14:02 ` Borislav Petkov
2026-03-31 14:27   ` Maciej Wieczor-Retman
2026-03-31 14:46     ` Borislav Petkov

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=ab741c4805c43c9bc8774dd935d62b61d03d945b.1774623092.git.m.wieczorretman@pm.me \
    --to=m.wieczorretman@pm.me \
    --cc=ak@linux.intel.com \
    --cc=babu.moger@amd.com \
    --cc=bp@alien8.de \
    --cc=chang.seok.bae@intel.com \
    --cc=darwi@linutronix.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=elena.reshetova@intel.com \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maciej.wieczor-retman@intel.com \
    --cc=mingo@redhat.com \
    --cc=nik.borisov@suse.com \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=ptesarik@suse.com \
    --cc=sohil.mehta@intel.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    /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