Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

linux/bits.h: fix compilation error with GENMASK

GENMASK() has an input check which uses __builtin_choose_expr() to
enable a compile time sanity check of its inputs if they are known at
compile time.

However, it turns out that __builtin_constant_p() does not always return
a compile time constant [0]. It was thought this problem was fixed with
gcc 4.9 [1], but apparently this is not the case [2].

Switch to use __is_constexpr() instead which always returns a compile time
constant, regardless of its inputs.

Link: https://lore.kernel.org/lkml/42b4342b-aefc-a16a-0d43-9f9c0d63ba7a@rasmusvillemoes.dk [0]
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449 [1]
Link: https://lore.kernel.org/lkml/1ac7bbc2-45d9-26ed-0b33-bf382b8d858b@I-love.SAKURA.ne.jp [2]
Link: https://lkml.kernel.org/r/20210511203716.117010-1-rikard.falkeborn@gmail.com
Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Yury Norov <yury.norov@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Rikard Falkeborn and committed by
Linus Torvalds
f747e666 0f90b88d

+20 -10
+1 -1
include/linux/bits.h
··· 22 22 #include <linux/build_bug.h> 23 23 #define GENMASK_INPUT_CHECK(h, l) \ 24 24 (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ 25 - __builtin_constant_p((l) > (h)), (l) > (h), 0))) 25 + __is_constexpr((l) > (h)), (l) > (h), 0))) 26 26 #else 27 27 /* 28 28 * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
+8
include/linux/const.h
··· 3 3 4 4 #include <vdso/const.h> 5 5 6 + /* 7 + * This returns a constant expression while determining if an argument is 8 + * a constant expression, most importantly without evaluating the argument. 9 + * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de> 10 + */ 11 + #define __is_constexpr(x) \ 12 + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) 13 + 6 14 #endif /* _LINUX_CONST_H */
+2 -8
include/linux/minmax.h
··· 2 2 #ifndef _LINUX_MINMAX_H 3 3 #define _LINUX_MINMAX_H 4 4 5 + #include <linux/const.h> 6 + 5 7 /* 6 8 * min()/max()/clamp() macros must accomplish three things: 7 9 * ··· 18 16 */ 19 17 #define __typecheck(x, y) \ 20 18 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) 21 - 22 - /* 23 - * This returns a constant expression while determining if an argument is 24 - * a constant expression, most importantly without evaluating the argument. 25 - * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de> 26 - */ 27 - #define __is_constexpr(x) \ 28 - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) 29 19 30 20 #define __no_side_effects(x, y) \ 31 21 (__is_constexpr(x) && __is_constexpr(y))
+1 -1
tools/include/linux/bits.h
··· 22 22 #include <linux/build_bug.h> 23 23 #define GENMASK_INPUT_CHECK(h, l) \ 24 24 (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ 25 - __builtin_constant_p((l) > (h)), (l) > (h), 0))) 25 + __is_constexpr((l) > (h)), (l) > (h), 0))) 26 26 #else 27 27 /* 28 28 * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
+8
tools/include/linux/const.h
··· 3 3 4 4 #include <vdso/const.h> 5 5 6 + /* 7 + * This returns a constant expression while determining if an argument is 8 + * a constant expression, most importantly without evaluating the argument. 9 + * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de> 10 + */ 11 + #define __is_constexpr(x) \ 12 + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) 13 + 6 14 #endif /* _LINUX_CONST_H */