From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C16022EBBA4 for ; Thu, 9 Apr 2026 18:48:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775760518; cv=none; b=kXFZntRJ5pPWB0Y9MKPmCMla3vKcqUOYv65I4fyd4MBv4XuhbJu1ZeyfEANncUv/lNjIG6UV7La8qaK4QnwXPcsQ3PGrwBaVkWIacc5CfbJ0RJX4Z2RNsl1XXuNku/Cx3IvtGc4P0PK6MrJJJhifw7M6lg/tPuYvsUD2kpyt2UU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775760518; c=relaxed/simple; bh=dZ8Hj3auCMI0Wog8o4LpqHsRcACznaDMn5ZtGAjWUhs=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fi6OZ3gc/P+2onGAhwdjMg3ElksMB7UmVRgRmNelxHGt4hg5prDHDyJ4ta6xsRYSbvkhlK0LOW0X/XUb62EPv+wox4OC6rKE6a/qVW9O/F0UQ6hSo9GwuhL+LOg5u/Qcr3LNdu2VG7Sg7X4PprjqPW80E7J7vDrGoEVeoWvc4eA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KIRsN9+U; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KIRsN9+U" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-43cff5dafc3so891096f8f.1 for ; Thu, 09 Apr 2026 11:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775760515; x=1776365315; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=nW+RNy5GxRfTM5rc65EfiX/0HX0owqqDIm3rkAH6v1k=; b=KIRsN9+UsmGqKBnUg0ozK1BVFNzyU23MFzEcTqZJppkQUD5v0Md9ce1bjyOG7qnL4n RV5Uwa6aGCqqTaAMWU8Ccwf5obFatZIz+QS1nEJOImwjNuAuPywKQSiiWk6GtXu0sKNw 1w5R/Ww2PxPwAOOQS7RJTqWerkYlTEz860tKllXuJAJT13zlOMOXjWMhXEukWoy7nGsi /0olLEAUJzi/QGVTDdeVigSNkv7IJtFNblBpl7uUnHZbV7q5qV33XEF5mi0Tx/c2K5Tn ugjZQWlFWaisRpRiX80kqIBN0XpenMt3Wyjc2IpKnTY0pOfxjo7mo7x1HQDJziDG/Pcv LqGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775760515; x=1776365315; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nW+RNy5GxRfTM5rc65EfiX/0HX0owqqDIm3rkAH6v1k=; b=DmCOUlTDC6kqfbbokGuZW0CRwoLH8Q8i45sV05CIvGCK3cywuxQCKNY7msCaPw6ZiA p2/ftw84YM7w5gHY5ENpC29+7FDGHubMfe7/svMOajGFTKtKWnbXXsqiGWbTXOrmPREb 3VTbUJxoEl2qIj+UwGjtipSXD50myTGg96q9yBh7UiW0ZuEtRFx5Cw+8WpDhliW1KrYy TKid6rqzkwTrleE05Cn48oaQqLoBX7TcATW0RcepsFWBBo5P5XWxbkY8+uD50ZKU/O4j LSWHwC9/x5Etc0msQmidqGuSWXvch7cic2aUID7ImOdTypEsb6BZWDd8g4K9cBEgmKL8 1xOw== X-Forwarded-Encrypted: i=1; AJvYcCVbVypsXLM+nuQRil18evFgWoGpoWziqFvWGnm7rHFKQCUuW7/XgDBlfkhd/wiiAYccjTzVhyLt8wFPiHM=@vger.kernel.org X-Gm-Message-State: AOJu0YzzUZX53y+yZdTMqZNqInwhvAlHKXX649sQgv6/AS1/ELiskzJB LdRa9dJKJXiyLpegXotfgo/snKnHb8rmlS0HGuf2kwuiix0WoV6pO6fB X-Gm-Gg: AeBDievZ+6wLZyvZhsk7MNcwKaYThwCUQy9LowndY85jq34CHWjWgZ9+nbcTKYY+HN5 o6Zh8ejHoPWFTZyAeuoM0pMECN8wseT/Og/Rv9sJtN8Y4avekGzzPf9MfL3OjGd0zERixY+ao91 K3cdvvHofZ4K9Nhb+kgw7Wq+uFMH8h1tWpwwu9psF8s4K7aJZ/0JdTGw1WGCLBy5y5C/ChQyig5 A/wOQskfwrOptyQf+yqsJKPbInQv6Ykmrjs2AYNn0M0zoBWH8WQjdfwDhoK6Vh/Anz0kOZUMhr4 89gOIczL9Iy2EIl2zZUWRVBgkuJBS/ulotf8sERB5oMblOS4PH6P1cIInTgRy95domyaWI3qieK 6NJiYmN47hzogPKnxJO/iTvqzsAO8qKQ0i9mhT1WwEo7f4eCYrII1JMFASNnTBj8Ni3HmosOYF6 YFmeHLhbG8yJCf/2bA7aJ8dlJ2DLZDDVQPyDBE3jZNrAKaE+DnD8N3EnnbmSNm+WYd7Sapk7WH4 3M= X-Received: by 2002:a05:6000:18a9:b0:43d:1e22:26c9 with SMTP id ffacd0b85a97d-43d642ea5e6mr281893f8f.42.1775760514848; Thu, 09 Apr 2026 11:48:34 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d63dec3b6sm822393f8f.16.2026.04.09.11.48.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2026 11:48:34 -0700 (PDT) Date: Thu, 9 Apr 2026 19:48:33 +0100 From: David Laight To: Matt Coster Cc: Yury Norov , Rasmus Villemoes , Frank Binns , Alessio Belle , Brajesh Gupta , Alexandru Dadu , , Vincent Mailhol , kernel test robot Subject: Re: [PATCH] bitfield: Fix FIELD_PREP_CONST() with __GENMASK_ULL() on gcc<14 Message-ID: <20260409194833.3214139d@pumpkin> In-Reply-To: <20260409-field-prep-fix-v1-1-f0e9ae64f63c@imgtec.com> References: <20260409-field-prep-fix-v1-1-f0e9ae64f63c@imgtec.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) 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=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 9 Apr 2026 15:57:53 +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); > | ^ > > 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*()"). Can you give the actual code that is failing. I can only find the old version - that is pretty horrid itself. It really isn't necessary to use GENMASK_ULL(15, 0) to get 0xffffu. I've bumped into that file before. It gave a false positive for FIELD_GET() returning a constant value for non-constant input - which would be a nice check for invalid usage. Someone went over the top on getting the pre-processor to bloat the intermediate file by using the longest way possible to perform the simplest of code patterns. I suspect they've made it worse. > > 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. Using a completely different expression instead of ffs() and shifts. For constants it doesn't matter what you do. Multiply/divide by mask & (~mask + 1) will be the same. (I think it is used lower down the file.) But passing the output of GENMASK() (any variant) into FIELD_PREP() (any current variant) is a good way to generate pre-processor output lines that are many kb long. Oh, in my experiments, the value passed to ffsll() must be unsigned and have the top bit set. David > > 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. > + * > + * 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 > + > #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)) \ > ) > > /** > > --- > base-commit: b20a9b5f9c4baeae0b2e143046b195b910c59714 > change-id: 20260324-field-prep-fix-fdfc4eb68156 > >