public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bitfield: Use argument type for size comparison on Bitfield access macros
@ 2022-10-29  5:34 Gwan-gyeong Mun
  2022-10-29 13:34 ` kernel test robot
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Gwan-gyeong Mun @ 2022-10-29  5:34 UTC (permalink / raw)
  To: ndesaulniers; +Cc: peterz, llvm, ashutosh.dixit, andi.shyti, linux-kernel

Fix the size comparison code that implicitly assumes that the mask argument
of bitfield access macros is an unsigned long long type.
If unsigned int type is used for mask, the first argument of Bitfield
access macros, and clang is used to compile, this [1] option causes a build
error.[2]

[1] [-Werror,-Wtautological-constant-out-of-range-compare]
[2] https://lore.kernel.org/intel-gfx/c1c548f8-71a8-0d4d-d591-58a0cd5dac20@intel.com

Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: llvm@lists.linux.dev
Cc: Ashutosh Dixit <ashutosh.dixit@intel.com>
Cc: Andi Shyti <andi.shyti@linux.intel.com>
Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
---
 include/linux/bitfield.h | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index c9be1657f03d..4382bd62b14f 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -9,6 +9,7 @@
 
 #include <linux/build_bug.h>
 #include <asm/byteorder.h>
+#include <linux/overflow.h>
 
 /*
  * Bitfield access macros
@@ -69,7 +70,8 @@
 				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
 				 _pfx "value too large for the field"); \
 		BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) >	\
-				 __bf_cast_unsigned(_reg, ~0ull),	\
+				 __bf_cast_unsigned(_reg,		\
+						    type_max(__unsigned_scalar_typeof(_reg))), \
 				 _pfx "type of reg too small for mask"); \
 		__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) +			\
 					      (1ULL << __bf_shf(_mask))); \
@@ -84,7 +86,10 @@
  */
 #define FIELD_MAX(_mask)						\
 	({								\
-		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: ");	\
+		__BF_FIELD_CHECK(_mask,					\
+				 type_min(__unsigned_scalar_typeof(_mask)), \
+				 type_min(__unsigned_scalar_typeof(_mask)), \
+				 "FIELD_MAX: ");			\
 		(typeof(_mask))((_mask) >> __bf_shf(_mask));		\
 	})
 
@@ -97,7 +102,10 @@
  */
 #define FIELD_FIT(_mask, _val)						\
 	({								\
-		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: ");	\
+		__BF_FIELD_CHECK(_mask,					\
+				 type_min(__unsigned_scalar_typeof(_mask)), \
+				 type_min(__unsigned_scalar_typeof(_val)), \
+				 "FIELD_FIT: ");			\
 		!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
 	})
 
@@ -111,7 +119,9 @@
  */
 #define FIELD_PREP(_mask, _val)						\
 	({								\
-		__BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: ");	\
+		__BF_FIELD_CHECK(_mask,					\
+				 type_min(__unsigned_scalar_typeof(_mask)), \
+				 _val, "FIELD_PREP: ");			\
 		((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask);	\
 	})
 
@@ -125,7 +135,9 @@
  */
 #define FIELD_GET(_mask, _reg)						\
 	({								\
-		__BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: ");	\
+		__BF_FIELD_CHECK(_mask, _reg,				\
+				 type_min(__unsigned_scalar_typeof(_reg)), \
+				 "FIELD_GET: ");			\
 		(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask));	\
 	})
 
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2022-11-02  1:46 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-29  5:34 [PATCH] bitfield: Use argument type for size comparison on Bitfield access macros Gwan-gyeong Mun
2022-10-29 13:34 ` kernel test robot
2022-10-29 13:45 ` kernel test robot
2022-10-29 14:05 ` kernel test robot
2022-10-29 15:05 ` kernel test robot
2022-10-29 16:46 ` kernel test robot
2022-11-01  8:24 ` kernel test robot
2022-11-01  8:45 ` kernel test robot
2022-11-01  9:05 ` kernel test robot
2022-11-01 14:18 ` kernel test robot
2022-11-01 14:59 ` kernel test robot
2022-11-01 22:13 ` kernel test robot
2022-11-02  0:35 ` kernel test robot
2022-11-02  1:46 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox