From: Lucas De Marchi <lucas.demarchi@intel.com>
Implement fixed-type BIT_U*() to help drivers add stricter checks,
like it was done for GENMASK_U*().
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
Acked-by: Jani Nikula <jani.nikula@intel.com>
Co-developed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
---
Changelog:
v6 -> v7:
v5 -> v6:
- No changes.
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 | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/include/linux/bits.h b/include/linux/bits.h
index beb3ee2f1bc74a9346dd72eb06c722a9bc536051..6a942ea9ab380d3bd0e521916caa1d59db8031c0 100644
--- a/include/linux/bits.h
+++ b/include/linux/bits.h
@@ -24,7 +24,7 @@
/*
* Missing asm support
*
- * GENMASK_U*() depend on BITS_PER_TYPE() which relies on sizeof(),
+ * GENMASK_U*() and BIT_U*() depend on BITS_PER_TYPE() which relies on sizeof(),
* something not available in asm. Nevertheless, fixed width integers is a C
* concept. Assembly code can rely on the long and long long versions instead.
*/
@@ -55,6 +55,24 @@
#define GENMASK_U32(h, l) GENMASK_TYPE(u32, h, l)
#define GENMASK_U64(h, l) GENMASK_TYPE(u64, 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__) */
/*
--
2.48.1
On Sat, Mar 22, 2025 at 06:23:13PM +0900, Vincent Mailhol via B4 Relay wrote: > From: Lucas De Marchi <lucas.demarchi@intel.com> > > Implement fixed-type BIT_U*() to help drivers add stricter checks, > like it was done for GENMASK_U*(). ... > +/* > + * Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The > + * following examples generate compiler warnings due to shift-count-overflow: "...due to -Wshift-count-overflow:" ? Same idea — if you need a new version, since it's just a nit-pick. > + * > + * - BIT_U8(8) > + * - BIT_U32(-1) > + * - BIT_U32(40) > + */ -- With Best Regards, Andy Shevchenko
On 24/03/2025 at 22:41, Andy Shevchenko wrote: > On Sat, Mar 22, 2025 at 06:23:13PM +0900, Vincent Mailhol via B4 Relay wrote: >> From: Lucas De Marchi <lucas.demarchi@intel.com> >> >> Implement fixed-type BIT_U*() to help drivers add stricter checks, >> like it was done for GENMASK_U*(). > > ... > >> +/* >> + * Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The >> + * following examples generate compiler warnings due to shift-count-overflow: > > "...due to -Wshift-count-overflow:" ? > > Same idea — if you need a new version, since it's just a nit-pick. If you want. I staged this change locally, so if there is a v8, it will be addressed. I applied the same to the previous patch which also mentioned shift-count-overflow without the -W prefix. But honestly, I am not convinced of the added value. This is from Lucas original patch one year ago, and no one was bothered by this. IMHO, when writing: (...) generate compiler warnings due to shift-count-overflow: I do not see where the ambiguity is. The sentence clearly say that this is a compiler warning, so with or without the -W prefix, the sentence is equally understandable. Yours sincerely, Vincent Mailhol
On Mon, Mar 24, 2025 at 11:16:30PM +0900, Vincent Mailhol wrote: > On 24/03/2025 at 22:41, Andy Shevchenko wrote: > > On Sat, Mar 22, 2025 at 06:23:13PM +0900, Vincent Mailhol via B4 Relay wrote: ... > >> +/* > >> + * Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The > >> + * following examples generate compiler warnings due to shift-count-overflow: > > > > "...due to -Wshift-count-overflow:" ? > > > > Same idea — if you need a new version, since it's just a nit-pick. > > If you want. I staged this change locally, so if there is a v8, it will > be addressed. I applied the same to the previous patch which also > mentioned shift-count-overflow without the -W prefix. > > But honestly, I am not convinced of the added value. This is from Lucas > original patch one year ago, and no one was bothered by this. IMHO, when > writing: > > (...) generate compiler warnings due to shift-count-overflow: > > I do not see where the ambiguity is. The sentence clearly say that this > is a compiler warning, so with or without the -W prefix, the sentence is > equally understandable. As I marked, it's a nit-pick, but from my point of view the added value is immediate: The reader can be sure that we are talking about a compiler warning and not something else (C standard? some special term?). So it adds more context and makes it clearer. -- With Best Regards, Andy Shevchenko
© 2016 - 2025 Red Hat, Inc.