lkml.org 
[lkml]   [2019]   [Oct]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [Patch v4 2/2] linux/bits.h: Add compile time sanity check of GENMASK inputs
On Fri, Oct 11, 2019 at 07:27:37PM -0700, Andrew Morton wrote:
> On Wed, 9 Oct 2019 23:45:02 +0200 Rikard Falkeborn <rikard.falkeborn@gmail.com> 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" },
> ^~~~~~~
>

As of next-20191021, those breakages are fixed ([0], [1]). next-20191021
now builds (at least for x86-64) with the GENMASK compile checks added.

I also stumbled upon [2], which is a fix for an unused macro in a header
in mips/include/asm. next-201008 contained both v3 of this patch and the
mips header with the bad GENMASK in without build regressions, so it
*should* be ok to include v4 (but given the track record of this patch,
I totally understand if you want to wait until [2] lands in mips-next and
linux-next).

Rikard

[0]: https://lore.kernel.org/patchwork/patch/1139777/
[1]: https://lore.kernel.org/patchwork/patch/1139778/
[2]: https://lore.kernel.org/linux-mips/20191022192547.480095-1-rikard.falkeborn@gmail.com/T/#u

\
 
 \ /
  Last update: 2019-10-22 21:53    [W:0.136 / U:0.096 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site