提交 cffb222b 编写于 作者: R Rikard Falkeborn 提交者: Zheng Zengkai

linux/bits.h: fix compilation error with GENMASK

stable inclusion
from stable-5.10.42
commit 1354ec840899e87259286cc844d4c161ea86fae7
bugzilla: 55093
CVE: NA

--------------------------------

[ Upstream commit f747e666 ]

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.comSigned-off-by: NRikard Falkeborn <rikard.falkeborn@gmail.com>
Reported-by: NTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Acked-by: NArnd Bergmann <arnd@arndb.de>
Reviewed-by: NAndy 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: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Acked-by: NWeilong Chen <chenweilong@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 6a3f590a
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include <linux/build_bug.h> #include <linux/build_bug.h>
#define GENMASK_INPUT_CHECK(h, l) \ #define GENMASK_INPUT_CHECK(h, l) \
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
__builtin_constant_p((l) > (h)), (l) > (h), 0))) __is_constexpr((l) > (h)), (l) > (h), 0)))
#else #else
/* /*
* BUILD_BUG_ON_ZERO is not available in h files included from asm files, * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
......
...@@ -3,4 +3,12 @@ ...@@ -3,4 +3,12 @@
#include <vdso/const.h> #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 <Martin.Uecker@med.uni-goettingen.de>
*/
#define __is_constexpr(x) \
(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
#endif /* _LINUX_CONST_H */ #endif /* _LINUX_CONST_H */
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#ifndef _LINUX_MINMAX_H #ifndef _LINUX_MINMAX_H
#define _LINUX_MINMAX_H #define _LINUX_MINMAX_H
#include <linux/const.h>
/* /*
* min()/max()/clamp() macros must accomplish three things: * min()/max()/clamp() macros must accomplish three things:
* *
...@@ -17,14 +19,6 @@ ...@@ -17,14 +19,6 @@
#define __typecheck(x, y) \ #define __typecheck(x, y) \
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) (!!(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 <Martin.Uecker@med.uni-goettingen.de>
*/
#define __is_constexpr(x) \
(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
#define __no_side_effects(x, y) \ #define __no_side_effects(x, y) \
(__is_constexpr(x) && __is_constexpr(y)) (__is_constexpr(x) && __is_constexpr(y))
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include <linux/build_bug.h> #include <linux/build_bug.h>
#define GENMASK_INPUT_CHECK(h, l) \ #define GENMASK_INPUT_CHECK(h, l) \
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
__builtin_constant_p((l) > (h)), (l) > (h), 0))) __is_constexpr((l) > (h)), (l) > (h), 0)))
#else #else
/* /*
* BUILD_BUG_ON_ZERO is not available in h files included from asm files, * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
......
...@@ -3,4 +3,12 @@ ...@@ -3,4 +3,12 @@
#include <vdso/const.h> #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 <Martin.Uecker@med.uni-goettingen.de>
*/
#define __is_constexpr(x) \
(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
#endif /* _LINUX_CONST_H */ #endif /* _LINUX_CONST_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册