[PATCH] bitfield: use BUILD_BUG_ON_ZERO()

From: Johannes Berg
Date: Fri Oct 12 2018 - 05:21:23 EST


From: Johannes Berg <johannes.berg@xxxxxxxxx>

John Garry requested to be able to use FIELD_PREP() and friends
in constant initializers, so the thing we can do is switch all
the compile-time assertions to use BUILD_BUG_ON_ZERO() instead
of regular BUILD_BUG_ON().

The only downside is that we lose all the error messages, but
it doesn't seem worthwhile to duplicate all of this into e.g.
CONST_FIELD_PREP() (for constants) just for that.

Requested-by: John Garry <john.garry@xxxxxxxxxx>
Signed-off-by: Johannes Berg <johannes.berg@xxxxxxxxx>
---
include/linux/bitfield.h | 40 +++++++++++++++-------------------------
1 file changed, 15 insertions(+), 25 deletions(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 3f1ef4450a7c..1840dae6448a 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -49,19 +49,15 @@

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

-#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
- ({ \
- 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)) & (_val) : 0, \
- _pfx "value too large for the field"); \
- BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
- _pfx "type of reg too small for mask"); \
- __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
- (1ULL << __bf_shf(_mask))); \
- })
+#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
+
+#define __BF_FIELD_CHECK(_mask, _reg, _val) \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(_mask)) + \
+ BUILD_BUG_ON_ZERO((_mask) == 0) + \
+ BUILD_BUG_ON_ZERO(__builtin_constant_p(_val) ? \
+ ~((_mask) >> __bf_shf(_mask)) & (_val) : 0) + \
+ BUILD_BUG_ON_ZERO((_mask) > (typeof(_reg))~0ull) + \
+ __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask)))

/**
* FIELD_FIT() - check if value fits in the field
@@ -71,10 +67,8 @@
* Return: true if @_val can fit inside @_mask, false if @_val is too big.
*/
#define FIELD_FIT(_mask, _val) \
- ({ \
- __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \
- !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
- })
+ (__BF_FIELD_CHECK(_mask, 0ULL, _val) + \
+ !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)))

/**
* FIELD_PREP() - prepare a bitfield element
@@ -85,10 +79,8 @@
* be combined with other fields of the bitfield using logical OR.
*/
#define FIELD_PREP(_mask, _val) \
- ({ \
- __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
- ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
- })
+ (__BF_FIELD_CHECK(_mask, 0ULL, _val) + \
+ (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)))

/**
* FIELD_GET() - extract a bitfield element
@@ -99,10 +91,8 @@
* bitfield passed in as @_reg by masking and shifting it down.
*/
#define FIELD_GET(_mask, _reg) \
- ({ \
- __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
- (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
- })
+ (__BF_FIELD_CHECK(_mask, _reg, 0U) + \
+ ((typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask))))

extern void __compiletime_error("value doesn't fit into mask")
__field_overflow(void);
--
2.14.4