From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-24417.protonmail.ch (mail-24417.protonmail.ch [109.224.244.17]) (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 631603E6DE7 for ; Tue, 31 Mar 2026 13:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774963768; cv=none; b=XwOy+QvxtmXZM4NdUDFyAn1fOnedm3PUB6M7OY3YmRszGyJdCpaHMeV1vrG4KijX4SKyxegLviPXjAzfmkdP/yiMzuM50OmIlvC4s5mmzZ5zAcOX9tTPKpBkwBExOJlhCD1+RL9WIvSlzQldv0hkYQkUiKS/QHmvT1+wl/eCX6Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774963768; c=relaxed/simple; bh=wjAkZMn1khQdGFkfoeIZN5Q6GyEx7amj12QQJnL2eH8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ul4ZsR7k5oW5MzWDD6pU9dnFYJKZyrlagl1dHKxBNHobRnB17qlWeA+iU/vkwK0THuEGNDEtAOJzpT3qVy6GVjelpS2+NrCrRynO7kV6AhZyjb1cgyl0vZQg8sa+wUBSx9Igtmtge6MaAP6jE/W6OrJzAh8gfhsreE8FumOw/to= 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=RtqO69VO; arc=none smtp.client-ip=109.224.244.17 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="RtqO69VO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1774963762; x=1775222962; bh=DerawIpLxxqJ7aOZs1nhMPKqlydIWbDkXX0szLy8Bn0=; 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=RtqO69VOiWgzXgAOHqD1aPpNwfMz3yLsiW92stD6mynqg7DOuJe/NrbKVShoPUSt6 ga9vHpU+QTJqQQGVSOONVtLIq4IgsPq+PXo7xmtel9CbvMeE1i+UkiPhQd510kRWsn du2Xw8Jk+JroowfU7EJNBnj09Pg6yODy3yQGAwqKqy2e7QHqLsNBjtfZLMqt6ED0kd +GkU71PvvjoMv+DmzOWcBr/jjTbFQMzbE0D8C+LST+ybG9xHeUzCwoplUOeDo99gJn BdPehhHVpvoGeVYqTPvYR4wXBX2H7d9Nz6P/scFHnxQiOi68y5jmYhNz50lBvRanOH 9SDAh9nCMhdLQ== Date: Tue, 31 Mar 2026 13:29:17 +0000 To: "H. Peter Anvin" From: Maciej Wieczor-Retman Cc: Pawan Gupta , 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 Message-ID: In-Reply-To: References: <7312c9f2feab8aea4612ed9a1841e8c22f5f69b1.1774008873.git.m.wieczorretman@pm.me> <20260321003015.4i7wrqmaunbljguw@desk> <20260326190430.i4dt2dxqpfzwlhcc@desk> Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 4b3d1e082ac22b2c59d83a21486f25b759acd2f4 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 2026-03-27 at 18:52:30 -0700, H. Peter Anvin wrote: >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) >>>>> { >>>>> =09u32 required_features[NCAPINTS + 1] =3D REQUIRED_MASK_INIT; >>>>> =09char cap_buf[X86_CAP_BUF_SIZE]; >>>>> =09int i, error =3D 0; >>>> >>>> Isn't this [NCAPINTS + 1] still a problem because for_each_set_bit() w= orks 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 uninitial= ized >>> part? >>=20 >> Ah okay, my bad. Right, it should be okay then. Thanks! >>=20 > >That being said, I would personally like to see an explicit assignment fro= m >REQUIRED_MASK_INIT into an automatic variable replaced with a memcpy() fro= m a >(possibly static) const array. It might be useful elsewhere, and it would >avoid compilers sometimes creating really ugly code. Did you have something like the following in mind? I also noticed the alignment issue is resolved in cpu_caps_cleared[] by add= ing the __aligned(sizeof(unsigned long)) to their declaration. With this I assu= me the cpu_caps_required[] I added would also be fine to get casted as unsigne= d long? arch/x86/kernel/cpu/common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 8d024e0ecaea..68de2d5f8a73 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -739,6 +739,8 @@ static const char *table_lookup_model(struct cpuinfo_x8= 6 *c) __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long= )) =3D DISABLED_MASK_INIT; __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long)); +static const __u32 cpu_caps_required[NCAPINTS] __aligned(sizeof(unsigned l= ong)) =3D + REQUIRED_MASK_INIT; #ifdef CONFIG_X86_32 /* The 32-bit entry code needs to find cpu_entry_area. */ @@ -2011,10 +2013,13 @@ const char *x86_feature_name(unsigned int bit, char= *buf) */ static void verify_required_features(const struct cpuinfo_x86 *c) { - u32 required_features[NCAPINTS + 1] =3D REQUIRED_MASK_INIT; + DECLARE_BITMAP(required_features, NCAPINTS * 32); char cap_buf[X86_NAMELESS_FEAT_BUFLEN]; int i, error =3D 0; + memset(required_features, 0, sizeof(required_features)); + memcpy(required_features, cpu_caps_required, NCAPINTS * sizeof(u32)= ); + for_each_set_bit(i, (unsigned long *)required_features, NCAPINTS * = 32) { if (test_bit(i, (unsigned long *)c->x86_capability)) continue; -- 2.53.0 --=20 Kind regards Maciej Wiecz=C3=B3r-Retman