[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250306-fixed-type-genmasks-v5-3-b443e9dcba63@wanadoo.fr>
Date: Thu, 06 Mar 2025 20:29:54 +0900
From: Vincent Mailhol via B4 Relay <devnull+mailhol.vincent.wanadoo.fr@...nel.org>
To: Yury Norov <yury.norov@...il.com>,
Lucas De Marchi <lucas.demarchi@...el.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Jani Nikula <jani.nikula@...ux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@...ux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@...el.com>,
Tvrtko Ursulin <tursulin@...ulin.net>, David Airlie <airlied@...il.com>,
Simona Vetter <simona@...ll.ch>, Andrew Morton <akpm@...ux-foundation.org>
Cc: linux-kernel@...r.kernel.org, intel-gfx@...ts.freedesktop.org,
dri-devel@...ts.freedesktop.org, Andi Shyti <andi.shyti@...ux.intel.com>,
David Laight <David.Laight@...LAB.COM>,
Dmitry Baryshkov <dmitry.baryshkov@...aro.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Jani Nikula <jani.nikula@...el.com>,
Vincent Mailhol <mailhol.vincent@...adoo.fr>
Subject: [PATCH v5 3/7] bits: introduce fixed-type BIT_U*()
From: Lucas De Marchi <lucas.demarchi@...el.com>
Implement fixed-type BIT_U*() to help drivers add stricter checks,
like was done for GENMASK_U*().
Signed-off-by: Lucas De Marchi <lucas.demarchi@...el.com>
Acked-by: Jani Nikula <jani.nikula@...el.com>
Co-developed-by: Vincent Mailhol <mailhol.vincent@...adoo.fr>
Signed-off-by: Vincent Mailhol <mailhol.vincent@...adoo.fr>
---
Changelog:
v4 -> v5:
- Rename GENMASK_t() to GENMASK_TYPE().
- Use tab indentations instead of single space to separate the
macro name from its body.
- Add a global comment at the beginning of the file to explain why
GENMASK_U*() and BIT_U*() are not available in asm.
- Add a new BIT_TYPE() helper function, similar to GENMASK_TYPE().
- Remove the unsigned int cast for the U8 and U16 variants. Move
the cast to BIT_TYPE().
- Rename the argument from BIT_U*(b) to BIT_U=(nr) for consistency
with vdso/bits.h.
v3 -> v4:
- Use const_true() to simplify BIT_INPUT_CHECK().
- Make BIT_U8() and BIT_U16() return an unsigned int instead of a
u8 and u16. Because of the integer promotion rules in C, an u8
or an u16 would become a signed integer as soon as these are
used in any expression. By casting these to unsigned ints, at
least the signedness is kept.
- Put the cast next to the BIT() macro.
- In BIT_U64(): use BIT_ULL() instead of BIT().
---
include/linux/bits.h | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/include/linux/bits.h b/include/linux/bits.h
index 74219521a56e2639ccff7fdc899d6805ee355a0c..f95e7815cb18636cc47ac17ef66d1bd6668e6819 100644
--- a/include/linux/bits.h
+++ b/include/linux/bits.h
@@ -22,10 +22,10 @@
/*
* Missing asm support
*
- * GENMASK_U*() depends on BITS_PER_TYPE() which relies on sizeof(),
- * something not available in asm. Nethertheless, fixed width integers
- * is a C concept. Assembly code can rely on the long and long long
- * versions instead.
+ * GENMASK_U*() and BIT_U*() depend on BITS_PER_TYPE() which relies on
+ * sizeof(), something not available in asm. Nethertheless, fixed
+ * width integers is a C concept. Assembly code can rely on the long
+ * and long long versions instead.
*/
#include <linux/build_bug.h>
@@ -58,6 +58,24 @@
#define GENMASK_U64(h, l) GENMASK_TYPE(u64, h, l)
#define GENMASK_U128(h, l) GENMASK_TYPE(u128, h, l)
+/*
+ * Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The
+ * following examples generate compiler warnings due to shift-count-overflow:
+ *
+ * - BIT_U8(8)
+ * - BIT_U32(-1)
+ * - BIT_U32(40)
+ */
+#define BIT_INPUT_CHECK(type, nr) \
+ BUILD_BUG_ON_ZERO(const_true((nr) >= BITS_PER_TYPE(type)))
+
+#define BIT_TYPE(type, nr) ((type)(BIT_INPUT_CHECK(type, nr) + BIT_ULL(nr)))
+
+#define BIT_U8(nr) BIT_TYPE(u8, nr)
+#define BIT_U16(nr) BIT_TYPE(u16, nr)
+#define BIT_U32(nr) BIT_TYPE(u32, nr)
+#define BIT_U64(nr) BIT_TYPE(u64, nr)
+
#else /* defined(__ASSEMBLY__) */
#define GENMASK(h, l) __GENMASK(h, l)
--
2.45.3
Powered by blists - more mailing lists