All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
To: ndesaulniers@google.com
Cc: peterz@infradead.org, llvm@lists.linux.dev,
	ashutosh.dixit@intel.com, andi.shyti@linux.intel.com,
	linux-kernel@vger.kernel.org
Subject: [PATCH] bitfield: Use argument type for size comparison on Bitfield access macros
Date: Sat, 29 Oct 2022 08:34:29 +0300	[thread overview]
Message-ID: <20221029053429.38381-1-gwan-gyeong.mun@intel.com> (raw)

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


             reply	other threads:[~2022-10-29  5:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-29  5:34 Gwan-gyeong Mun [this message]
2022-10-29 13:34 ` [PATCH] bitfield: Use argument type for size comparison on Bitfield access macros 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
2022-10-29 19:18 kernel test robot
2022-11-01  7:44 kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221029053429.38381-1-gwan-gyeong.mun@intel.com \
    --to=gwan-gyeong.mun@intel.com \
    --cc=andi.shyti@linux.intel.com \
    --cc=ashutosh.dixit@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.