diff options
author | Ian Abbott <abbotti@mev.co.uk> | 2017-07-10 18:51:07 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-07-10 19:32:34 -0400 |
commit | bc6245e5efd70c41eaf9334b1b5e646745cb0fb3 (patch) | |
tree | 534daeb120d3a1d716136eb32e0ce97bc69ff335 | |
parent | 47e81e59d98b90727a02ceb486407eeed5eb8727 (diff) |
bug: split BUILD_BUG stuff out into <linux/build_bug.h>
Including <linux/bug.h> pulls in a lot of bloat from <asm/bug.h> and
<asm-generic/bug.h> that is not needed to call the BUILD_BUG() family of
macros. Split them out into their own header, <linux/build_bug.h>.
Also correct some checkpatch.pl errors for the BUILD_BUG_ON_ZERO() and
BUILD_BUG_ON_NULL() macros by adding parentheses around the bitfield
widths that begin with a minus sign.
Link: http://lkml.kernel.org/r/20170525120316.24473-6-abbotti@mev.co.uk
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Acked-by: Michal Nazarewicz <mina86@mina86.com>
Acked-by: Kees Cook <keescook@chromium.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Jakub Kicinski <jakub.kicinski@netronome.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | include/linux/bug.h | 74 | ||||
-rw-r--r-- | include/linux/build_bug.h | 84 |
2 files changed, 85 insertions, 73 deletions
diff --git a/include/linux/bug.h b/include/linux/bug.h index 483207cb99fb..5d5554c874fd 100644 --- a/include/linux/bug.h +++ b/include/linux/bug.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | #include <asm/bug.h> | 4 | #include <asm/bug.h> |
5 | #include <linux/compiler.h> | 5 | #include <linux/compiler.h> |
6 | #include <linux/build_bug.h> | ||
6 | 7 | ||
7 | enum bug_trap_type { | 8 | enum bug_trap_type { |
8 | BUG_TRAP_TYPE_NONE = 0, | 9 | BUG_TRAP_TYPE_NONE = 0, |
@@ -13,82 +14,9 @@ enum bug_trap_type { | |||
13 | struct pt_regs; | 14 | struct pt_regs; |
14 | 15 | ||
15 | #ifdef __CHECKER__ | 16 | #ifdef __CHECKER__ |
16 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) | ||
17 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) | ||
18 | #define BUILD_BUG_ON_ZERO(e) (0) | ||
19 | #define BUILD_BUG_ON_NULL(e) ((void *)0) | ||
20 | #define BUILD_BUG_ON_INVALID(e) (0) | ||
21 | #define BUILD_BUG_ON_MSG(cond, msg) (0) | ||
22 | #define BUILD_BUG_ON(condition) (0) | ||
23 | #define BUILD_BUG() (0) | ||
24 | #define MAYBE_BUILD_BUG_ON(cond) (0) | 17 | #define MAYBE_BUILD_BUG_ON(cond) (0) |
25 | #else /* __CHECKER__ */ | 18 | #else /* __CHECKER__ */ |
26 | 19 | ||
27 | /* Force a compilation error if a constant expression is not a power of 2 */ | ||
28 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ | ||
29 | BUILD_BUG_ON(((n) & ((n) - 1)) != 0) | ||
30 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ | ||
31 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) | ||
32 | |||
33 | /* | ||
34 | * Force a compilation error if condition is true, but also produce a | ||
35 | * result (of value 0 and type size_t), so the expression can be used | ||
36 | * e.g. in a structure initializer (or where-ever else comma expressions | ||
37 | * aren't permitted). | ||
38 | */ | ||
39 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) | ||
40 | #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) | ||
41 | |||
42 | /* | ||
43 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the | ||
44 | * expression but avoids the generation of any code, even if that expression | ||
45 | * has side-effects. | ||
46 | */ | ||
47 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) | ||
48 | |||
49 | /** | ||
50 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied | ||
51 | * error message. | ||
52 | * @condition: the condition which the compiler should know is false. | ||
53 | * | ||
54 | * See BUILD_BUG_ON for description. | ||
55 | */ | ||
56 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ||
57 | |||
58 | /** | ||
59 | * BUILD_BUG_ON - break compile if a condition is true. | ||
60 | * @condition: the condition which the compiler should know is false. | ||
61 | * | ||
62 | * If you have some code which relies on certain constants being equal, or | ||
63 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to | ||
64 | * detect if someone changes it. | ||
65 | * | ||
66 | * The implementation uses gcc's reluctance to create a negative array, but gcc | ||
67 | * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to | ||
68 | * inline functions). Luckily, in 4.3 they added the "error" function | ||
69 | * attribute just for this type of case. Thus, we use a negative sized array | ||
70 | * (should always create an error on gcc versions older than 4.4) and then call | ||
71 | * an undefined function with the error attribute (should always create an | ||
72 | * error on gcc 4.3 and later). If for some reason, neither creates a | ||
73 | * compile-time error, we'll still have a link-time error, which is harder to | ||
74 | * track down. | ||
75 | */ | ||
76 | #ifndef __OPTIMIZE__ | ||
77 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | ||
78 | #else | ||
79 | #define BUILD_BUG_ON(condition) \ | ||
80 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) | ||
81 | #endif | ||
82 | |||
83 | /** | ||
84 | * BUILD_BUG - break compile if used. | ||
85 | * | ||
86 | * If you have some code that you expect the compiler to eliminate at | ||
87 | * build time, you should use BUILD_BUG to detect if it is | ||
88 | * unexpectedly used. | ||
89 | */ | ||
90 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") | ||
91 | |||
92 | #define MAYBE_BUILD_BUG_ON(cond) \ | 20 | #define MAYBE_BUILD_BUG_ON(cond) \ |
93 | do { \ | 21 | do { \ |
94 | if (__builtin_constant_p((cond))) \ | 22 | if (__builtin_constant_p((cond))) \ |
diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h new file mode 100644 index 000000000000..b7d22d60008a --- /dev/null +++ b/include/linux/build_bug.h | |||
@@ -0,0 +1,84 @@ | |||
1 | #ifndef _LINUX_BUILD_BUG_H | ||
2 | #define _LINUX_BUILD_BUG_H | ||
3 | |||
4 | #include <linux/compiler.h> | ||
5 | |||
6 | #ifdef __CHECKER__ | ||
7 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) | ||
8 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) | ||
9 | #define BUILD_BUG_ON_ZERO(e) (0) | ||
10 | #define BUILD_BUG_ON_NULL(e) ((void *)0) | ||
11 | #define BUILD_BUG_ON_INVALID(e) (0) | ||
12 | #define BUILD_BUG_ON_MSG(cond, msg) (0) | ||
13 | #define BUILD_BUG_ON(condition) (0) | ||
14 | #define BUILD_BUG() (0) | ||
15 | #else /* __CHECKER__ */ | ||
16 | |||
17 | /* Force a compilation error if a constant expression is not a power of 2 */ | ||
18 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ | ||
19 | BUILD_BUG_ON(((n) & ((n) - 1)) != 0) | ||
20 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ | ||
21 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) | ||
22 | |||
23 | /* | ||
24 | * Force a compilation error if condition is true, but also produce a | ||
25 | * result (of value 0 and type size_t), so the expression can be used | ||
26 | * e.g. in a structure initializer (or where-ever else comma expressions | ||
27 | * aren't permitted). | ||
28 | */ | ||
29 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) | ||
30 | #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) | ||
31 | |||
32 | /* | ||
33 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the | ||
34 | * expression but avoids the generation of any code, even if that expression | ||
35 | * has side-effects. | ||
36 | */ | ||
37 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) | ||
38 | |||
39 | /** | ||
40 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied | ||
41 | * error message. | ||
42 | * @condition: the condition which the compiler should know is false. | ||
43 | * | ||
44 | * See BUILD_BUG_ON for description. | ||
45 | */ | ||
46 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ||
47 | |||
48 | /** | ||
49 | * BUILD_BUG_ON - break compile if a condition is true. | ||
50 | * @condition: the condition which the compiler should know is false. | ||
51 | * | ||
52 | * If you have some code which relies on certain constants being equal, or | ||
53 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to | ||
54 | * detect if someone changes it. | ||
55 | * | ||
56 | * The implementation uses gcc's reluctance to create a negative array, but gcc | ||
57 | * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to | ||
58 | * inline functions). Luckily, in 4.3 they added the "error" function | ||
59 | * attribute just for this type of case. Thus, we use a negative sized array | ||
60 | * (should always create an error on gcc versions older than 4.4) and then call | ||
61 | * an undefined function with the error attribute (should always create an | ||
62 | * error on gcc 4.3 and later). If for some reason, neither creates a | ||
63 | * compile-time error, we'll still have a link-time error, which is harder to | ||
64 | * track down. | ||
65 | */ | ||
66 | #ifndef __OPTIMIZE__ | ||
67 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | ||
68 | #else | ||
69 | #define BUILD_BUG_ON(condition) \ | ||
70 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) | ||
71 | #endif | ||
72 | |||
73 | /** | ||
74 | * BUILD_BUG - break compile if used. | ||
75 | * | ||
76 | * If you have some code that you expect the compiler to eliminate at | ||
77 | * build time, you should use BUILD_BUG to detect if it is | ||
78 | * unexpectedly used. | ||
79 | */ | ||
80 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") | ||
81 | |||
82 | #endif /* __CHECKER__ */ | ||
83 | |||
84 | #endif /* _LINUX_BUILD_BUG_H */ | ||