Skip to content

Commit e3698b2

Browse files
committed
tools headers: Synchronize linux/bits.h with the kernel sources
To pick up the changes in these csets: 295bcca ("linux/bits.h: add compile time sanity check of GENMASK inputs") 3945ff3 ("linux/bits.h: Extract common header for vDSO") To address this tools/perf build warning: Warning: Kernel ABI header at 'tools/include/linux/bits.h' differs from latest version at 'include/linux/bits.h' diff -u tools/include/linux/bits.h include/linux/bits.h This clashes with usage of userspace's static_assert(), that, at least on glibc, is guarded by a ifnded/endif pair, do the same to our copy of build_bug.h and avoid that diff in check_headers.sh so that we continue checking for drifts with the kernel sources master copy. This will all be tested with the set of build containers that includes uCLibc, musl libc, lots of glibc versions in lots of distros and cross build environments. The tools/objtool, tools/bpf, etc were tested as well. Cc: Adrian Hunter <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Rikard Falkeborn <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vincenzo Frascino <[email protected]> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 5b992ad commit e3698b2

File tree

5 files changed

+115
-6
lines changed

5 files changed

+115
-6
lines changed

tools/include/linux/bits.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#define __LINUX_BITS_H
44

55
#include <linux/const.h>
6+
#include <vdso/bits.h>
67
#include <asm/bitsperlong.h>
78

8-
#define BIT(nr) (UL(1) << (nr))
99
#define BIT_ULL(nr) (ULL(1) << (nr))
1010
#define BIT_MASK(nr) (UL(1) << ((nr) % BITS_PER_LONG))
1111
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
@@ -18,12 +18,30 @@
1818
* position @h. For example
1919
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
2020
*/
21-
#define GENMASK(h, l) \
21+
#if !defined(__ASSEMBLY__) && \
22+
(!defined(CONFIG_CC_IS_GCC) || CONFIG_GCC_VERSION >= 49000)
23+
#include <linux/build_bug.h>
24+
#define GENMASK_INPUT_CHECK(h, l) \
25+
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
26+
__builtin_constant_p((l) > (h)), (l) > (h), 0)))
27+
#else
28+
/*
29+
* BUILD_BUG_ON_ZERO is not available in h files included from asm files,
30+
* disable the input check if that is the case.
31+
*/
32+
#define GENMASK_INPUT_CHECK(h, l) 0
33+
#endif
34+
35+
#define __GENMASK(h, l) \
2236
(((~UL(0)) - (UL(1) << (l)) + 1) & \
2337
(~UL(0) >> (BITS_PER_LONG - 1 - (h))))
38+
#define GENMASK(h, l) \
39+
(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
2440

25-
#define GENMASK_ULL(h, l) \
41+
#define __GENMASK_ULL(h, l) \
2642
(((~ULL(0)) - (ULL(1) << (l)) + 1) & \
2743
(~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
44+
#define GENMASK_ULL(h, l) \
45+
(GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
2846

2947
#endif /* __LINUX_BITS_H */

tools/include/linux/build_bug.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _LINUX_BUILD_BUG_H
3+
#define _LINUX_BUILD_BUG_H
4+
5+
#include <linux/compiler.h>
6+
7+
#ifdef __CHECKER__
8+
#define BUILD_BUG_ON_ZERO(e) (0)
9+
#else /* __CHECKER__ */
10+
/*
11+
* Force a compilation error if condition is true, but also produce a
12+
* result (of value 0 and type int), so the expression can be used
13+
* e.g. in a structure initializer (or where-ever else comma expressions
14+
* aren't permitted).
15+
*/
16+
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
17+
#endif /* __CHECKER__ */
18+
19+
/* Force a compilation error if a constant expression is not a power of 2 */
20+
#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
21+
BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
22+
#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
23+
BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
24+
25+
/*
26+
* BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
27+
* expression but avoids the generation of any code, even if that expression
28+
* has side-effects.
29+
*/
30+
#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
31+
32+
/**
33+
* BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
34+
* error message.
35+
* @condition: the condition which the compiler should know is false.
36+
*
37+
* See BUILD_BUG_ON for description.
38+
*/
39+
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
40+
41+
/**
42+
* BUILD_BUG_ON - break compile if a condition is true.
43+
* @condition: the condition which the compiler should know is false.
44+
*
45+
* If you have some code which relies on certain constants being equal, or
46+
* some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
47+
* detect if someone changes it.
48+
*/
49+
#define BUILD_BUG_ON(condition) \
50+
BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
51+
52+
/**
53+
* BUILD_BUG - break compile if used.
54+
*
55+
* If you have some code that you expect the compiler to eliminate at
56+
* build time, you should use BUILD_BUG to detect if it is
57+
* unexpectedly used.
58+
*/
59+
#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
60+
61+
/**
62+
* static_assert - check integer constant expression at build time
63+
*
64+
* static_assert() is a wrapper for the C11 _Static_assert, with a
65+
* little macro magic to make the message optional (defaulting to the
66+
* stringification of the tested expression).
67+
*
68+
* Contrary to BUILD_BUG_ON(), static_assert() can be used at global
69+
* scope, but requires the expression to be an integer constant
70+
* expression (i.e., it is not enough that __builtin_constant_p() is
71+
* true for expr).
72+
*
73+
* Also note that BUILD_BUG_ON() fails the build if the condition is
74+
* true, while static_assert() fails the build if the expression is
75+
* false.
76+
*/
77+
#ifndef static_assert
78+
#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
79+
#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
80+
#endif // static_assert
81+
82+
#endif /* _LINUX_BUILD_BUG_H */

tools/include/linux/kernel.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdarg.h>
66
#include <stddef.h>
77
#include <assert.h>
8+
#include <linux/build_bug.h>
89
#include <linux/compiler.h>
910
#include <endian.h>
1011
#include <byteswap.h>
@@ -35,9 +36,6 @@
3536
(type *)((char *)__mptr - offsetof(type, member)); })
3637
#endif
3738

38-
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
39-
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
40-
4139
#ifndef max
4240
#define max(x, y) ({ \
4341
typeof(x) _max1 = (x); \

tools/include/vdso/bits.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __VDSO_BITS_H
3+
#define __VDSO_BITS_H
4+
5+
#include <vdso/const.h>
6+
7+
#define BIT(nr) (UL(1) << (nr))
8+
9+
#endif /* __VDSO_BITS_H */

tools/perf/check-headers.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include/uapi/linux/usbdevice_fs.h
2222
include/uapi/linux/vhost.h
2323
include/uapi/sound/asound.h
2424
include/linux/bits.h
25+
include/vdso/bits.h
2526
include/linux/const.h
2627
include/vdso/const.h
2728
include/linux/hash.h
@@ -116,6 +117,7 @@ check arch/x86/lib/memcpy_64.S '-I "^EXPORT_SYMBOL" -I "^#include <asm/ex
116117
check arch/x86/lib/memset_64.S '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memset_\(erms\|orig\))"'
117118
check include/uapi/asm-generic/mman.h '-I "^#include <\(uapi/\)*asm-generic/mman-common\(-tools\)*.h>"'
118119
check include/uapi/linux/mman.h '-I "^#include <\(uapi/\)*asm/mman.h>"'
120+
check include/linux/build_bug.h '-I "^#\(ifndef\|endif\)\( \/\/\)* static_assert$"'
119121
check include/linux/ctype.h '-I "isdigit("'
120122
check lib/ctype.c '-I "^EXPORT_SYMBOL" -I "^#include <linux/export.h>" -B'
121123
check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"'

0 commit comments

Comments
 (0)