Skip to content

Non-const bitfield helpers

Geert Uytterhoeven linux-aspeed linux-clk linux-crypto linux-edac linux-gpio linux-iio linux-mtd linux-renesas-soc linux-sound linux-wireless lkml 5 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v6 Latest26 patches 30 repliesIn review
  2. v523 patches 20 repliesSuperseded
  3. v44 patches 11 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v34 patches8 repliesSuperseded
v23 patches16 repliesSuperseded

Patches in v6 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/26clk: at91: pmc: #undef field_{get,prep}() before definitiondrivers/clk/at91/pmc.h+2 -02 replies
2/26crypto: qat - #undef field_get() before local definitiondrivers/crypto/intel/qat/qat_common/adf_pm_dbgfs_utils.c+1 -00 replies
3/26EDAC/ie31200: #undef field_get() before local definitiondrivers/edac/ie31200_edac.c+1 -01 reply
4/26gpio: aspeed: #undef field_{get,prep}() before local definitiondrivers/gpio/gpio-aspeed.c+2 -00 replies
5/26iio: dac: ad3530r: #undef field_prep() before local definitiondrivers/iio/dac/ad3530r.c+1 -01 reply
6/26iio: mlx90614: #undef field_{get,prep}() before local definitiondrivers/iio/temperature/mlx90614.c+2 -01 reply
7/26pinctrl: ma35: #undef field_{get,prep}() before local definitiondrivers/pinctrl/nuvoton/pinctrl-ma35.c+2 -00 replies
8/26soc: renesas: rz-sysc: #undef field_get() before local definitiondrivers/soc/renesas/rz-sysc.c+1 -01 reply
9/26ALSA: usb-audio: #undef field_{get,prep}() before local definitionsound/usb/mixer_quirks.c+2 -00 replies
10/26iio: imu: smi330: #undef field_{get,prep}() before definitiondrivers/iio/imu/smi330/smi330_core.c+2 -01 reply
11/26mtd: rawnand: sunxi: #undef field_{get,prep}() before local definitiondrivers/mtd/nand/raw/sunxi_nand.c+2 -01 reply
12/26bitfield: Add less-checking __FIELD_{GET,PREP}()include/linux/bitfield.h+28 -812 replies
13/26bitfield: Add non-constant field_{prep,get}() helpersinclude/linux/bitfield.h+59 -01 reply
14/26clk: at91: Convert to common field_{get,prep}() helpersdrivers/clk/at91/clk-peripheral.c drivers/clk/at91/pmc.h+1 -52 replies
15/26crypto: qat - convert to common field_get() helperdrivers/crypto/intel/qat/qat_common/adf_pm_dbgfs_utils.c+1 -80 replies
16/26EDAC/ie31200: Convert to common field_get() helperdrivers/edac/ie31200_edac.c+1 -41 reply
17/26gpio: aspeed: Convert to common field_{get,prep}() helpersdrivers/gpio/gpio-aspeed.c+1 -60 replies
18/26iio: dac: Convert to common field_prep() helperdrivers/iio/dac/ad3530r.c+0 -40 replies
19/26iio: mlx90614: Convert to common field_{get,prep}() helpersdrivers/iio/temperature/mlx90614.c+1 -60 replies
20/26pinctrl: ma35: Convert to common field_{get,prep}() helpersdrivers/pinctrl/nuvoton/pinctrl-ma35.c+0 -60 replies
21/26soc: renesas: rz-sysc: Convert to common field_get() helperdrivers/soc/renesas/rz-sysc.c+1 -31 reply
22/26ALSA: usb-audio: Convert to common field_{get,prep}() helperssound/usb/mixer_quirks.c+0 -60 replies
23/26iio: imu: smi330: Convert to common field_{get,prep}() helpersdrivers/iio/imu/smi330/smi330_core.c+0 -61 reply
24/26mtd: rawnand: sunxi: Convert to common field_{get,prep}() helpersdrivers/mtd/nand/raw/sunxi_nand.c+0 -61 reply
25/26clk: renesas: Use bitfield helpersdrivers/clk/renesas/clk-div6.c drivers/clk/renesas/rcar-gen3-cpg.c drivers/clk/renesas/rcar-gen4-cpg.c+11 -191 reply
26/26soc: renesas: Use bitfield helpersdrivers/soc/renesas/renesas-soc.c+2 -20 replies

Reviews trailers per patch

20 of 26 patches have a Reviewed-by or Acked-by.

PatchAlexandre BelloniGiovanni CabidduBartosz GolaszewskiLinus WalleijTakashi IwaiJonathan CameronCrt MoriNuno SáRichard GenoudAndy ShevchenkoMiquel RaynalClaudiu BezneaYury Norov (NVIDIA)Qiuxu ZhuoStephen Boyd
1/26A-b A-b A-b
2/26A-b
3/26R-b
4/26A-b
5/26A-b
6/26A-b
7/26A-b
8/26R-b
9/26A-b
10/26
11/26
12/26
13/26A-b A-b A-b A-b A-b R-b R-b
14/26A-b A-b A-b
15/26A-b
16/26R-b
17/26
18/26A-b
19/26A-b A-b
20/26R-b
21/26R-b
22/26A-b
23/26
24/26A-b
25/26R-b
26/26

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 15 reviewers.

Read the v6 thread

Cover letter [PATCH v6 00/26] Non-const bitfield helpers

Hi all,

<linux/bitfield.h> contains various helpers for accessing bitfields, as typically used in hardware registers for memory-mapped I/O blocks. These helpers ensure type safety, and deduce automatically shift values from mask values, avoiding mistakes due to inconsistent shifts and masks, and leading to a reduction in source code size.

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 (e.g. it comes from a table, or is created by shifting a compile-time constant). To avoid this limitation, the AT91 clock driver introduced its own field_{prep,get}() macros. During the past four years, these have been copied to multiple drivers, and more copies are on their way[1], leading to the obvious review comment "please move this to <linux/bitfield.h>".

Hence this series
1. Takes preparatory steps in drivers definining local

     field_{get,prep}() macros (patches 1-11),
  2. Introduces __FIELD_{PREP,GET}() helpers to avoid clang W=1 warnings
     (patch 12),
  3. Makes field_{prep,get}() available for general use (patch 13),
  4. Converts drivers with local variants to the common helpers (patches
     14-24),
  5. Converts a few Renesas drivers to the existing FIELD_{GET,PREP}()
     and the new field_{get,prep}() helpers (patches 25-26).

Alternatives would be to use the typed {u*,be*,le*,...}_{get,encode}_bits() macros instead (which currently do not work with non-constant masks either, and the first attempt to change that generates much worse code), or to store the low bit and width of the mask instead (which would require changing all code that passes masks directly, and also generates worse code).

Changes compared to v5[2]:
- Add Acked-by, Reviewed-by,
- Update sunxi rawnand driver in linux-next,
- Align \,
- Add Return sections to kerneldoc,
- Document field_{get,prep} in top comment block,
- New patch to add less-checking __FIELD_{GET,PREP}() helpers,
- Use less-checking __FIELD_{GET,PREP}() to avoid build issues with clang and W=1.

Changes compared to v4[3]:
- Add preparatory patches to #undef field_{get,prep}() in individual drivers before defining local variants,
- Update new smi330 IIO IMU driver in linux-next,
- Add Acked-by,
- 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,
- Extract conversion of individual drivers into separate patches.

Changes compared to v3[4]:
- Update recently introduced FIELD_MODIFY() macro,
- 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

Changes compared to v2[5]:
- New patch "[PATCH v3 1/4] bitfield: Drop underscores from macro parameters",
- 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}(),

Changes compared to v1[6]:
- 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
- Drop the last 14 RFC patches.
They can be updated/resubmitted/applied later.

In the meantime, two more copies ended up in the IIO and MTD trees and in linux-next (commit 89cba586b8b4cde0 ("iio: imu: smi330: Add driver" in next-20251021 and later, commits 6fc2619af1eb6f59 ("mtd: rawnand:
sunxi: rework pattern found registers") and d21b4338159ff7d7 ("mtd:
rawnand: sunxi: introduce ecc_mode_mask in sunxi_nfc_caps") in next-20251029 and later). As these commits are not yet upstream, any updates (patches 10, 11, 23 and 24) for these drivers cannot be applied with the rest of this series yet.

Yury kindly offered to take the lionshare of this series through his bitmap tree, thanks for that!
I am fine with that, if he would also provide an immutable branch + tag, which I can merge into the Renesas tree before taking patches 25 and 26 myself. Once that tag has been merged in subsystem trees or upstream, I plan to update and resend actual conversions (see patches 4-17 in v1[6]).

To avoid build issues in linux-next, the IIO resp. MTD maintainers should:
1. Apply patch 10 resp. 11 now, and
2. Apply patch 23 resp. 24 later, either after
a. merging the immutable branch/tag, or
b. the new helpers in <linux/bitfield.h> are upstream,

Note that there is also a minor conflict with linux-next due to the removal of an include file from drivers/gpio/gpio-aspeed.c.

Thanks!

[1] Work-in-progress new copies posted during the last few months (there
may be more):
- "[PATCH v2 3/3] gpio: gpio-ltc4283: Add support for the LTC4283 Swap Controller"
https://lore.kernel.org/[email protected]
- "[PATCH v7 15/24] media: i2c: add Maxim GMSL2/3 serializer and deserializer framework"
https://lore.kernel.org/[email protected]
[2] "[PATCH v5 00/23] Non-const bitfield helpers"

    https://lore.kernel.org/all/[email protected]
[3] "[PATCH v4 0/4] Non-const bitfield helpers"
    https://lore.kernel.org/[email protected]
[4] "[PATCH v3 0/4] Non-const bitfield helpers"
    https://lore.kernel.org/all/[email protected]
[5] "[PATCH v2 0/3] Non-const bitfield helpers"
    https://lore.kernel.org/all/[email protected]
[6] "[PATCH 00/17] Non-const bitfield helper conversions"
    https://lore.kernel.org/all/[email protected]

Geert Uytterhoeven (26):
clk: at91: pmc: #undef field_{get,prep}() before definition
crypto: qat - #undef field_get() before local definition EDAC/ie31200: #undef field_get() before local definition
gpio: aspeed: #undef field_{get,prep}() before local definition
iio: dac: ad3530r: #undef field_prep() before local definition
iio: mlx90614: #undef field_{get,prep}() before local definition
pinctrl: ma35: #undef field_{get,prep}() before local definition
soc: renesas: rz-sysc: #undef field_get() before local definition
ALSA: usb-audio: #undef field_{get,prep}() before local definition [-next] iio: imu: smi330: #undef field_{get,prep}() before definition [-next] mtd: rawnand: sunxi: #undef field_{get,prep}() before local
definition
bitfield: Add less-checking __FIELD_{GET,PREP}()
bitfield: Add non-constant field_{prep,get}() helpers
clk: at91: Convert to common field_{get,prep}() helpers
crypto: qat - convert to common field_get() helper EDAC/ie31200: Convert to common field_get() helper
gpio: aspeed: Convert to common field_{get,prep}() helpers
iio: dac: Convert to common field_prep() helper
iio: mlx90614: Convert to common field_{get,prep}() helpers
pinctrl: ma35: Convert to common field_{get,prep}() helpers
soc: renesas: rz-sysc: Convert to common field_get() helper
ALSA: usb-audio: Convert to common field_{get,prep}() helpers [-next] iio: imu: smi330: Convert to common field_{get,prep}() helpers [-next] mtd: rawnand: sunxi: Convert to common field_{get,prep}()
helpers
clk: renesas: Use bitfield helpers
soc: renesas: Use bitfield helpers

 drivers/clk/at91/clk-peripheral.c             |  1 +
 drivers/clk/at91/pmc.h                        |  3 -
 drivers/clk/renesas/clk-div6.c                |  6 +-
 drivers/clk/renesas/rcar-gen3-cpg.c           | 15 +--
 drivers/clk/renesas/rcar-gen4-cpg.c           |  9 +-
 .../intel/qat/qat_common/adf_pm_dbgfs_utils.c |  8 +-
 drivers/edac/ie31200_edac.c                   |  4 +-
 drivers/gpio/gpio-aspeed.c                    |  5 +-
 drivers/iio/dac/ad3530r.c                     |  3 -
 drivers/iio/imu/smi330/smi330_core.c          |  4 -
 drivers/iio/temperature/mlx90614.c            |  5 +-
 drivers/mtd/nand/raw/sunxi_nand.c             |  4 -
 drivers/pinctrl/nuvoton/pinctrl-ma35.c        |  4 -
 drivers/soc/renesas/renesas-soc.c             |  4 +-
 drivers/soc/renesas/rz-sysc.c                 |  3 +-
 include/linux/bitfield.h                      | 95 +++++++++++++++++--
 sound/usb/mixer_quirks.c                      |  4 -
 17 files changed, 106 insertions(+), 71 deletions(-)
Signature · 12 lines
2.43.0

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
							    -- Linus Torvalds