From: Yury Norov <yury.norov@gmail.com>
To: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>,
Johannes Berg <johannes@sipsolutions.net>,
linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
qat-linux@intel.com, linux-gpio@vger.kernel.org,
linux-aspeed@lists.ozlabs.org, linux-iio@vger.kernel.org,
linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>,
Nicolas Ferre <nicolas.ferre@microchip.com>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Claudiu Beznea <claudiu.beznea@tuxon.dev>,
Giovanni Cabiddu <giovanni.cabiddu@intel.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
Linus Walleij <linus.walleij@linaro.org>,
Bartosz Golaszewski <brgl@bgdev.pl>,
Joel Stanley <joel@jms.id.au>,
Andrew Jeffery <andrew@codeconstruct.com.au>,
Crt Mori <cmo@melexis.com>, Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Jacky Huang <ychuang3@nuvoton.com>,
Shan-Chun Hung <schung@nuvoton.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
Jakub Kicinski <kuba@kernel.org>, Alex Elder <elder@ieee.org>
Subject: Re: [PATCH treewide v2 1/3] bitfield: Add non-constant field_{prep,get}() helpers
Date: Mon, 3 Feb 2025 11:48:00 -0500 [thread overview]
Message-ID: <Z6DzQHebEKBb12Wo@thinkpad> (raw)
In-Reply-To: <16e1568d-8747-41e0-91b9-ce23c5592799@wanadoo.fr>
On Tue, Feb 04, 2025 at 12:41:55AM +0900, Vincent Mailhol wrote:
> On 03/02/2025 at 22:59, Geert Uytterhoeven wrote:
> > Hi Vincent,
> >
> > On Mon, 3 Feb 2025 at 14:37, Vincent Mailhol <mailhol.vincent@wanadoo.fr> wrote:
> >> On 03/02/2025 at 16:44, Johannes Berg wrote:
> >>> On Sun, 2025-02-02 at 12:53 -0500, Yury Norov wrote:
> >>>>> Instead of creating another variant for
> >>>>> non-constant bitfields, wouldn't it be better to make the existing macro
> >>>>> accept both?
> >>>>
> >>>> Yes, it would definitely be better IMO.
> >>>
> >>> On the flip side, there have been discussions in the past (though I
> >>> think not all, if any, on the list(s)) about the argument order. Since
> >>> the value is typically not a constant, requiring the mask to be a
> >>> constant has ensured that the argument order isn't as easily mixed up as
> >>> otherwise.
> >>
> >> If this is a concern, then it can be checked with:
> >>
> >> BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask) &&
> >> __builtin_constant_p(_val),
> >> _pfx "mask is not constant");
> >>
> >> It means that we forbid FIELD_PREP(non_const_mask, const_val) but allow
> >> any other combination.
> >
> > Even that case looks valid to me. Actually there is already such a user
> > in drivers/iio/temperature/mlx90614.c:
> >
> > ret |= field_prep(chip_info->fir_config_mask, MLX90614_CONST_FIR);
> >
> > So if you want enhanced safety, having both the safer/const upper-case
> > variants and the less-safe/non-const lower-case variants makes sense.
I agree with that. I just don't want the same shift-and operation to be
opencoded again and again.
What I actually meant is that I'm OK with whatever number of field_prep()
macro flavors, if we make sure that they don't duplicate each other. So
for me, something like this would be the best solution:
#define field_prep(mask, val) \
(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))
#define FIELD_PREP(mask, val) \
( \
FIELD_PREP_INPUT_CHECK(_mask, _val,); \
field_prep(mask, val); \
)
#define FIELD_PREP_CONST(_mask, _val) \
( \
FIELD_PREP_CONST_INPUT_CHECK(mask, val);
FIELD_PREP(mask, val); // or field_prep()
)
We have a similar macro GENMASK() in linux/bits.h. It is implemented
like this:
#define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(const_true((l) > (h)))
#define GENMASK(h, l) \
(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
And it works just well. Can we end up with a similar approach here?
> So, we are scared of people calling FIELD_PREP() with the arguments in
> the wrong order:
>
> FIELD_PREP(val, mask)
>
> thus adding the check that mask must be a compile time constant.
Don't be scared. Kernel coding implies that people get used to read
function declarations and comments on top of them before using
something.
Thansk,
Yury
next prev parent reply other threads:[~2025-02-03 16:48 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-31 13:46 [PATCH v2 0/3] Non-const bitfield helpers Geert Uytterhoeven
2025-01-31 13:46 ` [PATCH treewide v2 1/3] bitfield: Add non-constant field_{prep,get}() helpers Geert Uytterhoeven
2025-01-31 16:29 ` Alexandre Belloni
2025-01-31 16:32 ` Jonathan Cameron
2025-01-31 19:03 ` David Laight
2025-02-14 10:28 ` Geert Uytterhoeven
2025-02-02 8:26 ` Vincent Mailhol
2025-02-02 17:53 ` Yury Norov
2025-02-03 7:44 ` Johannes Berg
2025-02-03 13:36 ` Vincent Mailhol
2025-02-03 13:59 ` Geert Uytterhoeven
2025-02-03 15:41 ` Vincent Mailhol
2025-02-03 16:48 ` Yury Norov [this message]
2025-02-14 11:03 ` Geert Uytterhoeven
2025-02-14 14:39 ` Yury Norov
2025-02-03 15:31 ` Johannes Berg
2025-02-04 15:30 ` Jakub Kicinski
2025-02-14 11:00 ` Geert Uytterhoeven
2025-01-31 13:46 ` [PATCH v2 2/3] clk: renesas: Use bitfield helpers Geert Uytterhoeven
2025-01-31 13:46 ` [PATCH v2 3/3] soc: " Geert Uytterhoeven
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z6DzQHebEKBb12Wo@thinkpad \
--to=yury.norov@gmail.com \
--cc=alexandre.belloni@bootlin.com \
--cc=andrew@codeconstruct.com.au \
--cc=brgl@bgdev.pl \
--cc=claudiu.beznea@tuxon.dev \
--cc=cmo@melexis.com \
--cc=davem@davemloft.net \
--cc=elder@ieee.org \
--cc=geert@linux-m68k.org \
--cc=giovanni.cabiddu@intel.com \
--cc=herbert@gondor.apana.org.au \
--cc=jic23@kernel.org \
--cc=joel@jms.id.au \
--cc=johannes@sipsolutions.net \
--cc=kuba@kernel.org \
--cc=lars@metafoo.de \
--cc=linus.walleij@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-aspeed@lists.ozlabs.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mailhol.vincent@wanadoo.fr \
--cc=mturquette@baylibre.com \
--cc=nicolas.ferre@microchip.com \
--cc=perex@perex.cz \
--cc=qat-linux@intel.com \
--cc=sboyd@kernel.org \
--cc=schung@nuvoton.com \
--cc=tiwai@suse.com \
--cc=ychuang3@nuvoton.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).