linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Nathan Chancellor <nathan@kernel.org>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Mike Rapoport <rppt@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"David Hildenbrand (Red Hat)" <david@kernel.org>,
	Ankit Khushwaha <ankitkhushwaha.linux@gmail.com>,
	Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>, Shuah Khan <shuah@kernel.org>,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] selftest/mm: fix pointer comparison in mremap_test
Date: Mon, 10 Nov 2025 14:43:49 -0700	[thread overview]
Message-ID: <20251110214349.GC302594@ax162> (raw)
In-Reply-To: <a7034e0c-a2ab-425a-8472-ef0a87a17681@suse.cz>

On Sun, Nov 09, 2025 at 08:11:09PM +0100, Vlastimil Babka wrote:
> >> > >>> Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment'
> >> > >>> triggers following warning:
> >> > >>>
> >> > >>> mremap_test.c:1035:31: warning: pointer comparison always evaluates to
> >> > >>> false [-Wtautological-compare]
> >> > >>>    1035 |                 if (addr + c.dest_alignment < addr) {
> >> > >>>         |                                             ^
> >> > >>>
> >> > >>> typecasting 'addr' to 'unsigned long long' to fix pointer comparison.
...
> >> I must say, applying this would be an unhappy life event.
> >> 
> >> 	if (void* + ulong < void*)
> >> 
> >> makes perfect sense in a world which permits void* arithmetic (ie,
> >> ours).  So what the heck is clang doing??
> 
> My (not very informed) guess would be something about undefined behavior
> because pointer arithmetic is strictly speaking only valid within an array,
> so void* + ulong is also still in the same array, and thus can't become
> smaller by an overflow, because overflow can't happen if we're still within
> the same valid array...

It is indeed due to undefined behavior but more so that without
-fwrapv-pointer (set via -fno-strict-overflow for the kernel build), the
addition of an unsigned index and a pointer cannot wrap. This warning is
a result of the following change in clang-20:

https://github.com/llvm/llvm-project/commit/6d34cfac53b993a6cdf3d6669e017eac3a2296c8
https://godbolt.org/z/hvMoPYb17

which I made sure respected the value of -fwrapv-pointer in

https://github.com/llvm/llvm-project/commit/f0dcf3240dffe3767c7f3a2e2da5b92ae9fd1bef

But it looks like the mm selftests do not build with
-fno-strict-overflow. Maybe it should?

> But I don't know if this strictness is only applied to the warning itself or
> to the actual compilation too (does it eliminate everything as dead code then?)

Yes, it would turn that

  if (addr + c.dest_alignment < addr) {

into just

  if (false) {

based on the above Godbolt link.

> >> If we do
> >> 
> >> 	void *addr2 = addr + c.dest_alignment;
> >> 	if (addr2 < addr)
> >> 		...
> >> 
> >> then which statement warns, and why?
> 
> As the answer was that nothing warns, I'd think it just isn't able to warn
> if it's no longer part of the same statement. Whether it also means it's not
> eliminated as dead code anymore, dunno.

Based on the above Godbolt link, it appears it will be optimized the
same way, just without the warning letting you know something is up.

Cheers,
Nathan


  reply	other threads:[~2025-11-10 21:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-06 10:49 [PATCH] selftest/mm: fix pointer comparison in mremap_test Ankit Khushwaha
2025-11-06 11:18 ` David Hildenbrand (Red Hat)
2025-11-06 12:02   ` Ankit Khushwaha
2025-11-06 14:26     ` Lorenzo Stoakes
2025-11-07  9:27     ` David Hildenbrand (Red Hat)
2025-11-08  0:08       ` Andrew Morton
2025-11-08 16:03         ` Ankit Khushwaha
2025-11-09  6:54         ` Mike Rapoport
2025-11-09 19:11           ` Vlastimil Babka
2025-11-10 21:43             ` Nathan Chancellor [this message]
2025-11-06 14:40 ` Lorenzo Stoakes

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=20251110214349.GC302594@ax162 \
    --to=nathan@kernel.org \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=ankitkhushwaha.linux@gmail.com \
    --cc=david@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@suse.cz \
    /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;
as well as URLs for NNTP newsgroup(s).