From nobody Thu Apr 2 17:21:42 2026 Received: from mail-106119.protonmail.ch (mail-106119.protonmail.ch [79.135.106.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4665F2C11D7 for ; Fri, 27 Mar 2026 15:11:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.119 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774624268; cv=none; b=G/jNyjn9SWR58WLMYflv8GCN36qKgf1tFW0mmmyWWdlXtIpND9GYprPGriJDwdSsN9ebqItNcE984yJKUa+ZwvVb69RNf6cNJCQI0/kD5Gp5eW9/g4BKEHhni4atOkhPjh3mIEz2uCXQ3xpwK6AH59VEiqvEpnkuHnwO7wbpoOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774624268; c=relaxed/simple; bh=yxxlwmCEyeX6nCx3350jDSoRypoG2h8WFYe+DaBBnoc=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iMfO7ytXPgBw0KV9YuxqL1+5w7lmRVhElyfZZXTMpDVpte1Z+wH5YJIxbm0XNNKmy+hzn4wlIOFIq8zsYCuMI4yHGHo7ACWnLIHcGLGbSERVsOw0mzkWqL4bKXA2wuAaS5ZCV9Z9GPg2Coz8IimG+k7XY7YOZXXJ3mJhYIyJhEc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=JnR2pR7U; arc=none smtp.client-ip=79.135.106.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="JnR2pR7U" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1774624258; x=1774883458; bh=WYXAX2wFgyb6ZzOJJJ27vgNubz05pDAJWxgS76VxSsk=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=JnR2pR7UZxYNbh4HEOofYqpp+SO9yFB9qv3hQ5sKvqg8bf5cyFFUd7jWQqiESUF8J 0l+/QLYfKj7hEbP8hAnT91kqzOmDZ6+7I+euAzHrEvxNNj7cP7gvRcvPbVU4n9BKFh fq0xHTtVfd+iYsMcnhUP7v1X9Q3lVmA6dsWHO1xAtBQurG1HH172aUg6ka6ZsOjxfE 3NPoM5KaVGS0wYjhNGXSeZqPqr5J2zOb1BFCl/4LODQy19feaNydhxhY7y9PCwbmgc dp3j7uU/AHCMZIjuydXTW5siP9tKQizmHwZ50DgrscMNwpQtnpWjFPRQUaWLFCUcsn ca9iz2uzdWe0w== Date: Fri, 27 Mar 2026 15:10:52 +0000 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 From: Maciej Wieczor-Retman 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 Message-ID: In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: e44ae0ec68d30d884c38291c2d34a592687cf43e Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman 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 Reviewed-by: Sohil Mehta --- 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[] =3D { static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) { const struct cpuid_dependent_feature *df; + char feature_buf[X86_NAMELESS_FEAT_BUFLEN]; =20 for (df =3D cpuid_dependent_features; df->feature; df++) { =20 @@ -700,7 +701,7 @@ static void filter_cpuid_features(struct cpuinfo_x86 *c= , bool warn) continue; =20 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); } } =20 @@ -1637,6 +1638,7 @@ static inline bool parse_set_clear_cpuid(char *arg, b= ool set) =20 while (arg) { bool found __maybe_unused =3D false; + char name_buf[X86_NAMELESS_FEAT_BUFLEN]; unsigned int bit; =20 opt =3D 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)); =20 taint++; } @@ -1983,6 +1982,27 @@ static void generic_identify(struct cpuinfo_x86 *c) #endif } =20 +/* + * 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 =3D bit >> 5; + const char *name =3D NULL; + + if (word < NCAPINTS) + name =3D 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 =3D=3D SPECTRE_V2_EIBRS_LFENCE; } =20 +#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-d= eps.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 #include =20 +#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); } =20 -/* - * 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; =20 for (d =3D cpuid_deps; d->feature; d++) { --=20 2.53.0