From: David Laight <david.laight.linux@gmail.com>
To: Vincent Mailhol <mailhol@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nsc@kernel.org>,
Nick Desaulniers <nick.desaulniers+lkml@gmail.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Chris Mason <clm@fb.com>, David Sterba <dsterba@suse.com>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
linux-kbuild@vger.kernel.org, linux-sparse@vger.kernel.org,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
dri-devel@lists.freedesktop.org, linux-btrfs@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2 4/4] minmax: remove useless cast in __is_nonneg()
Date: Sat, 20 Dec 2025 10:02:01 +0000 [thread overview]
Message-ID: <20251220100201.26d9b0db@pumpkin> (raw)
In-Reply-To: <20251219-remove_wtype-limits-v2-4-2e92b3f566c5@kernel.org>
On Fri, 19 Dec 2025 23:39:48 +0100
Vincent Mailhol <mailhol@kernel.org> wrote:
> The function like macro __is_nonneg() casts its argument to (long long)
> in an attempt to silence -Wtype-limits warnings on unsigned values.
nak.
The cast is needed for pointer types, not for -Wtype-limits.
which is why the '#if __SIZEOF_POINTER__ == __SIZEOF_LONG_LONG__'
test is there.
David
>
> But this workaround is incomplete as proven here:
>
> $ cat foo.c
> #include <linux/minmax.h>
>
> int foo(unsigned int a)
> {
> return __is_nonneg(a);
> }
> $ make CFLAGS_KERNEL="-Wtype-limits" foo.o
> CALL scripts/checksyscalls.sh
> DESCEND objtool
> INSTALL libsubcmd_headers
> CC foo.o
> foo.c: In function 'foo':
> ./include/linux/minmax.h:68:57: warning: comparison is always true due to limited range of data type [-Wtype-limits]
> 68 | #define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
> | ^~
> ./include/linux/compiler.h:350:50: note: in definition of macro 'statically_true'
> 350 | #define statically_true(x) (__builtin_constant_p(x) && (x))
> | ^
> foo.c:5:16: note: in expansion of macro '__is_nonneg'
> 5 | return __is_nonneg(a);
> | ^~~~~~~~~~~
> ./include/linux/minmax.h:68:57: warning: comparison is always true due to limited range of data type [-Wtype-limits]
> 68 | #define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
> | ^~
> ./include/linux/compiler.h:350:57: note: in definition of macro 'statically_true'
> 350 | #define statically_true(x) (__builtin_constant_p(x) && (x))
> | ^
> foo.c:5:16: note: in expansion of macro '__is_nonneg'
> 5 | return __is_nonneg(a);
> | ^~~~~~~~~~~
>
> And because -Wtype-limits is now globally disabled, such a workaround
> now becomes useless. Remove the __is_nonneg()'s cast and its related
> comment.
>
> Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
> ---
> Changelog:
>
> v1 -> v2: new patch
> ---
> include/linux/minmax.h | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/include/linux/minmax.h b/include/linux/minmax.h
> index a0158db54a04..3e2e3e539ba1 100644
> --- a/include/linux/minmax.h
> +++ b/include/linux/minmax.h
> @@ -52,9 +52,6 @@
> /*
> * Check whether a signed value is always non-negative.
> *
> - * A cast is needed to avoid any warnings from values that aren't signed
> - * integer types (in which case the result doesn't matter).
> - *
> * On 64-bit any integer or pointer type can safely be cast to 'long long'.
> * But on 32-bit we need to avoid warnings about casting pointers to integers
> * of different sizes without truncating 64-bit values so 'long' or 'long long'
> @@ -65,7 +62,7 @@
> * but they are handled by the !is_signed_type() case).
> */
> #if __SIZEOF_POINTER__ == __SIZEOF_LONG_LONG__
> -#define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
> +#define __is_nonneg(ux) statically_true((ux) >= 0)
> #else
> #define __is_nonneg(ux) statically_true( \
> (typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)))(ux) >= 0)
>
next prev parent reply other threads:[~2025-12-20 10:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-19 22:39 [PATCH v2 0/4] kbuild: remove gcc's -Wtype-limits Vincent Mailhol
2025-12-19 22:39 ` [PATCH v2 1/4] " Vincent Mailhol
2025-12-19 22:39 ` [PATCH v2 2/4] kbuild: cleanup local -Wno-type-limits exceptions Vincent Mailhol
2025-12-19 22:39 ` [PATCH v2 3/4] overflow: Remove is_non_negative() and is_negative() Vincent Mailhol
2025-12-19 22:39 ` [PATCH v2 4/4] minmax: remove useless cast in __is_nonneg() Vincent Mailhol
2025-12-20 10:02 ` David Laight [this message]
2025-12-20 10:53 ` Vincent Mailhol
2025-12-20 12:03 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251220100201.26d9b0db@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=airlied@gmail.com \
--cc=clm@fb.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=dsterba@suse.com \
--cc=gustavoars@kernel.org \
--cc=justinstitt@google.com \
--cc=kees@kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mailhol@kernel.org \
--cc=morbo@google.com \
--cc=mripard@kernel.org \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=nsc@kernel.org \
--cc=simona@ffwll.ch \
--cc=torvalds@linux-foundation.org \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox