From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S969158AbdEYOG0 (ORCPT ); Thu, 25 May 2017 10:06:26 -0400 Received: from mail-wm0-f47.google.com ([74.125.82.47]:38161 "EHLO mail-wm0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934669AbdEYOGY (ORCPT ); Thu, 25 May 2017 10:06:24 -0400 From: Michal Nazarewicz To: Ian Abbott , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Alexander Potapenko , Andrew Morton , Arnd Bergmann , Borislav Petkov , Hidehiro Kawai , Ian Abbott , Jakub Kicinski , Johannes Berg , Kees Cook , Peter Zijlstra , Rasmus Villemoes , Steven Rostedt Subject: Re: [PATCH v5 5/6] bug: split BUILD_BUG stuff out into In-Reply-To: <20170525120316.24473-6-abbotti@mev.co.uk> Organization: http://mina86.com/ References: <20170525120316.24473-1-abbotti@mev.co.uk> <20170525120316.24473-6-abbotti@mev.co.uk> Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJFBMVEWbfGlUPDDHgE57V0jUupKjgIObY0PLrom9mH4dFRK4gmjPs41MxjOgAAACP0lEQVQ4T23Sv2vbQBQHcBk1xE6WyALX107VUEgmn6+ouUwpEQQ6uRjttkWP4CkBg2M0BQLBdPFZYPsyFYo7qEtKDQ7on+t7+nF2Ux8ahD587717OmNYrOvycHsZ+o2r051wHTHysAvGb8ygvgu4QWT0sCmkgZCIEnlV2X8BtyraazFGDuxhmKSQJMlwHQ7v5MHSNxmz78rfElwAa3ieVD9e+hBhjaPDDG6NgFo2f4wBMNIo5YmRtF0RyDgFjJjlMIWbnuM4x9MMfABGTlN4qgIQB4A1DEyA1BHWtfeWNUMwiVJKoqh97KrkOO+qzgluVYLvFCUKAX73nONeBr7BGMdM6Sg0kuep03VywLaIzRiVr+GAzKlpQIsAFnWAG2e6DT5WmWDiudZMIc6hYrMOmeMQK9WX0B+/RfjzL9DI7Y9/Iayn29Ci0r2i4f9gMimMSZLCDMalgQGU5hnUtqAN0OGvEmO1Wnl0C0wWSCEHnuHBqmygxdxA8oWXwbipoc1EoNR9DqOpBpOJrnr0criQab9ZT4LL+wI+K7GBQH30CrhUruilgP9DRTrhVWZCiAyILP+wiuLeCKGTD6r/nc8LOJcAwR6IBTUs+7CASw3QFZ0MdA2PI3zNziH4ZKVhXCRMBjeZ1DWMekKwDCASwExy+NQ86TaykaDAFHO4aP48y4fIcDM5yOG8GcTLbOyp8A8azjJI93JFd1EA6yN8sSxMQJWoABqniRZVykYgRXErzrdqExAoUrRb0xfRp8p2A/4XmfilTtkDZ4cAAAAASUVORK5CYII= X-Face: -TR8(rDTHy/(xl?SfWd1|3:TTgDIatE^t'vop%*gVg[kn$t{EpK(P"VQ=~T2#ysNmJKN$"yTRLB4YQs$4{[.]Fc1)*O]3+XO^oXM>Q#b^ix,O)Zbn)q[y06$`e3?C)`CwR9y5riE=fv^X@x$y?D:XO6L&x4f-}}I4=VRNwiA^t1-ZrVK^07.Pi/57c_du'& OpenPGP: id=AC1F5F5CD41888F8CC8458582060401250751FF4; url=http://mina86.com/mina86.pub X-Hashcash: 1:20:170525:bp@suse.de::uQmSaaY0ODmmpKHW:00000000kdD X-Hashcash: 1:20:170525:peterz@infradead.org::indYsdsXvKKOkWz6:000000000000000000000000000000000000000000rg7 X-Hashcash: 1:20:170525:keescook@chromium.org::LLdVqMD3zsU9bvqX:000000000000000000000000000000000000000003bo X-Hashcash: 1:20:170525:jakub.kicinski@netronome.com::61zSivQHTlcpnYKO:0000000000000000000000000000000000cHi X-Hashcash: 1:20:170525:hidehiro.kawai.ez@hitachi.com::xhwJyUnfNo5dUDgO:000000000000000000000000000000000YnZ X-Hashcash: 1:20:170525:linux-arch@vger.kernel.org::uKCOBeaim8bkIVgr:000000000000000000000000000000000000Ku7 X-Hashcash: 1:20:170525:akpm@linux-foundation.org::12+CfyZXaFjsewId:0000000000000000000000000000000000001R4+ X-Hashcash: 1:20:170525:rostedt@goodmis.org::PhgMIgmg+DQyd1fX:0000000000000000000000000000000000000000001L27 X-Hashcash: 1:20:170525:linux-kernel@vger.kernel.org::9SgmiceGvQBJj6as:0000000000000000000000000000000001kOh X-Hashcash: 1:20:170525:abbotti@mev.co.uk::nK9nWH2XOP7lSuP8:000000000000000000000000000000000000000000002Drw X-Hashcash: 1:20:170525:johannes.berg@intel.com::hUnJrAz4UWxR6ZVP:000000000000000000000000000000000000002MeW X-Hashcash: 1:20:170525:abbotti@mev.co.uk::QWIVzoT1Lhr9TPi6:0000000000000000000000000000000000000000000043wW X-Hashcash: 1:20:170525:linux@rasmusvillemoes.dk::jGb9YbawWMUIEhIq:000000000000000000000000000000000000057xi X-Hashcash: 1:20:170525:arnd@arndb.de::o4tSVpnietqZWmRx:0000CgVh X-Hashcash: 1:20:170525:glider@google.com::MrHj5TW8mofWcqzs:00000000000000000000000000000000000000000000Clsc Date: Thu, 25 May 2017 16:06:20 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by mail.home.local id v4PE6aSg013987 On Thu, May 25 2017, Ian Abbott wrote: > Including pulls in a lot of bloat from and > that is not needed to call the BUILD_BUG() family of > macros. Split them out into their own header, . > > 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. > > Signed-off-by: Ian Abbott > Cc: Andrew Morton > Cc: Kees Cook > Cc: Steven Rostedt > Cc: Peter Zijlstra > Cc: Jakub Kicinski > Cc: Rasmus Villemoes Acked-by: Michal Nazarewicz > --- > v5: Actually, there was no v1 thru v4. I called this v5 to match the > series. > --- > include/linux/bug.h | 74 +---------------------------------------- > include/linux/build_bug.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 85 insertions(+), 73 deletions(-) > create mode 100644 include/linux/build_bug.h > > 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 @@ > > #include > #include > +#include > > enum bug_trap_type { > BUG_TRAP_TYPE_NONE = 0, > @@ -13,82 +14,9 @@ enum bug_trap_type { > struct pt_regs; > > #ifdef __CHECKER__ > -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > -#define BUILD_BUG_ON_ZERO(e) (0) > -#define BUILD_BUG_ON_NULL(e) ((void *)0) > -#define BUILD_BUG_ON_INVALID(e) (0) > -#define BUILD_BUG_ON_MSG(cond, msg) (0) > -#define BUILD_BUG_ON(condition) (0) > -#define BUILD_BUG() (0) > #define MAYBE_BUILD_BUG_ON(cond) (0) > #else /* __CHECKER__ */ > > -/* Force a compilation error if a constant expression is not a power of 2 */ > -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > - BUILD_BUG_ON(((n) & ((n) - 1)) != 0) > -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > - BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) > - > -/* > - * Force a compilation error if condition is true, but also produce a > - * result (of value 0 and type size_t), so the expression can be used > - * e.g. in a structure initializer (or where-ever else comma expressions > - * aren't permitted). > - */ > -#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) > -#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) > - > -/* > - * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the > - * expression but avoids the generation of any code, even if that expression > - * has side-effects. > - */ > -#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) > - > -/** > - * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied > - * error message. > - * @condition: the condition which the compiler should know is false. > - * > - * See BUILD_BUG_ON for description. > - */ > -#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) > - > -/** > - * BUILD_BUG_ON - break compile if a condition is true. > - * @condition: the condition which the compiler should know is false. > - * > - * If you have some code which relies on certain constants being equal, or > - * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to > - * detect if someone changes it. > - * > - * The implementation uses gcc's reluctance to create a negative array, but gcc > - * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to > - * inline functions). Luckily, in 4.3 they added the "error" function > - * attribute just for this type of case. Thus, we use a negative sized array > - * (should always create an error on gcc versions older than 4.4) and then call > - * an undefined function with the error attribute (should always create an > - * error on gcc 4.3 and later). If for some reason, neither creates a > - * compile-time error, we'll still have a link-time error, which is harder to > - * track down. > - */ > -#ifndef __OPTIMIZE__ > -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) > -#else > -#define BUILD_BUG_ON(condition) \ > - BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) > -#endif > - > -/** > - * BUILD_BUG - break compile if used. > - * > - * If you have some code that you expect the compiler to eliminate at > - * build time, you should use BUILD_BUG to detect if it is > - * unexpectedly used. > - */ > -#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") > - > #define MAYBE_BUILD_BUG_ON(cond) \ > do { \ > 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 @@ > +#ifndef _LINUX_BUILD_BUG_H > +#define _LINUX_BUILD_BUG_H > + > +#include > + > +#ifdef __CHECKER__ > +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > +#define BUILD_BUG_ON_ZERO(e) (0) > +#define BUILD_BUG_ON_NULL(e) ((void *)0) > +#define BUILD_BUG_ON_INVALID(e) (0) > +#define BUILD_BUG_ON_MSG(cond, msg) (0) > +#define BUILD_BUG_ON(condition) (0) > +#define BUILD_BUG() (0) > +#else /* __CHECKER__ */ > + > +/* Force a compilation error if a constant expression is not a power of 2 */ > +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > + BUILD_BUG_ON(((n) & ((n) - 1)) != 0) > +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > + BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) > + > +/* > + * Force a compilation error if condition is true, but also produce a > + * result (of value 0 and type size_t), so the expression can be used > + * e.g. in a structure initializer (or where-ever else comma expressions > + * aren't permitted). > + */ > +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) > +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) > + > +/* > + * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the > + * expression but avoids the generation of any code, even if that expression > + * has side-effects. > + */ > +#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) > + > +/** > + * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied > + * error message. > + * @condition: the condition which the compiler should know is false. > + * > + * See BUILD_BUG_ON for description. > + */ > +#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) > + > +/** > + * BUILD_BUG_ON - break compile if a condition is true. > + * @condition: the condition which the compiler should know is false. > + * > + * If you have some code which relies on certain constants being equal, or > + * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to > + * detect if someone changes it. > + * > + * The implementation uses gcc's reluctance to create a negative array, but gcc > + * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to > + * inline functions). Luckily, in 4.3 they added the "error" function > + * attribute just for this type of case. Thus, we use a negative sized array > + * (should always create an error on gcc versions older than 4.4) and then call > + * an undefined function with the error attribute (should always create an > + * error on gcc 4.3 and later). If for some reason, neither creates a > + * compile-time error, we'll still have a link-time error, which is harder to > + * track down. > + */ > +#ifndef __OPTIMIZE__ > +#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) > +#else > +#define BUILD_BUG_ON(condition) \ > + BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) > +#endif > + > +/** > + * BUILD_BUG - break compile if used. > + * > + * If you have some code that you expect the compiler to eliminate at > + * build time, you should use BUILD_BUG to detect if it is > + * unexpectedly used. > + */ > +#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") > + > +#endif /* __CHECKER__ */ > + > +#endif /* _LINUX_BUILD_BUG_H */ -- Best regards ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ «If at first you don’t succeed, give up skydiving» From mboxrd@z Thu Jan 1 00:00:00 1970 From: Michal Nazarewicz Subject: Re: [PATCH v5 5/6] bug: split BUILD_BUG stuff out into Date: Thu, 25 May 2017 16:06:20 +0200 Message-ID: References: <20170525120316.24473-1-abbotti@mev.co.uk> <20170525120316.24473-6-abbotti@mev.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <20170525120316.24473-6-abbotti@mev.co.uk> Sender: linux-kernel-owner@vger.kernel.org To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Alexander Potapenko , Andrew Morton , Arnd Bergmann , Borislav Petkov , Hidehiro Kawai , Ian Abbott , Jakub Kicinski , Johannes Berg , Kees Cook , Peter Zijlstra , Rasmus Villemoes , Steven Rostedt List-Id: linux-arch.vger.kernel.org On Thu, May 25 2017, Ian Abbott wrote: > Including pulls in a lot of bloat from and > that is not needed to call the BUILD_BUG() family of > macros. Split them out into their own header, . > > 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. > > Signed-off-by: Ian Abbott > Cc: Andrew Morton > Cc: Kees Cook > Cc: Steven Rostedt > Cc: Peter Zijlstra > Cc: Jakub Kicinski > Cc: Rasmus Villemoes Acked-by: Michal Nazarewicz > --- > v5: Actually, there was no v1 thru v4. I called this v5 to match the > series. > --- > include/linux/bug.h | 74 +---------------------------------------- > include/linux/build_bug.h | 84 +++++++++++++++++++++++++++++++++++++++++= ++++++ > 2 files changed, 85 insertions(+), 73 deletions(-) > create mode 100644 include/linux/build_bug.h > > 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 @@ >=20=20 > #include > #include > +#include >=20=20 > enum bug_trap_type { > BUG_TRAP_TYPE_NONE =3D 0, > @@ -13,82 +14,9 @@ enum bug_trap_type { > struct pt_regs; >=20=20 > #ifdef __CHECKER__ > -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > -#define BUILD_BUG_ON_ZERO(e) (0) > -#define BUILD_BUG_ON_NULL(e) ((void *)0) > -#define BUILD_BUG_ON_INVALID(e) (0) > -#define BUILD_BUG_ON_MSG(cond, msg) (0) > -#define BUILD_BUG_ON(condition) (0) > -#define BUILD_BUG() (0) > #define MAYBE_BUILD_BUG_ON(cond) (0) > #else /* __CHECKER__ */ >=20=20 > -/* Force a compilation error if a constant expression is not a power of = 2 */ > -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > - BUILD_BUG_ON(((n) & ((n) - 1)) !=3D 0) > -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > - BUILD_BUG_ON((n) =3D=3D 0 || (((n) & ((n) - 1)) !=3D 0)) > - > -/* > - * Force a compilation error if condition is true, but also produce a > - * result (of value 0 and type size_t), so the expression can be used > - * e.g. in a structure initializer (or where-ever else comma expressions > - * aren't permitted). > - */ > -#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) > -#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) > - > -/* > - * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of = the > - * expression but avoids the generation of any code, even if that expres= sion > - * has side-effects. > - */ > -#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) > - > -/** > - * BUILD_BUG_ON_MSG - break compile if a condition is true & emit suppli= ed > - * error message. > - * @condition: the condition which the compiler should know is false. > - * > - * See BUILD_BUG_ON for description. > - */ > -#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) > - > -/** > - * BUILD_BUG_ON - break compile if a condition is true. > - * @condition: the condition which the compiler should know is false. > - * > - * If you have some code which relies on certain constants being equal, = or > - * some other compile-time-evaluated condition, you should use BUILD_BUG= _ON to > - * detect if someone changes it. > - * > - * The implementation uses gcc's reluctance to create a negative array, = but gcc > - * (as of 4.4) only emits that error for obvious cases (e.g. not argumen= ts to > - * inline functions). Luckily, in 4.3 they added the "error" function > - * attribute just for this type of case. Thus, we use a negative sized = array > - * (should always create an error on gcc versions older than 4.4) and th= en call > - * an undefined function with the error attribute (should always create = an > - * error on gcc 4.3 and later). If for some reason, neither creates a > - * compile-time error, we'll still have a link-time error, which is hard= er to > - * track down. > - */ > -#ifndef __OPTIMIZE__ > -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) > -#else > -#define BUILD_BUG_ON(condition) \ > - BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) > -#endif > - > -/** > - * BUILD_BUG - break compile if used. > - * > - * If you have some code that you expect the compiler to eliminate at > - * build time, you should use BUILD_BUG to detect if it is > - * unexpectedly used. > - */ > -#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") > - > #define MAYBE_BUILD_BUG_ON(cond) \ > do { \ > 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 @@ > +#ifndef _LINUX_BUILD_BUG_H > +#define _LINUX_BUILD_BUG_H > + > +#include > + > +#ifdef __CHECKER__ > +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > +#define BUILD_BUG_ON_ZERO(e) (0) > +#define BUILD_BUG_ON_NULL(e) ((void *)0) > +#define BUILD_BUG_ON_INVALID(e) (0) > +#define BUILD_BUG_ON_MSG(cond, msg) (0) > +#define BUILD_BUG_ON(condition) (0) > +#define BUILD_BUG() (0) > +#else /* __CHECKER__ */ > + > +/* Force a compilation error if a constant expression is not a power of = 2 */ > +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > + BUILD_BUG_ON(((n) & ((n) - 1)) !=3D 0) > +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > + BUILD_BUG_ON((n) =3D=3D 0 || (((n) & ((n) - 1)) !=3D 0)) > + > +/* > + * Force a compilation error if condition is true, but also produce a > + * result (of value 0 and type size_t), so the expression can be used > + * e.g. in a structure initializer (or where-ever else comma expressions > + * aren't permitted). > + */ > +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) > +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) > + > +/* > + * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of = the > + * expression but avoids the generation of any code, even if that expres= sion > + * has side-effects. > + */ > +#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) > + > +/** > + * BUILD_BUG_ON_MSG - break compile if a condition is true & emit suppli= ed > + * error message. > + * @condition: the condition which the compiler should know is false. > + * > + * See BUILD_BUG_ON for description. > + */ > +#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) > + > +/** > + * BUILD_BUG_ON - break compile if a condition is true. > + * @condition: the condition which the compiler should know is false. > + * > + * If you have some code which relies on certain constants being equal, = or > + * some other compile-time-evaluated condition, you should use BUILD_BUG= _ON to > + * detect if someone changes it. > + * > + * The implementation uses gcc's reluctance to create a negative array, = but gcc > + * (as of 4.4) only emits that error for obvious cases (e.g. not argumen= ts to > + * inline functions). Luckily, in 4.3 they added the "error" function > + * attribute just for this type of case. Thus, we use a negative sized = array > + * (should always create an error on gcc versions older than 4.4) and th= en call > + * an undefined function with the error attribute (should always create = an > + * error on gcc 4.3 and later). If for some reason, neither creates a > + * compile-time error, we'll still have a link-time error, which is hard= er to > + * track down. > + */ > +#ifndef __OPTIMIZE__ > +#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) > +#else > +#define BUILD_BUG_ON(condition) \ > + BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) > +#endif > + > +/** > + * BUILD_BUG - break compile if used. > + * > + * If you have some code that you expect the compiler to eliminate at > + * build time, you should use BUILD_BUG to detect if it is > + * unexpectedly used. > + */ > +#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") > + > +#endif /* __CHECKER__ */ > + > +#endif /* _LINUX_BUILD_BUG_H */ --=20 Best regards =E3=83=9F=E3=83=8F=E3=82=A6 =E2=80=9C=F0=9D=93=B6=F0=9D=93=B2=F0=9D=93=B7= =F0=9D=93=AA86=E2=80=9D =E3=83=8A=E3=82=B6=E3=83=AC=E3=83=B4=E3=82=A4=E3=83= =84 =C2=ABIf at first you don=E2=80=99t succeed, give up skydiving=C2=BB From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f44.google.com ([74.125.82.44]:37789 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935611AbdEYOGZ (ORCPT ); Thu, 25 May 2017 10:06:25 -0400 Received: by mail-wm0-f44.google.com with SMTP id d127so99047410wmf.0 for ; Thu, 25 May 2017 07:06:25 -0700 (PDT) From: Michal Nazarewicz Subject: Re: [PATCH v5 5/6] bug: split BUILD_BUG stuff out into In-Reply-To: <20170525120316.24473-6-abbotti@mev.co.uk> References: <20170525120316.24473-1-abbotti@mev.co.uk> <20170525120316.24473-6-abbotti@mev.co.uk> Date: Thu, 25 May 2017 16:06:20 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Sender: linux-arch-owner@vger.kernel.org List-ID: To: Ian Abbott , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Alexander Potapenko , Andrew Morton , Arnd Bergmann , Borislav Petkov , Hidehiro Kawai , Jakub Kicinski , Johannes Berg , Kees Cook , Peter Zijlstra , Rasmus Villemoes , Steven Rostedt Message-ID: <20170525140620.XIMeIt8UTWzUW3JJ08Jp4Nzarb2drCUMG5Axd9qi6fU@z> On Thu, May 25 2017, Ian Abbott wrote: > Including pulls in a lot of bloat from and > that is not needed to call the BUILD_BUG() family of > macros. Split them out into their own header, . > > 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. > > Signed-off-by: Ian Abbott > Cc: Andrew Morton > Cc: Kees Cook > Cc: Steven Rostedt > Cc: Peter Zijlstra > Cc: Jakub Kicinski > Cc: Rasmus Villemoes Acked-by: Michal Nazarewicz > --- > v5: Actually, there was no v1 thru v4. I called this v5 to match the > series. > --- > include/linux/bug.h | 74 +---------------------------------------- > include/linux/build_bug.h | 84 +++++++++++++++++++++++++++++++++++++++++= ++++++ > 2 files changed, 85 insertions(+), 73 deletions(-) > create mode 100644 include/linux/build_bug.h > > 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 @@ >=20=20 > #include > #include > +#include >=20=20 > enum bug_trap_type { > BUG_TRAP_TYPE_NONE =3D 0, > @@ -13,82 +14,9 @@ enum bug_trap_type { > struct pt_regs; >=20=20 > #ifdef __CHECKER__ > -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > -#define BUILD_BUG_ON_ZERO(e) (0) > -#define BUILD_BUG_ON_NULL(e) ((void *)0) > -#define BUILD_BUG_ON_INVALID(e) (0) > -#define BUILD_BUG_ON_MSG(cond, msg) (0) > -#define BUILD_BUG_ON(condition) (0) > -#define BUILD_BUG() (0) > #define MAYBE_BUILD_BUG_ON(cond) (0) > #else /* __CHECKER__ */ >=20=20 > -/* Force a compilation error if a constant expression is not a power of = 2 */ > -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > - BUILD_BUG_ON(((n) & ((n) - 1)) !=3D 0) > -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > - BUILD_BUG_ON((n) =3D=3D 0 || (((n) & ((n) - 1)) !=3D 0)) > - > -/* > - * Force a compilation error if condition is true, but also produce a > - * result (of value 0 and type size_t), so the expression can be used > - * e.g. in a structure initializer (or where-ever else comma expressions > - * aren't permitted). > - */ > -#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) > -#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) > - > -/* > - * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of = the > - * expression but avoids the generation of any code, even if that expres= sion > - * has side-effects. > - */ > -#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) > - > -/** > - * BUILD_BUG_ON_MSG - break compile if a condition is true & emit suppli= ed > - * error message. > - * @condition: the condition which the compiler should know is false. > - * > - * See BUILD_BUG_ON for description. > - */ > -#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) > - > -/** > - * BUILD_BUG_ON - break compile if a condition is true. > - * @condition: the condition which the compiler should know is false. > - * > - * If you have some code which relies on certain constants being equal, = or > - * some other compile-time-evaluated condition, you should use BUILD_BUG= _ON to > - * detect if someone changes it. > - * > - * The implementation uses gcc's reluctance to create a negative array, = but gcc > - * (as of 4.4) only emits that error for obvious cases (e.g. not argumen= ts to > - * inline functions). Luckily, in 4.3 they added the "error" function > - * attribute just for this type of case. Thus, we use a negative sized = array > - * (should always create an error on gcc versions older than 4.4) and th= en call > - * an undefined function with the error attribute (should always create = an > - * error on gcc 4.3 and later). If for some reason, neither creates a > - * compile-time error, we'll still have a link-time error, which is hard= er to > - * track down. > - */ > -#ifndef __OPTIMIZE__ > -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) > -#else > -#define BUILD_BUG_ON(condition) \ > - BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) > -#endif > - > -/** > - * BUILD_BUG - break compile if used. > - * > - * If you have some code that you expect the compiler to eliminate at > - * build time, you should use BUILD_BUG to detect if it is > - * unexpectedly used. > - */ > -#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") > - > #define MAYBE_BUILD_BUG_ON(cond) \ > do { \ > 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 @@ > +#ifndef _LINUX_BUILD_BUG_H > +#define _LINUX_BUILD_BUG_H > + > +#include > + > +#ifdef __CHECKER__ > +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) > +#define BUILD_BUG_ON_ZERO(e) (0) > +#define BUILD_BUG_ON_NULL(e) ((void *)0) > +#define BUILD_BUG_ON_INVALID(e) (0) > +#define BUILD_BUG_ON_MSG(cond, msg) (0) > +#define BUILD_BUG_ON(condition) (0) > +#define BUILD_BUG() (0) > +#else /* __CHECKER__ */ > + > +/* Force a compilation error if a constant expression is not a power of = 2 */ > +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > + BUILD_BUG_ON(((n) & ((n) - 1)) !=3D 0) > +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ > + BUILD_BUG_ON((n) =3D=3D 0 || (((n) & ((n) - 1)) !=3D 0)) > + > +/* > + * Force a compilation error if condition is true, but also produce a > + * result (of value 0 and type size_t), so the expression can be used > + * e.g. in a structure initializer (or where-ever else comma expressions > + * aren't permitted). > + */ > +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) > +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) > + > +/* > + * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of = the > + * expression but avoids the generation of any code, even if that expres= sion > + * has side-effects. > + */ > +#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) > + > +/** > + * BUILD_BUG_ON_MSG - break compile if a condition is true & emit suppli= ed > + * error message. > + * @condition: the condition which the compiler should know is false. > + * > + * See BUILD_BUG_ON for description. > + */ > +#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) > + > +/** > + * BUILD_BUG_ON - break compile if a condition is true. > + * @condition: the condition which the compiler should know is false. > + * > + * If you have some code which relies on certain constants being equal, = or > + * some other compile-time-evaluated condition, you should use BUILD_BUG= _ON to > + * detect if someone changes it. > + * > + * The implementation uses gcc's reluctance to create a negative array, = but gcc > + * (as of 4.4) only emits that error for obvious cases (e.g. not argumen= ts to > + * inline functions). Luckily, in 4.3 they added the "error" function > + * attribute just for this type of case. Thus, we use a negative sized = array > + * (should always create an error on gcc versions older than 4.4) and th= en call > + * an undefined function with the error attribute (should always create = an > + * error on gcc 4.3 and later). If for some reason, neither creates a > + * compile-time error, we'll still have a link-time error, which is hard= er to > + * track down. > + */ > +#ifndef __OPTIMIZE__ > +#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) > +#else > +#define BUILD_BUG_ON(condition) \ > + BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) > +#endif > + > +/** > + * BUILD_BUG - break compile if used. > + * > + * If you have some code that you expect the compiler to eliminate at > + * build time, you should use BUILD_BUG to detect if it is > + * unexpectedly used. > + */ > +#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") > + > +#endif /* __CHECKER__ */ > + > +#endif /* _LINUX_BUILD_BUG_H */ --=20 Best regards =E3=83=9F=E3=83=8F=E3=82=A6 =E2=80=9C=F0=9D=93=B6=F0=9D=93=B2=F0=9D=93=B7= =F0=9D=93=AA86=E2=80=9D =E3=83=8A=E3=82=B6=E3=83=AC=E3=83=B4=E3=82=A4=E3=83= =84 =C2=ABIf at first you don=E2=80=99t succeed, give up skydiving=C2=BB