Skip to content

Commit 0a0ed86

Browse files
committed
generic: backport field_prep()/get() for non-const bitmask
Backport new field_prep()/get() particularly useful to handle case where a bitmask is not const and FIELD_PREP can't be used. This permit to replace manual shift with these macro. (also needed to permit backport of some patch without modification) Backport reworked patch that drop the local field_prep()/get() macro in favor of the global one. Link: openwrt/openwrt#22479 (cherry picked from commit 25f92aa) Link: openwrt/openwrt#22820 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
1 parent 659df25 commit 0a0ed86

6 files changed

Lines changed: 350 additions & 0 deletions
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
From 2a6c045640c38a407a39cd40c3c4d8dd2fd89aa8 Mon Sep 17 00:00:00 2001
2+
From: Geert Uytterhoeven <geert+renesas@glider.be>
3+
Date: Thu, 6 Nov 2025 14:34:00 +0100
4+
Subject: [PATCH 1/2] bitfield: Add less-checking __FIELD_{GET,PREP}()
5+
6+
The BUILD_BUG_ON_MSG() check against "~0ull" works only with "unsigned
7+
(long) long" _mask types. For constant masks, that condition is usually
8+
met, as GENMASK() yields an UL value. The few places where the
9+
constant mask is stored in an intermediate variable were fixed by
10+
changing the variable type to u64 (see e.g. [1] and [2]).
11+
12+
However, for non-constant masks, smaller unsigned types should be valid,
13+
too, but currently lead to "result of comparison of constant
14+
18446744073709551615 with expression of type ... is always
15+
false"-warnings with clang and W=1.
16+
17+
Hence refactor the __BF_FIELD_CHECK() helper, and factor out
18+
__FIELD_{GET,PREP}(). The later lack the single problematic check, but
19+
are otherwise identical to FIELD_{GET,PREP}(), and are intended to be
20+
used in the fully non-const variants later.
21+
22+
[1] commit 5c667d5a5a3ec166 ("clk: sp7021: Adjust width of _m in
23+
HWM_FIELD_PREP()")
24+
[2] commit cfd6fb45cfaf46fa ("crypto: ccree - avoid out-of-range
25+
warnings from clang")
26+
27+
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
28+
Link: https://git.kernel.org/torvalds/c/5c667d5a5a3ec166 [1]
29+
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
30+
---
31+
include/linux/bitfield.h | 36 ++++++++++++++++++++++++++++--------
32+
1 file changed, 28 insertions(+), 8 deletions(-)
33+
34+
--- a/include/linux/bitfield.h
35+
+++ b/include/linux/bitfield.h
36+
@@ -60,7 +60,7 @@
37+
38+
#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
39+
40+
-#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
41+
+#define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \
42+
({ \
43+
BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
44+
_pfx "mask is not constant"); \
45+
@@ -69,13 +69,33 @@
46+
~((_mask) >> __bf_shf(_mask)) & \
47+
(0 + (_val)) : 0, \
48+
_pfx "value too large for the field"); \
49+
- BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \
50+
- __bf_cast_unsigned(_reg, ~0ull), \
51+
- _pfx "type of reg too small for mask"); \
52+
__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
53+
(1ULL << __bf_shf(_mask))); \
54+
})
55+
56+
+#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \
57+
+ BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \
58+
+ __bf_cast_unsigned(reg, ~0ull), \
59+
+ pfx "type of reg too small for mask")
60+
+
61+
+#define __BF_FIELD_CHECK(mask, reg, val, pfx) \
62+
+ ({ \
63+
+ __BF_FIELD_CHECK_MASK(mask, val, pfx); \
64+
+ __BF_FIELD_CHECK_REG(mask, reg, pfx); \
65+
+ })
66+
+
67+
+#define __FIELD_PREP(mask, val, pfx) \
68+
+ ({ \
69+
+ __BF_FIELD_CHECK_MASK(mask, val, pfx); \
70+
+ ((typeof(mask))(val) << __bf_shf(mask)) & (mask); \
71+
+ })
72+
+
73+
+#define __FIELD_GET(mask, reg, pfx) \
74+
+ ({ \
75+
+ __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \
76+
+ (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \
77+
+ })
78+
+
79+
/**
80+
* FIELD_MAX() - produce the maximum value representable by a field
81+
* @_mask: shifted mask defining the field's length and position
82+
@@ -112,8 +132,8 @@
83+
*/
84+
#define FIELD_PREP(_mask, _val) \
85+
({ \
86+
- __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
87+
- ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
88+
+ __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \
89+
+ __FIELD_PREP(_mask, _val, "FIELD_PREP: "); \
90+
})
91+
92+
#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
93+
@@ -152,8 +172,8 @@
94+
*/
95+
#define FIELD_GET(_mask, _reg) \
96+
({ \
97+
- __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
98+
- (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
99+
+ __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \
100+
+ __FIELD_GET(_mask, _reg, "FIELD_GET: "); \
101+
})
102+
103+
extern void __compiletime_error("value doesn't fit into mask")
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
From c1c6ab80b25c8db1e2ef5ae3ac8075d2c242ae13 Mon Sep 17 00:00:00 2001
2+
From: Geert Uytterhoeven <geert+renesas@glider.be>
3+
Date: Thu, 6 Nov 2025 14:34:01 +0100
4+
Subject: [PATCH 2/2] bitfield: Add non-constant field_{prep,get}() helpers
5+
MIME-Version: 1.0
6+
Content-Type: text/plain; charset=UTF-8
7+
Content-Transfer-Encoding: 8bit
8+
9+
The existing FIELD_{GET,PREP}() macros are limited to compile-time
10+
constants. However, it is very common to prepare or extract bitfield
11+
elements where the bitfield mask is not a compile-time constant.
12+
13+
To avoid this limitation, the AT91 clock driver and several other
14+
drivers already have their own non-const field_{prep,get}() macros.
15+
Make them available for general use by adding them to
16+
<linux/bitfield.h>, and improve them slightly:
17+
1. Avoid evaluating macro parameters more than once,
18+
2. Replace "ffs() - 1" by "__ffs()",
19+
3. Support 64-bit use on 32-bit architectures,
20+
4. Wire field_{get,prep}() to FIELD_{GET,PREP}() when mask is
21+
actually constant.
22+
23+
This is deliberately not merged into the existing FIELD_{GET,PREP}()
24+
macros, as people expressed the desire to keep stricter variants for
25+
increased safety, or for performance critical paths.
26+
27+
Yury: use __mask withing new macros.
28+
29+
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
30+
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
31+
Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
32+
Acked-by: Crt Mori <cmo@melexis.com>
33+
Acked-by: Nuno Sá <nuno.sa@analog.com>
34+
Acked-by: Richard Genoud <richard.genoud@bootlin.com>
35+
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
36+
Reviewed-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
37+
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
38+
---
39+
include/linux/bitfield.h | 59 ++++++++++++++++++++++++++++++++++++++++
40+
1 file changed, 59 insertions(+)
41+
42+
--- a/include/linux/bitfield.h
43+
+++ b/include/linux/bitfield.h
44+
@@ -16,6 +16,7 @@
45+
* FIELD_{GET,PREP} macros take as first parameter shifted mask
46+
* from which they extract the base mask and shift amount.
47+
* Mask must be a compilation time constant.
48+
+ * field_{get,prep} are variants that take a non-const mask.
49+
*
50+
* Example:
51+
*
52+
@@ -223,4 +224,62 @@ __MAKE_OP(64)
53+
#undef __MAKE_OP
54+
#undef ____MAKE_OP
55+
56+
+#define __field_prep(mask, val) \
57+
+ ({ \
58+
+ __auto_type __mask = (mask); \
59+
+ typeof(__mask) __val = (val); \
60+
+ unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
61+
+ __ffs(__mask) : __ffs64(__mask); \
62+
+ (__val << __shift) & __mask; \
63+
+ })
64+
+
65+
+#define __field_get(mask, reg) \
66+
+ ({ \
67+
+ __auto_type __mask = (mask); \
68+
+ typeof(__mask) __reg = (reg); \
69+
+ unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
70+
+ __ffs(__mask) : __ffs64(__mask); \
71+
+ (__reg & __mask) >> __shift; \
72+
+ })
73+
+
74+
+/**
75+
+ * field_prep() - prepare a bitfield element
76+
+ * @mask: shifted mask defining the field's length and position, must be
77+
+ * non-zero
78+
+ * @val: value to put in the field
79+
+ *
80+
+ * Return: field value masked and shifted to its final destination
81+
+ *
82+
+ * field_prep() masks and shifts up the value. The result should be
83+
+ * combined with other fields of the bitfield using logical OR.
84+
+ * Unlike FIELD_PREP(), @mask is not limited to a compile-time constant.
85+
+ * Typical usage patterns are a value stored in a table, or calculated by
86+
+ * shifting a constant by a variable number of bits.
87+
+ * If you want to ensure that @mask is a compile-time constant, please use
88+
+ * FIELD_PREP() directly instead.
89+
+ */
90+
+#define field_prep(mask, val) \
91+
+ (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \
92+
+ : __field_prep(mask, val))
93+
+
94+
+/**
95+
+ * field_get() - extract a bitfield element
96+
+ * @mask: shifted mask defining the field's length and position, must be
97+
+ * non-zero
98+
+ * @reg: value of entire bitfield
99+
+ *
100+
+ * Return: extracted field value
101+
+ *
102+
+ * field_get() extracts the field specified by @mask from the
103+
+ * bitfield passed in as @reg by masking and shifting it down.
104+
+ * Unlike FIELD_GET(), @mask is not limited to a compile-time constant.
105+
+ * Typical usage patterns are a value stored in a table, or calculated by
106+
+ * shifting a constant by a variable number of bits.
107+
+ * If you want to ensure that @mask is a compile-time constant, please use
108+
+ * FIELD_GET() directly instead.
109+
+ */
110+
+#define field_get(mask, reg) \
111+
+ (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \
112+
+ : __field_get(mask, reg))
113+
+
114+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
From b1cff2f4b2391a13bd3e9263502072df1ee5d035 Mon Sep 17 00:00:00 2001
2+
From: Geert Uytterhoeven <geert+renesas@glider.be>
3+
Date: Thu, 6 Nov 2025 14:34:10 +0100
4+
Subject: [PATCH] ALSA: usb-audio: Convert to common field_{get,prep}() helpers
5+
6+
Drop the driver-specific field_get() and field_prep() macros, in favor
7+
of the globally available variants from <linux/bitfield.h>.
8+
9+
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
10+
Acked-by: Takashi Iwai <tiwai@suse.de>
11+
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
12+
---
13+
sound/usb/mixer_quirks.c | 6 ------
14+
1 file changed, 6 deletions(-)
15+
16+
--- a/sound/usb/mixer_quirks.c
17+
+++ b/sound/usb/mixer_quirks.c
18+
@@ -3367,10 +3367,6 @@ static int snd_bbfpro_controls_create(st
19+
#define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
20+
#define RME_DIGIFACE_INVERT BIT(31)
21+
22+
-/* Nonconst helpers */
23+
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
24+
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
25+
-
26+
static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
27+
{
28+
struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
From 0f8407a1f1c795c417e4c7750654a6024a3ec68b Mon Sep 17 00:00:00 2001
2+
From: Geert Uytterhoeven <geert+renesas@glider.be>
3+
Date: Thu, 6 Nov 2025 14:34:02 +0100
4+
Subject: [PATCH] clk: at91: Convert to common field_{get,prep}() helpers
5+
6+
Drop the driver-specific field_get() and field_prep() macros, in favor
7+
of the globally available variants from <linux/bitfield.h>.
8+
9+
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
10+
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
11+
Acked-by: Stephen Boyd <sboyd@kernel.org>
12+
Acked-by: Claudiu Beznea <claudiu.beznea@tuxon.dev>
13+
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
14+
---
15+
drivers/clk/at91/clk-peripheral.c | 1 +
16+
drivers/clk/at91/pmc.h | 5 -----
17+
2 files changed, 1 insertion(+), 5 deletions(-)
18+
19+
--- a/drivers/clk/at91/clk-peripheral.c
20+
+++ b/drivers/clk/at91/clk-peripheral.c
21+
@@ -3,6 +3,7 @@
22+
* Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
23+
*/
24+
25+
+#include <linux/bitfield.h>
26+
#include <linux/bitops.h>
27+
#include <linux/clk-provider.h>
28+
#include <linux/clkdev.h>
29+
--- a/drivers/clk/at91/pmc.h
30+
+++ b/drivers/clk/at91/pmc.h
31+
@@ -116,9 +116,6 @@ struct at91_clk_pms {
32+
unsigned int parent;
33+
};
34+
35+
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
36+
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
37+
-
38+
#define ndck(a, s) (a[s - 1].id + 1)
39+
#define nck(a) (a[ARRAY_SIZE(a) - 1].id + 1)
40+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
From 1fe1c28a108e4953f083c0106575ee0eccc296ae Mon Sep 17 00:00:00 2001
2+
From: Geert Uytterhoeven <geert+renesas@glider.be>
3+
Date: Thu, 6 Nov 2025 14:34:07 +0100
4+
Subject: [PATCH] iio: mlx90614: Convert to common field_{get,prep}() helpers
5+
6+
Drop the driver-specific field_get() and field_prep() macros, in favor
7+
of the globally available variants from <linux/bitfield.h>.
8+
9+
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
10+
Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
11+
Acked-by: Crt Mori <cmo@melexis.com>
12+
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
13+
---
14+
drivers/iio/temperature/mlx90614.c | 7 +------
15+
1 file changed, 1 insertion(+), 6 deletions(-)
16+
17+
--- a/drivers/iio/temperature/mlx90614.c
18+
+++ b/drivers/iio/temperature/mlx90614.c
19+
@@ -22,6 +22,7 @@
20+
* the "wakeup" GPIO is not given, power management will be disabled.
21+
*/
22+
23+
+#include <linux/bitfield.h>
24+
#include <linux/delay.h>
25+
#include <linux/err.h>
26+
#include <linux/gpio/consumer.h>
27+
@@ -68,10 +69,6 @@
28+
#define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */
29+
#define MLX90614_CONST_FIR 0x7 /* Fixed value for FIR part of low pass filter */
30+
31+
-/* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */
32+
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
33+
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
34+
-
35+
struct mlx_chip_info {
36+
/* EEPROM offsets with 16-bit data, MSB first */
37+
/* emissivity correction coefficient */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
From bb0e7fda87753a973cb4a86c22905b1177f00d4e Mon Sep 17 00:00:00 2001
2+
From: Geert Uytterhoeven <geert+renesas@glider.be>
3+
Date: Thu, 6 Nov 2025 14:34:08 +0100
4+
Subject: [PATCH] pinctrl: ma35: Convert to common field_{get,prep}() helpers
5+
6+
Drop the driver-specific field_get() and field_prep() macros, in favor
7+
of the globally available variants from <linux/bitfield.h>.
8+
9+
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
10+
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
11+
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
12+
---
13+
drivers/pinctrl/nuvoton/pinctrl-ma35.c | 6 ------
14+
1 file changed, 6 deletions(-)
15+
16+
--- a/drivers/pinctrl/nuvoton/pinctrl-ma35.c
17+
+++ b/drivers/pinctrl/nuvoton/pinctrl-ma35.c
18+
@@ -81,10 +81,6 @@
19+
#define MVOLT_1800 0
20+
#define MVOLT_3300 1
21+
22+
-/* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */
23+
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
24+
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
25+
-
26+
static const char * const gpio_group_name[] = {
27+
"gpioa", "gpiob", "gpioc", "gpiod", "gpioe", "gpiof", "gpiog",
28+
"gpioh", "gpioi", "gpioj", "gpiok", "gpiol", "gpiom", "gpion",

0 commit comments

Comments
 (0)