linux/bits.h: Fix compilation error with GENMASK
PATCH 1/1 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.
[0]: https://lore.kernel.org/lkml/[email protected]
[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449
[2]: https://lore.kernel.org/lkml/[email protected]
---
Feedback on placing __is_constexpr() in const.h is welcome, at least the name is appropriate...
include/linux/bits.h | 2 +- include/linux/const.h | 8 ++++++++ include/linux/minmax.h | 10 ++-------- tools/include/linux/bits.h | 2 +- tools/include/linux/const.h | 8 ++++++++ 5 files changed, 20 insertions(+), 10 deletions(-)
include/linux/bits.h +1 -1
@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
include/linux/const.h +8 -0
@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */
include/linux/minmax.h +2 -8
@@ -2,6 +2,8 @@#ifndef _LINUX_MINMAX_H#define _LINUX_MINMAX_H +#include <linux/const.h> +/* * min()/max()/clamp() macros must accomplish three things: *
@@ -17,14 +19,6 @@#define __typecheck (x, y) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) -/* -* This returns a constant expression while determining if an argument is -* a constant expression, most importantly without evaluating the argument. -* Glory to Martin Uecker <[email protected]> -*/ -#define __is_constexpr (x) \ - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) -#define __no_side_effects (x, y) \ (__is_constexpr(x) && __is_constexpr(y))
tools/include/linux/bits.h +1 -1
@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
tools/include/linux/const.h +8 -0
@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */
Signature · 1 line
2.31.1
(+ Arnd)
Quoted 118 lines · Rikard Falkeborn
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.[0]: https://lore.kernel.org/lkml/[email protected]
[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449
[2]: https://lore.kernel.org/lkml/[email protected]Reported-by Tetsuo Handa <[email protected]>Signed-off-by Rikard Falkeborn <[email protected]>---
Feedback on placing__is_constexpr()inconst.his welcome, at least the name is appropriate...include/linux/bits.h | 2 +- include/linux/const.h | 8 ++++++++ include/linux/minmax.h | 10 ++-------- tools/include/linux/bits.h | 2 +- tools/include/linux/const.h | 8 ++++++++ 5 files changed, 20 insertions(+), 10 deletions(-)include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, include/linux/const.h +8
-0@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ include/linux/minmax.h +2
-8@@ -2,6 +2,8 @@#ifndef _LINUX_MINMAX_H#define _LINUX_MINMAX_H +#include <linux/const.h> +/* * min()/max()/clamp() macros must accomplish three things: * @@ -17,14 +19,6 @@#define __typecheck (x, y) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) -/* -* This returns a constant expression while determining if an argument is -* a constant expression, most importantly without evaluating the argument. -* Glory to Martin Uecker <[email protected]> -*/ -#define __is_constexpr (x) \ - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) -#define __no_side_effects (x, y) \ (__is_constexpr(x) && __is_constexpr(y))tools/include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, tools/include/linux/const.h +8
-1@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ --2.31.1
Quoted 110 lines · Ard Biesheuvel
(+ Arnd)
Quoted 106 lines · Rikard Falkeborn
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.[0]: https://lore.kernel.org/lkml/[email protected]
[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449
[2]: https://lore.kernel.org/lkml/[email protected]Reported-by Tetsuo Handa <[email protected]>Signed-off-by Rikard Falkeborn <[email protected]>---
Feedback on placing__is_constexpr()inconst.his welcome, at least the name is appropriate...include/linux/bits.h | 2 +- include/linux/const.h | 8 ++++++++ include/linux/minmax.h | 10 ++-------- tools/include/linux/bits.h | 2 +- tools/include/linux/const.h | 8 ++++++++ 5 files changed, 20 insertions(+), 10 deletions(-)include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, include/linux/const.h +8
-0@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ include/linux/minmax.h +2
-8@@ -2,6 +2,8 @@#ifndef _LINUX_MINMAX_H#define _LINUX_MINMAX_H +#include <linux/const.h> +/* * min()/max()/clamp() macros must accomplish three things: * @@ -17,14 +19,6 @@#define __typecheck (x, y) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) -/* -* This returns a constant expression while determining if an argument is -* a constant expression, most importantly without evaluating the argument. -* Glory to Martin Uecker <[email protected]> -*/ -#define __is_constexpr (x) \ - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) -#define __no_side_effects (x, y) \ (__is_constexpr(x) && __is_constexpr(y))tools/include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, tools/include/linux/const.h +0
-0@@ -3,4 +3,12 @@#include <vdso/const.h>
Quoted 12 lines
Quoted 10 lines
tools/include/linux/const.h +8
-1+/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ --Quoted 2 lines
2.31.1
Friendly ping.
Rikard
Yury Norov
maintainerQuoted 111 lines · Rikard Falkeborn
Quoted 110 lines · Ard Biesheuvel
(+ Arnd)
Quoted 106 lines · Rikard Falkeborn
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.[0]: https://lore.kernel.org/lkml/[email protected]
[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449
[2]: https://lore.kernel.org/lkml/[email protected]Reported-by Tetsuo Handa <[email protected]>Signed-off-by Rikard Falkeborn <[email protected]>---
Feedback on placing__is_constexpr()inconst.his welcome, at least the name is appropriate...include/linux/bits.h | 2 +- include/linux/const.h | 8 ++++++++ include/linux/minmax.h | 10 ++-------- tools/include/linux/bits.h | 2 +- tools/include/linux/const.h | 8 ++++++++ 5 files changed, 20 insertions(+), 10 deletions(-)include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, include/linux/const.h +8
-0@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ include/linux/minmax.h +2
-8@@ -2,6 +2,8 @@#ifndef _LINUX_MINMAX_H#define _LINUX_MINMAX_H +#include <linux/const.h> +/* * min()/max()/clamp() macros must accomplish three things: * @@ -17,14 +19,6 @@#define __typecheck (x, y) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) -/* -* This returns a constant expression while determining if an argument is -* a constant expression, most importantly without evaluating the argument. -* Glory to Martin Uecker <[email protected]> -*/ -#define __is_constexpr (x) \ - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) -#define __no_side_effects (x, y) \ (__is_constexpr(x) && __is_constexpr(y))tools/include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, tools/include/linux/const.h +0
-0@@ -3,4 +3,12 @@#include <vdso/const.h>
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
tools/include/linux/const.h +4
-0+/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> Quoted 8 lines
Quoted 8 lines
tools/include/linux/const.h +4
-1+*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ --2.31.1
Friendly ping.
I added this patch in my Bitmap tree, but since it's actually a build bug fix, I think it's worth to move it faster with Arnd's or Andrew's trees?
Arnd Bergmann
Acked-byQuoted 16 lines · Yury Norov
Quoted 6 lines · Rikard Falkeborn
Quoted 6 lines
Quoted 6 lines
include/linux/const.h +6
-0+/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
include/linux/const.h +2
-1+ (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ --2.31.1
Friendly ping.
Quoted 3 lines
I added this patch in my Bitmap tree, but since it's actually a build bug fix, I think it's worth to move it faster with Arnd's or Andrew's trees?
I have a few days off next week and won't be able to validate it before then, so it would be faster to have Andrew pick it up.
Moving the definition to const.h seem reasonable to me here, feel free to add
include/linux/const.h line 12
Quoted 13 lines · On Tue, 11 May 2021 22:37:15 +0200 Rikard Falkeborn…
include/linux/const.h +7
-0@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
Boggle.
Could someone please sometime enhance that comment a bit? What need does this thing satisfy and how on earth does it work?
Quoted 18 lines · Andrew Morton
include/linux/const.h line 12
Quoted 13 lines · On Tue, 11 May 2021 22:37:15 +0200 Rikard Falkeborn…
include/linux/const.h +7
-0@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))Boggle.
Quoted 2 lines
Could someone please sometime enhance that comment a bit? What need does this thing satisfy and how on earth does it work?
Some summary based on (links from) https://vegard.wiki/w/is_constexpr() ?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Rikard Falkeborn
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].
Quoted 6 lines
Switch to use
__is_constexpr()instead which always returns a compile time constant, regardless of its inputs.[0]: https://lore.kernel.org/lkml/[email protected]
[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449
[2]: https://lore.kernel.org/lkml/[email protected]
Thanks for fixing this issue!
Since there is a consensus about the place, I have no objection either.
Quoted 104 lines
Reported-by Tetsuo Handa <[email protected]>Signed-off-by Rikard Falkeborn <[email protected]>---
Feedback on placing__is_constexpr()inconst.his welcome, at least the name is appropriate...include/linux/bits.h | 2 +- include/linux/const.h | 8 ++++++++ include/linux/minmax.h | 10 ++-------- tools/include/linux/bits.h | 2 +- tools/include/linux/const.h | 8 ++++++++ 5 files changed, 20 insertions(+), 10 deletions(-)include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, include/linux/const.h +8
-0@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ include/linux/minmax.h +2
-8@@ -2,6 +2,8 @@#ifndef _LINUX_MINMAX_H#define _LINUX_MINMAX_H +#include <linux/const.h> +/* * min()/max()/clamp() macros must accomplish three things: * @@ -17,14 +19,6 @@#define __typecheck (x, y) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) -/* -* This returns a constant expression while determining if an argument is -* a constant expression, most importantly without evaluating the argument. -* Glory to Martin Uecker <[email protected]> -*/ -#define __is_constexpr (x) \ - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) -#define __no_side_effects (x, y) \ (__is_constexpr(x) && __is_constexpr(y))tools/include/linux/bits.h +1
-1@@ -22,7 +22,7 @@#include <linux/build_bug.h> #define GENMASK_INPUT_CHECK (h, l) \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __builtin_constant_p((l) > (h)), (l) > (h), 0))) + __is_constexpr((l) > (h)), (l) > (h), 0)))#else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, tools/include/linux/const.h +8
-1@@ -3,4 +3,12 @@#include <vdso/const.h> +/* +* This returns a constant expression while determining if an argument is +* a constant expression, most importantly without evaluating the argument. +* Glory to Martin Uecker <[email protected]> +*/ +#define __is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#endif /* _LINUX_CONST_H */ --2.31.1
Signature · 2 lines
With Best Regards, Andy Shevchenko