On Thu, 8 Feb 2024 at 09:45, Lucas De Marchi <lucas.demarchi@xxxxxxxxx> wrote: > > From: Yury Norov <yury.norov@xxxxxxxxx> > > Generalize __GENMASK() to support different types, and implement > fixed-types versions of GENMASK() based on it. The fixed-type version > allows more strict checks to the min/max values accepted, which is > useful for defining registers like implemented by i915 and xe drivers > with their REG_GENMASK*() macros. > > The strict checks rely on shift-count-overflow compiler check to > fail the build if a number outside of the range allowed is passed. > Example: > > #define FOO_MASK GENMASK_U32(33, 4) > > will generate a warning like: > > ../include/linux/bits.h:41:31: error: left shift count >= width of type [-Werror=shift-count-overflow] > 41 | (((t)~0ULL - ((t)(1) << (l)) + 1) & \ > | ^~ > > Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx> > Signed-off-by: Lucas De Marchi <lucas.demarchi@xxxxxxxxx> > Acked-by: Jani Nikula <jani.nikula@xxxxxxxxx> > --- > include/linux/bitops.h | 1 - > include/linux/bits.h | 32 ++++++++++++++++++++++---------- > 2 files changed, 22 insertions(+), 11 deletions(-) > > diff --git a/include/linux/bitops.h b/include/linux/bitops.h > index 2ba557e067fe..1db50c69cfdb 100644 > --- a/include/linux/bitops.h > +++ b/include/linux/bitops.h > @@ -15,7 +15,6 @@ > # define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n))) > #endif > > -#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) > #define BITS_TO_LONGS(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) > #define BITS_TO_U64(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64)) > #define BITS_TO_U32(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32)) > diff --git a/include/linux/bits.h b/include/linux/bits.h > index 7c0cf5031abe..bd56f32de44e 100644 > --- a/include/linux/bits.h > +++ b/include/linux/bits.h > @@ -6,6 +6,8 @@ > #include <vdso/bits.h> > #include <asm/bitsperlong.h> > > +#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) > + > #define BIT_MASK(nr) (UL(1) << ((nr) % BITS_PER_LONG)) > #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) > #define BIT_ULL_MASK(nr) (ULL(1) << ((nr) % BITS_PER_LONG_LONG)) > @@ -30,16 +32,26 @@ > #define GENMASK_INPUT_CHECK(h, l) 0 > #endif > > -#define __GENMASK(h, l) \ > - (((~UL(0)) - (UL(1) << (l)) + 1) & \ > - (~UL(0) >> (BITS_PER_LONG - 1 - (h)))) > -#define GENMASK(h, l) \ > - (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l)) > +/* > + * Generate a mask for the specified type @t. Additional checks are made to > + * guarantee the value returned fits in that type, relying on > + * shift-count-overflow compiler check to detect incompatible arguments. > + * For example, all these create build errors or warnings: > + * > + * - GENMASK(15, 20): wrong argument order > + * - GENMASK(72, 15): doesn't fit unsigned long > + * - GENMASK_U32(33, 15): doesn't fit in a u32 > + */ > +#define __GENMASK(t, h, l) \ > + (GENMASK_INPUT_CHECK(h, l) + \ > + (((t)~0ULL - ((t)(1) << (l)) + 1) & \ > + ((t)~0ULL >> (BITS_PER_TYPE(t) - 1 - (h))))) > > -#define __GENMASK_ULL(h, l) \ > - (((~ULL(0)) - (ULL(1) << (l)) + 1) & \ > - (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h)))) > -#define GENMASK_ULL(h, l) \ > - (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l)) > +#define GENMASK(h, l) __GENMASK(unsigned long, h, l) > +#define GENMASK_ULL(h, l) __GENMASK(unsigned long long, h, l) > +#define GENMASK_U8(h, l) __GENMASK(u8, h, l) > +#define GENMASK_U16(h, l) __GENMASK(u16, h, l) > +#define GENMASK_U32(h, l) __GENMASK(u32, h, l) > +#define GENMASK_U64(h, l) __GENMASK(u64, h, l) This breaks drm-tip on arm64 architecture: arch/arm64/kernel/entry-fpsimd.S: Assembler messages: 465arch/arm64/kernel/entry-fpsimd.S:271: Error: found 'l', expected: ')' 466arch/arm64/kernel/entry-fpsimd.S:66: Info: macro invoked from here 467arch/arm64/kernel/entry-fpsimd.S:271: Error: found 'l', expected: ')' 468arch/arm64/kernel/entry-fpsimd.S:66: Info: macro invoked from here 469arch/arm64/kernel/entry-fpsimd.S:271: Error: found 'l', expected: ')' 470arch/arm64/kernel/entry-fpsimd.S:66: Info: macro invoked from here 471arch/arm64/kernel/entry-fpsimd.S:271: Error: found 'l', expected: ')' 472arch/arm64/kernel/entry-fpsimd.S:66: Info: macro invoked from here 473arch/arm64/kernel/entry-fpsimd.S:271: Error: unexpected characters following instruction at operand 3 -- `bic x2,x1,(0+(((unsigned long)~0ULL-((unsigned long)(1)<<(0))+1)&((unsigned long)~0ULL>>((sizeof(unsigned long)*8)-1-(3)))))' 474arch/arm64/kernel/entry-fpsimd.S:66: Info: macro invoked from here 475arch/arm64/kernel/entry-fpsimd.S:282: Error: found 'l', expected: ')' 476arch/arm64/kernel/entry-fpsimd.S:98: Info: macro invoked from here 477arch/arm64/kernel/entry-fpsimd.S:282: Error: found 'l', expected: ')' 478arch/arm64/kernel/entry-fpsimd.S:98: Info: macro invoked from here 479arch/arm64/kernel/entry-fpsimd.S:282: Error: found 'l', expected: ')' 480arch/arm64/kernel/entry-fpsimd.S:98: Info: macro invoked from here 481arch/arm64/kernel/entry-fpsimd.S:282: Error: found 'l', expected: ')' 482arch/arm64/kernel/entry-fpsimd.S:98: Info: macro invoked from here 483arch/arm64/kernel/entry-fpsimd.S:282: Error: unexpected characters following instruction at operand 3 -- `bic x2,x1,(0+(((unsigned long)~0ULL-((unsigned long)(1)<<(0))+1)&((unsigned long)~0ULL>>((sizeof(unsigned long)*8)-1-(3)))))' 484arch/arm64/kernel/entry-fpsimd.S:98: Info: macro invoked from here > > #endif /* __LINUX_BITS_H */ > -- > 2.43.0 > -- With best wishes Dmitry