* [PATCH v1 1/5] bitfield: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:56 [PATCH v1 0/5] Silence some clang -Wshorten-64-to-32 warnings Ian Rogers
@ 2025-04-03 16:56 ` Ian Rogers
2025-04-03 16:56 ` [PATCH v1 2/5] bitmap: " Ian Rogers
` (3 subsequent siblings)
4 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-04-03 16:56 UTC (permalink / raw)
To: Yury Norov, Rasmus Villemoes, Arnd Bergmann, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Ian Rogers,
Adrian Hunter, Thomas Gleixner, Jakub Kicinski, Jacob Keller,
linux-arch, linux-kernel, llvm
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.
Signed-off-by: Ian Rogers <irogers@google.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) \
--
2.49.0.504.g3bcea36a83-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH v1 2/5] bitmap: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:56 [PATCH v1 0/5] Silence some clang -Wshorten-64-to-32 warnings Ian Rogers
2025-04-03 16:56 ` [PATCH v1 1/5] bitfield: Silence a clang -Wshorten-64-to-32 warning Ian Rogers
@ 2025-04-03 16:56 ` Ian Rogers
2025-04-04 5:49 ` Arnd Bergmann
2025-04-03 16:57 ` [PATCH v1 3/5] bitops: " Ian Rogers
` (2 subsequent siblings)
4 siblings, 1 reply; 10+ messages in thread
From: Ian Rogers @ 2025-04-03 16:56 UTC (permalink / raw)
To: Yury Norov, Rasmus Villemoes, Arnd Bergmann, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Ian Rogers,
Adrian Hunter, Thomas Gleixner, Jakub Kicinski, Jacob Keller,
linux-arch, linux-kernel, llvm
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/linux/bitmap.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 595217b7a6e7..4395e0a618f4 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -442,7 +442,7 @@ static __always_inline
unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
{
if (small_const_nbits(nbits))
- return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
+ return (int)hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
return __bitmap_weight(src, nbits);
}
--
2.49.0.504.g3bcea36a83-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH v1 2/5] bitmap: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:56 ` [PATCH v1 2/5] bitmap: " Ian Rogers
@ 2025-04-04 5:49 ` Arnd Bergmann
2025-04-04 11:31 ` Ian Rogers
0 siblings, 1 reply; 10+ messages in thread
From: Arnd Bergmann @ 2025-04-04 5:49 UTC (permalink / raw)
To: Ian Rogers, Yury Norov, Rasmus Villemoes, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Adrian Hunter,
Thomas Gleixner, Jakub Kicinski, Jacob Keller, Linux-Arch,
linux-kernel, llvm
On Thu, Apr 3, 2025, at 18:56, Ian Rogers 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.
> unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
> {
> if (small_const_nbits(nbits))
> - return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
> + return (int)hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
> return __bitmap_weight(src, nbits);
> }
I don't understand this one. hweight_long() and bitmap_weight()
both return unsigned value, so why do you need to cast this to
a signed value to avoid a signedness problem?
hweight_long() should never return anything larger than 64ul
anyway, which is way outside of the range where it would get
sign-extended.
A more logical change to me would be to make hweight_long()
and bitmap_weight() have the same return type, either
'unsigned long' or 'unsigned int'.
Arnd
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH v1 2/5] bitmap: Silence a clang -Wshorten-64-to-32 warning
2025-04-04 5:49 ` Arnd Bergmann
@ 2025-04-04 11:31 ` Ian Rogers
0 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-04-04 11:31 UTC (permalink / raw)
To: Arnd Bergmann
Cc: Yury Norov, Rasmus Villemoes, Nathan Chancellor, Nick Desaulniers,
Bill Wendling, Justin Stitt, Adrian Hunter, Thomas Gleixner,
Jakub Kicinski, Jacob Keller, Linux-Arch, linux-kernel, llvm
On Thu, Apr 3, 2025 at 10:49 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Thu, Apr 3, 2025, at 18:56, Ian Rogers 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.
>
>
> > unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
> > {
> > if (small_const_nbits(nbits))
> > - return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
> > + return (int)hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
> > return __bitmap_weight(src, nbits);
> > }
>
> I don't understand this one. hweight_long() and bitmap_weight()
> both return unsigned value, so why do you need to cast this to
> a signed value to avoid a signedness problem?
>
> hweight_long() should never return anything larger than 64ul
> anyway, which is way outside of the range where it would get
> sign-extended.
>
> A more logical change to me would be to make hweight_long()
> and bitmap_weight() have the same return type, either
> 'unsigned long' or 'unsigned int'.
I don't disagree but the scope of that change would be much larger.
Yury has expressed concern over needing to update printf modifiers. I
was aiming for the minimal change that silences clang's
-Wshorten-64-to-32 warning.
Thanks,
Ian
> Arnd
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v1 3/5] bitops: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:56 [PATCH v1 0/5] Silence some clang -Wshorten-64-to-32 warnings Ian Rogers
2025-04-03 16:56 ` [PATCH v1 1/5] bitfield: Silence a clang -Wshorten-64-to-32 warning Ian Rogers
2025-04-03 16:56 ` [PATCH v1 2/5] bitmap: " Ian Rogers
@ 2025-04-03 16:57 ` Ian Rogers
2025-04-04 5:43 ` Arnd Bergmann
2025-04-03 16:57 ` [PATCH v1 4/5] math64: " Ian Rogers
2025-04-03 16:57 ` [PATCH v1 5/5] hash.h: " Ian Rogers
4 siblings, 1 reply; 10+ messages in thread
From: Ian Rogers @ 2025-04-03 16:57 UTC (permalink / raw)
To: Yury Norov, Rasmus Villemoes, Arnd Bergmann, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Ian Rogers,
Adrian Hunter, Thomas Gleixner, Jakub Kicinski, Jacob Keller,
linux-arch, linux-kernel, llvm
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/asm-generic/bitops/fls64.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/asm-generic/bitops/fls64.h b/include/asm-generic/bitops/fls64.h
index 866f2b2304ff..9ad3ff12f454 100644
--- a/include/asm-generic/bitops/fls64.h
+++ b/include/asm-generic/bitops/fls64.h
@@ -21,7 +21,7 @@ static __always_inline int fls64(__u64 x)
__u32 h = x >> 32;
if (h)
return fls(h) + 32;
- return fls(x);
+ return fls((__u32)x);
}
#elif BITS_PER_LONG == 64
static __always_inline int fls64(__u64 x)
--
2.49.0.504.g3bcea36a83-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v1 3/5] bitops: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:57 ` [PATCH v1 3/5] bitops: " Ian Rogers
@ 2025-04-04 5:43 ` Arnd Bergmann
2025-04-04 11:35 ` Ian Rogers
0 siblings, 1 reply; 10+ messages in thread
From: Arnd Bergmann @ 2025-04-04 5:43 UTC (permalink / raw)
To: Ian Rogers, Yury Norov, Rasmus Villemoes, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Adrian Hunter,
Thomas Gleixner, Jakub Kicinski, Jacob Keller, Linux-Arch,
linux-kernel, llvm
On Thu, Apr 3, 2025, at 18:57, Ian Rogers 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.
The fls64() function only seems to deal with unsigned values, so
I don't see how it would change the sign.
> diff --git a/include/asm-generic/bitops/fls64.h
> b/include/asm-generic/bitops/fls64.h
> index 866f2b2304ff..9ad3ff12f454 100644
> --- a/include/asm-generic/bitops/fls64.h
> +++ b/include/asm-generic/bitops/fls64.h
> @@ -21,7 +21,7 @@ static __always_inline int fls64(__u64 x)
> __u32 h = x >> 32;
> if (h)
> return fls(h) + 32;
> - return fls(x);
> + return fls((__u32)x);
> }
Maybe this would be clearer with an explicit upper_32_bits()/
lower_32_bits() instead of the cast and the shift?
Arnd
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH v1 3/5] bitops: Silence a clang -Wshorten-64-to-32 warning
2025-04-04 5:43 ` Arnd Bergmann
@ 2025-04-04 11:35 ` Ian Rogers
0 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-04-04 11:35 UTC (permalink / raw)
To: Arnd Bergmann
Cc: Yury Norov, Rasmus Villemoes, Nathan Chancellor, Nick Desaulniers,
Bill Wendling, Justin Stitt, Adrian Hunter, Thomas Gleixner,
Jakub Kicinski, Jacob Keller, Linux-Arch, linux-kernel, llvm
On Thu, Apr 3, 2025 at 10:43 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Thu, Apr 3, 2025, at 18:57, Ian Rogers 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.
>
> The fls64() function only seems to deal with unsigned values, so
> I don't see how it would change the sign.
You are right. I was trying to motivate in the message why building
with -Wshorten-64-to-32 is a good thing, and in this case we're making
an implicit cast explicit.
> > diff --git a/include/asm-generic/bitops/fls64.h
> > b/include/asm-generic/bitops/fls64.h
> > index 866f2b2304ff..9ad3ff12f454 100644
> > --- a/include/asm-generic/bitops/fls64.h
> > +++ b/include/asm-generic/bitops/fls64.h
> > @@ -21,7 +21,7 @@ static __always_inline int fls64(__u64 x)
> > __u32 h = x >> 32;
> > if (h)
> > return fls(h) + 32;
> > - return fls(x);
> > + return fls((__u32)x);
> > }
>
> Maybe this would be clearer with an explicit upper_32_bits()/
> lower_32_bits() instead of the cast and the shift?
It feels a little overkill to me, but if others prefer it then it is a
minor change.
Thanks,
Ian
> Arnd
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v1 4/5] math64: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:56 [PATCH v1 0/5] Silence some clang -Wshorten-64-to-32 warnings Ian Rogers
` (2 preceding siblings ...)
2025-04-03 16:57 ` [PATCH v1 3/5] bitops: " Ian Rogers
@ 2025-04-03 16:57 ` Ian Rogers
2025-04-03 16:57 ` [PATCH v1 5/5] hash.h: " Ian Rogers
4 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-04-03 16:57 UTC (permalink / raw)
To: Yury Norov, Rasmus Villemoes, Arnd Bergmann, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Ian Rogers,
Adrian Hunter, Thomas Gleixner, Jakub Kicinski, Jacob Keller,
linux-arch, linux-kernel, llvm
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/linux/math64.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/math64.h b/include/linux/math64.h
index 6aaccc1626ab..f32fcb2a2331 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -179,7 +179,7 @@ static __always_inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
#ifndef mul_u64_u32_shr
static __always_inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
{
- u32 ah = a >> 32, al = a;
+ u32 ah = a >> 32, al = (u32)a;
u64 ret;
ret = mul_u32_u32(al, mul) >> shift;
--
2.49.0.504.g3bcea36a83-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH v1 5/5] hash.h: Silence a clang -Wshorten-64-to-32 warning
2025-04-03 16:56 [PATCH v1 0/5] Silence some clang -Wshorten-64-to-32 warnings Ian Rogers
` (3 preceding siblings ...)
2025-04-03 16:57 ` [PATCH v1 4/5] math64: " Ian Rogers
@ 2025-04-03 16:57 ` Ian Rogers
4 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-04-03 16:57 UTC (permalink / raw)
To: Yury Norov, Rasmus Villemoes, Arnd Bergmann, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt, Ian Rogers,
Adrian Hunter, Thomas Gleixner, Jakub Kicinski, Jacob Keller,
linux-arch, linux-kernel, llvm
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/linux/hash.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/hash.h b/include/linux/hash.h
index 38edaa08f862..ecc8296cb397 100644
--- a/include/linux/hash.h
+++ b/include/linux/hash.h
@@ -75,7 +75,7 @@ static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
{
#if BITS_PER_LONG == 64
/* 64x64-bit multiply is efficient on all 64-bit processors */
- return val * GOLDEN_RATIO_64 >> (64 - bits);
+ return (u32)(val * GOLDEN_RATIO_64 >> (64 - bits));
#else
/* Hash 64 bits using only 32x32-bit multiply. */
return hash_32((u32)val ^ __hash_32(val >> 32), bits);
--
2.49.0.504.g3bcea36a83-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread