From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011007.outbound.protection.outlook.com [52.101.52.7]) (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 1D3B12D3EC1 for ; Thu, 9 Apr 2026 17:46:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.52.7 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775756790; cv=fail; b=VcaISOebhClPYXg58oCJY0glh342kja78ISJWaH8X+dwyZTjoVgpIPNnoUYYtjmvHF1JXm7Z/8icTn3LkairNS4yddOv0UnSXgGd4kkhQgTMXSsHo9k6jj3m04Abjwu4cijr2mgqwJLcIEy7Cr97hkKETw2Rj3JR2lX4cTJOZQI= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775756790; c=relaxed/simple; bh=sDvGG8QZDA9u/UexlhkKwtaUA2A5IXSIDQmo77HVDjs=; h=Date:From:To:Cc:Subject:Message-ID:References:Content-Type: Content-Disposition:In-Reply-To:MIME-Version; b=UGvYBP56LnTIFByVTB0Yy+8hjXNq/sb3o7+xyYJCa+B7lo0xTh8TRFDe6MrUdYDTgJ61+QzKi3ly7vj8m6GpU85GIHvvkX4hiNVV0dhfyyA2tY6GasoeH4IKrh2+8wuLJeJgLCkqLoCqAFOY4dDDmXi11MhM+9ECniv1IVKkSdg= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=MeoG8YwI; arc=fail smtp.client-ip=52.101.52.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="MeoG8YwI" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=k5tTRUKltjIYykWvuneO98oBMjqELBSlh5tATlKD5BQX6jwxusxMhUkFb9JWZ5wfVyLVohOObw94E+oRXv+gvcuPDCB99ZpTeQwN35beLDoBtsEPDWJMzFMruJQOXAVKNPGpcOYouPdsP3Ga3nm5x52D4o+Dnj34X3KK/WbXxKlGqlkwEgQNzReUeOike01H4jh+r/lH5vKLTrrV1AtRIkN60CT1dTtJtKMlJ4Ps00jW42MiRH3MRGVsv4dAHmtYPLeswn2kh8Sd49u8Z7DzU4b1dF1cW4xGXC1+wArzWkhoitAJ0mv50KjUS6I2BJs4S5HsCzAfRMxWrl3gN5o93Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vus/by3YDNKFzeASxrF9c5nLE7O7vFvKItHlEi2kHGo=; b=VnaBHgTLCvWHA1mMnBQQf896oS8Ba2tOQ++44RP3feyNwAzx2CiqkduSaIF47DIqwnrVMGDZk7WBdSZkF/9EQR5I6Z6/xvVcCBDw2vad9GTMSfmJyORLd2VL8st3Kg8rzOHZAThNdf7zl52uw5DfS8ZWLvm2I+EHsMoCv4fH6pto67yxyed8/9WSbRo2CVDnYFME5Wi6FgbYEPhk43hUQ2RmddskZ11Ezs25JuBdjsLevWwR2T5EZ90OA4gQ6FuWiQa6hm4IZ6tmNEhYjTsWihpB4B5VGe7FnHIkO4Lu8gky+imxKpOfwFfnSuYxwMjD915rPxoGA8T7JIEW/9r3bQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vus/by3YDNKFzeASxrF9c5nLE7O7vFvKItHlEi2kHGo=; b=MeoG8YwIW9fzwgb6q+ZO6thsfci9uVDnTqHoNxq/gnfAiCfDLexrOYIjnZa1yqSp768VITiRCdbRwuWgh/0urwgOwW4iYI3p0HqTuAHEdbjPquYpj56jhSv0Cqrm5EFBeoqQxOUvaiFl1TwJNd6rN2BxAfPxOq8mAIcB3kf1/hprfLcjhIH6D0uY37M3euRPMfhVN18QLoEXbpjYDw8dOLgMI7Iw8UNsAFD/E6ZAerQ5zxU5013m5dSHmNWndVgom3fTXMjLlstp+TtJ8AKlZF8dO4OOWJxt+H5ZZlLu6XNTnBhSgYymtt7K8Px/Zf4gY/Z/PlRctyJU1g92x0ajUw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CY8PR12MB8300.namprd12.prod.outlook.com (2603:10b6:930:7d::16) by CH1PPF2C6B99E0C.namprd12.prod.outlook.com (2603:10b6:61f:fc00::609) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9769.11; Thu, 9 Apr 2026 17:46:22 +0000 Received: from CY8PR12MB8300.namprd12.prod.outlook.com ([fe80::ce75:8187:3ac3:c5de]) by CY8PR12MB8300.namprd12.prod.outlook.com ([fe80::ce75:8187:3ac3:c5de%3]) with mapi id 15.20.9769.018; Thu, 9 Apr 2026 17:46:22 +0000 Date: Thu, 9 Apr 2026 13:46:20 -0400 From: Yury Norov To: Matt Coster Cc: Yury Norov , Rasmus Villemoes , Frank Binns , Alessio Belle , Brajesh Gupta , Alexandru Dadu , linux-kernel@vger.kernel.org, Vincent Mailhol , kernel test robot Subject: Re: [PATCH] bitfield: Fix FIELD_PREP_CONST() with __GENMASK_ULL() on gcc<14 Message-ID: References: <20260409-field-prep-fix-v1-1-f0e9ae64f63c@imgtec.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260409-field-prep-fix-v1-1-f0e9ae64f63c@imgtec.com> X-ClientProxiedBy: BN9PR03CA0683.namprd03.prod.outlook.com (2603:10b6:408:10e::28) To CY8PR12MB8300.namprd12.prod.outlook.com (2603:10b6:930:7d::16) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR12MB8300:EE_|CH1PPF2C6B99E0C:EE_ X-MS-Office365-Filtering-Correlation-Id: de3c95e0-06e7-4b79-bfc9-08de965fe9aa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016|10070799003|56012099003|22082099003|18002099003|13003099007; X-Microsoft-Antispam-Message-Info: dJUlR9YazuhCUgbhYjJhNcOO/5WyzwDcS7kcQMysCiGfA95thwRT63TDSoXNelNPmpJljnrgBvwSmZwNmyXyiz6f4RPxgYGu8QyOusOVrUPcEMaSxRYQ4IwlTOPRNi0/fN+7taiFa/8k88IU7neqj8V5jJau9qqwnbESsvSIUleqz/pz3Qfw3ded7cZe10sVYd/hliTLhPrBdbvEyFhaWVy2BJylm8QIzC6ZlQNG0aQIk/0UspTWPqS75nRA6SKsctMrw3BlU1r3bEMF7X1G8SvMEpFg53hhWIRLotfxxHnq6t+Wxq+AAABGLk1ONg2JHXNKwQYHZ+Z0gyzFfLf7Rflyb2UrW19sBHIjCwjH+drVJCtgkVmzEamEJveVWIS0y/2BeY4VQJpCVZpkeiIowOyhTIb+mjNeNlSCdmf2hbJAl1X8Nfo3GnfseEgd3xRTOyMEcQtD+3dO0fRokAnGGZCBoxz/cPC8rchfD0eVjrNFdfGVOQLWKURtxjztXUnUvTyIP3WsDHT/A0vxSwHb+ABCr1JC6yupIlo/XloRitGJPczFPcpi+Kb9xK6ATHnVFwwS7yyovbZEdd0mqm0k1pn/9UTyEGDXuymauDqz9/Gs7Y9ZHsKtw2k0zvmMX4l5LOpeukPgnpTuwwDF4qvnh5mgVOOeDYMbxNbVNld1A7Gr65hH0YfJ4luGdBVijkYETPlCQek4lSiWgdJDdJt8j8poQAvLGB/GgKFoyiga6R4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY8PR12MB8300.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(366016)(10070799003)(56012099003)(22082099003)(18002099003)(13003099007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Hcmu4ecdS3nEL/u65Ohvf91dJHDgN/1mfgQ4QIzZKWG5gBhi+O0py8qQN76W?= =?us-ascii?Q?fWG97+S94O1dbvB+uWe/C56iEMIpotkfeEmohbmsHUKDfpM/vcSZ7tx6Zy8s?= =?us-ascii?Q?+Fh8FXIyROD5k3gZmkpHjYzuxY+CR+mGn7aGIQUPiwBBIQqbcw2Zf2k3jblC?= =?us-ascii?Q?x/D073xbOeLckffrBGjKp/QkydYBLKmLuWMacYEVkKJpr36d92+55CoZR6PB?= =?us-ascii?Q?C5zktDg8Dok1X0g/sXXQoQPG9zr9mrT/K/Dm7WNJs2VO0FLxVn3fM87p8jRH?= =?us-ascii?Q?PD07LIS9n/w0nHVyMVEIzOH0CA3bHr8O9B8PaaJpgNNbIAVJCC0RkC/IJ5F1?= =?us-ascii?Q?1pa0a/p6hb6VIGuFPWySYAhOK95ypq77y9qsY8EbdbmpJcWkXdVRRqbgDlxv?= =?us-asc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u9j22Tnn3cXSf?= =?us-ascii?Q?XyZRt9OB7ZswVM0NH0jHgxV15EOW154U0JL+HCS7akg8I7qUSSMV7jtB87p5?= =?us-ascii?Q?XpzPR+q1ov9eyw5bRKbdEg1ohVXYIUQO+5qiFI6pKJ4yifMIc9f0KcTia6FL?= =?us-ascii?Q?cybHVq2HALd/l+kT+vtHcAGu8h14VPdG+pyOEyU6GCJJo1ikSNxUd2CnLhKj?= =?us-ascii?Q?S1OWLl6OntbLLuGDditC42pv9QYNJ0YSVJv/DpVpmFdNFO2tgi7hIIqMz2LT?= =?us-ascii?Q?JdSexInpjozv8whEs6BA1qPNtZmMojp5W+HVduNzpyN0C9Lb3EVFiS9DcPEa?= =?us-ascii?Q?4kxocHAkfAS5NM37D1Ocqgq5BAmq3/LhSeuJWpjGket1V74iOT/FBSgk08Q6?= =?us-ascii?Q?W4Vn5e2R6z2fZVcI0ATl40Aq4jegrEQbUoIkJK3NiXIc3CZzQpTZbnClQc3S?= =?us-ascii?Q?pcMq8x0DEheRbQepltTpX9lhsq/YtZq/oPv6C3Dv2z8bCJNHuXWz?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: de3c95e0-06e7-4b79-bfc9-08de965fe9aa X-MS-Exchange-CrossTenant-AuthSource: CY8PR12MB8300.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2026 17:46:22.2014 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: p4eP7rqptGDJ2MrEuFwVl4HXfP9xxVPopiKPCP9MP6fN/w/ZyEmE9KXxVjsSti1FWwnRPLOjb9tLFrCwbZ0XZQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH1PPF2C6B99E0C On Thu, Apr 09, 2026 at 03:57:53PM +0100, Matt Coster wrote: > There is a bug in gcc<14[1] that causes the following minimal example to > not be treated as a constant: > > int main() { > sizeof(struct { > int t : !(__builtin_ffsll(~0ULL) + 1 < 0); > }); > } > > test.c: In function 'main': > test.c:3:21: error: bit-field 't' width not an integer constant > 3 | int t : !(__builtin_ffsll(~0ULL) + 1 < 0); > | ^ > Hi Matt, Thanks for digging this! So for me even int t : __builtin_ffsll(~1ul); doesn't work. But '~1' or '1ull' works well. My GCC is 13.3.0. Clearly a compiler bug. > The result of this bug is a bizarre interaction between FIELD_PREP_CONST() > and __GENMASK_ULL(). Note that this does not occur with GENMASK_ULL() since > that has not been based on the UAPI variant since commit 104ea1c84b91 > ("bits: unify the non-asm GENMASK*()"). > > The underlying compiler bug has been fixed in gcc since 14.1.0, but the fix > appears to have been incidental in a larger change[2]. > > [1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124699 > [2]: https://gcc.gnu.org/cgit/gcc/commit/?id=0d00385eaf72ccacff17935b0d214a26773e095f > > Reported-by: kernel test robot > Closes: https://lore.kernel.org/oe-kbuild-all/202603222211.A2XiR1YU-lkp@intel.com/ > Signed-off-by: Matt Coster > --- > Some below-the-dash thoughts: > > This is the most minimal workaround I could find. I'm not sure what a > "real" fix would look like here so I'm open to suggestions. > > The reproduction case is amazing because changing almost any subtle > detail of it will result in the expression correctly being parsed as > constant (even raising the construct to file scope; the use of > BUILD_BUG_ON_ZERO() is part of the bizarre confluence required to > trigger the bug). > > The complexity of the associated change in gcc makes it difficult to > trace what actually changed to fix the underlying bug. If I had more > time, I'd have dug in further and (tried to) come up with an answer. > > In reality, the (long long) cast could probably just be univerally > applied without the conditional compilation. My only concern with that > approach is that it risks turning the workaround into an arcane > incantation whose meaning will eventually have been lost to time. > --- > include/linux/bitfield.h | 30 +++++++++++++++++++++++++++--- > 1 file changed, 27 insertions(+), 3 deletions(-) > > diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h > index 54aeeef1f0ec7..12f5c5a3c8d72 100644 > --- a/include/linux/bitfield.h > +++ b/include/linux/bitfield.h > @@ -46,6 +46,30 @@ > > #define __bf_shf(x) (__builtin_ffsll(x) - 1) > > +#if defined(GCC_VERSION) && (GCC_VERSION < 140000) > +/* > + * This workaround is required for gcc<14. The issue is an interaction between > + * FIELD_PREP_CONST() and __GENMASK_ULL() that can be boiled down this MCVE, > + * which fails to compile as GCC doesn't recognize the expression as constant: > + * > + * int main() { > + * sizeof(struct { > + * int t : !(__builtin_ffsll(~0ULL) + 1 < 0); > + * }); > + * } > + * > + * The underlying issue was inadvertently "fixed" (or perhaps sidestepped) in > + * commit 0d00385eaf7 ("wide-int: Allow up to 16320 bits wide_int and change > + * widest_int precision to 32640 bits [PR102989]"), which first appeared in > + * GCC 14.1. So GCC_VERSION should be < 140100 I guess? > + * > + * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124699 > + */ > +#define __const_bf_shf(x) __bf_shf((long long)(x)) > +#else > +#define __const_bf_shf(x) __bf_shf(x) > +#endif The 'const' part looks misleading. There's really nothing const in there. Can you just fix the __bf_shf() instead? > #define __scalar_type_to_unsigned_cases(type) \ > unsigned type: (unsigned type)0, \ > signed type: (unsigned type)0 > @@ -157,11 +181,11 @@ > /* mask must be non-zero */ \ > BUILD_BUG_ON_ZERO((_mask) == 0) + \ > /* check if value fits */ \ > - BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \ > + BUILD_BUG_ON_ZERO(~((_mask) >> __const_bf_shf(_mask)) & (_val)) + \ > /* check if mask is contiguous */ \ > - __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) + \ > + __BF_CHECK_POW2((_mask) + (1ULL << __const_bf_shf(_mask))) + \ > /* and create the value */ \ > - (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)) \ > + (((typeof(_mask))(_val) << __const_bf_shf(_mask)) & (_mask)) \ > ) Now you fix FIELD_PREP_CONST() only, but there might be other potential victims, so fixing __bf_shf() would help them all. Thanks, Yury > /** > > --- > base-commit: b20a9b5f9c4baeae0b2e143046b195b910c59714 > change-id: 20260324-field-prep-fix-fdfc4eb68156