[PATCH v2 10/16] bitfield: Split the 'val' check out of __BF_FIELD_CHECK_MASK()

david.laight.linux
2025-12-12T19:38:10Z

From: David Laight <david.laight.linux@gmail.com>

Three of the five calls to __BF_FIELD_CHECK_MASK() don't have a 'value'
to check, separate out as was done to __BF_FIELD_CHECK_REG().

Since __BF_FIELD_CHECK_MASK() doesn't return a value, use
do { ... } while (0) rather than ({ ... }).

There is no point checking a 'val' of zero or a 'reg' of 0ULL (both
are placeholders) - remove/change the calls.

There should be a check of __BF_FIELD_CHECK_REG() when __BF_FIELD_GET()
is called from field_get().
Move the check from FIELD_GET() into __BF_FIELD_GET().

Delete the now-unused __BF_FIELD_CHECK().

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
include/linux/bitfield.h | 38 +++++++++++++++++---------------------
1 file changed, 17 insertions(+), 21 deletions(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index ed5735c13a64..138f4c14786d 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -45,39 +45,36 @@

#define __bf_shf(x) (__builtin_ffsll(x) - 1)

-#define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \
- ({ \
+#define __BF_FIELD_CHECK_MASK(_mask, _pfx) \
+ do { \
BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
_pfx "mask is not constant"); \
BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
- BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
- ~((_mask) >> __bf_shf(_mask)) & \
- (0 + (_val)) : 0, \
- _pfx "value too large for the field"); \
__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
(1ULL << __bf_shf(_mask))); \
- })
+ } while (0)
+
+#define __BF_FIELD_CHECK_VAL(mask, val, pfx) \
+ BUILD_BUG_ON_MSG(__builtin_constant_p(val) && \
+ ~((mask) >> __bf_shf(mask)) & (val), \
+ pfx "value too large for the field")

#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \
BUILD_BUG_ON_MSG((mask) + 0U + 0UL + 0ULL > \
~0ULL >> (64 - 8 * sizeof (reg)), \
pfx "type of reg too small for mask")

-#define __BF_FIELD_CHECK(mask, reg, val, pfx) \
- ({ \
- __BF_FIELD_CHECK_MASK(mask, val, pfx); \
- __BF_FIELD_CHECK_REG(mask, reg, pfx); \
- })
-
#define __BF_FIELD_PREP(mask, val, pfx) \
({ \
- __BF_FIELD_CHECK_MASK(mask, val, pfx); \
+ __BF_FIELD_CHECK_MASK(mask, pfx); \
+ __BF_FIELD_CHECK_VAL(mask, val, pfx); \
((val) << __bf_shf(mask)) & (mask); \
})

#define __BF_FIELD_GET(mask, reg, pfx) \
({ \
- __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \
+ __BF_FIELD_CHECK_MASK(mask, pfx); \
+ __BF_FIELD_CHECK_REG(mask, reg, pfx); \
((reg) & (mask)) >> __bf_shf(mask); \
})

@@ -91,7 +88,7 @@
#define FIELD_MAX(mask) \
({ \
__auto_type _mask = mask; \
- __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
+ __BF_FIELD_CHECK_MASK(_mask, "FIELD_MAX: "); \
(_mask >> __bf_shf(_mask)); \
})

@@ -106,7 +103,7 @@
({ \
__auto_type _mask = mask; \
__auto_type _val = 1 ? (val) : _mask; \
- __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
+ __BF_FIELD_CHECK_MASK(_mask, "FIELD_FIT: "); \
!((_val << __bf_shf(_mask)) & ~_mask); \
})

@@ -122,7 +119,6 @@
({ \
__auto_type _mask = mask; \
__auto_type _val = 1 ? (val) : _mask; \
- __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \
__BF_FIELD_PREP(_mask, _val, "FIELD_PREP: "); \
})

@@ -164,7 +160,6 @@
({ \
__auto_type _mask = mask; \
__auto_type _reg = reg; \
- __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \
__BF_FIELD_GET(_mask, _reg, "FIELD_GET: "); \
})

@@ -182,8 +177,9 @@
__auto_type _mask = mask; \
__auto_type _reg_p = reg_p; \
__auto_type _val = 1 ? (val) : _mask; \
- typecheck_pointer(_reg_p); \
- __BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: "); \
+ __BF_FIELD_CHECK_MASK(_mask, "FIELD_MODIFY: "); \
+ __BF_FIELD_CHECK_VAL(_mask, _val, "FIELD_MODIFY: "); \
+ __BF_FIELD_CHECK_REG(_mask, *_reg_p, "FIELD_MODIFY: "); \
*_reg_p = (*_reg_p & ~_mask) | ((_val << __bf_shf(_mask)) & _mask); \
})

--
2.39.5