[PATCH v2 04/16] bitfield: Copy #define parameters to locals

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

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

Use __auto_type to take copies of parameters to both ensure they are
evaluated only once and to avoid bloating the pre-processor output.
In particular 'mask' is likely to be GENMASK() and the expension
of FIELD_GET() is then about 18KB.

The 'val' parameter is sometimes a bitfield (so _auto_type cannot be
used), the type of _val also needs to be large enough to hold the
shifted value, using '__auto_type _val = 1 ? (val) : _mask;' solves
both problems.

Update kerneldoc to match the renamed (no leading _) formal parameters.

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

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 126dc5b380af..3800744281c7 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -99,40 +99,45 @@

/**
* FIELD_MAX() - produce the maximum value representable by a field
- * @_mask: shifted mask defining the field's length and position
+ * @mask: shifted mask defining the field's length and position
*
* FIELD_MAX() returns the maximum value that can be held in the field
- * specified by @_mask.
+ * specified by @mask.
*/
-#define FIELD_MAX(_mask) \
+#define FIELD_MAX(mask) \
({ \
+ __auto_type _mask = mask; \
__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
(typeof(_mask))((_mask) >> __bf_shf(_mask)); \
})

/**
* FIELD_FIT() - check if value fits in the field
- * @_mask: shifted mask defining the field's length and position
- * @_val: value to test against the field
+ * @mask: shifted mask defining the field's length and position
+ * @val: value to test against the field
*
- * Return: true if @_val can fit inside @_mask, false if @_val is too big.
+ * Return: true if @val can fit inside @mask, false if @val is too big.
*/
-#define FIELD_FIT(_mask, _val) \
+#define FIELD_FIT(mask, val) \
({ \
+ __auto_type _mask = mask; \
+ __auto_type _val = 1 ? (val) : _mask; \
__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
})

/**
* FIELD_PREP() - prepare a bitfield element
- * @_mask: shifted mask defining the field's length and position
- * @_val: value to put in the field
+ * @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.
*/
-#define FIELD_PREP(_mask, _val) \
+#define FIELD_PREP(mask, val) \
({ \
+ __auto_type _mask = mask; \
+ __auto_type _val = 1 ? (val) : _mask; \
__BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \
__FIELD_PREP(_mask, _val, "FIELD_PREP: "); \
})
@@ -165,29 +170,34 @@

/**
* FIELD_GET() - extract a bitfield element
- * @_mask: shifted mask defining the field's length and position
- * @_reg: value of entire bitfield
+ * @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.
+ * FIELD_GET() extracts the field specified by @mask from the
+ * bitfield passed in as @reg by masking and shifting it down.
*/
-#define FIELD_GET(_mask, _reg) \
+#define FIELD_GET(mask, reg) \
({ \
+ __auto_type _mask = mask; \
+ __auto_type _reg = reg; \
__BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \
__FIELD_GET(_mask, _reg, "FIELD_GET: "); \
})

/**
* FIELD_MODIFY() - modify a bitfield element
- * @_mask: shifted mask defining the field's length and position
- * @_reg_p: pointer to the memory that should be updated
- * @_val: value to store in the bitfield
+ * @mask: shifted mask defining the field's length and position
+ * @reg_p: pointer to the memory that should be updated
+ * @val: value to store in the bitfield
*
- * FIELD_MODIFY() modifies the set of bits in @_reg_p specified by @_mask,
- * by replacing them with the bitfield value passed in as @_val.
+ * FIELD_MODIFY() modifies the set of bits in @reg_p specified by @mask,
+ * by replacing them with the bitfield value passed in as @val.
*/
-#define FIELD_MODIFY(_mask, _reg_p, _val) \
+#define FIELD_MODIFY(mask, reg_p, val) \
({ \
+ __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: "); \
*(_reg_p) &= ~(_mask); \
--
2.39.5