Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2665558lqo; Mon, 20 May 2024 12:30:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXyGyfwKLVsBwNQkVYj+CIB4c0D5idX6qU6p10b6y45tIU3IbAJaWo0xYM9z4w7w7bLr375glJyx3xHecsRkHnenVD/Z5l+1hA9uW8E2A== X-Google-Smtp-Source: AGHT+IFUf25oXklBeWu3d37RusZwhzif3stVVvAajpfI/nM/o4wiiFceNaK89MinKUAQzeSCZokj X-Received: by 2002:a17:906:4a95:b0:a5a:1562:5184 with SMTP id a640c23a62f3a-a5a2d53af6amr3009095566b.3.1716233407344; Mon, 20 May 2024 12:30:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716233407; cv=pass; d=google.com; s=arc-20160816; b=CaoY9uz4V9oDKe/im+OLFgEkQQgRjagSZEI5gT+PAcKwvyY0SEZO9b+Qr0I3rEgLZZ +dljc8u9Kyyep0y2iTKOeMA4nYHM+4z7bNN2iGuUB8rftSiN99/32R6O5Ut5l+ZrTq98 mD5hKUpinckuG1lMoQ0JwfjAfRRBHVu4qrr8g1AnISTZf1YeyLgwcSpqbFBK/bwRiAbb QThhzTMsqAgEdn9dPRCXyKZ5KtHLC8DJz0JghdW6lc4Ufzpa9ybB9eXNSsldle4XlE4H XvTBjzkr6rhCEpjsoDA5lpJ0ifNydJsqB2X8uVRlq9++JBDSFi9j3oNtp+UXlDa7Oxu+ MxUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=DHjVcta07avJK/5JfCv9aDON78HlV0YJswPHnBEv0i8=; fh=2CIWpZErjcTaI8DDH7i+u3xI3BopQEbR4LqdXo2XZB0=; b=FF6wZcbY6zBKH9MuiD8Bt/GtTKtsYhc8V1ggPQ+l3Lg13kIDy+B1tCPGPM3TmtGFGk FGW9qpRbEKMCD10QcM20MtWjD4fIXh1+Z473aiW/Z71j9VuSbO2LhOYL3OT+VDIFYmnJ uYgFTqb8nPQz5SWFzMbo7WiZKR5EnkjMPWhcEp9WrNcS7ZWOmXm3bCkdErc5YjGLDx2A HnXNDmw0t1BkbvnoIYfEdOs5pJudqcFW/OHtAM11q593N8vtkY3Cp4CzWp3ZIhastTSj adt3tfxc3Y4eQKm2IP8p5g1/ygAg7E5w5JL92/F1iCi3hPwGWBj0DvkSs1Q1ELeYEENO LQzA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=RfWOuOEk; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-184114-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184114-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a5b19755dsi1039099666b.575.2024.05.20.12.30.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 12:30:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184114-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=RfWOuOEk; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-184114-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184114-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E700F1F22473 for ; Mon, 20 May 2024 19:30:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8CEDA1384A2; Mon, 20 May 2024 19:29:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RfWOuOEk" Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (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 3509C138496 for ; Mon, 20 May 2024 19:29:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716233398; cv=none; b=Mg1QftkoGtcF86hLB9/LoPJpiUFlCfx+tGJeufecBY/+diIJHU6YFpyUxoS+fv+z5vo1bTcmbgIct2D6QVBof2fKkzAx19FPgdwPCLoLW1wNDuDYvPrOTdbqi3Wj182/rxa2HBs9D5MQd0+rCSIbrHM3JnL73hPIUAMoc1dJd+8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716233398; c=relaxed/simple; bh=+zQ3M4ICtmrucW/KehKSA4Uv/vxERuUOVio4+qBsEQc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JYQ4cMM73GEX+cBl3k/o/4ytgok8UVkzPmKXkfbhDzC/3DNrUxb1r3guzSbOcRerCd7NjnwJXu1ZRunxClMQxWgqPFAXAXXUoNjT7GHZfnrIWm1iuYdSWgKn7D7nuYDe6OSZAWCA8pRxcAHTopYa+TAv9crgDbExp4rWkQ7/7SI= 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=RfWOuOEk; arc=none smtp.client-ip=209.85.128.172 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 Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-61af74a010aso28404817b3.0 for ; Mon, 20 May 2024 12:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716233396; x=1716838196; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=DHjVcta07avJK/5JfCv9aDON78HlV0YJswPHnBEv0i8=; b=RfWOuOEk8c8GGam/9AknT4vfe5tWePtgPy6Wl49b7dIjxep5XEvS4M/hl08D/8ntPp 5SyGAQBHjVbIO1D51imBPf2/gszAo+08SswnJcVyB1yRPWlcev4V/oFb65BTd723K0XM GdVxmNUJS4iCpkc+BABEzqbvUhW/zoSQOahTKFt/P+QFXjmVpsbVN7yizClAQyX0oCW3 aCty6kONxhlEyrYyTgL4+DYFrEEf0fssoebn/IhZtQSm34cpu49OXVVm41NX6VivRvHF qfIgJqauEKT0JjfAq+4q2cqMwMOK4Nstk1dH4Oh3MUFUBr1ZKG41sSebn7RCjlkT+MeJ UP0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716233396; x=1716838196; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DHjVcta07avJK/5JfCv9aDON78HlV0YJswPHnBEv0i8=; b=ou2+RiuOFH1GU/cH5rfEEX0Nxis8IKzWeAc2tLp/popiZV2vq7d4flH0B7Yx3fvQNR x9aOlWR9t7FpgZyOFuoguLcBV/uwKLqsmEH4oTPl3G+r/UObxMuqVeQ2hx/9n52GIyL0 RiZKDKjpDO1UpfY0orU5QQFrEn/H0RqKQ228xFvdAx6JjhcSxfQiUdFtrB6JBJVggTwX 8gGwCD2sgtgBzkkYOygg7JlB1jO5cDYQZE4Q9cMep0PeGK5oJxLIoIj2q/4e/OLy7oNb 7j/TJ4GJNDhQRVCk/1oVD8Z7MPGYmVEiInAqCYHtHxll3HK2QFP4d3DOhL/Hnxjz4jcK P8Cg== X-Forwarded-Encrypted: i=1; AJvYcCXcHLjlQjIQjjZlUFg7UeFJZ6ol74g7nVc55N8ryDBwZR7egmDet2qtmKVGGQoiQLFn4cb7/7N82ujlDj7E0k8DNsIe1e0UTSRxenRJ X-Gm-Message-State: AOJu0Yz2B+ny0IKOKt40r7VVaWzXOpcLQ2wS1BUmU8F4EVFVECEN3MWa P386eaqf+RSGGhkPdkzzZWaRBWRbF0Qk/pfpkALvBaAOdo9DqBTZ X-Received: by 2002:a81:4e06:0:b0:61a:b7c8:ea05 with SMTP id 00721157ae682-622b001ec63mr310219357b3.35.1716233395949; Mon, 20 May 2024 12:29:55 -0700 (PDT) Received: from localhost ([2601:344:8301:57f0:1842:2e87:56d3:d3fc]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e345e75sm50221297b3.97.2024.05.20.12.29.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 12:29:55 -0700 (PDT) Date: Mon, 20 May 2024 12:29:54 -0700 From: Yury Norov To: Michal Schmidt Cc: Rasmus Villemoes , linux-kernel@vger.kernel.org, Alex Elder , Jakub Kicinski , "David S. Miller" Subject: Re: [PATCH] bitfield.h: add FIELD_MAX_CONST Message-ID: References: <20240515172732.288391-1-mschmidt@redhat.com> 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-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240515172732.288391-1-mschmidt@redhat.com> + Alex Elder , Jakub Kicinski and David S. Miller On Wed, May 15, 2024 at 07:27:31PM +0200, Michal Schmidt wrote: > FIELD_MAX_CONST is like FIELD_MAX, but it can be used where statement > expressions are forbidden. For example, using FIELD_MAX in a > static_assert gives: > error: braced-group within expression allowed only inside a function > > It can be used also in array declarations, where using FIELD_MAX would > trigger a warning : > warning: ISO C90 forbids variable length array ‘buf’ [-Wvla] > (It's a bit surprising, because despite the warning, gcc calculated > the array size correctly at compile time.) > > A simplified example of what I actually want to use in a driver: > #define DATA_SIZE_M GENMASK(3, 0) > #define MAX_DATA_SIZE FIELD_MAX_CONST(DATA_SIZE_M) > static void f(void) { > char buf[MAX_DATA_SIZE]; > /* ... */ > } > > In the implementation, reuse the existing compile-time checks from > FIELD_PREP_CONST. > > Signed-off-by: Michal Schmidt Hi Michal, So... FIELD_MAX() already requires the _mask to be a const value. Now you add a FIELD_MAX_CONST(), which makes it more confusing. It looks like your new _CONST() macro would work anywhere where the existing FIELD_MAX() works. At least for me, if I apply your patch and do: #define FIELD_MAX FIELD_MAX_CONST The implementation of the 'const' version looks the same as the 'variable' one, except for that sanity checking business. I think the right path to go would be making the __BF_FIELD_CHECK() a structure initializers friendly by using the BUILD_BUG_ON_ZERO(), just like you did with the __BF_FIELD_CHECK_CONST(), so that the FIELD_MAX() would work in all cases. Thanks, Yury > --- > include/linux/bitfield.h | 34 +++++++++++++++++++++++++++------- > 1 file changed, 27 insertions(+), 7 deletions(-) > > diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h > index 63928f173223..50bbab317319 100644 > --- a/include/linux/bitfield.h > +++ b/include/linux/bitfield.h > @@ -76,6 +76,16 @@ > (1ULL << __bf_shf(_mask))); \ > }) > > +#define __BF_FIELD_CHECK_CONST(_mask, _val) \ > + ( \ > + /* mask must be non-zero */ \ > + BUILD_BUG_ON_ZERO((_mask) == 0) + \ > + /* check if value fits */ \ > + BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \ > + /* check if mask is contiguous */ \ > + __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) \ > + ) > + > /** > * FIELD_MAX() - produce the maximum value representable by a field > * @_mask: shifted mask defining the field's length and position > @@ -89,6 +99,22 @@ > (typeof(_mask))((_mask) >> __bf_shf(_mask)); \ > }) > > +/** > + * FIELD_MAX_CONST() - produce the maximum value representable by a field > + * @_mask: shifted mask defining the field's length and position > + * > + * FIELD_MAX_CONST() returns the maximum value that can be held in > + * the field specified by @_mask. > + * > + * Unlike FIELD_MAX(), it can be used where statement expressions can't. > + * Error checking is less comfortable for this version. > + */ > +#define FIELD_MAX_CONST(_mask) \ > + ( \ > + __BF_FIELD_CHECK_CONST(_mask, 0ULL) + \ > + (typeof(_mask))((_mask) >> __bf_shf(_mask)) \ > + ) > + > /** > * FIELD_FIT() - check if value fits in the field > * @_mask: shifted mask defining the field's length and position > @@ -132,13 +158,7 @@ > */ > #define FIELD_PREP_CONST(_mask, _val) \ > ( \ > - /* mask must be non-zero */ \ > - BUILD_BUG_ON_ZERO((_mask) == 0) + \ > - /* check if value fits */ \ > - BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \ > - /* check if mask is contiguous */ \ > - __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) + \ > - /* and create the value */ \ > + __BF_FIELD_CHECK_CONST(_mask, _val) + \ > (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)) \ > ) > > -- > 2.44.0