Re: [PATCH v4 2/4] bitfield: Add non-constant field_{prep,get}() helpers

Yury Norov
2025-10-17T18:51:42Z

On Fri, Oct 17, 2025 at 12:54:10PM +0200, 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 consolidating them in
> <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.
>
> 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>
> ---
> 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

Can you split out the part that actually introduces the new API?

...

> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index 7ff817bdae19b468..c999fe70076f6684 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -220,4 +220,40 @@ __MAKE_OP(64)
> #undef __MAKE_OP
> #undef ____MAKE_OP
>
> +/**
> + * field_prep() - prepare a bitfield element
> + * @mask: shifted mask defining the field's length and position
> + * @val: value to put in the field
> + *
> + * 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.
> + */
> +#define field_prep(mask, val) \
> + ({ \
> + __auto_type __mask = (mask); \
> + typeof(mask) __val = (val); \
> + unsigned int __shift = sizeof(mask) <= 4 ? \
> + __ffs(__mask) : __ffs64(__mask); \
> + (__val << __shift) & __mask; \

__ffs(0) is undef. The corresponding comment in
include/asm-generic/bitops/__ffs.h explicitly says: "code should check
against 0 first".

I think mask = 0 is a sign of error here. Can you add a code catching
it at compile time, and maybe at runtime too? Something like:

#define __field_prep(mask, val)
({
unsigned __shift = sizeof(mask) <= 4 ? __ffs(mask) : __ffs64(mask);
(val << __shift) & mask;
})

#define field_prep(mask, val)
({
unsigned int __shift;
__auto_type __mask = (mask), __ret = 0;
typeof(mask) __val = (val);

BUILD_BUG_ON_ZERO(const_true(mask == 0));

if (WARN_ON_ONCE(mask == 0))
goto out;

__ret = __field_prep(__mask, __val);
out:
ret;
})

> +
> +/**
> + * field_get() - extract a bitfield element
> + * @mask: shifted mask defining the field's length and position
> + * @reg: value of entire bitfield
> + *
> + * 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.
> + */
> +#define field_get(mask, reg) \
> + ({ \
> + __auto_type __mask = (mask); \
> + typeof(mask) __reg = (reg); \

This would trigger Wconversion warning. Consider
unsigned reg = 0xfff;
field_get(0xf, reg);

:6:26: warning: conversion to 'int' from 'unsigned int' may change the sign of the result [-Wsign-conversion]
6 | typeof(mask) __reg = reg;
| ^~~

Notice, the __auto_type makes the __mask to be int, while the reg is
unsigned int. You need to do:

typeof(mask) __reg = (typeof(mask))(reg);

Please enable higher warning levels for the next round.

Also, because for numerals __auto_type is int, when char is enough - are
you sure that the macro generates the optimal code? User can workaround it
with:

field_get((u8)0xf, reg)

but it may not be trivial. Can you add an example and explanation please?

> + unsigned int __shift = sizeof(mask) <= 4 ? \
> + __ffs(__mask) : __ffs64(__mask); \

Can you use BITS_PER_TYPE() here?

> + (__reg & __mask) >> __shift; \
> + })
> +

When mask == 0, we shouldn't touch 'val' at all. Consider

field_get(0, get_user(ptr))

In this case, evaluating 'reg' is an error, similarly to memcpy().

Thanks,
Yury

> #endif
> diff --git a/sound/usb/mixer_quirks.c b/sound/usb/mixer_quirks.c
> index 828af3095b86ee0a..6eee89cbc0867f2b 100644
> --- a/sound/usb/mixer_quirks.c
> +++ b/sound/usb/mixer_quirks.c
> @@ -3311,10 +3311,6 @@ static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
> #define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
> #define RME_DIGIFACE_INVERT BIT(31)
>
> -/* Nonconst helpers */
> -#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
> -#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
> -
> static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
> {
> struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
> --
> 2.43.0