[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250503113307.37b71fdf@pumpkin>
Date: Sat, 3 May 2025 11:33:07 +0100
From: David Laight <david.laight.linux@...il.com>
To: Ian Rogers <irogers@...gle.com>
Cc: Yury Norov <yury.norov@...il.com>, Rasmus Villemoes
<linux@...musvillemoes.dk>, Arnd Bergmann <arnd@...db.de>, Nathan
Chancellor <nathan@...nel.org>, Nick Desaulniers
<nick.desaulniers+lkml@...il.com>, Bill Wendling <morbo@...gle.com>, Justin
Stitt <justinstitt@...gle.com>, Adrian Hunter <adrian.hunter@...el.com>,
Thomas Gleixner <tglx@...utronix.de>, Jakub Kicinski <kuba@...nel.org>,
Jacob Keller <jacob.e.keller@...el.com>, linux-arch@...r.kernel.org,
linux-kernel@...r.kernel.org, llvm@...ts.linux.dev, Leo Yan
<leo.yan@....com>
Subject: Re: [PATCH v2 1/5] bitfield: Silence a clang -Wshorten-64-to-32
warning
On Wed, 30 Apr 2025 10:15:30 -0700
Ian Rogers <irogers@...gle.com> wrote:
> The clang warning -Wshorten-64-to-32 can be useful to catch
> inadvertent truncation. In some instances this truncation can lead to
> changing the sign of a result, for example, truncation to return an
> int to fit a sort routine. Silence the warning by making the implicit
> truncation explicit. This isn't to say the code is currently incorrect
> but without silencing the warning it is hard to spot the erroneous
> cases.
I suspect that is going to add an explicit mask in many cases.
Probably for u8, u16 and u32 if the return value from the old
code would return a u64 and the value is assigned to a u64.
Now if 'field_mask()' and 'field_multiplier()' are constants
then the compiler might optimise away the extra mask.
But in that case it shouldn't be bleating about the truncation
because it knows it can't happen.
So get the compiler fixed to do proper 'value tracking' and only
report about truncation when the high bits might be non-zero.
It also needs to not warn when the high bits are saved separately
(int h = v64 >> 32, l = v64;).
Then you have to worry about stopping the compiler bleating
for the return values of read (and similar).
Once you've got the compiler fixed to remove most of the false
positives worry about the Linux code.
David
>
> Signed-off-by: Ian Rogers <irogers@...gle.com>
> ---
> include/linux/bitfield.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index 63928f173223..cc5cfed041bb 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -176,7 +176,7 @@ static __always_inline __##type type##_encode_bits(base v, base field) \
> { \
> if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
> __field_overflow(); \
> - return to((v & field_mask(field)) * field_multiplier(field)); \
> + return to((__##type)((v & field_mask(field)) * field_multiplier(field))); \
> } \
> static __always_inline __##type type##_replace_bits(__##type old, \
> base val, base field) \
Powered by blists - more mailing lists