kmemcheck: clean up kmemcheck_annotate_bitfield
Subsystems (6)
PATCH 1/1 kmemcheck: clean up kmemcheck_annotate_bitfield
The commit 181f7c5dd3832763bdf2756b6d2d8a49bdf12791 change traded a style warning from sparse for a syntax warning from the real compiler (and perhaps an error with compilers old enough). The use of a local variable inside BUILD_BUG_ON() is pretty questionable too, though it works with the compiler's constant-folding in practice.
This change cleans it all up a little more sanely. The details are self-explanatory.
--- include/linux/kmemcheck.h | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-)
include/linux/kmemcheck.h +6 -8
@@ -143,16 +143,14 @@ static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)#define kmemcheck_bitfield_end (name) \ int name##_end[0]; +#define kmemcheck_bitfield_stretch (ptr, name) \ + ((long) &(ptr)->name##_end - (long) &(ptr)->name##_begin) +#define kmemcheck_annotate_bitfield (ptr, name) \ do { \ - if (!ptr) \ - break; \ - \ - int _n = (long) &((ptr)->name##_end) \ - - (long) &((ptr)->name##_begin); \ - BUILD_BUG_ON(_n < 0); \ - \ - kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \ + BUILD_BUG_ON(kmemcheck_bitfield_stretch(ptr, name) < 0);\ + kmemcheck_mark_initialized(&(ptr)->name##_begin, \ + kmemcheck_bitfield_stretch(ptr, name)); \ } while (0)#define kmemcheck_annotate_variable (var) \
Andrew Morton
maintainerQuoted 33 lines · On Tue, 22 Sep 2009 17:13:01 -0700 (PDT) Roland McGrath <[email protected]>…
The commit
181f7c5dd3832763bdf2756b6d2d8a49bdf12791change traded a style warning from sparse for a syntax warning from the real compiler (and perhaps an error with compilers old enough). The use of a local variable insideBUILD_BUG_ON()is pretty questionable too, though it works with the compiler's constant-folding in practice.This change cleans it all up a little more sanely. The details are self-explanatory.
Signed-off-by Roland McGrath <[email protected]>CC Johannes Berg <[email protected]>CC Vegard Nossum <[email protected]>--- include/linux/kmemcheck.h | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-)include/linux/kmemcheck.h +3
-4@@ -143,16 +143,14 @@ static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)#define kmemcheck_bitfield_end (name) \ int name##_end[0]; +#define kmemcheck_bitfield_stretch (ptr, name) \ + ((long) &(ptr)->name##_end - (long) &(ptr)->name##_begin) +#define kmemcheck_annotate_bitfield (ptr, name) \ do { \ - if (!ptr) \ - break; \ - \ - int _n = (long) &((ptr)->name##_end) \
include/linux/kmemcheck.h static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size) · line 156
Quoted 10 lines
include/linux/kmemcheck.h +3
-4- - (long) &((ptr)->name##_begin); \ -BUILD_BUG_ON (_n <0 ); \ - \ - kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \ +BUILD_BUG_ON (kmemcheck_bitfield_stretch (ptr, name) <0 );\ + kmemcheck_mark_initialized(&(ptr)->name##_begin, \ +kmemcheck_bitfield_stretch (ptr, name)); \ }while (0 ) #define kmemcheck_annotate_variable(var) \
I just sent Linus the simple version:
include/linux/kmemcheck.h renamed from include/linux/kmemcheck.h~include-linux-kmemcheckh-fix-a-trillion-warnings +3 -1
@@ -145,10 +145,12 @@ static inline bool kmemcheck_is_obj_init#define kmemcheck_annotate_bitfield (ptr, name) \ do { \ + int _n; \ + \ if (!ptr) \ break; \ \ - int _n = (long) &((ptr)->name##_end) \ + _n = (long) &((ptr)->name##_end) \ - (long) &((ptr)->name##_begin); \ BUILD_BUG_ON(_n < 0); \ \
_
That BUILD_BUG_ON() is just wrong, but nobody knew that because the present version of BUILD_BUG_ON() silently does nothing if passed an expression which isn't a compile-time constant. This was all fixed in the just-sent build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it.patch:
From: "Jan Beulich" <[email protected]>
gcc permitting variable length arrays makes the current construct used for BUILD_BUG_ON() useless, as that doesn't produce any diagnostic if the controlling expression isn't really constant. Instead, this patch makes it so that a bit field gets used here. Consequently, those uses where the condition isn't really constant now also need fixing.
Note that in the gfp.h, kmemcheck.h, and virtio_config.h cases MAYBE_BUILD_BUG_ON() really just serves documentation purposes - even if the expression is compile time constant (__builtin_constant_p() yields true), the array is still deemed of variable length by gcc, and hence the whole expression doesn't have the intended effect.
---
arch/sparc/include/asm/vio.h | 2 +- drivers/char/tpm/tpm.c | 4 ++-- drivers/net/niu.c | 2 +- include/linux/gfp.h | 2 +- include/linux/kernel.h | 8 ++++++-- include/linux/kmemcheck.h | 2 +- include/linux/virtio_config.h | 3 +-- 7 files changed, 13 insertions(+), 10 deletions(-)
arch/sparc/include/asm/vio.h renamed from arch/sparc/include/asm/vio.h~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +1 -1
@@ -258,7 +258,7 @@ static inline void *vio_dring_entry(strustatic inline u32 vio_dring_avail (struct vio_dring_state *dr,unsigned int ring_size) { -BUILD_BUG_ON (!is_power_of_2 (ring_size)); +MAYBE_BUILD_BUG_ON (!is_power_of_2 (ring_size));return (dr->pending - ((dr->prod - dr->cons) & (ring_size -1 )));
drivers/char/tpm/tpm.c renamed from drivers/char/tpm/tpm.c~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +2 -2
@@ -696,7 +696,7 @@ int __tpm_pcr_read(struct tpm_chip *chip cmd.header.in = pcrread_header; cmd.params.pcrread_in.pcr_idx =cpu_to_be32 (pcr_idx); -BUILD_BUG_ON (cmd.header.in.length >READ_PCR_RESULT_SIZE ); +BUG_ON (cmd.header.in.length >READ_PCR_RESULT_SIZE ); rc =transmit_cmd (chip, &cmd, cmd.header.in.length,"attempting to read a pcr value" );
@@ -760,7 +760,7 @@ int tpm_pcr_extend(u32 chip_num, int pcrreturn -ENODEV ; cmd.header.in = pcrextend_header; -BUILD_BUG_ON (be32_to_cpu (cmd.header.in.length) >EXTEND_PCR_SIZE ); +BUG_ON (be32_to_cpu (cmd.header.in.length) >EXTEND_PCR_SIZE ); cmd.params.pcrextend_in.pcr_idx =cpu_to_be32 (pcr_idx);memcpy (cmd.params.pcrextend_in.hash, hash,TPM_DIGEST_SIZE ); rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
drivers/net/niu.c renamed from drivers/net/niu.c~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +1 -1
@@ -5615,7 +5615,7 @@ static void niu_init_tx_mac(struct niu */* The XMAC_MIN register only accepts values for TX min which * have the low 3 bits cleared. */ -BUILD_BUG_ON (min &0x7 ); +BUG_ON (min &0x7 );if (np->flags &NIU_FLAGS_XMAC )niu_init_tx_xmac (np, min, max);
include/linux/gfp.h renamed from include/linux/gfp.h~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +1 -1
@@ -220,7 +220,7 @@ static inline enum zone_type gfp_zone(gf ((1 <<ZONES_SHIFT ) -1 );if (__builtin_constant_p (bit)) -BUILD_BUG_ON ((GFP_ZONE_BAD >> bit) &1 ); +MAYBE_BUILD_BUG_ON ((GFP_ZONE_BAD >> bit) &1 );else {#ifdef CONFIG_DEBUG_VM BUG_ON ((GFP_ZONE_BAD >> bit) &1 );
include/linux/kernel.h renamed from include/linux/kernel.h~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +6 -2
@@ -678,13 +678,17 @@ struct sysinfo { };/* Force a compilation error if condition is true */ -#define BUILD_BUG_ON (condition) ((void)sizeof(char[1 - 2*!!(condition)])) +#define BUILD_BUG_ON (condition) ((void)BUILD_BUG_ON_ZERO(condition)) + +/* Force a compilation error if condition is constant and true */ +#define MAYBE_BUILD_BUG_ON (cond) ((void)sizeof(char[1 - 2 * !!(cond)]))/* 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(char[1 - 2 * !!(e)]) - 1) +#define BUILD_BUG_ON_ZERO (e) (sizeof(struct { int:-!!(e); })) +#define BUILD_BUG_ON_NULL (e) ((void *)sizeof(struct { int:-!!(e); }))/* Trap pasters of __FUNCTION__ at compile-time */ #define __FUNCTION__ (__func__)
include/linux/kmemcheck.h renamed from include/linux/kmemcheck.h~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +1 -1
@@ -152,7 +152,7 @@ static inline bool kmemcheck_is_obj_init \ _n = (long) &((ptr)->name##_end) \ - (long) &((ptr)->name##_begin); \ -BUILD_BUG_ON (_n <0 ); \ +MAYBE_BUILD_BUG_ON (_n <0 ); \ \ kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \ }while (0 )
include/linux/virtio_config.h renamed from include/linux/virtio_config.h~build_bug_on-fix-it-and-a-couple-of-bogus-uses-of-it +1 -2
@@ -109,8 +109,7 @@ static inline bool virtio_has_feature(co unsigned int fbit) {/* Did you forget to fix assumptions on max features? */ -if (__builtin_constant_p (fbit)) -BUILD_BUG_ON (fbit >=32 ); +MAYBE_BUILD_BUG_ON (fbit >=32 );if (fbit <VIRTIO_TRANSPORT_F_START )virtio_check_driver_offered_feature (vdev, fbit);
_