From: Yury Norov <yury.norov@gmail.com>
To: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: "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 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>,
"Johannes Berg" <johannes@sipsolutions.net>,
"Jakub Kicinski" <kuba@kernel.org>, "Alex Elder" <elder@ieee.org>,
"David Laight" <david.laight.linux@gmail.com>,
"Vincent Mailhol" <mailhol.vincent@wanadoo.fr>,
"Jason Baron" <jbaron@akamai.com>,
"Borislav Petkov" <bp@alien8.de>,
"Tony Luck" <tony.luck@intel.com>,
"Michael Hennerich" <Michael.Hennerich@analog.com>,
"Kim Seer Paller" <kimseer.paller@analog.com>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Richard Genoud" <richard.genoud@bootlin.com>,
"Cosmin Tanislav" <demonsingur@gmail.com>,
"Biju Das" <biju.das.jz@bp.renesas.com>,
"Jianping Shen" <Jianping.Shen@de.bosch.com>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
"Miquel Raynal" <miquel.raynal@bootlin.com>,
"Richard Weinberger" <richard@nod.at>,
"Vignesh Raghavendra" <vigneshr@ti.com>,
linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
linux-edac@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-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
"Jonathan Cameron" <Jonathan.Cameron@huawei.com>,
"Andy Shevchenko" <andriy.shevchenko@intel.com>
Subject: Re: [PATCH v6 13/26] bitfield: Add non-constant field_{prep,get}() helpers
Date: Thu, 6 Nov 2025 11:31:48 -0500 [thread overview]
Message-ID: <aQzNdJFcStVak5jS@yury> (raw)
In-Reply-To: <62a5058e460129155e4e7539e37140bd0f0d4abc.1762435376.git.geert+renesas@glider.be>
On Thu, Nov 06, 2025 at 02:34:01PM +0100, Geert Uytterhoeven wrote:
> The existing FIELD_{GET,PREP}() macros are limited to compile-time
> constants. However, it is very common to prepare or extract bitfield
> elements where the bitfield mask is not a compile-time constant.
>
> To avoid this limitation, the AT91 clock driver and several other
> drivers already have their own non-const field_{prep,get}() macros.
> Make them available for general use by adding them to
> <linux/bitfield.h>, and improve them slightly:
> 1. Avoid evaluating macro parameters more than once,
> 2. Replace "ffs() - 1" by "__ffs()",
> 3. Support 64-bit use on 32-bit architectures,
> 4. Wire field_{get,prep}() to FIELD_{GET,PREP}() when mask is
> actually constant.
>
> This is deliberately not merged into the existing FIELD_{GET,PREP}()
> macros, as people expressed the desire to keep stricter variants for
> increased safety, or for performance critical paths.
>
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Acked-by: Crt Mori <cmo@melexis.com>
> Acked-by: Nuno Sá <nuno.sa@analog.com>
> Acked-by: Richard Genoud <richard.genoud@bootlin.com>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> ---
> v6:
> - Align \,
> - Add Return sections to kerneldoc,
> - Add Reviewed-by,
> - Document field_{get,prep} in top comment block,
> - Use less-checking __FIELD_{GET,PREP}() to avoid build issues with
> clang and W=1:
> https://lore.kernel.org/all/202510281335.UpSLYJG9-lkp@intel.com
> https://lore.kernel.org/all/202510281414.DnejZh4n-lkp@intel.com
> https://lore.kernel.org/all/202510281304.RK3J3c3t-lkp@intel.com
>
> v5:
> - Add Acked-by,
> - Split off changes outside <linux/bitfield.h>,
> - Document that mask must be non-zero,
> - Document typical usage pattern,
> - Recommend using FIELD_{PREP,GET}() directly to ensure compile-time
> constant masks,
> - Check BITS_PER_TYPE(mask) instead of sizeof(mask),
> - Wire field_{get,prep}() to FIELD_{GET,PREP}() when mask is
> constant, to improve type checking.
>
> v4:
> - Add Acked-by,
> - Rebase on top of commit 7c68005a46108ffa ("crypto: qat - relocate
> power management debugfs helper APIs") in v6.17-rc1,
> - Convert more recently introduced upstream copies:
> - drivers/edac/ie31200_edac.c
> - drivers/iio/dac/ad3530r.c
>
> v3:
> - Add Acked-by,
> - Drop underscores from macro parameters,
> - Use __auto_type where possible,
> - Correctly cast reg to the mask type,
> - Introduces __val and __reg intermediates to simplify the actual
> operation,
> - Drop unneeded parentheses,
> - Clarify having both FIELD_{GET,PREP}() and field_{get,prep}(),
>
> v2:
> - Cast val resp. reg to the mask type,
> - Fix 64-bit use on 32-bit architectures,
> - Convert new upstream users:
> - drivers/crypto/intel/qat/qat_common/adf_gen4_pm_debugfs.c
> - drivers/gpio/gpio-aspeed.c
> - drivers/iio/temperature/mlx90614.c
> - drivers/pinctrl/nuvoton/pinctrl-ma35.c
> - sound/usb/mixer_quirks.c
> - Convert new user queued in renesas-devel for v6.15:
> - drivers/soc/renesas/rz-sysc.c
> ---
> include/linux/bitfield.h | 59 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 59 insertions(+)
>
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index bf8e0ae4b5b41038..f92e18c9629a59c6 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -17,6 +17,7 @@
> * FIELD_{GET,PREP} macros take as first parameter shifted mask
> * from which they extract the base mask and shift amount.
> * Mask must be a compilation time constant.
> + * field_{get,prep} are variants that take a non-const mask.
> *
> * Example:
> *
> @@ -240,4 +241,62 @@ __MAKE_OP(64)
> #undef __MAKE_OP
> #undef ____MAKE_OP
>
> +#define __field_prep(mask, val) \
> + ({ \
> + __auto_type __mask = (mask); \
> + typeof(mask) __val = (val); \
typeof(__mask), I guess.
> + unsigned int __shift = BITS_PER_TYPE(mask) <= 32 ? \
Same here: BITS_PER_TYPE(__mask). BITS_PER_TYPE() doesn't evaluate the
expression because it is based on sizeof(). But we definitely don't
want people check it every time they read the code.
No need to send another version for this. I can fix it inplace.
Reviewed-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
> + __ffs(__mask) : __ffs64(__mask); \
> + (__val << __shift) & __mask; \
> + })
> +
> +#define __field_get(mask, reg) \
> + ({ \
> + __auto_type __mask = (mask); \
> + typeof(mask) __reg = (reg); \
> + unsigned int __shift = BITS_PER_TYPE(mask) <= 32 ? \
> + __ffs(__mask) : __ffs64(__mask); \
> + (__reg & __mask) >> __shift; \
> + })
> +
> +/**
> + * field_prep() - prepare a bitfield element
> + * @mask: shifted mask defining the field's length and position, must be
> + * non-zero
> + * @val: value to put in the field
> + *
> + * Return: field value masked and shifted to its final destination
> + *
> + * field_prep() masks and shifts up the value. The result should be
> + * combined with other fields of the bitfield using logical OR.
> + * Unlike FIELD_PREP(), @mask is not limited to a compile-time constant.
> + * Typical usage patterns are a value stored in a table, or calculated by
> + * shifting a constant by a variable number of bits.
> + * If you want to ensure that @mask is a compile-time constant, please use
> + * FIELD_PREP() directly instead.
> + */
> +#define field_prep(mask, val) \
> + (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \
> + : __field_prep(mask, val))
> +
> +/**
> + * field_get() - extract a bitfield element
> + * @mask: shifted mask defining the field's length and position, must be
> + * non-zero
> + * @reg: value of entire bitfield
> + *
> + * Return: extracted field value
> + *
> + * field_get() extracts the field specified by @mask from the
> + * bitfield passed in as @reg by masking and shifting it down.
> + * Unlike FIELD_GET(), @mask is not limited to a compile-time constant.
> + * Typical usage patterns are a value stored in a table, or calculated by
> + * shifting a constant by a variable number of bits.
> + * If you want to ensure that @mask is a compile-time constant, please use
> + * FIELD_GET() directly instead.
> + */
> +#define field_get(mask, reg) \
> + (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \
> + : __field_get(mask, reg))
> +
> #endif
> --
> 2.43.0
next prev parent reply other threads:[~2025-11-06 16:31 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-06 13:33 [PATCH v6 00/26] Non-const bitfield helpers Geert Uytterhoeven
2025-11-06 13:33 ` [PATCH v6 01/26] clk: at91: pmc: #undef field_{get,prep}() before definition Geert Uytterhoeven
2025-11-06 15:36 ` Claudiu Beznea
2025-11-14 2:29 ` Stephen Boyd
2025-11-06 13:33 ` [PATCH v6 02/26] crypto: qat - #undef field_get() before local definition Geert Uytterhoeven
2025-11-06 13:33 ` [PATCH v6 03/26] EDAC/ie31200: " Geert Uytterhoeven
2025-11-10 14:02 ` Zhuo, Qiuxu
2025-11-06 13:33 ` [PATCH v6 04/26] gpio: aspeed: #undef field_{get,prep}() " Geert Uytterhoeven
2025-11-06 13:33 ` [PATCH v6 05/26] iio: dac: ad3530r: #undef field_prep() " Geert Uytterhoeven
2025-11-09 13:35 ` Jonathan Cameron
2025-11-06 13:33 ` [PATCH v6 06/26] iio: mlx90614: #undef field_{get,prep}() " Geert Uytterhoeven
2025-11-09 13:35 ` Jonathan Cameron
2025-11-06 13:33 ` [PATCH v6 07/26] pinctrl: ma35: " Geert Uytterhoeven
2025-11-06 13:33 ` [PATCH v6 08/26] soc: renesas: rz-sysc: #undef field_get() " Geert Uytterhoeven
2025-11-06 15:39 ` Claudiu Beznea
2025-11-06 13:33 ` [PATCH v6 09/26] ALSA: usb-audio: #undef field_{get,prep}() " Geert Uytterhoeven
2025-11-06 13:33 ` [PATCH -next v6 10/26] iio: imu: smi330: #undef field_{get,prep}() before definition Geert Uytterhoeven
2025-11-09 13:34 ` Jonathan Cameron
2025-11-06 13:33 ` [PATCH -next v6 11/26] mtd: rawnand: sunxi: #undef field_{get,prep}() before local definition Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}() Geert Uytterhoeven
2025-11-06 14:44 ` Andy Shevchenko
2025-11-06 14:49 ` Geert Uytterhoeven
2025-11-06 16:09 ` Andy Shevchenko
2025-11-06 16:20 ` Geert Uytterhoeven
2025-11-07 1:13 ` Ping-Ke Shih
2025-11-07 7:59 ` Andy Shevchenko
2025-11-10 9:33 ` Geert Uytterhoeven
2025-11-07 8:35 ` Geert Uytterhoeven
2025-11-07 9:16 ` Ping-Ke Shih
2025-11-10 2:43 ` Ping-Ke Shih
2025-11-06 16:01 ` Yury Norov
2025-11-06 16:08 ` Andy Shevchenko
2025-11-06 13:34 ` [PATCH v6 13/26] bitfield: Add non-constant field_{prep,get}() helpers Geert Uytterhoeven
2025-11-06 16:31 ` Yury Norov [this message]
2025-11-06 13:34 ` [PATCH v6 14/26] clk: at91: Convert to common field_{get,prep}() helpers Geert Uytterhoeven
2025-11-06 15:44 ` Claudiu Beznea
2025-11-14 2:26 ` Stephen Boyd
2025-11-06 13:34 ` [PATCH v6 15/26] crypto: qat - convert to common field_get() helper Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH v6 16/26] EDAC/ie31200: Convert " Geert Uytterhoeven
2025-11-10 14:03 ` Zhuo, Qiuxu
2025-11-06 13:34 ` [PATCH v6 17/26] gpio: aspeed: Convert to common field_{get,prep}() helpers Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH v6 18/26] iio: dac: Convert to common field_prep() helper Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH v6 19/26] iio: mlx90614: Convert to common field_{get,prep}() helpers Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH v6 20/26] pinctrl: ma35: " Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH v6 21/26] soc: renesas: rz-sysc: Convert to common field_get() helper Geert Uytterhoeven
2025-11-06 15:40 ` Claudiu Beznea
2025-11-06 13:34 ` [PATCH v6 22/26] ALSA: usb-audio: Convert to common field_{get,prep}() helpers Geert Uytterhoeven
2025-11-06 13:34 ` [PATCH -next v6 23/26] iio: imu: smi330: " Geert Uytterhoeven
2025-11-09 13:39 ` Jonathan Cameron
2025-11-06 13:34 ` [PATCH -next v6 24/26] mtd: rawnand: sunxi: " Geert Uytterhoeven
2025-11-06 13:51 ` Miquel Raynal
2025-11-06 13:34 ` [PATCH v6 25/26] clk: renesas: Use bitfield helpers Geert Uytterhoeven
2025-11-14 2:28 ` Stephen Boyd
2025-11-06 13:34 ` [PATCH v6 26/26] soc: " Geert Uytterhoeven
2025-11-06 16:45 ` [PATCH v6 00/26] Non-const " Yury Norov
2025-11-19 14:50 ` Yury Norov
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=aQzNdJFcStVak5jS@yury \
--to=yury.norov@gmail.com \
--cc=Jianping.Shen@de.bosch.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=Michael.Hennerich@analog.com \
--cc=alexandre.belloni@bootlin.com \
--cc=andrew@codeconstruct.com.au \
--cc=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=biju.das.jz@bp.renesas.com \
--cc=bp@alien8.de \
--cc=brgl@bgdev.pl \
--cc=claudiu.beznea@tuxon.dev \
--cc=cmo@melexis.com \
--cc=davem@davemloft.net \
--cc=david.laight.linux@gmail.com \
--cc=demonsingur@gmail.com \
--cc=dlechner@baylibre.com \
--cc=elder@ieee.org \
--cc=geert+renesas@glider.be \
--cc=giovanni.cabiddu@intel.com \
--cc=herbert@gondor.apana.org.au \
--cc=jbaron@akamai.com \
--cc=jic23@kernel.org \
--cc=joel@jms.id.au \
--cc=johannes@sipsolutions.net \
--cc=kimseer.paller@analog.com \
--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-edac@vger.kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mailhol.vincent@wanadoo.fr \
--cc=miquel.raynal@bootlin.com \
--cc=mturquette@baylibre.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=nicolas.ferre@microchip.com \
--cc=nuno.sa@analog.com \
--cc=perex@perex.cz \
--cc=qat-linux@intel.com \
--cc=richard.genoud@bootlin.com \
--cc=richard@nod.at \
--cc=sboyd@kernel.org \
--cc=schung@nuvoton.com \
--cc=tiwai@suse.com \
--cc=tony.luck@intel.com \
--cc=vigneshr@ti.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).