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

From: Andrew Morton
Date: Fri Oct 11 2019 - 22:28:10 EST


On Wed, 9 Oct 2019 23:45:02 +0200 Rikard Falkeborn <rikard.falkeborn@xxxxxxxxx> 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
> 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_ON_ZERO() must be
> used instead of BUILD_BUG_ON().
>
> __builtin_constant_p does not evaluate is argument, it only checks if it
> is a constant or not at compile time, and __builtin_choose_expr does not
> evaluate the expression that is not chosen. Therefore, GENMASK(x++, 0)
> does only evaluate x++ once.
>
> Commit 95b980d62d52 ("linux/bits.h: make BIT(), GENMASK(), and friends
> available in assembly") made the macros in linux/bits.h available in
> assembly. Since BUILD_BUG_OR_ZERO() is not asm compatible, disable the
> checks if the file is included in an asm file.
>
> Due to bugs in GCC versions before 4.9 [0], disable the check if
> building with a too old GCC compiler.
>
> [0]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449

I'm seeing some breakage in code which is newly added in linux-next:

sound/soc/codecs/tas2562.c: In function tas2562_set_dai_tdm_slot:
./include/linux/build_bug.h:16:51: error: negative width in bit-field <anonymous>
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/bits.h:25:3: note: in expansion of macro BUILD_BUG_ON_ZERO
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
^~~~~~~~~~~~~~~~~
./include/linux/bits.h:39:3: note: in expansion of macro GENMASK_INPUT_CHECK
(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
^~~~~~~~~~~~~~~~~~~
sound/soc/codecs/tas2562.h:65:37: note: in expansion of macro GENMASK
#define TAS2562_TDM_CFG2_RXLEN_MASK GENMASK(0, 1)
^~~~~~~
sound/soc/codecs/tas2562.c:160:11: note: in expansion of macro TAS2562_TDM_CFG2_RXLEN_MASK
TAS2562_TDM_CFG2_RXLEN_MASK,
^~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/build_bug.h:16:51: error: negative width in bit-field <anonymous>
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/bits.h:25:3: note: in expansion of macro BUILD_BUG_ON_ZERO
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \


and

In file included from ./include/linux/bits.h:23:0,
from ./include/linux/ioport.h:15,
from ./include/linux/acpi.h:12,
from drivers/crypto/hisilicon/hpre/hpre_main.c:3:
./include/linux/build_bug.h:16:51: error: negative width in bit-field â<anonymous>â
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/bits.h:25:3: note: in expansion of macro âBUILD_BUG_ON_ZEROâ
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
^~~~~~~~~~~~~~~~~
./include/linux/bits.h:39:3: note: in expansion of macro âGENMASK_INPUT_CHECKâ
(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
^~~~~~~~~~~~~~~~~~~
drivers/crypto/hisilicon/hpre/hpre_main.c:119:15: note: in expansion of macro âGENMASKâ
{ .int_msk = GENMASK(10, 15), .msg = "hpre_ooo_rdrsp_err" },
^~~~~~~
./include/linux/build_bug.h:16:51: error: negative width in bit-field â<anonymous>â
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/bits.h:25:3: note: in expansion of macro âBUILD_BUG_ON_ZEROâ
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
^~~~~~~~~~~~~~~~~
./include/linux/bits.h:39:3: note: in expansion of macro âGENMASK_INPUT_CHECKâ
(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
^~~~~~~~~~~~~~~~~~~
drivers/crypto/hisilicon/hpre/hpre_main.c:120:15: note: in expansion of macro âGENMASKâ
{ .int_msk = GENMASK(16, 21), .msg = "hpre_ooo_wrrsp_err" },
^~~~~~~