Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp3047506lqo; Tue, 21 May 2024 05:33:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUC5uXhyEQyFhAD1XKR6aDBrEM+4rTTBccljvI69XvWCliRZXa1bapMorYXwu+puUSFBL4RbqTLp3tvfJgwzPTFm0csgr63ZgFZ8FAQCw== X-Google-Smtp-Source: AGHT+IHbS/4+G9aCJGNuiLMSN85cL+XvBcBFzUVsSCxkxewvdQ6ml/i0SviqbFeVqzci8CKyjpQz X-Received: by 2002:a05:620a:820f:b0:792:daab:b209 with SMTP id af79cd13be357-792daabb44fmr2632115885a.6.1716294836715; Tue, 21 May 2024 05:33:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716294836; cv=pass; d=google.com; s=arc-20160816; b=oBUrGqBO9H4EaVPrWau4hVq9V/gS/beIXSXu+6HLRyzgc+DMvS/biHnSzBBDuLoaKC 1WGxZ+mrpzwn9ENhBwqBMi3OCdzKTnCtqXbHSbtv7QpRUbWfXp0LG3iBSk7sZPC62yEG pQhRzvkJC92aMKSsYq94u8sD4JsUVIy7TOh4GxwZVVvPFONQQSGpCQhfSAJG8EZl4gQP wSRpMGBgtQSJKVOfYWr5WNe546KlTmDxV6ubscuHnFswxdIm0faqUrrHSKiITo7xUqqr BnBY72ktj/E4hN0yoaW90kesqFmpr6PWWzYhceopsnEhZAPGWhG/zpO/eafrsjMUrMEN Hutg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=nSYyFEA0u90HBMfg2v5nbo6i1zSNMAA+C8CsRFK1nT0=; fh=Yik8NX7AC6gkhuk61g8UNUNLuzGfl9ugUxQdYycnCgo=; b=FZJszlmJYrcB3eTzeQWDwqGYlRb0Izv80OH5ikmSXl0tXDULdVqu/En7WB3idKp5HH 9k2gkVU+D7gtUnKvmOvvp63XWPuQewtp2w/fbJ8Aylcqgxr45FdVyQccDGi6iNzTDRh9 b3Pws8R3TEOKbtw5v/yvuaAzilI7P1JbAls8vWcSpovGY26ifR+42pu9HCZQMxnCcG4B H2Yn1elsUAhmNs794s25hnBrY2yHZAnUWzflX7BzCo+m2zguopjWc5HeCaUQ6ErxYzZa GOTp9RXJykEmc0F5EJj237iPri3EiXx47dPMqS4XeXgaxAYAJgluh5eqCWnWSJ04dC+g sVRQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eH3Jcigy; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-184953-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184953-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-792e367f438si1911742585a.646.2024.05.21.05.33.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 05:33:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184953-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eH3Jcigy; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-184953-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184953-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 6180B1C21281 for ; Tue, 21 May 2024 12:33:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8E6B7581F; Tue, 21 May 2024 12:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="eH3Jcigy" Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) (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 0031D6BB33 for ; Tue, 21 May 2024 12:33:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716294830; cv=none; b=S1ppA5aZd/isugYoHi8hKt7wQJPobcOJ+skZfqCGT4jUbdZQp4vGJjp3KMKKUIYkDbPDFRBiK7THLxcNYk8g+fHJvJSHtXZqmiMGMs6i/haVpEAKNIAyBwmT67ZYrb41qCReNTyRZGidJVmQd30VCWcPxgp8UL4PK/6jMBduJ6I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716294830; c=relaxed/simple; bh=oAzwjNwCAQTsDGGzTI2IqR5CjgdQSLsyhBRqPlil1xg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=AKBhXqr6Do6kVAHyYNGF8CYQgAQ87YtnEH09wM4NM/xLMuTfJOKBGvZLyH6oB6Wx09e7m2lkBneSbHekb+dmm3+siBq4md5/fKn6Rq3gMp3SbLrzlxuxDqH9DYiGx9TjNMP66dUAX+m4ZXggznxiyOASXKYhfdKUibaTv0T8XBg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=eH3Jcigy; arc=none smtp.client-ip=209.85.166.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-io1-f46.google.com with SMTP id ca18e2360f4ac-7e1b4f96994so291562839f.2 for ; Tue, 21 May 2024 05:33:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1716294828; x=1716899628; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=nSYyFEA0u90HBMfg2v5nbo6i1zSNMAA+C8CsRFK1nT0=; b=eH3Jcigy/72uOll+4sdh2TJt7Vy7WD78Vvki3R9PhDoG7UU6JwbHGMZYroMFBXI1Md lvGVJA3bjV1dP//DjRrcSLCCRz59TNobHQ89dPwCWMfSXwwsU2+MUtV5cVGbTXsPKJ5J yIloozQG1PDXHmGIi9iC2yntQ2Uv/mba/th+KbUVmlJZnLNwjejcHL08vNVy9712Kdrv VCfeEdZ+cN7Y9CbdaNCGADupngXUkcDsqF/i4P4WEJCXEpGH5hhU8Xv6RtgTfpQCCxpm FgmeQST9qHiF/OIhlnMLQoy9QeUArGJCPKVK3WrSCCI7IB6X7BidqqEtp6+ccBeVDSMR 5wdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716294828; x=1716899628; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nSYyFEA0u90HBMfg2v5nbo6i1zSNMAA+C8CsRFK1nT0=; b=lHN6aa7CSCNx01IZwhY13PejJfbGI9sETFckvjpsK2dGggikMW+Hl2oWNVIPWD1r2R O1NU2dEFYLkNcbliKWZxxykKus/WLF60TVjyIHs7KTsJ9d9fZKaIp/tOYMMh9wwrHo+v aJYooHOFX3L7QqYMIF4iYPTMXbTXIj3jY1+b1pP8FzEu+eE3B8hAIpR0MHs2x4d6ZVsF 004akmzByKZ4ViTkN4CoURfGiuBt9CsxndRAAbcLxbMAGRSUPenFO7+KFuwWtaKIb3a6 Oskckec5cnT6bo8C2wmV/Wt8EDMEMR6p7A+j+UFQOyetpI99w0I8fn/nZuDgQ3y2lKJ2 oHig== X-Forwarded-Encrypted: i=1; AJvYcCUQTziqSvvjuHn0SRD6tsfhc+eLbG/rvPzJtLa+E5bmVSIuoA05VzyxH99FMBXws37q9rQzpKKVdU5sinEzfc0lKwLQ/WIYQxP28NPW X-Gm-Message-State: AOJu0YxKRxr62oQrIuiOgrrnL+REmk9yPCbmPbtsk1J1+Sq6qZzznprW uugOz0YJyfcD00TZoprGbiGK4jgsU09OMvGD4eIl7VlvDS+NxWg9QwSmHlVlJik= X-Received: by 2002:a5d:8e0b:0:b0:7e1:7b87:add3 with SMTP id ca18e2360f4ac-7e1b50107abmr3408425939f.0.1716294827978; Tue, 21 May 2024 05:33:47 -0700 (PDT) Received: from [172.22.22.28] (c-73-228-159-35.hsd1.mn.comcast.net. [73.228.159.35]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-489376de185sm6863661173.150.2024.05.21.05.33.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 May 2024 05:33:47 -0700 (PDT) Message-ID: <3f3b75df-3488-4915-bc21-54cb6a6e2a74@linaro.org> Date: Tue, 21 May 2024 07:33:46 -0500 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] bitfield.h: add FIELD_MAX_CONST To: Yury Norov , Michal Schmidt Cc: Rasmus Villemoes , linux-kernel@vger.kernel.org, Jakub Kicinski , "David S. Miller" References: <20240515172732.288391-1-mschmidt@redhat.com> Content-Language: en-US From: Alex Elder In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 5/20/24 2:29 PM, Yury Norov wrote: > + Alex Elder , Jakub Kicinski and > David S. Miller Thanks for adding me to this. My bottom line response is that I don't understand exactly what problem this is solving (but I trust it solves a problem for you). It *seems* like the existing macro(s) should work for you, and if they don't, you might not be using it (them) correctly. And... if a fix is needed, it should be made to the existing macro if possible. > 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 So you want to use FIELD_MAX() in the outer scope in a file, not within a function? And the way it's currently defined doesn't permit that? >> 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] Are you passing a constant to FIELD_MAX() when computing the array size? If so, I don't understand this warning. >> (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) FIELD_MAX() returns the maximum representable value, not the number of possible values. >> 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. Yes, it's not clear when you would use one rather than the other. I think a better fix is to fix the existing FIELD_MAX() (and field_max(), defined later in that file). > 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. I haven't investigated this much further, but yes, I agree that you should fix what's there to work the way you like if possible, while preserving all guarantees provided before. Still, I'll provide some comments on the patch below. > 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) + \ This ^^^ implements the opposite of what the comment says. Use: BUILD_BUG_ON_ZERO(_mask); Also, why are you adding? The way this macro is defined, it really should return Boolean, but you're returning an integer sum. >> + /* 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))) \ I don't really see why __BUILD_BUG_ON_NOT_POWER_OF_2() isn't used here (and in FIELD_PREP_CONST() for that matter--other than line length). Each of the above checks can stand alone, and if they all pass, you can simply return true (or return the result of the last check, but I really think they should be treated as void type). >> + ) >> + >> /** >> * 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. I don't think this part of the description adds much; it basically restates what the one-liner does. -Alex >> + * >> + * 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