public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Maciej Wieczor-Retman <m.wieczorretman@pm.me>,
	Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
Cc: tglx@kernel.org, peterz@infradead.org, xin@zytor.com,
	maciej.wieczor-retman@intel.com, babu.moger@amd.com,
	chang.seok.bae@intel.com, sohil.mehta@intel.com,
	dave.hansen@linux.intel.com, jpoimboe@kernel.org,
	elena.reshetova@intel.com, ak@linux.intel.com,
	darwi@linutronix.de, bp@alien8.de, mingo@redhat.com,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v11 3/4] x86/cpu: Do a sanity check on required feature bits
Date: Fri, 27 Mar 2026 18:52:30 -0700	[thread overview]
Message-ID: <d3468280-e7a8-4dc6-b6e2-0499f3e7a4fc@zytor.com> (raw)
In-Reply-To: <acWEtUj8GNV5Kh51@wieczorr-mobl1.localdomain>

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

On 2026-03-26 12:11, Maciej Wieczor-Retman wrote:
> On 2026-03-26 at 12:04:30 -0700, Pawan Gupta wrote:
>> On Thu, Mar 26, 2026 at 06:36:15PM +0000, Maciej Wieczor-Retman wrote:
>>>> Do we need 2 loops? Can this be simplified as below:
>>>>
>>>> static void verify_required_features(const struct cpuinfo_x86 *c)
>>>> {
>>>> 	u32 required_features[NCAPINTS + 1] = REQUIRED_MASK_INIT;
>>>> 	char cap_buf[X86_CAP_BUF_SIZE];
>>>> 	int i, error = 0;
>>>
>>> Isn't this [NCAPINTS + 1] still a problem because for_each_set_bit() works in 64
>>> bit chunks? If NCAPINTS becomes an odd number in the future, the
>>> required_features[] last 32 bits will be uninitialized - REQUIRED_MASK_INIT is
>>> of (NCAPINTS * sizeof(u32)) size. So they might have some bits set and trigger
>>> the pr_warn() below.
>>
>> Isn't a partially initialized array always zeroed out for the uninitialized
>> part?
> 
> Ah okay, my bad. Right, it should be okay then. Thanks!
> 

That being said, I would personally like to see an explicit assignment from
REQUIRED_MASK_INIT into an automatic variable replaced with a memcpy() from a
(possibly static) const array. It might be useful elsewhere, and it would
avoid compilers sometimes creating really ugly code.

One thing that matters here is that these bitmaps are *already* accessed using
bitop operations. Therefore, if this is a problem *here*, then it is a problem
*everywhere*. The simplest way to deal with it is probably to require NCAPINTS
and NBUGINTS to be even, even (pun intended) if that means a temporarily
unused word at the end of the array. That doesn't even require any code
changes, just a statement at the top of cpufeatures.h (see attached patch for
an untested example.)

A more bespoke variant would be to script-generate NCAPINTS and NBUGINTS, but
that might have other problems.

	-hpa

[-- Attachment #2: diff --]
[-- Type: text/plain, Size: 1636 bytes --]

diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
index dbe104df339b..ca9ab8a7a4ee 100644
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -3,18 +3,37 @@
 #define _ASM_X86_CPUFEATURES_H
 
 /*
- * Defines x86 CPU feature bits
+ * Defines x86 CPU feature bits.
+ */
+
+/*
+ * Number of words of features and bugs, respectively.
+ *
+ * These should be even, as these arrays can be accessed by bitmask operations that
+ * use "unsigned long", which is 64 bits on x86-64.
+ *
+ * These must be expressed as decimal constants as they are read
+ * by scripts.
  */
 #define NCAPINTS			22	   /* N 32-bit words worth of info */
 #define NBUGINTS			2	   /* N 32-bit bug flags */
 
+#if (NCAPINTS | NBUGINTS) & 1
+# error "NCAPINTS and NBUGINTS must be even, just increment any odd number by one"
+#endif
+
 /*
- * Note: If the comment begins with a quoted string, that string is used
- * in /proc/cpuinfo instead of the macro name.  Otherwise, this feature
- * bit is not displayed in /proc/cpuinfo at all.
+ * Note: If the comment begins with a quoted string, that string is
+ * displayed in /proc/cpuinfo. Otherwise, this feature bit is not
+ * displayed in /proc/cpuinfo at all.
+ *
+ * This string should be in lower case and match C identifier rules.
  *
  * When adding new features here that depend on other features,
  * please update the table in kernel/cpu/cpuid-deps.c as well.
+ *
+ * As this file is read by scripts, the format of each of these lines
+ * must be strictly followed.
  */
 
 /* Intel-defined CPU features, CPUID level 0x00000001 (EDX), word 0 */

  reply	other threads:[~2026-03-28  1:53 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-20 12:50 [PATCH v11 0/4] x86: Capability bits fix and required bits sanity check Maciej Wieczor-Retman
2026-03-20 12:50 ` [PATCH v11 1/4] x86/cpu: Clear feature bits disabled at compile-time Maciej Wieczor-Retman
2026-03-20 12:50 ` [PATCH v11 2/4] x86/cpu: Check if feature string is non-zero Maciej Wieczor-Retman
2026-03-23 14:24   ` Borislav Petkov
2026-03-23 15:52     ` Maciej Wieczor-Retman
2026-03-23 16:23       ` Borislav Petkov
2026-03-23 16:58         ` Maciej Wieczor-Retman
2026-03-23 17:51           ` Borislav Petkov
2026-03-23 18:11             ` Maciej Wieczor-Retman
2026-03-23 18:15               ` H. Peter Anvin
2026-03-20 12:50 ` [PATCH v11 3/4] x86/cpu: Do a sanity check on required feature bits Maciej Wieczor-Retman
2026-03-21  0:31   ` Pawan Gupta
2026-03-21  5:58     ` Maciej Wieczór-Retman
2026-03-23 18:16       ` Pawan Gupta
2026-03-23 18:33         ` Maciej Wieczor-Retman
2026-03-26 18:36     ` Maciej Wieczor-Retman
2026-03-26 19:04       ` Pawan Gupta
2026-03-26 19:11         ` Maciej Wieczor-Retman
2026-03-28  1:52           ` H. Peter Anvin [this message]
2026-03-28  2:01             ` H. Peter Anvin
2026-03-30  9:47               ` Maciej Wieczor-Retman
2026-03-30 10:09             ` Maciej Wieczor-Retman
2026-03-30 16:01               ` Pawan Gupta
2026-03-30 21:24               ` David Laight
2026-03-31  8:12                 ` Maciej Wieczor-Retman
2026-03-31 13:29             ` Maciej Wieczor-Retman
2026-03-23 16:31   ` Borislav Petkov
2026-03-23 17:05     ` Maciej Wieczor-Retman
2026-03-23 17:55       ` Borislav Petkov
2026-03-23 18:43         ` H. Peter Anvin
2026-03-23 18:43     ` H. Peter Anvin
2026-03-23 19:19       ` Borislav Petkov
2026-03-23 20:24         ` H. Peter Anvin
2026-03-23 20:58           ` Borislav Petkov
2026-03-23 21:40             ` H. Peter Anvin
2026-03-23 21:50               ` Borislav Petkov
2026-03-23 21:56                 ` Borislav Petkov
2026-03-23 22:03                 ` H. Peter Anvin
2026-03-23 22:09                   ` Borislav Petkov
2026-03-24  1:16                     ` H. Peter Anvin
2026-03-20 12:50 ` [PATCH v11 4/4] x86/cpu: Clear feature bits whose dependencies were cleared Maciej Wieczor-Retman
2026-03-23 16:35   ` Borislav Petkov
2026-03-23 17:23     ` Maciej Wieczor-Retman
2026-03-23 17:59       ` Borislav Petkov
2026-03-23 18:18         ` Maciej Wieczor-Retman
2026-03-23 18:57         ` H. Peter Anvin
2026-03-23 19:30           ` Borislav Petkov
2026-03-25  9:33             ` Maciej Wieczor-Retman
2026-03-23 19:33           ` Ahmed S. Darwish

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=d3468280-e7a8-4dc6-b6e2-0499f3e7a4fc@zytor.com \
    --to=hpa@zytor.com \
    --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=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.wieczorretman@pm.me \
    --cc=maciej.wieczor-retman@intel.com \
    --cc=mingo@redhat.com \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=peterz@infradead.org \
    --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