Re: [PATCH] linux/bits.h: Add compile time sanity check of GENMASK inputs

From: Masahiro Yamada
Date: Fri Aug 02 2019 - 23:12:32 EST


On Sat, Aug 3, 2019 at 3:19 AM Rikard Falkeborn
<rikard.falkeborn@xxxxxxxxx> wrote:
>
> On Fri, Aug 02, 2019 at 12:25:06PM +0900, Masahiro Yamada wrote:
> > On Fri, Aug 2, 2019 at 12:14 PM Joe Perches <joe@xxxxxxxxxxx> wrote:
> > >
> > > On Fri, 2019-08-02 at 10:40 +0900, Masahiro Yamada wrote:
> > > > On Thu, Aug 1, 2019 at 4:27 AM Joe Perches <joe@xxxxxxxxxxx> wrote:
> > > > > On Wed, 2019-07-31 at 21:03 +0200, Rikard Falkeborn wrote:
> > > > > > GENMASK() and GENMASK_ULL() are supposed to be called with the high bit
> > > > > > as the first argument and the low bit as the second argument. Mixing
> > > > > > them will return a mask with zero bits set.
> > > > > >
> > > > > > Recent commits show getting this wrong is not uncommon, see e.g.
> > > > > > commit aa4c0c9091b0 ("net: stmmac: Fix misuses of GENMASK macro") and
> > > > > > commit 9bdd7bb3a844 ("clocksource/drivers/npcm: Fix misuse of GENMASK
> > > > > > macro").
> > > > > >
> > > > > > To prevent such mistakes from appearing again, add compile time sanity
> > > > > > checking to the arguments of GENMASK() and GENMASK_ULL(). If both the
> > > > > > arguments are known at compile time, and the low bit is higher than the
> > > > > > high bit, break the build to detect the mistake immediately.
> > > > > >
> > > > > > Since GENMASK() is used in declarations, BUILD_BUG_OR_ZERO() must be
> > > > > > used instead of BUILD_BUG_ON(), and __is_constexpr() must be used instead
> > > > > > of __builtin_constant_p().
> > > > > >
> > > > > > Commit 95b980d62d52 ("linux/bits.h: make BIT(), GENMASK(), and friends
> > > > > > available in assembly") made the macros in linux/bits.h available in
> > > > > > assembly. Since neither BUILD_BUG_OR_ZERO() or __is_constexpr() are asm
> > > > > > compatible, disable the checks if the file is included in an asm file.
> > > > > >
> > > > > > Signed-off-by: Rikard Falkeborn <rikard.falkeborn@xxxxxxxxx>
> > > > > > ---
> > > > > > Joe Perches sent a series to fix the existing misuses of GENMASK() that
> > > > > > needs to be merged before this to avoid build failures. Currently, 7 of
> > > > > > the patches were not in Linus tree, and 2 were not in linux-next.
> > > > > >
> > > > > > Also, there's currently no asm users of bits.h, but since it was made
> > > > > > asm-compatible just two weeks ago it would be a shame to break it right
> > > > > > away...
> > > > > []
> > > > > > diff --git a/include/linux/bits.h b/include/linux/bits.h
> > > > > []
> > > > > > @@ -18,12 +18,22 @@
> > > > > > * position @h. For example
> > > > > > * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
> > > > > > */
> > > > > > +#ifndef __ASSEMBLY__
> > > > > > +#include <linux/build_bug.h>
> > > > > > +#define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
> > > > > > + __is_constexpr(h) && __is_constexpr(l), (l) > (h), 0))
> > > > > > +#else
> > > > > > +#define GENMASK_INPUT_CHECK(h, l) 0
> > > > >
> > > > > A few things:
> > > > >
> > > > > o Reading the final code is a bit confusing.
> > > > > Perhaps add a comment description saying it's not checked
> > > > > in asm .h uses.
> > > > >
> > > > > o Maybe use:
> > > > > #define GENMASK_INPUT_CHECK(h, l) UL(0)
> > > >
> > > > Why?
> > >
> > > Consistency with the uses in what's now called __GENMASK
> >
> > Inconsistent with __GENMASK_ULL.
>
> Would you prefer to add GENMASK_ULL_INPUT_CHECK?

No.

> Or replace UL(0) with
> 0 and then probably move the cast of BUILD_BUG_OR_ZERO (to avoid
> GENMASK be of type size_t) to GENMASK and GENMASK_ULL?

No.



Your original code is absolutely fine.


C aligns the types to the wider one.


(unsigned long) + (int) -> (unsigned long)
(unsigned long long) + (int) -> (unsigned long long)


Having GENMASK_INPUT_CHECK to return 'int' is OK.
The resulted GENMASK(), GENMASK_ULL() still
have unsigned long, unsigned long long, respectively.




BTW, v2 is already inconsistent.
If you wanted GENMASK_INPUT_CHECK() to return 'unsigned long',,
you would have to cast (low) > (high) as well:

(unsigned long)((low) > (high)), UL(0))))

This is totally redundant, and weird.





--
Best Regards
Masahiro Yamada